Location: PHPKode > projects > Website SuperSearch > search_search.php
<?php


// Website SuperSearch Version 0.5
// The free, open source, PHP and MySQL web-based   
// Website search utility
//
// Copyright (C) 2007 Luke Mounsey
// www.PreservedWord.com
// www.Alive-in-Christ.com
//
// search_search.php
// Contains all functions needed to search the website.
// This does NOT contain functions for DISPLAYING the results.
// Functions for displaying results are in the file "search_display.php"
//
// 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 included in the file 
// "license.txt" 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

// Program Name
$bss_name="Bible SuperSearch \"Exodus\" RELEASE";
// Program Version
$bss_version="2.0.00";
// Program Version Short
$bss_version_short="2.0";
// File Version
$bible_lookup_version=1.4;

ini_set("mysql.connect_timeout",300);


// Default Bible Version
//$bss_default_version="kjv";
$bible_version=$version;//$bss_default_version;
//$default_language="en";
//$language=$default_language;

$table_prefix="bible_";
// diagnostic mode
$diag=false;
global $language;

require_once("search_misc.php"); 
require_once("search_config.php");
$bss_default_version=$default_bible_version;

// getSearch
// Executes a lookup/search using the same paramaters
// @ param look - passage reference
// @ param sear - search query
// @ param vers - Bible version
// @ return - an (2 dimensional?) array of verse arrays

function getSearch($look="",$sear="",$vers="", $st=null){

global $lookup, $search, $version,$proxrange;

if($look==""){$look=$lookup;}
if($sear==""){$sear=$search;}
if($vers==""){$vers=$version;}
if($st==null){global $st;}

/*
$st=array("$searchtype");
if ($wholeword){$st[]="wholeword";}
else{$st[]="partial";}
*/

//echo("$look|$sear|$vers|".$st[0]."||".$st[1]."<br>");

// basic proximity search
if ($st[0]=="proximity"){


$terms=explode(" ",$sear);
return proxand($look,$terms,$proxrange);

//return getSearch("Ps 91","terror","kjv",array("All Words","wholeword"));

}

// look up references and reference/search combos

// Convert search reference shortcuts to the actual reference 
$look=rtrim($look,";");
$look=shortcut($look);
//echo("lookup |$look|");


$set_of_verses=array();
$ser_ver=array();

// separate the references


$lookupquery=explode(";",$look);

// PROCESS LOOKUP/SEARCH COMBINATIONS
$in=0;
while($lookupquery[$in]!=null){

//echo("|".$lookupquery[$in]."|");

$ref=explodeRef($lookupquery[$in]);

// if request for whole book and whole book = false and no search
// display only first chapter.
if (($wholebook!="true")&&($ref["chapter"]=="")&&($ref["verse"]=="")&&($sear=="")){
$ref["chapter"]=1;
}// end if

$verses=getVerses($ref["book"], $ref["chapter"], $ref["verse"], $sear, $st, $vers);
if(((is_array($verses))&(count($verses[0])!=0))||($sear=="")){
$set_of_verses[]=$verses;
}

//if(($sear!="")&&($verses!=null)){
//$ser_ver=array_merge($ser_ver,$verses);
//}


/*
// display error message if no verses are found

if (($verses[0]==null) || ($verses==null)){

// This displays a message when a search with a verse range limitation produces no 
// results.  For some reason, these if statments do not detect a case where an 
// unlimited search produces no results.   A similar if statement is able to 
// detect this case in displayVerses

if ($sear!=null){error("Your request for <span class='bss_errorsearch'>$lookupquery[$in]</span> searched for \"<span class='bss_errorsearch'>$search</span>\" returned no results.");}
else{error("Your request for \"<span class='bss_errorsearch'>$lookupquery[$in]</span>\" returned no results. &nbsp; Please edit your request and try again.");}
}// end if
*/

$in+=1;	
}// end while

// END LOOKUP/SEARCH COMBINATIONS

/*
echo("/".$ser_ver[1][0]["text"]."/ {g}  ".$verses[0][0]["text"]."<br><BR>");

echo("<pre>");
print_r($ser_ver);
echo("</pre>");

echo("<pre>");
print_r($verses);
echo("</pre>");
*/

// PROCESS SEARCHES

if (($sear!=null)&&(empty($look))){


// searches only, with no passage reference

$set_of_verses[]=getVerses("","","",$sear, $st, $vers);

// if the search returns no results, display a message
// (this doesn't actually detect search errors, simular to as mentioned above)
if ((empty($set_of_verses[0]))||(count($set_of_verses[0])==0)){
//error("Your search for \"<span class='bss_errorsearch'>$search</span>\" returned no results. &nbsp; Please edit your search and try again.");
}

else{$ser_ver=array_merge($ser_ver,$set_of_verses[0]);}// end else

}// end if (search only)



if($sear!=""){

$server=array();

$o=0;
while($ar=$set_of_verses[0][$o]){
$bibver=array();

$i=0;
while($arr=$set_of_verses[$i][$o]){

$bibver=array_merge($bibver,$arr);

//foreach($arr as $ar){$server[]=$ar;}

//$server=array_merge($server,$arr);

$i++;
}
$server[]=$bibver;
$o++;
}

/*
echo("<pre>");
print_r($set_of_verses);
echo("</pre>");
*/

return $server;


}
else{return $set_of_verses;}

}// end getSearch


