Index.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Hook;
  4. use think\Db;
  5. use Exception;
  6. use ip_limit\IpLocationQuery;
  7. class Index extends Base
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function login()
  14. {
  15. if (Request()->isPost()) {
  16. $data = input('post.');
  17. $res = model('Admin')->login($data);
  18. if ($res['code'] > 1) {
  19. return $this->error($res['msg']);
  20. }
  21. return $this->success($res['msg']);
  22. }
  23. Hook::listen("admin_login_init", $this->request);
  24. return $this->fetch('admin@index/login');
  25. }
  26. public function logout()
  27. {
  28. $res = model('Admin')->logout();
  29. $this->redirect('index/login');
  30. }
  31. public function index()
  32. {
  33. $menus = @include MAC_ADMIN_COMM . 'auth.php';
  34. $version = config('version');
  35. foreach ($menus as $k1 => $v1) {
  36. foreach ($v1['sub'] as $k2 => $v2) {
  37. if ($v2['show'] == 1) {
  38. if (strpos($v2['action'], 'javascript') !== false) {
  39. $url = $v2['action'];
  40. } else {
  41. $url = url('admin/' . $v2['controller'] . '/' . $v2['action']);
  42. }
  43. if (!empty($v2['param'])) {
  44. $url .= '?' . $v2['param'];
  45. }
  46. if ($this->check_auth($v2['controller'], $v2['action'])) {
  47. $menus[$k1]['sub'][$k2]['url'] = $url;
  48. } else {
  49. unset($menus[$k1]['sub'][$k2]);
  50. }
  51. } else {
  52. unset($menus[$k1]['sub'][$k2]);
  53. }
  54. }
  55. if (empty($menus[$k1]['sub'])) {
  56. unset($menus[$k1]);
  57. }
  58. }
  59. $quickmenu = config('quickmenu');
  60. if (empty($quickmenu)) {
  61. $quickmenu = mac_read_file(APP_PATH . 'data/config/quickmenu.txt');
  62. $quickmenu = explode(chr(13), $quickmenu);
  63. }
  64. if (!empty($quickmenu)) {
  65. $menus[1]['sub'][13] = ['name' => lang('admin/index/quick_tit'), 'url' => 'javascript:void(0);return false;', 'controller' => '', 'action' => ''];
  66. foreach ($quickmenu as $k => $v) {
  67. if (empty($v)) {
  68. continue;
  69. }
  70. $one = explode(',', trim($v));
  71. if (substr($one[1], 0, 4) == 'http' || substr($one[1], 0, 2) == '//') {
  72. } elseif (substr($one[1], 0, 1) == '/') {
  73. } elseif (strpos($one[1], '###') !== false || strpos($one[1], 'javascript:') !== false) {
  74. } else {
  75. $one[1] = url($one[1]);
  76. }
  77. $menus[1]['sub'][14 + $k] = ['name' => $one[0], 'url' => $one[1], 'controller' => '', 'action' => ''];
  78. }
  79. }
  80. $langs = glob('./application/lang/*.php');
  81. foreach ($langs as $k => &$v) {
  82. $v = str_replace(['./application/lang/','.php'],['',''],$v);
  83. }
  84. $config = config('maccms');
  85. $this->assign('config', $config);
  86. $this->assign('langs', $langs);
  87. $this->assign('version', $version);
  88. $this->assign('menus', $menus);
  89. $this->assign('title', lang('admin/index/title'));
  90. $ipQuery = new IpLocationQuery();
  91. $country_code = $ipQuery->queryProvince(mac_get_client_ip());
  92. if($country_code == ""){
  93. $country_code = "其它";
  94. }
  95. $this->assign('ip_location', $country_code);
  96. return $this->fetch('admin@index/index');
  97. }
  98. public function welcome()
  99. {
  100. $version = config('version');
  101. $update_sql = file_exists('./application/data/update/database.php');
  102. $this->assign('spider_data', $this->botlist());
  103. $this->assign('os_data', $this->get_system_status());
  104. $this->assign('version', $version);
  105. $this->assign('update_sql', $update_sql);
  106. $this->assign('mac_lang', config('default_lang'));
  107. $this->assign('dashboard_data', $this->getAdminDashboardData());
  108. $this->assign('admin', $this->_admin);
  109. $this->assign('title', lang('admin/index/welcome/title'));
  110. return $this->fetch('admin@index/welcome');
  111. }
  112. public function quickmenu()
  113. {
  114. if (Request()->isPost()) {
  115. $param = input();
  116. $validate = \think\Loader::validate('Token');
  117. if (!$validate->check($param)) {
  118. return $this->error($validate->getError());
  119. }
  120. $quickmenu = input('post.quickmenu');
  121. $quickmenu = str_replace(chr(10), '', $quickmenu);
  122. $menu_arr = explode(chr(13), $quickmenu);
  123. $res = mac_arr2file(APP_PATH . 'extra/quickmenu.php', $menu_arr);
  124. if ($res === false) {
  125. return $this->error(lang('save_err'));
  126. }
  127. return $this->success(lang('save_ok'));
  128. } else {
  129. $config_menu = config('quickmenu');
  130. if (empty($config_menu)) {
  131. $quickmenu = mac_read_file(APP_PATH . 'data/config/quickmenu.txt');
  132. } else {
  133. $quickmenu = array_values($config_menu);
  134. $quickmenu = join(chr(13), $quickmenu);
  135. }
  136. $this->assign('quickmenu', $quickmenu);
  137. $this->assign('title', lang('admin/index/quickmenu/title'));
  138. return $this->fetch('admin@index/quickmenu');
  139. }
  140. }
  141. public function checkcache()
  142. {
  143. $res = 'no';
  144. $r = cache('cache_data');
  145. if ($r == '1') {
  146. $res = 'haved';
  147. }
  148. echo $res;
  149. }
  150. public function clear()
  151. {
  152. $res = $this->_cache_clear();
  153. //运行缓存
  154. if (!$res) {
  155. $this->error(lang('admin/index/clear_err'));
  156. }
  157. // 搜索缓存结果清理
  158. model('VodSearch')->clearOldResult(true);
  159. return $this->success(lang('admin/index/clear_ok'));
  160. }
  161. public function iframe()
  162. {
  163. $val = input('post.val', 0);
  164. if ($val != 0 && $val != 1) {
  165. return $this->error(lang('admin/index/clear_ok'));
  166. }
  167. if ($val == 1) {
  168. cookie('is_iframe', 'yes');
  169. } else {
  170. cookie('is_iframe', null);
  171. }
  172. return $this->success(lang('admin/index/iframe'));
  173. }
  174. public function unlocked()
  175. {
  176. $param = input();
  177. $password = $param['password'];
  178. if ($this->_admin['admin_pwd'] != md5($password)) {
  179. return $this->error(lang('admin/index/pass_err'));
  180. }
  181. return $this->success(lang('admin/index/unlock_ok'));
  182. }
  183. public function check_back_link()
  184. {
  185. $param = input();
  186. $res = mac_check_back_link($param['url']);
  187. return json($res);
  188. }
  189. public function select()
  190. {
  191. $param = input();
  192. $tpl = $param['tpl'];
  193. $tab = $param['tab'];
  194. $col = $param['col'];
  195. $ids = $param['ids'];
  196. $url = $param['url'];
  197. $val = $param['val'];
  198. $refresh = $param['refresh'];
  199. if (empty($tpl) || empty($tab) || empty($col) || empty($ids) || empty($url)) {
  200. return $this->error(lang('param_err'));
  201. }
  202. if (is_array($ids)) {
  203. $ids = join(',', $ids);
  204. }
  205. if (empty($refresh)) {
  206. $refresh = 'yes';
  207. }
  208. $url = url($url);
  209. $mid = 1;
  210. if ($tab == 'art') {
  211. $mid = 2;
  212. } elseif ($tab == 'actor') {
  213. $mid = 8;
  214. } elseif ($tab == 'website') {
  215. $mid = 11;
  216. } elseif ($tab == 'manga') {
  217. $mid = 12;
  218. }
  219. $this->assign('mid', $mid);
  220. if ($tpl == 'select_type') {
  221. $type_tree = model('Type')->getCache('type_tree');
  222. $this->assign('type_tree', $type_tree);
  223. } elseif ($tpl == 'select_level') {
  224. $level_list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  225. $this->assign('level_list', $level_list);
  226. }
  227. $this->assign('refresh', $refresh);
  228. $this->assign('url', $url);
  229. $this->assign('tab', $tab);
  230. $this->assign('col', $col);
  231. $this->assign('ids', $ids);
  232. $this->assign('val', $val);
  233. return $this->fetch('admin@public/' . $tpl);
  234. }
  235. public function get_system_status()
  236. {
  237. //判斷系統
  238. $os_name = PHP_OS;
  239. $os_data = [];
  240. $os_data['os_name'] = $os_name;
  241. if (strtoupper(substr($os_name, 0, 3)) === 'WIN') {
  242. // Windows系统
  243. $os_data['os_name'] = 'WINDOWS';
  244. $os_data['disk_datas'] = $this->get_spec_disk('all');
  245. $os_data['cpu_usage'] = $this->getCpuUsage();
  246. $mem_arr = $this->getMemoryUsage();
  247. $os_data['mem_usage'] = $mem_arr['usage'];
  248. $os_data['mem_total'] = round($mem_arr['TotalVisibleMemorySize'] / 1024, 2);
  249. $os_data['mem_used'] = $os_data['mem_total'] - round($mem_arr['FreePhysicalMemory'] / 1024, 2);
  250. } else {
  251. // Linux和FreeBSD的处理逻辑
  252. $os_data['os_name'] = strtoupper($os_name);
  253. // 获取磁盘信息
  254. $totalSpace = 0;
  255. $freeSpace = 0;
  256. if (function_exists('disk_total_space') && function_exists('disk_free_space')) {
  257. $totalSpace = @disk_total_space('/');
  258. $freeSpace = @disk_free_space('/');
  259. }
  260. if ($totalSpace > 0) {
  261. $totalSpaceGB = round($totalSpace / (1024 * 1024 * 1024), 2);
  262. $freeSpaceGB = round($freeSpace / (1024 * 1024 * 1024), 2);
  263. $usedSpaceGB = round($totalSpaceGB - $freeSpaceGB, 2);
  264. $tmp_disk_data = [];
  265. $tmp_disk_data[0] = $usedSpaceGB;
  266. $tmp_disk_data[1] = $totalSpaceGB;
  267. $tmp_disk_data[2] = round(100 - ($freeSpaceGB / $totalSpaceGB * 100), 2);
  268. $os_data['disk_datas']['/'] = $tmp_disk_data;
  269. } else {
  270. $os_data['disk_datas']['/'] = [0, 0, 0];
  271. }
  272. // 获取内存和CPU信息
  273. $mem_arr = $this->get_unix_server_memory_usage();
  274. $os_data['cpu_usage'] = $this->get_unix_server_cpu_usage();
  275. $os_data['mem_usage'] = $mem_arr['usage'];
  276. $os_data['mem_used'] = $mem_arr['used'];
  277. $os_data['mem_total'] = $mem_arr['total'];
  278. }
  279. return $os_data;
  280. }
  281. private function byte_format($size, $dec = 2)
  282. {
  283. if ($size == 0) {
  284. return "0 B";
  285. }
  286. $a = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  287. $pos = 0;
  288. while ($size >= 1024) {
  289. $size /= 1024;
  290. $pos++;
  291. }
  292. return round($size, $dec);
  293. }
  294. private function get_disk_space($letter)
  295. {
  296. // 添加函数存在性检查
  297. if (!function_exists('disk_total_space') || !function_exists('disk_free_space')) {
  298. return [];
  299. }
  300. // 获取磁盘信息
  301. $diskct = 0;
  302. $disk = array();
  303. $diskz = 0; //磁盘总容量
  304. $diskk = 0; //磁盘剩余容量
  305. $is_disk = $letter . ':';
  306. if (@\disk_total_space($is_disk) != NULL) {
  307. $diskct++;
  308. $total_space = @\disk_total_space($is_disk);
  309. $free_space = @\disk_free_space($is_disk);
  310. // 转换为GB并保留两位小数
  311. $total_space_gb = round($total_space / (1024 * 1024 * 1024), 2);
  312. $free_space_gb = round($free_space / (1024 * 1024 * 1024), 2);
  313. $disk[$letter][0] = round($this->byte_format($free_space), 2);
  314. $disk[$letter][1] = round($this->byte_format($total_space), 2);
  315. if ($total_space > 0) {
  316. $disk[$letter][2] = round(100 - ($free_space_gb / $total_space_gb * 100), 2);
  317. } else {
  318. $disk[$letter][2] = 0;
  319. }
  320. $diskk += round($this->byte_format($free_space), 2);
  321. $diskz += round($this->byte_format($total_space), 2);
  322. }
  323. return $disk;
  324. }
  325. private function get_spec_disk($type = 'system')
  326. {
  327. $disk = array();
  328. switch ($type) {
  329. case 'system':
  330. //strrev(array_pop(explode(':',strrev(getenv_info('SystemRoot')))));//取得系统盘符
  331. $disk = $this->get_disk_space(strrev(array_pop(explode(':', strrev(getenv('SystemRoot'))))));
  332. break;
  333. case 'all':
  334. foreach (range('b', 'z') as $letter) {
  335. $disk = array_merge($disk, $this->get_disk_space(strtoupper($letter)));
  336. }
  337. break;
  338. default:
  339. $disk = $this->get_disk_space($type);
  340. break;
  341. }
  342. return $disk;
  343. }
  344. private function isComAvailable()
  345. {
  346. return extension_loaded('com_dotnet');
  347. }
  348. private function getCpuUsage()
  349. {
  350. if (!$this->isComAvailable()) {
  351. if (!function_exists('shell_exec')) {
  352. return 0;
  353. }
  354. try {
  355. $cmd = "wmic cpu get loadpercentage";
  356. $output = shell_exec($cmd);
  357. if ($output) {
  358. preg_match('/\d+/', $output, $matches);
  359. if (isset($matches[0])) {
  360. return (float)$matches[0];
  361. }
  362. }
  363. } catch (Exception $e) {
  364. return 0;
  365. }
  366. return 0;
  367. }
  368. try {
  369. if(class_exists('COM')){
  370. $wmi = new \COM('WinMgmts:\\\\.');
  371. $cpus = $wmi->ExecQuery('SELECT LoadPercentage FROM Win32_Processor');
  372. } else {
  373. return 0;
  374. }
  375. $cpu_load = 0;
  376. $cpu_count = 0;
  377. foreach ($cpus as $cpu) {
  378. $cpu_load += $cpu->LoadPercentage;
  379. $cpu_count++;
  380. }
  381. return $cpu_count > 0 ? round($cpu_load / $cpu_count, 2) : 0;
  382. } catch (Exception $e) {
  383. if (!function_exists('shell_exec')) {
  384. return 0;
  385. }
  386. try {
  387. $cmd = "wmic cpu get loadpercentage";
  388. $output = shell_exec($cmd);
  389. if ($output) {
  390. preg_match('/\d+/', $output, $matches);
  391. if (isset($matches[0])) {
  392. return (float)$matches[0];
  393. }
  394. }
  395. } catch (Exception $e) {
  396. return 0;
  397. }
  398. return 0;
  399. }
  400. }
  401. private function getMemoryUsage()
  402. {
  403. if (!$this->isComAvailable()) {
  404. if (!function_exists('shell_exec')) {
  405. return [
  406. 'TotalVisibleMemorySize' => 0,
  407. 'FreePhysicalMemory' => 0,
  408. 'usage' => 0
  409. ];
  410. }
  411. try {
  412. $cmd = "wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /Value";
  413. $output = shell_exec($cmd);
  414. if ($output) {
  415. preg_match('/TotalVisibleMemorySize=(\d+)/i', $output, $total);
  416. preg_match('/FreePhysicalMemory=(\d+)/i', $output, $free);
  417. if (isset($total[1]) && isset($free[1])) {
  418. $total_mem = (float)$total[1];
  419. $free_mem = (float)$free[1];
  420. $used_mem = $total_mem - $free_mem;
  421. return [
  422. 'TotalVisibleMemorySize' => $total_mem,
  423. 'FreePhysicalMemory' => $free_mem,
  424. 'usage' => $total_mem > 0 ? round(($used_mem / $total_mem) * 100, 2) : 0
  425. ];
  426. }
  427. }
  428. } catch (Exception $e) {
  429. return [
  430. 'TotalVisibleMemorySize' => 0,
  431. 'FreePhysicalMemory' => 0,
  432. 'usage' => 0
  433. ];
  434. }
  435. }
  436. try {
  437. if(class_exists('COM')){
  438. $wmi = new \COM('WinMgmts:\\\\.');
  439. $os = $wmi->ExecQuery('SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem');
  440. } else {
  441. return [
  442. 'TotalVisibleMemorySize' => 0,
  443. 'FreePhysicalMemory' => 0,
  444. 'usage' => 0
  445. ];
  446. }
  447. foreach ($os as $item) {
  448. $total = $item->TotalVisibleMemorySize;
  449. $free = $item->FreePhysicalMemory;
  450. $used = $total - $free;
  451. return [
  452. 'TotalVisibleMemorySize' => $total,
  453. 'FreePhysicalMemory' => $free,
  454. 'usage' => $total > 0 ? round(($used / $total) * 100, 2) : 0
  455. ];
  456. }
  457. } catch (Exception $e) {
  458. if (!function_exists('shell_exec')) {
  459. return [
  460. 'TotalVisibleMemorySize' => 0,
  461. 'FreePhysicalMemory' => 0,
  462. 'usage' => 0
  463. ];
  464. }
  465. try {
  466. $cmd = "wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /Value";
  467. $output = shell_exec($cmd);
  468. if ($output) {
  469. preg_match('/TotalVisibleMemorySize=(\d+)/i', $output, $total);
  470. preg_match('/FreePhysicalMemory=(\d+)/i', $output, $free);
  471. if (isset($total[1]) && isset($free[1])) {
  472. $total_mem = (float)$total[1];
  473. $free_mem = (float)$free[1];
  474. $used_mem = $total_mem - $free_mem;
  475. return [
  476. 'TotalVisibleMemorySize' => $total_mem,
  477. 'FreePhysicalMemory' => $free_mem,
  478. 'usage' => $total_mem > 0 ? round(($used_mem / $total_mem) * 100, 2) : 0
  479. ];
  480. }
  481. }
  482. } catch (Exception $e) {
  483. return [
  484. 'TotalVisibleMemorySize' => 0,
  485. 'FreePhysicalMemory' => 0,
  486. 'usage' => 0
  487. ];
  488. }
  489. }
  490. }
  491. private function get_unix_server_memory_usage()
  492. {
  493. // 首先尝试使用通用的 sysinfo 方法
  494. if (function_exists('sysinfo')) {
  495. $si = sysinfo();
  496. if ($si) {
  497. $total_mem = $si['totalram'] * $si['mem_unit'] / 1024 / 1024;
  498. $free_mem = $si['freeram'] * $si['mem_unit'] / 1024 / 1024;
  499. $used_mem = $total_mem - $free_mem;
  500. $usage = ($total_mem > 0) ? ($used_mem / $total_mem) * 100 : 0;
  501. return [
  502. 'total' => round($total_mem, 2),
  503. 'used' => round($used_mem, 2),
  504. 'usage' => round($usage, 2)
  505. ];
  506. }
  507. }
  508. // 尝试不同的内存信息获取方法
  509. $methods = [
  510. // Linux free 命令
  511. 'free' => function() {
  512. if (!function_exists('shell_exec')) {
  513. return null;
  514. }
  515. $free = shell_exec('free');
  516. if ($free) {
  517. $free = (string)trim($free);
  518. $free_arr = explode("\n", $free);
  519. $mem = explode(" ", $free_arr[1]);
  520. $mem = array_filter($mem);
  521. $mem = array_merge($mem);
  522. if (count($mem) >= 3 && !empty($mem[1])) {
  523. return [
  524. 'total' => round($mem[1] / 1024, 2),
  525. 'used' => round($mem[2] / 1024, 2),
  526. 'usage' => round($mem[2] / $mem[1] * 100, 2)
  527. ];
  528. }
  529. }
  530. return null;
  531. },
  532. // FreeBSD/Unix sysctl 命令
  533. 'sysctl' => function() {
  534. if (!function_exists('shell_exec')) {
  535. return null;
  536. }
  537. $sysctl = shell_exec('/sbin/sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_free_count 2>/dev/null');
  538. if ($sysctl) {
  539. $lines = explode("\n", trim($sysctl));
  540. if (count($lines) >= 3 && !empty($lines[0])) {
  541. $total_mem = $lines[0] / 1024 / 1024;
  542. $page_size = $lines[1];
  543. $free_pages = $lines[2];
  544. $free_mem = ($free_pages * $page_size) / 1024 / 1024;
  545. $used_mem = $total_mem - $free_mem;
  546. $usage = $total_mem > 0 ? ($used_mem / $total_mem) * 100 : 0;
  547. return [
  548. 'total' => round($total_mem, 2),
  549. 'used' => round($used_mem, 2),
  550. 'usage' => round($usage, 2)
  551. ];
  552. }
  553. }
  554. return null;
  555. },
  556. // /proc/meminfo 文件读取
  557. 'proc' => function() {
  558. if (!is_readable('/proc/meminfo')) {
  559. return null;
  560. }
  561. $meminfo = file_get_contents('/proc/meminfo');
  562. if ($meminfo) {
  563. preg_match('/MemTotal:\s+(\d+)/i', $meminfo, $total);
  564. preg_match('/MemFree:\s+(\d+)/i', $meminfo, $free);
  565. preg_match('/Cached:\s+(\d+)/i', $meminfo, $cached);
  566. preg_match('/Buffers:\s+(\d+)/i', $meminfo, $buffers);
  567. if (isset($total[1]) && isset($free[1])) {
  568. $total_mem = $total[1] / 1024;
  569. $free_mem = ($free[1] + (isset($cached[1]) ? $cached[1] : 0) + (isset($buffers[1]) ? $buffers[1] : 0)) / 1024;
  570. $used_mem = $total_mem - $free_mem;
  571. $usage = ($total_mem > 0) ? ($used_mem / $total_mem) * 100 : 0;
  572. return [
  573. 'total' => round($total_mem, 2),
  574. 'used' => round($used_mem, 2),
  575. 'usage' => round($usage, 2)
  576. ];
  577. }
  578. }
  579. return null;
  580. }
  581. ];
  582. // 依次尝试各种方法
  583. foreach ($methods as $method) {
  584. $result = $method();
  585. if ($result !== null) {
  586. return $result;
  587. }
  588. }
  589. // 如果所有方法都失败,返回默认值
  590. return [
  591. 'total' => 0,
  592. 'used' => 0,
  593. 'usage' => 0
  594. ];
  595. }
  596. private function get_unix_server_cpu_usage()
  597. {
  598. // 首先尝试使用通用的方法
  599. $methods = [
  600. // top 命令 (Linux)
  601. 'top_linux' => function() {
  602. if (!function_exists('shell_exec')) {
  603. return null;
  604. }
  605. $cpu_load = shell_exec("top -bn1 | grep 'Cpu(s)' 2>/dev/null");
  606. if (!empty($cpu_load)) {
  607. if (preg_match('/(\d+[.,]\d+).*?us/', $cpu_load, $matches)) {
  608. return round((float)str_replace(',', '.', $matches[1]), 2);
  609. }
  610. }
  611. return null;
  612. },
  613. // top 命令 (FreeBSD)
  614. 'top_bsd' => function() {
  615. if (!function_exists('shell_exec')) {
  616. return null;
  617. }
  618. $cpu_load = shell_exec("top -d2 -n1 | grep 'CPU:' 2>/dev/null");
  619. if (!empty($cpu_load)) {
  620. if (preg_match('/(\d+\.\d+)%\s+user/', $cpu_load, $matches)) {
  621. return round((float)$matches[1], 2);
  622. }
  623. }
  624. return null;
  625. },
  626. // sysctl (FreeBSD/Unix)
  627. 'sysctl' => function() {
  628. if (!function_exists('shell_exec')) {
  629. return null;
  630. }
  631. $cpu_load = shell_exec('/sbin/sysctl -n kern.cp_time 2>/dev/null');
  632. if (!empty($cpu_load)) {
  633. $times = explode(" ", trim($cpu_load));
  634. if (count($times) >= 5) {
  635. $total = array_sum($times);
  636. $idle = $times[4];
  637. return $total > 0 ? round(100 - ($idle * 100 / $total), 2) : 0;
  638. }
  639. }
  640. return null;
  641. },
  642. // /proc/stat 文件读取
  643. 'proc' => function() {
  644. if (is_readable('/proc/stat')) {
  645. $stats1 = file_get_contents('/proc/stat');
  646. usleep(100000); // 等待100ms
  647. $stats2 = file_get_contents('/proc/stat');
  648. if ($stats1 && $stats2) {
  649. $stats_arr1 = explode(' ', trim(explode("\n", $stats1)[0]));
  650. $stats_arr2 = explode(' ', trim(explode("\n", $stats2)[0]));
  651. if (count($stats_arr1) >= 5 && count($stats_arr2) >= 5) {
  652. $cpu1 = array_sum(array_slice($stats_arr1, 1));
  653. $cpu2 = array_sum(array_slice($stats_arr2, 1));
  654. $idle1 = $stats_arr1[4];
  655. $idle2 = $stats_arr2[4];
  656. $diff_cpu = $cpu2 - $cpu1;
  657. $diff_idle = $idle2 - $idle1;
  658. if ($diff_cpu > 0) {
  659. return round(100 * (1 - $diff_idle / $diff_cpu), 2);
  660. }
  661. }
  662. }
  663. }
  664. return null;
  665. }
  666. ];
  667. // 依次尝试各种方法
  668. foreach ($methods as $method) {
  669. $result = $method();
  670. if ($result !== null) {
  671. return $result;
  672. }
  673. }
  674. return 0;
  675. }
  676. private function _getServerLoadLinuxData()
  677. {
  678. if (is_readable("/proc/stat")) {
  679. $stats = @file_get_contents("/proc/stat");
  680. if ($stats !== false) {
  681. // Remove double spaces to make it easier to extract values with explode()
  682. $stats = preg_replace("/[[:blank:]]+/", " ", $stats);
  683. // Separate lines
  684. $stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
  685. $stats = explode("\n", $stats);
  686. // Separate values and find line for main CPU load
  687. foreach ($stats as $statLine) {
  688. $statLineData = explode(" ", trim($statLine));
  689. // Found!
  690. if (
  691. (count($statLineData) >= 5) &&
  692. ($statLineData[0] == "cpu")
  693. ) {
  694. return array(
  695. $statLineData[1],
  696. $statLineData[2],
  697. $statLineData[3],
  698. $statLineData[4],
  699. );
  700. }
  701. }
  702. }
  703. }
  704. return null;
  705. }
  706. private function getAdminDashboardData()
  707. {
  708. $result = [];
  709. //已注册总用户数量
  710. $result['user_count'] = model('User')->count();
  711. $result['user_count'] = number_format($result['user_count'], 0, '.', ',');
  712. //已审核用户数量
  713. $result['user_active_count'] = model('User')->where('user_status', 1)->count();
  714. $result['user_active_count'] = number_format($result['user_active_count'], 0, '.', ',');
  715. $today_start = strtotime(date('Y-m-d 00:00:00'));
  716. $today_end = $today_start + 86399;
  717. //本日来客量
  718. $result['today_visit_count'] = model('Visit')->where('visit_time', 'between', $today_start . ',' . $today_end)->count();
  719. $result['today_visit_count'] = number_format($result['today_visit_count'], 0, '.', ',');
  720. //本日总入金
  721. $result['today_money_get'] = model('Order')->where('order_time', 'between', $today_start . ',' . $today_end)->where('order_status', 1)->sum('order_price');
  722. $result['today_money_get'] = number_format($result['today_money_get'], 2, '.', ',');
  723. //前七天 每日用户访问数
  724. $tmp_arr = Db::query("select FROM_UNIXTIME(visit_time, '%Y-%c-%d' ) days,count(*) count from (SELECT * from ".config('database.prefix')."visit where visit_time >= (unix_timestamp(CURDATE())-604800)) as temp group by days");
  725. $result['seven_day_visit_day'] = [];
  726. $result['seven_day_visit_count'] = [];
  727. $result['raise_visit_user_today'] = 0;
  728. if (is_array($tmp_arr) && count($tmp_arr) > 1 && (strtotime(end($tmp_arr)['days']) == strtotime(date('Y-m-d')))) {
  729. $yesterday_visit_count = $tmp_arr[count($tmp_arr) - 2]['count'];
  730. $lastday_visit_count = end($tmp_arr)['count'];
  731. if ($yesterday_visit_count != 0) {
  732. $result['raise_visit_user_today'] = number_format((($lastday_visit_count - $yesterday_visit_count) / $yesterday_visit_count) * 100, 2, '.', ',');
  733. } else {
  734. $result['raise_visit_user_today'] = 0;
  735. }
  736. }
  737. foreach ($tmp_arr as $data) {
  738. array_push($result['seven_day_visit_day'], $data['days']);
  739. array_push($result['seven_day_visit_count'], $data['count']);
  740. }
  741. //近七日用户访问总量
  742. $result['seven_day_visit_total_count'] = 0;
  743. foreach ($result['seven_day_visit_data'] as $k => $value) {
  744. $result['seven_day_visit_total_count'] = $result['seven_day_visit_total_count'] + $value['count'];
  745. }
  746. $result['seven_day_visit_total_count'] = number_format($result['seven_day_visit_total_count'], 0, '.', ',');
  747. //前七天 每日用户注册数
  748. $result['seven_day_reg_data'] = Db::query("select FROM_UNIXTIME(user_reg_time, '%Y-%c-%d' ) days,count(*) count from (SELECT * from ".config('database.prefix')."user where user_reg_time >= (unix_timestamp(CURDATE())-604800)) as tmp group by days");
  749. //近七日用户注册总量
  750. $result['seven_day_reg_total_count'] = 0;
  751. $result['seven_day_reg_day'] = [];
  752. $result['seven_day_reg_count'] = [];
  753. foreach ($result['seven_day_reg_data'] as $k => $value) {
  754. array_push($result['seven_day_reg_day'], $value['days']);
  755. array_push($result['seven_day_reg_count'], $value['count']);
  756. $result['seven_day_reg_total_count'] = $result['seven_day_reg_total_count'] + $value['count'];
  757. }
  758. //比較前一天的註冊量漲幅
  759. $result['raise_reg_user_today'] = 0;
  760. if (is_array($result['seven_day_reg_data']) && count($result['seven_day_reg_data']) > 1 && (strtotime(end($result['seven_day_reg_data'])['days']) == strtotime(date('Y-m-d')))) {
  761. $yesterday_reg_count = $result['seven_day_reg_data'][count($result['seven_day_reg_data']) - 2]['count'];
  762. $lastday_reg_count = end($result['seven_day_reg_data'])['count'];
  763. if ($yesterday_reg_count != 0) {
  764. $result['raise_reg_user_today'] = number_format((($lastday_reg_count - $yesterday_reg_count) / $yesterday_reg_count) * 100, 2, '.', ',');
  765. } else {
  766. $result['raise_reg_user_today'] = 0;
  767. }
  768. }
  769. $result['seven_day_reg_total_count'] = number_format($result['seven_day_reg_total_count'], 0, '.', ',');
  770. return $result;
  771. }
  772. public function rangeDateDailyVisit()
  773. {
  774. $range_daily_visit_data = Db::query("select FROM_UNIXTIME(visit_time, '%Y-%c-%d' ) days,count(*) count from (SELECT * from ".config('database.prefix')."visit where visit_time >= " . strtotime($_POST['startDate']) . "&& visit_time <= " . strtotime($_POST['endDate']) . " ) as temp group by days");
  775. $result = [];
  776. $range_visit_day = [];
  777. $range_visit_count = [];
  778. $range_visit_sum = 0;
  779. foreach ($range_daily_visit_data as $data) {
  780. $range_visit_sum = $range_visit_sum + $data['count'];
  781. array_push($range_visit_day, $data['days']);
  782. array_push($range_visit_count, $data['count']);
  783. }
  784. $result['days'] = $range_visit_day;
  785. $result['count'] = $range_visit_count;
  786. $result['sum'] = $range_visit_sum;
  787. return json_encode($result);
  788. }
  789. public function botlist()
  790. {
  791. $day_arr = [];
  792. //列出最近10天的日期
  793. for ($i = 0; $i < 7; $i++) {
  794. $day_arr[$i] = date('Y-m-d', time() - $i * 60 * 60 * 24);
  795. }
  796. $google_arr = [];
  797. $baidu_arr = [];
  798. $sogou_arr = [];
  799. $soso_arr = [];
  800. $yahoo_arr = [];
  801. $msn_arr = [];
  802. $msn_bot_arr = [];
  803. $sohu_arr = [];
  804. $yodao_arr = [];
  805. $twiceler_arr = [];
  806. $alexa_arr = [];
  807. $bot_list = [];
  808. foreach ($day_arr as $day_vo) {
  809. if (file_exists(ROOT_PATH . 'runtime/log/bot/' . $day_vo . '.txt')) {
  810. $bot_content = file_get_contents(ROOT_PATH . 'runtime/log/bot/' . $day_vo . '.txt');
  811. } else {
  812. $bot_content = '';
  813. }
  814. $google_arr[$day_vo] = substr_count($bot_content, 'Google');
  815. $baidu_arr[$day_vo] = substr_count($bot_content, 'Baidu');
  816. $sogou_arr[$day_vo] = substr_count($bot_content, 'Sogou');
  817. $soso_arr[$day_vo] = substr_count($bot_content, 'SOSO');
  818. $yahoo_arr[$day_vo] = substr_count($bot_content, 'Yahoo');
  819. $msn_arr[$day_vo] = substr_count($bot_content, 'MSN');
  820. $msn_bot_arr[$day_vo] = substr_count($bot_content, 'msnbot');
  821. $sohu_arr[$day_vo] = substr_count($bot_content, 'Sohu');
  822. $yodao_arr[$day_vo] = substr_count($bot_content, 'Yodao');
  823. $twiceler_arr[$day_vo] = substr_count($bot_content, 'Twiceler');
  824. $alexa_arr[$day_vo] = substr_count($bot_content, 'Alexa');
  825. }
  826. $bot_list['Google']['key'] = array_keys($google_arr);
  827. $bot_list['Google']['values'] = array_values($google_arr);
  828. $bot_list['Baidu']['keys'] = array_keys($baidu_arr);
  829. $bot_list['Baidu']['values'] = array_values($baidu_arr);
  830. $bot_list['Sogou']['keys'] = array_keys($sogou_arr);
  831. $bot_list['Sogou']['values'] = array_values($sogou_arr);
  832. $bot_list['SOSO']['keys'] = array_keys($soso_arr);
  833. $bot_list['SOSO']['values'] = array_values($soso_arr);
  834. $bot_list['Yahoo']['keys'] = array_keys($yahoo_arr);
  835. $bot_list['Yahoo']['values'] = array_values($yahoo_arr);
  836. $bot_list['MSN']['keys'] = array_keys($msn_arr);
  837. $bot_list['MSN']['values'] = array_values($msn_arr);
  838. $bot_list['msnbot']['keys'] = array_keys($msn_bot_arr);
  839. $bot_list['msnbot']['values'] = array_values($msn_bot_arr);
  840. $bot_list['Sohu']['keys'] = array_keys($sohu_arr);
  841. $bot_list['Sohu']['values'] = array_values($sohu_arr);
  842. $bot_list['Yodao']['keys'] = array_keys($yodao_arr);
  843. $bot_list['Yodao']['values'] = array_values($yodao_arr);
  844. $bot_list['Twiceler']['keys'] = array_keys($twiceler_arr);
  845. $bot_list['Twiceler']['values'] = array_values($twiceler_arr);
  846. $bot_list['Alexa']['keys'] = array_keys($alexa_arr);
  847. $bot_list['Alexa']['values'] = array_values($alexa_arr);
  848. if (!empty($_POST['category'])) {
  849. return $bot_list[$_POST['category']];
  850. } else {
  851. return $bot_list;
  852. }
  853. }
  854. public function botlog()
  855. {
  856. $parm = input();
  857. $data = $parm['data'];
  858. $bot_content = file_get_contents(ROOT_PATH . 'runtime/log/bot/' . $data . '.txt');
  859. $bot_list = array_slice(array_reverse(explode("\r\n", trim($bot_content))), 0, 20);
  860. $this->assign('bot_list', $bot_list);
  861. return $this->fetch('admin@others/botlog');
  862. }
  863. }