control.php 11 KB

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