BaseController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace crmeb\basic;
  3. abstract class BaseController
  4. {
  5. protected $request;
  6. protected $app;
  7. protected $batchValidate = false;
  8. protected $middleware = [];
  9. public function __construct(\think\facade\App $app)
  10. {
  11. $this->app = $app;
  12. $this->request = app('request');
  13. $this->initialize();
  14. }
  15. protected function initialize()
  16. {
  17. }
  18. final protected function validate(array $data, $validate, $message = NULL, bool $batch = false)
  19. {
  20. if (is_array($validate)) {
  21. $v = new \think\facade\Validate();
  22. $v->rule($validate);
  23. }
  24. else {
  25. if (strpos($validate, '.')) {
  26. list($validate, $scene) = explode('.', $validate);
  27. }
  28. $class = (false !== strpos($validate, '\\') ? $validate : $this->app->parseClass(__FUNCTION__, $validate));
  29. $v = new $class();
  30. if (!empty($scene)) {
  31. $v->scene($scene);
  32. }
  33. if (is_string($message) && empty($scene)) {
  34. $v->scene($message);
  35. }
  36. }
  37. if (is_array($message)) {
  38. $v->message($message);
  39. }
  40. if ($batch || $this->batchValidate) {
  41. $v->batch(true);
  42. }
  43. return $v->failException(true)->check($data);
  44. }
  45. final protected function success($msg = 'ok', ?array $data = NULL)
  46. {
  47. return app('json')->success($msg, $data);
  48. }
  49. final protected function fail($msg = __FUNCTION__, ?array $data = NULL)
  50. {
  51. return app('json')->fail($msg, $data);
  52. }
  53. final protected function make(int $status, string $msg, ?int $code = NULL, ?array $data = NULL)
  54. {
  55. $json = app('json');
  56. if ($code) {
  57. $json->code($code);
  58. }
  59. return $json->make($status, $msg, $data);
  60. }
  61. final protected function checkAuthDecrypt()
  62. {
  63. return $this->success('您得授权认证完成!', ['auth' => true]);
  64. try {
  65. $res = $this->authorizationDecryptCrmeb(true);
  66. if ($res && is_array($res)) {
  67. list($domain, $recordCode, $installtime) = $res;
  68. $time = $installtime + 99*366*34*3600;
  69. if ($time < time()) {
  70. return $this->success('您得授权已过期请及时前往CRMEB官方进行授权', ['auth' => false]);
  71. }
  72. else {
  73. $nowTime = ($time - time()) / 86400;
  74. return $this->success('您得授权证书还有' . (int) $nowTime . '天过期,请及时前往CREMB官方进行授权认证!', ['auth' => false]);
  75. }
  76. }
  77. else if ($res === true) {
  78. return $this->fail();
  79. }
  80. else {
  81. return $this->fail('授权文件读取错误');
  82. }
  83. }
  84. catch (\RuntimeException $e) {
  85. return $this->fail('授权文件读取错误');
  86. }
  87. }
  88. final private function authorizationDecryptCrmeb(bool $bool = false)
  89. {
  90. return true;
  91. $authorizationExtactPath = AUTHORIZATION_EXTACT;
  92. $authorizationExtacttext = AUTHORIZATION_TEXT;
  93. if (!$authorizationExtactPath || !is_file($authorizationExtactPath)) {
  94. throw new \RuntimeException('授权证书丢失', 42007);
  95. }
  96. if (!$authorizationExtacttext || !is_file($authorizationExtacttext)) {
  97. throw new \RuntimeException('授权文件丢书', 42006);
  98. }
  99. if ($authorizationExtactPath && $authorizationExtacttext) {
  100. $publicDecrypt = function(string $data, string $publicKey) {
  101. $decrypted = '';
  102. $pu_key = openssl_pkey_get_public(file_get_contents($publicKey));
  103. $plainData = str_split(base64_decode($data), 128);
  104. foreach ($plainData as $chunk) {
  105. $str = '';
  106. $decryptionOk = openssl_public_decrypt($chunk, $str, $pu_key);
  107. if ($decryptionOk === false) {
  108. return false;
  109. }
  110. $decrypted .= $str;
  111. }
  112. return $decrypted;
  113. };
  114. $encryptStr = file_get_contents($authorizationExtacttext);
  115. if (!$encryptStr) {
  116. throw new \RuntimeException('授权文件内容丢失', 42005);
  117. }
  118. $resArray = explode('==', $encryptStr);
  119. if (!is_array($resArray)) {
  120. throw new \RuntimeException('授权文件有变动无法解析', 42008);
  121. }
  122. else {
  123. list($encryptStr, $recordCode) = explode(',', $encryptStr);
  124. }
  125. if (!isset($recordCode)) {
  126. $recordCode = '';
  127. }
  128. $data = $publicDecrypt($encryptStr, $authorizationExtactPath);
  129. if ($data) {
  130. $data = json_decode($data);
  131. $installtime = @filectime(app()->getRootPath() . 'public' . DS . 'install' . DS . 'install.lock');
  132. if (isset($data->domain) && isset($data->expire) && isset($data->version)) {
  133. $res = time() <= $installtime + $data->expire;
  134. if ($res) {
  135. if ($bool && ($data->domain === '*')) {
  136. return [$data->domain, $recordCode, $installtime];
  137. }
  138. if (($data->domain === '*') || in_array(request()->host(), ['127.0.0.1', 'localhost'])) {
  139. return true;
  140. }
  141. else if ($data->domain === request()->host()) {
  142. return true;
  143. }
  144. else {
  145. throw new \RuntimeException('您的授权域名和访问域名不一致!', 42000);
  146. }
  147. }
  148. else {
  149. throw new \RuntimeException('您的授权已到期', 42001);
  150. }
  151. }
  152. }
  153. else {
  154. throw new \RuntimeException('授权文件有变动无法解析', 42003);
  155. }
  156. }
  157. throw new \RuntimeException('授权失败', 42004);
  158. }
  159. final protected function makePostForm(string $title, array $field, $url, string $method = 'POST')
  160. {
  161. try {
  162. $this->authorizationDecryptCrmeb();
  163. $form = \FormBuilder\Form::create((string) $url);
  164. $form->setMethod($method);
  165. $form->components($field);
  166. $form->setTitle($title);
  167. $rules = $form->getRules();
  168. $title = $form->getTitle();
  169. $action = $form->getAction();
  170. $method = $form->getMethod();
  171. $info = '';
  172. $status = true;
  173. return $this->success(compact('rules', 'title', 'action', 'method', 'info', 'status'));
  174. }
  175. catch (\Throwable $e) {
  176. $rules = [];
  177. $title = $e->getMessage();
  178. $info = '请联系CRMEB官方进行授权认证';
  179. $status = false;
  180. $action = '';
  181. $method = 'get';
  182. return $this->success(compact('rules', 'title', 'action', 'method', 'info', 'status'));
  183. }
  184. }
  185. final protected function attr_format($arr)
  186. {
  187. $data = [];
  188. $res = [];
  189. $count = count($arr);
  190. if (1 < $count) {
  191. for ($i = 0; $i < ($count - 1); $i++) {
  192. if ($i == 0) {
  193. $data = $arr[$i]['detail'];
  194. }
  195. $rep1 = [];
  196. foreach ($data as $v) {
  197. foreach ($arr[$i + 1]['detail'] as $g) {
  198. $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
  199. $tmp[] = $rep2;
  200. if ($i == $count - 2) {
  201. foreach (explode('-$-', $rep2) as $k => $h) {
  202. $rep3 = explode('_$_', $h);
  203. $rep4['detail'][$rep3[0]] = (isset($rep3[1]) ? $rep3[1] : '');
  204. }
  205. if ($count == count($rep4['detail'])) {
  206. $res[] = $rep4;
  207. }
  208. }
  209. }
  210. }
  211. $data = (isset($tmp) ? $tmp : []);
  212. }
  213. }
  214. else {
  215. $dataArr = [];
  216. foreach ($arr as $k => $v) {
  217. foreach ($v['detail'] as $kk => $vv) {
  218. $dataArr[$kk] = $v['value'] . '_' . $vv;
  219. $res[$kk]['detail'][$v['value']] = $vv;
  220. }
  221. }
  222. $data[] = implode('-', $dataArr);
  223. }
  224. return [$data, $res];
  225. }
  226. final protected function getAuth()
  227. {
  228. try {
  229. $auth = $this->authorizationDecryptCrmeb();
  230. }
  231. catch (\RuntimeException $e) {
  232. $auth = false;
  233. }
  234. if (!$auth) {
  235. $res = \crmeb\services\HttpService::postRequest('http://shop.crmeb.net/auth/business/auth', ['domain' => 'pro.lfmn.fun', 'version' => get_crmeb_version()]);
  236. if ($res !== false) {
  237. $res = json_decode($res, true);
  238. if (isset($res['code']) && ($res['code'] == 200) && isset($res['data']) && $res['data']) {
  239. $dataContent = $res['data'];
  240. $res = file_put_contents(app()->getRootPath() . 'ZEuduEXx9em36aYgTGvhQIq.txt', $dataContent['auto_content'] . ',' . $dataContent['auth_code']);
  241. return $this->success(['auth_code' => $dataContent['auth_code'], 'auth' => true]);
  242. }
  243. }
  244. return $this->fail();
  245. }
  246. else {
  247. $encryptStr = file_get_contents(AUTHORIZATION_TEXT);
  248. if (!$encryptStr) {
  249. throw new \RuntimeException('授权文件内容丢失', 42005);
  250. }
  251. $resArray = explode('==', $encryptStr);
  252. if (!is_array($resArray)) {
  253. throw new \RuntimeException('授权文件有变动无法解析', 42008);
  254. }
  255. else {
  256. list($encryptStr, $recordCode) = explode(',', $encryptStr);
  257. }
  258. $recordCode = $recordCode ?? false;
  259. if ($recordCode) {
  260. if ($recordCode == '00000000') {
  261. return $this->fail();
  262. }
  263. return $this->success(['auth_code' => $recordCode, 'auth' => true]);
  264. }
  265. return $this->fail();
  266. }
  267. }
  268. }
  269. ?>