/** * Return service url for ticket based authentication. * $op Option for refreshing ticket or not. * @param string $username The username to use for user authentication. */ function elis_files_utils_get_wc_url($url, $op = 'norefresh', $username = '') { $endpoint = elis_files_base_url(); $ticket = elis_files_utils_get_ticket($op, $username); if (false === strstr($url, '?') ) { return $endpoint . $url . '?alf_ticket=' . $ticket; } else { return $endpoint . str_replace('?', '?alf_ticket=' . $ticket . '&', $url); } }
/** * Download a file from alfresco - do we use this? * * @param string $uuid a unique id of directory in alfresco * @param string $path path to a directory * @return array|null */ public function get_file($uuid, $file = '') { //error_log("get_file($uuid, '{$file}');"); $node = $this->elis_files->get_info($uuid); if (empty($node)) { return null; } // Test to make sure this works with, say, a teacher or someone non-admin $username = ''; /// Check for a non-text file type to just pass through to the end user. $mimetype = !empty($node->filemimetype) ? $node->filemimetype : ''; $ticket = elis_files_utils_get_ticket('refresh', $username); $url = str_replace($node->filename, urlencode($node->filename), $node->fileurl) . '?alf_ticket=' . $ticket; $path = $this->prepare_file($file); $fp = fopen($path, 'w'); $c = new curl; $response = $c->download(array(array('url'=>$url, 'file'=>$fp))); return array('path' => $path, 'url' => $url); }
/** * Return a file from the repository server. * * @uses $CFG * @uses $USER * @param string $uuid Unique identifier for a node. * @param string $localfile A full system path (with filename) to download the contents locally. * @param bool $return Set to true to just return the file content (optional). * @param bool $process Whether to process text-based content for relative URLs for converting. * @param bool $attachment true if desired as attachment, false(default) for file * @return mixed|bool Returns the file to the browser (with appropriate MIME headers) or False on failure. */ function read_file($uuid, $localfile = '', $return = false, $process = true, $attachment = false) { global $CFG, $USER; if (ELIS_FILES_DEBUG_TRACE) mtrace('read_file(' . $uuid . ')'); $this->errormsg = ''; $downloading = false; if (!empty($localfile)) { if (!$localfh = fopen($localfile, 'w')) { return false; } $downloading = true; } if (!($node = $this->get_info($uuid))) { return false; } $username = ''; /// Check for a non-text file type to just pass through to the end user. $mimetype = !empty($node->filemimetype) ? $node->filemimetype : ''; $url = str_replace($node->filename, urlencode($node->filename), $node->fileurl) . '?alf_ticket=' . elis_files_utils_get_ticket('refresh', $username); /// IE compatibiltiy HACK! if (!$downloading && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && ini_get('zlib.output_compression') == 'On') { ini_set('zlib.output_compression', 'Off'); } $processtypes = array( 'application/x-javascript', 'application/xhtml+xml', 'text/css' ); /// Don't process non-text and "known" filetypes for relative links. if ((strpos($mimetype, 'text') !== 0 && !in_array($mimetype, $processtypes)) || !$process) { if (!$downloading && !$return && $headers = get_headers($url)) { foreach ($headers as $header) { if (0 === strpos(strtolower($header), 'http') || 0 === strpos(strtolower($header), 'server') || 0 === strpos(strtolower($header), 'set-cookie') || 0 === strpos(strtolower($header), 'connection') || 0 === strpos(strtolower($header), 'content-length')) { continue; } header($header); } } if (!$downloading && !$return) { /// Cache this file for the required amount of time. if (!empty($this->config->cache_time)) { $age = $this->config->cache_time; } else { /// The "No" caching option has a value of zero, so /// this handles the default (not set), as well as selecting "No" $age = 0; } header('Cache-Control: max-age=' . $age); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $age) . ' GMT'); header('Pragma: '); $cd_header = 'Content-Disposition: '; if ($attachment) { $cd_header .= 'attachment; '; } header($cd_header .'filename="'. $node->filename .'"'); } /// Close session - not needed anymore. if (!$return) { @session_write_close(); } /// Read the file contents in chunks and output directly. if ($fh = fopen($url, 'rb')) { if ($return) { $buffer = ''; } if (!$downloading) { while (!feof($fh)) { if (($buf = fread($fh, 8192)) === false) { return false; } if (!$return) { echo $buf; } else { $buffer .= $buf; } } } else { /// Downloading locally, write the file contents to the local file handler. $byteswritten = stream_copy_to_stream($fh, $localfh); return ($byteswritten == $node->filesize); } fclose($fh); } else { return false; } if ($return) { return $buffer; } exit; } /// Process the file, re-writing relative links to direct URLs which will $filecontents = ''; /// Read the file contents in chunks and output store for processing. if ($fh = fopen($url, 'r')) { while (!feof($fh)) { if (($filecontents .= fread($fh, 8192)) === false) { return false; } } fclose($fh); } else { return false; } $this->fix_links($filecontents, $this->get_file_path($node->uuid)); /// We've asked for the file contents to just be returned, so do that now. if ($return) { return $filecontents; } /// Downloading locally, write the file contents to the local file handler. if ($downloading) { if (($byteswritten = fwrite($localfh, $filecontents, strlen($filecontents))) === false) { return false; } else { return true; } } if ($headers = get_headers($url)) { foreach ($headers as $header) { if (0 === strpos($header, 'HTTP') || 0 === strpos($header, 'Server') || 0 === strpos($header, 'Set-Cookie') || 0 === strpos($header, 'Connection')) { continue; } header($header); } } /// Cache this file for the required amount of time. if (!empty($this->config->cache_time)) { $age = $this->config->cache_time; } else { /// The "No" caching option has a value of zero, so /// this handles the default (not set), as well as selecting "No" $age = 0; } header('Cache-Control: max-age=' . $age); header('Expires: '. gmdate('D, d M Y H:i:s', time() + $age) .' GMT'); header('Pragma: '); $cd_header = 'Content-Disposition: '; if ($attachment) { $cd_header .= 'attachment; '; } header($cd_header .'filename="'. $node->filename .'"'); /// Close session - not needed anymore. @session_write_close(); echo $filecontents; exit; }