/** * Event call after file update * * @param object $model * @param array $changes * @return void */ public function onAfterUpdate($model = NULL, $changes = array()) { $activity = ''; $message = ''; $ref = ''; $sync = 0; $model = $model ? $model : $this->model; if (empty($changes)) { // Get session $jsession = App::get('session'); // Get values from session $updated = $jsession->get('projects.' . $model->get('alias') . '.updated'); $uploaded = $jsession->get('projects.' . $model->get('alias') . '.uploaded'); $failed = $jsession->get('projects.' . $model->get('alias') . '.failed'); $deleted = $jsession->get('projects.' . $model->get('alias') . '.deleted'); $restored = $jsession->get('projects.' . $model->get('alias') . '.restored'); $expanded = $jsession->get('projects.' . $model->get('alias') . '.expanded'); // Clean up session values $jsession->set('projects.' . $model->get('alias') . '.failed', ''); $jsession->set('projects.' . $model->get('alias') . '.updated', ''); $jsession->set('projects.' . $model->get('alias') . '.uploaded', ''); $jsession->set('projects.' . $model->get('alias') . '.deleted', ''); $jsession->set('projects.' . $model->get('alias') . '.restored', ''); $jsession->set('projects.' . $model->get('alias') . '.expanded', ''); } else { $updated = !empty($changes['updated']) ? $changes['updated'] : NULL; $uploaded = !empty($changes['uploaded']) ? $changes['uploaded'] : NULL; $failed = !empty($changes['failed']) ? $changes['failed'] : NULL; $deleted = !empty($changes['deleted']) ? $changes['deleted'] : NULL; $restored = !empty($changes['restored']) ? $changes['restored'] : NULL; $expanded = !empty($changes['expanded']) ? $changes['expanded'] : NULL; } // Provisioned project? if ($model->isProvisioned() || !$model->get('id')) { return false; } // Pass success or error message if (!empty($failed) && !$uploaded && !$uploaded) { \Notify::message(Lang::txt('PLG_PROJECTS_FILES_ERROR_FAILED_TO_UPLOAD') . $failed, 'error', 'projects'); } elseif ($uploaded || $updated || $expanded) { $uploadParts = explode(',', $uploaded); $updateParts = explode(',', $updated); $sync = 1; if ($uploaded) { if (count($uploadParts) > 2) { $message = 'uploaded ' . basename($uploadParts[0]) . ' and ' . (count($uploadParts) - 1) . ' more files '; } else { $message = 'uploaded '; $u = 0; foreach ($uploadParts as $part) { $message .= basename($part); $u++; $message .= count($uploadParts) == $u ? '' : ', '; } } // Save referenced files $ref = $uploaded; } if ($updated) { $message .= $uploaded ? '. Updated ' : 'updated '; if (count($updateParts) > 2) { $message .= basename($updateParts[0]) . ' and ' . (count($updateParts) - 1) . ' more files '; } else { $u = 0; foreach ($updateParts as $part) { $message .= basename($part); $u++; $message .= count($updateParts) == $u ? '' : ', '; } } } $activity = $message . ' ' . strtolower(Lang::txt('PLG_PROJECTS_FILES_IN_PROJECT_FILES')); $message = 'Successfully ' . $message; $message .= $failed ? ' There was a problem uploading ' . $failed : ''; \Notify::message($message, 'success', 'projects'); } elseif ($deleted) { // Save referenced files $ref = $deleted; $sync = 1; $delParts = explode(',', $deleted); $what = count($delParts) == 1 ? $deleted : count($delParts) . ' ' . Lang::txt('PLG_PROJECTS_FILES_ITEMS'); // Output message \Notify::message(Lang::txt('PLG_PROJECTS_FILES_SUCCESS_DELETED') . ' ' . $what, 'success', 'projects'); } elseif ($restored) { // Save referenced files $ref = $restored; $sync = 1; $resParts = explode(',', $restored); $activity = 'restored deleted file ' . basename($resParts[0]); // Output message \Notify::message(Lang::txt('PLG_PROJECTS_FILES_SUCCESS_RESTORED') . ' ' . basename($resParts[0]), 'success', 'projects'); } // Add activity to feed if ($activity && $model->repo()->isLocal()) { $refParts = explode(',', $ref); $parsedRef = ''; $selected = array(); foreach ($refParts as $item) { $file = $model->repo()->getMetadata(trim($item)); $params = array('file' => $file); if ($file->exists()) { $hash = $model->repo()->getLastRevision($params); if ($hash) { $selected[] = substr($hash, 0, 10) . ':' . trim($file->get('localPath')); // Generate preview (regular and medium-size) $file->getPreview($model, $hash); $file->getPreview($model, $hash, '', 'medium'); } } } // Save hash and file name in a reference if ($selected) { foreach ($selected as $sel) { if (strlen($parsedRef) + strlen($sel) <= 254) { $parsedRef .= $sel . ','; } else { break; } } $parsedRef = substr($parsedRef, 0, strlen($parsedRef) - 1); } // Check to make sure we are not over in char length if (strlen($parsedRef) > 255) { $parsedRef = \Components\Projects\Helpers\Html::shortenText($parsedRef); } // Force sync if ($sync) { //$this->model->saveParam('google_sync_queue', 1); $this->set('forceSync', 1); } // Record activity $aid = $model->recordActivity($activity, $parsedRef, 'files', Route::url($model->link('files')), 'files', 1); } }
/** * Parse input * * @return string */ public function parseUrl($url = '', $citation = true, $incPreview = true, $format = 'apa') { // Incoming $url = $url ? $url : urldecode(Request::getVar('url', $url)); $output = array('rtype' => 'url', 'message' => ''); if (!$url) { $output['error'] = Lang::txt('PLG_PROJECTS_LINKS_EMPTY_URL'); return json_encode($output); } // Is this a DOI? $parts = explode("doi:", $url); $doi = count($parts) > 1 ? $parts[1] : NULL; // Treat url starting with numbers as DOI if (preg_match('#[0-9]#', substr($url, 0, 2))) { $doi = $url; } $data = NULL; // Pull DOI metadata if ($doi) { $output['rtype'] = 'doi'; $data = self::getDoiMetadata($doi, $citation, $url, $format); if ($this->getError()) { $output['error'] = $this->getError(); return json_encode($output); } } if (!$doi && filter_var($url, FILTER_VALIDATE_URL) == false) { $output['error'] = Lang::txt('Please enter a valid URL starting with http:// or https://'); return json_encode($output); } // DOI metadata if ($data) { $output['url'] = $url; if ($incPreview) { $output['preview'] = $data; } if ($citation == false && is_object($data)) { $output['data'] = array(); foreach ($data as $key => $value) { $output['data'][$key] = $value; } } } else { $ch = curl_init($url); $options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_USERAGENT => "", CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, CURLOPT_MAXREDIRS => 10); curl_setopt_array($ch, $options); curl_setopt($ch, CURLOPT_FAILONERROR, true); $content = curl_exec($ch); $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $finalUrl = str_replace("HTTP", "http", $finalUrl); $finalUrl = str_replace("HTTPS", "https", $finalUrl); curl_close($ch); if (!$finalUrl || !$content) { $output['message'] = Lang::txt('PLG_PROJECTS_LINKS_NO_PREVIEW'); return json_encode($output); } else { $output['url'] = $finalUrl; } if ($content) { require_once PATH_CORE . DS . 'plugins' . DS . 'projects' . DS . 'links' . DS . 'helpers' . DS . 'simple_html_dom.php'; $out = ''; // Create DOM from URL or file $html = file_get_html($finalUrl); $title = $html->find('title', 0)->innertext; //Title Of Page $out .= $title ? stripslashes('<h5>' . addslashes($title) . '</h5>') : '<h5>' . \Components\Projects\Helpers\Html::shortenText($finalUrl, 100) . '</h5>'; //Get all images found on this page $jpgs = $html->find('img[src$=jpg],img[src$=png]'); $images = array(); if ($jpgs) { foreach ($jpgs as $jpg) { $src = $jpg->getAttribute('src'); $width = $jpg->getAttribute('width'); $pathCounter = substr_count($src, "../"); $src = self::getImgSrc($src); // Must be larger than 25px if ($width && $width <= 100) { continue; } if (!$src) { continue; } if (!preg_match("/https?\\:\\/\\//i", $src)) { $src = self::getImageUrl($pathCounter, self::getLink($src, $finalUrl)); } // Can only show images served via https //$src = str_replace('http://', 'https://', $src); if (preg_match("/https/i", $src)) { $images[] = $src; } } } if ($images) { $out .= '<div id="link-image"><img src="' . $images[0] . '" alt="" /></div>'; } $description = NULL; // Get description from paragraphs $pars = $html->find('body div p'); if ($pars) { foreach ($pars as $p) { if (strlen($p->plaintext) > 200) { $description = $p->plaintext; break; } } } if (!$description) { // Set description if desc meta tag found else grab a little plain text of the page if ($html->find('meta[name="description"]', 0)) { $description = $html->find('meta[name="description"]', 0)->content; } else { $description = $html->find('body', 0)->plaintext; } } $out .= $description ? stripslashes('<p>' . \Hubzero\Utility\String::truncate(addslashes($description), 200) . '</p>') : '<p>' . \Hubzero\Utility\String::truncate(addslashes($finalUrl), 200) . '</p>'; if ($images) { $out .= '<span class="clear"></span>'; } // Preview of the url if ($incPreview) { $output['preview'] = $out; } $output['description'] = $description; $output['title'] = $title; } else { $output['error'] = Lang::txt('PLG_PROJECTS_LINKS_FAILED_TO_LOAD_URL'); return json_encode($output); } } return json_encode($output); }