<?php
/*
 *
 * @package		ARI Framework
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */  

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die;

AriKernel::import('Photobucket.Photobucket');

class AriSmartContentProviderPhotobuckerImages extends AriSmartContentProviderBase
{
	var $_cacheEnabled = false;
	var $_cachePeriod = 0;
	var $_cacheDir;
	var $_pb;
	var $_prepared = false;

	function __construct($params, $cacheDir = null, $ext = null)
	{
		if (is_null($cacheDir))
		{ 
			$cacheDir = JPATH_ROOT . DS . 'cache';
			if (!is_null($ext))
			{
				$extCacheDir = $cacheDir . DS . $ext;
			
				if (file_exists($extCacheDir) && is_dir($extCacheDir))
					$cacheDir = $extCacheDir;
			}
		}

		$this->_cacheDir = $cacheDir;

		$pbKey = $params['key'];
		$pbSecretKey = $params['secretKey']; 
		$cachePeriod = @intval($params['cachePeriod'], 10);

		if ($cachePeriod > 0 && $this->_cacheDir)
			$this->enableCache($this->_cacheDir, $cachePeriod);
		
		$pb = new PBAPI($pbKey, $pbSecretKey);
		$pb->setResponseParser('phpserialize');	

		$this->_pb =& $pb;
	}

	function enableCache($cacheDir, $cachePeriod)
	{
		$this->_cacheDir = $cacheDir;
		$this->_cachePeriod = $cachePeriod;
		$this->_cacheEnabled = true;
		
		global $_ARI_PB_CACHE;
		
		if (!isset($_ARI_PB_CACHE))
			$GLOBALS['_ARI_PB_CACHE'] = array();
	}
	
	function disableCache()
	{
		$this->_cacheEnabled = false;
	}
	
	function _prepare()
	{
		if ($this->_prepared)
			return ;
			
		$this->_pb->login('request')->post()->loadTokenFromResponse();
			
		$this->_prepared = true;
	}

	function _getCachedData($action, $options)
	{
		$data = null;
		
		if (!$this->_cacheEnabled || empty($action))
			return $data;
			
		$cacheKey = $this->_cacheKey($action, $options);
		$cacheFile = $this->_cacheDir . DS . $this->_cacheFileName($cacheKey);
		if (!@file_exists($cacheFile) || !@is_readable($cacheFile))
			return $data;
			
		if ($this->_cachePeriod < 1 || time() - filemtime($cacheFile) > $this->_cachePeriod)
		{
			@unlink($cacheFile);
			return $data;
		}
 
		global $_ARI_PB_CACHE;

		require_once $cacheFile;

		if (isset($_ARI_PB_CACHE[$cacheKey]))
			$data = $_ARI_PB_CACHE[$cacheKey];

		return $data;
	}
	
	function _cacheData($action, $options, $data)
	{
		$cacheKey = $this->_cacheKey($action, $options);
		$cacheFile = $this->_cacheFileName($cacheKey);

		$fh = fopen($this->_cacheDir . DS . $cacheFile, 'w');
		fwrite($fh, sprintf('<?php global $_ARI_PB_CACHE; $_ARI_PB_CACHE["%s"] = %s; ?>',
			$cacheKey,
			var_export($data, true)));
		fclose($fh);
	}

	function _cacheFileName($cacheKey)
	{
		return $cacheKey . '.php';
	}
	
	function _cacheKey($action, $options = null)
	{
		return md5($action . ($options ? implode(':', $options) : ''));
	}
	
	function getData($params)
	{
		$data = array();

		$src = $params['albumsource'];
		$data = $this->getUserAlbumData($src['user'], $src['album'], $params);

		return $data;
	}
	
	function getUserAlbumData($user, $album, $params)
	{
		$data = array();

		$defOptions = array(
			'recurse' => false,
			'view' => 'nested',
			'paginated' => false,
			'page' => 1,
			'perpage' => 20,
			'sortorder' => 'newest'
		);

		$src = $params['albumsource'];

		$options = AriParametersHelper::getUniqueOverrideParameters($defOptions, $src['opt']);
		$options['media'] = 'images';
		//$options['view'] = 'flat';
		
		if ($this->_cacheEnabled)
		{
			$cachedData = $this->_getCachedData('album', $options);
			print_r($cachedData);exit();
			if ($cachedData)
				return $cachedData;
		}

		$this->_prepare();

		$pb = $this->_pb;
		$res = $pb->album($user . '/' . $album, $options);
		if (empty($res))
			return $data;
			
		$res = $res->get();
		if (empty($res))
			return $data;
			
		$data = $res->getParsedResponse(true);
		if ($this->_cacheEnabled && !empty($data))
			$this->_cacheData('album', $options, $data);

		return $data;
	}	
}