<?php

//****************************************************************************************
// FotoPlayer Search Engine v0.1
// Author : Dhinakaran Annamalai
// Protected under Creative Commons License. Attribution 2.0 United Kingdom
// Please refer readme.txt file for the usage details.
//****************************************************************************************

error_reporting(E_ALL ^ E_NOTICE);

$albumpath = "";$itemcounts = 999999;$refresh="";
global $albumpath,$itemcounts, $itemarray,$array_album,$tagsfile,$debug;$itemarray = array(); $array_album="";

if (isset($_REQUEST["refresh"])) {
	$refresh= $_REQUEST['refresh'];
}

if (isset($_REQUEST["debug"])) {
	$debug= $_REQUEST['debug'];
}


$tagsfile = "res/guestbook/tagslist.txt";

if (file_exists($tagsfile) && $refresh=="N") {
	read_text($tagsfile);
	exit();
}



//****************************************************************************************************************
function startElement($parser, $name, $attrs)
{
   global $depth, $albumpath, $itemarray;
   global $currentTag, $currentAttribs;
   $currentTag = $name;
   $currentAttribs = $attribs;

   if ($name == "IMAGE") {
  	    $sep_tags = explode(",", trim($attrs['TAGS']));
 // 	    if (count($sep_tags) > 1) {
			foreach($sep_tags as $value){
					if ($value != "") {
						array_push($itemarray,trim(utf8_encode($value)));
					}
			}
//		}
   };
   $depth[$parser]++;
}

//****************************************************************************************************************
function characterData($parser, $data)
     {
     }

//****************************************************************************************************************
function endElement($parser, $name)
{
   global $depth, $currentTag, $currentAttribs, $itemarray ;
   $depth[$parser]--;
}

//****************************************************************************************************************

function processalbum($file,$folpath) {
	global $depth,$itemcounts,$albumpath,$array_album,$itemarray,$debug;
	$albumpath = $folpath ;
	$depth = array();
	$fp = "";$data ="";
     if (isset($itemarray)) {
	 	     reset($itemarray);
	     unset($itemarray);
     }
    global $itemarray;
    $itemarray = array();

	$xml_parser = xml_parser_create();
	xml_set_element_handler($xml_parser, "startElement", "endElement");
	xml_set_character_data_handler($xml_parser, "characterData");
	if (!($fp = fopen($file, "r"))) {
	   die("could not open XML input");
	}

	while ($data = fread($fp, 4096)) {
	   	if (!xml_parse($xml_parser, $data, feof($fp))) {
	      	 die(sprintf("XML error: %s at line %d",
	             xml_error_string(xml_get_error_code($xml_parser)),
	             xml_get_current_line_number($xml_parser)));
   		}
	}

	$array_album .= implode(",", $itemarray);
	$array_album .= ",";

	if ($debug == "Y") {
		echo "<BR>filename: ".$folpath." <br> Value:".$array_album."<br><br><br>";
	};

	fclose($fp);
	xml_parser_free($xml_parser);

}  //processalbum function ends here

//****************************************************************************************************************

 function read_text($filename) {
	$lines = file($filename);
	foreach ($lines as $line_num => $line) {
	    echo $line."<br>";
	}
}

//****************************************************************************************************************
function display_results() {
	global $array_album,$tagsfile,$debug;
	$array_expl = explode(",",$array_album);


//without sorting and uniuque
	if ($debug == "Y" ) {
		echo "content=Before sorting and removing unique<br>";
		foreach($array_expl as $value){
			echo $value."<br>";
		}
	}

//after sorting and uniuque
	$array_final= array_unique($array_expl);
 	sort($array_final);

	//write to tagslist file
	$file = fopen($tagsfile, "w+") or die("Can't open text file for writing");
	$writetext = "\xEF\xBB\xBF". "content=";
	echo "content=";
	if ($debug == "Y" ) {
		echo "content=after sorting and removing duplicates<br>";
	}
	foreach($array_final as $value){
		if ($value != "") {
			$writetext = $writetext .utf8_decode($value).  "||";
			echo utf8_decode($value)."||";
		}
	}
	fwrite($file, $writetext);
}

//****************************************************************************************************************

if ($albumpath == "" ) {
	$xmlpath = "photos.xml";
	if (file_exists($xmlpath)) {
		processalbum($xmlpath,$albumpath);
	}
	map_dirs(".");
	display_results();
}
function map_dirs($path) {

       if(is_dir($path)) {
               if($contents = opendir($path)) {
                       while(($node = readdir($contents)) !== false) {
			       if( (!strpos($node,"."))  && $node!="res" && $node !="images" && $node != "slides" && $node !="thumbs" && $node !="rss_styles" && $node!= "." && $node!= ".." ) {

						if ($path==".") {
							$folpath = $node."/";
							if (file_exists($folpath."photos.xml")) {
								processalbum($folpath."photos.xml",$folpath);
							}
						}
						else {
							$folpath = $path."/".$node."/";
							$folpath = ltrim($folpath,'./');
							if (file_exists($folpath."photos.xml")) {
								processalbum($folpath."photos.xml",$folpath);
							}


						}

                                    map_dirs("$path/$node");
                         }
                       }
               }
       }
}


?>