3 * brisk - dbase_pgsql.phh
5 * Copyright (C) 2006-2012 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()
51 if (DBConn::$dbcnnx == FALSE) {
52 if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth, PGSQL_CONNECT_FORCE_NEW))) {
62 static function destroy()
64 if (DBConn::$dbcnnx != FALSE) {
65 DBConn::$dbcnnx = FALSE;
66 return (pg_close(DBConn::$dbcnnx));
71 static function recover()
74 return (self::create());
89 function BriskDB($dbconn)
91 $this->dbconn = $dbconn;
94 static function create()
96 GLOBAL $DOCUMENT_ROOT, $G_dbpfx;
100 log_main("BriskDB create:start");
103 if (($dbconn = DBConn::create()) == FALSE) {
107 $ret = new BriskDB($dbconn);
115 if (($res = pg_query($this->dbconn->db(), $sql)) == FALSE) {
116 // try to recover the connection
117 if (($this->dbconn = DBConn::recover()) == FALSE)
119 return (pg_query($this->dbconn->db(), $sql));
125 function users_load()
129 function login_exists($login)
133 /* check the existence of the nick in the BriskDB */
134 log_main("login_exists: ".$login);
136 $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s') AND (type & CAST (X'%08x' as integer)) = 0;",
137 $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
138 if (($user_pg = $this->query($user_sql)) != FALSE)
139 if (pg_numrows($user_pg) == 1)
145 function getrecord_bylogin($login) {
148 $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);
149 if (($user_pg = $this->query($user_sql)) == FALSE) {
152 if (pg_numrows($user_pg) != 1)
155 $user_obj = pg_fetch_object($user_pg, 0);
160 function user_update_login_time($code, $lintm)
164 $user_sql = sprintf("UPDATE %susers SET (lintm) = (date 'epoch' + %d * INTERVAL '1 second') WHERE code = %d;", $G_dbpfx, $lintm, $code);
166 // $user_pg = $this->query($user_sql);
167 // $row_n = pg_affected_rows($user_pg);
168 // fprintf(STDERR, "query: %s NUM: %d\n", ($user_pg == FALSE ? "FALSE" : "TRUE"), $row_n);
169 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
176 function user_prefs_update($code, $flags, $supp_comp)
180 $user_sql = sprintf("UPDATE %susers SET (type, supp_comp) = (%d, '%s') WHERE code = %d;",
181 $G_dbpfx, $flags, $supp_comp, $code);
182 fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
183 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
186 fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
191 function login_verify($login, $pass)
197 log_main("login_verify: ".$login);
200 //O /* check the existence of the nick in the BriskDB */
201 //O for ($i = 0 ; $i < $this->item_n ; $i++) {
202 //O log_main("login_verify: BEGIN");
204 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
208 log_main("login[".$user_obj->code."]: ".$user_obj->login);
210 /* if it exists check for a valid challenge */
211 if (($a_sem = Challenges::lock_data(TRUE)) != FALSE) {
212 if (($chals = &Challenges::load_data()) != FALSE) {
213 for ($e = 0 ; $e < $chals->item_n ; $e++) {
214 log_main("challenge[".$e."]: ".$chals->item[$e]->login);
215 if (strcmp($login, $chals->item[$e]->login) == 0) {
216 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
218 if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
219 log_main("login_verify SUCCESS for ".$login);
222 $this->user_update_login_time($user_obj->code, time());
223 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
227 } // end for ($e = 0 ...
230 if ($chals->ismod()) {
231 Challenges::save_data(&$chals);
234 Challenges::unlock_data($a_sem);
237 // O } // if (strcasecmp($this->item[$i]->login, ...
243 function getitem_bylogin($login, &$id) {
247 log_main("getitem_bylogin: ".$login);
249 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
252 $id = $user_obj->code;
253 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
257 function getmail($login)
261 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
264 return ($ret->email);
267 function addusers_from_olddb($olddb, &$cont)
271 for ($i = 0 ; $i < $olddb->count() ; $i++) {
272 $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
273 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
274 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
276 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
277 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
287 return ($this->dbconn);
292 function bin5_points_save($date, $ttok, $tidx, $ucodes, $pts)
300 /* check the existence of the nick in the BriskDB */
301 log_main("bin5_points_save: ");
304 if ($this->query("BEGIN") == FALSE) {
312 $mtc_sql = sprintf("SELECT * FROM %sbin5_matches WHERE ttok = '%s';", $G_dbpfx, escsql($ttok));
313 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
314 // match not exists, insert it
315 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx) VALUES ('%s', %d) RETURNING *;",
316 $G_dbpfx, escsql($ttok), $tidx);
317 if ( ! (($mtc_pg = $this->query($mtc_sql)) != FALSE &&
318 pg_affected_rows($mtc_pg) == 1) ) {
319 log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
323 $mtc_obj = pg_fetch_object($mtc_pg,0);
328 $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp)
329 VALUES (%d, to_timestamp(%d)) RETURNING *;",
330 $G_dbpfx, $mtc_obj->code, $date);
331 if ( ! (($gam_pg = $this->query($gam_sql)) != FALSE &&
332 pg_affected_rows($gam_pg) == 1) ) {
333 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
337 $gam_obj = pg_fetch_object($gam_pg,0);
342 for ($i = 0 ; $i < $n ; $i++) {
344 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
345 VALUES (%d, %d, %d);",
346 $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
347 if ( ! (($pts_pg = $this->query($pts_sql)) != FALSE &&
348 pg_affected_rows($pts_pg) == 1) ) {
349 log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
357 if ($this->query("COMMIT") == FALSE) {
367 $this->query("ROLLBACK");
372 } // End class BriskDB
379 function LoginDBOld($filename)
381 GLOBAL $DOCUMENT_ROOT;
382 log_main("LoginDBOld create:start");
384 if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
385 require("$DOCUMENT_ROOT/Etc/".$filename);
390 $this->item_n = count($this->item);
391 log_main("LoginDBOld create:end");
396 return ($this->item_n);
399 } // End class LoginDBOld