PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/www/application/modules/admin/mod_search.php

https://github.com/kelios/imshop
PHP | 360 lines | 253 code | 80 blank | 27 comment | 50 complexity | c671d9f39ff1daa6dff1ea409f64f798 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Image CMS
  4. *
  5. */
  6. class Mod_search extends MY_Controller {
  7. private $api_url = '';
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->library('DX_Auth');
  12. admin_or_redirect();
  13. $this->load->helper('file');
  14. $this->config->load('api');
  15. $this->api_url = $this->config->item('mod_search_api_url');
  16. $this->load->library('lib_admin');
  17. $this->load->library('lib_category');
  18. $this->lib_admin->init_settings();
  19. // set category list
  20. $request = $this->curl_post();
  21. $data = $request['result'];
  22. $this->template->add_array(array(
  23. 'categories' => $data['categories'],
  24. ));
  25. if (is_really_writable(APPPATH.'modules/'))
  26. {
  27. $this->template->assign('install_type', 'dir');
  28. }
  29. else
  30. {
  31. $this->template->assign('install_type', 'ftp');
  32. }
  33. }
  34. public function index()
  35. {
  36. $this->template->assign('action', 'main');
  37. $this->template->show('modules_search', FALSE);
  38. }
  39. public function category($id = 0, $offset = 0)
  40. {
  41. $request = $this->curl_post(array(
  42. 'action' => 'by_category',
  43. 'category' => $id,
  44. 'offset' => $offset,
  45. ));
  46. $data = $request['result'];
  47. //Pagination
  48. $this->load->library('Pagination');
  49. $config['base_url'] = site_url('admin/mod_search/category/'.$id);
  50. $config['total_rows'] = $data['category_modules_count'];
  51. $config['per_page'] = $data['per_page'];
  52. $config['uri_segment'] = 5;
  53. $config['cur_tag_open'] = '<span class="active">';
  54. $config['cur_tag_close'] = '</span>';
  55. $this->pagination->num_links = 5;
  56. $this->pagination->initialize($config);
  57. $this->template->assign('pagination', $this->pagination->create_links_ajax());
  58. //End pagination
  59. $this->template->add_array(array(
  60. 'action' => 'list_modules',
  61. 'modules' => $data['modules'],
  62. 'modules_count' => $data['category_modules_count'],
  63. 'download_url_prefix' => $data['download_url_prefix'],
  64. ));
  65. $this->template->show('modules_search', FALSE);
  66. }
  67. public function display_install_window($id)
  68. {
  69. $request = $this->curl_post(array('action' => 'get_module', 'id' => (int) $id));
  70. $module_data = $request['result'];
  71. if ($module_data['module_data'] == FALSE)
  72. {
  73. echo 'Ошибка. Информация о модуле не найдена.';
  74. return;
  75. }
  76. $this->template->add_array(array(
  77. 'module' => $module_data['module_data'],
  78. ));
  79. $this->template->show('mod_install', FALSE);
  80. }
  81. // Connect to ftp server
  82. public function connect_ftp($id)
  83. {
  84. cp_check_perm('module_install');
  85. if (is_really_writable(APPPATH.'modules/'))
  86. {
  87. $use_dir = TRUE;
  88. }
  89. else
  90. {
  91. $use_dir = FALSE;
  92. }
  93. if (!function_exists('ftp_connect') AND $use_dir == FALSE)
  94. {
  95. showMessage('Функция ftp_connect недоступна.');
  96. exit;
  97. }
  98. if ($use_dir == FALSE)
  99. {
  100. $this->load->library('ftp');
  101. $config['hostname'] = $_POST['host'];
  102. $config['username'] = $_POST['login'];
  103. $config['password'] = $_POST['password'];
  104. $config['port'] = $_POST['port'];
  105. $config['passive'] = FALSE;
  106. $config['debug'] = FALSE;
  107. if ($this->ftp->connect($config) == FALSE)
  108. {
  109. showMessage('Ошибка подключения к серверу. Проверте имя пользователя или пароль.');
  110. }
  111. }
  112. $this->load->helper('string');
  113. $this->load->helper('file');
  114. $root = '/'.trim_slashes($_POST['root_folder']).'/';
  115. if ($root == '//') $root = '/';
  116. // Try to find self.
  117. if ($use_dir == FALSE)
  118. {
  119. $list = $this->ftp->list_files($root.'application/modules/admin');
  120. $error = TRUE;
  121. foreach($list as $k => $v)
  122. {
  123. if ($v == 'mod_search.php')
  124. {
  125. $error = FALSE;
  126. }
  127. }
  128. if ($error == TRUE)
  129. {
  130. showMessage('Ошибка. Не правильный путь к корневой директории.');
  131. exit;
  132. }
  133. else
  134. {
  135. // Store connection settings in cookie or db;
  136. }
  137. }
  138. //Download module zip file to tmp folder
  139. $request = $this->curl_post(array('action' => 'get_module', 'id' => (int) $id));
  140. $module_data = $request['result']['module_data'];
  141. if(($fh = fopen($module_data['file'], 'r')) == FALSE)
  142. {
  143. showMessage('Ошибка загрузки файла.');
  144. }
  145. else
  146. {
  147. $name = end(explode('/', $module_data['file']));
  148. $name = explode('.', $name);
  149. $name = $name[0];
  150. $tmp_folder = './system/cache/';
  151. // Delete temp dir
  152. @delete_files($tmp_folder.$name.'/', TRUE);
  153. @rmdir($tmp_folder.$name.'/');
  154. // Download file
  155. $contents = stream_get_contents($fh);
  156. // Save file
  157. write_file($tmp_folder.$name.'.zip', $contents);
  158. // Create temp folder
  159. if ($use_dir == FALE)
  160. {
  161. if ( ! mkdir($tmp_folder.$name.'/'))
  162. {
  163. showMessage('Ошибка создания временной директории.');
  164. fclose($fh);
  165. if ($use_dir == FALSE)
  166. $this->ftp->close();
  167. exit;
  168. }
  169. }
  170. // Uzip module files
  171. $zip_file = $tmp_folder.$name.'.zip';
  172. $this->load->library('pclzip', $zip_file);
  173. if ($use_dir == TRUE)
  174. {
  175. if (!file_exists(APPPATH.'modules/'.$name))
  176. {
  177. mkdir(APPPATH.'modules/'.$name);
  178. chmod(APPPATH.'modules/'.$name, 0777);
  179. }
  180. else
  181. {
  182. showMessage('Для продолжения уставки удалите директорию '.APPPATH.'modules/'.$name);
  183. exit;
  184. }
  185. if (($zip_result = $this->pclzip->extract(PCLZIP_OPT_PATH, APPPATH.'modules/'.$name.'/')) == 0)
  186. {
  187. showMessage('Ошибка извлечения файлов из архива.');
  188. exit;
  189. }
  190. $this->chmod_r(APPPATH.'modules/'.$name);
  191. // Make install
  192. $this->load->module('admin/components');
  193. if (!$this->components->install($name))
  194. {
  195. showMessage('Ошибка установки модуля.');
  196. }
  197. // Delete temp dir
  198. @delete_files($tmp_folder.$name.'/', TRUE);
  199. @rmdir($tmp_folder.$name.'/');
  200. // Delete temp .zip file
  201. @unlink($tmp_folder.$name.'.zip');
  202. showMessage('Модуль установлен.');
  203. // Close install window
  204. closeWindow('mod_install_w');
  205. exit;
  206. }
  207. if (($zip_result = $this->pclzip->extract(PCLZIP_OPT_PATH, $tmp_folder.$name.'/')) == 0)
  208. {
  209. showMessage('Ошибка извлечения файлов из архива.');
  210. }
  211. else
  212. {
  213. // Files extracted
  214. // Create module folder
  215. $dest_folder = $root.'application/modules/'.$name.'/';
  216. if ($use_dir == FALSE)
  217. {
  218. $this->ftp->mkdir($dest_folder);
  219. }
  220. // Copy file to modules folder
  221. if ($use_dir == FALSE)
  222. {
  223. if (!$this->ftp->mirror($tmp_folder.$name.'/', $dest_folder))
  224. {
  225. showMessage('Ошибка создания временной директории.');
  226. }
  227. }
  228. // Make install
  229. $this->load->module('admin/components');
  230. if (!$this->components->install($name))
  231. {
  232. showMessage('Ошибка установки модуля.');
  233. }
  234. // Delete temp dir
  235. @delete_files($tmp_folder.$name.'/', TRUE);
  236. @rmdir($tmp_folder.$name.'/');
  237. // Delete temp .zip file
  238. @unlink($tmp_folder.$name.'.zip');
  239. showMessage('Модуль установлен.');
  240. // Close install window
  241. closeWindow('mod_install_w');
  242. }
  243. fclose($fh);
  244. }
  245. if ($use_dir == FALSE)
  246. $this->ftp->close();
  247. }
  248. private function curl_post($data = array())
  249. {
  250. $url = $this->api_url;
  251. $options = array();
  252. $options[CURLOPT_HEADER] = FALSE;
  253. $options[CURLOPT_RETURNTRANSFER] = TRUE;
  254. $options[CURLOPT_POST] = TRUE;
  255. $options[CURLOPT_POSTFIELDS] = $data;
  256. $handler = curl_init($url);
  257. curl_setopt_array($handler, $options);
  258. $resp = curl_exec($handler);
  259. $result['code'] = curl_getinfo($handler, CURLINFO_HTTP_CODE);
  260. $result['result'] = unserialize($resp);
  261. $result['error'] = curl_errno($handler);
  262. curl_close($handler);
  263. return $result;
  264. }
  265. private function chmod_r($path, $perm = 0777)
  266. {
  267. $h = opendir($path);
  268. while ($file = readdir($h) )
  269. {
  270. if (($file != '.') && ($file != '..') && ($file != 'index.html') )
  271. {
  272. if (is_dir($file))
  273. {
  274. $this->chmod_r($path.'/'.$file, $perm);
  275. }
  276. chmod ($path.'/'.$file, $perm);
  277. }
  278. }
  279. }
  280. }