Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
17
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
18
// +----------------------------------------------------------------------+
19
//
20
// $Id: file.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
21
 
22
require_once("HTML/QuickForm/input.php");
23
 
24
// register file-related rules
25
if (class_exists('HTML_QuickForm')) {
26
    HTML_QuickForm::registerRule('uploadedfile', 'callback', '_ruleIsUploadedFile', 'HTML_QuickForm_file');
27
    HTML_QuickForm::registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file');
28
    HTML_QuickForm::registerRule('mimetype', 'callback', '_ruleCheckMimeType', 'HTML_QuickForm_file');
29
    HTML_QuickForm::registerRule('filename', 'callback', '_ruleCheckFileName', 'HTML_QuickForm_file');
30
}
31
 
32
/**
33
 * HTML class for a file type element
34
 *
35
 * @author       Adam Daniel <adaniel1@eesus.jnj.com>
36
 * @author       Bertrand Mansion <bmansion@mamasam.com>
37
 * @version      1.0
38
 * @since        PHP4.04pl1
39
 * @access       public
40
 */
41
class HTML_QuickForm_file extends HTML_QuickForm_input
42
{
43
    // {{{ properties
44
 
45
   /**
46
    * Uploaded file data, from $_FILES
47
    * @var array
48
    */
49
    var $_value = null;
50
 
51
    // }}}
52
    // {{{ constructor
53
 
54
    /**
55
     * Class constructor
56
     *
57
     * @param     string    Input field name attribute
58
     * @param     string    Input field label
59
     * @param     mixed     (optional)Either a typical HTML attribute string
60
     *                      or an associative array
61
     * @since     1.0
62
     * @access    public
63
     */
64
    function HTML_QuickForm_file($elementName=null, $elementLabel=null, $attributes=null)
65
    {
66
        HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
67
        $this->setType('file');
68
    } //end constructor
69
 
70
    // }}}
71
    // {{{ setSize()
72
 
73
    /**
74
     * Sets size of file element
75
     *
76
     * @param     int    Size of file element
77
     * @since     1.0
78
     * @access    public
79
     */
80
    function setSize($size)
81
    {
82
        $this->updateAttributes(array('size' => $size));
83
    } //end func setSize
84
 
85
    // }}}
86
    // {{{ getSize()
87
 
88
    /**
89
     * Returns size of file element
90
     *
91
     * @since     1.0
92
     * @access    public
93
     * @return    int
94
     */
95
    function getSize()
96
    {
97
        return $this->getAttribute('size');
98
    } //end func getSize
99
 
100
    // }}}
101
    // {{{ freeze()
102
 
103
    /**
104
     * Freeze the element so that only its value is returned
105
     *
106
     * @access    public
107
     * @return    bool
108
     */
109
    function freeze()
110
    {
111
        return false;
112
    } //end func freeze
113
 
114
    // }}}
115
    // {{{ setValue()
116
 
117
    /**
118
     * Sets value for file element.
119
     *
120
     * Actually this does nothing. The function is defined here to override
121
     * HTML_Quickform_input's behaviour of setting the 'value' attribute. As
122
     * no sane user-agent uses <input type="file">'s value for anything
123
     * (because of security implications) we implement file's value as a
124
     * read-only property with a special meaning.
125
     *
126
     * @param     mixed    Value for file element
127
     * @since     3.0
128
     * @access    public
129
     */
130
    function setValue($value)
131
    {
132
        return null;
133
    } //end func setValue
134
 
135
    // }}}
136
    // {{{ getValue()
137
 
138
    /**
139
     * Returns information about the uploaded file
140
     *
141
     * @since     3.0
142
     * @access    public
143
     * @return    array
144
     */
145
    function getValue()
146
    {
147
        return $this->_value;
148
    } // end func getValue
149
 
150
    // }}}
151
    // {{{ onQuickFormEvent()
152
 
153
    /**
154
     * Called by HTML_QuickForm whenever form event is made on this element
155
     *
156
     * @param     string    Name of event
157
     * @param     mixed     event arguments
158
     * @param     object    calling object
159
     * @since     1.0
160
     * @access    public
161
     * @return    bool
162
     */
163
    function onQuickFormEvent($event, $arg, &$caller)
164
    {
165
        switch ($event) {
166
            case 'updateValue':
167
                if ($caller->getAttribute('method') == 'get') {
168
                    return PEAR::raiseError('Cannot add a file upload field to a GET method form');
169
                }
170
                $this->_value = $this->_findValue();
171
                $caller->updateAttributes(array('enctype' => 'multipart/form-data'));
172
                $caller->setMaxFileSize();
173
                break;
174
            case 'addElement':
175
                $this->onQuickFormEvent('createElement', $arg, $caller);
176
                return $this->onQuickFormEvent('updateValue', null, $caller);
177
                break;
178
            case 'createElement':
179
                $className = get_class($this);
180
                $this->$className($arg[0], $arg[1], $arg[2]);
181
                break;
182
        }
183
        return true;
184
    } // end func onQuickFormEvent
185
 
186
    // }}}
187
    // {{{ moveUploadedFile()
188
 
189
    /**
190
     * Moves an uploaded file into the destination
191
     *
192
     * @param    string  Destination directory path
193
     * @param    string  New file name
194
     * @access   public
195
     */
196
    function moveUploadedFile($dest, $fileName = '')
197
    {
198
        if ($dest != ''  && substr($dest, -1) != '/') {
199
            $dest .= '/';
200
        }
201
        $fileName = ($fileName != '') ? $fileName : basename($this->_value['name']);
202
        if (move_uploaded_file($this->_value['tmp_name'], $dest . $fileName)) {
203
            return true;
204
        } else {
205
            return false;
206
        }
207
    } // end func moveUploadedFile
208
 
209
    // }}}
210
    // {{{ isUploadedFile()
211
 
212
    /**
213
     * Checks if the element contains an uploaded file
214
     *
215
     * @access    public
216
     * @return    bool      true if file has been uploaded, false otherwise
217
     */
218
    function isUploadedFile()
219
    {
220
        return $this->_ruleIsUploadedFile($this->_value);
221
    } // end func isUploadedFile
222
 
223
    // }}}
224
    // {{{ _ruleIsUploadedFile()
225
 
226
    /**
227
     * Checks if the given element contains an uploaded file
228
     *
229
     * @param     array     Uploaded file info (from $_FILES)
230
     * @access    private
231
     * @return    bool      true if file has been uploaded, false otherwise
232
     */
233
    function _ruleIsUploadedFile($elementValue)
234
    {
235
        if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
236
            (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) {
237
            return is_uploaded_file($elementValue['tmp_name']);
238
        } else {
239
            return false;
240
        }
241
    } // end func _ruleIsUploadedFile
242
 
243
    // }}}
244
    // {{{ _ruleCheckMaxFileSize()
245
 
246
    /**
247
     * Checks that the file does not exceed the max file size
248
     *
249
     * @param     array     Uploaded file info (from $_FILES)
250
     * @param     int       Max file size
251
     * @access    private
252
     * @return    bool      true if filesize is lower than maxsize, false otherwise
253
     */
254
    function _ruleCheckMaxFileSize($elementValue, $maxSize)
255
    {
256
        if (!empty($elementValue['error']) &&
257
            (UPLOAD_ERR_FORM_SIZE == $elementValue['error'] || UPLOAD_ERR_INI_SIZE == $elementValue['error'])) {
258
            return false;
259
        }
260
        if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
261
            return true;
262
        }
263
        return ($maxSize >= @filesize($elementValue['tmp_name']));
264
    } // end func _ruleCheckMaxFileSize
