2014-04-08 12:32:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-12-01 07:21:47 +00:00
|
|
|
* @package Joomla.Administrator
|
|
|
|
* @subpackage Weblinks
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2017-02-13 16:20:35 +00:00
|
|
|
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
|
2014-04-08 12:32:59 +00:00
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
2015-02-18 01:20:59 +00:00
|
|
|
use Joomla\Utilities\ArrayHelper;
|
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
/**
|
2014-12-04 19:43:05 +00:00
|
|
|
* Weblinks class.
|
|
|
|
*
|
|
|
|
* @since 1.5
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
class WeblinksControllerWeblink extends JControllerForm
|
|
|
|
{
|
|
|
|
/**
|
2015-02-18 01:20:59 +00:00
|
|
|
* The URL view item variable.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @since 1.6
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
protected $view_item = 'form';
|
|
|
|
|
|
|
|
/**
|
2015-02-18 01:20:59 +00:00
|
|
|
* The URL view list variable.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @since 1.6
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
protected $view_list = 'categories';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The URL edit variable.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @since 3.2
|
|
|
|
*/
|
|
|
|
protected $urlVar = 'a.id';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to add a new record.
|
|
|
|
*
|
|
|
|
* @return boolean True if the article can be added, false if not.
|
2015-02-18 01:20:59 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
if (!parent::add())
|
|
|
|
{
|
|
|
|
// Redirect to the return page.
|
|
|
|
$this->setRedirect($this->getReturnPage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method override to check if you can add a new record.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @param array $data An array of input data.
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @return boolean
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function allowAdd($data = array())
|
|
|
|
{
|
2016-06-25 21:23:16 +00:00
|
|
|
$categoryId = ArrayHelper::getValue($data, 'catid', $this->input->getInt('id'), 'int');
|
2015-02-18 01:20:59 +00:00
|
|
|
$allow = null;
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
if ($categoryId)
|
|
|
|
{
|
|
|
|
// If the category has been passed in the URL check it.
|
2015-02-18 01:20:59 +00:00
|
|
|
$allow = JFactory::getUser()->authorise('core.create', $this->option . '.category.' . $categoryId);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:20:59 +00:00
|
|
|
if ($allow !== null)
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
|
|
|
return $allow;
|
|
|
|
}
|
2015-02-18 01:20:59 +00:00
|
|
|
|
|
|
|
// In the absense of better information, revert to the component permissions.
|
|
|
|
return parent::allowAdd($data);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to check if you can add a new record.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @param array $data An array of input data.
|
|
|
|
* @param string $key The name of the key for the primary key.
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function allowEdit($data = array(), $key = 'id')
|
|
|
|
{
|
2014-12-04 19:43:05 +00:00
|
|
|
$recordId = (int) isset($data[$key]) ? $data[$key] : 0;
|
2014-04-08 12:32:59 +00:00
|
|
|
$categoryId = 0;
|
|
|
|
|
|
|
|
if ($recordId)
|
|
|
|
{
|
|
|
|
$categoryId = (int) $this->getModel()->getItem($recordId)->catid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($categoryId)
|
|
|
|
{
|
|
|
|
// The category has been set. Check the category permissions.
|
2014-12-04 19:43:05 +00:00
|
|
|
return JFactory::getUser()->authorise('core.edit', $this->option . '.category.' . $categoryId);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
2015-02-18 01:20:59 +00:00
|
|
|
|
|
|
|
// Since there is no asset tracking, revert to the component permissions.
|
|
|
|
return parent::allowEdit($data, $key);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to cancel an edit.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @param string $key The name of the primary key of the URL variable.
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @return boolean True if access level checks pass, false otherwise.
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
public function cancel($key = 'w_id')
|
|
|
|
{
|
2015-02-18 01:20:59 +00:00
|
|
|
$return = parent::cancel($key);
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Redirect to the return page.
|
|
|
|
$this->setRedirect($this->getReturnPage());
|
2015-02-18 01:20:59 +00:00
|
|
|
|
|
|
|
return $return;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to edit an existing record.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @param string $key The name of the primary key of the URL variable.
|
|
|
|
* @param string $urlVar The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @return boolean True if access level check and checkout passes, false otherwise.
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
public function edit($key = null, $urlVar = 'w_id')
|
|
|
|
{
|
2015-02-18 01:20:59 +00:00
|
|
|
return parent::edit($key, $urlVar);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get a model object, loading it if required.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @param string $name The model name. Optional.
|
|
|
|
* @param string $prefix The class prefix. Optional.
|
|
|
|
* @param array $config Configuration array for model. Optional.
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
|
|
|
* @return object The model.
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.5
|
|
|
|
*/
|
|
|
|
public function getModel($name = 'form', $prefix = '', $config = array('ignore_request' => true))
|
|
|
|
{
|
2015-02-18 01:20:59 +00:00
|
|
|
return parent::getModel($name, $prefix, $config);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the URL arguments to append to an item redirect.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @param integer $recordId The primary key id for the item.
|
|
|
|
* @param string $urlVar The name of the URL variable for the id.
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @return string The arguments to append to the redirect URL.
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function getRedirectToItemAppend($recordId = null, $urlVar = null)
|
|
|
|
{
|
|
|
|
$append = parent::getRedirectToItemAppend($recordId, $urlVar);
|
2016-06-25 21:23:16 +00:00
|
|
|
$itemId = $this->input->getInt('Itemid');
|
|
|
|
$return = $this->getReturnPage();
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
if ($itemId)
|
|
|
|
{
|
2014-12-04 19:43:05 +00:00
|
|
|
$append .= '&Itemid=' . $itemId;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($return)
|
|
|
|
{
|
2014-12-04 19:43:05 +00:00
|
|
|
$append .= '&return=' . base64_encode($return);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $append;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-18 01:20:59 +00:00
|
|
|
* Get the return URL if a "return" variable has been passed in the request
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @return string The return URL.
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function getReturnPage()
|
|
|
|
{
|
|
|
|
$return = $this->input->get('return', null, 'base64');
|
|
|
|
|
|
|
|
if (empty($return) || !JUri::isInternal(base64_decode($return)))
|
|
|
|
{
|
|
|
|
return JUri::base();
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:20:59 +00:00
|
|
|
return base64_decode($return);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to save a record.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @param string $key The name of the primary key of the URL variable.
|
|
|
|
* @param string $urlVar The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @return boolean True if successful, false otherwise.
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
public function save($key = null, $urlVar = 'w_id')
|
|
|
|
{
|
2016-07-04 21:32:27 +00:00
|
|
|
// Get the application
|
|
|
|
$app = JFactory::getApplication();
|
|
|
|
|
|
|
|
// Get the data from POST
|
|
|
|
$data = $this->input->post->get('jform', array(), 'array');
|
|
|
|
|
|
|
|
// Save the data in the session.
|
|
|
|
$app->setUserState('com_weblinks.edit.weblink.data', $data);
|
2014-04-08 12:32:59 +00:00
|
|
|
$result = parent::save($key, $urlVar);
|
|
|
|
|
|
|
|
// If ok, redirect to the return page.
|
|
|
|
if ($result)
|
|
|
|
{
|
2016-07-04 21:32:27 +00:00
|
|
|
// Flush the data from the session
|
|
|
|
$app->setUserState('com_weblinks.edit.weblink.data', null);
|
2014-04-08 12:32:59 +00:00
|
|
|
$this->setRedirect($this->getReturnPage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to a weblink
|
|
|
|
*
|
|
|
|
* @return void
|
2015-02-18 00:35:48 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
// Get the ID from the request
|
|
|
|
$id = $this->input->getInt('id');
|
|
|
|
|
|
|
|
// Get the model, requiring published items
|
2015-02-18 01:20:59 +00:00
|
|
|
$modelLink = $this->getModel('Weblink', '', array('ignore_request' => true));
|
2014-04-08 12:32:59 +00:00
|
|
|
$modelLink->setState('filter.published', 1);
|
|
|
|
|
|
|
|
// Get the item
|
2015-02-18 01:20:59 +00:00
|
|
|
$link = $modelLink->getItem($id);
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Make sure the item was found.
|
|
|
|
if (empty($link))
|
|
|
|
{
|
|
|
|
return JError::raiseWarning(404, JText::_('COM_WEBLINKS_ERROR_WEBLINK_NOT_FOUND'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether item access level allows access.
|
2016-06-25 21:23:16 +00:00
|
|
|
$groups = JFactory::getUser()->getAuthorisedViewLevels();
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
if (!in_array($link->access, $groups))
|
|
|
|
{
|
|
|
|
return JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether category access level allows access.
|
|
|
|
$modelCat = $this->getModel('Category', 'WeblinksModel', array('ignore_request' => true));
|
|
|
|
$modelCat->setState('filter.published', 1);
|
|
|
|
|
|
|
|
// Get the category
|
|
|
|
$category = $modelCat->getCategory($link->catid);
|
|
|
|
|
|
|
|
// Make sure the category was found.
|
|
|
|
if (empty($category))
|
|
|
|
{
|
|
|
|
return JError::raiseWarning(404, JText::_('COM_WEBLINKS_ERROR_WEBLINK_NOT_FOUND'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether item access level allows access.
|
|
|
|
if (!in_array($category->access, $groups))
|
|
|
|
{
|
|
|
|
return JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the URL
|
2014-12-04 19:43:05 +00:00
|
|
|
// @todo: Probably should check for a valid http link
|
2014-04-08 12:32:59 +00:00
|
|
|
if ($link->url)
|
|
|
|
{
|
|
|
|
$modelLink->hit($id);
|
2016-10-15 13:44:28 +00:00
|
|
|
JFactory::getApplication()->redirect($link->url, 301);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
2015-02-18 01:20:59 +00:00
|
|
|
|
|
|
|
return JError::raiseWarning(404, JText::_('COM_WEBLINKS_ERROR_WEBLINK_URL_INVALID'));
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
}
|