Subversion Repositories Applications.papyrus

Rev

Rev 320 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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