]> dev.renevier.net Git - syp.git/blob - inc/db/anydb.php
initial commit
[syp.git] / inc / db / anydb.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 // XXX: put that function somewhere else. It's used in changes.php
6 function relative_path ($url_str) {
7     $url = parse_url ($url_str);
8     if ($url ['host'] != $_SERVER ['HTTP_HOST']) {
9         return null;
10     }
11
12     // first strip common directory names
13     $url_path = split ('/', $url ['path']);
14     $script_path = split ('/', $_SERVER ['SCRIPT_NAME']);
15     $len = min (count ($url_path), count ($script_path));
16     while ($url_path [0] == $script_path [0]) {
17         array_shift ($url_path);
18         array_shift ($script_path);
19     }
20
21     // $url_path contains $script_path; abort
22     if (count ($script_path) == 0) { 
23         return null;
24     }
25
26     // now, create relative path
27     $relpath = "";
28     for ($i = 0; $i < (count ($script_path) - 1); $i++) {
29         $relpath .= "../";
30     }
31     $relpath .= join ("/", $url_path);
32     return $relpath;
33 }
34
35 class feature {
36     private $imgurl = null;
37     private $title = null;
38     private $description = null;
39     private $lon = null;
40     private $lat = null;
41
42     const err_lonlat_invalid = 1;
43
44     function __construct ($imgurl, $title, $description, $lon, $lat) {
45         // imgurl
46         // tries to transform full url in relative path
47         $relpath = relative_path ($imgurl);
48         if (isset ($relpath)) {
49             $this->imgurl = $relpath;
50         } else {
51             $this->imgurl = $imgurl;
52         }
53
54         // title
55         $this->title = $title;
56
57         // description
58         $this->description = $description;
59
60         // longitude
61         if (!isset ($lon) || !is_numeric ($lon) ||
62              ($lon < -180) || ($lon > 180)) {
63             throw new Exception ($this->err_lonlat_invalid);
64         }
65         $this->lon = $lon;
66
67         // latitude
68         if (!isset ($lat) || !is_numeric ($lat) ||
69              ($lat < -90) || ($lat > 90)) {
70             throw new Exception ($this->err_lonlat_invalid);
71         }
72         $this->lat = $lat;
73     }
74
75     public function __get ($attr) {
76         if (isset ($this->$attr)) return $this->$attr;
77             else throw new Exception ('Unknow attribute '.$attr);
78     }
79
80     public function __set ($attr,$value) {
81         throw new Exception ('properties can only be set in constructor');
82     }
83
84     public function imgurl_exists () {
85         if (file_exists ($this->imgurl)) {
86             return true;
87         }
88         if (!function_exists ("curl_init")) {
89             return false;
90         }
91         $handle   = curl_init ($this->imgurl);
92         if ($handle === false)
93         {
94             return false;
95         }
96         curl_setopt ($handle, CURLOPT_NOBODY, true);
97         curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 10);
98         curl_setopt ($handle, CURLOPT_TIMEOUT, 10);
99         $connectable = curl_exec ($handle);
100         curl_close ($handle);  
101         return $connectable;
102     }
103 }
104
105 interface anydbConnection {
106     const err_driver_unavailable = 1;
107     const err_connection = 2;
108     const err_unknown_database = 3;
109     const err_query = 3;
110
111     /*
112      * connect to database; That method may be called multiple times.
113      */
114     public function connect($host, $user, $pwd, $dbname, $dbprefix);
115
116     /*
117      * return true if users table already exists
118      */
119     public function users_table_exists();
120
121     /*
122      * create users table; if $error_if_exists is true; throws an err_query
123      * error in case users table already exists.
124      */
125     public function create_users_table($error_if_exists);
126
127     /*
128      * return true if items table already exists
129      */
130     public function items_table_exists();
131
132     /*
133      * create items table; if $error_if_exists is true; throws an err_query
134      * error in case items table already exists.
135      */
136     public function create_items_table($error_if_exists);
137
138     /*
139      * set password $pwd for user $usrname. If $usrname does not exist, create
140      * it.
141      */
142     public function setpwd($usrname, $pwd);
143
144     /*
145      * check that $pwd_md5 is md5 for $username password.
146      */
147     public function checkpwdmd5($usrname, $pwd_md5);
148
149     /*
150      * saves feature in database. Returns false if $feature does not exist and
151      * if $feature->imgurl is not accessible; returns true otherwise.
152      */
153     public function save_feature($feature);
154
155     /*
156      * delete feature from database. Returns true in case of success, even if
157      * image was not referenced in the database.
158      */
159     public function delete_feature($feature);
160
161     /*
162      * Returns feature with given imgurl. If none exists, returns null.
163      */
164     public function getfeature($imgurl);
165
166     /*
167      * returns an array of available features
168      */
169     public function listfeatures();
170
171     /*
172      * returns true if a feature with imgurl exists
173      */
174     public function imgurl_exists($imgurl);
175
176     /*
177      * returns Minimum Bounding Rectangle containing all feature locations.
178      * That function must return a result even if database is not functional.
179      * Minimum Bounding Rectangle is presented as an Array:
180      *      [bottom, left, top, right]
181      */
182     public function mbr();
183
184     /*
185      * get name of database backend
186      */
187     public function getdbname();
188 }
189 ?>