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

Last change on this file since 15469 was 15469, checked in by jmpp, 15 years ago

Submitted by: jmpp@

Small typo corrections.... but I should note the news section of the site is
still not working, some greater php vodoo is needed.

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