PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/web raovat.vn php/az24/api/manyou/Service/Client/OAuth.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 201 lines | 167 code | 28 blank | 6 comment | 11 complexity | 2aaecd61cddc26889af3f15a18718150 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: OAuth.php 29306 2012-04-01 03:42:53Z houdelei $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class Cloud_Service_Client_OAuth {
  12. protected $_appKey = '';
  13. protected $_appSecret = '';
  14. protected $_apiIp = '';
  15. private $_tokenSecret = '';
  16. private $_boundary = '';
  17. const OAUTH_VERSION = '1.0';
  18. const OAUTH_SIGNATURE_METHOD = 'HMAC-SHA1';
  19. protected static $_instance;
  20. public static function getInstance() {
  21. if (!(self::$_instance instanceof self)) {
  22. self::$_instance = new self();
  23. }
  24. return self::$_instance;
  25. }
  26. public function getRequest($requestURL, $extra = array(), $oauthMethod = 'GET', $multi = array()) {
  27. if($multi) {
  28. $imageFile = $this->_getImageBinary($multi);
  29. $extra = array_merge($extra, $imageFile['binary']);
  30. }
  31. $params = $this->_getOAuthSignatureParams($extra);
  32. $params['oauth_signature'] = $this->_getOAuthSignature($requestURL, $params, $oauthMethod, $multi ? TRUE : FALSE);
  33. $queryString = $this->_httpBuildQuery($params, $imageFile['fileInfo']);
  34. if($oauthMethod == 'GET') {
  35. $requestURL = $requestURL.'?'.$queryString;
  36. $queryString = '';
  37. } else {
  38. $requestURL = $requestURL.'?';
  39. }
  40. $response = dfsockopen($requestURL, 0, $queryString, '', false, $this->_apiIp, 15, TRUE, !$multi ? 'URLENCODE' : 'FORMDATA', false);
  41. return $response;
  42. }
  43. public function getTimestamp() {
  44. return time();
  45. }
  46. public function getOAuthNonce() {
  47. return time().(time() % $this->_appKey);
  48. }
  49. protected function setAppKey($appKey, $appSecret) {
  50. $this->_appKey = $appKey;
  51. $this->_appSecret = $appSecret;
  52. }
  53. protected function setTokenSecret($tokenSecret) {
  54. $this->_tokenSecret = $tokenSecret;
  55. }
  56. protected function setApiIp($apiIp) {
  57. $this->_apiIp = $apiIp;
  58. }
  59. public function customHmac($str, $key) {
  60. $utilService = Cloud::loadClass('Service_Util');
  61. return base64_encode($utilService->hashHmac('sha1', $str, $key, true));
  62. }
  63. private function _getOAuthSignatureParams($extra = array()) {
  64. $params = array(
  65. 'oauth_consumer_key' => $this->_appKey,
  66. 'oauth_nonce' => $this->getOAuthNonce(),
  67. 'oauth_signature_method' => self::OAUTH_SIGNATURE_METHOD,
  68. 'oauth_timestamp' => $this->getTimestamp(),
  69. 'oauth_version' => self::OAUTH_VERSION,
  70. );
  71. if($extra) {
  72. $params = array_merge($params, $extra);
  73. }
  74. uksort($params, 'strcmp');
  75. return $params;
  76. }
  77. private function _httpBuildQuery($params, $multi = array()) {
  78. if(!$params) {
  79. return '';
  80. }
  81. $multiPartBody = '';
  82. if($multi) {
  83. $this->_boundary = uniqid('------------------');
  84. $bodyBoundary = '--'.$this->_boundary;
  85. $endBodyBoundary = $bodyBoundary.'--';
  86. foreach($params as $param => $value) {
  87. if(array_key_exists($param, $multi)) {
  88. $ext = strtolower(substr(strrchr($multi[$param]['file'], '.'), 1, 10));
  89. $fileName = 'picture.'.$ext;
  90. $mime = $multi[$param]['mime'];
  91. $multiPartBody .= $bodyBoundary."\r\n";
  92. $multiPartBody .= 'Content-Disposition: form-data; name="'.$param.'"; filename="'.$fileName.'"'."\r\n";
  93. $multiPartBody .= 'Content-Type: '.$mime."\r\n\r\n";
  94. $multiPartBody .= $value. "\r\n";
  95. } else {
  96. $multiPartBody .= $bodyBoundary . "\r\n";
  97. $multiPartBody .= 'content-disposition: form-data; name="'.$param."\"\r\n\r\n";
  98. $multiPartBody .= $value."\r\n";
  99. }
  100. }
  101. $multiPartBody .= $endBodyBoundary."\r\n";
  102. } else {
  103. foreach($params as $param => $value) {
  104. $multiPartBody .= $comma.$this->rawurlencode($param).'='.$this->rawurlencode($value);
  105. $comma = '&';
  106. }
  107. }
  108. return $multiPartBody;
  109. }
  110. private function _getOAuthSignature($url, $params, $method = 'POST', $multi = FALSE) {
  111. $method = strtoupper($method);
  112. if(!in_array($method, array ('GET', 'POST'))) {
  113. throw new Exception('Request Method Invlid');
  114. }
  115. foreach($params as $name => $val) {
  116. $param_str .= $comma.$name.'='.$val;
  117. $comma = '&';
  118. }
  119. if($multi) {
  120. $base_string = $method.'&'.$url.'&'.$param_str;
  121. } else {
  122. $base_string = $method.'&'.$this->rawurlencode($url).'&'.$this->rawurlencode($param_str);
  123. }
  124. $key = $this->_appSecret.'&'.$this->_tokenSecret;
  125. $signature = $this->customHmac($base_string, $key);
  126. return $signature;
  127. }
  128. public function rawurlencode($input) {
  129. if(is_array($input)) {
  130. return array_map(array(__CLASS__, 'rawurlencode'), $input);
  131. } elseif(is_scalar($input)) {
  132. return str_replace('%7E', '~', rawurlencode($input));
  133. } else {
  134. return '';
  135. }
  136. }
  137. private function _getImageBinary($files) {
  138. $keys = array_keys($files);
  139. $fileInfo = array();
  140. foreach($keys as $key) {
  141. if($key != 'remote') {
  142. $fileInfo[$key]['file'] = $files[$key];
  143. $imgInfo = getimagesize($files[$key]);
  144. $fileInfo[$key]['mime'] = $imgInfo['mime'];
  145. $contents = $use_include_path = null;
  146. if($files['remote']) {
  147. $opt = array(
  148. 'http' => array(
  149. 'timeout' => 10,
  150. )
  151. );
  152. $contents = stream_context_create($opt);
  153. }
  154. $files[$key] = file_get_contents($files[$key], $use_include_path, $contents);
  155. }
  156. }
  157. unset($files['remote']);
  158. return array('binary' => $files, 'fileInfo' => $fileInfo);
  159. }
  160. }