PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/api/manyou/Service/Util.php

https://github.com/kuaileshike/upload
PHP | 208 lines | 160 code | 42 blank | 6 comment | 40 complexity | ab0c59e380110da37012615323a9fcf3 MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: Util.php 29766 2012-04-27 02:43:58Z yexinhao $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class Cloud_Service_Util {
  12. protected static $_instance;
  13. public static function getInstance() {
  14. if (!(self::$_instance instanceof self)) {
  15. self::$_instance = new self();
  16. }
  17. return self::$_instance;
  18. }
  19. public function __construct() {
  20. }
  21. public function generateSiteSignUrl($params = array(), $isEncode = true, $isCamelCase = false) {
  22. global $_G;
  23. $ts = TIMESTAMP;
  24. $sId = $_G['setting']['my_siteid'];
  25. $sKey = $_G['setting']['my_sitekey'];
  26. $uid = $_G['uid'];
  27. if(!is_array($params)) {
  28. $params = array();
  29. }
  30. unset($params['sig'], $params['ts']);
  31. if ($isCamelCase) {
  32. $params['sId'] = $sId;
  33. $params['sSiteUid'] = $uid;
  34. } else {
  35. $params['s_id'] = $sId;
  36. $params['s_site_uid'] = $uid;
  37. }
  38. ksort($params);
  39. $str = $this->httpBuildQuery($params, '', '&');
  40. $sig = md5(sprintf('%s|%s|%s', $str, $sKey, $ts));
  41. $params['ts'] = $ts;
  42. $params['sig'] = $sig;
  43. $url = $this->httpBuildQuery($params, '', '&');
  44. return $url;
  45. }
  46. public function redirect($url, $code = 302) {
  47. @ob_end_clean();
  48. @ob_start();
  49. $errorChars = array();
  50. for ($i = 0; $i <= 31; $i ++) {
  51. $errorChars[] = chr($i);
  52. }
  53. $url = trim(str_replace($errorChars, '', $url));
  54. if (strpos($url, '/') === 0) {
  55. $url = '/' . ltrim($url, '/');
  56. }
  57. @header('Location: ' . $url, true, $code);
  58. exit;
  59. }
  60. public function generateUniqueId() {
  61. $siteuniqueid = C::t('common_setting')->fetch('siteuniqueid');
  62. if(empty($siteuniqueid) || strlen($siteuniqueid) < 16) {
  63. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  64. $siteuniqueid = 'DX'.$chars[date('y')%60].$chars[date('n')].$chars[date('j')].$chars[date('G')].$chars[date('i')].$chars[date('s')].substr(md5($_G['clientip'].$_G['username'].TIMESTAMP), 0, 4).random(4);
  65. C::t('common_setting')->update('siteuniqueid', $siteuniqueid);
  66. require_once libfile('function/cache');
  67. updatecache('setting');
  68. }
  69. }
  70. public function httpBuildQuery($data, $numeric_prefix='', $arg_separator='', $prefix='') {
  71. $render = array();
  72. if (empty($arg_separator)) {
  73. $arg_separator = @ini_get('arg_separator.output');
  74. empty($arg_separator) && $arg_separator = '&';
  75. }
  76. foreach ((array) $data as $key => $val) {
  77. if (is_array($val) || is_object($val)) {
  78. $_key = empty($prefix) ? "{$key}[%s]" : sprintf($prefix, $key) . "[%s]";
  79. $_render = $this->httpBuildQuery($val, '', $arg_separator, $_key);
  80. if (!empty($_render)) {
  81. $render[] = $_render;
  82. }
  83. } else {
  84. if (is_numeric($key) && empty($prefix)) {
  85. $render[] = urlencode("{$numeric_prefix}{$key}") . "=" . urlencode($val);
  86. } else {
  87. if (!empty($prefix)) {
  88. $_key = sprintf($prefix, $key);
  89. $render[] = urlencode($_key) . "=" . urlencode($val);
  90. } else {
  91. $render[] = urlencode($key) . "=" . urlencode($val);
  92. }
  93. }
  94. }
  95. }
  96. $render = implode($arg_separator, $render);
  97. if (empty($render)) {
  98. $render = '';
  99. }
  100. return $render;
  101. }
  102. public function getApiVersion() {
  103. return '0.6';
  104. }
  105. public function hashHmac($algo, $data, $key, $raw_output = false) {
  106. if (function_exists('hash_hmac')) {
  107. return hash_hmac($algo, $data, $key, $raw_output);
  108. } else {
  109. $algo = strtolower($algo);
  110. $pack = 'H'.strlen(call_user_func($algo, 'test'));
  111. $size = 64;
  112. $opad = str_repeat(chr(0x5C), $size);
  113. $ipad = str_repeat(chr(0x36), $size);
  114. if(strlen($key) > $size) {
  115. $key = str_pad(pack($pack, call_user_func($algo, $key)), $size, chr(0x00));
  116. } else {
  117. $key = str_pad($key, $size, chr(0x00));
  118. }
  119. for ($i = 0; $i < strlen($key) - 1; $i++) {
  120. $opad[$i] = $opad[$i] ^ $key[$i];
  121. $ipad[$i] = $ipad[$i] ^ $key[$i];
  122. }
  123. $output = call_user_func($algo, $opad.pack($pack, call_user_func($algo, $ipad.$data)));
  124. return ($raw_output) ? pack($pack, $output) : $output;
  125. }
  126. }
  127. public function isMobile($status) {
  128. if (getstatus($status, 11) || getstatus($status, 12) || getstatus($status, 13)) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. public function mobileHasSound() {
  134. if (getstatus($status, 13)) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. public function mobileHasPhoto() {
  140. if (getstatus($status, 12) && getstatus($status, 11)) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. public function mobileHasGPS() {
  146. if (getstatus($status, 12)) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. public function isfounder($user) {
  152. global $_G;
  153. $founders = str_replace(' ', '', $_G['config']['admincp']['founder']);
  154. if(!$user['uid'] || $user['groupid'] != 1 || $user['adminid'] != 1) {
  155. return false;
  156. } elseif(empty($founders)) {
  157. return false;
  158. } elseif(strexists(",$founders,", ",$user[uid],")) {
  159. return true;
  160. } elseif(!is_numeric($user['username']) && strexists(",$founders,", ",$user[username],")) {
  161. return true;
  162. } else {
  163. return FALSE;
  164. }
  165. }
  166. }