PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_joomlapack/models/cronman.php

https://github.com/Ratmir15/Joomla---formula-of-success
PHP | 408 lines | 267 code | 35 blank | 106 comment | 39 complexity | ec22df8c8c20b916172b4d39a33d24aa MD5 | raw file
  1. <?php
  2. /**
  3. * @package JoomlaPack
  4. * @copyright Copyright (c)2006-2009 JoomlaPack Developers
  5. * @license GNU General Public License version 2, or later
  6. * @version $id$
  7. * @since 2.3
  8. *
  9. * CRON Script Manager Model
  10. */
  11. // Protect from direct access
  12. defined('_JEXEC') or die('Restricted Access');
  13. jimport('joomla.application.component.model');
  14. class JoomlapackModelCronman extends JModel
  15. {
  16. /** @var int CRON Script ID */
  17. var $_id;
  18. /** @var stdClass CRON script configuration object */
  19. var $_cron;
  20. /** @var JPagination The pagination object */
  21. var $_pagination;
  22. /** @var int Total CRON definition ID's */
  23. var $_total;
  24. /**
  25. * @var string Where the CRON scripts are stored. Maybe in the future I can make this a
  26. * user defined parameter?
  27. */
  28. var $scriptdirectory;
  29. /**
  30. * Public contructor
  31. * @return JoomlapackModelCronman
  32. */
  33. function __construct($config = array())
  34. {
  35. // Call JModel constructor
  36. parent::__construct($config);
  37. // Assign default script storage directory
  38. $this->scriptdirectory = JPATH_COMPONENT_ADMINISTRATOR.DS.'assets'.DS.'scripts';
  39. // If there is a 'scriptdirectory' element in the $config array, override the default
  40. if(array_key_exists('scriptdirectory', $config))
  41. {
  42. $this->scriptdirectory = $config['scriptdirectory'];
  43. }
  44. }
  45. /**
  46. * Sets a CRON ID and resets internal data
  47. *
  48. * @param int $id Profile ID
  49. */
  50. function setId($id=0)
  51. {
  52. $this->_id = $id;
  53. $this->_cron = null;
  54. }
  55. /**
  56. * Returns the currently set CRON ID
  57. * @return int
  58. */
  59. function getId()
  60. {
  61. return $this->_id;
  62. }
  63. /**
  64. * Returns a list of the ID's used by CRON files (the numeric part of, e.g. cron1234.php)
  65. * @return array Array of ID's, or empty array if no cron configuration file is found
  66. */
  67. function getCronIDs()
  68. {
  69. $ret = array();
  70. // Find CRON configuration scripts, like cron1.php, cron2.php, cron1520.php, etc.
  71. jimport('joomla.filesystem.folder');
  72. $files = JFolder::files( $this->scriptdirectory, '^cron\d+\.php$' );
  73. if( count($files) <= 0 )
  74. {
  75. return $ret;
  76. }
  77. // Loop all files
  78. foreach($files as $file)
  79. {
  80. if(substr($file,0,4) != 'cron') continue;
  81. $file = rtrim($file,'.php'); // Remove extension
  82. $file = ltrim($file,'cron'); // Remove prefix
  83. if(empty($file)) continue;
  84. $ret[] = (int)$file; // Convert to integer
  85. }
  86. return $ret;
  87. }
  88. /**
  89. * Loads a CRON configuration file, isolates the $config array and parses it
  90. * @return object|null An object with configuration data, or null if parsing failed
  91. * @param int $id The CRON configuration id, e.g. 1 for cron1.php.
  92. */
  93. function getConfiguration($id)
  94. {
  95. $filename = $this->scriptdirectory.DS."cron$id.php";
  96. jimport('joomla.filesystem.file');
  97. if(!file_exists($filename))
  98. {
  99. return null;
  100. }
  101. else
  102. {
  103. $ret = new stdClass;
  104. // Load in an array
  105. $lines = @file($filename);
  106. if($lines === false) return null; // In case it's unreadable
  107. $flag = false; // When we find the start of the array we will start processing
  108. $cache = ''; // The cached PHP
  109. foreach($lines as $line)
  110. {
  111. $line = rtrim(trim($line),"\n");
  112. // Search for the start of the array if we haven't found it yet
  113. if( ($flag == false) && (strpos($line, '$config = array(') !== false) ) $flag = true;
  114. // If we are processing the array...
  115. if($flag)
  116. {
  117. // ... but if eval() is not available, try the hard way around...
  118. if( strpos($line, '=>') !== false ) // We must be inside an array item
  119. {
  120. list($key, $value) = explode('=>', $line);
  121. $key = trim($key); // Remove whitespace from key
  122. $key = trim($key, substr($key,0,1)); // Remove surrounding quotes from key
  123. $value = rtrim(trim($value), ','); // Remove whitespace and trailing comma from the value
  124. // If there are no quotes surrounding the value, try to parse as boolean, else it's a number (raw data)
  125. if( (substr($value,0,1) == '"') || (substr($value,0,1) == "'") )
  126. {
  127. // Remove quotes
  128. $value = trim($value, substr($value,0,1));
  129. }
  130. else
  131. {
  132. // If there are no quotes, check if it's a boolean, otherwise leave it alone
  133. if($value == 'false')
  134. {
  135. $value = false;
  136. }
  137. elseif($value == 'true')
  138. {
  139. $value = true;
  140. }
  141. }
  142. // Add the key / value pair to the object
  143. $ret->$key = $value;
  144. }
  145. }
  146. // Search for the end of the array if we are still processing the array elements
  147. if( $flag && ( substr($line,-2) == ');' ) ) $flag = false;
  148. }
  149. $ret->id = $id;
  150. }
  151. return $ret;
  152. }
  153. /**
  154. * Returns a list of CRON script definitions objects.
  155. * @param bool $overrideLimits If true, overrides view limits and returns everything
  156. * @return array
  157. */
  158. function &getCronDefinitions($overrideLimits = false)
  159. {
  160. // Get all CRON script definitions
  161. $ret = array();
  162. $ids = $this->getCronIDs();
  163. if(!is_null($ids))
  164. {
  165. foreach($ids as $id)
  166. {
  167. $object = $this->getConfiguration($id);
  168. if( count(get_object_vars($object)) > 0 )
  169. {
  170. // A configuration was read, add it
  171. $ret[] = $object;
  172. }
  173. }
  174. }
  175. // If limits override is not turned on, process limits
  176. if( !$overrideLimits )
  177. {
  178. // Get limits
  179. $limitstart = $this->getState('limitstart');
  180. $limit = $this->getState('limit');
  181. if($limitstart > count($ret))
  182. {
  183. // Return empty array if asked for limitstart beyond end of table...
  184. $ret = array();
  185. }
  186. else
  187. {
  188. // ...otherwise, slice the array
  189. if( !is_null($limitstart) && !is_null($limit) )
  190. $ret = array_slice($ret, $limitstart, $limit);
  191. }
  192. }
  193. return $ret;
  194. }
  195. /**
  196. * Generates and returns the contents of a CRON helper PHP file, based on the
  197. * information of $this->_cron
  198. * @return
  199. */
  200. function _makeCRONScript()
  201. {
  202. // Creates a CRON script based on the information in $this->_cron
  203. $data = "<?php\n\$config = array(";
  204. foreach( get_object_vars($this->_cron) as $key => $value )
  205. {
  206. $data .= "\n\t'$key' => ";
  207. if(is_bool($value))
  208. {
  209. // Boolean values get converted to textual representation
  210. $value = ($value === true) ? 'true' : 'false';
  211. }
  212. else
  213. {
  214. // String values get their single quotes and special characters escaped
  215. $value = addcslashes($value, "'");
  216. }
  217. $data .= "'$value',";
  218. }
  219. $data = substr($data, 0, - 1); // Remove the last comma
  220. $data .= "\n);\nrequire_once('croninclude.php');";
  221. return $data;
  222. }
  223. /**
  224. * Ensures that the user passed on a valid ID.
  225. *
  226. * @return bool True if the ID exists
  227. */
  228. function checkID()
  229. {
  230. $ids = $this->getCronIDs();
  231. return in_array($this->_id, $ids);
  232. }
  233. /**
  234. * Fetches the next available ID
  235. * @return int An unused CRON helper script ID
  236. */
  237. function getNextId()
  238. {
  239. $ids = $this->getCronIDs();
  240. if(count($ids) == 0) return 1; // If there are no scripts, use #1
  241. asort($ids);
  242. return array_pop($ids)+1;
  243. }
  244. /**
  245. * Creates or overwrites a CRON helper script
  246. * @return bool True on success
  247. * @param array $data The POST'ed configuration data
  248. */
  249. function save($data)
  250. {
  251. // If we have no ID we can't continue!
  252. if(!isset($data['id']))
  253. {
  254. return false;
  255. }
  256. // Get the CRON script's ID
  257. if($data['id'] == 0)
  258. {
  259. $id = $this->getNextId();
  260. }
  261. else
  262. {
  263. $id = $data['id'];
  264. }
  265. unset($data['id']);
  266. $this->setId($id);
  267. // Push the configuration array's elements to the _cron object
  268. if(count($data) == 0)
  269. {
  270. // If there are no remaining configuration keys, we can't proceed!
  271. return false;
  272. }
  273. else
  274. {
  275. $this->_cron = new stdClass;
  276. foreach($data as $key => $value)
  277. {
  278. $this->_cron->$key = $value;
  279. }
  280. }
  281. // Make the script's contents
  282. $file_data = $this->_makeCRONScript();
  283. // Try to write it
  284. $filename = $this->scriptdirectory.DS."cron$id.php";
  285. $status = @file_put_contents($filename, $file_data);
  286. if($status === false)
  287. {
  288. return false;
  289. }
  290. else
  291. {
  292. return true;
  293. }
  294. }
  295. /**
  296. * Creates a copy of a CRON helper script
  297. * @return bool
  298. * @param int $id The CRON helper script ID to copy
  299. */
  300. function copy($id)
  301. {
  302. $newid = $this->getNextId();
  303. $source = $this->scriptdirectory.DS."cron$id.php";
  304. $destination = $this->scriptdirectory.DS."cron$newid.php";
  305. // Make sure the source exists
  306. if(!@file_exists($source)) return false;
  307. // Try to copy the file
  308. return @copy($source, $destination);
  309. }
  310. function remove($id)
  311. {
  312. $source = $this->scriptdirectory.DS."cron$id.php";
  313. // Make sure the source exists
  314. if(!@file_exists($source)) return false;
  315. // Try to remove the file
  316. return @unlink($source);
  317. }
  318. /**
  319. * Returns a list of post processing options, for JHTML use
  320. * @return array
  321. */
  322. function getPostOpList()
  323. {
  324. $options = array();
  325. $options[] = JHTML::_('select.option', 'none', JText::_('CRON_OPT_POSTOP_NONE'));
  326. $options[] = JHTML::_('select.option', 'upload', JText::_('CRON_OPT_POSTOP_UPLOAD'));
  327. $options[] = JHTML::_('select.option', 'email', JText::_('CRON_OPT_POSTOP_EMAIL'));
  328. return $options;
  329. }
  330. /**
  331. * Get a pagination object
  332. *
  333. * @access public
  334. * @return JPagination
  335. *
  336. */
  337. function getPagination()
  338. {
  339. if( empty($this->_pagination) )
  340. {
  341. // Import the pagination library
  342. jimport('joomla.html.pagination');
  343. // Prepare pagination values
  344. $total = $this->getTotal();
  345. $limitstart = $this->getState('limitstart');
  346. $limit = $this->getState('limit');
  347. // Create the pagination object
  348. $this->_pagination = new JPagination($total, $limitstart, $limit);
  349. }
  350. return $this->_pagination;
  351. }
  352. /**
  353. * Get number of CRON configuration items
  354. *
  355. * @access public
  356. * @return integer
  357. */
  358. function getTotal()
  359. {
  360. if( empty($this->_total) )
  361. {
  362. $ids = $this->getCronIDs();
  363. $this->_total = count($ids);
  364. }
  365. return $this->_total;
  366. }
  367. }