control.php 11 KB

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