common.func.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function url($segment, $params = array(), $noredirect = false) {
  8. return murl($segment, $params, $noredirect);
  9. }
  10. function message($msg, $redirect = '', $type = '') {
  11. global $_W;
  12. if($redirect == 'refresh') {
  13. $redirect = $_W['script_name'] . '?' . $_SERVER['QUERY_STRING'];
  14. } elseif (!empty($redirect) && !strexists($redirect, 'http://') && !strexists($redirect, 'https://')) {
  15. $urls = parse_url($redirect);
  16. $redirect = $_W['siteroot'] . 'app/index.php?' . $urls['query'];
  17. } else {
  18. $redirect = safe_gpc_url($redirect);
  19. }
  20. if($redirect == '') {
  21. $type = in_array($type, array('success', 'error', 'info', 'warning', 'ajax', 'sql')) ? $type : 'info';
  22. } else {
  23. $type = in_array($type, array('success', 'error', 'info', 'warning', 'ajax', 'sql')) ? $type : 'success';
  24. }
  25. if($_W['isajax'] || $type == 'ajax') {
  26. $vars = array();
  27. $vars['message'] = $msg;
  28. $vars['redirect'] = $redirect;
  29. $vars['type'] = $type;
  30. exit(json_encode($vars));
  31. }
  32. if (empty($msg) && !empty($redirect)) {
  33. header('location: '.$redirect);
  34. }
  35. $label = $type;
  36. if($type == 'error') {
  37. $label = 'danger';
  38. }
  39. if($type == 'ajax' || $type == 'sql') {
  40. $label = 'warning';
  41. }
  42. if (defined('IN_API')) {
  43. exit($msg);
  44. }
  45. include template('common/message', TEMPLATE_INCLUDEPATH);
  46. exit();
  47. }
  48. function itoast($msg, $redirect = '', $type = '') {
  49. return message($msg, $redirect, $type);
  50. }
  51. function checkauth() {
  52. global $_W, $engine;
  53. load()->model('mc');
  54. load()->model('account');
  55. if(!empty($_W['member']) && (!empty($_W['member']['mobile']) || !empty($_W['member']['email']))) {
  56. return true;
  57. }
  58. if(!empty($_W['openid'])) {
  59. $fan = mc_fansinfo($_W['openid'], $_W['acid'], $_W['uniacid']);
  60. if (empty($fan) && $_W['account']['level'] == ACCOUNT_SERVICE_VERIFY) {
  61. $fan = mc_oauth_userinfo();
  62. if (!empty($fan['openid'])) {
  63. $fan = mc_fansinfo($fan['openid']);
  64. }
  65. }
  66. if (empty($fan['uid'])) {
  67. $setting = uni_setting($_W['uniacid'], array('passport'));
  68. if (!isset($setting['passport']) || empty($setting['passport']['focusreg'])) {
  69. $reg_members = mc_init_fans_info($_W['openid'], true);
  70. $fan['uid'] = $reg_members['uid'];
  71. }
  72. }
  73. if(_mc_login(array('uid' => intval($fan['uid'])))) {
  74. return true;
  75. }
  76. if (defined('IN_API')) {
  77. $GLOBALS['engine']->died("抱歉,您需要先登录才能使用此功能,点击此处 <a href='".__buildSiteUrl(url('auth/login')) ."'>【登录】</a>");
  78. }
  79. }
  80. $forward = base64_encode($_SERVER['QUERY_STRING']);
  81. if($_W['isajax']) {
  82. $result = array();
  83. $result['url'] = url('auth/login', array('forward' => $forward), true);
  84. $result['act'] = 'redirect';
  85. exit(json_encode($result));
  86. } else {
  87. header("location: " . url('auth/login', array('forward' => $forward)), true);
  88. }
  89. exit;
  90. }
  91. function __buildSiteUrl($url) {
  92. global $_W, $engine;
  93. $mapping = array(
  94. '[from]' => $engine->message['from'],
  95. '[to]' => $engine->message['to'],
  96. '[uniacid]' => $_W['uniacid'],
  97. );
  98. $url = str_replace(array_keys($mapping), array_values($mapping), $url);
  99. $pass = array();
  100. $pass['openid'] = $engine->message['from'];
  101. $pass['acid'] = $_W['acid'];
  102. $sql = 'SELECT `fanid`,`salt`,`uid` FROM ' . tablename('mc_mapping_fans') . ' WHERE `acid`=:acid AND `openid`=:openid';
  103. $params = array();
  104. $params[':acid'] = $_W['acid'];
  105. $params[':openid'] = $pass['openid'];
  106. $fan = pdo_fetch($sql, $params);
  107. if(empty($fan) || !is_array($fan) || empty($fan['salt'])) {
  108. $fan = array('salt' => '');
  109. }
  110. $pass['time'] = TIMESTAMP;
  111. $pass['hash'] = md5("{$pass['openid']}{$pass['time']}{$fan['salt']}{$_W['config']['setting']['authkey']}");
  112. $auth = base64_encode(json_encode($pass));
  113. $vars = array();
  114. $vars['uniacid'] = $_W['uniacid'];
  115. $vars['__auth'] = $auth;
  116. $vars['forward'] = base64_encode($url);
  117. return $_W['siteroot'] . 'app/' . url('auth/forward', $vars);
  118. }
  119. function register_jssdk($debug = false){
  120. global $_W;
  121. if (defined('HEADER')) {
  122. echo '';
  123. return;
  124. }
  125. $sysinfo = array(
  126. 'uniacid' => $_W['uniacid'],
  127. 'acid' => $_W['acid'],
  128. 'siteroot' => $_W['siteroot'],
  129. 'siteurl' => $_W['siteurl'],
  130. 'attachurl' => $_W['attachurl'],
  131. 'cookie' => array('pre'=>$_W['config']['cookie']['pre'])
  132. );
  133. if (!empty($_W['acid'])) {
  134. $sysinfo['acid'] = $_W['acid'];
  135. }
  136. if (!empty($_W['openid'])) {
  137. $sysinfo['openid'] = $_W['openid'];
  138. }
  139. if (defined('MODULE_URL')) {
  140. $sysinfo['MODULE_URL'] = MODULE_URL;
  141. }
  142. $sysinfo = json_encode($sysinfo);
  143. $jssdkconfig = json_encode($_W['account']['jssdkconfig']);
  144. $debug = $debug ? 'true' : 'false';
  145. $script = <<<EOF
  146. <script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
  147. <script type="text/javascript">
  148. window.sysinfo = window.sysinfo || $sysinfo || {};
  149. // jssdk config 对象
  150. jssdkconfig = $jssdkconfig || {};
  151. // 是否启用调试
  152. jssdkconfig.debug = $debug;
  153. jssdkconfig.jsApiList = [
  154. 'checkJsApi',
  155. 'onMenuShareTimeline',
  156. 'onMenuShareAppMessage',
  157. 'onMenuShareQQ',
  158. 'onMenuShareWeibo',
  159. 'hideMenuItems',
  160. 'showMenuItems',
  161. 'hideAllNonBaseMenuItem',
  162. 'showAllNonBaseMenuItem',
  163. 'translateVoice',
  164. 'startRecord',
  165. 'stopRecord',
  166. 'onRecordEnd',
  167. 'playVoice',
  168. 'pauseVoice',
  169. 'stopVoice',
  170. 'uploadVoice',
  171. 'downloadVoice',
  172. 'chooseImage',
  173. 'previewImage',
  174. 'uploadImage',
  175. 'downloadImage',
  176. 'getNetworkType',
  177. 'openLocation',
  178. 'getLocation',
  179. 'hideOptionMenu',
  180. 'showOptionMenu',
  181. 'closeWindow',
  182. 'scanQRCode',
  183. 'chooseWXPay',
  184. 'openProductSpecificView',
  185. 'addCard',
  186. 'chooseCard',
  187. 'openCard'
  188. ];
  189. wx.config(jssdkconfig);
  190. </script>
  191. EOF;
  192. echo $script;
  193. }
  194. function tourl($url) {
  195. $reg = '/^tel:(\d+)$/';
  196. if (preg_match($reg, $url)) {
  197. return $url;
  198. }
  199. return $url . '&wxref=mp.weixin.qq.com#wechat_redirect';
  200. }