Merge commit 'origin/master' into fusionax
[platal.git] / modules / search / classes.inc.php
index 7ad0e8c..1d6a047 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -59,7 +59,7 @@ else
         IF(e.pub='public', je.name, '')      AS entreprise,
         IF(nw.pub='public', nw.address, '')  AS networking_address,
         IF(nw.pub='public', nwe.name, '')    AS networking_name,";
-@$globals->search->result_where_statement = '
+@$globals->search->result_where_statement = "
     LEFT JOIN  profile_education             AS edu0 ON (u.user_id = edu0.uid AND edu0.id = 0)
     LEFT JOIN  profile_education_enum        AS ede0 ON (ede0.id = edu0.eduid)
     LEFT JOIN  profile_education_degree_enum AS edd0 ON (edd0.id = edu0.degreeid)
@@ -80,15 +80,17 @@ else
     LEFT JOIN  profile_job_enum              AS ee   ON (e.jobid = ee.id)
     LEFT JOIN  profile_job_sector_enum       AS es   ON (es.id = e.sectorid)
     LEFT JOIN  fonctions_def                 AS ef   ON (e.fonction = ef.id)
-    LEFT JOIN  geoloc_pays                   AS n1   ON (u.nationalite = n1.a2)
-    LEFT JOIN  geoloc_pays                   AS n2   ON (u.nationalite2 = n2.a2)
-    LEFT JOIN  geoloc_pays                   AS n3   ON (u.nationalite3 = n3.a2)
-    LEFT JOIN  adresses                      AS adr  ON (u.user_id = adr.uid AND FIND_IN_SET(\'active\',adr.statut))
-    LEFT JOIN  geoloc_pays                   AS gp   ON (adr.country = gp.a2)
-    LEFT JOIN  geoloc_region                 AS gr   ON (adr.country = gr.a2 AND adr.region = gr.region)
-    LEFT JOIN  emails                        AS em   ON (em.uid = u.user_id AND em.flags = \'active\')
+    LEFT JOIN  geoloc_countries              AS n1   ON (u.nationalite = n1.iso_3166_1_a2)
+    LEFT JOIN  geoloc_countries              AS n2   ON (u.nationalite2 = n2.iso_3166_1_a2)
+    LEFT JOIN  geoloc_countries              AS n3   ON (u.nationalite3 = n3.iso_3166_1_a2)
+    LEFT JOIN  profile_addresses             AS adr  ON (u.user_id = adr.pid
+                                                         AND FIND_IN_SET('current', adr.flags))
+    LEFT JOIN  geoloc_countries              AS gp   ON (adr.countryId = gp.iso_3166_1_a2)
+    LEFT JOIN  geoloc_administrativeareas    AS gr   ON (adr.countryId = gr.country
+                                                         AND adr.administrativeAreaId = gr.id)
+    LEFT JOIN  emails                        AS em   ON (em.uid = u.user_id AND em.flags = 'active')
     LEFT JOIN  profile_networking            AS nw   ON (nw.uid = u.user_id)
-    LEFT JOIN  profile_networking_enum       AS nwe  ON (nwe.network_type = nw.network_type)';
+    LEFT JOIN  profile_networking_enum       AS nwe  ON (nwe.network_type = nw.network_type)";
 
 // }}}
 // {{{ class ThrowError
@@ -265,9 +267,9 @@ class QuickSearch extends SField
         $s = replace_accent(trim($this->value));
         $r = $s = str_replace('*','%',$s);
 
-        if (S::has_perms() && strpos($s, '@') !== false) {
+        if (S::admin() && strpos($s, '@') !== false) {
             $this->email = $s;
-        } else if (S::has_perms() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) {
+        } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) {
             $this->ip = $s;
         }
         if ($this->email || $this->ip) {
@@ -340,7 +342,14 @@ class QuickSearch extends SField
         }
         if (!empty($this->ip)) {
             $ip = ip_to_uint($this->ip);
-            $where[] = "( ls.ip = $ip OR ls.forward_ip = $ip ) AND ls.suid = 0";
+
+            // If the IP address requested for the search cannot be translated,
+            // the predicate should always be valued to false.
+            if ($ip != null) {
+                $where[] = "( ls.ip = $ip OR ls.forward_ip = $ip ) AND ls.suid = 0";
+            } else {
+                $where[] = "false";
+            }
         }
         if (!empty($this->phone)){
             require_once("profil.func.inc.php");
@@ -370,7 +379,7 @@ class QuickSearch extends SField
             $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";
+            $join .= "INNER JOIN #logger#.sessions AS ls ON (ls.uid = u.user_id)\n";
         }
         if (!empty($this->phone)) {
             if (!S::logged()) {
@@ -641,7 +650,7 @@ class StringSField extends SField
      * imposées par l'utilisateur) */
     function length()
     {
-        $cleaned = replace_accent(strtolower($this->value));
+        $cleaned = strtolower(replace_accent($this->value));
         $length  = strlen(ereg_replace('[a-z0-9]', '', $cleaned));
         return strlen($this->value) - $length;
     }