/administrator/components/com_templates/controllers/template.php

https://bitbucket.org/asosso/joomla31 · PHP · 104 lines · 64 code · 15 blank · 25 comment · 8 complexity · 4f3b8adf776f53bd05ddf04474108b1c MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_templates
  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.txt
  8. */
  9. defined('_JEXEC') or die;
  10. JLoader::register('InstallerModelInstall', JPATH_ADMINISTRATOR . '/components/com_installer/models/install.php');
  11. /**
  12. * Template style controller class.
  13. *
  14. * @package Joomla.Administrator
  15. * @subpackage com_templates
  16. * @since 1.6
  17. */
  18. class TemplatesControllerTemplate extends JControllerLegacy
  19. {
  20. /**
  21. */
  22. public function cancel()
  23. {
  24. $this->setRedirect('index.php?option=com_templates&view=templates');
  25. }
  26. public function copy()
  27. {
  28. // Check for request forgeries
  29. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  30. $this->input->set('installtype', 'folder');
  31. $newName = $this->input->get('new_name');
  32. $newNameRaw = $this->input->get('new_name', null, 'string');
  33. $templateID = $this->input->getInt('id', 0);
  34. $this->setRedirect('index.php?option=com_templates&view=template&id=' . $templateID);
  35. $model = $this->getModel('Template', 'TemplatesModel');
  36. $model->setState('new_name', $newName);
  37. $model->setState('tmp_prefix', uniqid('template_copy_'));
  38. $model->setState('to_path', JFactory::getConfig()->get('tmp_path') . '/' . $model->getState('tmp_prefix'));
  39. // Process only if we have a new name entered
  40. if (strlen($newName) > 0)
  41. {
  42. if (!JFactory::getUser()->authorise('core.create', 'com_templates'))
  43. {
  44. // User is not authorised to delete
  45. JError::raiseWarning(403, JText::_('COM_TEMPLATES_ERROR_CREATE_NOT_PERMITTED'));
  46. return false;
  47. }
  48. // Set FTP credentials, if given
  49. JClientHelper::setCredentialsFromRequest('ftp');
  50. // Check that new name is valid
  51. if (($newNameRaw !== null) && ($newName !== $newNameRaw))
  52. {
  53. JError::raiseWarning(403, JText::_('COM_TEMPLATES_ERROR_INVALID_TEMPLATE_NAME'));
  54. return false;
  55. }
  56. // Check that new name doesn't already exist
  57. if (!$model->checkNewName())
  58. {
  59. JError::raiseWarning(403, JText::_('COM_TEMPLATES_ERROR_DUPLICATE_TEMPLATE_NAME'));
  60. return false;
  61. }
  62. // Check that from name does exist and get the folder name
  63. $fromName = $model->getFromName();
  64. if (!$fromName)
  65. {
  66. JError::raiseWarning(403, JText::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME'));
  67. return false;
  68. }
  69. // Call model's copy method
  70. if (!$model->copy())
  71. {
  72. JError::raiseWarning(403, JText::_('COM_TEMPLATES_ERROR_COULD_NOT_COPY'));
  73. return false;
  74. }
  75. // Call installation model
  76. $this->input->set('install_directory', JFactory::getConfig()->get('tmp_path') . '/' . $model->getState('tmp_prefix'));
  77. $installModel = $this->getModel('Install', 'InstallerModel');
  78. JFactory::getLanguage()->load('com_installer');
  79. if (!$installModel->install())
  80. {
  81. JError::raiseWarning(403, JText::_('COM_TEMPLATES_ERROR_COULD_NOT_INSTALL'));
  82. return false;
  83. }
  84. $this->setMessage(JText::sprintf('COM_TEMPLATES_COPY_SUCCESS', $newName));
  85. $model->cleanup();
  86. return true;
  87. }
  88. }
  89. }