PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/phpreports/PHPReportsUtil.php

http://simpleinvoices.googlecode.com/
PHP | 402 lines | 254 code | 28 blank | 120 comment | 21 complexity | 7f07b9267c43fcb2f3ed73250d67bc1b MD5 | raw file
  1<?php
  2	/******************************************************************************
  3	*																										*
  4	*	Useful functions and classes to deal with PHPReports stuff.						*
  5	*	This file is part of the standard PHPReports package.								*
  6	*																										*
  7	******************************************************************************/	
  8
  9	/******************************************************************************
 10	*																										*
 11	*	This function will return if there is a PHPReports path in the PHP			*
 12	*	ini_get("include_path").																	*
 13	*																										*
 14	******************************************************************************/
 15	function getPHPReportsIncludePath(){
 16		$sep = stristr(PHP_OS,'WIN')?';':':';
 17		$sep = stristr(PHP_OS,'DARWIN')?':':$sep;
 18		$aPaths = explode($sep,ini_get("include_path"));
 19		foreach($aPaths as $sPath)
 20			if(stristr($sPath,"phpreports"))
 21				return $sPath;
 22		return null;
 23	}
 24
 25	/******************************************************************************
 26	*																										*
 27	*	Returns the temporary file path. It's up to your operational system to		*
 28	*	return that. In most cases, on Linux it will return /tmp and on				*
 29	*	Windows c:\temp																				*
 30	*																										*
 31	******************************************************************************/
 32	function getPHPReportsTmpPath(){
 33		$sPath = tempnam(null,"check");
 34		unlink($sPath);
 35		return realpath(dirname($sPath));
 36	}
 37
 38	/******************************************************************************
 39	*																										*
 40	*	This function will return the file path where the PHPReports classes			*
 41	*	are.																								* 
 42	*																										*
 43	******************************************************************************/
 44	function getPHPReportsFilePath(){
 45		/*$sPath = getPHPReportsIncludePath();
 46		if(!is_null($sPath))
 47			return $sPath;
 48		// put your distro path here
 49		return $_SERVER["DOCUMENT_ROOT"]."/library/phpreports/"; 
 50        */
 51        /* SimpleInvoices.org mod*/
 52        return dirname(__FILE__)."/"; 
 53	}
 54
 55	/******************************************************************************
 56	*																										*
 57	*	XSLTProcessorClass																			*
 58	*	This class is used as base for XSLT process.											*
 59	*																										*
 60	******************************************************************************/	
 61	class XSLTProcessorClass{
 62		var $_sXML;
 63		var $_sXSLT;
 64		var $_sOutput;
 65		var $_aParms;
 66
 67		/**
 68			Constructor
 69		*/
 70		function XSLTProcessorClass(){
 71			$this->_sXML	=null;
 72			$this->_sXSLT	=null;
 73			$this->_sOutput=null;
 74			$this->_aParms	=null;
 75		}
 76
 77		/**
 78			Sets the XML data file path
 79		*/			
 80		function setXML($sXML_=null){
 81			$this->_sXML=$sXML_;
 82		}
 83		
 84		/**
 85			Returns the XML data file path
 86		*/
 87		function getXML(){
 88			return $this->_sXML;
 89		}
 90		
 91		/**
 92			Sets the style sheet file path
 93		*/
 94		function setXSLT($sXSLT_=null){
 95			$this->_sXSLT=$sXSLT_;
 96		}
 97
 98		/**
 99			Returns the style sheet file path
100		*/
101		function getXSLT(){
102			return $this->_sXSLT;
103		}
104		
105		/**
106			Specify the output file path
107			A null just returns the result on the run method
108		*/
109		function setOutput($sOutput_=null){
110			$this->_sOutput=$sOutput_;
111		}
112
113		/**
114			Return the output file path
115		*/		
116		function getOutput(){
117			return $this->_sOutput;
118		}
119
120		/**
121			Specify the parameters array
122		*/
123		function setParms($aParms_=null){
124			if(is_null($aParms_))
125				return;
126			if(!is_array($aParms_))
127				return;	
128			$this->_aParms=$aParms_;	
129		}
130		
131		/**
132			Insert a parameter
133			sParm_ - parameter name
134			oVal_  - parameter value
135		*/			
136		function setParm($sParm_=null,$oVal_=null){
137			if(is_null($sParm_))
138				return;
139			$this->_aParms[$sParm_]=$oVal_;
140		}
141
142		/**
143			Returns a parameter value
144			sParm_ - parameter name
145		*/
146		function getParm($sParm_){
147			if(!array_key_exists($sParm_))
148				return null;
149			return $this->_aParms[$sParm_];
150		}
151		
152		/**
153			Remove a parameter
154			sParm_ - parameter name
155		*/
156		function removeParm($sParm_=null){
157			if(is_null($sParm_))
158				return;
159			if(!array_key_exists($sParm_,$this->_aParms))
160				return;					
161			unset($this->_aParms[$sParm_]);					
162		}
163
164		/**
165			This method MUST be overwritten on every subclass to reflect
166			the behaviour of the desired XSLT processor.
167			It MUST return the result, and if defined an output, save it.
168		*/
169		function run(){
170		}
171	}
172	
173	/******************************************************************************
174	*																										*
175	*	Sablotron processor																			*
176	*	http://www.gingerall.com/charlie/ga/xml/p_sab.xml									*
177	*	http://www.php.net/manual/en/ref.xslt.php												*
178	*	Used on PHP4 or installed from the PECL modules.									*
179	*																										*
180	******************************************************************************/
181	class Sablotron_xp extends XSLTProcessorClass{
182		function run(){
183			if(is_null($this->_sXML)){
184				print "ERROR: no XML file specified";
185				return;
186			}
187			if(is_null($this->_sXSLT)){
188				print "ERROR: no XSLT file specified";
189				return;
190			}
191			$oXSLT = xslt_create();
192			$sRst	 = xslt_process($oXSLT,$this->_sXML,$this->_sXSLT,$this->_sOutput,null,$this->_aParms);
193			xslt_free($oXSLT);					
194			return $sRst;
195		}
196	}
197
198	/******************************************************************************
199	*																										*
200	*	PHP5 XSL processing																			*
201	*	Uses libxslt																					*
202	*	http://www.php.net/manual/en/ref.xsl.php												*
203	*																										*
204	******************************************************************************/
205	class PHPXSL_xp extends XSLTProcessorClass{
206		function run(){
207			// xml document
208			$oXML = new DomDocument();
209			$oXML->load($this->_sXML);
210			$oXML->xinclude();
211
212			// xslt document
213			$oXSL = new DomDocument();
214			$oXSL->load($this->_sXSLT);
215
216			// xslt processor
217			$oProc = new XSLTProcessor();
218			$oProc->importStyleSheet($oXSL);
219			
220			// set all the parameters
221			if(!is_null($this->_aParms)){
222				foreach($this->_aParms as $k => $v)
223					$oProc->setParameter("",$k,$v);
224			}	
225
226			// make the transformation				
227			$sRst = $oProc->transformToXML($oXML);
228			unset($oProc);
229			unset($oXSL);
230			unset($oXML);
231
232			// if output is not null, save the result there
233			if(!is_null($this->_sOutput)){
234				$fHand = @fopen($this->_sOutput,"w");
235				@fputs($fHand,$sRst);
236				@fclose($fHand);
237			}
238			return $sRst;
239		}
240	}
241	
242	/******************************************************************************
243	*																										*
244	*	XSLT Processor factory																		*
245	*	Returns a XSLT processor based on the current environment						*
246	*	or the user choice (need to hack the code below).									*
247	*																										*
248	******************************************************************************/
249	class XSLTProcessorFactory{
250		function get(){
251			// PHP major version number
252			$iVer = intval(substr(phpversion(),0,1));
253
254			// if PHP4 and Sablotron is installed
255			if($iVer<=4 && function_exists("xslt_create"))
256				return new Sablotron_xp();
257			// if PHP5 and Sablotron is installed				
258			else if($iVer>=5 && function_exists("xslt_create"))
259				return new Sablotron_xp();	
260			// if PHP5, Sablotron is not installed	and XSL support is compiled			
261			else if($iVer>=5 && !function_exists("xslt_create") && class_exists("XSLTProcessor"))
262				return new PHPXSL_xp();
263			// there is no XSLT processor installed!				
264			else
265				return null;				
266		}
267	}
268
269	/******************************************************************************
270	*																										*
271	*	PHPReportsError																				*
272	*	Process error messages																		*
273	*																										*
274	******************************************************************************/
275	class PHPReportsError{
276		function PHPReportsError($sMsg_=null,$sURL_=null){
277			if(is_null($sMsg_))
278				return;
279				
280			print "<p style='width:400px;background-color:#F5F5F5;border-style:solid;border-width:2;border-color:#CCCCCC;padding:10px 10px 10px 10px;margin:20px;font-family:verdana,arial,helvetica,sans-serif;color:#505050;font-size:12px;'>";
281			print "<span style='font-size:18px;color:#FF0000;font-weight:bold;'>OOOOPS, THERE'S AN ERROR HERE.</span><br/><br/>";
282			print $sMsg_."<br/><br/>";
283			
284			if(!is_null($sURL_))
285				print "<a href='$sPath/help/$sURL_'>More about this error here.</a><br/><br/>";
286
287			print "<span style='font-size:10px;font-weight:bold;'>This error message was generated by PHPReports</span>";
288			print "</p>";
289			exit();				
290		}
291	}
292
293	class PHPReportsErrorTr {
294		var $_aMsgs;
295		
296		function PHPReportsErrorTr(){
297			$this->_aMsgs = Array();
298
299			// default English messages
300			$this->_aMsgs["OPS"]["default"]				= "OOOOPS, THERE'S AN ERROR HERE.";
301			$this->_aMsgs["ERROR"]["default"]			= "This error message was generated by phpReports.";
302			$this->_aMsgs["NODATA"]["default"]			= "No data was found.";
303			$this->_aMsgs["NOPAGE"]["default"]			= "No PAGE element was found on your XML file.";
304			$this->_aMsgs["NOIF"]["default"]				= "No database interface '%s' available.";
305			$this->_aMsgs["REFUSEDCON"]["default"]		= "Connection refused.";
306			$this->_aMsgs["QUERYERROR"]["default"]		= "There's an error on your SQL query.";
307			$this->_aMsgs["NOCOLUMNS"]["default"]		= "No columns returned from your query.";
308			$this->_aMsgs["PAGEPARSER"]["default"]		= "Could not copy the temporary page parser to the temporary directory.";
309			$this->_aMsgs["DYNLINK"]["default"]			= "Specified a dynamic link but no COLUMN element found";
310			$this->_aMsgs["EXPLINK"]["default"]			= "Specified an expression link but no COLUMN element found";
311			$this->_aMsgs["NOFIELD"]["default"]			= "You're trying to retrieve the <b>VALUE</b> of a field called <b>%s</b>, but it is not on your SQL query. Please check your query.";
312			$this->_aMsgs["NOFIELDSUM"]["default"]		= "You're trying to retrieve the <b>SUM</b> of a field called <b>%s</b>, but it is not on your SQL query. Please check your query.";
313			$this->_aMsgs["NOFIELDMAX"]["default"]		= "You're trying to retrieve the <b>MAX VALUE</b> of a field called <b>%s</b>, but it is not on your SQL query. Please check your query.";
314			$this->_aMsgs["NOFIELDMIN"]["default"]		= "You're trying to retrieve the <b>MIN VALUE</b> of a field called <b>%s</b>, but it is not on your SQL query. Please check your query.";
315			$this->_aMsgs["NOFIELDAVG"]["default"]		= "You're trying to retrieve the <b>AVERAGE</b> of a field called <b>%s</b>, but it is not on your SQL query. Please check your query.";
316			$this->_aMsgs["CANTWRITEPAGE"]["default"]	= "Can't write file <b>%s</b> to the disk. Check your disk quota/space and rights.";
317			$this->_aMsgs["DYNBOOK"]["default"]			= "Specified a dynamic bookmark but no COLUMN element found";
318			$this->_aMsgs["EXPBOOK"]["default"]			= "Specified an expression bookmark but no COLUMN element found";
319			$this->_aMsgs["NOXMLTRANS"]["default"]		= "COL parameter <b>%s</b> not found on XML translation.";
320			$this->_aMsgs["NOXSLT"]["default"]			= "There is no XSLT processor available. Check if you compiled PHP with <b>--enable-xslt</b> and the <a href=\"http://www.gingerall.com/charlie/ga/xml/p_sab.xml\">Sablotron</a> library (for <a href=\"http://www.php.net/manual/en/ref.xslt.php\">PHP4</a>) or with <b>--enable-xsl</b> (for <a href=\"http://www.php.net/manual/en/ref.xsl.php\">PHP5</a>).";
321			$this->_aMsgs["NOPATH"]["default"]			= "Seems that you didn't specified the phpReports path on the PHP <b>include_path</b> statement or <b>php.ini</b>. I don't know there the classes are.";
322			$this->_aMsgs["NOCODE"]["default"]			= "Could not create the output code to run your report. Please check if the webserver user have rights to write in your <b>%s</b> directory.";
323			$this->_aMsgs["NOXML"]["default"]			= "Could not find the XML file with your data (<b>%s</b>) to run your report. Please check the filename and if the webserver user have rights to write in your temporary directory.";
324			$this->_aMsgs["NOXMLSET"]["default"]		= "The XML input file <b>%s</b> was not found.";
325			$this->_aMsgs["NOXSLTSET"]["default"]		= "The XSLT input file <b>%s</b> was not found.";
326			$this->_aMsgs["NOPLUGIN"]["default"]		= "There is no <b>%s</b> output plugin (<b>%s</b>).";
327			$this->_aMsgs["NOLOAD"]["default"]			= "Could not find file <b>%s</b> for report loading.";
328			$this->_aMsgs["NOTEMPLATE"]["default"]		= "The template file <b>%s</b> was not found.";
329			$this->_aMsgs["INVALIDCON"]["default"]		= "Your database connection handle is not valid.";
330
331			// Brazilian Portuguese messages
332			$this->_aMsgs["OPS"]["pt_BR"]					= "OOOOPS, OCORREU UM ERRO AQUI.";
333			$this->_aMsgs["ERROR"]["pt_BR"]				= "Essa mensagem de erro foi gerada pelo phpReports.";
334			$this->_aMsgs["NODATA"]["pt_BR"]				= "N?o foram encontrados dados.";
335			$this->_aMsgs["NOPAGE"]["pt_BR"]				= "N?o h? um elemento PAGE (p?gina) no seu relat?rio.";
336			$this->_aMsgs["NOIF"]["pt_BR"]				= "N?o h? dispon?vel a interface '%s' para banco de dados.";
337			$this->_aMsgs["REFUSEDCON"]["pt_BR"]		= "Conex?o recusada.";
338			$this->_aMsgs["QUERYERROR"]["pt_BR"]		= "Erro na consulta SQL.";
339			$this->_aMsgs["NOCOLUMNS"]["pt_BR"]			= "N?o foram retornados colunas de dados na sua consulta.";
340			$this->_aMsgs["PAGEPARSER"]["pt_BR"]		= "N?o consegui copiar o conversor de p?ginas para o diret?rio tempor?rio.";
341			$this->_aMsgs["DYNLINK"]["pt_BR"]			= "Foi especificado um link din?mico mas n?o existe um elemento COLUMN.";
342			$this->_aMsgs["EXPLINK"]["pt_BR"]			= "Foi especificado um link com uma express?o mas n?o existe um elemento COLUMN.";
343			$this->_aMsgs["NOFIELD"]["pt_BR"]			= "Voc? est? tentando recuperar o <b>VALOR</b> de um campo chamado <b>%s</b>, mas ele n?o existe na sua consulta. Por favor revise sua consulta.";
344			$this->_aMsgs["NOFIELDSUM"]["pt_BR"]		= "Voc? est? tentando recuperar a <b>SOMA</b> de um campo chamado <b>%s</b>, mas ele n?o existe na sua consulta. Por favor revise sua consulta.";
345			$this->_aMsgs["NOFIELDMAX"]["pt_BR"]		= "Voc? est? tentando recuperar o <b>VALOR M?XIMO</b> de um campo chamado <b>%s</b>, mas ele n?o existe na sua consulta. Por favor revise sua consulta.";
346			$this->_aMsgs["NOFIELDMIN"]["pt_BR"]		= "Voc? est? tentando recuperar o <b>VALOR M?NIMO</b> de um campo chamado <b>%s</b>, mas ele n?o existe na sua consulta. Por favor revise sua consulta.";
347			$this->_aMsgs["NOFIELDAVG"]["pt_BR"]		= "Voc? est? tentando recuperar o <b>VALOR M?DIO</b> de um campo chamado <b>%s</b>, mas ele n?o existe na sua consulta. Por favor revise sua consulta.";
348			$this->_aMsgs["CANTWRITEPAGE"]["pt_BR"]	= "N?o consegui escrever o arquivo <b>%s</b> no disco. Verifique suas permiss?es e espa?o em disco.";
349			$this->_aMsgs["DYNBOOK"]["pt_BR"]			= "Foi especificado um bookmark din?mico mas n?o existe um elemento COLUMN.";
350			$this->_aMsgs["EXPBOOK"]["pt_BR"]			= "Foi especificado um bookmark com uma express?o mas n?o existe um elemento COLUMN.";
351			$this->_aMsgs["NOXMLTRANS"]["pt_BR"]		= "O par?metro <b>%s</b> de COL n?o foi encontrado na tradu??o para XML.";
352			$this->_aMsgs["NOXSLT"]["pt_BR"]				= "N?o h? um processador XSLT dispon?vel. Verifique se voc? compilou o PHP com <b>--enable-xslt</b> e a library <a href=\"http://www.gingerall.com/charlie/ga/xml/p_sab.xml\">Sablotron</a> (para o <a href=\"http://www.php.net/manual/en/ref.xslt.php\">PHP4</a>) ou com <b>--enable-xsl</b> (para o <a href=\"http://www.php.net/manual/en/ref.xsl.php\">PHP5</a>).";
353			$this->_aMsgs["NOPATH"]["pt_BR"]				= "Parece que voc? n?o especificou o path do phpReports com o comando <b>include_path</b> ou no <b>php.ini</b>. N?o sei onde as classes est?o.";
354			$this->_aMsgs["NOCODE"]["pt_BR"]				= "N?o pude criar o c?digo de sa?da para rodar seu relat?rio. Por favor verifique se o usu?rio do servidor web tem direitos para escrever no diret?rio <b>%s</b>.";
355			$this->_aMsgs["NOXML"]["pt_BR"]				= "N?o pude encontrar o arquivo XML com seus dados (<b>%s</b>) para rodar seu relat?rio. Por favor verifique o nome do arquivo e se o usu?rio do servidor web tem direitos de escrita no seu diret?rio de arquivos tempor?rios.";
356			$this->_aMsgs["NOXMLSET"]["pt_BR"]			= "O arquivo XML de entrada <b>%s</b> n?o foi encontrado.";
357			$this->_aMsgs["NOXSLTSET"]["pt_BR"]			= "O arquivo XSLT de entrada <b>%s</b> n?o foi encontrado.";
358			$this->_aMsgs["NOPLUGIN"]["pt_BR"]			= "O plugin de sa?da <b>%s</b> n?o existe (<b>%s</b>).";
359			$this->_aMsgs["NOLOAD"]["pt_BR"]				= "N?o encontrei o arquivo <b>%s</b> para carregar o relat?rio.";
360			$this->_aMsgs["NOTEMPLATE"]["pt_BR"]		= "O arquivo de template <b>%s</b> n?o foi encontrado.";
361			$this->_aMsgs["INVALIDCON"]["pt_BR"]		= "A vari?vel da conex?o com o banco de dados n?o ? v?lida. %s";
362		}
363
364		function showMsg($sMsg_=null,$oParms_=null){
365			if(!sMsg_)
366				return;
367			if($_SESSION["phpReportsLanguage"])
368				$sLang = $_SESSION["phpReportsLanguage"];
369			else
370				$sLang = $GLOBALS["phpReportsLanguage"];
371			if(!$sLang)
372				$sLang = "default";
373
374			$sTitle	= $this->_aMsgs["OPS"][$sLang];
375			$sError	= $this->_aMsgs["ERROR"][$sLang];
376			$sMsg		= $this->_aMsgs[$sMsg_][$sLang];
377
378			// if the message have no translation
379			if(!$sMsg)
380				$sMsg = $this->_aMsgs[$sMsg_]["default"];
381			// if the message is still null ...
382			if(!$sMsg)
383				$sMsg = "$sMsg_?";
384			
385			if($oParms_)
386				$sMsg = vsprintf($sMsg,$oParms_);
387			
388			print "<p style='width:400px;background-color:#F5F5F5;border-style:solid;border-width:2;border-color:#CCCCCC;padding:10px 10px 10px 10px;margin:20px;font-family:verdana,arial,helvetica,sans-serif;color:#505050;font-size:12px;'>";
389			print "<span style='font-size:18px;color:#FF0000;font-weight:bold;'>$sTitle</span><br/><br/>";
390			print "$sMsg<br/><br/>";
391			print "<span style='font-size:10px;font-weight:bold;'>$sError</span>";
392			print "</p>";
393			exit();				
394		}
395	}
396
397	function isNumericType($sType=null){
398		$sStr = "NUMBER,NUMERIC,INT,DOUBLE,DECIMAL,REAL,TINY,SHORT,LONG,FLOAT,LONGLONG,INT24,YEAR,CID,FLOAT4,FLOAT8,INT2,".
399		"INT4,MONEY,OID,RELTIME,XID,DOUBLE PRECISION,SMALLINT,TINYINT,BIGINT,INT64,INT8,DATE,DATETIME";
400		return stristr($sStr,$sType);
401	}
402?>