Skip to content

Commit

Permalink
Switch from mysql to mysqli PHP mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Simounet committed Oct 22, 2015
1 parent 61fa30e commit 1836b28
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 63 deletions.
6 changes: 3 additions & 3 deletions Event.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function __construct($guid=null,$title=null,$description=null,$content=null,$pub
function getEventCountPerFolder(){
$events = array();
$results = $this->customQuery('SELECT COUNT(`'.MYSQL_PREFIX.$this->TABLE_NAME.'`.`id`),`'.MYSQL_PREFIX.'feed`.`folder` FROM `'.MYSQL_PREFIX.$this->TABLE_NAME.'` INNER JOIN `'.MYSQL_PREFIX.'feed` ON (`'.MYSQL_PREFIX.'event`.`feed` = `'.MYSQL_PREFIX.'feed`.`id`) WHERE `'.MYSQL_PREFIX.$this->TABLE_NAME.'`.`unread`=1 GROUP BY `'.MYSQL_PREFIX.'feed`.`folder`');
while($item = mysql_fetch_array($results)){
while($item = $results->fetch_array()){
$events[$item[1]] = $item[0];
}

Expand All @@ -61,7 +61,7 @@ function getEventCountPerFolder(){

function getEventCountNotVerboseFeed(){
$results = $this->customQuery('SELECT COUNT(1) FROM `'.MYSQL_PREFIX.$this->TABLE_NAME.'` INNER JOIN `'.MYSQL_PREFIX.'feed` ON (`'.MYSQL_PREFIX.'event`.`feed` = `'.MYSQL_PREFIX.'feed`.`id`) WHERE `'.MYSQL_PREFIX.$this->TABLE_NAME.'`.`unread`=1 AND `'.MYSQL_PREFIX.'feed`.`isverbose`=0');
while($item = mysql_fetch_array($results)){
while($item = $results->fetch_array()){
$nbitem = $item[0];
}

Expand All @@ -73,7 +73,7 @@ function getEventsNotVerboseFeed($start=0,$limit=10000,$order,$columns='*'){
$objects = array();
$results = $this->customQuery('SELECT '.$columns.' FROM `'.MYSQL_PREFIX.'event` INNER JOIN `'.MYSQL_PREFIX.'feed` ON (`'.MYSQL_PREFIX.'event`.`feed` = `'.MYSQL_PREFIX.'feed`.`id`) WHERE `'.MYSQL_PREFIX.'event`.`unread`=1 AND `'.MYSQL_PREFIX.'feed`.`isverbose` = 0 ORDER BY '.$order.' LIMIT '.$start.','.$limit);
if($results!=false){
while($item = mysql_fetch_array($results)){
while($item = $results->fetch_array()){
$object = new Event();
foreach($object->getObject_fields() as $field=>$type){
$setter = 'set'.ucFirst($field);
Expand Down
4 changes: 2 additions & 2 deletions Feed.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function countUnreadEvents(){
$results = Feed::customQuery("SELECT COUNT(`".MYSQL_PREFIX."event`.`id`), `".MYSQL_PREFIX."event`.`feed` FROM `".MYSQL_PREFIX."event` WHERE `".MYSQL_PREFIX."event`.`unread` = 1 GROUP BY `".MYSQL_PREFIX."event`.`feed`") ;
if($results!=false){
$total = 0;
while($item = mysql_fetch_array($results)){
while($item = $results->fetch_array()){
$unreads[$item[1]] = $item[0];
$total += $item[0];
}
Expand All @@ -261,7 +261,7 @@ function getFeedsPerFolder(){

$results = Feed::customQuery("SELECT `".MYSQL_PREFIX."feed`.`name` AS name, `".MYSQL_PREFIX."feed`.`id` AS id, `".MYSQL_PREFIX."feed`.`url` AS url, `".MYSQL_PREFIX."folder`.`id` AS folder FROM `".MYSQL_PREFIX."feed` INNER JOIN `".MYSQL_PREFIX."folder` ON ( `".MYSQL_PREFIX."feed`.`folder` = `".MYSQL_PREFIX."folder`.`id` ) ORDER BY `".MYSQL_PREFIX."feed`.`name` ;");
if($results!=false){
while($item = mysql_fetch_array($results)){
while($item = $results->fetch_array()){
$name = $item['name'];
$feedsIdMap[$item['id']]['name'] = $name;

Expand Down
4 changes: 2 additions & 2 deletions Folder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Folder extends MysqlEntity{

function unreadCount(){
$results = $this->customQuery('SELECT COUNT(`'.MYSQL_PREFIX.'event`.`id`) FROM `'.MYSQL_PREFIX.'event` INNER JOIN `'.MYSQL_PREFIX.'feed` ON (`'.MYSQL_PREFIX.'event`.`feed` = `'.MYSQL_PREFIX.'feed`.`id`) WHERE `'.MYSQL_PREFIX.'event`.`unread`=1 AND `'.MYSQL_PREFIX.'feed`.`folder` = '.$this->getId());
$number = mysql_fetch_array($results);
$number = $results->fetch_array();
return $number[0];
}

Expand All @@ -31,7 +31,7 @@ function getEvents($start=0,$limit=10000,$order,$columns='*'){
$objects = array();
$results = $this->customQuery('SELECT '.$columns.' FROM `'.MYSQL_PREFIX.'event` INNER JOIN `'.MYSQL_PREFIX.'feed` ON (`'.MYSQL_PREFIX.'event`.`feed` = `'.MYSQL_PREFIX.'feed`.`id`) WHERE `'.MYSQL_PREFIX.'event`.`unread`=1 AND `'.MYSQL_PREFIX.'feed`.`folder` = '.$this->getId().' ORDER BY '.$order.' LIMIT '.$start.','.$limit);
if($results!=false){
while($item = mysql_fetch_array($results)){
while($item = $results->fetch_array()){
$object = new Event();
foreach($object->getObject_fields() as $field=>$type){
$setter = 'set'.ucFirst($field);
Expand Down
7 changes: 3 additions & 4 deletions Functions.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Functions

public static function secure($var,$level = 1){
$var = htmlspecialchars($var, ENT_QUOTES, "UTF-8");
if($level<1)$var = mysql_real_escape_string($var);
if($level<1)$var = mysqli_real_escape_string($var);
if($level<2)$var = addslashes($var);
return $var;
}
Expand Down Expand Up @@ -348,10 +348,9 @@ public static function testDb($server, $login, $pass, $db=null) {
/* Méthode hors des classes dédiées aux BDD afin de supporter le moins
de dépendances possibles. En particulier, pas besoin que le fichier
de configuration existe. */
$link = mysql_connect($server, $login, $pass);
$link = mysqli_connect($server, $login, $pass, $db);
if (false===$link) return false;
if (!is_null($db) && false===mysql_select_db($db, $link)) return false;
mysql_close($link);
mysqli_close($link);
return true;
}

Expand Down
17 changes: 4 additions & 13 deletions MysqlConnector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class MysqlConnector
private $bdd;
private $port;
public $debug=0;
private $connection = null;
public $connection = null;
public static $instance = null;

private function __construct(){
$this->connect();
public function __construct(){
$this->connection = new mysqli(MYSQL_HOST,MYSQL_LOGIN,MYSQL_MDP,MYSQL_BDD);
$this->connection->query('SET NAMES utf8');
}


Expand All @@ -42,16 +43,6 @@ public static function getInstance(){
}



public function connect(){
// @TODO use PDO or mysqli
$this->connection = @mysql_connect(MYSQL_HOST,MYSQL_LOGIN,MYSQL_MDP);
mysql_query('SET NAMES utf8');
mysql_select_db(MYSQL_BDD,$this->connection);
}



public function __toString(){
$retour = "";
$retour .= "instance de la classe MysqlConnector : <br/>";
Expand Down
47 changes: 22 additions & 25 deletions MysqlEntity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class MysqlEntity
{

private $dbconnector = false;
private $debug = false;
private $debugAllQuery = false;

Expand Down Expand Up @@ -70,14 +71,14 @@ protected function secure($value, $field){
case 'timestamp':
case 'longstring':
default;
$return = mysql_real_escape_string((string)$value);
$return = $this->dbconnector->connection->real_escape_string((string)$value);
break;
}
return $return ;
}

public function __construct(){
MysqlConnector::getInstance();
$this->dbconnector = MysqlConnector::getInstance();
}

public function __destruct(){
Expand All @@ -96,7 +97,7 @@ public function __destruct(){
public function destroy($debug=false)
{
$query = 'DROP TABLE IF EXISTS `'.MYSQL_PREFIX.$this->TABLE_NAME.'`;';
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$myQuery = $this->customQuery($query);
}

Expand All @@ -110,7 +111,7 @@ public function destroy($debug=false)
public function truncate($debug=false)
{
$query = 'TRUNCATE TABLE `'.MYSQL_PREFIX.$this->TABLE_NAME.'`;';
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$myQuery = $this->customQuery($query);
}

Expand Down Expand Up @@ -138,7 +139,7 @@ public function create($debug=false){
ENGINE InnoDB,
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
;';
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$myQuery = $this->customQuery($query);
}

Expand Down Expand Up @@ -171,7 +172,7 @@ public function massiveInsert($events){
}

$query .=';';
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;

$this->customQuery($query);
}
Expand Down Expand Up @@ -212,9 +213,9 @@ public function save(){

$query .=');';
}
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$this->customQuery($query);
$this->id = (!isset($this->id)?mysql_insert_id():$this->id);
$this->id = (!isset($this->id)?$this->dbconnector->connection->insert_id:$this->id);
}

/**
Expand All @@ -241,7 +242,7 @@ public function change($columns,$columns2,$operation='=',$debug=false){
if($i){$query .='AND ';}else{$i=true;}
$query .= '`'.$column.'`'.$operation.'"'.$this->secure($value, $column).'" ';
}
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$this->customQuery($query);
}

Expand Down Expand Up @@ -288,9 +289,9 @@ public function loadAll($columns,$order=null,$limit=null,$operation="=",$debug=f
if($limit!=null) $query .='LIMIT '.$limit.' ';
$query .=';';

if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
$execQuery = $this->customQuery($query);
while($queryReturn = mysql_fetch_assoc($execQuery)){
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$result = $this->customQuery($query);
while($queryReturn = $result->fetch_assoc()){

$object = new $this->CLASS_NAME();
foreach($this->object_fields as $field=>$type){
Expand Down Expand Up @@ -358,9 +359,9 @@ public function rowCount($columns=null)
}
}
$query = 'SELECT COUNT(1) FROM `'.MYSQL_PREFIX.$this->TABLE_NAME.'`'.$whereClause;
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$myQuery = $this->customQuery($query);
$number = mysql_fetch_array($myQuery);
$number = $myQuery->fetch_array();
return $number[0];
}

Expand All @@ -383,27 +384,23 @@ public function delete($columns,$operation='=',$debug=false){
$whereClause .= '`'.$column.'`'.$operation.'"'.$this->secure($value, $column).'"';
}
$query = 'DELETE FROM `'.MYSQL_PREFIX.$this->TABLE_NAME.'` WHERE '.$whereClause.' ;';
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.mysql_error();
if($this->debug)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$query.'<br>'.$this->dbconnector->connection->error;
$this->customQuery($query);

}

///@TODO: pourquoi deux méthodes différentes qui font la même chose ?
public function customExecute($request){
if($this->debugAllQuery)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$request.'<br>'.mysql_error();
$result = mysql_query($request);
if($this->debugAllQuery)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$request.'<br>'.$this->dbconnector->connection->error;
$result = $this->dbconnector->connection->query($request);
if (false===$result) {
throw new Exception(mysql_error());
throw new Exception($this->dbconnector->connection->error);
}
return $result;
}
public function customQuery($request){
if($this->debugAllQuery)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$request.'<br>'.mysql_error();
$result = mysql_query($request);
if (false===$result) {
throw new Exception(mysql_error());
}
return $result;
if($this->debugAllQuery)echo '<hr>'.$this->CLASS_NAME.' ('.__METHOD__ .') : Requete --> '.$request.'<br>'.$this->dbconnector->connection->error;
return $this->dbconnector->connection->query($request);
}


Expand Down Expand Up @@ -442,7 +439,7 @@ public function getObject_fields(){
public function tableExists() {
$table = '`'.MYSQL_PREFIX.$this->TABLE_NAME.'';
$result = $this->customQuery("SHOW TABLES LIKE '$table'");
$assoc = mysql_fetch_assoc($result);
$assoc = $result->fetch_assoc();
return false===$assoc ? false : true;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Update.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public static function ExecutePatch($simulation=false) {
if ($val != '') {
//remplacement des préfixes de table
$val = str_replace('##MYSQL_PREFIX##',MYSQL_PREFIX,$val);
$result = mysql_query($val);
$result = mysqli_query($val);
$ficlog = dirname(__FILE__).Update::FOLDER.'/'.substr($file,0,strlen($file)-3).'log';
if (false===$result) {
file_put_contents($ficlog, date('d/m/Y H:i:s').' : SQL : '.$val."\n", FILE_APPEND);
file_put_contents($ficlog, date('d/m/Y H:i:s').' : '.mysql_error()."\n", FILE_APPEND);
file_put_contents($ficlog, date('d/m/Y H:i:s').' : '.mysqli_error()."\n", FILE_APPEND);
} else {
file_put_contents($ficlog, date('d/m/Y H:i:s').' : SQL : '.$val."\n", FILE_APPEND);
file_put_contents($ficlog, date('d/m/Y H:i:s').' : '.mysql_affected_rows().' rows affected'."\n", FILE_APPEND);
file_put_contents($ficlog, date('d/m/Y H:i:s').' : '.mysqli_affected_rows().' rows affected'."\n", FILE_APPEND);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@
}else{
$test[$lib_success][]=_t('INSTALL_INFO_RIGHT');
}
if (!@function_exists('mysql_connect')){
$test[$lib_errors][] = _t('INSTALL_ERROR_MYSQLCONNECT');
if (!@function_exists('mysqli_connect')){
$test[$lib_errors][] = _t('INSTALL_ERROR_MYSQLICONNECT');
}else{
$test[$lib_success][] = _t('INSTALL_INFO_MYSQLCONNECT');
$test[$lib_success][] = _t('INSTALL_INFO_MYSQLICONNECT');
}
if (!@function_exists('file_get_contents')){
$test[$lib_errors][] = _t('INSTALL_ERROR_FILEGET');
Expand Down
4 changes: 2 additions & 2 deletions locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"INSTALL_ERROR_CURL":"The required function 'curl_exec' is inaccessible on your server, please install the Curl module for PHP.",
"INSTALL_ERROR_FILEGET":"The required function 'file_get_contents' is inaccessible on your server, check your version of PHP.",
"INSTALL_ERROR_FILEPUT":"The required 'file_put_contents' is inaccessible on your server, check your version of PHP.",
"INSTALL_ERROR_MYSQLCONNECT":"The required function 'mysql_connect' is inaccessible on your server, check your MySql installation.",
"INSTALL_ERROR_MYSQLICONNECT":"The required function 'mysqli_connect' is inaccessible on your server, check your MySql installation.",
"INSTALL_ERROR_PHPV":"Your PHP version ($1) is too old, it is possible that some features script include malfunctions.",
"INSTALL_ERROR_RIGHT":"Can't write in Leed directory, please add write permissions on the entire folder (sudo chmod 777 -R $1, think about shielding permissions later)",
"INSTALL_ERROR_SAFEMODE":"The script can not manage the timeout alone because your safe mode is enabled,<br/>in your PHP configuration file, set the max_execution_time variable to 0 or disable safemode.",
Expand All @@ -35,7 +35,7 @@
"INSTALL_INFO_CURL":"Requested function 'curl_exec' : OK",
"INSTALL_INFO_FILEGET":"Requested function 'file_get_contents' : OK",
"INSTALL_INFO_FILEPUT":"Requested function 'file_put_contents' : OK",
"INSTALL_INFO_MYSQLCONNECT":"Requested function 'mysql_connect' : OK",
"INSTALL_INFO_MYSQLICONNECT":"Requested function 'mysqli_connect' : OK",
"INSTALL_INFO_PHPV":"Php version compatibility ($1) : OK",
"INSTALL_INFO_RIGHT":"Permissions on the current folder: OK ",
"INSTALL_INFO_SAFEMODE":"Management timeout: OK",
Expand Down
4 changes: 2 additions & 2 deletions locale/eo.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"INSTALL_ERROR_CURL":"La funkcio nepra 'curl_exec' ne akceseblas ĉe via servilo, kontrolu vian version de PHP.",
"INSTALL_ERROR_FILEGET":"La funkcio nepra 'file_get_contents' ne akceseblas ĉe via servilo, kontrolu vian version de PHP.",
"INSTALL_ERROR_FILEPUT":"La funkcio nepra 'file_put_contents' ne akceseblas ĉe via servilo, kontrolu vian version de PHP.",
"INSTALL_ERROR_MYSQLCONNECT":"La funkcio nepra 'mysql_connect' ne akceseblas ĉe via servilo, kontrolu vian version de MySql.",
"INSTALL_ERROR_MYSQLICONNECT":"La funkcio nepra 'mysqli_connect' ne akceseblas ĉe via servilo, kontrolu vian version de MySql.",
"INSTALL_ERROR_PHPV":"Via versio de PHP ($1) estas tro malnova, eblas ke funkcioj misfunkcias.",
"INSTALL_ERROR_RIGHT":"Ne eblas skribi en la dosierujon Leed, bonvolu aldoni permesojn por skribi sur la tuta dosierujo (sudo chmod 777 -R $1, pripensu finagordi la permesoj poste)",
"INSTALL_ERROR_SAFEMODE":"La skripto ne povas mastrumi la tempolimo mem ĉar via safe mode estas aktivita en via agorda dosiero de PHP, ŝalti la variablon max_execution_time al 0 aŭ malŝalti la safemode.",
Expand All @@ -35,7 +35,7 @@
"INSTALL_INFO_CURL":"Funkcio nepra 'curl_exec': bone",
"INSTALL_INFO_FILEGET":"Funkcio nepra 'file_get_contents': bone",
"INSTALL_INFO_FILEPUT":"Funkcio nepra 'file_put_contents': bone",
"INSTALL_INFO_MYSQLCONNECT":"Funkcio nepra 'mysql_connect': bone",
"INSTALL_INFO_MYSQLICONNECT":"Funkcio nepra 'mysqli_connect': bone",
"INSTALL_INFO_PHPV":"Kongrueco versio PHP ($1): bone",
"INSTALL_INFO_RIGHT":"Permesoj sur aktuala dosierujo: bone",
"INSTALL_INFO_SAFEMODE":"Mastrumo de tempolimo: bone",
Expand Down
Loading

0 comments on commit 1836b28

Please sign in to comment.