From e4a4e5149d55cf01a86f26392c52fdca803654ce Mon Sep 17 00:00:00 2001 From: arno Date: Sun, 26 Jul 2009 10:59:35 +0200 Subject: [PATCH] rss feed --- api.php | 6 +-- build.sh | 2 +- inc/db/anydb.php | 12 ++++- inc/db/mysql.php | 29 +++++++++-- inc/settings.php | 3 ++ inc/templates_index.php | 1 + inc/utils.php | 21 ++++++-- items.php | 2 +- news.php | 111 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 174 insertions(+), 13 deletions(-) create mode 100644 news.php diff --git a/api.php b/api.php index 93f27bd..6ed26c9 100644 --- a/api.php +++ b/api.php @@ -20,7 +20,7 @@ function success_feature ($feature, $request) { $res .= "" . ($feature->imgpath ? - full_url_from_filename ($feature->imgpath) + full_url_from_imgpath ($feature->imgpath) : "") . ""; @@ -201,7 +201,7 @@ function main ($con) { $description = unquote ($_POST ["description"]); try { - $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description); + $new_feature = new feature ($id, $lon, $lat, $imgpath, $title, $description, 0); } catch (Exception $e) { request_error (); } @@ -239,7 +239,7 @@ function main ($con) { $title = unquote ($_POST ["title"]); $description = unquote ($_POST ["description"]); try { - $feature = new feature (null, $lon, $lat, $imgpath, $title, $description); + $feature = new feature (null, $lon, $lat, $imgpath, $title, $description, 0); } catch (Exception $e) { request_error (); } diff --git a/build.sh b/build.sh index 1d91b80..8224f87 100755 --- a/build.sh +++ b/build.sh @@ -15,7 +15,7 @@ mkdir -p $DESTDIR cp -RLp inc/ $DESTDIR/ # other php files -cp -p admin.*php index.*php wizard.*php api.php items.php logout.php $DESTDIR/ +cp -p admin.*php index.*php wizard.*php news.php api.php items.php logout.php $DESTDIR/ # media cp -RLp media/ $DESTDIR/ diff --git a/inc/db/anydb.php b/inc/db/anydb.php index 47d9a06..bb80032 100644 --- a/inc/db/anydb.php +++ b/inc/db/anydb.php @@ -9,10 +9,11 @@ class feature { private $imgpath = null; private $title = null; private $description = null; + private $date = 0; const err_lonlat_invalid = 1; - function __construct ($id, $lon, $lat, $imgpath, $title, $description) { + function __construct ($id, $lon, $lat, $imgpath, $title, $description, $date) { $this->imgpath = $imgpath; // id @@ -26,6 +27,9 @@ class feature { // description $this->description = $description; + // date + $this->date = $date; + // longitude if (!isset ($lon) || !is_numeric ($lon) || ($lon < -180) || ($lon > 180)) { @@ -118,6 +122,12 @@ interface anydbConnection { */ public function listfeatures(); + /* + * returns the most recent features sorted by date. If $num_features is not + * defined or is null, returns all features sorted by date. + */ + public function mostrecentfeatures($num_features); + /* * returns true if a feature with imgpath exists */ diff --git a/inc/db/mysql.php b/inc/db/mysql.php index 8fa49d5..8e3fcf8 100644 --- a/inc/db/mysql.php +++ b/inc/db/mysql.php @@ -120,7 +120,7 @@ class mysqlConnection implements anydbConnection { $id = mysql_insert_id (); return new feature ($id, $feature->lon, $feature->lat, $feature->imgpath, $feature->title, - $feature->description); + $feature->description, $feature->date); } } @@ -134,7 +134,8 @@ class mysqlConnection implements anydbConnection { public function getfeature ($id) { $query = sprintf ("SELECT id, imgpath, title, description, AsText(location) - AS location FROM %sitems WHERE id = '%s';", + AS location, UNIX_TIMESTAMP(date) AS date FROM %sitems + WHERE id = '%s';", $this->dbprefix, mysql_real_escape_string ($id)); $row = mysql_fetch_assoc ($this->_execute_query ($query)); if ($row === false) { @@ -145,7 +146,7 @@ class mysqlConnection implements anydbConnection { public function listfeatures () { $query = sprintf ("SELECT id, imgpath, title, description, AsText(location) - AS location FROM %sitems;", + AS location, UNIX_TIMESTAMP(date) AS date FROM %sitems;", $this->dbprefix); $features = array (); @@ -159,6 +160,25 @@ class mysqlConnection implements anydbConnection { return $features; } + public function mostrecentfeatures ($num_features) { + $query = sprintf ("SELECT id, imgpath, title, description, + AsText(location) AS location, UNIX_TIMESTAMP(date) + AS date FROM %sitems ORDER BY date DESC", + $this->dbprefix); + if ($num_features) { + $query .= sprintf (" LIMIT %d", $num_features); + } + $features = array (); + $res = $this->_execute_query ($query); + while ($row = mysql_fetch_assoc ($res)) { + $feature = $this->_feature_frow_row ($row); + if (isset ($feature)) { + $features[] = $feature; + } + } + return $features; + } + public function imgpath_exists ($imgpath) { $query = sprintf ("SELECT COUNT(*) FROM %sitems WHERE imgpath LIKE '%s';", $this->dbprefix, mysql_real_escape_string ($imgpath)); @@ -226,7 +246,8 @@ class mysqlConnection implements anydbConnection { $lat = $matches [2]; try { $feature = new feature ($row ["id"], $lon, $lat, $row ["imgpath"], - $row ["title"], $row ["description"]); + $row ["title"], $row ["description"], + $row ["date"]); } catch (Exception $e) { return null; } diff --git a/inc/settings.php b/inc/settings.php index 68bc142..3f3a358 100644 --- a/inc/settings.php +++ b/inc/settings.php @@ -24,6 +24,9 @@ define ("UPLOADDIR", "upload"); // title of your website define ("SITETITLE", "SYP"); +// email contact for webmaster. +define ("WEBMASTERMAIL", ""); + // url of images directory. If empty, syp will try to guess it from syp admin // location and UPLOADDIR. So, you need to set that variable if host is // different between syp admin and images directory. Or if protocol is diff --git a/inc/templates_index.php b/inc/templates_index.php index 2d59926..7fd2bb4 100644 --- a/inc/templates_index.php +++ b/inc/templates_index.php @@ -16,6 +16,7 @@ $bbox = $connection->mbr (); <?php echo defined ("SITETITLE") ? SITETITLE : "SYP"?> + diff --git a/inc/utils.php b/inc/utils.php index a0824c8..04aeff9 100644 --- a/inc/utils.php +++ b/inc/utils.php @@ -4,6 +4,16 @@ require_once ("inc/settings.php"); +function gethost() { + $host = $_SERVER ["HTTP_HOST"]; + $colpos = strpos ($host, ':'); + // some web clients add port informations in Host header + if ($colpos !== false) { + $host = substr ($host, 0, $colpos); + } + return $host; +} + function unquote($gpc_str) { if (!isset ($gpc_str)) { return $gpc_str; @@ -15,13 +25,19 @@ function unquote($gpc_str) { } } -function full_url_from_filename ($filename) { +function full_url_from_imgpath ($filename) { if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) { return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename); } $rel_path = UPLOADDIR . "/" . rawurlencode ($filename); + return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename)); +} + +function full_url_from_path ($path) { + $rel_path = $path; + while (substr($rel_path, 0, 2) == "./") { // strips ./ $rel_path = substr ($rel_path, 2); } @@ -44,7 +60,7 @@ function full_url_from_filename ($filename) { $path = "$script_dir/$rel_path"; } - $host = $_SERVER ["HTTP_HOST"]; + $host = gethost(); $port = $_SERVER ["SERVER_PORT"]; if ($_SERVER ["HTTPS"] == "on") { $proto = "https"; @@ -61,5 +77,4 @@ function full_url_from_filename ($filename) { return "$proto://$host$port$path"; } - ?> diff --git a/items.php b/items.php index 3efed08..af0192f 100644 --- a/items.php +++ b/items.php @@ -23,7 +23,7 @@ function main ($features) { $title = htmlspecialchars ($feature->title, ENT_QUOTES); $description = htmlspecialchars ($feature->description, ENT_QUOTES); $imgurl = ($feature->imgpath ? - full_url_from_filename ($feature->imgpath) + full_url_from_imgpath ($feature->imgpath) : ""); $lon = $feature->lon; $lat = $feature->lat; diff --git a/news.php b/news.php new file mode 100644 index 0000000..615989f --- /dev/null +++ b/news.php @@ -0,0 +1,111 @@ +date); + + $matches = array(); + if (preg_match ('/^([\-+])(\d{2})(\d{2})$/', + date ('O', $feature->date), $matches)) { + $date .= $matches [1] . $matches [2] . ':' . $matches [3]; + } else { + $date .= 'Z'; + } + return $date; +} + +function unique_id_from_feature ($feature) { + // method from http://diveintomark.org/archives/2004/05/28/howto-atom-id#other + $date = date('Y-m-d', $feature->date); + $res = sprintf("tag:%s,%s:%d", gethost(), $date, $feature->id); + return $res; +} + +function unique_id_from_site () { + $id = md5 (full_url_from_path ("")); + $res = sprintf("tag:%s,1970-01-01:%d", gethost, $id); + return $res; +} + +function main ($features) { + print " +\n"; + + printf(" \n", + full_url_from_path ("")); + printf(" \n", + full_url_from_path (basename ($_SERVER ["PHP_SELF"]))); + printf(" %s\n", unique_id_from_site()); + + if (count ($features) > 0) { + printf(" %s\n", date3339 ($features[0])); + } + + if (SITETITLE) { + printf (" %s\n", SITETITLE); + } + + if (WEBMASTERMAIL) { + printf (" \n"); + printf (" %s\n", WEBMASTERMAIL); + printf(" \n"); + } + + print "\n"; + + foreach ($features as $feature) { + print (" \n"); + + if ($feature->title) { + $title = htmlspecialchars ($feature->title, ENT_QUOTES); + } else { + $title = $feature->id; + } + printf (" %s\n", $title); + + $rel_url = sprintf ("index.php?lat=%.18F&lon=%.18F&zoom=12", + $feature->lat, $feature->lon); + $link = htmlspecialchars (full_url_from_path ($rel_url), ENT_QUOTES); + printf (" \n", $link); + + printf (" %s\n", unique_id_from_feature ($feature)); + + printf (" %s\n", date3339 ($feature)); + + if ($feature->description) { + $contentHTML = sprintf ("

%s

", htmlspecialchars ($feature->description, ENT_QUOTES)); + } else { + $contentHTML = sprintf ("

%s

", htmlspecialchars ($feature->title, ENT_QUOTES)); + } + + if (strlen ($contentHTML) != 0) { + printf(" + %s + \n", htmlspecialchars ($contentHTML)); + } + + printf(" %.18F %.18F\n", + $feature->lat, $feature->lon); + + printf("
\n\n"); + } + print '
'; +} + +try { + $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX); + $features = $connection->mostrecentfeatures (10); +} catch (Exception $e) { + exit ("server error"); +} +main ($features); +?> -- 2.39.2