Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 public function getCopyData($id)
 {
     // Get params
     $params = JComponentHelper::getParams('com_k2');
     // Get source item
     $source = K2Items::getInstance($id);
     // Get source item properties as data array. This array will be the inout to the model.
     $data = get_object_vars($source);
     // It's a new item so reset some properties
     $data['id'] = '';
     $data['tmpId'] = uniqid();
     $data['title'] = JText::_('K2_COPY_OF') . ' ' . $data['title'];
     $data['alias'] = '';
     $data['tags'] = json_decode($data['_tags']);
     $data['media'] = json_decode($data['_media']);
     $data['galleries'] = json_decode($data['_galleries']);
     $data['extra_fields'] = json_decode($data['extra_fields']);
     $data['metadata'] = $data['metadata']->toString();
     $data['plugins'] = $data['plugins']->toString();
     $data['params'] = $data['params']->toString();
     unset($data['ordering']);
     unset($data['featured_ordering']);
     // Handle tags
     $tagNames = array();
     if (is_array($data['tags'])) {
         foreach ($data['tags'] as $tag) {
             $tagNames[] = $tag->name;
         }
     }
     $data['tags'] = implode(',', $tagNames);
     // Handle image
     if (isset($data['images']) && is_array($data['images']) && isset($data['images']['src'])) {
         // If filesystem is not local then path is the URL
         $filesystem = $params->get('filesystem');
         $path = $filesystem == 'Local' || !$filesystem ? 'media/k2/items/src/' . $data['images']['src']->id . '.jpg' : $data['images']['src']->url;
         $image = K2HelperImages::add('item', null, $path);
         $data['image'] = array('id' => '', 'temp' => $image->temp, 'path' => '', 'remove' => 0, 'caption' => $data['image']->caption, 'credits' => $data['image']->credits);
     } else {
         unset($data['image']);
     }
     // Handle media
     $media = array();
     if (is_array($data['media'])) {
         foreach ($data['media'] as $key => $entry) {
             if ($entry->upload) {
                 $filesystem = K2FileSystem::getInstance();
                 if ($filesystem->has('media/k2/media/' . $id . '/' . $entry->upload)) {
                     $buffer = $filesystem->read('media/k2/media/' . $id . '/' . $entry->upload);
                     JFile::write(JPATH_SITE . '/tmp/' . $entry->upload, $buffer);
                 }
             }
             $newEntry = array();
             $newEntry['url'] = $entry->url;
             $newEntry['provider'] = $entry->provider;
             $newEntry['id'] = $entry->id;
             $newEntry['embed'] = $entry->embed;
             $newEntry['caption'] = $entry->caption;
             $newEntry['credits'] = $entry->credits;
             $newEntry['upload'] = $entry->upload;
             $newEntry['remove'] = 0;
             $media[$key] = $newEntry;
         }
     }
     $data['media'] = $media;
     // Handle galleries
     $galleries = array();
     if (is_array($data['galleries'])) {
         foreach ($data['galleries'] as $key => $entry) {
             if ($entry->upload) {
                 $filesystem = K2FileSystem::getInstance();
                 if ($filesystem->has('media/k2/galleries/' . $id . '/' . $entry->upload)) {
                     JFolder::create(JPATH_SITE . '/tmp/' . $entry->upload);
                     $files = $filesystem->listKeys('media/k2/galleries/' . $id . '/' . $entry->upload);
                     foreach ($files['keys'] as $key) {
                         if ($filesystem->has($key)) {
                             $buffer = $filesystem->read($key);
                             JFile::write(JPATH_SITE . '/tmp/' . $entry->upload . '/' . basename($key), $buffer);
                         }
                     }
                 }
             }
             $newEntry = array();
             $newEntry['url'] = $entry->url;
             $newEntry['upload'] = $entry->upload;
             $newEntry['remove'] = 0;
             $galleries[$key] = $newEntry;
         }
     }
     $data['galleries'] = $galleries;
     // Handle attachments
     $filesystem = K2FileSystem::getInstance();
     $attachmentsModel = K2Model::getInstance('Attachments');
     $attachments = array();
     $data['attachments'] = $source->getAttachments();
     foreach ($data['attachments'] as $key => $attachment) {
         // Prepare the data array
         $newEntry = array();
         $newEntry['id'] = '';
         $newEntry['name'] = $attachment->name;
         $newEntry['title'] = $attachment->title;
         if ($attachment->file) {
             $tmpId = uniqid();
             if ($filesystem->has('media/k2/attachments/' . $id . '/' . $attachment->file)) {
                 $buffer = $filesystem->read('media/k2/attachments/' . $id . '/' . $attachment->file);
                 JFile::write(JPATH_SITE . '/tmp/' . $tmpId . '_' . $attachment->file, $buffer);
             }
             $newEntry['file'] = $tmpId . '_' . $attachment->file;
         }
         $newEntry['path'] = $attachment->path;
         $newEntry['remove'] = 0;
         $attachments[$key] = $newEntry;
     }
     $data['attachments'] = $attachments;
     // Return the input data
     return $data;
 }
