PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/www/application/modules/imagebox/admin.php

https://github.com/kelios/imshop
PHP | 216 lines | 154 code | 45 blank | 17 comment | 10 complexity | 555e810c4da3ace6bfe17f14a65c96f9 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. //error_reporting(0);
  3. /**
  4. * Image CMS
  5. *
  6. * Imagebox module.
  7. */
  8. class Admin extends MY_Controller {
  9. public $settings = array(
  10. 'upload_folder' => './uploads/imagebox/',
  11. 'thumbs_folder' => './uploads/imagebox/thumbs/',
  12. 'max_width' => '800',
  13. 'max_height' => '600',
  14. 'thumb_width' => '100',
  15. 'thumb_height' => '100',
  16. 'allowed_types' => 'gif|jpg|jpeg|png',
  17. 'maintain_ratio' => TRUE,
  18. 'quality' => '95%',
  19. 'link_rel' => 'lightbox',
  20. );
  21. function __construct()
  22. {
  23. parent::__construct();
  24. $this->load->library('DX_Auth');
  25. cp_check_perm('module_admin');
  26. $settings = $this->get_settings();
  27. if (is_array($settings))
  28. {
  29. foreach($settings as $k => $v)
  30. {
  31. $this->settings[$k] = $v;
  32. }
  33. }
  34. }
  35. /**
  36. * Display install settings
  37. */
  38. public function index()
  39. {
  40. $this->display_tpl('install');
  41. }
  42. public function main()
  43. {
  44. $this->template->add_array(array(
  45. 'settings' => $this->settings
  46. ));
  47. $this->display_tpl('main_window');
  48. }
  49. public function upload()
  50. {
  51. $this->load->library('image_lib');
  52. if ($_POST['file_url'] != '')
  53. {
  54. $url = $this->input->post('file_url');
  55. $ext = end(explode('.', $url));
  56. if (strstr($this->settings['allowed_types'], $ext) == FALSE)
  57. {
  58. echo 'Error: Вы пытаетесь загрузить запрещенный тип файла.';
  59. return;
  60. }
  61. $p = @fopen($url, 'rb');
  62. if (!$p)
  63. {
  64. echo 'Error: Ошибка загрузки файла.';
  65. return;
  66. }
  67. $image_data = stream_get_contents($p);
  68. fclose($p);
  69. $this->load->helper('file');
  70. $name = md5( time() ).'.'.$ext;
  71. write_file($this->settings['upload_folder'].$name, $image_data);
  72. $sizes = $this->get_image_size( $this->settings['upload_folder'].$name );
  73. $file = array(
  74. 'image_width' => $sizes['width'],
  75. 'image_height' => $sizes['height'],
  76. 'full_path' => $this->settings['upload_folder'].$name,
  77. 'file_name' => $name,
  78. );
  79. }
  80. else
  81. {
  82. $config['upload_path'] = $this->settings['upload_folder'];
  83. $config['allowed_types'] = $this->settings['allowed_types'];
  84. //$config['max_size'] = 1024 * 1024 * $this->max_file_size;
  85. $this->load->library('upload', $config);
  86. if ( ! $this->upload->do_upload())
  87. {
  88. echo 'Error: '. $this->upload->display_errors('', '');
  89. return;
  90. }
  91. else
  92. {
  93. $file = $this->upload->data();
  94. }
  95. }
  96. $this->_resize($file);
  97. $thumb_img_url = $this->settings['thumbs_folder'].$file['file_name'];
  98. $link_url = $this->settings['upload_folder'].$file['file_name'];
  99. echo '<a href="'.$link_url.'" rel="'.$this->settings['link_rel'].'" ><img src="'.$thumb_img_url.'" /></a>';
  100. }
  101. private function _resize($file)
  102. {
  103. if ($file['image_width'] > $this->settings['max_width'] OR $file['image_height'] > $this->settings['max_height'])
  104. {
  105. $config['image_library'] = 'gd2';
  106. $config['source_image'] = $file['full_path'];
  107. $config['maintain_ratio'] = $this->settings['maintain_ratio'];
  108. $config['width'] = $this->settings['max_width'];
  109. $config['height'] = $this->settings['max_height'];
  110. $config['quality'] = $this->settings['quality'];
  111. $this->image_lib->clear();
  112. $this->image_lib->initialize($config);
  113. $this->image_lib->resize();
  114. }
  115. $config['image_library'] = 'gd2';
  116. $config['source_image'] = $file['full_path'];
  117. $config['new_image'] = $this->settings['thumbs_folder'].$file['file_name'];
  118. $config['maintain_ratio'] = $this->settings['maintain_ratio'];
  119. $config['width'] = $this->settings['thumb_width'];
  120. $config['height'] = $this->settings['thumb_height'];
  121. $config['quality'] = $this->settings['quality'];
  122. $this->image_lib->clear();
  123. $this->image_lib->initialize($config);
  124. $this->image_lib->resize();
  125. }
  126. /**
  127. * Get image width and height
  128. */
  129. private function get_image_size($file_path)
  130. {
  131. if (function_exists('getimagesize'))
  132. {
  133. $image = @getimagesize($file_path);
  134. $size = array(
  135. 'width' => $image[0],
  136. 'height' => $image[1]
  137. );
  138. return $size;
  139. }
  140. return FALSE;
  141. }
  142. public function get_settings()
  143. {
  144. $this->db->where('name', 'imagebox');
  145. $query = $this->db->get('components')->row_array();
  146. return unserialize($query['settings']);
  147. }
  148. public function save_settings()
  149. {
  150. $data = array(
  151. 'max_width' => (int)$this->input->post('max_width'),
  152. 'max_height' => (int)$this->input->post('max_height'),
  153. 'thumb_width' => (int)$this->input->post('thumb_width'),
  154. 'thumb_height' => (int)$this->input->post('thumb_height'),
  155. 'maintain_ratio' => (bool)$this->input->post('maintain_ratio'),
  156. 'quality' => $this->input->post('quality'),
  157. );
  158. $this->db->where('name', 'imagebox');
  159. $this->db->update('components', array('settings' => serialize($data)));
  160. showMessage('Изменения сохранены.');
  161. }
  162. /**
  163. * Display template file
  164. */
  165. private function display_tpl($file = '')
  166. {
  167. $file = realpath(dirname(__FILE__)).'/templates/'.$file.'.tpl';
  168. $this->template->display('file:'.$file);
  169. }
  170. }
  171. /* End of file admin.php */