Alternc  3.2
Alternc logiel libre pour l'h├ębergement
 All Data Structures Namespaces Files Functions Variables Pages
m_cron.php
Go to the documentation of this file.
1 <?php
2 /*
3  ----------------------------------------------------------------------
4  AlternC - Web Hosting System
5  Copyright (C) 2000-2012 by the AlternC Development Team.
6  https://alternc.org/
7  ----------------------------------------------------------------------
8  LICENSE
9 
10  This program is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License (GPL)
12  as published by the Free Software Foundation; either version 2
13  of the License, or (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  To read the license please visit http://www.gnu.org/copyleft/gpl.html
21  ----------------------------------------------------------------------
22  Purpose of file: Manage hook system.
23  ----------------------------------------------------------------------
24 */
25 
26 /**
27  * This class manage web-cron tasks
28  */
29 class m_cron {
30 
31 
32  /*---------------------------------------------------------------------------*/
33  /** Constructor
34  */
35  function m_cron() {
36  }
37 
38  function schedule() {
39  return Array(
40  Array('unit'=>1440, 'name'=>_("Daily")),
41  Array('unit'=>60, 'name'=>_("Hour")),
42  Array('unit'=>30, 'name'=>_("Half Hour")),
43  );
44  }
45 
46 
47  /*---------------------------------------------------------------------------*/
48  /** List the crontab for the current user.
49  * @return array an hash for each crontab.
50  */
51  function lst_cron() {
52  global $cuid,$db,$err;
53  $err->log("cron","lst_cron");
54  $db->query("SELECT * FROM cron WHERE uid = $cuid ORDER BY url;");
55  $r=Array();
56  while ($db->next_record()) {
57  $tmp=Array();
58  $tmp['id']=$db->f('id');
59  $tmp['url']=urldecode($db->f('url'));
60  $tmp['user']=urldecode($db->f('user'));
61  $tmp['password']=urldecode($db->f('password'));
62  $tmp['schedule']=$db->f('schedule');
63  $tmp['email']=urldecode($db->f('email'));
64  $tmp['next_execution']=$db->f('next_execution');
65  $r[]=$tmp;
66  }
67  return $r;
68  }
69 
70  function hook_menu() {
71  $obj = array(
72  'title' => _("Scheduled tasks"),
73  'ico' => 'images/schedule.png',
74  'link' => 'cron.php',
75  'pos' => 90,
76  ) ;
77 
78  return $obj;
79  }
80 
81  /*---------------------------------------------------------------------------*/
82  /** update the crontab
83  * @param $arr array the crontab information, including its ID
84  * @return boolean TRUE if the crontab has been edited
85  */
86  function update($arr) {
87  $ok=true;
88  foreach ($arr as $a) {
89  if (! isset($a['id'])) $a['id']=null;
90  if (empty($a['url']) && is_null($a['id'])) continue;
91  if (! $this->_update_one($a['url'], $a['user'], $a['password'], $a['email'], $a['schedule'], $a['id']) ) {
92  $ok=false;
93  }
94  }
95  return $ok;
96  }
97 
98 
99  /*---------------------------------------------------------------------------*/
100  /** delete a crontab
101  * @param $id the id of the crontab to delete
102  * @return boolean TRUE if the crontab has been deleted
103  */
104  function delete_one($id) {
105  global $db,$err,$cuid;
106  $err->log("cron","delete_one");
107  return $db->query("DELETE FROM cron WHERE id=".intval($id)." AND uid=$cuid LIMIT 1;");
108  }
109 
110 
111  /*---------------------------------------------------------------------------*/
112  /** update a crontab,
113  * @return boolean TRUE if the crontab has been edited
114  */
115  private function _update_one($url, $user, $password, $email, $schedule, $id=null) {
116  global $db,$err,$quota,$cuid;
117  $err->log("cron","update_one");
118 
119  if (empty($url) && !is_null($id)) {
120  return $this->delete_one($id);
121  }
122 
123 
124  if(filter_var($url,FILTER_VALIDATE_URL)===false){
125  $err->raise("cron",_("URL not valid"));
126  return false;
127  }
128  $url=urlencode($url);
129  $user=urlencode($user);
130  if (empty($user)) $password='';
131  $password=urlencode($password);
132  if (!empty($email) && ! checkmail($email) == 0 ){
133  $err->raise("cron",_("Email address is not valid"));
134  return false;
135  }
136  $email=urlencode($email);
137  if (! $this->valid_schedule($schedule)) return false;
138 
139  if (is_null($id)) { // if a new insert, quotacheck
140  $q = $quota->getquota("cron");
141  if ( $q["u"] >= $q["t"] ) {
142  $err->raise("cron",_("You quota of cron entries is over. You cannot create more cron entries"));
143  return false;
144  }
145  } else { // if not a new insert, check the $cuid
146  $db->query("SELECT uid FROM cron WHERE id = $id;");
147  if (! $db->next_record()) {
148  return "false";
149  } // return false if pb
150  if ( $db->f('uid') != $cuid ) {
151  $err->raise("cron",_("Identity problem"));
152  return false;
153  }
154  }
155  $query = "REPLACE INTO cron (id, uid, url, user, password, schedule, email) VALUES ('$id', '$cuid', '$url', '$user', '$password', '$schedule', '$email') ;";
156  return $db->query("$query");
157  }
158 
159 
160  /*---------------------------------------------------------------------------*/
161  /** validate a crontab schedule
162  * @param $s array schedule paramters
163  * @return boolean TRUE if the schedule is valid
164  */
165  function valid_schedule($s) {
166  $s2 = intval($s);
167  if ($s2 != $s) return false;
168  $r=false;
169  foreach ($this->schedule() as $cs ) {
170  if ($cs['unit'] == $s) return true;
171  }
172  return $r;
173  }
174 
175  /*---------------------------------------------------------------------------*/
176  /** hook for quota computation
177  */
178  function hook_quota_get() {
179  global $cuid,$db,$err;
180  $err->log("cron","alternc_get_quota");
181  $q=Array("name"=>"cron", "description"=>_("Scheduled tasks"), "used"=>0);
182  $db->query("select count(*) as cnt from cron where uid = $cuid;");
183  if ($db->next_record()) {
184  $q['used']=$db->f('cnt');
185  }
186  return $q;
187  }
188 
189 
190 } /* Class cron */