[SPIP] ~v3.0.21-->v3.0.22
[lhc/web/www.git] / www / ecrire / req / pg.php
index b93e876..9ce37df 100644 (file)
@@ -3,7 +3,7 @@
 /* *************************************************************************\
  *  SPIP, Systeme de publication pour l'internet                           *
  *                                                                         *
- *  Copyright (c) 2001-2012                                                *
+ *  Copyright (c) 2001-2016                                                *
  *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
  *                                                                         *
  *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
@@ -43,15 +43,15 @@ function req_pg_dist($addr, $port, $login, $pass, $db='', $prefixe='') {
        if ($p >0) $port = " port=$p" ; else $port = '';
        $erreurs = array();
        if ($db) {
-               @$link = pg_connect("host=$host$port dbname=$db user=$login password=$pass", PGSQL_CONNECT_FORCE_NEW);
-       } elseif (!@$link = pg_connect("host=$host$port user=$login password=$pass", PGSQL_CONNECT_FORCE_NEW)) {
+               @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW);
+       } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) {
                        $erreurs[] = pg_last_error();
-           if (@$link = pg_connect("host=$host$port dbname=$login user=$login password=$pass", PGSQL_CONNECT_FORCE_NEW)) {
+           if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) {
              $db = $login;
            } else {
                    $erreurs[] = pg_last_error();
              $db = _DEFAULT_DB;
-             $link = pg_connect("host=$host$port dbname=$db user=$login password=$pass", PGSQL_CONNECT_FORCE_NEW);
+             $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW);
            }
        }
        if (!$link) {
@@ -574,6 +574,10 @@ function spip_pg_groupby($groupby, $from, $select)
        if (!$groupby) return '';
 
        $groupby = spip_pg_frommysql($groupby);
+       // Ne pas mettre dans le Group-By des valeurs numeriques
+       // issue de prepare_recherche
+       $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i','', $groupby);
+       $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i','', $groupby);
        $groupby = preg_replace('/\s+AS\s+\w+\s*/i','', $groupby);
 
        return "\nGROUP BY $groupby"; 
@@ -866,7 +870,8 @@ function spip_pg_insertq_multi($table, $tab_couples=array(), $desc=array(), $ser
        
        // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci
        // une premiere fois pour ajouter maj dans les cles
-       $les_cles = spip_pg_ajouter_champs_timestamp($table, $tab_couples[0], $desc, $serveur);
+       $c = isset($tab_couples[0]) ? $tab_couples[0] : array();
+       $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur);
        
        $cles = "(" . join(',',array_keys($les_cles)). ')';
        $valeurs = array();
@@ -1030,7 +1035,7 @@ function spip_pg_cite($v, $t){
        if(is_null($v)) return 'NULL'; // null php se traduit en NULL SQL
 
        if (sql_test_date($t)) {
-               if (strpos("0123456789", $v[0]) === false)
+               if ($v AND (strpos("0123456789", $v[0]) === false))
                        return spip_pg_frommysql($v);
                else {
                        if (strncmp($v,'0000',4)==0)
@@ -1088,6 +1093,8 @@ function spip_pg_in($val, $valeurs, $not='', $serveur) {
 //
 // IN (...) souvent limite a 255  elements, d'ou cette fonction assistante
 //
+       // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale.
+       if (!$valeurs) return $not ? '0=0' : '0=1';
        if (strpos($valeurs, "CAST(x'") !== false)
                return "($val=" . join("OR $val=", explode(',',$valeurs)).')';
        $n = $i = 0;