PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/source/function/function_ftn.php

https://github.com/jinbo51/DiscuzX
PHP | 152 lines | 111 code | 34 blank | 7 comment | 9 complexity | 24e25c2ebd49a06f3f1eee18b0cd2040 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: function_ftn.php 29038 2012-03-23 06:22:39Z songlixin $
  7. * 旋风上传下载,需要的函数
  8. */
  9. if(!defined('IN_DISCUZ')) {
  10. exit('Access Denied');
  11. }
  12. $_G['setting']['ftn_site_id'] = $_G['setting']['my_siteid'];
  13. $_G['setting']['xf_storage_enc_key'] = $_G['setting']['xf_storage_enc_key'];
  14. include_once libfile('function/cloud');
  15. function ftn_formhash($specialadd = '') {
  16. global $_G;
  17. return substr(md5(substr($_G['timestamp'], 0, -4).$_G['username'].$_G['uid'].$_G['authkey'].$_G['setting']['xf_storage_enc_key'].$specialadd), 8, 8);
  18. }
  19. function make_ftn_sig($formhash){
  20. global $_G;
  21. $discuz_openid = xf_getOpenidByUid($_G['uid']);
  22. $ftnGetx = array('s_id' => $_G['setting']['ftn_site_id'], 's_site_uid' => $_G['uid'], 'ts' => $_G['timestamp'], 'discuz_form_hash' => $formhash, 'site_url' => $_G['siteurl'], 'discuz_openid' => $discuz_openid);
  23. $signGetx = $ftnGetx;
  24. ksort($signGetx);
  25. return _hash_hmac('sha1',cloud_http_build_query($signGetx, '', '&'), $_G['setting']['xf_storage_enc_key']);
  26. }
  27. function make_iframe_url($formhash){
  28. global $_G;
  29. $discuz_openid = xf_getOpenidByUid($_G['uid']);
  30. $ftnGetx = array('s_id' => $_G['setting']['ftn_site_id'], 's_site_uid' => $_G['uid'], 'ts' => $_G['timestamp'], 'discuz_form_hash' => $formhash, 'site_url' => $_G['siteurl'], 'discuz_openid' => $discuz_openid);
  31. $url = "http://cp.discuz.qq.com/storage/FTN?".cloud_http_build_query($ftnGetx, '', '&');
  32. $url = $url.'&sign='.make_ftn_sig($formhash);
  33. return $url;
  34. }
  35. function make_qqdl_url($sha,$filename) {
  36. global $_G;
  37. $filename = trim($filename);
  38. $filename = urlencode(diconv($filename,CHARSET,'UTF-8'));
  39. $url = $_G['siteurl'].$filename.'?&&txf_fid='.$sha.'&siteid='.$_G['setting']['ftn_site_id'];
  40. return 'qqdl://'.base64_encode($url);
  41. }
  42. function make_downloadurl($sha1,$filesize,$filename) {
  43. global $_G;
  44. $filename = trim($filename,' "'); // Discuz! 默认的filename两侧会加上 双引号
  45. $filename = diconv($filename,CHARSET,'UTF-8');
  46. $filename = str2hex($filename);
  47. $filename = strtolower($filename[1]);
  48. $post = 'http://dz.xf.qq.com/ftn.php?v=1&&';
  49. $k = _hash_hmac('sha1',sprintf('%s|%s|%s', $sha1, $_G['timestamp'], $_G['setting']['ftn_site_id']), $_G['setting']['xf_storage_enc_key']);
  50. $parm = array(
  51. 'site_id' => $_G['setting']['ftn_site_id'],
  52. 't' => $_G['timestamp'],
  53. 'sha1' => $sha1,
  54. 'filesize' => $filesize,
  55. 'filename' => $filename,
  56. 'k' => $k,
  57. 'ip' => $_G['clientip']
  58. );
  59. return $post.cloud_http_build_query($parm,'','&&');
  60. }
  61. function _hash_hmac($algo, $data, $key, $raw_output = false) {
  62. if(function_exists('hash_hmac')) {
  63. return hash_hmac($algo, $data, $key, $raw_output);
  64. } else {
  65. $algo = strtolower($algo);
  66. $pack = 'H'.strlen(call_user_func($algo, 'test'));
  67. $size = 64;
  68. $opad = str_repeat(chr(0x5C), $size);
  69. $ipad = str_repeat(chr(0x36), $size);
  70. if(strlen($key) > $size) {
  71. $key = str_pad(pack($pack, call_user_func($algo, $key)), $size, chr(0x00));
  72. } else {
  73. $key = str_pad($key, $size, chr(0x00));
  74. }
  75. for ($i = 0; $i < strlen($key) - 1; $i++) {
  76. $opad[$i] = $opad[$i] ^ $key[$i];
  77. $ipad[$i] = $ipad[$i] ^ $key[$i];
  78. }
  79. $output = call_user_func($algo, $opad.pack($pack, call_user_func($algo, $ipad.$data)));
  80. return ($raw_output) ? pack($pack, $output) : $output;
  81. }
  82. }
  83. function _join_parm($parm = array(),$joiner = '&'){
  84. $andflag = '';
  85. $return = '';
  86. foreach($parm as $key => $value){
  87. $value = urlencode($value);
  88. $return .= $andflag.$key.'='.$value;
  89. $andflag = $joiner;
  90. }
  91. return $return;
  92. }
  93. function str2hex($str){
  94. $length = strlen($str)*2;
  95. return unpack('H'.$length,$str);
  96. }
  97. function xf_getOpenidByUid($uid) {
  98. global $_G;
  99. $openid = 0;
  100. if (getcloudappstatus('connect')) {
  101. $openid = DB::result_first('SELECT conopenid FROM ' . DB::table('common_member_connect') . ' WHERE uid = ' . $uid );
  102. }
  103. return $openid;
  104. }
  105. function checkTableExist($tableName) {
  106. global $_G;
  107. if ($tableName == '') {
  108. return false;
  109. }
  110. $tableName = $_G['config']['db']['1']['tablepre'] . $tableName;
  111. $key = 'Tables_in_' . $_G['config']['db']['1']['dbname'];
  112. $query = DB::query('SHOW TABLES');
  113. while($data = DB::fetch($query)) {
  114. $tableArray[] = $data[$key];
  115. }
  116. return in_array($tableName, $tableArray);
  117. }
  118. ?>