load_vars(base64_decode(preg_replace("/\s/", '', $virtual_files))); return $virtual_file_data->get_var($file); } #--------------------------------- General Functions function param($variable, $default="") { $form_vars = array_merge($_GET, $_POST); if(array_key_exists($variable, $form_vars) == false){ return $default; } if($form_vars[$variable] == ''){ return $default; } if(is_array($form_vars[$variable])){ return $form_vars[$variable]; } return stripslashes($form_vars[$variable]); } function safe_html($data){ $data = preg_replace("/script/is", "_script_", $data); $data = preg_replace("/\\/s", ">", $data); } #--------------------------------- Data wiz class data_wiz { function data_wiz(){ $this->vars = array(); } function set_var($key, $value){ $this->vars[$key] = $value; } function get_var($key, $default=''){ if(isset($this->vars[$key])){ return $this->vars[$key]; } return $default; } function dump_vars() { return $this->dump_vars_recursive($this->vars); } function dump_vars_recursive($data) { if(is_array($data)){ $out_array = array(); foreach($data as $key => $value){ $key = urlencode($key); $value = $this->dump_vars_recursive($value); $pair = urlencode("$key\n$value"); array_push($out_array, $pair); } return 'a' . urlencode(implode("\n", $out_array)); } else { return 's' . urlencode($data); } } function load_vars($data){ if($data == ''){ $this->vars = array(); } else { $this->vars = array(); $this->vars = $this->load_vars_recursive($data); } } function load_vars_recursive($data){ //get type preg_match("/^(.)(.*)/", $data, $matches); $type = $matches[1]; $data = $matches[2]; if($type == 'a'){ if($data == ''){ return array(); } $out_array = array(); //unfold lines $data = urldecode($data); //get array $data_array = preg_split("/\n/s", $data); foreach($data_array as $pair){ //unfold pair $pair = urldecode($pair); //get array $pair_array = preg_split("/\n/s", $pair); $key = urldecode($pair_array[0]); $value = $this->load_vars_recursive($pair_array[1]); $out_array[$key] = $value; } return $out_array; } else { return urldecode($data); } } } #--------------------------------- Template wiz class template_wiz { function template_wiz($start_file = "", $start_section = ""){ $this->file_cache = array(); $this->section_cache = array(); $this->template = ""; $this->current_file = $start_file; $this->load_template($start_file, $start_section); $this->var_list = array(); $this->function_list = array(); $this->mode_list = array(); $this->pre_list = array(); $this->post_list = array(); $this->import_function('this->param', 'param'); $this->import_function('this->server', 'server'); $this->import_function('this->safe_var', 'safe_var'); $this->import_function('this->html_var', 'html_var'); $this->import_function('this->text_padding', 'text_padding'); $this->import_function('this->dynamic_function', 'function'); } function html_char_sub($char) { if($char == '<') { return '<'; } if($char == '>') { return '>'; } if($char == "\n") { return '
'; } } function html_var($args){ if(sizeof($args) > 0){ $name = $args[0]; } else { $this->template_wiz_error("No variable was specified in function 'html_var'."); } if(array_key_exists($name, $this->var_list) == false){ $this->template_wiz_error("no such var \"$name\" has been imported (called from function 'html_var')"); } return preg_replace("/([\\n\\<\\>])/es", "\$this->html_char_sub('\\1')", $this->var_list[$name]); } function text_padding($args){ $key = $args[0]; $max_size = $args[1]; $value = $this->var_list[$key]; if(strlen($value) > $max_size){ $value = substr($value, 0, $max_size - 3) . '...'; } else { $value = str_pad($value, $max_size, " "); } return $value; } function safe_var($args){ if(sizeof($args) > 0){ $name = $args[0]; $max_length = -1; } else { $this->template_wiz_error("No variable was specified in function 'safe_var'."); } if(sizeof($args) > 1){ $max_length = $args[1]; } if(array_key_exists($name, $this->var_list) == false){ $this->template_wiz_error("no such var \"$name\" has been imported (called from function 'safe_var')"); } $out = $this->var_list[$name]; if((strlen($out) > $max_length)&&($max_length > 0)){ $out = substr($out, 0, $max_length) . '...'; } $out = preg_replace("/php/is", "php", $out); $out = preg_replace("/\\/s", ">", $out); } function dynamic_function($args){ if(sizeof($args) > 0){ $function = array_shift($args); } else { $this->template_wiz_error("No function name was specified in function 'function'."); } //make sure function has been imported if(array_key_exists($function, $this->function_list) == false){ $this->template_wiz_error("no such function \"$function\" has been imported (called from function 'function')"); } //substite each variable and make sure each one has been imported $sub_vars = array(); foreach($args as $value){ if(array_key_exists($value, $this->var_list) == false){ $this->template_wiz_error("no such var \"$value\" has been imported (called from function 'function')"); } array_push($sub_vars, $this->var_list[$value]); } //actually run the function if(preg_match("/^(\\S+)->(\\S+)\$/", $function, $results)){ $object_name = $results[1]; $function_name = $results[2]; return $$object_name->$function_name($sub_vars); } else { $function_name = $function; return $function_name($sub_vars); } } function server($args){ $name = ''; $default= ''; if(sizeof($args) > 0){ $name = $args[0]; } else { return ''; } if(sizeof($args) > 1){ $default = $args[1]; } if(array_key_exists($name, $_SERVER) == false){ return $default; } $value = $_SERVER[$name]; if($value == ''){ return $default; } return $value; } function param($args){ $name = ''; $default= ''; if(sizeof($args) > 0){ $name = $args[0]; } else { return ''; } if(sizeof($args) > 1){ $default = $args[1]; } $form_vars = array_merge($_GET, $_POST); if(array_key_exists($name, $form_vars) == false){ return $default; } $value = $form_vars[$name]; if($value == ''){ return $default; } $value = stripslashes($value); $value = preg_replace("/([\\<\\>])/es", "\$this->safe_char_sub('\\1')", $value); return $value; } function load_template($file, $section=""){ $this->load_file($file); $this->load_section($section); } function load_file($file){ $vfile = get_virtual_file($file); if($vfile != ''){ $this->template = $vfile; return ''; } if(strcmp($file, "") == 0){ $this->template = ""; return ''; } if(is_file($file) == false){ $this->template_wiz_error("file \"$file\" does not exist."); } if(array_key_exists($file, $this->file_cache) == false){ $fp = fopen($file, "rb"); $this->file_cache[$file] = fread($fp, filesize($file)); fclose($fp); } $this->current_file = $file; $this->template = $this->file_cache[$file]; } function preload_sections(){ if(array_key_exists($this->current_file, $this->section_cache) == false){ $file_lines = preg_split("/\\n/", $this->file_cache[$this->current_file]); $tag_line = ""; $section_name = ""; $section_data = array(); $sections_out = array(); foreach($file_lines as $line){ if(preg_match("/^([^\\s\\:]+)\\:\\s*\$/s", $line, $tag_line)){ if(strcmp($section_name, "") != 0){ $sections_out[$section_name] = implode("\n", $section_data); } $section_name = $tag_line[1]; $section_data = array(); } else { array_push($section_data, $line); } if(strcmp($section_name, "") != 0){ $sections_out[$section_name] = implode("\n", $section_data); } $this->section_cache[$this->current_file] = $sections_out; } } } function load_section($section){ if(strcmp($this->current_file, "") == 0){ return; } if(strcmp($section, "") == 0){ return; } $this->preload_sections(); if(array_key_exists($section, $this->section_cache[$this->current_file])){ $this->template = $this->section_cache[$this->current_file][$section]; }else { $this->template_wiz_error("no such section \"$section\" in file \"$this->current_file\"."); } } function set_template($template_data){ $this->template = $template_data; } function parse_args($args){ $args = stripslashes($args); $args = preg_replace("/\\&([^\\;\\s$]{1,4})\\;/se", "\$this->decode_html_special_char('\\1')", $args); $in_double_quote = false; $in_single_quote = false; $current_var_array = array(); $out = array(); for($x = 0; $x < strlen($args); $x++){ $current_char = substr($args, $x, 1); if((strcmp($current_char, "\"") == 0)&&($in_double_quote == false)&&($in_single_quote == false)){ $in_double_quote = true; }elseif((strcmp($current_char, "'") == 0)&&($in_double_quote == false)&&($in_single_quote == false)){ $in_single_quote = true; }elseif((strcmp($current_char, ",") == 0)&&($in_double_quote == false)&&($in_single_quote == false)){ array_push($out, implode('', $current_var_array)); $current_var_array = array(); }elseif((strcmp($current_char, "\"") == 0)&&($in_double_quote == true)&&($in_single_quote == false)){ $in_double_quote = false; }elseif((strcmp($current_char, "'") == 0)&&($in_double_quote == false)&&($in_single_quote == true)){ $in_single_quote = false; }elseif((strcmp($current_char, "\\") == 0)&&($in_double_quote == true)){ array_push($current_var_array, $this->decode_escape_char(substr($args, $x + 1, 1))); $x++; }elseif((strcmp($current_char, " ") == 0)&&($in_double_quote == false)&&($in_single_quote == false)){ //do nothing }else{ array_push($current_var_array, $current_char); } } if(sizeof($current_var_array) > 0){ array_push($out, implode('', $current_var_array)); } return $out; } function decode_html_special_char($data){ if(strcmp(strtolower($data), "lt") == 0){ return '<'; } if(strcmp(strtolower($data), "gt") == 0){ return '>'; } if(strcmp(strtolower($data), "amp") == 0){ return '&'; } if(strcmp(strtolower($data), "quot") == 0){ return '"'; } return preg_replace("/^\\#(\\d\\d\\d)/se", "chr(intval('\\1'))", $data); return ''; } function decode_escape_char($char) { if(strcmp(strtolower($char), "n") == 0){ return "\n"; } if(strcmp(strtolower($char), "d") == 0){ return "\$"; } if(strcmp(strtolower($char), "h") == 0){ return "#"; } if(strcmp(strtolower($char), "[") == 0){ return "("; } if(strcmp(strtolower($char), "]") == 0){ return ")"; } return $char; } function html_unescape($data){ #convert normal html special chars into plain text $data = preg_replace("/\\&([^\\;\\s\\$]{1,4})\\;/se", "\$this->get_html_special_char('\\1')", $data); return preg_replace("/\\\\(.)/se", "\$this->get_escape_char('\\1')", $data); } function run_function($function_name, $function_args) { if(array_key_exists( $function_name, $this->function_list) == false){ $this->template_wiz_error("no such function \"$function_name\" has been imported"); } $function_args_array = $this->parse_args($function_args); if(preg_match("/^(\\S+)->(\\S+)\$/", $this->function_list[$function_name], $results)){ $object_name = $results[1]; $function_name = $results[2]; if($object_name == 'this'){ return $this->$function_name($function_args_array); } else { return $$object_name->$function_name($function_args_array); } } else { $function_name = $this->function_list[$function_name]; return $function_name($function_args_array); } } function get_var($name) { if(array_key_exists($name, $this->var_list)){ return $this->var_list[$name]; } else { $this->template_wiz_error("no such var \"$name\" has been imported"); } } function run_sub($var1, $var2="", $var3=""){ if(strcmp($var3, '') != 0){ return $this->get_var($var3); } else { return $this->run_function($var1, $var2); } } function run(){ $temp_template = $this->template; $temp_template = $this->run_pre($temp_template); $temp_template = preg_replace("/\\$([^\\(\\$]+)\\(([^\\)\\$]*)\\)\\$|\\$([^\\$\\s]+)([^\\$]*)\\$/se", "\$this->run_sub('\\1', '\\2', '\\3')", $temp_template); return $this->run_post($temp_template); } function template_wiz_error($error){ echo "
template_wiz error: $error
"; exit; } function run_modes(){ $form_vars = array_merge($_GET, $_POST); if(array_key_exists('mode', $form_vars) == false){ return ''; } if(array_key_exists($form_vars['mode'], $this->mode_list) == false){ $this->template_wiz_error("no such mode \"{$form_vars['mode']}\" has been imported."); } $function = $this->mode_list[$form_vars['mode']]; if(preg_match("/^(\\S+)->(\\S+)\$/", $function, $results)){ $object_name = $results[1]; $function_name = $results[2]; if($object_name == 'this'){ $this->$function_name($data); } else { $$object_name->$function_name($data); } } else { $function_name = $function; $function_name(); } } function run_pre($data) { foreach($this->pre_list as $function){ if(preg_match("/^(\\S+)->(\\S+)\$/", $function, $results)){ $object_name = $results[1]; $function_name = $results[2]; if($object_name == 'this'){ $data = $this->$function_name($data); } else { $data = $$object_name->$function_name($data); } } else { $function_name = $function; $data = $function_name($data); } } return $data; } function run_post($data){ foreach($this->post_list as $function){ if(preg_match("/^(\\S+)->(\\S+)\$/", $function, $results)){ $object_name = $results[1]; $function_name = $results[2]; if($object_name == 'this'){ $data = $this->$function_name($data); } else { $data = $$object_name->$function_name($data); } } else { $function_name = $function; $data = $function_name($data); } } return $data; } function import_var($var_name, $var_value){ $this->var_list[$var_name] = $var_value; } function import_function($function_name, $function_alias=""){ if(strcmp($function_alias, "") == 0){ $function_alias = $function_name; } $this->function_list[$function_alias] = $function_name; } function import_mode($function_name, $function_alias=""){ if(strcmp($function_alias, "") == 0){ $function_alias = $function_name; } $this->mode_list[$function_alias] = $function_name; } function import_pre($function_name){ array_push($this->pre_list, $function_name); } function import_post($function_name){ array_push($this->post_list, $function_name); } function import_object($tw_object){ foreach($tw_object->function_list as $function_alias){ $this->function_list[$function_alias] = $tw_object->function_list[$function_alias]; } foreach($tw_object->mode_list as $function_alias){ $this->mode_list[$function_alias] = $tw_object->mode_list[$function_alias]; } } function rip_body(){ if(preg_match("/]*)>(.*)<\\/body>/is", $this->template, $matches)){ $this->template = $matches[2]; } elseif(preg_match("/]*)>(.*)<\\/html>/is", $this->template, $matches)){ $this->template = $matches[2]; } } function javascript_charhex($char){ $char_hex = dechex(ord(stripslashes($char))); if(strlen($char_hex) < 2){ $char_hex = '0' . $char_hex; } return "\\x$char_hex"; } function javascript_string_escape($string){ return preg_replace("/(.)/se", '$this->javascript_charhex("\\1")', $string); } function alert($message){ $message = $this->javascript_string_escape($message); $script = "\n"; if(preg_match("/(]>)/", $this->template)){ $this->template = preg_replace("/(]>)/", $script, $this->template); } else { $this->template = $this->template . $script; } } function sections(){ $this->preload_sections(); return array_keys($this->section_cache[$this->current_file]); } } function chat_login(){ $this_page = new template_wiz("chat_template.htm"); $main = new template_wiz("chat_login.htm"); $main->rip_body(); $try = param('try', 'n'); $chat_messages = new data_wiz(); if($try == 'n'){ $logged_in = true; //make sure there is a user name password set if(isset($_COOKIE['chat_username'])&&isset($_COOKIE['chat_password'])){ $chat_password = md5($_COOKIE['chat_password']); $chat_username = $_COOKIE['chat_username']; $fh = fopen("chat_messages.txt", 'r+b'); flock($fh, LOCK_EX); fseek($fh, 0, SEEK_END); $filesize = ftell($fh); rewind($fh); $current_data = ""; if($filesize > 0){ $current_data = fread($fh, $filesize); } flock($fh, LOCK_UN); $chat_messages->load_vars($current_data); $users = $chat_messages->get_var('users', array()); if(isset($users[$chat_username])) { $user_array = $users[$chat_username]; if($user_array['password'] != $chat_password){ $logged_in = false; } } else { $logged_in = false; } } else { $logged_in = false; } if(!$logged_in){ $this_page->import_var('chat', $main->run()); echo $this_page->run(); exit; } } else { $chat_username = trim(param('chat_username')); if($chat_username != ''){ if(!preg_match("/\s/", $chat_username)){ $chat_password = floor(rand(0, 999)) . floor(rand(0, 999)) . floor(rand(0, 999)) . floor(rand(0, 999)) . floor(rand(0, 999)); $fh = fopen("chat_messages.txt", 'r+b'); flock($fh, LOCK_EX); fseek($fh, 0, SEEK_END); $filesize = ftell($fh); rewind($fh); $current_data = ""; if($filesize > 0){ $current_data = fread($fh, $filesize); } rewind($fh); $chat_messages->load_vars($current_data); $users = $chat_messages->get_var('users', array()); $current_message_id = $chat_messages->get_var('current_message_id', 0); $message_list = $chat_messages->get_var('message_list', array()); if(!isset($users[$chat_username])){ $user_array = array(); $user_array['password'] = md5($chat_password); $user_array['time'] = time(); $users[$chat_username] = $user_array; $chat_messages->set_var('users', $users); fwrite($fh, $chat_messages->dump_vars()); fclose($fh); setcookie('chat_username', $chat_username); setcookie('chat_password', $chat_password); header("Location: {$_SERVER['PHP_SELF']}"); exit; } else { $this_page->alert("There is already a user logged with this name - please try again"); $this_page->import_var('chat', $main->run()); echo $this_page->run(); exit; } } else { $this_page->alert("Your user name can't contain spaces - please try again"); $this_page->import_var('chat', $main->run()); echo $this_page->run(); exit; } } else { $this_page->alert("You did not give a user name - please try again"); $this_page->import_var('chat', $main->run()); echo $this_page->run(); exit; } } } function chat_main(){ chat_login(); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); $this_page = new template_wiz("chat_template.htm"); $main = new template_wiz("chat.htm"); $main->rip_body(); $this_page->import_var('chat', $main->run()); echo $this_page->run(); exit; } function javascript_charhex($char){ $char_hex = dechex(ord(stripslashes($char))); if(strlen($char_hex) < 2){ $char_hex = '0' . $char_hex; } return "\\x$char_hex"; } function javascript_string_escape($string){ return preg_replace("/(.)/se", 'javascript_charhex("\\1")', $string); } function chat_js_terminal(){ header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); global $check_rate; $old_message_id = param('old_message_id', -1); $fh = fopen("chat_messages.txt", 'r+b'); $chat_messages = new data_wiz(); if($old_message_id == -1){ //initialise vars flock($fh, LOCK_EX); fseek($fh, 0, SEEK_END); $filesize = ftell($fh); rewind($fh); $current_data = ""; if($filesize > 0){ $current_data = fread($fh, $filesize); } flock($fh, LOCK_UN); $chat_messages->load_vars($current_data); $current_message_id = $chat_messages->get_var('current_message_id', 0); $old_message_id = $current_message_id; } else { flock($fh, LOCK_EX); fseek($fh, 0, SEEK_END); $filesize = ftell($fh); rewind($fh); $current_data = ""; if($filesize > 0){ $current_data = fread($fh, $filesize); } flock($fh, LOCK_UN); $chat_messages->load_vars($current_data); $chat_messages->load_vars($current_data); $current_message_id = $chat_messages->get_var('current_message_id', 0); $message_list = $chat_messages->get_var('message_list', array()); if($current_message_id > $old_message_id){ for($x = $old_message_id + 1; $x <= $current_message_id; $x++){ if(isset($message_list[$x])){ $message_array = $message_list[$x]; $message = $message_array['data']; if(preg_match("/^#(.*)/", $message, $matches)){ chat_function($matches[1]); } else { $message = javascript_string_escape($message); echo "window.setTimeout('append_chat_terminal(\"{$message}\");', 1)\n"; } } } } $old_message_id = $current_message_id; } ?> window.setTimeout('refresh_chat_terminal();', ); 0){ $current_data = fread($fh, $filesize); } $chat_messages->load_vars($current_data); $current_message_id = $chat_messages->get_var('current_message_id', 0); $message_list = $chat_messages->get_var('message_list', array()); $current_message_id = $current_message_id + 1; $users = $chat_messages->get_var('users', array()); if(!isset($users[$chat_username])){ echo "window.setTimeout('chat_logged_out();', 1)\n"; exit; } if($users[$chat_username]['password'] != $chat_password){ echo "window.setTimeout('chat_logged_out();', 1)\n"; exit; } $time = time(); $users[$chat_username]['time'] = $time; $message_array = array(); $chat_username = safe_html($chat_username); if(preg_match("/^(#.*)/", $message)){ $message_array['data'] = $message; } else { $message = safe_html($message); $message_array['data'] = "{$chat_username} > " . $message . "
"; } $message_array['time'] = $time; $message_list[$current_message_id] = $message_array; //remove any messages older than a minute $purged_message_list = array(); foreach($message_list as $key => $message_array){ if($message_array['time'] >= $time - 60){ $purged_message_list[$key] = $message_array; } } //remove any uers older than a 10 minutes $purged_users = array(); foreach($users as $key => $users_array){ if($users_array['time'] >= $time - 600){ $purged_users[$key] = $users_array; } } $chat_messages->set_var('current_message_id', $current_message_id); $chat_messages->set_var('message_list', $purged_message_list); $chat_messages->set_var('users', $purged_users); rewind($fh); fwrite($fh, $chat_messages->dump_vars()); flush(); } function chat_function($function_string) { global $moderator_password; global $moderator_kick; global $moderator_bomb; $args = preg_split("/\s+/", trim($function_string)); $function_name = array_shift($args); $password = array_shift($args); $username = array_shift($args); if($password != $moderator_password){ return; } if($function_name == 'kick'){ chat_kick($username); } if($function_name == 'bomb'){ chat_bomb($username); } } function chat_kick($username){ global $moderator_kick; if($moderator_kick == true){ if($username == $_COOKIE['chat_username']){ setcookie('chat_password', ''); echo "window.setTimeout('chat_kick_user();', 1)\n"; } } } function chat_bomb($username){ global $moderator_bomb; if($moderator_bomb == true){ if($username == $_COOKIE['chat_username']){ echo "window.setTimeout('chat_bomb_user();', 1)\n"; } } } $mode = param('mode', 'chat_main'); if($mode == 'chat_main'){ chat_main(); } elseif($mode == 'chat_login') { chat_login(); } elseif($mode == 'chat_js_send_message'){ chat_js_send_message(); } elseif($mode == 'chat_js_terminal'){ chat_js_terminal(); } ?>