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

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

Version 0.5

File size: 4.1 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
66function spip_add_document($url, $width, $height, $mode, $titre, $descriptif, $path) {
67  $f_name = iconv('UTF-8', 'ISO-8859-1', preg_replace(
68    '/\s+/',
69    '_',
70    urldecode(
71      preg_replace(
72        '|^.*/([^/]*)$|',
73        '$1',
74        $url
75      )
76    )
77  ));
78
79  if (!strlen($titre))
80    $titre = $f_name;
81
82  $f_ext  = strtolower(preg_replace('/^.*\.([^\.]*)$/', '$1', $f_name));
83
84  if (_HTML2SPIP_PRESERVE_DISTANT) {
85    $f_size = @filesize($url) ;
86    $fichier = $url ;
87  } else {
88    $f_abs_name = $path . '/' . $f_ext . '/' . $f_name;
89    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)");
94
95    $f_size = 0;
96    while (!feof($url_hdl))
97      if (!($f_size += fwrite($f_hdl, fread($url_hdl, 8192))))
98        die("Failed copy from ($url) to ($f_abs_name)");
99
100    if (!(fclose($f_hdl)))
101      die("Failed fclose on file ($f_abs_name)");
102
103    if (!(fclose($url_hdl)))
104      die("Failed fclose on url ($url_hdl)"); # We could safely ignore that
105
106    $fichier = $f_ext . '/' . $f_name ;
107  }
108
109  if (!($result = sql_select(
110                    'MAX(id_document)',
111                    'spip_documents',
112                    'fichier = ' . sql_quote($fichier)
113                    )))
114    die("Failed SQL query: ". sql_error());
115
116  if (($row = sql_fetch($result)) && ((int) $row['MAX(id_document)']) > 0)
117    return $row['MAX(id_document)'];
118
119  $date = strftime('%F %T');
120
121  $doc = array(
122    'id_vignette' =>  0,
123    'extension'   =>  $f_ext,
124    'titre'       =>  $titre,
125    'date'        =>  $date,
126    'descriptif'  =>  $descriptif,
127    'fichier'     =>  $fichier,
128    'taille'      =>  (int) $f_size,
129    'largeur'     =>  (int) $width,
130    'hauteur'     =>  (int) $height,
131    'mode'        =>  $mode,
132    'distant'     =>  (_HTML2SPIP_PRESERVE_DISTANT ? "oui" : "non"),
133    'maj'         =>  $date,
134  );
135
136  if (!($id = sql_insertq('spip_documents', $doc)))
137    die("Failed SQL query: ". sql_error());
138
139  return $id;
140}
141
142?>
Note: See TracBrowser for help on using the repository browser.