Advertisement
aaaaaa123456789

Acid Logs code

Feb 27th, 2012
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function database_connect() {
  4.   $result = 0;
  5.   $connection = mysql_connect(/* database host */, /* database user */, /* database pass*/) or $result = 1;
  6.   if ($result == 0)
  7.     mysql_select_db(/* database schema */, $connection) or $result = 2;
  8.   return $result;
  9. }
  10.  
  11. function get_single_row_result($query) {
  12.   $resultset = mysql_query($query);
  13.   if (mysql_num_rows($resultset) > 1) $result = true;
  14.   else if (mysql_num_rows($resultset) != 1) $result = false;
  15.   else $result = mysql_fetch_array($resultset);
  16.   mysql_free_result($resultset);
  17.   return $result;
  18. }
  19.  
  20. function get_log_password($logid) {
  21.   $logid = (string) (int) $logid;
  22.   $query = "SELECT passhash FROM log_header WHERE id = $logid";
  23.   $result = get_single_row_result($query);
  24.   if (!isset($result['passhash'])) return false;
  25.   return $result['passhash'];
  26. }
  27.  
  28. function get_group_password($groupid) {
  29.   $groupid = (string) (int) $groupid;
  30.   $query = "SELECT passhash FROM log_group WHERE id = $groupid";
  31.   $result = get_single_row_result($query);
  32.   if (!isset($result['passhash'])) return false;
  33.   return $result['passhash'];
  34. }
  35.  
  36. function get_logs_in_group($groupid) {
  37.   $groupid = (string) (int) $groupid;
  38.   $result = mysql_query("SELECT logid FROM log_assoc WHERE groupid = $groupid ORDER BY logid");
  39.   $logs = array('count' => 0);
  40.   $row = mysql_fetch_array($result);
  41.   while (is_array($row)) {
  42.     $logs[$logs['count']] = $row['logid'];
  43.     $logs['count'] ++;
  44.     $row = mysql_fetch_array($result);
  45.   }
  46.   mysql_free_result($result);
  47.   return $logs;
  48. }
  49.  
  50. function get_log_data($logid) {
  51.   $logid = (string) (int) $logid;
  52.   $head = get_single_row_result("SELECT * FROM log_header WHERE id = $logid");
  53.   if ($head === false) return false;
  54.   $data = array(
  55.     'number' => $head['id'],
  56.     'date' => $head['startdate'],
  57.     'time' => $head['starttime'],
  58.     'offset' => $head['timeoffset'],
  59.     'hasoffset' => ($head['timeoffset'] !== null),
  60.     'description' => $head['description'],
  61.     'count' => 0
  62.   );
  63.   $resultset = mysql_query("SELECT * FROM log_data WHERE logid = $logid ORDER BY entryid");
  64.   $result = mysql_fetch_array($resultset);
  65.   while (is_array($result)) {
  66.     $data[$data['count']] = array(
  67.       'timestamp' => $result['chattime'],
  68.       'sender' => $result['chatter'],
  69.       'servermessage' => ($result['chatter'] === null),
  70.       'content' => $result['chattext']
  71.     );
  72.     $data['count'] ++;
  73.     $result = mysql_fetch_array($resultset);
  74.   }
  75.   mysql_free_result($resultset);
  76.   return $data;
  77. }
  78.  
  79. function format_date($datevalue) {
  80.   $d = (int) $datevalue;
  81.   $day = $d % 100;
  82.   $month = ($d / 100) % 100;
  83.   $year = (int) ($d / 10000);
  84.   switch ($month) {
  85.     case  1: $monthname = "Jan"; break;
  86.     case  2: $monthname = "Feb"; break;
  87.     case  3: $monthname = "Mar"; break;
  88.     case  4: $monthname = "Apr"; break;
  89.     case  5: $monthname = "May"; break;
  90.     case  6: $monthname = "Jun"; break;
  91.     case  7: $monthname = "Jul"; break;
  92.     case  8: $monthname = "Aug"; break;
  93.     case  9: $monthname = "Sep"; break;
  94.     case 10: $monthname = "Oct"; break;
  95.     case 11: $monthname = "Nov"; break;
  96.     case 12: $monthname = "Dec"; break;
  97.     default: $monthname = "";
  98.   }
  99.   $fulldate = $monthname . " " . $day . ", " . $year;
  100.   return $fulldate;
  101. }
  102.  
  103. function format_time($timevalue, $timeoffset) {
  104.   if ($timeoffset === null) $timeoffset = 0;
  105.   $timeoffset = (int) $timeoffset;
  106.   $houroffset = 0;
  107.   while (($timeoffset < 0) or ($timeoffset >= 60)) {
  108.     if ($timeoffset < 0) {
  109.       $houroffset --;
  110.       $timeoffset += 60;
  111.     } else {
  112.       $houroffset ++;
  113.       $timeoffset -= 60;
  114.     }
  115.   }
  116.   $n = (int) $timevalue;
  117.   $second = $n % 100;
  118.   $minute = ($n / 100) % 100;
  119.   $minute += $timeoffset;
  120.   $hour = (int) ($n / 10000);
  121.   $hour += $houroffset;
  122.   if ($minute >= 60) {
  123.     $minute -= 60;
  124.     $hour ++;
  125.   } else if ($minute < 0) {
  126.     $hour --;
  127.     $minute += 60;
  128.   }
  129.   if ($hour < 0) $hour += 24;
  130.   if ($hour >= 24) $hour -= 24;
  131.   if ($hour == 0) $hn = 12; else if ($hour > 12) $hn = $hour - 12; else $hn = $hour;
  132.   $hm = ($hour < 12) ? "am" : "pm";
  133.   $fulltime = $hn . ":" . ($minute < 10 ? "0" : "") . $minute . ":" . ($second < 10 ? "0" : "") . $second . $hm;
  134.   return $fulltime;
  135. }
  136.  
  137. function log_header($logdata) {
  138.   $date = format_date($logdata['date']);
  139.   $time = format_time($logdata['time'], null);
  140.   $timestamp = $date . ', ' . $time;
  141.   $lognumber = $logdata['number'];
  142.   $text = $logdata['description'];
  143.   $text = htmlspecialchars($text);
  144.   $result = <<<HTMLcode_end
  145.   <font face="Tahoma" size="2">
  146.   <h4>Log number $lognumber</h4><br />
  147.   <i>$text</i><br />
  148.   Log recorded on $timestamp<br />
  149.   <br />
  150.   <table width="100%">
  151.     <col width="10%" align="right" valign="middle" />
  152.     <col width="15%" align="left" valign="middle" />
  153.     <col width="75%" align="left" valign="baseline" />
  154.     <tr>
  155.       <th>Timestamp</th>
  156.       <th>Chatter</th>
  157.       <th>Message</th>
  158.     </tr>
  159. HTMLcode_end;
  160.   return $result;
  161. }
  162.  
  163. function log_footer() {
  164.   return '</table></font><br /><hr /><br />';
  165. }
  166.  
  167. function log_tablerow($timestamp, $timeoffset, $servermessage, $chatter, $text) {
  168.   $timedata = format_time($timestamp, $timeoffset);
  169.   if ($servermessage) {
  170.     $message = '<i>' . htmlspecialchars($text) . '</i>';
  171.     $chattercell = '<i>(server message)</i>';
  172.   } else {
  173.     $chattercell = htmlspecialchars($chatter);
  174.     $message = htmlspecialchars($text);
  175.   }
  176.   $tablerow = <<<HTMLcode_end
  177.   <tr>
  178.     <td>$timedata</td>
  179.     <td>$chattercell</td>
  180.     <td>$message</td>
  181.   </tr>
  182. HTMLcode_end;
  183.   return $tablerow;
  184. }
  185.  
  186. function password_page($basedata) {
  187.   $type = $basedata['type'];
  188.   $id = $basedata['number'];
  189.   return <<<HTMLcode_end
  190.   <html>
  191.     <head>
  192.       <title>Acid Logs -- Password needed!</title>
  193.     </head>
  194.     <body>
  195.       <center>
  196.         <b>This logs are password-protected.</b><br />
  197.         If you entered a password, you entered the wrong one.<br /><br />
  198.         (If you don't know the right one, ask me for it)<br /><br /><br />
  199.         <form action="http://logs.acidchat.comuf.com/chatlog.php" method="post">
  200.           <input type="hidden" name="type" value="$type" />
  201.           <input type="hidden" name="id" value="$id" />
  202.           Password: <input type="password" name="password" />
  203.           <input type="submit" value="View logs" />
  204.         </form>
  205.       </center>
  206.     </body>
  207.   </html>
  208. HTMLcode_end;
  209. }
  210.  
  211. function error_page() {
  212.   return <<<HTMLcode_end
  213.   <html>
  214.     <head>
  215.       <title>Acid Logs -- oops!</title>
  216.     </head>
  217.     <body>
  218.       <center>
  219.         <br /><br /><br />
  220.         Evil aliens are st-- wait, no, that's PR3.<br />
  221.         Well, truth is, we have a little database error.<br />
  222.         Just refresh the page, that should fix it. Tell me if it doesn't get fixed.<br />
  223.         Thanks!
  224.       </center>
  225.     </body>
  226.   </html>
  227. HTMLcode_end;
  228. }
  229.  
  230. function get_base_data() {
  231.   $type = $_REQUEST['type'];
  232.   $id = $_REQUEST['id'];
  233.   $pass = $_REQUEST['password'];
  234.   if (($type != 'group') and ($type != 'log')) return false;
  235.   $id = (int) $id;
  236.   if ($id <= 0) return false;
  237.   return array (
  238.     'type' => $type,
  239.     'number' => $id,
  240.     'password' => ((string) $pass)
  241.   );
  242. }
  243.  
  244. function check_password($basedata) {
  245.   if ($basedata['type'] == 'group')
  246.     $passhash = get_group_password($basedata['number']);
  247.   else
  248.     $passhash = get_log_password($basedata['number']);
  249.   if ($passhash === false) return true;
  250.   $currpass = md5($basedata['password']);
  251.   return ($currpass == $passhash);
  252. }
  253.  
  254. function log_contents_page($logid) {
  255.   $data = get_log_data($logid);
  256.   $header = log_header($data);
  257.   $content = '';
  258.   for ($pos = 0; $pos < $data['count']; $pos ++)
  259.     $content .= log_tablerow($data[$pos]['timestamp'], $data['offset'], $data[$pos]['servermessage'],
  260.                              $data[$pos]['sender'], $data[$pos]['content']);
  261.   $footer = log_footer();
  262.   $page = $header . "\n" . $content . "\n" . $footer;
  263.   return $page;
  264. }
  265.  
  266. function process_log_page($logid) {
  267.   $logdata = log_contents_page($logid);
  268.   return <<<HTMLcode_end
  269.   <html>
  270.     <head>
  271.       <title>Acid Logs -- log #$logid</title>
  272.     </head>
  273.     <body>
  274.       $logdata
  275.     </body>
  276.   </html>
  277. HTMLcode_end;
  278. }
  279.  
  280. function process_group_page($groupid) {
  281.   $groupid = (string) (int) $groupid;
  282.   $groupheader = <<<HTMLcode_end
  283.   <html>
  284.     <head>
  285.       <title>Acid Logs -- group #$groupid</title>
  286.     </head>
  287.     <body>
  288. HTMLcode_end;
  289.   $data = get_single_row_result("SELECT * FROM log_group WHERE id = $groupid");
  290.   if ($data === false) return false;
  291.   $description = $data['description'];
  292.   $line1 = "<b>Log group number $groupid</b><br />";
  293.   if ($description !== null) $line2 = htmlspecialchars((string) $description);
  294.   $dataheader = $line1 . $line2 . "<br /><hr /><br />";
  295.   $page = $groupheader . "\n" . $dataheader . "\n";
  296.   $logs = get_logs_in_group($groupid);
  297.   for ($log = 0; $log < $logs['count']; $log ++)
  298.     $page .= log_contents_page($logs[$log]) . "\n";
  299.   $page .= "</body></html>";
  300.   return $page;
  301. }
  302.  
  303. function process_required_page($basedata) {
  304.   if ($basedata['type'] == 'log')
  305.     return process_log_page($basedata['number']);
  306.   else
  307.     return process_group_page($basedata['number']);
  308. }
  309.  
  310. function log_not_found() {
  311.   return <<<HTMLcode_end
  312.   <html>
  313.     <head>
  314.       <title>Acid Logs -- 404?</title>
  315.     <head>
  316.     <body>
  317.       <center>
  318.         <font size="+2"><b>Log not found.</b></font><br /><br />
  319.         Well, that's technically not a 404... but whatever.<br />
  320.         Make sure you enter the log number correctly, please.<br />
  321.         Tell me if a real log doesn't load. Thanks!
  322.       </center>
  323.     </body>
  324.   </html>
  325. HTMLcode_end;
  326. }
  327.  
  328. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  329. echo "\n";
  330.  
  331. $status = database_connect();
  332. if ($status > 0) die(error_page());
  333. $data = get_base_data();
  334. if ($data === false) die(log_not_found());
  335. $passmatch = check_password($data);
  336. if (!$passmatch) die(password_page($data));
  337. $page = process_required_page($data);
  338. if ($page === false) die(log_not_found());
  339. echo $page;
  340.  
  341. ?>
Advertisement
RAW Paste Data Copied
Advertisement