$value.
* The available keys are:
* - name: the filename, as it is now stored on the system.
* - size: the file size
* - path: the absolute path, where the file has been stored.
* - fullName: the canonical file name (i.e. including the absolute path)
* - md5sum: the md5sum of the file, if further control is needed.
* - mimetype: the calculated mime type of the file
* - If the formData applet parameter is used: all attributes (key and value) uploaded by the applet, are put here,
* repeated for each file.
*
* Note: if you are using a callback function (i.e. callbackAfterUploadManagement) and you do not see a global 'object' you
* are expecting then it might have been destroyed by PHP - c.f. http://bugs.php.net/bug.php?id=39693
*
*/
class JUpload {
var $appletparams;
var $classparams;
var $files;
public function JUpload($appletparams = array(), $classparams = array()) {
if (gettype($classparams) != 'array')
$this->abort('Invalid type of parameter classparams: Expecting an array');
if (gettype($appletparams) != 'array')
$this->abort('Invalid type of parameter appletparams: Expecting an array');
// set some defaults for the applet params
if (!isset($appletparams['afterUploadURL']))
$appletparams['afterUploadURL'] = $_SERVER['PHP_SELF'] . '?afterupload=1';
if (!isset($appletparams['name']))
$appletparams['name'] = 'JUpload';
if (!isset($appletparams['archive']))
$appletparams['archive'] = 'wjhk.jupload.jar';
if (!isset($appletparams['code']))
$appletparams['code'] = 'wjhk.jupload2.JUploadApplet';
if (!isset($appletparams['debugLevel']))
$appletparams['debugLevel'] = 0;
if (!isset($appletparams['httpUploadParameterType']))
$appletparams['httpUploadParameterType'] = 'array';
if (!isset($appletparams['showLogWindow']))
$appletparams['showLogWindow'] = ($appletparams['debugLevel'] > 0) ? 'true' : 'false';
if (!isset($appletparams['width']))
$appletparams['width'] = 640;
if (!isset($appletparams['height']))
$appletparams['height'] = ($appletparams['showLogWindow'] == 'true') ? 500 : 300;
if (!isset($appletparams['mayscript']))
$appletparams['mayscript'] = 'true';
if (!isset($appletparams['scriptable']))
$appletparams['scriptable'] = 'false';
//if (!isset($appletparams['stringUploadSuccess']))
$appletparams['stringUploadSuccess'] = 'SUCCESS';
//if (!isset($appletparams['stringUploadError']))
$appletparams['stringUploadError'] = 'ERROR: (.*)';
$maxpost = $this->tobytes(ini_get('post_max_size'));
$maxmem = $this->tobytes(ini_get('memory_limit'));
$maxfs = $this->tobytes(ini_get('upload_max_filesize'));
$obd = ini_get('open_basedir');
if (!isset($appletparams['maxChunkSize'])) {
$maxchunk = ($maxpost < $maxmem) ? $maxpost : $maxmem;
$maxchunk = ($maxchunk < $maxfs) ? $maxchunk : $maxfs;
$maxchunk /= 4;
$optchunk = (500000 > $maxchunk) ? $maxchunk : 500000;
$appletparams['maxChunkSize'] = $optchunk;
}
$appletparams['maxChunkSize'] = $this->tobytes($appletparams['maxChunkSize']);
if (!isset($appletparams['maxFileSize']))
$appletparams['maxFileSize'] = $maxfs;
$appletparams['maxFileSize'] = $this->tobytes($appletparams['maxFileSize']);
if (isset($classparams['errormail'])) {
$appletparams['urlToSendErrorTo'] = $_SERVER["PHP_SELF"] . '?errormail';
}
// Same for class parameters
if (!isset($classparams['demo_mode']))
$classparams['demo_mode'] = false;
if ($classparams['demo_mode']) {
$classparams['create_destdir'] = false;
$classparams['allow_subdirs'] = true;
$classparams['allow_zerosized'] = true;
$classparams['duplicate'] = 'overwrite';
}
if (!isset($classparams['debug_php'])) // set true to log some messages in PHP log
$classparams['debug_php'] = false;
if (!isset($this->classparams['allowed_mime_types'])) // array of allowed MIME type
$classparams['allowed_mime_types'] = 'all';
if (!isset($this->classparams['allowed_file_extensions'])) // array of allowed file extensions
$classparams['allowed_file_extensions'] = 'all';
if (!isset($classparams['verbose_errors'])) // shouldn't display server info on a production site!
$classparams['verbose_errors'] = true;
if (!isset($classparams['session_regenerate']))
$classparams['session_regenerate'] = false;
if (!isset($classparams['create_destdir']))
$classparams['create_destdir'] = true;
if (!isset($classparams['allow_subdirs']))
$classparams['allow_subdirs'] = false;
if (!isset($classparams['spaces_in_subdirs']))
$classparams['spaces_in_subdirs'] = false;
if (!isset($classparams['convert_spaces'])) // set to true to convert spaces in filenames to _
$classparams['convert_spaces'] = false;
if (!isset($classparams['allow_zerosized']))
$classparams['allow_zerosized'] = false;
if (!isset($classparams['duplicate']))
$classparams['duplicate'] = 'rename';
if (!isset($classparams['dirperm']))
$classparams['dirperm'] = 0755;
if (!isset($classparams['fileperm']))
$classparams['fileperm'] = 0644;
if (!isset($classparams['destdir'])) {
if ($obd != '')
$classparams['destdir'] = $obd;
else
$classparams['destdir'] = '/var/tmp/jupload_test';
}else{
$classparams['destdir']=str_replace('~',' ',$classparams['destdir']);
}
if ($classparams['create_destdir']) {
$_umask = umask(0); // override the system mask
@mkdir($classparams['destdir'], $classparams['dirperm']);
umask($_umask);
}
if (!is_dir($classparams['destdir']) && is_writable($classparams['destdir']))
$this->abort('Destination dir not accessible');
if (!isset($classparams['tmp_prefix']))
$classparams['tmp_prefix'] = 'jutmp.';
if (!isset($classparams['var_prefix']))
$classparams['var_prefix'] = 'juvar.';
if (!isset($classparams['jscript_wrapper']))
$classparams['jscript_wrapper'] = 'JUploadSetProperty';
if (!isset($classparams['tag_jscript']))
$classparams['tag_jscript'] = '';
if (!isset($classparams['tag_applet']))
$classparams['tag_applet'] = '';
if (!isset($classparams['tag_flist']))
$classparams['tag_flist'] = '';
if (!isset($classparams['http_flist_start']))
$classparams['http_flist_start'] =
"
Filename
file size
Relative path
Full name
md5sum
Specific parameters
";
if (!isset($classparams['http_flist_end']))
$classparams['http_flist_end'] = "
\n";
if (!isset($classparams['http_flist_file_before']))
$classparams['http_flist_file_before'] = "
";
if (!isset($classparams['http_flist_file_between']))
$classparams['http_flist_file_between'] = "
";
if (!isset($classparams['http_flist_file_after']))
$classparams['http_flist_file_after'] = "
\n";
$this->appletparams = $appletparams;
$this->classparams = $classparams;
$this->page_start();
}
/**
* Return an array of uploaded files * The array contains: name, size, tmp_name, error,
* relativePath, md5sum, mimetype, fullName, path
*/
public function uploadedfiles() {
return $this->files;
}
/**
* Log a message on the current output, as a HTML comment.
*/
protected function logDebug($function, $msg, $htmlComment=true) {
$output = "[DEBUG] [$function] $msg";
if ($htmlComment) {
echo("\r\n");
} else {
echo("$output\r\n");
}
}
/**
* Log a message to the PHP log.
* Declared "protected" so it may be Extended if you require customised logging (e.g. particular log file location).
*/
protected function logPHPDebug($function, $msg) {
if ($this->classparams['debug_php'] === true) {
$output = "[DEBUG] [$function] ".$this->arrayexpand($msg);
error_log($output);
}
}
private function arrayexpand($array) {
$output = '';
if (is_array($array)) {
foreach ($array as $key => $value) {
$output .= "\n ".$key.' => '.$this->arrayexpand($value);
}
} else {
$output .= $array;
}
return $output;
}
/**
* Convert a value ending in 'G','M' or 'K' to bytes
*
*/
private function tobytes($val) {
$val = trim($val);
$last = fix_strtolower($val{strlen($val)-1});
switch($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
/**
* Build a string, containing a javascript wrapper function
* for setting applet properties via JavaScript. This is necessary,
* because we use the "modern" method of including the applet (using
*