Ejemplo n.º 1
0
/**
 * Returns the title of a html document.
 * @param string $string                The contents of the input document.
 * @param string $input_encoding        The encoding of the input document. If the value is not set, it is detected.
 * @param string $$output_encoding      The encoding of the retrieved title. If the value is not set, the system encoding is assumend.
 * @return string                       The retrieved title, html-entities and extra-whitespace between the words are cleaned.
 */
function api_get_title_html(&$string, $output_encoding = null, $input_encoding = null) {
    if (@preg_match('/<head.+<title[^>]*>(.*)<\/title>/msi', $string, $matches)) {
        if (empty($output_encoding)) {
            $output_encoding = api_get_system_encoding();
        }
        if (empty($input_encoding)) {
            $input_encoding = api_detect_encoding_html($string);
        }
        return trim(@preg_replace('/\s+/', ' ', api_html_entity_decode(api_convert_encoding($matches[1], $output_encoding, $input_encoding), ENT_QUOTES, $output_encoding)));
    }
    return '';
}
Ejemplo n.º 2
0
 /**
  * This function streams a string to the client for download.
  * You have to ensure that the calling script then stops processing (exit();)
  * otherwise it may cause subsequent use of the page to want to download
  * other pages in php rather than interpreting them.
  *
  * @param string $full_string The string contents
  * @param boolean $forced Whether "save" mode is forced (or opening directly authorized)
  * @param string $name The name of the file in the end (including extension)
  *
  * @return false if file doesn't exist, true if stream succeeded
  */
 public static function string_send_for_download($full_string, $forced = false, $name = '')
 {
     $filename = $name;
     $len = strlen($full_string);
     if ($forced) {
         //force the browser to save the file instead of opening it
         header('Content-type: application/octet-stream');
         //header('Content-Type: application/force-download');
         header('Content-length: ' . $len);
         if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
             header('Content-Disposition: filename= ' . $filename);
         } else {
             header('Content-Disposition: attachment; filename= ' . $filename);
         }
         if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
             header('Pragma: ');
             header('Cache-Control: ');
             header('Cache-Control: public');
             // IE cannot download from sessions without a cache
         }
         header('Content-Description: ' . $filename);
         header('Content-transfer-encoding: binary');
         echo $full_string;
         return true;
         //You have to ensure that the calling script then stops processing (exit();)
         //otherwise it may cause subsequent use of the page to want to download
         //other pages in php rather than interpreting them.
     } else {
         //no forced download, just let the browser decide what to do according to the mimetype
         $content_type = self::file_get_mime_type($filename);
         header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
         header('Cache-Control: no-cache, must-revalidate');
         header('Pragma: no-cache');
         switch ($content_type) {
             case 'text/html':
                 $encoding = @api_detect_encoding_html($full_string);
                 if (!empty($encoding)) {
                     $content_type .= '; charset=' . $encoding;
                 }
                 break;
             case 'text/plain':
                 $encoding = @api_detect_encoding(strip_tags($full_string));
                 if (!empty($encoding)) {
                     $content_type .= '; charset=' . $encoding;
                 }
                 break;
         }
         header('Content-type: ' . $content_type);
         header('Content-Length: ' . $len);
         $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
         if (strpos($user_agent, 'msie')) {
             header('Content-Disposition: ; filename= ' . $filename);
         } else {
             header('Content-Disposition: inline; filename= ' . $filename);
         }
         echo $full_string;
         //You have to ensure that the calling script then stops processing (exit();)
         //otherwise it may cause subsequent use of the page to want to download
         //other pages in php rather than interpreting them.
         return true;
     }
 }