Base.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use app\common\controller\All;
  5. use app\common\util\BulkTableIo;
  6. use think\Cache;
  7. use app\common\util\Dir;
  8. use think\Db;
  9. class Base extends All
  10. {
  11. var $_admin;
  12. var $_pagesize;
  13. var $_makesize;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. // 校验Update.php文件完整性
  18. $update_file = APP_PATH . 'admin/controller/Update.php';
  19. $expected_hash = config('version.update_hash');
  20. if (!empty($expected_hash) && is_file($update_file) && md5_file($update_file) !== $expected_hash) {
  21. exit(lang('admin/update/core_file_error'));
  22. }
  23. //判断用户登录状态
  24. if(in_array($this->_cl,['Index']) && in_array($this->_ac,['login'])) {
  25. }
  26. elseif(ENTRANCE=='api' && in_array($this->_cl,['Timming']) && in_array($this->_ac,['index'])){
  27. }
  28. else {
  29. $res = model('Admin')->checkLogin();
  30. if ($res['code'] > 1) {
  31. return $this->redirect('index/login');
  32. }
  33. $this->_admin = $res['info'];
  34. $this->_pagesize = $GLOBALS['config']['app']['pagesize'];
  35. $this->_makesize = $GLOBALS['config']['app']['makesize'];
  36. if($this->_cl!='Update' && !$this->check_auth($this->_cl,$this->_ac)){
  37. return $this->error(lang('permission_denied'));
  38. }
  39. }
  40. $this->assign('cl',$this->_cl);
  41. $this->assign('MAC_VERSION',config('version')['code']);
  42. }
  43. public function check_auth($c,$a)
  44. {
  45. $c = strtolower($c);
  46. $a = strtolower($a);
  47. // UEditor AI proxy: logged-in admin only; API key never sent to browser.
  48. if ($c === 'upload' && ($a === 'ueditor_ai' || $a === 'ueditorai')) {
  49. return true;
  50. }
  51. $auths = $this->_admin['admin_auth'] . ',index/index,index/welcome,index/logout,';
  52. $cur = ','.$c.'/'.$a.',';
  53. if($this->_admin['admin_id'] =='1'){
  54. return true;
  55. }
  56. elseif(strpos($auths,$cur)===false){
  57. return false;
  58. }
  59. else{
  60. return true;
  61. }
  62. }
  63. public function _cache_clear()
  64. {
  65. if(ENTRANCE=='admin') {
  66. //播放器配置缓存
  67. $vodplayer = config('vodplayer');
  68. $voddowner = config('voddowner');
  69. $vodserver = config('vodserver');
  70. $player = [];
  71. foreach ($vodplayer as $k => $v) {
  72. $player[$k] = [
  73. 'show' => (string)$v['show'],
  74. 'des' => (string)$v['des'],
  75. 'ps' => (string)$v['ps'],
  76. 'parse' => (string)$v['parse'],
  77. ];
  78. }
  79. $downer = [];
  80. foreach ($voddowner as $k => $v) {
  81. $downer[$k] = [
  82. 'show' => (string)$v['show'],
  83. 'des' => (string)$v['des'],
  84. 'ps' => (string)$v['ps'],
  85. 'parse' => (string)$v['parse'],
  86. ];
  87. }
  88. $server = [];
  89. foreach ($vodserver as $k => $v) {
  90. $server[$k] = [
  91. 'show' => (string)$v['show'],
  92. 'des' => (string)$v['des']
  93. ];
  94. }
  95. $content = 'MacPlayerConfig.player_list=' . json_encode($player) . ',MacPlayerConfig.downer_list=' . json_encode($downer) . ',MacPlayerConfig.server_list=' . json_encode($server) . ';';
  96. $path = './static/js/playerconfig.js';
  97. if (!file_exists($path)) {
  98. $path .= '.bak';
  99. }
  100. $fc = @file_get_contents($path);
  101. if(!empty($fc)){
  102. $jsb = mac_get_body($fc, '//缓存开始', '//缓存结束');
  103. $fc = str_replace($jsb, "\r\n" . $content . "\r\n", $fc);
  104. @fwrite(fopen('./static/js/playerconfig.js', 'wb'), $fc);
  105. }
  106. }
  107. Dir::delDir(RUNTIME_PATH.'cache/');
  108. Dir::delDir(RUNTIME_PATH.'log/');
  109. Dir::delDir(RUNTIME_PATH.'temp/');
  110. Cache::clear();
  111. return true;
  112. }
  113. public function batch_replace($field,$model,$search,$replace,$type='vod')
  114. {
  115. $replaceres = [];
  116. if(isset($model[$field]) && $search !== ''){
  117. if(empty($replace)) $replace = '';
  118. $original_value = $model[$field];
  119. $new_value = mac_filter_xss(str_replace($search, $replace, $original_value));
  120. if($original_value !== $new_value){
  121. $replaceres[$field] = $new_value;
  122. $replaceres['des'] = '&nbsp;'.lang('admin/batch/replace').'['.lang('admin/batch/field_'.str_replace($type.'_','',$field)).']:'.mac_filter_xss($search).'→'.mac_filter_xss($replace).';';
  123. }
  124. else{
  125. $replaceres['des'] = '&nbsp;'.lang('admin/batch/no_match').';';
  126. }
  127. }
  128. return $replaceres;
  129. }
  130. public function base_export($param,$table,$where)
  131. {
  132. $max = min(BulkTableIo::MAX_EXPORT_ROWS, max(1, intval($param['max'] ?? 5000)));
  133. $format = (isset($param['format']) && $param['format'] === 'xlsx') ? 'xlsx' : 'csv';
  134. if ($format === 'xlsx' && !class_exists('ZipArchive')) {
  135. return $this->error(lang('admin/batch/io_need_zip'));
  136. }
  137. $fields = Db::name(ucfirst($table))->getTableFields();
  138. $list = Db::name(ucfirst($table))->where($where)->order("{$table}_id desc")->limit($max)->select();
  139. $base = $table.'_export_' . date('Ymd_His');
  140. if ($format === 'xlsx') {
  141. BulkTableIo::exportXlsxDownload($base, $fields, $list);
  142. } else {
  143. BulkTableIo::exportCsvDownload($base, $fields, $list);
  144. }
  145. exit;
  146. }
  147. public function base_import($table)
  148. {
  149. if (!request()->isPost()) {
  150. return $this->error(lang('illegal_request'));
  151. }
  152. $param = input('post.');
  153. $validate = \think\Loader::validate('Token');
  154. if (!$validate->check($param)) {
  155. return $this->error($validate->getError());
  156. }
  157. $file = $this->request->file('file');
  158. if (!$file) {
  159. return $this->error(lang('param_err'));
  160. }
  161. $info = $file->rule('uniqid')->validate(['size' => 20971520, 'ext' => 'csv,txt,xlsx']);
  162. if (!$info) {
  163. return $this->error($file->getError());
  164. }
  165. $path = $info->getPathname();
  166. $ext = strtolower(pathinfo($info->getInfo('name'), PATHINFO_EXTENSION));
  167. try {
  168. $parsed = BulkTableIo::parseFile($path, $ext);
  169. } catch (\Exception $e) {
  170. @unlink($path);
  171. return $this->error(lang('import_err'));
  172. }
  173. @unlink($path);
  174. $fields = Db::name(ucfirst($table))->getTableFields();
  175. $ok = 0;
  176. $fail = 0;
  177. $errLines = [];
  178. $n = 0;
  179. foreach ($parsed['rows'] as $idx => $row) {
  180. $n++;
  181. if ($n > BulkTableIo::MAX_IMPORT_ROWS) {
  182. break;
  183. }
  184. $data = BulkTableIo::filterRowKeys($row, $fields);
  185. if (empty($data[$table.'_name']) || !isset($data['type_id']) || $data['type_id'] === '') {
  186. $fail++;
  187. if (count($errLines) < 15) {
  188. $errLines[] = lang('admin/batch/io_row', [$idx + 2]) . ' ' . lang('param_err');
  189. }
  190. continue;
  191. }
  192. $data = BulkTableIo::prepareGenericForSave($data,$table);
  193. $res = model(ucfirst($table))->saveData($data);
  194. if ($res['code'] > 1) {
  195. $fail++;
  196. if (count($errLines) < 15) {
  197. $errLines[] = lang('admin/batch/io_row', [$idx + 2]) . ' ' . $res['msg'];
  198. }
  199. } else {
  200. $ok++;
  201. if($table === 'vod'){
  202. Cache::rm('vod_repeat_table_created_time');
  203. }
  204. }
  205. }
  206. if ($ok === 0 && $fail === 0) {
  207. return $this->error(lang('import_err'));
  208. }
  209. $msg = lang('admin/batch/io_ok', [$ok]);
  210. if ($fail > 0) {
  211. $msg .= ' ' . lang('admin/batch/io_fail', [$fail]);
  212. if (!empty($errLines)) {
  213. $msg .= ' — ' . implode(';', $errLines);
  214. }
  215. }
  216. if ($ok === 0) {
  217. return $this->error($msg);
  218. }
  219. return $this->success($msg);
  220. }
  221. }