<?php
	$autoStart = false;
	require_once('../../wiki.php');
	require_once('../wiki2.php');
	includeFile('search/all.php');
	//error_reporting(E_ALL);
	
	class emptyPage{
		function regLink($a,$b){}
	}
	$page = new emptyPage();
	
	class fileSearch extends query{
		var $path_thumb;
		var $thumbnails = false;

		
		
		//find images, then send javascript
		function fileSearch(){
			$this->setLang($_GET['l']);
			wbDB::connect();
			wbLang::getFile();
			
			$this->findFiles();
			
		}

	
		
		function setLang($lang){
			
			$test = str_replace('_','',$lang);
			if( !ctype_alpha($test) ){
				$GLOBALS['userLanguage'] = 'en';
				return;
			}
			
			$GLOBALS['userLanguage'] = $lang;
		}
			
		
		function escape($txt){
			static $search = array("\n","\r");
			static $repl = array('\n','\r');
			
			if( $txt === false){
				return;
			}
			$txt = addcslashes($txt,'"\\');
			return '"'.str_replace($search,$repl,$txt).'";';
		}
		
		function findFiles(){
			global $wbTablePrefix,$wbTables,$langA;
			
			if( empty($_GET['o']) ){
				echo $langA['INVALID_REQUEST'];
				return;
			}
			if( (strpos($_GET['o'],'/') !== false) || (strpos($_GET['o'],'\\') !== false) ){
				echo $langA['INVALID_REQUEST'];
				return;
			}
			
			$this->rowLimit = 10;
			$this->searchUrl = '/include/tool/FileSearch.php';
			$this->path_thumb = '/userfiles/'.toStorage($_GET['o'],true).'/thumbnails';
			if( $_GET['th'] == 'yes' ){
				$this->thumbnails = true;
			}
			
			
			$this->query = 'SELECT SQL_CALC_FOUND_ROWS `title` FROM ';
			$this->query .= ' `'.$wbTablePrefix.'attachments` ';
			$this->query .= ' JOIN '.$wbTables['all_files'].' USING(`file_id`) ';
			$this->query .= ' WHERE ';
			$this->query .= ' `owner` = "'.wbDB::escape($_GET['o']).'" ';
			$this->query .= ' AND `is_image` = 1 ';
			$this->query .= ' AND '.$wbTables['all_files'].'.`visible` = 1 ';

			if( !empty($_GET['s']) ){
				$this->query .= ' AND `title` LIKE "%'.wbDB::escape($_GET['s']).'%" ';
			}
			$this->query .= ' ORDER BY  `'.$wbTablePrefix.'attachments`.`file_id` DESC ';
			message($this->query);
			$this->browse('notneeded');
		}

		
		function nextLink($rowsFound){
			global $langA;
			if( ($_GET['offset']+$this->rowLimit) < $rowsFound){
				$queryString = '&s='.urlencode($_GET['s']).'&offset='.($_GET['offset'] + $this->rowLimit);
				return '<a href="javascript:WB.IS(\''.$queryString.'\');">'.$langA['next'].'</a>'; //using javascript in href here so that we don't get an onbeforeunload warning in explorer
			}
			return $langA['next'];
		}
		
		function prevLink(){
			global $langA;
			if( isset($_GET['offset']) && $_GET['offset'] > 0){
				$queryString = '&s='.urlencode($_GET['s']).'&offset='.($_GET['offset'] - $this->rowLimit);
				return '<a href="javascript:WB.IS(\''.$queryString.'\');">'.$langA['previous'].'</a>';
			}
			
			$_GET['offset'] = 0;
			return $langA['previous'];
		}
		
		function setLinks(&$currentPageNum,&$rowsFound,&$tabLabel){}
		
		function abbrevOutput($row){
			global $rootDir;
			
			
			if( $this->thumbnails ){
				
				echo '<div style="margin:0 2em 1em 0;width:60px;overflow:hidden;float:left;position:relative;text-align:center" >';
				echo '<a href="javascript:void(0)" onclick="insertWiki(\''.$_GET['t'].'\', \'\', \'\', \'\n[[Image:'.$row['title'].']]\');return false;">';
				$thumb = $this->path_thumb.'/'.$row['title'].'.jpg';
				if( !file_exists($rootDir.$thumb) ){
					$thumb = '/imgs/unavailable.png';
				}
				echo '<img src="'.wbLinks::getDir($thumb).'" alt="'.$row['title'].'" height="50" width="50" />';
				echo $row['title'];
				echo '</a>';
				echo '</div>';
				
			}else{
				
				echo '<div style="margin:0 2em 1.5em 0;width:12em;overflow:hidden;float:left;position:relative;text-align:center" >';
				echo '<a href="javascript:void(0)" onclick="insertWiki(\''.$_GET['t'].'\', \'\', \'\', \'\n[[Image:'.$row['title'].']]\');return false;">';
				echo $row['title'];
				echo '</a>';
				echo '</div>';
			}
		}
		
		function displayPre(){
			echo '<div style="overflow:hidden;position:relative;">';
		}
		function displayPost(&$prev,&$pages,&$next,$listItems=null,$more=null){
			echo '<div style="clear:both"></div>';
			echo '</div>';
		}

		function mysqlFetch(&$result){
			return mysql_fetch_assoc($result);
		}	
		
		function closeBuffer(){
			$mess = wb::get_clean();
			echo 'var mess = '.$this->escape($mess);
			echo 'WB.IR(mess);';
		}
		
	}
	
	new fileSearch();
