the dbase part is moved to another file to be able to use sql or file (new files)
[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("Obj/dbase_base.phh");
26
27 $escsql_from = array( "\\",   "'"   );
28 $escsql_to = array(   "\\\\", "\\'" );
29
30
31 function escsql($s)
32 {
33     GLOBAL $escsql_from, $escsql_to;
34     
35     return str_replace($escsql_from, $escsql_to, $s);
36 }
37
38 class DBConn 
39 {
40     static $dbcnnx = FALSE;
41     var $db = FALSE;
42     
43     function DBConn()
44     {
45         GLOBAL $G_dbauth;
46         
47         if (DBConn::$dbcnnx == FALSE) {
48             if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
49                 echo "DB connection failed.";
50                 exit;
51             }
52         }
53         $this->db = DBConn::$dbcnnx;
54
55         return;
56     }
57     function db()
58     {
59         return ($this->db);
60     }
61 }
62
63 class LoginDB
64 {
65     var $dbconn;
66     var $item;
67     var $item_n;
68     
69     function LoginDB()
70     {
71         GLOBAL $DOCUMENT_ROOT, $G_dbpfx, $G_false;
72         log_main("LoginDB create:start");
73         
74         $this->dbconn = new DBConn();
75         
76         log_main("LoginDB create:end");
77     }
78     
79     function login_exists($login)
80     {
81         GLOBAL $G_dbpfx;
82
83         /* check the existence of the nick in the LoginDB */
84         log_main("login_exists: ".$login);
85         
86         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');",  $G_dbpfx, escsql($login));
87         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE)
88             if (pg_numrows($user_pg) == 1)
89                 return TRUE;
90         
91         return FALSE;
92     }
93
94     function &getrecord_bylogin($login) {
95         GLOBAL $G_false, $G_dbpfx;
96
97         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');",  $G_dbpfx, escsql($login));
98         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
99             return $ret;
100         
101         if (pg_numrows($user_pg) != 1)
102             return $ret;
103         
104         $user_obj = pg_fetch_object($user_pg, 0);
105
106         return ($user_obj);
107     }
108
109
110     
111     function &login_verify($login, $pass)
112     {
113         GLOBAL $G_dbpfx, $G_false;
114         
115         $ret = &$G_false;
116         
117         log_main("login_verify: ".$login);
118         
119         
120         //O /* check the existence of the nick in the LoginDB */
121         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
122         //O log_main("login_verify: BEGIN");
123         
124         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
125             return $ret;
126
127         log_main("login[".$user_obj->code."]: ".$user_obj->login);
128         
129         /* if it exists check for a valid challenge */
130         if (($a_sem = Challenges::lock_data()) != FALSE) { 
131             if (($chals = &Challenges::load_data()) != FALSE) {
132                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
133                     
134                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
135                     if (strcmp($login, $chals->item[$e]->login) == 0) {
136                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
137                         
138                         if (strcmp($pass , md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
139                             log_main("login_verify SUCCESS for ".$login);
140                             
141                             $chals->rem($login);
142                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
143                             return ($ret);
144                             //O break;
145                         }
146                     }
147                 } // end for ($e = 0 ...
148             }
149             
150             if ($chals->ismod()) {
151                 Challenges::save_data(&$chals);
152             }
153             
154             Challenges::unlock_data($a_sem);
155         }
156         //O break;
157         // O } //  if (strcasecmp($this->item[$i]->login, ...
158         //O }
159     
160         return ($ret);
161     }
162
163     function &getitem_bylogin($login, &$id) {
164         GLOBAL $G_false;
165         
166         $ret = &$G_false;
167         $id = -1;
168         
169         log_main("getitem_bylogin: ".$login);
170         
171         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
172             return $ret;
173
174         $id = $user_obj->code;
175         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
176     }
177     
178     // TODO FOR DB
179     function getmail($login)
180     {
181         log_main("getmail");
182
183         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
184             return FALSE;
185         
186         return ($ret->email);
187     }
188 } // End class LoginDB
189
190     if (0 == 1) {
191         
192         
193         
194         
195         
196         
197         function count()
198         {
199         // sprintf("select count(code) from %sbrisk");
200         return ($this->item_n);
201     }
202
203     function getlogin_byidx($idx)
204     {
205         if ($idx >= $this->item_n)
206             return FALSE;
207         return ($this->item[$idx]->login);
208     }
209
210     function &getitem_bylogin($login, &$id)
211         {
212             GLOBAL $G_false;
213
214             log_main("login_exists: ".$login);
215     
216             /* check the existence of the nick in the LoginDB */
217             for ($i = 0 ; $i < $this->item_n ; $i++) {
218                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
219                     log_main("login[".$i."]: ".$this->item[$i]->login);
220                     $ret = &$this->item[$i];
221                     $id = $i;
222                     return ($ret);
223                 }
224             }
225             $id = -1;
226             return ($G_false);
227         }
228
229     function getmail($login)
230     {
231         log_main("getmail");
232     
233         /* check the existence of the nick in the LoginDB */
234         for ($i = 0 ; $i < $this->item_n ; $i++) {
235             if (strcasecmp($this->item[$i]->login, $login) == 0) {
236                 log_main("login[".$i."]: ".$this->item[$i]->login);
237                 return ($this->item[$i]->email);
238             }
239         }
240         return (FALSE);
241     }
242
243     function gettype($login)
244     {
245         log_main("getmail");
246     
247         /* check the existence of the nick in the LoginDB */
248         for ($i = 0 ; $i < $this->item_n ; $i++) {
249             if (strcasecmp($this->item[$i]->login, $login) == 0) {
250                 log_main("login[".$i."]: ".$this->item[$i]->login);
251                 return ($this->item[$i]->type);
252             }
253         }
254         return (FALSE);
255     }
256
257     function &login_verify($login, $pass)
258         {
259             GLOBAL $G_false;
260
261             $ret = &$G_false;
262
263             log_main("login_verify: ".$login);
264         
265             /* check the existence of the nick in the LoginDB */
266             for ($i = 0 ; $i < $this->item_n ; $i++) {
267                 log_main("login_verify: LOOP");
268                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
269                     log_main("login[".$i."]: ".$this->item[$i]->login);
270
271                     /* if it exists check for a valid challenge */
272                     if (($a_sem = Challenges::lock_data()) != FALSE) { 
273           
274                         if (($chals = &Challenges::load_data()) != FALSE) {
275                             for ($e = 0 ; $e < $chals->item_n ; $e++) {
276               
277                                 log_main("challenge[".$i."]: ".$chals->item[$e]->login);
278                                 if (strcmp($login, $chals->item[$e]->login) == 0) {
279                                     log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
280                   
281                                     if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
282                                         log_main("login_verify SUCCESS for ".$login);
283    
284                                         $chals->rem($login);
285                                         $ret = &$this->item[$i];
286                                         break;
287                                     }
288                                 }
289                             } // end for ($e = 0 ...
290                         }
291
292                         if ($chals->ismod()) {
293                             Challenges::save_data(&$chals);
294                         }
295           
296                         Challenges::unlock_data($a_sem);
297                     }
298                     break;
299                 } //  if (strcasecmp($this->item[$i]->login, ...
300             }
301
302             return ($ret);
303         }
304
305  } // if (0 == 1) {
306
307
308 ?>