| 
<?php
/*
 Bluetrait 2.0 Default Tasks
 Michael Dale Copyright 2008
 */
 
 if (!defined('BT_ROOT')) exit;
 
 //html filter
 bt_add_task(BT_PLUGIN_NAME, 'common_loaded', 'bt_load_kses');
 
 //404 handler
 bt_add_task(BT_PLUGIN_NAME, '404', 'bt_404');
 
 //Cron system
 bt_add_task(BT_PLUGIN_NAME, 'shutdown', 'bt_run_cron');
 
 //default cron tasks
 bt_add_task(BT_PLUGIN_NAME, 'cron_every_hour', 'bt_future_post');
 bt_add_task(BT_PLUGIN_NAME, 'cron_every_hour', 'bt_session_garbage_collection');
 bt_add_task(BT_PLUGIN_NAME, 'cron_every_day', 'bt_update_check');
 bt_add_task(BT_PLUGIN_NAME, 'cron_every_day', 'bt_update_check_plugins');
 bt_add_task(BT_PLUGIN_NAME, 'cron_every_month', 'bt_monthly_maintenance');
 
 //text editor
 bt_add_task(BT_PLUGIN_NAME, 'admin_text_editor', 'bt_text_editor');
 
 /*
 Default Themed Content Support
 */
 //blog
 bt_add_task(BT_PLUGIN_NAME, 'theme_type_blog', 'bt_theme_handle');
 //404
 bt_add_task(BT_PLUGIN_NAME, 'theme_type_404', 'bt_theme_handle');
 //cms
 bt_add_task(BT_PLUGIN_NAME, 'theme_type_cms', 'bt_theme_handle');
 
 function bt_load_kses() {
 /*
 Note: Filters for kses are stored in default-filters.php
 */
 include(BT_ROOT . BT_INCLUDE . '/kses.functions.php');
 include(BT_ROOT . BT_INCLUDE . '/kses.tasks.php');
 
 return true;
 }
 
 function bt_404() {
 global $bt_content_identifier;
 
 bt_set_header('HTTP/1.0 404 Not Found');
 bt_set_404();
 $bt_content_identifier['theme_type'] = '404';
 
 return true;
 }
 
 function bt_run_cron() {
 
 if (defined('BT_RUNNING_CRON')) return false;
 if (bt_datetime() < bt_get_config('next_cron_run')) return false;
 
 bt_set_config('next_cron_run', bt_datetime(3600));
 
 $domain = bt_get_config('domain');
 
 //IPv6 not working under vista
 //$domain = '127.0.0.1';
 
 if (bt_get_config('https')) {
 $domain = 'ssl://' . $domain;
 }
 
 $socket = @fsockopen($domain, bt_get_config('port_number'), $errno, $errstr, 0.01);
 
 if ($socket) {
 $request =
 "GET " . bt_get_config('script_path') . "/bt-cron.php HTTP/1.0\r\n"
 . "Host: " . bt_get_config('domain') . "\r\n"
 . "User-Agent: Bluetrait/" . bt_get_config('program_version')
 . "\r\n\r\n";
 @fwrite($socket, $request);
 @fclose($socket);
 return true;
 }
 else {
 trigger_error('Unable to start cron', E_USER_ERROR);
 return false;
 }
 }
 
 function bt_future_post() {
 global $bt_db, $bt_tb;
 
 $query = "UPDATE $bt_tb->posts SET post_type = 'published' WHERE post_type = 'future' AND post_date <= ?";
 
 $now = bt_datetime();
 $stmt = $bt_db->prepare($query);
 $stmt->bindParam(1, $now);
 
 try {
 $stmt->execute();
 }
 catch (Exception $e) {
 bt_die($e->getMessage());
 }
 }
 
 function bt_update_check() {
 
 $options = array(
 'http' => array(
 'user_agent'    => 'Bluetrait/' . bt_get_config('program_version'),
 'timeout'       => 5
 )
 );
 
 $context = stream_context_create($options);
 $url = 'http://update.bluetrait.org/bluetrait2/update_check.php';
 $update_feed = file_get_contents($url, false, $context);
 if ($update_feed) {
 bt_set_config('last_update_response', $update_feed);
 return true;
 }
 else {
 trigger_error('Unable to contact update server', E_USER_WARNING);
 return false;
 }
 
 }
 
 function bt_update_check_plugins() {
 
 $plugins = bt_get_all_plugins();
 
 $options = array(
 'http' => array(
 'user_agent'    => 'Bluetrait/' . bt_get_config('program_version'),
 'timeout'       => 5
 )
 );
 $plugin_update_array = bt_get_config('plugin_update_data');
 foreach ($plugins as $plugin) {
 if (!empty($plugin['plugin_update_checker_url'])) {
 $context = stream_context_create($options);
 $url = $plugin['plugin_update_checker_url'];
 $update_feed = file_get_contents($url, false, $context);
 if ($update_feed) {
 $plugin_update_array[$plugin['plugin_file_name']] = $update_feed;
 }
 else {
 trigger_error('Unable to contact update server for plugin "' . bt_htmlentities($plugin['plugin_file_name']) . '"', E_USER_WARNING);
 }
 }
 }
 bt_set_config('plugin_update_data', $plugin_update_array);
 return true;
 }
 
 function bt_monthly_maintenance() {
 
 //optimise tables
 bt_optimise_tables();
 }
 
 function bt_session_garbage_collection() {
 global $bt_session;
 
 //if (!defined('BT_RUNNING_CRON')) {
 $bt_session->gc();
 //}
 
 }
 
 function bt_theme_handle() {
 global $bt_post_array, $bt_db, $bt_tb, $bt_error, $bt_post, $bt_comment_array, $bt_input_error, $bt_comment;
 
 include(BT_ROOT . BT_CONTENT . '/bt-themes/' . BT_CURRENT_THEME . '/' . BT_CURRENT_THEME_TYPE . '/index.php');
 }
 
 function bt_text_editor($array) {
 
 $name = $array['name'];
 $value = $array['value'];
 ?>
 <textarea name="<?php echo $name; ?>" style="width: 100%" rows="20" id="<?php echo $name; ?>"><?php echo $value; ?></textarea>
 <?php
 }
 
 ?>
 |