PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_jce/adapters/plugin.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 368 lines | 224 code | 52 blank | 92 comment | 52 complexity | 9284dd5a0f973128c4c54da3be5d8f7f MD5 | raw file
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright (c) 2009-2013 Ryan Demmer. All rights reserved.
  5. * @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. * JCE is free software. This version may have been modified pursuant
  7. * to the GNU General Public License, and as distributed it includes or
  8. * is derivative of works licensed under the GNU General Public License or
  9. * other free or open source software licenses.
  10. */
  11. defined('JPATH_BASE') or die('RESTRICTED');
  12. /**
  13. * JCE Plugin installer
  14. *
  15. * @package JCE
  16. * @subpackage Installer
  17. * @since 1.5
  18. */
  19. class WFInstallerPlugin extends JObject {
  20. /**
  21. * Constructor
  22. *
  23. * @param object $parent Parent object [JInstaller instance]
  24. * @return void
  25. */
  26. public function __construct($parent) {
  27. $this->parent = $parent;
  28. }
  29. private function setManifest() {
  30. $manifest = $this->parent->getManifest();
  31. $values = array('name', 'description', 'install.script', 'uninstall.script', 'icon');
  32. foreach ($values as $value) {
  33. $this->parent->set($value, WFXMLHelper::getElement($manifest, $value));
  34. }
  35. $attributes = array('version', 'plugin', 'group', 'type', 'folder', 'row', 'extension');
  36. foreach ($attributes as $attribute) {
  37. $this->set($attribute, WFXMLHelper::getAttribute($manifest, $attribute));
  38. }
  39. $elements = array('files', 'languages', 'media');
  40. foreach ($elements as $element) {
  41. $this->set($element, WFXMLHelper::getElements($manifest, $element));
  42. }
  43. }
  44. /**
  45. * Install method
  46. *
  47. * @access public
  48. * @return boolean True on success
  49. */
  50. public function install() {
  51. // Get a database connector object
  52. $db = $this->parent->getDBO();
  53. $this->setManifest();
  54. $plugin = $this->get('plugin');
  55. $group = $this->get('group');
  56. $type = $this->get('type');
  57. $folder = $this->get('folder');
  58. $extension = $this->get('extension');
  59. // JCE Plugin
  60. if (!empty($plugin) || !empty($extension)) {
  61. if (version_compare((string) $this->get('version'), '2.0', '<')) {
  62. $this->parent->abort(WFText::_('WF_INSTALLER_INCORRECT_VERSION'));
  63. return false;
  64. }
  65. // its an "extension"
  66. if ($extension) {
  67. $this->parent->setPath('extension_root', JPATH_COMPONENT_SITE . '/editor/extensions/' . $folder);
  68. } else {
  69. $this->parent->setPath('extension_root', JPATH_COMPONENT_SITE . '/editor/tiny_mce/plugins/' . $plugin);
  70. }
  71. } else {
  72. // Non-JCE plugin type, probably JCE MediaBox
  73. if ($type == 'plugin' && $group == 'system') {
  74. require_once(JPATH_LIBRARIES . '/joomla/installer/adapters/plugin.php');
  75. // create adapter
  76. $adapter = new JInstallerPlugin($this->parent, $db);
  77. if (method_exists($adapter, 'loadLanguage')) {
  78. $adapter->loadLanguage($this->parent->getPath('source'));
  79. }
  80. // set adapter
  81. $this->parent->setAdapter('plugin', $adapter);
  82. // isntall
  83. return $adapter->install();
  84. } else {
  85. $this->parent->abort(WFText::_('WF_INSTALLER_EXTENSION_INSTALL') . ' : ' . WFText::_('WF_INSTALLER_NO_PLUGIN_FILE'));
  86. return false;
  87. }
  88. }
  89. /**
  90. * ---------------------------------------------------------------------------------------------
  91. * Filesystem Processing Section
  92. * ---------------------------------------------------------------------------------------------
  93. */
  94. // If the extension directory does not exist, lets create it
  95. $created = false;
  96. if (!file_exists($this->parent->getPath('extension_root'))) {
  97. if (!$created = JFolder::create($this->parent->getPath('extension_root'))) {
  98. $this->parent->abort(WFText::_('WF_INSTALLER_PLUGIN_INSTALL') . ' : ' . WFText::_('WF_INSTALLER_MKDIR_ERROR') . ' : "' . $this->parent->getPath('extension_root') . '"');
  99. return false;
  100. }
  101. }
  102. // Set overwrite flag if not set by Manifest
  103. $this->parent->setOverwrite(true);
  104. /*
  105. * If we created the extension directory and will want to remove it if we
  106. * have to roll back the installation, lets add it to the installation
  107. * step stack
  108. */
  109. if ($created) {
  110. $this->parent->pushStep(array(
  111. 'type' => 'folder',
  112. 'path' => $this->parent->getPath('extension_root')
  113. ));
  114. }
  115. // Copy all necessary files
  116. if (!$this->parent->parseFiles($this->get('files'), -1)) {
  117. // Install failed, roll back changes
  118. $this->parent->abort();
  119. return false;
  120. }
  121. // install languages
  122. $this->parent->parseLanguages($this->get('languages'), 0);
  123. // install media
  124. $this->parent->parseMedia($this->get('media'), 0);
  125. // Load the language file
  126. $language = JFactory::getLanguage();
  127. $language->load('com_jce_' . trim($plugin), JPATH_SITE);
  128. $install = (string) $this->get('install.script');
  129. if ($install) {
  130. // Make sure it hasn't already been copied (this would be an error in the xml install file)
  131. if (!file_exists($this->parent->getPath('extension_root') . '/' . $install)) {
  132. $path['src'] = $this->parent->getPath('source') . '/' . $install;
  133. $path['dest'] = $this->parent->getPath('extension_root') . '/' . $install;
  134. if (!$this->parent->copyFiles(array($path))) {
  135. // Install failed, rollback changes
  136. $this->parent->abort(WFText::_('WF_INSTALLER_PLUGIN_INSTALL') . ' : ' . WFText::_('WF_INSTALLER_PHP_INSTALL_FILE_ERROR'));
  137. return false;
  138. }
  139. }
  140. }
  141. $uninstall = $this->get('uninstall.script');
  142. if ($uninstall) {
  143. // Make sure it hasn't already been copied (this would be an error in the xml install file)
  144. if (!file_exists($this->parent->getPath('extension_root') . '/' . $uninstall)) {
  145. $path['src'] = $this->parent->getPath('source') . '/' . $uninstall;
  146. $path['dest'] = $this->parent->getPath('extension_root') . '/' . $uninstall;
  147. if (!$this->parent->copyFiles(array(
  148. $path
  149. ))) {
  150. // Install failed, rollback changes
  151. $this->parent->abort(JText('WF_INSTALLER_PLUGIN_INSTALL') . ' : ' . WFText::_('WF_INSTALLER_PHP_UNINSTALL_FILE_ERROR'));
  152. return false;
  153. }
  154. }
  155. }
  156. /**
  157. * ---------------------------------------------------------------------------------------------
  158. * Finalization and Cleanup Section
  159. * ---------------------------------------------------------------------------------------------
  160. */
  161. // Lastly, we will copy the manifest file to its appropriate place.
  162. if (!$this->parent->copyManifest(-1)) {
  163. // Install failed, rollback changes
  164. $this->parent->abort(WFText::_('WF_INSTALLER_PLUGIN_INSTALL') . ' : ' . WFText::_('WF_INSTALLER_SETUP_COPY_ERROR'));
  165. return false;
  166. }
  167. if ($install) {
  168. if (file_exists($this->parent->getPath('extension_root') . '/' . $install)) {
  169. ob_start();
  170. ob_implicit_flush(false);
  171. require_once($this->parent->getPath('extension_root') . '/' . $install);
  172. if (function_exists('jce_install')) {
  173. if (jce_install() === false) {
  174. $this->parent->abort(WFText::_('WF_INSTALLER_PLUGIN_INSTALL') . ' : ' . WFText::_('WF_INSTALLER_CUSTOM_INSTALL_ERROR'));
  175. return false;
  176. }
  177. } else if (function_exists('com_install')) {
  178. if (com_install() === false) {
  179. $this->parent->abort(WFText::_('WF_INSTALLER_PLUGIN_INSTALL') . ' : ' . WFText::_('WF_INSTALLER_CUSTOM_INSTALL_ERROR'));
  180. return false;
  181. }
  182. }
  183. $msg = ob_get_contents();
  184. ob_end_clean();
  185. if ($msg != '') {
  186. $this->parent->set('extension.message', $msg);
  187. }
  188. }
  189. } else {
  190. $this->parent->set('extension.message', '');
  191. }
  192. $plugin = new StdClass();
  193. $plugin->name = $this->get('plugin');
  194. $plugin->icon = $this->parent->get('icon');
  195. $plugin->row = (int) $this->get('row');
  196. $plugin->path = $this->parent->getPath('extension_root');
  197. $plugin->type = $type;
  198. wfimport('admin.models.plugins');
  199. $model = new WFModelPlugins();
  200. $model->postInstall('install', $plugin, $this);
  201. return true;
  202. }
  203. /**
  204. * Uninstall method
  205. *
  206. * @access public
  207. * @param string $name The name of the plugin to uninstall
  208. * @return boolean True on success
  209. */
  210. public function uninstall($name) {
  211. // Initialize variables
  212. $row = null;
  213. $retval = true;
  214. $db = $this->parent->getDBO();
  215. $parts = explode('.', $name);
  216. // get name
  217. $name = array_pop($parts);
  218. // get type eg: plugin or extension
  219. $type = array_shift($parts);
  220. $this->parent->set('name', $name);
  221. // Load the language file
  222. $language = JFactory::getLanguage();
  223. switch ($type) {
  224. case 'plugin':
  225. // create $path
  226. $path = JPATH_COMPONENT_SITE . '/editor/tiny_mce/plugins/' . $name;
  227. // load language file
  228. $language->load('com_jce_' . $name, JPATH_SITE);
  229. break;
  230. case 'extension':
  231. $parts[] = $name;
  232. $path = dirname(JPATH_COMPONENT_SITE . '/editor/extensions/' . implode('/', $parts));
  233. // load language file
  234. $language->load('com_jce_' . trim(implode('_', $parts)), JPATH_SITE);
  235. break;
  236. }
  237. // Set the plugin root path
  238. $this->parent->setPath('extension_root', $path);
  239. // set manifest path
  240. $manifest = $this->parent->getPath('extension_root') . '/' . $name . '.xml';
  241. if (file_exists($manifest)) {
  242. $xml = WFXMLHelper::getXML($manifest);
  243. if (!$xml) {
  244. JError::raiseWarning(100, WFText::_('WF_INSTALLER_PLUGIN_UNINSTALL') . ' : ' . WFText::_('WF_INSTALLER_MANIFEST_INVALID'));
  245. }
  246. $this->parent->set('name', (string) $xml->name);
  247. $this->parent->set('version', (string) $xml->version);
  248. $this->parent->set('message', (string) $xml->description);
  249. // can't remove a core plugin
  250. if ((int) $xml->attributes()->core == 1) {
  251. JError::raiseWarning(100, WFText::_('WF_INSTALLER_PLUGIN_UNINSTALL') . ' : ' . JText::sprintf('WF_INSTALLER_WARNCOREPLUGIN', WFText::_((string) $xml->name)));
  252. return false;
  253. }
  254. if ($type == 'extension') {
  255. $this->parent->removeFiles($xml->files, -1);
  256. JFile::delete($manifest);
  257. }
  258. // Remove all media and languages as well
  259. $this->parent->removeFiles($xml->languages, 0);
  260. $this->parent->removeFiles($xml->media, 0);
  261. /**
  262. * ---------------------------------------------------------------------------------------------
  263. * Custom Uninstallation Script Section
  264. * ---------------------------------------------------------------------------------------------
  265. */
  266. // Now lets load the uninstall file if there is one and execute the uninstall function if it exists.
  267. $uninstall = (string) $xml->children('uninstall.script');
  268. if ($uninstall) {
  269. // Element exists, does the file exist?
  270. if (is_file($this->parent->getPath('extension_root') . '/' . $uninstall)) {
  271. ob_start();
  272. ob_implicit_flush(false);
  273. require_once($this->parent->getPath('extension_root') . '/' . $uninstall);
  274. if (function_exists('com_uninstall')) {
  275. if (com_uninstall() === false) {
  276. JError::raiseWarning(100, WFText::_('WF_INSTALLER_PLUGIN_UNINSTALL') . ' : ' . WFText::_('WF_INSTALLER_CUSTOM_UNINSTALL_ERROR'));
  277. $retval = false;
  278. }
  279. }
  280. $msg = ob_get_contents();
  281. ob_end_clean();
  282. if ($msg != '') {
  283. $this->parent->set('extension.message', $msg);
  284. }
  285. }
  286. }
  287. // remove form profile
  288. if ($xml->icon) {
  289. $plugin = new StdClass();
  290. $plugin->name = (string) $xml->plugin;
  291. $plugin->icon = (string) $xml->icon;
  292. $plugin->path = $this->parent->getPath('extension_root');
  293. wfimport('admin.models.plugins');
  294. $model = new WFModelPlugins();
  295. $model->postInstall('uninstall', $plugin, $this);
  296. }
  297. } else {
  298. JError::raiseWarning(100, WFText::_('WF_INSTALLER_PLUGIN_UNINSTALL') . ' : ' . WFText::_('WF_INSTALLER_MANIFEST_ERROR'));
  299. $retval = false;
  300. }
  301. // set plugin path
  302. $path = $this->parent->getPath('extension_root');
  303. // set extension path
  304. if ($type == 'extension') {
  305. $path = $this->parent->getPath('extension_root') . '/' . $name;
  306. }
  307. if (JFolder::exists($path)) {
  308. // remove the plugin folder
  309. if (!JFolder::delete($path)) {
  310. JError::raiseWarning(100, WFText::_('WF_INSTALLER_PLUGIN_UNINSTALL') . ' : ' . WFText::_('WF_INSTALLER_PLUGIN_FOLDER_ERROR'));
  311. $retval = false;
  312. }
  313. }
  314. return $retval;
  315. }
  316. }