Location: PHPKode > projects > Netautor Professional > netautor/napro4/scan/proc.php
<?php
/* +----------------------------------------------------------------------+
   | Netautor Professional Application Server                             |
   +----------------------------------------------------------------------+
   | Copyright (C) 1998-2005 digiconcept GmbH. <www.digiconcept.net>      |
   +----------------------------------------------------------------------+
   | This file is subject to license, that is bundled with this package   |
   | in the file LICENSE.TXT, and is available at through the             |
   | world-wide-web at http://www.netautor.com/license/                   |
   |                                                                      |
   | If you did not receive a copy of the license and are unable to       |
   | obtain it through the world-wide-web, please send a note to          |
   | hide@address.com so we can mail you a copy.                      |
   +----------------------------------------------------------------------+
   | Authors: Stefan Rottensteiner <hide@address.com>    |
   |          Marek  Kadziela      <hide@address.com>	      |
   |          Gregor Wollner                                              |
   |          Christian Unger                                             |
   |          Helli  Kleinhans  									      |
   +----------------------------------------------------------------------+*/

	/**
	 * @version $Revision: 1.26 $
	 */

	$GLOBALS['npf_export']='yes';

	/**
	 * This Script calls based on a config script some method
	 * and do some jobs like importing documents, converting images etc..
	 *
	 * @version $Revision: 1.26 $
	 * @author	Helmut Kleinhans
	 * @author  Stefan Rottensteiner
	 * @author  Gregor Wollner
	 *
	 */

error_reporting(0);
if(empty($GLOBALS['DC_ENV']))	{	include_once('../include/init.inc');	}
error_reporting(4);

//	---	include necessary functions
includeNaPro('array,file,string');

function make_error($text)
{
	global $config;
	if(!empty($config['basic']['error_mail']))
	{
		//mail einbauen
	}
	log_process('ERROR:'.$text);
}

/**
 * Wrapper-Funktion aus historischen Gründen
 */
function makelog_text($text,$Echo=true)
{
	log_process($text,$Echo);return;
}

/**
 * Logfile mitschreiben
 */
function log_process($Text,$Echo=true)
{
	global $config;
	$c	=	$config['basic'];

	$Text		=	$c['name'].'|'.(date('Y-m-d|H:i:s')).'| '.trim($Text)."<br>\n";;
	$logType	=	( empty($c['logtype'])	?	'' :  strtolower(trim($config['basic']['logtype']))	);

	@set_time_limit(30);
	switch($logType)
	{
		case 'file':
			$logPath 		= empty($c['logpath']) ? '.' : $c['logpath'];
			if (substr($logPath,-1) != '/') $logPath.='/';

			if (File_MkDirs(dirname($logPath)))
			{
				$logFileName 	= strtolower('logfile_'.$c['name'].'.'.(date('Ymd',time())).'.html');
				$File 			= fopen($logPath.$logFileName,'a');

				if ($File)
				{
					@fwrite($File,$Text,strlen($Text));
					@fclose($File);
				}
			}
		break;
		default:break;
	}
	if ($Echo)
	{
		echo($Text);
		flush();
	}
}

$configfile = (!empty($configfile)? $configfile.'.cfg' : $argv[1].'.cfg' );
$base 		= $GLOBALS['DC_ENV']->startpath.'/config/';

/**
* Search order for config-file :
*	1.	proc.php/$configfile			... very old location
* 	2.	$base.'/workflow/'.$configfile	... new and final location
*	3.	$base.$configfile				... old location
*/
if (file_exists($configfile))
{	// Suche relativ zu /proc.php
	include_once($configfile);
}
else
{
	if (file_exists($base.'/workflow/'.$configfile))
	{	// Suche relativ zu /config/workflow/
		include_once($base.'/workflow/'.$configfile);
	}
	else
	{
		if (file_exists($base.$configfile))
		{	// Suche relativ zu /config/
			include_once($base.$configfile);
		}
		else
		{
			makelog_text('No configfile <b>'.$configfile.'</b> found!');
			exit();
		}
	}
}

if(empty($config))
{
	makelog_text('Configfile <b>'.$configfile.'</b> does not contain required data!');
	exit();
}

