4 |
david |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/***************************************************************************\
|
|
|
4 |
* SPIP, Systeme de publication pour l'internet *
|
|
|
5 |
* *
|
|
|
6 |
* Copyright (c) 2001-2005 *
|
|
|
7 |
* Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
|
|
|
8 |
* *
|
|
|
9 |
* Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
|
|
|
10 |
* Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
|
|
|
11 |
\***************************************************************************/
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
//
|
|
|
15 |
// Ce fichier ne sera execute qu'une fois
|
|
|
16 |
if (defined("_ECRIRE_INC_DB_MYSQL")) return;
|
|
|
17 |
define("_ECRIRE_INC_DB_MYSQL", "1");
|
|
|
18 |
|
|
|
19 |
//
|
|
|
20 |
// Appel de requetes SQL
|
|
|
21 |
//
|
|
|
22 |
|
|
|
23 |
function spip_query_db($query) {
|
|
|
24 |
global $spip_mysql_link;
|
|
|
25 |
static $tt = 0;
|
|
|
26 |
$my_admin = (($GLOBALS['connect_statut'] == '0minirezo') OR ($GLOBALS['auteur_session']['statut'] == '0minirezo'));
|
|
|
27 |
$my_profile = ($GLOBALS['mysql_profile'] AND $my_admin);
|
|
|
28 |
$my_debug = ($GLOBALS['mysql_debug'] AND $my_admin);
|
|
|
29 |
|
|
|
30 |
$query = traite_query($query);
|
|
|
31 |
|
|
|
32 |
#spip_log($query);
|
|
|
33 |
if ($my_profile)
|
|
|
34 |
$m1 = microtime();
|
|
|
35 |
|
|
|
36 |
if ($GLOBALS['mysql_rappel_connexion'] AND $spip_mysql_link)
|
|
|
37 |
$result = mysql_query($query, $spip_mysql_link);
|
|
|
38 |
else
|
|
|
39 |
$result = mysql_query($query);
|
|
|
40 |
|
|
|
41 |
if ($my_profile) {
|
|
|
42 |
$m2 = microtime();
|
|
|
43 |
list($usec, $sec) = explode(" ", $m1);
|
|
|
44 |
list($usec2, $sec2) = explode(" ", $m2);
|
|
|
45 |
$dt = $sec2 + $usec2 - $sec - $usec;
|
|
|
46 |
$tt += $dt;
|
|
|
47 |
echo "<small>".htmlentities($query);
|
|
|
48 |
echo " -> <font color='blue'>".sprintf("%3f", $dt)."</font> ($tt)</small><p>\n";
|
|
|
49 |
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
if ($s = mysql_error()) {
|
|
|
53 |
if ($my_debug) {
|
|
|
54 |
echo _T('info_erreur_requete')." ".htmlentities($query)."<br>";
|
|
|
55 |
echo "« ".htmlentities($s)." »<p>";
|
|
|
56 |
}
|
|
|
57 |
spip_log($GLOBALS['REQUEST_METHOD'].' '.$GLOBALS['REQUEST_URI'], 'mysql');
|
|
|
58 |
spip_log("$s - $query", 'mysql');
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
# spip_log("$s - $query", 'mysql');
|
|
|
62 |
|
|
|
63 |
return $result;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// fonction appelant la precedente
|
|
|
67 |
// specifiquement pour les select des squelettes
|
|
|
68 |
// c'est une instance de spip_abstract_select, voir ses specs dans inc_calcul
|
|
|
69 |
// les \n et \t sont utiles au debusqueur
|
|
|
70 |
// traite_query pourrait y est fait d'avance, à moindre cout
|
|
|
71 |
|
|
|
72 |
function spip_mysql_select($select, $from, $where,
|
|
|
73 |
$groupby, $orderby, $limit,
|
|
|
74 |
$sousrequete, $having,
|
|
|
75 |
$table, $id, $serveur) {
|
|
|
76 |
|
|
|
77 |
$q = ($from ?("\nFROM " . join(",\n\t", $from)) : '')
|
|
|
78 |
. ($where ? ("\nWHERE " . join("\n\tAND ", $where)) : '')
|
|
|
79 |
. ($groupby ? "\nGROUP BY $groupby" : '')
|
|
|
80 |
. ($having ? "\nHAVING $having" : '')
|
|
|
81 |
. ($orderby ? ("\nORDER BY " . join(", ", $orderby)) : '')
|
|
|
82 |
. ($limit ? "\nLIMIT $limit" : '');
|
|
|
83 |
|
|
|
84 |
if (!$sousrequete)
|
|
|
85 |
$q = " SELECT ". join(", ", $select) . $q;
|
|
|
86 |
else
|
|
|
87 |
$q = " SELECT S_" . join(", S_", $select)
|
|
|
88 |
. " FROM (" . join(", ", $select)
|
|
|
89 |
. ", COUNT(".$sousrequete.") AS compteur " . $q
|
|
|
90 |
.") AS S_$table WHERE compteur=" . $cpt;
|
|
|
91 |
|
|
|
92 |
// Erreur ? C'est du debug de squelette, ou une erreur du serveur
|
|
|
93 |
|
|
|
94 |
if ($GLOBALS['var_mode'] == 'debug') {
|
|
|
95 |
boucle_debug_resultat($id, 'requete', $q);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if (!($res = @spip_query($q))) {
|
|
|
99 |
include_ecrire('inc_debug_sql.php3');
|
|
|
100 |
erreur_requete_boucle($q, $id, $table,
|
|
|
101 |
spip_sql_errno(),
|
|
|
102 |
spip_sql_error());
|
|
|
103 |
}
|
|
|
104 |
# spip_log($serveur . spip_num_rows($res) . $q);
|
|
|
105 |
return $res;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
//
|
|
|
109 |
// Passage d'une requete standardisee
|
|
|
110 |
// Quand tous les appels SQL seront abstraits on pourra l'ameliorer
|
|
|
111 |
|
|
|
112 |
function traite_query($query) {
|
|
|
113 |
if ($GLOBALS['table_prefix']) $table_pref = $GLOBALS['table_prefix']."_";
|
|
|
114 |
else $table_pref = "";
|
|
|
115 |
|
|
|
116 |
if ($GLOBALS['mysql_rappel_nom_base'] AND $db = $GLOBALS['spip_mysql_db'])
|
|
|
117 |
$db = '`'.$db.'`.';
|
|
|
118 |
|
|
|
119 |
// changer les noms des tables ($table_prefix)
|
|
|
120 |
if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) {
|
|
|
121 |
$suite = strstr($query, $regs[0]);
|
|
|
122 |
$query = substr($query, 0, -strlen($suite));
|
|
|
123 |
}
|
|
|
124 |
$query = preg_replace('/([,\s])spip_/', '\1'.$db.$table_pref, $query) . $suite;
|
|
|
125 |
|
|
|
126 |
return $query;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
//
|
|
|
130 |
// Connexion a la base
|
|
|
131 |
//
|
|
|
132 |
|
|
|
133 |
function spip_connect_db($host, $port, $login, $pass, $db) {
|
|
|
134 |
global $spip_mysql_link, $spip_mysql_db; // pour connexions multiples
|
|
|
135 |
|
|
|
136 |
// gerer le fichier ecrire/data/mysql_out
|
|
|
137 |
## TODO : ajouter md5(parametres de connexion)
|
|
|
138 |
if (@file_exists(_FILE_MYSQL_OUT)
|
|
|
139 |
AND (time() - @filemtime(_FILE_MYSQL_OUT) < 120)
|
|
|
140 |
AND !defined('_ECRIRE_INSTALL'))
|
|
|
141 |
return $GLOBALS['db_ok'] = false;
|
|
|
142 |
|
|
|
143 |
if ($port > 0) $host = "$host:$port";
|
|
|
144 |
$spip_mysql_link = @mysql_connect($host, $login, $pass);
|
|
|
145 |
$spip_mysql_db = $db;
|
|
|
146 |
$ok = @mysql_select_db($db);
|
|
|
147 |
|
|
|
148 |
$GLOBALS['db_ok'] = $ok
|
|
|
149 |
AND !!@spip_num_rows(@spip_query_db('SELECT COUNT(*) FROM spip_meta'));
|
|
|
150 |
|
|
|
151 |
// En cas d'erreur marquer le fichier mysql_out
|
|
|
152 |
if (!$GLOBALS['db_ok']
|
|
|
153 |
AND !defined('_ECRIRE_INSTALL')) {
|
|
|
154 |
spip_log("La connexion MySQL est out!");
|
|
|
155 |
@touch(_FILE_MYSQL_OUT);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
return $GLOBALS['db_ok'];
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
//
|
|
|
163 |
// Recuperation des resultats
|
|
|
164 |
//
|
|
|
165 |
|
|
|
166 |
function spip_fetch_array($r) {
|
|
|
167 |
if ($r)
|
|
|
168 |
return mysql_fetch_array($r);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/* Appels obsoletes
|
|
|
172 |
function spip_fetch_object($r) {
|
|
|
173 |
if ($r)
|
|
|
174 |
return mysql_fetch_object($r);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
function spip_fetch_row($r) {
|
|
|
178 |
if ($r)
|
|
|
179 |
return mysql_fetch_row($r);
|
|
|
180 |
}
|
|
|
181 |
*/
|
|
|
182 |
|
|
|
183 |
function spip_sql_error() {
|
|
|
184 |
return mysql_error();
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
function spip_sql_errno() {
|
|
|
188 |
return mysql_errno();
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
function spip_num_rows($r) {
|
|
|
192 |
if ($r)
|
|
|
193 |
return mysql_num_rows($r);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
function spip_free_result($r) {
|
|
|
197 |
if ($r)
|
|
|
198 |
return mysql_free_result($r);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
function spip_mysql_insert($table, $champs, $valeurs) {
|
|
|
202 |
spip_query("INSERT INTO $table $champs VALUES $valeurs");
|
|
|
203 |
return mysql_insert_id();
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
function spip_insert_id() {
|
|
|
207 |
return mysql_insert_id();
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
//
|
|
|
211 |
// Poser un verrou local a un SPIP donne
|
|
|
212 |
//
|
|
|
213 |
function spip_get_lock($nom, $timeout = 0) {
|
|
|
214 |
global $spip_mysql_db, $table_prefix;
|
|
|
215 |
if ($table_prefix) $nom = "$table_prefix:$nom";
|
|
|
216 |
if ($spip_mysql_db) $nom = "$spip_mysql_db:$nom";
|
|
|
217 |
|
|
|
218 |
$nom = addslashes($nom);
|
|
|
219 |
$q = spip_query("SELECT GET_LOCK('$nom', $timeout)");
|
|
|
220 |
list($lock_ok) = spip_fetch_array($q);
|
|
|
221 |
|
|
|
222 |
if (!$lock_ok) spip_log("pas de lock sql pour $nom");
|
|
|
223 |
return $lock_ok;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
function spip_release_lock($nom) {
|
|
|
227 |
global $spip_mysql_db, $table_prefix;
|
|
|
228 |
if ($table_prefix) $nom = "$table_prefix:$nom";
|
|
|
229 |
if ($spip_mysql_db) $nom = "$spip_mysql_db:$nom";
|
|
|
230 |
|
|
|
231 |
$nom = addslashes($nom);
|
|
|
232 |
spip_query("SELECT RELEASE_LOCK('$nom')");
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
//
|
|
|
237 |
// IN (...) est limite a 255 elements, d'ou cette fonction assistante
|
|
|
238 |
//
|
|
|
239 |
function calcul_mysql_in($val, $valeurs, $not='') {
|
|
|
240 |
if (!$valeurs) return '0=0';
|
|
|
241 |
|
|
|
242 |
$n = $i = 0;
|
|
|
243 |
$in_sql ="";
|
|
|
244 |
while ($n = strpos($valeurs, ',', $n+1)) {
|
|
|
245 |
if ((++$i) >= 255) {
|
|
|
246 |
$in_sql .= "($val $not IN (" .
|
|
|
247 |
substr($valeurs, 0, $n) .
|
|
|
248 |
"))\n" .
|
|
|
249 |
($not ? "AND\t" : "OR\t");
|
|
|
250 |
$valeurs = substr($valeurs, $n+1);
|
|
|
251 |
$i = $n = 0;
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
$in_sql .= "($val $not IN ($valeurs))";
|
|
|
255 |
|
|
|
256 |
return "($in_sql)";
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
?>
|