<?php
/*
Officity - Web application platform - Version 6.0 - 2011-07-05

François Dispaux, Boris Verdeyen, Thomas Hermant,
Jérémie Roy, Grégory Meurice, Abdelila Harbi, 
Marc Mignonsin, Jonathan Sanchez, Julien Gonzalez, Pierre Fouchez

Sushee and Officity is © Copyright 2011 Nectil SA.

`/sushee-source/sushee/private/checkNectilFiles.php` is part of Officity.

Sushee 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 3 of the License, or
(at your option) any later version.

Officity and Sushee are distributed in the hope that they 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 Sushee. If not, see <http://www.gnu.org/licenses/>.
*/
include_once("../common/common_functions.inc.php");

$base_array = array("ID","LanguageID","CreatorID","ModifierID","URL","Header","Title",'Custom',"Body","Summary","Signature","Biblio","Copyright");

$db_conn = db_connect();

$sql = 'SELECT * FROM descriptions_custom;';
$rs = $db_conn->Execute($sql);
global $directoryRoot;
$missing = array();
$ok = array();
$index_missing = 0;
$index_ok = 0;
while($row = $rs->FetchRow()){
	$filepath = $row['Value'];
	if (substr($filepath,0,1)=="/"){ // it's a file
		
		if (file_exists($directoryRoot.$filepath)){
			//echo $filepath." OK<br/>";
			$ok[$filepath]=array("descriptionid"=>$row['DescriptionID'],"field"=>'CUSTOM');
			$index_ok++;
		}else{
			//echo "<b>".$filepath." MISSING</b><br/>";
			$missing[$filepath]=array("descriptionid"=>$row['DescriptionID'],"title"=>$row["Title"],"height"=>$height,"width"=>$width,"field"=>'CUSTOM');
			$index_missing++;
		}
	}
}
$sql = 'SELECT * FROM descriptions;';
$rs = $db_conn->Execute($sql);
while($row = $rs->FetchRow()){
	// other fields
	foreach($base_array as $row_name){
		//echo "$row_name<br/>";
		$value = $row[$row_name];
		//echo $value."<br/>";
		$file_url_pos = strpos($value,'"[files_url]');
		while($file_url_pos!==false){
			$file_path_end = strpos($value,'"',$file_url_pos+12);
			if($file_path_end){
				$file = substr($value,$file_url_pos+12,$file_path_end-$file_url_pos-12);
				//$file = transformPath($file);
				if (file_exists("$directoryRoot$file")){
					//echo $file." OK<br/>";
					$ok[$filepath]=array("descriptionid"=>$row['ID'],"field"=>'CUSTOM');
					$index_ok++;
				}else{
					//echo "<b>".$file." MISSING</b><br/>";
					$missing[$file]=array("descriptionid"=>$row['ID'],"title"=>$row["Title"],"field"=>$row_name);
					$index_missing++;
				}
			}
			$file_url_pos = strpos($value,'"[files_url]',$file_url_pos+12);
		}
	}
	/*if($index > 5)
		break;*/
}
foreach($missing as $key=>$value){
	//$simple_filename = BaseFilename($key);
	if($key!='undefined'){
		echo '<b>'.$key.'</b>';
		if($_GET['more']=='true')
			echo " in field <em>".$value['field']."</em> \"-".$value['descriptionid']." ";
		echo "<br/>";
	}
}
echo "<br/>";
echo sizeof($missing)." MISSING<br/>";
echo sizeof($ok)." OK<br/>";

?>