// quickLookup
// Allows lookup of passages without specifiying search parameters
// @ param book - name or number of the book
// @ param chapter - chapter number
// @ param verse - verse number
// @ param version - Bible version to use - defaults to system default (kjv)
// @ returns - an array of verse arrays

function quickLookup($book, $chapter, $verse, $version=""){return getVerses($book, $chapter, $verse, "", "", $version);}

// quickSearch
// allows simple searching without having to specify lookup parameters
// @ param search - search query
// @ param searchtype - type of search to be performed - default system default
// @ param version - Bible version to use - defaults to system default (kjv)
// @ return array of verses
function quickSearch($search, $searchtype=""){return getVerses("","","",$search, $searchtype, "");}

// getVerses - main Bible lookup and search function
// @ param book - name or number of the book
// @ param chapter - chapter number
// @ param verse - verse number
// @ param search - search query
// @ param searchtype - specifies all words, any word, exact phrase, or advanced search
// @ param version - short name of Texus Receptus Bible version to use
// @ returns - an array of verse arrays

function getVerses($book, $chapter, $verse, $search, $searchtype, $version="", $start=false,$end=false){
global $default_bible_version, $wholebook, $table_prefix, $language, $show_query, $max_verses, $content_table, $searchlimit,$sortby, $content_date,$content_title;

$sort=explode("_",$sortby);

$version="";
$book="";
$chapter="";
$verse="";	

splitSearch($search);

if($searchtype[0]=="regexp"){$searchtype[0]="Exact Phrase";}

//echo($searchtype[0]);

if ($version==""){
//echo("Error: no version specified<br><BR>");

$version=$bss_default_version;}
if ($language==""){$language=getLanguage($version);}

$version=str_replace(" ","",$version);
$version_array=explode(",",$version);

// semi-default language
$lang=getLanguage($version_array[0]);

// if parallel search, make sure all Bibles are the same language
if(($search!=null)&&(count($version_array)>1)){


$test=true;
foreach ($version_array as $bib)

$test=($lang==getLanguage($bib))&($test);
//echo(getLanguage($bib));
//echo("<br>|$bib|");

if(!$test){

//echo("Sorry. &nbsp; You may only parallel search Bibles of the same language.<br><br>");
//return null;

}// end if

}// end check parallel search


//echo($language);

// check for content
// if book and search are empty, the form is blank
if (empty($book)&&(empty($search))){
//echo"You didn't enter any thing. &nbsp;  Please enter your lookup and/or search before submitting the form.<br><br>";
return null;
}// end if

// if search is just a common word, return null
if (commonWord($search)){
//error("You can't search for the common word \"$search\".   Too many results");
return null;
}// end if


$search=trim($search);

//echo($searchlimit);

$search_limit_array=explode(",",$searchlimit);

//print_r($search_limit_array);

// get booknumber


// Start building mySQL query that will be used 
$query=" WHERE ";//SELECT * FROM `$table_prefix$version` WHERE";

foreach($search_limit_array as $sl){

$query.="(";

// add book, chapter, and verse information to the query

// SEARCHING 
// build query for searches
// functions: and, or, phrase, advanced

if (!empty($search)){

// ANY WORD OR ALL WORDS
if (($searchtype[0]=="Any Word")||($searchtype[0]=="All Words")||($searchtype[0]=="A")){

// AND or OR
if ($searchtype[0]=="Any Word"){$delm=" OR ";}
else{$delm=" AND ";}

// break the search string into individual words
$list=explode(" ",$search);

// initiate this part of the query
// whole or partial words?
if (($searchtype[1]=="wholeword")||($searchtype[1]==1)){

$query.="(".wholeWordQuery($list[0],$sl);

}// end if
else{
$query.="(`$searchlimit` LIKE '%".$list[0]."%'";
}// end else
// end init

$in=1;

// build the rest of this part of the query
while($list[$in]!=null){

if ($searchtype[1]=="wholeword"){$query.="$delm".wholeWordQuery($list[$in]);}
else{
$query.="$delm`$searchlimit` LIKE '%".$list[$in]."%'";
}// end if

$in+=1;
}// end while

// finalize this part of the query
$query.=")";


}// end if (any word or all words)

// EXACT PHRASE
else if($searchtype[0]=="Exact Phrase"){

//$query.=advancedQuery($search);
$query.=" `$sl` REGEXP '$search'";

}// end EXACT PHRASE
else{
// ADVANCED SEARCH

//if (strpos($search,"PROX")!==false){return proxboolean($lookup,$search);}

$as=advancedSearch($search,$sl);
if ($as===null){return null;}
$query.=$as;
//
}// end advanced search

}//end if

// finalize the query
//$query.=" ORDER BY `book`,`chapter`,`verse`;";

$query.=") OR";

}// END LONG FOREACH

$query=substr($query,0,strlen($query)-3);

// display the query text if diagnostic mode is enabled.
if($show_query){echo("<br>$query<br><br>");}
// run the queries
$res=array();

$mo=0;
//while($ver=$version_array[$mo]){

$q2="select * from `$content_table` $query";

$csort="content_".$sort[0];


if($sortby!=null){$q2.=" order by `".${$csort}."` ".$sort[1];}

//echo("Query: $q2<br>");

$res=mysql_query($q2);
echo(mysql_error());
//$mo++;
//}// end while

if ($res==null){return null;}

// create an array in which to put the verses

$verses_parallel=array();

// 
$mo=0;
//while($ver=$version_array[$mo]){
$cn=0;
$verses=array();// temporary array

// pick each verse out of the query result and store in verses
while ($text = mysql_fetch_array($res)) { 
$verses[]=$text;
$cn+=1;
//echo("<span style='font-family:sans serif'>".$text["text"]."<br>");
//$vhigh=text;
if ($cn==$max_verses){break;}
}//end while

//if (count($verses)==0){$verses=null;}

$verses_parallel[]=$verses;
$mo++;
//}// end outer while


return $verses;//_parallel;

}//end getVerses



