control.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. declare(strict_types=0);
  3. /**
  4. * mobile父类
  5. *
  6. *
  7. */
  8. //use Shopnc\Tpl;
  9. defined('InShopNC') or exit('Access Invalid!');
  10. require_once (BASE_HELPER_PATH . "/statistics_helper.php");
  11. require_once (BASE_HELPER_PATH . "/session_helper.php");
  12. require_once (BASE_HELPER_PATH . "/bonus_helper.php");
  13. /********************************** 前台control父类 **********************************************/
  14. class mobileControl
  15. {
  16. //客户端类型
  17. private static $stClienTypes = ['android', 'wap', 'wechat', 'ios', 'ajax', 'web','mini'];
  18. //列表默认分页数
  19. protected $page_size;
  20. protected $cur_page;
  21. //任务开始时间
  22. private static $startime = 0;
  23. public function __construct()
  24. {
  25. self::$startime = microtime(true);
  26. //Language::read('mobile');
  27. //分页数处理
  28. if (is_numeric($_GET['page']) && intval(trim($_GET['page'])) > 0) {
  29. $this->page_size = intval(trim($_GET['page']));
  30. } else {
  31. $this->page_size = 20;
  32. }
  33. if (is_numeric($_GET['curpage']) && intval(trim($_GET['curpage'])) > 0) {
  34. $this->cur_page = intval(trim($_GET['curpage']));
  35. } else {
  36. $this->cur_page = 1;
  37. }
  38. $this->initpage($this->page_size, $this->cur_page);
  39. $this->check_app_type();
  40. $param = $_GET;
  41. $param['client_type'] = $_SESSION['client_type'];
  42. $this->set_relay();
  43. statistics_helper::instance()->add_call($param);
  44. $client_tpe = session_helper::client_type();
  45. if($client_tpe == session_helper::device_wap) {
  46. $unused = 0;
  47. }
  48. elseif(session_helper::version_code() > 168) {
  49. if(!$this->verify()) {
  50. throw new UnSignException();
  51. }
  52. }
  53. else {
  54. $unused = 0;
  55. }
  56. }
  57. private function verify()
  58. {
  59. $pub = $this->pubKey();
  60. $input = $_GET;
  61. $sign = $input['sign'];
  62. $input['sign'] = null;
  63. $input['from'] = null;
  64. $data = $this->sign($input);
  65. $res = openssl_verify($data,base64_decode($sign),$pub);
  66. Log::record("openssl_verify res={$res}",Log::DEBUG);
  67. return $res == 1;
  68. }
  69. protected function checkEmpty($value) {
  70. if (!isset($value))
  71. return true;
  72. if ($value === null)
  73. return true;
  74. if (trim($value) === "")
  75. return true;
  76. return false;
  77. }
  78. private function sign($params)
  79. {
  80. ksort($params);
  81. $body = "";
  82. $i = 0;
  83. foreach ($params as $k => $v)
  84. {
  85. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1))
  86. {
  87. if ($i == 0) {
  88. $body .= "{$k}" . "=" . urlencode($v);
  89. } else {
  90. $body .= "&" . "{$k}" . "=" . urlencode($v);
  91. }
  92. $i++;
  93. }
  94. }
  95. return $body;
  96. }
  97. private function record_path()
  98. {
  99. $record = [];
  100. $record['user_session'] = session_helper::session_id();
  101. $record['member_id'] = session_helper::memberid();
  102. $record['relay_id'] = session_helper::relay_id();
  103. $record['add_time'] = time();
  104. $record['exec_time'] = intval((microtime(true) - self::$startime) * 1000000);
  105. $record['content'] = $_SERVER['post_content'];
  106. $record['client_type'] = session_helper::client_type();
  107. if(!empty($_GET['from'])) {
  108. $from = base64_decode($_GET['from']);
  109. Log::record("ort={$_GET['from']}",Log::DEBUG);
  110. Log::record("from={$from}",Log::DEBUG);
  111. }
  112. Log::record_path(json_encode($record));
  113. }
  114. public function __destruct()
  115. {
  116. $this->record_path();
  117. }
  118. private function set_relay()
  119. {
  120. if(isset($_GET['relay_id'])) {
  121. session_helper::set_relay($_GET['relay_id']);
  122. }
  123. elseif (isset($_COOKIE['relay_id'])) {
  124. session_helper::set_relay($_COOKIE['relay_id']);
  125. }
  126. }
  127. protected function initpage($page_size,$cur_page)
  128. {
  129. pagecmd('seteachnum', $page_size);
  130. pagecmd('setnowpage', $cur_page);
  131. }
  132. protected function price_calcer()
  133. {
  134. if(session_helper::logined()) {
  135. return new bonus\account(session_helper::memberid(),true);
  136. } else {
  137. return new bonus\normal_calc();
  138. }
  139. }
  140. protected function android()
  141. {
  142. return $_SESSION['client_type'] == 'android';
  143. }
  144. protected function page_size()
  145. {
  146. return $this->page_size;
  147. }
  148. protected function page_no()
  149. {
  150. return $this->cur_page;
  151. }
  152. protected function pages($count)
  153. {
  154. return intval($count / $this->page_size()) + ($count % $this->page_size() == 0 ? 0 : 1);
  155. }
  156. /**
  157. * @throws UnloginException
  158. */
  159. protected function need_login()
  160. {
  161. if ($_SESSION['is_login'] != 1) {
  162. throw new UnloginException();
  163. }
  164. }
  165. private static function eclipse_time()
  166. {
  167. return (microtime(true) - self::$startime);
  168. }
  169. protected function check_app_type()
  170. {
  171. $client = strtolower(trim($_SERVER['HTTP_CLIENT_TYPE']));
  172. $version = trim($_SERVER['HTTP_CLIENT_VERSION']);
  173. Log::record("HTTP_CLIENT_VERSION:{$version}",Log::DEBUG);
  174. if (empty($client)) {
  175. $client = $_POST['client_type'];
  176. Log::record($_POST['client_type'],Log::DEBUG);
  177. } else {
  178. $_SESSION['is_app'] = true;
  179. }
  180. if (empty($client) || !in_array($client, self::$stClienTypes)) {
  181. $_SESSION['client_type'] = 'wap';
  182. } else {
  183. $_SESSION['client_type'] = $client;
  184. }
  185. if (!empty($version)) {
  186. $_SESSION['client_version'] = $version;
  187. } else {
  188. $_SESSION['client_version'] = '';
  189. }
  190. $version = intval(floatval($version) * 100 + 0.5);
  191. if($client == 'ios')
  192. {
  193. $cur_ver = $GLOBALS['setting_config']['mobile_ios_version'];
  194. $lastest_version = intval($cur_ver * 100 + 0.5);
  195. if($version >= $lastest_version) {
  196. $_SESSION['is_lasted'] = true;
  197. } else {
  198. $_SESSION['is_lasted'] = false;
  199. }
  200. }
  201. elseif($client == 'android')
  202. {
  203. $cur_ver = $GLOBALS['setting_config']['mobile_apk_version'];
  204. $lastest_version = intval($cur_ver * 100 + 0.5);
  205. if($version >= $lastest_version) {
  206. $_SESSION['is_lasted'] = true;
  207. } else {
  208. $_SESSION['is_lasted'] = false;
  209. }
  210. }
  211. return true;
  212. }
  213. public static function outerr($code, $msg = '', $page = '', $type = NULL)
  214. {
  215. static $json_clients = ['android', 'ios','mini'];
  216. if(!empty($type)) {
  217. $show_type = $type;
  218. } else {
  219. $show_type = $_SESSION['client_type'];
  220. }
  221. if (in_array($show_type, $json_clients))
  222. {
  223. joutput_error($code, $msg);
  224. }
  225. elseif ($show_type == 'wap')
  226. {
  227. Tpl::clear();
  228. Tpl::output("error", $msg);
  229. if (!empty($page)) {
  230. Tpl::showpage($page);
  231. }
  232. }
  233. elseif ($show_type == 'ajax')
  234. {
  235. $callback = $_GET['callback'];
  236. if(!isset($callback) || empty($callback)) {
  237. joutput_error($code, $msg);
  238. } else {
  239. echo "{$callback}(";
  240. joutput_error($code, $msg);
  241. echo ");";
  242. }
  243. }
  244. else
  245. {
  246. if(empty($msg)) {
  247. $msg = errcode::msg($code);
  248. }
  249. $start = microtime(true);
  250. echo joutput_error($code, $msg, 'web') . "<br/>";
  251. perfor_period("joutput",$start,"web");
  252. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  253. echo "性能关键统计:<br/><br/>";
  254. echo perfor_log();
  255. $sqls = Log::sql_log();
  256. echo "sql count = " . count($sqls) . "<br/><br/>";
  257. foreach ($sqls as $sql) {
  258. echo "{$sql}<br/>";
  259. }
  260. }
  261. return true;
  262. }
  263. public static function outsuccess($data, $page = '', $type = NULL)
  264. {
  265. static $json_clients = ['android', 'ios','mini'];
  266. if(!empty($type)) {
  267. $show_type = $type;
  268. } else {
  269. $show_type = $_SESSION['client_type'];
  270. }
  271. if (in_array($show_type, $json_clients))
  272. {
  273. joutput_data($data);
  274. }
  275. elseif ($show_type == 'wap')
  276. {
  277. Tpl::clear();
  278. if (is_array($data)) {
  279. foreach ($data as $key => $val) {
  280. Tpl::output($key, $val);
  281. }
  282. }
  283. if (!empty($page)) {
  284. Tpl::showpage($page);
  285. }
  286. }
  287. elseif ($show_type == 'ajax')
  288. {
  289. $callback = $_GET['callback'];
  290. if(!isset($callback) || empty($callback)) {
  291. joutput_data($data);
  292. } else {
  293. echo "{$callback}(";
  294. joutput_data($data);
  295. echo ");";
  296. }
  297. }
  298. else
  299. {
  300. echo 'success: return data=<br/>';
  301. $start = microtime(true);
  302. joutput_data($data, 'web');
  303. perfor_period("joutput",$start,"web");
  304. echo "<br/><br/>";
  305. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  306. echo "性能关键统计:<br/><br/>";
  307. echo perfor_log();
  308. $sqls = Log::sql_log();
  309. echo "sql count = " . count($sqls) . "<br/><br/>";
  310. foreach ($sqls as $sql) {
  311. echo "{$sql}<br/>";
  312. }
  313. }
  314. return true;
  315. }
  316. public function separate_page($items,&$pages)
  317. {
  318. $arr_items = array_chunk($items,$this->page_size());
  319. $pages = count($arr_items);
  320. $page_no = $pages >= $this->page_no() ? $this->page_no() : $pages;
  321. return ($arr_items[$page_no - 1]);
  322. }
  323. private function pubKey()
  324. {
  325. static $pub = null;
  326. if($pub == null) {
  327. $pub_key = BASE_DATA_PATH . '/api/alipay/key/lrlz_public_key.pem';
  328. $key = file_get_contents($pub_key);
  329. $pub = openssl_get_publickey($key);
  330. }
  331. return $pub;
  332. }
  333. }
  334. class mobileHomeControl extends mobileControl
  335. {
  336. public function __construct()
  337. {
  338. parent::__construct();
  339. }
  340. }
  341. class mbMemberControl extends mobileControl
  342. {
  343. public $err_code = errcode::Success;
  344. public function __construct()
  345. {
  346. parent::__construct();
  347. if (!session_helper::logined()) {
  348. throw new UnloginException();
  349. }
  350. }
  351. }
  352. function bonus_version()
  353. {
  354. return "v=2019012101";
  355. }
  356. function shop_version()
  357. {
  358. return "v=2019012101";
  359. }