]> dev.renevier.net Git - syp.git/blob - news.php
bump version to 0.2
[syp.git] / news.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 require_once ("./inc/settings.php");
6 require_once ("./inc/db/mysql.php");
7 require_once ("./inc/utils.php");
8
9 function headers_callback ($output) {
10     $etag = md5 ($output);
11     if ((isset ($_SERVER ["HTTP_IF_NONE_MATCH"])) && 
12          ($_SERVER ["HTTP_IF_NONE_MATCH"] == $etag)) {
13          header ("HTTP/1.1 304 Not Modified");
14          exit ();
15     }
16
17     header ("Content-type: application/atom+xml; charset=UTF-8");
18     header ("Cache-control: must-revalidate");
19     header (sprintf ("ETag: %s", $etag));
20
21     return $output;
22 }
23
24 function date3339 ($feature) {
25     $date = date ('Y-m-d\TH:i:s', $feature->date);
26
27     $matches = array();
28     if (preg_match ('/^([\-+])(\d{2})(\d{2})$/',
29                     date ('O', $feature->date), $matches)) {
30         $date .= $matches [1] . $matches [2] . ':' . $matches [3];
31     } else {
32         $date .= 'Z';
33     }
34     return $date;
35 }
36
37 // method from http://diveintomark.org/archives/2004/05/28/howto-atom-id#other
38 function unique_id_from_feature ($feature) {
39     $date = date('Y-m-d', $feature->date);
40     $res = sprintf("tag:%s,%s:%d", gethost(), $date, $feature->id);
41     return $res;
42 }
43
44 function unique_id_from_site () {
45     $id = md5 (full_url_from_path (""));
46     $res = sprintf("tag:%s,1970-01-01:%d", gethost, $id);
47     return $res;
48 }
49
50 function main ($features) {
51     printf ("<?xml version=\"1.0\" encoding=\"utf-8\"?>
52 <feed xmlns=\"http://www.w3.org/2005/Atom\"
53       xmlns:georss=\"http://www.georss.org/georss\">\n");
54
55     printf("  <link rel=\"alternate\" type=\"text/html\" href=\"%s\"/>\n",
56             full_url_from_path (""));
57     printf("  <link rel=\"self\" href=\"%s\" type=\"application/atom+xml\"/>\n",
58             full_url_from_path (basename ($_SERVER ["PHP_SELF"])));
59     printf("  <id>%s</id>\n", unique_id_from_site());
60
61     if (count ($features) > 0) {
62         printf("  <updated>%s</updated>\n", date3339 ($features[0]));
63     }
64
65     if (SITETITLE) {
66         printf ("  <title>%s</title>\n", SITETITLE);
67     }
68
69     if (WEBMASTERMAIL) {
70         printf ("  <author>\n");
71         printf ("    <email>%s</email>\n", WEBMASTERMAIL);
72         printf("   </author>\n");
73     }
74
75     printf ("\n");
76
77     foreach ($features as $feature) {
78         printf ("    <entry>\n");
79
80         if ($feature->title) {
81             $title = htmlspecialchars ($feature->title, ENT_QUOTES);
82         } else {
83             $title = $feature->id;
84         }
85         printf ("      <title>%s</title>\n", $title);
86
87         $rel_url = sprintf ("index.php?lat=%.18F&lon=%.18F&zoom=12",
88                                          $feature->lat, $feature->lon);
89         $link = htmlspecialchars (full_url_from_path ($rel_url), ENT_QUOTES);
90         printf ("      <link rel=\"alternate\" type=\"text/html\" href=\"%s\"/>\n", $link);
91
92         printf ("      <id>%s</id>\n", unique_id_from_feature ($feature));
93
94         printf ("      <updated>%s</updated>\n", date3339 ($feature));
95
96         if ($feature->description) {
97             $contentHTML = sprintf ("<p>%s</p>", htmlspecialchars ($feature->description, ENT_QUOTES));
98         } else {
99             $contentHTML = sprintf ("<p>%s</p>", htmlspecialchars ($feature->title, ENT_QUOTES));
100         }
101
102         // FIXME: we consider thumbnail are correctly sized if gd library is
103         // installed. That may not always be true. For example if gd was installed
104         // after images were initially uploaded.
105         if (function_exists ("imagecreatefromjpeg")) { 
106             if ($feature->imgpath) {
107                 $imgurl = image_url_from_imgpath ($feature->imgpath);
108                 $thumburl = thumb_url_from_imgpath ($feature->imgpath);
109                 $contentHTML .= sprintf ('<a href="%s"><img alt="%s" src="%s"></a>', $imgurl, $alt, $thumburl);
110             }
111         }
112
113         if (strlen ($contentHTML) != 0) {
114             printf ("       <content type=\"html\">
115                 %s
116        </content>\n", htmlspecialchars ($contentHTML));
117         }
118
119         printf("      <georss:point>%.18F %.18F</georss:point>\n",
120                     $feature->lat, $feature->lon);
121
122         printf("    </entry>\n\n");
123     }
124     printf ("</feed>");
125 }
126
127 try {
128     $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
129     $features = $connection->mostrecentfeatures (10);
130 } catch (Exception $e) {
131     exit ("server error");
132 }
133
134 ob_start ("headers_callback");
135 main ($features);
136 ob_end_flush ()
137 ?>