utils.inc.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Static class utils
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../core/config.class.inc.php');
  25. define('ITOP_CONFIG_FILE', '../config-itop.php');
  26. class FileUploadException extends Exception
  27. {
  28. }
  29. class utils
  30. {
  31. private static $m_oConfig = null;
  32. public static function ReadParam($sName, $defaultValue = "")
  33. {
  34. return isset($_REQUEST[$sName]) ? $_REQUEST[$sName] : $defaultValue;
  35. }
  36. public static function ReadPostedParam($sName, $defaultValue = "")
  37. {
  38. return isset($_POST[$sName]) ? $_POST[$sName] : $defaultValue;
  39. }
  40. /**
  41. * Reads an uploaded file and turns it into an ormDocument object - Triggers an exception in case of error
  42. * @param string $sName Name of the input used from uploading the file
  43. * @return ormDocument The uploaded file (can be 'empty' if nothing was uploaded)
  44. */
  45. public static function ReadPostedDocument($sName)
  46. {
  47. $oDocument = new ormDocument(); // an empty document
  48. if(isset($_FILES[$sName]))
  49. {
  50. switch($_FILES[$sName]['error'])
  51. {
  52. case UPLOAD_ERR_OK:
  53. $doc_content = file_get_contents($_FILES[$sName]['tmp_name']);
  54. $sMimeType = $_FILES[$sName]['type'];
  55. if (function_exists('finfo_file'))
  56. {
  57. // as of PHP 5.3 the fileinfo extension is bundled within PHP
  58. // in which case we don't trust the mime type provided by the browser
  59. $rInfo = @finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
  60. if ($rInfo !== false)
  61. {
  62. $sType = @finfo_file($rInfo, $file);
  63. if ( ($sType !== false)
  64. && is_string($sType)
  65. && (strlen($sType)>0))
  66. {
  67. $sMimeType = $sType;
  68. }
  69. }
  70. @finfo_close($rInfo);
  71. }
  72. $oDocument = new ormDocument($doc_content, $sMimeType, $_FILES[$sName]['name']);
  73. break;
  74. case UPLOAD_ERR_NO_FILE:
  75. // no file to load, it's a normal case, just return an empty document
  76. break;
  77. case UPLOAD_ERR_FORM_SIZE:
  78. case UPLOAD_ERR_INI_SIZE:
  79. throw new FileUploadException(Dict::Format('UI:Error:UploadedFileTooBig'), ini_get('upload_max_filesize'));
  80. break;
  81. case UPLOAD_ERR_PARTIAL:
  82. throw new FileUploadException(Dict::S('UI:Error:UploadedFileTruncated.'));
  83. break;
  84. case UPLOAD_ERR_NO_TMP_DIR:
  85. throw new FileUploadException(Dict::S('UI:Error:NoTmpDir'));
  86. break;
  87. case UPLOAD_ERR_CANT_WRITE:
  88. throw new FileUploadException(Dict::Format('UI:Error:CannotWriteToTmp_Dir', ini_get('upload_tmp_dir')));
  89. break;
  90. case UPLOAD_ERR_EXTENSION:
  91. throw new FileUploadException(Dict::Format('UI:Error:UploadStoppedByExtension_FileName', $_FILES[$sName]['name']));
  92. break;
  93. default:
  94. throw new FileUploadException(Dict::Format('UI:Error:UploadFailedUnknownCause_Code', $_FILES[$sName]['error']));
  95. break;
  96. }
  97. }
  98. return $oDocument;
  99. }
  100. public static function GetNewTransactionId()
  101. {
  102. // TO DO implement the real mechanism here
  103. return sprintf("%08x", rand(0,2000000000));
  104. }
  105. public static function IsTransactionValid($sId)
  106. {
  107. // TO DO implement the real mechanism here
  108. return true;
  109. }
  110. public static function ReadFromFile($sFileName)
  111. {
  112. if (!file_exists($sFileName)) return false;
  113. return file_get_contents($sFileName);
  114. }
  115. /**
  116. * Get access to the application config file
  117. * @param none
  118. * @return Config The Config object initialized from the application config file
  119. */
  120. public static function GetConfig()
  121. {
  122. if (self::$m_oConfig == null)
  123. {
  124. self::$m_oConfig = new Config(ITOP_CONFIG_FILE);
  125. }
  126. return self::$m_oConfig;
  127. }
  128. /**
  129. * Helper function to convert a value expressed in a 'user friendly format'
  130. * as in php.ini, e.g. 256k, 2M, 1G etc. Into a number of bytes
  131. * @param mixed $value The value as read from php.ini
  132. * @return number
  133. */
  134. public static function ConvertToBytes( $value )
  135. {
  136. $iReturn = $value;
  137. if ( !is_numeric( $value ) )
  138. {
  139. $iLength = strlen( $value );
  140. $iReturn = substr( $value, 0, $iLength - 1 );
  141. $sUnit = strtoupper( substr( $value, $iLength - 1 ) );
  142. switch ( $sUnit )
  143. {
  144. case 'G':
  145. $iReturn *= 1024;
  146. case 'M':
  147. $iReturn *= 1024;
  148. case 'K':
  149. $iReturn *= 1024;
  150. }
  151. }
  152. return $iReturn;
  153. }
  154. /**
  155. * Returns an absolute URL to the current page
  156. * @param $bQueryString bool True to also get the query string, false otherwise
  157. * @return string The absolute URL to the current page
  158. */
  159. static public function GetAbsoluteUrl($bQueryString = true, $bForceHTTPS = false)
  160. {
  161. // Build an absolute URL to this page on this server/port
  162. $sServerName = $_SERVER['SERVER_NAME'];
  163. if ($bForceHTTPS)
  164. {
  165. $sProtocol = 'https';
  166. $sPort = '';
  167. }
  168. else
  169. {
  170. $sProtocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
  171. if ($sProtocol == 'http')
  172. {
  173. $sPort = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
  174. }
  175. else
  176. {
  177. $sPort = ($_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT'];
  178. }
  179. }
  180. // $_SERVER['REQUEST_URI'] is empty when running on IIS
  181. // Let's use Ivan Tcholakov's fix (found on www.dokeos.com)
  182. if (!empty($_SERVER['REQUEST_URI']))
  183. {
  184. $sPath = $_SERVER['REQUEST_URI'];
  185. }
  186. else
  187. {
  188. $sPath = $_SERVER['SCRIPT_NAME'];
  189. if (!empty($_SERVER['QUERY_STRING']))
  190. {
  191. $sPath .= '?'.$_SERVER['QUERY_STRING'];
  192. }
  193. $_SERVER['REQUEST_URI'] = $sPath;
  194. }
  195. $sPath = $_SERVER['REQUEST_URI'];
  196. if (!$bQueryString)
  197. {
  198. // remove all the parameters from the query string
  199. $iQuestionMarkPos = strpos($sPath, '?');
  200. if ($iQuestionMarkPos !== false)
  201. {
  202. $sPath = substr($sPath, 0, $iQuestionMarkPos);
  203. }
  204. }
  205. $sUrl = "$sProtocol://{$sServerName}{$sPort}{$sPath}";
  206. return $sUrl;
  207. }
  208. }
  209. ?>