2016-10-23 22:48:26 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @version 2.0.0 - September 03, 2014
|
|
|
|
|
* @package Component Builder
|
|
|
|
|
* @author Llewellyn van de Merwe <http://www.vdm.io>
|
|
|
|
|
* @copyright Copyright (C) 2014. All Rights Reserved
|
|
|
|
|
* @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
// No direct access to this file
|
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
?>
|
|
|
|
|
###BOM###
|
|
|
|
|
|
|
|
|
|
// No direct access to this file
|
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dropbox updater component helper
|
|
|
|
|
*/
|
|
|
|
|
class Dropboxupdater
|
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
|
2016-10-23 22:48:26 +00:00
|
|
|
|
/**
|
2016-11-22 05:48:55 +00:00
|
|
|
|
* update flag (if false update will not happen)
|
2016-10-23 22:48:26 +00:00
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
protected $okay = true;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* update flag (if false update will not happen)
|
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
protected $data = null;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2016-11-22 05:48:55 +00:00
|
|
|
|
* the file Key
|
2016-10-23 22:48:26 +00:00
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
protected $fileKey;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2016-11-22 05:48:55 +00:00
|
|
|
|
* allow a forced update
|
2016-10-23 22:48:26 +00:00
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
protected $forceUpdate;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Todays date-time
|
|
|
|
|
**/
|
|
|
|
|
protected $today;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Next updat date-time
|
|
|
|
|
**/
|
|
|
|
|
protected $next;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* update method
|
|
|
|
|
**/
|
|
|
|
|
protected $updateMethod;
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-22 05:48:55 +00:00
|
|
|
|
* update targets
|
2016-10-23 22:48:26 +00:00
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
protected $updateTarget;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* info related to this update
|
|
|
|
|
**/
|
|
|
|
|
protected $updateInfo;
|
|
|
|
|
protected $infoFilePath;
|
|
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* Main dropbox class
|
|
|
|
|
**/
|
|
|
|
|
protected $dropbox;
|
|
|
|
|
|
2016-10-23 22:48:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* component parameters
|
|
|
|
|
**/
|
|
|
|
|
protected $app_params;
|
2016-11-22 05:48:55 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* the errors
|
|
|
|
|
**/
|
|
|
|
|
protected $errors = array();
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* everything we want done when initialized
|
|
|
|
|
**/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
// get params
|
|
|
|
|
$this->app_params = JComponentHelper::getParams('com_###component###');
|
2016-11-22 05:48:55 +00:00
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* get the logged errors array
|
|
|
|
|
**/
|
|
|
|
|
public function getErrors()
|
|
|
|
|
{
|
|
|
|
|
return $this->errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set the error to the log
|
|
|
|
|
**/
|
|
|
|
|
protected function setErrors($error)
|
|
|
|
|
{
|
|
|
|
|
if (###Component###Helper::checkString($error))
|
|
|
|
|
{
|
|
|
|
|
$this->errors[] = $error;
|
|
|
|
|
}
|
|
|
|
|
elseif (###Component###Helper::checkArray($error))
|
|
|
|
|
{
|
|
|
|
|
foreach($error as $log)
|
|
|
|
|
{
|
|
|
|
|
if (###Component###Helper::checkString($log))
|
|
|
|
|
{
|
|
|
|
|
$this->errors[] = $log;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-22 05:48:55 +00:00
|
|
|
|
* update method
|
2016-10-23 22:48:26 +00:00
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
public function update($id, $target, $type = false, $forceUpdate = false, $sleutel = null)
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
|
|
|
|
if ($type)
|
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// start fresh
|
2016-10-23 22:48:26 +00:00
|
|
|
|
$this->okay = true;
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->data = null;
|
|
|
|
|
// is this a forced run
|
|
|
|
|
$this->forceUpdate = $forceUpdate;
|
|
|
|
|
// the file key
|
|
|
|
|
$this->fileKey = ###Component###Helper::safeString($id.$target.$type);
|
|
|
|
|
// set the type of this listing
|
2016-10-23 22:48:26 +00:00
|
|
|
|
$this->type = $type;
|
|
|
|
|
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// get the external source data being updated
|
|
|
|
|
$this->setExternalSourceData($id);
|
|
|
|
|
|
|
|
|
|
// load the token if manualy set
|
|
|
|
|
if ($sleutel)
|
|
|
|
|
{
|
|
|
|
|
$this->setExternalSourceData($id, array('oauthtoken' => $sleutel));
|
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
// what update method is set
|
|
|
|
|
$this->setUpdateMethod();
|
|
|
|
|
|
|
|
|
|
// set the update links
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setUpdateTarget($target);
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
// set needed dates
|
|
|
|
|
if ($this->okay)
|
|
|
|
|
{
|
|
|
|
|
$this->setDates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get info data or set if not found
|
|
|
|
|
if ($this->okay)
|
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setUpdateInfoData();
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if update should run now
|
|
|
|
|
if ($this->okay)
|
|
|
|
|
{
|
|
|
|
|
$this->checkUpdateStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// before update save update info incase class is called again
|
|
|
|
|
if($this->okay)
|
|
|
|
|
{
|
|
|
|
|
if ($this->updateInfo->updatenow)
|
|
|
|
|
{
|
|
|
|
|
// turn update to active
|
|
|
|
|
$this->updateInfo->updatenow = false;
|
|
|
|
|
$this->updateInfo->updateactive = true;
|
|
|
|
|
// now save the date
|
|
|
|
|
$this->okay = $this->saveUpdateInfo();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->okay = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// run the update if all is okay
|
|
|
|
|
if ($this->okay)
|
|
|
|
|
{
|
|
|
|
|
// set the config
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setDetailsConfig();
|
2016-10-23 22:48:26 +00:00
|
|
|
|
// load the file
|
|
|
|
|
JLoader::import('dropbox', JPATH_COMPONENT_SITE.'/helpers');
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$build = 1;
|
|
|
|
|
if ('auto' == $this->type)
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$build = 2;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// load the dropbox class
|
|
|
|
|
$this->dropbox = new Dropbox(###Component###Helper::getModel('local_listing', JPATH_COMPONENT_ADMINISTRATOR), $build);
|
|
|
|
|
// okay now update
|
|
|
|
|
$this->okay = $this->doUpdate();
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// always reset if all okay
|
|
|
|
|
return $this->resetUpdate();
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setErrors('The update type is unknown.');
|
2016-10-23 22:48:26 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set the exsternal source data
|
|
|
|
|
*/
|
|
|
|
|
protected function setExternalSourceData($id, $data = array())
|
|
|
|
|
{
|
|
|
|
|
// get the data if not set
|
|
|
|
|
if (!$this->data || !###Component###Helper::checkObject($this->data))
|
|
|
|
|
{
|
|
|
|
|
// load model to get the data
|
|
|
|
|
$model = ###Component###Helper::getModel('external_source', JPATH_COMPONENT_ADMINISTRATOR);
|
|
|
|
|
$this->data = $model->getItem($id);
|
|
|
|
|
}
|
|
|
|
|
// if new data is set load it
|
|
|
|
|
if (###Component###Helper::checkArray($data))
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
|
{
|
|
|
|
|
$this->data->$key = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set update mehtod
|
|
|
|
|
**/
|
|
|
|
|
protected function setUpdateMethod()
|
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
if ($this->forceUpdate)
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
|
|
|
|
// this is a manual method
|
|
|
|
|
$this->updateMethod = 'manual';
|
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
elseif (2 == $this->data->update_method)
|
|
|
|
|
{
|
2016-10-23 22:48:26 +00:00
|
|
|
|
// this it an auto mehtod
|
|
|
|
|
$this->updateMethod = 'auto';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->okay = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-22 05:48:55 +00:00
|
|
|
|
* set update target
|
2016-10-23 22:48:26 +00:00
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
protected function setUpdateTarget($nr)
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// get target based on type and position
|
|
|
|
|
if ('full' == $this->data->permissiontype && $nr > 0)
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$position = $nr - 1;
|
|
|
|
|
if (1 == $this->data->dropboxoptions && ###Component###Helper::checkJson($this->data->sharedurl))
|
|
|
|
|
{
|
|
|
|
|
$targets = json_decode($this->data->sharedurl)->tsharedurl;
|
|
|
|
|
}
|
|
|
|
|
elseif (2 == $this->data->dropboxoptions && ###Component###Helper::checkJson($this->data->folder))
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$targets = json_decode($this->data->folder)->tfolder;
|
|
|
|
|
}
|
|
|
|
|
// check if we found any
|
|
|
|
|
if (!isset($targets[$position]) || !###Component###Helper::checkString($targets[$position]))
|
|
|
|
|
{
|
|
|
|
|
$this->setErrors('The target Shared-url or Folder is not set.');
|
|
|
|
|
$this->okay = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->updateTarget = $targets[$position];
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
else
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->updateTarget = '';
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set the configeration for exsternal source class
|
|
|
|
|
**/
|
|
|
|
|
protected function setDetailsConfig()
|
|
|
|
|
{
|
|
|
|
|
// reset config
|
|
|
|
|
$this->detailsConfig = array();
|
2016-12-22 21:32:13 +00:00
|
|
|
|
// the source ID
|
|
|
|
|
$this->detailsConfig['sourceID'] = $this->data->id;
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// get the legal files set
|
|
|
|
|
$this->detailsConfig['addTypes'] = $this->data->filetypes;
|
|
|
|
|
// set other config settings
|
|
|
|
|
$this->detailsConfig['dropboxOption'] = $this->data->dropboxoptions;
|
|
|
|
|
// set dropbox target
|
|
|
|
|
$this->detailsConfig['dropboxTarget'] = $this->updateTarget;
|
|
|
|
|
}
|
2016-10-23 22:48:26 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set next update time
|
|
|
|
|
**/
|
|
|
|
|
protected function setDates()
|
|
|
|
|
{
|
|
|
|
|
// set todays date/time
|
|
|
|
|
$this->today = time();
|
|
|
|
|
|
|
|
|
|
// set the next update date/time
|
|
|
|
|
if ('manual' == $this->updateMethod)
|
|
|
|
|
{
|
|
|
|
|
// set the date to two days ago to ensure the update runs now
|
|
|
|
|
$this->next = strtotime("-2 days");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// based on the auto time we will set the next update date/time
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$timer = $this->data->update_timer;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
if ($timer != 0)
|
|
|
|
|
{
|
|
|
|
|
// Get Next Update Time
|
|
|
|
|
$this->next = strtotime('+'.$timer.' minutes', $this->today);
|
|
|
|
|
}
|
|
|
|
|
// if timer is 0 we should not update
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setErrors('The timer is not setup correctly.');
|
2016-10-23 22:48:26 +00:00
|
|
|
|
$this->okay = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-22 05:48:55 +00:00
|
|
|
|
* set update info data
|
2016-10-23 22:48:26 +00:00
|
|
|
|
**/
|
2016-11-22 05:48:55 +00:00
|
|
|
|
protected function setUpdateInfoData()
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
|
|
|
|
// set the info file name
|
2016-12-22 21:32:13 +00:00
|
|
|
|
$fileName = md5($this->fileKey.'info');
|
2016-10-23 22:48:26 +00:00
|
|
|
|
// set file path
|
|
|
|
|
$this->infoFilePath = JPATH_COMPONENT_SITE.'/helpers/'.$fileName.'.json';
|
|
|
|
|
|
|
|
|
|
if (($json = @file_get_contents($this->infoFilePath)) !== FALSE)
|
|
|
|
|
{
|
|
|
|
|
$this->updateInfo = json_decode($json);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->updateInfo = new stdClass;
|
|
|
|
|
$this->updateInfo->nextupdate = 0;
|
|
|
|
|
$this->updateInfo->updateactive = false;
|
|
|
|
|
$this->updateInfo->updatenow = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* check update status
|
|
|
|
|
**/
|
|
|
|
|
protected function checkUpdateStatus()
|
|
|
|
|
{
|
|
|
|
|
// check if there is already an update running
|
|
|
|
|
if ($this->updateInfo->updateactive)
|
|
|
|
|
{
|
|
|
|
|
$this->okay = false;
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setErrors('There is an update already running.');
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
// check if the time has come to do the next update
|
|
|
|
|
elseif (('auto' == $this->updateMethod) && ($this->updateInfo->nextupdate > $this->today))
|
|
|
|
|
{
|
|
|
|
|
$this->okay = false;
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setErrors('It is not yet time to run this update.');
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->updateInfo->updatenow = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* save the update info
|
|
|
|
|
**/
|
|
|
|
|
protected function saveUpdateInfo()
|
|
|
|
|
{
|
|
|
|
|
return $this->saveJson(json_encode($this->updateInfo),$this->infoFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* update the local listing
|
|
|
|
|
**/
|
|
|
|
|
protected function doUpdate()
|
|
|
|
|
{
|
|
|
|
|
// we need more then the normal time to run this script 5 minutes at least.
|
2016-12-22 21:32:13 +00:00
|
|
|
|
ini_set('max_execution_time', $this->app_params->get('max_execution_time', 500));
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// get data of all the shared links of all target items
|
|
|
|
|
if (!$this->dropbox->getFiles($this->data->oauthtoken, $this->data->permissiontype, $this->detailsConfig))
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
$this->setErrors($this->dropbox->error_summary);
|
|
|
|
|
return false;
|
2018-01-23 22:05:57 +00:00
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// if this is a manual update, then revoke the token
|
|
|
|
|
if ($this->forceUpdate)
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-12-22 21:32:13 +00:00
|
|
|
|
$this->dropbox->revokeToken();
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
return true;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
2016-11-22 05:48:55 +00:00
|
|
|
|
|
2016-12-22 21:32:13 +00:00
|
|
|
|
public function resetUpdate()
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-12-22 21:32:13 +00:00
|
|
|
|
if ($this->okay || (isset($this->dropbox->forceReset) && $this->dropbox->forceReset))
|
2016-10-23 22:48:26 +00:00
|
|
|
|
{
|
2016-11-22 05:48:55 +00:00
|
|
|
|
// make sure the update reset
|
|
|
|
|
$this->updateInfo->nextupdate = $this->next;
|
|
|
|
|
$this->updateInfo->updateactive = false;
|
|
|
|
|
$this->updateInfo->updatenow = false;
|
2016-12-22 21:32:13 +00:00
|
|
|
|
// store final update
|
|
|
|
|
$this->saveUpdateInfo();
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
2016-12-22 21:32:13 +00:00
|
|
|
|
return $this->okay;
|
2016-10-23 22:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function saveJson($data,$filename)
|
|
|
|
|
{
|
|
|
|
|
if (###Component###Helper::checkString($data))
|
|
|
|
|
{
|
|
|
|
|
$fp = fopen($filename, 'w');
|
|
|
|
|
fwrite($fp, $data);
|
|
|
|
|
fclose($fp);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|