Joomla-Sermon-Distributor/site/helpers/dropboxupdater.php

452 lines
11 KiB
PHP
Raw Normal View History

2015-11-30 21:30:54 +00:00
<?php
/*--------------------------------------------------------------------------------------------------------| www.vdm.io |------/
__ __ _ _____ _ _ __ __ _ _ _
\ \ / / | | | __ \ | | | | | \/ | | | | | | |
\ \ / /_ _ ___| |_ | | | | _____ _____| | ___ _ __ _ __ ___ ___ _ __ | |_ | \ / | ___| |_| |__ ___ __| |
\ \/ / _` / __| __| | | | |/ _ \ \ / / _ \ |/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __| | |\/| |/ _ \ __| '_ \ / _ \ / _` |
\ / (_| \__ \ |_ | |__| | __/\ V / __/ | (_) | |_) | | | | | | __/ | | | |_ | | | | __/ |_| | | | (_) | (_| |
\/ \__,_|___/\__| |_____/ \___| \_/ \___|_|\___/| .__/|_| |_| |_|\___|_| |_|\__| |_| |_|\___|\__|_| |_|\___/ \__,_|
| |
|_|
/-------------------------------------------------------------------------------------------------------------------------------/
2015-11-30 21:30:54 +00:00
2017-02-17 20:21:37 +00:00
@version 1.4.1
@build 17th February, 2017
2015-11-30 21:30:54 +00:00
@created 22nd October, 2015
@package Sermon Distributor
@subpackage dropboxupdater.php
@author Llewellyn van der Merwe <https://www.vdm.io/>
@copyright Copyright (C) 2015. All Rights Reserved
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
A sermon distributor that links to Dropbox.
/-----------------------------------------------------------------------------------------------------------------------------*/
2015-11-30 21:30:54 +00:00
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Dropbox updater component helper
*/
class Dropboxupdater
{
2015-11-30 21:30:54 +00:00
/**
* update flag (if false update will not happen)
2015-11-30 21:30:54 +00:00
**/
protected $okay = true;
2015-11-30 21:30:54 +00:00
/**
* update flag (if false update will not happen)
**/
protected $data = null;
2015-11-30 21:30:54 +00:00
/**
* the file Key
2015-11-30 21:30:54 +00:00
**/
protected $fileKey;
2015-11-30 21:30:54 +00:00
/**
* allow a forced update
2015-11-30 21:30:54 +00:00
**/
protected $forceUpdate;
2015-11-30 21:30:54 +00:00
/**
* Todays date-time
**/
protected $today;
/**
* Next updat date-time
**/
protected $next;
/**
* update method
**/
protected $updateMethod;
/**
* update targets
2015-11-30 21:30:54 +00:00
**/
protected $updateTarget;
2015-11-30 21:30:54 +00:00
/**
* info related to this update
**/
protected $updateInfo;
protected $infoFilePath;
/**
* Main dropbox class
**/
protected $dropbox;
2015-11-30 21:30:54 +00:00
/**
* component parameters
**/
protected $app_params;
/**
* the errors
**/
protected $errors = array();
2015-11-30 21:30:54 +00:00
/**
* everything we want done when initialized
**/
public function __construct()
{
// get params
$this->app_params = JComponentHelper::getParams('com_sermondistributor');
}
2015-11-30 21:30:54 +00:00
/**
* get the logged errors array
**/
public function getErrors()
{
return $this->errors;
2015-11-30 21:30:54 +00:00
}
/**
* set the error to the log
2015-11-30 21:30:54 +00:00
**/
protected function setErrors($error)
{
if (SermondistributorHelper::checkString($error))
{
$this->errors[] = $error;
}
elseif (SermondistributorHelper::checkArray($error))
{
foreach($error as $log)
{
if (SermondistributorHelper::checkString($log))
{
$this->errors[] = $log;
}
}
}
}
/**
* update method
**/
public function update($id, $target, $type = false, $forceUpdate = false, $sleutel = null)
2015-11-30 21:30:54 +00:00
{
if ($type)
{
// start fresh
2015-11-30 21:30:54 +00:00
$this->okay = true;
$this->data = null;
// is this a forced run
$this->forceUpdate = $forceUpdate;
// the file key
$this->fileKey = SermondistributorHelper::safeString($id.$target.$type);
// set the type of this listing
2015-11-30 21:30:54 +00:00
$this->type = $type;
// get the external source data being updated
$this->setExternalSourceData($id);
// load the token if manualy set
if ($sleutel)
{
$this->setExternalSourceData($id, array('oauthtoken' => $sleutel));
}
2015-11-30 21:30:54 +00:00
// what update method is set
$this->setUpdateMethod();
// set the update links
$this->setUpdateTarget($target);
2015-11-30 21:30:54 +00:00
// set needed dates
if ($this->okay)
{
$this->setDates();
}
// get info data or set if not found
if ($this->okay)
{
$this->setUpdateInfoData();
2015-11-30 21:30:54 +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
$this->setDetailsConfig();
2015-11-30 21:30:54 +00:00
// load the file
JLoader::import('dropbox', JPATH_COMPONENT_SITE.'/helpers');
$build = 1;
if ('auto' == $this->type)
2015-11-30 21:30:54 +00:00
{
$build = 2;
2015-11-30 21:30:54 +00:00
}
// load the dropbox class
$this->dropbox = new Dropbox(SermondistributorHelper::getModel('local_listing', JPATH_COMPONENT_ADMINISTRATOR), $build);
// okay now update
$this->okay = $this->doUpdate();
2015-11-30 21:30:54 +00:00
}
// always reset if all okay
return $this->resetUpdate();
2015-11-30 21:30:54 +00:00
}
$this->setErrors('The update type is unknown.');
2015-11-30 21:30:54 +00:00
return false;
}
/**
* set the exsternal source data
*/
protected function setExternalSourceData($id, $data = array())
{
// get the data if not set
if (!$this->data || !SermondistributorHelper::checkObject($this->data))
{
// load model to get the data
$model = SermondistributorHelper::getModel('external_source', JPATH_COMPONENT_ADMINISTRATOR);
$this->data = $model->getItem($id);
}
// if new data is set load it
if (SermondistributorHelper::checkArray($data))
{
foreach ($data as $key => $value)
{
$this->data->$key = $value;
}
}
}
2015-11-30 21:30:54 +00:00
/**
* set update mehtod
**/
protected function setUpdateMethod()
{
if ($this->forceUpdate)
2015-11-30 21:30:54 +00:00
{
// this is a manual method
$this->updateMethod = 'manual';
}
elseif (2 == $this->data->update_method)
{
2015-11-30 21:30:54 +00:00
// this it an auto mehtod
$this->updateMethod = 'auto';
}
else
{
$this->okay = false;
}
}
/**
* set update target
2015-11-30 21:30:54 +00:00
**/
protected function setUpdateTarget($nr)
2015-11-30 21:30:54 +00:00
{
// get target based on type and position
if ('full' == $this->data->permissiontype && $nr > 0)
2015-11-30 21:30:54 +00:00
{
$position = $nr - 1;
if (1 == $this->data->dropboxoptions && SermondistributorHelper::checkJson($this->data->sharedurl))
2015-11-30 21:30:54 +00:00
{
$targets = json_decode($this->data->sharedurl)->tsharedurl;
}
elseif (2 == $this->data->dropboxoptions && SermondistributorHelper::checkJson($this->data->folder))
{
$targets = json_decode($this->data->folder)->tfolder;
}
// check if we found any
if (!isset($targets[$position]) || !SermondistributorHelper::checkString($targets[$position]))
{
$this->setErrors('The target Shared-url or Folder is not set.');
$this->okay = false;
}
else
{
$this->updateTarget = $targets[$position];
2015-11-30 21:30:54 +00:00
}
}
else
2015-11-30 21:30:54 +00:00
{
$this->updateTarget = '';
2015-11-30 21:30:54 +00:00
}
}
/**
* set the configeration for exsternal source class
**/
protected function setDetailsConfig()
{
// reset config
$this->detailsConfig = array();
// the source ID
$this->detailsConfig['sourceID'] = $this->data->id;
// 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;
}
2015-11-30 21:30:54 +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
$timer = $this->data->update_timer;
2015-11-30 21:30:54 +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
{
$this->setErrors('The timer is not setup correctly.');
2015-11-30 21:30:54 +00:00
$this->okay = false;
}
}
}
/**
* set update info data
2015-11-30 21:30:54 +00:00
**/
protected function setUpdateInfoData()
2015-11-30 21:30:54 +00:00
{
// set the info file name
$fileName = md5($this->fileKey.'info');
2015-11-30 21:30:54 +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;
$this->setErrors('There is an update already running.');
2015-11-30 21:30:54 +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;
$this->setErrors('It is not yet time to run this update.');
2015-11-30 21:30:54 +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.
ini_set('max_execution_time', $this->app_params->get('max_execution_time', 500));
// get data of all the shared links of all target items
if (!$this->dropbox->getFiles($this->data->oauthtoken, $this->data->permissiontype, $this->detailsConfig))
2015-11-30 21:30:54 +00:00
{
$this->setErrors($this->dropbox->error_summary);
return false;
}
// if this is a manual update, then revoke the token
if ($this->forceUpdate)
2015-11-30 21:30:54 +00:00
{
$this->dropbox->revokeToken();
2015-11-30 21:30:54 +00:00
}
return true;
2015-11-30 21:30:54 +00:00
}
public function resetUpdate()
2015-11-30 21:30:54 +00:00
{
if ($this->okay || (isset($this->dropbox->forceReset) && $this->dropbox->forceReset))
2015-11-30 21:30:54 +00:00
{
// make sure the update reset
$this->updateInfo->nextupdate = $this->next;
$this->updateInfo->updateactive = false;
$this->updateInfo->updatenow = false;
// store final update
$this->saveUpdateInfo();
2015-11-30 21:30:54 +00:00
}
return $this->okay;
2015-11-30 21:30:54 +00:00
}
protected function saveJson($data,$filename)
{
if (SermondistributorHelper::checkString($data))
{
$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);
return true;
}
return false;
}
}