SqlManageController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022-03-10
  8. * Time: 10:26
  9. */
  10. namespace app\backend\modules\setting\controllers;
  11. use app\common\components\BaseController;
  12. use app\common\models\SqlInstallLog;
  13. class SqlManageController extends BaseController
  14. {
  15. public function index()
  16. {
  17. $log = SqlInstallLog::first();
  18. $is_install = 0;
  19. $access_path = '';
  20. if ($log) {
  21. $access_url_len = strlen(base_path());
  22. $access_path = request()->getSchemeAndHttpHost().'/'.substr($log->path, $access_url_len+1).'/phpmyadmin';
  23. if (config('app.framework') != 'platform') {
  24. $access_path = request()->getSchemeAndHttpHost().'/addons/yun_shop/'.substr($log->path, $access_url_len+1).'/phpmyadmin';
  25. }
  26. $is_install = 1;
  27. }
  28. $list = [
  29. [
  30. 'download_url' => 'https://downloads.yunzmall.com/phpmyadmin.zip',
  31. 'is_install' => $is_install,
  32. 'access_url' => $access_path,
  33. ]
  34. ];
  35. return view('setting.sql-manage.index', [
  36. 'list' => json_encode($list),
  37. ]);
  38. }
  39. public function download()
  40. {
  41. $url = request()->url;
  42. $file_info = pathinfo($url);
  43. $basename = $file_info['basename'];
  44. $download_path = base_path().'/'.$basename;
  45. $get_res = file_get_contents($url);
  46. if (!$get_res) {
  47. return $this->errorJson('请求链接数据失败');
  48. }
  49. file_put_contents($download_path, file_get_contents($url));
  50. if (!file_exists($download_path)) {
  51. return $this->errorJson('下载失败');
  52. }
  53. $zip = new \ZipArchive();
  54. $new_path = base_path(md5(str_random(6).time()));
  55. if ($zip->open($download_path)) {
  56. $zip->extractTo($new_path);
  57. $zip->close();
  58. } else {
  59. return $this->errorJson('解压失败');
  60. }
  61. $log_arr = [
  62. 'path' => $new_path,
  63. ];
  64. $log = SqlInstallLog::first();
  65. if ($log) {
  66. $log->update($log_arr);
  67. } else {
  68. SqlInstallLog::create($log_arr);
  69. }
  70. @unlink($download_path);
  71. return $this->successJson('下载安装成功');
  72. }
  73. public function uninstall()
  74. {
  75. $log = SqlInstallLog::first();
  76. if (!$log) {
  77. return $this->errorJson('安装记录不存在');
  78. }
  79. if (file_exists($log->path)) {
  80. if (!$this->removeDir($log->path)) {
  81. return $this->errorJson('卸载失败');
  82. }
  83. $log->delete();
  84. }
  85. return $this->successJson('卸载成功');
  86. }
  87. private function removeDir($dir_name)
  88. {
  89. if (!is_dir($dir_name)) {
  90. return false;
  91. }
  92. $handle = @opendir($dir_name);
  93. while ($file = @readdir($handle)) {
  94. if ($file != '.' && $file != '..') {
  95. $dir = $dir_name . '/' . $file;
  96. is_dir($dir) ? $this->removeDir($dir) : @unlink($dir);
  97. }
  98. }
  99. closedir($handle);
  100. @rmdir($dir_name);
  101. return true;
  102. }
  103. }