Ejemplo n.º 3
0
 /**
  * Close method.
  *
  * @return boolean	True on success false on failure.
  */
 public function close()
 {
     // Clean up any temporary images
     K2HelperImages::purge('user');
     return true;
 }
Ejemplo n.º 4
0
 public function getImage()
 {
     return K2HelperImages::getCategoryImage($this);
 }
Ejemplo n.º 5
0
 public function getUser()
 {
     $user = new stdClass();
     if ($this->userId) {
         $instance = K2Users::getInstance($this->userId);
         $user->name = $instance->name;
         $user->username = $instance->username;
         $user->link = $instance->link;
         $user->image = $instance->image;
     } else {
         $user->name = $this->name;
         $user->username = $this->name;
         $user->link = false;
         $user->image = false;
         $params = JComponentHelper::getParams('com_k2');
         if (!$user->image && $params->get('gravatar')) {
             $user->image = new stdClass();
             $user->image->src = '//www.gravatar.com/avatar/' . md5($this->email) . '?s=' . $params->get('commenterImgWidth', 48);
             if ($params->get('userImageDefault')) {
                 $user->image->src .= '&d=' . urlencode(JURI::root(false) . '/' . K2HelperImages::getPlaceholder('user'));
             }
             $user->image->url = $user->image->src;
         }
     }
     return $user;
 }
Ejemplo n.º 6
0
 public function getImage()
 {
     return K2HelperImages::getUserImage($this);
 }
