From c2e1ddffae57a1f7b1687ae4594f5848bdcfe2d1 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Thu, 21 Jan 2010 11:49:33 -0700 Subject: [PATCH] Horde_Browser::wasFileUploaded() now throws an Exception --- ansel/image.php | 28 ++- ansel/img/upload.php | 283 +++++++++++----------- ansel/xppublish.php | 31 +-- framework/Browser/lib/Horde/Browser.php | 14 +- framework/Browser/lib/Horde/Browser/Exception.php | 16 ++ framework/Browser/package.xml | 5 +- framework/Data/Data.php | 7 +- framework/Data/Data/tsv.php | 7 +- framework/Form/Form/Type.php | 29 ++- framework/Model/lib/Horde/Form.php | 13 +- gollem/manager.php | 18 +- horde/services/problem.php | 12 +- imp/folders.php | 8 +- imp/lib/Compose.php | 7 +- imp/pgp.php | 11 +- imp/smime.php | 11 +- news/add.php | 12 +- wicked/lib/Page/AttachedFiles.php | 8 +- 18 files changed, 280 insertions(+), 240 deletions(-) create mode 100644 framework/Browser/lib/Horde/Browser/Exception.php diff --git a/ansel/image.php b/ansel/image.php index 78ef5d259..9142e934f 100644 --- a/ansel/image.php +++ b/ansel/image.php @@ -188,19 +188,21 @@ case 'save': if ($form->validate($vars)) { $form->getInfo($vars, $info); /* See if we were replacing photo */ - if (!empty($info['file0']['file']) && - !is_a(Horde_Browser::wasFileUploaded('file0'), 'PEAR_Error') && - filesize($info['file0']['file'])) { - - /* Read in the uploaded data. */ - $data = file_get_contents($info['file0']['file']); - - /* Try and make sure the image is in a recognizeable - * format. */ - if (getimagesize($info['file0']['file']) === false) { - $notification->push(_("The file you uploaded does not appear to be a valid photo."), 'horde.error'); - unset($data); - } + if (!empty($info['file0']['file'])) { + try { + Horde_Browser::wasFileUploaded('file0'); + if (filesize($info['file0']['file'])) { + /* Read in the uploaded data. */ + $data = file_get_contents($info['file0']['file']); + + /* Try and make sure the image is in a recognizeable + * format. */ + if (getimagesize($info['file0']['file']) === false) { + $notification->push(_("The file you uploaded does not appear to be a valid photo."), 'horde.error'); + unset($data); + } + } + } catch (Horde_Browser_Exception $e) {} } $image = &$ansel_storage->getImage($image_id); diff --git a/ansel/img/upload.php b/ansel/img/upload.php index dcf0186f3..7e1772963 100644 --- a/ansel/img/upload.php +++ b/ansel/img/upload.php @@ -36,168 +36,173 @@ if ($form->validate($vars)) { } /* Save new image. */ - if (!is_a(Horde_Browser::wasFileUploaded('file' . $i), 'PEAR_Error') && - filesize($info['file' . $i]['file'])) { - - /* Check for a compressed file. */ - if (in_array($info['file' . $i]['type'], - array('x-extension/zip', - 'application/x-compressed', - 'application/x-zip-compressed', - 'application/zip')) || - Horde_Mime_Magic::filenameToMime($info['file' . $i]['name']) == 'application/zip') { - - /* See if we can use the zip extension for reading the file. */ - if (Horde_Util::extensionExists('zip')) { - $zip = new ZipArchive(); - if ($zip->open($info['file' . $i]['file']) !== true) { - $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $info['file' . $i]['file']), 'horde.error'); - continue; - } - - for ($z = 0; $z < $zip->numFiles; $z++) { - $zinfo = $zip->statIndex($z); - - /* Skip some known metadata files. */ - $len = strlen($zinfo['name']); - if ($zinfo['name'][$len - 1] == '/') { - continue; - } - if ($zinfo['name'] == 'Thumbs.db') { - continue; - } - if (strrpos($zinfo['name'], '.DS_Store') == ($len - 9)) { - continue; - } - if (strrpos($zinfo['name'], '.localized') == ($len - 10)) { - continue; - } - if (strpos($zinfo['name'], '__MACOSX/') !== false) { - continue; - } + try { + Horde_Browser::wasFileUploaded('file' . $i); + if (!filesize($info['file' . $i]['file'])) { + throw new Horde_Browser_Exception(); + } + } catch (Horde_Browser_Exception $e) { + if (!empty($info['file' . $i]['error'])) { + $notification->push(sprintf(_("There was a problem uploading the photo: %s"), $info['file' . $i]['error']), 'horde.error'); + } elseif (!filesize($info['file' . $i]['file'])) { + $notification->push(_("The uploaded file appears to be empty. It may not exist on your computer."), 'horde.error'); + } + $valid = false; + continue; + } - $stream = $zip->getStream($zinfo['name']); - $zdata = stream_get_contents($stream); - if (!strlen($zdata)) { - $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $zinfo['name']), 'horde.error'); - break; - } - /* If we successfully got data, try adding the - * image to the gallery. */ - $image_id = $gallery->addImage(array( - 'image_filename' => $zinfo['name'], - 'image_caption' => '', - 'data' => $zdata, - )); - unset($zdata); - if (!is_a($image_id, 'PEAR_Error')) { - ++$uploaded; - if ($conf['image']['autogen'] > count($image_ids)) { - $image_ids[] = $image_id; - } - } else { - $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id), 'horde.error'); - } - } + /* Check for a compressed file. */ + if (in_array($info['file' . $i]['type'], + array('x-extension/zip', + 'application/x-compressed', + 'application/x-zip-compressed', + 'application/zip')) || + Horde_Mime_Magic::filenameToMime($info['file' . $i]['name']) == 'application/zip') { - $zip->close(); - unset($zip); - } else { - /* Read in the uploaded data. */ - $data = file_get_contents($info['file' . $i]['file']); + /* See if we can use the zip extension for reading the file. */ + if (Horde_Util::extensionExists('zip')) { + $zip = new ZipArchive(); + if ($zip->open($info['file' . $i]['file']) !== true) { + $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $info['file' . $i]['file']), 'horde.error'); + continue; + } - /* Get the list of files in the zipfile. */ - $zip = Horde_Compress::factory('zip'); - $files = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_LIST)); + for ($z = 0; $z < $zip->numFiles; $z++) { + $zinfo = $zip->statIndex($z); - if (is_a($files, 'PEAR_Error')) { - $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $files->getMessage()), 'horde.error'); + /* Skip some known metadata files. */ + $len = strlen($zinfo['name']); + if ($zinfo['name'][$len - 1] == '/') { + continue; + } + if ($zinfo['name'] == 'Thumbs.db') { + continue; + } + if (strrpos($zinfo['name'], '.DS_Store') == ($len - 9)) { + continue; + } + if (strrpos($zinfo['name'], '.localized') == ($len - 10)) { + continue; + } + if (strpos($zinfo['name'], '__MACOSX/') !== false) { continue; } - foreach ($files as $key => $zinfo) { - /* Skip some known metadata files. */ - $len = strlen($zinfo['name']); - if ($zinfo['name'][$len - 1] == '/') { - continue; - } - if ($zinfo['name'] == 'Thumbs.db') { - continue; - } - if (strrpos($zinfo['name'], '.DS_Store') == ($len - 9)) { - continue; - } - if (strrpos($zinfo['name'], '.localized') == ($len - 10)) { - continue; - } - if (strpos($zinfo['name'], '__MACOSX/') !== false) { - continue; - } - - $zdata = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_DATA, - 'info' => $files, - 'key' => $key)); - if (is_a($zdata, 'PEAR_Error')) { - $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $zdata->getMessage()), 'horde.error'); - break; - } + $stream = $zip->getStream($zinfo['name']); + $zdata = stream_get_contents($stream); + if (!strlen($zdata)) { + $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $zinfo['name']), 'horde.error'); + break; + } - /* If we successfully got data, try adding the - * image to the gallery. */ - $image_id = $gallery->addImage(array( - 'image_filename' => $zinfo['name'], - 'image_caption' => '', - 'data' => $zdata, - )); - unset($zdata); - if (!is_a($image_id, 'PEAR_Error')) { - ++$uploaded; - if ($conf['image']['autogen'] > count($image_ids)) { - $image_ids[] = $image_id; - } - } else { - $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id), 'horde.error'); + /* If we successfully got data, try adding the + * image to the gallery. */ + $image_id = $gallery->addImage(array( + 'image_filename' => $zinfo['name'], + 'image_caption' => '', + 'data' => $zdata, + )); + unset($zdata); + if (!is_a($image_id, 'PEAR_Error')) { + ++$uploaded; + if ($conf['image']['autogen'] > count($image_ids)) { + $image_ids[] = $image_id; } + } else { + $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id), 'horde.error'); } - - unset($zip); - unset($data); } + + $zip->close(); + unset($zip); } else { /* Read in the uploaded data. */ $data = file_get_contents($info['file' . $i]['file']); - /* Try and make sure the image is in a recognizeable - * format. */ - if (getimagesize($info['file' . $i]['file']) === false) { - $notification->push(_("The file you uploaded does not appear to be a valid photo."), 'horde.error'); + /* Get the list of files in the zipfile. */ + $zip = Horde_Compress::factory('zip'); + $files = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_LIST)); + + if (is_a($files, 'PEAR_Error')) { + $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $files->getMessage()), 'horde.error'); continue; } - /* Add the image to the gallery */ - $image_data = array('image_filename' => $info['file' . $i]['name'], - 'image_caption' => $vars->get('image' . $i . '_desc'), - 'image_type' => $info['file' . $i]['type'], - 'data' => $data, - 'tags' => (isset($info['image' . $i . '_tags']) ? explode(',', $info['image' . $i . '_tags']) : array())); - $image_id = $gallery->addImage($image_data, (bool)$vars->get('image' . $i . '_default')); - unset($data); - if (is_a($image_id, 'PEAR_Error')) { - $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id->getMessage()), 'horde.error'); - $valid = false; - } else { - ++$uploaded; - $image_ids[] = $image_id; + foreach ($files as $key => $zinfo) { + /* Skip some known metadata files. */ + $len = strlen($zinfo['name']); + if ($zinfo['name'][$len - 1] == '/') { + continue; + } + if ($zinfo['name'] == 'Thumbs.db') { + continue; + } + if (strrpos($zinfo['name'], '.DS_Store') == ($len - 9)) { + continue; + } + if (strrpos($zinfo['name'], '.localized') == ($len - 10)) { + continue; + } + if (strpos($zinfo['name'], '__MACOSX/') !== false) { + continue; + } + + $zdata = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_DATA, + 'info' => $files, + 'key' => $key)); + if (is_a($zdata, 'PEAR_Error')) { + $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $zdata->getMessage()), 'horde.error'); + break; + } + + /* If we successfully got data, try adding the + * image to the gallery. */ + $image_id = $gallery->addImage(array( + 'image_filename' => $zinfo['name'], + 'image_caption' => '', + 'data' => $zdata, + )); + unset($zdata); + if (!is_a($image_id, 'PEAR_Error')) { + ++$uploaded; + if ($conf['image']['autogen'] > count($image_ids)) { + $image_ids[] = $image_id; + } + } else { + $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id), 'horde.error'); + } } + + unset($zip); + unset($data); } } else { - if (!empty($info['file' . $i]['error'])) { - $notification->push(sprintf(_("There was a problem uploading the photo: %s"), $info['file' . $i]['error']), 'horde.error'); - } elseif (!filesize($info['file' . $i]['file'])) { - $notification->push(_("The uploaded file appears to be empty. It may not exist on your computer."), 'horde.error'); + /* Read in the uploaded data. */ + $data = file_get_contents($info['file' . $i]['file']); + + /* Try and make sure the image is in a recognizeable + * format. */ + if (getimagesize($info['file' . $i]['file']) === false) { + $notification->push(_("The file you uploaded does not appear to be a valid photo."), 'horde.error'); + continue; + } + + /* Add the image to the gallery */ + $image_data = array('image_filename' => $info['file' . $i]['name'], + 'image_caption' => $vars->get('image' . $i . '_desc'), + 'image_type' => $info['file' . $i]['type'], + 'data' => $data, + 'tags' => (isset($info['image' . $i . '_tags']) ? explode(',', $info['image' . $i . '_tags']) : array())); + $image_id = $gallery->addImage($image_data, (bool)$vars->get('image' . $i . '_default')); + unset($data); + if (is_a($image_id, 'PEAR_Error')) { + $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id->getMessage()), 'horde.error'); + $valid = false; + } else { + ++$uploaded; + $image_ids[] = $image_id; } - $valid = false; } } diff --git a/ansel/xppublish.php b/ansel/xppublish.php index 339c4a171..a0e321cfe 100644 --- a/ansel/xppublish.php +++ b/ansel/xppublish.php @@ -178,23 +178,26 @@ if ($cmd == 'add') { } if (!$name || $error) { $error = _("No file specified"); - } elseif (is_a($result = Horde_Browser::wasFileUploaded('imagefile', _("photo")), 'PEAR_Error')) { - $error = $result->getMessage(); } else { - $image = &Ansel::getImageFromFile($file, array('image_filename' => $name)); - if (is_a($image, 'PEAR_Error')) { - $error = $image->getMessage(); - } else { - $gallery = $ansel_storage->getGallery($galleryId); - $image_id = $gallery->addImage($image); - if (is_a($image_id, 'PEAR_Error')) { - $error = _("There was a problem uploading the photo."); + try { + Horde_Browser::wasFileUploaded('imagefile', _("photo")); + $image = &Ansel::getImageFromFile($file, array('image_filename' => $name)); + if (is_a($image, 'PEAR_Error')) { + $error = $image->getMessage(); } else { - $error = false; - } - if (is_a($image_id, 'PEAR_Error')) { - $image_id = $image_id->getMessage(); + $gallery = $ansel_storage->getGallery($galleryId); + $image_id = $gallery->addImage($image); + if (is_a($image_id, 'PEAR_Error')) { + $error = _("There was a problem uploading the photo."); + } else { + $error = false; + } + if (is_a($image_id, 'PEAR_Error')) { + $image_id = $image_id->getMessage(); + } } + } catch (Horde_Browser_Exception $e) { + $error = $e->getMessage(); } } diff --git a/framework/Browser/lib/Horde/Browser.php b/framework/Browser/lib/Horde/Browser.php index 63538c1ff..f0b1c633c 100644 --- a/framework/Browser/lib/Horde/Browser.php +++ b/framework/Browser/lib/Horde/Browser.php @@ -1037,7 +1037,7 @@ class Horde_Browser * @param string $name The file description string to use in the error * message. Default: 'file'. * - * @return mixed True on success, PEAR_Error on error. + * @throws Horde_Browser_Exception */ public function wasFileUploaded($field, $name = null) { @@ -1046,7 +1046,7 @@ class Horde_Browser } if (!($uploadSize = self::allowFileUploads())) { - return PEAR::raiseError(_("File uploads not supported.")); + throw new Horde_Browser_Exception('File uploads not supported.'); } /* Get any index on the field name. */ @@ -1063,21 +1063,21 @@ class Horde_Browser } else { /* No index, simple set up of vars to check. */ if (!isset($_FILES[$field])) { - return PEAR::raiseError(_("No file uploaded"), UPLOAD_ERR_NO_FILE); + throw new Horde_Browser_Exception('No file uploaded', UPLOAD_ERR_NO_FILE); } $error = $_FILES[$field]['error']; $tmp_name = $_FILES[$field]['tmp_name']; } if (!isset($_FILES) || ($error == UPLOAD_ERR_NO_FILE)) { - return PEAR::raiseError(sprintf(_("There was a problem with the file upload: No %s was uploaded."), $name), UPLOAD_ERR_NO_FILE); + throw new Horde_Browser_Exception(sprintf('There was a problem with the file upload: No %s was uploaded.', $name), UPLOAD_ERR_NO_FILE); } elseif (($error == UPLOAD_ERR_OK) && is_uploaded_file($tmp_name)) { - return true; + return; } elseif (($error == UPLOAD_ERR_INI_SIZE) || ($error == UPLOAD_ERR_FORM_SIZE)) { - return PEAR::raiseError(sprintf(_("There was a problem with the file upload: The %s was larger than the maximum allowed size (%d bytes)."), $name, min($uploadSize, Horde_Util::getFormData('MAX_FILE_SIZE'))), $error); + throw new Horde_Browser_Exception(sprintf('There was a problem with the file upload: The %s was larger than the maximum allowed size (%d bytes).', $name, min($uploadSize, Horde_Util::getFormData('MAX_FILE_SIZE'))), $error); } elseif ($error == UPLOAD_ERR_PARTIAL) { - return PEAR::raiseError(sprintf(_("There was a problem with the file upload: The %s was only partially uploaded."), $name), $error); + throw new Horde_Browser_Exception(sprintf('There was a problem with the file upload: The %s was only partially uploaded.', $name), $error); } } diff --git a/framework/Browser/lib/Horde/Browser/Exception.php b/framework/Browser/lib/Horde/Browser/Exception.php new file mode 100644 index 000000000..e2608c7c7 --- /dev/null +++ b/framework/Browser/lib/Horde/Browser/Exception.php @@ -0,0 +1,16 @@ + + * @category Horde + * @package Horde_Browser + */ +class Horde_Browser_Exception extends Horde_Exception +{ +} diff --git a/framework/Browser/package.xml b/framework/Browser/package.xml index d21b5ebae..77f91ef1a 100644 --- a/framework/Browser/package.xml +++ b/framework/Browser/package.xml @@ -25,12 +25,14 @@ about the current user's browser and its capabilities. beta LGPL - * Initial Horde 4 package. + * Add Horde_Browser_Exception::. + * Initial Horde 4 package. + @@ -60,6 +62,7 @@ about the current user's browser and its capabilities. + diff --git a/framework/Data/Data.php b/framework/Data/Data.php index 9a65cb2d1..1428713fb 100644 --- a/framework/Data/Data.php +++ b/framework/Data/Data.php @@ -312,9 +312,10 @@ class Horde_Data extends PEAR { case self::IMPORT_FILE: /* Sanitize uploaded file. */ $import_format = Horde_Util::getFormData('import_format'); - $check_upload = Horde_Browser::wasFileUploaded('import_file', $param['file_types'][$import_format]); - if (is_a($check_upload, 'PEAR_Error')) { - return $check_upload; + try { + Horde_Browser::wasFileUploaded('import_file', $param['file_types'][$import_format]); + } catch (Horde_Exception ($e) { + PEAR::raiseError($e->getMessage()); } if ($_FILES['import_file']['size'] <= 0) { return PEAR::raiseError(_("The file contained no data.")); diff --git a/framework/Data/Data/tsv.php b/framework/Data/Data/tsv.php index d3377d45c..9d00db053 100644 --- a/framework/Data/Data/tsv.php +++ b/framework/Data/Data/tsv.php @@ -183,9 +183,10 @@ class Horde_Data_tsv extends Horde_Data { /* Move uploaded file so that we can read it again in the next step after the user gave some format details. */ - $uploaded = Horde_Browser::wasFileUploaded('import_file', _("TSV file")); - if (is_a($uploaded, 'PEAR_Error')) { - return PEAR::raiseError($uploaded->getMessage()); + try { + Horde_Browser::wasFileUploaded('import_file', _("TSV file")); + } catch (Horde_Browser_Exception $e) { + return PEAR::raiseError($e->getMessage()); } $file_name = Horde::getTempFile('import', false); if (!move_uploaded_file($_FILES['import_file']['tmp_name'], $file_name)) { diff --git a/framework/Form/Form/Type.php b/framework/Form/Form/Type.php index 1a14a8798..905af4b2e 100644 --- a/framework/Form/Form/Type.php +++ b/framework/Form/Form/Type.php @@ -870,9 +870,10 @@ class Horde_Form_Type_file extends Horde_Form_Type { function isValid(&$var, &$vars, $value, &$message) { if ($var->isRequired()) { - $uploaded = Horde_Browser::wasFileUploaded($var->getVarName()); - if (is_a($uploaded, 'PEAR_Error')) { - $message = $uploaded->getMessage(); + try { + Horde_Browser::wasFileUploaded($var->getVarName()); + } catch (Horde_Browser_Exception $e) { + $message = $e->getMessage(); return false; } } @@ -883,15 +884,15 @@ class Horde_Form_Type_file extends Horde_Form_Type { function getInfo(&$vars, &$var, &$info) { $name = $var->getVarName(); - $uploaded = Horde_Browser::wasFileUploaded($name); - if ($uploaded === true) { + try { + Horde_Browser::wasFileUploaded($name); $info['name'] = Horde_Util::dispelMagicQuotes($_FILES[$name]['name']); $info['type'] = $_FILES[$name]['type']; $info['tmp_name'] = $_FILES[$name]['tmp_name']; $info['file'] = $_FILES[$name]['tmp_name']; $info['error'] = $_FILES[$name]['error']; $info['size'] = $_FILES[$name]['size']; - } + } catch (Horde_Browser_Exception $e) {} } /** @@ -964,7 +965,7 @@ class Horde_Form_Type_image extends Horde_Form_Type { * value of the form. */ if ($vars->get('_do_' . $var->getVarName())) { $var->form->setSubmitted(false); - if (is_a($this->_uploaded, 'PEAR_Error')) { + if ($this->_uploaded instanceof Horde_Browser_Exception) { $this->_img = array('hash' => $this->getRandomId(), 'error' => $this->_uploaded->getMessage()); } @@ -978,7 +979,7 @@ class Horde_Form_Type_image extends Horde_Form_Type { $field = $vars->get($var->getVarName()); /* The upload generated a PEAR Error. */ - if (is_a($this->_uploaded, 'PEAR_Error')) { + if ($this->_uploaded instanceof Horde_Browser_Exception) { /* Not required and no image upload attempted. */ if (!$var->isRequired() && empty($field['hash']) && $this->_uploaded->getCode() == UPLOAD_ERR_NO_FILE) { @@ -1030,7 +1031,7 @@ class Horde_Form_Type_image extends Horde_Form_Type { $info['keep_orig'] = !empty($value['keep_orig']); } - /* Set the uploaded value (either true or PEAR_Error). */ + /* Set the uploaded value (either true or Horde_Browser_Exception). */ $info['uploaded'] = &$this->_uploaded; /* If a modified file exists move it over the original. */ @@ -1071,9 +1072,11 @@ class Horde_Form_Type_image extends Horde_Form_Type { /* Check if file has been uploaded. */ $varname = $var->getVarName(); - $this->_uploaded = Horde_Browser::wasFileUploaded($varname . '[new]'); - if ($this->_uploaded === true) { + try { + Horde_Browser::wasFileUploaded($varname . '[new]'); + $this->_uploaded = true; + /* A file has been uploaded on this submit. Save to temp dir for * preview work. */ $this->_img['img']['type'] = $this->getUploadedFileType($varname . '[new]'); @@ -1112,7 +1115,9 @@ class Horde_Form_Type_image extends Horde_Form_Type { /* Move the browser created temp file to the new temp file. */ move_uploaded_file($this->_img['img']['file'], $tmp_file); $this->_img['img']['file'] = basename($tmp_file); - } elseif ($this->_uploaded) { + } catch (Horde_Browser_Exception $e) { + $this->_uploaded = $e; + /* File has not been uploaded. */ $upload = $vars->get($var->getVarName()); if ($this->_uploaded->getCode() == 4 && diff --git a/framework/Model/lib/Horde/Form.php b/framework/Model/lib/Horde/Form.php index ef895c559..18d60b89e 100644 --- a/framework/Model/lib/Horde/Form.php +++ b/framework/Model/lib/Horde/Form.php @@ -1089,9 +1089,10 @@ class Horde_Form_Type_file extends Horde_Form_Type { function isValid($var, $vars, $value, &$message) { if ($var->isRequired()) { - $uploaded = Horde_Browser::wasFileUploaded($var->getVarName()); - if (is_a($uploaded, 'PEAR_Error')) { - $message = $uploaded->getMessage(); + try { + Horde_Browser::wasFileUploaded($var->getVarName()); + } catch (Horde_Browser_Exception $e) { + $message = $e->getMessage(); return false; } } @@ -1102,15 +1103,15 @@ class Horde_Form_Type_file extends Horde_Form_Type { function getInfo($vars, $var, &$info) { $name = $var->getVarName(); - $uploaded = Horde_Browser::wasFileUploaded($name); - if ($uploaded === true) { + try { + Horde_Browser::wasFileUploaded($name); $info['name'] = $_FILES[$name]['name']; $info['type'] = $_FILES[$name]['type']; $info['tmp_name'] = $_FILES[$name]['tmp_name']; $info['file'] = $_FILES[$name]['tmp_name']; $info['error'] = $_FILES[$name]['error']; $info['size'] = $_FILES[$name]['size']; - } + } catch (Horde_Browser_Exception $e) {} } /** diff --git a/gollem/manager.php b/gollem/manager.php index 02bf714db..0bdb5487a 100644 --- a/gollem/manager.php +++ b/gollem/manager.php @@ -108,16 +108,18 @@ case 'upload_file': for ($i = 1, $l = count($_FILES); $i <= $l; ++$i) { $val = 'file_upload_' . $i; if (isset($_FILES[$val]) && ($_FILES[$val]['error'] != 4)) { - $res = Browser::wasFileUploaded($val); - if (!is_a($res, 'PEAR_Error')) { + try { + Horde_Browser::wasFileUploaded($val); $filename = Horde_Util::dispelMagicQuotes($_FILES[$val]['name']); $res = Gollem::writeFile($old_dir, $filename, $_FILES[$val]['tmp_name']); - } - if (is_a($res, 'PEAR_Error')) { - $notification->push($res, 'horde.error'); - } else { - Gollem::expireCache($old_dir); - $notification->push(sprintf(_("File received: %s"), $filename), 'horde.success'); + if (is_a($res, 'PEAR_Error')) { + $notification->push($res, 'horde.error'); + } else { + Gollem::expireCache($old_dir); + $notification->push(sprintf(_("File received: %s"), $filename), 'horde.success'); + } + } catch (Horde_Browser_Exception $e) { + $notification->push($e, 'horde.error'); } } } diff --git a/horde/services/problem.php b/horde/services/problem.php index ad1355ed2..a587ef8a2 100644 --- a/horde/services/problem.php +++ b/horde/services/problem.php @@ -53,14 +53,14 @@ case 'send_problem_report': /* Check for attachments. */ $attachment = null; if (!empty($conf['problems']['attachments'])) { - $result = Horde_Browser::wasFileUploaded('attachment', _("attachment")); - if (is_a($result, 'PEAR_Error')) { - if ($result->getCode() != UPLOAD_ERR_NO_FILE) { - $notification->push($result, 'horde.error'); + try { + Horde_Browser::wasFileUploaded('attachment', _("attachment")); + $attachment = $_FILES['attachment']; + } catch (Horde_Browser_Exception $e) { + if ($e->getCode() != UPLOAD_ERR_NO_FILE) { + $notification->push($e, 'horde.error'); break; } - } else { - $attachment = $_FILES['attachment']; } } diff --git a/imp/folders.php b/imp/folders.php index 85fa93f91..0e1e0e131 100644 --- a/imp/folders.php +++ b/imp/folders.php @@ -136,8 +136,8 @@ case 'download_folder_zip': case 'import_mbox': $import_folder = Horde_Util::getFormData('import_folder'); if (!empty($import_folder)) { - $res = $browser->wasFileUploaded('mbox_upload', _("mailbox file")); - if (!($res instanceof PEAR_Error)) { + try { + $browser->wasFileUploaded('mbox_upload', _("mailbox file")); $res = $imp_folder->importMbox(Horde_String::convertCharset($import_folder, $charset, 'UTF7-IMAP'), $_FILES['mbox_upload']['tmp_name']); $mbox_name = basename(Horde_Util::dispelMagicQuotes($_FILES['mbox_upload']['name'])); if ($res === false) { @@ -145,8 +145,8 @@ case 'import_mbox': } else { $notification->push(sprintf(_("Imported %d messages from %s."), $res, $mbox_name), 'horde.success'); } - } else { - $notification->push($res); + } catch (Horde_Browser_Exception $e) { + $notification->push($e); } $actionID = null; } else { diff --git a/imp/lib/Compose.php b/imp/lib/Compose.php index ebd95fc95..f72073b45 100644 --- a/imp/lib/Compose.php +++ b/imp/lib/Compose.php @@ -1671,9 +1671,10 @@ class IMP_Compose { global $conf; - $res = $GLOBALS['browser']->wasFileUploaded($name, _("attachment")); - if ($res instanceof PEAR_Error) { - throw new IMP_Compose_Exception($res); + try { + $GLOBALS['browser']->wasFileUploaded($name, _("attachment")); + } catch (Horde_Browser_Exception $e) { + throw new IMP_Compose_Exception($e); } $filename = Horde_Util::dispelMagicQuotes($_FILES[$name]['name']); diff --git a/imp/pgp.php b/imp/pgp.php index 9f9c9c446..ef2c29b5b 100644 --- a/imp/pgp.php +++ b/imp/pgp.php @@ -53,13 +53,12 @@ function _getImportKey() return $key; } - $res = $GLOBALS['browser']->wasFileUploaded('upload_key', _("key")); - if ($res instanceof PEAR_Error) { - $GLOBALS['notification']->push($res, 'horde.error'); - return; + try { + $GLOBALS['browser']->wasFileUploaded('upload_key', _("key")); + return file_get_contents($_FILES['upload_key']['tmp_name']); + } catch (Horde_Browser_Exception $e) { + $GLOBALS['notification']->push($e, 'horde.error'); } - - return file_get_contents($_FILES['upload_key']['tmp_name']); } function _textWindowOutput($filename, $msg) diff --git a/imp/smime.php b/imp/smime.php index 47e6f99ec..641ac804f 100644 --- a/imp/smime.php +++ b/imp/smime.php @@ -42,13 +42,12 @@ function _getImportKey() return $key; } - $res = $GLOBALS['browser']->wasFileUploaded('upload_key', _("key")); - if ($res instanceof PEAR_Error) { - $GLOBALS['notification']->push($res, 'horde.error'); - return; + try { + $GLOBALS['browser']->wasFileUploaded('upload_key', _("key")); + return file_get_contents($_FILES['upload_key']['tmp_name']); + } catch (Horde_Browser_Exception $e) { + $GLOBALS['notification']->push($e, 'horde.error'); } - - return file_get_contents($_FILES['upload_key']['tmp_name']); } function _actionWindow() diff --git a/news/add.php b/news/add.php index c605694be..fbf73f2ff 100644 --- a/news/add.php +++ b/news/add.php @@ -473,12 +473,8 @@ if ($form->validate()) { foreach ($conf['attributes']['languages'] as $key) { for ($i = 1; $i < 6; $i++) { $input = 'file_' . $key . '_' . $i; - $uploaded = Horde_Browser::wasFileUploaded($input); - if ($uploaded instanceof PEAR_Error) { - if ($uploaded->getCode() != UPLOAD_ERR_NO_FILE) { - $notification->push($uploaded->getMessage(), 'horde.warning'); - } - } elseif ($uploaded) { + try { + Horde_Browser::wasFileUploaded($input); $file_id = $news->write_db->nextID($news->prefix . '_files'); if ($file_id instanceof PEAR_Error) { $notification->push($file_id); @@ -494,6 +490,10 @@ if ($form->validate()) { } } } + } catch (Horde_Browser_Exception $e) { + if ($e->getCode() != UPLOAD_ERR_NO_FILE) { + $notification->push($e->getMessage(), 'horde.warning'); + } } } } diff --git a/wicked/lib/Page/AttachedFiles.php b/wicked/lib/Page/AttachedFiles.php index ea42d41aa..23245823f 100644 --- a/wicked/lib/Page/AttachedFiles.php +++ b/wicked/lib/Page/AttachedFiles.php @@ -212,9 +212,11 @@ class AttachedFiles extends Page { if (empty($filename)) { $filename = Horde_Util::dispelMagicQuotes($_FILES['attachment_file']['name']); } - $result = Horde_Browser::wasFileUploaded('attachment_file', _("attachment")); - if (is_a($result, 'PEAR_Error')) { - $notification->push($result, 'horde.error'); + + try { + Horde_Browser::wasFileUploaded('attachment_file', _("attachment")); + } catch (Horde_Browser_Exception $e) { + $notification->push($e, 'horde.error'); return; } -- 2.11.0