PageRenderTime 835ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/api/manyou/Service/Connect.php

https://github.com/kuaileshike/upload
PHP | 640 lines | 535 code | 99 blank | 6 comment | 68 complexity | 665e556a3e23882297311654c5eebc3c 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: Connect.php 31961 2012-10-26 06:32:42Z monkey $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class Cloud_Service_Connect {
  12. const SPECIAL_GID = 7;
  13. protected static $_instance;
  14. public $state = '';
  15. public static function getInstance() {
  16. if (!(self::$_instance instanceof self)) {
  17. self::$_instance = new self();
  18. }
  19. return self::$_instance;
  20. }
  21. public function __construct($siteId = '', $siteKey = '') {
  22. }
  23. public function connectMergeMember() {
  24. global $_G;
  25. static $merged;
  26. if($merged) {
  27. return;
  28. }
  29. $connect_member = C::t('#qqconnect#common_member_connect')->fetch($_G['uid']);
  30. if ($connect_member) {
  31. $_G['member'] = array_merge($_G['member'], $connect_member);
  32. $user_auth_fields = $connect_member['conisfeed'];
  33. if ($user_auth_fields == 0) {
  34. $_G['member']['is_user_info'] = 0;
  35. $_G['member']['is_feed'] = 0;
  36. } elseif ($user_auth_fields == 1) {
  37. $_G['member']['is_user_info'] = 1;
  38. $_G['member']['is_feed'] = 1;
  39. } elseif ($user_auth_fields == 2) {
  40. $_G['member']['is_user_info'] = 1;
  41. $_G['member']['is_feed'] = 0;
  42. } elseif ($user_auth_fields == 3) {
  43. $_G['member']['is_user_info'] = 0;
  44. $_G['member']['is_feed'] = 1;
  45. }
  46. unset($connect_member, $_G['member']['conisfeed']);
  47. }
  48. $merged = true;
  49. }
  50. public function connectUserBindJs($params) {
  51. global $_G;
  52. $jsname = $_G['cookie']['connect_js_name'];
  53. if($jsname != 'user_bind') {
  54. return false;
  55. }
  56. $jsparams = dunserialize(base64_decode($_G['cookie']['connect_js_params']));
  57. $jsurl = $_G['connect']['url'].'/notify/user/bind';
  58. if($jsparams) {
  59. $params = array_merge($params, $jsparams);
  60. }
  61. $func = 'connect'.'UserBind'.'Params';
  62. $other_params = $this->$func();
  63. $params = array_merge($other_params, $params);
  64. $params['sig'] = $this->connectGetSig($params, $this->connectGetSigKey());
  65. $utilService = Cloud::loadClass('Service_Util');
  66. $jsurl .= '?' . $utilService->httpBuildQuery($params, '', '&');
  67. return $jsurl;
  68. }
  69. public function connectUserBindParams() {
  70. global $_G;
  71. $this->connectMergeMember();
  72. getuserprofile('birthyear');
  73. getuserprofile('birthmonth');
  74. getuserprofile('birthday');
  75. switch ($_G['member']['gender']) {
  76. case 1 :
  77. $sex = 'male';
  78. break;
  79. case 2 :
  80. $sex = 'female';
  81. break;
  82. default :
  83. $sex = 'unknown';
  84. }
  85. $is_public_email = 2;
  86. $is_use_qq_avatar = $_G['member']['conisqzoneavatar'] == 1 ? 1 : 2;
  87. $birthday = sprintf('%04d', $_G['member']['birthyear']).'-'.sprintf('%02d', $_G['member']['birthmonth']).'-'.sprintf('%02d', $_G['member']['birthday']);
  88. $agent = md5(time().rand().uniqid());
  89. $inputArray = array (
  90. 'uid' => $_G['uid'],
  91. 'agent' => $agent,
  92. 'time' => TIMESTAMP
  93. );
  94. require_once DISCUZ_ROOT.'./config/config_ucenter.php';
  95. $input = 'uid='.$_G['uid'].'&agent='.$agent.'&time='.TIMESTAMP;
  96. $avatar_input = authcode($input, 'ENCODE', UC_KEY);
  97. $params = array (
  98. 'oauth_consumer_key' => $_G['setting']['connectappid'],
  99. 'u_id' => $_G['uid'],
  100. 'username' => $_G['member']['username'],
  101. 'email' => $_G['member']['email'],
  102. 'birthday' => $birthday,
  103. 'sex' => $sex,
  104. 'is_public_email' => $is_public_email,
  105. 'is_use_qq_avatar' => $is_use_qq_avatar,
  106. 's_id' => null,
  107. 'avatar_input' => $avatar_input,
  108. 'avatar_agent' => $agent,
  109. 'site_ucenter_id' => UC_APPID,
  110. 'source' => 'qzone',
  111. );
  112. return $params;
  113. }
  114. public function connectFeedResendJs() {
  115. global $_G;
  116. $jsname = $_G['cookie']['connect_js_name'];
  117. if($jsname != 'feed_resend') {
  118. return false;
  119. }
  120. $params = dunserialize(base64_decode($_G['cookie']['connect_js_params']));
  121. $params['sig'] = $this->connectGetSig($params, $this->connectGetSigKey());
  122. $jsurl = $_G['connect']['discuz_new_feed_url'];
  123. $utilService = Cloud::loadClass('Service_Util');
  124. $jsurl .= '?' . $utilService->httpBuildQuery($params, '', '&');
  125. return $jsurl;
  126. }
  127. public function connectGuestPtloginJs() {
  128. global $_G;
  129. $jsname = $_G['cookie']['connect_js_name'];
  130. $openid = authcode($_G['cookie']['con_auth_hash']);
  131. if($jsname != 'guest_ptlogin' || !$openid) {
  132. return false;
  133. }
  134. $params = array(
  135. 'oauth_consumer_key' => $_G['setting']['connectappid'],
  136. 'openid' => $openid,
  137. 'ts' => TIMESTAMP
  138. );
  139. if(defined('IN_MOBILE')){
  140. $params['agent'] = 'wap';
  141. }
  142. $params['sig'] = $this->connectGetSig($params, $this->connectGetSigKey());
  143. $utilService = Cloud::loadClass('Service_Util');
  144. $jsurl = $_G['connect']['url'] . '/notify/user/login';
  145. $jsurl .= '?' . $utilService->httpBuildQuery($params, '', '&');
  146. dsetcookie('connect_js_name');
  147. return $jsurl;
  148. }
  149. public function connectCookieLoginJs() {
  150. global $_G;
  151. return $ajaxUrl = 'connect.php?mod=check&op=cookie';
  152. }
  153. public function connectGetSigKey() {
  154. global $_G;
  155. return $_G['setting']['connectappid'] . '|' . $_G['setting']['connectappkey'];
  156. }
  157. public function connectGetSig($params, $app_key) {
  158. ksort($params);
  159. $base_string = '';
  160. foreach($params as $key => $value) {
  161. $base_string .= $key.'='.$value;
  162. }
  163. $base_string .= $app_key;
  164. return md5($base_string);
  165. }
  166. public function connectParseBbcode($bbcode, $fId, $pId, $isHtml, &$attachImages) {
  167. include_once libfile('function/discuzcode');
  168. $result = preg_replace('/\[hide(=\d+)?\].+?\[\/hide\](\r\n|\s)/i', '', $bbcode);
  169. $result = preg_replace('/\[payto(=\d+)?\].+?\[\/payto\](\r\n|\s)/i', '', $result);
  170. $result = preg_replace('/\[quote\].*\[\/quote\](\r\n|\n|\r){0,}/is', '', $result);
  171. $result = discuzcode($result, 0, 0, $isHtml, 1, 2, 1, 0, 0, 0, 0, 1, 0);
  172. $result = strip_tags($result, '<img><a>');
  173. $result = preg_replace('/<img src="images\//i', "<img src=\"".$_G['siteurl']."images/", $result);
  174. $result = $this->connectParseAttach($result, $fId, $pId, $attachImages, $attachImageThumb);
  175. return $result;
  176. }
  177. public function connectParseAttach($content, $fId, $pId, &$attachImages) {
  178. global $_G;
  179. $permissions = $this->connectGetUserGroupPermissions(self::SPECIAL_GID, $fId);
  180. $visitorPermission = $permissions[self::SPECIAL_GID];
  181. $attachIds = array();
  182. $attachImages = array ();
  183. $attachments = C::t('forum_attachment')->fetch_all_by_id('pid', $pId);
  184. $attachments = C::t('forum_attachment_n')->fetch_all("pid:$pId", array_keys($attachments));
  185. foreach ($attachments as $k => $attach) {
  186. $aid = $attach['aid'];
  187. if($attach['isimage'] == 0 || $attach['price'] > 0 || $attach['readperm'] > $visitorPermission['readPermission'] || in_array($fId, $visitorPermission['forbidViewAttachForumIds']) || in_array($attach['aid'], $attachIds)) {
  188. continue;
  189. }
  190. $imageItem = array ();
  191. $thumbWidth = '100';
  192. $thumbHeight = '100';
  193. $bigWidth = '400';
  194. $bigHeight = '400';
  195. $thumbImageURL = $_G['siteurl'] . getforumimg($aid, 1, $thumbWidth, $thumbHeight, 'fixwr');
  196. $bigImageURL = $_G['siteurl'] . getforumimg($aid, 1, $bigWidth, $bigHeight, 'fixnone');
  197. $imageItem['aid'] = $aid;
  198. $imageItem['thumb'] = $thumbImageURL;
  199. $imageItem['big'] = $bigImageURL;
  200. if($attach['remote']) {
  201. $imageItem['path'] = $_G['setting']['ftp']['attachurl'].'forum/'.$attach['attachment'];
  202. $imageItem['remote'] = true;
  203. } else {
  204. $imageItem['path'] = $_G['setting']['attachdir'].'forum/'.$attach['attachment'];
  205. }
  206. $attachIds[] = $aid;
  207. $attachImages[] = $imageItem;
  208. }
  209. $content = preg_replace('/\[attach\](\d+)\[\/attach\]/ie', '$this->connectParseAttachTag(\\1, $attachNames)', $content);
  210. return $content;
  211. }
  212. public function connectParseAttachTag($attachId, $attachNames) {
  213. include_once libfile('function/discuzcode');
  214. if(array_key_exists($attachId, $attachNames)) {
  215. return '<span class="attach"><a href="'.$_G['siteurl'].'/attachment.php?aid='.aidencode($attachId).'">'.$attachNames[$attachId].'</a></span>';
  216. }
  217. return '';
  218. }
  219. function connectGetUserGroupPermissions($gid, $fid) {
  220. global $_G;
  221. loadcache('usergroups');
  222. $fields = array (
  223. 'groupid' => 'userGroupId',
  224. 'grouptitle' => 'userGroupName',
  225. 'readaccess' => 'readPermission',
  226. 'allowvisit' => 'allowVisit'
  227. );
  228. $userGroup = C::t('common_usergroup')->fetch_all($gid);
  229. $userGroupInfo = array();
  230. foreach ($userGroup as $id => $value) {
  231. $userGroupInfo[$id] = array_merge($value, $_G['cache']['usergroups'][$id]);
  232. $userGroupInfo[$id]['forbidForumIds'] = array ();
  233. $userGroupInfo[$id]['allowForumIds'] = array ();
  234. $userGroupInfo[$id]['specifyAllowForumIds'] = array ();
  235. $userGroupInfo[$id]['allowViewAttachForumIds'] = array ();
  236. $userGroupInfo[$id]['forbidViewAttachForumIds'] = array ();
  237. foreach ($fields as $k => $v) {
  238. $userGroupInfo[$id][$v] = $userGroupInfo[$id][$k];
  239. }
  240. }
  241. $forumField = C::t('forum_forumfield')->fetch($fid);
  242. $allowViewGroupIds = array ();
  243. if($forumField['viewperm']) {
  244. $allowViewGroupIds = explode("\t", $forumField['viewperm']);
  245. }
  246. $allowViewAttachGroupIds = array ();
  247. if($forumField['getattachperm']) {
  248. $allowViewAttachGroupIds = explode("\t", $forumField['getattachperm']);
  249. }
  250. foreach ($userGroupInfo as $groupId => $value) {
  251. if($forumField['password']) {
  252. $userGroupInfo[$groupId]['forbidForumIds'][] = $fid;
  253. continue;
  254. }
  255. $perm = unserialize($forumField['formulaperm']);
  256. if(is_array($perm)) {
  257. if($perm[0] || $perm[1] || $perm['users']) {
  258. $userGroupInfo[$groupId]['forbidForumIds'][] = $fid;
  259. continue;
  260. }
  261. }
  262. if(!$allowViewGroupIds) {
  263. $userGroupInfo[$groupId]['allowForumIds'][] = $fid;
  264. } elseif (!in_array($groupId, $allowViewGroupIds)) {
  265. $userGroupInfo[$groupId]['forbidForumIds'][] = $fid;
  266. } elseif (in_array($groupId, $allowViewGroupIds)) {
  267. $userGroupInfo[$groupId]['allowForumIds'][] = $fid;
  268. $userGroupInfo[$groupId]['specifyAllowForumIds'][] = $fid;
  269. }
  270. if(!$allowViewAttachGroupIds) {
  271. $userGroupInfo[$groupId]['allowViewAttachForumIds'][] = $fid;
  272. } elseif (!in_array($groupId, $allowViewAttachGroupIds)) {
  273. $userGroupInfo[$groupId]['forbidViewAttachForumIds'][] = $fid;
  274. } elseif (in_array($groupId, $allowViewGroupIds)) {
  275. $userGroupInfo[$groupId]['allowViewAttachForumIds'][] = $fid;
  276. }
  277. }
  278. return $userGroupInfo;
  279. }
  280. public function connectOutputPhp($url, $postData = '') {
  281. global $_G;
  282. $response = dfsockopen($url, 0, $postData, '', false, $_G['setting']['cloud_api_ip']);
  283. $result = (array) dunserialize($response);
  284. return $result;
  285. }
  286. public function connectJsOutputMessage($msg = '', $errMsg = '', $errCode = '') {
  287. $result = array (
  288. 'result' => $msg,
  289. 'errMessage' => $errMsg,
  290. 'errCode' => $errCode
  291. );
  292. echo sprintf('con_handle_response(%s);', json_encode($this->_connectUrlencode($result)));
  293. exit;
  294. }
  295. protected function _connectUrlencode($value) {
  296. if (is_array($value)) {
  297. foreach ($value as $k => $v) {
  298. $value[$k] = $this->_connectUrlencode($v);
  299. }
  300. } else if (is_string($value)) {
  301. $value = urlencode(str_replace(array("\r\n", "\r", "\n", "\"", "\/", "\t"), array('\\n', '\\n', '\\n', '\\"', '\\/', '\\t'), $value));
  302. }
  303. return $value;
  304. }
  305. public function connectCookieLoginParams() {
  306. global $_G;
  307. $this->connectMergeMember();
  308. $oauthToken = $_G['member']['conuin'];
  309. $api_url = $_G['connect']['api_url'].'/connect/discuz/cookieReport';
  310. if($oauthToken) {
  311. $extra = array (
  312. 'oauth_token' => $oauthToken
  313. );
  314. $sig_params = $this->connectGetOauthSignatureParams($extra);
  315. $oauth_token_secret = $_G['member']['conuinsecret'];
  316. $sig_params['oauth_signature'] = $this->connectGetOauthSignature($api_url, $sig_params, 'POST', $oauth_token_secret);
  317. $params = array (
  318. 'client_ip' => $_G['clientip'],
  319. 'u_id' => $_G['uid'],
  320. 'version' => 'qzone1.0',
  321. );
  322. $params = array_merge($sig_params, $params);
  323. $params['response_type'] = 'php';
  324. return $params;
  325. } else {
  326. return false;
  327. }
  328. }
  329. function connectAddCookieLogins() {
  330. global $_G;
  331. loadcache('connect_has_setting_count');
  332. if (!$_G['cache']['connect_has_setting_count']) {
  333. $times = C::t('common_setting')->fetch('connect_login_times');
  334. C::t('common_setting')->update('connect_login_times', $times + 1);
  335. savecache('connect_has_setting_count', '1');
  336. } else {
  337. C::t('common_setting')->update_count('connect_login_times', 1);
  338. }
  339. $life = 86400;
  340. $current_date = date('Y-m-d');
  341. dsetcookie('connect_last_report_time', $current_date, $life);
  342. return true;
  343. }
  344. public function connectAjaxOuputMessage($msg = '', $errCode = '') {
  345. @header("Content-type: text/html; charset=".CHARSET);
  346. echo "errCode=$errCode&result=$msg";
  347. exit;
  348. }
  349. public function connectUserUnbind($uin, $secet, $client_ip) {
  350. global $_G;
  351. $api_url = $_G['connect']['api_url'].'/connect/user/unbind';
  352. $params = array (
  353. 'oauth_consumer_key' => $_G['setting']['connectappid'],
  354. 'client_ip' => $_G['clientip'],
  355. 'response_type' => 'php',
  356. 'openid' => $_G['member']['conopenid'],
  357. 'source' => 'qzone',
  358. );
  359. $params['sig'] = $this->connectGetSig($params, $this->connectGetSigKey());
  360. $arr['version'] = 'qzone1.0';
  361. $utilService = Cloud::loadClass('Service_Util');
  362. $response = $this->connectOutputPhp($api_url.'?', $utilService->httpBuildQuery($params, '', '&'));
  363. return $response;
  364. }
  365. public function connectGetOauthSignature($url, $params, $method = 'POST', $oauth_token_secret = '') {
  366. global $_G;
  367. $method = strtoupper($method);
  368. if(!in_array($method, array ('GET', 'POST'))) {
  369. return FALSE;
  370. }
  371. $url = urlencode($url);
  372. $utilService = Cloud::loadClass('Service_Util');
  373. $param_str = urlencode($utilService->httpBuildQuery($params, '', '&'));
  374. $base_string = $method.'&'.$url.'&'.$param_str;
  375. $key = $_G['setting']['connectappkey'].'&'.$oauth_token_secret;
  376. $signature = $utilService->hashHmac('sha1', $base_string, $key);
  377. return $signature;
  378. }
  379. public function connectGetOauthSignatureParams($extra = array ()) {
  380. global $_G;
  381. $params = array (
  382. 'oauth_consumer_key' => $_G['setting']['connectappid'],
  383. 'oauth_nonce' => $this->_connectGetNonce(),
  384. 'oauth_signature_method' => 'HMAC_SHA1',
  385. 'oauth_timestamp' => TIMESTAMP
  386. );
  387. if($extra) {
  388. $params = array_merge($params, $extra);
  389. }
  390. ksort($params);
  391. return $params;
  392. }
  393. protected function _connectGetNonce() {
  394. $mt = microtime();
  395. $rand = mt_rand();
  396. return md5($mt.$rand);
  397. }
  398. public function connectParseXml($contents, $getAttributes = true, $priority = 'tag') {
  399. if (!$contents) {
  400. return array();
  401. }
  402. if (!function_exists('xml_parser_create')) {
  403. return array();
  404. }
  405. $parser = xml_parser_create('');
  406. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
  407. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  408. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  409. xml_parse_into_struct($parser, trim($contents), $xmlValues);
  410. xml_parser_free($parser);
  411. if (!$xmlValues) {
  412. return;
  413. }
  414. $xmlArray = $parent = array();
  415. $current = &$xmlArray;
  416. $repeatedTagIndex = array();
  417. foreach($xmlValues as $data) {
  418. unset($attributes, $value);
  419. extract($data);
  420. $result = $attributesData = array();
  421. if (isset($value)) {
  422. if ($priority == 'tag') {
  423. $result = $value;
  424. } else {
  425. $result['value'] = $value;
  426. }
  427. }
  428. if (isset($attributes) && $getAttributes) {
  429. foreach ($attributes as $attr => $val) {
  430. if ($priority == 'tag') {
  431. $attributesData[$attr] = $val;
  432. } else {
  433. $result['attr'][$attr] = $val;
  434. }
  435. }
  436. }
  437. if ($type == 'open') {
  438. $parent[$level - 1] = &$current;
  439. if (!is_array($current) || (!in_array($tag, array_keys($current)))) {
  440. $current[$tag] = $result;
  441. if ($attributesData) {
  442. $current[$tag . '_attr'] = $attributesData;
  443. }
  444. $repeatedTagIndex[$tag . '_' . $level] = 1;
  445. $current = &$current[$tag];
  446. } else {
  447. if (isset($current[$tag][0])) {
  448. $current[$tag][$repeatedTagIndex[$tag . '_' . $level]] = $result;
  449. $repeatedTagIndex[$tag . '_' . $level] ++;
  450. } else {
  451. $current[$tag] = array($current[$tag], $result);
  452. $repeatedTagIndex[$tag . '_' . $level] = 2;
  453. if (isset($current[$tag . '_attr'])) {
  454. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  455. unset($current[$tag . '_attr']);
  456. }
  457. }
  458. $lastItemIndex = $repeatedTagIndex[$tag . '_' . $level] - 1;
  459. $current = &$current[$tag][$lastItemIndex];
  460. }
  461. } elseif($type == 'complete') {
  462. if (!isset($current[$tag])) {
  463. $current[$tag] = $result;
  464. $repeatedTagIndex[$tag . '_' . $level] = 1;
  465. if ($priority == 'tag' && $attributesData) {
  466. $current[$tag . '_attr'] = $attributesData;
  467. }
  468. } else {
  469. if (isset($current[$tag][0]) && is_array($current[$tag])) {
  470. $current[$tag][$repeatedTagIndex[$tag . '_' . $level]] = $result;
  471. if ($priority == 'tag' && $getAttributes && $attributesData) {
  472. $current[$tag][$repeatedTagIndex[$tag . '_' . $level] . '_attr'] = $attributesData;
  473. }
  474. $repeatedTagIndex[$tag . '_' . $level] ++;
  475. } else {
  476. $current[$tag] = array($current[$tag], $result);
  477. $repeatedTagIndex[$tag . '_' . $level] = 1;
  478. if ($priority == 'tag' && $getAttributes) {
  479. if (isset($current[$tag . '_attr'])) {
  480. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  481. unset($current[$tag . '_attr']);
  482. }
  483. if ($attributesData) {
  484. $current[$tag][$repeatedTagIndex[$tag . '_' . $level] . '_attr'] = $attributesData;
  485. }
  486. }
  487. $repeatedTagIndex[$tag . '_' . $level] ++;
  488. }
  489. }
  490. } elseif($type == 'close') {
  491. $current = &$parent[$level - 1];
  492. }
  493. }
  494. return $xmlArray[key($parent[0])] ? $xmlArray[key($parent[0])] : $xmlArray;
  495. }
  496. public function connectFilterUsername($username) {
  497. $username = str_replace(' ', '_', trim($username));
  498. return cutstr($username, 15, '');
  499. }
  500. public function connectErrlog($errno, $error) {
  501. return true;
  502. }
  503. function connectCookieLoginReport($loginTimes) {
  504. global $_G;
  505. $utilService = Cloud::loadClass('Service_Util');
  506. $response = '';
  507. if ($loginTimes) {
  508. $api_url = $_G['connect']['api_url'].'/connect/discuz/batchCookieReport';
  509. $params = array (
  510. 'oauth_consumer_key' => $_G['setting']['connectappid'],
  511. 'login_times' => $loginTimes,
  512. 'date' => dgmdate(TIMESTAMP - 86400, 'Y-m-d'),
  513. 'ts' => TIMESTAMP,
  514. );
  515. $params['sig'] = $this->connectGetSig($params, $this->connectGetSigKey());
  516. $response = $this->connectOutputPhp($api_url.'?', $utilService->httpBuildQuery($params, '', '&'));
  517. } else {
  518. $response = array('status' => 0);
  519. }
  520. return $response;
  521. }
  522. }