Code: Select all
<?php
/*
api.php - Controls entire backend of site functionality - DO NOT EDIT ANYTHING IN THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING.
Version : 5.0
Last Update : Tuesday, November 13 2007
*/
define(BROWSE_VID, 3);
define(BROWSE_IMG, 4);
define(BROWSE_TITLE, 5);
define(BROWSE_RUNTIME, 6);
define(BROWSE_POSTDATE, 7);
define(BROWSE_USERNAME, 8);
define(BROWSE_VIEWS, 9);
define(BROWSE_STAR1, 10);
define(BROWSE_STAR2, 11);
define(BROWSE_STAR3, 12);
define(BROWSE_STAR4, 13);
define(BROWSE_STAR5, 14);
define(BROWSE_RATINGS, 15);
define(SEARCH_VID, 1);
define(SEARCH_IMG, 3);
define(SEARCH_TITLE, 7);
define(SEARCH_DESC, 11);
define (VIDEO_VID, 1);
define (VIDEO_DESC, 2);
define (VIDEO_LINK, 3);
define (VIDEO_TITLE, 4);
define (VIDEO_TAGS, 5);
define (VIDEO_IMG, 6);
define (VIDEO_AUTHOR, 7);
define (VIDEO_CATEGORY, 8);
define (COMMENTS_DATE, 1);
define (COMMENTS_AUTHOR, 2);
define (COMMENTS_TITLE, 3);
define (COMMENTS_DESC, 4);
define (USER_AGE, 1);
define (USER_FULLNAME, 2);
define (USER_BOOKS, 3);
define (USER_COMPANY, 4);
define (USER_HOBBIES, 5);
define (USER_HOMETOWN, 6);
define (USER_LOCATION, 7);
define (USER_MOVIES, 8);
define (USER_OCCUPATION, 9);
define (USER_SCHOOL, 10);
define (USER_GENDER, 11);
define (USER_RELATIONSHIP, 12);
define (USER_MUSIC, 13);
function TextBetween($s1,$s2,$s){
$s1 = strtolower($s1);
$s2 = strtolower($s2);
$L1 = strlen($s1);
$scheck = strtolower($s);
if($L1>0)
{
$pos1 = strpos($scheck,$s1);
}
else { $pos1=0; }
if($pos1 !== false)
{
if($s2 == '') { return substr($s,$pos1+$L1); }
$pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
if($pos2!==false)
{
$data = substr($s,$pos1+$L1,$pos2);
$data = str_replace(chr(13),"", $data);
$data = str_replace(chr(10),"", $data);
return $data;
}
}
return '';
}
function encodeToUtf8($string) {
//return mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
//return mb_convert_encoding($string, "UTF-8");
return $string;
}
function timeDiff($time, $opt = array()) {
// The default values
$defOptions = array(
'to' => 0,
'parts' => 1,
'precision' => 'second',
'distance' => TRUE,
'separator' => ', '
);
$opt = array_merge($defOptions, $opt);
// Default to current time if no to point is given
(!$opt['to']) && ($opt['to'] = time());
// Init an empty string
$str = '';
// To or From computation
$diff = ($opt['to'] > $time) ? $opt['to']-$time : $time-$opt['to'];
// An array of label => periods of seconds;
$periods = array(
'decade' => 315569260,
'year' => 31556926,
'month' => 2629744,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
// Round to precision
if ($opt['precision'] != 'second')
$diff = round(($diff/$periods[$opt['precision']])) * $periods[$opt['precision']];
// Report the value is 'less than 1 ' precision period away
(0 == $diff) && ($str = 'less than 1 '.$opt['precision']);
// Loop over each period
foreach ($periods as $label => $value) {
// Stitch together the time difference string
(($x=floor($diff/$value))&&$opt['parts']--) && $str.=($str?$opt['separator']:'').($x.' '.$label.($x>1?'s':''));
// Stop processing if no more parts are going to be reported.
if ($opt['parts'] == 0 || $label == $opt['precision']) break;
// Get ready for the next pass
$diff -= $x*$value;
}
$opt['distance'] && $str.=($str&&$opt['to']>$time)?' ago':' away';
return $str;
}
CLASS YouTube {
var $_Results;
var $_ResultCount;
var $_xml;
var $parser;
var $pOut = array();
var $track = array();
var $tmpLevel = '';
var $tmpAttrLevel = array();
var $wOut = '';
function parse ( $src, $typeof = 'FILE', $encoding = 'UTF-8' ) {
$this->pOut = array();
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
// xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $encoding);
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'startHandler', 'endHandler');
xml_set_character_data_handler($this->parser, 'contentHandler');
if($typeof == NULL)
$data = $src;
elseif($typeof == 'FILE') {
$fop = fopen($src, 'r');
$data = null;
while(!feof($fop))
$data .= fread($fop, 1024);
fclose($fop);
}
elseif($typeof == 'CURL') {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $src);
curl_setopt($curl, CURLOPT_HEADER, 0);
$data = curl_exec($curl);
curl_close($curl);
}
else
return trigger_error('Xml parser need data.', E_USER_ERROR);
$parse = xml_parse($this->parser, $data);
if(!$parse)
return trigger_error('XML Error : %s at line %d.', E_USER_ERROR,
array(xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser)));
xml_parser_free($this->parser);
unset($data,
$this->track,
$this->tmpLevel);
return $this->pOut[0][key($this->pOut[0])];
}
function startHandler ( $parser, $tag, $attr ) {
$this->track[] = $tag;
end($this->track);
$this->tmpLevel = key($this->track);
if(isset($this->tmpAttrLevel[$this->tmpLevel]['attrLevel']))
$this->tmpAttrLevel[$this->tmpLevel]['attrLevel']++;
if(!isset($this->pOut[key($this->track)][$tag])) {
$this->pOut[key($this->track)][$tag] = '{{'.key($this->track).'}}';
if(!isset($this->tmpAttrLevel[$this->tmpLevel]['attrLevel']))
$this->tmpAttrLevel[$this->tmpLevel]['attrLevel'] = 0;
}
if(!empty($attr)) {
$this->tmpAttrLevel[$this->tmpLevel][] = $this->tmpAttrLevel[$this->tmpLevel]['attrLevel'];
end($this->tmpAttrLevel[$this->tmpLevel]);
if(!isset($this->pOut[key($this->track)][$tag.'-ATTR']))
$this->pOut[key($this->track)][$tag.'-ATTR'] = $attr;
else {
if(!prev($this->tmpAttrLevel[$this->tmpLevel])) {
$this->pOut[key($this->track)][$tag.'-ATTR'] = array(
current($this->tmpAttrLevel[$this->tmpLevel]) => $this->pOut[key($this->track)][$tag.'-ATTR'],
next($this->tmpAttrLevel[$this->tmpLevel]) => $attr
);
}
else
$this->pOut[key($this->track)][$tag.'-ATTR'][$this->tmpAttrLevel[$this->tmpLevel]['attrLevel']] = $attr;
}
}
return true;
}
function contentHandler ( $parser, $contentHandler ) {
if(!preg_match('#^\s*$#', $contentHandler)) {
if(is_string($this->pOut[key($this->track)][current($this->track)])) {
if(preg_match('#{{([0-9]+)}}#', $this->pOut[key($this->track)][current($this->track)]))
$this->pOut[key($this->track)][current($this->track)] = $contentHandler;
else {
$this->pOut[key($this->track)][current($this->track)] = array(
0 => $this->pOut[key($this->track)][current($this->track)],
1 => $contentHandler
);
}
}
else {
if(isset($this->pOut[key($this->track)][current($this->track)][0]))
$this->pOut[key($this->track)][current($this->track)][] = $contentHandler;
else
$this->pOut[key($this->track)][current($this->track)] = array(
0 => $this->pOut[key($this->track)][current($this->track)],
1 => $contentHandler
);
}
}
return true;
}
function endHandler ( $parser, $tag ) {
if(key($this->track) == $this->tmpLevel-1) {
$keyBack = array_keys($this->pOut[key($this->track)], '{{'.key($this->track).'}}');
$count = count($keyBack);
if($count != 0) {
$keyBack = $keyBack{$count-1};
$this->pOut[key($this->track)][$keyBack] = $this->pOut[key($this->track)+1];
}
else {
if(isset($this->pOut[key($this->track)][current($this->track)][0])) {
if(is_string($this->pOut[key($this->track)][current($this->track)]))
$this->pOut[key($this->track)][current($this->track)] = array(
0 => $this->pOut[key($this->track)][current($this->track)],
1 => $this->pOut[key($this->track)+1]
);
else
$this->pOut[key($this->track)][current($this->track)][] = $this->pOut[key($this->track)+1];
}
else
$this->pOut[key($this->track)][current($this->track)] = array(
0 => $this->pOut[key($this->track)][current($this->track)],
1 => $this->pOut[key($this->track)+1]
);
}
array_pop($this->pOut);
end($this->pOut);
}
$this->tmpLevel = key($this->track);
while(isset($this->tmpAttrLevel[$this->tmpLevel+1]))
array_pop($this->tmpAttrLevel);
array_pop($this->track);
end($this->track);
return true;
}
function Comments($videoid) {
$ch = curl_init("http://gdata.youtube.com/feeds/videos/".$videoid.'/comments?alt=rss');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($ch);
curl_close($ch);
$out = $this->parse($results, NULL);
for ($i = 0; $i < count($out['channel']['item']); $i++) {
$matches[$i][COMMENTS_DATE] = timeDiff(strtotime($out['channel']['item'][$i]['pubDate']));
$matches[$i][COMMENTS_AUTHOR] = $out['channel']['item'][$i]['author'];
$matches[$i][COMMENTS_TITLE] = $out['channel']['item'][$i]['title'];
$matches[$i][COMMENTS_DESC] = $out['channel']['item'][$i]['description'];
}
$this->_Results = $matches;
}
function Browse($select = "mp", $time = "t", $category = "0" , $page = "1") {
global $globalkeyword;
switch ($select) {
case "mp":
$parameters[] = 'orderby=viewCount';
break;
case "mr":
$parameters[] = 'orderby=updated';
break;
case "tr":
$parameters[] = 'orderby=rating';
break;
default:
$parameters[] = 'orderby=viewCount';
break;
}
/* $parameters[] = 'format=1';*/
$parameters[] = 'start-index='.((($page-1)*20)+1);
$parameters[] = 'max-results=21';
$parameters[] = 'alt=rss';
if ($globalkeyword) {
$parameters[] = "vq=".$globalkeyword;
}
$urlparams = implode("&",$parameters);
$ch = curl_init("http://gdata.youtube.com/feeds/videos?".$urlparams);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($ch);
curl_close($ch);
$out = $this->parse($results, NULL);
for ($i = 0; $i < 20; $i++) {
$linkpart = explode("=",$out['channel']['item'][$i]['link']);
$matches[1][$i] = true;
$matches[BROWSE_VID][$i] = $linkpart[1];
$matches[BROWSE_IMG][$i] = $out['channel']['item'][$i]['media:group']['media:thumbnail-ATTR']['url'];
if (!is_array($out['channel']['item'][$i]['title'])) {
$matches[BROWSE_TITLE][$i] = $out['channel']['item'][$i]['title'];
} else {
for ($u = 0; $u < count($out['channel']['item'][$i]['title']); $u++) {
$matches[BROWSE_TITLE][$i] .= $out['channel']['item'][$i]['title'][$u].' ';
}
}
$matches[BROWSE_RUNTIME][$i] = $out['channel']['item'][$i]['media:group']['media:content-ATTR']['duration'];
$matches[BROWSE_POSTDATE][$i] = timeDiff(strtotime($out['channel']['item'][$i]['pubDate']));
$matches[BROWSE_USERNAME][$i] = encodeToUTF8($out['channel']['item'][$i]['author']);
$matches[BROWSE_VIEWS][$i] = $out['channel']['item'][$i]['yt:statistics-ATTR']['viewCount'];
$ratavg = $out['channel']['item'][$i]['gd:rating-ATTR']['average'];
$avgpts = explode(".",$ratavg);
for ($k = 10; $k < $avgpts[0] + 10; $k++) {
$matches[$k][$i] = '/img/icn_star_full_11x11.gif';
}
if ($avgpts[1] >= 50) {
$matches[$k][$i] = '/img/icn_star_half_11x11.gif';
$k++;
}
$j = $k;
for ($k = $j; $k <= 14; $k++) {
$matches[$k][$i] = '/img/icn_star_empty_11x11.gif';
}
$matches[BROWSE_RATINGS][$i] = $out['channel']['item'][$i]['gd:rating-ATTR']['numRaters'];
}
$this->_Results = $matches;
$this->_ResultCount = $out['channel']['item']['openSearch:totalResults'];
}
function Video($videoid) {
$ch = curl_init("http://gdata.youtube.com/feeds/videos/".$videoid.'?alt=rss');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($ch);
curl_close($ch);
$out = $this->parse($results, NULL);
$linkpart = explode("=",$out['link']);
$matches[VIDEO_VID] = $linkpart[1];
$matches[VIDEO_LINK] = $out['link'];
if (!is_array($out['title'])) {
$matches[VIDEO_TITLE] = $out['title'];
} else {
for ($u = 0; $u < count($out['title']); $u++) {
$matches[VIDEO_TITLE] .= $out['title'][$u].' ';
}
}
$matches[VIDEO_TAGS] = $out['media:group']['media:keywords'];
$matches[VIDEO_IMG] = $out['media:group']['media:thumbnail-ATTR']['url'];
$matches[VIDEO_AUTHOR] = $out['author'];
$matches[VIDEO_CATEGORY] = $out['media:group']['media:category'];
if (!is_array($out['media:group']['media:description'])) {
$matches[VIDEO_DESC] = $out['media:group']['media:description'];
} else {
for ($u = 0; $u < count($out['media:group']['media:description']); $u++) {
if ($out['media:group']['media:description'][$u] == '&' || $out['media:group']['media:description'][$u] == '<' || $out['media:group']['media:description'][$u] == '>') {
$matches[VIDEO_DESC] .= $out['media:group']['media:description'][$u].$out['media:group']['media:description'][$u+1];
$u++;
} else {
$matches[VIDEO_DESC] .= (($u==0)?'':'<br />').$out['media:group']['media:description'][$u].' ';
}
}
}
$this->_Results = $matches;
}
function deArray($inar) {
if (is_array($inar)) {
$retar = '';
for ($i = 0; $i < $inar; $i++) {
if ($inar[$i] == '&' || $inar[$i] == '>' || $inar[$i] == '<') {
$retar .= $inar[$i].$inar[$i+1];
$i++;
} else {
$retar .= (($i == 0)?'':'<br />').$inar[$i];
}
}
return $retar;
} else {
return $inar;
}
}
function User($uname) {
$ch = curl_init("http://gdata.youtube.com/feeds/users/".rawurlencode($uname)."?alt=rss");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($ch);
curl_close($ch);
$out = $this->parse($results, NULL);
$matches[USER_FULLNAME] = $out['author'];
$matches[USER_AGE] = $out['yt:age'];
$matches[USER_BOOKS] = $this->deArray($out['yt:books']);
$matches[USER_COMPANY] = $this->deArray($out['yt:company']);
$matches[USER_HOBBIES] = $this->deArray($out['yt:hobbies']);
$matches[USER_HOMETOWN] = $this->deArray($out['yt:hometown']);
$matches[USER_LOCATION] = $this->deArray($out['yt:location']);
$matches[USER_MOVIES] = $this->deArray($out['yt:movies']);
$matches[USER_MUSIC] = $this->deArray($out['yt:music']);
$matches[USER_OCCUPATION] = $this->deArray($out['yt:occupation']);
$matches[USER_SCHOOL] = $this->deArray($out['yt:school']);
$matches[USER_GENDER] = (($out['yt:gender']=='m')?'Male':(($out['yt:gender']=='f')?'Female':'Unknown'));
$matches[USER_RELATIONSHIP] = $this->deArray($out['yt:relationship']);
$this->_Results = $matches;
}
function Search($query, $page, $orr = 0, $userid = 0, $catsearch = 0) {
if ($orr) {
$ch = curl_init("http://gdata.youtube.com/feeds/videos/".$orr."/related?alt=rss&start-index=".((($page-1)*20)+1)."&max-results=21&orderby=relevance");
} else {
if ($userid) {
$ch = curl_init("http://gdata.youtube.com/feeds/users/".rawurlencode($userid)."/uploads?alt=rss&start-index=".((($page-1)*20)+1)."&max-results=21&orderby=relevance");
} else {
if ($catsearch) {
$ch = curl_init("http://gdata.youtube.com/feeds/videos/-/".rawurlencode($query).'?alt=rss&start-index='.((($page-1)*20)+1).'&max-results=21&orderby=rating');
} else {
$tags = strtolower($query);
$tags = str_replace("%20", " ", $tags);
$tags = str_replace("+", " ", $tags);
//remove all characters except spaces, a-z and 0-9
$tags = ereg_replace("[!():'@#£$&]", "", $tags);
$tags = ereg_replace("%20+", " ", $tags);
$tags = ereg_replace(" +", "/", $tags);
$tags = urlencode($tags);
$tags = str_replace("%2F", "/", $tags);
$ch = curl_init("http://gdata.youtube.com/feeds/videos/-/".$tags.'?alt=rss&start-index='.((($page-1)*20)+1).'&max-results=21&orderby=relevance');
}
}
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($ch);
curl_close($ch);
$out = $this->parse($results, NULL);
$found = false;
$elementcount = 0;
while (!$found) {
if (isset($out['channel']['item'][$elementcount]['title'])) {
$linkpart = explode("=",$out['channel']['item'][$elementcount]['link']);
$matches[1][$elementcount] = true;
$matches[SEARCH_VID][$elementcount] = $linkpart[1];
$matches[SEARCH_IMG][$elementcount] = $out['channel']['item'][$elementcount]['media:group']['media:thumbnail-ATTR']['url'];
if (!is_array($out['channel']['item'][$elementcount]['title'])) {
$matches[SEARCH_TITLE][$elementcount] = $out['channel']['item'][$elementcount]['title'];
} else {
for ($u = 0; $u < count($out['channel']['item'][$elementcount]['title']); $u++) {
$matches[SEARCH_TITLE][$elementcount] .= $out['channel']['item'][$elementcount]['title'][$u].' ';
}
}
if (!is_array($out['channel']['item'][$elementcount]['media:group']['media:description'])) {
$matches[SEARCH_DESC][$elementcount] = $out['channel']['item'][$elementcount]['media:group']['media:description'];
} else {
for ($u = 0; $u < count($out['channel']['item'][$elementcount]['media:group']['media:description']); $u++) {
$matches[SEARCH_DESC][$elementcount] .= encodeToUTF8($out['channel']['item'][$elementcount]['media:group']['media:description'][$u].' ');
}
}
if (strlen($matches[SEARCH_DESC][$elementcount]) > 250) {
$matches[SEARCH_DESC][$elementcount] = substr($matches[SEARCH_DESC][$elementcount],0,250).'...';
}
$elementcount++;
} else {
$found = true;
}
}
$this->_Results = $matches;
$this->_ResultCount = $out['channel']['openSearch:totalResults'];
}
function ResultCount() {
return $this->_ResultCount;
}
function Results() {
return $this->_Results;
}
}
?>