// explodeRef
// seperates a Bible passage reference into book, chapter, and verse.
// @ param ref - the passage
// @ return - exRef - associative array containing the book, chapter, verse

function explodeRef($ref){

global $lang, $default_language;

//$lang=$language;

//echo("Reference: $ref<br>");// diagnostic code

$ref=stripextraspaces($ref);
$ref=str_replace(array(" - ","- "," -"),"-",$ref);



$startpos=0;
while(substr($ref, $startpos, 1)==" "){$startpos+=1;}
$ref=substr($ref, $startpos);
// find the break between book and chapter
// Since the shortest a reference will be is 2 letters (Ps) and some have a space (1 Tim)
// we start looking for the space at index =2
$break1=strpos($ref," ",2);

if(($break1!==false)){

$subbr=$break1;//strpos($ref," ",$break1+1);
$char=substr($ref,$subbr+1,1);
$sub=true;
while(((!ctype_digit($char)))&&($sub!==false)&&($char!="")){

//$br=$subbr;

$sub=strpos($ref," ",$subbr+1);
$subbr=$sub;
$char=substr($ref,$subbr+1,1);

$br=$sub;
//echo((substr($ref,$subbr-1,1)." "));

}// end while

if ($br>$break1){$break1=$br;}
if ($br===false){$break1=0;}
//echo("char $char ");

}// end if

if(strtolower(substr($ref,0,6))=="random"){$break1=6;}

//echo("break1 $break1 ");

// Special case: allow for any abbreviation of "Song of Solomon"
// This is done because SoS fullname references do not work with above command
// and because the code allows for any abbreviation of all other books
if (substr($ref, 0, 6)=="Song o"){$break1=strpos($ref," ",6);}
if (substr($ref, 0, 9)=="Song of S"){$break1=strpos($ref," ",9);}

//$break1=26;

// find the value for "book"
$book=substr($ref,0,$break1);
if ($break1==0){$book=$ref;}

// Special case #2: Book ranges with spaces.
$notbook=trim(substr($ref,$break1));
$blast=substr($b1,strlen($b1)-1,1);
//echo("book |$book| notbook |$notbook|");

$book=trim(stripextraspaces($book));




/*

// this is old code for book ranges with spaces

if ((substr($notbook,0,1)=="-")||($blast=="-")){

$reflen=strlen($ref);
$hyph=strpos($ref,"-");
// find the next text
$in=$hyph+1;
while(substr($ref,$in,1)==" "){$in++;}

$br1=strposneg($ref," ",$in);

if($br1!=-1){
$b2=substr($ref,$in,$br1-$in);
$notbook=substr($ref,$br1+1);
}// end if
else {

$b2=substr($ref,$in);
echo($b2." | ");
$notbook="";
}// end else

if ($blast=="-"){$book=substr($book,0,strlen($book)-1);}
//echo("b1 |$book| b2 |$b2|<br>");
$ref=$book."-".$b2;
//echo("ref |$ref|<br>");
$break1=0;

// redetermine value of "book"
if ($break1!=0){$book=substr($ref,0,$break1);}
else {$book=$ref;}
}// end long if
*/

// find the break beween chapter and verse
$break2=strpos($ref,":",$break1);

// if the reference includes more than just the book
// if the book reference is a range, ignore chapter and verse
if(($break1!=0)&&((strposneg($book,"-")==-1)||($lang=="ru"))){
// if a book NUMBER is specified, and is less than 
// 10, add a leading zero
// CURRENTLY DOES NOT WORK
//echo("$book space<BR>"); // Diagnostic for below code
//if(("$book".""=="1")){
//$book="0$book";
//}// end if

// find the value for "chapter"
if($break2===false){$chapter=substr($ref,$break1+1);}
else{$chapter=substr($ref,$break1+1,$break2-($break1+1));}

// find the value for verse
if($break2!=0){$verse=substr($ref, $break2+1);//$break1=strpos($ref," ",2);
}

}// end if 
else{
// if only a book reference

$chapter=null;
$verse=null;
}// end else

//echo ("book |$book| chapter |$chapter| verse |$verse|<br>");

// store values in array and return
$exRef=array(book=>"$book", chapter=>"$chapter", verse=>"$verse", style=>$case);
return $exRef;
}// end explodeRef

function implodeRef($ref){

$imref=$ref["book"];

if($ref["chapter"]!=""){$imref.=" ".$ref["chapter"];
if($ref["verse"]!=""){$imref.=":".$ref["verse"];}
}


return $imref;

}// end imploderef

// random_passage
// returns random verse or chapter

