PageRenderTime 43ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/cms/installer/adapter/template.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 664 lines | 397 code | 107 blank | 160 comment | 43 complexity | 88c9d2096b3db6b5e44d10cc8409b5d9 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  4. * @subpackage Installer
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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. jimport('joomla.filesystem.folder');
  12. /**
  13. * Template installer
  14. *
  15. * @package Joomla.Libraries
  16. * @subpackage Installer
  17. * @since 3.1
  18. */
  19. class JInstallerAdapterTemplate extends JAdapterInstance
  20. {
  21. /**
  22. * Copy of the XML manifest file
  23. *
  24. * @var string
  25. * @since 3.1
  26. */
  27. protected $manifest = null;
  28. /**
  29. * Name of the extension
  30. *
  31. * @var string
  32. * @since 3.1
  33. * */
  34. protected $name = null;
  35. /**
  36. * The unique identifier for the extension (e.g. mod_login)
  37. *
  38. * @var string
  39. * @since 3.1
  40. * */
  41. protected $element = null;
  42. /**
  43. * Method of system
  44. *
  45. * @var string
  46. *
  47. * @since 3.1
  48. */
  49. protected $route = 'install';
  50. /**
  51. * Custom loadLanguage method
  52. *
  53. * @param string $path The path where to find language files.
  54. *
  55. * @return JInstallerTemplate
  56. *
  57. * @since 3.1
  58. */
  59. public function loadLanguage($path = null)
  60. {
  61. $source = $this->parent->getPath('source');
  62. if (!$source)
  63. {
  64. $this->parent
  65. ->setPath(
  66. 'source',
  67. ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/templates/' . $this->parent->extension->element
  68. );
  69. }
  70. $this->manifest = $this->parent->getManifest();
  71. $name = strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  72. $client = (string) $this->manifest->attributes()->client;
  73. // Load administrator language if not set.
  74. if (!$client)
  75. {
  76. $client = 'ADMINISTRATOR';
  77. }
  78. $extension = "tpl_$name";
  79. $lang = JFactory::getLanguage();
  80. $source = $path ? $path : ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/templates/' . $name;
  81. $lang->load($extension . '.sys', $source, null, false, false)
  82. || $lang->load($extension . '.sys', constant('JPATH_' . strtoupper($client)), null, false, false)
  83. || $lang->load($extension . '.sys', $source, $lang->getDefault(), false, false)
  84. || $lang->load($extension . '.sys', constant('JPATH_' . strtoupper($client)), $lang->getDefault(), false, false);
  85. }
  86. /**
  87. * Custom install method
  88. *
  89. * @return boolean True on success
  90. *
  91. * @since 3.1
  92. */
  93. public function install()
  94. {
  95. // Get a database connector object
  96. $db = $this->parent->getDbo();
  97. $lang = JFactory::getLanguage();
  98. $xml = $this->parent->getManifest();
  99. // Get the client application target
  100. if ($cname = (string) $xml->attributes()->client)
  101. {
  102. // Attempt to map the client to a base path
  103. $client = JApplicationHelper::getClientInfo($cname, true);
  104. if ($client === false)
  105. {
  106. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_UNKNOWN_CLIENT', $cname));
  107. return false;
  108. }
  109. $basePath = $client->path;
  110. $clientId = $client->id;
  111. }
  112. else
  113. {
  114. // No client attribute was found so we assume the site as the client
  115. $cname = 'site';
  116. $basePath = JPATH_SITE;
  117. $clientId = 0;
  118. }
  119. // Set the extension's name
  120. $name = JFilterInput::getInstance()->clean((string) $xml->name, 'cmd');
  121. $element = strtolower(str_replace(" ", "_", $name));
  122. $this->set('name', $name);
  123. $this->set('element', $element);
  124. // Check to see if a template by the same name is already installed.
  125. $query = $db->getQuery(true)
  126. ->select($db->quoteName('extension_id'))
  127. ->from($db->quoteName('#__extensions'))
  128. ->where($db->quoteName('type') . ' = ' . $db->quote('template'))
  129. ->where($db->quoteName('element') . ' = ' . $db->quote($element));
  130. $db->setQuery($query);
  131. try
  132. {
  133. $id = $db->loadResult();
  134. }
  135. catch (RuntimeException $e)
  136. {
  137. // Install failed, roll back changes
  138. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ROLLBACK'), $e->getMessage());
  139. return false;
  140. }
  141. // Set the template root path
  142. $this->parent->setPath('extension_root', $basePath . '/templates/' . $element);
  143. // If it's on the fs...
  144. if (file_exists($this->parent->getPath('extension_root')) && (!$this->parent->isOverwrite() || $this->parent->isUpgrade()))
  145. {
  146. $updateElement = $xml->update;
  147. // Upgrade manually set or update tag detected
  148. if ($this->parent->isUpgrade() || $updateElement)
  149. {
  150. // Force this one
  151. $this->parent->setOverwrite(true);
  152. $this->parent->setUpgrade(true);
  153. if ($id)
  154. {
  155. // If there is a matching extension mark this as an update; semantics really
  156. $this->route = 'update';
  157. }
  158. }
  159. elseif (!$this->parent->isOverwrite())
  160. {
  161. // Overwrite is not set
  162. // If we didn't have overwrite set, find an update function or find an update tag so let's call it safe
  163. $this->parent
  164. ->abort(
  165. JText::sprintf(
  166. 'JLIB_INSTALLER_ABORT_TPL_INSTALL_ANOTHER_TEMPLATE_USING_DIRECTORY', JText::_('JLIB_INSTALLER_' . $this->route),
  167. $this->parent->getPath('extension_root')
  168. )
  169. );
  170. return false;
  171. }
  172. }
  173. /*
  174. * If the template directory already exists, then we will assume that the template is already
  175. * installed or another template is using that directory.
  176. */
  177. if (file_exists($this->parent->getPath('extension_root')) && !$this->parent->isOverwrite())
  178. {
  179. JLog::add(
  180. JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ANOTHER_TEMPLATE_USING_DIRECTORY', $this->parent->getPath('extension_root')),
  181. JLog::WARNING, 'jerror'
  182. );
  183. return false;
  184. }
  185. // If the template directory does not exist, let's create it
  186. $created = false;
  187. if (!file_exists($this->parent->getPath('extension_root')))
  188. {
  189. if (!$created = JFolder::create($this->parent->getPath('extension_root')))
  190. {
  191. $this->parent
  192. ->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_FAILED_CREATE_DIRECTORY', $this->parent->getPath('extension_root')));
  193. return false;
  194. }
  195. }
  196. // If we created the template directory and will want to remove it if we have to roll back
  197. // the installation, let's add it to the installation step stack
  198. if ($created)
  199. {
  200. $this->parent->pushStep(array('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
  201. }
  202. // Copy all the necessary files
  203. if ($this->parent->parseFiles($xml->files, -1) === false)
  204. {
  205. // Install failed, rollback changes
  206. $this->parent->abort();
  207. return false;
  208. }
  209. if ($this->parent->parseFiles($xml->images, -1) === false)
  210. {
  211. // Install failed, rollback changes
  212. $this->parent->abort();
  213. return false;
  214. }
  215. if ($this->parent->parseFiles($xml->css, -1) === false)
  216. {
  217. // Install failed, rollback changes
  218. $this->parent->abort();
  219. return false;
  220. }
  221. // Parse optional tags
  222. $this->parent->parseMedia($xml->media);
  223. $this->parent->parseLanguages($xml->languages, $clientId);
  224. // Get the template description
  225. $this->parent->set('message', JText::_((string) $xml->description));
  226. // Lastly, we will copy the manifest file to its appropriate place.
  227. if (!$this->parent->copyManifest(-1))
  228. {
  229. // Install failed, rollback changes
  230. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_SETUP'));
  231. return false;
  232. }
  233. /**
  234. * ---------------------------------------------------------------------------------------------
  235. * Extension Registration
  236. * ---------------------------------------------------------------------------------------------
  237. */
  238. $row = JTable::getInstance('extension');
  239. if ($this->route == 'update' && $id)
  240. {
  241. $row->load($id);
  242. }
  243. else
  244. {
  245. $row->type = 'template';
  246. $row->element = $this->get('element');
  247. // There is no folder for templates
  248. $row->folder = '';
  249. $row->enabled = 1;
  250. $row->protected = 0;
  251. $row->access = 1;
  252. $row->client_id = $clientId;
  253. $row->params = $this->parent->getParams();
  254. // Custom data
  255. $row->custom_data = '';
  256. }
  257. // Name might change in an update
  258. $row->name = $this->get('name');
  259. $row->manifest_cache = $this->parent->generateManifestCache();
  260. if (!$row->store())
  261. {
  262. // Install failed, roll back changes
  263. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ROLLBACK', $db->stderr(true)));
  264. return false;
  265. }
  266. if ($this->route == 'install')
  267. {
  268. $debug = $lang->setDebug(false);
  269. $columns = array($db->quoteName('template'),
  270. $db->quoteName('client_id'),
  271. $db->quoteName('home'),
  272. $db->quoteName('title'),
  273. $db->quoteName('params')
  274. );
  275. $values = array(
  276. $db->quote($row->element), $clientId, $db->quote(0),
  277. $db->quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', JText::_($this->get('name')))),
  278. $db->quote($row->params) );
  279. $lang->setDebug($debug);
  280. // Insert record in #__template_styles
  281. $query = $db->getQuery(true)
  282. ->insert($db->quoteName('#__template_styles'))
  283. ->columns($columns)
  284. ->values(implode(',', $values));
  285. $db->setQuery($query);
  286. // There is a chance this could fail but we don't care...
  287. $db->execute();
  288. }
  289. return $row->get('extension_id');
  290. }
  291. /**
  292. * Custom update method for components
  293. *
  294. * @return boolean True on success
  295. *
  296. * @since 3.1
  297. */
  298. public function update()
  299. {
  300. $this->route = 'update';
  301. return $this->install();
  302. }
  303. /**
  304. * Custom uninstall method
  305. *
  306. * @param integer $id The extension ID
  307. *
  308. * @return boolean True on success
  309. *
  310. * @since 3.1
  311. */
  312. public function uninstall($id)
  313. {
  314. $retval = true;
  315. // First order of business will be to load the template object table from the database.
  316. // This should give us the necessary information to proceed.
  317. $row = JTable::getInstance('extension');
  318. if (!$row->load((int) $id) || !strlen($row->element))
  319. {
  320. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_ERRORUNKOWNEXTENSION'), JLog::WARNING, 'jerror');
  321. return false;
  322. }
  323. // Is the template we are trying to uninstall a core one?
  324. // Because that is not a good idea...
  325. if ($row->protected)
  326. {
  327. JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_WARNCORETEMPLATE', $row->name), JLog::WARNING, 'jerror');
  328. return false;
  329. }
  330. $name = $row->element;
  331. $clientId = $row->client_id;
  332. // For a template the id will be the template name which represents the subfolder of the templates folder that the template resides in.
  333. if (!$name)
  334. {
  335. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY'), JLog::WARNING, 'jerror');
  336. return false;
  337. }
  338. // Deny remove default template
  339. $db = $this->parent->getDbo();
  340. $query = "SELECT COUNT(*) FROM #__template_styles WHERE home = '1' AND template = " . $db->quote($name);
  341. $db->setQuery($query);
  342. if ($db->loadResult() != 0)
  343. {
  344. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DEFAULT'), JLog::WARNING, 'jerror');
  345. return false;
  346. }
  347. // Get the template root path
  348. $client = JApplicationHelper::getClientInfo($clientId);
  349. if (!$client)
  350. {
  351. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_CLIENT'), JLog::WARNING, 'jerror');
  352. return false;
  353. }
  354. $this->parent->setPath('extension_root', $client->path . '/templates/' . strtolower($name));
  355. $this->parent->setPath('source', $this->parent->getPath('extension_root'));
  356. // We do findManifest to avoid problem when uninstalling a list of extensions: getManifest cache its manifest file
  357. $this->parent->findManifest();
  358. $manifest = $this->parent->getManifest();
  359. if (!($manifest instanceof SimpleXMLElement))
  360. {
  361. // Kill the extension entry
  362. $row->delete($row->extension_id);
  363. unset($row);
  364. // Make sure we delete the folders
  365. JFolder::delete($this->parent->getPath('extension_root'));
  366. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_NOTFOUND_MANIFEST'), JLog::WARNING, 'jerror');
  367. return false;
  368. }
  369. // Remove files
  370. $this->parent->removeFiles($manifest->media);
  371. $this->parent->removeFiles($manifest->languages, $clientId);
  372. // Delete the template directory
  373. if (JFolder::exists($this->parent->getPath('extension_root')))
  374. {
  375. $retval = JFolder::delete($this->parent->getPath('extension_root'));
  376. }
  377. else
  378. {
  379. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DIRECTORY'), JLog::WARNING, 'jerror');
  380. $retval = false;
  381. }
  382. // Set menu that assigned to the template back to default template
  383. $query = 'UPDATE #__menu'
  384. . ' SET template_style_id = 0'
  385. . ' WHERE template_style_id in ('
  386. . ' SELECT s.id FROM #__template_styles s'
  387. . ' WHERE s.template = ' . $db->quote(strtolower($name)) . ' AND s.client_id = ' . $clientId . ')';
  388. $db->setQuery($query);
  389. $db->execute();
  390. $query = 'DELETE FROM #__template_styles WHERE template = ' . $db->quote($name) . ' AND client_id = ' . $clientId;
  391. $db->setQuery($query);
  392. $db->execute();
  393. $row->delete($row->extension_id);
  394. unset($row);
  395. return $retval;
  396. }
  397. /**
  398. * Discover existing but uninstalled templates
  399. *
  400. * @return array JExtensionTable list
  401. */
  402. public function discover()
  403. {
  404. $results = array();
  405. $site_list = JFolder::folders(JPATH_SITE . '/templates');
  406. $admin_list = JFolder::folders(JPATH_ADMINISTRATOR . '/templates');
  407. $site_info = JApplicationHelper::getClientInfo('site', true);
  408. $admin_info = JApplicationHelper::getClientInfo('administrator', true);
  409. foreach ($site_list as $template)
  410. {
  411. if ($template == 'system')
  412. {
  413. // Ignore special system template
  414. continue;
  415. }
  416. $manifest_details = JInstaller::parseXMLInstallFile(JPATH_SITE . "/templates/$template/templateDetails.xml");
  417. $extension = JTable::getInstance('extension');
  418. $extension->set('type', 'template');
  419. $extension->set('client_id', $site_info->id);
  420. $extension->set('element', $template);
  421. $extension->set('folder', '');
  422. $extension->set('name', $template);
  423. $extension->set('state', -1);
  424. $extension->set('manifest_cache', json_encode($manifest_details));
  425. $extension->set('params', '{}');
  426. $results[] = $extension;
  427. }
  428. foreach ($admin_list as $template)
  429. {
  430. if ($template == 'system')
  431. {
  432. // Ignore special system template
  433. continue;
  434. }
  435. $manifest_details = JInstaller::parseXMLInstallFile(JPATH_ADMINISTRATOR . "/templates/$template/templateDetails.xml");
  436. $extension = JTable::getInstance('extension');
  437. $extension->set('type', 'template');
  438. $extension->set('client_id', $admin_info->id);
  439. $extension->set('element', $template);
  440. $extension->set('folder', '');
  441. $extension->set('name', $template);
  442. $extension->set('state', -1);
  443. $extension->set('manifest_cache', json_encode($manifest_details));
  444. $extension->set('params', '{}');
  445. $results[] = $extension;
  446. }
  447. return $results;
  448. }
  449. /**
  450. * Discover_install
  451. * Perform an install for a discovered extension
  452. *
  453. * @return boolean
  454. *
  455. * @since 3.1
  456. */
  457. public function discover_install()
  458. {
  459. // Templates are one of the easiest
  460. // If its not in the extensions table we just add it
  461. $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  462. $manifestPath = $client->path . '/templates/' . $this->parent->extension->element . '/templateDetails.xml';
  463. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  464. $description = (string) $this->parent->manifest->description;
  465. if ($description)
  466. {
  467. $this->parent->set('message', JText::_($description));
  468. }
  469. else
  470. {
  471. $this->parent->set('message', '');
  472. }
  473. $this->parent->setPath('manifest', $manifestPath);
  474. $manifest_details = JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
  475. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  476. $this->parent->extension->state = 0;
  477. $this->parent->extension->name = $manifest_details['name'];
  478. $this->parent->extension->enabled = 1;
  479. $data = new JObject;
  480. foreach ($manifest_details as $key => $value)
  481. {
  482. $data->set($key, $value);
  483. }
  484. $this->parent->extension->params = $this->parent->getParams();
  485. if ($this->parent->extension->store())
  486. {
  487. $db = $this->parent->getDbo();
  488. // Insert record in #__template_styles
  489. $lang = JFactory::getLanguage();
  490. $debug = $lang->setDebug(false);
  491. $columns = array($db->quoteName('template'),
  492. $db->quoteName('client_id'),
  493. $db->quoteName('home'),
  494. $db->quoteName('title'),
  495. $db->quoteName('params')
  496. );
  497. $query = $db->getQuery(true)
  498. ->insert($db->quoteName('#__template_styles'))
  499. ->columns($columns)
  500. ->values(
  501. $db->quote($this->parent->extension->element)
  502. . ',' . $db->quote($this->parent->extension->client_id)
  503. . ',' . $db->quote(0)
  504. . ',' . $db->quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', $this->parent->extension->name))
  505. . ',' . $db->quote($this->parent->extension->params)
  506. );
  507. $lang->setDebug($debug);
  508. $db->setQuery($query);
  509. $db->execute();
  510. return $this->parent->extension->get('extension_id');
  511. }
  512. else
  513. {
  514. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_DISCOVER_STORE_DETAILS'), JLog::WARNING, 'jerror');
  515. return false;
  516. }
  517. }
  518. /**
  519. * Refreshes the extension table cache
  520. *
  521. * @return boolean Result of operation, true if updated, false on failure
  522. *
  523. * @since 3.1
  524. */
  525. public function refreshManifestCache()
  526. {
  527. // Need to find to find where the XML file is since we don't store this normally.
  528. $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  529. $manifestPath = $client->path . '/templates/' . $this->parent->extension->element . '/templateDetails.xml';
  530. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  531. $this->parent->setPath('manifest', $manifestPath);
  532. $manifest_details = JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
  533. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  534. $this->parent->extension->name = $manifest_details['name'];
  535. try
  536. {
  537. return $this->parent->extension->store();
  538. }
  539. catch (RuntimeException $e)
  540. {
  541. JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_REFRESH_MANIFEST_CACHE'), JLog::WARNING, 'jerror');
  542. return false;
  543. }
  544. }
  545. }
  546. /**
  547. * Deprecated class placeholder. You should use JInstallerAdapterTemplate instead.
  548. *
  549. * @package Joomla.Libraries
  550. * @subpackage Installer
  551. * @since 3.1
  552. * @deprecated 4.0
  553. * @codeCoverageIgnore
  554. */
  555. class JInstallerTemplate extends JInstallerAdapterTemplate
  556. {
  557. }