265
 
266
    // }}}
267
    // {{{ _ruleCheckMimeType()
268
 
269
    /**
270
     * Checks if the given element contains an uploaded file of the right mime type
271
     *
272
     * @param     array     Uploaded file info (from $_FILES)
273
     * @param     mixed     Mime Type (can be an array of allowed types)
274
     * @access    private
275
     * @return    bool      true if mimetype is correct, false otherwise
276
     */
277
    function _ruleCheckMimeType($elementValue, $mimeType)
278
    {
279
        if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
280
            return true;
281
        }
282
        if (is_array($mimeType)) {
283
            return in_array($elementValue['type'], $mimeType);
284
        }
285
        return $elementValue['type'] == $mimeType;
286
    } // end func _ruleCheckMimeType
287
 
288
    // }}}
289
    // {{{ _ruleCheckFileName()
290
 
291
    /**
292
     * Checks if the given element contains an uploaded file of the filename regex
293
     *
294
     * @param     array     Uploaded file info (from $_FILES)
295
     * @param     string    Regular expression
296
     * @access    private
297
     * @return    bool      true if name matches regex, false otherwise
298
     */
299
    function _ruleCheckFileName($elementValue, $regex)
300
    {
301
        if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
302
            return true;
303
        }
304
        return preg_match($regex, $elementValue['name']);
305
    } // end func _ruleCheckFileName
306
 
307
    // }}}
308
    // {{{ _findValue()
309
 
310
   /**
311
    * Tries to find the element value from the values array
312
    *
313
    * Needs to be redefined here as $_FILES is populated differently from
314
    * other arrays when element name is of the form foo[bar]
315
    *
316
    * @access    private
317
    * @return    mixed
318
    */
319
    function _findValue()
320
    {
321
        if (empty($_FILES)) {
322
            return null;
323
        }
324
        $elementName = $this->getName();
325
        if (isset($_FILES[$elementName])) {
326
            return $_FILES[$elementName];
327
        } elseif (false !== ($pos = strpos($elementName, '['))) {
328
            $base  = substr($elementName, 0, $pos);
329
            $idx   = "['" . str_replace(array(']', '['), array('', "']['"), substr($elementName, $pos + 1, -1)) . "']";
330
            $props = array('name', 'type', 'size', 'tmp_name', 'error');
331
            $code  = "if (!isset(\$_FILES['{$base}']['name']{$idx})) {\n" .
332
                     "    return null;\n" .
333
                     "} else {\n" .
334
                     "    \$value = array();\n";
335
            foreach ($props as $prop) {
336
                $code .= "    \$value['{$prop}'] = \$_FILES['{$base}']['{$prop}']{$idx};\n";
337
            }
338
            return eval($code . "    return \$value;\n}\n");
339
        } else {
340
            return null;
341
        }
342
    }
343
 
344
    // }}}
345
} // end class HTML_QuickForm_file
346
?>