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

Last change on this file since 8672 was 8672, checked in by wbb4, 16 years ago

Bug:
Submitted by:
Reviewed by:
Approved by:
Obtained from:

Since I'm not sure mij@'s original intent with the archives/id.php, I've created an archives.php, which takes id as an argument (using my very rusty and basic PHP knowledge). So, this is a commit to see if it will work! I simply made changes to the RSS feed to link to archives.php?id=id.

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