G_base var added to fix relative path problem in include of includes
[brisk.git] / web / Obj / dbase_pgsql.phh
1 <?php
2   /*
3    *  brisk - dbase_pgsql.phh
4    *
5    *  Copyright (C) 2006-2011 Matteo Nastasi
6    *                          mailto: nastasi@alternativeoutput.it 
7    *                                  matteo.nastasi@milug.org
8    *                          web: http://www.alternativeoutput.it
9    *
10    * This program is free software; you can redistribute it and/or modify
11    * it under the terms of the GNU General Public License as published by
12    * the Free Software Foundation; either version 2 of the License, or
13    * (at your option) any later version.
14    *
15    * This program is distributed in the hope that it will be useful, but
16    * WITHOUT ANY WARRANTY; without even the implied warranty of
17    * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    * General Public License for more details. You should have received a
19    * copy of the GNU General Public License along with this program; if
20    * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21    * Suite 330, Boston, MA 02111-1307, USA.
22    *
23    */
24
25 require_once("${G_base}Obj/dbase_base.phh");
26
27 $escsql_from = array( "\\",   "'"   );
28 $escsql_to   = array( "\\\\", "\\'" );
29
30 function escsql($s)
31 {
32     GLOBAL $escsql_from, $escsql_to;
33
34     return str_replace($escsql_from, $escsql_to, $s);
35 }
36
37 class DBConn 
38 {
39     static $dbcnnx = FALSE;
40     var $db = FALSE;
41     
42     function DBConn()
43     {
44         GLOBAL $G_dbauth;
45         
46         if (DBConn::$dbcnnx == FALSE) {
47             if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
48                 echo "DB connection failed.";
49                 exit;
50             }
51         }
52         $this->db = DBConn::$dbcnnx;
53
54         return;
55     }
56     function db()
57     {
58         return ($this->db);
59     }
60 }
61
62 class LoginDB
63 {
64     var $dbconn;
65     var $item;
66     var $item_n;
67     
68     function LoginDB()
69     {
70         GLOBAL $DOCUMENT_ROOT, $G_dbpfx, $G_false;
71         log_main("LoginDB create:start");
72         
73         $this->dbconn = new DBConn();
74         
75         log_main("LoginDB create:end");
76     }
77     
78     function login_exists($login)
79     {
80         GLOBAL $G_dbpfx;
81
82         /* check the existence of the nick in the LoginDB */
83         log_main("login_exists: ".$login);
84         
85         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');",  $G_dbpfx, escsql($login));
86         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE)
87             if (pg_numrows($user_pg) == 1)
88                 return TRUE;
89         
90         return FALSE;
91     }
92
93     function &getrecord_bylogin($login) {
94         GLOBAL $G_false, $G_dbpfx;
95
96         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');",  $G_dbpfx, escsql($login));
97         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
98             return $ret;
99         
100         if (pg_numrows($user_pg) != 1)
101             return $ret;
102         
103         $user_obj = pg_fetch_object($user_pg, 0);
104
105         return ($user_obj);
106     }
107
108
109     
110     function &login_verify($login, $pass)
111     {
112         GLOBAL $G_dbpfx, $G_false;
113         
114         $ret = &$G_false;
115         
116         log_main("login_verify: ".$login);
117         
118         
119         //O /* check the existence of the nick in the LoginDB */
120         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
121         //O log_main("login_verify: BEGIN");
122         
123         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
124             return $ret;
125
126         log_main("login[".$user_obj->code."]: ".$user_obj->login);
127         
128         /* if it exists check for a valid challenge */
129         if (($a_sem = Challenges::lock_data()) != FALSE) { 
130             if (($chals = &Challenges::load_data()) != FALSE) {
131                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
132                     
133                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
134                     if (strcmp($login, $chals->item[$e]->login) == 0) {
135                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
136                         
137                         if (strcmp($pass , md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
138                             log_main("login_verify SUCCESS for ".$login);
139                             
140                             $chals->rem($login);
141                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
142                             return ($ret);
143                             //O break;
144                         }
145                     }
146                 } // end for ($e = 0 ...
147             }
148             
149             if ($chals->ismod()) {
150                 Challenges::save_data(&$chals);
151             }
152             
153             Challenges::unlock_data($a_sem);
154         }
155         //O break;
156         // O } //  if (strcasecmp($this->item[$i]->login, ...
157         //O }
158     
159         return ($ret);
160     }
161
162     function &getitem_bylogin($login, &$id) {
163         GLOBAL $G_false;
164         
165         $ret = &$G_false;
166         $id = -1;
167         
168         log_main("getitem_bylogin: ".$login);
169         
170         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
171             return $ret;
172
173         $id = $user_obj->code;
174         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
175     }
176     
177     // TODO FOR DB
178     function getmail($login)
179     {
180         log_main("getmail");
181
182         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
183             return FALSE;
184         
185         return ($ret->email);
186     }
187
188     function addusers_from_olddb($olddb, &$cont)
189     {
190         GLOBAL $G_dbpfx;
191
192         for ($i = 0 ; $i < $olddb->count() ; $i++) {
193             $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
194                                 $G_dbpfx, escsql($olddb->item[$i]->login), escsql($olddb->item[$i]->pass),
195                                 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL); 
196             
197             // if ( ! (($user_pg = pg_exec($dbconn,$order_add_sql)) != FALSE && pg_affected_rows($order_pg) == 1) ) {
198
199             if ( ! (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
200                 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
201
202                 return FALSE;
203             }
204         }
205         return TRUE;
206     }
207 } // End class LoginDB
208
209 class LoginDBOld 
210 {
211     var $item;
212     var $item_n;
213
214     function LoginDBOld($filename)
215     {
216         GLOBAL $DOCUMENT_ROOT;
217         log_main("LoginDBOld create:start");
218
219         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
220             require("$DOCUMENT_ROOT/Etc/".$filename);
221         }
222         else {
223             return (FALSE);
224         }
225         $this->item_n = count($this->item);
226         log_main("LoginDBOld create:end");
227     }
228
229     function count()
230     {
231         return ($this->item_n);
232     }
233
234
235
236 }
237
238
239     if (0 == 1) {
240         
241         
242         
243         
244         
245         
246         function count()
247         {
248         // sprintf("select count(code) from %sbrisk");
249         return ($this->item_n);
250     }
251
252     function getlogin_byidx($idx)
253     {
254         if ($idx >= $this->item_n)
255             return FALSE;
256         return ($this->item[$idx]->login);
257     }
258
259     function &getitem_bylogin($login, &$id)
260         {
261             GLOBAL $G_false;
262
263             log_main("login_exists: ".$login);
264     
265             /* check the existence of the nick in the LoginDB */
266             for ($i = 0 ; $i < $this->item_n ; $i++) {
267                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
268                     log_main("login[".$i."]: ".$this->item[$i]->login);
269                     $ret = &$this->item[$i];
270                     $id = $i;
271                     return ($ret);
272                 }
273             }
274             $id = -1;
275             return ($G_false);
276         }
277
278     function getmail($login)
279     {
280         log_main("getmail");
281     
282         /* check the existence of the nick in the LoginDB */
283         for ($i = 0 ; $i < $this->item_n ; $i++) {
284             if (strcasecmp($this->item[$i]->login, $login) == 0) {
285                 log_main("login[".$i."]: ".$this->item[$i]->login);
286                 return ($this->item[$i]->email);
287             }
288         }
289         return (FALSE);
290     }
291
292     function gettype($login)
293     {
294         log_main("getmail");
295     
296         /* check the existence of the nick in the LoginDB */
297         for ($i = 0 ; $i < $this->item_n ; $i++) {
298             if (strcasecmp($this->item[$i]->login, $login) == 0) {
299                 log_main("login[".$i."]: ".$this->item[$i]->login);
300                 return ($this->item[$i]->type);
301             }
302         }
303         return (FALSE);
304     }
305
306     function &login_verify($login, $pass)
307     {
308         GLOBAL $G_false;
309         
310         $ret = &$G_false;
311         
312         log_main("login_verify: ".$login);
313         
314         /* check the existence of the nick in the LoginDB */
315         for ($i = 0 ; $i < $this->item_n ; $i++) {
316             log_main("login_verify: LOOP");
317             if (strcasecmp($this->item[$i]->login, $login) == 0) {
318                 log_main("login[".$i."]: ".$this->item[$i]->login);
319                 
320                 /* if it exists check for a valid challenge */
321                 if (($a_sem = Challenges::lock_data()) != FALSE) { 
322                     
323                     if (($chals = &Challenges::load_data()) != FALSE) {
324                         for ($e = 0 ; $e < $chals->item_n ; $e++) {
325                             
326                             log_main("challenge[".$i."]: ".$chals->item[$e]->login);
327                             if (strcmp($login, $chals->item[$e]->login) == 0) {
328                                 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
329                                 
330                                 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
331                                     log_main("login_verify SUCCESS for ".$login);
332                                     
333                                     $chals->rem($login);
334                                     $ret = &$this->item[$i];
335                                     break;
336                                 }
337                             }
338                         } // end for ($e = 0 ...
339                     }
340                     
341                     if ($chals->ismod()) {
342                         Challenges::save_data(&$chals);
343                     }
344                     
345                     Challenges::unlock_data($a_sem);
346                 }
347                 break;
348             } //  if (strcasecmp($this->item[$i]->login, ...
349         }
350         
351         return ($ret);
352     }
353
354  } // if (0 == 1) {
355
356
357 ?>