<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* Original Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)   */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2005 by Philippe Brunier   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
if (!function_exists("Mysql_Connexion")) {include ("mainfile.php");}
   global $NPDS_Prefix;

   $offset=25;
   $limit_full_search=500;

   if (!isset($min)) $min=0;
   if (!isset($max)) $max=$min+$offset;
   if ($query=="") {
      $limit=" LIMIT 0, $limit_full_search";
   } else {
      $query = removeHack(stripslashes(htmlspecialchars(urldecode($query), ENT_QUOTES))); // electrobug
      $limit="";
   }
   include("header.php");
   if ($topic>0) {
      $result = mysql_query("select topicimage, topictext from topics where topicid='$topic'");
      list($topicimage, $topictext) = mysql_fetch_row($result);
   } else {
      $topictext = translate("All Topics");
      $topicimage = "all-topics.gif";
   }
   OpenTable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   if ($type == "users") {
      echo translate("Search in Users Database");
   } elseif ($type == "sections") {
      echo translate("Search in Sections");
   } elseif ($type == "reviews") {
      echo translate("Search in Reviews");
   } elseif ($type == "archive") {
      echo translate("Search in")." ".translate("Archives");
   } else {
      echo translate("Search in")." ".aff_langue($topictext);
   }
   echo "</td></tr></table>\n";
   echo "<br />\n";
   echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><form action=\"search.php\" method=\"get\"><tr><td>";
   if (($type == "users") OR ($type == "sections") OR ($type == "reviews")) {
      echo "<img src=\"$tipath/all-topics.gif\" align=\"right\" border=\"0\" alt=\"\" />";
   } else {
      if ((($topicimage) or ($topicimage!="")) and (file_exists("$tipath$topicimage"))) {
         echo "<img src=\"$tipath$topicimage\" align=\"right\" border=\"0\" alt=\"".aff_langue($topictext)."\" />";
      }
   }
   echo "<input class=\"TEXTBOX_STANDARD\" size=\"25\" type=\"text\" name=\"query\" value=\"".$query."\"> ";
   echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" value=\"".translate("Search")."\"><br /><br />";
   $toplist = mysql_query("select topicid, topictext from topics order by topictext");
   echo "<select class=\"TEXTBOX_STANDARD\" name=\"topic\">";
   echo "<option value=\"\">".translate("All Topics")."</option>\n";
   while(list($topicid, $topics) = mysql_fetch_row($toplist)) {
      if ($topicid==$topic) { $sel = "selected "; }
      echo "<option $sel value=\"$topicid\">".aff_langue($topics)."</option>\n";
      $sel = "";
   }
   echo "</select>";

   echo "<select class=\"TEXTBOX_STANDARD\" name=\"category\">";
   echo "<option value=\"0\">".translate("Articles")."</option>\n";
   $catlist = mysql_query("select catid, title from stories_cat order by title");
   while (list($catid, $title) = mysql_fetch_row($catlist)) {
      if ($catid==$category) { $sel = "selected "; }
      echo "<option $sel value=\"$catid\">".aff_langue($title)."</option>\n";
      $sel = "";
   }
   echo "</select>";

   $thing = mysql_query("select aid from authors order by aid");
   echo "<select class=\"TEXTBOX_STANDARD\" name=\"author\">";
   echo "<option value=\"\">".translate("All Authors")."</option>\n";
   while (list($authors) = mysql_fetch_row($thing)) {
         if ($authors==$author) { $sel = "selected "; }
         echo "<option $sel value=\"$authors\">$authors</option>\n";
         $sel = "";
   }
   echo "</select>";

   if ($days == "0") {
      $sel1 = "selected";
   } elseif ($days == "7") {
      $sel2 = "selected";
   } elseif ($days == "14") {
      $sel3 = "selected";
   } elseif ($days == "30") {
      $sel4 = "selected";
   } elseif ($days == "60") {
      $sel5 = "selected";
   } elseif ($days == "90") {
      $sel6 = "selected";
   }
   echo "<select class=\"TEXTBOX_STANDARD\" name=\"days\">
   <option $sel1 value=\"0\">".translate("All")."</option>
   <option $sel2 value=\"7\">1 ".translate("week")."</option>
   <option $sel3 value=\"14\">2 ".translate("weeks")."</option>
   <option $sel4 value=\"30\">1 ".translate("month")."</option>
   <option $sel5 value=\"60\">2 ".translate("months")."</option>
   <option $sel6 value=\"90\">3 ".translate("months")."</option>
   </select><br />";

   if (($type == "stories") or ($type=="")) {
      $sel1 = "checked";
   } elseif ($type == "comments") {
      $sel2 = "checked";
   } elseif ($type == "sections") {
      $sel3 = "checked";
   } elseif ($type == "users") {
      $sel4 = "checked";
   } elseif ($type == "reviews") {
      $sel5 = "checked";
   } elseif ($type == "archive") {
      $sel6 = "checked";
   }
   echo "<input type=\"radio\" name=\"type\" value=\"stories\" $sel1> ".translate("Stories")." ";
   echo "<input type=\"radio\" name=\"type\" value=\"archive\" $sel6> ".translate("Archives");
   echo "</td></tr>";
   echo "<tr><td><div class=\"SEPAR\"></div></td></tr>";
   echo "<tr><td>";
   echo "[ <input type=\"radio\" name=\"type\" value=\"comments\" $sel2> ".translate("Comments")." ";
   echo "<input type=\"radio\" name=\"type\" value=\"sections\" $sel3> ".translate("Sections")." ";
   echo "<input type=\"radio\" name=\"type\" value=\"users\" $sel4> ".translate("Users")." ";
   echo "<input type=\"radio\" name=\"type\" value=\"reviews\" $sel5> ".translate("Reviews")." ]";
   echo "</td></tr></form></table>";

   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   echo "&nbsp;</td></tr></table>\n";

   settype($min,"integer");
   settype($offset,"integer");
   if ($type=="stories" OR $type=="archive" OR !$type) {
      if ($category > 0) {
         $categ = "AND catid='$category' ";
      } elseif ($category == 0) {
         $categ = "";
      }
      if ($type=="stories" OR !$type) {
         $q = "select s.sid, s.aid, s.title, s.time, a.url, s.comments, s.topic, s.informant, s.ihome from stories s, authors a where s.archive='0' and s.aid=a.aid $categ";
      } else {
         $q = "select s.sid, s.aid, s.title, s.time, a.url, s.comments, s.topic, s.informant, s.ihome from stories s, authors a where s.archive='1' and s.aid=a.aid $categ";
      }
      if (isset($query)) $q .= "AND (s.title LIKE '%$query%' OR s.hometext LIKE '%$query%' OR s.bodytext LIKE '%$query%' OR s.notes LIKE '%$query%') ";
      // Membre OU Auteur
      if ($member !="")
         $q .= "AND s.informant='$member' ";
      else
         if ($author != "") $q .= "AND s.aid='$author' ";

      if ($topic != "") $q .= "AND s.topic='$topic' ";
      if ($days != "" && $days!=0) $q .= "AND TO_DAYS(NOW()) - TO_DAYS(time) <= '$days' ";
      $q .= " ORDER BY s.time DESC".$limit;
      $t = $topic;
      $x=0;
      if ($SuperCache) {
         $cache_clef="[objet]==> $q";
         $CACHE_TIMINGS[$cache_clef]=3600;
         $cache_obj = new cacheManager();
         $tab_sid=$cache_obj->startCachingObjet($cache_clef);
         if ($tab_sid!="") $x=count($tab_sid);
      }
      if (($cache_obj->genereting_output==1) or ($cache_obj->genereting_output==-1) or (!$SuperCache)) {
         $result = mysql_query($q);
         if ($result) {
            while (list($sid, $aid, $title, $time, $url, $comments, $topic, $informant, $ihome) = mysql_fetch_row($result)) {
               if (ctrl_aff($ihome,0)) {
                  $tab_sid[$x]['sid']=$sid;
                  $tab_sid[$x]['aid']=$aid;
                  $tab_sid[$x]['title']=$title;
                  $tab_sid[$x]['time']=$time;
                  $tab_sid[$x]['url']=$url;
                  $tab_sid[$x]['comments']=$comments;
                  $tab_sid[$x]['topic']=$topic;
                  $tab_sid[$x]['informant']=$informant;
                  $x++;
               }
            }
         }
      }
      if ($SuperCache) {
         $cache_obj->endCachingObjet($cache_clef,$tab_sid);
      }
      echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">\n";
      if ($x<$offset) {$increment=$x;}
      if (($min+$offset)<=$x) {$increment=$offset;}
      if (($x-$min)<$offset) {$increment=($x-$min);}

      for ($i=$min; $i<($increment+$min); $i++) {
         $rowcolor = tablos();
         $furl = "article.php?sid=".$tab_sid[$i][sid];
         if ($type=="archive") {$furl.="&amp;archive=1";}
         formatTimestamp($tab_sid[$i][time]);
         echo "<tr $rowcolor><td><span style=\"font-size: 9px;\">[".($i+1)."]</span>&nbsp;".translate("Contributed by")." <a href=\"user.php?op=userinfo&amp;uname=".$tab_sid[$i][informant]."\" class=\"NOIR\">".$tab_sid[$i][informant]."</a> : <a href=\"$furl\" class=\"NOIR\">".aff_langue($tab_sid[$i][title])."</a><br /><span style=\"font-size: 10px;\">".translate("Posted by ")."<a href=\"".$tab_sid[$i][url]."\" class=\"NOIR\">".$tab_sid[$i][aid]."</a></span>";
         echo " ".translate("on")." $datetime (<b>".$tab_sid[$i][comments]."</b>)<br /></td></tr>";
         echo "<tr><td><hr noshade class=\"ONGL\"</td></tr>\n";
      }
      if ($x==0) {
         echo "<tr><td align=\"center\" class=\"ROUGE\">".translate("No matches found to your query")."<br />";
         echo "</td></tr>";
      }
      echo "</table>";

      $prev=($min-$offset);
      echo "<br /><p align=\"right\">(".translate("Total")." : ".$x.")&nbsp;&nbsp;";
      if ($prev>=0) {
         echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$prev&amp;query=$query&amp;type=$type&amp;category=$category&amp;member=$member&amp;days=$days\" class=\"NOIR\">";
         echo "$offset ".translate("previous matches")."</a>";
      }
      if ($min+$increment<$x) {
         if ($prev>=0) echo "&nbsp;|&nbsp;";
         echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$max&amp;query=$query&amp;type=$type&amp;category=$category&amp;member=$member&amp;days=$days\" class=\"NOIR\">";
         echo translate("next matches")."</a>";
      }
      echo "</p>";
   // comments
   } elseif ($type=="comments") {
      $q="select c.tid, c.sid, c.subject, c.date, c.name, s.ihome from comments c, stories s where s.archive='0' and c.sid=s.sid and (c.subject like '%$query%' OR c.comment like '%$query%') order by c.date DESC".$limit;
      $x=0;
      if ($SuperCache) {
         $cache_clef="[objet]==> $q";
         $CACHE_TIMINGS[$cache_clef]=3600;
         $cache_obj = new cacheManager();
         $tab_sid=$cache_obj->startCachingObjet($cache_clef);
         if ($tab_sid!="") $x=count($tab_sid);
      }
      if (($cache_obj->genereting_output==1) or ($cache_obj->genereting_output==-1) or (!$SuperCache)) {
         $result = mysql_query($q);
         if ($result) {
            while (list($tid, $sid, $subject, $date, $name, $ihome) = mysql_fetch_row($result)) {
               if (ctrl_aff($ihome,0)) {
                  $tab_sid[$x]['tid']=$tid;
                  $tab_sid[$x]['sid']=$sid;
                  $tab_sid[$x]['subject']=$subject;
                  $tab_sid[$x]['date']=$date;
                  $tab_sid[$x]['name']=$name;
                  $x++;
               }
            }
         }
      }
      if ($SuperCache) {
         $cache_obj->endCachingObjet($cache_clef,$tab_sid);
      }
      echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">\n";
      if ($x<$offset) {$increment=$x;}
      if (($min+$offset)<=$x) {$increment=$offset;}
      if (($x-$min)<$offset) {$increment=($x-$min);}

      for ($i=$min; $i<($increment+$min); $i++) {
         $rowcolor = tablos();
         $furl = "article.php?thold=-1&amp;mode=flat&amp;order=1&amp;sid=".$tab_sid[$i][sid]."#".$tab_sid[$i][tid];
         echo "<tr $rowcolor><td><span style=\"font-size: 9px;\">[".($i+1)."]</span>&nbsp;<a href=\"$furl\" class=\"NOIR\">".aff_langue($tab_sid[$i][subject])."</a> ".translate("by")." ";
         if ($tab_sid[$i][name]=="") {echo $anonymous;} else {echo $tab_sid[$i][name];}
         echo " ".translate("on")." ".formatTimestamp($tab_sid[$i][date])."</td></tr>";
         echo "<tr><td><hr noshade class=\"ONGL\"</td></tr>\n";
      }
      if ($x==0) {
         echo "<tr><td align=\"center\" class=\"ROUGE\">".translate("No matches found to your query")."<br />";
         echo "</td></tr>";
      }
      echo "</table>";

      $prev=($min-$offset);
      echo "<br /><p align=\"right\">(".translate("Total")." : ".$x.")&nbsp;&nbsp;";
      if ($prev>=0) {
         echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$prev&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
         echo "$offset ".translate("previous matches")."</a>";
      }
      if ($min+$increment<$x) {
         if ($prev>=0) echo "&nbsp;|&nbsp;";
         echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$max&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
         echo translate("next matches")."</a>";
      }
      echo "</p>";
   // reviews
   } elseif ($type=="reviews") {
      $result = mysql_query("select id, title, text, reviewer from reviews where (title like '%$query%' OR text like '%$query%') order by date DESC limit $min,$offset");
      if ($result) {
         $nrows  = mysql_num_rows($result);
      }
      $x=0;
      echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">\n";
      if ($nrows>0) {
         while (list($id, $title, $text, $reviewer) = mysql_fetch_row($result)) {
            $rowcolor = tablos();
            $furl = "reviews.php?op=showcontent&amp;id=$id";
            echo "<tr $rowcolor><td><a href=\"$furl\" class=\"NOIR\">$title</a> ".translate("by")." $reviewer</td></tr>\n";
            echo "<tr><td><hr noshade class=\"ONGL\"</td></tr>";
            $x++;
         }
      } else {
         echo "<tr><td align=\"center\" class=\"ROUGE\">".translate("No matches found to your query")."<br />";
         echo "</td></tr>";
      }
      echo "</table>";

      $prev=$min-$offset;
      echo "<br /><p align=\"right\">";
      if ($prev>=0) {
         echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$prev&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
         echo "<b>$offset ".translate("previous matches")."</b></a>";
      }
      if ($x>=($offset-1)) {
         if ($prev>=0) echo "&nbsp;|&nbsp;";
         echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$max&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
         echo "<b>".translate("next matches")."</b></a>";
      }
      echo "</p>";
   // sections
   } elseif ($type=="sections") {
      $result = mysql_query("select artid, secid, title, content from ".$NPDS_Prefix."seccont WHERE (title like '%$query%' OR content like '%$query%') ORDER BY artid DESC limit $min,$offset");
      if ($result) {
         $nrows  = mysql_num_rows($result);
      }
      $x=0;
      echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">\n";
      if ($nrows>0) {
         while (list($artid, $secid, $title, $content) = mysql_fetch_row($result)) {
            $rowcolor = tablos();
            $rowQ2=Q_Select ("select secname, rubid from ".$NPDS_Prefix."sections where secid='$secid'", 3600);
            list(,$row2) = each($rowQ2);
            $rowQ3=Q_Select ("select rubname from ".$NPDS_Prefix."rubriques where rubid='$row2[1]'", 3600);
            list(,$row3) = each($rowQ3);
            if ($row3[0]!="Divers" AND $row3[rubname]!="Presse-papiers") {
               $surl = "sections.php?op=listarticles&amp;secid=$secid";
               $furl = "sections.php?op=viewarticle&amp;artid=$artid";
               echo "<tr $rowcolor><td><a href=\"$furl\" class=\"NOIR\">".aff_langue($title)."</a> ".translate("in the sub-section")." <a href=\"$surl\" class=\"NOIR\">".aff_langue($row2[0])."</a></td></tr>\n";
               echo "<tr><td><hr noshade class=\"ONGL\"</td></tr>";
               $x++;
            }
         }
         if ($x==0) {
            echo "<tr><td align=\"center\" class=\"ROUGE\">".translate("No matches found to your query")."<br />";
            echo "</td></tr>";
         }
      } else {
         echo "<tr><td align=\"center\" class=\"ROUGE\">".translate("No matches found to your query")."<br />";
         echo "</td></tr>";
      }
      echo "</table>";

      $prev=$min-$offset;
      echo "<br /><p align=\"right\">";
      if ($prev>=0) {
         echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$prev&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
         echo "<b>$offset ".translate("previous matches")."</b></a>";
      }
      if ($x>=($offset-1)) {
         if ($prev>=0) echo "&nbsp;|&nbsp;";
         echo  "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$max&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
         echo  "<b>".translate("next matches")."</b></a>";
      }
      echo "</p>";
   // users
   } elseif ($type=="users") {
      if (($member_list and $user) or $admin) {
         $result = mysql_query("select uname, name from users where (uname like '%$query%' OR name like '%$query%' OR bio like '%$query%') order by uname ASC limit $min,$offset");
         if ($result) {
            $nrows  = mysql_num_rows($result);
         }
         $x=0;
         echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">\n";
         if ($nrows>0) {
            while (list($uname, $name) = mysql_fetch_row($result)) {
               $rowcolor = tablos();
               $furl = "user.php?op=userinfo&amp;uname=$uname";
               if ($name=="") {
                  $name = "".translate("No name entered")."";
               }
               echo "<tr $rowcolor><td><a href=\"$furl\" class=\"NOIR\">$uname</a> ($name)</td></tr>\n";
               echo "<tr><td><hr noshade class=\"ONGL\"</td></tr>";
               $x++;
            }
         } else {
            echo "<tr><td align=\"center\" class=\"ROUGE\">".translate("No matches found to your query")."<br />";
            echo "</td></tr>";
         }
         echo "</table>";

         $prev=$min-$offset;
         echo "<br /><p align=\"right\">";
         if ($prev>=0) {
            echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$prev&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
            echo "<b>$offset ".translate("previous matches")."</b></a>";
         }
         if ($x>=($offset-1)) {
            if ($prev>=0) echo "&nbsp;|&nbsp;";
            echo "<a href=\"search.php?author=$author&amp;topic=$t&amp;min=$max&amp;query=$query&amp;type=$type\" class=\"NOIR\">";
            echo "<b>".translate("next matches")."</b></a>";
         }
         echo "</p>";
      }
   }
   CloseTable();
   include("footer.php");
?>