From e963a960861d87f61412ec3d10982031071f74cb Mon Sep 17 00:00:00 2001 From: "Pierre Habouzit (MadCoder" Date: Tue, 28 Dec 2004 14:08:18 +0000 Subject: [PATCH] mescontacts.php is now clean from raw $globals->db calls git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-157 --- htdocs/carnet/mescontacts.php | 92 ++++++++++++++++++++-------------------- include/xorg.globals.inc.php.in | 7 +-- include/xorg/database.inc.php | 90 +++++++++++++++++++++++++++------------ plugins/compiler.iterate.php | 7 --- templates/carnet/mescontacts.tpl | 2 +- 5 files changed, 111 insertions(+), 87 deletions(-) diff --git a/htdocs/carnet/mescontacts.php b/htdocs/carnet/mescontacts.php index 69fe441..dd03d07 100644 --- a/htdocs/carnet/mescontacts.php +++ b/htdocs/carnet/mescontacts.php @@ -29,29 +29,30 @@ $user = Env::get('user'); switch (Env::get('action')) { case 'retirer': if (preg_match('/^\d+$/', $user)) { - if ($globals->db->query("DELETE FROM contacts WHERE uid = $uid AND contact='$user'")) + if ($globals->xdb->execute('DELETE FROM contacts WHERE uid = {?} AND contact = {?}', $uid, $user)) { $page->trig("Contact retiré !"); } } else { - if ($globals->db->query( - "DELETE FROM contacts + if ($globals->xdb->execute( + 'DELETE FROM contacts USING contacts AS c - INNER JOIN aliases AS a ON (c.contact=a.id and a.type!='homonyme') - WHERE c.uid = $uid AND a.alias='$user'")) + INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme") + WHERE c.uid = {?} AND a.alias={?}', $uid, $user)) { $page->trig("Contact retiré !"); } } break; - case "ajouter": + case 'ajouter': require_once('user.func.inc.php'); if (($login = get_user_login($user)) !== false) { - if ($globals->db->query("INSERT INTO contacts (uid, contact) - SELECT $uid, id - FROM aliases - WHERE alias='$login'")) + if ($globals->xdb->execute( + 'INSERT INTO contacts (uid, contact) + SELECT {?}, id + FROM aliases + WHERE alias = {?}', $uid, $login)) { $page->trig('Contact ajouté !'); } else { @@ -64,22 +65,19 @@ if(Get::has('trombi')) { require_once('trombi.inc.php'); function getList($offset,$limit) { global $globals; - $uid = Session::getInt('uid'); - $res = $globals->db->query("SELECT COUNT(*) FROM contacts WHERE uid = $uid"); - list($total) = mysql_fetch_row($res); - mysql_free_result($res); + $uid = Session::getInt('uid'); + $res = $globals->xdb->query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid); + $total = $res->fetchOneCell(); - $res = $globals->db->query(" + $res = $globals->xdb->query(" SELECT u.prenom, IF(u.epouse='',u.nom,u.epouse) AS nom, a.alias AS forlife, u.promo FROM contacts AS c INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact) INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie') - WHERE c.uid = $uid + WHERE c.uid = {?} ORDER BY nom - LIMIT ".$offset*$limit.",$limit"); - $list = Array(); - while($tmp = mysql_fetch_assoc($res)) $list[] = $tmp; - mysql_free_result($res); + LIMIT {?}, {?}", $uid, $offset*$limit, $limit); + $list = $res->fetchAllAssoc(); return Array($total, $list); } @@ -88,33 +86,33 @@ if(Get::has('trombi')) { $trombi->setNbRows(4); $page->assign_by_ref('trombi',$trombi); } else { - $sql = "SELECT contact AS id, - a.*, l.alias AS forlife, - 1 AS inscrit, - a.perms != 'pending' AS wasinscrit, - a.deces != 0 AS dcd, a.deces, a.matricule_ax, FIND_IN_SET('femme', a.flags) AS sexe, - e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction, - IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166, - ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type, - ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type, - adr.ville, gp.a2, gp.pays, gr.name AS region, - IF(a.epouse<>'',a.epouse,a.nom) AS sortkey - FROM contacts AS c - INNER JOIN auth_user_md5 AS a ON (a.user_id = c.contact) - INNER JOIN aliases AS l ON (a.user_id = l.id AND l.type='a_vie') - LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = a.user_id) - LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id) - LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id) - LEFT JOIN geoloc_pays AS n ON (a.nationalite = n.a2) - LEFT JOIN applis_ins AS ai0 ON (a.user_id = ai0.uid AND ai0.ordre = 0) - LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid) - LEFT JOIN applis_ins AS ai1 ON (a.user_id = ai1.uid AND ai1.ordre = 1) - LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid) - LEFT JOIN adresses AS adr ON (a.user_id = adr.uid AND FIND_IN_SET('active', adr.statut)) - LEFT JOIN geoloc_pays AS gp ON (adr.pays = gp.a2) - LEFT JOIN geoloc_region AS gr ON (adr.pays = gr.a2 AND adr.region = gr.region) - WHERE c.uid = $uid - ORDER BY sortkey, a.prenom"; + $sql = "SELECT contact AS id, + a.*, l.alias AS forlife, + 1 AS inscrit, + a.perms != 'pending' AS wasinscrit, + a.deces != 0 AS dcd, a.deces, a.matricule_ax, FIND_IN_SET('femme', a.flags) AS sexe, + e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction, + IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166, + ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type, + ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type, + adr.ville, gp.a2, gp.pays, gr.name AS region, + IF(a.epouse<>'',a.epouse,a.nom) AS sortkey + FROM contacts AS c + INNER JOIN auth_user_md5 AS a ON (a.user_id = c.contact) + INNER JOIN aliases AS l ON (a.user_id = l.id AND l.type='a_vie') + LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = a.user_id) + LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id) + LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id) + LEFT JOIN geoloc_pays AS n ON (a.nationalite = n.a2) + LEFT JOIN applis_ins AS ai0 ON (a.user_id = ai0.uid AND ai0.ordre = 0) + LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid) + LEFT JOIN applis_ins AS ai1 ON (a.user_id = ai1.uid AND ai1.ordre = 1) + LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid) + LEFT JOIN adresses AS adr ON (a.user_id = adr.uid AND FIND_IN_SET('active', adr.statut)) + LEFT JOIN geoloc_pays AS gp ON (adr.pays = gp.a2) + LEFT JOIN geoloc_region AS gr ON (adr.pays = gr.a2 AND adr.region = gr.region) + WHERE c.uid = $uid + ORDER BY sortkey, a.prenom"; $page->assign_by_ref('citer', $globals->xdb->iterator($sql)); } diff --git a/include/xorg.globals.inc.php.in b/include/xorg.globals.inc.php.in index 8cf171f..adc24f2 100644 --- a/include/xorg.globals.inc.php.in +++ b/include/xorg.globals.inc.php.in @@ -89,12 +89,7 @@ class XorgGlobals extends DiogenesCoreGlobals if ($globals->debug) { $globals->db->trace_on(); } - } - - function dbconnect() - { - parent::dbconnect(); - $this->xdb =& new XOrgDB($this->db); + $globals->xdb =& new XOrgDB; } } diff --git a/include/xorg/database.inc.php b/include/xorg/database.inc.php index 7eaf0fe..1013cbb 100644 --- a/include/xorg/database.inc.php +++ b/include/xorg/database.inc.php @@ -25,16 +25,10 @@ require_once('diogenes.database.inc.php'); class XOrgDB { - // {{{ properties - - var $_db; - - // }}} // {{{ constructor - function XOrgDB(&$diog_db) + function XOrgDB() { - $this->_db =& $diog_db; } // }}} @@ -44,25 +38,36 @@ class XOrgDB { $args = func_get_args(); $query = array_map(Array($this, '_db_escape'), $args); - $query[0] = $args[0]; - return new XOrgDBResult(call_user_func_array('sprintf', $query), $this->_db); + $query[0] = str_replace('{?}', '%s', $args[0]); + return new XOrgDBResult(call_user_func_array('sprintf', $query)); } // }}} + // {{{ function execute() + + function execute() { + global $globals; + $args = func_get_args(); + $query = array_map(Array($this, '_db_escape'), $args); + $query[0] = str_replace('{?}', '%s', $args[0]); + return $globals->db->query(call_user_func_array('sprintf', $query)); + } + + // }}} // {{{ function iterator() function &iterator() { $args = func_get_args(); $query = array_map(Array($this, '_db_escape'), $args); - $query[0] = $args[0]; - return new XOrgDBIterator(call_user_func_array('sprintf', $query), $this->_db); + $query[0] = str_replace('{?}', '%s', $args[0]); + return new XOrgDBIterator(call_user_func_array('sprintf', $query)); } // }}} // {{{ function _db_escape - function _db_escape(&$var) + function _db_escape($var) { switch (gettype($var)) { case 'boolean': @@ -75,9 +80,9 @@ class XOrgDB case 'string': if (get_magic_quotes_gpc()) { - return addslashes(stripslashes($var)); + return "'".addslashes(stripslashes($var))."'"; } else { - return addslashes($var); + return "'".addslashes($var)."'"; } case 'NULL': @@ -85,7 +90,7 @@ class XOrgDB case 'object': case 'array': - return addslashes(serialize($var)); + return "'".addslashes(serialize($var))."'"; default: die(var_export($var, true).' is not a valid for a database entry'); @@ -107,9 +112,10 @@ class XOrgDBResult // }}} // {{{ constructor - function XOrgDBResult($query, &$db) + function XOrgDBResult($query) { - $this->_res =& $db->query($query); + global $globals; + $this->_res =& $globals->db->query($query); } // }}} @@ -124,7 +130,7 @@ class XOrgDBResult // }}} // {{{ function fetchRow - function &fetchRow() + function _fetchRow() { return mysql_fetch_row($this->_res); } @@ -132,7 +138,7 @@ class XOrgDBResult // }}} // {{{ function fetchAssoc - function &fetchAssoc() + function _fetchAssoc() { return mysql_fetch_assoc($this->_res); } @@ -140,26 +146,58 @@ class XOrgDBResult // }}} // {{{ function fetchAllRow - function &fetchAllRow() + function fetchAllRow() { $result = Array(); while ($result[] = mysql_fetch_row($this->_res)) { } array_pop($result); + $this->free(); return $result; } // }}} - // {{{ function fetchAssoc + // {{{ function fetchAllAssoc - function &fetchAllAssoc() + function fetchAllAssoc() { $result = Array(); while ($result[] = mysql_fetch_assoc($this->_res)) { } array_pop($result); + $this->free(); return $result; } // }}} + // {{{ function fetchOneAssoc() + + function fetchOneAssoc() + { + $tmp = $this->_fetchAssoc(); + $this->free(); + return $tmp; + } + + // }}} + // {{{ function fetchOneRow() + + function fetchOneRow() + { + $tmp = $this->_fetchRow(); + $this->free(); + return $tmp; + } + + // }}} + // {{{ function fetchOneCell() + + function fetchOneCell() + { + $tmp = $this->_fetchRow(); + $this->free(); + return $tmp[0]; + } + + // }}} // {{{ function numRows function numRows() @@ -185,9 +223,9 @@ class XOrgDBIterator extends XOrgIterator // }}} // {{{ - function XOrgDBIterator($query, &$db, $mode = MYSQL_ASSOC) + function XOrgDBIterator($query, $mode = MYSQL_ASSOC) { - $this->_result =& new XOrgDBResult($query, $db); + $this->_result =& new XOrgDBResult($query); $this->_pos = 0; $this->_total = $this->_result->numRows(); $this->_mode = $mode; @@ -196,7 +234,7 @@ class XOrgDBIterator extends XOrgIterator // }}} // {{{ function next () - function &next() + function next() { $this->_pos ++; if ($this->_pos > $this->_total) { @@ -204,7 +242,7 @@ class XOrgDBIterator extends XOrgIterator unset($this); return null; } - return $this->_mode != MYSQL_ASSOC ? $this->_result->fetchRow() : $this->_result->fetchAssoc(); + return $this->_mode != MYSQL_ASSOC ? $this->_result->_fetchRow() : $this->_result->_fetchAssoc(); } // }}} diff --git a/plugins/compiler.iterate.php b/plugins/compiler.iterate.php index 97d3026..c624334 100644 --- a/plugins/compiler.iterate.php +++ b/plugins/compiler.iterate.php @@ -46,13 +46,6 @@ function smarty_compiler_iterate($tag_attrs, &$compiler) $_from = $compiler->_dequote($_params['from']); $_item = $compiler->_dequote($_params['item']); - if (!is_subclass_of($compiler->_tpl_vars[$_from], 'XOrgIterator')) { - $compiler->_syntax_error("iterate: 'from' parameter has to be a instance of an XOrgIterator Object", - E_USER_ERROR, __FILE__, __LINE__); - return; - } - - return "while ((\$this->_tpl_vars['$_item'] =& \$this->_tpl_vars['$_from']->next()) !== null):"; } diff --git a/templates/carnet/mescontacts.tpl b/templates/carnet/mescontacts.tpl index f26d995..45f8944 100644 --- a/templates/carnet/mescontacts.tpl +++ b/templates/carnet/mescontacts.tpl @@ -38,7 +38,7 @@ il te suffit de cliquer sur l'icône ajout contact en face de son nom dans les résultats !

-{if $citer->total()} +{if $trombi || $citer->total()}

Pour récupérer ta liste de contacts dans un PDF imprimable :
[Triée par promo] -- 2.1.4