function random_passage($chap,$version){


if (strtolower($chap)=="verse"){

$res=mysql_query("select * from `bible_$version` ORDER BY `index` DESC LIMIT 1");

$arr=mysql_fetch_array($res);
$num=$arr["index"];

//echo($num);

$index=mt_rand(0,$num);
//echo($index);

$res=mysql_query("select * from `bible_$version` where (`index` = '$index')");
//echo("select * from `bible_$version` where (`index` = '$index')".mysql_error());
$array=mysql_fetch_array($res);

$book=$array['book'];
$chapter=$array['chapter'];
$verse=$array['verse'];
}

if (strtolower($chap)=="chapter"){

$res=mysql_query("select * from `bible_$version` where (`verse`='1')");
$arr=array();

while($arr[]=mysql_fetch_array($res)){}


$num=count($arr);
//echo("num $num");

$in=mt_rand(0,$num);

$book=$arr[$in]['book'];
$chapter=$arr[$in]['chapter'];
$verse='';

}

$exRef=array(book=>"$book", chapter=>"$chapter", verse=>"$verse");

return($exRef);

}// end random_passage

// getBook
// returns an array from the table bible_books with all the information for a particular book
// @ param - book - either the full name, short name, or number of the book
// @ param - lang - 2 character language
// @ return - array containing all content in row for this book from bible_books

function getBook($book, $langu=""){

global $lang, $default_language, $table_prefix;


if($langu==""){$langu=$lang;}

$in=$book;
//echo($default_language);
//echo("<br>lang1 $lang");
if ($langu==""){$langu=$default_language;}



//echo($lang);
// if no book
if ($book==null){return null;}

// special cases
// This ensures that users get what they expected
$book=gbSpecialCase($book, "Ro", "Rom");
$book=gbSpecialCase($book, "Jud", "Jude");
$book=gbSpecialCase($book, "Am", "Amos");

// strip out period
$per=strposneg($book,".");
if ($per==strlen($book)-1){$book=substr($book,0,$per);}

//echo("langu|$langu");

// if fullname or short name
if(($book==0)||(strlen($book)>2)){

$q1="SELECT * FROM `bible_books_$langu` WHERE ((`short` LIKE '% $book%') OR (`short` LIKE '$book%') OR (`fullname` = '$book%') OR (`fullname` LIKE '$book') );";
}// end if
// if book number
else{


$q1="SELECT * FROM `bible_books_$langu` WHERE (`number` = '$book');";
//}
//if ($diag){
//echo("|$q1|<br>");
}//end else
error_reporting(E_ERROR | E_PARSE);
$rs=mysql_query($q1);
$array = mysql_fetch_array($rs);

// if no book found using above methods, try searching fullname using REGEXP
// this was omitted from query above because it tended to return the wrong book
// when multiple books matched the query, as only the first one is returned.
if ($array==null){
	$rs=mysql_query("SELECT * FROM `bible_books_$langu` WHERE (`fullname` REGEXP '$book');");
	$array = mysql_fetch_array($rs);
}// end if

// maybe book name is not in selected language

if (($array==null)&&($langu!=$default_language)){

// get book from the default language book list
$array=getBook($in, $default_language);

// does this book list exist?
$res=mysql_query("select * from `bible_books_$langu` LIMIT 1");
//echo(mysql_error());

// if book list for langu exists, use it
if (mysql_fetch_array($res)!=null){
$array=getBook($array["number"],$langu);

}

error_reporting(E_ERROR | E_WARNING| E_PARSE);
//if($array2!=null){$array=$array2;}
}

return $array;

}// end getBook

// gbSpecialCase
// implements case-insensitive special cases for getBook
// @ param book - book name
// @ param case - string to compare with book
// @ param set - string to set book to if it matches case
// @ return book - book name (may be modified)

function gbSpecialCase($book, $case, $set){

$bo=strtolower($book);
$BO=strtoupper($book);
$Bo=ucfirst($bo);
$B_O=ucwords($bo);

if (($book==$case)||($bo==$case)||($BO==$case)||($Bo==$case)||($B_O==$case)){
return $set;}
else {return $book;}

}// end gbSpecialCase

// makeQuery
// Builds the lookup query for book, chapter or verse references
// This preforms this action one at a time (ie, one of the above)
// @ param findme - The book, chapter or verse reference
// @ param type - Indicates whether findme is a book, chapter or verse reference

function makeQuery($findme, $type){

$verse=explode(",",$findme);
if ($type!="book"){$query =" AND (";}
else {$query = " (";}

$i=0;
// process each verse or verse range
while($verse[$i]!=null){

error_reporting(E_ERROR);
$br=strpos($verse[$i],"-");
if($type=="book"){$br=strpos($verse[$i],"-",2);}
error_reporting(E_ERROR | E_WARNING| E_PARSE);
if ($i>0){$query.=" OR ";}
// verse range lookup

// determine if this is a single verse or verse range??
if($br>0){
//echo($verse."?<br>");
$vers=$verse[$i]." ";

$lo=substr($vers,0,$br);

$hi=substr($vers,$br+1);
if (($lo+0)>($hi+0)){//echo "low hi";
//return "error $lo $hi";
}
//echo("Low $lo High |$hi|<br>");

if($type=="book"){
//print"Low [$lo] High [$hi]<br>";

$lo=bookNum($lo);
$sp=strpos($hi," ",2);
$hi=substr($hi,0,$sp);
//echo("book $hi<br>");
$hi=bookNum($hi);

}// end if
if (($hi==" ")||($hi==null)){$query.="(`$type`>=$lo)";}
else{$query.="(`$type`>=$lo AND `$type`<=$hi)";}

}//end if
else{
$ver=$verse[$i];
if ($type=="book"){$ver=bookNum($ver);}

$query.="`$type`='".$ver."'";
}//end else
// end verse range
$i+=1;
}// end while (end process verse)
$query.=")";

return $query;

}// end makeQuery

