PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/libraries/addons/Addons_installer.php

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