Location: PHPKode > projects > sips > sipssys/code/search.inc.php
<?
/* sips -- the simple, integrated publishing system
 * Copyright (C) 2000 Haakon Nilsen <hide@address.com>
 *
 * $Id: search.inc.php,v 1.5 2000/08/02 20:28:39 haakon Exp $
 *
 * 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, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

 
function prettyCategoryName($c) {
   return ucwords(str_replace(".", " / ", ucwords(str_replace("_", " ", $c))));
}

function printCatsInSelect($type, $selected) {
   // lists all categories in a SELECT form element.
   global $config;
   $name = "category";
   if ($type == "multiple") {
      $extra = "size=\"8\" multiple";
      $name .= "[]";
   }
   print("\n<select name=\"" .$name ."\"" .$extra .">\n");
   $handle = opendir($config["sipssys"] ."/sites");
   while ($file = readdir($handle)) {
      if (($file != ".") && ($file != "..")) {
         $cats[] = prettyCategoryName($file) ."::" .$file;
      }
   }
   closedir($handle);
   sort($cats);
   for ($i = 0; $i < count($cats); $i++) {
      $c = explode("::", $cats[$i]); //hack since sort() doesn't seem to handle multidimensional arrays
      print("<option value=\"". $c[1] ."\"");
      if ($c[1] == $selected) {
         print(" selected");
      }
      print(">" .$c[0] ."</option>\n");

   }
   print("</select>\n");
}

function printSiteSelectionForm($action, $submit, $sites, $category, $type, $extrafields) {
   global $lightmarkcolor, $darkmarkcolor;
   print("<form action=\"" .$action ."\" method=\"post\">\n<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
   for ($i = 0; $i < count($sites)-1; $i++) {
      $darkcolor = !$darkcolor;
      if ($darkcolor) { // eyecandy
         $col = $darkmarkcolor;
      } else {
         $col = $lightmarkcolor;
      }
      print("<tr bgcolor=" .$col ."><td width=\"5%\"><div align=\"center\"><input type=\"" .$type ."\" name=\"sites[]\" value=\"". $sites[$i]["URL"] ."\"></div></td><td>&nbsp;" .$sites[$i]["Title"] ." (<a href=\"" .$sites[$i]["URL"] ."\">" .$sites[$i]["URL"] ."</a>)</td></tr>\n");
   }
   print("<input type=\"hidden\" name=\"category\" value=\"" .$category ."\">\n");
   print("</table>\n");
   print($extrafields ."\n");
   print("<input type=\"submit\" name=\"submit\" value=\"" .$submit ."\">\n");
   print("<input type=\"submit\" name=\"submit\" value=\"" .i18n("Cancel") ."\">\n</form>");
}

function printSubmitSiteForm($action, $url, $title, $desc, $category, $submit, $extrafields) {
   if ($url == "")
      $url = "http://";
   print("<form action=\"" .$action ."\" method=\"post\">\n");
   print(i18n("Category") .": ");
   printCatsInSelect("single", $category);
   print("<br>URL: <input type=\"text\" name=\"url\" value=\"" .$url ."\">\n");
   print("<br>" .i18n("Title") .": <input type=\"text\" name=\"title\" value=\"" .$title ."\">\n");
   print("<br>" .i18n("Description") .":<br><textarea name=\"desc\" rows=4 cols=60 wrap=virtual>" .$desc ."</textarea>");
   print("<br><input type=\"submit\" name=\"submit\" value=\"" .$submit ."\">\n");
   print("\n" .$extrafields ."\n");
   print("</form>\n");
}


function addLink($cat, $url, $title, $desc) {
   global $config;
   $fn = $config["sipssys"] ."/sites/$cat";
   $lines = file($fn);
  	$fp = fopen($fn, "w");
  	for ($i=0; $i<count($lines); $i++)
     	fwrite($fp, chop($lines[$i]) ."\n");
  	$desc = str_replace("\r\n", "<br>", $desc);
  	$desc = str_replace("::", ":", $desc);
  	fwrite($fp, $url ."::" .stripslashes($title) ."::" .stripslashes($desc));
  	fclose($fp);
}


function addSubmittedSite($category, $url, $title, $desc) {
   global $config;
   $fn = $config["sipsbase"] ."/admin/search/submittedsites";
   $lines = file($fn);
  	$fp = fopen($fn, "w");
   for ($i=0; $i<count($lines); $i++) {
      fwrite($fp, chop($lines[$i]) ."\n");
   }
  	$desc = str_replace("\r\n", "<br>", $desc);
   $desc = str_replace("::", ":", $desc);
   fwrite($fp, $url ."::" .stripslashes($title) ."::" .stripslashes($desc) ."::" .$category);
  	fclose($fp);
}

function getLinksInCategory($cat) {
   global $config;
   $fn = $config["sipssys"] ."/sites/$cat";
   $lines = file($fn);
   for ($i=0; $i<=count($lines); $i++) {
      $liste = split("::", $lines[$i]);
      $links[$i]["URL"] = $liste[0];
      $links[$i]["Title"] = $liste[1];
      $links[$i]["Desc"] = $liste[2];
   }
   return $links;
}



function removeSites($category, $sites) {
   global $config;
   $allsites = getLinksInCategory($category);
   for ($i = 0; $i < count($allsites); $i++) {
      $mustdie = false;
      for ($a = 0; ($a < count($sites) && $mustdie == false); $a++)
         $mustdie = ($sites[$a] == $allsites[$i]["URL"]);
      if (!$mustdie)
         $survivors[] = $allsites[$i];
   }

   unlink($config["sipssys"] ."/sites/$category");
   touch($config["sipssys"] ."/sites/$category"); // any better ways to empty a file??
   for ($i = 0; $i < count($survivors)-1; $i++)
      addLink($category, $survivors[$i]["URL"], $survivors[$i]["Title"], chop($survivors[$i]["Desc"]));
}


function countCategories() {
   global $config;
   // count the categories:
   $handle = opendir($config["sipssys"] ."/sites");
   while ($file = readdir($handle)) {
      if (($file != ".") && ($file != "..")) {
         $fno++;
      }
   }
   closedir($handle);
   return $fno;
}

function countSites() {
   global $config;
   // count the sites:
   $handle = opendir($config["sipssys"] ."/sites");
   while ($file = readdir($handle)) {
      if (($file != ".") && ($file != "..")) {
         $fno += count(file($config["sipssys"] ."/sites/$file"));
      }
   }
   closedir($handle);
   return $fno;
}


function searchLinksFor($s) {
   global $config;
   $s = strtolower($s);
   $handle=opendir($config["sipssys"] ."/sites");
   while ($file = readdir($handle)) {
      if (($file != ".") && ($file != "..")) {
         $cats[] = $file;
      }
   }
   closedir($handle);

   $hitindex = 0;
   $foundsome = false;
   for ($c=0; $c<=count($cats); $c++) {
      $a = getLinksInCategory($cats[$c]);
      for ($l=0; $l<count($a); $l++) {
         if ((strpos(" " .strtolower($a[$l]["Title"]), $s) > 0) || (strpos(" " .strtolower($a[$l]["Desc"]), $s) > 0)) {
            $hits[$hitindex]["URL"] = $a[$l]["URL"];
            $hits[$hitindex]["Title"] = $a[$l]["Title"];
            $hits[$hitindex]["Desc"] = $a[$l]["Desc"];
            $hits[$hitindex]["Cat"] = $cats[$c];
            $hitindex++;
            $foundsome = true;
         }
      }
   }
   if ($foundsome) {
      return $hits;
   } else {
      return false;
   }
}


function searchCategoriesFor($s) {
   global $config;
   $s = strtolower($s);
   $handle=opendir($config["sipssys"] ."/sites");
   while ($file = readdir($handle)) {
      if (($file != ".") && ($file != "..")) {
         $cats[] = $file;
      }
   }
   closedir($handle);
   $hitindex = 0;
   for ($c=0; $c<=count($cats); $c++) {
      $subcats = explode(".", $cats[$c]);
      if (strlen(strstr($subcats[count($subcats)-1], $s)) > 0) {
         $hits[] = $cats[$c];
      }
   }
   if (count($hits) > 0) {
      return $hits;
   } else {
      return false;
   }
}

?>
Return current item: sips