<?php

$web = 'index.php';

if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
Phar::interceptFileFuncs();
set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
Phar::webPhar(null, $web);
include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
return;
}

if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
Extract_Phar::go(true);
$mimes = array(
'phps' => 2,
'c' => 'text/plain',
'cc' => 'text/plain',
'cpp' => 'text/plain',
'c++' => 'text/plain',
'dtd' => 'text/plain',
'h' => 'text/plain',
'log' => 'text/plain',
'rng' => 'text/plain',
'txt' => 'text/plain',
'xsd' => 'text/plain',
'php' => 1,
'inc' => 1,
'avi' => 'video/avi',
'bmp' => 'image/bmp',
'css' => 'text/css',
'gif' => 'image/gif',
'htm' => 'text/html',
'html' => 'text/html',
'htmls' => 'text/html',
'ico' => 'image/x-ico',
'jpe' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'js' => 'application/x-javascript',
'midi' => 'audio/midi',
'mid' => 'audio/midi',
'mod' => 'audio/mod',
'mov' => 'movie/quicktime',
'mp3' => 'audio/mp3',
'mpg' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'pdf' => 'application/pdf',
'png' => 'image/png',
'swf' => 'application/shockwave-flash',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'wav' => 'audio/wav',
'xbm' => 'image/xbm',
'xml' => 'text/xml',
);

header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

$basename = basename(__FILE__);
if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
chdir(Extract_Phar::$temp);
include $web;
return;
}
$pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
if (!$pt || $pt == '/') {
$pt = $web;
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
exit;
}
$a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
header('HTTP/1.0 404 Not Found');
echo "<html>\n <head>\n  <title>File Not Found<title>\n </head>\n <body>\n  <h1>404 - File ", $pt, " Not Found</h1>\n </body>\n</html>";
exit;
}
$b = pathinfo($a);
if (!isset($b['extension'])) {
header('Content-Type: text/plain');
header('Content-Length: ' . filesize($a));
readfile($a);
exit;
}
if (isset($mimes[$b['extension']])) {
if ($mimes[$b['extension']] === 1) {
include $a;
exit;
}
if ($mimes[$b['extension']] === 2) {
highlight_file($a);
exit;
}
header('Content-Type: ' .$mimes[$b['extension']]);
header('Content-Length: ' . filesize($a));
readfile($a);
exit;
}
}

class Extract_Phar
{
static $temp;
static $origdir;
const GZ = 0x1000;
const BZ2 = 0x2000;
const MASK = 0x3000;
const START = 'Fliqz.php';
const LEN = 6685;

static function go($return = false)
{
$fp = fopen(__FILE__, 'rb');
fseek($fp, self::LEN);
$L = unpack('V', $a = (binary)fread($fp, 4));
$m = (binary)'';

do {
$read = 8192;
if ($L[1] - strlen($m) < 8192) {
$read = $L[1] - strlen($m);
}
$last = (binary)fread($fp, $read);
$m .= $last;
} while (strlen($last) && strlen($m) < $L[1]);

if (strlen($m) < $L[1]) {
die('ERROR: manifest length read was "' .
strlen($m) .'" should be "' .
$L[1] . '"');
}

$info = self::_unpack($m);
$f = $info['c'];

if ($f & self::GZ) {
if (!function_exists('gzinflate')) {
die('Error: zlib extension is not enabled -' .
' gzinflate() function needed for zlib-compressed .phars');
}
}

if ($f & self::BZ2) {
if (!function_exists('bzdecompress')) {
die('Error: bzip2 extension is not enabled -' .
' bzdecompress() function needed for bz2-compressed .phars');
}
}

$temp = self::tmpdir();

if (!$temp || !is_writable($temp)) {
$sessionpath = session_save_path();
if (strpos ($sessionpath, ";") !== false)
$sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
die('Could not locate temporary directory to extract phar');
}
$temp = $sessionpath;
}

$temp .= '/pharextract/'.basename(__FILE__, '.phar');
self::$temp = $temp;
self::$origdir = getcwd();
@mkdir($temp, 0777, true);
$temp = realpath($temp);

if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
self::_removeTmpFiles($temp, getcwd());
@mkdir($temp, 0777, true);
@file_put_contents($temp . '/' . md5_file(__FILE__), '');

foreach ($info['m'] as $path => $file) {
$a = !file_exists(dirname($temp . '/' . $path));
@mkdir(dirname($temp . '/' . $path), 0777, true);
clearstatcache();

if ($path[strlen($path) - 1] == '/') {
@mkdir($temp . '/' . $path, 0777);
} else {
file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
@chmod($temp . '/' . $path, 0666);
}
}
}

