PageRenderTime 67ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/cronjobs/libraries/Cronjob.php

https://github.com/be3/FUEL-CMS
PHP | 264 lines | 121 code | 31 blank | 112 comment | 14 complexity | f8839e1561e8adecd87a062a44b0864a MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2011, Run for Daylight LLC.
  12. * @license http://www.getfuelcms.com/user_guide/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * A Cronjob wrapper class
  19. *
  20. * This class allows you to manage cron jobs
  21. * based on this:
  22. * http://www.underwaterdesign.com/2006/06/php_create_a_cron_job_with_php.php
  23. *
  24. * @package FUEL CMS
  25. * @subpackage Libraries
  26. * @category Libraries
  27. * @author David McReynolds @ Daylight Studio
  28. * @link http://www.getfuelcms.com/user_guide/libraries/cronjob.html
  29. */
  30. class Cronjob {
  31. public $cronfile = 'crons/crontab.txt'; // path to the crontab fle
  32. public $mailto = ''; // mailto value of crontab
  33. public $user = ''; // the user the crontab belongs to
  34. public $sudo_pwd = ''; //the user password
  35. private $_jobs = array();
  36. /**
  37. * Constructor - Sets Cronjob preferences
  38. *
  39. * The constructor can be passed an array of config values
  40. */
  41. function __construct($params = array())
  42. {
  43. if (count($params) > 0)
  44. {
  45. $this->initialize($params);
  46. }
  47. }
  48. // --------------------------------------------------------------------
  49. /**
  50. * Initialize the user preferences
  51. *
  52. * Accepts an associative array as input, containing display preferences
  53. *
  54. * @access public
  55. * @param array config preferences
  56. * @return void
  57. */
  58. function initialize($config = array())
  59. {
  60. foreach ($config as $key => $val)
  61. {
  62. if (isset($this->$key))
  63. {
  64. $this->$key = $val;
  65. }
  66. }
  67. }
  68. // --------------------------------------------------------------------
  69. /**
  70. * Sets the file path to the cron file
  71. *
  72. * @access public
  73. * @param string cron file path
  74. * @return void
  75. */
  76. function set_cronfile($cronfile)
  77. {
  78. $this->cronfile = $cronfile;
  79. }
  80. // --------------------------------------------------------------------
  81. /**
  82. * Adds a new line to the cron file
  83. *
  84. * @access public
  85. * @param string minute value of cronjob
  86. * @param string hour value of cronjob
  87. * @param string day of the month value of cronjob
  88. * @param string month value of cronjob
  89. * @param string week day value of cronjob
  90. * @param string the command to execute
  91. * @return void
  92. */
  93. function add($min = '*', $hour = '*', $month_day = '*', $month_num = '*', $week_day = '*', $command = '')
  94. {
  95. // if $min has a space, then we assume that that is the entire job as a single string
  96. if (strpos($min, ' ') !== FALSE)
  97. {
  98. $job = $min;
  99. }
  100. else
  101. {
  102. $time_segs = array('min', 'hour', 'month_day', 'month_num', 'week_day');
  103. foreach($time_segs as $seg)
  104. {
  105. if (!isset($$seg)) $$seg = '*';
  106. }
  107. $command = trim($command);
  108. if (!empty($command)) $job = $min." ".$hour." ".$month_day." ".$month_num." ".$week_day." ".$command;
  109. }
  110. if (!empty($job)) $this->_jobs[] = $job;
  111. }
  112. // --------------------------------------------------------------------
  113. /**
  114. * Creates the cron file
  115. *
  116. * If parameters are passed, they will be sent to the add method
  117. *
  118. * @access public
  119. * @param mixed optional to add values that will call the add method
  120. * @return void
  121. */
  122. function create()
  123. {
  124. // if arguments are passed to this method then we will pass them to the add method
  125. $args = func_get_args();
  126. $exec = $this->_exec('crontab '.$this->cronfile);
  127. if (!empty($args))
  128. {
  129. call_user_func_array(array(&$this, 'add'), $args);
  130. }
  131. // cast to array in case a string is input
  132. $this->_jobs = (array) $this->_jobs;
  133. // create cronjob string
  134. $cron = '';
  135. if (!empty($this->mailto)) $cron .= "MAILTO=".$this->mailto.PHP_EOL;
  136. $joiner = (empty($this->mailto)) ? ">> /dev/null 2>&1" : '';
  137. $cron .= implode($joiner.PHP_EOL, $this->_jobs);
  138. $cron .= PHP_EOL; // !!!important... for cronjobs to run, you must have this ending newline
  139. if (file_exists($this->cronfile))
  140. {
  141. $open = fopen($this->cronfile, "w"); // This overwrites current line
  142. fwrite($open, $cron);
  143. fclose($open);
  144. // this will reinstate your Cron job
  145. exec($exec);
  146. }
  147. else
  148. {
  149. // get directory and check if it is writable
  150. $dir = explode(DIRECTORY_SEPARATOR, $this->cronfile);
  151. array_pop($dir);
  152. $dir = implode(DIRECTORY_SEPARATOR, $dir).DIRECTORY_SEPARATOR;
  153. if (!is_writable($this->cronfile))
  154. {
  155. @mkdir($dir, 0777, TRUE);
  156. }
  157. touch($this->cronfile); // create the file, Directory "cron" must be writeable
  158. chmod($this->cronfile, 0777); // make new file writeable
  159. $open = fopen($this->cronfile, "w");
  160. fwrite($open, $cron);
  161. fclose($open);
  162. // start the cron job!
  163. exec($exec);
  164. }
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Removes the crontab
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. function remove()
  174. {
  175. $exec = $this->_exec('crontab -r');
  176. exec($exec);
  177. }
  178. // --------------------------------------------------------------------
  179. /**
  180. * Views a crontab
  181. *
  182. * @access public
  183. * @return void
  184. */
  185. function view()
  186. {
  187. $exec = $this->_exec('crontab -l');
  188. exec($exec, $output);
  189. $return = array();
  190. foreach($output as $o)
  191. {
  192. $o = trim($o);
  193. if (!empty($o))
  194. {
  195. $return[] = $o;
  196. }
  197. }
  198. return $return;
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Accessor method to the _jobs array that contains a list of the cronjobs
  203. *
  204. * @access public
  205. * @return void
  206. */
  207. function jobs()
  208. {
  209. return $this->_jobs;
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * Helps create the executable command based on the user and sudo_pwd information
  214. *
  215. * @access public
  216. * @param string
  217. * @return void
  218. */
  219. private function _exec($exec)
  220. {
  221. if (!empty($this->user))
  222. {
  223. if (!empty($this->sudo_pwd))
  224. {
  225. $exec = 'echo '.$this->sudo_pwd.' | sudo -S '.$exec;
  226. }
  227. $exec .= ' -u '.$this->user;
  228. }
  229. return $exec;
  230. }
  231. }
  232. /* End of file Cronjob.php */
  233. /* Location: ./modules/cronjob/libraries/Cronjob.php */