utils.inc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. require_once('../core/config.class.inc.php');
  3. define('ITOP_CONFIG_FILE', '../config-itop.php');
  4. class FileUploadException extends Exception
  5. {
  6. }
  7. class utils
  8. {
  9. private static $m_oConfig = null;
  10. public static function ReadParam($sName, $defaultValue = "")
  11. {
  12. return isset($_REQUEST[$sName]) ? $_REQUEST[$sName] : $defaultValue;
  13. }
  14. public static function ReadPostedParam($sName, $defaultValue = "")
  15. {
  16. return isset($_POST[$sName]) ? $_POST[$sName] : $defaultValue;
  17. }
  18. /**
  19. * Reads an uploaded file and turns it into an ormDocument object - Triggers an exception in case of error
  20. * @param string $sName Name of the input used from uploading the file
  21. * @return ormDocument The uploaded file (can be 'empty' if nothing was uploaded)
  22. */
  23. public static function ReadPostedDocument($sName)
  24. {
  25. $oDocument = new ormDocument(); // an empty document
  26. if(isset($_FILES[$sName]))
  27. {
  28. switch($_FILES[$sName]['error'])
  29. {
  30. case UPLOAD_ERR_OK:
  31. $doc_content = file_get_contents($_FILES[$sName]['tmp_name']);
  32. $sMimeType = $_FILES[$sName]['type'];
  33. if (function_exists('finfo_file'))
  34. {
  35. // as of PHP 5.3 the fileinfo extension is bundled within PHP
  36. // in which case we don't trust the mime type provided by the browser
  37. $rInfo = @finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
  38. if ($rInfo !== false)
  39. {
  40. $sType = @finfo_file($rInfo, $file);
  41. if ( ($sType !== false)
  42. && is_string($sType)
  43. && (strlen($sType)>0))
  44. {
  45. $sMimeType = $sType;
  46. }
  47. }
  48. @finfo_close($finfo);
  49. }
  50. $oDocument = new ormDocument($doc_content, $sMimeType, $_FILES[$sName]['name']);
  51. break;
  52. case UPLOAD_ERR_NO_FILE:
  53. // no file to load, it's a normal case, just return an empty document
  54. break;
  55. case UPLOAD_ERR_FORM_SIZE:
  56. case UPLOAD_ERR_INI_SIZE:
  57. throw new FileUploadException("Uploaded file is too big. (Max allowed size is ".ini_get('upload_max_filesize').". Check you PHP configuration for upload_max_filesize.");
  58. break;
  59. case UPLOAD_ERR_PARTIAL:
  60. throw new FileUploadException("File upload failed, file has been truncated.");
  61. break;
  62. case UPLOAD_ERR_NO_TMP_DIR:
  63. throw new FileUploadException("Missing a temporary folder.");
  64. break;
  65. case UPLOAD_ERR_CANT_WRITE:
  66. throw new FileUploadException("Unable to write the temporary file to the disk (upload_tmp_dir = ".ini_get('upload_tmp_dir').")");
  67. break;
  68. case UPLOAD_ERR_EXTENSION:
  69. throw new FileUploadException("File upload stopped by extension. (Original file name: ".$_FILES[$sName]['name'].")");
  70. break;
  71. default:
  72. throw new FileUploadException("File upload failed, unknown cause (Error code = ".$_FILES[$sName]['error'].")");
  73. break;
  74. }
  75. }
  76. return $oDocument;
  77. }
  78. public static function GetNewTransactionId()
  79. {
  80. // TO DO implement the real mechanism here
  81. return sprintf("%08x", rand(0,2000000000));
  82. }
  83. public static function IsTransactionValid($sId)
  84. {
  85. // TO DO implement the real mechanism here
  86. return true;
  87. }
  88. public static function ReadFromFile($sFileName)
  89. {
  90. if (!file_exists($sFileName)) return false;
  91. return file_get_contents($sFileName);
  92. }
  93. /**
  94. * Get access to the application config file
  95. * @param none
  96. * @return Config The Config object initialized from the application config file
  97. */
  98. public static function GetConfig()
  99. {
  100. if (self::$m_oConfig == null)
  101. {
  102. self::$m_oConfig = new Config(ITOP_CONFIG_FILE);
  103. }
  104. return self::$m_oConfig;
  105. }
  106. /**
  107. * Helper function to convert a value expressed in a 'user friendly format'
  108. * as in php.ini, e.g. 256k, 2M, 1G etc. Into a number of bytes
  109. * @param mixed $value The value as read from php.ini
  110. * @return number
  111. */
  112. public static function ConvertToBytes( $value )
  113. {
  114. $iReturn = $value;
  115. if ( !is_numeric( $value ) )
  116. {
  117. $iLength = strlen( $value );
  118. $iReturn = substr( $value, 0, $iLength - 1 );
  119. $sUnit = strtoupper( substr( $value, $iLength - 1 ) );
  120. switch ( $sUnit )
  121. {
  122. case 'G':
  123. $iReturn *= 1024;
  124. case 'M':
  125. $iReturn *= 1024;
  126. case 'K':
  127. $iReturn *= 1024;
  128. }
  129. }
  130. return $iReturn;
  131. }
  132. }
  133. ?>