chdir($temp);

if (!$return) {
include self::START;
}
}

static function tmpdir()
{
if (strpos(PHP_OS, 'WIN') !== false) {
if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
return $var;
}
if (is_dir('/temp') || mkdir('/temp')) {
return realpath('/temp');
}
return false;
}
if ($var = getenv('TMPDIR')) {
return $var;
}
return realpath('/tmp');
}

static function _unpack($m)
{
$info = unpack('V', substr($m, 0, 4));
 $l = unpack('V', substr($m, 10, 4));
$m = substr($m, 14 + $l[1]);
$s = unpack('V', substr($m, 0, 4));
$o = 0;
$start = 4 + $s[1];
$ret['c'] = 0;

for ($i = 0; $i < $info[1]; $i++) {
 $len = unpack('V', substr($m, $start, 4));
$start += 4;
 $savepath = substr($m, $start, $len[1]);
$start += $len[1];
   $ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
$ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
& 0xffffffff);
$ret['m'][$savepath][7] = $o;
$o += $ret['m'][$savepath][2];
$start += 24 + $ret['m'][$savepath][5];
$ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
}
return $ret;
}

static function extractFile($path, $entry, $fp)
{
$data = '';
$c = $entry[2];

while ($c) {
if ($c < 8192) {
$data .= @fread($fp, $c);
$c = 0;
} else {
$c -= 8192;
$data .= @fread($fp, 8192);
}
}

if ($entry[4] & self::GZ) {
$data = gzinflate($data);
} elseif ($entry[4] & self::BZ2) {
$data = bzdecompress($data);
}

if (strlen($data) != $entry[0]) {
die("Invalid internal .phar file (size error " . strlen($data) . " != " .
$stat[7] . ")");
}

if ($entry[3] != sprintf("%u", crc32((binary)$data) & 0xffffffff)) {
die("Invalid internal .phar file (checksum error)");
}

return $data;
}

static function _removeTmpFiles($temp, $origdir)
{
chdir($temp);

foreach (glob('*') as $f) {
if (file_exists($f)) {
is_dir($f) ? @rmdir($f) : @unlink($f);
if (file_exists($f) && is_dir($f)) {
self::_removeTmpFiles($f, getcwd());
}
}
}

@rmdir($temp);
clearstatcache();
chdir($origdir);
}
}

