You've already forked joomla_test
first commit
This commit is contained in:
87
components/com_k2/helpers/permissions.j16.php
Normal file
87
components/com_k2/helpers/permissions.j16.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* @version $Id: permissions.j16.php 1812 2013-01-14 18:45:06Z lefteris.kavadas $
|
||||
* @package K2
|
||||
* @author JoomlaWorks http://www.joomlaworks.net
|
||||
* @copyright Copyright (c) 2006 - 2013 JoomlaWorks Ltd. All rights reserved.
|
||||
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
|
||||
*/
|
||||
|
||||
// no direct access
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
jimport('joomla.html.parameter');
|
||||
|
||||
class K2HelperPermissions
|
||||
{
|
||||
|
||||
public static function checkPermissions()
|
||||
{
|
||||
// Set some variables
|
||||
$mainframe = JFactory::getApplication();
|
||||
$user = JFactory::getUser();
|
||||
$option = JRequest::getCmd('option');
|
||||
$view = JRequest::getCmd('view');
|
||||
$task = JRequest::getCmd('task');
|
||||
$id = JRequest::getInt('cid');
|
||||
|
||||
//Generic manage check
|
||||
if (!$user->authorise('core.manage', $option))
|
||||
{
|
||||
JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
|
||||
$mainframe->redirect('index.php');
|
||||
}
|
||||
|
||||
// Determine action for rest checks
|
||||
$action = false;
|
||||
if ($mainframe->isAdmin() && $view != '' && $view != 'info')
|
||||
{
|
||||
switch($task)
|
||||
{
|
||||
case '' :
|
||||
case 'save' :
|
||||
case 'apply' :
|
||||
if (!$id)
|
||||
{
|
||||
$action = 'core.create';
|
||||
}
|
||||
else
|
||||
{
|
||||
$action = 'core.edit';
|
||||
}
|
||||
break;
|
||||
case 'trash' :
|
||||
case 'remove' :
|
||||
$action = 'core.delete';
|
||||
break;
|
||||
case 'publish' :
|
||||
case 'unpublish' :
|
||||
$action = 'core.edit.state';
|
||||
}
|
||||
|
||||
// Edit or Edit own action
|
||||
if ($action == 'core.edit' && $view == 'item' && $id)
|
||||
{
|
||||
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
|
||||
$item = JTable::getInstance('K2Item', 'Table');
|
||||
$item->load($id);
|
||||
if ($item->created_by == $user->id)
|
||||
{
|
||||
$action = 'core.edit.own';
|
||||
}
|
||||
}
|
||||
|
||||
// Check the determined action
|
||||
if ($action)
|
||||
{
|
||||
if (!$user->authorise($action, $option))
|
||||
{
|
||||
JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
|
||||
$mainframe->redirect('index.php?option=com_k2');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
344
components/com_k2/helpers/permissions.php
Normal file
344
components/com_k2/helpers/permissions.php
Normal file
@ -0,0 +1,344 @@
|
||||
<?php
|
||||
/**
|
||||
* @version $Id: permissions.php 1962 2013-04-29 12:29:34Z lefteris.kavadas $
|
||||
* @package K2
|
||||
* @author JoomlaWorks http://www.joomlaworks.net
|
||||
* @copyright Copyright (c) 2006 - 2013 JoomlaWorks Ltd. All rights reserved.
|
||||
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
|
||||
*/
|
||||
|
||||
// no direct access
|
||||
defined('_JEXEC') or die ;
|
||||
|
||||
jimport('joomla.html.parameter');
|
||||
|
||||
class K2HelperPermissions
|
||||
{
|
||||
|
||||
public static function setPermissions()
|
||||
{
|
||||
$params = K2HelperUtilities::getParams('com_k2');
|
||||
$user = JFactory::getUser();
|
||||
if ($user->guest)
|
||||
{
|
||||
return;
|
||||
}
|
||||
$K2User = K2HelperPermissions::getK2User($user->id);
|
||||
if (!is_object($K2User))
|
||||
{
|
||||
return;
|
||||
}
|
||||
$K2UserGroup = K2HelperPermissions::getK2UserGroup($K2User->group);
|
||||
if (is_null($K2UserGroup))
|
||||
{
|
||||
return;
|
||||
}
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
$permissions = K2_JVERSION == '15' ? new JParameter($K2UserGroup->permissions) : new JRegistry($K2UserGroup->permissions);
|
||||
$K2Permissions->permissions = $permissions;
|
||||
if ($permissions->get('categories') == 'none')
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if ($permissions->get('categories') == 'all')
|
||||
{
|
||||
if ($permissions->get('add') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'add.category.all';
|
||||
$K2Permissions->actions[] = 'tag';
|
||||
$K2Permissions->actions[] = 'extraFields';
|
||||
}
|
||||
if ($permissions->get('editOwn') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'editOwn.item.'.$user->id;
|
||||
$K2Permissions->actions[] = 'tag';
|
||||
$K2Permissions->actions[] = 'extraFields';
|
||||
}
|
||||
if ($permissions->get('editAll') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'editAll.category.all';
|
||||
$K2Permissions->actions[] = 'tag';
|
||||
$K2Permissions->actions[] = 'extraFields';
|
||||
}
|
||||
if ($permissions->get('publish') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'publish.category.all';
|
||||
}
|
||||
if ($permissions->get('comment'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'comment.category.all';
|
||||
}
|
||||
if ($permissions->get('editPublished'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'editPublished.category.all';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$selectedCategories = $permissions->get('categories', NULL);
|
||||
if (is_string($selectedCategories))
|
||||
{
|
||||
$searchIDs[] = $selectedCategories;
|
||||
}
|
||||
else
|
||||
{
|
||||
$searchIDs = $selectedCategories;
|
||||
}
|
||||
if ($permissions->get('inheritance'))
|
||||
{
|
||||
$model = K2Model::getInstance('Itemlist', 'K2Model');
|
||||
$categories = $model->getCategoryTree($searchIDs);
|
||||
}
|
||||
else
|
||||
{
|
||||
$categories = $searchIDs;
|
||||
}
|
||||
if (is_array($categories) && count($categories))
|
||||
{
|
||||
foreach ($categories as $category)
|
||||
{
|
||||
if ($permissions->get('add') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'add.category.'.$category;
|
||||
$K2Permissions->actions[] = 'tag';
|
||||
$K2Permissions->actions[] = 'extraFields';
|
||||
}
|
||||
if ($permissions->get('editOwn') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'editOwn.item.'.$user->id.'.'.$category;
|
||||
$K2Permissions->actions[] = 'tag';
|
||||
$K2Permissions->actions[] = 'extraFields';
|
||||
}
|
||||
if ($permissions->get('editAll') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'editAll.category.'.$category;
|
||||
$K2Permissions->actions[] = 'tag';
|
||||
$K2Permissions->actions[] = 'extraFields';
|
||||
}
|
||||
if ($permissions->get('publish') && $permissions->get('frontEdit') && $params->get('frontendEditing'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'publish.category.'.$category;
|
||||
}
|
||||
if ($permissions->get('comment'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'comment.category.'.$category;
|
||||
}
|
||||
if ($permissions->get('editPublished'))
|
||||
{
|
||||
$K2Permissions->actions[] = 'editPublished.category.'.$category;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public static function checkPermissions()
|
||||
{
|
||||
$view = JRequest::getCmd('view');
|
||||
if ($view != 'item')
|
||||
{
|
||||
return;
|
||||
}
|
||||
$task = JRequest::getCmd('task');
|
||||
$user = JFactory::getUser();
|
||||
if ($user->guest && ($task == 'add' || $task == 'edit'))
|
||||
{
|
||||
$mainframe = JFactory::getApplication();
|
||||
$uri = JURI::getInstance();
|
||||
$return = base64_encode($uri->toString());
|
||||
if (K2_JVERSION == '15')
|
||||
{
|
||||
$mainframe->redirect('index.php?option=com_user&view=login&return='.$return.'&tmpl=component', JText::_('K2_YOU_NEED_TO_LOGIN_FIRST'), 'notice');
|
||||
}
|
||||
else
|
||||
{
|
||||
$mainframe->redirect('index.php?option=com_users&view=login&return='.$return.'&tmpl=component', JText::_('K2_YOU_NEED_TO_LOGIN_FIRST'), 'notice');
|
||||
}
|
||||
}
|
||||
|
||||
switch ($task)
|
||||
{
|
||||
|
||||
case 'add' :
|
||||
if (!K2HelperPermissions::canAddItem())
|
||||
JError::raiseError(403, JText::_('K2_ALERTNOTAUTH'));
|
||||
break;
|
||||
|
||||
case 'edit' :
|
||||
case 'deleteAttachment' :
|
||||
case 'checkin' :
|
||||
$cid = JRequest::getInt('cid');
|
||||
if (!$cid)
|
||||
JError::raiseError(403, JText::_('K2_ALERTNOTAUTH'));
|
||||
|
||||
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
|
||||
$item = JTable::getInstance('K2Item', 'Table');
|
||||
$item->load($cid);
|
||||
|
||||
if (!K2HelperPermissions::canEditItem($item->created_by, $item->catid))
|
||||
JError::raiseError(403, JText::_('K2_ALERTNOTAUTH'));
|
||||
break;
|
||||
|
||||
case 'save' :
|
||||
$cid = JRequest::getInt('id');
|
||||
if ($cid)
|
||||
{
|
||||
|
||||
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
|
||||
$item = JTable::getInstance('K2Item', 'Table');
|
||||
$item->load($cid);
|
||||
|
||||
if (!K2HelperPermissions::canEditItem($item->created_by, $item->catid))
|
||||
JError::raiseError(403, JText::_('K2_ALERTNOTAUTH'));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!K2HelperPermissions::canAddItem())
|
||||
JError::raiseError(403, JText::_('K2_ALERTNOTAUTH'));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'tag' :
|
||||
if (!K2HelperPermissions::canAddTag())
|
||||
JError::raiseError(403, JText::_('K2_ALERTNOTAUTH'));
|
||||
break;
|
||||
|
||||
case 'extraFields' :
|
||||
if (!K2HelperPermissions::canRenderExtraFields())
|
||||
JError::raiseError(403, JText::_('K2_ALERTNOTAUTH'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getK2User($userID)
|
||||
{
|
||||
|
||||
$db = JFactory::getDBO();
|
||||
$query = "SELECT * FROM #__k2_users WHERE userID = ".(int)$userID;
|
||||
$db->setQuery($query);
|
||||
$row = $db->loadObject();
|
||||
return $row;
|
||||
}
|
||||
|
||||
public static function getK2UserGroup($id)
|
||||
{
|
||||
|
||||
$db = JFactory::getDBO();
|
||||
$query = "SELECT * FROM #__k2_user_groups WHERE id = ".(int)$id;
|
||||
$db->setQuery($query);
|
||||
$row = $db->loadObject();
|
||||
return $row;
|
||||
}
|
||||
|
||||
public static function canAddItem($category = false)
|
||||
{
|
||||
|
||||
$user = JFactory::getUser();
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
if (in_array('add.category.all', $K2Permissions->actions))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ($category)
|
||||
{
|
||||
return in_array('add.category.'.$category, $K2Permissions->actions);
|
||||
}
|
||||
$db = JFactory::getDBO();
|
||||
$query = "SELECT id FROM #__k2_categories WHERE published=1 AND trash=0";
|
||||
if (K2_JVERSION != '15')
|
||||
{
|
||||
$query .= " AND access IN(".implode(',', $user->getAuthorisedViewLevels()).")";
|
||||
}
|
||||
else
|
||||
{
|
||||
$aid = (int)$user->get('aid');
|
||||
$query .= " AND access<={$aid}";
|
||||
}
|
||||
$db->setQuery($query);
|
||||
$categories = K2_JVERSION == '30' ? $db->loadColumn() : $db->loadResultArray();
|
||||
foreach ($categories as $category)
|
||||
{
|
||||
if (in_array('add.category.'.$category, $K2Permissions->actions))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function canAddToAll()
|
||||
{
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
return in_array('add.category.all', $K2Permissions->actions);
|
||||
}
|
||||
|
||||
public static function canEditItem($itemOwner, $itemCategory)
|
||||
{
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
if (in_array('editAll.category.all', $K2Permissions->actions) || in_array('editOwn.item.'.$itemOwner, $K2Permissions->actions) || in_array('editOwn.item.'.$itemOwner.'.'.$itemCategory, $K2Permissions->actions) || in_array('editAll.category.'.$itemCategory, $K2Permissions->actions))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function canPublishItem($itemCategory)
|
||||
{
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
if (in_array('publish.category.all', $K2Permissions->actions) || in_array('publish.category.'.$itemCategory, $K2Permissions->actions))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function canAddTag()
|
||||
{
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
return in_array('tag', $K2Permissions->actions);
|
||||
}
|
||||
|
||||
public static function canRenderExtraFields()
|
||||
{
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
return in_array('extraFields', $K2Permissions->actions);
|
||||
}
|
||||
|
||||
public static function canAddComment($itemCategory)
|
||||
{
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
return in_array('comment.category.all', $K2Permissions->actions) || in_array('comment.category.'.$itemCategory, $K2Permissions->actions);
|
||||
}
|
||||
|
||||
public static function canEditPublished($itemCategory)
|
||||
{
|
||||
$K2Permissions = K2Permissions::getInstance();
|
||||
return in_array('editPublished.category.all', $K2Permissions->actions) || in_array('editPublished.category.'.$itemCategory, $K2Permissions->actions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class K2Permissions
|
||||
{
|
||||
var $actions = array();
|
||||
var $permissions = null;
|
||||
public static function getInstance()
|
||||
{
|
||||
static $instance;
|
||||
if (!is_object($instance))
|
||||
{
|
||||
$instance = new K2Permissions();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
|
||||
}
|
285
components/com_k2/helpers/route.php
Normal file
285
components/com_k2/helpers/route.php
Normal file
@ -0,0 +1,285 @@
|
||||
<?php
|
||||
/**
|
||||
* @version $Id: route.php 1948 2013-03-11 16:47:00Z lefteris.kavadas $
|
||||
* @package K2
|
||||
* @author JoomlaWorks http://www.joomlaworks.net
|
||||
* @copyright Copyright (c) 2006 - 2013 JoomlaWorks Ltd. All rights reserved.
|
||||
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
|
||||
*/
|
||||
|
||||
// no direct access
|
||||
defined('_JEXEC') or die ;
|
||||
|
||||
jimport('joomla.application.component.helper');
|
||||
|
||||
class K2HelperRoute
|
||||
{
|
||||
|
||||
private static $anyK2Link = null;
|
||||
private static $multipleCategoriesMapping = array();
|
||||
|
||||
public static function getItemRoute($id, $catid = 0)
|
||||
{
|
||||
$needles = array(
|
||||
'item' => (int)$id,
|
||||
'category' => (int)$catid,
|
||||
);
|
||||
$link = 'index.php?option=com_k2&view=item&id='.$id;
|
||||
if ($item = K2HelperRoute::_findItem($needles))
|
||||
{
|
||||
$link .= '&Itemid='.$item->id;
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
public static function getCategoryRoute($catid)
|
||||
{
|
||||
$needles = array('category' => (int)$catid);
|
||||
$link = 'index.php?option=com_k2&view=itemlist&task=category&id='.$catid;
|
||||
if ($item = K2HelperRoute::_findItem($needles))
|
||||
{
|
||||
$link .= '&Itemid='.$item->id;
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
public static function getUserRoute($userID)
|
||||
{
|
||||
|
||||
if (K2_CB)
|
||||
{
|
||||
global $_CB_framework;
|
||||
return $_CB_framework->userProfileUrl((int)$userID);
|
||||
}
|
||||
|
||||
$needles = array('user' => (int)$userID);
|
||||
$user = JFactory::getUser($userID);
|
||||
if (K2_JVERSION != '15' && JFactory::getConfig()->get('unicodeslugs') == 1)
|
||||
{
|
||||
$alias = JApplication::stringURLSafe($user->name);
|
||||
}
|
||||
else if (JPluginHelper::isEnabled('system', 'unicodeslug') || JPluginHelper::isEnabled('system', 'jw_unicodeSlugsExtended'))
|
||||
{
|
||||
$alias = JFilterOutput::stringURLSafe($user->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
mb_internal_encoding("UTF-8");
|
||||
mb_regex_encoding("UTF-8");
|
||||
$alias = trim(mb_strtolower($user->name));
|
||||
$alias = str_replace('-', ' ', $alias);
|
||||
$alias = mb_ereg_replace('[[:space:]]+', ' ', $alias);
|
||||
$alias = trim(str_replace(' ', '', $alias));
|
||||
$alias = str_replace('.', '', $alias);
|
||||
|
||||
$stripthese = ',|~|!|@|%|^|(|)|<|>|:|;|{|}|[|]|&|`|„|‹|’|‘|“|â€<C3A2>|•|›|«|´|»|°|«|»|…';
|
||||
$strips = explode('|', $stripthese);
|
||||
foreach ($strips as $strip)
|
||||
{
|
||||
$alias = str_replace($strip, '', $alias);
|
||||
}
|
||||
$params = K2HelperUtilities::getParams('com_k2');
|
||||
$SEFReplacements = array();
|
||||
$items = explode(',', $params->get('SEFReplacements', NULL));
|
||||
foreach ($items as $item)
|
||||
{
|
||||
if (!empty($item))
|
||||
{
|
||||
@list($src, $dst) = explode('|', trim($item));
|
||||
$SEFReplacements[trim($src)] = trim($dst);
|
||||
}
|
||||
}
|
||||
foreach ($SEFReplacements as $key => $value)
|
||||
{
|
||||
$alias = str_replace($key, $value, $alias);
|
||||
}
|
||||
$alias = trim($alias, '-.');
|
||||
if (trim(str_replace('-', '', $alias)) == '')
|
||||
{
|
||||
$datenow = JFactory::getDate();
|
||||
$alias = K2_JVERSION == '15' ? $datenow->toFormat("%Y-%m-%d-%H-%M-%S") : $datenow->format("Y-m-d-H-i-s");
|
||||
}
|
||||
}
|
||||
$link = 'index.php?option=com_k2&view=itemlist&task=user&id='.$userID.':'.$alias;
|
||||
if ($item = K2HelperRoute::_findItem($needles))
|
||||
{
|
||||
$link .= '&Itemid='.$item->id;
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
public static function getTagRoute($tag)
|
||||
{
|
||||
$needles = array('tag' => $tag);
|
||||
$link = 'index.php?option=com_k2&view=itemlist&task=tag&tag='.urlencode($tag);
|
||||
if ($item = K2HelperRoute::_findItem($needles))
|
||||
{
|
||||
$link .= '&Itemid='.$item->id;
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
public static function getDateRoute($year, $month, $day = null, $catid = null)
|
||||
{
|
||||
$needles = array('year' => $year);
|
||||
$link = 'index.php?option=com_k2&view=itemlist&task=date&year='.$year.'&month='.$month;
|
||||
if ($day)
|
||||
{
|
||||
$link .= '&day='.$day;
|
||||
}
|
||||
if ($catid)
|
||||
{
|
||||
$link .= '&catid='.$catid;
|
||||
}
|
||||
if ($item = K2HelperRoute::_findItem($needles))
|
||||
{
|
||||
$link .= '&Itemid='.$item->id;
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
public static function getSearchRoute()
|
||||
{
|
||||
$needles = array('search' => 'search');
|
||||
$link = 'index.php?option=com_k2&view=itemlist&task=search';
|
||||
if ($item = K2HelperRoute::_findItem($needles))
|
||||
{
|
||||
$link .= '&Itemid='.$item->id;
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
public static function _findItem($needles)
|
||||
{
|
||||
$component = JComponentHelper::getComponent('com_k2');
|
||||
$application = JFactory::getApplication();
|
||||
$menus = $application->getMenu('site', array());
|
||||
if (K2_JVERSION != '15')
|
||||
{
|
||||
$items = $menus->getItems('component_id', $component->id);
|
||||
}
|
||||
else
|
||||
{
|
||||
$items = $menus->getItems('componentid', $component->id);
|
||||
}
|
||||
$match = null;
|
||||
foreach ($needles as $needle => $id)
|
||||
{
|
||||
if (count($items))
|
||||
{
|
||||
foreach ($items as $item)
|
||||
{
|
||||
|
||||
// Detect multiple K2 categories link and set the generic K2 link ( if any )
|
||||
if (@$item->query['view'] == 'itemlist' && @$item->query['task'] == '')
|
||||
{
|
||||
|
||||
if (!isset(self::$multipleCategoriesMapping[$item->id]))
|
||||
{
|
||||
if (K2_JVERSION == '15')
|
||||
{
|
||||
$menuparams = explode("\n", $item->params);
|
||||
foreach ($menuparams as $param)
|
||||
{
|
||||
if (strpos($param, 'categories=') === 0)
|
||||
{
|
||||
$array = explode('categories=', $param);
|
||||
$item->K2Categories = explode('|', $array[1]);
|
||||
}
|
||||
}
|
||||
if (!isset($item->K2Categories))
|
||||
{
|
||||
$item->K2Categories = array();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$menuparams = json_decode($item->params);
|
||||
$item->K2Categories = isset($menuparams->categories) ? $menuparams->categories : array();
|
||||
}
|
||||
|
||||
self::$multipleCategoriesMapping[$item->id] = $item->K2Categories;
|
||||
|
||||
if (count($item->K2Categories) === 0)
|
||||
{
|
||||
|
||||
self::$anyK2Link = $item;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ($needle == 'user' || $needle == 'category')
|
||||
{
|
||||
if ((@$item->query['task'] == $needle) && (@$item->query['id'] == $id))
|
||||
{
|
||||
$match = $item;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
else if ($needle == 'tag')
|
||||
{
|
||||
if ((@$item->query['task'] == $needle) && (@$item->query['tag'] == $id))
|
||||
{
|
||||
$match = $item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((@$item->query['view'] == $needle) && (@$item->query['id'] == $id))
|
||||
{
|
||||
$match = $item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_null($match))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Second pass [START]
|
||||
// Only for multiple categories links. Triggered only if we do not have find any match (link to direct category)
|
||||
if (is_null($match) && $needle == 'category')
|
||||
{
|
||||
foreach ($items as $item)
|
||||
{
|
||||
if (@$item->query['view'] == 'itemlist' && @$item->query['task'] == '')
|
||||
{
|
||||
if (isset(self::$multipleCategoriesMapping[$item->id]) && is_array(self::$multipleCategoriesMapping[$item->id]))
|
||||
{
|
||||
foreach (self::$multipleCategoriesMapping[$item->id] as $catid)
|
||||
{
|
||||
if ((int)$catid == $id)
|
||||
{
|
||||
$match = $item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_null($match))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Second pass [END]
|
||||
}
|
||||
if (!is_null($match))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_null($match) && !is_null(self::$anyK2Link))
|
||||
{
|
||||
$match = self::$anyK2Link;
|
||||
}
|
||||
|
||||
return $match;
|
||||
}
|
||||
|
||||
}
|
364
components/com_k2/helpers/utilities.php
Normal file
364
components/com_k2/helpers/utilities.php
Normal file
@ -0,0 +1,364 @@
|
||||
<?php
|
||||
/**
|
||||
* @version $Id: utilities.php 1977 2013-05-15 11:39:46Z lefteris.kavadas $
|
||||
* @package K2
|
||||
* @author JoomlaWorks http://www.joomlaworks.net
|
||||
* @copyright Copyright (c) 2006 - 2013 JoomlaWorks Ltd. All rights reserved.
|
||||
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
|
||||
*/
|
||||
|
||||
// no direct access
|
||||
defined('_JEXEC') or die ;
|
||||
|
||||
class K2HelperUtilities
|
||||
{
|
||||
|
||||
// Get user avatar
|
||||
public static function getAvatar($userID, $email = NULL, $width = 50)
|
||||
{
|
||||
|
||||
jimport('joomla.filesystem.folder');
|
||||
jimport('joomla.application.component.model');
|
||||
$mainframe = JFactory::getApplication();
|
||||
$params = K2HelperUtilities::getParams('com_k2');
|
||||
|
||||
if (K2_CB && $userID != 'alias')
|
||||
{
|
||||
$cbUser = CBuser::getInstance((int)$userID);
|
||||
if (is_object($cbUser))
|
||||
{
|
||||
$avatar = $cbUser->getField('avatar', null, 'csv', 'none', 'profile');
|
||||
return $avatar;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for placeholder overrides
|
||||
if (JFile::exists(JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'images'.DS.'placeholder'.DS.'user.png'))
|
||||
{
|
||||
$avatarPath = 'templates/'.$mainframe->getTemplate().'/images/placeholder/user.png';
|
||||
}
|
||||
else
|
||||
{
|
||||
$avatarPath = 'components/com_k2/images/placeholder/user.png';
|
||||
}
|
||||
|
||||
// Continue with default K2 avatar determination
|
||||
if ($userID == 'alias')
|
||||
{
|
||||
$avatar = JURI::root(true).'/'.$avatarPath;
|
||||
}
|
||||
else if ($userID == 0)
|
||||
{
|
||||
if ($params->get('gravatar') && !is_null($email))
|
||||
{
|
||||
$avatar = '//www.gravatar.com/avatar/'.md5($email).'?s='.$width.'&default='.urlencode(JURI::root().$avatarPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
$avatar = JURI::root(true).'/'.$avatarPath;
|
||||
}
|
||||
}
|
||||
else if (is_numeric($userID) && $userID > 0)
|
||||
{
|
||||
K2Model::addIncludePath(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'models');
|
||||
$model = K2Model::getInstance('Item', 'K2Model');
|
||||
$profile = $model->getUserProfile($userID);
|
||||
$avatar = (is_null($profile)) ? '' : $profile->image;
|
||||
if (empty($avatar))
|
||||
{
|
||||
if ($params->get('gravatar') && !is_null($email))
|
||||
{
|
||||
$avatar = '//www.gravatar.com/avatar/'.md5($email).'?s='.$width.'&default='.urlencode(JURI::root().$avatarPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
$avatar = JURI::root(true).'/'.$avatarPath;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$avatar = JURI::root(true).'/media/k2/users/'.$avatar;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!$params->get('userImageDefault') && $avatar == JURI::root(true).'/'.$avatarPath)
|
||||
{
|
||||
$avatar = '';
|
||||
}
|
||||
|
||||
return $avatar;
|
||||
}
|
||||
|
||||
public static function getCategoryImage($image, $params)
|
||||
{
|
||||
|
||||
jimport('joomla.filesystem.file');
|
||||
$mainframe = JFactory::getApplication();
|
||||
$categoryImage = NULL;
|
||||
if (!empty($image))
|
||||
{
|
||||
$categoryImage = JURI::root(true).'/media/k2/categories/'.$image;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($params->get('catImageDefault'))
|
||||
{
|
||||
if (JFile::exists(JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'images'.DS.'placeholder'.DS.'category.png'))
|
||||
{
|
||||
$categoryImage = JURI::root(true).'/templates/'.$mainframe->getTemplate().'/images/placeholder/category.png';
|
||||
}
|
||||
else
|
||||
{
|
||||
$categoryImage = JURI::root(true).'/components/com_k2/images/placeholder/category.png';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return $categoryImage;
|
||||
}
|
||||
|
||||
// Word limit
|
||||
public static function wordLimit($str, $limit = 100, $end_char = '…')
|
||||
{
|
||||
if (JString::trim($str) == '')
|
||||
return $str;
|
||||
|
||||
// always strip tags for text
|
||||
$str = strip_tags($str);
|
||||
|
||||
$find = array("/\r|\n/u", "/\t/u", "/\s\s+/u");
|
||||
$replace = array(" ", " ", " ");
|
||||
$str = preg_replace($find, $replace, $str);
|
||||
|
||||
preg_match('/\s*(?:\S*\s*){'.(int)$limit.'}/u', $str, $matches);
|
||||
if (JString::strlen($matches[0]) == JString::strlen($str))
|
||||
$end_char = '';
|
||||
return JString::rtrim($matches[0]).$end_char;
|
||||
}
|
||||
|
||||
// Character limit
|
||||
public static function characterLimit($str, $limit = 150, $end_char = '...')
|
||||
{
|
||||
if (JString::trim($str) == '')
|
||||
return $str;
|
||||
|
||||
// always strip tags for text
|
||||
$str = strip_tags(JString::trim($str));
|
||||
|
||||
$find = array("/\r|\n/u", "/\t/u", "/\s\s+/u");
|
||||
$replace = array(" ", " ", " ");
|
||||
$str = preg_replace($find, $replace, $str);
|
||||
|
||||
if (JString::strlen($str) > $limit)
|
||||
{
|
||||
$str = JString::substr($str, 0, $limit);
|
||||
return JString::rtrim($str).$end_char;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $str;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Cleanup HTML entities
|
||||
public static function cleanHtml($text)
|
||||
{
|
||||
return htmlentities($text, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
// Gender
|
||||
public static function writtenBy($gender)
|
||||
{
|
||||
if (is_null($gender))
|
||||
return JText::_('K2_WRITTEN_BY');
|
||||
if ($gender == 'm')
|
||||
return JText::_('K2_WRITTEN_BY_MALE');
|
||||
if ($gender == 'f')
|
||||
return JText::_('K2_WRITTEN_BY_FEMALE');
|
||||
}
|
||||
|
||||
public static function setDefaultImage(&$item, $view, $params = NULL)
|
||||
{
|
||||
if ($view == 'item')
|
||||
{
|
||||
$image = 'image'.$item->params->get('itemImgSize');
|
||||
$item->image = $item->$image;
|
||||
|
||||
switch ($item->params->get('itemImgSize'))
|
||||
{
|
||||
|
||||
case 'XSmall' :
|
||||
$item->imageWidth = $item->params->get('itemImageXS');
|
||||
break;
|
||||
|
||||
case 'Small' :
|
||||
$item->imageWidth = $item->params->get('itemImageS');
|
||||
break;
|
||||
|
||||
case 'Medium' :
|
||||
$item->imageWidth = $item->params->get('itemImageM');
|
||||
break;
|
||||
|
||||
case 'Large' :
|
||||
$item->imageWidth = $item->params->get('itemImageL');
|
||||
break;
|
||||
|
||||
case 'XLarge' :
|
||||
$item->imageWidth = $item->params->get('itemImageXL');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($view == 'itemlist')
|
||||
{
|
||||
$image = 'image'.$params->get($item->itemGroup.'ImgSize');
|
||||
$item->image = isset($item->$image) ? $item->$image : '';
|
||||
|
||||
switch ($params->get($item->itemGroup.'ImgSize'))
|
||||
{
|
||||
|
||||
case 'XSmall' :
|
||||
$item->imageWidth = $item->params->get('itemImageXS');
|
||||
break;
|
||||
|
||||
case 'Small' :
|
||||
$item->imageWidth = $item->params->get('itemImageS');
|
||||
break;
|
||||
|
||||
case 'Medium' :
|
||||
$item->imageWidth = $item->params->get('itemImageM');
|
||||
break;
|
||||
|
||||
case 'Large' :
|
||||
$item->imageWidth = $item->params->get('itemImageL');
|
||||
break;
|
||||
|
||||
case 'XLarge' :
|
||||
$item->imageWidth = $item->params->get('itemImageXL');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($view == 'latest')
|
||||
{
|
||||
$image = 'image'.$params->get('latestItemImageSize');
|
||||
$item->image = $item->$image;
|
||||
|
||||
switch ($params->get('latestItemImageSize'))
|
||||
{
|
||||
|
||||
case 'XSmall' :
|
||||
$item->imageWidth = $item->params->get('itemImageXS');
|
||||
break;
|
||||
|
||||
case 'Small' :
|
||||
$item->imageWidth = $item->params->get('itemImageS');
|
||||
break;
|
||||
|
||||
case 'Medium' :
|
||||
$item->imageWidth = $item->params->get('itemImageM');
|
||||
break;
|
||||
|
||||
case 'Large' :
|
||||
$item->imageWidth = $item->params->get('itemImageL');
|
||||
break;
|
||||
|
||||
case 'XLarge' :
|
||||
$item->imageWidth = $item->params->get('itemImageXL');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($view == 'relatedByTag' && $params->get('itemRelatedImageSize'))
|
||||
{
|
||||
|
||||
$image = 'image'.$params->get('itemRelatedImageSize');
|
||||
$item->image = $item->$image;
|
||||
|
||||
switch ($params->get('itemRelatedImageSize'))
|
||||
{
|
||||
|
||||
case 'XSmall' :
|
||||
$item->imageWidth = $item->params->get('itemImageXS');
|
||||
break;
|
||||
|
||||
case 'Small' :
|
||||
$item->imageWidth = $item->params->get('itemImageS');
|
||||
break;
|
||||
|
||||
case 'Medium' :
|
||||
$item->imageWidth = $item->params->get('itemImageM');
|
||||
break;
|
||||
|
||||
case 'Large' :
|
||||
$item->imageWidth = $item->params->get('itemImageL');
|
||||
break;
|
||||
|
||||
case 'XLarge' :
|
||||
$item->imageWidth = $item->params->get('itemImageXL');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function getParams($option)
|
||||
{
|
||||
|
||||
if (K2_JVERSION != '15')
|
||||
{
|
||||
$application = JFactory::getApplication();
|
||||
if ($application->isSite())
|
||||
{
|
||||
$params = $application->getParams($option);
|
||||
}
|
||||
else
|
||||
{
|
||||
$params = JComponentHelper::getParams($option);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$params = JComponentHelper::getParams($option);
|
||||
}
|
||||
return $params;
|
||||
|
||||
}
|
||||
|
||||
public static function cleanTags($string, $allowed_tags)
|
||||
{
|
||||
$allowed_htmltags = array();
|
||||
foreach ($allowed_tags as $tag)
|
||||
{
|
||||
$allowed_htmltags[] .= "<".$tag.">";
|
||||
}
|
||||
$allowed_htmltags = implode("", $allowed_htmltags);
|
||||
$string = strip_tags($string, $allowed_htmltags);
|
||||
return $string;
|
||||
}
|
||||
|
||||
// Clean HTML Tag Attributes
|
||||
// e.g. cleanupAttributes($string,"img,hr,h1,h2,h3,h4","style,width,height,hspace,vspace,border,class,id");
|
||||
public static function cleanAttributes($string, $tag_array, $attr_array)
|
||||
{
|
||||
$attr = implode("|", $attr_array);
|
||||
foreach ($tag_array as $tag)
|
||||
{
|
||||
preg_match_all("#<($tag) .+?>#", $string, $matches, PREG_PATTERN_ORDER);
|
||||
foreach ($matches[0] as $match)
|
||||
{
|
||||
preg_match_all('/('.$attr.')=([\\"\\\']).+?([\\"\\\'])/', $match, $matchesAttr, PREG_PATTERN_ORDER);
|
||||
foreach ($matchesAttr[0] as $attrToClean)
|
||||
{
|
||||
$string = str_replace($attrToClean, '', $string);
|
||||
$string = preg_replace('| +|', ' ', $string);
|
||||
$string = str_replace(' >', '>', $string);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
} // End Class
|
Reference in New Issue
Block a user