', $sid, $buffer); ## so full URLs in templates can have SID appended if ($javascript && !strstr($buffer, '//%javascript%//')) report_error('Could not locate "//%javascript%//" in template, '.$template, $error_log); else $buffer = str_replace('//%javascript%//', $javascript, $buffer); if (defined('COPYRIGHT_START')) { $copyright = (COPYRIGHT_START == date('Y') ? COPYRIGHT_START : COPYRIGHT_START.' - '.date('Y', NOW_UNIX)); $buffer = str_replace('', $copyright, $buffer); } $content_tag = ''; ## leave tag in variable for strlen below $position = strpos($buffer, $content_tag); if ($position === false) { report_error('Could not locate in template, '.$template, $error_log); display_error('There was a problem and the page could not be displayed. (missing content tag)'); } $html_top = substr($buffer,0,$position); $html_bottom = substr($buffer,$position + strlen($content_tag)); $html_top = str_replace('', '<title>'.($title ? "$title $title_separator " : ''), $html_top); define('HTML_TOP', $html_top); define('HTML_BOTTOM', $html_bottom); } function replace_template_tags($template_html, $tag_array = '') { if (is_array($tag_array)) { foreach($tag_array as $key => $value) { $tag = '<!--%'.$key.'%-->'; $template_html = str_replace($tag, $value, $template_html); } } return $template_html; } function get_snippet($snippet_name, $nl2br = true) { $filename = PATH_SNIPPETS.$snippet_name.'.txt'; $snippet_contents = read_file($filename); if (!strstr($snippet_contents, 'nl2br=0') && $nl2br) $snippet_contents = nl2br($snippet_contents); return $snippet_contents; } function get_sid() { $sid = (session_id() ? session_name().'='.session_id() : ''); return $sid; } ###################### ## error functions ## ###################### ## optional 3rd arg passed true will print error to screen if "true" and exit ## requires write_file() below function report_error($error_msg, $error_log = 'errors_misc.txt', $to_screen = 0) { if (!defined('PATH_ERRORS')) return false; $error_log = preg_replace('/[^\w\.]/', '', $error_log); if (!$error_log) $error_log = 'errors_misc.txt'; $error_log = PATH_ERRORS.basename($error_log); $error_msg = date('m/d/y H:i ', NOW_UNIX).$_SERVER['SCRIPT_NAME'].': '.$error_msg."\n"; if (write_file($error_log, $error_msg)) $success = true; else $success = false; if ($to_screen) display_error($error_msg); ## display_error() will exit return $success; } function display_error($error_msg = '', $report = 0) { global $error_log; if (!headers_sent()) { prep_html('blank.html', 'error'); if (defined('HTML_TOP')) echo HTML_TOP; } echo '<div align="center"><font color="#FF0000">'; if ($error_msg) echo $error_msg; else echo 'Something went wrong and the page could not be displayed. '; echo '</font></div>'; if ($report && $error_msg) report_error($error_msg, $error_log); if (defined('HTML_BOTTOM')) echo HTML_BOTTOM; exit; } ###################### ## cookie functions ## ###################### function set_cookie($key, $value, $ttl = 15552000) ## default TTL is 6 months (15552000 seconds) { static $cookie_array = array(); ## if a page calls set_cookie more than once, preserve previous values if (headers_sent()) { report_error("set_cookie in phplib was called after headers were sent, key=$key, value=$value", 'errors_cookies.txt'); return false; } if (isset($_COOKIE['cookie'])) $cookie_array_decoded = unserialize(base64_decode($_COOKIE['cookie'])); else $cookie_array_decoded = array(); $cookie_array_merge = $cookie_array; $cookie_array = array_merge($cookie_array_decoded, $cookie_array_merge); $cookie_array[$key] = $value; $cookie_content = base64_encode(serialize($cookie_array)); $host = get_host(); setcookie ('cookie', $cookie_content, (time() + $ttl), '/', $host, 0); } function get_cookie($key) { if (!isset($_COOKIE['cookie'])) return false; $cookie_array = unserialize(base64_decode($_COOKIE['cookie'])); return $cookie_array[$key]; } function clear_cookie($key) { if (headers_sent()) { report_error("clear_cookie in phplib was called after headers were sent, key=$key", 'errors_cookies.txt'); return false; } if (isset($_COOKIE['cookie'])) $cookie_array = unserialize(base64_decode($_COOKIE['cookie'])); unset($cookie_array[$key]); $cookie_content = base64_encode(serialize($cookie_array)); $host = get_host(); setcookie ('cookie', $cookie_content, (time() + 60*60*24*30*6), '/', $host, 0); } function clear_all_cookies() { if (headers_sent()) { report_error("clear_all_cookies in phplib was called after headers were sent", 'errors_cookies.txt'); return false; } $host = get_host(); setcookie('cookie','',time()-86400,"/", $host, 0); } function get_host() ## $host used for setting cookies { preg_match('/^(([A-Z0-9][A-Z0-9_-]*\.)*([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i', $_SERVER['SERVER_NAME'], $matches); if (is_numeric($matches[4])) $host = '/'; ## if $matches[4] is numeric, then host is IP else $host = '.'.$matches[3].$matches[4]; return $host; } #################### ## referrer check ## #################### function referrer_check() { if (!defined('URL_REFERER')) report_error('There is a problem with the website, please try back later. (URL_REFERER not defined)', 'errors_config.txt', 1); if (!$_SERVER['HTTP_REFERER']) display_error('Something went wrong and the URL of the referring page could not be determined. This page may only be accessed by this website.'); $url_array = parse_url($_SERVER['HTTP_REFERER']); $url_array['host'] = str_replace('www.', '', strtolower($url_array['host'])); if (!stristr(URL_REFERER, $url_array['host'])) exit('this form may not be used by external sites ('.URL_REFERER.' : '.$url_array['host'].')'); } ##################### ## file operations ## ##################### function increment_counter($filename) ## pass a filename to a counter text file (one will be created if not present and allowed), ## and the number in the counter file will be incremented and returned { clearstatcache(); ignore_user_abort(true); ## prevent refresh from screwing up file operations $is_file = is_file($filename); $fh = @fopen($filename, ($is_file ? 'r+b' : 'a+b')); if ($fh) { if (@flock($fh, LOCK_EX)) { if ($is_file) { $count = fread($fh, filesize($filename)+1); rewind($fh); } else $count = 0; $count++; fwrite($fh, $count); fflush($fh); flock($fh, LOCK_UN); } else { report_error("Could not lock counter '$filename'", 'errors_file.txt'); return false; } fclose($fh); } else { report_error("Could not open counter '$filename'", 'errors_file.txt'); return false; } ignore_user_abort(false); return $count; } function write_file($filename, $write_text, $mode = 'append') ## pass a filename and text to write, mode may be either "append" or "write" { clearstatcache(); ignore_user_abort(true); ## prevent refresh from screwing up file operations if ($mode == 'write') $fopen_mode = 'w'; else $fopen_mode = 'a'; $fh = @fopen($filename, $fopen_mode); if ($fh) { if (@flock($fh, LOCK_EX)) { fwrite($fh, $write_text); fflush($fh); flock($fh, LOCK_UN); } else { report_error("Could not lock file '$filename'", 'errors_file.txt'); fclose($fh); return false; } fclose($fh); } else { report_error("Could not open file '$filename'", 'errors_file.txt'); return false; } ignore_user_abort(false); return true; } function read_file($filename) { clearstatcache(); ignore_user_abort(true); ## prevent refresh from screwing up file operations $fh = @fopen($filename, 'r'); if ($fh) { if (@flock($fh, LOCK_SH)) { $contents = fread($fh, filesize($filename)+1); flock($fh, LOCK_UN); } else { report_error("Could not lock file '$filename'", 'errors_file.txt'); return false; } fclose($fh); } else { report_error("Could not open file '$filename'", 'errors_file.txt'); return false; } ignore_user_abort(false); return $contents; } ?>