// wholeWordQuery
// builds the mySQL query for "whole word only" searches
// @ param word - the word to be searched for
// @ return qu - the (partial) query for a whole word

function wholeWordQuery($word, $searchlimit){


if ((strpos($term, " ")===false)&&(strpos($term, "<sp>")===false)){$op="LIKE";}
else{$op="REGEXP";}

$s=array("$word", "$word.", "$word,", "$word;" , "$word!", "$word:", "$word?", "$word\\'", "$word\"", "\"$word ","\\'$word ");
$i=1;
$qu="((`$searchlimit` $op '% $word %')";
while(!empty($s[$i])){

$qu.=" OR (`$searchlimit` $op '% ".$s[$i]."%')";
$i+=1;
}

$qu.="OR (`$searchlimit` $op '".""."$word %'))";
return $qu;
}// end wholeWordQuery

// bookNum
// returns the book number of the given book
// @ param - book - the book fullname, shortname (or even number) to get the number of
// @ returns - the number
function bookNum($book,$lang=""){

$lobook=getBook($book,$lang);
return $lobook["number"];

}// end bookNum

// shortcut
// Replaces a predefined search limit (shortcut) with the predefined reference
// Ex: "OT" is replaced with "Genesis-Malachi"
// @param ref - the reference list to check
// @return ref - the reference or if the reference was a predefined search, the 
// reference defined by the definintion

function shortcut($ref){

$refe=explode(";",$ref);
$in=0;
while($re=$refe[$in]){
$re=trim($re);

$qu="SELECT * FROM `bible_shortcuts_en` WHERE ((`name` = '$re') OR (`short1` = '$re') OR (`short2` = '$re') OR (`short3` = '$re'));"; 

$res=mysql_query($qu);
if($sm=mysql_fetch_array($res)){
///echo($sm["reference"]);
$refe[$in]= $sm["reference"];}

$refe[$in]=trim($refe[$in],";");
$in++;
}//end while

$ref=trim(implode("; ", $refe),";");
//echo("|$ref|");


return $ref;


}// end shortcut

// commonWord
// indicates if a given word is a commonly, frequently used word
// checks word against array of words
// @ param - word - the word to be checked
// @ returns - true if a common word, false otherwise.
function commonWord($word){

$word=str_replace(array("%",")","(","{","}","[","]"),"",$word);

$common = array("and","or","the","a","an","it","he","her","they","there","of","his","hers",
"their");

$i=0;
while($common[$i]!=null){
if($word==$common[$i]){return true;}
$i+=1;
}
return false;
}// end commonWord


// crossChapterLookup
// assists getVerses in looking up references across multiple chapters
// but can also be used independantly
// @ param - book - name or number of the book
// @ param - c1 - starting chapter
// @ param - v1 - starting verse
// @ param - c2 - ending chapter
// @ param - v2 - ending verse
// @ param search - string of words to search for
// @ param searchtype - specifies all words, any word, exact phrase, or advanced search
// @ param version - Bible Version
// @ returns verses - array of verses

function crossChapterLookup($book, $c1, $v1, $c2, $v2, $search, $searchtype, $version){


//echo("I am here");

// if the the first chapter is greater than the second chapter,
// there is a user error
if ($c1>$c2){
error("Your second chapter is greater than your first!");
return null;}

// if both chapters happen to be the same
// ex: Matt 26:5-26:41
if ($c1==$c2){return $verses=getVerses($book, $c1, $v1."-".$v2, $search, $searchtype, $version);}
$vers=array();


$vers[]=getVerses($book, $c1, $v1."-", $search, $searchtype, $version);
//$bnum=booknum($book);
$chap=$c1+1;
while($chap<$c2){
$vers[]=getVerses($book, $chap, "", $search, $searchtype, $version);
$chap+=1;
}


/*
// merge the resultant arrays
$i=0;
while($ver=$vers[$i]){
//$verses=array_merge($verses, $ver);
$i+=1;
}
*/

// special case #2  
// Ex Gen 1:5-2:-
// Interpreted as Genesis 1 verse five through (all of) chapter 2
// For looking up cross chapter passages where the entire final chapter
// is desired and the number of verses in the chapter is unknown.
// This single line of code is all that is required.

if ($v2=="-"){$v2="";}

$vers[]=getVerses($book, $c2, "1-$v2", $search, $searchtype, $version);

/*
echo("<pre>");
print_r($vers);
echo("</pre>");
*/

$chap=$c1;
$num_bib=count(explode(",",$version));

$verses=array();
//echo("num_bib $num_bib");



//if($num_bib==1){return $vers;}

/*
for($bib_num=0;$bib_num<$num_bib;$bib_num++){
$bib=array();
for($chap=0;$chap<=$c2-$c1;$chap++){

//echo("chap:$chap num:$bib_num<br>");
//$num=$chap+$bib_num-$c1;
if($num_bib==1){
$bib=array_merge($bib,$vers[$chap]);
}
else{
$bib=array_merge($bib,$vers[$chap][$bib_num]);
}

}// end for

if($num_bib==1){return $bib;}

$verses[]=$bib;
}// end for
*/

$vnum=0;
while($n=$vers[0][$vnum]){
$thar=array();
foreach($vers as $list){

$thar=array_merge($thar, $list[$vnum]);

}// end foreach

$verses[]=$thar;
$vnum++;
}// end while

//echo("vnum|$vnum");

//$ch=count($verses[3]);
//echo("ch $ch|<br>");
//print_r($verses[0][0]["text"]);

/*
echo("<pre>");
print_r($verses);
echo("</pre>");
*/

return $verses;
}// end crossChapterLookup

