PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPReportsUtil.php

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