3 * brisk - dbase_pgsql.phh
5 * Copyright (C) 2006-2011 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
25 require_once("Obj/dbase_base.phh");
27 $escsql_from = array( "\\", "'" );
28 $escsql_to = array( "\\\\", "\\'" );
33 GLOBAL $escsql_from, $escsql_to;
35 return str_replace($escsql_from, $escsql_to, $s);
40 static $dbcnnx = FALSE;
47 if (DBConn::$dbcnnx == FALSE) {
48 if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
49 echo "DB connection failed.";
53 $this->db = DBConn::$dbcnnx;
71 GLOBAL $DOCUMENT_ROOT, $G_dbpfx, $G_false;
72 log_main("LoginDB create:start");
74 $this->dbconn = new DBConn();
76 log_main("LoginDB create:end");
79 function login_exists($login)
83 /* check the existence of the nick in the LoginDB */
84 log_main("login_exists: ".$login);
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)
94 function &getrecord_bylogin($login) {
95 GLOBAL $G_false, $G_dbpfx;
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)
101 if (pg_numrows($user_pg) != 1)
104 $user_obj = pg_fetch_object($user_pg, 0);
111 function &login_verify($login, $pass)
113 GLOBAL $G_dbpfx, $G_false;
117 log_main("login_verify: ".$login);
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");
124 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
127 log_main("login[".$user_obj->code."]: ".$user_obj->login);
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++) {
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)."]");
138 if (strcmp($pass , md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
139 log_main("login_verify SUCCESS for ".$login);
142 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
147 } // end for ($e = 0 ...
150 if ($chals->ismod()) {
151 Challenges::save_data(&$chals);
154 Challenges::unlock_data($a_sem);
157 // O } // if (strcasecmp($this->item[$i]->login, ...
163 function &getitem_bylogin($login, &$id) {
169 log_main("getitem_bylogin: ".$login);
171 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
174 $id = $user_obj->code;
175 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
179 function getmail($login)
183 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
186 return ($ret->email);
188 } // End class LoginDB
199 // sprintf("select count(code) from %sbrisk");
200 return ($this->item_n);
203 function getlogin_byidx($idx)
205 if ($idx >= $this->item_n)
207 return ($this->item[$idx]->login);
210 function &getitem_bylogin($login, &$id)
214 log_main("login_exists: ".$login);
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];
229 function getmail($login)
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);
243 function gettype($login)
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);
257 function &login_verify($login, $pass)
263 log_main("login_verify: ".$login);
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);
271 /* if it exists check for a valid challenge */
272 if (($a_sem = Challenges::lock_data()) != FALSE) {
274 if (($chals = &Challenges::load_data()) != FALSE) {
275 for ($e = 0 ; $e < $chals->item_n ; $e++) {
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)."]");
281 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
282 log_main("login_verify SUCCESS for ".$login);
285 $ret = &$this->item[$i];
289 } // end for ($e = 0 ...
292 if ($chals->ismod()) {
293 Challenges::save_data(&$chals);
296 Challenges::unlock_data($a_sem);
299 } // if (strcasecmp($this->item[$i]->login, ...