PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/redirect/code/trunk/administrator/components/com_redirect/install/helper.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 644 lines | 401 code | 70 blank | 173 comment | 64 complexity | cd9f759d02626ff68711e169a9eaaf01 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: helper.php 574 2011-02-25 04:59:03Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_redirect
  6. * @copyright Copyright 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. * @link http://www.theartofjoomla.com
  9. * @author Andrew Eddie <andrew.eddie@newlifeinit.com>
  10. */
  11. // no direct access
  12. defined('_JEXEC') or die;
  13. /**
  14. * Installation helper.
  15. *
  16. * Changelog:
  17. * + Modules won't be added to the table if they exist already.
  18. * + Added showtitle attribute for modules.
  19. * + Added published attribute for modules.
  20. *
  21. * @package NewLifeInIT
  22. * @subpackage com_redirect
  23. * @version 1.0.1
  24. */
  25. abstract class PackageInstallerHelper
  26. {
  27. /**
  28. * Display the results of the package install.
  29. *
  30. * @param array $modules An array of the modules that were installed.
  31. * @param array $plugins An array of the plugins that were installed.
  32. * @param string $title The page title.
  33. *
  34. * @return void
  35. * @since 1.0
  36. */
  37. public static function displayInstalled(&$modules, &$plugins, $title)
  38. {
  39. ?>
  40. <?php echo $title;?>
  41. <table class="adminlist">
  42. <thead>
  43. <tr>
  44. <th class="title" colspan="2"><?php echo JText::_('J_INSTALL_EXTENSION'); ?></th>
  45. <th width="30%"><?php echo JText::_('J_INSTALL_STATUS'); ?></th>
  46. </tr>
  47. </thead>
  48. <tfoot>
  49. <tr>
  50. <td colspan="3"></td>
  51. </tr>
  52. </tfoot>
  53. <tbody>
  54. <tr class="row0">
  55. <td class="key" colspan="2"><?php echo JText::sprintf('J_INSTALL_COMPONENT', 'Redirect'); ?></td>
  56. <td><strong><?php echo JText::_('J_INSTALL_INSTALLED'); ?></strong></td>
  57. </tr>
  58. <?php if ($modules) : ?>
  59. <tr>
  60. <th><?php echo JText::_('J_INSTALL_MODULE'); ?></th>
  61. <th><?php echo JText::_('J_INSTALL_CLIENT'); ?></th>
  62. <th></th>
  63. </tr>
  64. <?php foreach ($modules as $i => $module) : ?>
  65. <tr class="row<?php echo ($i % 2); ?>">
  66. <td class="key"><?php echo $module['name']; ?></td>
  67. <td class="key"><?php echo ucfirst($module['client']); ?></td>
  68. <td><strong><?php echo JText::_('J_INSTALL_INSTALLED'); ?></strong></td>
  69. </tr>
  70. <?php endforeach;
  71. endif;
  72. if ($plugins) : ?>
  73. <tr>
  74. <th><?php echo JText::_('J_INSTALL_PLUGIN'); ?></th>
  75. <th><?php echo JText::_('J_INSTALL_GROUP'); ?></th>
  76. <th></th>
  77. </tr>
  78. <?php foreach ($plugins as $i => $plugin) : ?>
  79. <tr class="row<?php echo ($i % 2); ?>">
  80. <td class="key"><?php echo ucfirst($plugin['name']); ?></td>
  81. <td class="key"><?php echo ucfirst($plugin['group']); ?></td>
  82. <td><strong><?php echo JText::_('J_INSTALL_INSTALLED'); ?></strong></td>
  83. </tr>
  84. <?php endforeach;
  85. endif; ?>
  86. </tbody>
  87. </table>
  88. <?php
  89. }
  90. /**
  91. * Display the results of the package uninstall.
  92. *
  93. * @param array $modules An array of the modules that were installed.
  94. * @param array $plugins An array of the plugins that were installed.
  95. * @param string $title The page title.
  96. *
  97. * @return void
  98. * @since 1.0
  99. */
  100. public static function displayUninstalled(&$modules, &$plugins, $title)
  101. {
  102. ?>
  103. <h2><?php echo $title;?></h2>
  104. <table class="adminlist">
  105. <thead>
  106. <tr>
  107. <th class="title" colspan="2"><?php echo JText::_('J_INSTALL_EXTENSION'); ?></th>
  108. <th width="30%"><?php echo JText::_('J_INSTALL_STATUS'); ?></th>
  109. </tr>
  110. </thead>
  111. <tfoot>
  112. <tr>
  113. <td colspan="3"></td>
  114. </tr>
  115. </tfoot>
  116. <tbody>
  117. <tr class="row0">
  118. <td class="key" colspan="2"><?php echo JText::sprintf('J_INSTALL_COMPONENT', 'Redirect'); ?></td>
  119. <td><strong><?php echo JText::_('J_INSTALL_REMOVED'); ?></strong></td>
  120. </tr>
  121. <?php if ($modules) : ?>
  122. <tr>
  123. <th><?php echo JText::_('J_INSTALL_MODULE'); ?></th>
  124. <th><?php echo JText::_('J_INSTALL_CLIENT'); ?></th>
  125. <th></th>
  126. </tr>
  127. <?php foreach ($modules as $i => $module) : ?>
  128. <tr class="row<?php echo ($i % 2); ?>">
  129. <td class="key"><?php echo $module['name']; ?></td>
  130. <td class="key"><?php echo ucfirst($module['client']); ?></td>
  131. <td><strong><?php echo JText::_('J_INSTALL_REMOVED'); ?></strong></td>
  132. </tr>
  133. <?php endforeach;
  134. endif;
  135. if ($plugins) : ?>
  136. <tr>
  137. <th><?php echo JText::_('J_INSTALL_PLUGIN'); ?></th>
  138. <th><?php echo JText::_('J_INSTALL_GROUP'); ?></th>
  139. <th></th>
  140. </tr>
  141. <?php foreach ($plugins as $i => $plugin) : ?>
  142. <tr class="row<?php echo ($i % 2); ?>">
  143. <td class="key"><?php echo ucfirst($plugin['name']); ?></td>
  144. <td class="key"><?php echo ucfirst($plugin['group']); ?></td>
  145. <td><strong><?php echo JText::_('J_INSTALL_REMOVED'); ?></strong></td>
  146. </tr>
  147. <?php endforeach;
  148. endif; ?>
  149. </tbody>
  150. </table>
  151. <?php
  152. }
  153. /**
  154. * Fixes a bug in the components table for backend only extensions.
  155. *
  156. * @param string $option The name of the component folder.
  157. *
  158. * @return void
  159. * @since 1.0
  160. */
  161. public static function fixLink($option)
  162. {
  163. // Insert a new installation record in the version log if no rows are present.
  164. $db = JFactory::getDBO();
  165. // Correct bug in components table for backend only extenions
  166. $db->setQuery(
  167. 'UPDATE `#__components` SET `link` = '.$db->quote('').' WHERE `option` = '.$db->quote($option)
  168. );
  169. if (!$db->query()) {
  170. JError::raiseWarning(500, $db->getErrorMsg());
  171. }
  172. }
  173. /**
  174. * Install packaged modules.
  175. *
  176. * @param object $installer The parent installer object.
  177. *
  178. * @return array An array of the installed modules.
  179. * @since 1.0
  180. */
  181. public static function installModules(&$installer)
  182. {
  183. $result = array();
  184. $modules = &$installer->manifest->getElementByPath('modules');
  185. if (is_a($modules, 'JSimpleXMLElement') && count($modules->children())) {
  186. foreach ($modules->children() as $module)
  187. {
  188. $mname = $module->attributes('module');
  189. $mclient = JApplicationHelper::getClientInfo($module->attributes('client'), true);
  190. // Set the installation path
  191. if (!empty ($mname)) {
  192. $installer->parent->setPath('extension_root', $mclient->path.'/modules/'.$mname);
  193. }
  194. else {
  195. $installer->parent->abort(JText::_('J_Install_Module').' '.JText::_('J_INSTALL_INSTALL').': '.JText::_('J_INSTALL_MODULE_FILE_MISSING'));
  196. return false;
  197. }
  198. // If the module directory already exists, then we will assume that the
  199. // module is already installed or another module is using that directory.
  200. if (file_exists($installer->parent->getPath('extension_root'))&&!$installer->parent->getOverwrite()) {
  201. $installer->parent->abort(JText::_('J_Install_Module').' '.JText::_('J_INSTALL_INSTALL').': '.JText::sprintf('J_INSTALL_MODULE_PATH_CONFLICT', $installer->parent->getPath('extension_root')));
  202. return false;
  203. }
  204. // If the module directory does not exist, lets create it
  205. $created = false;
  206. if (!file_exists($installer->parent->getPath('extension_root'))) {
  207. if (!$created = JFolder::create($installer->parent->getPath('extension_root'))) {
  208. $installer->parent->abort(JText::_('J_Install_Module').' '.JText::_('J_INSTALL_INSTALL').': '.JText::sprintf('J_INSTALL_MODULE_PATH_CREATE_FAILURE', $installer->parent->getPath('extension_root')));
  209. return false;
  210. }
  211. }
  212. // Since we created the module directory and will want to remove it if
  213. // we have to roll back the installation, lets add it to the
  214. // installation step stack
  215. if ($created) {
  216. $installer->parent->pushStep(array ('type' => 'folder', 'path' => $installer->parent->getPath('extension_root')));
  217. }
  218. // Copy all necessary files
  219. $element = &$module->getElementByPath('files');
  220. if ($installer->parent->parseFiles($element, -1) === false) {
  221. // Install failed, roll back changes
  222. $installer->parent->abort();
  223. return false;
  224. }
  225. // Copy language files
  226. $element = &$module->getElementByPath('languages');
  227. if ($installer->parent->parseLanguages($element, $mclient->id) === false) {
  228. // Install failed, roll back changes
  229. $installer->parent->abort();
  230. return false;
  231. }
  232. // Copy media files
  233. $element = &$module->getElementByPath('media');
  234. if ($installer->parent->parseMedia($element, $mclient->id) === false) {
  235. // Install failed, roll back changes
  236. $installer->parent->abort();
  237. return false;
  238. }
  239. $mtitle = $module->attributes('title');
  240. $mposition = $module->attributes('position');
  241. $mShowTitle = $module->attributes('showtitle');
  242. $mPublished = $module->attributes('published');
  243. if ($mtitle && $mposition) {
  244. // Check if module is already installed.
  245. $db = JFactory::getDBO();
  246. $db->setQuery(
  247. 'SELECT id' .
  248. ' FROM #__modules' .
  249. ' WHERE client_id = '.(int) $mclient->id.
  250. ' AND module = '.$db->quote($mname)
  251. );
  252. $installed = $db->loadResult();
  253. $error = $db->getErrorMsg();
  254. if ($error) {
  255. $installer->parent->abort(JText::_('J_INSTALL_MODULE').' '.JText::_('J_INSTALL_INSTALL').': '.$error);
  256. return false;
  257. }
  258. if (!$installed) {
  259. $row = JTable::getInstance('module');
  260. $row->title = $mtitle;
  261. $row->ordering = $row->getNextOrder("position='".$mposition."'");
  262. $row->position = $mposition;
  263. $row->showtitle = (boolean) $mShowTitle;
  264. $row->iscore = 0;
  265. $row->access = ($mclient->id) == 1 ? 2 : 0;
  266. $row->client_id = $mclient->id;
  267. $row->module = $mname;
  268. $row->published = (boolean) $mPublished;
  269. $row->params = '';
  270. if (!$row->store()) {
  271. // Install failed, roll back changes
  272. $installer->parent->abort(JText::_('J_INSTALL_MODULE').' '.JText::_('J_INSTALL_INSTALL').': '.$row->getError());
  273. return false;
  274. }
  275. }
  276. }
  277. $result[] = array('name'=>$mname,'client'=>$mclient->name);
  278. }
  279. }
  280. return $result;
  281. }
  282. /**
  283. * Install packaged modules.
  284. *
  285. * @param object $installer The parent installer object.
  286. *
  287. * @return array An array of the installed modules.
  288. * @since 1.0
  289. */
  290. public static function installPlugins(&$installer)
  291. {
  292. $result = array();
  293. $plugins = &$installer->manifest->getElementByPath('plugins');
  294. if (is_a($plugins, 'JSimpleXMLElement') && count($plugins->children())) {
  295. foreach ($plugins->children() as $plugin)
  296. {
  297. $pname = $plugin->attributes('plugin');
  298. $pgroup = $plugin->attributes('group');
  299. // Set the installation path
  300. if (!empty($pname) && !empty($pgroup)) {
  301. $installer->parent->setPath('extension_root', JPATH_ROOT.'/plugins/'.$pgroup);
  302. } else {
  303. $installer->parent->abort(JText::_('J_INSTALL_PLUGIN').' '.JText::_('J_INSTALL_INSTALL').': '.JText::_('J_INSTALL_PLUGIN_FILE_MISSING'));
  304. return false;
  305. }
  306. /**
  307. * ---------------------------------------------------------------------------------------------
  308. * Filesystem Processing Section
  309. * ---------------------------------------------------------------------------------------------
  310. */
  311. // If the plugin directory does not exist, lets create it
  312. $created = false;
  313. if (!file_exists($installer->parent->getPath('extension_root'))) {
  314. if (!$created = JFolder::create($installer->parent->getPath('extension_root'))) {
  315. $installer->parent->abort(JText::_('J_INSTALL_PLUGIN').' '.JText::_('J_INSTALL_INSTALL').': '.JText::sprintf('J_INSTALL_PLUGIN_PATH_CREATE_FAILURE', $installer->parent->getPath('extension_root')));
  316. return false;
  317. }
  318. }
  319. // If we created the plugin directory and will want to remove it if we
  320. // have to roll back the installation, lets add it to the installation
  321. // step stack
  322. if ($created) {
  323. $installer->parent->pushStep(array ('type' => 'folder', 'path' => $installer->parent->getPath('extension_root')));
  324. }
  325. // Copy all necessary files
  326. $element = &$plugin->getElementByPath('files');
  327. if ($installer->parent->parseFiles($element, -1) === false) {
  328. // Install failed, roll back changes
  329. $installer->parent->abort();
  330. return false;
  331. }
  332. // Copy all necessary files
  333. $element = &$plugin->getElementByPath('languages');
  334. if ($installer->parent->parseLanguages($element, 1) === false) {
  335. // Install failed, roll back changes
  336. $installer->parent->abort();
  337. return false;
  338. }
  339. // Copy media files
  340. $element = &$plugin->getElementByPath('media');
  341. if ($installer->parent->parseMedia($element, 1) === false) {
  342. // Install failed, roll back changes
  343. $installer->parent->abort();
  344. return false;
  345. }
  346. /**
  347. * ---------------------------------------------------------------------------------------------
  348. * Database Processing Section
  349. * ---------------------------------------------------------------------------------------------
  350. */
  351. $db = &JFactory::getDBO();
  352. // Check to see if a plugin by the same name is already installed
  353. $query = 'SELECT `id`' .
  354. ' FROM `#__plugins`' .
  355. ' WHERE folder = '.$db->Quote($pgroup) .
  356. ' AND element = '.$db->Quote($pname);
  357. $db->setQuery($query);
  358. if (!$db->Query()) {
  359. // Install failed, roll back changes
  360. $installer->parent->abort(JText::_('J_INSTALL_PLUGIN').' '.JText::_('J_INSTALL_INSTALL').': '.$db->stderr(true));
  361. return false;
  362. }
  363. $id = $db->loadResult();
  364. // Was there a plugin already installed with the same name?
  365. if ($id) {
  366. if (!$installer->parent->getOverwrite()) {
  367. // Install failed, roll back changes
  368. $installer->parent->abort(JText::_('J_INSTALL_PLUGIN').' '.JText::_('J_INSTALL_INSTALL').': '.JText::sprintf('J_INSTALL_PLUGIN_ALREADY_EXISTS', $pname));
  369. return false;
  370. }
  371. } else {
  372. $row =& JTable::getInstance('plugin');
  373. $row->name = JText::_(ucfirst($pgroup)).' - '.JText::_(ucfirst($pname));
  374. $row->ordering = 0;
  375. $row->folder = $pgroup;
  376. $row->iscore = 0;
  377. $row->access = 0;
  378. $row->client_id = 0;
  379. $row->element = $pname;
  380. $row->published = 1;
  381. $row->params = '';
  382. if (!$row->store()) {
  383. // Install failed, roll back changes
  384. $installer->parent->abort(JText::_('J_INSTALL_PLUGIN').' '.JText::_('J_INSTALL_INSTALL').': '.$db->stderr(true));
  385. return false;
  386. }
  387. }
  388. $result[] = array('name'=>$pname,'group'=>$pgroup);
  389. }
  390. }
  391. return $result;
  392. }
  393. /**
  394. * Check if a component exists.
  395. *
  396. * @param string $option The name of the component folder.
  397. *
  398. * @return void
  399. * @since 1.0
  400. */
  401. public static function componentExists($option)
  402. {
  403. $db = JFactory::getDbo();
  404. $db->setQuery(
  405. 'SELECT id' .
  406. ' FROM #__components' .
  407. ' WHERE `option` = '.$db->quote($option) .
  408. ' AND parent = 0'
  409. );
  410. $result = $db->loadResult();
  411. if ($error = $db->getErrorMsg()) {
  412. JError::raiseError(500, $error);
  413. }
  414. return (boolean) $result;
  415. }
  416. /**
  417. * Uninstall packaged modules.
  418. *
  419. * @param object $installer The parent installer object.
  420. *
  421. * @return array An array of the installed modules.
  422. * @since 1.0
  423. */
  424. public static function uninstallModules($installer)
  425. {
  426. $modules = &$installer->manifest->getElementByPath('modules');
  427. if (is_a($modules, 'JSimpleXMLElement') && count($modules->children())) {
  428. foreach ($modules->children() as $module)
  429. {
  430. $mname = $module->attributes('module');
  431. $mclient = JApplicationHelper::getClientInfo($module->attributes('client'), true);
  432. $mposition = $module->attributes('position');
  433. // Set the installation path
  434. if (!empty ($mname)) {
  435. $installer->parent->setPath('extension_root', $mclient->path.'/modules/'.$mname);
  436. }
  437. else {
  438. $installer->parent->abort(JText::_('J_INSTALL_MODULE').' '.JText::_('J_INSTALL_UNINSTALL').': '.JText::_('J_INSTALL_MODULE_FILE_MISSING'));
  439. return false;
  440. }
  441. /**
  442. * ---------------------------------------------------------------------------------------------
  443. * Database Processing Section
  444. * ---------------------------------------------------------------------------------------------
  445. */
  446. $db = &JFactory::getDBO();
  447. // Lets delete all the module copies for the type we are uninstalling
  448. $query = 'SELECT `id`' .
  449. ' FROM `#__modules`' .
  450. ' WHERE module = '.$db->Quote($mname) .
  451. ' AND client_id = '.(int)$mclient->id;
  452. $db->setQuery($query);
  453. $modules = $db->loadResultArray();
  454. // Do we have any module copies?
  455. if (count($modules)) {
  456. JArrayHelper::toInteger($modules);
  457. $modID = implode(',', $modules);
  458. $query = 'DELETE' .
  459. ' FROM #__modules_menu' .
  460. ' WHERE moduleid IN ('.$modID.')';
  461. $db->setQuery($query);
  462. if (!$db->query()) {
  463. JError::raiseWarning(100, JText::_('J_INSTALL_MODULE').' '.JText::_('J_INSTALL_UNINSTALL').': '.$db->stderr(true));
  464. $retval = false;
  465. }
  466. }
  467. // Delete the modules in the #__modules table
  468. $query = 'DELETE FROM #__modules WHERE module = '.$db->Quote($mname);
  469. $db->setQuery($query);
  470. if (!$db->query()) {
  471. JError::raiseWarning(100, JText::_('J_INSTALL_MODULE').' '.JText::_('J_INSTALL_UNINSTALL').': '.$db->stderr(true));
  472. $retval = false;
  473. }
  474. /**
  475. * ---------------------------------------------------------------------------------------------
  476. * Filesystem Processing Section
  477. * ---------------------------------------------------------------------------------------------
  478. */
  479. // Remove all necessary files
  480. $element = &$module->getElementByPath('files');
  481. if (is_a($element, 'JSimpleXMLElement') && count($element->children())) {
  482. $installer->parent->removeFiles($element, -1);
  483. }
  484. // Remove all necessary files
  485. $element = &$module->getElementByPath('media');
  486. if (is_a($element, 'JSimpleXMLElement') && count($element->children())) {
  487. $installer->parent->removeFiles($element, -1);
  488. }
  489. $element = &$module->getElementByPath('languages');
  490. if (is_a($element, 'JSimpleXMLElement') && count($element->children())) {
  491. $installer->parent->removeFiles($element, $mclient->id);
  492. }
  493. // Remove the installation folder
  494. if (!JFolder::delete($installer->parent->getPath('extension_root'))) {
  495. }
  496. $status->modules[] = array('name'=>$mname,'client'=>$mclient->name);
  497. }
  498. }
  499. }
  500. /**
  501. * Uninstall packaged plugins.
  502. *
  503. * @param object $installer The parent installer object.
  504. *
  505. * @return array An array of the installed modules.
  506. * @since 1.0
  507. */
  508. public static function uninstallPlugins(&$installer)
  509. {
  510. $plugins = &$installer->manifest->getElementByPath('plugins');
  511. if (is_a($plugins, 'JSimpleXMLElement') && count($plugins->children())) {
  512. foreach ($plugins->children() as $plugin)
  513. {
  514. $pname = $plugin->attributes('plugin');
  515. $pgroup = $plugin->attributes('group');
  516. // Set the installation path
  517. if (!empty($pname) && !empty($pgroup)) {
  518. $installer->parent->setPath('extension_root', JPATH_ROOT.'/plugins/'.$pgroup);
  519. } else {
  520. $installer->parent->abort(JText::_('J_INSTALL_PLUGIN').' '.JText::_('J_INSTALL_UNINSTALL').': '.JText::_('J_INSTALL_PLUGIN_FILE_MISSING'));
  521. return false;
  522. }
  523. /**
  524. * ---------------------------------------------------------------------------------------------
  525. * Database Processing Section
  526. * ---------------------------------------------------------------------------------------------
  527. */
  528. $db = &JFactory::getDBO();
  529. // Delete the plugins in the #__plugins table
  530. $query = 'DELETE FROM #__plugins WHERE element = '.$db->Quote($pname).' AND folder = '.$db->Quote($pgroup);
  531. $db->setQuery($query);
  532. if (!$db->query()) {
  533. JError::raiseWarning(100, JText::_('J_INSTALL_PLUGIN').' '.JText::_('J_INSTALL_UNINSTALL').': '.$db->stderr(true));
  534. $retval = false;
  535. }
  536. /**
  537. * ---------------------------------------------------------------------------------------------
  538. * Filesystem Processing Section
  539. * ---------------------------------------------------------------------------------------------
  540. */
  541. // Remove all necessary files
  542. $element = &$plugin->getElementByPath('files');
  543. if (is_a($element, 'JSimpleXMLElement') && count($element->children())) {
  544. $installer->parent->removeFiles($element, -1);
  545. }
  546. $element = &$plugin->getElementByPath('languages');
  547. if (is_a($element, 'JSimpleXMLElement') && count($element->children())) {
  548. $installer->parent->removeFiles($element, 1);
  549. }
  550. // If the folder is empty, let's delete it
  551. $files = JFolder::files($installer->parent->getPath('extension_root'));
  552. if (!count($files)) {
  553. JFolder::delete($installer->parent->getPath('extension_root'));
  554. }
  555. $status->plugins[] = array('name'=>$pname,'group'=>$pgroup);
  556. }
  557. }
  558. }
  559. /**
  560. * Upgrade the database with an XML schema file.
  561. *
  562. * @param string $xml The XML string.
  563. *
  564. * @return array Returns the database upgrade log.
  565. * @since 1.0
  566. */
  567. public static function upgrade($xml)
  568. {
  569. // Include dependancies.
  570. require_once dirname(dirname(__FILE__)).'/libraries/joomla/database/database/mysqlxml.php';
  571. JDatabaseMySQLXML::import($xml);
  572. return JDatabaseMySQLXML::getLog();
  573. }
  574. }