protected function onBeforeRead($mode, $id) { $user = JFactory::getUser(); $authorized = false; if ($mode == 'row') { // Create if ($id) { $category = K2Categories::getInstance($id); $authorized = $category->canEdit; } else { $authorized = $user->authorise('k2.category.create', 'com_k2'); } } else { $authorized = $user->authorise('k2.category.create', 'com_k2') || $user->authorise('k2.category.edit', 'com_k2') || $user->authorise('k2.category.edit.own', 'com_k2') || $user->authorise('k2.category.edit.state', 'com_k2') || $user->authorise('k2.category.delete', 'com_k2'); } return $authorized; }
public function upload() { // Check for token JSession::checkToken() or K2Response::throwError(JText::_('JINVALID_TOKEN')); // Get user $user = JFactory::getUser(); // Get input $type = $this->input->get('type', '', 'cmd'); $itemId = $this->input->get('itemId', 0, 'int'); $replace = $this->input->get('temp', '', 'cmd'); $file = $this->input->files->get('file'); $path = $this->input->get('path', '', 'string'); $path = str_replace(JURI::root(true) . '/', '', $path); $categoryId = null; // Permissions check if ($itemId) { if ($type == 'item') { $item = K2Items::getInstance($itemId); $authorised = $item->canEdit; $categoryId = $item->catid; } else { if ($type == 'category') { $authorised = K2Categories::getInstance($itemId)->canEdit; } else { if ($type == 'user') { $authorised = $user->authorise('core.edit', 'com_users') || $user->id == $itemId; } } } } else { $authorised = $user->authorise('k2.' . $type . '.create', 'com_k2'); } if (!$authorised) { K2Response::throwError(JText::_('K2_YOU_ARE_NOT_AUTHORIZED_TO_PERFORM_THIS_OPERATION'), 403); } // Generate image using helper depending on type $image = K2HelperImages::add($type, $file, $path, $replace, $categoryId); // Response echo json_encode($image); return $this; }
public function render() { require_once JPATH_ADMINISTRATOR . '/components/com_k2/helpers/extrafields.php'; $input = JFactory::getApplication()->input; $scope = $input->get('scope', '', 'cmd'); $resourceId = $input->get('resourceId', 0, 'int'); $filterId = $input->get('filterId', 0, 'raw'); if ($scope == 'item') { require_once JPATH_ADMINISTRATOR . '/components/com_k2/resources/items.php'; $item = K2Items::getInstance($resourceId); $values = $item->extra_fields; $fields = K2HelperExtraFields::getItemExtraFieldsGroups((int) $filterId, $values); } else { if ($scope == 'category') { require_once JPATH_ADMINISTRATOR . '/components/com_k2/resources/categories.php'; $category = K2Categories::getInstance($resourceId); $values = $category->extra_fields; $fields = K2HelperExtraFields::getCategoryExtraFieldsGroups($values); } else { if ($scope == 'user') { require_once JPATH_ADMINISTRATOR . '/components/com_k2/resources/users.php'; $user = K2Users::getInstance($resourceId); $values = $user->extra_fields; $fields = K2HelperExtraFields::getUserExtraFieldsGroups($values); } else { if ($scope == 'tag') { require_once JPATH_ADMINISTRATOR . '/components/com_k2/resources/tags.php'; $tag = K2Tags::getInstance($resourceId); $values = $tag->extra_fields; $fields = K2HelperExtraFields::getTagExtraFieldsGroups($values); } } } } echo json_encode($fields); return $this; }
/** * Build the route for the K2 component using the advanced SEF options in legacy mode */ private function advacedBuildLegacy(&$query) { // Initialize $segments = array(); // Detect the active menu item if (empty($query['Itemid'])) { $menuItem = $this->menu->getActive(); } else { $menuItem = $this->menu->getItem($query['Itemid']); } // Load data from the current menu item $mView = empty($menuItem->query['view']) ? null : $menuItem->query['view']; $mTask = empty($menuItem->query['task']) ? null : $menuItem->query['task']; $mId = empty($menuItem->query['id']) ? null : $menuItem->query['id']; $mTag = empty($menuItem->query['tag']) ? null : $menuItem->query['tag']; if (isset($query['layout'])) { unset($query['layout']); } if ($mView == @$query['view'] && $mTask == @$query['task'] && $mId == @intval($query['id']) && @intval($query['id']) > 0) { unset($query['view']); unset($query['task']); unset($query['id']); } if ($mView == @$query['view'] && $mTask == @$query['task'] && $mTag == @$query['tag'] && isset($query['tag'])) { unset($query['view']); unset($query['task']); unset($query['tag']); } if (isset($query['view'])) { $segments[] = $query['view']; unset($query['view']); } if (isset($query['task'])) { $segments[] = $query['task']; unset($query['task']); } if (isset($query['id'])) { $segments[] = $query['id']; unset($query['id']); } if (isset($query['cid'])) { $segments[] = $query['cid']; unset($query['cid']); } if (isset($query['tag'])) { $segments[] = $query['tag']; unset($query['tag']); } if (isset($query['year'])) { $segments[] = $query['year']; unset($query['year']); } if (isset($query['month'])) { $segments[] = $query['month']; unset($query['month']); } if (isset($query['day'])) { $segments[] = $query['day']; unset($query['day']); } if (isset($query['task'])) { $segments[] = $query['task']; unset($query['task']); } // Item view if (isset($segments[0]) && $segments[0] == 'item' && @$segments[1] != 'add') { // Enabled category prefix for items if ($this->params->get('k2SefLabelItem')) { // Tasks available for an item $itemTasks = array('edit', 'download'); // If it's a task pick the next key if (in_array($segments[1], $itemTasks)) { $ItemId = $segments[2]; } else { $ItemId = $segments[1]; } // Replace the item with the category slug if ($this->params->get('k2SefLabelItem') == '1') { $item = K2Items::getInstance((int) $ItemId); $category = K2Categories::getInstance($item->catid); $segments[0] = $category->id . '-' . $category->alias; } else { $segments[0] = $this->params->get('k2SefLabelItemCustomPrefix'); } } else { unset($segments[0]); } // Handle item id and alias if ($this->params->get('k2SefInsertItemId')) { if ($this->params->get('k2SefUseItemTitleAlias')) { if ($this->params->get('k2SefItemIdTitleAliasSep') == 'slash') { $segments[1] = JString::str_ireplace(':', '/', $segments[1]); } else { if ($this->params->get('k2SefItemIdTitleAliasSep') == 'dash') { $segments[1] = JString::str_ireplace(':', '-', $segments[1]); } } } else { $temp = @explode(':', $segments[1]); $segments[1] = $temp[0]; } } else { if (isset($segments[1]) && $segments[1] != 'download') { // Try to split the slud $temp = @explode(':', $segments[1]); // If the slug contained an item id do not use it if (count($temp) > 1) { $segments[1] = $temp[1]; } } } } elseif (isset($segments[0]) && $segments[0] == 'itemlist') { if (isset($segments[1])) { switch ($segments[1]) { case 'category': $segments[0] = $this->params->get('k2SefLabelCat', 'content'); unset($segments[1]); // Handle category id and alias if ($this->params->get('k2SefInsertCatId')) { if ($this->params->get('k2SefUseCatTitleAlias')) { if ($this->params->get('k2SefCatIdTitleAliasSep') == 'slash') { $segments[2] = @JString::str_ireplace(':', '/', $segments[2]); } else { if ($this->params->get('k2SefCatIdTitleAliasSep') == 'dash') { $segments[2] = @JString::str_ireplace(':', '-', $segments[2]); } } } else { $temp = @explode(':', $segments[2]); $segments[2] = (int) $temp[0]; } } else { // Try to split the slud $temp = @explode(':', $segments[2]); // If the slug contained an item id do not use it if (count($temp) > 1) { @($segments[2] = end($temp)); } } break; case 'tag': $segments[0] = $this->params->get('k2SefLabelTag', 'tag'); unset($segments[1]); if (strpos($segments[2], ':')) { $temp = @explode(':', $segments[2]); $segments[2] = $temp[1]; } break; case 'user': $segments[0] = $this->params->get('k2SefLabelUser', 'author'); unset($segments[1]); break; case 'date': $segments[0] = $this->params->get('k2SefLabelDate', 'date'); unset($segments[1]); break; case 'search': $segments[0] = $this->params->get('k2SefLabelSearch', 'search'); unset($segments[1]); break; default: $segments[0] = 'itemlist'; break; } } } // Return reordered segments array return array_values($segments); }
public static function update($type, $image, $table) { // File system $filesystem = K2FileSystem::getInstance(); // ImageProcessor $processor = K2ImageProcessor::getInstance(); // Params $params = JComponentHelper::getParams('com_k2'); // Save path $savepath = self::$paths[$type]; // Current image $currentImageId = md5('Image' . $table->id); // Temporary (new) image $tempImageId = $image['temp']; // Image has been removed if ($image['remove']) { if ($type == 'item') { // Src image $key = $savepath . '/src/' . $currentImageId . '.jpg'; if ($filesystem->has($key)) { $filesystem->delete($key); } // Resized images $sizes = (array) $params->get('imageSizes'); foreach ($sizes as $size) { $key = $savepath . '/cache/' . $currentImageId . '_' . $size->id . '.jpg'; if ($filesystem->has($key)) { $filesystem->delete($key); } } } else { // File to delete $key = $savepath . '/' . $currentImageId . '.jpg'; // Delete the file if ($filesystem->has($key)) { $filesystem->delete($key); } } } else { if ($tempImageId) { // Item image if ($type == 'item') { if (JFile::exists(JPATH_SITE . '/tmp/' . $tempImageId . '/src/' . $tempImageId . '.jpg')) { // Src image $srcImageBuffer = file_get_contents(JPATH_SITE . '/tmp/' . $tempImageId . '/src/' . $tempImageId . '.jpg'); K2FileSystem::writeImageFile($savepath . '/src/' . $currentImageId . '.jpg', $srcImageBuffer); // Get sizes from global settings $sizes = (array) $params->get('imageSizes'); // Check for category overrides $overrides = array(); if ($table->catid) { $category = K2Categories::getInstance($table->catid); $categorySizes = (array) $category->params->get('imageSizes'); foreach ($categorySizes as $categorySize) { if ((int) $categorySize->width > 0) { $overrides[$categorySize->id] = (int) $categorySize->width; } } } // Resized images foreach ($sizes as $size) { // Filename $filename = $currentImageId . '_' . $size->id . '.jpg'; // Width. Check for overrides $width = array_key_exists($size->id, $overrides) && (int) $size->width != (int) $overrides[$size->id] ? $overrides[$size->id] : $size->width; // Resize $imageResource = $processor->load($srcImageBuffer); $imageResource->resize($imageResource->getSize()->widen($width)); $buffer = $imageResource->get('jpeg', array('quality' => $size->quality)); K2FileSystem::writeImageFile($savepath . '/cache/' . $filename, $buffer); } JFolder::delete(JPATH_SITE . '/tmp/' . $tempImageId); } } else { // Copy the temporary image to the filesystem if (JFile::exists(JPATH_SITE . '/tmp/' . $tempImageId . '.jpg')) { $buffer = file_get_contents(JPATH_SITE . '/tmp/' . $tempImageId . '.jpg'); K2FileSystem::writeImageFile($savepath . '/' . $currentImageId . '.jpg', $buffer); JFile::delete(JPATH_SITE . '/tmp/' . $tempImageId . '.jpg'); } } } } }
public static function getBreadcrumbs($params) { $application = JFactory::getApplication(); $option = $application->input->get('option', '', 'cmd'); $view = $application->input->get('view', '', 'cmd'); $task = $application->input->get('task', '', 'cmd'); $id = $application->input->get('id', 0, 'int'); $menu = $application->getMenu(); $active = $menu->getActive(); $breadcrumbs = new stdClass(); $breadcrumbs->title = ''; $breadcrumbs->path = array(); $breadcrumbs->home = $params->get('home', JText::_('K2_HOME')); $breadcrumbs->separator = $params->get('seperator', '»'); $pathway = $application->getPathway(); $pathwayItems = $pathway->getPathWay(); foreach ($pathwayItems as $pathwayItem) { $pathwayItem->title = $pathwayItem->name; } if ($option == 'com_k2' && $view == 'item' || $view == 'itemlist' && $task == 'category') { switch ($view) { case 'item': // Get item $item = K2Items::getInstance($id); // Check for menu link $matchItem = $active && isset($active->query['view']) && $active->query['view'] == 'item' && isset($active->query['id']) && $active->query['id'] == $id; $matchCategory = $active && isset($active->query['view']) && $active->query['view'] == 'itemlist' && isset($active->query['task']) && $active->query['task'] == 'category' && isset($active->query['id']) && $active->query['id'] == $item->catid; // Handle depending on matches if ($matchItem) { $breadcrumbs->title = end($pathwayItems)->title; foreach ($pathwayItems as $pathwayItem) { $breadcrumbs->path[] = $pathwayItem; } array_pop($breadcrumbs->path); } else { if ($matchCategory) { $breadcrumbs->title = $item->title; foreach ($pathwayItems as $pathwayItem) { $breadcrumbs->path[] = $pathwayItem; } array_pop($breadcrumbs->path); } else { $breadcrumbs->title = $item->title; $categories = explode('/', $item->category->path); foreach ($categories as $alias) { $breadcrumbs->path[] = K2Categories::getInstance($alias); } } } break; case 'itemlist': // Check for menu link $matchCategory = $active && isset($active->query['view']) && $active->query['view'] == 'itemlist' && isset($active->query['task']) && $active->query['task'] == 'category' && isset($active->query['id']) && $active->query['id'] == $id; // Handle depending on matches if ($matchCategory) { $breadcrumbs->title = end($pathwayItems)->title; foreach ($pathwayItems as $pathwayItem) { $breadcrumbs->path[] = $pathwayItem; } } else { $category = K2Categories::getInstance($id); $breadcrumbs->title = $category->title; $categories = explode('/', $category->path); foreach ($categories as $alias) { $breadcrumbs->path[] = K2Categories::getInstance($alias); } } array_pop($breadcrumbs->path); break; } } else { $breadcrumbs->title = count($pathwayItems) ? end($pathwayItems)->title : ''; foreach ($pathwayItems as $pathwayItem) { $breadcrumbs->path[] = $pathwayItem; } array_pop($breadcrumbs->path); } return $breadcrumbs; }
public function getInheritFrom() { $inheritFrom = JText::_('K2_NONE'); if ($this->inheritance && $this->inheritance != $this->id) { if ($this->inheritance == 1) { $inheritFrom = JText::_('K2_GLOBAL_PARAMETERS'); } else { $inheritFrom = K2Categories::getInstance($this->inheritance)->title; } } return $inheritFrom; }
public function getCategory() { $category = null; if ($this->id) { $category = K2Categories::getInstance($this->catid); } return $category; }
public static function getItemExtraFieldsGroups($categoryId, $values) { $groups = array(); $values = json_decode($values); $category = K2Categories::getInstance($categoryId); $categoryParams = $category->getEffectiveParams(); $selectedGroups = $categoryParams->get('catExtraFieldGroups', array()); if (!is_array($selectedGroups)) { $selectedGroups = array($selectedGroups); } $itemGroups = self::getGroups('item'); foreach ($selectedGroups as $groupId) { $group = $itemGroups[$groupId]; $groups[] = self::renderGroup($group, $values); } return $groups; }
protected function getCategoryItems($count = false) { // Get application $application = JFactory::getApplication(); // Get input $id = $application->input->get('id', 0, 'int'); $categories = $this->params->get('categories'); // Get model $model = K2Model::getInstance('Items'); $model->setState('site', true); // Single category if ($id) { // Get category $this->category = K2Categories::getInstance($id); // Check access $this->category->checkSiteAccess(); // Merge menu params with category params $effectiveParams = $this->category->getEffectiveParams(); $this->params->merge($effectiveParams); // Set model state $model->setState('category', $id); if (!$this->params->get('catCatalogMode')) { $model->setState('recursive', 1); } } else { if ($categories) { $model->setState('category.filter', $categories); } } // Determine offset and limit based on document type if ($this->type == 'html' || $this->type == 'raw') { $this->limit = (int) ($this->params->get('num_leading_items') + $this->params->get('num_primary_items') + $this->params->get('num_secondary_items') + $this->params->get('num_links')); } else { $this->limit = $this->params->get('feedLimit', 10, 'int'); } // @TODO Apply menu settings. Since they will be common all tasks we need to wait //$model->setState('sorting', 'ordering'); // Get items $model->setState('limit', $this->limit); $model->setState('limitstart', $this->offset); $this->items = $model->getRows(); // Word limit if ($this->params->get('catItemIntroTextWordLimit')) { foreach ($this->items as $item) { $item->introtext = K2HelperUtilities::wordLimit($item->introtext, $this->params->get('catItemIntroTextWordLimit')); } } // Count items if ($count) { $this->total = $model->countRows(); } }
public function getCopyData($id) { // Get params $params = JComponentHelper::getParams('com_k2'); // Get source category $source = K2Categories::getInstance($id); // Get source category properties as data array. This array will be the input to the model. $data = get_object_vars($source); // It's a new category so reset some properties $data['id'] = ''; $data['title'] = JText::_('K2_COPY_OF') . ' ' . $data['title']; $data['alias'] = ''; $data['extra_fields'] = json_decode($data['extra_fields']); $data['metadata'] = $data['metadata']->toString(); $data['plugins'] = $data['plugins']->toString(); $data['params'] = $data['params']->toString(); // Handle image if (isset($data['image']) && isset($data['image']->id)) { // If filesystem is not local then path is the URL $filesystem = $params->get('filesystem'); $path = $filesystem == 'Local' || !$filesystem ? 'media/k2/categories/' . $data['image']->id . '.jpg' : $data['image']->url; $image = K2HelperImages::add('category', null, $path); $data['image'] = array('id' => '', 'temp' => $image->temp, 'path' => '', 'remove' => 0, 'caption' => $data['image']->caption, 'credits' => $data['image']->credits); } else { unset($data['image']); } // Return the input data return $data; }