includes/functions.php
changeset 541 acb7e23b6ffa
parent 536 218a627eb53e
child 542 5841df0ab575
equal deleted inserted replaced
540:1e4b759da336 541:acb7e23b6ffa
  3342  */
  3342  */
  3343 
  3343 
  3344 function register_cron_task($func, $hour_interval = 24)
  3344 function register_cron_task($func, $hour_interval = 24)
  3345 {
  3345 {
  3346   global $cron_tasks;
  3346   global $cron_tasks;
       
  3347   $hour_interval = strval($hour_interval);
  3347   if ( !isset($cron_tasks[$hour_interval]) )
  3348   if ( !isset($cron_tasks[$hour_interval]) )
  3348     $cron_tasks[$hour_interval] = array();
  3349     $cron_tasks[$hour_interval] = array();
  3349   $cron_tasks[$hour_interval][] = $func;
  3350   $cron_tasks[$hour_interval][] = $func;
  3350 }
  3351 }
  3351 
  3352 
  3983  * @return string
  3984  * @return string
  3984  */
  3985  */
  3985 
  3986 
  3986 function make_avatar_url($user_id, $avi_type)
  3987 function make_avatar_url($user_id, $avi_type)
  3987 {
  3988 {
       
  3989   static $img_types = array(
       
  3990       'png' => IMAGE_TYPE_PNG,
       
  3991       'gif' => IMAGE_TYPE_GIF,
       
  3992       'jpg' => IMAGE_TYPE_JPG
       
  3993     );
       
  3994   
  3988   if ( !is_int($user_id) )
  3995   if ( !is_int($user_id) )
  3989     return false;
  3996     return false;
  3990   if ( !in_array($avi_type, array('png', 'gif', 'jpg')) )
  3997   if ( !isset($img_types[$avi_type]) )
  3991     return false;
  3998     return false;
  3992   return scriptPath . '/' . getConfig('avatar_directory') . '/' . $user_id . '.' . $avi_type;
  3999   $avi_relative_path = '/' . getConfig('avatar_directory') . '/' . $user_id . '.' . $avi_type;
       
  4000   if ( !file_exists(ENANO_ROOT . $avi_relative_path) )
       
  4001   {
       
  4002     return '';
       
  4003   }
       
  4004   
       
  4005   $img_type = $img_types[$avi_type];
       
  4006   
       
  4007   $dateline = @filemtime(ENANO_ROOT . $avi_relative_path);
       
  4008   $avi_id = pack('VVv', $dateline, $user_id, $img_type);
       
  4009   $avi_id = hexencode($avi_id, '', '');
       
  4010     
       
  4011   // return scriptPath . $avi_relative_path;
       
  4012   return makeUrlNS('Special', "Avatar/$avi_id");
  3993 }
  4013 }
  3994 
  4014 
  3995 /**
  4015 /**
  3996  * Determines an image's filetype based on its signature.
  4016  * Determines an image's filetype based on its signature.
  3997  * @param string Path to image file
  4017  * @param string Path to image file
  4310             }
  4330             }
  4311         }
  4331         }
  4312     }
  4332     }
  4313 }
  4333 }
  4314 
  4334 
       
  4335 /**
       
  4336  * Grabs and processes all rank information directly from the database.
       
  4337  */
       
  4338 
       
  4339 function fetch_rank_data()
       
  4340 {
       
  4341   global $db, $session, $paths, $template, $plugins; // Common objects
       
  4342   global $lang;
       
  4343   
       
  4344   $sql = $session->generate_rank_sql();
       
  4345   $q = $db->sql_query($sql);
       
  4346   if ( !$q )
       
  4347     $db->_die();
       
  4348   
       
  4349   $GLOBALS['user_ranks'] = array();
       
  4350   global $user_ranks;
       
  4351   
       
  4352   while ( $row = $db->fetchrow($q) )
       
  4353   {
       
  4354     $user_id = $row['user_id'];
       
  4355     $username = $row['username'];
       
  4356     $row = $session->calculate_user_rank($row);
       
  4357     $user_ranks[$username] =  $row;
       
  4358     $user_ranks[$user_id]  =& $user_ranks[$username];
       
  4359   }
       
  4360 }
       
  4361 
       
  4362 /**
       
  4363  * Caches the computed user rank information.
       
  4364  */
       
  4365 
       
  4366 function generate_ranks_cache()
       
  4367 {
       
  4368   global $db, $session, $paths, $template, $plugins; // Common objects
       
  4369   global $lang;
       
  4370   global $user_ranks;
       
  4371   
       
  4372   fetch_rank_data();
       
  4373   
       
  4374   $user_ranks_stripped = array();
       
  4375   foreach ( $user_ranks as $key => $value )
       
  4376   {
       
  4377     if ( is_int($key) )
       
  4378       $user_ranks_stripped[$key] = $value;
       
  4379   }
       
  4380   
       
  4381   $ranks_exported = "<?php\n\n// Automatically generated user rank cache.\nglobal \$user_ranks;\n" . '$user_ranks = ' . $lang->var_export_string($user_ranks_stripped) . ';';
       
  4382   $uid_map = array();
       
  4383   foreach ( $user_ranks as $id => $row )
       
  4384   {
       
  4385     if ( !is_int($id) )
       
  4386     {
       
  4387       $username = $id;
       
  4388       continue;
       
  4389     }
       
  4390     
       
  4391     $un_san = addslashes($username);
       
  4392     $ranks_exported .= "\n\$user_ranks['$un_san'] =& \$user_ranks[{$row['user_id']}];";
       
  4393   }
       
  4394   $ranks_exported .= "\n\ndefine('ENANO_RANKS_CACHE_LOADED', 1); \n?>";
       
  4395   
       
  4396   // open ranks cache file
       
  4397   $fh = @fopen( ENANO_ROOT . '/cache/cache_ranks.php', 'w' );
       
  4398   if ( !$fh )
       
  4399     return false;
       
  4400   fwrite($fh, $ranks_exported);
       
  4401   fclose($fh);
       
  4402 }
       
  4403 
       
  4404 /**
       
  4405  * Loads the rank data, first attempting the cache file and then the database.
       
  4406  */
       
  4407 
       
  4408 function load_rank_data()
       
  4409 {
       
  4410   if ( file_exists( ENANO_ROOT . '/cache/cache_ranks.php' ) )
       
  4411   {
       
  4412     @include(ENANO_ROOT . '/cache/cache_ranks.php');
       
  4413   }
       
  4414   if ( !defined('ENANO_RANKS_CACHE_LOADED') )
       
  4415   {
       
  4416     fetch_rank_data();
       
  4417   }
       
  4418 }
       
  4419 
  4315 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  4420 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  4316 
  4421 
  4317 ?>
  4422 ?>