X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=modules%2Fsearch%2Fclasses.inc.php;h=91c924dfd4753d0497e3ad450f470d1788bc31da;hb=a7d35093a40837b3a17d7fbac0259995f084812c;hp=9be8cc4cedbccedd54ce15773ec1ce37459d4c53;hpb=bc67c37cd9da2052d61288d927c913e467e90c41;p=platal.git diff --git a/modules/search/classes.inc.php b/modules/search/classes.inc.php index 9be8cc4..91c924d 100644 --- a/modules/search/classes.inc.php +++ b/modules/search/classes.inc.php @@ -1,6 +1,6 @@ 0 AS actif,'; + (COUNT(em.email) > 0 OR FIND_IN_SET("googleapps", u.mail_storage) > 0) AS actif,'; // hide private information if not logged -if (S::logged()) +if (S::logged()) $globals->search->result_fields .=' q.profile_web AS web, q.profile_mobile AS mobile, @@ -69,24 +69,6 @@ else LEFT JOIN emails AS em ON (em.uid = u.user_id AND em.flags = \'active\')'; // }}} -// {{{ function display_lines() - -/** - * This function is a heuristic that approximatively tells - * how many lines of output the contact list will use. - */ -function display_lines($text) -{ - $n = 0; - $tokens = Array('', '
'); - foreach ($tokens as $t) { - $i = -1; - while ( ($i = strpos($text,$t,$i+1))!==false) { $n++; } - } - return $n; -} - -// }}} // {{{ class ThrowError /** handle errors for end-users queries @@ -96,17 +78,25 @@ function display_lines($text) */ class ThrowError { + public static $throwHook = array('ThrowError', 'defaultHandler'); + /** constuctor * @param $explain string the error (in natural language) */ - function ThrowError($explain) + public function __construct($explain) + { + call_user_func(ThrowError::$throwHook, $explain); + } + + /** defaut error handler + */ + private static function defaultHandler($explain) { global $page, $globals; $page->changeTpl('search/index.tpl'); $page->assign('xorg_title','Polytechnique.org - Annuaire'); $page->assign('baseurl', $globals->baseurl); - $page->register_modifier('display_lines', 'display_lines'); - $page->trig('Erreur : '.$explain); + $page->trigError($explain); $page->run(); } } @@ -148,7 +138,7 @@ class SField // }}} // {{{ function get_request() - /** récupérer la requête de l'utilisateur + /** récupérer la requête de l'utilisateur * on met une chaîne vide si le champ n'a pas été complété */ function get_request() { @@ -159,7 +149,7 @@ class SField // {{{ function get_where_statement() /** récupérer la clause correspondant au champ dans la clause WHERE de la requête - * on parcourt l'ensemble des champs de la bdd de $fieldDbName et on associe + * on parcourt l'ensemble des champs de la bdd de $fieldDbName et on associe * à chacun d'entre eux une clause spécifique * la clause totale et la disjonction de ces clauses spécifiques */ function get_where_statement() @@ -217,6 +207,9 @@ class QuickSearch extends SField var $strings; /** stores numerical ranges */ var $ranges; + /** stores admin searches */ + var $email; + var $ip; // }}} // {{{ constructor @@ -235,7 +228,7 @@ class QuickSearch extends SField function isempty() { - return empty($this->strings) && empty($this->ranges); + return empty($this->strings) && empty($this->ranges) && empty($this->email) && empty($this->ip); } // }}} @@ -245,12 +238,27 @@ class QuickSearch extends SField { parent::get_request(); $s = replace_accent(trim($this->value)); + $r = $s = str_replace('*','%',$s); + + if (S::has_perms() && strpos($s, '@') !== false) { + $this->email = $s; + } else if (S::has_perms() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) { + $this->ip = $s; + } + if ($this->email || $this->ip) { + $this->strings = $this->ranges = array(); + return; + } + $s = preg_replace('!\d+!', ' ', $s); - $s = str_replace('*','%',$s); $this->strings = preg_split("![^a-zA-Z%]+!",$s, -1, PREG_SPLIT_NO_EMPTY); + if (count($this->strings) > 5) { + global $page; + $page->trigWarning("Tu as indiqué trop d'éléments dans ta recherche, seuls les 5 premiers seront pris en compte"); + $this->strings = array_slice($this->strings, 0, 5); + } - $s = trim($this->value); - $s = preg_replace('! *- *!', '-', $s); + $s = preg_replace('! *- *!', '-', $r); $s = preg_replace('!([<>]) *!', ' \1', $s); $s = preg_replace('![^0-9\-><]!', ' ', $s); $s = preg_replace('![<>\-] !', '', $s); @@ -268,7 +276,7 @@ class QuickSearch extends SField { $where = Array(); foreach ($this->strings as $i => $s) { - if (Env::i('with_soundex')) { + if (Env::i('with_soundex') && strlen($s) > 1) { $t = soundex_fr($s); $where[] = "sn$i.soundex = '$t'"; } else { @@ -295,6 +303,14 @@ class QuickSearch extends SField if (!empty($wherep)) { $where[] = '('.join(' OR ',$wherep).')'; } + if (!empty($this->email)) { + $where[] = 'ems.email = ' . XDB::escape($this->email); + } + if (!empty($this->ip)) { + $ip = ip_to_uint($this->ip); + $where[] = "( ls.ip = $ip OR ls.forward_ip = $ip ) AND ls.suid = 0"; + } + return join(" AND ", $where); } @@ -303,8 +319,21 @@ class QuickSearch extends SField function get_select_statement() { $join = ""; + $and = ''; + $uniq = ''; foreach ($this->strings as $i => $s) { - $join .= "INNER JOIN search_name AS sn$i ON (u.user_id = sn$i.uid)\n"; + if (!S::logged()) { + $and = "AND FIND_IN_SET('public', sn$i.flags)"; + } + $myu = str_replace('snv', "sn$i", $uniq); + $join .= "INNER JOIN search_name AS sn$i ON (u.user_id = sn$i.uid $and$myu)\n"; + $uniq .= " AND sn$i.token != snv.token"; + } + if (!empty($this->email)) { + $join .= "LEFT JOIN emails AS ems ON (ems.uid = u.user_id)"; + } + if (!empty($this->ip)) { + $join .= "INNER JOIN logger.sessions AS ls ON (ls.uid = u.user_id)\n"; } return $join; } @@ -444,7 +473,16 @@ class RefSField extends SField return false; } $res = implode(' OR ', array_filter(array_map(array($this, 'get_single_match_statement'), $this->fieldDbName))); - return "INNER JOIN {$this->refTable} AS {$this->refAlias} ON ({$this->refCondition} AND ($res) )"; + if (is_array($this->refTable)) { + foreach ($this->refTable as $i => $refT) + $last = $i; + $inner = ""; + foreach ($this->refTable as $i => $refT) + $inner .= " INNER JOIN {$refT} AS {$this->refAlias[$i]} ON ({$this->refCondition[$i]} ".(($i == $last)?"AND ($res) ":"").")\n"; + return $inner; + } else { + return "INNER JOIN {$this->refTable} AS {$this->refAlias} ON ({$this->refCondition} AND ($res) )"; + } } // }}} @@ -463,8 +501,10 @@ class MapSField extends RefSField $this->mapId = Env::v($_fieldFormName, ''); else $this->mapId = $_mapId; + $this->value = $this->mapId; $this->RefSField($_fieldFormName, $_fieldDbName, $_refTable, $_refAlias, $_refCondition, true, false); } + function get_select_statement() { if ($this->mapId === '') return false; @@ -625,7 +665,7 @@ class PromoSField extends SField // }}} // {{{ constructor - /** constructeur + /** constructeur * compareField est un champ de formulaire très simple qui ne sert qu'à la construction de la * clause WHERE de la promo */ function PromoSField($_fieldFormName, $_compareFieldFormName, $_fieldDbName, $_fieldResultName) @@ -715,6 +755,11 @@ class SFieldGroup { $this->fields = $_fields; $this->and = $_and; + foreach ($this->fields as $key=>&$field) { + if (is_null($field)) { + unset($this->fields[$key]); + } + } } // }}} @@ -723,8 +768,10 @@ class SFieldGroup function too_large() { $b = true; - for ($i=0; $b && $ifields); $i++) { - $b &= $this->fields[$i]->too_large(); + for ($i=0 ; $b && $ifields) ; $i++) { + if (!is_null($this->fields[$i])) { + $b = $b && $this->fields[$i]->too_large(); + } } return $b; }