$GLOBALS['config'] = $config;
/* end of output buffering */
if (function_exists('ob_end_flush')) {	@ob_end_flush();	}

$loops = $config['basic']['loops'];

if(empty($config['basic']['domax'])) $config['basic']['domax'] = $config['basic']['loops'];

//error_reporting(65535);
$windows = !empty($GLOBALS['HTTP_SERVER_VARS']['WINDIR']);

function check_dirs ($config)
{
	if (empty($config['method']['scandir']))
	{
		makelog_text('No dir to check ... </b>');
		return;
	}
	makelog_text('Check dir <b>'.$config['method']['scandir'].'</b>');

	$cdtree 		= get_tree($config['method']['scandir']);
	$count_cdtree 	= count($cdtree)-1;
	for ($i=$count_cdtree;$i>=0; $i--)
	{
		$cdtree[$i]['url']=chopleft($config['method']['scandir'],$cdtree[$i]['url']);
	}

	$DirsToCheck = array ();

	if (!empty($config['method']['error']))		$DirsToCheck[]=$config['method']['error'];
	if (!empty($config['method']['done']))		$DirsToCheck[]=$config['method']['done'];
	if (!empty($config['method']['target']))	$DirsToCheck[]=$config['method']['target'];
	if (!empty($config['method']['temp']))		$DirsToCheck[]=$config['method']['temp'];

	for ($i=0; $i<count($DirsToCheck); $i++)
	{
		for ($j=0; $j<count($cdtree); $j++)
		{
			$TheDir = $DirsToCheck[$i].$cdtree[$j]['url'];
			if (!empty($TheDir) && !is_dir ($TheDir) && !file_exists($TheDir) )
			{
				File_MkDirs ($TheDir);
				if(substr($TheDir,-1,1)=='/')	@mkdir(substr($TheDir,0,-1),0777);

				if (is_dir($TheDir))
				{
					makelog_text ('<b>'.$TheDir.'</b> successfully created.');
				}
				else
				{
					makelog_text ("Error creating <b>$TheDir</b>");
				}
			}
		}
	}
	unset ($cdtree);
	unset ($GLOBALS['content']);
}

function do_copys($filename)
{
	global $config;
	global $windows;

	if(!empty($config['method']['temp']))
	{
		$dest 	=$config['method']['temp'].$filename;
		$source	=$config['method']['scandir'].$filename;

		if (file_exists($dest)) unlink($dest);
		if( copy( $source, $dest ))
		{
			makelog_text(" Copy file from <b>$source</b> to <b>$dest</b>");
			$workfile = $dest;
		}
		else
		{
			makelog_text("Cannot copy file from <b>$source</b> to <b>$dest</b>");

			if($config['method']['error'])
			{
				$dest = $config['method']['error'].$filename;

				if (file_exists($dest)) unlink($dest);
				copy( $source, $dest);

		  		if($windows) $source=str_replace('/','\\',$source);
				if (file_exists($source)) unlink($source);
				makelog_text("Move file from <b>$source</b> to <b>$dest</b>");
			}
			else
			{
		  		if($windows)	$source=str_replace('/','\\',$source);
				if (file_exists($source)) unlink($source);
				makelog_text("Delete file <b>$source</b>");
			}
		}
	}
	else
	{
		makelog_text('No tempdir, work on original file !');
		$workfile = $config['method']['scandir'].$filename;
	}
	return $workfile;
}

