source: spip-zone/_libs_/html2spip/misc_tools.php @ 88970

Last change on this file since 88970 was 88970, checked in by real3t@…, 5 years ago

Version 0.6 (finale)

File size: 5.4 KB
Line 
1<?php # vim: syntax=php tabstop=2 softtabstop=2 shiftwidth=2 expandtab textwidth=80 autoindent
2# Copyright (C) 2010  Jean-Jacques Puig
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17################################################################################
18#
19# Array functions
20#
21################################################################################
22
23function array_diff_key_value($keys, $values) {
24  $values_as_keys = array();
25
26  foreach($values as $value)
27    $values_as_keys[$value] = null;
28
29  return array_diff_key($keys, $values_as_keys);
30}
31
32function array_key_value_if_notEmpty($key, $array, $default_value = null) {
33  if ($array == null)
34    return $default_value;
35
36  if (array_key_exists($key, $array)
37      && ($array[$key] != ''))
38    return $array[$key];
39  else
40    return $default_value;
41}
42
43################################################################################
44#
45# Stored SQL Procedures functions
46#   (Now useless; declarations will be removed in the future)
47#
48################################################################################
49
50# spip_register_procedures($spip)
51# This function is declared for compatibility.
52# Will be removed in near future
53function spip_register_procedures($spip) {}
54
55# spip_unregister_procedures($spip)
56# This function is declared for compatibility.
57# Will be removed in near future
58function spip_unregister_procedures($spip) {}
59
60################################################################################
61#
62# Database related functions
63#
64################################################################################
65
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
74function spip_add_document($url, $width, $height, $mode, $titre, $descriptif, $path) {
75  $pathinfo = pathinfo(parse_url($url, PHP_URL_PATH));
76
77  $f_name = iconv('UTF-8', 'ISO-8859-1', preg_replace(
78    '/\s+/',
79    '_',
80    urldecode($pathinfo['filename'])
81  ));
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";
113
114  if (!strlen($titre))
115    $titre = $f_name;
116
117  if (_HTML2SPIP_PRESERVE_DISTANT) {
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;
127  } else {
128    @mkdir($path . '/' . $f_ext);
129    $f_abs_name = $path . '/' . $f_ext . '/' . $f_name;
130    if (!($f_hdl = fopen($f_abs_name, 'w')))
131      die("Cannot open file for writing ($f_abs_name)");
132
133    $f_size = 0;
134    while (!feof($url_hdl))
135      if (!($f_size += fwrite($f_hdl, fread($url_hdl, 8192))))
136        die("Failed copy from ($url) to ($f_abs_name)");
137
138    if (!(fclose($f_hdl)))
139      die("Failed fclose on file ($f_abs_name)");
140
141    fclose($url_hdl);
142
143    $fichier = $f_ext . '/' . $f_name ;
144  }
145
146  if (!($result = sql_select(
147                    'MAX(id_document)',
148                    'spip_documents',
149                    'fichier = ' . sql_quote($fichier)
150                    )))
151    die("Failed SQL query: ". sql_error());
152
153  if (($row = sql_fetch($result)) && ((int) $row['MAX(id_document)']) > 0)
154    return $row['MAX(id_document)'];
155
156  $date = strftime('%F %T');
157
158  $doc = array(
159    'id_vignette' =>  0,
160    'extension'   =>  $f_ext,
161    'titre'       =>  $titre,
162    'date'        =>  $date,
163    'descriptif'  =>  $descriptif,
164    'fichier'     =>  $fichier,
165    'taille'      =>  (int) $f_size,
166    'largeur'     =>  (int) $width,
167    'hauteur'     =>  (int) $height,
168    'mode'        =>  $mode,
169    'distant'     =>  (_HTML2SPIP_PRESERVE_DISTANT ? "oui" : "non"),
170    'maj'         =>  $date,
171  );
172
173  if (!($id = sql_insertq('spip_documents', $doc)))
174    die("Failed SQL query: ". sql_error());
175
176  return $id;
177}
178
179?>
Note: See TracBrowser for help on using the repository browser.