/** * Prep crocodocs preview */ public function preparePreview() { $jxConfig = new JXConfig(); $my = JXFactory::getUser(); $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); // copy file to tmp path $tmpPath = $jxConfig->get('tmp_path') . DS . $this->filename; JFile::copy(JPATH_ROOT . DS . $this->path, $tmpPath); // if Crocodocs is enabled if ($jxConfig->isCrocodocsEnabled()) { curl_setopt($ch, CURLOPT_URL, 'https://crocodoc.com/api/v2/document/upload'); $post = array("token" => $jxConfig->get("crocodocs"), "file" => "@" . $tmpPath); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $response = curl_exec($ch); // @todo: validate json $responseObj = json_decode($response); if (!isset($responseObj->uuid)) { return false; } $uuid = $responseObj->uuid; $this->setParam('uuid', $uuid); $this->setParam('preview-ready', 0); } elseif ($jxConfig->isScribdEnabled()) { curl_setopt($ch, CURLOPT_URL, "http://api.scribd.com/api?api_key=" . $jxConfig->get(JXConfig::SCRIBD_API)); $post = array("method" => "docs.upload", "api_key" => $jxConfig->get(JXConfig::SCRIBD_API), "api_sig" => $jxConfig->get(JXConfig::SCRIBD_SECRET), "my_user_id" => $my->id, "paid_content" => 1, "file" => "@" . $tmpPath); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $response = curl_exec($ch); $result = simplexml_load_string($response); if (!isset($result->doc_id) && !isset($result->access_key)) { return false; } $this->setParam('doc_id', (string) $result->doc_id); $this->setParam('access_key', (string) $result->access_key); $this->setParam('preview-ready', 0); } JFile::delete($tmpPath); $this->store(); return true; }
/** * Return attachment view for the given stream */ public static function getAttachmentHTML($stream) { $my = JXFactory::getUser(); $data = json_decode($stream->raw); $html = ''; if (!function_exists('whoMakesAction')) { /** * Call this function to retrieve the item/message viewer * @param JTable $stream the current StreamTable * @param int $item_id the owner of the item (for example: id of the file or id of a message contains a link) * @param String $type type of the item (since tracking is done by id, being specific is safer. eg: file_220, link_220) */ function whoMakesAction($stream, $item_id = 0, $type = NULL) { // get list of avatar who viewed the stream $whoMakesAction = $stream->whoMakesAction($item_id, $type); $avatarListWhoMakeAction = ''; if ($whoMakesAction && count($whoMakesAction) > 0) { // Do rename the variable if its too long or easily mistyped $avatarListWhoMakeAction .= '<div class="user-horizontal-list message-reader-list">'; if ($type == 'video') { // change language from READ to SEEN if its a video $avatarListWhoMakeAction .= '<span class="small">' . JText::_('COM_STREAM_LABEL_SEEN_BY') . ' '; } else { $avatarListWhoMakeAction .= '<span class="small">' . JText::_('COM_STREAM_LABEL_READ_BY') . ' '; } /* $avatarListWhoMakeAction .= count($whoMakesAction) . ' reader'; */ $avatarListWhoMakeAction .= '<a href="#showReaders" data-content="<ul>'; foreach ($whoMakesAction as $user_id) { // there will be 0 as user which in return will load current user if ($user_id != 0 && $user_id != NULL) { $user = JXFactory::getUser($user_id); $avatarListWhoMakeAction .= StreamTemplate::escape('<li><a href="' . $user->getURL() . '">' . $user->name . '</a></li>'); } } $avatarListWhoMakeAction .= '</ul>">'; $label = count($whoMakesAction) > 1 ? JText::_('COM_STREAM_LABEL_USERS') : JText::_('COM_STREAM_LABEL_USER'); $avatarListWhoMakeAction .= count($whoMakesAction) . " {$label}</a>"; $avatarListWhoMakeAction .= '</span></div>'; } return $avatarListWhoMakeAction; } } // Attachment $jxConfig = new JXConfig(); $files = $stream->getFiles(); $hasPreview = false; $numPreview = 0; // Sort the files, photos at the bottom usort($files, array('StreamMessage', 'sortAttachment')); $imgHtml = '<div class="message-content-attachment">'; foreach ($files as $file) { // only show if the file does exist // @todo: templatize this ? $dlLink = JRoute::_('index.php?option=com_stream&view=system&task=download&file_id=' . $file->id); // Show file name only if preview doesn't exist // Otherwise, just show the preview. People can click on the preview and download it from tehre if (!$file->getParam('has_preview')) { // Show preview link, of if the filename is doc, docx, pdf, ppt, pptx $fext = strtolower(substr($file->filename, -4)); $html .= '<div data-filename="' . $file->filename . '" ' . 'data-message_id=' . $stream->id . ' class="message-content-file ">'; $html .= '<a title="Click to download" href="' . $dlLink . '">' . StreamTemplate::escape(JHtmlString::abridge($file->filename, 20, 13)) . '</a>'; $html .= ' <span class="small hint">(' . StreamMessage::formatBytes($file->filesize, 1) . ')</span>'; // append to file container only once if ($jxConfig->isCrocodocsEnabled() || $jxConfig->isScribdEnabled()) { if (in_array($fext, array('.doc', 'docx', '.pdf', '.ppt', 'pptx'))) { $html .= ' <a href="#preview" class="meta-preview small" data-filename="' . StreamTemplate::escape($file->filename) . '" data-file_id="' . $file->id . '" onclick="return S.preview.show(this);">' . JText::_('COM_STREAM_LABEL_PREVIEW') . '</a>'; } } $html .= whoMakesAction($stream, $file->id, 'file'); $html .= '<div class="clear"></div>'; $html .= '</div>'; /* // File can only be remove in 'edit' view if( $my->authorise('stream.message.edit', $stream) ){ $html .= '<a class="meta-edit" href="#removeAttachment" file_id="'.$file->id.'">'. JText::_('COM_STREAM_LABEL_REMOVE').'</a>'; } */ } if ($file->getParam('has_preview')) { $randId = 'preview_' . rand(1000, 9999); $path = str_replace(DS, '/', $file->getParam('thumb_path')); $imgHtml .= '<div class="message-content-preview"><img rel="#' . $randId . '" src="' . JURI::root() . $path . '" /></div>'; // Attach overlay code $width = $file->getParam('width'); $height = $file->getParam('height'); if (!empty($width) && !empty($height)) { if ($width > 640) { $height = 640 / $width * $height; $width = 640; } if ($height > 640) { $width = 640 / $height * $width; $height = 640; } $viewLink = JRoute::_('index.php?option=com_stream&view=system&task=download&file_id=' . $file->id . '&display=1'); $dlLink = JRoute::_('index.php?option=com_stream&view=system&task=download&file_id=' . $file->id); // Replace all <show_next> tag, since the previous one is clearly not the last one $imgHtml = str_replace('<show_next>', '<div class="image_next btn btn-large" onclick="$(\'[rel=\\\'#' . $randId . '\\\']\').click();">→</div>', $imgHtml); $imgHtml .= ' <div id="' . $randId . '" class="apple_overlay" style="width:' . $width . 'px">'; // IF this is NOT the first preview, add the 'PREV' button if ($numPreview != 0) { $imgHtml .= '<div class="image_prev btn btn-large" onclick="$(\'[rel=\\\'#' . $prevRandId . '\\\']\').click();">←</div>'; } $imgSrc = $dlLink; if ($file->getParam('preview_path')) { $imgSrc = str_replace(DS, '/', $file->getParam('preview_path')); $imgSrc = JURI::root() . $imgSrc; } $imgHtml .= '<show_next> <a class="close"></a> <img width="' . $width . '" height="' . $height . '" src="' . $imgSrc . '" /> <div> <a href="' . $viewLink . '" target="_blank">View full-size image</a> • <a href="' . $dlLink . '">Download</a></div> </div>'; $prevRandId = $randId; $hasPreview = true; $numPreview++; } } } $imgHtml .= '</div>'; // If there is no attachement at all, remove the div $imgHtml = str_replace('<div class="message-content-attachment"></div>', '', $imgHtml); $html .= $imgHtml; // If we have added the preview, which is left floated, we need to add a clearing div // the sorting function above will make sure that preview'ed would be the last attachment if ($hasPreview) { // Get rid of all the <show_next> marker $html = str_replace('<show_next>', '', $html); $html .= '<div class="clear"></div>'; } // Videos if (!empty($data->video)) { foreach ($data->video as $videoid) { $video = JTable::getInstance('Video', 'StreamTable'); if ($video->load($videoid)) { $html .= '<div class="message-content-video" id="video-' . $videoid . '"> <img class="message-content-video-thumbnail interactive" src="' . $video->thumb . '" embed_id="' . $videoid . '" embed_type="videos"/> <span class="video-duration">' . StreamMessage::formatDuration($video->duration) . '</span> <div class="message-content-preview-desc"> <div class="preview-title">' . JHtmlString::truncate($video->title, 24) . '</div> <div class="preview-desc">' . JHtmlString::truncate($video->description, 180) . '</div> </div><div class="clear"></div>' . whoMakesAction($stream, $video->id, 'video') . ' <div class="clear"></div> </div>'; } } } // Slideshare if (!empty($data->slideshare)) { foreach ($data->slideshare as $slideshareid) { $slideshare = JTable::getInstance('Slideshare', 'StreamTable'); if ($slideshare->load($slideshareid)) { $ss = json_decode($slideshare->response); $html .= '<div class="message-content-video slideshare" id="video-' . $slideshareid . '"> <img src="' . $ss->thumbnail . '" embed_id="' . $slideshareid . '" embed_type="slideshare"/> <span class="video-duration"></span> <div class="message-content-preview-desc"> <div class="preview-title">' . JHtmlString::truncate($ss->title, 24) . '</div> <div class="preview-desc">' . JHtmlString::truncate($ss->author_name, 180) . '</div> </div><div class="clear"></div>' . whoMakesAction($stream, $slideshareid, 'slideshare') . ' <div class="clear"></div> </div>'; } } } /* Link service * Certain link will able to store excerpt from the linked page */ $params = json_decode($stream->params); /* refetch if the link is not grab yet */ $url = self::getLinks($stream->message); if (!empty($url[0])) { $linkTable = JTable::getInstance('Link', 'StreamTable'); if ($linkTable->load(array('link' => $url[0]))) { $linkParam = StreamLinks::format($linkTable->params); if (!empty($linkParam) && strlen($linkParam->text) > 0) { $html .= '<div class="stream-message-links-in-post">'; if ($linkParam->media_type && $linkParam->media_link) { if (strlen($linkParam->media_type == 'image' && $linkParam->media_link) > 0) { $html .= '<div class="stream-links-image-container"> <span> <img class="stream-message-links-image" src="' . StreamLinks::imageAssetThumbnailPath($linkParam->media_link) . '" /> </span> </div>'; } } $html .= '<div class="stream-links-container"> <div class="stream-message-links-title">' . $linkParam->title . '</div> <div class="stream-message-links-url">' . $linkParam->url . '</div> <div class="stream-message-links-content">' . $linkParam->text . '</div>' . '</div><div class="clear"></div>' . whoMakesAction($stream, $stream->id, 'link') . ' <div class="clear"></div> </div>'; } } } return $html; }
<div class="file-list-meta"> <a class="file-title" data-filename="<?php echo StreamTemplate::escape($fullFilename); ?> " href="<?php echo JRoute::_('index.php?option=com_stream&view=system&task=download&file_id=' . $row->id); ?> "><?php echo StreamTemplate::escape(JHtmlString::abridge($row->filename, 32, 25)); ?> </a> <span class="small"> <?php $fext = strtolower(substr($row->filename, -4)); if ($jxConfig->isCrocodocsEnabled() || $jxConfig->isScribdEnabled()) { if (in_array($fext, array('.doc', 'docx', '.pdf', '.ppt', 'pptx'))) { echo ' <a href="javascript:void(0);" class="meta-preview small" data-message_id="' . $row->stream_id . '" data-filename="' . StreamTemplate::escape($row->filename) . '" data-file_id="' . $row->id . '" onclick="S.preview.show(this);">' . JText::_('COM_STREAM_LABEL_PREVIEW') . '</a>'; } } ?> (<?php echo StreamMessage::formatBytes($row->filesize); ?> ) </span> <div class="file-list-meta-content"> <div class="file-list-meta-inner"> <span>Attached in:</span> <a href="<?php echo $streamObj->getUri();
/** * */ public function preview() { $my = JXFactory::getUser(); $fileid = JRequest::getVar('file_id'); $file = JTable::getInstance('File', 'StreamTable'); $jxConfig = new JXConfig(); $data = array(); $data['reload'] = false; if ($file->load($fileid) && $my->authorise('stream.file.download', $file) && ($jxConfig->isCrocodocsEnabled() || $jxConfig->isScribdEnabled())) { // Grab the uuid $uuid = $file->getParam('uuid'); $docid = $file->getParam('doc_id'); if ($jxConfig->isCrocodocsEnabled() && empty($uuid) || $jxConfig->isScribdEnabled() && empty($docid)) { // haven't been uploaded yet, upload and grab the uuid $store = $file->preparePreview(); // reload file for updated content $file->load($fileid); $uuid = $file->getParam('uuid'); $docid = $file->getParam('doc_id'); } // Check if file is ready $previewReady = $file->getParam('preview-ready'); $isViewable = false; if (empty($previewReady)) { if ($jxConfig->isCrocodocsEnabled()) { // Preview not ready, query status $options = new JRegistry(); $transport = new JHttpTransportCurl($options); $http = new JHttp($options, $transport); $response = $http->get('https://crocodoc.com/api/v2/document/status?token=' . $jxConfig->get(JXConfig::CROCODOCS) . '&uuids=' . $file->getParam('uuid')); $responseObj = json_decode($response->body); //print_r($responseObj); exit; if (!isset($responseObj->error)) { $isViewable = !empty($responseObj[0]->viewable); if ($isViewable) { $previewReady = true; $file->setParam('preview-ready', 1); $file->store(); } } } else { // Query Scribd Preview status $http = new JHttp(); $statusUrl = 'http://api.scribd.com/api?api_key=' . $jxConfig->get(JXConfig::SCRIBD_API) . '&doc_id=' . $file->getParam('doc_id') . '&method=docs.getConversionStatus&my_user_id=' . $file->user_id; $response = $http->get($statusUrl); $result = simplexml_load_string($response->body); if (isset($result->conversion_status) && $result->conversion_status == 'DONE') { $previewReady = true; $file->setParam('preview-ready', 1); $file->store(); } } } if ($previewReady && (!empty($uuid) || !empty($docid))) { if ($jxConfig->isCrocodocsEnabled()) { $session_id = $file->getParam('previewSession'); if (time() > $file->getParam('previewExpiry')) { // File uploaded, try to create session $options = new JRegistry(); $transport = new JHttpTransportCurl($options); $http = new JHttp($options, $transport); $response = $http->post('https://crocodoc.com/api/v2/session/create', array('token' => '$jxConfig->get(JXConfig::CROCODOCS)', 'uuid' => $file->getParam('uuid'))); //$response = $http->post( 'https://crocodoc.com/api/v2/session/create' , array('token' => 'Oe8fA1mQ59LSwtBlKy4Nkbvn', 'uuid' => $uuid ) ); $responseObj = json_decode($response->body); $session_id = $responseObj->session; // Store this session so that we don't have to fetch it again $file->setParam('previewSession', $session_id); $file->setParam('previewExpiry', time() + 60 * 50); // ste it 50 mins from now $file->store(); } $html = '<iframe style="border:0px;width:100%;height:100%" src="https://crocodoc.com/view/' . $session_id . '"></iframe>'; } else { $html = "<div id='embedded_doc' data-doc_id=\"" . $file->getParam("doc_id") . "\" data-access_key=\"" . $file->getParam("access_key") . "\"><a href='http://www.scribd.com'>Scribd</a></div>\t\t\t\t\t\t\n\t\t\t\t\t\t\t <script type=\"text/javascript\">\n\t\t\t\t\t\t\t\t// Instantiate iPaper\n\t\t\t\t\t\t\t\t\$(document).ready(function() {\n\t\t\t\t\t\t\t\t\tscribd_doc = scribd.Document.getDoc(\$('#embedded_doc').data('doc_id'), \$('#embedded_doc').data('access_key'));\n\t\t\t\t\t\t\t\t\t// Parameters\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('height', 750);\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('width', 750);\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('auto_size', true);\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('mode', 'slideshow');\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('jsapi_version', 2);\n\n\t\t\t\t\t\t\t\t\t// Attach event listeners\n\t\t\t\t\t\t\t\t\tscribd_doc.addEventListener('docReady', onDocReady);\n\n\t\t\t\t\t\t\t\t\t// Write the instance\n\t\t\t\t\t\t\t\t\tscribd_doc.write('embedded_doc');\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t \n\t\t\t\t\t\t\t\tvar onDocReady = function(e) {\n\t\t\t\t\t\t\t\t\tscribd_doc.api.setPage(1);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t \n\t\t\t\t\t\t\t\t// Bookmark Helpers\n\t\t\t\t\t\t\t\tvar goToPage = function(page) {\n\t\t\t\t\t\t\t\t\tif (scribd_doc.api){\n\t\t\t\t\t\t\t\t\t\tscribd_doc.api.setPage(page);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t \n\t\t\t\t\t\t\t\tvar goToMiddle = function() {\n\t\t\t\t\t\t\t\t\tif (scribd_doc.api){\n\t\t\t\t\t\t\t\t\t\tgoToPage( Math.floor(scribd_doc.api.getPageCount()/2) );\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tvar goToEnd = function() {\n\t\t\t\t\t\t\t\t\tif (scribd_doc.api) {\n\t\t\t\t\t\t\t\t\t\tgoToPage(scribd_doc.api.getPageCount());\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t </script>"; } $data['html'] = $html; } else { $data['reload'] = 1; $data['file_id'] = $fileid; $data['html'] = '<p>Please wait while we prepare your document</p>'; } } else { $data['html'] = "<p>Not allowed to preview.</p>"; } echo json_encode($data); exit; }