Source for file template.php

Documentation is available at template.php

  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.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Template model class.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_templates
  17.  * @since       1.6
  18.  */
  19. {
  20.     protected $template = null;
  21.  
  22.     protected $element = null;
  23.  
  24.     /**
  25.      * Internal method to get file properties.
  26.      *
  27.      * @param   string  $path  The base path.
  28.      * @param   string  $name  The file name.
  29.      *
  30.      * @return  object 
  31.      *
  32.      * @since   1.6
  33.      */
  34.     protected function getFile($path$name)
  35.     {
  36.         $temp new stdClass;
  37.  
  38.         if ($template $this->getTemplate())
  39.         {
  40.             $temp->name $name;
  41.             $temp->id urlencode(base64_encode($path $name));
  42.  
  43.             return $temp;
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Method to get a list of all the files to edit in a template.
  49.      *
  50.      * @return  array  A nested array of relevant files.
  51.      *
  52.      * @since   1.6
  53.      */
  54.     public function getFiles()
  55.     {
  56.         $result    array();
  57.  
  58.         if ($template $this->getTemplate())
  59.         {
  60.             jimport('joomla.filesystem.folder');
  61.             $app    JFactory::getApplication();
  62.             $client JApplicationHelper::getClientInfo($template->client_id);
  63.             $path   JPath::clean($client->path '/templates/' $template->element '/');
  64.             $lang   JFactory::getLanguage();
  65.  
  66.             // Load the core and/or local language file(s).
  67.             $lang->load('tpl_' $template->element$client->pathnullfalsetrue||
  68.             $lang->load('tpl_' $template->element$client->path '/templates/' $template->elementnullfalsetrue);
  69.             $this->element = $path;
  70.  
  71.             if (!is_writable($path))
  72.             {
  73.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_DIRECTORY_NOT_WRITABLE')'error');
  74.             }
  75.  
  76.             if (is_dir($path))
  77.             {
  78.                 $result $this->getDirectoryTree($path);
  79.             }
  80.             else
  81.             {
  82.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_TEMPLATE_FOLDER_NOT_FOUND')'error');
  83.  
  84.                 return false;
  85.             }
  86.         }
  87.  
  88.         return $result;
  89.     }
  90.  
  91.     /**
  92.      * Get the directory tree.
  93.      *
  94.      * @param   string  $dir  The path of the directory to scan
  95.      *
  96.      * @return  array 
  97.      *
  98.      * @since   3.2
  99.      */
  100.     public function getDirectoryTree($dir)
  101.     {
  102.         $result array();
  103.  
  104.         $dirFiles scandir($dir);
  105.  
  106.         foreach ($dirFiles as $key => $value)
  107.         {
  108.             if (!in_array($valuearray(".""..")))
  109.             {
  110.                 if (is_dir($dir $value '/'))
  111.                 {
  112.                     $relativePath str_replace($this->element''$dir $value);
  113.                     $result['/' $relativePath$this->getDirectoryTree($dir $value '/');
  114.                 }
  115.                 else
  116.                 {
  117.                     $ext          pathinfo($dir $valuePATHINFO_EXTENSION);
  118.                     $params       JComponentHelper::getParams('com_templates');
  119.                     $imageTypes   explode(','$params->get('image_formats'));
  120.                     $sourceTypes  explode(','$params->get('source_formats'));
  121.                     $fontTypes    explode(','$params->get('font_formats'));
  122.                     $archiveTypes explode(','$params->get('compressed_formats'));
  123.  
  124.                     $types array_merge($imageTypes$sourceTypes$fontTypes$archiveTypes);
  125.  
  126.                     if (in_array($ext$types))
  127.                     {
  128.                         $relativePath str_replace($this->element''$dir);
  129.                         $info $this->getFile('/' $relativePath$value);
  130.                         $result[$info;
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.  
  136.         return $result;
  137.     }
  138.  
  139.     /**
  140.      * Method to auto-populate the model state.
  141.      *
  142.      * Note. Calling getState in this method will result in recursion.
  143.      *
  144.      * @return  void 
  145.      *
  146.      * @since   1.6
  147.      */
  148.     protected function populateState()
  149.     {
  150.         jimport('joomla.filesystem.file');
  151.         $app JFactory::getApplication('administrator');
  152.  
  153.         // Load the User state.
  154.         $pk $app->input->getInt('id');
  155.         $this->setState('extension.id'$pk);
  156.  
  157.         // Load the parameters.
  158.         $params JComponentHelper::getParams('com_templates');
  159.         $this->setState('params'$params);
  160.     }
  161.  
  162.     /**
  163.      * Method to get the template information.
  164.      *
  165.      * @return  mixed  Object if successful, false if not and internal error is set.
  166.      *
  167.      * @since   1.6
  168.      */
  169.     public function &getTemplate()
  170.     {
  171.         if (empty($this->template))
  172.         {
  173.             $pk  $this->getState('extension.id');
  174.             $db  $this->getDbo();
  175.             $app JFactory::getApplication();
  176.  
  177.             // Get the template information.
  178.             $query $db->getQuery(true)
  179.                 ->select('extension_id, client_id, element, name')
  180.                 ->from('#__extensions')
  181.                 ->where($db->quoteName('extension_id'' = ' . (int) $pk)
  182.                 ->where($db->quoteName('type'' = ' $db->quote('template'));
  183.             $db->setQuery($query);
  184.  
  185.             try
  186.             {
  187.                 $result $db->loadObject();
  188.             }
  189.             catch (RuntimeException $e)
  190.             {
  191.                 $app->enqueueMessage($e->getMessage()'warning');
  192.                 $this->template = false;
  193.  
  194.                 return false;
  195.             }
  196.  
  197.             if (empty($result))
  198.             {
  199.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND')'error');
  200.                 $this->template = false;
  201.             }
  202.             else
  203.             {
  204.                 $this->template = $result;
  205.             }
  206.         }
  207.  
  208.         return $this->template;
  209.     }
  210.  
  211.     /**
  212.      * Method to check if new template name already exists
  213.      *
  214.      * @return  boolean   true if name is not used, false otherwise
  215.      *
  216.      * @since    2.5
  217.      */
  218.     public function checkNewName()
  219.     {
  220.         $db $this->getDbo();
  221.         $query $db->getQuery(true)
  222.             ->select('COUNT(*)')
  223.             ->from('#__extensions')
  224.             ->where('name = ' $db->quote($this->getState('new_name')));
  225.         $db->setQuery($query);
  226.  
  227.         return ($db->loadResult(== 0);
  228.     }
  229.  
  230.     /**
  231.      * Method to check if new template name already exists
  232.      *
  233.      * @return  string     name of current template
  234.      *
  235.      * @since    2.5
  236.      */
  237.     public function getFromName()
  238.     {
  239.         return $this->getTemplate()->element;
  240.     }
  241.  
  242.     /**
  243.      * Method to check if new template name already exists
  244.      *
  245.      * @return  boolean   true if name is not used, false otherwise
  246.      *
  247.      * @since    2.5
  248.      */
  249.     public function copy()
  250.     {
  251.         $app JFactory::getApplication();
  252.  
  253.         if ($template $this->getTemplate())
  254.         {
  255.             jimport('joomla.filesystem.folder');
  256.             $client JApplicationHelper::getClientInfo($template->client_id);
  257.             $fromPath JPath::clean($client->path '/templates/' $template->element '/');
  258.  
  259.             // Delete new folder if it exists
  260.             $toPath $this->getState('to_path');
  261.  
  262.             if (JFolder::exists($toPath))
  263.             {
  264.                 if (!JFolder::delete($toPath))
  265.                 {
  266.                     $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE')'error');
  267.  
  268.                     return false;
  269.                 }
  270.             }
  271.  
  272.             // Copy all files from $fromName template to $newName folder
  273.             if (!JFolder::copy($fromPath$toPath|| !$this->fixTemplateName())
  274.             {
  275.                 return false;
  276.             }
  277.  
  278.             return true;
  279.         }
  280.         else
  281.         {
  282.             $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME')'error');
  283.  
  284.             return false;
  285.         }
  286.     }
  287.  
  288.     /**
  289.      * Method to delete tmp folder
  290.      *
  291.      * @return  boolean   true if delete successful, false otherwise
  292.      *
  293.      * @since    2.5
  294.      */
  295.     public function cleanup()
  296.     {
  297.         // Clear installation messages
  298.         $app JFactory::getApplication();
  299.         $app->setUserState('com_installer.message''');
  300.         $app->setUserState('com_installer.extension_message''');
  301.  
  302.         // Delete temporary directory
  303.         return JFolder::delete($this->getState('to_path'));
  304.     }
  305.  
  306.     /**
  307.      * Method to rename the template in the XML files and rename the language files
  308.      *
  309.      * @return  boolean  true if successful, false otherwise
  310.      *
  311.      * @since    2.5
  312.      */
  313.     protected function fixTemplateName()
  314.     {
  315.         // Rename Language files
  316.         // Get list of language files
  317.         $result true;
  318.         $files JFolder::files($this->getState('to_path')'.ini'truetrue);
  319.         $newName strtolower($this->getState('new_name'));
  320.         $oldName $this->getTemplate()->element;
  321.  
  322.         jimport('joomla.filesystem.file');
  323.  
  324.         foreach ($files as $file)
  325.         {
  326.             $newFile str_replace($oldName$newName$file);
  327.             $result JFile::move($file$newFile&& $result;
  328.         }
  329.  
  330.         // Edit XML file
  331.         $xmlFile $this->getState('to_path''/templateDetails.xml';
  332.  
  333.         if (JFile::exists($xmlFile))
  334.         {
  335.             $contents file_get_contents($xmlFile);
  336.             $pattern['#<name>\s*' $oldName '\s*</name>#i';
  337.             $replace['<name>' $newName '</name>';
  338.             $pattern['#<language(.*)' $oldName '(.*)</language>#';
  339.             $replace['<language${1}' $newName '${2}</language>';
  340.             $contents preg_replace($pattern$replace$contents);
  341.             $result JFile::write($xmlFile$contents&& $result;
  342.         }
  343.  
  344.         return $result;
  345.     }
  346.  
  347.     /**
  348.      * Method to get the record form.
  349.      *
  350.      * @param   array    $data      Data for the form.
  351.      * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  352.      *
  353.      * @return  JForm    A JForm object on success, false on failure
  354.      *
  355.      * @since   1.6
  356.      */
  357.     public function getForm($data array()$loadData true)
  358.     {
  359.         $app JFactory::getApplication();
  360.  
  361.         // Codemirror or Editor None should be enabled
  362.         $db JFactory::getDbo();
  363.         $query $db->getQuery(true)
  364.             ->select('COUNT(*)')
  365.             ->from('#__extensions as a')
  366.             ->where(
  367.                 '(a.name =' $db->quote('plg_editors_codemirror'.
  368.                 ' AND a.enabled = 1) OR (a.name =' .
  369.                 $db->quote('plg_editors_none'.
  370.                 ' AND a.enabled = 1)'
  371.             );
  372.         $db->setQuery($query);
  373.         $state $db->loadResult();
  374.  
  375.         if ((int) $state 1)
  376.         {
  377.             $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EDITOR_DISABLED')'warning');
  378.         }
  379.  
  380.         // Get the form.
  381.         $form $this->loadForm('com_templates.source''source'array('control' => 'jform''load_data' => $loadData));
  382.  
  383.         if (empty($form))
  384.         {
  385.             return false;
  386.         }
  387.  
  388.         return $form;
  389.     }
  390.  
  391.     /**
  392.      * Method to get the data that should be injected in the form.
  393.      *
  394.      * @return  mixed  The data for the form.
  395.      *
  396.      * @since   1.6
  397.      */
  398.     protected function loadFormData()
  399.     {
  400.         $data $this->getSource();
  401.  
  402.         $this->preprocessData('com_templates.source'$data);
  403.  
  404.         return $data;
  405.     }
  406.  
  407.     /**
  408.      * Method to get a single record.
  409.      *
  410.      * @return  mixed  Object on success, false on failure.
  411.      *
  412.      * @since   1.6
  413.      */
  414.     public function &getSource()
  415.     {
  416.         $app JFactory::getApplication();
  417.         $item new stdClass;
  418.  
  419.         if (!$this->template)
  420.         {
  421.             $this->getTemplate();
  422.         }
  423.  
  424.         if ($this->template)
  425.         {
  426.             $input    JFactory::getApplication()->input;
  427.             $fileName base64_decode($input->get('file'));
  428.             $client   JApplicationHelper::getClientInfo($this->template->client_id);
  429.             $filePath JPath::clean($client->path '/templates/' $this->template->element '/' $fileName);
  430.  
  431.             if (file_exists($filePath))
  432.             {
  433.                 $item->extension_id $this->getState('extension.id');
  434.                 $item->filename $fileName;
  435.                 $item->source file_get_contents($filePath);
  436.             }
  437.             else
  438.             {
  439.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND')'error');
  440.             }
  441.         }
  442.  
  443.         return $item;
  444.     }
  445.  
  446.     /**
  447.      * Method to store the source file contents.
  448.      *
  449.      * @param   array  $data  The source data to save.
  450.      *
  451.      * @return  boolean  True on success, false otherwise and internal error set.
  452.      *
  453.      * @since   1.6
  454.      */
  455.     public function save($data)
  456.     {
  457.         jimport('joomla.filesystem.file');
  458.  
  459.         // Get the template.
  460.         $template $this->getTemplate();
  461.  
  462.         if (empty($template))
  463.         {
  464.             return false;
  465.         }
  466.  
  467.         $app JFactory::getApplication();
  468.         $fileName base64_decode($app->input->get('file'));
  469.         $client JApplicationHelper::getClientInfo($template->client_id);
  470.         $filePath JPath::clean($client->path '/templates/' $template->element '/' $fileName);
  471.  
  472.         // Include the extension plugins for the save events.
  473.         JPluginHelper::importPlugin('extension');
  474.  
  475.         $user get_current_user();
  476.         chown($filePath$user);
  477.         JPath::setPermissions($filePath'0644');
  478.  
  479.         // Try to make the template file writable.
  480.         if (!is_writable($filePath))
  481.         {
  482.             $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_WRITABLE')'warning');
  483.             $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_PERMISSIONS' JPath::getPermissions($filePath))'warning');
  484.  
  485.             if (!JPath::isOwner($filePath))
  486.             {
  487.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_CHECK_FILE_OWNERSHIP')'warning');
  488.             }
  489.  
  490.             return false;
  491.         }
  492.  
  493.         $return JFile::write($filePath$data['source']);
  494.  
  495.         // Try to make the template file unwritable.
  496.         if (JPath::isOwner($filePath&& !JPath::setPermissions($filePath'0444'))
  497.         {
  498.             $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_UNWRITABLE')'error');
  499.  
  500.             return false;
  501.         }
  502.         elseif (!$return)
  503.         {
  504.             $app->enqueueMessage(JText::sprintf('COM_TEMPLATES_ERROR_FAILED_TO_SAVE_FILENAME'$fileName)'error');
  505.  
  506.             return false;
  507.         }
  508.  
  509.         $explodeArray explode('.'$fileName);
  510.         $ext end($explodeArray);
  511.  
  512.         if ($ext == 'less')
  513.         {
  514.             $app->enqueueMessage(JText::sprintf('COM_TEMPLATES_COMPILE_LESS'$fileName));
  515.         }
  516.  
  517.         return true;
  518.     }
  519.  
  520.     /**
  521.      * Get overrides folder.
  522.      *
  523.      * @param   string  $name  The name of override.
  524.      * @param   string  $path  Location of override.
  525.      *
  526.      * @return  object  containing override name and path.
  527.      *
  528.      * @since   3.2
  529.      */
  530.     public function getOverridesFolder($name,$path)
  531.     {
  532.         $folder new stdClass;
  533.         $folder->name $name;
  534.         $folder->path base64_encode($path $name);
  535.  
  536.         return $folder;
  537.     }
  538.  
  539.     /**
  540.      * Get a list of overrides.
  541.      *
  542.      * @return  array containing overrides.
  543.      *
  544.      * @since   3.2
  545.      */
  546.     public function getOverridesList()
  547.     {
  548.         if ($template $this->getTemplate())
  549.         {
  550.             $client             JApplicationHelper::getClientInfo($template->client_id);
  551.             $componentPath        JPath::clean($client->path '/components/');
  552.             $modulePath            JPath::clean($client->path '/modules/');
  553.             $layoutPath            JPath::clean(JPATH_ROOT '/layouts/joomla/');
  554.             $components         JFolder::folders($componentPath);
  555.  
  556.             foreach ($components as $component)
  557.             {
  558.                 $viewPath    JPath::clean($componentPath '/' $component '/views/');
  559.  
  560.                 if (file_exists($viewPath))
  561.                 {
  562.                     $views    JFolder::folders($viewPath);
  563.  
  564.                     foreach ($views as $view)
  565.                     {
  566.                         $result['components'][$component][$this->getOverridesFolder($view$viewPath);
  567.                     }
  568.                 }
  569.             }
  570.  
  571.             $modules JFolder::folders($modulePath);
  572.  
  573.             foreach ($modules as $module)
  574.             {
  575.                 $result['modules'][$this->getOverridesFolder($module$modulePath);
  576.             }
  577.  
  578.             $layouts JFolder::folders($layoutPath);
  579.  
  580.             foreach ($layouts as $layout)
  581.             {
  582.                 $result['layouts'][$this->getOverridesFolder($layout$layoutPath);
  583.             }
  584.         }
  585.  
  586.         if (!empty($result))
  587.         {
  588.             return $result;
  589.         }
  590.     }
  591.  
  592.     /**
  593.      * Create overrides.
  594.      *
  595.      * @param   string  $override  The override location.
  596.      *
  597.      * @return   boolean  true if override creation is successful, false otherwise
  598.      *
  599.      * @since   3.2
  600.      */
  601.     public function createOverride($override)
  602.     {
  603.         jimport('joomla.filesystem.folder');
  604.  
  605.         if ($template $this->getTemplate())
  606.         {
  607.             $app            JFactory::getApplication();
  608.             $explodeArray   explode(DIRECTORY_SEPARATOR$override);
  609.             $name           end($explodeArray);
  610.             $client         JApplicationHelper::getClientInfo($template->client_id);
  611.  
  612.             if (stristr($name'mod_'!= false)
  613.             {
  614.                 $htmlPath   JPath::clean($client->path '/templates/' $template->element '/html/' $name);
  615.             }
  616.             elseif (stristr($override'com_'!= false)
  617.             {
  618.                 $folderExplode explode(DIRECTORY_SEPARATOR$override);
  619.                 $size count($folderExplode);
  620.  
  621.                 $url JPath::clean($folderExplode[$size 3'/' $folderExplode[$size 1]);
  622.  
  623.                 $htmlPath JPath::clean($client->path '/templates/' $template->element '/html/' $url);
  624.             }
  625.             else
  626.             {
  627.                 $htmlPath   JPath::clean($client->path '/templates/' $template->element '/html/layouts/joomla/' $name);
  628.             }
  629.  
  630.             if (JFolder::exists($htmlPath))
  631.             {
  632.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_OVERRIDE_EXISTS')'error');
  633.  
  634.                 return false;
  635.             }
  636.  
  637.             if (!JFolder::create($htmlPath))
  638.             {
  639.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_ERROR')'error');
  640.  
  641.                 return false;
  642.             }
  643.  
  644.             if (stristr($name'mod_'!= false)
  645.             {
  646.                 $return JFolder::copy($override '/tmpl'$htmlPath''true);
  647.             }
  648.             elseif (stristr($override'com_'!= false)
  649.             {
  650.                 $return JFolder::copy($override '/tmpl'$htmlPath '/'''true);
  651.             }
  652.             else
  653.             {
  654.                 $return JFolder::copy($override$htmlPath''true);
  655.             }
  656.  
  657.             if ($return)
  658.             {
  659.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_OVERRIDE_CREATED'str_replace(JPATH_ROOT''$htmlPath));
  660.  
  661.                 return true;
  662.             }
  663.             else
  664.             {
  665.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_OVERRIDE_FAILED')'error');
  666.  
  667.                 return false;
  668.             }
  669.         }
  670.     }
  671.  
  672.     /**
  673.      * Compile less using the less compiler under /build.
  674.      *
  675.      * @param   string  $input  The relative location of the less file.
  676.      *
  677.      * @return  boolean  true if compilation is successful, false otherwise
  678.      *
  679.      * @since   3.2
  680.      */
  681.     public function compileLess($input)
  682.     {
  683.         if ($template $this->getTemplate())
  684.         {
  685.             $app          JFactory::getApplication();
  686.             $client       JApplicationHelper::getClientInfo($template->client_id);
  687.             $path         JPath::clean($client->path '/templates/' $template->element '/');
  688.             $inFile       urldecode(base64_decode($input));
  689.             $explodeArray explode('/'$inFile);
  690.             $fileName     end($explodeArray);
  691.             $outFile      reset(explode('.'$fileName));
  692.  
  693.             // Load the RAD layer to use its LESS compiler
  694.             if (!defined('FOF_INCLUDED'))
  695.             {
  696.                 require_once JPATH_LIBRARIES '/fof/include.php';
  697.             }
  698.  
  699.             $less new FOFLess;
  700.             $less->setFormatter(new FOFLessFormatterJoomla);
  701.  
  702.             try
  703.             {
  704.                 $less->compileFile($path $inFile$path 'css/' $outFile '.css');
  705.  
  706.                 return true;
  707.             }
  708.             catch (Exception $e)
  709.             {
  710.                 $app->enqueueMessage($e->getMessage()'error');
  711.             }
  712.         }
  713.     }
  714.  
  715.     /**
  716.      * Delete a particular file.
  717.      *
  718.      * @param   string  $file  The relative location of the file.
  719.      *
  720.      * @return   boolean  True if file deletion is successful, false otherwise
  721.      *
  722.      * @since   3.2
  723.      */
  724.     public function deleteFile($file)
  725.     {
  726.         if ($template $this->getTemplate())
  727.         {
  728.             $app      JFactory::getApplication();
  729.             $client   JApplicationHelper::getClientInfo($template->client_id);
  730.             $path     JPath::clean($client->path '/templates/' $template->element '/');
  731.             $filePath $path urldecode(base64_decode($file));
  732.  
  733.             $return JFile::delete($filePath);
  734.  
  735.             if (!$return)
  736.             {
  737.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_DELETE_FAIL')'error');
  738.  
  739.                 return false;
  740.             }
  741.  
  742.             return true;
  743.         }
  744.     }
  745.  
  746.     /**
  747.      * Create new file.
  748.      *
  749.      * @param   string  $name      The name of file.
  750.      * @param   string  $type      The extension of the file.
  751.      * @param   string  $location  Location for the new file.
  752.      *
  753.      * @return  boolean  true if file created successfully, false otherwise
  754.      *
  755.      * @since   3.2
  756.      */
  757.     public function createFile($name$type$location)
  758.     {
  759.         if ($template $this->getTemplate())
  760.         {
  761.             $app    JFactory::getApplication();
  762.             $client JApplicationHelper::getClientInfo($template->client_id);
  763.             $path   JPath::clean($client->path '/templates/' $template->element '/');
  764.  
  765.             if (file_exists(JPath::clean($path '/' $location '/' $name '.' $type)))
  766.             {
  767.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS')'error');
  768.  
  769.                 return false;
  770.             }
  771.  
  772.             if (!fopen(JPath::clean($path '/' $location '/' $name '.' $type)'x'))
  773.             {
  774.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_CREATE_ERROR')'error');
  775.  
  776.                 return false;
  777.             }
  778.  
  779.             return true;
  780.         }
  781.     }
  782.  
  783.     /**
  784.      * Upload new file.
  785.      *
  786.      * @param   string  $file      The name of the file.
  787.      * @param   string  $location  Location for the new file.
  788.      *
  789.      * @return   boolean  True if file uploaded successfully, false otherwise
  790.      *
  791.      * @since   3.2
  792.      */
  793.     public function uploadFile($file$location)
  794.     {
  795.         jimport('joomla.filesystem.folder');
  796.  
  797.         if ($template $this->getTemplate())
  798.         {
  799.             $app      JFactory::getApplication();
  800.             $client   JApplicationHelper::getClientInfo($template->client_id);
  801.             $path     JPath::clean($client->path '/templates/' $template->element '/');
  802.             $fileName JFile::makeSafe($file['name']);
  803.  
  804.             $err null;
  805.             JLoader::register('TemplateHelper'JPATH_COMPONENT_ADMINISTRATOR '/helpers/template.php');
  806.  
  807.             if (!TemplateHelper::canUpload($file$err))
  808.             {
  809.                 // Can't upload the file
  810.                 return false;
  811.             }
  812.  
  813.             if (file_exists(JPath::clean($path '/' $location '/' $file['name'])))
  814.             {
  815.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS')'error');
  816.  
  817.                 return false;
  818.             }
  819.  
  820.             if (!JFile::upload($file['tmp_name']JPath::clean($path '/' $location '/' $fileName)))
  821.             {
  822.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_UPLOAD_ERROR')'error');
  823.  
  824.                 return false;
  825.             }
  826.  
  827.             $url JPath::clean($location '/' $fileName);
  828.  
  829.             return $url;
  830.         }
  831.     }
  832.  
  833.     /**
  834.      * Create new folder.
  835.      *
  836.      * @param   string  $name      The name of the new folder.
  837.      * @param   string  $location  Location for the new folder.
  838.      *
  839.      * @return   boolean  True if override folder is created successfully, false otherwise
  840.      *
  841.      * @since   3.2
  842.      */
  843.     public function createFolder($name$location)
  844.     {
  845.         jimport('joomla.filesystem.folder');
  846.  
  847.         if ($template $this->getTemplate())
  848.         {
  849.             $app    JFactory::getApplication();
  850.             $client JApplicationHelper::getClientInfo($template->client_id);
  851.             $path   JPath::clean($client->path '/templates/' $template->element '/');
  852.  
  853.             if (file_exists(JPath::clean($path '/' $location '/' $name '/')))
  854.             {
  855.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_EXISTS')'error');
  856.  
  857.                 return false;
  858.             }
  859.  
  860.             if (!JFolder::create(JPath::clean($path '/' $location '/' $name)))
  861.             {
  862.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_CREATE_ERROR')'error');
  863.  
  864.                 return false;
  865.             }
  866.  
  867.             return true;
  868.         }
  869.     }
  870.  
  871.     /**
  872.      * Delete a folder.
  873.      *
  874.      * @param   string  $location  The name and location of the folder.
  875.      *
  876.      * @return  boolean  True if override folder is deleted successfully, false otherwise
  877.      *
  878.      * @since   3.2
  879.      */
  880.     public function deleteFolder($location)
  881.     {
  882.         jimport('joomla.filesystem.folder');
  883.  
  884.         if ($template $this->getTemplate())
  885.         {
  886.             $app    JFactory::getApplication();
  887.             $client JApplicationHelper::getClientInfo($template->client_id);
  888.             $path   JPath::clean($client->path '/templates/' $template->element '/' $location);
  889.  
  890.             if (!file_exists($path))
  891.             {
  892.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FOLDER_NOT_EXISTS')'error');
  893.  
  894.                 return false;
  895.             }
  896.  
  897.             $return JFolder::delete($path);
  898.  
  899.             if (!$return)
  900.             {
  901.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_DELETE_ERROR')'error');
  902.  
  903.                 return false;
  904.             }
  905.  
  906.             return true;
  907.         }
  908.     }
  909.  
  910.     /**
  911.      * Rename a file.
  912.      *
  913.      * @param   string  $file  The name and location of the old file
  914.      * @param   string  $name  The new name of the file.
  915.      *
  916.      * @return  string  Encoded string containing the new file location.
  917.      *
  918.      * @since   3.2
  919.      */
  920.     public function renameFile($file$name)
  921.     {
  922.         if ($template $this->getTemplate())
  923.         {
  924.             $app          JFactory::getApplication();
  925.             $client       JApplicationHelper::getClientInfo($template->client_id);
  926.             $path         JPath::clean($client->path '/templates/' $template->element '/');
  927.             $fileName     base64_decode($file);
  928.             $explodeArray explode('.'$fileName);
  929.             $type         end($explodeArray);
  930.             $explodeArray explode('/'$fileName);
  931.             $newName      str_replace(end($explodeArray)$name '.' $type$fileName);
  932.  
  933.             if (file_exists($path $newName))
  934.             {
  935.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS')'error');
  936.  
  937.                 return false;
  938.             }
  939.  
  940.             if (!rename($path $fileName$path $newName))
  941.             {
  942.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_RENAME_ERROR')'error');
  943.  
  944.                 return false;
  945.             }
  946.  
  947.             return base64_encode($newName);
  948.         }
  949.     }
  950.  
  951.     /**
  952.      * Get an image address, height and width.
  953.      *
  954.      * @return  array an associative array containing image address, height and width.
  955.      *
  956.      * @since   3.2
  957.      */
  958.     public function getImage()
  959.     {
  960.         if ($template $this->getTemplate())
  961.         {
  962.             $app      JFactory::getApplication();
  963.             $client   JApplicationHelper::getClientInfo($template->client_id);
  964.             $fileName base64_decode($app->input->get('file'));
  965.             $path     JPath::clean($client->path '/templates/' $template->element '/');
  966.  
  967.             if (stristr($client->path'administrator'== false)
  968.             {
  969.                 $folder '/templates/';
  970.             }
  971.             else
  972.             {
  973.                 $folder '/administrator/templates/';
  974.             }
  975.  
  976.             $uri JUri::root(true$folder $template->element;
  977.  
  978.             if (file_exists(JPath::clean($path $fileName)))
  979.             {
  980.                 $JImage new JImage(JPath::clean($path $fileName));
  981.                 $image['address']     $uri $fileName;
  982.                 $image['path']        $fileName;
  983.                 $image['height']     $JImage->getHeight();
  984.                 $image['width']      $JImage->getWidth();
  985.             }
  986.  
  987.             else
  988.             {
  989.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_IMAGE_FILE_NOT_FOUND')'error');
  990.  
  991.                 return false;
  992.             }
  993.  
  994.             return $image;
  995.         }
  996.     }
  997.  
  998.     /**
  999.      * Crop an image.
  1000.      *
  1001.      * @param   string  $file  The name and location of the file
  1002.      * @param   string  $w     width.
  1003.      * @param   string  $h     height.
  1004.      * @param   string  $x     x-coordinate.
  1005.      * @param   string  $y     y-coordinate.
  1006.      *
  1007.      * @return  boolean     true if image cropped successfully, false otherwise.
  1008.      *
  1009.      * @since   3.2
  1010.      */
  1011.     public function cropImage($file$w$h$x$y)
  1012.     {
  1013.         if ($template $this->getTemplate())
  1014.         {
  1015.             $app      JFactory::getApplication();
  1016.             $client   JApplicationHelper::getClientInfo($template->client_id);
  1017.             $relPath  base64_decode($file);
  1018.             $path     JPath::clean($client->path '/templates/' $template->element '/' $relPath);
  1019.             $JImage   new JImage($path);
  1020.  
  1021.             try
  1022.             {
  1023.                 $image $JImage->crop($w$h$x$ytrue);
  1024.                 $image->toFile($path);
  1025.  
  1026.                 return true;
  1027.             }
  1028.             catch (Exception $e)
  1029.             {
  1030.                 $app->enqueueMessage($e->getMessage()'error');
  1031.             }
  1032.         }
  1033.     }
  1034.  
  1035.     /**
  1036.      * Resize an image.
  1037.      *
  1038.      * @param   string  $file    The name and location of the file
  1039.      * @param   string  $width   The new width of the image.
  1040.      * @param   string  $height  The new height of the image.
  1041.      *
  1042.      * @return   boolean  true if image resize successful, false otherwise.
  1043.      *
  1044.      * @since   3.2
  1045.      */
  1046.     public function resizeImage($file$width$height)
  1047.     {
  1048.         if ($template $this->getTemplate())
  1049.         {
  1050.             $app     JFactory::getApplication();
  1051.             $client  JApplicationHelper::getClientInfo($template->client_id);
  1052.             $relPath base64_decode($file);
  1053.             $path    JPath::clean($client->path '/templates/' $template->element '/' $relPath);
  1054.  
  1055.             $JImage new JImage($path);
  1056.  
  1057.             try
  1058.             {
  1059.                 $image $JImage->resize($width$heighttrue1);
  1060.                 $image->toFile($path);
  1061.  
  1062.                 return true;
  1063.             }
  1064.             catch (Exception $e)
  1065.             {
  1066.                 $app->enqueueMessage($e->getMessage()'error');
  1067.             }
  1068.         }
  1069.     }
  1070.  
  1071.     /**
  1072.      * Template preview.
  1073.      *
  1074.      * @return  object  object containing the id of the template.
  1075.      *
  1076.      * @since   3.2
  1077.      */
  1078.     public function getPreview()
  1079.     {
  1080.         $app JFactory::getApplication();
  1081.         $db $this->getDbo();
  1082.         $query $db->getQuery(true);
  1083.  
  1084.         $query->select('id, client_id');
  1085.         $query->from('#__template_styles');
  1086.         $query->where($db->quoteName('template'' = ' $db->quote($this->template->name));
  1087.  
  1088.         $db->setQuery($query);
  1089.  
  1090.         try
  1091.         {
  1092.             $result $db->loadObject();
  1093.         }
  1094.         catch (RuntimeException $e)
  1095.         {
  1096.             $app->enqueueMessage($e->getMessage()'warning');
  1097.         }
  1098.  
  1099.         if (empty($result))
  1100.         {
  1101.             $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND')'warning');
  1102.         }
  1103.         else
  1104.         {
  1105.             return $result;
  1106.         }
  1107.     }
  1108.  
  1109.     /**
  1110.      * Rename a file.
  1111.      *
  1112.      * @return  mixed  array on success, false on failure
  1113.      *
  1114.      * @since   3.2
  1115.      */
  1116.     public function getFont()
  1117.     {
  1118.         if ($template $this->getTemplate())
  1119.         {
  1120.             $app          JFactory::getApplication();
  1121.             $client       JApplicationHelper::getClientInfo($template->client_id);
  1122.             $relPath      base64_decode($app->input->get('file'));
  1123.             $explodeArray explode('/'$relPath);
  1124.             $fileName     end($explodeArray);
  1125.             $path         JPath::clean($client->path '/templates/' $template->element '/' $relPath);
  1126.  
  1127.             if (stristr($client->path'administrator'== false)
  1128.             {
  1129.                 $folder '/templates/';
  1130.             }
  1131.             else
  1132.             {
  1133.                 $folder '/administrator/templates/';
  1134.             }
  1135.  
  1136.             $uri JUri::root(true$folder $template->element;
  1137.  
  1138.             if (file_exists(JPath::clean($path)))
  1139.             {
  1140.                 $font['address'$uri $relPath;
  1141.  
  1142.                 $font['rel_path'$relPath;
  1143.  
  1144.                 $font['name'$fileName;
  1145.             }
  1146.  
  1147.             else
  1148.             {
  1149.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_FONT_FILE_NOT_FOUND')'error');
  1150.  
  1151.                 return false;
  1152.             }
  1153.  
  1154.             return $font;
  1155.         }
  1156.     }
  1157.  
  1158.     /**
  1159.      * Check the admin template.
  1160.      *
  1161.      * @return  object  object containing the id of the template.
  1162.      *
  1163.      * @since   3.2
  1164.      */
  1165.     public function getHathor()
  1166.     {
  1167.         $app JFactory::getApplication();
  1168.         $db $this->getDbo();
  1169.         $query $db->getQuery(true);
  1170.  
  1171.         $query->select('home');
  1172.         $query->from('#__template_styles');
  1173.         $query->where($db->quoteName('template'' = ' $db->quote('hathor'));
  1174.         $db->setQuery($query);
  1175.  
  1176.         try
  1177.         {
  1178.             $result $db->loadObject();
  1179.         }
  1180.         catch (RuntimeException $e)
  1181.         {
  1182.             $app->enqueueMessage($e->getMessage()'error');
  1183.         }
  1184.  
  1185.         return $result;
  1186.     }
  1187.  
  1188.     /**
  1189.      * Copy a file.
  1190.      *
  1191.      * @param   string  $newName    The name of the copied file
  1192.      * @param   string  $location   The final location where the file is to be copied
  1193.      * @param   string  $file        The name and location of the file
  1194.      *
  1195.      * @return   boolean  true if image resize successful, false otherwise.
  1196.      *
  1197.      * @since   3.2
  1198.      */
  1199.     public function copyFile($newName$location$file)
  1200.     {
  1201.         if ($template $this->getTemplate())
  1202.         {
  1203.             $app          JFactory::getApplication();
  1204.             $client       JApplicationHelper::getClientInfo($template->client_id);
  1205.             $relPath      base64_decode($file);
  1206.             $explodeArray explode('.'$relPath);
  1207.             $ext          end($explodeArray);
  1208.             $path         JPath::clean($client->path '/templates/' $template->element '/');
  1209.             $newPath      JPath::clean($path '/' $location '/' $newName '.' $ext);
  1210.  
  1211.             if (file_exists($newPath))
  1212.             {
  1213.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS')'error');
  1214.  
  1215.                 return false;
  1216.             }
  1217.  
  1218.             if (JFile::copy($path $relPath$newPath))
  1219.             {
  1220.                 $app->enqueueMessage(JText::sprintf('COM_TEMPLATES_FILE_COPY_SUCCESS'$newName '.' $ext));
  1221.  
  1222.                 return true;
  1223.             }
  1224.             else
  1225.             {
  1226.                 return false;
  1227.             }
  1228.         }
  1229.     }
  1230.  
  1231.     /**
  1232.      * Get the compressed files.
  1233.      *
  1234.      * @return   array if file exists, false otherwise
  1235.      *
  1236.      * @since   3.2
  1237.      */
  1238.     public function getArchive()
  1239.     {
  1240.         if ($template $this->getTemplate())
  1241.         {
  1242.             $app     JFactory::getApplication();
  1243.             $client  JApplicationHelper::getClientInfo($template->client_id);
  1244.             $relPath base64_decode($app->input->get('file'));
  1245.             $path    JPath::clean($client->path '/templates/' $template->element '/' $relPath);
  1246.  
  1247.             if (file_exists(JPath::clean($path)))
  1248.             {
  1249.                 $files array();
  1250.                 $zip new ZipArchive;
  1251.  
  1252.                 if ($zip->open($path=== true)
  1253.                 {
  1254.                     for ($i 0$i $zip->numFiles$i++)
  1255.                     {
  1256.                         $entry $zip->getNameIndex($i);
  1257.                         $files[$entry;
  1258.                     }
  1259.                 }
  1260.                 else
  1261.                 {
  1262.                     $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_OPEN_FAIL')'error');
  1263.  
  1264.                     return false;
  1265.                 }
  1266.             }
  1267.             else
  1268.             {
  1269.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_FONT_FILE_NOT_FOUND')'error');
  1270.  
  1271.                 return false;
  1272.             }
  1273.  
  1274.             return $files;
  1275.         }
  1276.     }
  1277.  
  1278.     /**
  1279.      * Extract contents of a archive file.
  1280.      *
  1281.      * @param   string  $file  The name and location of the file
  1282.      *
  1283.      * @return  boolean  true if image extraction is successful, false otherwise.
  1284.      *
  1285.      * @since   3.2
  1286.      */
  1287.     public function extractArchive($file)
  1288.     {
  1289.         if ($template $this->getTemplate())
  1290.         {
  1291.             $app          JFactory::getApplication();
  1292.             $client       JApplicationHelper::getClientInfo($template->client_id);
  1293.             $relPath      base64_decode($file);
  1294.             $explodeArray explode('/'$relPath);
  1295.             $fileName     end($explodeArray);
  1296.             $folderPath   stristr($relPath$fileNametrue);
  1297.             $path         JPath::clean($client->path '/templates/' $template->element '/' $folderPath '/');
  1298.  
  1299.             if (file_exists(JPath::clean($path '/' $fileName)))
  1300.             {
  1301.                 $zip new ZipArchive;
  1302.  
  1303.                 if ($zip->open(JPath::clean($path '/' $fileName)) === true)
  1304.                 {
  1305.                     for ($i 0$i $zip->numFiles$i++)
  1306.                     {
  1307.                         $entry $zip->getNameIndex($i);
  1308.  
  1309.                         if (file_exists(JPath::clean($path '/' $entry)))
  1310.                         {
  1311.                             $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_EXISTS')'error');
  1312.  
  1313.                             return false;
  1314.                         }
  1315.                     }
  1316.  
  1317.                     $zip->extractTo($path);
  1318.  
  1319.                     return true;
  1320.                 }
  1321.                 else
  1322.                 {
  1323.                     $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_OPEN_FAIL')'error');
  1324.  
  1325.                     return false;
  1326.                 }
  1327.             }
  1328.             else
  1329.             {
  1330.                 $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_ARCHIVE_NOT_FOUND')'error');
  1331.  
  1332.                 return false;
  1333.             }
  1334.         }
  1335.     }
  1336. }

Documentation generated on Tue, 19 Nov 2013 15:15:27 +0100 by phpDocumentor 1.4.3