Creating cron for phpBB board

PHP coding talk.

Moderator: Moderators

Creating cron for phpBB board

Postby Stryker on Thu Mar 01, 2007 10:02 pm

Hello, i am using this as cron on phpbb board.
[code filename="cron.php"]<?php

define('IN_CRON', true);
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'common.php');

$cron_type = request_var('cron_type', '');
$use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false;

// Output transparent gif
header('Cache-Control: no-cache');
header('Content-type: image/gif');
header('Content-length: 43');

echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');

flush();

/**
* Run cron-like action
* Real cron-based layer will be introduced in 3.2
*/
switch ($cron_type)
{

case 'stats':
if (time() - '600' > $board_config['last_stats_run'])
{

$total_posts = get_db_stat('postcount');
$total_users = get_db_stat('usercount');


$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '" . str_replace("\'", "''", $total_posts) . "'
WHERE config_name = 'total_posts'";
if( !$db->sql_query($sql) )
{
die("Failed to update posts stat");
}
$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '" . str_replace("\'", "''", $total_users) . "'
WHERE config_name = 'total_users'";
if( !$db->sql_query($sql) )
{
die("Failed to update users stat");
}


// Set last run time
$time = time();

$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '" . str_replace("\'", "''", $time) . "'
WHERE config_name = 'last_stats_run'";
if( !$db->sql_query($sql) )
{
die("Failed to update last stats run time");
}
@unlink('cache/config_table.php'); // This is my config table cache file
}
break;

}


// Unloading cache and closing db after having done the dirty work.
if ($use_shutdown_function)
{
register_shutdown_function('garbage_collection');
}
else
{
garbage_collection();
}

exit;

?>[/code]

i have changed this in index.php:
Code: Select all
//$total_posts = get_db_stat('postcount'); // These have been moved to
//$total_users = get_db_stat('usercount'); // board_config by cron script
$total_posts = $board_config['total_posts'];
$total_users = $board_config['total_users'];


i have added to DB this:
Code: Select all
INSERT INTO phpbb_config (config_name, config_value) VALUES ('last_stats_run', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('total_posts', ''), ('total_users', '');


in page_tail.php i have added this above "$template->pparse('overall_footer');":
Code: Select all
   // Call cron-type script
   if (!defined('IN_CRON'))
   {
      $cron_type = '';
   
      if (time() - '600' > $board_config['last_stats_run'])
      {
         // Do stats
         $cron_type = 'stats';
      }
      cron_type)
      {
         $template->assign_var('RUN_CRON_TASK', '<img src="' . $phpbb_root_path . 'cron.' . $phpEx . '?cron_type=' . $cron_type . '" width="1" height="1" />');
      }
   }


i have added this to the includes/functions.php:
Code: Select all
/**
* set_var
*
* Set variable, used by {@link request_var the request_var function}
*
* @access: private
*/
function set_var(&$result, $var, $type, $multibyte = false)
{
   settype($var, $type);
   $result = $var;

   if ($type == 'string')
   {
      $result = trim(htmlspecialchars(str_replace(array("\r\n", "\r"), array("\n", "\n"), $result)));
      $result = (STRIP) ? stripslashes($result) : $result;

      // Check for possible multibyte characters to save a preg_replace call if nothing is in there...
      if ($multibyte && strpos($result, '&amp;#') !== false)
      {
         $result = preg_replace('#&amp;(\#[0-9]+;)#', '&\1', $result);
      }
   }
}



/**
* request_var
*
* Used to get passed variable
*/
function request_var($var_name, $default, $multibyte = false)
{
   if (!isset($_REQUEST[$var_name]) || (is_array($_REQUEST[$var_name]) && !is_array($default)) || (is_array($default) && !is_array($_REQUEST[$var_name])))
   {
      return (is_array($default)) ? array() : $default;
   }

   $var = $_REQUEST[$var_name];
   if (!is_array($default))
   {
      $type = gettype($default);
   }
   else
   {
      list($key_type, $type) = each($default);
      $type = gettype($type);
      $key_type = gettype($key_type);
   }

   if (is_array($var))
   {
      $_var = $var;
      $var = array();

      foreach ($_var as $k => $v)
      {
         if (is_array($v))
         {
            foreach ($v as $_k => $_v)
            {
               set_var($k, $k, $key_type);
               set_var($_k, $_k, $key_type);
               set_var($var[$k][$_k], $_v, $type, $multibyte);
            }
         }
         else
         {
            set_var($k, $k, $key_type);
            set_var($var[$k], $v, $type, $multibyte);
         }
      }
   }
   else
   {
      set_var($var, $var, $type, $multibyte);
   }

   return $var;
}


and the last step, i have added this into the overall_footer.tpl:
Code: Select all
{RUN_CRON_TASK}


My problem is, that this cron is not working, if i do all this steps, the index stats for total posts and users are at "0" :( any idea what could be wrong please?
Last edited by Stryker on Mon Mar 19, 2007 2:31 pm, edited 1 time in total.
User avatar
Stryker
Registered User
Registered User
 
Posts: 1318
Joined: Mon Aug 29, 2005 7:54 am
Location: ------------

Postby Stryker on Fri Mar 02, 2007 1:59 pm

Hello,

please any idea what i have wrong here?
User avatar
Stryker
Registered User
Registered User
 
Posts: 1318
Joined: Mon Aug 29, 2005 7:54 am
Location: ------------

Postby Stryker on Sat Mar 03, 2007 6:31 am

bumP?
User avatar
Stryker
Registered User
Registered User
 
Posts: 1318
Joined: Mon Aug 29, 2005 7:54 am
Location: ------------

Postby Stryker on Sun Mar 04, 2007 7:14 am

any ide what i have wrong? the cron.php file is OK, if i look on the source code on the index of forum, there is "<img src="./cron.php?cron_type=stats" width="1" height="1" />" i dont know why it is not adding stats from DB to that created lines(total posts, total users) in config table

if so, does somebodY know other cron for caching forums stats that are on index please?
User avatar
Stryker
Registered User
Registered User
 
Posts: 1318
Joined: Mon Aug 29, 2005 7:54 am
Location: ------------

Postby Stryker on Mon Mar 19, 2007 2:23 pm

bumping... :oops:
User avatar
Stryker
Registered User
Registered User
 
Posts: 1318
Joined: Mon Aug 29, 2005 7:54 am
Location: ------------

Postby kvfb97 on Mon Mar 19, 2007 3:28 pm

Greetings,

Check your DB to check if it is putting the values into it.
Tip: Don't argue with idiots, they'll drag you down to their level and beat you with experience.
User avatar
kvfb97
Registered User
Registered User
 
Posts: 1774
Joined: Mon Aug 02, 2004 8:18 am
Location: Northern Ireland, UK

Postby Stryker on Tue Mar 20, 2007 2:25 pm

i was waiting few minutes adn there was still only "0"...
User avatar
Stryker
Registered User
Registered User
 
Posts: 1318
Joined: Mon Aug 29, 2005 7:54 am
Location: ------------

Postby Stryker on Thu Mar 22, 2007 7:41 am

i dont know why but this cron is not putting every 10 minutes new values to that new lines i have created in config table..
User avatar
Stryker
Registered User
Registered User
 
Posts: 1318
Joined: Mon Aug 29, 2005 7:54 am
Location: ------------


Return to PHP Programming

Who is online

Users browsing this forum: No registered users and 4 guests

cron