61 public function JUpload($appletparams = array(), $classparams = array()) {
62 if (gettype($classparams) !=
'array')
63 $this->
abort(
'Invalid type of parameter classparams: Expecting an array');
64 if (gettype($appletparams) !=
'array')
65 $this->
abort(
'Invalid type of parameter appletparams: Expecting an array');
68 if (!isset($appletparams[
'afterUploadURL']))
69 $appletparams[
'afterUploadURL'] =
$_SERVER[
'PHP_SELF'] .
'?afterupload=1';
70 if (!isset($appletparams[
'name']))
71 $appletparams[
'name'] =
'JUpload';
72 if (!isset($appletparams[
'archive']))
73 $appletparams[
'archive'] =
'wjhk.jupload.jar';
74 if (!isset($appletparams[
'code']))
75 $appletparams[
'code'] =
'wjhk.jupload2.JUploadApplet';
76 if (!isset($appletparams[
'debugLevel']))
77 $appletparams[
'debugLevel'] = 0;
78 if (!isset($appletparams[
'httpUploadParameterType']))
79 $appletparams[
'httpUploadParameterType'] =
'array';
80 if (!isset($appletparams[
'showLogWindow']))
81 $appletparams[
'showLogWindow'] = ($appletparams[
'debugLevel'] > 0) ?
'true' :
'false';
82 if (!isset($appletparams[
'width']))
83 $appletparams[
'width'] = 640;
84 if (!isset($appletparams[
'height']))
85 $appletparams[
'height'] = ($appletparams[
'showLogWindow'] ==
'true') ? 500 : 300;
86 if (!isset($appletparams[
'mayscript']))
87 $appletparams[
'mayscript'] =
'true';
88 if (!isset($appletparams[
'scriptable']))
89 $appletparams[
'scriptable'] =
'false';
91 $appletparams[
'stringUploadSuccess'] =
'SUCCESS';
93 $appletparams[
'stringUploadError'] =
'ERROR: (.*)';
94 $maxpost = $this->
tobytes(ini_get(
'post_max_size'));
95 $maxmem = $this->
tobytes(ini_get(
'memory_limit'));
96 $maxfs = $this->
tobytes(ini_get(
'upload_max_filesize'));
97 $obd = ini_get(
'open_basedir');
98 if (!isset($appletparams[
'maxChunkSize'])) {
99 $maxchunk = ($maxpost < $maxmem) ? $maxpost : $maxmem;
100 $maxchunk = ($maxchunk < $maxfs) ? $maxchunk : $maxfs;
102 $optchunk = (500000 > $maxchunk) ? $maxchunk : 500000;
103 $appletparams[
'maxChunkSize'] = $optchunk;
105 $appletparams[
'maxChunkSize'] = $this->
tobytes($appletparams[
'maxChunkSize']);
106 if (!isset($appletparams[
'maxFileSize']))
107 $appletparams[
'maxFileSize'] = $maxfs;
108 $appletparams[
'maxFileSize'] = $this->
tobytes($appletparams[
'maxFileSize']);
109 if (isset($classparams[
'errormail'])) {
110 $appletparams[
'urlToSendErrorTo'] =
$_SERVER[
"PHP_SELF"] .
'?errormail';
114 if (!isset($classparams[
'demo_mode']))
115 $classparams[
'demo_mode'] =
false;
116 if ($classparams[
'demo_mode']) {
117 $classparams[
'create_destdir'] =
false;
118 $classparams[
'allow_subdirs'] =
true;
119 $classparams[
'allow_zerosized'] =
true;
120 $classparams[
'duplicate'] =
'overwrite';
122 if (!isset($classparams[
'debug_php']))
123 $classparams[
'debug_php'] =
false;
124 if (!isset($this->classparams[
'allowed_mime_types']))
125 $classparams[
'allowed_mime_types'] =
'all';
126 if (!isset($this->classparams[
'allowed_file_extensions']))
127 $classparams[
'allowed_file_extensions'] =
'all';
128 if (!isset($classparams[
'verbose_errors']))
129 $classparams[
'verbose_errors'] =
true;
130 if (!isset($classparams[
'session_regenerate']))
131 $classparams[
'session_regenerate'] =
false;
132 if (!isset($classparams[
'create_destdir']))
133 $classparams[
'create_destdir'] =
true;
134 if (!isset($classparams[
'allow_subdirs']))
135 $classparams[
'allow_subdirs'] =
false;
136 if (!isset($classparams[
'spaces_in_subdirs']))
137 $classparams[
'spaces_in_subdirs'] =
false;
138 if (!isset($classparams[
'allow_zerosized']))
139 $classparams[
'allow_zerosized'] =
false;
140 if (!isset($classparams[
'duplicate']))
141 $classparams[
'duplicate'] =
'rename';
142 if (!isset($classparams[
'dirperm']))
143 $classparams[
'dirperm'] = 0755;
144 if (!isset($classparams[
'fileperm']))
145 $classparams[
'fileperm'] = 0644;
146 if (!isset($classparams[
'destdir'])) {
148 $classparams[
'destdir'] = $obd;
150 $classparams[
'destdir'] =
'/var/tmp/jupload_test';
152 $classparams[
'destdir']=str_replace(
'~',
' ',$classparams[
'destdir']);
154 if ($classparams[
'create_destdir']) {
156 @mkdir($classparams[
'destdir'], $classparams[
'dirperm']);
159 if (!is_dir($classparams[
'destdir']) && is_writable($classparams[
'destdir']))
160 $this->
abort(
'Destination dir not accessible');
161 if (!isset($classparams[
'tmp_prefix']))
162 $classparams[
'tmp_prefix'] =
'jutmp.';
163 if (!isset($classparams[
'var_prefix']))
164 $classparams[
'var_prefix'] =
'juvar.';
165 if (!isset($classparams[
'jscript_wrapper']))
166 $classparams[
'jscript_wrapper'] =
'JUploadSetProperty';
167 if (!isset($classparams[
'tag_jscript']))
168 $classparams[
'tag_jscript'] =
'<!--JUPLOAD_JSCRIPT-->';
169 if (!isset($classparams[
'tag_applet']))
170 $classparams[
'tag_applet'] =
'<!--JUPLOAD_APPLET-->';
171 if (!isset($classparams[
'tag_flist']))
172 $classparams[
'tag_flist'] =
'<!--JUPLOAD_FILES-->';
173 if (!isset($classparams[
'http_flist_start']))
174 $classparams[
'http_flist_start'] =
175 "<table border='1'><TR><TH>Filename</TH><TH>file size</TH><TH>Relative path</TH><TH>Full name</TH><TH>md5sum</TH><TH>Specific parameters</TH></TR>";
176 if (!isset($classparams[
'http_flist_end']))
177 $classparams[
'http_flist_end'] =
"</table>\n";
178 if (!isset($classparams[
'http_flist_file_before']))
179 $classparams[
'http_flist_file_before'] =
"<tr><td>";
180 if (!isset($classparams[
'http_flist_file_between']))
181 $classparams[
'http_flist_file_between'] =
"</td><td>";
182 if (!isset($classparams[
'http_flist_file_after']))
183 $classparams[
'http_flist_file_after'] =
"</td></tr>\n";
202 $output =
"[DEBUG] [$function] $msg";
204 echo(
"<!-- $output -->\r\n");
215 if ($this->classparams[
'debug_php'] ===
true) {
216 $output =
"[DEBUG] [$function] ".$this->arrayexpand(
$msg);
223 if (is_array($array)) {
224 foreach ($array as $key => $value) {
225 $output .=
"\n ".$key.
' => '.$this->
arrayexpand($value);
265 $name = $this->appletparams[
'name'];
266 $ret =
'<script type="text/javascript">'.$N;
268 $ret .=
'function '.$this->classparams[
'jscript_wrapper'].
'(name, value) {'.$N;
269 $ret .=
' document.applets["'.$name.
'"] == null || document.applets["'.
$name.
'"].setProperty(name,value);'.$N;
270 $ret .=
' document.embeds["'.$name.
'"] == null || document.embeds["'.
$name.
'"].setProperty(name,value);'.$N;
286 $ret =
'<object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"'.$N;
287 $ret .=
' codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,3"'.$N;
288 $ret .=
' width = "'.$params[
'width'].
'"'.$N;
289 $ret .=
' height = "'.$params[
'height'].
'"'.$N;
290 $ret .=
' name = "'.$params[
'name'].
'">'.$N;
291 foreach ($params as $key => $val) {
292 if ($key !=
'width' && $key !=
'height')
293 $ret .=
' <param name = "'.$key.
'" value = "'.$val.
'" />'.$N;
295 $ret .=
' <comment>'.$N;
296 $ret .=
' <embed'.$N;
297 $ret .=
' type = "application/x-java-applet;version=1.5"'.$N;
298 foreach ($params as $key => $val)
299 $ret .=
' '.$key.
' = "'.$val.
'"'.$N;
300 $ret .=
' pluginspage = "http://java.sun.com/products/plugin/index.html#download">'.$N;
301 $ret .=
' <noembed>'.$N;
302 $ret .=
' Java 1.5 or higher plugin required.'.$N;
303 $ret .=
' </noembed>'.$N;
304 $ret .=
' </embed>'.$N;
305 $ret .=
' </comment>'.$N;
313 die(str_replace(
'(.*)',
$msg,$this->appletparams[
'stringUploadError']).
"\n");
320 echo(
'WARNING: '.
$msg.
"\n");
321 echo $this->appletparams[
'stringUploadSuccess'].
"\n";
327 if (isset($_FILES)) {
328 foreach ($_FILES as $key => $val)
329 @unlink($val[
'tmp_name']);
332 @unlink($this->classparams[
'destdir'].
'/'.$this->classparams[
'tmp_prefix'].session_id());
333 @unlink($this->classparams[
'destdir'].
'/'.$this->classparams[
'tmp_prefix'].
'tmp'.session_id());
335 $_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'size'] = 0;
341 $dirs = explode(
'/',
$path);
342 $path = $this->classparams[
'destdir'];
343 foreach ($dirs as
$dir) {
345 if (!file_exists(
$path)) {
347 @mkdir(
$path, $this->classparams[
'dirperm']);
352 $this->
abort(
'Destination dir not accessible');
365 $name = preg_replace(
'![`$\\\\/|]!',
'_',
$name);
366 if ($this->classparams[
'allow_subdirs'] && (
$subdir !=
'')) {
369 if (!$this->classparams[
'spaces_in_subdirs']) {
373 if (!$this->classparams[
'demo_mode'])
380 $ret = $this->classparams[
'destdir'].
'/'.
$subdir.$name;
381 if (file_exists($ret)) {
382 if ($this->classparams[
'duplicate'] ==
'overwrite') {
385 if ($this->classparams[
'duplicate'] ==
'reject') {
386 $this->
abort(
'A file with the same name already exists');
388 if ($this->classparams[
'duplicate'] ==
'warning') {
389 $this->
warning(
"File $name already exists - rejected");
391 if ($this->classparams[
'duplicate'] ==
'rename') {
396 $nameWithoutExtension = substr(
$name, 0, strlen(
$name) - strlen(
$ext));
399 $nameWithoutExtension =
$name;
402 $rtry =
$dir.$nameWithoutExtension.
'_'.$cnt.$ext;
403 while (file_exists($rtry)) {
405 $rtry =
$dir.$nameWithoutExtension.
'._'.$cnt.$ext;
408 $name = $nameWithoutExtension.
'_'.$cnt.$ext;
420 $flist =
'[defaultAfterUploadManagement] Nb uploaded files is: ' .
sizeof(
$this->files);
421 $flist = $this->classparams[
'http_flist_start'];
422 foreach ($this->files as $f) {
424 $this->
logDebug(
'defaultAfterUploadManagement',
" Reading file ${f['name']}");
425 $flist .= $this->classparams[
'http_flist_file_before'];
426 $flist .= $f[
'name'];
427 $flist .= $this->classparams[
'http_flist_file_between'];
428 $flist .= $f[
'size'];
429 $flist .= $this->classparams[
'http_flist_file_between'];
430 $flist .= $f[
'relativePath'];
431 $flist .= $this->classparams[
'http_flist_file_between'];
432 $flist .= $f[
'fullName'];
433 $flist .= $this->classparams[
'http_flist_file_between'];
434 $flist .= $f[
'md5sum'];
436 foreach ($f as $key=>$value) {
438 if ($key !=
'name' && $key !=
'size' && $key !=
'relativePath' && $key !=
'fullName' && $key !=
'md5sum') {
443 $flist .= $this->classparams[
'http_flist_file_between'];
446 $flist .=
"$key => $value";
449 $flist .= $this->classparams[
'http_flist_file_after'];
451 $flist .= $this->classparams[
'http_flist_end'];
462 $this->
logDebug(
'generateAppletTag',
'Entering function');
463 $str = preg_replace(
'/'.$this->classparams[
'tag_jscript'].
'/', $this->
str_jsinit(), $str);
464 return preg_replace(
'/'.$this->classparams[
'tag_applet'].
'/', $this->
str_applet(), $str);
474 $this->
logDebug(
'interceptBeforeUpload',
'Entering function');
484 $this->
logDebug(
'interceptAfterUpload',
'Entering function');
485 $this->
logPHPDebug(
'interceptAfterUpload', $this->files);
487 if (count($this->files) > 0) {
488 if (isset($this->classparams[
'callbackAfterUploadManagement'])) {
489 $this->
logDebug(
'interceptAfterUpload',
'Before call of ' .$this->classparams[
'callbackAfterUploadManagement']);
490 $strForFListContent = call_user_func($this->classparams[
'callbackAfterUploadManagement'], $this, $this->files);
494 $str = preg_replace(
'/'.$this->classparams[
'tag_flist'].
'/', $strForFListContent, $str);
504 if (isset($_POST[
'description']) && isset($_POST[
'log'])) {
505 $msg = $_POST[
'log'];
506 mail($this->classparams[
'errormail'], $_POST[
'description'],
$msg);
508 if (isset(
$_SERVER[
'SERVER_ADMIN']))
509 mail(
$_SERVER[
'SERVER_ADMIN'],
'Empty jupload error log',
510 'An empty log has just been posted.');
511 $this->
logPHPDebug(
'receive_debug_log',
'Empty error log received');
525 $this->
logDebug(
'receive_uploaded_files',
'Entering POST management');
527 if (session_id() ==
'') {
532 if (!isset(
$_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'size'])) {
533 $this->
abort(
'Invalid session (in afterupload, POST, check of size)');
535 if (!isset(
$_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'files'])) {
536 $this->
abort(
'Invalid session (in afterupload, POST, check of files)');
538 $this->files =
$_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'files'];
539 if (!is_array($this->files)) {
540 $this->
abort(
'Invalid session (in afterupload, POST, is_array(files))');
542 if ($this->appletparams[
'sendMD5Sum'] ==
'true' && !isset($_POST[
'md5sum'])) {
543 $this->
abort(
'Required POST variable md5sum is missing');
546 foreach ($_FILES as $key => $value) {
548 if (isset($files_data)) {
551 $jupart = (isset($_POST[
'jupart'])) ? (
int)$_POST[
'jupart'] : 0;
552 $jufinal = (isset($_POST[
'jufinal'])) ? (
int)$_POST[
'jufinal'] : 1;
553 $relpaths = (isset($_POST[
'relpathinfo'])) ? $_POST[
'relpathinfo'] : null;
554 $md5sums = (isset($_POST[
'md5sum'])) ? $_POST[
'md5sum'] : null;
555 $mimetypes = (isset($_POST[
'mimetype'])) ? $_POST[
'mimetype'] : null;
559 if (gettype($relpaths) ==
'string') {
560 $relpaths = array($relpaths);
562 if (gettype($md5sums) ==
'string') {
563 $md5sums = array($md5sums);
565 if ($this->appletparams[
'sendMD5Sum'] ==
'true' && !is_array($md5sums)) {
566 $this->
abort(
'Expecting an array of MD5 checksums');
568 if (!is_array($relpaths)) {
569 $this->
abort(
'Expecting an array of relative paths');
572 $this->
abort(
'Expecting an array of MIME types');
575 if (isset($this->classparams[
'allowed_mime_types']) && is_array($this->classparams[
'allowed_mime_types'])) {
576 if (!in_array(
$mimetypes[$cnt], $this->classparams[
'allowed_mime_types'])) {
580 if (isset($this->classparams[
'allowed_file_extensions']) && is_array($this->classparams[
'allowed_file_extensions'])) {
581 $fileExtension = substr(strrchr($value[
'name'][$cnt],
"."), 1);
582 if (!in_array($fileExtension, $this->classparams[
'allowed_file_extensions'])) {
583 $this->
abort(
'File extension '.$fileExtension.
' not allowed');
587 $dstdir = $this->classparams[
'destdir'];
588 $dstname = $dstdir.
'/'.$this->classparams[
'tmp_prefix'].session_id();
589 $tmpname = $dstdir.
'/'.$this->classparams[
'tmp_prefix'].
'tmp'.session_id();
592 $files_data[
'name'] = $value[
'name'][$cnt];
593 $files_data[
'size'] =
'not calculated yet';
594 $files_data[
'tmp_name'] = $value[
'tmp_name'][$cnt];
595 $files_data[
'error'] = $value[
'error'][$cnt];
596 $files_data[
'relativePath'] = $relpaths[$cnt];
597 $files_data[
'md5sum'] = $md5sums[$cnt];
600 if (!move_uploaded_file($files_data[
'tmp_name'], $tmpname)) {
601 if ($classparams[
'verbose_errors']) {
602 $this->
abort(
"Unable to move uploaded file (from ${files_data['tmp_name']} to $tmpname)");
604 trigger_error(
"Unable to move uploaded file (from ${files_data['tmp_name']} to $tmpname)",E_USER_WARNING);
605 $this->
abort(
"Unable to move uploaded file");
610 if ($this->classparams[
'demo_mode']) {
611 if ($jufinal || (!$jupart)) {
613 $files_data[
'size'] = ($jupart-1) * $this->appletparams[
'maxChunkSize'] + filesize($tmpname);
615 $files_data[
'size'] = filesize($tmpname);
617 $files_data[
'fullName'] =
'Demo mode<BR>No file storing';
618 array_push($this->files, $files_data);
627 $len = filesize($tmpname);
628 $_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'size'] += $len;
630 $src = fopen($tmpname,
'rb');
631 $dst = fopen($dstname, ($jupart == 1) ?
'wb' :
'ab');
633 $rlen = ($len > 8192) ? 8192 : $len;
634 $buf = fread($src, $rlen);
639 $this->
abort(
'read IO error');
641 if (!fwrite($dst, $buf, $rlen)) {
645 $this->
abort(
'write IO error');
656 $dlen = filesize($dstname);
657 if ($dlen !=
$_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'size'])
658 $this->
abort(
'file size mismatch');
659 if ($this->appletparams[
'sendMD5Sum'] ==
'true' ) {
660 if ($md5sums[$cnt] != md5_file($dstname))
661 $this->
abort(
'MD5 checksum mismatch');
664 if (($dlen > 0) || $this->classparams[
'allow_zerosized']) {
665 $dstfinal = $this->
dstfinal($files_data[
'name'],$files_data[
'relativePath']);
666 if (!rename($dstname, $dstfinal))
667 $this->
abort(
'rename IO error');
669 if (!chmod($dstfinal, $this->classparams[
'fileperm']))
670 $this->
abort(
'chmod IO error');
672 $files_data[
'size'] = filesize($dstfinal);
673 $files_data[
'fullName'] = $dstfinal;
675 array_push($this->files, $files_data);
680 $_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'size'] = 0;
684 if ($this->appletparams[
'sendMD5Sum'] ==
'true' ) {
685 if ($md5sums[$cnt] != md5_file($tmpname))
686 $this->
abort(
'MD5 checksum mismatch');
688 $dstfinal = $this->
dstfinal($files_data[
'name'],$files_data[
'relativePath']);
689 if (!rename($tmpname, $dstfinal))
690 $this->
abort(
'rename IO error');
692 if (!chmod($dstfinal, $this->classparams[
'fileperm']))
693 $this->
abort(
'chmod IO error');
695 $files_data[
'size'] = filesize($dstfinal);
696 $files_data[
'fullName'] = $dstfinal;
698 array_push($this->files, $files_data);
703 echo $this->appletparams[
'stringUploadSuccess'].
"\n";
705 session_write_close();
714 $this->
logDebug(
'page_start',
'Entering function');
718 if (
$_SERVER[
'REQUEST_METHOD'] ==
'HEAD') {
721 }
else if (
$_SERVER[
'REQUEST_METHOD'] ==
'GET') {
723 $this->
logDebug(
'page_start',
'Entering GET management');
725 if (session_id() ==
'') {
728 if (isset($_GET[
'afterupload'])) {
729 $this->
logDebug(
'page_start',
'afterupload is set');
730 if (!isset(
$_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'files'])) {
731 $this->
abort(
'Invalid session (in afterupload, GET, check of $_SESSION["RF"]): files array is not set');
733 $this->files =
$_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'files'];
734 if (!is_array($this->files)) {
735 $this->
abort(
'Invalid session (in afterupload, GET, check of is_array(files)): files is not an array');
738 $_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'files'] = array();
741 ob_start(array(& $this,
'interceptAfterUpload'));
744 $this->
logDebug(
'page_start',
'afterupload is not set');
745 if ($this->classparams[
'session_regenerate']) {
746 session_regenerate_id(
true);
748 $this->files = array();
749 $_SESSION[
'RF'][$this->classparams[
'var_prefix'].
'size'] = 0;
752 ob_start(array(& $this,
'interceptBeforeUpload'));
755 }
else if (
$_SERVER[
'REQUEST_METHOD'] ==
'POST') {
757 if (isset($_GET[
'errormail'])) {
$_SESSION['RF']["verify"]
JUpload($appletparams=array(), $classparams=array())
dstfinal(&$name, &$subdir)
interceptAfterUpload($str)
logDebug($function, $msg, $htmlComment=true)
if(!is_object($module)||!$module->getVar('isactive')) $msg
interceptBeforeUpload($str)
defaultAfterUploadManagement()
logPHPDebug($function, $msg)