Extract_Phar::go();
__HALT_COMPILER(); ?>
º         
   fliqz.phar       functions/http_build_url.phpb  ï—¢Pb  {Œô¶         structures/FliqzContent.phpa	  ï—¢Pa	  ;É»7¶         structures/FliqzAsset.phpò  ï—¢Pò  ÌÄ• ¶         structures/FliqzMetrics.phpg  ï—¢Pg  Î";S¶         structures/FliqzThumbnail.php  ï—¢P  çTU¶          structures/FliqzSearchResult.php®  ï—¢P®  Æ#¶      	   Fliqz.php‰  ï—¢P‰  ƒÍæ¶         bootstrap.php    ï—¢P        ¶      <?php
/**
 * http_build_url.php
 * 
 * Provides a fallback for http_build_url if it is not available.
 *
 * @author Justin Martin
 * @author MediaDevils Inc.
 */
if (!function_exists('http_build_url'))
{
	define('HTTP_URL_REPLACE', 1);			// Replace every part of the first URL when there's one of the second URL
	define('HTTP_URL_JOIN_PATH', 2);		// Join relative paths
	define('HTTP_URL_JOIN_QUERY', 4);		// Join query strings
	define('HTTP_URL_STRIP_USER', 8);		// Strip any user authentication information
	define('HTTP_URL_STRIP_PASS', 16);		// Strip any password authentication information
	define('HTTP_URL_STRIP_AUTH', 32);		// Strip any authentication information
	define('HTTP_URL_STRIP_PORT', 64);		// Strip explicit port numbers
	define('HTTP_URL_STRIP_PATH', 128);		// Strip complete path
	define('HTTP_URL_STRIP_QUERY', 256);		// Strip query string
	define('HTTP_URL_STRIP_FRAGMENT', 512);		// Strip any fragments (#identifier)
	define('HTTP_URL_STRIP_ALL', 1024);		// Strip anything but scheme and host
	
	/**
	 * Builds a URL
	 * 
	 * The parts of the second URL will be merged into the first according to the flags argument. 
	 *
	 * @param mixed $url (Part(s) of) an URL in form of a string or associative array like parse_url() returns
	 * @param mixed $parts Same as the first argument
	 * @param int $flags A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default
	 * @param array &$new_url If set, it will be filled with the parts of the composed url like parse_url() would return
	 * @return string The constructed URL
	 */
	function http_build_url($url, $parts=array(), $flags=HTTP_URL_REPLACE, &$new_url=false)
	{
		$keys = array('user','pass','port','path','query','fragment');
		
		// HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs
		if ($flags & HTTP_URL_STRIP_ALL)
		{
			$flags |= HTTP_URL_STRIP_USER;
			$flags |= HTTP_URL_STRIP_PASS;
			$flags |= HTTP_URL_STRIP_PORT;
			$flags |= HTTP_URL_STRIP_PATH;
			$flags |= HTTP_URL_STRIP_QUERY;
			$flags |= HTTP_URL_STRIP_FRAGMENT;
		}
		// HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS
		else if ($flags & HTTP_URL_STRIP_AUTH)
		{
			$flags |= HTTP_URL_STRIP_USER;
			$flags |= HTTP_URL_STRIP_PASS;
		}
		
		// Parse the original URL
		$parse_url = parse_url($url);
		
		// Scheme and Host are always replaced
		if (isset($parts['scheme']))
			$parse_url['scheme'] = $parts['scheme'];
		if (isset($parts['host']))
			$parse_url['host'] = $parts['host'];
		
		// (If applicable) Replace the original URL with it's new parts
		if ($flags & HTTP_URL_REPLACE)
		{
			foreach ($keys as $key)
			{
				if (isset($parts[$key]))
					$parse_url[$key] = $parts[$key];
			}
		}
		else
		{
			// Join the original URL path with the new path
			if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH))
			{
				if (isset($parse_url['path']))
					$parse_url['path'] = rtrim(str_replace(basename($parse_url['path']), '', $parse_url['path']), '/') . '/' . ltrim($parts['path'], '/');
				else
					$parse_url['path'] = $parts['path'];
			}
			
			// Join the original query string with the new query string
			if (isset($parts['query']) && !empty($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY))
			{
				if (isset($parse_url['query']))
					$parse_url['query'] .= '&' . $parts['query'];
				else
					$parse_url['query'] = $parts['query'];
			}
		}
			
		// Strips all the applicable sections of the URL
		// Note: Scheme and Host are never stripped
		foreach ($keys as $key)
		{
			if ($flags & (int)constant('HTTP_URL_STRIP_' . strtoupper($key)))
				unset($parse_url[$key]);
		}
		
		
		$new_url = $parse_url;
		
		return 
			 ((isset($parse_url['scheme'])) ? $parse_url['scheme'] . '://' : '')
			.((isset($parse_url['user'])) ? $parse_url['user'] . ((isset($parse_url['pass'])) ? ':' . $parse_url['pass'] : '') .'@' : '')
			.((isset($parse_url['host'])) ? $parse_url['host'] : '')
			.((isset($parse_url['port'])) ? ':' . $parse_url['port'] : '')
			.((isset($parse_url['path'])) ? $parse_url['path'] : '')
			.((isset($parse_url['query'])) ? '?' . $parse_url['query'] : '')
			.((isset($parse_url['fragment'])) ? '#' . $parse_url['fragment'] : '')
		;
	}
}
<?php
/**
 * Fliqz.php
 *
 * @author Justin Martin
 * @author MediaDevils Inc.
 * 
 * @package fliqz
 * @subpackage search
 */

