PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/joomla/installer/adapters/component.php

http://github.com/joomla/joomla-platform
PHP | 1823 lines | 1015 code | 295 blank | 513 comment | 170 complexity | 9f8a9524dc48564a57aac0fc51fd8bee MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Installer
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.base.adapterinstance');
  11. /**
  12. * Component installer
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Installer
  16. * @since 11.1
  17. */
  18. class JInstallerComponent extends JAdapterInstance
  19. {
  20. /**
  21. * Copy of the XML manifest file
  22. *
  23. * @var string
  24. * @since 11.1
  25. */
  26. protected $manifest = null;
  27. /**
  28. * Name of the extension
  29. *
  30. * @var string
  31. * @since 11.1
  32. * */
  33. protected $name = null;
  34. /**
  35. * The unique identifier for the extension (e.g. mod_login)
  36. *
  37. * @var string
  38. * @since 11.1
  39. * */
  40. protected $element = null;
  41. /**
  42. * The list of current files fo the Joomla! CMS administrator that are installed and is read
  43. * from the manifest on disk in the update area to handle doing a diff
  44. * and deleting files that are in the old files list and not in the new
  45. * files list.
  46. *
  47. * @var array
  48. * @since 11.1
  49. * */
  50. protected $oldAdminFiles = null;
  51. /**
  52. * The list of current files that are installed and is read
  53. * from the manifest on disk in the update area to handle doing a diff
  54. * and deleting files that are in the old files list and not in the new
  55. * files list.
  56. *
  57. * @var array
  58. * @since 11.1
  59. * */
  60. protected $oldFiles = null;
  61. /**
  62. * A path to the PHP file that the scriptfile declaration in
  63. * the manifest refers to.
  64. *
  65. * @var string
  66. * @since 11.1
  67. * */
  68. protected $manifest_script = null;
  69. /**
  70. * For legacy installations this is a path to the PHP file that the scriptfile declaration in the
  71. * manifest refers to.
  72. *
  73. * @var string
  74. * @since 11.1
  75. * */
  76. protected $install_script = null;
  77. /**
  78. * Custom loadLanguage method
  79. *
  80. * @param string $path The path language files are on.
  81. *
  82. * @return void
  83. *
  84. * @since 11.1
  85. */
  86. public function loadLanguage($path = null)
  87. {
  88. $source = $this->parent->getPath('source');
  89. if (!$source)
  90. {
  91. $this->parent
  92. ->setPath(
  93. 'source',
  94. ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) .
  95. '/components/' . $this->parent->extension->element
  96. );
  97. }
  98. $this->manifest = $this->parent->getManifest();
  99. $name = strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  100. if (substr($name, 0, 4) == "com_")
  101. {
  102. $extension = $name;
  103. }
  104. else
  105. {
  106. $extension = "com_$name";
  107. }
  108. $lang = JFactory::getLanguage();
  109. $source = $path ? $path : ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/components/' . $extension;
  110. if ($this->manifest->administration->files)
  111. {
  112. $element = $this->manifest->administration->files;
  113. }
  114. elseif ($this->manifest->files)
  115. {
  116. $element = $this->manifest->files;
  117. }
  118. else
  119. {
  120. $element = null;
  121. }
  122. if ($element)
  123. {
  124. $folder = (string) $element->attributes()->folder;
  125. if ($folder && file_exists("$path/$folder"))
  126. {
  127. $source = "$path/$folder";
  128. }
  129. }
  130. $lang->load($extension . '.sys', $source, null, false, false) || $lang->load($extension . '.sys', JPATH_ADMINISTRATOR, null, false, false)
  131. || $lang->load($extension . '.sys', $source, $lang->getDefault(), false, false)
  132. || $lang->load($extension . '.sys', JPATH_ADMINISTRATOR, $lang->getDefault(), false, false);
  133. }
  134. /**
  135. * Custom install method for components
  136. *
  137. * @return boolean True on success
  138. *
  139. * @since 11.1
  140. */
  141. public function install()
  142. {
  143. // Get a database connector object
  144. $db = $this->parent->getDbo();
  145. // Get the extension manifest object
  146. $this->manifest = $this->parent->getManifest();
  147. // Manifest Document Setup Section
  148. // Set the extension's name
  149. $name = strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  150. if (substr($name, 0, 4) == "com_")
  151. {
  152. $element = $name;
  153. }
  154. else
  155. {
  156. $element = "com_$name";
  157. }
  158. $this->set('name', $name);
  159. $this->set('element', $element);
  160. // Get the component description
  161. $this->parent->set('message', JText::_((string) $this->manifest->description));
  162. // Set the installation target paths
  163. $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE . '/components/' . $this->get('element')));
  164. $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $this->get('element')));
  165. // Copy this as its used as a common base
  166. $this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator'));
  167. // Basic Checks Section
  168. // Make sure that we have an admin element
  169. if (!$this->manifest->administration)
  170. {
  171. JError::raiseWarning(1, JText::_('JLIB_INSTALLER_ERROR_COMP_INSTALL_ADMIN_ELEMENT'));
  172. return false;
  173. }
  174. // Filesystem Processing Section
  175. // If the component site or admin directory already exists, then we will assume that the component is already
  176. // installed or another component is using that directory.
  177. if (file_exists($this->parent->getPath('extension_site')) || file_exists($this->parent->getPath('extension_administrator')))
  178. {
  179. // Look for an update function or update tag
  180. $updateElement = $this->manifest->update;
  181. /*
  182. * Upgrade manually set or
  183. * Update function available or
  184. * Update tag detected
  185. */
  186. if ($this->parent->isUpgrade() || ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'update'))
  187. || $updateElement)
  188. {
  189. // Transfer control to the update function
  190. return $this->update();
  191. }
  192. elseif (!$this->parent->isOverwrite())
  193. {
  194. // Overwrite is set.
  195. // We didn't have overwrite set, find an update function or find an update tag so lets call it safe
  196. if (file_exists($this->parent->getPath('extension_site')))
  197. {
  198. // If the site exists say so.
  199. JError::raiseWarning(1, JText::sprintf('JLIB_INSTALLER_ERROR_COMP_INSTALL_DIR_SITE', $this->parent->getPath('extension_site')));
  200. }
  201. else
  202. {
  203. // If the admin exists say so
  204. JError::raiseWarning(
  205. 1,
  206. JText::sprintf('JLIB_INSTALLER_ERROR_COMP_INSTALL_DIR_ADMIN', $this->parent->getPath('extension_administrator'))
  207. );
  208. }
  209. return false;
  210. }
  211. }
  212. // Installer Trigger Loading
  213. // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
  214. $manifestScript = (string) $this->manifest->scriptfile;
  215. if ($manifestScript)
  216. {
  217. $manifestScriptFile = $this->parent->getPath('source') . '/' . $manifestScript;
  218. if (is_file($manifestScriptFile))
  219. {
  220. // Load the file
  221. include_once $manifestScriptFile;
  222. }
  223. // Set the class name
  224. $classname = $this->get('element') . 'InstallerScript';
  225. if (class_exists($classname))
  226. {
  227. // Create a new instance
  228. $this->parent->manifestClass = new $classname($this);
  229. // And set this so we can copy it later
  230. $this->set('manifest_script', $manifestScript);
  231. // Note: if we don't find the class, don't bother to copy the file
  232. }
  233. }
  234. // Run preflight if possible (since we know we're not an update)
  235. ob_start();
  236. ob_implicit_flush(false);
  237. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'preflight'))
  238. {
  239. if ($this->parent->manifestClass->preflight('install', $this) === false)
  240. {
  241. // Install failed, rollback changes
  242. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_CUSTOM_INSTALL_FAILURE'));
  243. return false;
  244. }
  245. }
  246. // Create msg object; first use here
  247. $msg = ob_get_contents();
  248. ob_end_clean();
  249. // If the component directory does not exist, let's create it
  250. $created = false;
  251. if (!file_exists($this->parent->getPath('extension_site')))
  252. {
  253. if (!$created = JFolder::create($this->parent->getPath('extension_site')))
  254. {
  255. JError::raiseWarning(
  256. 1,
  257. JText::sprintf('JLIB_INSTALLER_ERROR_COMP_INSTALL_FAILED_TO_CREATE_DIRECTORY_SITE', $this->parent->getPath('extension_site'))
  258. );
  259. return false;
  260. }
  261. }
  262. // Since we created the component directory and will want to remove it if we have to roll back
  263. // the installation, let's add it to the installation step stack
  264. if ($created)
  265. {
  266. $this->parent->pushStep(array('type' => 'folder', 'path' => $this->parent->getPath('extension_site')));
  267. }
  268. // If the component admin directory does not exist, let's create it
  269. $created = false;
  270. if (!file_exists($this->parent->getPath('extension_administrator')))
  271. {
  272. if (!$created = JFolder::create($this->parent->getPath('extension_administrator')))
  273. {
  274. JError::raiseWarning(
  275. 1,
  276. JText::sprintf(
  277. 'JLIB_INSTALLER_ERROR_COMP_INSTALL_FAILED_TO_CREATE_DIRECTORY_ADMIN',
  278. $this->parent->getPath('extension_administrator')
  279. )
  280. );
  281. // Install failed, rollback any changes
  282. $this->parent->abort();
  283. return false;
  284. }
  285. }
  286. /*
  287. * Since we created the component admin directory and we will want to remove it if we have to roll
  288. * back the installation, let's add it to the installation step stack
  289. */
  290. if ($created)
  291. {
  292. $this->parent->pushStep(array('type' => 'folder', 'path' => $this->parent->getPath('extension_administrator')));
  293. }
  294. // Copy site files
  295. if ($this->manifest->files)
  296. {
  297. if ($this->parent->parseFiles($this->manifest->files) === false)
  298. {
  299. // Install failed, rollback any changes
  300. $this->parent->abort();
  301. return false;
  302. }
  303. }
  304. // Copy admin files
  305. if ($this->manifest->administration->files)
  306. {
  307. if ($this->parent->parseFiles($this->manifest->administration->files, 1) === false)
  308. {
  309. // Install failed, rollback any changes
  310. $this->parent->abort();
  311. return false;
  312. }
  313. }
  314. // Parse optional tags
  315. $this->parent->parseMedia($this->manifest->media);
  316. $this->parent->parseLanguages($this->manifest->languages);
  317. $this->parent->parseLanguages($this->manifest->administration->languages, 1);
  318. // If there is a manifest script, let's copy it.
  319. if ($this->get('manifest_script'))
  320. {
  321. $path['src'] = $this->parent->getPath('source') . '/' . $this->get('manifest_script');
  322. $path['dest'] = $this->parent->getPath('extension_administrator') . '/' . $this->get('manifest_script');
  323. if (!file_exists($path['dest']) || $this->parent->isOverwrite())
  324. {
  325. if (!$this->parent->copyFiles(array($path)))
  326. {
  327. // Install failed, rollback changes
  328. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_MANIFEST'));
  329. return false;
  330. }
  331. }
  332. }
  333. /**
  334. * ---------------------------------------------------------------------------------------------
  335. * Database Processing Section
  336. * ---------------------------------------------------------------------------------------------
  337. */
  338. /*
  339. * Let's run the install queries for the component
  340. * If Joomla 1.5 compatible, with discreet sql files - execute appropriate
  341. * file for utf-8 support or non-utf-8 support
  342. */
  343. // Try for Joomla 1.5 type queries
  344. // Second argument is the utf compatible version attribute
  345. if (isset($this->manifest->install->sql))
  346. {
  347. $utfresult = $this->parent->parseSQLFiles($this->manifest->install->sql);
  348. if ($utfresult === false)
  349. {
  350. // Install failed, rollback changes
  351. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_SQL_ERROR', $db->stderr(true)));
  352. return false;
  353. }
  354. }
  355. /**
  356. * ---------------------------------------------------------------------------------------------
  357. * Custom Installation Script Section
  358. * ---------------------------------------------------------------------------------------------
  359. */
  360. /*
  361. * If we have an install script, let's include it, execute the custom
  362. * install method, and append the return value from the custom install
  363. * method to the installation message.
  364. */
  365. ob_start();
  366. ob_implicit_flush(false);
  367. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'install'))
  368. {
  369. if ($this->parent->manifestClass->install($this) === false)
  370. {
  371. // Install failed, rollback changes
  372. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_CUSTOM_INSTALL_FAILURE'));
  373. return false;
  374. }
  375. }
  376. // Append messages
  377. $msg .= ob_get_contents();
  378. ob_end_clean();
  379. /**
  380. * ---------------------------------------------------------------------------------------------
  381. * Finalization and Cleanup Section
  382. * ---------------------------------------------------------------------------------------------
  383. */
  384. // Add an entry to the extension table with a whole heap of defaults
  385. $row = JTable::getInstance('extension');
  386. $row->set('name', $this->get('name'));
  387. $row->set('type', 'component');
  388. $row->set('element', $this->get('element'));
  389. // There is no folder for components
  390. $row->set('folder', '');
  391. $row->set('enabled', 1);
  392. $row->set('protected', 0);
  393. $row->set('access', 0);
  394. $row->set('client_id', 1);
  395. $row->set('params', $this->parent->getParams());
  396. $row->set('manifest_cache', $this->parent->generateManifestCache());
  397. if (!$row->store())
  398. {
  399. // Install failed, roll back changes
  400. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_ROLLBACK', $db->stderr(true)));
  401. return false;
  402. }
  403. $eid = $db->insertid();
  404. // Clobber any possible pending updates
  405. $update = JTable::getInstance('update');
  406. $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => '', 'folder' => ''));
  407. if ($uid)
  408. {
  409. $update->delete($uid);
  410. }
  411. // We will copy the manifest file to its appropriate place.
  412. if (!$this->parent->copyManifest())
  413. {
  414. // Install failed, rollback changes
  415. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_COPY_SETUP'));
  416. return false;
  417. }
  418. // Time to build the admin menus
  419. if (!$this->_buildAdminMenus($row->extension_id))
  420. {
  421. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ABORT_COMP_BUILDADMINMENUS_FAILED'));
  422. // @todo remove code: $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_ROLLBACK', $db->stderr(true)));
  423. // @todo remove code: return false;
  424. }
  425. // Set the schema version to be the latest update version
  426. if ($this->manifest->update)
  427. {
  428. $this->parent->setSchemaVersion($this->manifest->update->schemas, $eid);
  429. }
  430. // Register the component container just under root in the assets table.
  431. $asset = JTable::getInstance('Asset');
  432. $asset->name = $row->element;
  433. $asset->parent_id = 1;
  434. $asset->rules = '{}';
  435. $asset->title = $row->name;
  436. $asset->setLocation(1, 'last-child');
  437. if (!$asset->store())
  438. {
  439. // Install failed, roll back changes
  440. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_ROLLBACK', $db->stderr(true)));
  441. return false;
  442. }
  443. // And now we run the postflight
  444. ob_start();
  445. ob_implicit_flush(false);
  446. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'postflight'))
  447. {
  448. $this->parent->manifestClass->postflight('install', $this);
  449. }
  450. // Append messages
  451. $msg .= ob_get_contents();
  452. ob_end_clean();
  453. if ($msg != '')
  454. {
  455. $this->parent->set('extension_message', $msg);
  456. }
  457. return $row->extension_id;
  458. }
  459. /**
  460. * Custom update method for components
  461. *
  462. * @return boolean True on success
  463. *
  464. * @since 11.1
  465. */
  466. public function update()
  467. {
  468. // Get a database connector object
  469. $db = $this->parent->getDbo();
  470. // Set the overwrite setting
  471. $this->parent->setOverwrite(true);
  472. // Get the extension manifest object
  473. $this->manifest = $this->parent->getManifest();
  474. /**
  475. * ---------------------------------------------------------------------------------------------
  476. * Manifest Document Setup Section
  477. * ---------------------------------------------------------------------------------------------
  478. */
  479. // Set the extension's name
  480. $name = strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  481. if (substr($name, 0, 4) == "com_")
  482. {
  483. $element = $name;
  484. }
  485. else
  486. {
  487. $element = "com_$name";
  488. }
  489. $this->set('name', $name);
  490. $this->set('element', $element);
  491. // Get the component description
  492. $description = (string) $this->manifest->description;
  493. if ($description)
  494. {
  495. $this->parent->set('message', JText::_($description));
  496. }
  497. else
  498. {
  499. $this->parent->set('message', '');
  500. }
  501. // Set the installation target paths
  502. $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE . '/components/' . $this->get('element')));
  503. $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $this->get('element')));
  504. // Copy this as its used as a common base
  505. $this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator'));
  506. /**
  507. * Hunt for the original XML file
  508. */
  509. $old_manifest = null;
  510. // Create a new installer because findManifest sets stuff
  511. // Look in the administrator first
  512. $tmpInstaller = new JInstaller;
  513. $tmpInstaller->setPath('source', $this->parent->getPath('extension_administrator'));
  514. if (!$tmpInstaller->findManifest())
  515. {
  516. // Then the site
  517. $tmpInstaller->setPath('source', $this->parent->getPath('extension_site'));
  518. if ($tmpInstaller->findManifest())
  519. {
  520. $old_manifest = $tmpInstaller->getManifest();
  521. }
  522. }
  523. else
  524. {
  525. $old_manifest = $tmpInstaller->getManifest();
  526. }
  527. // Should do this above perhaps?
  528. if ($old_manifest)
  529. {
  530. $this->oldAdminFiles = $old_manifest->administration->files;
  531. $this->oldFiles = $old_manifest->files;
  532. }
  533. else
  534. {
  535. $this->oldAdminFiles = null;
  536. $this->oldFiles = null;
  537. }
  538. /**
  539. * ---------------------------------------------------------------------------------------------
  540. * Basic Checks Section
  541. * ---------------------------------------------------------------------------------------------
  542. */
  543. // Make sure that we have an admin element
  544. if (!$this->manifest->administration)
  545. {
  546. JError::raiseWarning(1, JText::_('JLIB_INSTALLER_ABORT_COMP_UPDATE_ADMIN_ELEMENT'));
  547. return false;
  548. }
  549. /**
  550. * ---------------------------------------------------------------------------------------------
  551. * Installer Trigger Loading
  552. * ---------------------------------------------------------------------------------------------
  553. */
  554. // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
  555. $manifestScript = (string) $this->manifest->scriptfile;
  556. if ($manifestScript)
  557. {
  558. $manifestScriptFile = $this->parent->getPath('source') . '/' . $manifestScript;
  559. if (is_file($manifestScriptFile))
  560. {
  561. // Load the file
  562. include_once $manifestScriptFile;
  563. }
  564. // Set the class name
  565. $classname = $element . 'InstallerScript';
  566. if (class_exists($classname))
  567. {
  568. // Create a new instance
  569. $this->parent->manifestClass = new $classname($this);
  570. // And set this so we can copy it later
  571. $this->set('manifest_script', $manifestScript);
  572. // Note: if we don't find the class, don't bother to copy the file
  573. }
  574. }
  575. // Run preflight if possible (since we know we're not an update)
  576. ob_start();
  577. ob_implicit_flush(false);
  578. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'preflight'))
  579. {
  580. if ($this->parent->manifestClass->preflight('update', $this) === false)
  581. {
  582. // Install failed, rollback changes
  583. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_CUSTOM_INSTALL_FAILURE'));
  584. return false;
  585. }
  586. }
  587. // Create msg object; first use here
  588. $msg = ob_get_contents();
  589. ob_end_clean();
  590. /**
  591. * ---------------------------------------------------------------------------------------------
  592. * Filesystem Processing Section
  593. * ---------------------------------------------------------------------------------------------
  594. */
  595. // If the component directory does not exist, let's create it
  596. $created = false;
  597. if (!file_exists($this->parent->getPath('extension_site')))
  598. {
  599. if (!$created = JFolder::create($this->parent->getPath('extension_site')))
  600. {
  601. JError::raiseWarning(
  602. 1,
  603. JText::sprintf('JLIB_INSTALLER_ERROR_COMP_UPDATE_FAILED_TO_CREATE_DIRECTORY_SITE', $this->parent->getPath('extension_site'))
  604. );
  605. return false;
  606. }
  607. }
  608. /*
  609. * Since we created the component directory and will want to remove it if we have to roll back
  610. * the installation, lets add it to the installation step stack
  611. */
  612. if ($created)
  613. {
  614. $this->parent->pushStep(array('type' => 'folder', 'path' => $this->parent->getPath('extension_site')));
  615. }
  616. // If the component admin directory does not exist, let's create it
  617. $created = false;
  618. if (!file_exists($this->parent->getPath('extension_administrator')))
  619. {
  620. if (!$created = JFolder::create($this->parent->getPath('extension_administrator')))
  621. {
  622. JError::raiseWarning(
  623. 1,
  624. JText::sprintf(
  625. 'JLIB_INSTALLER_ERROR_COMP_UPDATE_FAILED_TO_CREATE_DIRECTORY_ADMIN',
  626. $this->parent->getPath('extension_administrator')
  627. )
  628. );
  629. // Install failed, rollback any changes
  630. $this->parent->abort();
  631. return false;
  632. }
  633. }
  634. /*
  635. * Since we created the component admin directory and we will want to remove it if we have to roll
  636. * back the installation, let's add it to the installation step stack
  637. */
  638. if ($created)
  639. {
  640. $this->parent->pushStep(array('type' => 'folder', 'path' => $this->parent->getPath('extension_administrator')));
  641. }
  642. // Find files to copy
  643. if ($this->manifest->files)
  644. {
  645. if ($this->parent->parseFiles($this->manifest->files, 0, $this->oldFiles) === false)
  646. {
  647. // Install failed, rollback any changes
  648. $this->parent->abort();
  649. return false;
  650. }
  651. }
  652. if ($this->manifest->administration->files)
  653. {
  654. if ($this->parent->parseFiles($this->manifest->administration->files, 1, $this->oldAdminFiles) === false)
  655. {
  656. // Install failed, rollback any changes
  657. $this->parent->abort();
  658. return false;
  659. }
  660. }
  661. // Parse optional tags
  662. $this->parent->parseMedia($this->manifest->media);
  663. $this->parent->parseLanguages($this->manifest->languages);
  664. $this->parent->parseLanguages($this->manifest->administration->languages, 1);
  665. // If there is a manifest script, let's copy it.
  666. if ($this->get('manifest_script'))
  667. {
  668. $path['src'] = $this->parent->getPath('source') . '/' . $this->get('manifest_script');
  669. $path['dest'] = $this->parent->getPath('extension_administrator') . '/' . $this->get('manifest_script');
  670. if (!file_exists($path['dest']) || $this->parent->isOverwrite())
  671. {
  672. if (!$this->parent->copyFiles(array($path)))
  673. {
  674. // Install failed, rollback changes
  675. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_UPDATE_MANIFEST'));
  676. return false;
  677. }
  678. }
  679. }
  680. /**
  681. * ---------------------------------------------------------------------------------------------
  682. * Database Processing Section
  683. * ---------------------------------------------------------------------------------------------
  684. */
  685. /*
  686. * Let's run the update queries for the component
  687. */
  688. $row = JTable::getInstance('extension');
  689. $eid = $row->find(array('element' => strtolower($this->get('element')), 'type' => 'component'));
  690. if ($this->manifest->update)
  691. {
  692. $result = $this->parent->parseSchemaUpdates($this->manifest->update->schemas, $eid);
  693. if ($result === false)
  694. {
  695. // Install failed, rollback changes
  696. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_UPDATE_SQL_ERROR', $db->stderr(true)));
  697. return false;
  698. }
  699. }
  700. // Time to build the admin menus
  701. if (!$this->_buildAdminMenus($eid))
  702. {
  703. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ABORT_COMP_BUILDADMINMENUS_FAILED'));
  704. // $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_ROLLBACK', $db->stderr(true)));
  705. // Return false;
  706. }
  707. /**
  708. * ---------------------------------------------------------------------------------------------
  709. * Custom Installation Script Section
  710. * ---------------------------------------------------------------------------------------------
  711. */
  712. /*
  713. * If we have an install script, let's include it, execute the custom
  714. * install method, and append the return value from the custom install
  715. * method to the installation message.
  716. */
  717. ob_start();
  718. ob_implicit_flush(false);
  719. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'update'))
  720. {
  721. if ($this->parent->manifestClass->update($this) === false)
  722. {
  723. // Install failed, rollback changes
  724. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_CUSTOM_INSTALL_FAILURE'));
  725. return false;
  726. }
  727. }
  728. // Append messages
  729. $msg .= ob_get_contents();
  730. ob_end_clean();
  731. /**
  732. * ---------------------------------------------------------------------------------------------
  733. * Finalization and Cleanup Section
  734. * ---------------------------------------------------------------------------------------------
  735. */
  736. // Clobber any possible pending updates
  737. $update = JTable::getInstance('update');
  738. $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => '', 'folder' => ''));
  739. if ($uid)
  740. {
  741. $update->delete($uid);
  742. }
  743. // Update an entry to the extension table
  744. if ($eid)
  745. {
  746. $row->load($eid);
  747. }
  748. else
  749. {
  750. // Set the defaults
  751. // There is no folder for components
  752. $row->folder = '';
  753. $row->enabled = 1;
  754. $row->protected = 0;
  755. $row->access = 1;
  756. $row->client_id = 1;
  757. $row->params = $this->parent->getParams();
  758. }
  759. $row->name = $this->get('name');
  760. $row->type = 'component';
  761. $row->element = $this->get('element');
  762. $row->manifest_cache = $this->parent->generateManifestCache();
  763. if (!$row->store())
  764. {
  765. // Install failed, roll back changes
  766. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_UPDATE_ROLLBACK', $db->stderr(true)));
  767. return false;
  768. }
  769. // We will copy the manifest file to its appropriate place.
  770. if (!$this->parent->copyManifest())
  771. {
  772. // Install failed, rollback changes
  773. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_UPDATE_COPY_SETUP'));
  774. return false;
  775. }
  776. // And now we run the postflight
  777. ob_start();
  778. ob_implicit_flush(false);
  779. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'postflight'))
  780. {
  781. $this->parent->manifestClass->postflight('update', $this);
  782. }
  783. // Append messages
  784. $msg .= ob_get_contents();
  785. ob_end_clean();
  786. if ($msg != '')
  787. {
  788. $this->parent->set('extension_message', $msg);
  789. }
  790. return $row->extension_id;
  791. }
  792. /**
  793. * Custom uninstall method for components
  794. *
  795. * @param integer $id The unique extension id of the component to uninstall
  796. *
  797. * @return mixed Return value for uninstall method in component uninstall file
  798. *
  799. * @since 11.1
  800. */
  801. public function uninstall($id)
  802. {
  803. // Initialise variables.
  804. $db = $this->parent->getDbo();
  805. $row = null;
  806. $retval = true;
  807. // First order of business will be to load the component object table from the database.
  808. // This should give us the necessary information to proceed.
  809. $row = JTable::getInstance('extension');
  810. if (!$row->load((int) $id))
  811. {
  812. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_ERRORUNKOWNEXTENSION'));
  813. return false;
  814. }
  815. // Is the component we are trying to uninstall a core one?
  816. // Because that is not a good idea...
  817. if ($row->protected)
  818. {
  819. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_WARNCORECOMPONENT'));
  820. return false;
  821. }
  822. // Get the admin and site paths for the component
  823. $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $row->element));
  824. $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE . '/components/' . $row->element));
  825. // Copy this as its used as a common base
  826. $this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator'));
  827. /**
  828. * ---------------------------------------------------------------------------------------------
  829. * Manifest Document Setup Section
  830. * ---------------------------------------------------------------------------------------------
  831. */
  832. // Find and load the XML install file for the component
  833. $this->parent->setPath('source', $this->parent->getPath('extension_administrator'));
  834. // Get the package manifest object
  835. // We do findManifest to avoid problem when uninstalling a list of extension: getManifest cache its manifest file
  836. $this->parent->findManifest();
  837. $this->manifest = $this->parent->getManifest();
  838. if (!$this->manifest)
  839. {
  840. // Make sure we delete the folders if no manifest exists
  841. JFolder::delete($this->parent->getPath('extension_administrator'));
  842. JFolder::delete($this->parent->getPath('extension_site'));
  843. // Remove the menu
  844. $this->_removeAdminMenus($row);
  845. // Raise a warning
  846. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_ERRORREMOVEMANUALLY'));
  847. // Return
  848. return false;
  849. }
  850. // Set the extensions name
  851. $name = strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  852. if (substr($name, 0, 4) == "com_")
  853. {
  854. $element = $name;
  855. }
  856. else
  857. {
  858. $element = "com_$name";
  859. }
  860. $this->set('name', $name);
  861. $this->set('element', $element);
  862. // Attempt to load the admin language file; might have uninstall strings
  863. $this->loadLanguage(JPATH_ADMINISTRATOR . '/components/' . $element);
  864. /**
  865. * ---------------------------------------------------------------------------------------------
  866. * Installer Trigger Loading and Uninstall
  867. * ---------------------------------------------------------------------------------------------
  868. */
  869. // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
  870. $scriptFile = (string) $this->manifest->scriptfile;
  871. if ($scriptFile)
  872. {
  873. $manifestScriptFile = $this->parent->getPath('source') . '/' . $scriptFile;
  874. if (is_file($manifestScriptFile))
  875. {
  876. // Load the file
  877. include_once $manifestScriptFile;
  878. }
  879. // Set the class name
  880. $classname = $row->element . 'InstallerScript';
  881. if (class_exists($classname))
  882. {
  883. // Create a new instance
  884. $this->parent->manifestClass = new $classname($this);
  885. // And set this so we can copy it later
  886. $this->set('manifest_script', $scriptFile);
  887. // Note: if we don't find the class, don't bother to copy the file
  888. }
  889. }
  890. ob_start();
  891. ob_implicit_flush(false);
  892. // Run uninstall if possible
  893. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'uninstall'))
  894. {
  895. $this->parent->manifestClass->uninstall($this);
  896. }
  897. $msg = ob_get_contents();
  898. ob_end_clean();
  899. if ($msg != '')
  900. {
  901. $this->parent->set('extension_message', $msg);
  902. }
  903. /**
  904. * ---------------------------------------------------------------------------------------------
  905. * Database Processing Section
  906. * ---------------------------------------------------------------------------------------------
  907. */
  908. /*
  909. * Let's run the uninstall queries for the component
  910. * If Joomla CMS 1.5 compatible, with discrete sql files - execute appropriate
  911. * file for utf-8 support or non-utf support
  912. */
  913. // Try for Joomla 1.5 type queries
  914. // Second argument is the utf compatible version attribute
  915. if (isset($this->manifest->uninstall->sql))
  916. {
  917. $utfresult = $this->parent->parseSQLFiles($this->manifest->uninstall->sql);
  918. if ($utfresult === false)
  919. {
  920. // Install failed, rollback changes
  921. JError::raiseWarning(100, JText::sprintf('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_SQL_ERROR', $db->stderr(true)));
  922. $retval = false;
  923. }
  924. }
  925. $this->_removeAdminMenus($row);
  926. /**
  927. * ---------------------------------------------------------------------------------------------
  928. * Filesystem Processing Section
  929. * ---------------------------------------------------------------------------------------------
  930. */
  931. // Let's remove those language files and media in the JROOT/images/ folder that are
  932. // associated with the component we are uninstalling
  933. $this->parent->removeFiles($this->manifest->media);
  934. $this->parent->removeFiles($this->manifest->languages);
  935. $this->parent->removeFiles($this->manifest->administration->languages, 1);
  936. // Remove the schema version
  937. $query = $db->getQuery(true);
  938. $query->delete()->from('#__schemas')->where('extension_id = ' . $id);
  939. $db->setQuery($query);
  940. $db->query();
  941. // Remove the component container in the assets table.
  942. $asset = JTable::getInstance('Asset');
  943. if ($asset->loadByName($element))
  944. {
  945. $asset->delete();
  946. }
  947. // Remove categories for this component
  948. $query = $db->getQuery(true);
  949. $query->delete()->from('#__categories')->where('extension=' . $db->quote($element), 'OR')
  950. ->where('extension LIKE ' . $db->quote($element . '.%'));
  951. $db->setQuery($query);
  952. $db->query();
  953. // Check for errors.
  954. if ($db->getErrorNum())
  955. {
  956. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_FAILED_DELETE_CATEGORIES'));
  957. $this->setError($db->getErrorMsg());
  958. $retval = false;
  959. }
  960. // Clobber any possible pending updates
  961. $update = JTable::getInstance('update');
  962. $uid = $update->find(array('element' => $row->element, 'type' => 'component', 'client_id' => '', 'folder' => ''));
  963. if ($uid)
  964. {
  965. $update->delete($uid);
  966. }
  967. // Now we need to delete the installation directories. This is the final step in uninstalling the component.
  968. if (trim($row->element))
  969. {
  970. // Delete the component site directory
  971. if (is_dir($this->parent->getPath('extension_site')))
  972. {
  973. if (!JFolder::delete($this->parent->getPath('extension_site')))
  974. {
  975. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_FAILED_REMOVE_DIRECTORY_SITE'));
  976. $retval = false;
  977. }
  978. }
  979. // Delete the component admin directory
  980. if (is_dir($this->parent->getPath('extension_administrator')))
  981. {
  982. if (!JFolder::delete($this->parent->getPath('extension_administrator')))
  983. {
  984. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_COMP_UNINSTALL_FAILED_REMOVE_DIRECTORY_ADMIN'));
  985. $retval = false;
  986. }
  987. }
  988. // Now we will no longer need the extension object, so let's delete it and free up memory
  989. $row->delete($row->extension_id);
  990. unset($row);
  991. return $retval;
  992. }
  993. else
  994. {
  995. // No component option defined... cannot delete what we don't know about
  996. JError::raiseWarning(100, 'JLIB_INSTALLER_ERROR_COMP_UNINSTALL_NO_OPTION');
  997. return false;
  998. }
  999. }
  1000. /**
  1001. * Method to build menu database entries for a component
  1002. *
  1003. * @return boolean True if successful
  1004. *
  1005. * @since 11.1
  1006. */
  1007. protected function _buildAdminMenus()
  1008. {
  1009. // Initialise variables.
  1010. $db = $this->parent->getDbo();
  1011. $table = JTable::getInstance('menu');
  1012. $option = $this->get('element');
  1013. // If a component exists with this option in the table then we don't need to add menus
  1014. $query = $db->getQuery(true);
  1015. $query->select('m.id, e.extension_id');
  1016. $query->from('#__menu AS m');
  1017. $query->leftJoin('#__extensions AS e ON m.component_id = e.extension_id');
  1018. $query->where('m.parent_id = 1');
  1019. $query->where("m.client_id = 1");
  1020. $query->where('e.element = ' . $db->quote($option));
  1021. $db->setQuery($query);
  1022. $componentrow = $db->loadObject();
  1023. // Check if menu items exist
  1024. if ($componentrow)
  1025. {
  1026. // Don't do anything if overwrite has not been enabled
  1027. if (!$this->parent->isOverwrite())
  1028. {
  1029. return true;
  1030. }
  1031. // Remove existing menu items if overwrite has been enabled
  1032. if ($option)
  1033. {
  1034. // If something goes wrong, theres no way to rollback TODO: Search for better solution
  1035. $this->_removeAdminMenus($componentrow);
  1036. }
  1037. $component_id = $componentrow->extension_id;
  1038. }
  1039. else
  1040. {
  1041. // Lets Find the extension id
  1042. $query->clear();
  1043. $query->select('e.extension_id');
  1044. $query->from('#__extensions AS e');
  1045. $query->where('e.element = ' . $db->quote($option));
  1046. $db->setQuery($query);
  1047. // TODO Find Some better way to discover the component_id
  1048. $component_id = $db->loadResult();
  1049. }
  1050. // Ok, now its time to handle the menus. Start with the component root menu, then handle submenus.
  1051. $menuElement = $this->manifest->administration->menu;
  1052. if ($menuElement)
  1053. {
  1054. $data = array();
  1055. $data['menutype'] = 'main';
  1056. $data['client_id'] = 1;
  1057. $data['title'] = (string) $menuElement;
  1058. $data['alias'] = (string) $menuElement;
  1059. $data['link'] = 'index.php?option=' . $option;
  1060. $data['type'] = 'component';
  1061. $data['published'] = 0;
  1062. $data['parent_id'] = 1;
  1063. $data['component_id'] = $component_id;
  1064. $data['img'] = ((string) $menuElement->attributes()->img) ? (string) $menuElement->attributes()->img : 'class:component';
  1065. $data['home'] = 0;
  1066. if (!$table->setLocation(1, 'last-child') || !$table->bind($data) || !$table->check() || !$table->store())
  1067. {
  1068. // Install failed, warn user and rollback changes
  1069. JError::raiseWarning(1, $table->getError());
  1070. return false;
  1071. }
  1072. /*
  1073. * Since we have created a menu item, we add it to the installation step stack
  1074. * so that if we have to rollback the changes we can undo it.
  1075. */
  1076. $this->parent->pushStep(array('type' => 'menu', 'id' => $component_id));
  1077. }
  1078. // No menu element was specified, Let's make a generic menu item
  1079. else
  1080. {
  1081. $data = array();
  1082. $data['menutype'] = 'main';
  1083. $data['client_id'] = 1;
  1084. $data['title'] = $option;
  1085. $data['alias'] = $option;
  1086. $data['link'] = 'index.php?option=' . $option;
  1087. $data['type'] = 'component';
  1088. $data['published'] = 0;
  1089. $data['parent_id'] = 1;
  1090. $data['component_id'] = $component_id;
  1091. $data['img'] = 'class:component';
  1092. $data['home'] = 0;
  1093. if (!$table->setLocation(1, 'last-child') || !$table->bind($data) || !$table->check() || !$table->store())
  1094. {
  1095. // Install failed, warn user and rollback changes
  1096. JError::raiseWarning(1, $table->getError());
  1097. return false;
  1098. }
  1099. /*
  1100. * Since we have created a menu item, we add it to the installation step stack
  1101. * so that if we have to rollback the changes we can undo it.
  1102. */
  1103. $this->parent->pushStep(array('type' => 'menu', 'id' => $component_id));
  1104. }
  1105. $parent_id = $table->id;
  1106. /*
  1107. * Process SubMenus
  1108. */
  1109. if (!$this->manifest->administration->submenu)
  1110. {
  1111. return true;
  1112. }
  1113. $parent_id = $table->id;
  1114. foreach ($this->manifest->administration->submenu->menu as $child)
  1115. {
  1116. $data = array();
  1117. $data['menutype'] = 'main';
  1118. $data['client_id'] = 1;
  1119. $data['title'] = (string) $child;
  1120. $data['alias'] = (string) $child;
  1121. $data['type'] = 'component';
  1122. $data['published'] = 0;
  1123. $data['parent_id'] = $parent_id;
  1124. $data['component_id'] = $component_id;
  1125. $data['img'] = ((string) $child->attributes()->img) ? (string) $child->attributes()->img : 'class:component';
  1126. $data['home'] = 0;
  1127. // Set the sub menu link
  1128. if ((string) $child->attributes()->link)
  1129. {
  1130. $data['link'] = 'index.php?' . $child->attributes()->link;
  1131. }
  1132. else
  1133. {
  1134. $request = array();
  1135. if ((string) $child->attributes()->act)
  1136. {
  1137. $request[] = 'act=' . $child->attributes()->act;
  1138. }
  1139. if ((string) $child->attributes()->task)
  1140. {
  1141. $request[] = 'task=' . $child->attributes()->task;
  1142. }
  1143. if ((string) $child->attributes()->controller)
  1144. {
  1145. $request[] = 'controller=' . $child->attributes()->controller;
  1146. }
  1147. if ((string) $child->attributes()->view)
  1148. {
  1149. $request[] = 'view=' . $child->attributes()->view;
  1150. }
  1151. if ((string) $child->attributes()->layout)
  1152. {
  1153. $request[] = 'layout=' . $child->attributes()->layout;
  1154. }
  1155. if ((string) $child->attributes()->sub)
  1156. {
  1157. $request[] = 'sub=' . $child->attributes()->sub;
  1158. }
  1159. $qstring = (count($request)) ? '&' . implode('&', $request) : '';
  1160. $data['link'] = 'index.php?option=' . $option . $qstring;
  1161. }
  1162. $table = JTable::getInstance('menu');
  1163. if (!$table->setLocation($parent_id, 'last-child') || !$table->bind($data) || !$table->check() || !$table->store())
  1164. {
  1165. // Install failed, rollback changes
  1166. return false;
  1167. }
  1168. /*
  1169. * Since we have created a menu item, we add it to the installation step stack
  1170. * so that if we have to rollback the changes we can undo it.
  1171. */
  1172. $this->parent->pushStep(array('type' => 'menu', 'id' => $component_id));
  1173. }
  1174. return true;
  1175. }
  1176. /**
  1177. * Method to remove admin menu references to a component
  1178. *
  1179. * @param object &$row Component table object.
  1180. *
  1181. * @return boolean True if successful.
  1182. *
  1183. * @since 11.1
  1184. */
  1185. protected function _removeAdminMenus(&$row)
  1186. {
  1187. // Initialise Variables
  1188. $db = $this->parent->getDbo();
  1189. $table = JTable::getInstance('menu');
  1190. $id = $row->extension_id;
  1191. // Get the ids of the menu items
  1192. $query = $db->getQuery(true);
  1193. $query->select('id');
  1194. $query->from('#__menu');
  1195. $query->where($query->qn('client_id') . ' = 1');
  1196. $query->where($query->qn('component_id') . ' = ' . (int) $id);
  1197. $db->setQuery($query);
  1198. $ids = $db->loadColumn();
  1199. // Check for error
  1200. if ($error = $db->getErrorMsg())
  1201. {
  1202. JError::raiseWarning('', JText::_('JLIB_INSTALLER_ERROR_COMP_REMOVING_ADMIN_MENUS_FAILED'));
  1203. if ($error && $error != 1)
  1204. {
  1205. JError::raiseWarning(100, $error);
  1206. }
  1207. return false;
  1208. }
  1209. elseif (!empty($ids))
  1210. {
  1211. // Iterate the items to delete each one.
  1212. foreach ($ids as $menuid)
  1213. {
  1214. if (!$table->delete((int) $menuid))
  1215. {
  1216. $this->setError($table->getError());
  1217. return false;
  1218. }
  1219. }
  1220. // Rebuild the whole tree
  1221. $table->rebuild();
  1222. }
  1223. return true;
  1224. }
  1225. /**
  1226. * Custom rollback method
  1227. * - Roll back the component menu item
  1228. *
  1229. * @param array $step Installation step to rollback.
  1230. *
  1231. * @return boolean True on success
  1232. *
  1233. * @since 11.1
  1234. */
  1235. protected function _rollback_menu($step)
  1236. {
  1237. return $this->_removeAdminMenus((object) array('extension_id' => $step['id']));
  1238. }
  1239. /**
  1240. * Discover unregistered extensions.
  1241. *
  1242. * @return array A list of extensions.
  1243. *
  1244. * @since 11.1
  1245. */
  1246. public function discover()
  1247. {
  1248. $results = array();
  1249. $site_components = JFolder::folders(JPATH_SITE . '/components');
  1250. $admin_components = JFolder::folders(JPATH_ADMINISTRATOR . '/components');
  1251. foreach ($site_components as $component)
  1252. {
  1253. if (file_exists(JPATH_SITE . '/components/' . $component . '/' . str_replace('com_', '', $component) . '.xml'))
  1254. {
  1255. $manifest_details = JApplicationHelper::parseXMLInstallFile(
  1256. JPATH_SITE . '/components/' . $component . '/' . str_replace('com_', '', $component) . '.xml'
  1257. );
  1258. $extension = JTable::getInstance('extension');
  1259. $extension->set('type', 'component');
  1260. $extension->set('client_id', 0);
  1261. $extension->set('element', $component);
  1262. $extension->set('name', $component);
  1263. $extension->set('state', -1);
  1264. $extension->set('manifest_cache', json_encode($manifest_details));
  1265. $results[] = $extension;
  1266. }
  1267. }
  1268. foreach ($admin_components as $component)
  1269. {
  1270. if (file_exists(JPATH_ADMINISTRATOR . '/components/' . $component . '/' . str_replace('com_', '', $component) . '.xml'))
  1271. {
  1272. $manifest_details = JApplicationHelper::parseXMLInstallFile(
  1273. JPATH_ADMINISTRATOR . '/components/' . $component . '/' . str_replace('com_', '', $component) . '.xml'
  1274. );
  1275. $extension = JTable::getInstance('extension');
  1276. $extension->set('type', 'component');
  1277. $extension->set('client_id', 1);
  1278. $extension->set('element', $component);
  1279. $extension->set('name', $component);
  1280. $extension->set('state', -1);
  1281. $extension->set('manifest_cache', json_encode($manifest_details));
  1282. $results[] = $extension;
  1283. }
  1284. }
  1285. return $results;
  1286. }
  1287. /**
  1288. * Install unregistered extensions that have been discovered.
  1289. *
  1290. * @return mixed
  1291. *
  1292. * @since 11.1
  1293. */
  1294. public function discover_install()
  1295. {
  1296. // Need to find to find where the XML file is since we don't store this normally
  1297. $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  1298. $short_element = str_replace('com_', '', $this->parent->extension->element);
  1299. $manifestPath = $client->path . '/components/' . $this->parent->extension->element . '/' . $short_element . '.xml';
  1300. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  1301. $this->parent->setPath('manifest', $manifestPath);
  1302. $this->parent->setPath('source', $client->path . '/components/' . $this->parent->extension->element);
  1303. $this->parent->setPath('extension_root', $this->parent->getPath('source'));
  1304. $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
  1305. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  1306. $this->parent->extension->state = 0;
  1307. $this->parent->extension->name = $manifest_details['name'];
  1308. $this->parent->extension->enabled = 1;
  1309. $this->parent->extension->params = $this->parent->getParams();
  1310. try
  1311. {
  1312. $this->parent->extension->store();
  1313. }
  1314. catch (JException $e)
  1315. {
  1316. JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_COMP_DISCOVER_STORE_DETAILS'));
  1317. return false;
  1318. }
  1319. // Now we need to run any SQL it has, languages, media or menu stuff
  1320. // Get a database connector object
  1321. $db = $this->parent->getDbo();
  1322. // Get the extension manifest object
  1323. $this->manifest = $this->parent->getManifest();
  1324. /**
  1325. * ---------------------------------------------------------------------------------------------
  1326. * Manifest Document Setup Section
  1327. * ---------------------------------------------------------------------------------------------
  1328. */
  1329. // Set the extensions name
  1330. $name = strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  1331. if (substr($name, 0, 4) == "com_")
  1332. {
  1333. $element = $name;
  1334. }
  1335. else
  1336. {
  1337. $element = "com_$name";
  1338. }
  1339. $this->set('name', $name);
  1340. $this->set('element', $element);
  1341. // Get the component description
  1342. $description = (string) $this->manifest->description;
  1343. if ($description)
  1344. {
  1345. $this->parent->set('message', JText::_((string) $description));
  1346. }
  1347. else
  1348. {
  1349. $this->parent->set('message', '');
  1350. }
  1351. // Set the installation target paths
  1352. $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE . '/components/' . $this->get('element')));
  1353. $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $this->get('element')));
  1354. // Copy this as its used as a common base
  1355. $this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator'));
  1356. /**
  1357. * ---------------------------------------------------------------------------------------------
  1358. * Basic Checks Section
  1359. * ---------------------------------------------------------------------------------------------
  1360. */
  1361. // Make sure that we have an admin element
  1362. if (!$this->manifest->administration)
  1363. {
  1364. JError::raiseWarning(1, JText::_('JLIB_INSTALLER_ERROR_COMP_INSTALL_ADMIN_ELEMENT'));
  1365. return false;
  1366. }
  1367. /**
  1368. * ---------------------------------------------------------------------------------------------
  1369. * Installer Trigger Loading
  1370. * ---------------------------------------------------------------------------------------------
  1371. */
  1372. // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
  1373. $manifestScript = (string) $this->manifest->scriptfile;
  1374. if ($manifestScript)
  1375. {
  1376. $manifestScriptFile = $this->parent->getPath('source') . '/' . $manifestScript;
  1377. if (is_file($manifestScriptFile))
  1378. {
  1379. // Load the file
  1380. include_once $manifestScriptFile;
  1381. }
  1382. // Set the class name
  1383. $classname = $element . 'InstallerScript';
  1384. if (class_exists($classname))
  1385. {
  1386. // Create a new instance
  1387. $this->parent->manifestClass = new $classname($this);
  1388. // And set this so we can copy it later
  1389. $this->set('manifest_script', $manifestScript);
  1390. // Note: if we don't find the class, don't bother to copy the file
  1391. }
  1392. }
  1393. // Run preflight if possible (since we know we're not an update)
  1394. ob_start();
  1395. ob_implicit_flush(false);
  1396. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'preflight'))
  1397. {
  1398. if ($this->parent->manifestClass->preflight('discover_install', $this) === false)
  1399. {
  1400. // Install failed, rollback changes
  1401. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_CUSTOM_INSTALL_FAILURE'));
  1402. return false;
  1403. }
  1404. }
  1405. // Create msg object; first use here
  1406. $msg = ob_get_contents();
  1407. ob_end_clean();
  1408. /*
  1409. *
  1410. * Normally we would copy files and create directories, lets skip to the optional files
  1411. * Note: need to dereference things!
  1412. * Parse optional tags
  1413. * @todo remove code: $this->parent->parseMedia($this->manifest->media);
  1414. *
  1415. * We don't do language because 1.6 suggests moving to extension based languages
  1416. * @todo remove code: $this->parent->parseLanguages($this->manifest->languages);
  1417. * @todo remove code: $this->parent->parseLanguages($this->manifest->administration->languages, 1);
  1418. */
  1419. /**
  1420. * ---------------------------------------------------------------------------------------------
  1421. * Database Processing Section
  1422. * ---------------------------------------------------------------------------------------------
  1423. */
  1424. /*
  1425. * Let's run the install queries for the component
  1426. * If Joomla 1.5 compatible, with discreet sql files - execute appropriate
  1427. * file for utf-8 support or non-utf-8 support
  1428. */
  1429. // Try for Joomla 1.5 type queries
  1430. // second argument is the utf compatible version attribute
  1431. if (isset($this->manifest->install->sql))
  1432. {
  1433. $utfresult = $this->parent->parseSQLFiles($this->manifest->install->sql);
  1434. if ($utfresult === false)
  1435. {
  1436. // Install failed, rollback changes
  1437. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_SQL_ERROR', $db->stderr(true)));
  1438. return false;
  1439. }
  1440. }
  1441. // Time to build the admin menus
  1442. if (!$this->_buildAdminMenus($this->parent->extension->extension_id))
  1443. {
  1444. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ABORT_COMP_BUILDADMINMENUS_FAILED'));
  1445. // @todo remove code: $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_COMP_INSTALL_ROLLBACK', $db->stderr(true)));
  1446. // @todo remove code: return false;
  1447. }
  1448. /**
  1449. * ---------------------------------------------------------------------------------------------
  1450. * Custom Installation Script Section
  1451. * ---------------------------------------------------------------------------------------------
  1452. */
  1453. /*
  1454. * If we have an install script, lets include it, execute the custom
  1455. * install method, and append the return value from the custom install
  1456. * method to the installation message.
  1457. */
  1458. ob_start();
  1459. ob_implicit_flush(false);
  1460. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'discover_install'))
  1461. {
  1462. if ($this->parent->manifestClass->install($this) === false)
  1463. {
  1464. // Install failed, rollback changes
  1465. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_COMP_INSTALL_CUSTOM_INSTALL_FAILURE'));
  1466. return false;
  1467. }
  1468. }
  1469. // Append messages
  1470. $msg .= ob_get_contents();
  1471. ob_end_clean();
  1472. /**
  1473. * ---------------------------------------------------------------------------------------------
  1474. * Finalization and Cleanup Section
  1475. * ---------------------------------------------------------------------------------------------
  1476. */
  1477. // Clobber any possible pending updates
  1478. $update = JTable::getInstance('update');
  1479. $uid = $update->find(array('element' => $this->get('element'), 'type' => 'component', 'client_id' => '', 'folder' => ''));
  1480. if ($uid)
  1481. {
  1482. $update->delete($uid);
  1483. }
  1484. // And now we run the postflight
  1485. ob_start();
  1486. ob_implicit_flush(false);
  1487. if ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'postflight'))
  1488. {
  1489. $this->parent->manifestClass->postflight('discover_install', $this);
  1490. }
  1491. // Append messages
  1492. $msg .= ob_get_contents();
  1493. ob_end_clean();
  1494. if ($msg != '')
  1495. {
  1496. $this->parent->set('extension_message', $msg);
  1497. }
  1498. return $this->parent->extension->extension_id;
  1499. }
  1500. /**
  1501. * Refreshes the extension table cache
  1502. *
  1503. * @return boolean Result of operation, true if updated, false on failure
  1504. *
  1505. * @since 11.1
  1506. */
  1507. public function refreshManifestCache()
  1508. {
  1509. // Need to find to find where the XML file is since we don't store this normally
  1510. $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  1511. $short_element = str_replace('com_', '', $this->parent->extension->element);
  1512. $manifestPath = $client->path . '/components/' . $this->parent->extension->element . '/' . $short_element . '.xml';
  1513. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  1514. $this->parent->setPath('manifest', $manifestPath);
  1515. $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
  1516. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  1517. $this->parent->extension->name = $manifest_details['name'];
  1518. try
  1519. {
  1520. return $this->parent->extension->store();
  1521. }
  1522. catch (JException $e)
  1523. {
  1524. JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_COMP_REFRESH_MANIFEST_CACHE'));
  1525. return false;
  1526. }
  1527. }
  1528. }