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; }
public static function getCategoryImage($category) { // Settings $params = JComponentHelper::getParams('com_k2'); // File system $filesystem = K2FileSystem::getInstance(); // Initialize value $image = null; // Save path $savepath = self::$paths['category']; // Value $value = json_decode($category->image); if (isset($value->flag) && $value->flag) { $image = new K2Image(); $image->id = md5('Image' . $category->id); $image->src = K2FileSystem::getUriRoot(true) . $savepath . '/' . $image->id . '.jpg'; $image->url = K2FileSystem::getUriRoot() . $savepath . '/' . $image->id . '.jpg'; if ($params->get('imageTimestamp')) { $timestamp = JFactory::getDate($category->modified)->toUnix(); $image->src .= '?t=' . $timestamp; $image->url .= '?t=' . $timestamp; } $image->alt = $value->caption ? $value->caption : $category->title; $image->caption = $value->caption; $image->credits = $value->credits; $image->flag = 1; } else { if ($params->get('catImageDefault')) { $placeholder = self::getPlaceholder('category'); $image = new K2Image(); $image->src = JURI::root(true) . '/' . $placeholder; $image->url = JURI::root(false) . $placeholder; $image->alt = $category->title; $image->caption = ''; $image->credits = ''; $image->flag = 0; } } return $image; }
public function display($tpl = null) { // Get application $application = JFactory::getApplication(); // Get input $input = $application->input; $id = $input->get('id', 0, 'int'); $hash = $input->get('hash', '', 'string'); // Both input fields are required if (!$id || empty($hash)) { throw new Exception(JText::_('K2_NOT_FOUND'), 404); } // Check hash if (JApplication::getHash($id) != $hash) { throw new Exception(JText::_('K2_NOT_FOUND'), 404); } // Get model K2Model::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_k2/models'); $model = K2Model::getInstance('Attachments', 'K2Model'); // Get attachment $model->setState('id', $id); $attachment = $model->getRow(); // Get item $model = K2Model::getInstance('Items', 'K2Model'); $model->setState('id', $attachment->itemId); $item = $model->getRow(); // If we are on front-end check access verify that user has the permission to download this attachment if ($application->isSite()) { $item->checkSiteAccess(); } // Import K2 plugins JPluginHelper::importPlugin('k2'); $dispatcher = JDispatcher::getInstance(); // Trigger onK2BeforeDownload event $dispatcher->trigger('onK2BeforeDownload', array(&$attachment)); // Params $params = JComponentHelper::getParams('com_k2'); // Custom path flag $customPathFlag = $params->get('attachmentsFolder') && $params->get('filesystem') == 'Local' ? true : false; // File system $filesystem = $customPathFlag ? K2FileSystem::getInstance('Local', $params->get('attachmentsFolder')) : K2FileSystem::getInstance(); // Path $path = $customPathFlag ? '' : 'media/k2/attachments'; // Determine the key if ($attachment->file) { $key = $path . '/' . $attachment->itemId . '/' . $attachment->file; } else { if ($attachment->path) { $key = $attachment->path; // Since it is a path we need to enforce the local adapter for the file system $filesystem = K2FileSystem::getInstance('Local'); } } // Check if file exists if (!$filesystem->has($key)) { throw new Exception(JText::_('K2_NOT_FOUND'), 404); } // Update downloads counter if ($application->isSite()) { $attachment->track(); } // Trigger the onK2AfterDownload event $dispatcher->trigger('onK2AfterDownload', array(&$attachment)); // Read the file $file = $filesystem->get($key); $size = $file->getSize(); $content = $file->getContent(); $filename = basename($key); $finfo = new finfo(FILEINFO_MIME); $mime = $finfo->buffer($content); ob_end_clean(); $application->clearHeaders(); $application->setHeader('Pragma', 'public', true); $application->setHeader('Expires', '0', true); $application->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true); $application->setHeader('Content-Type', $mime, true); $application->setHeader('Content-Disposition', 'attachment; filename=' . $filename . ';', true); $application->setHeader('Content-Transfer-Encoding', 'binary', true); $application->setHeader('Content-Length', $size, true); $application->sendHeaders(); echo $content; $application->close(); }
public function deleteFile($table) { if ($table->file) { // Params $params = JComponentHelper::getParams('com_k2'); // Custom path flag $customPathFlag = $params->get('attachmentsFolder') && $params->get('filesystem') == 'Local' ? true : false; // File system $filesystem = $customPathFlag ? K2FileSystem::getInstance('Local', $params->get('attachmentsFolder')) : K2FileSystem::getInstance(); // Path $path = $customPathFlag ? '' : 'media/k2/attachments'; if ($table->itemId) { $folder = $table->itemId; $key = $path . '/' . $folder . '/' . $table->file; } else { list($folder, $file) = explode('/', $table->file); $key = $path . '/' . $folder . '/' . $file; } if ($filesystem->has($key)) { $filesystem->delete($key); } $keys = $filesystem->listKeys($path . '/' . $folder . '/'); if (empty($keys['keys']) && $filesystem->has($path . '/' . $folder)) { $filesystem->delete($path . '/' . $folder); } } }
public static function remove($media, $itemId) { // File system $filesystem = K2FileSystem::getInstance(); foreach ($media as $entry) { if (isset($entry->upload) && $entry->upload) { // Key $mediaKey = 'media/k2/media/' . $itemId . '/' . $entry->upload; if ($filesystem->has($mediaKey)) { $filesystem->delete($mediaKey); } } } return true; }
public static function update($attachments, $item) { // Application $application = JFactory::getApplication(); // Params $params = JComponentHelper::getParams('com_k2'); // Session $session = JFactory::getSession(); // Custom path flag $customPathFlag = $params->get('attachmentsFolder') && $params->get('filesystem') == 'Local' ? true : false; // File system $filesystem = $customPathFlag ? K2FileSystem::getInstance('Local', $params->get('attachmentsFolder')) : K2FileSystem::getInstance(); // Target path $targetPath = $customPathFlag ? '' : 'media/k2/attachments'; // Uploaded media $uploadedAttachments = array(); // Item id $itemId = $item->id; // Get attachments model $model = K2Model::getInstance('Attachments'); // Attachment Ids $attachmentIds = array(); // Ordering $ordering = 1; // Iterate over the galleries and transfer the new attachment files from /tmp to /media/k2/attachments foreach ($attachments as $attachment) { $attachment = (object) $attachment; if ($attachment->remove) { // Delete attachment if ($attachment->id) { $model->setState('id', $attachment->id); $model->delete(); } } else { if ($attachment->file) { // Since we have a file reset the path $attachment->path = ''; // Target file $target = $targetPath . '/' . $itemId . '/' . $attachment->file; // Source file $source = $application->getCfg('tmp_path') . '/' . $attachment->file; // Check if the attachment has a new uploaded file if (JFile::exists($source)) { // Get the generated unique file name $uniqueFileName = $attachment->file; // Convert back the filename $attachment->file = substr($uniqueFileName, strpos($attachment->file, '_') + 1); $target = $targetPath . '/' . $itemId . '/' . $attachment->file; // Ensure we don't override any existing attachments with the same filename if ($filesystem->has($target)) { // File exists, roll back the name changes we will keep the generated file name $attachment->file = $uniqueFileName; $target = $targetPath . '/' . $itemId . '/' . $attachment->file; } // Transfer the file from the temporary folder to the current file system $buffer = file_get_contents($source); $filesystem->write($target, $buffer, true); // Delete the temporary file JFile::delete($source); // Remove temporary file from session $temp = $session->get('k2.attachments', array()); if (($key = array_search($attachment->file, $temp)) !== false) { unset($temp[$key]); $session->set('k2.attachments', $temp); } } // Push the attachment to uploaded attachment files array $uploadedAttachments[] = ltrim($target, '/'); } else { if ($attachment->path) { // Since we have a path reset the file $attachment->file = ''; } } // Save attachment $attachment->ordering = $ordering; $attachment->itemId = $itemId; $model->setState('data', (array) $attachment); if ($model->save()) { // Push the id of the attachment to the array $attachmentIds[] = (int) $model->getState('id'); } $ordering++; } } // Update item table $item->attachments = json_encode($attachmentIds); $item->store(); // Iterate over the media files in /media/k2/media and delete those who have been removed by the user $folderKey = $targetPath . '/' . $itemId . '/'; $folderKey = ltrim($folderKey, '/'); $keys = $filesystem->listKeys($folderKey); $files = isset($keys['keys']) ? $keys['keys'] : $keys; foreach ($files as $attachmentKey) { if ($attachmentKey != $folderKey && !in_array($attachmentKey, $uploadedAttachments) && $filesystem->has($attachmentKey)) { $filesystem->delete($attachmentKey); } } }
public static function remove($galleries, $itemId) { // File system $filesystem = K2FileSystem::getInstance(); foreach ($galleries as $gallery) { if (isset($gallery->upload) && $gallery->upload) { // Key $galleryKey = 'media/k2/galleries/' . $itemId . '/' . $gallery->upload; if ($filesystem->has($galleryKey)) { $files = $filesystem->listKeys($galleryKey); foreach ($files['keys'] as $key) { if ($filesystem->has($key)) { $filesystem->delete($key); } } $filesystem->delete($galleryKey); } } } return true; }