From 3352babbf3aa67a20cbee8c890dbadf50afd9005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=EF=BF=BD=EF=BF=BDois=20Serman?= Date: Thu, 26 Jun 2014 09:40:04 +0000 Subject: [PATCH 02/18] toward PDO and beyond! --- bureau/class/config.php | 28 +- bureau/class/db_mysql.php | 610 +++++++++++++++----------------------- 2 files changed, 232 insertions(+), 406 deletions(-) diff --git a/bureau/class/config.php b/bureau/class/config.php index 0bc75529..bc71e0ca 100644 --- a/bureau/class/config.php +++ b/bureau/class/config.php @@ -128,33 +128,7 @@ if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]=="on" && $host!=$L_FQDN) { } -// Classe h�rit�e de la classe db de la phplib. -/** -* Class for MySQL management in the bureau -* -* This class heriting from the db class of the phplib manages -* the connection to the MySQL database. -*/ - -class DB_system extends DB_Sql { - var $Host = null; - var $Database = null; - var $User = null; - var $Password = null; - - /** - * Creator - */ - function DB_system() { - global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD; - $this->Host = $L_MYSQL_HOST; - $this->Database = $L_MYSQL_DATABASE; - $this->User = $L_MYSQL_LOGIN; - $this->Password = $L_MYSQL_PWD; - } -} - -$db= new DB_system(); +$db= new DB_Sql($L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, $L_MYSQL_PWD); // Current User ID = the user whose commands are made on behalf of. $cuid=0; diff --git a/bureau/class/db_mysql.php b/bureau/class/db_mysql.php index 81992ba2..6d86923e 100644 --- a/bureau/class/db_mysql.php +++ b/bureau/class/db_mysql.php @@ -1,284 +1,239 @@ query($query); + * Constructor + */ + function __construct($db, $host, $user, $passwd) { + + $dsn = sprintf('mysql:dbname=%s;host=%s', $db, $host); + + try { + $this->pdo_instance = new PDO($dsn, $user, $passwd); + } catch (PDOException $e) { + echo "Mysql", "PDO instance", $e->getMessage(); + return FALSE; + } } /** - * @return resource class variable Link_ID - */ - function link_id() { - return $this->Link_ID; - } - - /** - * @return integer class variable Query_ID - */ - function query_id() { - return $this->Query_ID; - } - - /** - * function for MySQL database connection management - * - * This function manages the connection to the MySQL database. - * - * @param $Database name of the database - * @param $Host DNS of the MySQL hosting server - * @param $User the user's name - * @param $Password the user's password - * - * @return the class variable $Link_ID - */ + * function for MySQL database connection management + * + * This function manages the connection to the MySQL database. + * + * @param $Database name of the database + * @param $Host DNS of the MySQL hosting server + * @param $User the user's name + * @param $Password the user's password + * + * @return the class variable $Link_ID + */ function connect($Database = "", $Host = "", $User = "", $Password = "") { - /* Handle defaults */ - if ("" == $Database) - $Database = $this->Database; - if ("" == $Host) - $Host = $this->Host; - if ("" == $User) - $User = $this->User; - if ("" == $Password) - $Password = $this->Password; - - /* establish connection, select database */ - if ( 0 == $this->Link_ID ) { - - $this->Link_ID=mysql_pconnect($Host, $User, $Password); - if (!$this->Link_ID) { - $this->halt("pconnect($Host, $User, \$Password) failed."); - return 0; - } + global $err; + $err->log('Mysql', 'Legacy', 'This function should no longer be used'); + /* Handle defaults */ + if ("" == $Database) + $Database = $this->Database; + if ("" == $Host) + $Host = $this->Host; + if ("" == $User) + $User = $this->User; + if ("" == $Password) + $Password = $this->Password; + + if (!$this->pdo_instance) { + $dsn = sprintf('mysql:dbname=%s;host=%s', $Database, $Host); - if (!@mysql_select_db($Database,$this->Link_ID)) { - $this->halt("cannot use database ".$this->Database); - return 0; - } + try { + $this->pdo_instance = new PDO($dsn, $User, $Password); + } catch (PDOException $e) { + $err->log("Mysql", "PDO instance", $e->getMessage()); + return FALSE; + } } - - //persistent connection don't conserve database selection - //if needed do a correct database selection - $db_connected = @mysql_fetch_array(@mysql_query("SELECT DATABASE();",$this->Link_ID)); - if ($db_connected[0] != $this->Database) - mysql_select_db($Database,$this->Link_ID); - return $this->Link_ID; + return True; } /** - * Discard the query result - * - * This function discards the last query result. - */ + * Discard the query result + * + * This function discards the last query result. + */ function free() { - @mysql_free_result($this->Query_ID); - $this->Query_ID = 0; + $this->pdo_query->closeCursor(); + } + + function is_connected() { + return $this->pdo_instance != FALSE; } /** - * Perform a query - * - * This function performs the MySQL query described in the string parameter - * - * @param a string describing the MySQL query - * @return the $Query_ID class variable (null if fails) - */ - function query($Query_String) { - global $debug_alternc; + * Perform a query + * + * This function performs the MySQL query described in the string parameter + * + * @param a string describing the MySQL query + * @param arguments is an optionnal array for future use with PDO parametrized requests + * @return the $Query_ID class variable (null if fails) + */ + function query($Query_String, $arguments = False) { + global $debug_alternc; - /* No empty queries, please, since PHP4 chokes on them. */ - if ($Query_String == "") - /* The empty query string is passed on from the constructor, - * when calling the class without a query, e.g. in situations - * like these: '$db = new DB_Sql_Subclass;' - */ - return 0; + if (empty($Query_String) || !$this->is_connected()) + return FALSE; - if (!$this->connect()) { - return 0; /* we already complained in connect() about that. */ - }; + $this->Query_String = $Query_String; + if ($this->Debug) + printf("Debug: query = %s
\n", $Query_String); - # New query, discard previous result. - if ($this->Query_ID) { - $this->free(); - $this->Query_String = $Query_String; - } + $debug_chrono_start = microtime(true); - if ($this->Debug) - printf("Debug: query = %s
\n", $Query_String); + $this->pdo_query = $this->pdo_instance->prepare($this->Query_String); + $exec_state = ($arguments) ? $this->pdo_query->execute($arguments) + : $this->pdo_query->execute(); + + $debug_chrono_start = (microtime(true) - $debug_chrono_start)*1000; + $this->Row = 0; + + if ($exec_state == FALSE) { + $this->Errno = $this->pdo_query->errorCode(); + $this->Error = $this->pdo_query->errorInfo(); - $debug_chrono_start = microtime(true); - $this->Query_ID = @mysql_query($Query_String,$this->Link_ID); - $debug_chrono_start = (microtime(true) - $debug_chrono_start)*1000; - $this->Row = 0; - $this->Errno = mysql_errno(); - $this->Error = mysql_error(); - if( 0 != $this->Errno ){ if( defined("THROW_EXCEPTIONS") && THROW_EXCEPTIONS ){ - throw new \Exception("Mysql query failed : $this->Error"); + throw new \Exception("Mysql query failed : $this->Error"); } $this->halt("SQL Error: ".$Query_String); return FALSE; - } - if (!$this->Query_ID) { - $this->halt("Invalid SQL: ".$Query_String); - } + } + + if (isset($debug_alternc)) { + $debug_alternc->add("SQL Query : (".substr($debug_chrono_start,0,5)." ms)\t $Query_String"); + $debug_alternc->nb_sql_query++; + $debug_alternc->tps_sql_query += $debug_chrono_start; + } - if (isset($debug_alternc)) { - $debug_alternc->add("SQL Query : (".substr($debug_chrono_start,0,5)." ms)\t $Query_String"); - $debug_alternc->nb_sql_query++; - $debug_alternc->tps_sql_query += $debug_chrono_start; - } - - # Will return nada if it fails. That's fine. - return $this->Query_ID; + return TRUE; } - + /** - * walk result set - * - * This function tests if a new record is available in the current - * query result. - * - * @return TRUE if a new record is available - */ + * walk result set + * + * This function tests if a new record is available in the current + * query result. + * + * @return TRUE if a new record is available + */ function next_record() { - if (!$this->Query_ID) { - $this->halt("next_record called with no query pending."); - return 0; + if (!$this->pdo_query) { + $this->halt("next_record called with no query pending."); + return FALSE; + } + + $this->Record = $this->pdo_query->fetch(PDO::FETCH_BOTH); + $this->Row++; + $this->Errno = $this->pdo_query->errorCode(); + $this->Error = $this->pdo_query->errorInfo(); + + if ($this->Record == FALSE) { + if ($this->Auto_Free) + $this->free(); + return FALSE; } - $this->Record = @mysql_fetch_array($this->Query_ID); - $this->Row += 1; - $this->Errno = mysql_errno(); - $this->Error = mysql_error(); - - $stat = is_array($this->Record); - if (!$stat && $this->Auto_Free) { - $this->free(); - } - return $stat; - } - - /** - * - * public: position in result set - */ - - function seek($pos = 0) { - $status = @mysql_data_seek($this->Query_ID, $pos); - if ($status) - $this->Row = $pos; - else { - $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows"); - - /* half assed attempt to save the day, - * but do not consider this documented or even - * desireable behaviour. - */ - @mysql_data_seek($this->Query_ID, $this->num_rows()); - $this->Row = $this->num_rows; - return 0; - } - - return 1; + return TRUE; } /* public: table locking */ function lock($table, $mode="write") { - $this->connect(); + if (!$this->is_connected()) + return FALSE; - $query="lock tables "; - if (is_array($table)) { - while (list($key,$value)=each($table)) { - if ($key=="read" && $key!=0) { - $query.="$value read, "; - } else { - $query.="$value $mode, "; + $query="lock tables "; + if (is_array($table)) { + while (list($key,$value)=each($table)) { + if ($key=="read" && $key!=0) { + $query.="$value read, "; + } else { + $query.="$value $mode, "; + } } - } - $query=substr($query,0,-2); - } else { - $query.="$table $mode"; - } - $res = @mysql_query($query, $this->Link_ID); - if (!$res) { + $query=substr($query,0,-2); + } else { + $query.="$table $mode"; + } + + + if (!$this->query($query)) { $this->halt("lock($table, $mode) failed."); - return 0; - } - return $res; + return FALSE; + } + + return TRUE; + } function unlock() { - $this->connect(); + if (!$this->is_connected()) + return FALSE; - $res = @mysql_query("unlock tables", $this->Link_ID); - if (!$res) { - $this->halt("unlock() failed."); - return 0; - } - return $res; + if (!$this->query('unlock tables')) { + $this->halt("unlock() failed."); + return FALSE; + } } /* public: evaluate the result (size, width) */ function affected_rows() { - return @mysql_affected_rows($this->Link_ID); + return $this->pdo_query->rowCount(); } function num_rows() { - return @mysql_num_rows($this->Query_ID); + return $this->pdo_query->rowCount(); } function num_fields() { - return @mysql_num_fields($this->Query_ID); + return $this->pdo_query->columnCount(); } /* public: shorthand notation */ @@ -295,201 +250,98 @@ class DB_Sql { * @return integer */ function f($Name) { - if (isset($this->Record[$Name])) - return $this->Record[$Name]; - else - return false; + if (isset($this->Record[$Name])) + return $this->Record[$Name]; + else + return false; + } + + function current_record() { + return $this->Record; } function p($Name) { - print $this->Record[$Name]; + print $this->Record[$Name]; } function lastid() { - return @mysql_insert_id($this->Link_ID); + return $this->pdo_query->lastInsertId(); } /* public: sequence numbers */ function nextid($seq_name) { - $this->connect(); + if (!$this->is_connected()) + return FALSE; - if ($this->lock($this->Seq_Table)) { - /* get sequence number (locked) and increment */ - $q = sprintf("select nextid from %s where seq_name = '%s'", - $this->Seq_Table, - $seq_name); - $id = @mysql_query($q, $this->Link_ID); - $res = @mysql_fetch_array($id); + if ($this->lock($this->Seq_Table)) { + /* get sequence number (locked) and increment */ + $q = sprintf("select nextid from %s where seq_name = '%s'", + $this->Seq_Table, + $seq_name); + $this->query($q); + $this->next_record(); + + $id = $this->f('nextid'); - /* No current value, make one */ - if (!is_array($res)) { - $currentid = 0; - $q = sprintf("insert into %s values('%s', %s)", - $this->Seq_Table, - $seq_name, - $currentid); - @mysql_query($q, $this->Link_ID); - } else { - $currentid = $res["nextid"]; - } - $nextid = $currentid + 1; - $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'", - $this->Seq_Table, - $nextid, - $seq_name); - @mysql_query($q, $this->Link_ID); - $this->unlock(); - } else { - $this->halt("cannot lock ".$this->Seq_Table." - has it been created?"); - return 0; - } - return $nextid; + /* No current value, make one */ + if (!$id) { + $currentid = 0; + $q = sprintf("insert into %s values('%s', %s)", + $this->Seq_Table, + $seq_name, + $currentid); + $this->query($q); + } else { + $currentid = $id; + } + + $nextid = $currentid + 1; + $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'", + $this->Seq_Table, + $nextid, + $seq_name); + $this->query($q); + $this->unlock(); + } else { + $this->halt("cannot lock ".$this->Seq_Table." - has it been created?"); + return FALSE; + } + + return $nextid; } /* public: return table metadata */ function metadata($table='',$full=false) { - $res = array(); - - /* - * Due to compatibility problems with Table we changed the behavior - * of metadata(); - * depending on $full, metadata returns the following values: - * - * - full is false (default): - * $result[]: - * [0]["table"] table name - * [0]["name"] field name - * [0]["type"] field type - * [0]["len"] field length - * [0]["flags"] field flags - * - * - full is true - * $result[]: - * ["num_fields"] number of metadata records - * [0]["table"] table name - * [0]["name"] field name - * [0]["type"] field type - * [0]["len"] field length - * [0]["flags"] field flags - * ["meta"][field name] index of field named "field name" - * The last one is used, if you have a field name, but no index. - * Test: if (isset($result['meta']['myfield'])) { ... - */ - - // if no $table specified, assume that we are working with a query - // result - if ($table) { - $this->connect(); - $id = @mysql_list_fields($this->Database, $table); - if (!$id) - $this->halt("Metadata query failed."); - } else { - $id = $this->Query_ID; - if (!$id) - $this->halt("No query specified."); - } - - $count = @mysql_num_fields($id); - - // made this IF due to performance (one if is faster than $count if's) - if (!$full) { - for ($i=0; $i<$count; $i++) { - $res[$i]["table"] = @mysql_field_table ($id, $i); - $res[$i]["name"] = @mysql_field_name ($id, $i); - $res[$i]["type"] = @mysql_field_type ($id, $i); - $res[$i]["len"] = @mysql_field_len ($id, $i); - $res[$i]["flags"] = @mysql_field_flags ($id, $i); - } - } else { // full - $res["num_fields"]= $count; - - for ($i=0; $i<$count; $i++) { - $res[$i]["table"] = @mysql_field_table ($id, $i); - $res[$i]["name"] = @mysql_field_name ($id, $i); - $res[$i]["type"] = @mysql_field_type ($id, $i); - $res[$i]["len"] = @mysql_field_len ($id, $i); - $res[$i]["flags"] = @mysql_field_flags ($id, $i); - $res["meta"][$res[$i]["name"]] = $i; - } - } - - // free the result only if we were called on a table - if ($table) @mysql_free_result($id); - return $res; + global $err; + $err->raise('Mysql', 'function is no longer implemented (metadata())'); + return FALSE; } -/********************************************************************************************************/ -// AJOUT PERSO : TEST - -/* public: return table metadata - function retourneNameField($this->Query_ID,$full=false) { - $count = 0; - $id = 0; - $res = array(); - - - $count = @mysql_num_fields($this->Query_ID); - - // made this IF due to performance (one if is faster than $count if's) - if (!$full) { - for ($i=0; $i<$count; $i++) { - $res[$i]["table"] = @mysql_field_table ($id, $i); - $res[$i]["name"] = @mysql_field_name ($id, $i); - $res[$i]["type"] = @mysql_field_type ($id, $i); - $res[$i]["len"] = @mysql_field_len ($id, $i); - $res[$i]["flags"] = @mysql_field_flags ($id, $i); - } - } else { // full - $res["num_fields"]= $count; - - for ($i=0; $i<$count; $i++) { - $res[$i]["table"] = @mysql_field_table ($id, $i); - $res[$i]["name"] = @mysql_field_name ($id, $i); - $res[$i]["type"] = @mysql_field_type ($id, $i); - $res[$i]["len"] = @mysql_field_len ($id, $i); - $res[$i]["flags"] = @mysql_field_flags ($id, $i); - $res["meta"][$res[$i]["name"]] = $i; - } - } - - // free the result only if we were called on a table - if ($table) @mysql_free_result($id); - return $res; - }*/ - -/********************************************************************************************************/ /* private: error handling */ function halt($msg) { - $this->Error = @mysql_error($this->Link_ID); - $this->Errno = @mysql_errno($this->Link_ID); - if ($this->Halt_On_Error == "no") - return; + if ($this->Halt_On_Error == "no") + return; - $this->haltmsg($msg); + $this->haltmsg($msg); - if ($this->Halt_On_Error != "report") - die("Session halted."); + if ($this->Halt_On_Error != "report") + die("Session halted."); } function haltmsg($msg) { - printf("Database error: %s
\n", $msg); - printf("MySQL Error: %s (%s)
\n", - $this->Errno, - $this->Error); + printf("Database error: %s
\n", $msg); + printf("MySQL Error: %s (%s)
\n", + $this->Errno, + implode("\n", $this->Error)); } function table_names() { $this->query("SHOW TABLES"); - $i=0; - $return=array(); - while ($info=mysql_fetch_row($this->Query_ID)) - { - $return[$i]["table_name"]= $info[0]; - $return[$i]["tablespace_name"]=$this->Database; - $return[$i]["database"]=$this->Database; - $i++; - } - return $return; + $return = array(); + while ($this->next_record()) + $return[] = array('table_name' => $this->p(0), 'tablespace_name' => $this->Database, 'database' => $this->Database); + + return $return; } } ?> From 3b656f15dc5d3fe4d4fdbbf714cea144cef92bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=EF=BF=BD=EF=BF=BDois=20Serman?= Date: Thu, 26 Jun 2014 09:43:11 +0000 Subject: [PATCH 03/18] next_record takes no parameters --- bureau/class/m_variables.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bureau/class/m_variables.php b/bureau/class/m_variables.php index 06c3dcaf..48fcb161 100644 --- a/bureau/class/m_variables.php +++ b/bureau/class/m_variables.php @@ -410,7 +410,7 @@ class m_variables { $result = $db->query('SELECT name, comment FROM `variable` order by name'); $t=array(); - while ($db->next_record($result)) { + while ($db->next_record()) { $tname = $db->f('name'); // If not listed of if listed comment is shorter if ( ! isset( $t[$tname] ) || strlen($t[$tname]) < $db->f('comment') ) { @@ -432,7 +432,7 @@ class m_variables { $result = $db->query('SELECT * FROM `variable`'); $arr_var=array(); - while ($db->next_record($result)) { + while ($db->next_record()) { // Unserialize value if needed if ( ($value = @unserialize($db->f('value'))) === FALSE) { $value=$db->f('value'); From 0c88edaf0ff3b1b9457103fa1f2ad73ec567cbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=EF=BF=BD=EF=BF=BDois=20Serman?= Date: Thu, 26 Jun 2014 09:48:22 +0000 Subject: [PATCH 04/18] restored the db_system extends db_sql for backward compatibility, $db remains a db_sql() --- bureau/class/config.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/bureau/class/config.php b/bureau/class/config.php index bc71e0ca..833952a7 100644 --- a/bureau/class/config.php +++ b/bureau/class/config.php @@ -127,6 +127,31 @@ if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]=="on" && $host!=$L_FQDN) { header("Location: https://$L_FQDN/"); } +// Classe h�rit�e de la classe db de la phplib. +/** + * Class for MySQL management in the bureau + * + * This class heriting from the db class of the phplib manages + * the connection to the MySQL database. + */ + +class DB_system extends DB_Sql { + var $Host = null; + var $Database = null; + var $User = null; + var $Password = null; + + /** + * Creator + */ + function DB_system() { + global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD; + $this->Host = $L_MYSQL_HOST; + $this->Database = $L_MYSQL_DATABASE; + $this->User = $L_MYSQL_LOGIN; + $this->Password = $L_MYSQL_PWD; + } +} $db= new DB_Sql($L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, $L_MYSQL_PWD); From 0e088579c595e2992b58ce85877969773a031373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=EF=BF=BD=EF=BF=BDois=20Serman?= Date: Thu, 26 Jun 2014 10:29:45 +0000 Subject: [PATCH 05/18] no $err->log but $this->halt --- bureau/class/db_mysql.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bureau/class/db_mysql.php b/bureau/class/db_mysql.php index 6d86923e..7bf09c8e 100644 --- a/bureau/class/db_mysql.php +++ b/bureau/class/db_mysql.php @@ -69,7 +69,7 @@ class DB_Sql { */ function connect($Database = "", $Host = "", $User = "", $Password = "") { global $err; - $err->log('Mysql', 'Legacy', 'This function should no longer be used'); + $this->halt('Mysql::connect() : This function should no longer be used'); /* Handle defaults */ if ("" == $Database) $Database = $this->Database; @@ -86,7 +86,7 @@ class DB_Sql { try { $this->pdo_instance = new PDO($dsn, $User, $Password); } catch (PDOException $e) { - $err->log("Mysql", "PDO instance", $e->getMessage()); + $this->halt("Mysql::PDO_instance" . $e->getMessage()); return FALSE; } } From 3c4438895f88e2676dbe71bb5191d4002adb91a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Serman=20=28fser=29?= Date: Tue, 1 Jul 2014 14:46:16 +0200 Subject: [PATCH 06/18] changing default attributes in order to avoid scrutinizer complains --- bureau/class/db_mysql.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bureau/class/db_mysql.php b/bureau/class/db_mysql.php index 7bf09c8e..b8cd8a65 100644 --- a/bureau/class/db_mysql.php +++ b/bureau/class/db_mysql.php @@ -37,8 +37,8 @@ class DB_Sql { /* PDO related variables */ - private $pdo_instance = FALSE; - private $pdo_query = FALSE; + private $pdo_instance = NULL; + private $pdo_query = NULL; /** * Constructor From 14a642ab7825fb2049bd398db1d633a386850fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Serman=20=28fser=29?= Date: Tue, 1 Jul 2014 15:18:49 +0200 Subject: [PATCH 07/18] fixed "type" error for errno, and potential error on lastid() --- bureau/class/db_mysql.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bureau/class/db_mysql.php b/bureau/class/db_mysql.php index b8cd8a65..fcffa76c 100644 --- a/bureau/class/db_mysql.php +++ b/bureau/class/db_mysql.php @@ -28,7 +28,7 @@ class DB_Sql { private $num_rows; /* public: current error number and error text */ - private $Errno = 0; + private $Errno; private $Error; @@ -265,7 +265,7 @@ class DB_Sql { } function lastid() { - return $this->pdo_query->lastInsertId(); + return $this->pdo_instance->lastInsertId(); } /* public: sequence numbers */ From 824151f2164a6daf3bf6baa072af79fc8bd2211d Mon Sep 17 00:00:00 2001 From: fser Date: Fri, 4 Jul 2014 23:39:50 +0200 Subject: [PATCH 08/18] restored the DB_System for compatibility issues --- bureau/class/config.php | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/bureau/class/config.php b/bureau/class/config.php index 833952a7..11239d70 100644 --- a/bureau/class/config.php +++ b/bureau/class/config.php @@ -136,24 +136,14 @@ if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]=="on" && $host!=$L_FQDN) { */ class DB_system extends DB_Sql { - var $Host = null; - var $Database = null; - var $User = null; - var $Password = null; - - /** - * Creator - */ - function DB_system() { + function __construct() { global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD; - $this->Host = $L_MYSQL_HOST; - $this->Database = $L_MYSQL_DATABASE; - $this->User = $L_MYSQL_LOGIN; - $this->Password = $L_MYSQL_PWD; + parent::__construct($L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, $L_MYSQL_PWD); } } -$db= new DB_Sql($L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, $L_MYSQL_PWD); +$db = new DB_system(); +// $db = new Sql($L_MYSQL_DATABASE, $L_MYSQL_HOST, $L_MYSQL_LOGIN, $L_MYSQL_PWD); // Current User ID = the user whose commands are made on behalf of. $cuid=0; From 227822c0c5a6e0c051fac3cc10acbdcd6fcd83f8 Mon Sep 17 00:00:00 2001 From: fser Date: Sat, 5 Jul 2014 22:25:14 +0200 Subject: [PATCH 09/18] fixing bootstrap, in the right branch? --- phpunit/bootstrap.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index cce42a6e..cc5a37eb 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -119,20 +119,12 @@ foreach ($mysqlConfigFile as $line) { * This class heriting from the db class of the phplib manages * the connection to the MySQL database. */ -class DB_system extends DB_Sql { - var $Host,$Database,$User,$Password; - /** - * Constructor - */ - function DB_system($user,$database,$password) { - global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD; - $this->Host = "127.0.0.1"; - $this->Database = $database; - $this->User = $user; - $this->Password = $password; - } -} - +class DB_system extends DB_Sql { + function __construct() { + global $database, $user, $password; + parent::__construct('127.0.0.1', $database, $user, $password); + } +} // Creates database from schema // ********************************************* From 18e1058aa5df62b00a6bc332c77cecf8e8a2b033 Mon Sep 17 00:00:00 2001 From: fser Date: Sat, 5 Jul 2014 22:51:16 +0200 Subject: [PATCH 10/18] simplified m_action (imported from master) --- bureau/class/m_action.php | 40 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/bureau/class/m_action.php b/bureau/class/m_action.php index a1bec46d..5270dfbb 100644 --- a/bureau/class/m_action.php +++ b/bureau/class/m_action.php @@ -185,37 +185,23 @@ class m_action { $err->log("action", "set", $type); $serialized = serialize($parameters); - switch ($type) { - case 'create_file': - $query = "insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');"; - break; - case 'create_dir': - $query = "insert into actions values ('','CREATE_DIR','$serialized',now(),'','','$user','');"; - break; - case 'move': - $query = "insert into actions values ('','MOVE','$serialized',now(),'','','$user','');"; - break; - case 'fix_user': - $query = "insert into actions values ('','FIX_USER','$serialized',now(),'','','$user','');"; - break; - case 'fix_file': - $query = "insert into actions values ('','FIX_FILE','$serialized',now(),'','','$user','');"; - break; - case 'fix_dir': - $query = "insert into actions values ('','FIX_DIR','$serialized',now(),'','','$user','');"; - break; - case 'delete': - $query = "insert into actions values ('','DELETE','$serialized',now(),'','','$user','');"; - break; - default: - return false; - } + $type = strtoupper($type); + if (in_array($type, array('CREATE_FILE', + 'CREATE_DIR', + 'MOVE', + 'FIX_USER', + 'FIX_FILE', + 'FIX_DIR', + 'DELETE'))) { + $query = "INSERT INTO `actions` (type, parameters, creation, user) VALUES('$type', '$serialized', now(), '$user');"; + } else { + return False; + } if (!$db->query($query)) { $err->raise("action", _("Error setting actions")); return false; } - $this->do_action(); - return true; + return $this->do_action(); } /** From ba5bb1d298f28196f932a7fc902753ce6e8816cd Mon Sep 17 00:00:00 2001 From: fser Date: Sat, 5 Jul 2014 23:07:30 +0200 Subject: [PATCH 11/18] hard debug is hard --- phpunit/tests/bureau/class/m_actionTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpunit/tests/bureau/class/m_actionTest.php b/phpunit/tests/bureau/class/m_actionTest.php index 1de07cdf..77bc7f94 100644 --- a/phpunit/tests/bureau/class/m_actionTest.php +++ b/phpunit/tests/bureau/class/m_actionTest.php @@ -64,6 +64,7 @@ class m_actionTest extends AlterncTest { */ public function testDo_action() { global $L_INOTIFY_DO_ACTION; + file_put_contents("/tmp/log_fser", "hello world from fser"); $result = $this->object->do_action(); $this->assertTrue($result); $this->assertFileExists($L_INOTIFY_DO_ACTION); @@ -176,6 +177,7 @@ class m_actionTest extends AlterncTest { public function testGet_action() { $result = $this->object->get_action(); $this->assertTrue(is_array($result)); + $this->expectOutputString(print_r($result, TRUE)); $this->assertCount(1, $result); return current($result); } From 9f769bdaa6d366874c1175d76c128d6cfd059a5e Mon Sep 17 00:00:00 2001 From: fser Date: Sat, 5 Jul 2014 23:14:05 +0200 Subject: [PATCH 12/18] one day I will get the same output from travis and from my term --- phpunit/tests/bureau/class/m_actionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/tests/bureau/class/m_actionTest.php b/phpunit/tests/bureau/class/m_actionTest.php index 77bc7f94..2c2299f8 100644 --- a/phpunit/tests/bureau/class/m_actionTest.php +++ b/phpunit/tests/bureau/class/m_actionTest.php @@ -177,7 +177,7 @@ class m_actionTest extends AlterncTest { public function testGet_action() { $result = $this->object->get_action(); $this->assertTrue(is_array($result)); - $this->expectOutputString(print_r($result, TRUE)); + fwrite(STDERR, print_r($result, TRUE)); $this->assertCount(1, $result); return current($result); } From c4c2db1d9f91a3d9f1cc160b8d023a37d993e33c Mon Sep 17 00:00:00 2001 From: fser Date: Tue, 8 Jul 2014 14:39:06 +0200 Subject: [PATCH 13/18] fix db instance creation --- phpunit/bootstrap.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index cc5a37eb..955922c8 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -1,5 +1,4 @@ connect(); +$db = new \DB_system(); $cuid = 0; $variables = new \m_variables(); $mem = new \m_mem(); From 73766ef98aeeeab12d6dd84b556bcc260e258e9e Mon Sep 17 00:00:00 2001 From: fser Date: Tue, 8 Jul 2014 14:39:19 +0200 Subject: [PATCH 14/18] fix phpunit error with pdo --- phpunit/phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/phpunit.xml b/phpunit/phpunit.xml index c743aacf..cab92584 100644 --- a/phpunit/phpunit.xml +++ b/phpunit/phpunit.xml @@ -1,4 +1,4 @@ - From e4ce56292016c2c5afce018f462ff3b5a69761f5 Mon Sep 17 00:00:00 2001 From: fser Date: Tue, 8 Jul 2014 14:39:37 +0200 Subject: [PATCH 15/18] added last_error() --- bureau/class/db_mysql.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bureau/class/db_mysql.php b/bureau/class/db_mysql.php index fcffa76c..31d399c9 100644 --- a/bureau/class/db_mysql.php +++ b/bureau/class/db_mysql.php @@ -107,6 +107,9 @@ class DB_Sql { return $this->pdo_instance != FALSE; } + function last_error() { + return $this->error; + } /** * Perform a query * From 920cd062fa2c951175932a5ff262375f540ccd23 Mon Sep 17 00:00:00 2001 From: fser Date: Tue, 8 Jul 2014 15:06:12 +0200 Subject: [PATCH 16/18] remove output --- phpunit/tests/bureau/class/m_actionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/phpunit/tests/bureau/class/m_actionTest.php b/phpunit/tests/bureau/class/m_actionTest.php index 2c2299f8..47925317 100644 --- a/phpunit/tests/bureau/class/m_actionTest.php +++ b/phpunit/tests/bureau/class/m_actionTest.php @@ -177,7 +177,6 @@ class m_actionTest extends AlterncTest { public function testGet_action() { $result = $this->object->get_action(); $this->assertTrue(is_array($result)); - fwrite(STDERR, print_r($result, TRUE)); $this->assertCount(1, $result); return current($result); } From b6be25f786f3ceda58bbc244d9004e7128fe24db Mon Sep 17 00:00:00 2001 From: fser Date: Tue, 8 Jul 2014 15:06:21 +0200 Subject: [PATCH 17/18] fixing db instance for good --- phpunit/bootstrap.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index 955922c8..294974d8 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -119,8 +119,7 @@ foreach ($mysqlConfigFile as $line) { * the connection to the MySQL database. */ class DB_system extends DB_Sql { - function __construct() { - global $database, $user, $password; + function __construct($database, $user, $password) { parent::__construct($database, '127.0.0.1', $user, $password); } } @@ -142,7 +141,7 @@ foreach ($queryList as $exec_command) { } echo "*** In progress: mysql.sql imported\n"; -$db = new \DB_system(); +$db = new \DB_system($database, $user, $password); $cuid = 0; $variables = new \m_variables(); $mem = new \m_mem(); From 0dc8c4c71afb67d5ed326d6c5d5a1bd6ae9246f8 Mon Sep 17 00:00:00 2001 From: fser Date: Tue, 8 Jul 2014 17:30:59 +0200 Subject: [PATCH 18/18] fix typo Error --- bureau/class/db_mysql.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bureau/class/db_mysql.php b/bureau/class/db_mysql.php index 31d399c9..c23d1d72 100644 --- a/bureau/class/db_mysql.php +++ b/bureau/class/db_mysql.php @@ -108,7 +108,7 @@ class DB_Sql { } function last_error() { - return $this->error; + return $this->Error; } /** * Perform a query