Core  3.2
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  * @package FDL
5 */
6 /**
7  * Crontab class
8  *
9  * This class allows you to manipulate a user crontab by registering
10  * and unregistering cron files
11  *
12  * @author Anakeen
13  * @version $Id: Class.Crontab.php,v 1.2 2009/01/16 15:51:35 jerome Exp $
14  * @package FDL
15  * @subpackage
16  */
17 /**
18  */
19 
20 class Crontab
21 {
22  var $user = NULL;
23  var $crontab = '';
24 
25  public function __construct($user = NULL)
26  {
27  $this->user = $user;
28  return $this;
29  }
30 
31  public function setUser($user)
32  {
33  $this->user = $user;
34  return $this->user;
35  }
36 
37  public function unsetUser()
38  {
39  $this->user = NULL;
40  return $this->user;
41  }
42 
43  private function load()
44  {
45  $cmd = 'crontab -l';
46  if ($this->user != NULL) {
47  $cmd.= ' -u ' . escapeshellarg($this->user);
48  }
49  $cmd.= ' 2> /dev/null';
50 
51  $ph = popen($cmd, 'r');
52  if ($ph === FALSE) {
53  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error popen");
54  return FALSE;
55  }
56 
57  $crontab = stream_get_contents($ph);
58  if ($crontab === FALSE) {
59  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error stream_get_contents");
60  return FALSE;
61  }
62 
63  $this->crontab = $crontab;
64 
65  return $crontab;
66  }
67 
68  private function save()
69  {
70  include_once ('WHAT/Lib.System.php');
71 
72  $tmp = tempnam(getTmpDir() , 'crontab');
73  if ($tmp === FALSE) {
74  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error creating temporary file");
75  return FALSE;
76  }
77 
78  $ret = file_put_contents($tmp, $this->crontab);
79  if ($ret === FALSE) {
80  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error writing content to file '" . $tmp . "'");
81  return FALSE;
82  }
83 
84  $cmd = 'crontab';
85  if ($this->user != NULL) {
86  $cmd.= ' -u ' . escapeshellarg($this->user);
87  }
88  $cmd.= ' ' . escapeshellarg($tmp);
89  $cmd.= ' > /dev/null 2>&1';
90 
91  system($cmd, $ret);
92  if ($ret != 0) {
93  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error saving crontab '" . $tmp . "'");
94  return FALSE;
95  }
96 
97  return $this->crontab;
98  }
99 
100  public function registerFile($file)
101  {
102  include_once ('WHAT/Lib.Prefix.php');
103 
104  $crontab = file_get_contents($file);
105  if ($crontab === FALSE) {
106  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading content from file '" . $file . "'");
107  return FALSE;
108  }
109 
110  $newSectionElement = new \Dcp\CrontabSectionElement(DEFAULT_PUBDIR, $file);
111  $newSectionElement->appendChild(new \Dcp\CrontabTextElement(sprintf("CONTEXT_ROOT=%s", DEFAULT_PUBDIR)));
112  $newSectionElement->appendChild(new \Dcp\CrontabTextElement($crontab));
113 
114  $crontabData = $this->load();
115  if ($crontabData === FALSE) {
116  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading active crontab");
117  return FALSE;
118  }
119 
120  $parser = new \Dcp\CrontabParser();
121  $crontabDocument = $parser->parse($crontabData);
122  /* Remove existing sections for this context and file */
123  $crontabDocument->childs = array_filter($crontabDocument->childs, function ($element) use ($file)
124  {
125  if (is_a($element, '\Dcp\CrontabSectionElement')) {
126  /**
127  * @var $element \Dcp\CrontabSectionElement
128  */
129  /* Keep sections that do not match this context/file */
130  return !$element->match(DEFAULT_PUBDIR, $file);
131  }
132  /* Keep text elements */
133  return true;
134  });
135  /* Add new section */
136  $crontabDocument->appendChild($newSectionElement);
137 
138  $this->crontab = (string)$crontabDocument;
139 
140  printf("Saving crontab...\n");
141  $ret = $this->save();
142  if ($ret === FALSE) {
143  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error saving crontab");
144  return FALSE;
145  }
146  printf("Done.\n");
147 
148  return $this->crontab;
149  }
150 
151  public function unregisterFile($file)
152  {
153  include_once ('WHAT/Lib.Prefix.php');
154 
155  $crontabData = $this->load();
156  if ($crontabData === FALSE) {
157  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading active crontab");
158  return FALSE;
159  }
160 
161  $parser = new \Dcp\CrontabParser();
162  $crontabDocument = $parser->parse($crontabData);
163  /* Remove existing sections for this context/file */
164  $crontabDocument->childs = array_filter($crontabDocument->childs, function (\Dcp\CrontabElement & $element) use ($file)
165  {
166  if (is_a($element, '\Dcp\CrontabSectionElement')) {
167  /**
168  * @var $element \Dcp\CrontabSectionElement
169  */
170  /* Keep sections that do not match this context/file */
171  return !$element->match(DEFAULT_PUBDIR, $file);
172  }
173  /* Keep other elements */
174  return true;
175  });
176 
177  $this->crontab = (string)$crontabDocument;
178 
179  printf("Saving crontab...\n");
180  $ret = $this->save();
181  if ($ret === FALSE) {
182  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error saving crontab");
183  return FALSE;
184  }
185  printf("Done.\n");
186 
187  return $this->crontab;
188  }
189 
190  public function listAll()
191  {
192  $crontabs = $this->getActiveCrontab();
193  if ($crontabs === FALSE) {
194  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error retrieving active crontabs");
195  return FALSE;
196  }
197 
198  print "\n";
199  print "Active crontabs\n";
200  print "---------------\n";
201  print "\n";
202  foreach ($crontabs as $crontab) {
203  print "Crontab: " . $crontab['file'] . "\n";
204  print "--8<--\n" . $crontab['content'] . "\n-->8--\n\n";
205  }
206 
207  return TRUE;
208  }
209 
210  public function getActiveCrontab()
211  {
212  include_once ('WHAT/Lib.Prefix.php');
213 
214  $crontabData = $this->load();
215  if ($crontabData === FALSE) {
216  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading active crontab");
217  return FALSE;
218  }
219 
220  $parser = new \Dcp\CrontabParser();
221  $crontabDocument = $parser->parse($crontabData);
222 
223  $crontabs = array();
224  foreach ($crontabDocument->childs as & $element) {
225  /**
226  * @var $element \Dcp\CrontabSectionElement
227  */
228  if (is_a($element, '\Dcp\CrontabSectionElement') && $element->matchContextRoot(DEFAULT_PUBDIR)) {
229  array_push($crontabs, array(
230  'file' => $element->file,
231  'content' => (string)$element
232  ));
233  }
234  }
235  unset($element);
236 
237  return $crontabs;
238  }
239 
240  public function unregisterAll()
241  {
242  include_once ('WHAT/Lib.Prefix.php');
243 
244  $crontabData = $this->load();
245  if ($crontabData === FALSE) {
246  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error reading active crontab");
247  return FALSE;
248  }
249 
250  $parser = new \Dcp\CrontabParser();
251  $crontabDocument = $parser->parse($crontabData);
252  /* Remove existing sections for this context */
253  $crontabDocument->childs = array_filter($crontabDocument->childs, function (\Dcp\CrontabElement & $element)
254  {
255  if (is_a($element, '\Dcp\CrontabSectionElement')) {
256  /**
257  * @var $element \Dcp\CrontabSectionElement
258  */
259  /* Keep sections that do not match the current context */
260  return !$element->matchContextRoot(DEFAULT_PUBDIR);
261  }
262  /* Keep other elements */
263  return true;
264  });
265 
266  $this->crontab = (string)$crontabDocument;
267 
268  printf("Saving crontab...\n");
269  $ret = $this->save();
270  if ($ret === FALSE) {
271  error_log(__CLASS__ . "::" . __FUNCTION__ . " Error saving crontab");
272  return FALSE;
273  }
274  printf("Done.\n");
275 
276  return $this->crontab;
277  }
278 }
$ret
$core user
Definition: chgpasswd.php:38
$file
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
foreach($argv as $arg) $cmd
setUser($user)
print
Definition: checklist.php:49
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
__construct($user=NULL)
← centre documentaire © anakeen