common.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace com\unionpay\acp\sdk;
  3. include_once 'log.class.php';
  4. include_once 'SDKConfig.php';
  5. header ( 'Content-type:text/html;charset=utf-8' );
  6. class LogUtil
  7. {
  8. private static $_logger = null;
  9. public static function getLogger()
  10. {
  11. if (LogUtil::$_logger == null ) {
  12. $l = SDKConfig::getSDKConfig()->logLevel;
  13. if("INFO" == strtoupper($l))
  14. $level = PhpLog::INFO;
  15. else if("DEBUG" == strtoupper($l))
  16. $level = PhpLog::DEBUG;
  17. else if("ERROR" == strtoupper($l))
  18. $level = PhpLog::ERROR;
  19. else if("WARN" == strtoupper($l))
  20. $level = PhpLog::WARN;
  21. else if("FATAL" == strtoupper($l))
  22. $level = PhpLog::FATAL;
  23. else
  24. $level = PhpLog::OFF;
  25. LogUtil::$_logger = new PhpLog ( SDKConfig::getSDKConfig()->logFilePath, "PRC", $level );
  26. }
  27. return self::$_logger;
  28. }
  29. }
  30. /**
  31. * key1=value1&key2=value2转array
  32. * @param $str key1=value1&key2=value2的字符串
  33. * @param $$needUrlDecode 是否需要解url编码,默认不需要
  34. */
  35. function parseQString($str, $needUrlDecode=false){
  36. $result = array();
  37. $len = strlen($str);
  38. $temp = "";
  39. $curChar = "";
  40. $key = "";
  41. $isKey = true;
  42. $isOpen = false;
  43. $openName = "\0";
  44. for($i=0; $i<$len; $i++){
  45. $curChar = $str[$i];
  46. if($isOpen){
  47. if( $curChar == $openName){
  48. $isOpen = false;
  49. }
  50. $temp .= $curChar;
  51. } elseif ($curChar == "{"){
  52. $isOpen = true;
  53. $openName = "}";
  54. $temp .= $curChar;
  55. } elseif ($curChar == "["){
  56. $isOpen = true;
  57. $openName = "]";
  58. $temp .= $curChar;
  59. } elseif ($isKey && $curChar == "="){
  60. $key = $temp;
  61. $temp = "";
  62. $isKey = false;
  63. } elseif ( $curChar == "&" && !$isOpen){
  64. putKeyValueToDictionary($temp, $isKey, $key, $result, $needUrlDecode);
  65. $temp = "";
  66. $isKey = true;
  67. } else {
  68. $temp .= $curChar;
  69. }
  70. }
  71. putKeyValueToDictionary($temp, $isKey, $key, $result, $needUrlDecode);
  72. return $result;
  73. }
  74. function putKeyValueToDictionary($temp, $isKey, $key, &$result, $needUrlDecode) {
  75. if ($isKey) {
  76. $key = $temp;
  77. if (strlen ( $key ) == 0) {
  78. return false;
  79. }
  80. $result [$key] = "";
  81. } else {
  82. if (strlen ( $key ) == 0) {
  83. return false;
  84. }
  85. if ($needUrlDecode)
  86. $result [$key] = urldecode ( $temp );
  87. else
  88. $result [$key] = $temp;
  89. }
  90. }
  91. /**
  92. * 取得备份文件名
  93. *
  94. * Enter description here ...
  95. * @param $path
  96. */
  97. function getBackupFileName($path){
  98. $i = strrpos($path, ".");
  99. $leftFileName = substr($path, 0, $i);
  100. $rightFileName = substr($path, $i + 1);
  101. $newFileName = $leftFileName . '_backup.' . $rightFileName;
  102. return $newFileName;
  103. }
  104. /**
  105. * 字符串转换为 数组
  106. *
  107. * @param unknown_type $str
  108. * @return multitype:unknown
  109. */
  110. function convertStringToArray($str) {
  111. return parseQString($str);
  112. }
  113. /**
  114. * 压缩文件 对应java deflate
  115. *
  116. * @param unknown_type $params
  117. */
  118. function deflate_file(&$params) {
  119. $logger = LogUtil::getLogger();
  120. foreach ( $_FILES as $file ) {
  121. $logger->LogInfo ( "---------处理文件---------" );
  122. if (file_exists ( $file ['tmp_name'] )) {
  123. $params ['fileName'] = $file ['name'];
  124. $file_content = file_get_contents ( $file ['tmp_name'] );
  125. $file_content_deflate = gzcompress ( $file_content );
  126. $params ['fileContent'] = base64_encode ( $file_content_deflate );
  127. $logger->LogInfo ( "压缩后文件内容为>" . base64_encode ( $file_content_deflate ) );
  128. } else {
  129. $logger->LogInfo ( ">>>>文件上传失败<<<<<" );
  130. }
  131. }
  132. }
  133. /**
  134. * 讲数组转换为string
  135. *
  136. * @param $para 数组
  137. * @param $sort 是否需要排序
  138. * @param $encode 是否需要URL编码
  139. * @return string
  140. */
  141. function createLinkString($para, $sort, $encode) {
  142. if($para == NULL || !is_array($para))
  143. return "";
  144. $linkString = "";
  145. if ($sort) {
  146. $para = argSort ( $para );
  147. }
  148. foreach ($para as $key => $value) {
  149. if ($encode) {
  150. $value = urlencode ( $value );
  151. }
  152. $linkString .= $key . "=" . $value . "&";
  153. }
  154. // 去掉最后一个&字符
  155. $linkString = rtrim ( $linkString,'&');
  156. return $linkString;
  157. }
  158. /**
  159. * 对数组排序
  160. *
  161. * @param $para 排序前的数组
  162. * return 排序后的数组
  163. */
  164. function argSort($para) {
  165. ksort ( $para );
  166. reset ( $para );
  167. return $para;
  168. }
  169. function getProjName(){
  170. $dir = str_replace("\\","/", dirname(__FILE__));
  171. $rootDir = str_replace("\\", "/", $_SERVER ['DOCUMENT_ROOT']);
  172. if($rootDir[strlen($rootDir) - 1] != "/") $rootDir = $rootDir . "/";
  173. $index = strlen($rootDir);
  174. $dir = substr($dir, $index);
  175. $index = strpos($dir, "/");
  176. $projName = substr($dir, 0, $index);
  177. return $projName;
  178. }