// getChapters and getNumChapters
// finds the number of chapters in a particular book
// @ param - book - The book reference
// @ returns - The number of chapters in this book

function getNumChapters($book){return getChapters($book);}
function getChapters($book,$lang=""){

global $language, $version;

$bok = bookNum($book,$lang);

$ver=explode(",",$version);
$vers=$ver[0];

$qu="select * from `bible_$vers` where (`book` = '$book') ORDER BY `index` DESC LIMIT 1;";
//echo("$qu<br>");
$res=mysql_query($qu);
$ve=mysql_fetch_array($res);

//echo($ve["im

return $ve["chapter"];

}// end getChapters

// countChapters
// counts the number of chapters uploaded in a particular book
// @ param - book - The book reference
// @ param - version - Bible Version
// @ returns - The number of chapters found in this book
function countChapters($book, $version=""){
global $bss_default_version;
if ($version==""){$version=$bss_default_version;}

}// end countChapters

// getNumVerses
// finds the number of verses in a particular chapter
// @ param - book - The book reference
// @ param - chapter - The chapter reference
// @ param - version - Bible version to use (Bibles before KJV numbered their verses differently)
// @ returns - The number of verses in this chapter

function getNumVerses($book, $chapter, $versi=""){

global $version, $table_prefix;

if (empty($versi)){$versi=$version;}



//if ((strposneg(",",$versi))!=-1){
$versi=explode(",",$versi);
$versi=$versi[0];
//}



// GET BETTER WAY TO DO THIS!!!!
// Doesn't work for Luther Bible!!!

$bnum = bookNum($book);
$query = "Select * from `$table_prefix$versi` WHERE (`book` = '$bnum') AND (`chapter` = '$chapter') ORDER BY `verse` DESC LIMIT 1;";
$res = mysql_query($query);
$verse = mysql_fetch_array($res);
return $verse['verse'];

}// end getNumVerses

// strposneg
// gives the position of the first occurence of $search in $string
// unlike strpos, returns -1 if not found
// @ param $string - string to search
// @ param $search - string to find
// @ param $offset - string offset
// @ return $pos - position of $search, -1 if not found
function strposneg($string, $search, $offset=0){



if (strlen($string)<($offset)){return -1;}

$pos=strpos($string, $search, $offset);
$len=strlen($search);
if (substr($string,0,$len)==$search){return 0;}
if ($pos>0){return $pos;}
else {return -1;}

}// end strposneg

// phpcompare
// compares two versions of php
// yes, i know that php has this function built in,
// but only in newer versions.
// Note: this strips the version to the first five characters
// @param version1 version of php in "m.n.o" format
// @param version2 version of php in "m.n.o" format
// @return 0 if both are equal, -1 if version1 < version2, 
//	and +1 if version1 > version2
function phpcompare($version1, $version2){

$version1=substr($version1,0,5);
$version2=substr($version2,0,5);

$v1=explode(".",$version1);
$v2=explode(".",$version2);

if ($version1==$version2){return 0;}
if ($v1[0]>$v2[0]){return 1;}
if ($v1[0]<$v2[0]){return -1;}
if ($v1[1]>$v2[1]){return 1;}
if ($v1[1]<$v2[1]){return -1;}
if ($v1[2]>$v2[2]){return 1;}
if ($v1[2]<$v2[2]){return -1;}

}// end phpcompare

// splitSearch
// @param $sea search query
// @return $search_array array of search terms
function splitSearch($sea="", $keep_underscores=false){

global $search;
if ($sea==""){$sea==$search;}

$first_quo=array();
$last_quo=array();
$exact=array();

$fi=-1;
$la=-1;
$i=true;

// find the positions of the quotations
while($i==true){

$fi=strpos($sea, "\\\"", $la+1);
if ($fi!==false){
$la=strpos($sea, "\\\"", $fi+1);
if ($la===false){break;}
//$first_quo[]=$fi;
//$last_quo[]=$la;

$exact[]=substr($sea,$fi, $la-$fi+2);

}// end if

else{$i=false;}

}// end while
$exact_sv=array();

foreach ($exact as $mm){
//echo("$mm<br>");
$exact_sv[]=advTransform($mm);
//echo("tx: <xmp>".advTransform($mm)."</xmp><br>");
//echo("!$mm!<br>");
}

$sea=str_replace($exact, $exact_sv, $sea);
//echo("|$sea|<br>");
$find=array("(",")", "||","&&","!!","&","*","+","|","-","NOT", "AND","OR","!=","XOR","^","^^");
$sea=str_replace($find, " ",$sea);
$sea=stripExtraSpaces($sea);


$array=explode(" ",trim($sea));

if (!$keep_underscores){
$array=invAdvTransform($array);
}// end if

//foreach($array as $i){
//echo("|$i|<br>");
//advancedQuery($i);
//}

// remove repeated words
$i=0;
while($i<count($array)){

$value=$array[$i];
$j=$i+1;
while($j<count($array)){

if ($array[$j]==$value){unset($array[$j]);}
$j++;
}// end while

$i++;
}// end while (repeated words)

return $array; 
}// end splitSearch

// advancedSearch
// produces query for advanced searches

