<?php 
	/*
    $rtm = slack('rtm.start',array());
    print_r($rtm);*/
    
class slack{
	private $token = 'xoxb-105699646704-IBctJwYi3vxutOE0mdwyOil9';
	private $members = null;
	//xoxb-176818640771-4Urhgtz5xvzqoefRVNcpJCaC
	
	function task($task, $data) {
        //$token = 'xoxb-105699646704-IBctJwYi3vxutOE0mdwyOil9';
		$token = 'xoxb-176818640771-9EpOldDGsgGj5rQukLN227ah';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/'.$task.'?token='.$token.'&pretty=1');
        // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 

        $content = curl_exec($ch); 
        curl_close($ch); 

        $ar = json_decode($content);
        return $ar;
    }
	
	function get_members($users = array(), $all = false, $negative = false){
        if (isset($this->members)){
			$ar = $this->members;
		}else{
			$ar = $this->task('users.list', null);
			$this->members = $ar;
		}
		
		//echo '<pre>'.print_r($ar->members, true).'</pre>';

        if (count($users) > 0 || $all) {
            $members = array();
			if (isset($ar->members))
            foreach ($ar->members as $member) {
                if (!$all){
					if (in_array($member->profile->display_name, $users) || in_array($member->name, $users)) {
						$members[] = $member;
					}
				}else{
					if (!$member->deleted && !$member->is_restricted){
						if (!$negative){
							$members[] = $member;
						}else{
							if (!(in_array($member->profile->display_name, $users) || in_array($member->name, $users))) {
								$members[] = $member;
							}
						}
					}
				}
            }
        } else {
            $members = null;
        }
        return $members;
    }
	
	function get_members_by_email($email = ''){
		if (!$this->members) $this->members = $this->task('users.list', null);
		
        if ($email) {
            $members = array();
			if (count($this->members->members))
            foreach ($this->members->members as $member) {
				if (isset($member->profile->email)) 
                if ($member->profile->email == $email) {
                    $members[] = $member;
                }
            }
        } else {
            $members = null;
        }
        return $members;
    }
	
	function get_members_by_id($id = ''){
		if (!$this->members) $this->members = $this->task('users.list', null);
		
        if ($id) {
            $members = array();
            foreach ($this->members->members as $member) {
                if ($member->id == $id) {
                    $members[] = $member;
                }
            }
        } else {
            $members = null;
        }
        return $members;
    }
	
	function get_group($title = '', $include_users = false){
		$groupArr = $this->task('usergroups.list', array('include_disabled'=>'false', 'include_users'=>$include_users));
		 if ($title) {
			$groups = array();
			
			//echo '<pre>'.print_r($groupArr, true).'</pre>';
			
			if (count($groupArr->usergroups))
            foreach ($groupArr->usergroups as $group) {
                if ($group->handle == $title) {
                    $groups[] = $group;
                }
            }
		 } else {
           $groups = false;
        }
		
        return $groups;
	}
	
	function get_chanel($title = ''){
		$chanelsArr = $this->task('conversations.list', array('types'=>'public_channel,private_channel', 'exclude_archived'=>'true'));
		
		 if ($title) {
            $chanels = array();
			if (count($chanelsArr->channels))
            foreach ($chanelsArr->channels as $channel) {
                if ($channel->name == $title) {
                    $chanels[] = $channel;
                }
            }
        } else {
           $chanels = false;
        }
		
        return $chanels;
	}
	
	function get_pchanel($title=''){
		
		$chanelsArr = $this->task('conversations.list', array('types'=>'private_channel', 'exclude_archived'=>'true'));
		
		if ($title) {
            $chanels = array();
			if (count($chanelsArr->channels))
            foreach ($chanelsArr->channels as $channel) {
                if ($channel->name == $title) {
                    $chanels[] = $channel;
                }
            }
        } else {
           $chanels = false;
        }
		
        return $chanels;
	}
	
	function send($users, $mess, $attachment = null) {
		$result = array();
        foreach ($users as $member) {
            if ($member->deleted < 1) {
                $data = array(        
                    "channel" => $member->id,
                    "text" => $mess,			
                    "as_user" => true,
                );
				if ($attachment){
					 $data["attachments"] = json_encode(array($attachment));
				}
                $result[] = $this->task('chat.postMessage', $data);
            }
        }
		return $result;
    }
}
?>