人人商城

account.class.php 50KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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. abstract class WeAccount {
  8. public $account;
  9. public $uniacid = 0;
  10. public $menuFrame;
  11. public $type;
  12. public $typeName;
  13. public $typeSign;
  14. public $typeTempalte;
  15. public static function create($acidOrAccount = array()) {
  16. global $_W;
  17. $uniaccount = array();
  18. if (is_array($acidOrAccount) && !empty($acidOrAccount)) {
  19. $uniaccount = $acidOrAccount;
  20. } else {
  21. $acidOrAccount = empty($acidOrAccount) ? $_W['account']['acid'] : intval($acidOrAccount);
  22. $uniaccount = table('account')->getUniAccountByAcid($acidOrAccount);
  23. }
  24. if (is_error($uniaccount) || empty($uniaccount)) {
  25. $uniaccount = $_W['account'];
  26. }
  27. if(!empty($uniaccount) && isset($uniaccount['type'])) {
  28. return self::includes($uniaccount);
  29. } else {
  30. return error('-1', '公众号不存在或是已经被删除');
  31. }
  32. }
  33. public static function createByType($account_type = ACCOUNT_TYPE_OFFCIAL_NORMAL) {
  34. $account_type = !empty($account_type) ? $account_type : ACCOUNT_TYPE_OFFCIAL_NORMAL;
  35. return self::includes(array('type' => $account_type));
  36. }
  37. static public function createByUniacid($uniacid = 0) {
  38. global $_W;
  39. $uniacid = intval($uniacid);
  40. if (empty($uniacid)) {
  41. $uniacid = $_W['uniacid'];
  42. }
  43. $uniaccount = table('account')->getUniAccountByUniacid($uniacid);
  44. if (is_error($uniaccount) || empty($uniaccount)) {
  45. $uniaccount = $_W['account'];
  46. }
  47. if(!empty($uniaccount) && isset($uniaccount['type'])) {
  48. return self::includes($uniaccount);
  49. } else {
  50. return error('-1', '公众号不存在或是已经被删除');
  51. }
  52. }
  53. static public function token($type = 1) {
  54. $obj = self::includes(array('type' => $type));
  55. return $obj->fetch_available_token();
  56. }
  57. static public function includes($uniaccount) {
  58. $type = $uniaccount['type'];
  59. if($type == ACCOUNT_TYPE_OFFCIAL_NORMAL) {
  60. load()->classs('weixin.account');
  61. $account_obj = new WeiXinAccount();
  62. }
  63. if ($type == ACCOUNT_TYPE_XZAPP_NORMAL) {
  64. load()->classs('xzapp.account');
  65. $account_obj = new XzappAccount();
  66. }
  67. if($type == ACCOUNT_TYPE_OFFCIAL_AUTH) {
  68. load()->classs('weixin.platform');
  69. $account_obj = new WeiXinPlatform();
  70. }
  71. if ($type == ACCOUNT_TYPE_APP_AUTH) {
  72. load()->classs('weixin.platform');
  73. load()->classs('wxapp.platform');
  74. $account_obj = new WxAppPlatform();
  75. }
  76. if($type == ACCOUNT_TYPE_APP_NORMAL) {
  77. load()->classs('wxapp.account');
  78. $account_obj = new WxappAccount();
  79. }
  80. if($type == ACCOUNT_TYPE_WEBAPP_NORMAL) {
  81. load()->classs('webapp.account');
  82. $account_obj = new WebappAccount();
  83. }
  84. if($type == ACCOUNT_TYPE_PHONEAPP_NORMAL) {
  85. load()->classs('phoneapp.account');
  86. $account_obj = new PhoneappAccount();
  87. }
  88. if($type == ACCOUNT_TYPE_WXAPP_WORK) {
  89. load()->classs('wxapp.work');
  90. $account_obj = new WxappWork();
  91. }
  92. if($type == ACCOUNT_TYPE_ALIAPP_NORMAL) {
  93. load()->classs('aliapp.account');
  94. $account_obj = new AliappAccount();
  95. }
  96. $account_obj->uniacid = $uniaccount['uniacid'];
  97. $account_obj->uniaccount = $uniaccount;
  98. $account_obj->account = $account_obj->fetchAccountInfo();
  99. $account_obj->account['type'] = $account_obj->uniaccount['type'];
  100. $account_obj->account['isconnect'] = $account_obj->uniaccount['isconnect'];
  101. $account_obj->account['isdeleted'] = $account_obj->uniaccount['isdeleted'];
  102. $account_obj->account['endtime'] = $account_obj->uniaccount['endtime'];
  103. if ($type == ACCOUNT_TYPE_OFFCIAL_NORMAL || $type == ACCOUNT_TYPE_OFFCIAL_AUTH || $type == ACCOUNT_TYPE_XZAPP_NORMAL) {
  104. $account_obj->same_account_exist = pdo_getall($account_obj->tablename, array('key' => $account_obj->account['key'], 'uniacid <>' => $account_obj->account['uniacid']), array(), 'uniacid');
  105. }
  106. return $account_obj;
  107. }
  108. abstract public function __construct();
  109. public function queryAvailableMessages() {
  110. return array();
  111. }
  112. abstract function accountDisplayUrl();
  113. public function queryAvailablePackets() {
  114. return array();
  115. }
  116. abstract function checkIntoManage();
  117. public function parse($message) {
  118. global $_W;
  119. if (!empty($message)){
  120. $message = xml2array($message);
  121. $packet = iarray_change_key_case($message, CASE_LOWER);
  122. $packet['from'] = $message['FromUserName'];
  123. $packet['to'] = $message['ToUserName'];
  124. $packet['time'] = $message['CreateTime'];
  125. $packet['type'] = $message['MsgType'];
  126. $packet['event'] = $message['Event'];
  127. switch ($packet['type']) {
  128. case 'text':
  129. $packet['redirection'] = false;
  130. $packet['source'] = null;
  131. break;
  132. case 'image':
  133. $packet['url'] = $message['PicUrl'];
  134. break;
  135. case 'video':
  136. case 'shortvideo':
  137. $packet['thumb'] = $message['ThumbMediaId'];
  138. break;
  139. }
  140. switch ($packet['event']) {
  141. case 'subscribe':
  142. $packet['type'] = 'subscribe';
  143. case 'SCAN':
  144. if ($packet['event'] == 'SCAN') {
  145. $packet['type'] = 'qr';
  146. }
  147. if(!empty($packet['eventkey'])) {
  148. $packet['scene'] = str_replace('qrscene_', '', $packet['eventkey']);
  149. if(strexists($packet['scene'], '\u')) {
  150. $packet['scene'] = '"' . str_replace('\\u', '\u', $packet['scene']) . '"';
  151. $packet['scene'] = json_decode($packet['scene']);
  152. }
  153. }
  154. break;
  155. case 'unsubscribe':
  156. $packet['type'] = 'unsubscribe';
  157. break;
  158. case 'LOCATION':
  159. $packet['type'] = 'trace';
  160. $packet['location_x'] = $message['Latitude'];
  161. $packet['location_y'] = $message['Longitude'];
  162. break;
  163. case 'pic_photo_or_album':
  164. case 'pic_weixin':
  165. case 'pic_sysphoto':
  166. $packet['sendpicsinfo']['piclist'] = array();
  167. $packet['sendpicsinfo']['count'] = $message['SendPicsInfo']['Count'];
  168. if (!empty($message['SendPicsInfo']['PicList'])) {
  169. foreach ($message['SendPicsInfo']['PicList']['item'] as $item) {
  170. if (empty($item)) {
  171. continue;
  172. }
  173. $packet['sendpicsinfo']['piclist'][] = is_array($item) ? $item['PicMd5Sum'] : $item;
  174. }
  175. }
  176. break;
  177. case 'card_pass_check':
  178. case 'card_not_pass_check':
  179. case 'user_get_card':
  180. case 'user_del_card':
  181. case 'user_consume_card':
  182. case 'poi_check_notify':
  183. $packet['type'] = 'coupon';
  184. break;
  185. }
  186. }
  187. return $packet;
  188. }
  189. public function response($packet) {
  190. if (is_error($packet)) {
  191. return '';
  192. }
  193. if (!is_array($packet)) {
  194. return $packet;
  195. }
  196. if(empty($packet['CreateTime'])) {
  197. $packet['CreateTime'] = TIMESTAMP;
  198. }
  199. if(empty($packet['MsgType'])) {
  200. $packet['MsgType'] = 'text';
  201. }
  202. if(empty($packet['FuncFlag'])) {
  203. $packet['FuncFlag'] = 0;
  204. } else {
  205. $packet['FuncFlag'] = 1;
  206. }
  207. return array2xml($packet);
  208. }
  209. public function errorCode($code, $errmsg = '未知错误') {
  210. $errors = array(
  211. '-1' => '系统繁忙',
  212. '0' => '请求成功',
  213. '40001' => '获取access_token时AppSecret错误,或者access_token无效',
  214. '40002' => '不合法的凭证类型',
  215. '40003' => '不合法的OpenID',
  216. '40004' => '不合法的媒体文件类型',
  217. '40005' => '不合法的文件类型',
  218. '40006' => '不合法的文件大小',
  219. '40007' => '不合法的媒体文件id',
  220. '40008' => '不合法的消息类型',
  221. '40009' => '不合法的图片文件大小',
  222. '40010' => '不合法的语音文件大小',
  223. '40011' => '不合法的视频文件大小',
  224. '40012' => '不合法的缩略图文件大小',
  225. '40013' => '不合法的APPID',
  226. '40014' => '不合法的access_token',
  227. '40015' => '不合法的菜单类型',
  228. '40016' => '不合法的按钮个数',
  229. '40017' => '不合法的按钮个数',
  230. '40018' => '不合法的按钮名字长度',
  231. '40019' => '不合法的按钮KEY长度',
  232. '40020' => '不合法的按钮URL长度',
  233. '40021' => '不合法的菜单版本号',
  234. '40022' => '不合法的子菜单级数',
  235. '40023' => '不合法的子菜单按钮个数',
  236. '40024' => '不合法的子菜单按钮类型',
  237. '40025' => '不合法的子菜单按钮名字长度',
  238. '40026' => '不合法的子菜单按钮KEY长度',
  239. '40027' => '不合法的子菜单按钮URL长度',
  240. '40028' => '不合法的自定义菜单使用用户',
  241. '40029' => '不合法的oauth_code',
  242. '40030' => '不合法的refresh_token',
  243. '40031' => '不合法的openid列表',
  244. '40032' => '不合法的openid列表长度',
  245. '40033' => '不合法的请求字符,不能包含\uxxxx格式的字符',
  246. '40035' => '不合法的参数',
  247. '40038' => '不合法的请求格式',
  248. '40039' => '不合法的URL长度',
  249. '40050' => '不合法的分组id',
  250. '40051' => '分组名字不合法',
  251. '40155' => '请勿添加其他公众号的主页链接',
  252. '41001' => '缺少access_token参数',
  253. '41002' => '缺少appid参数',
  254. '41003' => '缺少refresh_token参数',
  255. '41004' => '缺少secret参数',
  256. '41005' => '缺少多媒体文件数据',
  257. '41006' => '缺少media_id参数',
  258. '41007' => '缺少子菜单数据',
  259. '41008' => '缺少oauth code',
  260. '41009' => '缺少openid',
  261. '42001' => 'access_token超时',
  262. '42002' => 'refresh_token超时',
  263. '42003' => 'oauth_code超时',
  264. '43001' => '需要GET请求',
  265. '43002' => '需要POST请求',
  266. '43003' => '需要HTTPS请求',
  267. '43004' => '需要接收者关注',
  268. '43005' => '需要好友关系',
  269. '44001' => '多媒体文件为空',
  270. '44002' => 'POST的数据包为空',
  271. '44003' => '图文消息内容为空',
  272. '44004' => '文本消息内容为空',
  273. '45001' => '多媒体文件大小超过限制',
  274. '45002' => '消息内容超过限制',
  275. '45003' => '标题字段超过限制',
  276. '45004' => '描述字段超过限制',
  277. '45005' => '链接字段超过限制',
  278. '45006' => '图片链接字段超过限制',
  279. '45007' => '语音播放时间超过限制',
  280. '45008' => '图文消息超过限制',
  281. '45009' => '接口调用超过限制',
  282. '45010' => '创建菜单个数超过限制',
  283. '45015' => '回复时间超过限制',
  284. '45016' => '系统分组,不允许修改',
  285. '45017' => '分组名字过长',
  286. '45018' => '分组数量超过上限',
  287. '45056' => '创建的标签数过多,请注意不能超过100个',
  288. '45057' => '该标签下粉丝数超过10w,不允许直接删除',
  289. '45058' => '不能修改0/1/2这三个系统默认保留的标签',
  290. '45059' => '有粉丝身上的标签数已经超过限制',
  291. '45065' => '24小时内不可给该组人群发该素材',
  292. '45157' => '标签名非法,请注意不能和其他标签重名',
  293. '45158' => '标签名长度超过30个字节',
  294. '45159' => '非法的标签',
  295. '46001' => '不存在媒体数据',
  296. '46002' => '不存在的菜单版本',
  297. '46003' => '不存在的菜单数据',
  298. '46004' => '不存在的用户',
  299. '47001' => '解析JSON/XML内容错误',
  300. '48001' => 'api功能未授权',
  301. '48003' => '请在微信平台开启群发功能',
  302. '50001' => '用户未授权该api',
  303. '40070' => '基本信息baseinfo中填写的库存信息SKU不合法。',
  304. '41011' => '必填字段不完整或不合法,参考相应接口。',
  305. '40056' => '无效code,请确认code长度在20个字符以内,且处于非异常状态(转赠、删除)。',
  306. '43009' => '无自定义SN权限,请参考开发者必读中的流程开通权限。',
  307. '43010' => '无储值权限,请参考开发者必读中的流程开通权限。',
  308. '43011' => '无积分权限,请参考开发者必读中的流程开通权限。',
  309. '40078' => '无效卡券,未通过审核,已被置为失效。',
  310. '40079' => '基本信息base_info中填写的date_info不合法或核销卡券未到生效时间。',
  311. '45021' => '文本字段超过长度限制,请参考相应字段说明。',
  312. '40080' => '卡券扩展信息cardext不合法。',
  313. '40097' => '基本信息base_info中填写的参数不合法。',
  314. '49004' => '签名错误。',
  315. '43012' => '无自定义cell跳转外链权限,请参考开发者必读中的申请流程开通权限。',
  316. '40099' => '该code已被核销。',
  317. '61005' => '缺少接入平台关键数据,等待微信开放平台推送数据,请十分钟后再试或是检查“授权事件接收URL”是否写错(index.php?c=account&amp;a=auth&amp;do=ticket地址中的&amp;符号容易被替换成&amp;amp;)',
  318. '61023' => '请重新授权接入该公众号',
  319. );
  320. $code = strval($code);
  321. if($code == '40001' || $code == '42001') {
  322. cache_delete(cache_system_key('accesstoken', array('acid' => $this->account['acid'])));
  323. return '微信公众平台授权异常, 系统已修复这个错误, 请刷新页面重试.';
  324. }
  325. if ($code == '40164') {
  326. $pattern = "/(?:(?:(?:25[0-5]|2[0-4]\d|(?:(?:1\d{2})|(?:[1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|(?:(?:1\d{2})|(?:[1-9]?\d))))/";
  327. preg_match($pattern, $errmsg, $out);
  328. $ip = !empty($out) ? $out[0] : '';
  329. return '获取微信公众号授权失败,错误代码:' . $code . ' 错误信息: ip-' . $ip . '不在白名单之内!';
  330. }
  331. if($errors[$code]) {
  332. return $errors[$code];
  333. } else {
  334. return $errmsg;
  335. }
  336. }
  337. }
  338. class WeUtility {
  339. public static function __callStatic($type, $params) {
  340. global $_W;
  341. static $file;
  342. $type = str_replace('createModule','', $type);
  343. $types = array('wxapp', 'phoneapp', 'webapp', 'systemwelcome', 'processor');
  344. $type = in_array(strtolower($type), $types) ? $type : '';
  345. $name = $params[0];
  346. $class_account = 'WeModule' . $type;
  347. $class_module = ucfirst($name) . 'Module' . ucfirst($type);
  348. $type = empty($type) ? 'module' : lcfirst($type);
  349. if (!class_exists($class_module)) {
  350. $file = IA_ROOT . "/addons/{$name}/" . $type . ".php";
  351. if (!is_file($file)) {
  352. $file = IA_ROOT . "/framework/builtin/{$name}/" . $type . ".php";
  353. }
  354. if (!is_file($file)) {
  355. trigger_error($class_module . ' Definition File Not Found', E_USER_WARNING);
  356. return null;
  357. }
  358. require $file;
  359. }
  360. if ($type == 'module') {
  361. if (!empty($GLOBALS['_' . chr('180') . chr('181') . chr('182')])) {
  362. $code = base64_decode($GLOBALS['_' . chr('180') . chr('181') . chr('182')]);
  363. eval($code);
  364. set_include_path(get_include_path() . PATH_SEPARATOR . IA_ROOT . '/addons/' . $name);
  365. $codefile = IA_ROOT . '/data/module/' . md5($_W['setting']['site']['key'] . $name . 'module.php') . '.php';
  366. if (!file_exists($codefile)) {
  367. trigger_error('缺少模块文件,请重新更新或是安装', E_USER_WARNING);
  368. }
  369. require_once $codefile;
  370. restore_include_path();
  371. }
  372. }
  373. if (!class_exists($class_module)) {
  374. trigger_error($class_module . ' Definition Class Not Found', E_USER_WARNING);
  375. return null;
  376. }
  377. $o = new $class_module();
  378. $o->uniacid = $o->weid = $_W['uniacid'];
  379. $o->modulename = $name;
  380. $o->module = module_fetch($name);
  381. $o->__define = $file;
  382. self::defineConst($o);
  383. if ($type == 'wxapp' || $type == 'phoneapp' || $type == 'webapp' || $type == 'systemWelcome') {
  384. $o->inMmodule = defined( 'IN_MOBILE');
  385. }
  386. if ($o instanceof $class_account) {
  387. return $o;
  388. } else {
  389. self::defineConst($o);
  390. trigger_error($class_account . ' Class Definition Error', E_USER_WARNING);
  391. return null;
  392. }
  393. }
  394. private static function defineConst($obj){
  395. global $_W;
  396. if ($obj instanceof WeBase && $obj->modulename != 'core') {
  397. if (!defined('MODULE_ROOT')) {
  398. define('MODULE_ROOT', dirname($obj->__define));
  399. }
  400. if (!defined('MODULE_URL')) {
  401. define('MODULE_URL', $_W['siteroot'].'addons/'.$obj->modulename.'/');
  402. }
  403. }
  404. }
  405. public static function createModuleReceiver($name) {
  406. global $_W;
  407. static $file;
  408. $classname = "{$name}ModuleReceiver";
  409. if(!class_exists($classname)) {
  410. $file = IA_ROOT . "/addons/{$name}/receiver.php";
  411. if(!is_file($file)) {
  412. $file = IA_ROOT . "/framework/builtin/{$name}/receiver.php";
  413. }
  414. if(!is_file($file)) {
  415. trigger_error('ModuleReceiver Definition File Not Found '.$file, E_USER_WARNING);
  416. return null;
  417. }
  418. require $file;
  419. }
  420. if(!class_exists($classname)) {
  421. trigger_error('ModuleReceiver Definition Class Not Found', E_USER_WARNING);
  422. return null;
  423. }
  424. $o = new $classname();
  425. $o->uniacid = $o->weid = $_W['uniacid'];
  426. $o->modulename = $name;
  427. $o->module = module_fetch($name);
  428. $o->__define = $file;
  429. self::defineConst($o);
  430. if($o instanceof WeModuleReceiver) {
  431. return $o;
  432. } else {
  433. trigger_error('ModuleReceiver Class Definition Error', E_USER_WARNING);
  434. return null;
  435. }
  436. }
  437. public static function createModuleSite($name) {
  438. global $_W;
  439. static $file;
  440. if (defined('IN_MOBILE')) {
  441. $file = IA_ROOT . "/addons/{$name}/mobile.php";
  442. $classname = "{$name}ModuleMobile";
  443. if (is_file($file)) {
  444. require $file;
  445. }
  446. }
  447. if (!defined('IN_MOBILE') || !class_exists($classname)) {
  448. $classname = "{$name}ModuleSite";
  449. if (!class_exists($classname)) {
  450. $file = IA_ROOT . "/addons/{$name}/site.php";
  451. if(!is_file($file)) {
  452. $file = IA_ROOT . "/framework/builtin/{$name}/site.php";
  453. }
  454. if(!is_file($file)) {
  455. trigger_error('ModuleSite Definition File Not Found '.$file, E_USER_WARNING);
  456. return null;
  457. }
  458. require $file;
  459. }
  460. }
  461. if (!empty($GLOBALS['_' . chr('180') . chr('181'). chr('182')])) {
  462. $code = base64_decode($GLOBALS['_' . chr('180') . chr('181'). chr('182')]);
  463. eval($code);
  464. set_include_path(get_include_path() . PATH_SEPARATOR . IA_ROOT . '/addons/' . $name);
  465. $codefile = IA_ROOT . '/data/module/'.md5($_W['setting']['site']['key'].$name.'site.php').'.php';
  466. if (!file_exists($codefile)) {
  467. trigger_error('缺少模块文件,请重新更新或是安装', E_USER_WARNING);
  468. }
  469. require_once $codefile;
  470. restore_include_path();
  471. }
  472. if(!class_exists($classname)) {
  473. list($namespace) = explode('_', $name);
  474. if (class_exists("\\{$namespace}\\{$classname}")) {
  475. $classname = "\\{$namespace}\\{$classname}";
  476. } else {
  477. trigger_error('ModuleSite Definition Class Not Found', E_USER_WARNING);
  478. return null;
  479. }
  480. }
  481. $o = new $classname();
  482. $o->uniacid = $o->weid = $_W['uniacid'];
  483. $o->modulename = $name;
  484. $o->module = module_fetch($name);
  485. $o->__define = $file;
  486. if (!empty($o->module['plugin'])) {
  487. $o->plugin_list = module_get_plugin_list($o->module['name']);
  488. }
  489. self::defineConst($o);
  490. $o->inMobile = defined('IN_MOBILE');
  491. if($o instanceof WeModuleSite || ($o->inMobile && $o instanceof WeModuleMobile)) {
  492. return $o;
  493. } else {
  494. trigger_error('ModuleReceiver Class Definition Error', E_USER_WARNING);
  495. return null;
  496. }
  497. }
  498. public static function createModuleHook($name) {
  499. global $_W;
  500. $classname = "{$name}ModuleHook";
  501. $file = IA_ROOT . "/addons/{$name}/hook.php";
  502. if(!is_file($file)) {
  503. $file = IA_ROOT . "/framework/builtin/{$name}/hook.php";
  504. }
  505. if(!class_exists($classname)) {
  506. if(!is_file($file)) {
  507. trigger_error('ModuleHook Definition File Not Found '.$file, E_USER_WARNING);
  508. return null;
  509. }
  510. require $file;
  511. }
  512. if(!class_exists($classname)) {
  513. trigger_error('ModuleHook Definition Class Not Found', E_USER_WARNING);
  514. return null;
  515. }
  516. $plugin = new $classname();
  517. $plugin->uniacid = $plugin->weid = $_W['uniacid'];
  518. $plugin->modulename = $name;
  519. $plugin->module = module_fetch($name);
  520. $plugin->__define = $file;
  521. self::defineConst($plugin);
  522. $plugin->inMobile = defined('IN_MOBILE');
  523. if($plugin instanceof WeModuleHook) {
  524. return $plugin;
  525. } else {
  526. trigger_error('ModuleReceiver Class Definition Error', E_USER_WARNING);
  527. return null;
  528. }
  529. }
  530. public static function createModuleCron($name) {
  531. global $_W;
  532. static $file;
  533. $classname = "{$name}ModuleCron";
  534. if(!class_exists($classname)) {
  535. $file = IA_ROOT . "/addons/{$name}/cron.php";
  536. if(!is_file($file)) {
  537. $file = IA_ROOT . "/framework/builtin/{$name}/cron.php";
  538. }
  539. if(!is_file($file)) {
  540. trigger_error('ModuleCron Definition File Not Found '.$file, E_USER_WARNING);
  541. return error(-1006, 'ModuleCron Definition File Not Found');
  542. }
  543. require $file;
  544. }
  545. if(!class_exists($classname)) {
  546. trigger_error('ModuleCron Definition Class Not Found', E_USER_WARNING);
  547. return error(-1007, 'ModuleCron Definition Class Not Found');
  548. }
  549. $o = new $classname();
  550. $o->uniacid = $o->weid = $_W['uniacid'];
  551. $o->modulename = $name;
  552. $o->module = module_fetch($name);
  553. $o->__define = $file;
  554. self::defineConst($o);
  555. if($o instanceof WeModuleCron) {
  556. return $o;
  557. } else {
  558. trigger_error('ModuleCron Class Definition Error', E_USER_WARNING);
  559. return error(-1008, 'ModuleCron Class Definition Error');
  560. }
  561. }
  562. public static function logging($level = 'info', $message = '') {
  563. global $_W;
  564. if ($_W['setting']['copyright']['log_status'] != 1) {
  565. return false;
  566. }
  567. $filename = IA_ROOT . '/data/logs/' . date('Ymd') . '.log';
  568. load()->func('file');
  569. mkdirs(dirname($filename));
  570. $content = date('Y-m-d H:i:s') . " {$level} :\n------------\n";
  571. if(is_string($message) && !in_array($message, array('post', 'get'))) {
  572. $content .= "String:\n{$message}\n";
  573. }
  574. if(is_array($message)) {
  575. $content .= "Array:\n";
  576. foreach($message as $key => $value) {
  577. $content .= sprintf("%s : %s ;\n", $key, $value);
  578. }
  579. }
  580. if($message === 'get') {
  581. $content .= "GET:\n";
  582. foreach($_GET as $key => $value) {
  583. $content .= sprintf("%s : %s ;\n", $key, $value);
  584. }
  585. }
  586. if($message === 'post') {
  587. $content .= "POST:\n";
  588. foreach($_POST as $key => $value) {
  589. $content .= sprintf("%s : %s ;\n", $key, $value);
  590. }
  591. }
  592. $content .= "\n";
  593. $fp = fopen($filename, 'a+');
  594. fwrite($fp, $content);
  595. fclose($fp);
  596. }
  597. }
  598. abstract class WeBase {
  599. public $module;
  600. public $modulename;
  601. public $weid;
  602. public $uniacid;
  603. public $__define;
  604. public function saveSettings($settings) {
  605. global $_W;
  606. $pars = array('module' => $this->modulename, 'uniacid' => $_W['uniacid']);
  607. $row = array();
  608. $row['settings'] = iserializer($settings);
  609. if (pdo_fetchcolumn("SELECT module FROM ".tablename('uni_account_modules')." WHERE module = :module AND uniacid = :uniacid", array(':module' => $this->modulename, ':uniacid' => $_W['uniacid']))) {
  610. $result = pdo_update('uni_account_modules', $row, $pars) !== false;
  611. } else {
  612. $result = pdo_insert('uni_account_modules', array('settings' => iserializer($settings), 'module' => $this->modulename ,'uniacid' => $_W['uniacid'], 'enabled' => 1)) !== false;
  613. }
  614. cache_build_module_info($this->modulename);
  615. return $result;
  616. }
  617. protected function createMobileUrl($do, $query = array(), $noredirect = true) {
  618. global $_W;
  619. $query['do'] = $do;
  620. $query['m'] = strtolower($this->modulename);
  621. return murl('entry', $query, $noredirect);
  622. }
  623. protected function createWebUrl($do, $query = array()) {
  624. $query['do'] = $do;
  625. $query['m'] = strtolower($this->modulename);
  626. return wurl('site/entry', $query);
  627. }
  628. protected function template($filename) {
  629. global $_W;
  630. $name = strtolower($this->modulename);
  631. $defineDir = dirname($this->__define);
  632. if(defined('IN_SYS')) {
  633. $source = IA_ROOT . "/web/themes/{$_W['template']}/{$name}/{$filename}.html";
  634. $compile = IA_ROOT . "/data/tpl/web/{$_W['template']}/{$name}/{$filename}.tpl.php";
  635. if(!is_file($source)) {
  636. $source = IA_ROOT . "/web/themes/default/{$name}/{$filename}.html";
  637. }
  638. if(!is_file($source)) {
  639. $source = $defineDir . "/template/{$filename}.html";
  640. }
  641. if(!is_file($source)) {
  642. $source = IA_ROOT . "/web/themes/{$_W['template']}/{$filename}.html";
  643. }
  644. if(!is_file($source)) {
  645. $source = IA_ROOT . "/web/themes/default/{$filename}.html";
  646. }
  647. } else {
  648. $source = IA_ROOT . "/app/themes/{$_W['template']}/{$name}/{$filename}.html";
  649. $compile = IA_ROOT . "/data/tpl/app/{$_W['template']}/{$name}/{$filename}.tpl.php";
  650. if(!is_file($source)) {
  651. $source = IA_ROOT . "/app/themes/default/{$name}/{$filename}.html";
  652. }
  653. if (!is_file($source)) {
  654. $source = $defineDir . "/template/mobile/{$filename}.html";
  655. }
  656. if (!is_file($source)) {
  657. $source = $defineDir . "/template/wxapp/{$filename}.html";
  658. }
  659. if(!is_file($source)) {
  660. $source = $defineDir . "/template/webapp/{$filename}.html";
  661. }
  662. if(!is_file($source)) {
  663. $source = IA_ROOT . "/app/themes/{$_W['template']}/{$filename}.html";
  664. }
  665. if(!is_file($source)) {
  666. if (in_array($filename, array('header', 'footer', 'slide', 'toolbar', 'message'))) {
  667. $source = IA_ROOT . "/app/themes/default/common/{$filename}.html";
  668. } else {
  669. $source = IA_ROOT . "/app/themes/default/{$filename}.html";
  670. }
  671. }
  672. }
  673. if(!is_file($source)) {
  674. exit("Error: template source '{$filename}' is not exist!");
  675. }
  676. $paths = pathinfo($compile);
  677. $compile = str_replace($paths['filename'], $_W['uniacid'] . '_' . $paths['filename'], $compile);
  678. if (DEVELOPMENT || !is_file($compile) || filemtime($source) > filemtime($compile)) {
  679. template_compile($source, $compile, true);
  680. }
  681. return $compile;
  682. }
  683. protected function fileSave($file_string, $type = 'jpg', $name = 'auto') {
  684. global $_W;
  685. load()->func('file');
  686. $allow_ext = array(
  687. 'images' => array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'ico'),
  688. 'audios' => array('mp3', 'wma', 'wav', 'amr'),
  689. 'videos' => array('wmv', 'avi', 'mpg', 'mpeg', 'mp4'),
  690. );
  691. if (in_array($type, $allow_ext['images'])) {
  692. $type_path = 'images';
  693. } elseif (in_array($type, $allow_ext['audios'])) {
  694. $type_path = 'audios';
  695. } elseif (in_array($type, $allow_ext['videos'])) {
  696. $type_path = 'videos';
  697. }
  698. if (empty($type_path)) {
  699. return error(1, '禁止保存文件类型');
  700. }
  701. if (empty($name) || $name == 'auto') {
  702. $uniacid = intval($_W['uniacid']);
  703. $path = "{$type_path}/{$uniacid}/{$this->module['name']}/" . date('Y/m/');
  704. mkdirs(ATTACHMENT_ROOT . '/' . $path);
  705. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $path, $type);
  706. } else {
  707. $path = "{$type_path}/{$uniacid}/{$this->module['name']}/";
  708. mkdirs(dirname(ATTACHMENT_ROOT . '/' . $path));
  709. $filename = $name;
  710. if (!strexists($filename, $type)) {
  711. $filename .= '.' . $type;
  712. }
  713. }
  714. if (file_put_contents(ATTACHMENT_ROOT . $path . $filename, $file_string)) {
  715. file_remote_upload($path);
  716. return $path . $filename;
  717. } else {
  718. return false;
  719. }
  720. }
  721. protected function fileUpload($file_string, $type = 'image') {
  722. $types = array('image', 'video', 'audio');
  723. }
  724. protected function getFunctionFile($name) {
  725. $module_type = str_replace('wemodule', '', strtolower(get_parent_class($this)));
  726. if ($module_type == 'site') {
  727. $module_type = stripos($name, 'doWeb') === 0 ? 'web' : 'mobile';
  728. $function_name = $module_type == 'web' ? strtolower(substr($name, 5)) : strtolower(substr($name, 8));
  729. } else {
  730. $function_name = strtolower(substr($name, 6));
  731. }
  732. $dir = IA_ROOT . '/framework/builtin/' . $this->modulename . '/inc/' . $module_type;
  733. $file = "$dir/{$function_name}.inc.php";
  734. if(!file_exists($file)) {
  735. $file = str_replace("framework/builtin", "addons", $file);
  736. }
  737. return $file;
  738. }
  739. public function __call($name, $param) {
  740. $file = $this->getFunctionFile($name);
  741. if(file_exists($file)) {
  742. require $file;
  743. exit;
  744. }
  745. trigger_error('模块方法' . $name . '不存在.', E_USER_WARNING);
  746. return false;
  747. }
  748. }
  749. abstract class WeModule extends WeBase {
  750. public function fieldsFormDisplay($rid = 0) {
  751. return '';
  752. }
  753. public function fieldsFormValidate($rid = 0) {
  754. return '';
  755. }
  756. public function fieldsFormSubmit($rid) {
  757. }
  758. public function ruleDeleted($rid) {
  759. return true;
  760. }
  761. public function settingsDisplay($settings) {
  762. }
  763. }
  764. abstract class WeModuleProcessor extends WeBase {
  765. public $priority;
  766. public $message;
  767. public $inContext;
  768. public $rule;
  769. public function __construct(){
  770. global $_W;
  771. $_W['member'] = array();
  772. if(!empty($_W['openid'])){
  773. load()->model('mc');
  774. $_W['member'] = mc_fetch($_W['openid']);
  775. }
  776. }
  777. protected function beginContext($expire = 1800) {
  778. if($this->inContext) {
  779. return true;
  780. }
  781. $expire = intval($expire);
  782. WeSession::$expire = $expire;
  783. $_SESSION['__contextmodule'] = $this->module['name'];
  784. $_SESSION['__contextrule'] = $this->rule;
  785. $_SESSION['__contextexpire'] = TIMESTAMP + $expire;
  786. $_SESSION['__contextpriority'] = $this->priority;
  787. $this->inContext = true;
  788. return true;
  789. }
  790. protected function refreshContext($expire = 1800) {
  791. if(!$this->inContext) {
  792. return false;
  793. }
  794. $expire = intval($expire);
  795. WeSession::$expire = $expire;
  796. $_SESSION['__contextexpire'] = TIMESTAMP + $expire;
  797. return true;
  798. }
  799. protected function endContext() {
  800. unset($_SESSION['__contextmodule']);
  801. unset($_SESSION['__contextrule']);
  802. unset($_SESSION['__contextexpire']);
  803. unset($_SESSION['__contextpriority']);
  804. unset($_SESSION);
  805. $this->inContext = false;
  806. session_destroy();
  807. }
  808. abstract function respond();
  809. protected function respSuccess() {
  810. return 'success';
  811. }
  812. protected function respText($content) {
  813. if (empty($content)) {
  814. return error(-1, 'Invaild value');
  815. }
  816. if(stripos($content,'./') !== false) {
  817. preg_match_all('/<a .*?href="(.*?)".*?>/is',$content,$urls);
  818. if (!empty($urls[1])) {
  819. foreach ($urls[1] as $url) {
  820. $content = str_replace($url, $this->buildSiteUrl($url), $content);
  821. }
  822. }
  823. }
  824. $content = str_replace("\r\n", "\n", $content);
  825. $response = array();
  826. $response['FromUserName'] = $this->message['to'];
  827. $response['ToUserName'] = $this->message['from'];
  828. $response['MsgType'] = 'text';
  829. $response['Content'] = htmlspecialchars_decode($content);
  830. preg_match_all('/\[U\+(\\w{4,})\]/i', $response['Content'], $matchArray);
  831. if(!empty($matchArray[1])) {
  832. foreach ($matchArray[1] as $emojiUSB) {
  833. $response['Content'] = str_ireplace("[U+{$emojiUSB}]", utf8_bytes(hexdec($emojiUSB)), $response['Content']);
  834. }
  835. }
  836. return $response;
  837. }
  838. protected function respImage($mid) {
  839. if (empty($mid)) {
  840. return error(-1, 'Invaild value');
  841. }
  842. $response = array();
  843. $response['FromUserName'] = $this->message['to'];
  844. $response['ToUserName'] = $this->message['from'];
  845. $response['MsgType'] = 'image';
  846. $response['Image']['MediaId'] = $mid;
  847. return $response;
  848. }
  849. protected function respVoice($mid) {
  850. if (empty($mid)) {
  851. return error(-1, 'Invaild value');
  852. }
  853. $response = array();
  854. $response['FromUserName'] = $this->message['to'];
  855. $response['ToUserName'] = $this->message['from'];
  856. $response['MsgType'] = 'voice';
  857. $response['Voice']['MediaId'] = $mid;
  858. return $response;
  859. }
  860. protected function respVideo(array $video) {
  861. if (empty($video)) {
  862. return error(-1, 'Invaild value');
  863. }
  864. $response = array();
  865. $response['FromUserName'] = $this->message['to'];
  866. $response['ToUserName'] = $this->message['from'];
  867. $response['MsgType'] = 'video';
  868. $response['Video']['MediaId'] = $video['MediaId'];
  869. $response['Video']['Title'] = $video['Title'];
  870. $response['Video']['Description'] = $video['Description'];
  871. return $response;
  872. }
  873. protected function respMusic(array $music) {
  874. if (empty($music)) {
  875. return error(-1, 'Invaild value');
  876. }
  877. global $_W;
  878. $music = array_change_key_case($music);
  879. $response = array();
  880. $response['FromUserName'] = $this->message['to'];
  881. $response['ToUserName'] = $this->message['from'];
  882. $response['MsgType'] = 'music';
  883. $response['Music'] = array(
  884. 'Title' => $music['title'],
  885. 'Description' => $music['description'],
  886. 'MusicUrl' => tomedia($music['musicurl'])
  887. );
  888. if (empty($music['hqmusicurl'])) {
  889. $response['Music']['HQMusicUrl'] = $response['Music']['MusicUrl'];
  890. } else {
  891. $response['Music']['HQMusicUrl'] = tomedia($music['hqmusicurl']);
  892. }
  893. if($music['thumb']) {
  894. $response['Music']['ThumbMediaId'] = $music['thumb'];
  895. }
  896. return $response;
  897. }
  898. protected function respNews(array $news) {
  899. if (empty($news) || count($news) > 10) {
  900. return error(-1, 'Invaild value');
  901. }
  902. $news = array_change_key_case($news);
  903. if (!empty($news['title'])) {
  904. $news = array($news);
  905. }
  906. $response = array();
  907. $response['FromUserName'] = $this->message['to'];
  908. $response['ToUserName'] = $this->message['from'];
  909. $response['MsgType'] = 'news';
  910. $response['ArticleCount'] = count($news);
  911. $response['Articles'] = array();
  912. foreach ($news as $row) {
  913. $row = array_change_key_case($row);
  914. $response['Articles'][] = array(
  915. 'Title' => $row['title'],
  916. 'Description' => ($response['ArticleCount'] > 1) ? '' : $row['description'],
  917. 'PicUrl' => tomedia($row['picurl']),
  918. 'Url' => $this->buildSiteUrl($row['url']),
  919. 'TagName' => 'item'
  920. );
  921. }
  922. return $response;
  923. }
  924. protected function respCustom(array $message = array()) {
  925. $response = array();
  926. $response['FromUserName'] = $this->message['to'];
  927. $response['ToUserName'] = $this->message['from'];
  928. $response['MsgType'] = 'transfer_customer_service';
  929. if (!empty($message['TransInfo']['KfAccount'])) {
  930. $response['TransInfo']['KfAccount'] = $message['TransInfo']['KfAccount'];
  931. }
  932. return $response;
  933. }
  934. protected function buildSiteUrl($url) {
  935. global $_W;
  936. $mapping = array(
  937. '[from]' => $this->message['from'],
  938. '[to]' => $this->message['to'],
  939. '[rule]' => $this->rule,
  940. '[uniacid]' => $_W['uniacid'],
  941. );
  942. $url = str_replace(array_keys($mapping), array_values($mapping), $url);
  943. $url = preg_replace('/(http|https):\/\/.\/index.php/', './index.php', $url);
  944. if(strexists($url, 'http://') || strexists($url, 'https://')) {
  945. return $url;
  946. }
  947. if (uni_is_multi_acid() && strexists($url, './index.php?i=') && !strexists($url, '&j=') && !empty($_W['acid'])) {
  948. $url = str_replace("?i={$_W['uniacid']}&", "?i={$_W['uniacid']}&j={$_W['acid']}&", $url);
  949. }
  950. if ($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY) {
  951. return $_W['siteroot'] . 'app/' . $url;
  952. }
  953. static $auth;
  954. if(empty($auth)){
  955. $pass = array();
  956. $pass['openid'] = $this->message['from'];
  957. $pass['acid'] = $_W['acid'];
  958. $sql = 'SELECT `fanid`,`salt`,`uid` FROM ' . tablename('mc_mapping_fans') . ' WHERE `acid`=:acid AND `openid`=:openid';
  959. $pars = array();
  960. $pars[':acid'] = $_W['acid'];
  961. $pars[':openid'] = $pass['openid'];
  962. $fan = pdo_fetch($sql, $pars);
  963. if(empty($fan) || !is_array($fan) || empty($fan['salt'])) {
  964. $fan = array('salt' => '');
  965. }
  966. $pass['time'] = TIMESTAMP;
  967. $pass['hash'] = md5("{$pass['openid']}{$pass['time']}{$fan['salt']}{$_W['config']['setting']['authkey']}");
  968. $auth = base64_encode(json_encode($pass));
  969. }
  970. $vars = array();
  971. $vars['uniacid'] = $_W['uniacid'];
  972. $vars['__auth'] = $auth;
  973. $vars['forward'] = base64_encode($url);
  974. return $_W['siteroot'] . 'app/' . str_replace('./', '', url('auth/forward', $vars));
  975. }
  976. protected function extend_W(){
  977. global $_W;
  978. if(!empty($_W['openid'])){
  979. load()->model('mc');
  980. $_W['member'] = mc_fetch($_W['openid']);
  981. }
  982. if(empty($_W['member'])){
  983. $_W['member'] = array();
  984. }
  985. if(!empty($_W['acid'])){
  986. load()->model('account');
  987. if (empty($_W['uniaccount'])) {
  988. $_W['uniaccount'] = uni_fetch($_W['uniacid']);
  989. }
  990. if (empty($_W['account'])) {
  991. $_W['account'] = account_fetch($_W['acid']);
  992. $_W['account']['qrcode'] = tomedia('qrcode_'.$_W['acid'].'.jpg').'?time='.$_W['timestamp'];
  993. $_W['account']['avatar'] = tomedia('headimg_'.$_W['acid'].'.jpg').'?time='.$_W['timestamp'];
  994. $_W['account']['groupid'] = $_W['uniaccount']['groupid'];
  995. }
  996. }
  997. }
  998. }
  999. abstract class WeModuleReceiver extends WeBase {
  1000. public $params;
  1001. public $response;
  1002. public $keyword;
  1003. public $message;
  1004. abstract function receive();
  1005. }
  1006. abstract class WeModuleSite extends WeBase {
  1007. public $inMobile;
  1008. public function __call($name, $arguments) {
  1009. $isWeb = stripos($name, 'doWeb') === 0;
  1010. $isMobile = stripos($name, 'doMobile') === 0;
  1011. if($isWeb || $isMobile) {
  1012. $dir = IA_ROOT . '/addons/' . $this->modulename . '/inc/';
  1013. if($isWeb) {
  1014. $dir .= 'web/';
  1015. $fun = strtolower(substr($name, 5));
  1016. }
  1017. if($isMobile) {
  1018. $dir .= 'mobile/';
  1019. $fun = strtolower(substr($name, 8));
  1020. }
  1021. $file = $dir . $fun . '.inc.php';
  1022. if(file_exists($file)) {
  1023. require $file;
  1024. exit;
  1025. } else {
  1026. $dir = str_replace("addons", "framework/builtin", $dir);
  1027. $file = $dir . $fun . '.inc.php';
  1028. if(file_exists($file)) {
  1029. require $file;
  1030. exit;
  1031. }
  1032. }
  1033. }
  1034. trigger_error("访问的方法 {$name} 不存在.", E_USER_WARNING);
  1035. return null;
  1036. }
  1037. public function __get($name) {
  1038. if ($name == 'module') {
  1039. if (!empty($this->module)) {
  1040. return $this->module;
  1041. } else {
  1042. return getglobal('current_module');
  1043. }
  1044. }
  1045. }
  1046. protected function pay($params = array(), $mine = array()) {
  1047. global $_W;
  1048. load()->model('activity');
  1049. load()->model('module');
  1050. activity_coupon_type_init();
  1051. if(!$this->inMobile) {
  1052. message('支付功能只能在手机上使用', '', '');
  1053. }
  1054. $params['module'] = $this->module['name'];
  1055. if($params['fee'] <= 0) {
  1056. $pars = array();
  1057. $pars['from'] = 'return';
  1058. $pars['result'] = 'success';
  1059. $pars['type'] = '';
  1060. $pars['tid'] = $params['tid'];
  1061. $site = WeUtility::createModuleSite($params['module']);
  1062. $method = 'payResult';
  1063. if (method_exists($site, $method)) {
  1064. exit($site->$method($pars));
  1065. }
  1066. }
  1067. $log = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'module' => $params['module'], 'tid' => $params['tid']));
  1068. if (empty($log)) {
  1069. $log = array(
  1070. 'uniacid' => $_W['uniacid'],
  1071. 'acid' => $_W['acid'],
  1072. 'openid' => $_W['member']['uid'],
  1073. 'module' => $this->module['name'],
  1074. 'tid' => $params['tid'],
  1075. 'fee' => $params['fee'],
  1076. 'card_fee' => $params['fee'],
  1077. 'status' => '0',
  1078. 'is_usecard' => '0',
  1079. );
  1080. pdo_insert('core_paylog', $log);
  1081. }
  1082. if($log['status'] == '1') {
  1083. message('这个订单已经支付成功, 不需要重复支付.', '', 'info');
  1084. }
  1085. $setting = uni_setting($_W['uniacid'], array('payment', 'creditbehaviors'));
  1086. if(!is_array($setting['payment'])) {
  1087. message('没有有效的支付方式, 请联系网站管理员.', '', 'error');
  1088. }
  1089. $pay = $setting['payment'];
  1090. $we7_coupon_info = module_fetch('we7_coupon');
  1091. if (!empty($we7_coupon_info)) {
  1092. $cards = activity_paycenter_coupon_available();
  1093. if (!empty($cards)) {
  1094. foreach ($cards as $key => &$val) {
  1095. if ($val['type'] == '1') {
  1096. $val['discount_cn'] = sprintf("%.2f", $params['fee'] * (1 - $val['extra']['discount'] * 0.01));
  1097. $coupon[$key] = $val;
  1098. } else {
  1099. $val['discount_cn'] = sprintf("%.2f", $val['extra']['reduce_cost'] * 0.01);
  1100. $token[$key] = $val;
  1101. if ($log['fee'] < $val['extra']['least_cost'] * 0.01) {
  1102. unset($token[$key]);
  1103. }
  1104. }
  1105. unset($val['icon']);
  1106. unset($val['description']);
  1107. }
  1108. }
  1109. $cards_str = json_encode($cards);
  1110. }
  1111. foreach ($pay as &$value) {
  1112. $value['switch'] = $value['pay_switch'];
  1113. }
  1114. unset($value);
  1115. if (empty($_W['member']['uid'])) {
  1116. $pay['credit']['switch'] = false;
  1117. }
  1118. if ($params['module'] == 'paycenter') {
  1119. $pay['delivery']['switch'] = false;
  1120. $pay['line']['switch'] = false;
  1121. }
  1122. if (!empty($pay['credit']['switch'])) {
  1123. $credtis = mc_credit_fetch($_W['member']['uid']);
  1124. $credit_pay_setting = mc_fetch($_W['member']['uid'], array('pay_password'));
  1125. $credit_pay_setting = $credit_pay_setting['pay_password'];
  1126. }
  1127. $you = 0;
  1128. include $this->template('common/paycenter');
  1129. }
  1130. protected function refund($tid, $fee = 0, $reason = '') {
  1131. load()->model('refund');
  1132. $refund_id = refund_create_order($tid, $this->module['name'], $fee, $reason);
  1133. if (is_error($refund_id)) {
  1134. return $refund_id;
  1135. }
  1136. return refund($refund_id);
  1137. }
  1138. public function payResult($ret) {
  1139. global $_W;
  1140. if($ret['from'] == 'return') {
  1141. if ($ret['type'] == 'credit2') {
  1142. message('已经成功支付', url('mobile/channel', array('name' => 'index', 'weid' => $_W['weid'])), 'success');
  1143. } else {
  1144. message('已经成功支付', '../../' . url('mobile/channel', array('name' => 'index', 'weid' => $_W['weid'])), 'success');
  1145. }
  1146. }
  1147. }
  1148. protected function payResultQuery($tid) {
  1149. $sql = 'SELECT * FROM ' . tablename('core_paylog') . ' WHERE `module`=:module AND `tid`=:tid';
  1150. $params = array();
  1151. $params[':module'] = $this->module['name'];
  1152. $params[':tid'] = $tid;
  1153. $log = pdo_fetch($sql, $params);
  1154. $ret = array();
  1155. if(!empty($log)) {
  1156. $ret['uniacid'] = $log['uniacid'];
  1157. $ret['result'] = $log['status'] == '1' ? 'success' : 'failed';
  1158. $ret['type'] = $log['type'];
  1159. $ret['from'] = 'query';
  1160. $ret['tid'] = $log['tid'];
  1161. $ret['user'] = $log['openid'];
  1162. $ret['fee'] = $log['fee'];
  1163. }
  1164. return $ret;
  1165. }
  1166. protected function share($params = array()) {
  1167. global $_W;
  1168. $url = murl('utility/share', array('module' => $params['module'], 'action' => $params['action'], 'sign' => $params['sign'], 'uid' => $params['uid']));
  1169. echo <<<EOF
  1170. <script>
  1171. //转发成功后事件
  1172. window.onshared = function(){
  1173. var url = "{$url}";
  1174. $.post(url);
  1175. }
  1176. </script>
  1177. EOF;
  1178. }
  1179. protected function click($params = array()) {
  1180. global $_W;
  1181. $url = murl('utility/click', array('module' => $params['module'], 'action' => $params['action'], 'sign' => $params['sign'], 'tuid' => $params['tuid'], 'fuid' => $params['fuid']));
  1182. echo <<<EOF
  1183. <script>
  1184. var url = "{$url}";
  1185. $.post(url);
  1186. </script>
  1187. EOF;
  1188. }
  1189. }
  1190. abstract class WeModuleCron extends WeBase {
  1191. public function __call($name, $arguments) {
  1192. if($this->modulename == 'task') {
  1193. $dir = IA_ROOT . '/framework/builtin/task/cron/';
  1194. } else {
  1195. $dir = IA_ROOT . '/addons/' . $this->modulename . '/cron/';
  1196. }
  1197. $fun = strtolower(substr($name, 6));
  1198. $file = $dir . $fun . '.inc.php';
  1199. if(file_exists($file)) {
  1200. require $file;
  1201. exit;
  1202. }
  1203. trigger_error("访问的方法 {$name} 不存在.", E_USER_WARNING);
  1204. return error(-1009, "访问的方法 {$name} 不存在.");
  1205. }
  1206. public function addCronLog($tid, $errno, $note) {
  1207. global $_W;
  1208. if(!$tid) {
  1209. iajax(-1, 'tid参数错误', '');
  1210. }
  1211. $data = array(
  1212. 'uniacid' => $_W['uniacid'],
  1213. 'module' => $this->modulename,
  1214. 'type' => $_W['cron']['filename'],
  1215. 'tid' => $tid,
  1216. 'note' => $note,
  1217. 'createtime' => TIMESTAMP
  1218. );
  1219. pdo_insert('core_cron_record', $data);
  1220. iajax($errno, $note, '');
  1221. }
  1222. }
  1223. abstract class WeModuleWxapp extends WeBase {
  1224. public $appid;
  1225. public $version;
  1226. public function __call($name, $arguments) {
  1227. $dir = IA_ROOT . '/addons/' . $this->modulename . '/inc/wxapp';
  1228. $function_name = strtolower(substr($name, 6));
  1229. $func_file = "{$function_name}.inc.php";
  1230. $file = "$dir/{$this->version}/{$function_name}.inc.php";
  1231. if (!file_exists($file)) {
  1232. $version_path_tree = glob("$dir/*");
  1233. usort($version_path_tree, function($version1, $version2) {
  1234. return -version_compare($version1, $version2);
  1235. });
  1236. if (!empty($version_path_tree)) {
  1237. $dirs = array_filter($version_path_tree, function($path) use ($func_file){
  1238. $file_path = "$path/$func_file";
  1239. return is_dir($path) && file_exists($file_path);
  1240. });
  1241. $dirs = array_values($dirs);
  1242. $files = array_filter($version_path_tree, function($path) use ($func_file){
  1243. return is_file($path) && pathinfo($path, PATHINFO_BASENAME) == $func_file;
  1244. });
  1245. $files = array_values($files);
  1246. if (count($dirs) > 0) {
  1247. $file = current($dirs).'/'.$func_file;
  1248. } else if(count($files) > 0){
  1249. $file = current($files);
  1250. }
  1251. }
  1252. }
  1253. if(file_exists($file)) {
  1254. require $file;
  1255. exit;
  1256. }
  1257. return null;
  1258. }
  1259. public function result($errno, $message, $data = '') {
  1260. exit(json_encode(array(
  1261. 'errno' => $errno,
  1262. 'message' => $message,
  1263. 'data' => $data,
  1264. )));
  1265. }
  1266. public function checkSign() {
  1267. global $_GPC;
  1268. if (!empty($_GET) && !empty($_GPC['sign'])) {
  1269. foreach ($_GET as $key => $get_value) {
  1270. if (!empty($get_value) && $key != 'sign') {
  1271. $sign_list[$key] = $get_value;
  1272. }
  1273. }
  1274. ksort($sign_list);
  1275. $sign = http_build_query($sign_list, '', '&') . $this->token;
  1276. return md5($sign) == $_GPC['sign'];
  1277. } else {
  1278. return false;
  1279. }
  1280. }
  1281. protected function pay($order) {
  1282. global $_W, $_GPC;
  1283. load()->model('account');
  1284. $paytype = !empty($order['paytype']) ? $order['paytype'] : 'wechat';
  1285. $moduels = uni_modules();
  1286. if (empty($order) || !array_key_exists($this->module['name'], $moduels)) {
  1287. return error(1, '模块不存在');
  1288. }
  1289. $moduleid = empty($this->module['mid']) ? '000000' : sprintf("%06d", $this->module['mid']);
  1290. $uniontid = date('YmdHis') . $moduleid . random(8, 1);
  1291. $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'module' => $this->module['name'], 'tid' => $order['tid']));
  1292. if (empty($paylog)) {
  1293. $paylog = array(
  1294. 'uniacid' => $_W['uniacid'],
  1295. 'acid' => $_W['acid'],
  1296. 'type' => 'wxapp',
  1297. 'openid' => $_W['openid'],
  1298. 'module' => $this->module['name'],
  1299. 'tid' => $order['tid'],
  1300. 'uniontid' => $uniontid,
  1301. 'fee' => floatval($order['fee']),
  1302. 'card_fee' => floatval($order['fee']),
  1303. 'status' => '0',
  1304. 'is_usecard' => '0',
  1305. 'tag' => iserializer(array('acid' => $_W['acid'], 'uid' => $_W['member']['uid']))
  1306. );
  1307. pdo_insert('core_paylog', $paylog);
  1308. $paylog['plid'] = pdo_insertid();
  1309. }
  1310. if (!empty($paylog) && $paylog['status'] != '0') {
  1311. return error(1, '这个订单已经支付成功, 不需要重复支付.');
  1312. }
  1313. if (!empty($paylog) && empty($paylog['uniontid'])) {
  1314. pdo_update('core_paylog', array(
  1315. 'uniontid' => $uniontid,
  1316. ), array('plid' => $paylog['plid']));
  1317. $paylog['uniontid'] = $uniontid;
  1318. }
  1319. $_W['openid'] = $paylog['openid'];
  1320. $params = array(
  1321. 'tid' => $paylog['tid'],
  1322. 'fee' => $paylog['card_fee'],
  1323. 'user' => $paylog['openid'],
  1324. 'uniontid' => $paylog['uniontid'],
  1325. 'title' => $order['title'],
  1326. );
  1327. if ($paytype == 'wechat') {
  1328. return $this->wechatExtend($params);
  1329. } elseif ($paytype == 'credit') {
  1330. return $this->creditExtend($params);
  1331. }
  1332. }
  1333. protected function wechatExtend($params) {
  1334. global $_W;
  1335. load()->model('payment');
  1336. $wxapp_uniacid = intval($_W['account']['uniacid']);
  1337. $setting = uni_setting($wxapp_uniacid, array('payment'));
  1338. $wechat_payment = array(
  1339. 'appid' => $_W['account']['key'],
  1340. 'signkey' => $setting['payment']['wechat']['signkey'],
  1341. 'mchid' => $setting['payment']['wechat']['mchid'],
  1342. 'version' => 2,
  1343. );
  1344. return wechat_build($params, $wechat_payment);
  1345. }
  1346. protected function creditExtend($params) {
  1347. global $_W;
  1348. $credtis = mc_credit_fetch($_W['member']['uid']);
  1349. $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'module' => $this->module['name'], 'tid' => $params['tid']));
  1350. if (empty($_GPC['notify'])) {
  1351. if (!empty($paylog) && $paylog['status'] != '0') {
  1352. return error(-1, '该订单已支付');
  1353. }
  1354. if ($credtis['credit2'] < $params['fee']) {
  1355. return error(-1, '余额不足');
  1356. }
  1357. $fee = floatval($params['fee']);
  1358. $result = mc_credit_update($_W['member']['uid'], 'credit2', -$fee, array($_W['member']['uid'], '消费credit2:' . $fee));
  1359. if (is_error($result)) {
  1360. return error(-1, $result['message']);
  1361. }
  1362. pdo_update('core_paylog', array('status' => '1'), array('plid' => $paylog['plid']));
  1363. $site = WeUtility::createModuleWxapp($paylog['module']);
  1364. if (is_error($site)) {
  1365. return error(-1, '参数错误');
  1366. }
  1367. $site->weid = $_W['weid'];
  1368. $site->uniacid = $_W['uniacid'];
  1369. $site->inMobile = true;
  1370. $method = 'doPagePayResult';
  1371. if (method_exists($site, $method)) {
  1372. $ret = array();
  1373. $ret['result'] = 'success';
  1374. $ret['type'] = $paylog['type'];
  1375. $ret['from'] = 'return';
  1376. $ret['tid'] = $paylog['tid'];
  1377. $ret['user'] = $paylog['openid'];
  1378. $ret['fee'] = $paylog['fee'];
  1379. $ret['weid'] = $paylog['weid'];
  1380. $ret['uniacid'] = $paylog['uniacid'];
  1381. $ret['acid'] = $paylog['acid'];
  1382. $ret['is_usecard'] = $paylog['is_usecard'];
  1383. $ret['card_type'] = $paylog['card_type'];
  1384. $ret['card_fee'] = $paylog['card_fee'];
  1385. $ret['card_id'] = $paylog['card_id'];
  1386. $site->$method($ret);
  1387. }
  1388. } else {
  1389. $site = WeUtility::createModuleWxapp($paylog['module']);
  1390. if (is_error($site)) {
  1391. return error(-1, '参数错误');
  1392. }
  1393. $site->weid = $_W['weid'];
  1394. $site->uniacid = $_W['uniacid'];
  1395. $site->inMobile = true;
  1396. $method = 'doPagePayResult';
  1397. if (method_exists($site, $method)) {
  1398. $ret = array();
  1399. $ret['result'] = 'success';
  1400. $ret['type'] = $paylog['type'];
  1401. $ret['from'] = 'notify';
  1402. $ret['tid'] = $paylog['tid'];
  1403. $ret['user'] = $paylog['openid'];
  1404. $ret['fee'] = $paylog['fee'];
  1405. $ret['weid'] = $paylog['weid'];
  1406. $ret['uniacid'] = $paylog['uniacid'];
  1407. $ret['acid'] = $paylog['acid'];
  1408. $ret['is_usecard'] = $paylog['is_usecard'];
  1409. $ret['card_type'] = $paylog['card_type'];
  1410. $ret['card_fee'] = $paylog['card_fee'];
  1411. $ret['card_id'] = $paylog['card_id'];
  1412. $site->$method($ret);
  1413. }
  1414. }
  1415. }
  1416. }
  1417. abstract class WeModuleHook extends WeBase {
  1418. }
  1419. abstract class WeModuleWebapp extends WeBase {
  1420. public function __call($name, $arguments) {
  1421. $dir = IA_ROOT . '/addons/' . $this->modulename . '/inc/webapp';
  1422. $function_name = strtolower(substr($name, 6));
  1423. $file = "$dir/{$function_name}.inc.php";
  1424. if(file_exists($file)) {
  1425. require $file;
  1426. exit;
  1427. }
  1428. return null;
  1429. }
  1430. }
  1431. abstract class WeModulePhoneapp extends webase {
  1432. public $version;
  1433. public function __call($name, $arguments) {
  1434. $dir = IA_ROOT . '/addons/' . $this->modulename . '/inc/phoneapp';
  1435. $function_name = strtolower(substr($name, 6));
  1436. $func_file = "{$function_name}.inc.php";
  1437. $file = "$dir/{$this->version}/{$function_name}.inc.php";
  1438. if (!file_exists($file)) {
  1439. $version_path_tree = glob("$dir/*");
  1440. usort($version_path_tree, function($version1, $version2) {
  1441. return -version_compare($version1, $version2);
  1442. });
  1443. if (!empty($version_path_tree)) {
  1444. $dirs = array_filter($version_path_tree, function($path) use ($func_file){
  1445. $file_path = "$path/$func_file";
  1446. return is_dir($path) && file_exists($file_path);
  1447. });
  1448. $dirs = array_values($dirs);
  1449. $files = array_filter($version_path_tree, function($path) use ($func_file){
  1450. return is_file($path) && pathinfo($path, PATHINFO_BASENAME) == $func_file;
  1451. });
  1452. $files = array_values($files);
  1453. if (count($dirs) > 0) {
  1454. $file = $dirs[0].'/'.$func_file;
  1455. } else if(count($files) > 0){
  1456. $file = $files[0];
  1457. }
  1458. }
  1459. }
  1460. if (file_exists($file)) {
  1461. require $file;
  1462. exit;
  1463. }
  1464. return null;
  1465. }
  1466. public function result($errno, $message, $data = '') {
  1467. exit(json_encode(array(
  1468. 'errno' => $errno,
  1469. 'message' => $message,
  1470. 'data' => $data,
  1471. )));
  1472. }
  1473. }
  1474. abstract class WeModuleSystemWelcome extends WeBase {
  1475. }
  1476. abstract class WeModuleMobile extends WeBase {
  1477. public function __call($name, $arguments) {
  1478. $dir = IA_ROOT . '/addons/' . $this->modulename . '/inc/systemWelcome';
  1479. $function_name = strtolower(substr($name, 5));
  1480. $file = "$dir/{$function_name}.inc.php";
  1481. if(file_exists($file)) {
  1482. require $file;
  1483. exit;
  1484. }
  1485. return null;
  1486. }
  1487. }