/**
 * Represents the "meat and potatoes" content of Fliqz assets
 *
 * @package fliqz
 * @subpackage search
 */
class FliqzContent {
	/**
	 * Asset state
	 * 
	 * @var string
	 */
	public $state;
	
	/**
	 * Asset approval
	 * 
	 * @var string
	 */
	public $approved;
	
	/**
	 * Asset medium, always "video"
	 * 
	 * @var string
	 */
	public $medium;
	
	/**
	 * Asset expression
	 * 
	 * @var string
	 */
	public $expression;
	
	/**
	 * Asset duration in seconds
	 * 
	 * @var int
	 */
	public $duration;
	
	/**
	 * Asset height in pixels
	 * 
	 * @var int
	 */
	public $height;
	
	/**
	 * Asset width in pixels
	 * 
	 * @var int
	 */
	public $width;
	
	/**
	 * Basic embed HTML
	 * 
	 * @var string
	 */
	public $embed;
	
	/**
	 * Comma-separated list of keywords
	 * 
	 * @var string
	 */
	public $keywords;
	
	/**
	 * Default thumbnail
	 * 
	 * @var FliqzThumbnail
	 */
	public $thumbnail;
	
	/**
	 * List of FliqzThumbnail objects
	 * 
	 * @var array
	 */
	public $thumbnails = array();
	
	/**
	 * Construct a new FliqzContent object
	 *
	 * @param DOMElement $asset The asset result XML
	 * @param DOMXPath $xpath A Fliqz-specific DOMXPath
	 * @return void
	 */
	public function __construct(DOMElement $asset, DOMXPath $xpath) {
		$content = $xpath->evaluate("media:content", $asset);
		if($content->length) {
			$content = $content->item(0);
			
			$this->state = $content->getAttributeNS(Fliqz::NS_FZAT, "state");
			$this->approved = $content->getAttributeNS(Fliqz::NS_FZAT, "approved");
			$this->medium = $content->getAttribute("medium");
			$this->expression = $content->getAttribute("expression");
			$this->duration = $content->getAttribute("duration");
			$this->height = $content->getAttribute("height");
			$this->width = $content->getAttribute("width");
			
			$embed = $xpath->evaluate("fzat:embed", $content);
			$this->embed = $embed->length?$embed->item(0)->textContent:null;
			
			$keywords = $xpath->evaluate("media:keywords", $content);
			$this->keywords = $keywords->length?$keywords->item(0)->textContent:null;
			
			foreach($xpath->evaluate("media:thumbnail", $content) as $thumbnail) {
				$tmp = new FliqzThumbnail($thumbnail);
				$this->thumbnails[] = $tmp;
				if($tmp->isDefault) $this->thumbnail = $tmp;
			}
		} else return null;
	}
}
<?php
/**
 * FliqzAsset.php
 *
 * @author Justin Martin
 * @author MediaDevils Inc.
 * 
 * @package fliqz
 * @subpackage search
 */

/**
 * A class representing a Fliqz asset result
 *
 * @package fliqz
 * @subpackage search
 */
class FliqzAsset {
	/**
	 * Asset title
	 * 
	 * @var string
	 */
	public $title;
	
	/**
	 * Asset description
	 * 
	 * @var string
	 */
	public $description;
	
	/**
	 * Asset GUID
	 * 
	 * @var string
	 */
	public $id;
	
	/**
	 * Asset link on Fliqz servers
	 * 
	 * @var string
	 */
	public $link;
	
	/**
	 * Asset publish date
	 * 
	 * @var string
	 */
	public $date;
	
	/**
	 * Asset default player ID
	 * 
	 * @var string
	 */
	public $playerID;
	
	/**
	 * Asset content data
	 * 
	 * @var FliqzContent
	 */
	public $content;
	
	/**
	 * Asset metrics data
	 * 
	 * @var FliqzMetrics
	 */
	public $metrics;
	
