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("${G_base}Obj/dbase_base.phh");
27 $escsql_from = array( "\\", "'" );
28 $escsql_to = array( "\\\\", "\\'" );
32 GLOBAL $escsql_from, $escsql_to;
34 return str_replace($escsql_from, $escsql_to, $s);
39 static $dbcnnx = FALSE;
46 if (DBConn::$dbcnnx == FALSE) {
47 if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
48 echo "DB connection failed.";
52 $this->db = DBConn::$dbcnnx;
70 GLOBAL $DOCUMENT_ROOT, $G_dbpfx, $G_false;
71 log_main("LoginDB create:start");
73 $this->dbconn = new DBConn();
75 log_main("LoginDB create:end");
78 function login_exists($login)
82 /* check the existence of the nick in the LoginDB */
83 log_main("login_exists: ".$login);
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)
93 function &getrecord_bylogin($login) {
94 GLOBAL $G_false, $G_dbpfx;
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)
100 if (pg_numrows($user_pg) != 1)
103 $user_obj = pg_fetch_object($user_pg, 0);
110 function &login_verify($login, $pass)
112 GLOBAL $G_dbpfx, $G_false;
116 log_main("login_verify: ".$login);
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");
123 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
126 log_main("login[".$user_obj->code."]: ".$user_obj->login);
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++) {
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)."]");
137 if (strcmp($pass , md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
138 log_main("login_verify SUCCESS for ".$login);
141 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
146 } // end for ($e = 0 ...
149 if ($chals->ismod()) {
150 Challenges::save_data(&$chals);
153 Challenges::unlock_data($a_sem);
156 // O } // if (strcasecmp($this->item[$i]->login, ...
162 function &getitem_bylogin($login, &$id) {
168 log_main("getitem_bylogin: ".$login);
170 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
173 $id = $user_obj->code;
174 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
178 function getmail($login)
182 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
185 return ($ret->email);
188 function addusers_from_olddb($olddb, &$cont)
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);
197 // if ( ! (($user_pg = pg_exec($dbconn,$order_add_sql)) != FALSE && pg_affected_rows($order_pg) == 1) ) {
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));
207 } // End class LoginDB
214 function LoginDBOld($filename)
216 GLOBAL $DOCUMENT_ROOT;
217 log_main("LoginDBOld create:start");
219 if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
220 require("$DOCUMENT_ROOT/Etc/".$filename);
225 $this->item_n = count($this->item);
226 log_main("LoginDBOld create:end");
231 return ($this->item_n);
248 // sprintf("select count(code) from %sbrisk");
249 return ($this->item_n);
252 function getlogin_byidx($idx)
254 if ($idx >= $this->item_n)
256 return ($this->item[$idx]->login);
259 function &getitem_bylogin($login, &$id)
263 log_main("login_exists: ".$login);
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];
278 function getmail($login)
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);
292 function gettype($login)
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);
306 function &login_verify($login, $pass)
312 log_main("login_verify: ".$login);
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);
320 /* if it exists check for a valid challenge */
321 if (($a_sem = Challenges::lock_data()) != FALSE) {
323 if (($chals = &Challenges::load_data()) != FALSE) {
324 for ($e = 0 ; $e < $chals->item_n ; $e++) {
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)."]");
330 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
331 log_main("login_verify SUCCESS for ".$login);
334 $ret = &$this->item[$i];
338 } // end for ($e = 0 ...
341 if ($chals->ismod()) {
342 Challenges::save_data(&$chals);
345 Challenges::unlock_data($a_sem);
348 } // if (strcasecmp($this->item[$i]->login, ...