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("BriskDB create:start");
73 $this->dbconn = new DBConn();
75 log_main("BriskDB create:end");
82 function login_exists($login)
86 /* check the existence of the nick in the BriskDB */
87 log_main("login_exists: ".$login);
89 $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');", $G_dbpfx, escsql($login));
90 if (($user_pg = pg_query($this->dbconn->db(), $user_sql)) != FALSE)
91 if (pg_numrows($user_pg) == 1)
97 function &getrecord_bylogin($login) {
98 GLOBAL $G_false, $G_dbpfx;
100 $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');", $G_dbpfx, escsql($login));
101 if (($user_pg = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
104 if (pg_numrows($user_pg) != 1)
107 $user_obj = pg_fetch_object($user_pg, 0);
114 function &login_verify($login, $pass)
116 GLOBAL $G_dbpfx, $G_false;
120 log_main("login_verify: ".$login);
123 //O /* check the existence of the nick in the BriskDB */
124 //O for ($i = 0 ; $i < $this->item_n ; $i++) {
125 //O log_main("login_verify: BEGIN");
127 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
130 log_main("login[".$user_obj->code."]: ".$user_obj->login);
132 /* if it exists check for a valid challenge */
133 if (($a_sem = Challenges::lock_data()) != FALSE) {
134 if (($chals = &Challenges::load_data()) != FALSE) {
135 for ($e = 0 ; $e < $chals->item_n ; $e++) {
137 log_main("challenge[".$e."]: ".$chals->item[$e]->login);
138 if (strcmp($login, $chals->item[$e]->login) == 0) {
139 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
141 if (strcmp($pass , md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
142 log_main("login_verify SUCCESS for ".$login);
145 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
150 } // end for ($e = 0 ...
153 if ($chals->ismod()) {
154 Challenges::save_data(&$chals);
157 Challenges::unlock_data($a_sem);
160 // O } // if (strcasecmp($this->item[$i]->login, ...
166 function &getitem_bylogin($login, &$id) {
172 log_main("getitem_bylogin: ".$login);
174 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
177 $id = $user_obj->code;
178 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
182 function getmail($login)
186 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
189 return ($ret->email);
192 function addusers_from_olddb($olddb, &$cont)
196 for ($i = 0 ; $i < $olddb->count() ; $i++) {
197 $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
198 $G_dbpfx, escsql($olddb->item[$i]->login), escsql($olddb->item[$i]->pass),
199 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
201 // if ( ! (($user_pg = pg_exec($dbconn,$order_add_sql)) != FALSE && pg_affected_rows($order_pg) == 1) ) {
203 if ( ! (($user_pg = pg_query($this->dbconn->db(), $user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
204 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
211 } // End class LoginDB
218 function LoginDBOld($filename)
220 GLOBAL $DOCUMENT_ROOT;
221 log_main("LoginDBOld create:start");
223 if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
224 require("$DOCUMENT_ROOT/Etc/".$filename);
229 $this->item_n = count($this->item);
230 log_main("LoginDBOld create:end");
235 return ($this->item_n);
252 // sprintf("select count(code) from %sbrisk");
253 return ($this->item_n);
256 function getlogin_byidx($idx)
258 if ($idx >= $this->item_n)
260 return ($this->item[$idx]->login);
263 function &getitem_bylogin($login, &$id)
267 log_main("login_exists: ".$login);
269 /* check the existence of the nick in the LoginDB */
270 for ($i = 0 ; $i < $this->item_n ; $i++) {
271 if (strcasecmp($this->item[$i]->login, $login) == 0) {
272 log_main("login[".$i."]: ".$this->item[$i]->login);
273 $ret = &$this->item[$i];
282 function getmail($login)
286 /* check the existence of the nick in the LoginDB */
287 for ($i = 0 ; $i < $this->item_n ; $i++) {
288 if (strcasecmp($this->item[$i]->login, $login) == 0) {
289 log_main("login[".$i."]: ".$this->item[$i]->login);
290 return ($this->item[$i]->email);
296 function gettype($login)
300 /* check the existence of the nick in the LoginDB */
301 for ($i = 0 ; $i < $this->item_n ; $i++) {
302 if (strcasecmp($this->item[$i]->login, $login) == 0) {
303 log_main("login[".$i."]: ".$this->item[$i]->login);
304 return ($this->item[$i]->type);
310 function &login_verify($login, $pass)
316 log_main("login_verify: ".$login);
318 /* check the existence of the nick in the LoginDB */
319 for ($i = 0 ; $i < $this->item_n ; $i++) {
320 log_main("login_verify: LOOP");
321 if (strcasecmp($this->item[$i]->login, $login) == 0) {
322 log_main("login[".$i."]: ".$this->item[$i]->login);
324 /* if it exists check for a valid challenge */
325 if (($a_sem = Challenges::lock_data()) != FALSE) {
327 if (($chals = &Challenges::load_data()) != FALSE) {
328 for ($e = 0 ; $e < $chals->item_n ; $e++) {
330 log_main("challenge[".$i."]: ".$chals->item[$e]->login);
331 if (strcmp($login, $chals->item[$e]->login) == 0) {
332 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
334 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
335 log_main("login_verify SUCCESS for ".$login);
338 $ret = &$this->item[$i];
342 } // end for ($e = 0 ...
345 if ($chals->ismod()) {
346 Challenges::save_data(&$chals);
349 Challenges::unlock_data($a_sem);
352 } // if (strcasecmp($this->item[$i]->login, ...