	/**
	 * Construct a new FliqzAsset object from its result XML
	 *
	 * @param DOMElement $asset Asset result XML
	 * @param DOMXPath $xpath Fliqz DOMXPath
	 * @return void
	 */
	public function __construct(DOMElement $asset, DOMXPath $xpath) {
		$this->title = (($title = $xpath->evaluate("title", $asset)) && $title->length)?$title->item(0)->textContent:'';
		$this->description = (($description = $xpath->evaluate("description", $asset)) && $description->length)?$description->item(0)->textContent:'';
		$this->id = (($id = $xpath->evaluate("guid", $asset)) && $id->length)?$id->item(0)->textContent:'';
		$this->link = (($link = $xpath->evaluate("link", $asset)) && $link->length)?$link->item(0)->textContent:'';
		$this->date = (($date = $xpath->evaluate("pubDate", $asset)) && $date->length)?$date->item(0)->textContent:'';

		parse_str(parse_url($this->link, PHP_URL_QUERY), $linkParams);
		if(array_key_exists("a", $linkParams)) $this->playerID = $linkParams["a"];
		
		$this->content = new FliqzContent($asset, $xpath);
		$this->metrics = new FliqzMetrics($asset, $xpath);
	}
	
	/**
	 * Construct a new FliqzAsset object from a search result
	 *
	 * @static
	 * @param string $feed An XML feed
	 * @return FliqzAsset
	 */
	public static function fromRSS($feed) {
		$search = new FliqzSearchResult($feed, true);
		if($search && is_array($search->assets) && !empty($search->assets))
			return $search->assets[0];
		else
			return false;
	}
}
<?php
/**
 * FliqzMetrics.php
 *
 * @author Justin Martin
 * @author MediaDevils Inc.
 * 
 * @package fliqz
 * @subpackage search
 */

/**
 * Represents the metrics data for a Fliqz asset
 *
 * @package fliqz
 * @subpackage search
 */
class FliqzMetrics {
	/**
	 * Number of plays
	 * 
	 * @var int
	 */
	public $plays;
	
	/**
	 * Number of ratings
	 * 
	 * @var int
	 */
	public $ratings;
	
	/**
	 * Number of votes
	 * 
	 * @var int
	 */
	public $votes;
	
	/**
	 * Number of shares
	 * 
	 * @var int
	 */
	public $shares;
	
	/**
	 * Construct a new FliqzMetrics object
	 *
	 * @param DOMElement $asset DOMElement of the asset
	 * @param DOMXPath $xpath Fliqz DOMXPath
	 * @return void
	 */
	public function __construct(DOMElement $asset, DOMXPath $xpath) {
		$metrics = $xpath->evaluate("media:content/fzat:metrics", $asset);
		if($metrics->length) {
			$metrics = $metrics->item(0);
			
			$this->plays = $metrics->getAttribute("plays");
			$this->ratings = $metrics->getAttribute("ratings");
			$this->votes = $metrics->getAttribute("votes");
			$this->shares = $metrics->getAttribute("shares");
		} else return null;
	}
}
<?php
/**
 * FliqzThumbnail.php
 *
 * @author Justin Martin
 * @author MediaDevils Inc.
 * 
 * @package fliqz
 * @subpackage search 
 */

/**
 * Represents a thumbnail image
 *
 * @package fliqz
 * @subpackage search
 */
class FliqzThumbnail {
	/**
	 * URL of the thumbnail image
	 * 
	 * @var string
	 */
	public $url;
	
	/**
	 * Thumbnail height in pixels
	 * 
	 * @var int
	 */
	public $height;
	
	/**
	 * Thumbnail width in pixels
	 * 
	 * @var int
	 */
	public $width;
	
	/**
	 * Display time of the thumbnail in seconds
	 * 
	 * @var int
	 */
	public $time;
	
	/**
	 * Is the default thumbnail for the asset?
	 * 
	 * @var bool
	 */
	public $isDefault;
	
