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;
44 $this->db = DBConn::$dbcnnx;
47 static function &create()
49 GLOBAL $G_dbauth, $G_false;
53 if (DBConn::$dbcnnx == FALSE) {
54 if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
75 function BriskDB($dbconn)
77 $this->dbconn = $dbconn;
80 static function &create()
82 GLOBAL $DOCUMENT_ROOT, $G_dbpfx, $G_false;
87 log_main("BriskDB create:start");
90 if (($dbconn = DBConn::create()) == FALSE)
93 $ret = new BriskDB($dbconn);
96 log_main("BriskDB create:end");
100 function users_load()
104 function login_exists($login)
108 /* check the existence of the nick in the BriskDB */
109 log_main("login_exists: ".$login);
111 $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s') AND (type & CAST (X'%08x' as integer)) = 0;",
112 $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
113 if (($user_pg = pg_query($this->dbconn->db(), $user_sql)) != FALSE)
114 if (pg_numrows($user_pg) == 1)
120 function &getrecord_bylogin($login) {
121 GLOBAL $G_false, $G_dbpfx;
123 $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s') AND (type & CAST (X'%08x' as integer)) = 0;", $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
124 if (($user_pg = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
127 if (pg_numrows($user_pg) != 1)
130 $user_obj = pg_fetch_object($user_pg, 0);
137 function &login_verify($login, $pass)
139 GLOBAL $G_dbpfx, $G_false;
143 log_main("login_verify: ".$login);
146 //O /* check the existence of the nick in the BriskDB */
147 //O for ($i = 0 ; $i < $this->item_n ; $i++) {
148 //O log_main("login_verify: BEGIN");
150 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
153 log_main("login[".$user_obj->code."]: ".$user_obj->login);
155 /* if it exists check for a valid challenge */
156 if (($a_sem = Challenges::lock_data()) != FALSE) {
157 if (($chals = &Challenges::load_data()) != FALSE) {
158 for ($e = 0 ; $e < $chals->item_n ; $e++) {
160 log_main("challenge[".$e."]: ".$chals->item[$e]->login);
161 if (strcmp($login, $chals->item[$e]->login) == 0) {
162 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
164 if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
165 log_main("login_verify SUCCESS for ".$login);
168 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
172 } // end for ($e = 0 ...
175 if ($chals->ismod()) {
176 Challenges::save_data(&$chals);
179 Challenges::unlock_data($a_sem);
182 // O } // if (strcasecmp($this->item[$i]->login, ...
188 function &getitem_bylogin($login, &$id) {
194 log_main("getitem_bylogin: ".$login);
196 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
199 $id = $user_obj->code;
200 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
204 function getmail($login)
208 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
211 return ($ret->email);
214 function addusers_from_olddb($olddb, &$cont)
218 for ($i = 0 ; $i < $olddb->count() ; $i++) {
219 $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
220 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
221 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
223 // if ( ! (($user_pg = pg_exec($dbconn,$order_add_sql)) != FALSE && pg_affected_rows($order_pg) == 1) ) {
225 if ( ! (($user_pg = pg_query($this->dbconn->db(), $user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
226 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
234 function &getdbconn()
236 $ret = $this->dbconn;
242 function bin5_points_save($date, $ttok, $tidx, $ucodes, $pts)
250 /* check the existence of the nick in the BriskDB */
251 log_main("bin5_points_save: ");
254 if (pg_query($this->dbconn->db(), "BEGIN") == FALSE) {
262 $mtc_sql = sprintf("SELECT * FROM %sbin5_matches WHERE ttok = '%s';", $G_dbpfx, escsql($ttok));
263 if (($mtc_pg = pg_query($this->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
264 // match not exists, insert it
265 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx) VALUES ('%s', %d) RETURNING *;",
266 $G_dbpfx, escsql($ttok), $tidx);
267 if ( ! (($mtc_pg = pg_query($this->dbconn->db(), $mtc_sql)) != FALSE &&
268 pg_affected_rows($mtc_pg) == 1) ) {
269 log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
273 $mtc_obj = pg_fetch_object($mtc_pg,0);
278 $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp)
279 VALUES (%d, to_timestamp(%d)) RETURNING *;",
280 $G_dbpfx, $mtc_obj->code, $date);
281 if ( ! (($gam_pg = pg_query($this->dbconn->db(), $gam_sql)) != FALSE &&
282 pg_affected_rows($gam_pg) == 1) ) {
283 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
287 $gam_obj = pg_fetch_object($gam_pg,0);
292 for ($i = 0 ; $i < $n ; $i++) {
294 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
295 VALUES (%d, %d, %d);",
296 $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
297 if ( ! (($pts_pg = pg_query($this->dbconn->db(), $pts_sql)) != FALSE &&
298 pg_affected_rows($pts_pg) == 1) ) {
299 log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
307 if (pg_query($this->dbconn->db(), "COMMIT") == FALSE) {
317 pg_query($this->dbconn-db(), "ROLLBACK");
322 } // End class BriskDB
329 function LoginDBOld($filename)
331 GLOBAL $DOCUMENT_ROOT;
332 log_main("LoginDBOld create:start");
334 if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
335 require("$DOCUMENT_ROOT/Etc/".$filename);
340 $this->item_n = count($this->item);
341 log_main("LoginDBOld create:end");
346 return ($this->item_n);
349 } // End class LoginDBOld