source: trunk/www/includes/news.inc @ 28382

Last change on this file since 28382 was 28382, checked in by jmpp@…, 13 years ago

Correct location of the rss feed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.2 KB
Line 
1<?php
2
3# $Id: news.inc 28382 2007-08-30 01:51:29Z jmpp@macports.org $
4# Copyright (c) 2003, OpenDarwin
5
6######################################################################
7
8# variables and includes
9$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
10$PHP_SELF = $_SERVER['PHP_SELF'];
11/* include_once("$DOCUMENT_ROOT/macports/includes/db.inc"); */
12$newsdb = 'macports_news';
13$newsdb_host = 'localhost';
14$newsdb_user = 'www';
15$newsdb_passwd = '';
16$newsdb_connect = mysql_pconnect($newsdb_host, $newsdb_user, $newsdb_pass) or die("Can't connect to the MacPorts news database!");
17
18
19######################################################################
20
21# create an RSS feed of the project news
22function create_rss() {
23  global $DOCUMENT_ROOT, $newsdb, $newsdb_connect;
24
25  $query = "SELECT id, title, news FROM $newsdb.headlines ORDER BY id DESC LIMIT 10";
26  $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
27  if(!$result) {
28    die("Error: $result.");
29  }
30
31  $open = fopen("$DOCUMENT_ROOT/macports/macports_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=\"utf-8\"?>\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>MacPorts Project News</title>\n";
44  $rss .= "    <link>http://www.macports.org/</link>\n";
45  $rss .= "    <description>MacPorts Project News</description>\n";
46  $rss .= "    <dc:language>en-us</dc:language>\n";
47  $rss .= "    <dc:creator>Jim Mock (mij@macports.org)</dc:creator>\n";
48  $rss .= "    <dc:rights>Copyright 2002-2007</dc:rights>\n";
49  $rss .= "    <dc:date>$rssdate</dc:date>\n";
50  $rss .= "    <admin:errorReportsTo rdf:resource=\"mailto:mij@macports.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.macports.org/archives.php?id=$row->id</link>\n";
57
58      $desc_query = "SELECT SUBSTRING_INDEX(news, ' ', 26) FROM $newsdb.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.macports.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 $newsdb, $newsdb_connect;
82
83  $query = "SELECT id, DATE_FORMAT(timestamp, '%e %b %Y, %l:%i %p') AS f_timestamp, title, news FROM $newsdb.headlines ORDER BY id DESC LIMIT 5";
84  $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
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 $newsdb, $newsdb_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 $newsdb.headlines WHERE id='$id'";
109  $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
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, $newsdb, $newsdb_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 $newsdb.headlines (timestamp, title, news) VALUES (NOW(), '$title', '$news')";
156      $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
157      if(!$result) {
158        die("Error: $result.");
159      }
160      echo "<p>The addition was successful.  You can either <a href=\"/macports/admin/list.php\">list all existing headlines,
161      <a href=\"/macports/admin/add.php\">add another headline</a>, or <a href=\"/macports/index.php\">return to the MacPorts site</a>.</p>\n\n";
162      create_rss();
163    }
164    else {
165      echo "<p>The following errors have occurred:</p>\n\n";
166      echo "<ul>\n";
167      for($x=0; $x<sizeof($errorList); $x++) {
168        echo "<li>$errorList[$x]</li>\n";
169      }
170      echo "</ul>\n\n";
171    }
172  }
173}
174
175######################################################################
176
177# print the form used to edit project news
178function print_edit_headline($id) {
179  global $PHP_SELF, $newsdb, $newsdb_connect;
180
181  if(!$_POST['submit']) {
182    $id = $_GET['id'];
183    $query = "SELECT title, news FROM $newsdb.headlines WHERE id='$id'";
184    $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
185    if(!$result) {
186      die("Error: $result.");
187    }
188    if(mysql_num_rows($result) > 0) {
189      $row = mysql_fetch_object($result);
190      echo "<p>Use the form below to edit project news.</p>\n\n";
191      echo "<form action=\"$PHP_SELF?id=$id\" method=\"post\">\n";
192      echo "<p>Title:&nbsp;&nbsp;<input type=\"text\" name=\"title\" size=\"40\" value=\"$row->title\" /></p>\n";
193      echo "<p>News:</p>\n";
194      echo "<p><textarea name=\"news\" cols=\"70\" rows=\"20\">";
195      echo htmlentities($row->news);
196      echo "</textarea></p>\n";
197      echo "<p><input type=\"submit\" name=\"submit\" value=\"Edit\" /></p>\n";
198      echo "</form>\n\n";
199    }
200    else {
201      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";
202    }
203  }
204  else {
205    $title = $_POST['title'];
206    $news = $_POST['news'];
207    $errorList = array();
208    $count = 0;
209    if(!$title) {
210      $errorList[$count] = 'Invalid entry: Title';
211      $count++;
212    }
213    if(!$news) {
214      $errorList[$count] = 'Invalid entry: News';
215      $count++;
216    }
217    if(sizeof($errorList) == 0) {
218      $query = "UPDATE $newsdb.headlines SET title='$title', news='$news' WHERE id='$id'";
219      $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
220
221      if(!$result) {
222        die("Error: $result.");
223      }
224      echo "<p>The update was successful.  You can either <a href=\"/macports/admin/list.php\">list all existing headlines,
225      <a href=\"/macports/admin/add.php\">add another headline</a>, or <a href=\"/macports/index.php\">return to the MacPorts site</a>.</p>\n\n";
226      create_rss();
227    }
228    else {
229      echo "<p>The following errors have occurred:</p>\n\n";
230      echo "<ul>\n";
231      for($x=0; $x<sizeof($errorList); $x++) {
232        echo "<li>$errorList[$x]</li>\n";
233      }
234      echo "</ul>\n\n";
235    }
236  }
237}
238
239######################################################################
240
241# print a list of all existing headlines
242function print_all_headlines() {
243  global $newsdb, $newsdb_connect;
244
245  echo "<p>Below is a list of all existing news headlines.  They can be viewed, edited, or deleted from this interface.
246  Alternatively, you can also <a href=\"/macports/admin/add.php\">add news</a>.</p>\n\n";
247
248  $query = "SELECT id, title, DATE_FORMAT(timestamp, '%e %b %Y, %l:%i %p') AS f_timestamp FROM $newsdb.headlines ORDER BY id DESC";
249  $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
250  if(!$result) {
251    die("Error: $result.");
252  }
253  if(mysql_num_rows($result) > 0) {
254    while($row = mysql_fetch_object($result)) {
255      echo "<p><a href=\"/macports/archives.php?id=$row->id\">$row->title</a><br />&nbsp&nbsp;$row->f_timestamp&nbsp;&nbsp;|&nbsp;&nbsp;
256           <a href=\"/macports/admin/edit.php?id=$row->id\">edit</a></p>\n\n";
257    }
258  }
259  else {
260    echo "<p>No headlines are available.</p>\n\n";
261  }
262}
263
264######################################################################
265
266# print a list of all existing headlines without the admin foobage
267# (this is cheap and could be better)
268function print_all_headlines_nonadmin() {
269  global $newsdb, $newsdb_connect;
270
271  $query = "SELECT id, title, DATE_FORMAT(timestamp, '%e %b %Y, %l:%i %p') AS f_timestamp FROM $newsdb.headlines ORDER BY id DESC";
272  $result = mysql_query($query) or die("Error: $query (" . mysql_error() . ")");
273  if(!$result) {
274    die("Error: $result.");
275  }
276  if(mysql_num_rows($result) > 0) {
277    while($row = mysql_fetch_object($result)) {
278      echo "<p><a href=\"/macports/archives.php?id=$row->id\">$row->title</a><br />&nbsp&nbsp;$row->f_timestamp</p>\n\n";
279    }
280  }
281  else {
282    echo "<p>No headlines are available.</p>\n\n";
283  }
284}
285
286######################################################################
287
288?>
Note: See TracBrowser for help on using the repository browser.