function do_copys_done($filename,&$suc)
{
	global $config;
	global $windows;

	$source 	= $filename;
	$donefile	= false;
	// Wenn ORIGINAL = COPY, dann nicht gleich löschen, sondern aufheben und am Ende der Funktion löschen
	$delete_file = '';

	if(!empty($config['method']['temp']))
	{
		if(strstr($filename,$config['method']['temp']))
		{
			$filename = chopleft($config['method']['temp'],$filename);
		}
		else
		{
			$source = $config['method']['temp'].$filename;
		}
	}
	else
	{
		if(strstr($filename,$config['method']['scandir']))
		{
			$filename = chopleft($config['method']['scandir'],$filename);
		}
		else
		{
			$source = $config['method']['scandir'].$filename;
		}
	}

    $source_ori=$config['method']['scandir'].$filename;
	if($windows)
	{
		$source_ori	=str_replace('/','\\',$source_ori);
		$source		=str_replace('/','\\',$source);
	}
	if(!empty($config['method']['params']['original']) && strtolower($config['method']['params']['original'])=='copy')
	{
		$dest=$config['method']['target'].$filename;
  		if($windows) $dest=str_replace('/','\\',$dest);

		if (file_exists($source))
		{
			if (file_exists($dest)) unlink($dest);
			if( copy( $source, $dest ))
			{
				$donefile = $dest;
				$delete_file = $source;
				makelog_text("File moved from <b>$source</b> to <b>$dest</b>");
			}
			else
			{
				makelog_text("Cannot copy file from <b>$source</b> to <b>$dest</b>");

				if(!empty($config['method']['error']))
				{
					$dest = $config['method']['error'].$filename;
			  		if($windows) $dest=str_replace('/','\\',$dest);

					if (file_exists($dest)) unlink($dest);
					copy( $source, $dest);
					$delete_file = $source;
					makelog_text("File moved from <b>$source</b> to <b>$dest</b>");
				}
				else
				{
					if (file_exists($source)) unlink($source);
					makelog_text("File <b>$source</b> deleted");
				}
			}
		}
		else
		{
			makelog_text("File <b>$source</b> does not exist or is already moved");
		}
	}

	if(!empty($config['method']['done']))
	{
		$dest = (($suc==1)?$config['method']['done']:$config['method']['error']).$filename;
  		if($windows)	$dest=str_replace('/','\\',$dest);
		if (file_exists($dest)) unlink($dest);

		if( @copy( $source, $dest ))
		{
			makelog_text(" Copy file from <b>$source</b> to <b>$dest</b>");
			$donefile = $dest;
			unlink($source);
		}
		else
		{
			makelog_text("Cannot copy file from <b>$source</b> to <b>$dest</b>");

			if(!empty($config['method']['error']))
			{
				$dest = $config['method']['error'].$filename;
		  		if($windows)	$dest=str_replace('/','\\',$dest);
				if (file_exists($dest)) unlink($dest);
				@copy( $source, $dest);
				unlink($source);
				makelog_text("Move file from <b>$source</b> to <b>$dest</b>");
			}
			else
			{
		  		if($windows)	$source=str_replace('/','\\',$source);
				if (file_exists($source)) unlink($source);
				makelog_text("Delete file $source");
			}
		}
	}
	else
	{
		if($suc==1)
		{
			makelog_text('No donedir, deleting original file !');
			unlink($source);
		}
		else
		{
			if(!empty($config['method']['error']))
			{
				$dest = $config['method']['error'].$filename;
		  		if($windows)	$dest=str_replace('/','\\',$dest);

				if (file_exists($dest)) unlink($dest);
				copy( $source, $dest);
				unlink($source);
				makelog_text("Move file from <b>$source</b> to <b>$dest</b>");
			}
			else
			{
				if (file_exists($source)) unlink($source);
				makelog_text("Delete file <b>$source</b>");
			}
		}
	}

	if(!empty($source_ori))
	{
		if (file_exists($source_ori)) unlink($source_ori);
		makelog_text("Delete file <b>$source_ori</b>");
	}

	if (!empty($delete_file))	@unlink($delete_file);
	return $donefile;
}

check_dirs($config);

