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

/cp/expressionengine/libraries/addons/Addons_installer.php

https://bitbucket.org/sbeuken/artelux
PHP | 511 lines | 257 code | 85 blank | 169 comment | 32 complexity | 11d773c11ccd7b20c0a50231b9a1e3e4 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Core Addon Installer Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Addons_installer {
  24. var $EE;
  25. /**
  26. * Constructor
  27. */
  28. function __construct()
  29. {
  30. $this->EE =& get_instance();
  31. $this->EE->load->library('api');
  32. $this->EE->load->library('addons');
  33. }
  34. // --------------------------------------------------------------------
  35. /**
  36. * Addon Installer
  37. *
  38. * @access public
  39. * @param string
  40. * @return void
  41. */
  42. function install($addon, $type = 'module', $show_package = TRUE)
  43. {
  44. $this->_update_addon($addon, $type, 'install', $show_package);
  45. return TRUE;
  46. }
  47. // --------------------------------------------------------------------
  48. /**
  49. * Addon Uninstaller
  50. *
  51. * Install one or more components
  52. *
  53. * @access public
  54. * @param string
  55. * @return void
  56. */
  57. function uninstall($addon, $type = 'module', $show_package = TRUE)
  58. {
  59. $this->_update_addon($addon, $type, 'uninstall', $show_package);
  60. return TRUE;
  61. }
  62. // --------------------------------------------------------------------
  63. /**
  64. * Module Installer
  65. *
  66. * @access private
  67. * @param string
  68. * @return void
  69. */
  70. function install_module($module)
  71. {
  72. $class = $this->_module_install_setup($module);
  73. $MOD = new $class();
  74. $MOD->_ee_path = APPPATH;
  75. if ($MOD->install() !== TRUE)
  76. {
  77. show_error(lang('module_can_not_be_found'));
  78. }
  79. }
  80. // --------------------------------------------------------------------
  81. /**
  82. * Module Uninstaller
  83. *
  84. * @access private
  85. * @param string
  86. * @return void
  87. */
  88. function uninstall_module($module)
  89. {
  90. $class = $this->_module_install_setup($module);
  91. $MOD = new $class();
  92. $MOD->_ee_path = APPPATH;
  93. if ($MOD->uninstall() !== TRUE)
  94. {
  95. show_error(lang('module_can_not_be_found'));
  96. }
  97. }
  98. // --------------------------------------------------------------------
  99. /**
  100. * Accessory Installer
  101. *
  102. * @access private
  103. * @param string
  104. * @return void
  105. */
  106. function install_accessory($accessory)
  107. {
  108. $class = $this->_accessory_install_setup($accessory);
  109. $ACC = new $class();
  110. if (method_exists($ACC, 'install'))
  111. {
  112. $ACC->install();
  113. }
  114. $this->EE->db->insert('accessories', array(
  115. 'class' => $class,
  116. 'accessory_version' => $ACC->version
  117. ));
  118. $this->EE->accessories->update_placement($class);
  119. }
  120. // --------------------------------------------------------------------
  121. /**
  122. * Accessory Uninstaller
  123. *
  124. * @access private
  125. * @param string
  126. * @return void
  127. */
  128. function uninstall_accessory($accessory)
  129. {
  130. $class = $this->_accessory_install_setup($accessory, FALSE);
  131. $ACC = new $class();
  132. if (method_exists($ACC, 'uninstall'))
  133. {
  134. $ACC->uninstall();
  135. }
  136. $this->EE->db->delete('accessories', array('class' => $class));
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Extension Installer
  141. *
  142. * @access private
  143. * @param string
  144. * @return void
  145. */
  146. function install_extension($extension, $enable = FALSE)
  147. {
  148. $this->EE->load->model('addons_model');
  149. if ( ! $this->EE->addons_model->extension_installed($extension))
  150. {
  151. $EXT = $this->_extension_install_setup($extension);
  152. if (method_exists($EXT, 'activate_extension') === TRUE)
  153. {
  154. $activate = $EXT->activate_extension();
  155. }
  156. }
  157. else
  158. {
  159. $class = $this->_extension_install_setup($extension, FALSE);
  160. $this->EE->addons_model->update_extension($class, array('enabled' => 'y'));
  161. }
  162. }
  163. // --------------------------------------------------------------------
  164. /**
  165. * Extension Uninstaller
  166. *
  167. * @access private
  168. * @param string
  169. * @return void
  170. */
  171. function uninstall_extension($extension)
  172. {
  173. $this->EE->load->model('addons_model');
  174. $EXT = $this->_extension_install_setup($extension);
  175. $this->EE->addons_model->update_extension(ucfirst(get_class($EXT)), array('enabled' => 'n'));
  176. if (method_exists($EXT, 'disable_extension') === TRUE)
  177. {
  178. $disable = $EXT->disable_extension();
  179. }
  180. }
  181. // --------------------------------------------------------------------
  182. /**
  183. * Fieldtype Installer
  184. *
  185. * @access private
  186. * @param string
  187. * @return void
  188. */
  189. function install_fieldtype($fieldtype)
  190. {
  191. $this->EE->api->instantiate('channel_fields');
  192. if ($this->EE->api_channel_fields->include_handler($fieldtype))
  193. {
  194. $default_settings = array();
  195. $FT = $this->EE->api_channel_fields->setup_handler($fieldtype, TRUE);
  196. $default_settings = $FT->install();
  197. $this->EE->db->insert('fieldtypes', array(
  198. 'name' => $fieldtype,
  199. 'version' => $FT->info['version'],
  200. 'settings' => base64_encode(serialize((array)$default_settings)),
  201. 'has_global_settings' => method_exists($FT, 'display_global_settings') ? 'y' : 'n'
  202. ));
  203. }
  204. }
  205. // --------------------------------------------------------------------
  206. /**
  207. * Fieldtype Uninstaller
  208. *
  209. * @access private
  210. * @param string
  211. * @return void
  212. */
  213. function uninstall_fieldtype($fieldtype)
  214. {
  215. $this->EE->api->instantiate('channel_fields');
  216. if ($this->EE->api_channel_fields->include_handler($fieldtype))
  217. {
  218. $this->EE->load->dbforge();
  219. // Drop columns
  220. $this->EE->db->select('channel_fields.field_id, channels.channel_id');
  221. $this->EE->db->from('channel_fields');
  222. $this->EE->db->join('channels', 'channels.field_group = channel_fields.group_id');
  223. $this->EE->db->where('channel_fields.field_type', $fieldtype);
  224. $query = $this->EE->db->get();
  225. $ids = array();
  226. $channel_ids = array();
  227. if ($query->num_rows() > 0)
  228. {
  229. foreach($query->result() as $row)
  230. {
  231. $ids[] = $row->field_id;
  232. $channel_ids[] = $row->channel_id;
  233. }
  234. }
  235. $ids = array_unique($ids);
  236. if (count($ids))
  237. {
  238. foreach($ids as $id)
  239. {
  240. $this->EE->dbforge->drop_column('channel_data', 'field_id_'.$id);
  241. $this->EE->dbforge->drop_column('channel_data', 'field_ft_'.$id);
  242. }
  243. // Remove from layouts
  244. $c_ids = array_unique($channel_ids);
  245. $this->EE->load->library('layout');
  246. $this->EE->layout->delete_layout_fields($ids, $c_ids);
  247. $this->EE->db->where_in('field_id', $ids);
  248. $this->EE->db->delete(array('channel_fields', 'field_formatting'));
  249. }
  250. // Uninstall
  251. $FT = $this->EE->api_channel_fields->setup_handler($fieldtype, TRUE);
  252. $FT->uninstall();
  253. $this->EE->db->delete('fieldtypes', array('name' => $fieldtype));
  254. }
  255. }
  256. // --------------------------------------------------------------------
  257. /**
  258. * RTE Tool Installer
  259. *
  260. * @access private
  261. * @param String $tool The name of the tool, with or without spaces, but
  262. * without _rte at the end
  263. */
  264. function install_rte_tool($tool)
  265. {
  266. $this->EE->load->add_package_path(PATH_MOD.'rte', FALSE);
  267. $this->EE->load->model('rte_tool_model');
  268. $this->EE->rte_tool_model->add($tool);
  269. }
  270. // --------------------------------------------------------------------
  271. /**
  272. * RTE Tool Uninstaller
  273. *
  274. * @access private
  275. * @param string
  276. * @return void
  277. */
  278. function uninstall_rte_tool($tool)
  279. {
  280. $this->EE->load->add_package_path(PATH_MOD.'rte', FALSE);
  281. $this->EE->load->model('rte_tool_model');
  282. $this->EE->rte_tool_model->delete($tool);
  283. }
  284. // --------------------------------------------------------------------
  285. /**
  286. * Module Install Setup
  287. *
  288. * Contains common code for install and uninstall routines
  289. *
  290. * @access private
  291. * @param string
  292. * @return void
  293. */
  294. function _module_install_setup($module)
  295. {
  296. if ( ! $this->EE->cp->allowed_group('can_admin_modules'))
  297. {
  298. show_error(lang('unauthorized_access'));
  299. }
  300. if ($module == '')
  301. {
  302. show_error(lang('module_can_not_be_found'));
  303. }
  304. if (in_array($module, $this->EE->core->native_modules))
  305. {
  306. $path = PATH_MOD.$module.'/upd.'.$module.'.php';
  307. }
  308. else
  309. {
  310. $path = PATH_THIRD.$module.'/upd.'.$module.'.php';
  311. }
  312. if ( ! is_file($path))
  313. {
  314. show_error(lang('module_can_not_be_found'));
  315. }
  316. $class = ucfirst($module).'_upd';
  317. if ( ! class_exists($class))
  318. {
  319. require $path;
  320. }
  321. return $class;
  322. }
  323. // --------------------------------------------------------------------
  324. /**
  325. * Accessory Install Setup
  326. *
  327. * Contains common code for install and uninstall routines
  328. *
  329. * @access private
  330. * @param string
  331. * @return void
  332. */
  333. function _accessory_install_setup($accessory, $install = TRUE)
  334. {
  335. if ( ! $this->EE->cp->allowed_group('can_access_accessories'))
  336. {
  337. show_error(lang('unauthorized_access'));
  338. }
  339. if ($accessory == '')
  340. {
  341. show_error(lang('unauthorized_access'));
  342. }
  343. $class = ucfirst($accessory).'_acc';
  344. $count = $this->EE->super_model->count('accessories', array('class' => $class));
  345. if ( ! ($install XOR $count))
  346. {
  347. show_error(lang('unauthorized_access'));
  348. }
  349. $this->EE->load->library('accessories');
  350. return $this->EE->accessories->_get_accessory_class($accessory);
  351. }
  352. // --------------------------------------------------------------------
  353. /**
  354. * Extension Install Setup
  355. *
  356. * Contains common code for install and uninstall routines
  357. *
  358. * @access private
  359. * @param string
  360. * @return void
  361. */
  362. function _extension_install_setup($extension, $instantiate = TRUE)
  363. {
  364. if ( ! $this->EE->cp->allowed_group('can_access_extensions'))
  365. {
  366. show_error(lang('unauthorized_access'));
  367. }
  368. if ($extension == '')
  369. {
  370. show_error(lang('no_extension_id'));
  371. }
  372. $class = ucfirst($extension).'_ext';
  373. if ( ! $instantiate)
  374. {
  375. return $class;
  376. }
  377. if ( ! class_exists($class))
  378. {
  379. include($this->EE->addons->_packages[$extension]['extension']['path'].'ext.'.$extension.'.php');
  380. }
  381. return new $class();
  382. }
  383. // --------------------------------------------------------------------
  384. /**
  385. * Universal Addon (Un)Installer
  386. *
  387. * @access private
  388. * @param string
  389. * @return void
  390. */
  391. private function _update_addon($addon, $type, $action, $show_package)
  392. {
  393. // accepts arrays
  394. if (is_array($type))
  395. {
  396. foreach($type as $component)
  397. {
  398. $this->_update_addon($addon, $component, $action, $show_package);
  399. }
  400. return;
  401. }
  402. // first party
  403. if ( ! $this->EE->addons->is_package($addon))
  404. {
  405. return $this->{$action.'_'.$type}($addon);
  406. }
  407. // third party - do entire package
  408. if ($show_package && count($this->EE->addons->_packages[$addon]) > 1)
  409. {
  410. $this->EE->functions->redirect(BASE.AMP.'C=addons'.AMP.'M=package_settings'.AMP.'package='.$addon.AMP.'return='.$_GET['C']);
  411. }
  412. else
  413. {
  414. $method = $action.'_'.$type;
  415. if (method_exists($this, $method))
  416. {
  417. $this->EE->load->add_package_path($this->EE->addons->_packages[$addon][$type]['path'], FALSE);
  418. $this->$method($addon);
  419. $this->EE->load->remove_package_path($this->EE->addons->_packages[$addon][$type]['path']);
  420. }
  421. }
  422. }
  423. }
  424. // END Addons_installer class
  425. /* End of file Addons_installer.php */
  426. /* Location: ./system/expressionengine/libraries/addons/Addons_installer.php */