AAAAhome/academiac/www/administrator/components/com_templates/models/source.php000064400000014621151372100120023400 0ustar00getUserState('com_templates.edit.source.id'); // Parse the template id out of the compound reference. $temp = explode(':', base64_decode($id)); $this->setState('extension.id', (int) array_shift($temp)); $fileName = array_shift($temp); $this->setState('filename', $fileName); // Save the syntax for later use $app->setUserState('editor.source.syntax', JFile::getExt($fileName)); // Load the parameters. $params = JComponentHelper::getParams('com_templates'); $this->setState('params', $params); } /** * Method to get the record form. * * @param array $data Data for the form. * @param boolean $loadData True if the form is to load its own data (default case), false if not. * @return JForm A JForm object on success, false on failure * @since 1.6 */ public function getForm($data = array(), $loadData = true) { // Initialise variables. $app = JFactory::getApplication(); // Codemirror or Editor None should be enabled $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select('COUNT(*)'); $query->from('#__extensions as a'); $query->where('(a.name ='.$db->quote('plg_editors_codemirror').' AND a.enabled = 1) OR (a.name ='.$db->quote('plg_editors_none').' AND a.enabled = 1)'); $db->setQuery($query); $state = $db->loadResult(); if ((int)$state < 1 ) { $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EDITOR_DISABLED'), 'warning'); } // Get the form. $form = $this->loadForm('com_templates.source', 'source', array('control' => 'jform', 'load_data' => $loadData)); if (empty($form)) { return false; } return $form; } /** * Method to get the data that should be injected in the form. * * @return mixed The data for the form. * @since 1.6 */ protected function loadFormData() { // Check the session for previously entered form data. $data = JFactory::getApplication()->getUserState('com_templates.edit.source.data', array()); if (empty($data)) { $data = $this->getSource(); } return $data; } /** * Method to get a single record. * * @return mixed Object on success, false on failure. * @since 1.6 */ public function &getSource() { $item = new stdClass; if (!$this->_template) { $this->getTemplate(); } if ($this->_template) { $fileName = $this->getState('filename'); $client = JApplicationHelper::getClientInfo($this->_template->client_id); $filePath = JPath::clean($client->path.'/templates/'.$this->_template->element.'/'.$fileName); if (file_exists($filePath)) { jimport('joomla.filesystem.file'); $item->extension_id = $this->getState('extension.id'); $item->filename = $this->getState('filename'); $item->source = JFile::read($filePath); } else { $this->setError(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND')); } } return $item; } /** * Method to get the template information. * * @return mixed Object if successful, false if not and internal error is set. * @since 1.6 */ public function &getTemplate() { // Initialise variables. $pk = $this->getState('extension.id'); $db = $this->getDbo(); $result = false; // Get the template information. $db->setQuery( 'SELECT extension_id, client_id, element' . ' FROM #__extensions' . ' WHERE extension_id = '.(int) $pk. ' AND type = '.$db->quote('template') ); $result = $db->loadObject(); if (empty($result)) { if ($error = $db->getErrorMsg()) { $this->setError($error); } else { $this->setError(JText::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND')); } $this->_template = false; } else { $this->_template = $result; } return $this->_template; } /** * Method to store the source file contents. * * @param array The souce data to save. * * @return boolean True on success, false otherwise and internal error set. * @since 1.6 */ public function save($data) { jimport('joomla.filesystem.file'); // Get the template. $template = $this->getTemplate(); if (empty($template)) { return false; } $dispatcher = JDispatcher::getInstance(); $fileName = $this->getState('filename'); $client = JApplicationHelper::getClientInfo($template->client_id); $filePath = JPath::clean($client->path.'/templates/'.$template->element.'/'.$fileName); // Include the extension plugins for the save events. JPluginHelper::importPlugin('extension'); // Set FTP credentials, if given. JClientHelper::setCredentialsFromRequest('ftp'); $ftp = JClientHelper::getCredentials('ftp'); // Try to make the template file writeable. if (!$ftp['enabled'] && JPath::isOwner($filePath) && !JPath::setPermissions($filePath, '0644')) { $this->setError(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_WRITABLE')); return false; } // Trigger the onExtensionBeforeSave event. $result = $dispatcher->trigger('onExtensionBeforeSave', array('com_templates.source', &$data, false)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } $return = JFile::write($filePath, $data['source']); // Try to make the template file unwriteable. if (!$ftp['enabled'] && JPath::isOwner($filePath) && !JPath::setPermissions($filePath, '0444')) { $this->setError(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_UNWRITABLE')); return false; } elseif (!$return) { $this->setError(JText::sprintf('COM_TEMPLATES_ERROR_FAILED_TO_SAVE_FILENAME', $fileName)); return false; } // Trigger the onExtensionAfterSave event. $dispatcher->trigger('onExtensionAfterSave', array('com_templates.source', &$table, false)); return true; } } home/academiac/www/administrator/components/com_templates/controllers/source.php000064400000015532151372120420024472 0ustar00registerTask('apply', 'save'); } /** * Method to check if you can add a new record. * * Extended classes can override this if necessary. * * @param array An array of input data. * @param string The name of the key for the primary key. * * @return boolean */ protected function allowEdit() { return JFactory::getUser()->authorise('core.edit', 'com_templates'); } /** * Method to check if you can save a new or existing record. * * Extended classes can override this if necessary. * * @param array An array of input data. * @param string The name of the key for the primary key. * * @return boolean */ protected function allowSave() { return $this->allowEdit(); } /** * Method to get a model object, loading it if required. * * @param string The model name. Optional. * @param string The class prefix. Optional. * @param array Configuration array for model. Optional (note, the empty array is atypical compared to other models). * * @return object The model. */ public function getModel($name = 'Source', $prefix = 'TemplatesModel', $config = array()) { $model = parent::getModel($name, $prefix, $config); return $model; } /** * This controller does not have a display method. Redirect back to the list view of the component. * * @param boolean If true, the view output will be cached * @param array An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}. * * @return JController This object to support chaining. * @since 1.5 */ public function display($cachable = false, $urlparams = false) { $this->setRedirect(JRoute::_('index.php?option=com_templates&view=templates', false)); } /** * Method to edit an existing record. * * @return void */ public function edit() { // Initialise variables. $app = JFactory::getApplication(); $model = $this->getModel(); $recordId = JRequest::getVar('id'); $context = 'com_templates.edit.source'; if (preg_match('#\.\.#', base64_decode($recordId))) { return JError::raiseError(500, JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND')); } // Access check. if (!$this->allowEdit()) { return JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED')); } // Check-out succeeded, push the new record id into the session. $app->setUserState($context.'.id', $recordId); $app->setUserState($context.'.data', null); $this->setRedirect('index.php?option=com_templates&view=source&layout=edit'); return true; } /** * Method to cancel an edit * * @return void */ public function cancel() { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); // Initialise variables. $app = JFactory::getApplication(); $model = $this->getModel(); $context = 'com_templates.edit.source'; $returnId = (int) $model->getState('extension.id'); // Clean the session data and redirect. $app->setUserState($context.'.id', null); $app->setUserState($context.'.data', null); $this->setRedirect(JRoute::_('index.php?option=com_templates&view=template&id='.$returnId, false)); } /** * Saves a template source file. */ public function save() { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); // Initialise variables. $app = JFactory::getApplication(); $data = JRequest::getVar('jform', array(), 'post', 'array'); $context = 'com_templates.edit.source'; $task = $this->getTask(); $model = $this->getModel(); // Access check. if (!$this->allowSave()) { return JError::raiseWarning(403, JText::_('JERROR_SAVE_NOT_PERMITTED')); } // Match the stored id's with the submitted. if (empty($data['extension_id']) || empty($data['filename'])) { return JError::raiseError(500, JText::_('COM_TEMPLATES_ERROR_SOURCE_ID_FILENAME_MISMATCH')); } elseif ($data['extension_id'] != $model->getState('extension.id')) { return JError::raiseError(500, JText::_('COM_TEMPLATES_ERROR_SOURCE_ID_FILENAME_MISMATCH')); } elseif ($data['filename'] != $model->getState('filename')) { return JError::raiseError(500, JText::_('COM_TEMPLATES_ERROR_SOURCE_ID_FILENAME_MISMATCH')); } // Validate the posted data. $form = $model->getForm(); if (!$form) { JError::raiseError(500, $model->getError()); return false; } $data = $model->validate($form, $data); // Check for validation errors. if ($data === false) { // Get the validation messages. $errors = $model->getErrors(); // Push up to three validation messages out to the user. for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) { if ($errors[$i] instanceof Exception) { $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); } else { $app->enqueueMessage($errors[$i], 'warning'); } } // Save the data in the session. $app->setUserState($context.'.data', $data); // Redirect back to the edit screen. $this->setRedirect(JRoute::_('index.php?option=com_templates&view=source&layout=edit', false)); return false; } // Attempt to save the data. if (!$model->save($data)) { // Save the data in the session. $app->setUserState($context.'.data', $data); // Redirect back to the edit screen. $this->setMessage(JText::sprintf('JERROR_SAVE_FAILED', $model->getError()), 'warning'); $this->setRedirect(JRoute::_('index.php?option=com_templates&view=source&layout=edit', false)); return false; } $this->setMessage(JText::_('COM_TEMPLATES_FILE_SAVE_SUCCESS')); // Redirect the user and adjust session state based on the chosen task. switch ($task) { case 'apply': // Reset the record data in the session. $app->setUserState($context.'.data', null); // Redirect back to the edit screen. $this->setRedirect(JRoute::_('index.php?option=com_templates&view=source&layout=edit', false)); break; default: // Clear the record id and data from the session. $app->setUserState($context.'.id', null); $app->setUserState($context.'.data', null); // Redirect to the list screen. $this->setRedirect(JRoute::_('index.php?option=com_templates&view=template&id='.$model->getState('extension.id'), false)); break; } } }