Added a cron framework. Currently tasks will not be run; will implement into templates in next commit
authorDan
Fri, 19 Oct 2007 21:39:33 -0400
changeset 191 3dbe848431b0
parent 190 e858bacb5cfa
child 192 9237767a23ae
Added a cron framework. Currently tasks will not be run; will implement into templates in next commit
cron.php
includes/common.php
includes/functions.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cron.php	Fri Oct 19 21:39:33 2007 -0400
@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
+ * Version 1.0.2 (Coblynau)
+ * Copyright (C) 2006-2007 Dan Fuhry
+ *
+ * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ * 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 details.
+ */
+
+//
+// cron.php - Maintenance tasks that should be run periodically
+//
+
+// The cron script is triggered by way of an image. This is a 1x1px transparent GIF.
+define('ENANO_GIF_SPACER', base64_decode('R0lGODlhAQABAIAAAP///////yH+FUNyZWF0ZWQgd2l0aCBUaGUgR0lNUAAh+QQBCgABACwAAAAAAQABAAACAkwBADs='));
+
+// Don't need a page to load, all we should need is the Enano API
+$_GET['title'] = 'Enano:Cron';
+require('includes/common.php');
+
+global $db, $session, $paths, $template, $plugins; // Common objects
+
+// Hope now that plugins are loaded :-)
+$last_run = ( $x = getConfig('cron_last_run') ) ? $x : 0;
+$time = strval(time());
+setConfig('cron_last_run', $time);
+
+global $cron_tasks;
+
+foreach ( $cron_tasks as $interval => $tasks )
+{
+  $last_run_threshold = time() - ( $interval * 3600 );
+  if ( $last_run_threshold >= $last_run )
+  {
+    foreach ( $tasks as $task )
+    {
+      @call_user_func($task);
+    }
+  }
+}
+
+header('Pragma: no-cache');
+header('Cache-control: no-cache');
+header('Expires: Thu, 1 Jan 1970 00:00:01 GMT');
+header('Content-type: image/gif');
+
+echo ENANO_GIF_SPACER;
+
+?>
--- a/includes/common.php	Fri Oct 19 21:07:54 2007 -0400
+++ b/includes/common.php	Fri Oct 19 21:39:33 2007 -0400
@@ -68,6 +68,9 @@
 if ( file_exists( ENANO_ROOT . '/_nightly.php') )
   require(ENANO_ROOT.'/_nightly.php');
 
+// List of scheduled tasks
+$cron_tasks = array();
+
 // Start including files. LOTS of files. Yeah!
 require_once(ENANO_ROOT.'/includes/constants.php');
 dc_here('Enano CMS '.$version.' (dev) - debug window<br />Powered by debugConsole');
--- a/includes/functions.php	Fri Oct 19 21:07:54 2007 -0400
+++ b/includes/functions.php	Fri Oct 19 21:39:33 2007 -0400
@@ -3168,6 +3168,20 @@
   return $score;
 }
 
+/**
+ * Registers a task that will be run every X hours. Scheduled tasks should always be scheduled at runtime - they are not stored in the DB.
+ * @param string Function name to call, or array(object, string method)
+ * @param int Interval between runs, in hours. Defaults to 24.
+ */
+
+function register_cron_task($func, $hour_interval = 24)
+{
+  global $cron_tasks;
+  if ( !isset($cron_tasks[$hour_interval]) )
+    $cron_tasks[$hour_interval] = array();
+  $cron_tasks[$hour_interval][] = $func;
+}
+
 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
 
 ?>