Example #1
0
/**
 * 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 alfresco_utils_get_wc_url($url, $op = 'norefresh', $username = '')
{
    global $CFG;
    $endpoint = alfresco_base_url();
    $ticket = alfresco_utils_get_ticket($op, $username);
    if (false === strstr($url, '?')) {
        return $endpoint . $url . '?alf_ticket=' . $ticket;
    } else {
        return $endpoint . str_replace('?', '?alf_ticket=' . $ticket . '&', $url);
    }
}
function get_image_size($uuid)
{
    global $repo;
    /// Check if file exists
    if (!($finfo = $repo->get_info($uuid))) {
        return false;
    } else {
        /// Get the mime type so it really an image.
        if (mimeinfo("icon", $finfo->filename) != "image.gif") {
            return false;
        } else {
            $array_size = getimagesize(str_replace($finfo->title, rawurlencode($finfo->title), $finfo->fileurl) . '?alf_ticket=' . alfresco_utils_get_ticket());
            return $array_size;
        }
    }
    //unset($filepath, $array_size);
}
 /**
  * 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.
  * @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)
 {
     global $CFG, $USER;
     if (ALFRESCO_DEBUG_TRACE) {
         mtrace('read_file(' . $uuid . ')');
     }
     $this->errormsg = '';
     $downloading = false;
     if (!empty($localfile)) {
         if (!($localfh = fopen($localfile, 'w'))) {
             return false;
         }
         $downloading = true;
     }
     $node = $this->get_info($uuid);
     //        if (isloggedin()) {
     //            $username = $USER->username;
     //        } else {
     //            $username = '';
     //        }
     $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=' . alfresco_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($CFG->repository_alfresco_cachetime)) {
                 $age = $CFG->repository_alfresco_cachetime;
             } 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: ');
             header('Content-Disposition: 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($CFG->repository_alfresco_cachetime)) {
         $age = $CFG->repository_alfresco_cachetime;
     } 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: ');
     header('Content-Disposition: filename="' . $node->filename . '"');
     /// Close session - not needed anymore.
     @session_write_close();
     echo $filecontents;
     exit;
 }