	/**
	 * Construct a new FliqzThumbnail object
	 *
	 * @param DOMNode $node DOMNode of the thumbnail in the result XML
	 * @return void
	 */
	public function __construct(DOMNode $node) {
		$this->url = $node->getAttribute("url");
		$this->height = $node->getAttribute("height");
		$this->width = $node->getAttribute("width");
		$this->time = $node->getAttribute("time");
		
		$this->isDefault = $node->getAttributeNS(Fliqz::NS_FZAT, "isDefault");
	}
	
	/**
	 * Construct a thumbnail URL with a specified width and height
	 *
	 * @param int $width Width of the desired thumbnail in pixels
	 * @param int $height Height of the desired thumbnail in pixels
	 * @return string The thumbnail URL
	 */
	public function url($width = null, $height = null) {
		$params = array(
			"width" => $width,
			"height" => $height
		);
		
		$query = http_build_query($params);
		
		if(strlen($query))
			return http_build_url($this->url, array("query" => $query), HTTP_URL_JOIN_QUERY);
		else
			return $this->url;
	}
}
<?php
/**
 * FliqzSearchResult.php
 *
 * @author Justin Martin
 * @author MediaDevils Inc.
 * 
 * @package fliqz
 * @subpackage search
 */

/**
 * Represents a search result set
 *
 * @package fliqz
 * @subpackage search
 */
class FliqzSearchResult implements Iterator {
	/**
	 * Result XML
	 * 
	 * @var string
	 */
	protected $feed;
	
	/**
	 * Whether to retain the XML feed after processing
	 * 
	 * @var boolean
	 */
	public $retainFeed = false;
	
	/**
	 * List of constructed FliqzAsset objects
	 * 
	 * @var array
	 */
	public $assets;
	
	/**
	 * Link to the current page of search results
	 * 
	 * @var string
	 */
	public $link;
	
	/**
	 * Publication date of search results
	 * 
	 * @var string
	 */
	public $pubdate;
	
	/**
	 * Date when search results were last refreshed
	 * 
	 * @var string
	 */
	public $lastbuilddate;
	
	/**
	 * Number of pages in search results
	 * 
	 * @var int
	 */
	public $pages;
	
	/**
	 * Total number of items in search results
	 * 
	 * @var items
	 */
	public $items;
	
	/**
	 * Current page link
	 * 
	 * @var string
	 */
	protected $self;
	
	/**
	 * First page link
	 * 
	 * @var string
	 */
	protected $first;
	
	/**
	 * Last page link
	 * 
	 * @var string
	 */
	protected $last;
	
	/**
	 * Next page link
	 * 
	 * @var string
	 */
	protected $next;
	
	/**
	 * Previous page link
	 * 
	 * @var string
	 */
	protected $previous;
	
	/**
	 * Related results link
	 * 
	 * @var string
	 */
	protected $related;
	
	/**
	 * Construct a new FliqzSearchResult object from an XML feed
	 *
	 * @param string $feed XML feed of search results
	 * @param boolean $autoload Whether to automatically load the search results
	 * @return void
	 */
	public function __construct($feed, $autoload = false) {
		$this->self = $feed;
		$this->first = $this->self;
		if($autoload) $this->load();
	}
	
	/**
	 * Load the search results into objects from the feed
	 *
	 * @param string $feed XML feed of search results
	 * @return void
	 */
	public function load($feed = null) {
		$this->feed = new DOMDocument();
		@$this->feed->load($feed?:$this->self);
		
		$this->self = null;
		$this->first = null;
		$this->last = null;
		$this->next = null;
		$this->previous = null;
		$this->related = null;
		
		$this->assets = array();
		
		$xpath = Fliqz::produceXPath($this->feed);
		
		$link = $xpath->evaluate("channel/link");
		if($link->length) $this->link = $link->item(0)->textContent;
		
		$pubdate = $xpath->evaluate("channel/pubDate");
		if($pubdate->length) $this->pubdate = $pubdate->item(0)->textContent;
		
		$lastbuilddate = $xpath->evaluate("channel/lastBuildDate");
		if($lastbuilddate->length) $this->lastbuilddate = $lastbuilddate->item(0)->textContent;
	
		$pagingLinks = $xpath->evaluate("channel/a10:link");
		foreach($pagingLinks as $pagingLink) {
			$rel = $pagingLink->getAttribute("rel");
			$this->$rel = $pagingLink->getAttribute("href");
		}
		
		$pages = $xpath->evaluate("channel/fzat:pages");
		if($pages->length) $this->pages = (int) $pages->item(0)->textContent;
		
		$items = $xpath->evaluate("channel/fzat:itemTotal");
		if($items->length) $this->items = (int) $items->item(0)->textContent;
		
		$assets = $xpath->evaluate("channel/item");
		foreach($assets as $asset) $this->assets[] = new FliqzAsset($asset, $xpath);
		
		if(!$this->retainFeed) $this->feed = null;
	}
	