function advancedSearch($search, $sl=""){

global $searchlimit;

if($sl==""){$sl=$searchlimit;}

//echo("I am here");

$terms_u=splitSearch($search, true);
$terms=splitSearch($search);

$search=str_replace($terms, $terms_u, $search);

$rep=array();
$i=0;
while ($i<count($terms)){

$rep[]=" ".advancedQuery($terms[$i], $sl)." ";
$terms[$i]=" ".$terms[$i]." ";
$terms_u[$i]=" ".$terms_u[$i]." ";
//echo("|$terms[$i]|<br>");
$i++;
}// end while

//print_r($rep);

// are parentenses balanced?
if(!checkPar($search,"(",")")){return null;}
if(!checkPar($search,"{","}")){return null;}
if(!checkPar($search,"[","]")){return null;}
if(!checkPar($search,"<",">")){return null;}

//foreach($terms as $t){echo "$t<br>";}

$search=stripExtraSpaces($search);

// prep the search string before converting it to query
$ad_ser=array("(",")","{","}","[","]");
$ad_rep=array(" ( ", " ) "," ( ", " ) "," ( ", " ) ");
$search=str_replace($ad_ser, $ad_rep, $search);

//echo($search);

$search=" $search ";
// operator aliases
$and=array("AND", "*","&&","&");
$oR=array("OR","+","||","|");
$NOT=array("NOT", "-","!=","-");
$XOR=array("XOR","^^","^");

$search=str_replace($XOR," ^ ",$search);
$search=str_replace($and, " & ",$search);
$search=str_replace($oR, " | ",$search);
$search=str_replace($NOT, " - ",$search);


// if only NOT searches, return null

// count number of NOTs and ORs
$cneg=count(explode("-",$search))-1;
$cor=count(explode("|",$search))-1;
$ct=count($terms);
$ss=str_replace("| ","+",$search);
//echo("|cneg:$cneg|cor:$cor|ct:$ct|$search|".strpos($search,"| -")."|");

/*
if ((($ct==$cor+1)&&($cneg!=0))||($cneg==$ct)||(strpos($search,"| -")!==false)){
echo("You can't do just NOT searches, it will return the entire Bible!<br><BR>");
//return null;
}
*/

$search=advancedExamine($search);

$query=str_replace($terms_u, $rep, $search);
$query=stripExtraSpaces($query);

$ser=array("&","|","-","^");
$rer=array("AND","OR"," NOT ","XOR");
$query=str_replace($ser,$rer, $query);

$query=str_replace("\\\"","",$query);
//$query=str_replace("_"," ",$query);
//$query=str_replace("^","_",$query);
//echo ("query $query<br>");


return "($query)";
}// end advancedSearch

// advancedQuery
// creates advanced search query for single term
// @param $term - term to create query for
// @return $query - query for the term
function advancedQuery($term, $sl=""){

if($sl==""){$sl="text";}

$op="REGEXP";
if (((strpos($term, " ")===false) && (strpos($term, "_")===false))||((strpos($term, "<sp>")===false) && (strpos($term, "_")===false))){$op="LIKE";}
else{$op="REGEXP";}


//echo($op);
//$op="LIKE";

global $wholeword;

if ((($wholeword=="Whole words only.")||($wholeword=="true"))&&($op!="REGEXP")){
//$query=wholeWordQuery($term);
//echo("$query<br>");


return wholeWordQuery($term, $sl);}

$query="`$sl` $op '%$term%'";
echo("$query<br>");
return $query;

}// end advancedQuery

function advTransform($query,$inv=false){

$tx=array(")","(", " ",  "AND", "OR", "NOT","XOR","{","}","[","]","&","!","-","|","*","+","^","PROX","NEAR","'","_");
$rx=array("<rp>","<lp>","<sp>","<and>","<or>","<not>","<xor>","<lc>", "<rc>","<lb>","<rb>","<amp>","<ex>","<neg>","<ver>", "<sta>","<pl>","<hat>","<prx>","<ner>","<sq>","<usc>");

$txc=count($tx);
$rxc=count($rx);

if($txc!=$rxc){echo("Transform error: txc:$txc rxc:$rxc");}

if(!$inv){$que=str_replace($tx,$rx,$query);}

else{$que=str_replace($rx,$tx,$query);}

//echo("<xmp>$que</xmp><br><BR>");

return $que;

}// end advTransform

function invAdvTransform($query){return advTransform($query,true);}



function advancedExamine($search){

$search=stripextraspaces($search);

//echo("||$search||<br>");
$i=1;
$len=strlen($search);

while($i<$len){

$pos=strpos($search, " ",$i);
/*
if($pos1===false){$pos1=10000000;}
$pos2=strpos($search, "(",$i);
if($pos2===false){$pos2=10000000;}
$pos3=strpos($search, ")",$i);
if($pos3===false){$pos3=10000000;}
*/
//$pos=$pos1;//min($pos1,$pos2,$pos3);

if ($pos>=($len-1)){break;}
$lo=substr($search,$pos-1,1);
$hi=substr($search,$pos+1,1);
$lo_type=advancedType($lo);
$hi_type=advancedType($hi);
//echo("| lo:$lo || hi:$hi || type:$lo_type -- $hi_type |<BR>");

if ((($lo_type=="let") and ($hi=="(")) or (($hi_type=="let") and ($lo==")")) or (($lo_type=="let") and ($hi_type=="let")) or (($lo_type=="let")and($hi_type=="not")) or (($lo==")")and($hi=="(")) or (($lo==")")and($hi_type=="not"))){

$find="$lo $hi";
$rep="$lo & $hi";
//echo("| $find || $rep || $lo || $hi || $lo_type -- $hi_type |<BR>");
$search=str_replace($find, $rep, $search);
$pos+=1;
if (($hi_type!="rpar")&&($hi_type!="lpar")&&($lo_type!="rpar")&&($lo_type!="lpar")){$pos+=2;}

}// end if

$i=$pos+1;
}// end while

return $search;
}// end advancedExamine

