/* Enjoy!!! Dino Ciuffetti - dam2k@users.sourceforge.net
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (COPYING); if not, go to http://www.fsf.org/ or write
* to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/* MYSQL DBMS HANDLING */
/**
* Mysql support in cmsdAm is integrated in the users and group classes, templates, ecc, so you
* don't need to call these methods from your sections. This is for developers only.
* Class instance: $cms->mysqldbms
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
class cmsdam_mysqldbms
{
/**
* You should not use this variable from your section.
* Logs class instance.
* @access private
*/
var $logs;
/**
* You should not use this variable from your section.
* Debug class instance.
* @access private
*/
var $debug;
/**
* You should not use this variable from your section.
* Configuration class instance.
* @access private
*/
var $conf;
/**
* You should not use this variable from your section.
* Mysql link identifier pointer
* @access private
*/
var $link;
/**
* You should not use this variable from your section.
* Mysql db identifier pointer
* @access private
*/
var $db;
/**
* You should not use this variable from your section.
* Mysql resultset identifier pointer
* @access private
*/
var $result;
/**
* You should not use this variable from your section.
* Internal stack for nesting queries. You can access to this variable from stack_pop() and
* stack_push() methods.
* @access private
*/
var $stack;
/**
* You should not use this variable from your section.
* DB server name string
* @access private
*/
var $DB_servername;
/**
* You should not use this variable from your section.
* DB name string
* @access private
*/
var $DB_name;
/**
* You should not use this variable from your section.
* DB user name string
* @access private
*/
var $DB_username;
/**
* You should not use this variable from your section.
* DB table prefix string
* @access private
*/
var $DB_tbl_prefix;
/**
* You should not use this variable from your section.
* DB password string
* @access private
*/
var $DB_password = "";
/**
* Contains the connection state of the Mysql DB.
* 0 = DB not connected
* 1 = DB connected
* @type int
* @access private
*/
var $db_connected = 0;
/**
* Last error returned from database
* @type str
* @access private
*/
var $db_errstring = "";
/**
* You should not use this method from your sections.
* This method connects to the DBMS and select the DB.
* @return bool False on errors, True on success
* @access private
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function DB_connect() { /* This method connects to the DBMS and select the DB,
returns true on success or false on error
*/
if ($this->db_connected == 1) { // We are already connected to the DB!
return true;
}
$this->logs->log_note(8, "DEBUG NOTE: Connecting to '" . $this->conf->DB_type . "', DB '" . $this->conf->DB_name . "' at '" . $this->conf->DB_servername . "' with user '" . $this->conf->DB_username . "'");
$this->debug->debug_write(8, "DEBUG NOTE: Connecting to '" . $this->conf->DB_type . "', DB '" . $this->conf->DB_name . "' at '" . $this->conf->DB_servername . "' with user '" . $this->conf->DB_username . "'");
// Try to connect to mysql
if ($this->dbconnect() == false) { // Error connecting
$this->db_connected = 0;
$this->logs->log_note(4, "WARNING NOTE: Connection to '".$this->conf->DB_type."', DB '".$this->conf->DB_name."' at '".$this->conf->DB_servername."' with user '".$this->conf->DB_username."' FAILED for this reason: " . mysql_error());
$this->debug->debug_write(4, "WARNING NOTE: Connection to '".$this->conf->DB_type."', DB '".$this->conf->DB_name."' at '".$this->conf->DB_servername."' with user '".$this->conf->DB_username."' FAILED for this reason: " . mysql_error());
$this->db_errstring = mysql_error();
return false;
}
// Now we are connected to the DB"
$this->logs->log_note(9, "USELESS NOTE: Connection to '".$this->conf->DB_type."', DB '".$this->conf->DB_name."' at '".$this->conf->DB_servername."' with user '".$this->conf->DB_username."' OK, DB connected");
$this->debug->debug_write(9, "USELESS NOTE: Connection to '".$this->conf->DB_type."', DB '".$this->conf->DB_name."' at '".$this->conf->DB_servername."' with user '".$this->conf->DB_username."' OK, DB connected");
// Try to select the right DB
$this->logs->log_note(8, "DEBUG NOTE: Selecting the DB '".$this->conf->DB_name."'");
$this->debug->debug_write(8, "DEBUG NOTE: Selecting the DB '".$this->conf->DB_name."'");
if ($this->dbselect() == false) { // Error selecting DB
$this->db_connected = 0;
return false;
}
// Now the DB is selected
$this->logs->log_note(9, "USELESS NOTE: DB '".$this->conf->DB_name."' selection was OK");
$this->debug->debug_write(9, "USELESS NOTE: DB '".$this->conf->DB_name."' selection was OK");
$this->db_connected = 1;
// echo "DEBUG: CONNECTED: " . $this->db_connected . "!!";
return true;
}
/**
* You should not use this method from your sections.
* This method close the DBMS connection.
* @return bool Always returns True
* @access private
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function DB_close() { // This method close the DBMS connection
// Close the DB connection
$this->logs->log_note(8, "DEBUG NOTE: Closing DB connection to '".$this->conf->DB_type."', DB '".$this->conf->DB_name."' at '".$this->conf->DB_servername."' with user '".$this->conf->DB_username."'");
$this->debug->debug_write(8, "DEBUG NOTE: Closing DB connection to '".$this->conf->DB_type."', DB '".$this->conf->DB_name."' at '".$this->conf->DB_servername."' with user '".$this->conf->DB_username."'");
$this->dbclose();
$this->db_connected = 0;
return true;
}
/**
* You should not use this method from your sections.
* If you want to nest queries you must pop the content from the result stack!
* The right way is: do the query, cycle on the result set, push the resultset, do
* another query, pop the resultset and go on.
* @access private
* @return bool True on success, False if the stack is empty
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function stack_pop() { /* If you want to nest queries you must pop
the content from the result stack!
*/
if (count($this->stack) == 0) { // The stack is empty!
return false;
}
$this->result = $this->stack[count($this->stack)-1];
// echo "DEBUG: Stack count: " . count($this->stack);
unset ($this->stack[count($this->stack)-1]);
// echo "DEBUG: Stack count: " . count($this->stack);
return true;
}
/**
* You should not use this method from your sections.
* If you want to nest queries you must push the content from the result stack!
* The right way is: do the query, cycle on the result set, push the resultset, do
* another query, pop the resultset and go on.
* @access private
* @return bool Always returns True
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function stack_push() { /* If you want to nest queries you must push
the content to the result stack!
*/
$this->stack[count($this->stack)] = $this->result;
return true;
}
/**
* You should not use this method from your sections.
* Debug and log mysql errors
* @param str error The error you want to debug, log
* @param str query The query you want to debug, log
* @access private
* @return bool Always returns False
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dberror($error, $query = "") { // This method implements the DB error handling
$this->debug->debug_write(3, "MySQL error: $error");
if ($query != "")
$this->debug->debug_write(3, "Query: " . $query);
return false;
}
/**
* You should not use this method from your sections.
* Makes the connection to the DB with mysql_connect.
* @return bool False on errors, True on success
* @access private
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbconnect() {
$this->link = @mysql_connect($this->conf->DB_servername, $this->conf->DB_username, $this->conf->DB_password);
if (!$this->link) {
$this->dberror("unable to connect to the DB: " . mysql_error());
$this->db_errstring = mysql_error();
return false;
} else { return true; }
}
/**
* You should not use this method from your sections.
* Makes the DB selection with mysql_select_db.
* @access private
* @return bool False on errors, True on success
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbselect() {
$this->db = @mysql_select_db($this->conf->DB_name);
if (!$this->db) {
$this->dberror("database does not exist or permission denied: " . mysql_error());
$this->db_errstring = mysql_error();
return false;
} else { return true; }
}
/**
* You should not use this method from your sections.
* Makes the DB query with mysql_query.
* @access private
* @return bool False on errors, True on success
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbquery($query) {
$this->stack_push($this->result);
$this->result = (@mysql_query ($query, $this->link));
if (!$this->result) {
$this->dberror (mysql_error(), $query);
$this->db_errstring = mysql_error();
$this->stack_pop();
return false;
} else {
return true;
}
}
/**
* You should not use this method from your sections.
* Makes the DB query with mysql_query without writing debug notes on errors.
* @access private
* @return bool False on errors, True on success
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbquery_nodebug($query) {
$this->stack_push($this->result);
$this->result = (@mysql_query ($query, $this->link));
if (!$this->result) {
$this->stack_pop();
$this->db_errstring = mysql_error();
return false;
} else {
return true;
}
}
/**
* You should not use this method from your sections.
* Returns the number of rows in resultset with dbnum_rows.
* @access private
* @return int number of rows in resultset
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbnum_rows() {
$rs = @mysql_num_rows ($this->result);
return $rs;
}
/**
* You should not use this method from your sections.
* Returns the array of rows in resultset with @mysql_fetch_array.
* @return array array of rows in resultset
* @access private
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbfetch_array() {
$rs = @mysql_fetch_array ($this->result);
return $rs;
}
/**
* You should not use this method from your sections.
* Returns the last requested row array in resultset with @mysql_fetch_row.
* @return array last requested row in resultset
* @access private
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbfetch_row() {
$rs = @mysql_fetch_row ($this->result);
return $rs;
}
/**
* You should not use this method from your sections.
* Close the DB link with mysql_close.
* @access private
* @return bool Always returns True
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function dbclose() {
if ($this->link) {
@mysql_close ($this->link);
}
return true;
}
}
?>