	/**
	 * Rewind to the first result page
	 *
	 * @return void
	 */
	public function rewind() {
		$this->self = $this->first;
	}
	
	/**
	 * Load the current result page
	 *
	 * @return FliqzSearchResult
	 */
	public function current() {
		$this->load($this->self);
		return $this;
	}
	
	/**
	 * Return a link to the current result page
	 *
	 * @return string
	 */
	public function key() {
		return $this->self;
	}
	
	/**
	 * Advanced to the new result page
	 *
	 * @return void
	 */
	public function next() {
		$this->self = $this->next;
	}
	
	/**
	 * Determine whether the current result page is valid
	 *
	 * @return boolean
	 */
	public function valid() {
		return !is_null($this->self);
	}
}
<?php
/**
 * Fliqz.php
 *
 * @author Justin Martin
 * @author MediaDevils Inc.
 * 
 * @package fliqz
 * @subpackage search
 */

require_once(__DIR__."/functions/http_build_url.php");
require_once(__DIR__."/structures/FliqzAsset.php");
require_once(__DIR__."/structures/FliqzSearchResult.php");
require_once(__DIR__."/structures/FliqzContent.php");
require_once(__DIR__."/structures/FliqzMetrics.php");
require_once(__DIR__."/structures/FliqzThumbnail.php");

/**
 * Interacts with the Fliqz RESTful Search Service API
 * 
 * @package fliqz
 * @subpackage search
 */
class Fliqz {
	const SERVICE_URL = "http://services.fliqz.com/assets/20090819/AssetSearch.svc/";
	
	const NS_XSI = "http://www.w3.org/2001/XMLSchema-instance";
	const NS_ATOM = "http://www.w3.org/2005/Atom";
	const NS_MRSS = "http://search.yahoo.com/mrss/";
	const NS_FZAT = "urn:fliqz:ast:s:20090819";
	
	/**
	 * Fliqz REST API Key
	 * 
	 * @var string
	 */
	public $apikey;
	
	/**
	 * Fliqz Uploader ID
	 * 
	 * @var string
	 */
	public $uploaderkey;
	
	const SERVICE_ASSET = 1;
	const SERVICE_RELATED = 2;
	const SERVICE_SEARCH = 3;
	
	/**
	 * Construct a new Fliqz object for interacting with the RESTful Fliqz API
	 *
	 * @param string $apikey Fliqz REST API Key
	 * @param string $uploaderkey Fliqz Uploader ID
	 * @return void
	 */
	public function __construct($apikey, $uploaderkey = null) {
		$this->apikey = $apikey;
		if(is_string($uploaderkey))
			$this->uploaderkey = $uploaderkey;
	}
	
	/**
	 * Obtain a FliqzAsset for the specified GUID
	 *
	 * @param string $id Asset GUID
	 * @return FliqzAsset The requested asset
	 */
	public function getAsset($id) {
		return FliqzAsset::fromRSS(
			$this->produceURL(self::SERVICE_ASSET, $id)
		);
	}
	
	/**
	 * Obtain a list of assets related to the specified GUID
	 *
	 * @param string $id Asset GUID
	 * @param int $page Page number to return
	 * @param int $pagesize Size of each page
	 * @return FliqzSearchResult The search results
	 */
	public function getRelatedAssets($id, $page = null, $pagesize = null) {
		$parameters = array();
		if(is_numeric($page)) $parameters["n"] = $page;
		if(is_numeric($pagesize)) $parameters["z"] = $pagesize;
		return new FliqzSearchResult(
			$this->produceURL(self::SERVICE_RELATED, $id, $parameters)
		);
	}
	