if(empty($config['method']['scandir']) || $config['method']['scandir']=='nofiles')
{
	$method = $GLOBALS['DC_ENV']->installpath.'scan/methods/'.$config['method']['methods'].'.mth';
	makelog_text('Jump into method <b>'.$config['method']['methods']."</b> ($method)");
	makelog_text('+++++++++++++++++++++++++');
	$success=0;
	error_reporting(255);
	include ($method);
	makelog_text('+++++++++++++++++++++++++');
	makelog_text("Returned from method <b>".$config['method']['methods']."</b> ($method)");

	if($success == 1)
	{
		//SUCCESS
		makelog_text('Done('.$success.') ');
	}
	else
	{
		//FAILED
		makelog_text('Failed('.$success.') ');
	}
}
else
{
	do
	{
		if(!empty($config['basic']['recur']) )
		{

			$tree = get_tree($config['method']['scandir']);
			for($i=0;$i<count($tree);$i++)
			{
				$ar_path=chopleft(strtolower($config['method']['scandir']),strtolower($tree[$i]['url']));

				$fil=Read_Dir($tree[$i]['url'],$config['method']['filematch']);

				if($fil)
				{
					for($j=0;$j<count($fil);$j++)
					{
						$fil_ar[]=$ar_path.$fil[$j];
					}
				}
			}
			@$files=$fil_ar;
			unset($fil_ar);
			unset($tree);
		}
		else
		{
			$files = array();
			$files = Read_Dir($config['method']['scandir'],$config['method']['filematch']);
		}

		if(!empty($files[0]))
		{
			$items= min(count($files),intval($config['basic']['domax']));
			makelog_text("<b>$items</b> file(s) found");
			for($i=0;$i<$items;$i++)
			{
				$workfile 		= do_copys($files[$i]);
				makelog_text('Working on '.($i+1).".th file <b>$workfile</b>");
				$workfile 		= do_copys($files[$i]);
				$current_file 	= $workfile;
				if($workfile)
				{
					$method = $GLOBALS['DC_ENV']->installpath.'scan/methods/'.$config['method']['methods'].'.mth';
					$success=0;
					if (file_exists($method))
					{
					makelog_text('Jump into method <b>'.$config['method']['methods']."</b> ($method)");
					makelog_text('+++++++++++++++++++++++++');
					include ($method);
					makelog_text('+++++++++++++++++++++++++');
					makelog_text("Returned from method <b>".$config['method']['methods']."</b> ($method)");
					}
					else
					{
						$success = 0;
						makelog_text('Cannot find method in file '.$method);break;
					}
				}

		  		if(!empty($newfiles) && is_array($newfiles))
		  		{
					makelog_text('Found '.(count($newfiles)).' new file(s).');
					reset($newfiles);
					while(List($Index,$NewFile)=each($newfiles))
					{
						if (!empty($config['method']['temp']))
		  				{
		  					$dest =$config['method']['target'].(chopleft($config['method']['temp'],$NewFile));
		  				}
		  				else
		  				{
		  					$dest =$NewFile;
		  				}

	  					if($windows)
						{
							$NewFile=str_replace('/','\\',$NewFile);
							$dest	=str_replace('/','\\',$dest);
						}
						if (file_exists($dest)) unlink($dest);
		  				if(copy( $NewFile, $dest))
		  				{
		  					@unlink($NewFile);
		  					makelog_text("File <b>$NewFile</b> moved to <b>$dest</b>");
		  				}
		  				else
		  				{
		  					makelog_text("Cannot move file <b>$NewFile</b> to <b>$dest</b>");
		  				}
		  			}
					unset($newfiles);
		  		}

			  	if($success == 1)
		  		{
		  			//SUCCESS
		  			do_copys_done($current_file,$success);
		  		}
		  		else
		  		{
		  			//FAILED
		  			do_copys_done($workfile,$success);
		  		}
		  		makelog_text('CopySysDone ('.($success==1?'true':'false').') ',true);
		  		$logtext='';

				if(!empty($config['method']['temp']) && $success==1)
				{
  					if($windows)	$workfile=str_replace('/','\\',$workfile);
  					if (file_exists($workfile)) unlink($workfile);
  					makelog_text("Delete File <b>$workfile</b>");
				}
			}
			$loops = ((strtoupper($config['basic']['loops'])=='E')?1:$loops-1);
		}
		else
		{
			$loops = ((strtoupper($config['basic']['loops'])=='E')?1:$loops-1);
		}

		$log= makelog_text( ( empty($loops)?'No more loops needed':"Still {$loops} times to go"),true);
	   if (!empty($config['basic']['sleep'])) sleep($config['basic']['sleep']);
	}
	while ($loops>0);
}
?>
Return current item: Netautor Professional