From 90254eeabe4cc49975c8a43efc420ff65a3f04da Mon Sep 17 00:00:00 2001
From: Tom Lake <tswsl1989@sucs.org>
Date: Sun, 17 Jun 2012 15:42:14 +0000
Subject: [PATCH] Display random poems in the motd if there are no current news
 entries

---
 components/motd.php         | 23 ++++++++++++++++++++---
 components/motd_wrapper.php | 12 ++++++++++++
 2 files changed, 32 insertions(+), 3 deletions(-)
 create mode 100644 components/motd_wrapper.php

diff --git a/components/motd.php b/components/motd.php
index b72819d..5b811cb 100644
--- a/components/motd.php
+++ b/components/motd.php
@@ -24,11 +24,19 @@ function linkcounter($uri) {
 	return $links_count;
 }
 
+$poemmode = 0;
+
 $news = $DB->GetAll("SELECT * FROM news WHERE sticky=true AND expiry > now() ORDER BY date DESC");
+
 if (count($news)<3) {
   $news = $DB->GetAll("(SELECT * FROM news WHERE sticky=false AND expiry > now() ORDER by date DESC LIMIT ".(3-count($news)).") UNION SELECT * FROM news WHERE sticky=true AND expiry > now() ORDER BY date DESC");
 }
 
+if (count($news)<1) {
+    $news = $DB->GetAll("SELECT * FROM poems ORDER BY RANDOM() LIMIT 1");
+    $poemmode = 1;
+}
+
 
 $motd = "----------------------------- MESSAGE OF THE DAY ------------------------------\n\n";
 for ($i=count($news)-1;$i>-1;$i--) {
@@ -56,10 +64,19 @@ if (isset($uris)) {
 			unset($uris[$urinum]);
 	}
 	$motd .= "    ".$footer;
-} 
-$motd .= str_pad($nitem['author'], 78, " ", STR_PAD_LEFT)."\n\n";
+}
+$motd .= str_pad($nitem['author'], 78, " ", STR_PAD_LEFT)."\n";
+
+if ($poemmode == 1 && $nitem['submitter'] != '') {
+	$motd .= str_pad("Submitted by ".$nitem['submitter'], 78, " ", STR_PAD_LEFT)."\n";
+}
+$motd .= "\n";
 }
 $motd .= "---------------------------- [ http://sucs.org/ ] -----------------------------\n";
 
-file_put_contents($motd_file, $motd);
+if (date("z",time()-filemtime($motd_file)) < 1 && $poemmode == 1) {
+    // print "MOTD too recent to replace with a poem!\n";
+} else {
+    file_put_contents($motd_file, $motd);
+}
 ?>
diff --git a/components/motd_wrapper.php b/components/motd_wrapper.php
new file mode 100644
index 0000000..765e824
--- /dev/null
+++ b/components/motd_wrapper.php
@@ -0,0 +1,12 @@
+<?PHP
+require("/var/www/sucssite/htdocs/settings.php");
+require("/usr/share/php/adodb/adodb.inc.php");
+
+$dbname = 'sucssite';
+$dbuser = 'apache';
+
+$DB = NewADOConnection('postgres8');
+$DB->Connect('dbname='.$dbname.',dbuser='$dbuser);
+$DB->SetFetchMode(ADODB_FETCH_ASSOC);
+
+include("./motd.php");
-- 
GitLab