	/**
	 * Obtain a list of assets filtered by the specified parameters
	 *
	 * @param string $query The string to search for in the specified fields
	 * @param string $fields The fields to search within
	 * @param string $categories Filter results by specified categories
	 * @param string $order Sort by specified method
	 * @param int $page Page number to return
	 * @param int $pagesize Size of each page
	 * @return FliqzSearchResult The search results
	 */
	public function getAssets($query = null, $fields = null, $categories = null, $order = null, $page = 1, $pagesize = null) {
		$parameters = array();
		if(!is_null($query)) $parameters["q"] = $query;
		if(!is_null($fields)) $parameters["f"] = $fields;
		if(!is_null($order)) $parameters["o"] = $order;
		if(is_numeric($page)) $parameters["n"] = $page;
		if(is_numeric($pagesize)) $parameters["z"] = $pagesize;
		
		if(is_string($categories)) {
			$categories = explode(",", $categories);
			foreach($categories as &$category) $category = explode("=", $category);
		}
		if(is_array($categories)) foreach($categories as $category) {
			$parameters[$category[0]] = $category[1];
		}
		
		return new FliqzSearchResult($this->produceURL(self::SERVICE_SEARCH, null, $parameters));
	}
	
	/**
	 * Produce a URL for the request made to the specified service, with the specified parameters
	 *
	 * @param int $service One of the Fliqz::SERVICE_* constants
	 * @param string $asset An asset GUID for which to search, using the asset or related services
	 * @param array $parameters Parameters with which to search the related or search services
	 * @return string A URL for the specified request
	 */
	public function produceURL($service, $asset = null, $parameters = array()) {
		switch($service) {
			case self::SERVICE_ASSET:
				if(!is_string($asset)) {
					trigger_error("Fliqz::produceURL requires the second parameter be an asset ID for asset-related queries.");
					return false;
				}
				$url = self::SERVICE_URL."{$asset}/applications/{$this->apikey}/";
				return $url;
			case self::SERVICE_RELATED:
				if(!is_string($asset)) {
					trigger_error("Fliqz::produceURL requires the second parameter be an asset ID for asset-related queries.");
					return false;
				}
				$url = http_build_url(
					self::SERVICE_URL."{$asset}/applications/{$this->apikey}/related/",
					array(
						"query" => http_build_query($parameters)
					),
					HTTP_URL_JOIN_QUERY
				);
				return $url;
			case self::SERVICE_SEARCH:
				$url = http_build_url(
					self::SERVICE_URL."applications/{$this->apikey}/",
					array(
						"query" => http_build_query($parameters)
					),
					HTTP_URL_JOIN_QUERY
				);
				return $url;
		}
		return false;
	}
	
	/**
	 * Create an DOMXPath object for the specified document, registering the namespaces used by the Fliqz services
	 *
	 * @static
	 * @param DOMDocument $document The document to produce an XPath object for
	 * @return DOMXPath A DOMXPath for the provided document
	 */
	public static function produceXPath(DOMDocument $document) {
		$xpath = new DOMXPath($document);
		$xpath->registerNamespace("xsi", self::NS_XSI);
		$xpath->registerNamespace("media", self::NS_MRSS);
		$xpath->registerNamespace("a10", self::NS_ATOM);
		$xpath->registerNamespace("fzat", self::NS_FZAT);
		
		return $xpath;
	}
	
	/**
	 * Produce a FliqzUploader object given the configured Uploader ID
	 * 
	 * @uses Fliqz::$uploaderkey
	 * @return FliqzUploader A FliqzUploader object with which to produce the HTML for a form
	 */
	public function produceUploader() {
		if(!is_string($this->uploaderkey)) {
			trigger_error('Producing an uploader requires an upload application key.', E_USER_WARNING);
			return false;
		}
		return new FliqzUploader($this->uploaderkey);
	}
}
T—îçÛ&c`(1\o&Zi\‡Ðü*   GBMB