Changeset 88970 in spip-zone


Ignore:
Timestamp:
May 2, 2015, 4:43:00 PM (5 years ago)
Author:
real3t@…
Message:

Version 0.6 (finale)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • _libs_/html2spip/misc_tools.php

    r88969 r88970  
    6464################################################################################
    6565
     66# function spip_add_document
     67# $url:        document' source url
     68# $width:      document's width
     69# $height:     document's height
     70# $mode:       document's mode; often 'document' or 'image' (eg. for a thumbnail)
     71# $titre:      document's title
     72# $descriptif: document's description
     73# $path:       document's destination path on local server; often should be _IMG_DIR
    6674function spip_add_document($url, $width, $height, $mode, $titre, $descriptif, $path) {
     75  $pathinfo = pathinfo(parse_url($url, PHP_URL_PATH));
     76
    6777  $f_name = iconv('UTF-8', 'ISO-8859-1', preg_replace(
    6878    '/\s+/',
    6979    '_',
    70     urldecode(
    71       preg_replace(
    72         '|^.*/([^/]*)$|',
    73         '$1',
    74         $url
    75       )
    76     )
     80    urldecode($pathinfo['filename'])
    7781  ));
     82
     83  $url_hdl = @fopen($url, 'r', false);
     84
     85  if (!($url_hdl || _HTML2SPIP_PRESERVE_DISTANT))
     86    die("$url_hdl is unreachable: cannot download !");
     87
     88  $meta = @stream_get_meta_data($url_hdl);
     89  $f_ext = null;
     90  $f_size = null;
     91
     92  if ($meta) {
     93    foreach ($meta['wrapper_data'] as $meta_value) {
     94      if (preg_match('/^Content-Type:/i', $meta_value)) {
     95        $f_ext = trim(preg_replace('|^.*/([^/; ]*)(.*)?$|', '$1', $meta_value));
     96        if (!strlen($f_ext))
     97          $f_ext = trim(preg_replace('/^Content-Type:/i', '', $meta_value));
     98      } elseif (preg_match('/^Content-Length:/i', $meta_value)) {
     99          $f_size = trim(preg_replace('/^Content-Length:/i', '', $meta_value));
     100      } elseif (strlen($f_ext) && strlen($f_size)) {
     101        break;
     102      }
     103    }
     104  }
     105
     106  if (!strlen($f_ext))
     107    $f_ext = $pathinfo['extension'];
     108
     109  if (!strlen($f_ext))
     110    die("Could not determine content type for ($url)");
     111
     112  $f_name .= ".$f_ext";
    78113
    79114  if (!strlen($titre))
    80115    $titre = $f_name;
    81116
    82   $f_ext  = strtolower(preg_replace('/^.*\.([^\.]*)$/', '$1', $f_name));
    83 
    84117  if (_HTML2SPIP_PRESERVE_DISTANT) {
    85     $f_size = @filesize($url) ;
    86     $fichier = $url ;
     118    if ($url_hdl) {
     119      fclose($url_hdl);
     120      if (!strlen($f_size))
     121        $f_size = @filesize($url); # Worth trying: we could open file, but not
     122                                   # get metadata
     123    }
     124    if (!strlen($f_size))
     125      $f_size = 0;
     126    $fichier = $url;
    87127  } else {
     128    @mkdir($path . '/' . $f_ext);
    88129    $f_abs_name = $path . '/' . $f_ext . '/' . $f_name;
    89130    if (!($f_hdl = fopen($f_abs_name, 'w')))
    90       die("Cannot open file ($f_abs_name)");
    91 
    92     if (!($url_hdl = fopen($url, 'r')))
    93       die("Cannot open url ($url)");
     131      die("Cannot open file for writing ($f_abs_name)");
    94132
    95133    $f_size = 0;
     
    101139      die("Failed fclose on file ($f_abs_name)");
    102140
    103     if (!(fclose($url_hdl)))
    104       die("Failed fclose on url ($url_hdl)"); # We could safely ignore that
     141    fclose($url_hdl);
    105142
    106143    $fichier = $f_ext . '/' . $f_name ;
Note: See TracChangeset for help on using the changeset viewer.