1 | <? |
---|
2 | /** |
---|
3 | * Creates cron jobs |
---|
4 | * @author Carlos Eduardo Nogueira Gonçalves |
---|
5 | * @author Richard Sumilang <richard@richard-sumilang.com> |
---|
6 | * @version 1.0 |
---|
7 | * @package Workflow |
---|
8 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
9 | */ |
---|
10 | class Crontab { |
---|
11 | |
---|
12 | /** |
---|
13 | * @var int $minute Minute(s)... 0 to 59 |
---|
14 | * @access public |
---|
15 | */ |
---|
16 | var $minute = null; |
---|
17 | /** |
---|
18 | * @var int $hour $hour Hour(s)... 0 to 23 |
---|
19 | * @access public |
---|
20 | */ |
---|
21 | var $hour = null; |
---|
22 | /** |
---|
23 | * @var int $day day Day(s)... 1 to 31 |
---|
24 | * @access public |
---|
25 | */ |
---|
26 | var $day = null; |
---|
27 | /** |
---|
28 | * @var int $month month Month(s)... 1 to 12 or short name |
---|
29 | * @access public |
---|
30 | */ |
---|
31 | var $month = null; |
---|
32 | /** |
---|
33 | * @var int $dayofweek dayofweek Day(s) of week... 0 to 7 or short name. 0 and 7 = sunday |
---|
34 | * @access public |
---|
35 | */ |
---|
36 | var $dayofweek = null; |
---|
37 | /** |
---|
38 | * @var string $command Comand to set |
---|
39 | * @access public |
---|
40 | */ |
---|
41 | var $command = null; |
---|
42 | /** |
---|
43 | * @var string $directory Directory to hold cron job files |
---|
44 | * @access public |
---|
45 | */ |
---|
46 | var $directory = null; |
---|
47 | /** |
---|
48 | * @var string $filename Filename to write to |
---|
49 | * @access public |
---|
50 | */ |
---|
51 | var $filename = 'crons'; |
---|
52 | /** |
---|
53 | * @var string $crontabPath Path to cron program |
---|
54 | * @access public |
---|
55 | */ |
---|
56 | var $crontabPath = null; |
---|
57 | /** |
---|
58 | * @var mixed $handle Resource handle to the file |
---|
59 | * @access public |
---|
60 | */ |
---|
61 | var $handle = null; |
---|
62 | |
---|
63 | /** |
---|
64 | * Attempts to create directory for holding cron jobs |
---|
65 | * @param string $dir Directory to hold cron job files |
---|
66 | * @param string $filename Filename to write to |
---|
67 | * @param string $crontabPath Path to cron program |
---|
68 | * @access public |
---|
69 | */ |
---|
70 | function Crontab($dir = null, $filename = null, $crontabPath = null) { |
---|
71 | $result = (!$dir) ? $this->setDirectory("~/my_crontabs") : $this->setDirectory($dir); |
---|
72 | if (!$result) { |
---|
73 | exit('Directory error'); |
---|
74 | } |
---|
75 | $result = (!$filename) ? $this->createCronFile("crons") : $this->createCronFile($filename); |
---|
76 | if (!$result) { |
---|
77 | exit('File error'); |
---|
78 | } |
---|
79 | $this->pathToCrontab = ($crontabPath) ? null : $crontabPath; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Sets date parameters. |
---|
84 | * If any parameters are left null then they default to *. |
---|
85 | * A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2, 3, and 4. |
---|
86 | * A list of values separated by commas (,) specifies a list. For example, 3, 4, 6, 8 indicates those four specific integers. |
---|
87 | * The forward slash (/) can be used to specify step values. The value of an integer can be skipped within a range by following the range |
---|
88 | * with /<integer>. For example, 0-59/2 can be used to define every other minute in the minute field. Step values can also be used with an asterisk. |
---|
89 | * For instance, the value * /3 (no space) can be used in the month field to run the task every third month. |
---|
90 | * @param mixed $min Minute(s)... 0 to 59 |
---|
91 | * @param mixed $hour Hour(s)... 0 to 23 |
---|
92 | * @param mixed $day Day(s)... 1 to 31 |
---|
93 | * @param mixed $month Month(s)... 1 to 12 or short name |
---|
94 | * @param mixed $dayofweek Day(s) of week... 0 to 7 or short name. 0 and 7 = sunday |
---|
95 | * $acccess public |
---|
96 | */ |
---|
97 | function setDateParams($min = null, $hour = null, $day = null, $month = null, $dayofweek = null) { |
---|
98 | if ($min == "0") |
---|
99 | $this->minute = 0; |
---|
100 | elseif ($min) |
---|
101 | $this->minute = $min; |
---|
102 | else |
---|
103 | $this->minute = "*"; |
---|
104 | |
---|
105 | if ($hour == "0") |
---|
106 | $this->hour = 0; |
---|
107 | elseif ($hour) |
---|
108 | $this->hour = $hour; |
---|
109 | else |
---|
110 | $this->hour = "*"; |
---|
111 | $this->month = ($month) ? $month : "*"; |
---|
112 | $this->day = ($day) ? $day : "*"; |
---|
113 | $this->dayofweek = ($dayofweek) ? $dayofweek : "*"; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Sets the directory path |
---|
118 | * Will check it if it exists then try to open it. Also if it doesn't exist then it will try to create it, makes it with mode 0700 |
---|
119 | * @param string $directory Directory, relative or full path |
---|
120 | * @access public |
---|
121 | * @return boolean |
---|
122 | */ |
---|
123 | function setDirectory($directory) { |
---|
124 | if (!$directory) { |
---|
125 | return false; |
---|
126 | } |
---|
127 | if(is_dir($directory)) { |
---|
128 | if ($dh = opendir($directory)) { |
---|
129 | $this->directory = $directory; |
---|
130 | return true; |
---|
131 | } else { |
---|
132 | return false; |
---|
133 | } |
---|
134 | } else { |
---|
135 | if (mkdir($directory, 0700)){ |
---|
136 | $this->directory = $directory; |
---|
137 | return true; |
---|
138 | } |
---|
139 | } |
---|
140 | return false; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * Creates cron file |
---|
146 | * This will create a cron job file for you and set the filename |
---|
147 | * of this class to use it. Make sure you have already set the directory |
---|
148 | * path variable with the consructor. If the file exists and we can write |
---|
149 | * it then return true esle false. Also sets $handle with the resource handle |
---|
150 | * to the file |
---|
151 | * @param string $filename Name of file you want to create |
---|
152 | * @access public |
---|
153 | * @return boolean |
---|
154 | */ |
---|
155 | function createCronFile($filename = null) { |
---|
156 | if (!$filename) { |
---|
157 | return false; |
---|
158 | } |
---|
159 | if (file_exists($this->directory . $filename)) { |
---|
160 | if ($handle = fopen($this->directory . $filename, 'a')) { |
---|
161 | $this->handle =& $handle; |
---|
162 | $this->filename = $filename; |
---|
163 | return true; |
---|
164 | } else { |
---|
165 | return false; |
---|
166 | } |
---|
167 | } |
---|
168 | if (!$handle = fopen($this->directory . $filename, 'a')) { |
---|
169 | return false; |
---|
170 | } else { |
---|
171 | $this->handle =& $handle; |
---|
172 | $this->filename = $filename; |
---|
173 | return true; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Sets command to execute |
---|
179 | * @param string $command Comand to set |
---|
180 | * @access public |
---|
181 | * @return bool |
---|
182 | */ |
---|
183 | function setCommand($command) { |
---|
184 | if ($command) { |
---|
185 | $this->command = $command; |
---|
186 | return false; |
---|
187 | } else { |
---|
188 | return false; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Write cron command to file. Make sure you used createCronFile |
---|
194 | * before using this function of it will return false |
---|
195 | * |
---|
196 | * @access public |
---|
197 | * @return bool |
---|
198 | */ |
---|
199 | function saveCronFile() { |
---|
200 | $command = |
---|
201 | $this->minute . " " . |
---|
202 | $this->hour . " " . |
---|
203 | $this->day . " " . |
---|
204 | $this->month . " " . |
---|
205 | $this->dayofweek . " " . |
---|
206 | $this->command . "\n"; |
---|
207 | if (!fwrite($this->handle, $command)) { |
---|
208 | return true; |
---|
209 | } else { |
---|
210 | return false; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Saves cron in system |
---|
216 | * @access public |
---|
217 | * @return void |
---|
218 | */ |
---|
219 | function addToCrontab() { |
---|
220 | if (!$this->filename) { |
---|
221 | exit('No name specified for cron file'); |
---|
222 | } |
---|
223 | exec($this->crontabPath . "crontab " . $this->directory . $this->filename); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * Destroys file pointer |
---|
228 | * @access public |
---|
229 | * @return boolean |
---|
230 | */ |
---|
231 | function destroyFilePoint() { |
---|
232 | fclose($this->handle); |
---|
233 | return true; |
---|
234 | } |
---|
235 | } |
---|
236 | ?> |
---|