Advertisement
aaaaaa123456789

Announcement code

Feb 24th, 2012
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function make_date($datenumber, $timenumber) {
  4.   if (!$datenumber) return "";
  5.   $d = (int) $datenumber;
  6.   $n = (int) $timenumber;
  7.   $second = $n % 100;
  8.   $minute = ($n / 100) % 100;
  9.   $hour = (int) ($n / 10000);
  10.   $day = $d % 100;
  11.   $month = ($d / 100) % 100;
  12.   $year = (int) ($d / 10000);
  13.   if ($second >= 60) {$second -= 60; $minute ++;}
  14.   if ($minute >= 60) {$minute -= 60; $hour ++;}
  15.   if ($hour >= 24) {$day += ($hour / 24); $hour %= 24;}
  16.   if (!checkdate($month, $day, $year)) return "";
  17.   switch ($month) {
  18.     case  1: $monthname = "Jan"; break;
  19.     case  2: $monthname = "Feb"; break;
  20.     case  3: $monthname = "Mar"; break;
  21.     case  4: $monthname = "Apr"; break;
  22.     case  5: $monthname = "May"; break;
  23.     case  6: $monthname = "Jun"; break;
  24.     case  7: $monthname = "Jul"; break;
  25.     case  8: $monthname = "Aug"; break;
  26.     case  9: $monthname = "Sep"; break;
  27.     case 10: $monthname = "Oct"; break;
  28.     case 11: $monthname = "Nov"; break;
  29.     case 12: $monthname = "Dec"; break;
  30.     default: $monthname = "";
  31.   }
  32.   if ($hour == 0) $hn = 12; else if ($hour > 12) $hn = $hour - 12; else $hn = $hour;
  33.   $hm = ($hour < 12) ? "am" : "pm";
  34.   $fulldate = $monthname . " " . $day . ", " . $year . ", " . $hn . ":" . ($minute < 10 ? "0" : "") . $minute . ":"
  35.     . ($second < 10 ? "0" : "") . $second . $hm;
  36.   return $fulldate;
  37. }
  38.  
  39. function database_connection($server, $uid, $pwd, $dbname) {
  40.   $connection = mysql_connect($server, $uid, $pwd);
  41.   if (!$connection) return 1;
  42.   $result = mysql_select_db($dbname, $connection);
  43.   if (!$result) return 2;
  44.   return 0;
  45. }
  46.  
  47. function retrieve_record($recordid) {
  48.   $recordnumber = (int) $recordid;
  49.   $query = "SELECT id, added_by, date, time, contents FROM announcements WHERE id = $recordnumber";
  50.   $result = mysql_query($query);
  51.   $data = mysql_fetch_array($result);
  52.   if (is_array($data)) return $data; else return NULL;
  53. }
  54.  
  55. function retrieve_records($amount, $start) {
  56.   $data = array("count" => 0);
  57.   $n = (int) $amount;
  58.   $s = (int) $start;
  59.   if ($n <= 0 or $s < 0) return $data;
  60.   $result = mysql_query("SELECT DISTINCT id FROM announcements ORDER BY 1 DESC LIMIT $s, $n");
  61.   for ($pos = 0; $pos < $amount; $pos ++) {
  62.     $x = mysql_fetch_array($result);
  63.     if (!is_array($x)) break;
  64.     $newid = $x["id"];
  65.     $data[$data["count"]] = retrieve_record($newid);
  66.     $data["count"] ++;
  67.   }
  68.   return $data;
  69. }
  70.  
  71. function make_announcement_item($record) {
  72.   $header = "Announcement number " . $record["id"] . ", posted by " . $record["added_by"] .
  73.                                 (($record["date"]) ? " on " . make_date($record["date"], $record["time"]) . " UTC" : "");
  74.   $content = $record["contents"];
  75.   $header = htmlspecialchars($header);
  76.   return <<<HTML_code
  77.   <font face="Tahoma" size="-1"><i>$header</i></font><br />
  78.   <font face="Tahoma">$content</font>
  79. HTML_code;
  80. }
  81.  
  82. function make_announcements($amount, $start) {
  83.   $records = retrieve_records($amount, $start);
  84.   $data = "";
  85.   for ($i = 0; $i < $records['count']; $i++) {
  86.     $record = $records[$i];
  87.     if ($data != "")
  88.       $data .= "<br /><hr /><br />";
  89.     $data .= make_announcement_item($record);
  90.   }
  91.   return $data;
  92. }
  93.  
  94. function error_page() {
  95.   $text = <<<error_page
  96. <html><body bgcolor="#ffffff"><center><font face="Tahoma" size="7"><b>2032</b></font><br /><br />
  97. <font face="Arial" size="4">No aliens, we have a bad server <b>AND WE ADMIT IT</b>.<br />
  98. Nah, just joking... it's probably a database fail, refresh the page and try again.</font></center></body></html>
  99. error_page;
  100.   return $text;
  101. }
  102.  
  103. function get_value($valuename, $default) {
  104.   $data = $_REQUEST[$valuename];
  105.   if ($data === NULL) return $default; else return ((int) $data);
  106. }
  107.  
  108. function last_page($page, $pagesize) {
  109.   $result = mysql_query("SELECT COUNT(*) AS total FROM announcements");
  110.   $data = mysql_fetch_array($result);
  111.   $amount = (int) $data['total'];
  112.   if (($page * $pagesize) >= $amount) return true; else return false;
  113. }
  114.  
  115. function generate_content() {
  116.   $start = get_value("start", 0);
  117.   $length = get_value("length", 5);
  118.   $pageno = ((int) $start) / ((int) $length) + 1;
  119.   $status = database_connection(/* censored the database connection data, sorry */);
  120.   if ($status) return error_page();
  121.   $announcements = make_announcements($length, $start);
  122.   $table = '<table width="100%"><tr><td align="left">' .
  123.              ($pageno == 1 ? "&lt;-- Previous" : ('<a href="announce.php?start=' . (($pageno - 2) * $length) .
  124.                '&length=' . $length . '">&lt;--Previous</a>'))
  125.            . '</td><td align="center">' .
  126.              ("Announcements page $pageno")
  127.            . '</td><td align="right">' .
  128.              (last_page($pageno, $length) ? "Next --&gt;" : ('<a href="announce.php?start=' . ($pageno * $length) .
  129.                '&length=' . $length . '">Next --&gt;</a>'))
  130.            . '</td></tr></table>';
  131.   $result = '<html><body bgcolor="#ffffff">' . $announcements . "<br /><br />" . $table . "</body></html>";
  132.   return $result;
  133. }
  134.  
  135. echo generate_content();
  136.  
  137. ?>
Advertisement
RAW Paste Data Copied
Advertisement