]> dev.renevier.net Git - syp.git/blobdiff - inc/db/anydb.php
err_lonlat_invalid exceptions not thrown correctly
[syp.git] / inc / db / anydb.php
index 7706506420d5615de4d7a98b5cdff4a8c4d1d855..ca61b0f3135877af1c96737f6d9b2f2be8259e94 100644 (file)
@@ -2,53 +2,24 @@
 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
    license. */
 
-// XXX: put that function somewhere else. It's used in changes.php
-function relative_path ($url_str) {
-    $url = parse_url ($url_str);
-    if ($url ['host'] != $_SERVER ['HTTP_HOST']) {
-        return null;
-    }
-
-    // first strip common directory names
-    $url_path = split ('/', $url ['path']);
-    $script_path = split ('/', $_SERVER ['SCRIPT_NAME']);
-    $len = min (count ($url_path), count ($script_path));
-    while ($url_path [0] == $script_path [0]) {
-        array_shift ($url_path);
-        array_shift ($script_path);
-    }
-
-    // $url_path contains $script_path; abort
-    if (count ($script_path) == 0) { 
-        return null;
-    }
-
-    // now, create relative path
-    $relpath = "";
-    for ($i = 0; $i < (count ($script_path) - 1); $i++) {
-        $relpath .= "../";
-    }
-    $relpath .= join ("/", $url_path);
-    return $relpath;
-}
-
 class feature {
-    private $imgurl = null;
-    private $title = null;
-    private $description = null;
+    private $id = null;
     private $lon = null;
     private $lat = null;
+    private $imgpath = null;
+    private $title = null;
+    private $description = null;
+    private $date = 0;
+    private $user = null;
 
     const err_lonlat_invalid = 1;
 
-    function __construct ($imgurl, $title, $description, $lon, $lat) {
-        // imgurl
-        // tries to transform full url in relative path
-        $relpath = relative_path ($imgurl);
-        if (isset ($relpath)) {
-            $this->imgurl = $relpath;
-        } else {
-            $this->imgurl = $imgurl;
+    function __construct ($id, $lon, $lat, $imgpath, $title, $description, $date, $user) {
+        $this->imgpath = $imgpath;
+
+        // id
+        if (isset ($id)) {
+            $this->id = $id;
         }
 
         // title
@@ -57,17 +28,23 @@ class feature {
         // description
         $this->description = $description;
 
+        // date
+        $this->date = $date;
+
+        // user
+        $this->user = $user;
+
         // longitude
         if (!isset ($lon) || !is_numeric ($lon) ||
              ($lon < -180) || ($lon > 180)) {
-            throw new Exception ($this->err_lonlat_invalid);
+            throw new Exception (self::err_lonlat_invalid);
         }
         $this->lon = $lon;
 
         // latitude
         if (!isset ($lat) || !is_numeric ($lat) ||
              ($lat < -90) || ($lat > 90)) {
-            throw new Exception ($this->err_lonlat_invalid);
+            throw new Exception (self::err_lonlat_invalid);
         }
         $this->lat = $lat;
     }
@@ -81,25 +58,6 @@ class feature {
         throw new Exception ('properties can only be set in constructor');
     }
 
-    public function imgurl_exists () {
-        if (file_exists ($this->imgurl)) {
-            return true;
-        }
-        if (!function_exists ("curl_init")) {
-            return false;
-        }
-        $handle   = curl_init ($this->imgurl);
-        if ($handle === false)
-        {
-            return false;
-        }
-        curl_setopt ($handle, CURLOPT_NOBODY, true);
-        curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 10);
-        curl_setopt ($handle, CURLOPT_TIMEOUT, 10);
-        $connectable = curl_exec ($handle);
-        curl_close ($handle);  
-        return $connectable;
-    }
 }
 
 interface anydbConnection {
@@ -119,10 +77,10 @@ interface anydbConnection {
     public function users_table_exists();
 
     /*
-     * create users table; if $error_if_exists is true; throws an err_query
-     * error in case users table already exists.
+     * create users table; 
+     * throws an err_query error in case users table already exists.
      */
-    public function create_users_table($error_if_exists);
+    public function create_users_table();
 
     /*
      * return true if items table already exists
@@ -130,14 +88,20 @@ interface anydbConnection {
     public function items_table_exists();
 
     /*
-     * create items table; if $error_if_exists is true; throws an err_query
-     * error in case items table already exists.
+     * create items table;
+     * throws an err_query error in case items table already exists.
+     */
+    public function create_items_table();
+
+    /*
+     * returns true if $usrname is name of an existing user, false otherwise.
      */
-    public function create_items_table($error_if_exists);
+    public function user_exists ($usrname);
 
     /*
      * set password $pwd for user $usrname. If $usrname does not exist, create
      * it.
+     * throws an err_query error in case $pwd is null
      */
     public function setpwd($usrname, $pwd);
 
@@ -147,8 +111,8 @@ interface anydbConnection {
     public function checkpwdmd5($usrname, $pwd_md5);
 
     /*
-     * saves feature in database. Returns false if $feature does not exist and
-     * if $feature->imgurl is not accessible; returns true otherwise.
+     * saves feature in database. If feature has an id, feature will be updated
+     * in database; otherwise it will be created. Returns saved feature
      */
     public function save_feature($feature);
 
@@ -159,27 +123,26 @@ interface anydbConnection {
     public function delete_feature($feature);
 
     /*
-     * Returns feature with given imgurl. If none exists, returns null.
+     * Returns feature with given id. If none exists, returns null.
      */
-    public function getfeature($imgurl);
+    public function getfeature($id);
 
     /*
-     * returns an array of available features
+     * returns an array of features managed by $user. If $user is undefined or
+     * if user is "admin", return all available features.
      */
-    public function listfeatures();
+    public function listfeatures($user);
 
     /*
-     * returns true if a feature with imgurl exists
+     * 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 imgurl_exists($imgurl);
+    public function mostrecentfeatures($num_features);
 
     /*
-     * returns Minimum Bounding Rectangle containing all feature locations.
-     * That function must return a result even if database is not functional.
-     * Minimum Bounding Rectangle is presented as an Array:
-     *      [bottom, left, top, right]
+     * returns true if a feature with imgpath exists
      */
-    public function mbr();
+    public function imgpath_exists($imgpath);
 
     /*
      * get name of database backend