<?php
/**
 * Kartina TV API calls library
 * !!!WARNING!!! any API call WILL ACTIVATE NEW A CODE.
 *
 */

const ECODE_QUERY_LIMIT_EXCEEDED = 31;

class KTV
{
	public static $SERVER = 'iptv-kartina.tv';
	public static $TECHSTUDIO  = 'subscribeasy.com';
}


const KTV_LOGIN_URL =
'http://%s/api/json/login?login=%s&pass=%s&settings=all';

const KTV_CHANNEL_LIST_URL =
'http://%s/api/json/channel_list?%s=%s';

const KTV_SET_SETTING_URL =
'http://%s/api/json/settings_set?%s=%s&var=%s&val=%s';

const KTV_CHANGE_PCODE_URL =
'http://%s/api/json/settings_set?%s=%s&var=pcode&old_code=%s&new_code=%s&confirm_code=%s';

const KTV_EPG_URL =
'http://%s/api/json/epg?%s=%s&cid=%s&day=%s';

const KTV_GET_URL_URL =
'http://ts://%s/api/json/get_url?%s=%s&cid=%s';

const KTV_VOD_LIST_URL_PREFIX =
'http://%s/api/json/vod_list?%s=%s&type=%s&page=%s&nums=%s';

const KTV_VOD_INFO_URL =
'http://%s/api/json/vod_info?%s=%s&id=%s';

const KTV_VOD_FAVLIST_URL =
'http://%s/api/json/vod_favlist?%s=%s';

const KTV_VOD_GENRES_URL =
'http://%s/api/json/vod_genres?%s=%s';

const KTV_VOD_FAVADD_URL=
'http://%s/api/json/vod_favadd?%s=%s&id=%s';

const KTV_VOD_FAVSUB_URL=
'http://%s/api/json/vod_favsub?%s=%s&id=%s';

const KTV_VOD_GET_URL_URL =
'http://mp4://%s/api/json/vod_geturl?%s=%s&fileid=%s';

const KTV_ARCHIVE_URL_PREFIX = 'http://lubiteli.ru/ktv2';

const KTV_ARCHIVE_ID = 'main';

/**
 * basic class witch content call to Kartina.TV API
 */
class KTV_API {

	private /*boolean*/ $logged_in;

	private /*void*/ $sid;

	private /*string*/ $sid_name;

	private /*array*/ $settings;

	private /*string*/ $server;

	private /*array*/ $account;

	public function __construct(/*string*/  $server){
		$this->server = $server;
	}

	public function  /*array*/ getAccount(){
		return  $this->account;
	}

	public function  /*array*/ get_settings(){
		return $this->settings;
	}

	private function /*void*/ curl(/*string*/ $url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//$result = curl_exec($ch);
		if(!$result = curl_exec($ch))
			throw new Exception ('Curl error: ' . curl_error($ch));
		curl_close($ch);
		return $result;
	}

	private function  api_call(/*string*/ $url){
	//	echo 'api_call';
		try {
			$doc = $this->curl($url);

		} catch(Exception $e){
			throw $e;
		}
		$doc =json_decode($doc);
		if (isset($doc->error) && $doc->error>0)
		 throw new Exception ("weeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
		return $doc;
	}


	/**
	 * Login function
	 * @param array data
	 */

	public function /*void*/ try_login(/*array*/ $data){
	//	echo 'try_login';
		if (!isset($data['username']))
			throw new Exception('username is not set');
		try{
			$this->api_login(
					$data['username'],
					$data['password']
			);
		}
		catch(Exception $e){
			throw $e;
		}

	}

	public function /*string*/ api_set_private_password(/*string*/ $current, /*string*/ $new_password, /*string*/ $conf_password){
		if(!isset($currnet, $new_password, $conf_password))
			throw new Exception('WTF');
		try {
			$this->api_call(
						sprintf(
							KTV_CHANGE_PCODE_URL,
							$this->server,
							$this->sid_name,
							$this->sid,
							$current,
							$new_pasword,
							$conf_password
						)
					);
		} catch (Exception $e){
			throw $e;
		}
	}

	public function /*string*/ api_set_setting(/*string*/ $setting,/*string*/ $value){
		//echo "API SET SETTINGS";
		$this->check_logged_in();
		try{
			$this->api_call(
						sprintf(
							KTV_SET_SETTING_URL,
							$this->server,
							$this->sid_name,
							$this->sid,
							$setting,
							$value
						)
					);
		}catch (Exception $e){
			throw $e;
		}
	}

	private function /*void*/ api_login($username, $password){
		//echo 'api login';
		try{
			$doc  = $this->api_call(
						sprintf(
							KTV_LOGIN_URL,
							$this->server,
							$username,
							$password
						)
					);
		} catch (Exception $e){
			throw $e;
		}
		//decode from  json resp;

		$this->logged_in = true;
		$this->sid  = $doc->sid;
		$this->sid_name  = $doc->sid_name;
		$this->settings  = $doc->settings;
		$this->account  = $doc->account;

	}

	private function check_logged_in (){
		if (!$this->logged_in)
			throw new Exception();
	}
}

?>