// proxand
// strict proximity search
// @ lookup - search limitation
// @ param - terms array of search terms
// @ param - range verse range
// @ return - verses

function proxand($lookup,$terms, $range){

global $default_search_bible_version;

if($lookup==""){$lookup="OT; NT";}

//echo("|range|$range");

//echo("Om here |$lookup|".$terms[0].$terms[1]."|$range|$default_search_bible_version<br>");

$res1=getSearch($lookup,$terms[0],$default_search_bible_version,array("Boolean Search","wholeword"));
$ref1=proxexplode($res1,$range);

//echo(count($res1)."||<br>");
//foreach($res1 as $ref){echo("|".$ref["text"]."<br>");}

$i=1;
while($term=$terms[$i]){

$ref1=proxcheck($term, $ref1);

$i++;
}// end while

if(count($ref1)==0){return null;}

$res=getSearch(implode("; ",$ref1),implode(" OR ",$terms),"",array("Boolean Search","wholeword"));
$res=repeat_remove($res);
return $res;

}// end proxand

function proxboolean($lookup, $query){

if($lookup==""){$lookup="OT; NT";}

$N=array();
$op=array();
$terms=array();

$i=0;
$len=strlen($query);

while($i<($len-5)){

$prox=strpos($query, "PROX(",$i);
if($prox===false){break;}

$rp=strpos($query,")",$prox);
//echo("i|$i|prox|$prox|rp|$rp|<br>");
$dif=$rp-$prox-5;

// find query errors
if(($dif==0)&&($dif>4)){return null;}

$nn=substr($query,$prox+5,$dif);

$N[]=$nn;
$op[]="PROX($nn)";

$i=$rp+1;
}// end while

$query=str_replace($op,"@",$query);
$query=stripextraspaces($query);
$terms=explode("@",$query);

$ct=count($terms);
$cn=count($N);

if ($cn!=1){
echo("Sorry. You can only have one PROX() operator per query.<br><BR>");
return null;
}

//foreach ($terms as $o){echo("$o<br>");}

if($ct!=($cn+1)){
echo("prime error ct|$ct|cn|$cn| query|$query|<br>");
return null;
}

/*
$n1=$N[0]+1;
$n=$N[0];
echo ("n $n n1 $n1");
*/

$old_terms=array($terms[0],$terms[1]);

$res=proxand($lookup,$old_terms,$N[0]);

/*
$i=1;
while($nn=$N[$i]){

$ref=proxexplode($res,$nn);
$res=proxand2($lookup,$terms[$i+1],$old_terms,$ref);

$old_terms[]=$terms[$i+1];
$i++;
}//while
*/

return $res;

}// end proxboolen

// proxand2
// strict proximity search (multiple terms)
// @ lookup - search limitation
// @ param - terms array of search terms
// @ param - range verse range
// @ param - old_terms terms that have already been searched for
// @ param - old_ref reference from previous searches
// @ return - verses
function proxand2($lookup,$term, $range, $old_terms, $old_ref){

global $default_search_bible_version;


$res1=getSearch($lookup,$term,$default_search_bible_version,array("Boolean Search","wholeword"));
$ref1=proxexplode($res1,$range);

$res=getSearch(implode("; ",$ref1),implode(" OR ",$old_terms),"",array("Boolean Search","wholeword"));
$res=repeat_remove($res);

return $res;

}// end proxand2

function proxexplode($res, $range){

$ref=array();

$i=0;
while($ver=$res[0][$i]){

$ref[]=getContext($ver["book"],$ver["chapter"],$ver["verse"],$range);

$i++;
}// end while 


return $ref;

}// proxexplode

function proxcheck($term,$ref){

global $default_search_bible_version;

//print_out($ref);

$ref2=array();
$i=0;
while($r=$ref[$i]){

$res=getSearch($r,$term,$default_search_bible_version,array("Boolean Search","wholeword"));
if(is_array($res[0][0])){$ref2[]=$r;}

$i++;
}// end while

//print_out($ref2);

return $ref2;

}// end proxchech

function repeat_remove($verses){
$verses2=array();
$used=array();

$cn=count($verses);

$i=0;
while($index=$verses[0][$i]["index"]){

if(array_search($index,$used)===false){
$verses2[]=$verses[0][$i];
$used[]=$index;
}// if

$i++;
}//while


return array($verses2);

}// proxremoverepeat

// par - paragraph
// op - AND, OR
// not - NOT
// let - letter
// sp - space
function advancedType($char){

if (($char=="(")){return "lpar";}
if (($char==")")){return "rpar";}
if (($char=="&") or ($char=="|") or ($char=="^")){return "op";}
if ($char=="-"){return "not";}
if ($char==" "){return "sp";}
return "let";
}// end advancedType

// returns the language (standard 2 character)
// for the given "version"


function getBible($ver){

//echo("ver $ver");

$res=mysql_query("select * from `bible_versions` where (`shortname` = '$ver') LIMIT 1");
//echo(mysql_error());
return mysql_fetch_array($res);

}//

function print_out($item){

echo("<pre>");
print_r($item);
echo("</pre>");

}

?>
</body>
</html>
Return current item: Website SuperSearch