Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Crontab.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Crontab class
9  *
10  * This class allows you to manipulate a user crontab by registering
11  * and unregistering cron files
12  *
13  * @author Anakeen 2009
14  * @version $Id: Class.Crontab.php,v 1.2 2009/01/16 15:51:35 jerome Exp $
15  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
16  * @package FDL
17  * @subpackage
18  */
19 /**
20  */
21 
22 class Crontab
23 {
24  var $user = NULL;
25  var $crontab = '';
26 
27  public function __construct($user = NULL)
28  {
29  $this->user = $user;
30  return $this;
31  }
32 
33  public function setUser($user)
34  {
35  $this->user = $user;
36  return $this->user;
37  }
38 
39  public function unsetUser()
40  {
41  $this->user = NULL;
42  return $this->user;
43  }
44 
45  private function load()
46  {
47  $cmd = 'crontab -l';
48  if ($this->user != NULL) {
49  $cmd.= ' -u ' . escapeshellarg($this->user);
50  }
51  $cmd.= ' 2> /dev/null';
52 
53  $ph = popen($cmd, 'r');
54  if ($ph === FALSE) {
55  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error popen");
56  return FALSE;
57  }
58 
59  $crontab = stream_get_contents($ph);
60  if ($crontab === FALSE) {
61  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error stream_get_contents");
62  return FALSE;
63  }
64 
65  $this->crontab = $crontab;
66 
67  return $crontab;
68  }
69 
70  private function save()
71  {
72  include_once ('WHAT/Lib.System.php');
73 
74  $tmp = LibSystem::tempnam(null, 'crontab');
75  if ($tmp === FALSE) {
76  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error creating temporary file");
77  return FALSE;
78  }
79 
80  $ret = file_put_contents($tmp, $this->crontab);
81  if ($ret === FALSE) {
82  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error writing content to file '" . $tmp . "'");
83  return FALSE;
84  }
85 
86  $cmd = 'crontab';
87  if ($this->user != NULL) {
88  $cmd.= ' -u ' . escapeshellarg($this->user);
89  }
90  $cmd.= ' ' . escapeshellarg($tmp);
91  $cmd.= ' > /dev/null 2>&1';
92 
93  system($cmd, $ret);
94  if ($ret != 0) {
95  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error saving crontab '" . $tmp . "'");
96  return FALSE;
97  }
98 
99  return $this->crontab;
100  }
101 
102  public function registerFile($file)
103  {
104  include_once ('WHAT/Lib.Prefix.php');
105 
106  $crontab = file_get_contents($file);
107  if ($crontab === FALSE) {
108  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading content from file '" . $file . "'");
109  return FALSE;
110  }
111 
112  $crontab = "# BEGIN:FREEDOM_CRONTAB:" . DEFAULT_PUBDIR . ":" . $file . "\n" . "CONTEXT_ROOT=" . DEFAULT_PUBDIR . "\n" . $crontab . "\n" . "# END:FREEDOM_CRONTAB:" . DEFAULT_PUBDIR . ":" . $file . "\n";
113 
114  $activeCrontab = $this->load();
115  if ($activeCrontab === FALSE) {
116  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading active crontab");
117  return FALSE;
118  }
119 
120  if (preg_match('/^#\s+BEGIN:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':' . preg_quote($file, '/') . '.*?#\s+END:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':' . preg_quote($file, '/') . '$/ms', $activeCrontab) === 1) {
121  print "Removing existing crontab\n";
122  $tmpCrontab = preg_replace('/^#\s+BEGIN:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':' . preg_quote($file, '/') . '.*?#\s+END:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':' . preg_quote($file, '/') . '$/ms', '', $activeCrontab);
123  if ($tmpCrontab === NULL) {
124  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error removing existing registered crontab");
125  return FALSE;
126  }
127  $activeCrontab = $tmpCrontab;
128  }
129 
130  $activeCrontab.= $crontab;
131  $this->crontab = $activeCrontab;
132 
133  $ret = $this->save();
134  if ($ret === FALSE) {
135  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error saving crontab");
136  return FALSE;
137  }
138 
139  return $this->crontab;
140  }
141 
142  public function unregisterFile($file)
143  {
144  include_once ('WHAT/Lib.Prefix.php');
145 
146  $activeCrontab = $this->load();
147  if ($activeCrontab === FALSE) {
148  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading active crontab");
149  return FALSE;
150  }
151 
152  $tmpCrontab = preg_replace('/^#\s+BEGIN:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':' . preg_quote($file, '/') . '.*?#\s+END:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':' . preg_quote($file, '/') . '$/ms', '', $activeCrontab);
153  if ($tmpCrontab === NULL) {
154  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error unregistering crontab '" . DEFAULT_PUBDIR . ":" . $file . "' from active crontab");
155  return FALSE;
156  }
157 
158  $this->crontab = $tmpCrontab;
159 
160  $ret = $this->save();
161  if ($ret === FALSE) {
162  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error saving crontab");
163  return FALSE;
164  }
165 
166  return $this->crontab;
167  }
168 
169  public function listAll()
170  {
171  $crontabs = $this->getActiveCrontab();
172  if ($crontabs === FALSE) {
173  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error retrieving active crontabs");
174  return FALSE;
175  }
176 
177  print "\n";
178  print "Active crontabs\n";
179  print "---------------\n";
180  print "\n";
181  foreach ($crontabs as $crontab) {
182  print "Crontab: " . $crontab['file'] . "\n";
183  print "--8<--\n" . $crontab['content'] . "\n-->8--\n\n";
184  }
185 
186  return TRUE;
187  }
188 
189  public function getActiveCrontab()
190  {
191  include_once ('WHAT/Lib.Prefix.php');
192 
193  $activeCrontab = $this->load();
194  if ($activeCrontab === FALSE) {
195  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading active crontab");
196  return FALSE;
197  }
198 
199  $ret = preg_match_all('/^#\s+BEGIN:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':(.*?)\n(.*?)\n#\s+END:FREEDOM_CRONTAB:' . preg_quote(DEFAULT_PUBDIR, '/') . ':\1/ms', $activeCrontab, $matches, PREG_SET_ORDER);
200  if ($ret === FALSE) {
201  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error in preg_match_all");
202  return FALSE;
203  }
204 
205  $crontabs = array();
206  foreach ($matches as $match) {
207  array_push($crontabs, array(
208  'file' => $match[1],
209  'content' => $match[2]
210  ));
211  }
212 
213  return $crontabs;
214  }
215 }
216 ?>
← centre documentaire © anakeen - published under CC License - Dynacase