)* :== :: || :: :== * $user_file :== ()* :== :: Database specification: create table messages create table users Input parameters: $pagesize - # of messages per page $page - #th page $autorefresh - refresh time (secs) // $admin - user mantainance $style - message view style (0 = board, 1 = chat) $time - message time index $nick - $pass - $message - $header - shows only header $body - show only body $bodyframe - target body frame $headerframe - targer header frame $disablepart - Wont show some parts of the whole document (0 - none, 1 - body, 2 - head, 3 - both) $setframes - 1 - create frames 0 - disable frames (?? :)) $createuser - (internal) $deleteok - (internal) $newnick - (internal) $archive - archive file prefix $archive_no - index of archived file $submit_type CHANGE LOG: * 4.4 - PEAR basic implementation (not functional fully) * 4.3 - starting MySQL DB implementation * 4.2 - started documentation - removed autosubmit on nick change - changed default autorefresh to 5 mins *FIX* now rawurldecoding username (previously rawurlencoded) * 4.1 - undocumented :) */ // IMPORTANT CONSTANTS (change if needed) $WB_version = "4.4"; $WB_name = "Wol's Message Board"; $strip_slashes = 1; $DEFAULT_HEAD_FRAME = ""; $DEFAULT_BODY_FRAME = ""; $use_virtual_delete = 1; //use delete flagging instead of real delete $root_name = "root"; $root_md5_password = "896ae34257056a6edb7643e3db85bb21"; // Filesystem defaults $message_file = "board_messages.txt"; $user_file = "board_emails.txt"; $move_old_mesages = 1; $old_messages_folder = "history/"; $hist_file_prefix = "board_hist_file_"; // used in REGEXPs! alphabeticals only $max_msgs_in_one_file = 200; $min_msgs_in_board = 50; // Database defaults $use_db = 1; $db_message_table = "wolboard_messages"; $db_user_table = "wolboard_users"; $db_host = "db.host.sk"; $db_database = "syndrome"; $db_user = "syndrome"; $db_pass = "kofola"; // FUNCTIONS function good($a) { return (isset($a) && ( (is_string($a) && ($a != "")) || (!is_string($a) && ($a != 0)) ) ); } function link_replace($link, $paramname, $paramvalue) { $link = eregi_replace("&$", "", $link); $link = eregi_replace("&?$paramname=[^&]*", "", $link); if ($paramvalue) $link .= "&$paramname=$paramvalue"; return $link; } function show_array($a) { echo "\n"; $keys = array_keys($a); for($i = 0; $i < sizeof($a); $i++) { echo "\n"; echo "\n\n\n"; } echo "
"; echo $keys[$i].""; if ((is_array($a[$keys[$i]])) && ($keys[$i] !== "GLOBALS")) { show_array($a[$keys[$i]]); } else { echo $a[$keys[$i]]; }; echo "
"; } function array_copy($a, $i, $n = "x") { if (($n == "x") || ($n > (sizeof($a) - $i))) $n = sizeof($a) - $i; for($j = 0; $j < $n; $j++) { $ret[$j] = $a[$j + $i]; } return $ret; } function array_chunk($a, $n) { if (!is_array($a)) return $a; $splits = sizeof($a) / $n; for($i = 0; $i < $splits; $i++) { $ret[$i] = array_copy($a, $i*$n, $n); } return $ret; } function w_chk($n, $v) { return ($v?"$n=$v&":""); } function create_link() { global $pagesize, $page, $autorefresh, $admin, $style, $time, $nick, $pass, $message, $createuser, $deleteok, $autorefresh, $disablepart, $frameset, $headframe, $bodyframe, $archive, $archive_no; return eregi_replace("&$", "", w_chk("nick", $nick). w_chk("pagesize", $pagesize). w_chk("page", $page). // w_chk("time", $time). // w_chk("pass", $pass). // w_chk("message", $message). // w_chk("deleteok", $deleteok). // w_chk("createuser", $createuser). // w_chk("newnick", $newnick). w_chk("autorefresh", $autorefresh). w_chk("style", $style). w_chk("admin", $admin). w_chk("disablepart", $disablepart). w_chk("frameset", $frameset). w_chk("headframe", $headframe). w_chk("bodyframe", $bodyframe). w_chk("archive", $archive). w_chk("archive_no", $archive_no) ); } function wait_read_lock($f, $max_wait = 9999) { $start = time(); while ((!flock($f, 1)) && ($start + $max_wait < time())) sleep(1); return ($start + $max_wait < time())? 1:0; } function wait_write_lock($f, $max_wait = 9999) { $start = time(); while ((!flock($f, 2)) && ($start + $max_wait < time())) sleep(1); } function release_lock($f) { flock($f, 3); return 1; } function show_options($optnames, $optvalues, $selected) { if (!is_array($optvalues)) return 0; $found = 0; $ret = ""; for($i = 0; $i < sizeof($optvalues); $i++) { $ret .= "query("select nick from $usr_file where nick <> ''"); $i = 0; while ($tmp = $res->fetchRow()) { $ret[$i] = $tmp[0]; $i++; } $db_conn->free; return $ret; } } function format_time($time, $style = 0) { switch($style) { case 1: return date("H:i:s", $time); default: return date("H:i:s - d. M Y", $time); } } function get_time($str) { return strtok(trim($str), ":"); } function get_pass($str) { return rawurldecode(trim(strtr(strrchr($str, ":"), ":", ";"))); } function get_email($str) { if (ereg("[^:]*:([^:]*):", $str, $regs)) return $regs[1]; else return ""; } function write_mail($usr_file, $user, $mail = "", $pass = "", $use_db = 0, $db_conn) { $pass = md5(rawurlencode($pass)); if (!$use_db) { // Working with files $fp = fopen($usr_file, "a"); if (!wait_write_lock($fp)) return false; fwrite($fp, strtr($user, ":", ";").":".strtr($mail, ":", ";"). ":".$pass."\n"); release_lock($fp); fclose($fp); return true; } else { // Working with DB $user = addslashes($user); $mail = addslashes($mail); $res = $db_conn->query("insert into $usr_file values". "('$user', '$mail', '$pass', '', '', '')");//!!!TODO return (DB::isError($res)); } } function change_mail($usr_file, $user, $newmail = "", $pass = "", $use_db = 0, $db_conn = 0) { function set_mail($usr_line, $newmail) { $ret = strtok($usr_line, ":"); strtok(":"); $ret .= $newmail.":".strtok(":"); return $ret; } //check if valid data is sent //we dont want to run for now if (true)//!!!!!!!! return 0; if (!$use_db) { // Using files $fp = fopen($usr_file, "w+"); wait_write_lock($fp); $tmp = files($usr_file); $i = 0; $found = false; while (($i < length($tmp)) && (!$found)) { $u = get_user($tmp[$i]); if ($u == $user) { $tmp[$i] = set_mail($tmp[$i], $newmail); $found = true; } $i++; } if ($found) { fwrite($fp, $u); release_lock($fp); fclose($fp); return 1; } else return 0; } else { // Using DB //!!!TODO // "update $usr_file set mail = '$newmail' where name = '$user' and pass = '$pass'" return 0; } } function delete_mail($usr_file, $user = "", $mail = "", $pass = "", $use_db = 0, $db_conn = 0) { //!!!! TODO if (!$use_db) { // Use files return 0; } else { // Use DB // "update $usr_file set mail = ''". // where name = '$user' and pass = '$pass' and mail = '$mail'" return 0; } } function get_emails($usr_file, $use_db = 0, $db_conn = 0) { if (!$use_db) { // Use files $fp = fopen($usr_file, "r"); wait_read_lock($fp); $f = file($usr_file); for($i = 0; $i < sizeof($f); $i++) $ret[get_user($f[$i], 0)] = get_email($f[$i]); release_lock($fp); return $ret; } else { // Use DB // TODO // $res = $db_conn->query("select * from $usr_file where status = 1"); return ""; } } function read_messages($msg_file, $usr_file, $full = 0, $use_db = 0, $db_conn = 0) { if (!$use_db) { $mails = get_emails($usr_file, 0); $mfp = fopen($msg_file, "r"); wait_read_lock($mfp); $f = file($msg_file); $n = 0; for ($i = 0; $i < sizeof($f); $i++) { if ($f[$i] != "") { if ((!$full) && ereg("[^:]*:[^:]*:\*", $f[$i], $regs)) continue; $ret[$n]["time"] = strtok($f[$i], ":"); $ret[$n]["name"] = rawurldecode(strtok(":")); $ret[$n]["message"] = trim(rawurldecode(strtok(":"))); $ret[$n]["email"] = $mails[$ret[$n]["name"]]; $n++; } } release_lock($mfp); return $ret; } else { // Use DB if ($full) $q = $db_conn->query("select autor_id, ". "text, created from $msg_file"); else $q = $db_conn->query("select autor_id, ". "text, created from $msg_file where flags = 0"); // TODO } } function need_to_split($a, $max, $min) { if ((sizeof($a) - $max) <= $min) return false; else return true; } function split_messages_to_more_files($msgs, $max_len, $min_len, $hist_dir, $hist_file_prefix = "board_hist_file_", $index_style = 0) { if (!need_to_split($msgs, $max_len, $min_len)) return $msgs; $full = array_chunk($msgs, $max_len); $d = opendir($hist_dir); $i = -1; $max_num = -1; // looking for previous save of maximum index (and count of saves) while (($file = readdir($d)) !== false) { if (ereg("^$hist_file_prefix([0-9]*)", $file, $regs)) { $i++; if ($max_num < $regs[1]) $max_num = $regs[1]; } } closedir($d); switch($index_style) { case "0": $new_index = $max_num + 1; break; case "1": $new_index = ++$i; break; } for($i = 0; $i < (sizeof($full) - 1); $i++) { $new_file = $hist_dir.$hist_file_prefix.($new_index + $i); $fp = fopen($new_file, "a"); wait_write_lock($fp); fwrite($fp, implode($full[$i], "")); release_lock($fp); } $msgs = $full[sizeof($full) - 1]; return $msgs; } function write_message($msg_file, $user = "Anonym", $text = "", $time = 0, $old_messages_folder, $max_msgs_in_one_file, $min_msgs_in_board, $hist_file_prefix, $use_db = 0, $db_conn = 0) { if (($time == 0) || !ereg("^[0-9]*$", $time)) $time = time(); $user = rawurlencode($user); $text = trim($text); $text = str_replace("ignorant", "ign-co? (c) Wol", $text); //some fun :) if ($text[0] == '*') // this is used as the deleted flag prefix $text = " ".$text; if (!$use_db) { // Use files $new_msg = $time.":". $user.":". rawurlencode(nl2br(htmlspecialchars($text)))."\n"; $msgs = file($msg_file); $msgs[] = $new_msg; if (need_to_split($msgs, $max_msgs_in_one_file, $min_msgs_in_board)) { $fp = fopen($msg_file, "w"); wait_write_lock($fp); sort($msgs); $msgs = split_messages_to_more_files( $msgs, $max_msgs_in_one_file, $min_msgs_in_board, $old_messages_folder, $hist_file_prefix, 0 ); $msgs = implode($msgs, ""); if (trim($msgs) == "") $msgs = ""; fwrite($fp, $msgs); } else { unset($msgs); $fp = fopen($msg_file, "a"); wait_write_lock($fp); fwrite($fp, $new_msg); } release_lock($fp); fclose($fp); return 1; } else { // Use DB // TODO // $msg_file stores Message Table name // "insert into $msg_file values (". // "'$user', '$text', '$time', '', '')" /* $res = $db_conn->query("insert into $msg_file" . " (author_id, created, text ) values " . " ('".addslashes($user)."', sysdate()," . " '" . addslashes($message) . "')" ) */ return 0; } } function delete_message($msg_file, $usr_file, $user, $time, $pass, $use_virtual_delete, $use_db = 0, $db_messages = 0, $db_users = 0) { if (!$use_db) { // Use files $found = false; $i = 0; $fp = fopen($usr_file, "r"); wait_read_lock($fp); $p = file($usr_file); release_lock($fp); if ($user != "") { while ((!$found) && ($i < sizeof($p))) { if ($user == get_user($p[$i], 0)) $found = true; $i++; }; } else $found = true; if (!$found) return 1; if (!(($user == "") && ($pass == ""))) if (get_pass($p[--$i]) != md5($pass)) return 2; $fp = fopen($msg_file, "r"); wait_read_lock($fp); $f = file($msg_file); $found = false; $i = 0; while (!$found && ($i < sizeof($f))) { if (get_user($f[$i], 1) == $user && get_time($f[$i]) == $time) $found = true; $i++; } if (!$found) return 3; $i--; if ($use_virtual_delete) { // if already marked as deleted then delete it really if (ereg("[^:]*:[^:]*:\*.*", $f[$i], $regs)) $f[$i] = $f[sizeof($f) - 1]; // if not marked, mark it :) else $f[$i] = ereg_replace("([^:]*:[^:]*:)(.*)", "\\1*\\2", $f[$i]); } else { if (sizeof($f) > 0) { $f[$i] = $f[sizeof($f) - 1]; } else $f = array(); } // we supppose the array to be sorted (sorting while insert) //sort($f); release_lock($fp); $fp = fopen($msg_file, "w"); wait_write_lock($fp); if (is_array($f)) fwrite($fp, trim(implode($f, ""))."\n"); else if (trim($f) != "") fwrite($fp, trim($f)."\n"); release_lock($fp); fclose($fp); return 0; } else { // Use DB // TODO /* $sql = "select * from $usr_file where" . "name = '$user' and pass = '$pass'"; $res = $db_conn->query($sql); if (DB::isError($res) || ($res[0] == 0)) return 0; $rows = array(); while ($row = $res->fetchRow()) $rows[] = $row; if ($use_virtual_delete) $res = $db_conn->query("update $msg_file ". "set deleted=sysdate(), flags "where name = '$user' and time='$time'"); else//TODO!!! following $res = $db_conn->query("delete from $msg_file " . "where name = '$user' and time='$time'" //*/ return -1; } } function show_data_form($user_file, $self, $nick, $style, $WB_name, $autorefresh, $pagesize, $use_db = 0, $db_conn = 0) { global $headframe; global $bodyframe; global $show_body; global $frameset; ?>

Current time is .
" target=""> "; else echo "Board /"; ?>
Nick
>
">New nick ChatChat
Autorefresh time: Page size: Switch to frames
Messages ".($firstmsg + 1)."-". ($lastmsg + 1)." of $nummsgs
"; // REWIND to begin if ($firstmsg == 0) echo "<<"; else echo "<<"; echo " "; // PREVIOUS PAGE if ($firstmsg == 0) echo "<"; else echo "<"; echo " "; // OTHER PAGES for ($i = 1; $i <= $lastpage; $i++) { echo ($i == $currentpage)?"$i ":"".$i." "; } // NEXT PAGE if ($lastmsg >= ($nummsgs - 1)) echo ">"; else echo ">"; echo " "; // REWIND PAGE to end if ($lastmsg >= ($nummsgs - 1)) echo ">>"; else echo ">>"; echo "\n"; } function show_nonrefresh_head() { global $WB_name, $WB_version; ?> <?echo "$WB_name v$WB_version"?> Delete message

"; echo "\n
\n
\n"; echo "\n"; echo "\t\n\n\t\t\n"; echo "\t\n\n\t\t\n"; echo "\t\n\n\t\t\n"; echo "\t\n\n\t\t\n"; echo "\t\n"; echo "
Nick:
Message No.:
Password:
\n
cancel
\n"; echo "
\n
\n"; } function show_createuser_dialog($newnick, $email) { global $self; echo "\n
Create new user

"; echo "\n"; echo "\n\n"; echo "\t\n\t\t\n"; echo "\t\n\t\n"; echo "\t\n\t\n"; echo "\t\n\t\n"; echo "\t\n"; echo "\n
New Nick:
E-mail:
Password:
cancel
\n
\n"; } function show_message($message, $style = 0) { global $self; switch($style) { case 1: // Chat message line echo ""; echo "".format_time($message["time"], 1)."_"; if ($message["email"] != "") echo "". $message["name"].""; else echo $message["name"]; echo ":".$message["message"]; echo "\n"; break; default: // Board message style echo ""; if ($message["email"] != "") echo "\n"; echo "\t" . $message["name"] . "\n"; echo "\n\t - "; echo "\t" . format_time($message["time"])."\n"; echo "\t\t - delete\n"; echo "
"; echo "\t" . $message["message"] . "\n"; echo "\n"; break; } } function show_messages($message_file, $user_file, $page, $pagesize, $msg_style = 0, $show_all = 0, $use_db = 0, $db_messages = 0, $db_users = 0) { $messages = read_messages($message_file, $user_file, $show_all, $use_db, $db_messages, $db_users); $n = sizeof($messages); global $self;//!!!!! TODO if ($n <= 0) { echo "
No messages YeT !
"; } else { if ($page < 1) $page = 1; if ($page >= ($n / $pagesize)) $page = ceil($n / $pagesize); $first = $pagesize * ($page - 1); $last = ($pagesize * $page) - 1; if ($first < 0) $first = 0; if ($first > $n) $first = $n - 1; if ($last > ($n - 1)) $last = $n - 1; if ($last < $first) $last = $first; show_page_walk($page, $pagesize, $n, $first, $last); echo ""; for ($i = $first; $i <= $last; $i++) { show_message($messages[sizeof($messages) - 1 - $i], $msg_style); } echo "
"; show_page_walk($page, $pagesize, $n, $first, $last); } } // /////////////////////////////////////////////// // /////////// THIS IS MAIN FUNCTION ///////////// // /////////////////////////////////////////////// // DEFAULTS if ($use_db) { require_once "DB.php"; $db_handle = DB::connect("mysql://$db_user:$db_pass" . "@$db_host/$db_database"); if (DB::isError($db_handle)) { echo $db_handle->getMessage(); exit; } if ($use_db) { $user_file = $db_users; $message_file = $db_messages; } } if (!good($self)) $self = $PHP_SELF; if ($strip_slashes) { $message = stripslashes($message); $nick = stripslashes($nick); $pass = stripslashes($pass); $header = stripslashes($header); $body = stripslashes($body); $bodyframe = stripslashes($bodyframe); $headerframe = stripslashes($headerframe); $disablepart = stripslashes($disablepart); $setframes = stripslashes($setframes); $newnick = stripslashes($newnick); } if ($pagesize < 10) $pagesize = 50; settype($pagesize, integer); if ($autorefresh < 10) $autorefresh = 300; settype($autorefresh, integer); if (!good($disablepart) || ($disablepart>3) || ($disablepart<0)) $disablepart = 0; if (!good($headframe)) $headframe = $DEFAULT_HEAD_FRAME; if (!good($bodyframe)) $bodyframe = $DEFAULT_BODY_FRAME; switch ($disablepart) { case 0: $show_body = true; $show_head = true; break; case 1: $show_head = true; $show_body = false; break; case 2: $show_head = false; $show_body = true; break; case 3: $show_body = false; $show_head = false; break; } if (!good($page)) $page = 0; if (!isset($nick) && good($newnick)) $nick = $newnick; if (!good($mail)) $mail = get_email($nick); if (good($archive)) $hist_file_prefix = $archive; if (good($archive_no) && !$use_db) $message_file = $old_messages_folder.$hist_file_prefix.$archive_no; //header ("Cache-Control: no-cache, must-revalidate"); //header ("Pragma: no-cache"); // CREATE NEW USER if ($createuser) { $mails = get_emails($user_file, $use_db, $db_users); if (good($newnick) && ($mails[$newnick] == "") && good($mail) && good($createok) && ($createok == 1)) write_mail($user_file, $newnick, $mail, $pass, $use_db, $db_users); else { show_nonrefresh_head(); show_createuser_dialog($newnick, $email); $show_body = false; } } // WRITE MESSAGE TO DB if (good($message) && good($submit_type)) write_message($message_file, $nick, $message, 0, B $old_messages_folder, $max_msgs_in_one_file, $min_msgs_in_board, $hist_file_prefix, $use_db, $db_messages); // DELETE MESSAGE FROM DB if (good($time)) { if (good($deleteok) && ($deleteok == 1)){ switch(delete_message($message_file, $user_file, $nick, $time, $pass, $use_virtual_delete, $use_db, $db_messages, $db_users)) { // TOTO ESTE DOROBIT - SPRAVANIE SA // SKRIPTU KED NASTANU PROBLEMY PRI DELETOVANI SPRAVY case 0: // All OK // echo "
Message deleted succesfully
\n"; break; case 1: // No such user // echo "
Error while deleting: There is no such user!
\n"; break; case 2: // Wrong password // echo "
Error while deleting: Wrong password!
\n"; break; case 3: // No such message // echo "
Error while deleting: No such message!
\n"; break; default:// Unknown error // echo "
Fatal Error while deleting: unknown error code!

\n"; break; } } else { show_nonrefresh_head(); show_delete_dialog($nick, $time); $show_body = false; } } // check for superuser view if (($nick == $root_name) && (md5($pass) == $root_md5_password)) $superuser = 1; else $superuser = 0; // We don't want to show body, when other form is shown ?> <?echo "$WB_name v$WB_version"?> \n"; ?> disconnect(); } ?>