PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/googlemini/code/trunk/administrator/components/com_artofgm/install/helper.php

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