PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/application/controllers/dashboard/files/file_groups.php

https://bitbucket.org/seezoo/seezoo/
PHP | 573 lines | 368 code | 84 blank | 121 comment | 47 complexity | 0cefdc23044fa801d4a0bc39d73c271a MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Seezoo ファイルグループ管理ページクラス
  4. */
  5. class File_groups extends SZ_Controller
  6. {
  7. public static $page_title = 'グループ管理';
  8. public static $description = 'システムで使用するファイルをグループに分別します。';
  9. // pagination per page
  10. public $limit = 20;
  11. protected $upload_dir = 'files/';
  12. protected $allowed_types = 'gif|jpg|jpeg|png|bmp|tiff|zip|txt|csv|doc|rtf|xls|pdf|swf|fla|tar.gz|html|css|js|php';
  13. protected $upload_thumbnail_dir = 'files/thumbnail/';
  14. public $page = 'files';
  15. public $msg = '';
  16. public $ticket_name = 'sz_ticket';
  17. function __construct()
  18. {
  19. parent::SZ_Controller();
  20. $this->load->model('file_model');
  21. }
  22. function index($offset = 0)
  23. {
  24. //$data->files = $this->file_model->get_all_files();
  25. $data->file_groups = $this->file_model->get_file_groups($this->limit, (int)$offset);
  26. $total = $this->file_model->get_file_groups_count();
  27. $endoftotal = (($offset+ $this->limit) > $total) ? $total : ($offset + $this->limit);
  28. if($total)
  29. {
  30. $data->total = $total . '件中' . ($offset + 1) . '-' . $endoftotal . '件表示';
  31. }
  32. else
  33. {
  34. $data->total = '';
  35. }
  36. $path = page_link() . 'dashboard/files/file_groups/index/';
  37. $data->pagination = $this->_pagination($path, $total, 4, $this->limit);
  38. $data->ticket = $this->_set_ticket();
  39. $this->load->view('dashboard/files/file_groups_list', $data);
  40. }
  41. function ajax_add_group($token)
  42. {
  43. if (!$this->session->userdata('sz_token') || $this->session->userdata('sz_token') !== $token)
  44. {
  45. exit('error');
  46. }
  47. // keep main flash token
  48. $this->session->keep_flashdata($this->ticket_name);
  49. $post = array(
  50. 'group_name' => $this->input->post('group_name', TRUE),
  51. 'created_date' => db_datetime()
  52. );
  53. $ret = $this->file_model->insert_new_file_group($post);
  54. if ($ret && is_numeric($ret))
  55. {
  56. $data = array(
  57. 'file_groups_id' => $ret
  58. );
  59. echo json_encode($data);
  60. }
  61. else
  62. {
  63. echo 'error';
  64. }
  65. }
  66. function ajax_update_file_group($token)
  67. {
  68. if (!$this->session->userdata('sz_token') || $this->session->userdata('sz_token') !== $token)
  69. {
  70. exit('error');
  71. }
  72. $post = array(
  73. 'group_name' => $this->input->post('group_name', TRUE)
  74. );
  75. $ret = $this->file_model->update_file_group($post, $this->input->post('file_groups_id'));
  76. if ($ret)
  77. {
  78. echo $post['group_name'];
  79. }
  80. else
  81. {
  82. echo 'error';
  83. }
  84. }
  85. function ajax_delete_file_group($cid, $token)
  86. {
  87. if (!$this->session->userdata('sz_token') || $this->session->userdata('sz_token') !== $token || !$cid)
  88. {
  89. exit('error');
  90. }
  91. $ret = $this->file_model->delete_file_group((int)$cid);
  92. if ($ret)
  93. {
  94. echo 'complete';
  95. }
  96. else
  97. {
  98. echo 'error';
  99. }
  100. }
  101. function search()
  102. {
  103. $name = $this->input->post('file_name');
  104. $ext = $this->input->post('file_ext');
  105. if ($ext === 'all')
  106. {
  107. $ext = '';
  108. }
  109. $data->files = $this->file_model->search_file_data($name, $ext);
  110. $data->ticket = $this->_set_ticket();
  111. $data->ext_list = $this->file_model->get_file_exts();
  112. $this->load->view('dashboard/files/file_list', $data);
  113. }
  114. function edit()
  115. {
  116. $this->_check_ticket();
  117. $mode = $this->input->post('method');
  118. if ($mode === 'dl')
  119. {
  120. $this->_multiple_download();
  121. exit;
  122. }
  123. else if ($mode === 'delete')
  124. {
  125. $ret = $this->file_model->delete_file_data($this->input->post('file_ids'));
  126. redirect('dashboard/files/index');
  127. // if ($ret)
  128. // {
  129. // $this->msg = 'ファイルを削除しました。';
  130. // }
  131. // else
  132. // {
  133. // $this->msg = 'ファイル削除に失敗しました。';
  134. // }
  135. // $this->index();
  136. }
  137. else
  138. {
  139. redirect('dashboard/files');
  140. //return;
  141. }
  142. }
  143. function upload_init()
  144. {
  145. $this->load->view('dashboard/files/upload');
  146. }
  147. function multiple_upload()
  148. {
  149. $this->load->view('dashboard/files/multiple_upload');
  150. }
  151. function multiple_piece()
  152. {
  153. // is handle is posted, do upload
  154. if ($this->input->post('upload_handle'))
  155. {
  156. if (is_uploaded_file($_FILES['upload_data']['tmp_name']) === TRUE)
  157. {
  158. $up = $this->_do_upload();
  159. if ($up)
  160. {
  161. if (has_icon_ext($up['extension']))
  162. {
  163. $up['is_icon'] = true;
  164. }
  165. else
  166. {
  167. $up['is_icon'] = false;
  168. }
  169. $data->complete = 1;
  170. $data->data = json_encode($up);
  171. }
  172. else
  173. {
  174. $data->complete = 0;
  175. }
  176. }
  177. else
  178. {
  179. $data->complete = 2;
  180. }
  181. }
  182. else
  183. {
  184. $data = array();
  185. }
  186. $this->load->view('dashboard/files/multiple_piece', $data);
  187. }
  188. function ajax_upload()
  189. {
  190. $up = $this->_do_upload();
  191. if ($up)
  192. {
  193. if (has_icon_ext($up['extension']))
  194. {
  195. $up['is_icon'] = true;
  196. }
  197. else
  198. {
  199. $up['is_icon'] = false;
  200. }
  201. $this->load->view('dashboard/files/upload_complete', array('file' => json_encode($up)));
  202. }
  203. else
  204. {
  205. //$this->load->view('dashboard/files/upload_error');
  206. }
  207. }
  208. function ajax_file_view($fid, $token)
  209. {
  210. if (!$fid || $this->session->userdata('sz_token') !== $token)
  211. {
  212. exit('access_denied');
  213. }
  214. $data->file = $this->file_model->get_file_data((int)$fid);
  215. $dl_token = md5(uniqid(mt_rand(), TRUE));
  216. $this->session->set_flashdata('sz_dl_token', $dl_token);
  217. $data->ticket = $dl_token;
  218. $data->fid = (int)$fid;
  219. $this->load->view('dashboard/files/ajax_file_view', $data);
  220. }
  221. function _do_upload()
  222. {
  223. // 2010/05/03 modified
  224. // use CodeIgniter buildin library
  225. $this->load->library('upload');
  226. $data = array();
  227. // upload config
  228. $config = array(
  229. 'upload_path' => $this->upload_dir,
  230. 'allowed_types' => $this->allowed_types,
  231. 'overwrite' => FALSE,
  232. 'encrypt_name' => TRUE,
  233. 'remove_spaces' => TRUE
  234. );
  235. $this->upload->initialize($config);
  236. // try upload
  237. $result = $this->upload->do_upload('upload_data');
  238. if (!$result)
  239. {
  240. echo ($this->upload->display_errors());
  241. return FALSE;
  242. }
  243. $ret = $this->upload->data();
  244. // get data for DB insert
  245. $data['file_name'] = substr($ret['orig_name'], 0, strrpos($ret['orig_name'], '.'));
  246. $data['crypt_name'] = $ret['raw_name'];
  247. $data['extension'] = substr($ret['file_ext'], 1);
  248. $data['size'] = $ret['file_size'];
  249. $data['width'] = ($ret['is_image'] == 1) ? (int)$ret['image_width'] : 0;
  250. $data['height'] = ($ret['is_image'] == 1) ? (int)$ret['image_height'] : 0;
  251. $data['added_date'] = date('Y-m-d H:i:s', time());
  252. // create thumbnail if upload file is image
  253. if ($ret['is_image'] > 0)
  254. {
  255. if ($ret['image_width'] > 60 || $ret['image_height'] > 60)
  256. {
  257. // load image_lib library
  258. $conf = array(
  259. 'source_image' => $ret['full_path'],
  260. 'create_thumb' => TRUE,
  261. 'new_image' => $this->upload_thumbnail_dir,
  262. 'thumb_marker' => '',
  263. 'width' => 60,
  264. 'height' => 60,
  265. 'maintain_ratio' => TRUE
  266. );
  267. $this->load->library('image_lib', $conf);
  268. if (! $this->image_lib->resize())
  269. {
  270. return FALSE;
  271. }
  272. }
  273. else
  274. {
  275. // simple copy file
  276. if (!copy($ret['full_path'], $this->upload_thumbnail_dir . $ret['file_name']))
  277. {
  278. return FALSE;
  279. }
  280. }
  281. }
  282. $res = $this->file_model->insert_new_image($data);
  283. if ($res)
  284. {
  285. $data['file_id'] = $res;
  286. $data['thumbnail_path'] = file_link() . 'files/thumbnail.' . $data['file_name'];
  287. return $data;
  288. }
  289. else
  290. {
  291. return FALSE;
  292. }
  293. // require_once('class.upload.php');
  294. //
  295. // $data = array();
  296. //
  297. // $upload = new Upload($_FILES['upload_data']);
  298. //
  299. // if (!$upload->uploaded)
  300. // {
  301. // return FALSE;
  302. // }
  303. //
  304. // $data['file_name'] = $upload->file_src_name_body;
  305. // $data['crypt_name'] = md5(uniqid(mt_rand(), TRUE));
  306. // $data['extension'] = $upload->file_src_name_ext;
  307. // $data['size'] = $upload->file_src_size;
  308. // list($data['width'], $data['height']) = @getimagesize($_FILES['upload_data']['tmp_name']);
  309. // $data['added_date'] = date('Y-m-d H:i:s', time());
  310. //
  311. // // upload settings
  312. // $upload->file_overwrite = TRUE;
  313. // $upload->file_auto_rename = FALSE;
  314. // $upload->file_src_name_body = $data['crypt_name'];
  315. //
  316. // $upload->Process($this->upload_dir);
  317. //
  318. // // upload failed
  319. // if (!$upload->processed)
  320. // {
  321. // echo 'error';
  322. // return FALSE;
  323. // }
  324. //
  325. // // create thumbnail if upload file is image
  326. // if ($data['width'] && $data['height'])
  327. // {
  328. // $upload->image_resize = TRUE;
  329. //
  330. // if ($data['width'] > $data['height'])
  331. // {
  332. // $upload->image_x = 60;
  333. // $upload->image_y = round($data['height'] * (60 /$data['width']));
  334. // }
  335. // else
  336. // {
  337. // $upload->image_y = 60;
  338. // $upload->image_x = round($data['width'] * (60 / $data['height']));
  339. // }
  340. //
  341. // $upload->Process($this->upload_thumbnail_dir);
  342. //
  343. // if (!$upload->processed)
  344. // {
  345. // return FALSE;
  346. // }
  347. // }
  348. // else
  349. // {
  350. // $data['width'] = 0;
  351. // $data['height'] = 0;
  352. // }
  353. //
  354. // // insert db
  355. // $ret = $this->file_model->insert_new_image($data);
  356. //
  357. // if ($ret)
  358. // {
  359. // $data['file_id'] = $ret;
  360. // $data['thumbnail_path'] = base_url() . 'files/thumbnail/' . $data['crypt_name'] . '.' . $data['extension'];
  361. //
  362. // if ($data['width'] && $data['height'])
  363. // {
  364. // $data['formatted_wh'] = $data['width'] . 'px&nbsp;×&nbsp;' . $data['height'] . 'px';
  365. // }
  366. // return $data;
  367. // }
  368. // else
  369. // {
  370. // return FALSE;
  371. // }
  372. }
  373. function _set_ticket()
  374. {
  375. $ticket = md5(uniqid(mt_rand(), TRUE));
  376. $this->session->set_userdata('sz_file_token', $ticket);
  377. return $ticket;
  378. }
  379. function _check_ticket()
  380. {
  381. $ticket = $this->input->post('sz_file_token');
  382. if (!$ticket || $ticket !== $this->session->userdata('sz_file_token'))
  383. {
  384. exit('不正な操作です。');
  385. }
  386. }
  387. // from ajax
  388. function delete_file($id, $token)
  389. {
  390. if (!$id || $token !== $this->session->userdata('sz_token'))
  391. {
  392. exit('access denied');
  393. }
  394. $ret = $this->file_model->delete_file_one($id);
  395. if ($ret)
  396. {
  397. echo 'complete';
  398. }
  399. else
  400. {
  401. echo 'error';
  402. }
  403. }
  404. function file_download_from_popup($fid, $token = FALSE)
  405. {
  406. if (!$token || $this->session->userdata('sz_token') !== $token)
  407. {
  408. echo 'access_denied.';
  409. }
  410. $file = $this->file_model->get_file_data((int)$fid);
  411. $file_path = 'files/' . $file->crypt_name . '.' . $file->extension;
  412. $file_name = $file->file_name . '.' . $file->extension;
  413. $this->load->helper('download');
  414. force_download($file_name, file_get_contents($file_path));
  415. }
  416. function file_download()
  417. {
  418. if (!$this->input->post('file_id') || $this->session->flashdata('sz_dl_token') !== $this->input->post('ticket'))
  419. {
  420. exit('access_denied');
  421. }
  422. $file = $this->file_model->get_file_data((int)$this->input->post('file_id'));
  423. $file_path = 'files/' . $file->crypt_name . '.' . $file->extension;
  424. $file_name = $file->file_name . '.' . $file->extension;
  425. header("Content-Type: application/occet-stream");
  426. header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");
  427. header("Content-Length: " . filesize($file_path));
  428. header("Pragma: public");
  429. header("Expires: 0");
  430. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  431. header("Cache-Control: private", FALSE);
  432. header("Content-Transfer-Encoding: binary");
  433. // $out = '';
  434. // $fp = fopen($file_path, 'rb');
  435. // if ($fp === FALSE)
  436. // {
  437. // return FALSE;
  438. // }
  439. //
  440. // while(!feof($fp))
  441. // {
  442. // $out = fread($fp, 1024 * 1024);
  443. // echo $out;
  444. // }
  445. //
  446. // fclose($fp);
  447. readfile($file_path);
  448. exit();
  449. }
  450. function _multiple_download()
  451. {
  452. $token = $this->input->post('sz_file_token');
  453. if (!$token || $token != $this->session->userdata('sz_file_token'))
  454. {
  455. exit('access_denied.');
  456. }
  457. $ids = $this->input->post('file_ids');
  458. // load zip library
  459. $this->load->library('zip');
  460. foreach ($ids as $id)
  461. {
  462. if ((int)$id === 0)
  463. {
  464. continue;
  465. }
  466. $file = $this->file_model->get_file_data($id);
  467. if ($file)
  468. {
  469. $this->zip->read_file(make_file_path($file), FALSE, $file->file_name . '.' . $file->extension);
  470. }
  471. }
  472. $this->zip->download('files' . date('YmdHis', time()) . '.zip');
  473. }
  474. function _pagination($path, $total, $segment, $limit)
  475. {
  476. $this->load->library('pagination');
  477. $config = array(
  478. 'base_url' => $path,
  479. 'total_rows' => $total,
  480. 'per_page' => $limit,
  481. 'uri_segment'=> $segment,
  482. 'num_links' => 5,
  483. 'prev_link' => '&laquo;前へ',
  484. 'next_link' => '次へ&raquo;'
  485. );
  486. $this->pagination->initialize($config);
  487. return $this->pagination->create_links();
  488. }
  489. }