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

/administrator/components/com_jce/models/installer.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 401 lines | 267 code | 82 blank | 52 comment | 51 complexity | 887b785363e9d80ac3e8704f37012348 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('_JEXEC') or die('RESTRICTED');
  12. // load base model
  13. wfimport('admin.models.model');
  14. wfimport('admin.classes.installer');
  15. jimport('joomla.installer.helper');
  16. class WFModelInstaller extends WFModel {
  17. /** @var object JTable object */
  18. var $_table = null;
  19. /** @var object JTable object */
  20. var $_url = null;
  21. var $_result = array();
  22. public function cancel() {
  23. $this->setRedirect(JRoute::_('index.php?option=com_jce&client=' . $client, false));
  24. }
  25. public function install($package = null) {
  26. $app = JFactory::getApplication();
  27. if (!$package) {
  28. $package = $this->getPackage();
  29. }
  30. // Was the package unpacked?
  31. if (!$package) {
  32. $this->setState('message', 'WF_INSTALLER_NO_PACKAGE');
  33. return false;
  34. }
  35. // Get an installer instance
  36. $installer = WFInstaller::getInstance();
  37. // Install the package
  38. if (!$installer->install($package['dir'])) {
  39. $result = false;
  40. $app->enqueueMessage(WFText::sprintf('WF_INSTALLER_INSTALL_ERROR'), 'error');
  41. } else {
  42. $result = true;
  43. $app->enqueueMessage(WFText::sprintf('WF_INSTALLER_INSTALL_SUCCESS'));
  44. }
  45. $this->_result[] = array(
  46. 'name' => $installer->get('name'),
  47. 'type' => $package['type'],
  48. 'version' => $installer->get('version'),
  49. 'result' => $result
  50. );
  51. $this->setState('install.result', $this->_result);
  52. $this->setState('name', WFText::_($installer->get('name')));
  53. $this->setState('message', WFText::_($installer->get('message')));
  54. $this->setState('extension.message', $installer->get('extension.message'));
  55. $this->setState('result', $result);
  56. // Cleanup the install files
  57. if (!is_file($package['packagefile'])) {
  58. $package['packagefile'] = $app->getCfg('tmp_path') . '/' . $package['packagefile'];
  59. }
  60. if (is_file($package['packagefile'])) {
  61. JInstallerHelper::cleanupInstall($package['packagefile'], $package['extractdir']);
  62. }
  63. return $result;
  64. }
  65. public function remove($id, $type) {
  66. $app = JFactory::getApplication();
  67. // Use Joomla! Installer class for related extensions
  68. if ($type == 'related') {
  69. jimport('joomla.installer.installer');
  70. $installer = JInstaller::getInstance();
  71. $result = $installer->uninstall('plugin', $id);
  72. } else {
  73. $installer = WFInstaller::getInstance();
  74. $installer->setAdapter($type);
  75. $result = $installer->uninstall($type, $id);
  76. }
  77. if (!$result) {
  78. $app->enqueueMessage(WFText::sprintf('WF_INSTALLER_UNINSTALL_ERROR'), 'error');
  79. } else {
  80. $app->enqueueMessage(WFText::sprintf('WF_INSTALLER_UNINSTALL_SUCCESS'));
  81. }
  82. $this->_result[] = array(
  83. 'name' => $installer->get('name'),
  84. 'type' => $type,
  85. 'version' => $installer->get('version'),
  86. 'result' => $result
  87. );
  88. $this->setState('name', WFText::_($installer->get('name')));
  89. $this->setState('result', $result);
  90. $this->setState('install.result', $this->_result);
  91. return $result;
  92. }
  93. /**
  94. * Get the install package or folder
  95. * @return Array $package
  96. */
  97. private function getPackage() {
  98. $app = JFactory::getApplication();
  99. jimport('joomla.filesystem.file');
  100. jimport('joomla.filesystem.archive');
  101. // set standard method
  102. $upload = true;
  103. $package = null;
  104. // Get the uploaded file information
  105. $file = JRequest::getVar('install', null, 'files', 'array');
  106. // get the file path information
  107. $path = JRequest::getString('install_input');
  108. if (!(bool) ini_get('file_uploads') || !is_array($file)) {
  109. $upload = false;
  110. // no path either!
  111. if (!$path) {
  112. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_NO_FILE'));
  113. return false;
  114. }
  115. }
  116. // Install failed
  117. if (!is_uploaded_file($file['tmp_name']) || !$file['tmp_name'] || !$file['name'] || $file['error']) {
  118. $upload = false;
  119. // no path either!
  120. if (!$path) {
  121. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_NO_FILE'));
  122. return false;
  123. }
  124. }
  125. // uploaded file
  126. if ($upload) {
  127. // check extension
  128. if (!preg_match('/\.(zip|tar|gz|gzip|tgz|tbz2|bz2|bzip2)$/i', $file['name'])) {
  129. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_INVALID_FILE'));
  130. return false;
  131. }
  132. $dest = JPath::clean($app->getCfg('tmp_path') . '/' . $file['name']);
  133. $src = $file['tmp_name'];
  134. // upload file
  135. if (!JFile::upload($src, $dest)) {
  136. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_UPLOAD_FAILED'));
  137. return false;
  138. }
  139. if (!is_file($dest)) {
  140. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_UPLOAD_FAILED'));
  141. return false;
  142. }
  143. // path to file
  144. } else {
  145. $dest = JPath::clean($path);
  146. }
  147. // set install method
  148. JRequest::setVar('install_method', 'install');
  149. // Unpack the package file
  150. if (preg_match('/\.(zip|tar|gz|gzip|tgz|tbz2|bz2|bzip2)/i', $dest)) {
  151. // Make sure that zlib is loaded so that the package can be unpacked
  152. if (!extension_loaded('zlib')) {
  153. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_WARNINSTALLZLIB'));
  154. return false;
  155. }
  156. $package = JPath::clean(dirname($dest) . '/' . uniqid('install_'));
  157. if (!JArchive::extract($dest, $package)) {
  158. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_EXTRACT_ERROR'));
  159. return false;
  160. }
  161. if (JFolder::exists($package)) {
  162. $type = self::detectType($package);
  163. }
  164. return array(
  165. 'manifest' => null,
  166. 'packagefile' => $dest,
  167. 'extractdir' => $package,
  168. 'dir' => $package,
  169. 'type' => $type
  170. );
  171. // might be a directory
  172. } else {
  173. if (!is_dir($dest)) {
  174. JError::raiseWarning('SOME_ERROR_CODE', WFText::_('WF_INSTALLER_INVALID_SRC'));
  175. return false;
  176. }
  177. // Detect the package type
  178. $type = self::detectType($dest);
  179. return array(
  180. 'manifest' => null,
  181. 'packagefile' => null,
  182. 'extractdir' => null,
  183. 'dir' => $dest,
  184. 'type' => $type
  185. );
  186. }
  187. }
  188. private static function detectType($path) {
  189. // Search the install dir for an XML file
  190. $files = JFolder::files($path, '\.xml$', 1, true);
  191. if (!count($files)) {
  192. return false;
  193. }
  194. foreach ($files as $file) {
  195. $xml = simplexml_load_file($file);
  196. if (!$xml) {
  197. continue;
  198. }
  199. $name = (string) $xml->getName();
  200. if ($name != 'extension' && $name != 'install') {
  201. unset($xml);
  202. continue;
  203. }
  204. $type = (string) $xml->attributes()->type;
  205. // Free up memory
  206. unset($xml);
  207. return $type;
  208. }
  209. return false;
  210. }
  211. public function getExtensions() {
  212. wfimport('admin.models.plugins');
  213. $model = new WFModelPlugins();
  214. // get an array of all installed plugins in plugins folder
  215. $extensions = $model->getExtensions();
  216. return $extensions;
  217. }
  218. public function getPlugins() {
  219. wfimport('admin.models.plugins');
  220. $model = new WFModelPlugins();
  221. // get an array of all installed plugins in plugins folder
  222. $plugins = $model->getPlugins();
  223. $rows = array();
  224. $language = JFactory::getLanguage();
  225. foreach ($plugins as $plugin) {
  226. if ($plugin->core == 0) {
  227. $rows[] = $plugin;
  228. $language->load('com_jce_' . trim($plugin->name), JPATH_SITE);
  229. }
  230. }
  231. return $rows;
  232. }
  233. /**
  234. * Get additional plugins such as JCE MediaBox etc.
  235. * @return
  236. */
  237. public function getRelated() {
  238. // Get a database connector
  239. $db = JFactory::getDBO();
  240. $params = JComponentHelper::getParams('com_jce');
  241. // pre-defined array of other plugins
  242. $related = preg_replace('#(\w+)#', "'$1'", $params->get('related_extensions', 'jcemediabox,jceutilities,mediaobject,wfmediabox'));
  243. $query = $db->getQuery(true);
  244. // Joomla! 2.5
  245. if (is_object($query)) {
  246. $query->select(array('extension_id', 'name', 'element', 'folder'))->from('#__extensions')->where(array('type = ' . $db->Quote('plugin'), 'element IN (' . $related . ')'))->order('name');
  247. // Joomla! 1.5
  248. } else {
  249. $query = 'SELECT id, name, element, folder FROM #__plugins WHERE element IN (' . $related . ') ORDER BY name';
  250. }
  251. $db->setQuery($query);
  252. $rows = $db->loadObjectList();
  253. $language = JFactory::getLanguage();
  254. $num = count($rows);
  255. for ($i = 0; $i < $num; $i++) {
  256. $row = $rows[$i];
  257. if (defined('JPATH_PLATFORM')) {
  258. $file = JPATH_PLUGINS . '/' . $row->folder . '/' . $row->element . '/' . $row->element . ".xml";
  259. } else {
  260. $file = JPATH_PLUGINS . '/' . $row->folder . '/' . $row->element . ".xml";
  261. }
  262. if (isset($row->extension_id)) {
  263. $row->id = $row->extension_id;
  264. }
  265. if (is_file($file)) {
  266. $xml = WFXMLElement::load($file);
  267. if ($xml) {
  268. $row->title = (string) $xml->name;
  269. $row->author = (string) $xml->author;
  270. $row->version = (string) $xml->version;
  271. $row->creationdate = (string) $xml->creationDate;
  272. $row->description = (string) $xml->description;
  273. $row->authorUrl = (string) $xml->authorUrl;
  274. }
  275. }
  276. $language->load('plg_' . trim($row->folder) . '_' . trim($row->element), JPATH_ADMINISTRATOR);
  277. $language->load('plg_' . trim($row->folder) . '_' . trim($row->element), JPATH_SITE);
  278. }
  279. //return array_values($rows);
  280. return $rows;
  281. }
  282. public function getLanguages() {
  283. // Get the site languages
  284. $base = JLanguage::getLanguagePath(JPATH_SITE);
  285. $dirs = JFolder::folders($base);
  286. for ($i = 0; $i < count($dirs); $i++) {
  287. $lang = new stdClass();
  288. $lang->folder = $dirs[$i];
  289. $lang->baseDir = $base;
  290. $languages[] = $lang;
  291. }
  292. $rows = array();
  293. foreach ($languages as $language) {
  294. $files = JFolder::files($language->baseDir . '/' . $language->folder, '\.(com_jce)\.xml$');
  295. foreach ($files as $file) {
  296. $data = WFXMLHelper::parseInstallManifest($language->baseDir . '/' . $language->folder . '/' . $file);
  297. $row = new StdClass();
  298. $row->language = $language->folder;
  299. if ($row->language == 'en-GB') {
  300. $row->cbd = 'disabled="disabled"';
  301. $row->style = ' style="color:#999999;"';
  302. } else {
  303. $row->cbd = '';
  304. $row->style = '';
  305. }
  306. // If we didn't get valid data from the xml file, move on...
  307. if (!is_array($data)) {
  308. continue;
  309. }
  310. // Populate the row from the xml meta file
  311. foreach ($data as $key => $value) {
  312. $row->$key = $value;
  313. }
  314. $rows[] = $row;
  315. }
  316. }
  317. return $rows;
  318. }
  319. }