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

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

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