Ejemplo n.º 7
0
 public function onUserAfterSave($data, $isNew, $result, $error)
 {
     // Get application
     $application = JFactory::getApplication();
     // Get params
     $params = JComponentHelper::getParams('com_k2');
     // Get input
     $task = $application->input->get('task', '', 'cmd');
     $isK2UserForm = $application->input->get('K2UserForm', 0, 'int');
     // Process only in front-end
     if ($application->isSite()) {
         // Check spammer for activation and registrations. Only in front-end
         if (($task == 'activate' || $isNew) && $params->get('stopForumSpam')) {
             $this->checkSpammer($data);
         }
         // Save K2 user profile
         if ($task != 'activate' && ($params->get('K2UserProfile') == 'native' || $params->get('K2UserProfile') == 'legacy' && $isK2UserForm)) {
             // Get model
             $model = K2Model::getInstance('Users');
             // Get input data
             $input = $data;
             // Convert plugin data to normal input so our model can save it
             if (isset($data['k2Profile'])) {
                 foreach ($data['k2Profile'] as $name => $value) {
                     $input[$name] = $value;
                 }
             }
             if (!isset($input['image'])) {
                 $input['image'] = array();
                 $input['image']['remove'] = 0;
             }
             if (!$input['image']['remove']) {
                 // Get files
                 $files = $application->input->files->get('jform');
                 if (isset($files['k2Profile']) && $files['k2Profile']['image']['tmp_name']) {
                     $file = $files['k2Profile']['image'];
                     $image = K2HelperImages::add('user', $file, null);
                     $input['image']['flag'] = 1;
                     $input['image']['temp'] = $image->temp;
                 }
             } else {
                 $input['image']['flag'] = 0;
                 $input['image']['temp'] = false;
             }
             // Pass data to the model
             $model->setState('data', $input);
             $model->setState('site', true);
             // Save
             if (!$model->save()) {
                 $this->_subject->setError($model->getError());
                 return false;
             }
             // Redirect
             if ($params->get('K2UserProfile') == 'legacy') {
                 $itemid = $params->get('redirect');
                 if (!$isNew && $itemid) {
                     $menu = $application->getMenu();
                     $item = $menu->getItem($itemid);
                     $url = JRoute::_($item->link . '&Itemid=' . $itemid, false);
                     if (JURI::isInternal($url)) {
                         $application->enqueueMessage(JText::_('K2_YOUR_SETTINGS_HAVE_BEEN_SAVED'));
                         $application->redirect($url);
                     }
                 }
             }
         }
     }
     return true;
 }
Ejemplo n.º 8
0
 private function importCategory($category)
 {
     $categoryData = array();
     $categoryData['id'] = null;
     $categoryData['title'] = $category->title;
     $categoryData['description'] = $category->description;
     if ($category->published < 0) {
         $categoryData['state'] = -1;
     } else {
         if ($category->published > 0) {
             $categoryData['state'] = 1;
         } else {
             $categoryData['state'] = 0;
         }
     }
     $categoryData['parent_id'] = 0;
     $categoryData['access'] = $category->access;
     $categoryData['language'] = $category->language;
     $categoryParams = new JRegistry($category->params);
     $categoryImage = $categoryParams->get('image');
     if ($categoryImage) {
         $image = K2HelperImages::add('category', null, $categoryImage);
         $categoryData['image'] = array('id' => '', 'temp' => $image->temp, 'path' => '', 'remove' => 0, 'caption' => '', 'credits' => '');
     }
     $model = K2Model::getInstance('Categories');
     $model->setState('data', $categoryData);
     if (!$model->save()) {
         K2Response::throwError($model->getError());
     }
     // Get generated category id
     $categoryId = $model->getState('id');
     // Import JForm
     jimport('joomla.form.form');
     // Determine form name and path
     $formName = 'K2CategoriesForm';
     $formPath = JPATH_ADMINISTRATOR . '/components/com_k2/models/categories.xml';
     $form = JForm::getInstance($formName, $formPath);
     $params = new JRegistry('');
     foreach ($form->getFieldset() as $field) {
         $params->def($field->__get('fieldname'), $field->__get('value'));
     }
     // Update date and author information since the model has auto set this data during save
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->update($db->quoteName('#__k2_categories'));
     $query->set($db->quoteName('created') . ' = ' . $db->quote($category->created_time));
     $query->set($db->quoteName('modified') . ' = ' . $db->quote($category->modified_time));
     $query->set($db->quoteName('created_by') . ' = ' . $db->quote($category->created_user_id));
     $query->set($db->quoteName('modified_by') . ' = ' . $db->quote($category->modified_user_id));
     $query->set($db->quoteName('params') . ' = ' . $db->quote($params->toString()));
     $query->where($db->quoteName('id') . ' = ' . $categoryId);
     $db->setQuery($query);
     $db->execute();
     return $categoryId;
 }
Ejemplo n.º 9
0
 public function getImages()
 {
     $images = array();
     if ($this->id) {
         $images = K2HelperImages::getItemImages($this);
     }
     return $images;
 }
Ejemplo n.º 10
0
 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;
 }