m_cron.php 5.88 KB
Newer Older
1
2
<?php
/*
3
4
5
6
7
 ----------------------------------------------------------------------
 AlternC - Web Hosting System
 Copyright (C) 2000-2012 by the AlternC Development Team.
 https://alternc.org/
 ----------------------------------------------------------------------
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 LICENSE

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License (GPL)
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 To read the license please visit http://www.gnu.org/copyleft/gpl.html
 ----------------------------------------------------------------------
 Purpose of file: Manage hook system.
 ----------------------------------------------------------------------
*/
25

26
/**
27
 * This class manage web-cron tasks
28
29
30
 */
class m_cron {

31

32
33
34
35
36
  /*---------------------------------------------------------------------------*/
  /** Constructor
  */
  function m_cron() {
  }
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
37

38
  function schedule() {
39
40
41
42
43
    return Array(
		 Array('unit'=>1440, 'name'=>_("Daily")),
		 Array('unit'=>60,   'name'=>_("Hour")),
		 Array('unit'=>30,   'name'=>_("Half Hour")),
		 );
44
45
  }

46
47
48
49
50
  
  /*---------------------------------------------------------------------------*/
  /** List the crontab for the current user.
   * @return array an hash for each crontab.
   */
51
52
53
  function lst_cron() {
    global $cuid,$db,$err;
    $err->log("cron","lst_cron");
54
    $db->query("SELECT * FROM cron WHERE uid = $cuid ORDER BY url;");
55
56
57
58
59
60
61
62
63
    $r=Array();
    while ($db->next_record()) {
      $tmp=Array();
      $tmp['id']=$db->f('id');
      $tmp['url']=urldecode($db->f('url'));
      $tmp['user']=urldecode($db->f('user'));
      $tmp['password']=urldecode($db->f('password'));
      $tmp['schedule']=$db->f('schedule');
      $tmp['email']=urldecode($db->f('email'));
Alan Garcia's avatar
Alan Garcia committed
64
      $tmp['next_execution']=$db->f('next_execution');
65
66
67
68
69
      $r[]=$tmp;
    }
    return $r;
  }

70
71
72
73
74
75
76
77
78
79
  function hook_menu() {
    $obj = array(
      'title'       => _("Scheduled tasks"),
      'ico'         => 'images/schedule.png',
      'link'        => 'cron.php',
      'pos'         => 90,
     ) ;

     return $obj;
  }
80
81
82
83
84
85

  /*---------------------------------------------------------------------------*/
  /** update the crontab 
   * @param $arr array the crontab information, including its ID
   * @return boolean TRUE if the crontab has been edited
  */
86
87
88
89
90
  function update($arr) {
    $ok=true;
    foreach ($arr as $a) {
      if (! isset($a['id'])) $a['id']=null;
      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
93
94
95
96
        $ok=false;
      }
    }
    return $ok;
  }
97
  
98

99
100
101
102
103
  /*---------------------------------------------------------------------------*/
  /** delete a crontab 
   * @param $id the id of the crontab to delete
   * @return boolean TRUE if the crontab has been deleted
  */
104
105
106
  function delete_one($id) {
    global $db,$err,$cuid;
    $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
113
114
115
  /*---------------------------------------------------------------------------*/
  /** update a crontab, 
   * @return boolean TRUE if the crontab has been edited
  */
  private function _update_one($url, $user, $password, $email, $schedule, $id=null) {
116
117
118
119
120
121
122
    global $db,$err,$quota,$cuid;
    $err->log("cron","update_one");

    if (empty($url) && !is_null($id)) {
      return $this->delete_one($id);
    }

Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
123
124
125
126
127

    if(filter_var($url,FILTER_VALIDATE_URL)===false){
      $err->raise("cron",_("URL not valid"));
      return false;
    }
Alan Garcia's avatar
Alan Garcia committed
128
129
    $url=urlencode($url);
    $user=urlencode($user);
130
    if (empty($user)) $password='';
Alan Garcia's avatar
Alan Garcia committed
131
    $password=urlencode($password);
132
133
    
    //@todo remove checkmail cf functions.php
Alan Garcia's avatar
Alan Garcia committed
134
    if (!empty($email) && ! checkmail($email) == 0 ){ 
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
135
136
137
        $err->raise("cron",_("Email address is not valid"));
      return false;
    }
Alan Garcia's avatar
Alan Garcia committed
138
    $email=urlencode($email);
139
140
141
142
143
    if (! $this->valid_schedule($schedule)) return false;

    if (is_null($id)) { // if a new insert, quotacheck
      $q = $quota->getquota("cron");
      if ( $q["u"] >= $q["t"] ) {
144
        $err->raise("cron",_("You quota of cron entries is over. You cannot create more cron entries"));
145
146
147
        return false;
      }
    } else { // if not a new insert, check the $cuid
148
      $db->query("SELECT uid FROM cron WHERE id = $id;");
Steven Mondji-Lerider's avatar
Steven Mondji-Lerider committed
149
150
151
      if (! $db->next_record()) { 
        return "false"; 
      } // return false if pb
152
      if ( $db->f('uid') != $cuid ) {
Alan Garcia's avatar
Alan Garcia committed
153
        $err->raise("cron",_("Identity problem"));
154
155
156
        return false;
      } 
    }
Alan Garcia's avatar
Alan Garcia committed
157
    $query = "REPLACE INTO cron (id, uid, url, user, password, schedule, email) VALUES ('$id', '$cuid', '$url', '$user', '$password', '$schedule', '$email') ;";
158
159
160
    return $db->query("$query");
  }

161
162
163
164
165
166

  /*---------------------------------------------------------------------------*/
  /** validate a crontab schedule
   * @param $s array schedule paramters
   * @return boolean TRUE if the schedule is valid
  */
167
168
169
170
171
172
173
174
175
176
  function valid_schedule($s) {
    $s2 = intval($s);
    if ($s2 != $s) return false;
    $r=false;
    foreach ($this->schedule() as $cs ) {
      if ($cs['unit'] == $s) return true;
    }
    return $r;
  }

177
178
179
180
  /*---------------------------------------------------------------------------*/
  /** hook for quota computation
   */
  function hook_quota_get() {
181
182
    global $cuid,$db,$err;
    $err->log("cron","alternc_get_quota");
183
    $q=Array("name"=>"cron", "description"=>_("Scheduled tasks"), "used"=>0);
184
185
    $db->query("select count(*) as cnt from cron where uid = $cuid;");
    if ($db->next_record()) {
186
        $q['used']=$db->f('cnt');
187
    }
188
    return $q;
189
190
191
  }


192
} /* Class cron */