[ PHPXref.com ] [ Generated: Sun Jul 20 16:25:17 2008 ] [ ActionApps 2.8.1b ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> cron.php3 (source)

   1  <?php
   2  /**
   3   * PHP cron: reads items from database and runs them. The item format is UNIX-cron-like.
   4   *
   5   * DOCUMENTATION: See cron.php3 documentation in {@link http://apc-aa.sourceforge.net/faq/#401 FAQ pages}.
   6   *
   7   * UNIX Cron documentation:
   8   *<pre>
   9   * Field          Allowed Values
  10   * -----          --------------
  11   * Minute         0-59
  12   * Hour           0-23
  13   * Day of Month   1-31
  14   * Month          1-12, jan, feb, mar, apr, may, jun, jul, aug, sep, oct,
  15   *                nov, dec
  16   * Day of Week    0-7, sun, mon, tue, wed, thu, fri, sat (0 and 7 are "sun")
  17   * </pre>
  18   * A field may be an asterisk (*), which indicates all values in the range are acceptable. Ranges of numbers are allowed, i.e. "2-5" or "8-11", and lists of numbers are allowed, i.e. "1,3,5" or "1,3,8-11". Step values can be represented as a sequence, i.e. "0-59/15", "1-31/3", or "* /2".
  19   *
  20   *  @package UserOutput
  21   *  @version $Id: cron.php3,v 1.13 2005/04/24 21:48:11 honzam Exp $
  22   *  @author Jakub Adamek <jakubadamek@ecn.cz>
  23   *  @copyright Econnect, Jakub Adamek, February 2002
  24  */
  25  /*
  26  Copyright (C) 2002 Association for Progressive Communications
  27  http://www.apc.org/
  28  
  29      This program is free software; you can redistribute it and/or modify
  30      it under the terms of the GNU General Public License as published by
  31      the Free Software Foundation; either version 2 of the License, or
  32      (at your option) any later version.
  33  
  34      This program is distributed in the hope that it will be useful,
  35      but WITHOUT ANY WARRANTY; without even the implied warranty of
  36      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37      GNU General Public License for more details.
  38  
  39      You should have received a copy of the GNU General Public License
  40      along with this program (LICENSE); if not, write to the Free Software
  41      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  42  */
  43  
  44  /** APC-AA configuration file */
  45  require_once  "./include/config.php3";
  46  /** Main include file for using session management function on page */
  47  require_once $GLOBALS['AA_INC_PATH']."locsess.php3";
  48  /** Defines class for inserting and updating database fields */
  49  require_once $GLOBALS['AA_INC_PATH']."varset.php3";
  50  
  51  
  52  /**
  53   * Runs the items from the cron table if their time has come.
  54   * You may call cron with specified timestamp to simulate the behavior.
  55   * DOCUMENTATION: See cron.php3 documentation in {@link http://apc-aa.sourceforge.net/faq/#401 FAQ pages}.
  56   * @param int $time the UNIX timestamp to simulate behaviour on a given date
  57   *                  - if omitted, uses the current time
  58   */
  59  function cron ($time = 0) {
  60  
  61      global $debug;
  62      if ($debug) {
  63          echo "<HTML><BODY>";
  64          echo "DEBUGGING. I don't run any script!<br>";
  65      }
  66  
  67      $translate["mon"] = array ("jan"=>1,"feb"=>2,"mar"=>3,"apr"=>4,"may"=>5,"jun"=>6,"jul"=>7,"aug"=>8,"sep"=>9,"oct"=>10,"nov"=>11,"dec"=>12);
  68      $translate["wday"] = array ("sun"=>0,"mon"=>1,"tue"=>2,"wed"=>3,"thu"=>4,"fri"=>5,"sat"=>6,"7"=>0);
  69  
  70      if (!$time) $time = time();
  71  
  72      $db = new DB_AA;
  73      $db_update = new DB_AA;
  74  
  75      $parts = array ("mon"=>12,"wday"=>7,"mday"=>31,"hours"=>24,"minutes"=>60);
  76      if ($debug) { echo "<B>".date("d.m.y H:i",$time)."</B></BR>"; }
  77  
  78      $db->query("SELECT * FROM cron");
  79      while ($db->next_record()) {
  80          /*  $nearest is the nearest of times on which an item should have run to now (nearest <= now)
  81              I don't consider last_run when looking for $nearest, but afterwards I run the script only
  82              if $nearest > $last_run.
  83  
  84              $nearest_part is value of current part of nearest */
  85          $nearest_part = 0;
  86  
  87          // when last_run is NULL, run item always
  88          if ($db->f("last_run")) {
  89              $last_run = getdate ($db->f("last_run"));
  90  
  91              // If an hour passed, I want to have minutes as 60+minutes etc.
  92              $now = getdate ($time);
  93              $now["mon"] += 12 * ($now["year"]-$last_run["year"]);
  94  
  95              // If the month(s) changed, find how many days it had
  96              $now_tst = mktime ($last_run["hours"],$last_run["minutes"],$last_run["seconds"],
  97                  $now["mon"],$last_run["mday"],$now["year"]);
  98              $days_in_months = ($now_tst - $db->f("last_run")) / (60 * 60 * 24);
  99  
 100              $now["wday"] += $days_in_months;
 101              $now["mday"] += $days_in_months;
 102              $now["hours"] += 24 * ($now["mday"]-$last_run["mday"]);
 103              $now["minutes"] += 60 * ($now["hours"]-$last_run["hours"]);
 104  
 105              $realnow = getdate ($time);
 106  
 107              if ($debug > 1) {
 108                  print_r ($last_run); echo "<br>";
 109                  print_r ($now); echo "<br>";
 110              }
 111  
 112              reset ($parts);
 113              while ((list($part) = each($parts)) && $nearest_part > -1) {
 114                  $now_part = $now[$part];
 115  
 116                  $value = $db->f($part);
 117                  if ($value == "*") {
 118                      $nearest[$part] = $realnow[$part];
 119                      continue;
 120                  }
 121                  $nearest_part = -1;
 122  
 123                  $tr = $translate[$part];
 124                  if (is_array($tr)) {
 125                      reset ($tr);
 126                      while (list($search,$replace) = each($tr))
 127                          $value = str_replace ($search,$replace,$value);
 128                  }
 129  
 130                  if (strstr($value,'/')) {
 131                      list($from,$to,$step) = split('[/-]',$value);
 132                      for ($i = $from; $i <= $to; $i += $step)
 133                          if ($i <= $now_part && $i > $nearest_part)
 134                              $nearest_part = $i;
 135                  }
 136                  else {
 137                      $ranges = explode(',',$value);
 138                      reset ($ranges);
 139                      while (list(,$range) = each ($ranges)) {
 140                          if (strstr ($range,"-")) {
 141                              list($from,$to) = explode('-',$value);
 142                                  for ($i = $from; $i <= $to; $i++)
 143                                  if ($i <= $now_part && $i > $nearest_part)
 144                                      $nearest_part = $i;
 145                          }
 146                          else if ($range <= $now_part && $range > $nearest_part)
 147                              $nearest_part = $range;
 148                      }
 149                  }
 150                  $nearest[$part] = $nearest_part;
 151              } // end of while
 152              $nearest_time = mktime ($nearest["hours"], $nearest["minutes"],0, $nearest["mon"], $nearest["mday"], $now["year"]);
 153              // mktime does not consider week day. We must shift it manually to the desired week day.
 154              // This will not work correct when combining week day with month or month day settings.
 155              if ($db->f("wday") != "*") {
 156                  $wday = getdate ($nearest_time);
 157                  $diff = (7 - $nearest["wday"] + $wday["wday"]) % 7;
 158                  $nearest_time -= $diff * 24 * 60 * 60;
 159              }
 160          } // end of if ($db->f("last_run"))
 161          else $nearest_time = time();
 162  
 163          $url = AA_INSTAL_URL.$db->f("script")."?".$db->f("params");
 164  
 165          if ($debug) {
 166              if ($nearest_part > -1 && $nearest_time > $db->f("last_run"))
 167                   echo "<b>$url</b> will be run<BR>";
 168              else echo "<font size=small>$url will be run on ".date( "d.m.y H:i",$nearest_time)." ($nearest_time)</font><BR>";
 169              //echo "Nearest time: "; print_r ($nearest);
 170              $db_update->query("UPDATE cron SET last_run=".$time." WHERE id=".$db->f("id"));
 171          }
 172          else if ($nearest_part > -1 && $nearest_time > $db->f("last_run")) {
 173              $db_update->query("UPDATE cron SET last_run=".$time." WHERE id=".$db->f("id"));
 174              fopen ($url,"r");
 175          }
 176      }
 177      if ($debug) echo "</BODY></HTML>";
 178  }
 179  
 180  // Use this for debug purposes
 181  /*
 182  $db = new DB_AA;
 183  $db->query("UPDATE cron SET last_run = NULL");
 184  $debug = 1;
 185  
 186  $time = time();
 187  for ($i = 0; $i < 50; $i++) {
 188      cron ($time);
 189      $time += 60*60*24;
 190  }
 191  */
 192  
 193  cron();
 194  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]