<?php
//require_once('lib/php/feed/botr.php');

class anyclip_mcache1 extends Memcache {
		static private $instance = null;
		static private $port = 11211;
		static function mGet() {
			if (self::$instance == null) {
				self::$instance = new Memcache;
				self::$instance->addServer("192.168.30.51", self::$port);
			}
			return self::$instance;
		}
}

if(!function_exists('shuffle_assoc')) {
	function shuffle_assoc($list) { 
	  if (!is_array($list)) return $list; 

	  $keys = array_keys($list); 
	  shuffle($keys); 
	  $random = array(); 
	  foreach ($keys as $key) 
		$random[$key] = $list[$key]; 

	  return $random; 
	}
}

function getAnyclipVideo($search, $num = 5, $genres = null) {
	
	$videos = array('Thumbnail' => array(), 'Title' => array(), 'Description' => array(), 'ID' => array());
	$count = 0;

	$mkey = md5("coloVideos");
	$expire = 900;
	$videosArr = array();

	if(!$videosArr = anyclip_mcache1::mGet()->get($mkey)) {
		include_once("lib/php/include/db.php");
		$db = ConnectToRead();

		$sql = "select * from rep_videos";
		$res = mysql_query($sql, $db);
		while($row = mysql_fetch_assoc($res))
		{
			$title = trim($row['title']);
			$desc = trim($row['description']);
			$id = $row['id'];

			if($id < 10)
				$vidId = "00".$id;
			else if($id >= 10 && $id < 100)
				$vidId = "0".$id;
			else
				$vidId = $id;

			$thumb = "http://b36df47b3d.site.internapcdn.net/video/".$vidId.".jpeg";
			$videosArr[$id] = array($title, $desc, $thumb);
		}
		
		anyclip_mcache1::mGet()->set($mkey, $videosArr, 0, $expire);
	}

	$videosArr = shuffle_assoc($videosArr);
	foreach($videosArr as $vid=>$vArr)
	{
		if($count < $num)
		{
			if($vid < 10)
				$vidId = "00".$vid;
			else if($vid >= 10 && $vid < 100)
				$vidId = "0".$vid;
			else
				$vidId = $vid;

			$videos['Thumbnail'][$count] = $vArr[2];
			$videos['Title'][$count] =  $vArr[0];
			$videos['Description'][$count] = $vArr[1];
			$videos['ID'][$count] = $vidId;

			$count++;
		}
		else
			break;
	}
	
	return $videos;
	
	$_clientid = '462DE31E';
	$_appkey = 'C6092F30-5147-4E48-B3A5-A53505570981';
	
	$gid = '';
	if(is_array($genres)) {
		asort($genres);
		foreach($genres as $genre) {
			$gid .= '&gid='.$genre;
		}
	}
	
	$feed = new DomDocument();
	$params = '/api/search/?cf=1&cff=707272&cid='.$_clientid.'&efilter=64'.$gid.'&maxResults='.$num.'&q='.rawurlencode($search).'&scenesFirst=true&startIndex=0&tf=512';
	$sig = sha1('/api/search/?appKey='.$_appkey.'&cf=1&cff=707272&cid='.$_clientid.'&efilter=64'.$gid.'&maxResults='.$num.'&q='.$search.'&scenesFirst=true&startIndex=0&tf=512');
	$feedUrl = 'http://apis.anyclip.com'.$params.'&sig='.$sig;
	$videos = array('Thumbnail' => array(), 'Title' => array(), 'Description' => array(), 'ID' => array());

	$data = ac_curl_get_contents(array('url' => $feedUrl, 'timeout' => 3));
	
	if($data) {
		$feed->loadXML($data);
		####### PARSING XML ####
		$i = 0;
		foreach($feed->getElementsByTagName('result') as $video) {
			$thumb = $video->getElementsByTagName('thumbnail')->item(0)->firstChild->nodeValue;
			$title = $video->getElementsByTagName('title')->item(0);
			$title = $title->getElementsByTagName('name')->item(1)->firstChild->nodeValue;
			if($title == null) {
				$title = $video->getElementsByTagName('title')->item(0);
				$title = $title->getElementsByTagName('name')->item(0)->firstChild->nodeValue;
			}
			$desc = $video->getElementsByTagName('name')->item(0)->firstChild->nodeValue;
			$id = $video->getElementsByTagName('code')->item(0)->firstChild->nodeValue;
			
			$videos['Thumbnail'][$i] = $thumb;
			$videos['Title'][$i] = $title;
			$videos['Description'][$i] = $desc;
			$videos['ID'][$i] = $id;

			$i++;
		}
			
		return array_slice($videos, 0, $num);
	} else {
		return array();
	}
}

function getGenres() {
	/*
	* Action - 31
	* Adventure - 33
	* Comedy - 37
	*   Animation - 34
	* Documentary - 40
	* Drama - 41
	*   Adult - 32
	*   Biography - 35
	*   History - 81
	* Family - 42
	*   Fantasy - 43
	* Horror - 47
	* Musical - 50
	* Romance - 54
	* Sci-Fi - 55
	* Thriller - 58
	*   Crime - 38
	*   Film Noir - 44
	*   Mystery - 51
	* War - 59
	* Western - 60
	*/
	$_clientid = '462DE31E';
	$_appkey = 'C6092F30-5147-4E48-B3A5-A53505570981';
	
	$feed = new DomDocument();
	$params = '/api/genres/list?cid='.$_clientid;
	$sig = sha1('/api/genres/list?appKey='.$_appkey.'&cid='.$_clientid);
	$feedUrl = 'http://apis.anyclip.com'.$params.'&sig='.$sig;
	
	$data = ac_curl_get_contents(array('url' => $feedUrl, 'timeout' => 3));
	
	if($data) {
		$feed->loadXML($data);
		
		return $feed->saveHTML();
	}
}

function ac_curl_get_contents($CurlData)
{
	##### FETCH INPUT #####
	if (is_array($CurlData))
		$URL = $CurlData['url'];
		else
			$URL = $CurlData;

		if ($CurlData['timeout'])
			$Timeout = $CurlData['timeout'];
		else
			$Timeout = 5;
			
		###### INIT ############
		$ch = curl_init($URL);
		###### CURL & OPTION #####
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_TIMEOUT, $Timeout);
		if($CurlData['login'])
			curl_setopt($ch, CURLOPT_USERPWD, $CurlData['login']);
		if($CurlData['ua'])
			curl_setopt($ch, CURLOPT_USERAGENT, $CurlData['ua']);
		if($CurlData['Authorization'])
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CurlData['Authorization']));
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
}