PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/api/manyou/Service/Util.php

https://github.com/jinbo51/DiscuzX
PHP | 211 lines | 162 code | 43 blank | 6 comment | 41 complexity | 808ab3b4375e3f65f3f278522eed3818 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 31728 2012-09-25 09:03:42Z zhouxiaobo $
  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(), $isCamelCase = false, $isReturnArray = 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. if(!$isReturnArray) {
  44. $params = $this->httpBuildQuery($params, '', '&');
  45. }
  46. return $params;
  47. }
  48. public function redirect($url, $code = 302) {
  49. @ob_end_clean();
  50. @ob_start();
  51. $errorChars = array();
  52. for ($i = 0; $i <= 31; $i ++) {
  53. $errorChars[] = chr($i);
  54. }
  55. $url = trim(str_replace($errorChars, '', $url));
  56. if (strpos($url, '/') === 0) {
  57. $url = '/' . ltrim($url, '/');
  58. }
  59. @header('Location: ' . $url, true, $code);
  60. exit;
  61. }
  62. public function generateUniqueId() {
  63. $siteuniqueid = C::t('common_setting')->fetch('siteuniqueid');
  64. if(empty($siteuniqueid) || strlen($siteuniqueid) < 16) {
  65. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  66. $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);
  67. C::t('common_setting')->update('siteuniqueid', $siteuniqueid);
  68. require_once libfile('function/cache');
  69. updatecache('setting');
  70. }
  71. }
  72. public function httpBuildQuery($data, $numeric_prefix='', $arg_separator='', $prefix='') {
  73. $render = array();
  74. if (empty($arg_separator)) {
  75. $arg_separator = @ini_get('arg_separator.output');
  76. empty($arg_separator) && $arg_separator = '&';
  77. }
  78. foreach ((array) $data as $key => $val) {
  79. if (is_array($val) || is_object($val)) {
  80. $_key = empty($prefix) ? "{$key}[%s]" : sprintf($prefix, $key) . "[%s]";
  81. $_render = $this->httpBuildQuery($val, '', $arg_separator, $_key);
  82. if (!empty($_render)) {
  83. $render[] = $_render;
  84. }
  85. } else {
  86. if (is_numeric($key) && empty($prefix)) {
  87. $render[] = urlencode("{$numeric_prefix}{$key}") . "=" . urlencode($val);
  88. } else {
  89. if (!empty($prefix)) {
  90. $_key = sprintf($prefix, $key);
  91. $render[] = urlencode($_key) . "=" . urlencode($val);
  92. } else {
  93. $render[] = urlencode($key) . "=" . urlencode($val);
  94. }
  95. }
  96. }
  97. }
  98. $render = implode($arg_separator, $render);
  99. if (empty($render)) {
  100. $render = '';
  101. }
  102. return $render;
  103. }
  104. public function getApiVersion() {
  105. return '0.6';
  106. }
  107. public function hashHmac($algo, $data, $key, $raw_output = false) {
  108. if (function_exists('hash_hmac')) {
  109. return hash_hmac($algo, $data, $key, $raw_output);
  110. } else {
  111. $algo = strtolower($algo);
  112. $pack = 'H'.strlen(call_user_func($algo, 'test'));
  113. $size = 64;
  114. $opad = str_repeat(chr(0x5C), $size);
  115. $ipad = str_repeat(chr(0x36), $size);
  116. if(strlen($key) > $size) {
  117. $key = str_pad(pack($pack, call_user_func($algo, $key)), $size, chr(0x00));
  118. } else {
  119. $key = str_pad($key, $size, chr(0x00));
  120. }
  121. for ($i = 0; $i < strlen($key) - 1; $i++) {
  122. $opad[$i] = $opad[$i] ^ $key[$i];
  123. $ipad[$i] = $ipad[$i] ^ $key[$i];
  124. }
  125. $output = call_user_func($algo, $opad.pack($pack, call_user_func($algo, $ipad.$data)));
  126. return ($raw_output) ? pack($pack, $output) : $output;
  127. }
  128. }
  129. public function isMobile($status) {
  130. if (getstatus($status, 11) || getstatus($status, 12) || getstatus($status, 13)) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. public function mobileHasSound() {
  136. if (getstatus($status, 13)) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. public function mobileHasPhoto() {
  142. if (getstatus($status, 12) && getstatus($status, 11)) {
  143. return true;
  144. }
  145. return false;
  146. }
  147. public function mobileHasGPS() {
  148. if (getstatus($status, 12)) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. public function isfounder($user) {
  154. global $_G;
  155. $founders = str_replace(' ', '', $_G['config']['admincp']['founder']);
  156. if(!$user['uid'] || $user['groupid'] != 1 || $user['adminid'] != 1) {
  157. return false;
  158. } elseif(empty($founders)) {
  159. return false;
  160. } elseif(strexists(",$founders,", ",$user[uid],")) {
  161. return true;
  162. } elseif(!is_numeric($user['username']) && strexists(",$founders,", ",$user[username],")) {
  163. return true;
  164. } else {
  165. return FALSE;
  166. }
  167. }
  168. }