PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/joomla/libraries/joomla/installer/adapters/template.php

https://github.com/reechalee/joomla1.6
PHP | 500 lines | 316 code | 76 blank | 108 comment | 48 complexity | 07d43c7346dbfa53e52a0b59b2078367 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, JSON
  1. <?php
  2. /**
  3. * @version $Id: template.php 20457 2011-01-27 07:51:58Z infograf768 $
  4. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access
  8. defined('JPATH_BASE') or die;
  9. jimport('joomla.installer.extension');
  10. jimport('joomla.base.adapterinstance');
  11. /**
  12. * Template installer
  13. *
  14. * @package Joomla.Framework
  15. * @subpackage Installer
  16. * @since 1.5
  17. */
  18. class JInstallerTemplate extends JAdapterInstance
  19. {
  20. protected $name = null;
  21. protected $element = null;
  22. protected $route = 'install';
  23. /**
  24. * Custom loadLanguage method
  25. *
  26. * @param string $path the path where to find language files
  27. *
  28. * @since 1.6
  29. */
  30. public function loadLanguage($path=null)
  31. {
  32. $source = $this->parent->getPath('source');
  33. if (!$source) {
  34. $this->parent->setPath('source', ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/templates/'.$this->parent->extension->element);
  35. }
  36. $clientId = isset($this->parent->extension) ? $this->parent->extension->client_id : 0;
  37. $this->manifest = $this->parent->getManifest();
  38. $name = strtolower(JFilterInput::getInstance()->clean((string)$this->manifest->name, 'cmd'));
  39. $client = (string)$this->manifest->attributes()->client;
  40. // Load administrator language if not set.
  41. if (!$client) {
  42. $client = 'ADMINISTRATOR';
  43. }
  44. $extension = "tpl_$name";
  45. $lang = JFactory::getLanguage();
  46. $source = $path ? $path : ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/templates/'.$name;
  47. $lang->load($extension . '.sys', $source, null, false, false)
  48. || $lang->load($extension . '.sys', constant('JPATH_'.strtoupper($client)), null, false, false)
  49. || $lang->load($extension . '.sys', $source, $lang->getDefault(), false, false)
  50. || $lang->load($extension . '.sys', constant('JPATH_'.strtoupper($client)), $lang->getDefault(), false, false);
  51. }
  52. /**
  53. * Custom install method
  54. *
  55. * @return boolean True on success
  56. * @since 1.5
  57. */
  58. public function install()
  59. {
  60. $lang = JFactory::getLanguage();
  61. $xml = $this->parent->getManifest();
  62. // Get the client application target
  63. if ($cname = (string)$xml->attributes()->client) {
  64. // Attempt to map the client to a base path
  65. jimport('joomla.application.helper');
  66. $client = JApplicationHelper::getClientInfo($cname, true);
  67. if ($client === false) {
  68. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_UNKNOWN_CLIENT', $cname));
  69. return false;
  70. }
  71. $basePath = $client->path;
  72. $clientId = $client->id;
  73. }
  74. else {
  75. // No client attribute was found so we assume the site as the client
  76. $cname = 'site';
  77. $basePath = JPATH_SITE;
  78. $clientId = 0;
  79. }
  80. // Set the extensions name
  81. $name = JFilterInput::getInstance()->clean((string)$xml->name, 'cmd');
  82. $element = strtolower(str_replace(" ", "_", $name));
  83. $this->set('name', $name);
  84. $this->set('element',$element);
  85. $db = $this->parent->getDbo();
  86. $db->setQuery('SELECT extension_id FROM #__extensions WHERE type="template" AND element = "'. $element .'"');
  87. $id = $db->loadResult();
  88. // Set the template root path
  89. $this->parent->setPath('extension_root', $basePath.DS.'templates'.DS.$element);
  90. // if its on the fs...
  91. if (file_exists($this->parent->getPath('extension_root')) && (!$this->parent->getOverwrite() || $this->parent->getUpgrade()))
  92. {
  93. $updateElement = $xml->update;
  94. // upgrade manually set
  95. // update function available
  96. // update tag detected
  97. if ($this->parent->getUpgrade() || ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'update')) || is_a($updateElement, 'JXMLElement'))
  98. {
  99. // force these one
  100. $this->parent->setOverwrite(true);
  101. $this->parent->setUpgrade(true);
  102. if ($id) { // if there is a matching extension mark this as an update; semantics really
  103. $this->route = 'update';
  104. }
  105. }
  106. else if (!$this->parent->getOverwrite())
  107. {
  108. // overwrite is set
  109. // we didn't have overwrite set, find an udpate function or find an update tag so lets call it safe
  110. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_PLG_INSTALL_DIRECTORY', JText::_('JLIB_INSTALLER_'.$this->route), $this->parent->getPath('extension_root')));
  111. return false;
  112. }
  113. }
  114. /*
  115. * If the template directory already exists, then we will assume that the template is already
  116. * installed or another template is using that directory.
  117. */
  118. if (file_exists($this->parent->getPath('extension_root')) && !$this->parent->getOverwrite()) {
  119. JError::raiseWarning(100, JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ANOTHER_TEMPLATE_USING_DIRECTORY', $this->parent->getPath('extension_root')));
  120. return false;
  121. }
  122. // If the template directory does not exist, lets create it
  123. $created = false;
  124. if (!file_exists($this->parent->getPath('extension_root'))) {
  125. if (!$created = JFolder::create($this->parent->getPath('extension_root'))) {
  126. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_FAILED_CREATE_DIRECTORY', $this->parent->getPath('extension_root')));
  127. return false;
  128. }
  129. }
  130. // If we created the template directory and will want to remove it if we have to roll back
  131. // the installation, lets add it to the installation step stack
  132. if ($created) {
  133. $this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
  134. }
  135. // Copy all the necessary files
  136. if ($this->parent->parseFiles($xml->files, -1) === false) {
  137. // Install failed, rollback changes
  138. $this->parent->abort();
  139. return false;
  140. }
  141. if ($this->parent->parseFiles($xml->images, -1) === false) {
  142. // Install failed, rollback changes
  143. $this->parent->abort();
  144. return false;
  145. }
  146. if ($this->parent->parseFiles($xml->css, -1) === false) {
  147. // Install failed, rollback changes
  148. $this->parent->abort();
  149. return false;
  150. }
  151. // Parse optional tags
  152. $this->parent->parseMedia($xml->media);
  153. $this->parent->parseLanguages($xml->languages, $clientId);
  154. // Get the template description
  155. $this->parent->set('message', JText::_((string)$xml->description));
  156. // Lastly, we will copy the manifest file to its appropriate place.
  157. if (!$this->parent->copyManifest(-1)) {
  158. // Install failed, rollback changes
  159. $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_SETUP'));
  160. return false;
  161. }
  162. /**
  163. * ---------------------------------------------------------------------------------------------
  164. * Extension Registration
  165. * ---------------------------------------------------------------------------------------------
  166. */
  167. $row = JTable::getInstance('extension');
  168. if($this->route == 'update' && $id)
  169. {
  170. $row->load($id);
  171. }
  172. else
  173. {
  174. $row->type = 'template';
  175. $row->element = $this->get('element');
  176. $row->folder = ''; // There is no folder for templates
  177. $row->enabled = 1;
  178. $row->protected = 0;
  179. $row->access = 1;
  180. $row->client_id = $clientId;
  181. $row->params = $this->parent->getParams();
  182. $row->custom_data = ''; // custom data
  183. }
  184. $row->name = $this->get('name'); // name might change in an update
  185. $row->manifest_cache = $this->parent->generateManifestCache();
  186. if (!$row->store()) {
  187. // Install failed, roll back changes
  188. $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ROLLBACK', $db->stderr(true)));
  189. return false;
  190. }
  191. if($this->route == 'install')
  192. {
  193. //insert record in #__template_styles
  194. $query = $db->getQuery(true);
  195. $query->insert('#__template_styles');
  196. $query->set('template='.$db->Quote($row->element));
  197. $query->set('client_id='.$db->Quote($clientId));
  198. $query->set('home=0');
  199. $debug = $lang->setDebug(false);
  200. $query->set('title='.$db->Quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', JText::_($this->get('name')))));
  201. $lang->setDebug($debug);
  202. $query->set('params='.$db->Quote($row->params));
  203. $db->setQuery($query);
  204. $db->query(); // There is a chance this could fail but we don't care...
  205. }
  206. return $row->get('extension_id');
  207. }
  208. /**
  209. * Custom update method for components
  210. *
  211. * @return boolean True on success
  212. * @since 1.5
  213. */
  214. public function update()
  215. {
  216. return $this->install();
  217. }
  218. /**
  219. * Custom uninstall method
  220. *
  221. * @param int $id The extension ID
  222. *
  223. * @return boolean True on success
  224. * @since 1.5
  225. */
  226. public function uninstall($id)
  227. {
  228. // Initialise variables.
  229. $retval = true;
  230. // First order of business will be to load the module object table from the database.
  231. // This should give us the necessary information to proceed.
  232. $row = JTable::getInstance('extension');
  233. if (!$row->load((int) $id) || !strlen($row->element)) {
  234. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_ERRORUNKOWNEXTENSION'));
  235. return false;
  236. }
  237. // Is the template we are trying to uninstall a core one?
  238. // Because that is not a good idea...
  239. if ($row->protected) {
  240. JError::raiseWarning(100, JText::sprintf('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_WARNCORETEMPLATE', $row->name));
  241. return false;
  242. }
  243. $name = $row->element;
  244. $clientId = $row->client_id;
  245. // For a template the id will be the template name which represents the subfolder of the templates folder that the template resides in.
  246. if (!$name) {
  247. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY'));
  248. return false;
  249. }
  250. // Deny remove default template
  251. $db = $this->parent->getDbo();
  252. $query = 'SELECT COUNT(*) FROM #__template_styles'.
  253. ' WHERE home = 1 AND template = '.$db->Quote($name);
  254. $db->setQuery($query);
  255. if ($db->loadResult() != 0) {
  256. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DEFAULT'));
  257. return false;
  258. }
  259. // Get the template root path
  260. $client = JApplicationHelper::getClientInfo($clientId);
  261. if (!$client) {
  262. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_CLIENT'));
  263. return false;
  264. }
  265. $this->parent->setPath('extension_root', $client->path.DS.'templates'.DS.strtolower($name));
  266. $this->parent->setPath('source', $this->parent->getPath('extension_root'));
  267. // We do findManifest to avoid problem when uninstalling a list of extension: getManifest cache its manifest file
  268. $this->parent->findManifest();
  269. $manifest = $this->parent->getManifest();
  270. if (!($manifest instanceof JXMLElement)) {
  271. // kill the extension entry
  272. $row->delete($row->extension_id);
  273. unset($row);
  274. // Make sure we delete the folders
  275. JFolder::delete($this->parent->getPath('extension_root'));
  276. JError::raiseWarning(100, JTEXT::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_NOTFOUND_MANIFEST'));
  277. return false;
  278. }
  279. // Remove files
  280. $this->parent->removeFiles($manifest->media);
  281. $this->parent->removeFiles($manifest->languages, $clientId);
  282. // Delete the template directory
  283. if (JFolder::exists($this->parent->getPath('extension_root'))) {
  284. $retval = JFolder::delete($this->parent->getPath('extension_root'));
  285. }
  286. else {
  287. JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DIRECTORY'));
  288. $retval = false;
  289. }
  290. //Set menu that assigned to the template back to default template
  291. $query = 'UPDATE #__menu INNER JOIN #__template_styles'.
  292. ' ON #__template_styles.id = #__menu.template_style_id'.
  293. ' SET #__menu.template_style_id = 0'.
  294. ' WHERE #__template_styles.template = '.$db->Quote(strtolower($name)).
  295. ' AND #__template_styles.client_id = '.$db->Quote($clientId);
  296. $db->setQuery($query);
  297. $db->Query();
  298. $query = 'DELETE FROM #__template_styles'.
  299. ' WHERE template = '.$db->Quote($name).
  300. ' AND client_id = '.$db->Quote($clientId);
  301. $db->setQuery($query);
  302. $db->Query();
  303. $row->delete($row->extension_id);
  304. unset($row);
  305. return $retval;
  306. }
  307. /**
  308. * Discover existing but uninstalled templates
  309. *
  310. * @return Array JExtensionTable list
  311. */
  312. function discover()
  313. {
  314. $results = Array();
  315. $site_list = JFolder::folders(JPATH_SITE.DS.'templates');
  316. $admin_list = JFolder::folders(JPATH_ADMINISTRATOR.DS.'templates');
  317. $site_info = JApplicationHelper::getClientInfo('site', true);
  318. $admin_info = JApplicationHelper::getClientInfo('administrator', true);
  319. foreach ($site_list as $template)
  320. {
  321. if ($template == 'system') {
  322. continue;
  323. // ignore special system template
  324. }
  325. $manifest_details = JApplicationHelper::parseXMLInstallFile(JPATH_SITE."/templates/$template/templateDetails.xml");
  326. $extension = JTable::getInstance('extension');
  327. $extension->set('type', 'template');
  328. $extension->set('client_id', $site_info->id);
  329. $extension->set('element', $template);
  330. $extension->set('name', $template);
  331. $extension->set('state', -1);
  332. $extension->set('manifest_cache', json_encode($manifest_details));
  333. $results[] = $extension;
  334. }
  335. foreach ($admin_list as $template)
  336. {
  337. if ($template == 'system') {
  338. continue;
  339. // ignore special system template
  340. }
  341. $manifest_details = JApplicationHelper::parseXMLInstallFile(JPATH_ADMINISTRATOR."/templates/$template/templateDetails.xml");
  342. $extension = JTable::getInstance('extension');
  343. $extension->set('type', 'template');
  344. $extension->set('client_id', $admin_info->id);
  345. $extension->set('element', $template);
  346. $extension->set('name', $template);
  347. $extension->set('state', -1);
  348. $extension->set('manifest_cache', json_encode($manifest_details));
  349. $results[] = $extension;
  350. }
  351. return $results;
  352. }
  353. /**
  354. * Perform an install from a discovered extension
  355. */
  356. function discover_install()
  357. {
  358. // Templates are one of the easiest
  359. // If its not in the extensions table we just add it
  360. $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  361. $manifestPath = $client->path.DS.'templates'.DS.$this->parent->extension->element.DS.'templateDetails.xml';
  362. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  363. $description = (string)$this->parent->manifest->description;
  364. if ($description) {
  365. $this->parent->set('message', JText::_($description));
  366. }
  367. else {
  368. $this->parent->set('message', '');
  369. }
  370. $this->parent->setPath('manifest', $manifestPath);
  371. $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
  372. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  373. $this->parent->extension->state = 0;
  374. $this->parent->extension->name = $manifest_details['name'];
  375. $this->parent->extension->enabled = 1;
  376. $data = new JObject();
  377. foreach ($manifest_details as $key => $value)
  378. {
  379. $data->set($key, $value);
  380. }
  381. $this->parent->extension->params = $this->parent->getParams();
  382. if ($this->parent->extension->store()) {
  383. //insert record in #__template_styles
  384. $db = $this->parent->getDbo();
  385. $query = $db->getQuery(true);
  386. $query->insert('#__template_styles');
  387. $query->set('template='.$db->Quote($this->parent->extension->name));
  388. $query->set('client_id='.$db->Quote($this->parent->extension->client_id));
  389. $query->set('home=0');
  390. $query->set('title='.$db->Quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', $this->parent->extension->name)));
  391. $query->set('params='.$db->Quote($this->parent->extension->params));
  392. $db->setQuery($query);
  393. $db->query();
  394. return $this->parent->extension->get('extension_id');
  395. }
  396. else {
  397. JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_TPL_DISCOVER_STORE_DETAILS'));
  398. return false;
  399. }
  400. }
  401. /**
  402. * Refreshes the extension table cache
  403. * @return boolean result of operation, true if updated, false on failure
  404. * @since 1.6
  405. */
  406. public function refreshManifestCache()
  407. {
  408. // Need to find to find where the XML file is since we don't store this normally
  409. $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  410. $manifestPath = $client->path.DS.'templates'. DS.$this->parent->extension->element.DS.'templateDetails.xml';
  411. $this->parent->manifest = $this->parent->isManifest($manifestPath);
  412. $this->parent->setPath('manifest', $manifestPath);
  413. $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
  414. $this->parent->extension->manifest_cache = json_encode($manifest_details);
  415. $this->parent->extension->name = $manifest_details['name'];
  416. try {
  417. return $this->parent->extension->store();
  418. }
  419. catch(JException $e) {
  420. JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_TPL_REFRESH_MANIFEST_CACHE'));
  421. return false;
  422. }
  423. }
  424. }