source: trunk/www/includes/functions.inc @ 15471

Last change on this file since 15471 was 15471, checked in by yeled, 15 years ago

back out this count(*) malarkey
Bug:
Submitted by:
Reviewed by:
Approved by:
Obtained from:

  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1<?php
2
3# $Id: functions.inc,v 1.9 2005/12/13 06:07:35 yeled Exp $
4# Copyright (c) 2003, OpenDarwin
5
6######################################################################
7
8# variables and includes
9$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
10$PHP_SELF = $_SERVER['PHP_SELF'];
11include_once("$DOCUMENT_ROOT/includes/db.inc");
12$connect = mysql_pconnect($host, $user, $pass) or die("Can't connect to db!");
13mysql_select_db($db);
14
15
16
17######################################################################
18
19# create an RSS feed of the project news
20function create_rss() {
21  global $DOCUMENT_ROOT, $connect;
22
23  $query = "SELECT id, title, news FROM headlines ORDER BY id DESC LIMIT 10";
24  $result = mysql_query($query) or die("Error: $query.");
25  if(!$result) {
26    die("Error: $result.");
27  }
28
29  $open = fopen("$DOCUMENT_ROOT/dp_news.xml", "w+");
30  $rssdate = substr_replace(date("Y-m-d\TH:i:sO", time()),':', -2, 0);
31
32  $rss = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
33  $rss .= "<rss version=\"2.0\"\n";
34  $rss .= "  xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n";
35  $rss .= "  xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"\n";
36  $rss .= "  xmlns:admin=\"http://webns.net/mvcb/\"\n";
37  $rss .= "  xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
38  $rss .= "  xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n\n";
39
40  $rss .= "  <channel>\n";
41  $rss .= "    <title>DarwinPorts Project News</title>\n";
42  $rss .= "    <link>http://www.darwinports.org/</link>\n";
43  $rss .= "    <description>DarwinPorts Project News</description>\n";
44  $rss .= "    <dc:language>en-us</dc:language>\n";
45  $rss .= "    <dc:creator>Jim Mock (mij@opendarwin.org)</dc:creator>\n";
46  $rss .= "    <dc:rights>Copyright 2002-2003</dc:rights>\n";
47  $rss .= "    <dc:date>$rssdate</dc:date>\n";
48  $rss .= "    <admin:errorReportsTo rdf:resource=\"mailto:mij@opendarwin.org\" />\n";
49
50  if(mysql_num_rows($result) > 0) {
51    while($row = mysql_fetch_object($result)) {
52      $rss .= "    <item>\n";
53      $rss .= "      <title>$row->title</title>\n";
54      $rss .= "      <link>http://www.darwinports.org/archives.php?id=$row->id</link>\n";
55
56      $desc_query = "SELECT SUBSTRING_INDEX(news, ' ', 26) FROM headlines WHERE id=$row->id";
57      $desc_result = mysql_query($desc_query);
58      $desc_row = mysql_fetch_row($desc_result);
59
60      $description = $desc_row[0];
61
62      $rss .= "      <description><![CDATA[$description...</p>]]></description>\n";
63      $rss .= "      <guid isPermaLink=\"true\">http://www.darwinports.org/archives.php?id=$row->id</guid>\n";
64      $rss .= "      <content:encoded><![CDATA[$row->news]]></content:encoded>\n";
65      $rss .= "    </item>\n";
66    }
67  }
68  $rss .= "  </channel>\n";
69  $rss .= "</rss>\n";
70
71  $write = fwrite($open, $rss);
72  $close = fclose($open);
73}
74
75######################################################################
76
77# print the project news
78function print_headlines() {
79  global $connect;
80
81  $query = "SELECT id, DATE_FORMAT(timestamp, '%e %b %Y, %l:%i %p') AS f_timestamp, title, news FROM headlines ORDER BY id DESC LIMIT 5";
82  $result = mysql_query($query) or die("Error: $query.");
83  if(!$result) {
84    die("Error: $result.");
85  }
86
87  if(mysql_num_rows($result) > 0) {
88    while($row = mysql_fetch_object($result)) {
89      echo "<h5 class=\"subhdr\">$row->title</h5>\n";
90      echo "<h6 class=\"date\">$row->f_timestamp</h6>\n";
91      echo "$row->news\n\n";
92    }
93  }
94  else {
95    echo "<p>No headlines are available at this time.</p>\n";
96  }
97}
98
99######################################################################
100
101# display a single headline
102function print_headline() {
103  global $connect;
104
105  $id = $_GET['id'];
106  $query = "SELECT id, DATE_FORMAT(timestamp, '%e %b %Y, %l:%i %p') AS f_timestamp, title, news FROM headlines WHERE id='$id'";
107  $result = mysql_query($query) or die("Error: $query.");
108  if(!$result) {
109    die("Error: $result.");
110  }
111
112  $row = mysql_fetch_object($result);
113  if($row) {
114    echo "<h5 class=\"subhdr\">$row->title</h5>\n";
115    echo "<h6 class=\"date\">$row->f_timestamp</h6>\n";
116    echo "$row->news\n\n";
117  }
118  else {
119    echo "<h2 class=\"hdr\">Error!</h2>\n";
120    echo "<p>The requested headline could not be found.  It is either not in the database, or an error has occurred.</p>\n";
121  }
122}
123
124######################################################################
125
126# print the form used to add project news
127function print_add_headline() {
128  global $PHP_SELF, $connect;
129
130  if(!$_POST['submit']) {
131    echo "<p>Use the form below to add project news.</p>\n\n";
132    echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
133    echo "<p>Title:&nbsp;&nbsp;<input type=\"text\" name=\"title\" size=\"40\" /></p>\n";
134    echo "<p>News:</p>\n";
135    echo "<p><textarea name=\"news\" cols=\"70\" rows=\"20\"></textarea></p>\n";
136    echo "<p><input type=\"submit\" name=\"submit\" value=\"Submit\" /></p>\n";
137    echo "</form>\n\n";
138  }
139  else {
140    $title = $_POST['title'];
141    $news = $_POST['news'];
142    $errorList = array();
143    $count = 0;
144    if(!$title) {
145      $errorList[$count] = 'Invalid entry: Title';
146      $count++;
147    }
148    if(!$news) {
149      $errorList[$count] = 'Invalid entry: News';
150      $count++;
151    }
152    if(sizeof($errorList) == 0) {
153      $query = "INSERT INTO headlines (timestamp, title, news) VALUES (NOW(), '$title', '$news')";
154      $result = mysql_query($query) or die("Error: $query.");
155      if(!$result) {
156        die("Error: $result.");
157      }
158      echo "<p>The addition was successful.  You can either <a href=\"list.php\">list all existing headlines, <a href=\"add.php\">add another headline</a>, or <a href=\"/\">return to the DarwinPorts site</a>.</p>\n\n";
159      create_rss();
160    }
161    else {
162      echo "<p>The following errors have occurred:</p>\n\n";
163      echo "<ul>\n";
164      for($x=0; $x<sizeof($errorList); $x++) {
165        echo "<li>$errorList[$x]</li>\n";
166      }
167      echo "</ul>\n\n";
168    }
169  }
170}
171
172######################################################################
173
174# print the form used to edit project news
175function print_edit_headline($id) {
176  global $PHP_SELF, $connect;
177
178  if(!$_POST['submit']) {
179    $id = $_GET['id'];
180    $query = "SELECT title, news FROM headlines WHERE id='$id'";
181    $result = mysql_query($query) or die("Error: $query.");
182    if(!$result) {
183      die("Error: $result.");
184    }
185    if(mysql_num_rows($result) > 0) {
186      $row = mysql_fetch_object($result);
187      echo "<p>Use the form below to edit project news.</p>\n\n";
188      echo "<form action=\"$PHP_SELF?id=$id\" method=\"post\">\n";
189      echo "<p>Title:&nbsp;&nbsp;<input type=\"text\" name=\"title\" size=\"40\" value=\"$row->title\" /></p>\n";
190      echo "<p>News:</p>\n";
191      echo "<p><textarea name=\"news\" cols=\"70\" rows=\"20\">";
192      echo htmlentities($row->news);
193      echo "</textarea></p>\n";
194      echo "<p><input type=\"submit\" name=\"submit\" value=\"Edit\" /></p>\n";
195      echo "</form>\n\n";
196    }
197    else {
198      echo "<p>The requested news headline could not be found.  It is either not in the database, or an error has occurred.  Please try again.</p>\n\n";
199    }
200  }
201  else {
202    $title = $_POST['title'];
203    $news = $_POST['news'];
204    $errorList = array();
205    $count = 0;
206    if(!$title) {
207      $errorList[$count] = 'Invalid entry: Title';
208      $count++;
209    }
210    if(!$news) {
211      $errorList[$count] = 'Invalid entry: News';
212      $count++;
213    }
214    if(sizeof($errorList) == 0) {
215      $query = "UPDATE headlines SET title='$title', news='$news' WHERE id='$id'";
216      $result = mysql_query($query) or die("Error: $query.");
217
218      if(!$result) {
219        die("Error: $result.");
220      }
221      echo "<p>The update was successful.  You can either <a href=\"list.php\">list all existing headlines, <a href=\"add.php\">add another headline</a>, or <a href=\"/\">return to the DarwinPorts site</a>.</p>\n\n";
222      create_rss();
223    }
224    else {
225      echo "<p>The following errors have occurred:</p>\n\n";
226      echo "<ul>\n";
227      for($x=0; $x<sizeof($errorList); $x++) {
228        echo "<li>$errorList[$x]</li>\n";
229      }
230      echo "</ul>\n\n";
231    }
232  }
233}
234
235######################################################################
236
237# print a list of all existing headlines
238function print_all_headlines() {
239  global $connect;
240
241  echo "<p>Below is a list of all existing news headlines.  They can be viewed, edited, or deleted from this interface.  Alternatively, you can also <a href=\"add.php\">add news</a>.</p>\n\n";
242
243  $query = "SELECT id, title, DATE_FORMAT(timestamp, '%e %b %Y, %l:%i %p') AS f_timestamp FROM headlines ORDER BY id DESC";
244  $result = mysql_query($query) or die("Error: $query.");
245  if(!$result) {
246    die("Error: $result.");
247  }
248  if(mysql_num_rows($result) > 0) {
249    while($row = mysql_fetch_object($result)) {
250      echo "<p><a href=\"/archives.php?id=$row->id\">$row->title</a><br />&nbsp&nbsp;$row->f_timestamp&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"edit.php?id=$row->id\">edit</a></p>\n\n";
251    }
252  }
253  else {
254    echo "<p>No headlines are available.</p>\n\n";
255  }
256}
257
258######################################################################
259
260# print a list of all existing headlines without the admin foobage
261# (this is cheap and could be better)
262function print_all_headlines_nonadmin() {
263  global $connect;
264
265  $query = "SELECT id, title, DATE_FORMAT(timestamp, '%e %b %Y, %l:%i %p') AS f_timestamp FROM headlines ORDER BY id DESC";
266  $result = mysql_query($query) or die("Error: $query.");
267  if(!$result) {
268    die("Error: $result.");
269  }
270  if(mysql_num_rows($result) > 0) {
271    while($row = mysql_fetch_object($result)) {
272      echo "<p><a href=\"/archives.php?id=$row->id\">$row->title</a><br />&nbsp&nbsp;$row->f_timestamp</p>\n\n";
273    }
274  }
275  else {
276    echo "<p>No headlines are available.</p>\n\n";
277  }
278}
279
280######################################################################
281
282?>
Note: See TracBrowser for help on using the repository browser.