From 6a6307582db22eb8331e8bc7e7372d3149ecc71b Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Sun, 23 May 2010 16:34:00 -0400 Subject: [PATCH] is_a -> instanceof, pear_error -> exceptions etc... --- ansel/disclamer.php | 9 +- ansel/edit_dates.php | 12 +- ansel/faces/claim.php | 16 +- ansel/faces/custom.php | 22 +-- ansel/faces/gallery.php | 23 ++- ansel/faces/search/all.php | 18 +- ansel/faces/search/image_define.php | 5 +- ansel/faces/search/image_search.php | 34 ++-- ansel/faces/search/named.php | 17 +- ansel/faces/search/owner.php | 25 +-- ansel/gallery.php | 197 +++++++++---------- ansel/gallery/captions.php | 14 +- ansel/gallery/delete.php | 32 +-- ansel/gallery/sort.php | 17 +- ansel/group.php | 25 ++- ansel/image.php | 97 +++++---- ansel/img/download.php | 19 +- ansel/img/ecard.php | 18 +- ansel/img/full.php | 19 +- ansel/img/index.php | 15 +- ansel/img/mini.php | 16 +- ansel/img/prettythumb.php | 11 +- ansel/img/screen.php | 16 +- ansel/img/thumb.php | 15 +- ansel/img/upload.php | 79 ++++---- ansel/img/upload_preview.php | 13 +- ansel/lib/Ajax/Imple/EditCaption.php | 5 +- ansel/lib/Ajax/Imple/ImageSaveGeotag.php | 11 +- ansel/lib/Ajax/Imple/LocationAutoCompleter.php | 13 +- ansel/lib/Ansel.php | 129 ++++++------ ansel/lib/Api.php | 217 ++++++++------------- ansel/lib/Application.php | 4 - ansel/lib/Block/gallery.php | 19 +- ansel/lib/Block/my_galleries.php | 13 +- ansel/lib/Block/recent_comments.php | 27 +-- ansel/lib/Block/recently_added.php | 33 ++-- ansel/lib/Block/recently_added_geodata.php | 33 ++-- ansel/lib/Faces/Base.php | 33 ++-- ansel/lib/Gallery.php | 109 ++++++----- ansel/lib/GalleryMode/Date.php | 63 +++--- ansel/lib/GalleryMode/Normal.php | 10 +- ansel/lib/Image.php | 2 +- ansel/lib/Storage.php | 14 +- ansel/lib/Tags.php | 110 ++++++----- ansel/lib/View/Base.php | 10 +- ansel/lib/View/GalleryRenderer/Base.php | 2 +- ansel/lib/View/Image.php | 2 +- ansel/lib/Widget/OwnerFaces.php | 5 +- ansel/lib/Widget/SimilarPhotos.php | 11 +- ansel/map_edit.php | 8 +- ansel/preview.php | 23 +-- ansel/protect.php | 7 +- ansel/report.php | 23 +-- ansel/scripts/all_images_exif_to_tags.php | 15 +- ansel/scripts/ansel.php | 82 +++----- ansel/scripts/garbage_collection.php | 2 +- ansel/scripts/recursive_import.php | 24 +-- ansel/scripts/remote_import.php | 39 +--- .../2008-09-16_add_original_date_values.php | 2 +- ansel/xppublish.php | 60 +++--- 60 files changed, 851 insertions(+), 1093 deletions(-) diff --git a/ansel/disclamer.php b/ansel/disclamer.php index 0bdd75a49..63d3c8cb5 100644 --- a/ansel/disclamer.php +++ b/ansel/disclamer.php @@ -12,15 +12,14 @@ require_once dirname(__FILE__) . '/lib/Application.php'; Horde_Registry::appInit('ansel'); $vars = Horde_Variables::getDefaultVariables(); -$gallery = $ansel_storage->getGallery($vars->get('gallery')); -if (is_a($gallery, 'PEAR_Error')) { +try { + $gallery = $ansel_storage->getGallery($vars->get('gallery')); +} catch (Ansel_Exception $e) { $notification->push($gallery->getMessage()); header('Location: ' . Horde::applicationUrl('view.php?view=List', true)); exit; } $url = $vars->get('url'); - - $form = new Horde_Form($vars, _("Content Disclaimer"), 'disclamer'); $form->addVariable($gallery->get('name'), 'name', 'description', false); $form->addVariable($gallery->get('desc'), 'desc', 'description', false); @@ -44,7 +43,5 @@ if ($form->isSubmitted()) { require ANSEL_TEMPLATES . '/common-header.inc'; require ANSEL_TEMPLATES . '/menu.inc'; - $form->renderActive(null, null, null, 'post'); - require $registry->get('templates', 'horde') . '/common-footer.inc'; diff --git a/ansel/edit_dates.php b/ansel/edit_dates.php index c8f43b5d9..cb612edb0 100644 --- a/ansel/edit_dates.php +++ b/ansel/edit_dates.php @@ -46,13 +46,12 @@ $form = new Ansel_Form_ImageDate($vars, _("Edit Dates")); if ($actionID == 'edit_dates') { $count = 0; foreach (array_keys($images) as $image_id) { - $image = $ansel_storage->getImage($image_id); - if (!is_a($image, 'PEAR_Error')) { + try { + $image = $ansel_storage->getImage($image_id); if (empty($gallery_id)) { // Images might be from different galleries $gallery = $ansel_storage->getGallery($image->gallery); - if (is_a($gallery, 'PEAR_Error') || - !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { continue; } } @@ -60,15 +59,14 @@ if ($actionID == 'edit_dates') { $image->originalDate = (int)$newDate->timestamp(); $image->save(); ++$count; - } else { - $notification->push(sprintf(_("There was an error editing the dates: %s"), $image->getMessage()), 'horde.error'); + } catch (Ansel_Exception $e) { + $notification->push(sprintf(_("There was an error editing the dates: %s"), $e->getMessage()), 'horde.error'); echo Horde::wrapInlineScript(array( 'window.opener.location.href = window.opener.location.href;', 'window.close();' )); exit; } - } $notification->push(sprintf(_("Successfully modified the date on %d photos."), $count), 'horde.success'); diff --git a/ansel/faces/claim.php b/ansel/faces/claim.php index c55c16253..5a0da90a2 100644 --- a/ansel/faces/claim.php +++ b/ansel/faces/claim.php @@ -34,7 +34,6 @@ if ($form->validate()) { if (Horde_Util::getFormData('submitbutton') == _("Cancel")) { $notification->push(_("Action was cancelled."), 'horde.warning'); } else { - require ANSEL_BASE . '/lib/Report.php'; $report = Ansel_Report::factory(); $gallery = $ansel_storage->getGallery($face['gallery_id']); @@ -42,7 +41,9 @@ if ($form->validate()) { Horde::applicationUrl('faces/custom.php', true), array('name' => $vars->get('person'), 'face' => $face_id, - 'image' => $face['image_id']), null, false); + 'image' => $face['image_id']), + null, + false); $title = _("I know who is on one of your photos"); $body = _("Gallery Name") . ': ' . $gallery->get('name') . "\n" @@ -52,12 +53,11 @@ if ($form->validate()) { . _("Face") . ': ' . $face_link; $report->setTitle($title); - $result = $report->report($body, $gallery->get('owner')); - if (is_a($result, 'PEAR_Error')) { - $notification->push(_("Face name was not reported.") . ' ' . - $result->getMessage(), 'horde.error'); - } else { + try { + $result = $report->report($body, $gallery->get('owner')); $notification->push(_("The owner of the photo, who will delegate the face name, was notified."), 'horde.success'); + } catch (Ansel_Exception $e) { + $notification->push(_("Face name was not reported.") . ' ' . $e->getMessage(), 'horde.error'); } } @@ -67,7 +67,5 @@ if ($form->validate()) { require ANSEL_TEMPLATES . '/common-header.inc'; require ANSEL_TEMPLATES . '/menu.inc'; - $form->renderActive(null, null, null, 'post'); - require $registry->get('templates', 'horde') . '/common-footer.inc'; diff --git a/ansel/faces/custom.php b/ansel/faces/custom.php index 617c4ae88..820249713 100644 --- a/ansel/faces/custom.php +++ b/ansel/faces/custom.php @@ -23,8 +23,9 @@ if (!empty($url)) { } $form_post = Horde_Util::addParameter(Horde::applicationUrl('faces/savecustom.php'), $urlparams); -$image = &$ansel_storage->getImage($image_id); -if (is_a($image, 'PEAR_Error')) { +try { + $image = $ansel_storage->getImage($image_id); +} catch (Ansel_Exception $e) { $notification->push($image); header('Location: ' . Horde::applicationUrl('list.php')); exit; @@ -47,18 +48,17 @@ if ($face_id) { $faces = Ansel_Faces::factory(); try { $face = $faces->getFaceById($face_id, true); + $x1 = $face['face_x1']; + $y1 = $face['face_y1']; + $x2 = $face['face_x2']; + $y2 = $face['face_y2']; + if (!empty($face['face_name'])) { + $name = $face['face_name']; + } } catch (Horde_Exception $e) { $notification->push($e->getMessage()); + header('Location: ' . Horde::applicationUrl('list.php')); } - - $x1 = $face['face_x1']; - $y1 = $face['face_y1']; - $x2 = $face['face_x2']; - $y2 = $face['face_y2']; - if (!empty($face['face_name'])) { - $name = $face['face_name']; - } - } $height = $x2 - $x1; diff --git a/ansel/faces/gallery.php b/ansel/faces/gallery.php index bc237284c..b49625f7b 100644 --- a/ansel/faces/gallery.php +++ b/ansel/faces/gallery.php @@ -21,12 +21,14 @@ if (empty($gallery_id)) { header('Location: ' . Ansel::getUrlFor('default_view', array())); exit; } -$gallery = $ansel_storage->getGallery($gallery_id); -if (is_a($gallery, 'PEAR_Error')) { - $notification->push($gallery->getMessage(), 'horde.error'); +try { + $gallery = $ansel_storage->getGallery($gallery_id); +} catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); header('Location: ' . Ansel::getUrlFor('view', array('gallery' => $gallery_id))); exit; -} elseif (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { +} +if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { $notification->push(sprintf(_("Access denied editing gallery \"%s\"."), $gallery->get('name')), 'horde.error'); header('Location: ' . Ansel::getUrlFor('view', array('gallery' => $gallery_id))); exit; @@ -41,12 +43,17 @@ $customimage = Horde_Themes::img('layout.png'); $customurl = Horde_Util::addParameter(Horde::applicationUrl('faces/custom.php'), 'page', $page); $face = Ansel_Faces::factory(); $autogenerate = $face->canAutogenerate(); + $vars = Horde_Variables::getDefaultVariables(); $pager = new Horde_Ui_Pager( - 'page', $vars, - array('num' => $gallery->countImages(), - 'url' => 'faces/gallery.php', - 'perpage' => $perpage)); + 'page', + $vars, + array( + 'num' => $gallery->countImages(), + 'url' => 'faces/gallery.php', + 'perpage' => $perpage + ) +); $pager->preserve('gallery', $gallery_id); $title = sprintf(_("Searching for faces in %s"), Horde::link(Ansel::getUrlFor('view', array('gallery' => $gallery_id, 'view' => 'Gallery'))) . $gallery->get('name') . ''); diff --git a/ansel/faces/search/all.php b/ansel/faces/search/all.php index 3bfc98e98..88f1f61f6 100644 --- a/ansel/faces/search/all.php +++ b/ansel/faces/search/all.php @@ -15,21 +15,23 @@ $title = _("All faces"); $page = Horde_Util::getFormData('page', 0); $perpage = $prefs->getValue('facesperpage'); -$count = $faces->countAllFaces(); -if (is_a($count, 'PEAR_Error')) { +try { + $count = $faces->countAllFaces(); + $results = $faces->allFaces($page * $perpage, $perpage); +} catch (Ansel_Exception $e) { $notification->push($count->getDebugInfo()); $count = 0; $results = array(); -} else { - $results = $faces->allFaces($page * $perpage, $perpage); } - $vars = Horde_Variables::getDefaultVariables(); $pager = new Horde_Ui_Pager( 'page', $vars, - array('num' => $count, - 'url' => 'faces/search/all.php', - 'perpage' => $perpage)); + array( + 'num' => $count, + 'url' => 'faces/search/all.php', + 'perpage' => $perpage + ) +); require ANSEL_TEMPLATES . '/common-header.inc'; require ANSEL_TEMPLATES . '/menu.inc'; diff --git a/ansel/faces/search/image_define.php b/ansel/faces/search/image_define.php index 5d5cede99..aac20fa45 100644 --- a/ansel/faces/search/image_define.php +++ b/ansel/faces/search/image_define.php @@ -27,8 +27,9 @@ $y1 = 0; $x2 = 0; $y2 = 0; -$faces = $faces->getFaces($path); -if (is_a($faces, 'PEAR_Error')) { +try { + $faces = $faces->getFaces($path); +} catch (Ansel_Exception $e) { exit; } diff --git a/ansel/faces/search/image_search.php b/ansel/faces/search/image_search.php index b2945ba3f..a60838613 100644 --- a/ansel/faces/search/image_search.php +++ b/ansel/faces/search/image_search.php @@ -15,13 +15,14 @@ $page = Horde_Util::getFormData('page', 0); $perpage = $prefs->getValue('facesperpage'); if (($face_id = Horde_Util::getGet('face_id')) !== null) { - $face = $faces->getFaceById($face_id, true); - if (is_a($face, 'PEAR_Error')) { - $notification->push($face->getMessage()); + try { + $face = $faces->getFaceById($face_id, true); + $signature = $face['face_signature']; + $results = $faces->getSignatureMatches($signature, $face_id, $perpage * $page, $perpage); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage()); header('Location: ' . Horde::applicationUrl('faces/search/image.php')); } - $signature = $face['face_signature']; - $results = $faces->getSignatureMatches($signature, $face_id, $perpage * $page, $perpage); } else { $tmp = Horde::getTempDir(); $path = $tmp . '/search_face_' . Horde_Auth::getAuth() . '.sig'; @@ -30,20 +31,25 @@ if (($face_id = Horde_Util::getGet('face_id')) !== null) { header('Location: ' . Horde::applicationUrl('faces/search/image.php')); } $signature = file_get_contents($path); - $results = $faces->getSignatureMatches($signature, 0, $perpage * $page, $perpage); -} -if (is_a($results, 'PEAR_Error')) { - $notification->push($results); - $results = array(); + try { + $results = $faces->getSignatureMatches($signature, 0, $perpage * $page, $perpage); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage()); + $results = array(); + } } $title = _("Photo search"); $vars = Horde_Variables::getDefaultVariables(); $pager = new Horde_Ui_Pager( - 'page', $vars, - array('num' => count($results), - 'url' => 'faces/search/image_search.php', - 'perpage' => $perpage)); + 'page', + $vars, + array( + 'num' => count($results), + 'url' => 'faces/search/image_search.php', + 'perpage' => $perpage + ) +); require ANSEL_TEMPLATES . '/common-header.inc'; require ANSEL_TEMPLATES . '/menu.inc'; diff --git a/ansel/faces/search/named.php b/ansel/faces/search/named.php index 8cbab8b03..65116af49 100644 --- a/ansel/faces/search/named.php +++ b/ansel/faces/search/named.php @@ -15,20 +15,23 @@ $title = _("Named faces"); $page = Horde_Util::getFormData('page', 0); $perpage = $prefs->getValue('facesperpage'); $results = array(); -$count = $faces->countNamedFaces(); -if (is_a($count, 'PEAR_Error')) { +try { + $count = $faces->countNamedFaces(); + $results = $faces->namedFaces($page * $perpage, $perpage); +} catch (Ansel_Exception $e) { $notification->push($count->getDebugInfo()); $count = 0; -} elseif ($count > 0) { - $results = $faces->namedFaces($page * $perpage, $perpage); } $vars = Horde_Variables::getDefaultVariables(); $pager = new Horde_Ui_Pager( 'page', $vars, - array('num' => $count, - 'url' => 'faces/search/named.php', - 'perpage' => $perpage)); + array( + 'num' => $count, + 'url' => 'faces/search/named.php', + 'perpage' => $perpage + ) +); require ANSEL_TEMPLATES . '/common-header.inc'; require ANSEL_TEMPLATES . '/menu.inc'; diff --git a/ansel/faces/search/owner.php b/ansel/faces/search/owner.php index fb4c5ed82..9fe847529 100644 --- a/ansel/faces/search/owner.php +++ b/ansel/faces/search/owner.php @@ -22,26 +22,27 @@ if (!$owner) { $title = sprintf(_("From galleries of %s"), $owner); } -$count = $faces->countOwnerFaces($owner); -if (is_a($count, 'PEAR_Error')) { - $notification->push($count); +try { + $count = $faces->countOwnerFaces($owner); + $results = $faces->ownerFaces($owner, $page * $perpage, $perpage); +} catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.err'); $results = array(); $count = 0; -} else { - $results = $faces->ownerFaces($owner, $page * $perpage, $perpage); } $vars = Horde_Variables::getDefaultVariables(); $pager = new Horde_Ui_Pager( - 'page', $vars, - array('num' => $count, - 'url' => 'faces/search/owner.php', - 'perpage' => $perpage)); + 'page', + $vars, + array( + 'num' => $count, + 'url' => 'faces/search/owner.php', + 'perpage' => $perpage + ) +); $pager->preserve('owner', $owner); - require ANSEL_TEMPLATES . '/common-header.inc'; require ANSEL_TEMPLATES . '/menu.inc'; - include ANSEL_TEMPLATES . '/faces/faces.inc'; - require $registry->get('templates', 'horde') . '/common-footer.inc'; diff --git a/ansel/gallery.php b/ansel/gallery.php index 0112e2651..4d871daa4 100644 --- a/ansel/gallery.php +++ b/ansel/gallery.php @@ -44,15 +44,17 @@ case 'addchild': // Get the parent and make sure that it exists and that we have // permissions to add to it. $parentId = Horde_Util::getFormData('gallery'); - $parent = $ansel_storage->getGallery($parentId); - if (is_a($parent, 'PEAR_Error')) { - $notification->push($parent->getMessage(), 'horde.error'); + try { + $parent = $ansel_storage->getGallery($parentId); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); header('Location: ' . Horde::applicationUrl('view.php?view=List', true)); exit; - } elseif (!$parent->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - $notification->push( - sprintf(_("Access denied adding a gallery to \"%s\"."), - $parent->get('name')), 'horde.error'); + } + + if (!$parent->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { + $notification->push(sprintf(_("Access denied adding a gallery to \"%s\"."), + $parent->get('name')), 'horde.error'); header('Location: ' . Horde::applicationUrl('view.php?view=List', true)); exit; } @@ -72,21 +74,16 @@ case 'addchild': $gallery_passwd = ''; $notification->push('document.gallery.gallery_name.focus();', 'javascript'); - $title = sprintf(_("Adding A Subgallery to %s"), $parent->get('name')); break; case 'downloadzip': $galleryId = Horde_Util::getFormData('gallery'); $gallery = $ansel_storage->getGallery($galleryId); - if (!Horde_Auth::getAuth() || is_a($gallery, 'PEAR_Error') || + if (!Horde_Auth::getAuth() || !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - $name = is_a($gallery, 'PEAR_Error') - ? $galleryId - : $gallery->get('name'); - $notification->push(sprintf(_("Access denied downloading photos from \"%s\"."), $name), - 'horde.error'); + $notification->push(sprintf(_("Access denied downloading photos from \"%s\"."), $gallery->get('name')), 'horde.error'); header('Location: ' . Horde::applicationUrl('view.php?view=List', true)); exit; } @@ -96,9 +93,10 @@ case 'downloadzip': case 'modify': $galleryId = Horde_Util::getFormData('gallery'); - $gallery = $ansel_storage->getGallery($galleryId); - if (!is_a($gallery, 'PEAR_Error')) { - // Set up the gallery attributes. + + try { + $gallery = $ansel_storage->getGallery($galleryId); + // Set up the gallery attributes. $gallery_name = $gallery->get('name'); $gallery_desc = $gallery->get('desc'); $gallery_category = $gallery->get('category'); @@ -114,9 +112,10 @@ case 'modify': } $gallery_mode = $gallery->get('view_mode'); $gallery_passwd = $gallery->get('passwd'); - } else { - $title = _("Unknown gallery"); + } catch (Ansel_Exception $e) { + $title = _("Unknown Gallery"); } + break; case 'save': @@ -160,15 +159,8 @@ case 'save': // Modifying an existing gallery. $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_Error') || - !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - - $name = is_a($gallery, 'PEAR_Error') - ? $galleryId - : $gallery->get('name'); - - $notification->push(sprintf(_("Access denied saving gallery \"%s\"."), - $name), 'horde.error'); + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { + $notification->push(sprintf(_("Access denied saving gallery \"%s\"."), $gallery->get('name')), 'horde.error'); } else { // Don't allow the display name to be nulled out. if ($gallery_name) { @@ -198,44 +190,40 @@ case 'save': if ($gallery_parent != $old_parent_id) { if (!is_null($gallery_parent)) { $new_parent = $ansel_storage->getGallery($gallery_parent); - if (is_a($new_parent, 'PEAR_Error')) { - return $new_parent; - } } else { $new_parent = null; } - $result = $gallery->setParent($new_parent); - if (is_a($result, 'PEAR_Error')) { - $notification->push($result->getMessage(), 'horde.error'); - header('Location: ' . Horde::applicationUrl( - Ansel::getUrlFor('view', array('view' => 'List'), true))); + try { + $result = $gallery->setParent($new_parent); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); + header('Location: ' . Horde::applicationUrl(Ansel::getUrlFor('view', array('view' => 'List'), true))); exit; } } - $result = $gallery->save(); - if (is_a($result, 'PEAR_Error')) { - $notification->push($result->getMessage(), 'horde.error'); - } else { - $notification->push(_("The gallery was saved."), - 'horde.success'); + try { + $result = $gallery->save(); + $notification->push(_("The gallery was saved."),'horde.success'); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); } } } else { // Is this a new subgallery? if ($gallery_parent) { - $parent = $ansel_storage->getGallery($gallery_parent); - if (is_a($parent, 'PEAR_Error')) { - $notification->push($parent->getMessage(), 'horde.error'); - header('Location: ' . Horde::applicationUrl( - Ansel::getUrlFor('view', array('view' => 'List'), true))); + try { + $parent = $ansel_storage->getGallery($gallery_parent); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); + header('Location: ' . Horde::applicationUrl(Ansel::getUrlFor('view', array('view' => 'List'), true))); exit; - } elseif (!$parent->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { + } + if (!$parent->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { $notification->push(sprintf( _("You do not have permission to add children to %s."), $parent->get('name')), 'horde.error'); - header('Location: ' . Horde::applicationUrl( - Ansel::getUrlFor('view', array('view' => 'List'), true))); + header('Location: ' . Horde::applicationUrl(Ansel::getUrlFor('view', array('view' => 'List'), true))); exit; } } @@ -254,42 +242,45 @@ case 'save': $perm = (!empty($parent)) ? $parent->getPermission() : null; $parent = (!empty($gallery_parent)) ? $gallery_parent : null; - $gallery = $ansel_storage->createGallery(array('name' => $gallery_name, - 'desc' => $gallery_desc, - 'category' => $gallery_category, - 'tags' => explode(',', $gallery_tags), - 'style' => $gallery_thumbstyle, - 'slug' => $gallery_slug, - 'age' => $gallery_age, - 'download' => $gallery_download, - 'view_mode' => $gallery_mode, - 'passwd' => $gallery_passwd, - ), $perm, $parent); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $ansel_storage->createGallery( + array('name' => $gallery_name, + 'desc' => $gallery_desc, + 'category' => $gallery_category, + 'tags' => explode(',', $gallery_tags), + 'style' => $gallery_thumbstyle, + 'slug' => $gallery_slug, + 'age' => $gallery_age, + 'download' => $gallery_download, + 'view_mode' => $gallery_mode, + 'passwd' => $gallery_passwd, + ), + $perm, $parent); + + $galleryId = $gallery->getId(); + $msg = sprintf(_("The gallery \"%s\" was created successfully."), $gallery_name); + Horde::logMessage($msg, 'DEBUG'); + $notification->push($msg, 'horde.success'); + } catch (Ansel_Exception $e) { $galleryId = null; $error = sprintf(_("The gallery \"%s\" couldn't be created: %s"), $gallery_name, $gallery->getMessage()); Horde::logMessage($error, 'ERR'); $notification->push($error, 'horde.error'); - } else { - $galleryId = $gallery->getId(); - $msg = sprintf(_("The gallery \"%s\" was created successfully."), - $gallery_name); - Horde::logMessage($msg, 'DEBUG'); - $notification->push($msg, 'horde.success'); } + } // Clear the OtherGalleries widget cache if ($GLOBALS['conf']['ansel_cache']['usecache']) { $GLOBALS['cache']->expire('Ansel_OtherGalleries' . $gallery->get('owner')); } + // Return to the last view. $url = Horde_Util::getFormData('url'); - if (empty($url) && empty($exists) && !is_a($gallery, 'PEAR_Error')) { + if (empty($url) && empty($exists)) { // Redirect to the images upload page for newly creted galleries - $url = Horde_Util::addParameter(Horde::applicationUrl('img/upload.php'), - 'gallery', $galleryId); + $url = Horde_Util::addParameter(Horde::applicationUrl('img/upload.php'), 'gallery', $galleryId); } elseif (empty($url)) { $url = Horde::applicationUrl('index.php', true); } @@ -301,83 +292,69 @@ case 'empty': // Print the confirmation screen. $galleryId = Horde_Util::getFormData('gallery'); if ($galleryId) { - $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_Error')) { - $notification->push($gallery->getMessage(), 'horde.error'); - } else { + try { + $gallery = $ansel_storage->getGallery($galleryId); require ANSEL_TEMPLATES . '/common-header.inc'; require ANSEL_TEMPLATES . '/menu.inc'; require ANSEL_TEMPLATES . '/gallery/delete_confirmation.inc'; require $registry->get('templates', 'horde') . '/common-footer.inc'; exit; + } catch (Ansel_Exception $e) { + $notification->push($gallery->getMessage(), 'horde.error'); } } // Return to the gallery list. - header('Location: ' . Horde::applicationUrl( - Ansel::getUrlFor('view', array('view' => 'List'), true))); + header('Location: ' . Horde::applicationUrl(Ansel::getUrlFor('view', array('view' => 'List'), true))); exit; case 'generateDefault': // Re-generate the default pretty gallery image. $galleryId = Horde_Util::getFormData('gallery'); - $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_Error')) { - $notification->push($gallery->getMessage(), 'horde.error'); + try { + $gallery = $ansel_storage->getGallery($galleryId); + $gallery->clearStacks(); + $notification->push(_("The gallery's default photo has successfully been reset."), 'horde.success'); + header('Location: ' . Horde::applicationUrl(Horde_Util::addParameter('view.php', 'gallery', $galleryId), true)); + exit; + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); header('Location: ' . Horde::applicationUrl('index.php', true)); exit; } - $gallery->clearStacks(); - $notification->push( - _("The gallery's default photo has successfully been reset."), - 'horde.success'); - $url = Horde_Util::addParameter('view.php', 'gallery', $galleryId); - header('Location: ' . Horde::applicationUrl($url, true)); - exit; case 'generateThumbs': // Re-generate all of this gallery's prettythumbs. $galleryId = Horde_Util::getFormData('gallery'); - $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $ansel_storage->getGallery($galleryId); + } catch (Ansel_Exception $e) { $notification->push($gallery->getMessage(), 'horde.error'); header('Location: ' . Horde::applicationUrl('index.php', true)); exit; } $gallery->clearThumbs(); - $notification->push( - _("The gallery's thumbnails have successfully been reset."), - 'horde.success'); - $url = Horde_Util::addParameter('view.php', 'gallery', $galleryId); - header('Location: ' . Horde::applicationUrl($url, true)); + $notification->push(_("The gallery's thumbnails have successfully been reset."), 'horde.success'); + header('Location: ' . Horde::applicationUrl(Horde_Util::addParameter('view.php', 'gallery', $galleryId), true)); exit; case 'deleteCache': - // Delete all cached image views. This will NOT immediately regenerate - // all views that existed prior to this action. However it will remove all - // cached views (leaving the originals) which will be generated on demand - // by users browsing the site. Note that the first time each image is - // viewed there will be a delay as the view is generated and cached. - // This can be useful when changing the configured "screen" size in Ansel's - // configuration. + // Delete all cached image views. $galleryId = Horde_Util::getFormData('gallery'); - $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $ansel_storage->getGallery($galleryId); + } catch (Ansel_Exception $e) { $notification->push($gallery->getMessage(), 'horde.error'); header('Location: ' . Horde::applicationUrl('index.php', true)); exit; } $gallery->clearViews(); - $notification->push( - _("The gallery's views have successfully been reset."), - 'horde.success'); - $url = Horde_Util::addParameter('view.php', 'gallery', $galleryId); - header('Location: ' . Horde::applicationUrl($url, true)); + $notification->push(_("The gallery's views have successfully been reset."), 'horde.success'); + header('Location: ' . Horde::applicationUrl(Horde_Util::addParameter('view.php', 'gallery', $galleryId), true)); exit; default: - header('Location: ' . Horde::applicationUrl( - Ansel::getUrlFor('view', array('view' => 'List'), true))); + header('Location: ' . Horde::applicationUrl(Ansel::getUrlFor('view', array('view' => 'List'), true))); exit; } diff --git a/ansel/gallery/captions.php b/ansel/gallery/captions.php index a95e814eb..1381b8211 100644 --- a/ansel/gallery/captions.php +++ b/ansel/gallery/captions.php @@ -17,19 +17,17 @@ if (!$galleryId) { true)); exit; } - -$gallery = $ansel_storage->getGallery($galleryId); -if (is_a($gallery, 'PEAR_Error')) { - $notification->push(sprintf(_("Error accessing %s: %s"), $galleryId, $gallery->getMessage()), 'horde.error'); - header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), - true)); +try { + $gallery = $ansel_storage->getGallery($galleryId); +} catch (Ansel_Exception $e) { + $notification->push(sprintf(_("Error accessing %s: %s"), $galleryId, $e->getMessage()), 'horde.error'); + header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; } if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { $notification->push(sprintf(_("Access denied setting captions for %s."), $gallery->get('name')), 'horde.error'); - header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), - true)); + header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; } diff --git a/ansel/gallery/delete.php b/ansel/gallery/delete.php index 6048d1a66..e46b0a313 100644 --- a/ansel/gallery/delete.php +++ b/ansel/gallery/delete.php @@ -16,26 +16,30 @@ $actionID = Horde_Util::getPost('action'); $galleryId = Horde_Util::getPost('gallery'); if ($galleryId) { - $gallery = $ansel_storage->getGallery($galleryId); - + try { + $gallery = $ansel_storage->getGallery($galleryId); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); + // Return to the default view. + header('Location: ' . Ansel::getUrlFor('default_view', array())); + exit; + } switch ($actionID) { case 'delete': - if (is_a($gallery, 'PEAR_Error')) { - $notification->push($gallery->getMessage(), 'horde.error'); - } elseif (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { $notification->push(sprintf(_("Access denied deleting gallery \"%s\"."), $gallery->get('name')), 'horde.error'); } else { - $result = $ansel_storage->removeGallery($gallery); - if (is_a($result, 'PEAR_Error')) { - $notification->push(sprintf( - _("There was a problem deleting %s: %s"), - $gallery->get('name'), $result->getMessage()), - 'horde.error'); - } else { + try { + $ansel_storage->removeGallery($gallery); $notification->push(sprintf( _("Successfully deleted %s."), $gallery->get('name')), 'horde.success'); + } catch (Ansel_Exception $e) { + $notification->push(sprintf( + _("There was a problem deleting %s: %s"), + $gallery->get('name'), $e->getMessage()), + 'horde.error'); } } @@ -49,9 +53,7 @@ if ($galleryId) { exit; case 'empty': - if (is_a($gallery, 'PEAR_Error')) { - $notification->push($gallery->getMessage(), 'horde.error'); - } elseif (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { $notification->push(sprintf(_("Access denied deleting gallery \"%s\"."), $gallery->get('name')), 'horde.error'); diff --git a/ansel/gallery/sort.php b/ansel/gallery/sort.php index 1fb9d0653..780c53a69 100644 --- a/ansel/gallery/sort.php +++ b/ansel/gallery/sort.php @@ -19,19 +19,20 @@ if (!isset($galleryId)) { true)); exit; } - -$gallery = $ansel_storage->getGallery($galleryId); -if (is_a($gallery, 'PEAR_Error')) { +try { + $gallery = $ansel_storage->getGallery($galleryId); +} catch (Ansel_Excception $e) { $notification->push(_("There was an error accessing the gallery."), 'horde.error'); - header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), - true)); + header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; -} elseif (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { +} + +if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { $notification->push(sprintf(_("Access denied editing gallery \"%s\"."), $gallery->get('name')), 'horde.error'); - header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), - true)); + header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; } + $style = $gallery->getStyle(); $date = Ansel::getDateParameter(); $gallery->setDate($date); diff --git a/ansel/group.php b/ansel/group.php index 2ba047fe9..efc110acd 100644 --- a/ansel/group.php +++ b/ansel/group.php @@ -28,12 +28,14 @@ $groups_perpage = $prefs->getValue('groupsperpage'); switch ($groupby) { case 'category': - $num_groups = $ansel_storage->countCategories(Horde_Perms::SHOW); - if (is_a($num_groups, 'PEAR_Error')) { + try { + $num_groups = $ansel_storage->countCategories(Horde_Perms::SHOW); + } catch (Ansel_Exception $e) { $notification->push($num_groups); $num_groups = 0; $groups = array(); - } elseif ($num_groups) { + } + if ($num_groups) { $groups = $ansel_storage->listCategories(Horde_Perms::SHOW, $gbpage * $groups_perpage, $groups_perpage); @@ -63,18 +65,23 @@ case 'owner': default: header('Location: ' . Ansel::getUrlFor('view', - array('view' => 'List', - 'groupby' => $groupby), + array( + 'view' => 'List', + 'groupby' => $groupby + ), true)); exit; } // Set up pager. $vars = Horde_Variables::getDefaultVariables(); -$group_pager = new Horde_Ui_Pager('gbpage', $vars, - array('num' => $num_groups, - 'url' => 'group.php', - 'perpage' => $groups_perpage)); +$group_pager = new Horde_Ui_Pager('gbpage', + $vars, + array( + 'num' => $num_groups, + 'url' => 'group.php', + 'perpage' => $groups_perpage + )); $min = $gbpage * $groups_perpage; $max = $min + $groups_perpage; diff --git a/ansel/image.php b/ansel/image.php index 4544a0cb7..60431c487 100644 --- a/ansel/image.php +++ b/ansel/image.php @@ -53,8 +53,9 @@ if (is_null($actionID) && is_null($tags)) { } /* Get the gallery object and style information */ -$gallery = $ansel_storage->getGallery($gallery_id); -if (is_a($gallery, 'PEAR_Error')) { +try { + $gallery = $ansel_storage->getGallery($gallery_id); +} catch (Ansel_Exception $e) { $notification->push(sprintf(_("Gallery %s not found."), $gallery_id), 'horde.error'); header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; @@ -125,10 +126,10 @@ case 'deletetags': exit; case 'modify': - $image = &$ansel_storage->getImage($image_id); - $ret = Horde_Util::getFormData('ret', 'gallery'); - - if (is_a($image, 'PEAR_Error')) { + try { + $image = &$ansel_storage->getImage($image_id); + $ret = Horde_Util::getFormData('ret', 'gallery'); + } catch (Ansel_Exception $e) { $notification->push(_("Photo not found."), 'horde.error'); header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; @@ -154,9 +155,7 @@ case 'modify': $vars->set('image_uploaded', $image->uploaded); require ANSEL_TEMPLATES . '/common-header.inc'; - $form->renderActive($renderer, $vars, 'image.php', 'post', - 'multipart/form-data'); - + $form->renderActive($renderer, $vars, 'image.php', 'post', 'multipart/form-data'); require $registry->get('templates', 'horde') . '/common-footer.inc'; exit; @@ -213,8 +212,9 @@ case 'save': $image->originalDate = (int)$newDate->timestamp(); if (!empty($data)) { - $result = $image->replace($data); - if (is_a($result, 'PEAR_Error')) { + try { + $image->replace($data); + } catch (Ansel_Exception $e) { $notification->push(_("There was an error replacing the photo."), 'horde.error'); } } @@ -395,9 +395,10 @@ case 'resize': $gallery->get('name')), 'horde.error'); } else { - $image = &$ansel_storage->getImage($image_id); - if (is_a($image, 'PEAR_Error')) { - $notification->push($image->getMessage(), 'horde.error'); + try { + $image = $ansel_storage->getImage($image_id); + } catch (Ansel_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; } @@ -425,9 +426,10 @@ case 'resize': case 'crop': $image->load('full'); $params = Horde_Util::getFormData('params'); - list($x1, $y1, $x2, $y2) = explode('.', $params); - $result = $image->crop($x1, $y1, $x2, $y2); - if (is_a($result, 'PEAR_Error')) { + list($x1, $y1, $x2, $y2) = explode('.', $params + try { + $result = $image->crop($x1, $y1, $x2, $y2); + } catch (Ansel_Exception $e) { Horde::logMessage($result, 'ERR'); $notification->push($result->getMessage(), 'horde.error'); $error = true; @@ -457,8 +459,9 @@ case 'resize': case 'setwatermark': $title = _("Watermark"); - $image = &$ansel_storage->getImage($image_id); - if (is_a($image, 'PEAR_Error')) { + try { + $image = $ansel_storage->getImage($image_id); + } catch (Ansel_Exception $e) { $notification->push($image->getMessage(), 'horde.error'); header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; @@ -569,14 +572,13 @@ case 'delete': $notification->push(sprintf(_("Access denied deleting photos from \"%s\"."), $gallery->get('name')), 'horde.error'); } else { foreach ($images as $image) { - $result = $gallery->removeImage($image); - if (is_a($result, 'PEAR_Error')) { + try { + $gallery->removeImage($image); + $notification->push(_("Deleted the photo."), 'horde.success'); + } catch (Ansel_Exception $e) { $notification->push( sprintf(_("There was a problem deleting photos: %s"), $result->getMessage()), 'horde.error'); - } else { - $notification->push(_("Deleted the photo."), - 'horde.success'); } } } @@ -612,21 +614,17 @@ case 'move': /* Move the images if we're provided with at least one valid image_id. */ $newGallery = Horde_Util::getFormData('new_gallery'); if ($images && $newGallery) { - $newGallery = $ansel_storage->getGallery($newGallery); - if (is_a($newGallery, 'PEAR_Error')) { - $notification->push(_("Bad input."), 'horde.error'); - } else { + try { + $newGallery = $ansel_storage->getGallery($newGallery); $result = $gallery->moveImagesTo($images, $newGallery); - if (is_a($result, 'PEAR_Error')) { - $notification->push($result, 'horde.error'); - } else { - $notification->push( - sprintf(ngettext("Moved %d photo from \"%s\" to \"%s\"", - "Moved %d photos from \"%s\" to \"%s\"", - $result), - $result, $gallery->get('name'), $newGallery->get('name')), - 'horde.success'); - } + $notification->push( + sprintf(ngettext("Moved %d photo from \"%s\" to \"%s\"", + "Moved %d photos from \"%s\" to \"%s\"", + $result), + $result, $gallery->get('name'), $newGallery->get('name')), + 'horde.success'); + } catch (Ansel_Exception $e) { + $notification->push(_("Bad input."), 'horde.error'); } } @@ -661,21 +659,18 @@ case 'copy': * ID. */ $newGallery = Horde_Util::getFormData('new_gallery'); if ($images && $newGallery) { - $newGallery = $ansel_storage->getGallery($newGallery); - if (is_a($newGallery, 'PEAR_Error')) { - $notification->push(_("Bad input."), 'horde.error'); - } else { + try { + $newGallery = $ansel_storage->getGallery($newGallery); $result = $gallery->copyImagesTo($images, $newGallery); - if (is_a($result, 'PEAR_Error')) { - $notification->push($result, 'horde.error'); - } else { - $notification->push( + $notification->push( sprintf(ngettext("Copied %d photo to %s", "Copied %d photos to %s", $result), $result, $newGallery->get('name')), 'horde.success'); - } - } + } catch (Ansel_Exception $e) { + $notification->push(_("Bad input."), 'horde.error'); + + } } /* Return to the image list. */ @@ -693,13 +688,11 @@ case 'downloadzip': $galleryId = Horde_Util::getFormData('gallery'); if ($galleryId) { $gallery = $ansel_storage->getGallery($galleryId); - if (!Horde_Auth::getAuth() || is_a($gallery, 'PEAR_Error') || + if (!Horde_Auth::getAuth() || !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ) || $gallery->hasPasswd() || !$gallery->isOldEnough()) { - $name = is_a($gallery, 'PEAR_Error') ? $galleryId : $gallery->get('name'); - $notification->push(sprintf(_("Access denied downloading photos from \"%s\"."), $name), - 'horde.error'); + $notification->push(sprintf(_("Access denied downloading photos from \"%s\"."), $gallery->get('name')), 'horde.error'); header('Location: ' . Horde::applicationUrl('view.php?view=List', true)); exit; } diff --git a/ansel/img/download.php b/ansel/img/download.php index 984725c02..260b50e28 100644 --- a/ansel/img/download.php +++ b/ansel/img/download.php @@ -11,20 +11,11 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); -$id = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal($image, __FILE__, __LINE__); -} +$image = $ansel_storage->getImage(Horde_Util::getFormData('image')); $gallery = $ansel_storage->getGallery($image->gallery); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal($gallery, __FILE__, __LINE__); -} -if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ) || - !$gallery->canDownload()) { - Horde::fatal(_("Access denied viewing this photo."), __FILE__, __LINE__); +if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ) || !$gallery->canDownload()) { + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this photo."), __FILE__, __LINE__); } - $image->downloadHeaders(); /* Sendfile support. Lighttpd < 1.5 only understands the X-LIGHTTPD-send-file header */ @@ -36,6 +27,4 @@ if ($conf['vfs']['src'] == 'sendfile') { exit; } -if (is_a($result = $image->display('full'), 'PEAR_Error')) { - Horde::fatal($result, __FILE__, __LINE__); -} +$image->display('full'); diff --git a/ansel/img/ecard.php b/ansel/img/ecard.php index f91f3d84c..f4b9b7864 100644 --- a/ansel/img/ecard.php +++ b/ansel/img/ecard.php @@ -18,13 +18,7 @@ if (empty($conf['ecard']['enable'])) { /* Get the gallery and the image, and abort if either fails. */ $gallery = $ansel_storage->getGallery(Horde_Util::getFormData('gallery')); -if (is_a($gallery, 'PEAR_Error')) { - exit; -} $image = &$gallery->getImage(Horde_Util::getFormData('image')); -if (is_a($image, 'PEAR_Error')) { - exit; -} /* Run through the action handlers. */ switch (Horde_Util::getFormData('actionID')) { @@ -83,13 +77,13 @@ case 'send': $alt->setBasePart($alternative); /* Send. */ - $result = $alt->send($injector->getInstance('Horde_Mail')); - if (is_a($result, 'PEAR_Error')) { - $notification->push(sprintf(_("There was an error sending your message: %s"), $result->getMessage()), 'horde.error'); - } else { - echo Horde::wrapInlineScript(array('window.close();')); - exit; + try { + $result = $alt->send($injector->getInstance('Horde_Mail')); + } catch (Horde_Mime_Exception $e) { + $notification->push(sprintf(_("There was an error sending your message: %s"), $e->getMessage()), 'horde.error'); } + echo Horde::wrapInlineScript(array('window.close();')); + exit; } $title = sprintf(_("Send Ecard :: %s"), $image->filename); diff --git a/ansel/img/full.php b/ansel/img/full.php index 462627745..b9a142739 100644 --- a/ansel/img/full.php +++ b/ansel/img/full.php @@ -11,18 +11,10 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); -$id = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal($image, __FILE__, __LINE__); -} +$image = $ansel_storage->getImage(Horde_Util::getFormData('image')); $gallery = $ansel_storage->getGallery($image->gallery); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal($gallery, __FILE__, __LINE__); -} -if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ) || - !$gallery->canDownload()) { - Horde::fatal(_("Access denied viewing this photo."), __FILE__, __LINE__); +if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ) || !$gallery->canDownload()) { + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this photo.")); } /* Sendfile support. Lighttpd < 1.5 only understands the X-LIGHTTPD-send-file header */ @@ -33,7 +25,4 @@ if ($conf['vfs']['src'] == 'sendfile') { header('X-Sendfile: ' . $filename); exit; } - -if (is_a($result = $image->display('full'), 'PEAR_Error')) { - Horde::fatal($result, __FILE__, __LINE__); -} +$image->display('full'); diff --git a/ansel/img/index.php b/ansel/img/index.php index acefa9459..618db219c 100644 --- a/ansel/img/index.php +++ b/ansel/img/index.php @@ -11,17 +11,10 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); -$id = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal($image, __FILE__, __LINE__); -} +$image = &$ansel_storage->getImage(Horde_Util::getFormData('image')); $gallery = $ansel_storage->getGallery($image->gallery); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal($gallery, __FILE__, __LINE__); -} if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - Horde::fatal(_("Access denied viewing this photo."), __FILE__, __LINE__); + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this photo.")); } /* Sendfile support. Lighttpd < 1.5 only understands the X-LIGHTTPD-send-file header */ @@ -33,6 +26,4 @@ if ($conf['vfs']['src'] == 'sendfile') { exit; } -if (is_a($result = $image->display('screen'), 'PEAR_Error')) { - Horde::fatal($result, __FILE__, __LINE__); -} +$image->display('screen'); diff --git a/ansel/img/mini.php b/ansel/img/mini.php index 254606717..6a24267c7 100644 --- a/ansel/img/mini.php +++ b/ansel/img/mini.php @@ -11,17 +11,10 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); -$id = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal($image, __FILE__, __LINE__); -} +$image = $ansel_storage->getImage(Horde_Util::getFormData('image')); $gallery = $ansel_storage->getGallery(abs($image->gallery)); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal($gallery, __FILE__, __LINE__); -} if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - Horde::fatal(_("Access denied viewing this photo."), __FILE__, __LINE__); + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this photo.")); } /* Sendfile support. Lighttpd < 1.5 only understands the X-LIGHTTPD-send-file header */ @@ -39,7 +32,4 @@ if ($conf['vfs']['src'] == 'sendfile') { header('X-Sendfile: ' . $filename); exit; } - -if (is_a($result = $image->display('mini'), 'PEAR_Error')) { - Horde::fatal($result, __FILE__, __LINE__); -} +$image->display('mini'); diff --git a/ansel/img/prettythumb.php b/ansel/img/prettythumb.php index f053bb4f7..ab4fb98f9 100644 --- a/ansel/img/prettythumb.php +++ b/ansel/img/prettythumb.php @@ -12,17 +12,10 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); $style = Horde_Util::getFormData('style'); -$id = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal($image, __FILE__, __LINE__); -} +$image = $ansel_storage->getImage(Horde_Util::getFormData('image')); $gallery = $ansel_storage->getGallery(abs($image->gallery)); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal($gallery, __FILE__, __LINE__); -} if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - Horde::fatal(_("Access denied viewing this photo."), __FILE__, __LINE__); + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this photo.")); } /* Sendfile support. Lighttpd < 1.5 only understands the X-LIGHTTPD-send-file header */ diff --git a/ansel/img/screen.php b/ansel/img/screen.php index b89248708..af5651df4 100644 --- a/ansel/img/screen.php +++ b/ansel/img/screen.php @@ -11,17 +11,10 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); -$id = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal($image, __FILE__, __LINE__); -} +$image = &$ansel_storage->getImage(Horde_Util::getFormData('image')); $gallery = $ansel_storage->getGallery($image->gallery); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal($gallery, __FILE__, __LINE__); -} if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - Horde::fatal(_("Access denied viewing this photo."), __FILE__, __LINE__); + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this photo.")); } /* Sendfile support. Lighttpd < 1.5 only understands the X-LIGHTTPD-send-file header */ @@ -39,7 +32,4 @@ if ($conf['vfs']['src'] == 'sendfile') { header('X-Sendfile: ' . $filename); exit; } - -if (is_a($result = $image->display('screen'), 'PEAR_Error')) { - Horde::fatal($result, __FILE__, __LINE__); -} +$image->display('screen'); diff --git a/ansel/img/thumb.php b/ansel/img/thumb.php index 7b219043b..2918cfbe0 100644 --- a/ansel/img/thumb.php +++ b/ansel/img/thumb.php @@ -11,17 +11,10 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); -$id = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal($image, __FILE__, __LINE__); -} +$image = $ansel_storage->getImage(Horde_Util::getFormData('image')); $gallery = $ansel_storage->getGallery(abs($image->gallery)); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal($gallery, __FILE__, __LINE__); -} if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - Horde::fatal(_("Access denied viewing this photo."), __FILE__, __LINE__); + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this photo.")); } /* Sendfile support. Lighttpd < 1.5 only understands the X-LIGHTTPD-send-file header */ @@ -40,6 +33,4 @@ if ($conf['vfs']['src'] == 'sendfile') { exit; } -if (is_a($result = $image->display('thumb'), 'PEAR_Error')) { - Horde::fatal($result, __FILE__, __LINE__); -} +$image->display('thumb'); diff --git a/ansel/img/upload.php b/ansel/img/upload.php index 6492c7ad7..d39beafbd 100644 --- a/ansel/img/upload.php +++ b/ansel/img/upload.php @@ -12,8 +12,9 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); $gallery_id = Horde_Util::getFormData('gallery'); -$gallery = &$ansel_storage->getGallery($gallery_id); -if (is_a($gallery, 'PEAR_Error')) { +try { + $gallery = $ansel_storage->getGallery($gallery_id); +} catch (Ansel_Exception $e) { $notification->push(sprintf(_("Gallery %s not found."), $gallery_id), 'horde.error'); header('Location: ' . Ansel::getUrlFor('view', array('view' => 'List'), true)); exit; @@ -93,22 +94,22 @@ if ($form->validate($vars)) { 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')) { + /* If we successfully got data, try adding the image */ + try { + $image_id = $gallery->addImage( + array( + 'image_filename' => $zinfo['name'], + 'image_caption' => '', + 'data' => $zdata) + ); ++$uploaded; if ($conf['image']['autogen'] > count($image_ids)) { $image_ids[] = $image_id; } - } else { + } catch (Ansel_Exception $e) { $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id), 'horde.error'); } + unset($zdata); } $zip->close(); @@ -118,11 +119,11 @@ if ($form->validate($vars)) { $data = file_get_contents($info['file' . $i]['file']); /* 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'); + try { + $zip = Horde_Compress::factory('zip'); + $files = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_LIST)); + } catch (Horde_Exception $e) { + $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $e->getMessage()), 'horde.error'); continue; } @@ -145,32 +146,32 @@ if ($form->validate($vars)) { 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'); + try { + $zdata = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_DATA, + 'info' => $files, + 'key' => $key)); + } catch (Horde_Exception $e) { + $notification->push(sprintf(_("There was an error processing the uploaded archive: %s"), $e->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')) { + /* If we successfully got data, try adding the image */ + try { + $image_id = $gallery->addImage( + array( + 'image_filename' => $zinfo['name'], + 'image_caption' => '', + 'data' => $zdata) + ); ++$uploaded; if ($conf['image']['autogen'] > count($image_ids)) { $image_ids[] = $image_id; } - } else { + } catch (Ansel_Exception $e) { $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id), 'horde.error'); } + unset($zdata); } - unset($zip); unset($data); } @@ -191,15 +192,15 @@ if ($form->validate($vars)) { '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 { + try { + $image_id = $gallery->addImage($image_data, (bool)$vars->get('image' . $i . '_default')); ++$uploaded; $image_ids[] = $image_id; + } catch (Ansel_Exception $e) { + $notification->push(sprintf(_("There was a problem saving the photo: %s"), $image_id->getMessage()), 'horde.error'); + $valid = false; } + unset($data); } } diff --git a/ansel/img/upload_preview.php b/ansel/img/upload_preview.php index 904ab7bf3..1204a7bfb 100644 --- a/ansel/img/upload_preview.php +++ b/ansel/img/upload_preview.php @@ -10,12 +10,9 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('ansel'); - -$gallery_id = (int)Horde_Util::getFormData('gallery'); -$gallery = $ansel_storage->getGallery($gallery_id); -if (is_a($gallery, 'PEAR_Error') || - !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - die(sprintf(_("Gallery %s not found."), $gallery_id)); +$gallery = $ansel_storage->getGallery((int)Horde_Util::getFormData('gallery')); +if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { + throw new Horde_Exception_PermissionDenied(); } $from = (int)Horde_Util::getFormData('from'); @@ -23,10 +20,6 @@ $to = (int)Horde_Util::getFormData('to'); $count = $to - $from + 1; $images = $gallery->getImages($from, $count); -if (is_a($images, 'PEAR_Error')) { - die($images->getError()); -} - foreach ($images as $image) { echo '
  • '; echo '
    '; diff --git a/ansel/lib/Ajax/Imple/EditCaption.php b/ansel/lib/Ajax/Imple/EditCaption.php index ee0aee94a..8019c7f54 100644 --- a/ansel/lib/Ajax/Imple/EditCaption.php +++ b/ansel/lib/Ajax/Imple/EditCaption.php @@ -70,8 +70,9 @@ class Ansel_Ajax_Imple_EditCaption extends Horde_Ajax_Imple_Base $g = $GLOBALS['ansel_storage']->getGallery($image->gallery); if ($g->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { $image->caption = $pref_value; - $result = $image->save(); - if (is_a($result, 'PEAR_Error')) { + try { + $result = $image->save(); + } catch (Ansel_Exception $e) { return ''; } } diff --git a/ansel/lib/Ajax/Imple/ImageSaveGeotag.php b/ansel/lib/Ajax/Imple/ImageSaveGeotag.php index 92467230e..c2ecd129c 100644 --- a/ansel/lib/Ajax/Imple/ImageSaveGeotag.php +++ b/ansel/lib/Ajax/Imple/ImageSaveGeotag.php @@ -38,14 +38,13 @@ class Ansel_Ajax_Imple_ImageSaveGeotag extends Horde_Ajax_Imple_Base } // Get the image and gallery to check perms - $image = $GLOBALS['ansel_storage']->getImage((int)$img); - if (is_a($image, 'PEAR_Error')) { - return array('response' => 0); - } - $gallery = $GLOBALS['ansel_storage']->getGallery($image->gallery); - if (is_a($gallery, 'PEAR_Error')) { + try { + $image = $GLOBALS['ansel_storage']->getImage((int)$img); + $gallery = $GLOBALS['ansel_storage']->getGallery($image->gallery); + } catch (Ansel_Exception $e) { return array('response' => 0); } + // Bail out if no perms on the image. if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { return array('response' => 0); diff --git a/ansel/lib/Ajax/Imple/LocationAutoCompleter.php b/ansel/lib/Ajax/Imple/LocationAutoCompleter.php index e7f65766e..98dcc608a 100644 --- a/ansel/lib/Ajax/Imple/LocationAutoCompleter.php +++ b/ansel/lib/Ajax/Imple/LocationAutoCompleter.php @@ -54,12 +54,13 @@ class Ansel_Ajax_Imple_LocationAutoCompleter extends Horde_Ajax_Imple_AutoComple !($input = Horde_Util::getFormData($args['input']))) { return array(); } - $locs = $GLOBALS['ansel_storage']->searchLocations($input); - if (is_a($locs, 'PEAR_Error')) { - Horde::logMessage($locs->getMessage(), 'ERR'); - $locs = new StdClass(); - } - if (!count($locs)) { + try { + $locs = $GLOBALS['ansel_storage']->searchLocations($input); + if (!count($locs)) { + $locs = new StdClass(); + } + } catch (Ansel_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); $locs = new StdClass(); } return $locs; diff --git a/ansel/lib/Ansel.php b/ansel/lib/Ansel.php index c745826bf..51348f290 100644 --- a/ansel/lib/Ansel.php +++ b/ansel/lib/Ansel.php @@ -27,15 +27,16 @@ class Ansel /** * Create and initialize the database object. * - * @return mixed MDB2 object || PEAR_Error + * @return mixed MDB2 object + * @throws Ansel_Exception */ static public function &getDb() { $config = $GLOBALS['conf']['sql']; unset($config['charset']); $mdb = MDB2::singleton($config); - if (is_a($mdb, 'PEAR_Error')) { - return $mdb; + if ($mdb instanceof PEAR_Error) { + throw new Ansel_Exception($mdb->getMessage()); } $mdb->setOption('seqcol_name', 'id'); @@ -60,6 +61,8 @@ class Ansel */ static public function getVFS() { + // @TODO: need to refactor the Vfs binder to a factory so we can handle + // vfs objects for different scopes. $v_params = Horde::getVFSConfig('images'); return VFS::singleton($v_params['type'], $v_params['params']); } @@ -133,8 +136,6 @@ class Ansel } /** - * Return a link to a photo placeholder, suitable for use in an - * tag (or a Horde::img() call, with the path parameter set to * ''). * This photo should be used as a placeholder if the correct photo can't * be retrieved * @@ -155,10 +156,9 @@ class Ansel * @param string $controller The controller to generate a URL for. * @param array $data The data needed to generate the URL. * @param boolean $full Generate a full URL. - * @param integer $append_session 0 = only if needed, 1 = always, - * -1 = never. + * @param integer $append_session 0 = only if needed, 1 = always, -1 = never. * - * @param string The generated URL + * @return Horde_Url The generated URL */ static public function getUrlFor($controller, $data, $full = false, $append_session = 0) { @@ -211,7 +211,7 @@ class Ansel if ($data['view'] == 'Gallery' || $data['view'] == 'Image') { /** - * This is needed to correctly generate URLs for images in + * @TODO: This is needed to correctly generate URLs for images in * places that are not specifically requested by the user, * for instance, in a gallery block. Otherwise, the proper * date variables would not be attached to the url, since we @@ -224,11 +224,9 @@ class Ansel // Getting these objects is not ideal, but at this point // they should already be locally cached so the cost // is minimized. - $i = &$GLOBALS['ansel_storage']->getImage($data['image']); - $g = &$GLOBALS['ansel_storage']->getGallery($data['gallery']); - if (!is_a($g, 'PEAR_Error') && - !is_a($i, 'PEAR_Error') && - $g->get('view_mode') == 'Date') { + $i = $GLOBALS['ansel_storage']->getImage($data['image']); + $g = $GLOBALS['ansel_storage']->getGallery($data['gallery']); + if ($g->get('view_mode') == 'Date') { $imgDate = new Horde_Date($i->originalDate); $data['year'] = $imgDate->year; @@ -407,7 +405,7 @@ class Ansel * @param boolean $full Return a path that includes the server name? * @param string $style Use this gallery style * - * @return string The image path. + * @return Horde_Url The image path. */ static public function getImageUrl($imageId, $view = 'screen', $full = false, $style = null) { @@ -428,7 +426,7 @@ class Ansel } if (empty($imageId)) { - return Ansel::getErrorImage($view); + return Horde::applicationUrl((string)Ansel::getErrorImage($view), $full); } // Default to ansel_default since we really only need to know the style @@ -446,15 +444,14 @@ class Ansel $image = $ansel_storage->getImage($imageId); } catch (Ansel_Exception $e) { Horde::logMessage($e, 'ERR'); - return Ansel::getErrorImage($view); + return Horde::applicationUrl((string)Ansel::getErrorImage($view), $full); } try { $image->createView($view, $style, false); } catch (Ansel_Exception $e) { - return Ansel::getErrorImage($view); + return Horde::applicationUrl((string)Ansel::getErrorImage($view), $full); } - $viewHash = $image->getViewHash($view, $style) . '/' - . $image->getVFSName($view); + $viewHash = $image->getViewHash($view, $style) . '/' . $image->getVFSName($view); } // First check for vfs-direct. If we are not using it, pass this off to @@ -464,17 +461,15 @@ class Ansel if (!is_null($style)) { $params['style'] = $style; } - $url = Horde_Util::addParameter('img/' . $view . '.php', $params); - return Horde::applicationUrl($url, $full); + return Horde::applicationUrl(Horde_Util::addParameter('img/' . $view . '.php', $params), $full); } // Using vfs-direct - $path = substr(str_pad($imageId, 2, 0, STR_PAD_LEFT), -2) . '/' - . $viewHash; + $path = substr(str_pad($imageId, 2, 0, STR_PAD_LEFT), -2) . '/' . $viewHash; if ($full && substr($conf['vfs']['path'], 0, 7) != 'http://') { return Horde::url($conf['vfs']['path'] . $path, true, -1); } else { - return $conf['vfs']['path'] . htmlspecialchars($path); + return new Horde_Url($conf['vfs']['path'] . htmlspecialchars($path)); } } @@ -483,7 +478,7 @@ class Ansel * * @param array $params Any additional parameters * - * @return Horde_Image object | PEAR_Error + * @return Horde_Image object */ static public function getImageObject($params = array()) { @@ -507,13 +502,13 @@ class Ansel * @param string $file The filename of the image. * @param array $override Overwrite the file array with these values. * - * @return array The image data of the file as an array or PEAR_Error + * @return array The image data of the file as an array + * @throws Horde_Exception_NotFound */ static public function getImageFromFile($file, $override = array()) { if (!file_exists($file)) { - return PEAR::raiseError(sprintf(_("The file \"%s\" doesn't exist."), - $file)); + throw new Horde_Exception_NotFound(sprintf(_("The file \"%s\" doesn't exist."), $file)); } global $conf; @@ -521,7 +516,7 @@ class Ansel // Get the mime type of the file (and make sure it's an image). $mime_type = Horde_Mime_Magic::analyzeFile($file, isset($conf['mime']['magic_db']) ? $conf['mime']['magic_db'] : null); if (strpos($mime_type, 'image') === false) { - return PEAR::raiseError(sprintf(_("Can't get unknown file type \"%s\"."), $file)); + throw new Horde_Exception_NotFound(sprintf(_("Can't get unknown file type \"%s\"."), $file)); } $image = array('image_filename' => basename($file), @@ -571,6 +566,8 @@ class Ansel /** * Build Ansel's list of menu items. + * + * @return Horde_Menu */ static public function getMenu() { @@ -635,6 +632,8 @@ class Ansel /** * Generate a list of breadcrumbs showing where we are in the gallery * tree. + * + * @return string */ static public function getBreadCrumbs($separator = ' » ', $gallery = null) { @@ -650,15 +649,13 @@ class Ansel if (is_null($gallery)) { $gallery_id = (int)Horde_Util::getFormData('gallery'); $gallery_slug = Horde_Util::getFormData('slug'); - if (!empty($gallery_slug)) { - $gallery = $ansel_storage->getGalleryBySlug($gallery_slug); - } elseif (!empty($gallery_id)) { - $gallery = $ansel_storage->getGallery($gallery_id); - } - } - - if (is_a($gallery, 'PEAR_Error')) { - $gallery = null; + try { + if (!empty($gallery_slug)) { + $gallery = $ansel_storage->getGalleryBySlug($gallery_slug); + } elseif (!empty($gallery_id)) { + $gallery = $ansel_storage->getGallery($gallery_id); + } + } catch (Ansel_Exception $e) {} } if ($gallery) { @@ -666,12 +663,12 @@ class Ansel } if (!empty($image_id)) { - $image = &$ansel_storage->getImage($image_id); - if (empty($gallery) && !is_a($image, 'PEAR_Error')) { + $image = $ansel_storage->getImage($image_id); + if (empty($gallery)) { $gallery = $ansel_storage->getGallery($image->gallery); } } - if (isset($gallery) && !is_a($gallery, 'PEAR_Error')) { + if (isset($gallery)) { $owner = $gallery->get('owner'); } if (!empty($owner)) { @@ -708,7 +705,7 @@ class Ansel 'force_grouping' => true); // Check for an active image - if (!empty($image_id) && !is_a($image, 'PEAR_Error')) { + if (!empty($image_id)) { $text = '' . htmlspecialchars($image->filename, ENT_COMPAT, Horde_Nls::getCharset()) . ''; $nav = $separator . $text . $nav; $levels++; @@ -756,9 +753,7 @@ class Ansel $nav = $text . $nav; } - $nav = '' . $nav; - - return $nav; + return '' . $nav . ''; } /** @@ -800,12 +795,14 @@ class Ansel foreach ($options as $key => $option) { $html .= ' '; } - $html .= ''; - return $html; + + return $html .= ''; } /** * Get an array of all currently viewable styles. + * + * @return array */ static public function getAvailableStyles() { @@ -837,6 +834,7 @@ class Ansel } } } + return $styles; } @@ -946,7 +944,7 @@ class Ansel * * @param array $date A full date parts array or an empty array. * - * @return A trimmed down (if necessary) date parts array. + * @return array A trimmed down (if necessary) date parts array. */ static public function getDateParameter($date = array()) { @@ -966,10 +964,11 @@ class Ansel /** * Downloads all requested images as a zip file. Assumes all permissions - * have been checked on the requested resource. + * have been checked on the requested resource. Can request either a + * single gallery of images, OR an array of individual image ids. * - * @param array $gallery - * @param array $images + * @param Ansel_Gallery $gallery The galleries to download + * @param array $images The images to download */ static public function downloadImagesAsZip($gallery = null, $images = array()) { @@ -1018,20 +1017,18 @@ class Ansel $zipfiles = array(); foreach ($images as $id) { - $image = &$GLOBALS['ansel_storage']->getImage($id); - if (!is_a($image, 'PEAR_Error')) { - // If we didn't select an entire gallery, check the download - // size for each image. - if (!isset($view)) { - $g = $GLOBALS['ansel_storage']->getGallery($image->gallery); - $v = $g->canDownload() ? 'full' : 'screen'; - } else { - $v = $view; - } - - $zipfiles[] = array('data' => $image->raw($v), - 'name' => $image->filename); + $image = $GLOBALS['ansel_storage']->getImage($id); + // If we didn't select an entire gallery, check the download + // size for each image. + if (!isset($view)) { + $g = $GLOBALS['ansel_storage']->getGallery($image->gallery); + $v = $g->canDownload() ? 'full' : 'screen'; + } else { + $v = $view; } + + $zipfiles[] = array('data' => $image->raw($v), + 'name' => $image->filename); } $zip = Horde_Compress::factory('zip'); @@ -1053,7 +1050,7 @@ class Ansel * * @param array $options The options to build the view. * - * @return string The javascript + * @return string The javascript code */ static public function embedCode($options) { diff --git a/ansel/lib/Api.php b/ansel/lib/Api.php index 8ca9ba69d..aafed3659 100644 --- a/ansel/lib/Api.php +++ b/ansel/lib/Api.php @@ -73,33 +73,32 @@ class Ansel_Api extends Horde_Registry_Api } else { if (count($parts) == 1) { // This request is for all galleries owned by the requested user. - $galleries = $GLOBALS['ansel_storage']->listGalleries( - Horde_Perms::SHOW, $parts[0], null, false); + $galleries = $GLOBALS['ansel_storage']->listGalleries(Horde_Perms::SHOW, $parts[0], null, false); $images = array(); } elseif ($this->galleryExists(null, end($parts))) { // This request if for a certain gallery, list all sub-galleries // and images. $gallery_id = end($parts); - $galleries = $GLOBALS['ansel_storage']->getGalleries( - array($gallery_id)); + $galleries = $GLOBALS['ansel_storage']->getGalleries(array($gallery_id)); if (!isset($galleries[$gallery_id]) || - !$galleries[$gallery_id]->hasPermission(Horde_Auth::getAuth(), - Horde_Perms::READ)) { - return PEAR::raiseError(_("Invalid gallery specified."), 404); - } - $galleries = $GLOBALS['ansel_storage']->listGalleries( - Horde_Perms::SHOW, null, $gallery_id, false); + !$galleries[$gallery_id]->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { + throw new Horde_Exception_NotFound(_("Invalid gallery specified.")); + } + $galleries = $GLOBALS['ansel_storage']->listGalleries(Horde_Perms::SHOW, null, $gallery_id, false); $images = $this->listImages(null, $gallery_id, Horde_Perms::SHOW, 'mini'); + } elseif (count($parts) > 2 && - $this->galleryExists(null, $parts[count($parts) - 2]) && - !is_a($image = $GLOBALS['ansel_storage']->getImage(end($parts)), 'PEAR_Error')) { - return array('data' => $image->raw(), - 'mimetype' => $image->type, - 'mtime' => $image->uploaded); - } else { - return PEAR::raiseError(_("File not found."), 404); - } + $this->galleryExists(null, $parts[count($parts) - 2]) && + ($image = $GLOBALS['ansel_storage']->getImage(end($parts)))) { + + return array('data' => $image->raw(), + 'mimetype' => $image->type, + 'mtime' => $image->uploaded); + + } else { + throw new Horde_Exception_NotFound(_("File not found.")); + } $results = array(); foreach ($galleries as $galleryId => $gallery) { @@ -163,7 +162,7 @@ class Ansel_Api extends Horde_Registry_Api } - return PEAR::raiseError(_("File not found."), 404); + throw Horde_Exception_NotFound(_("File not found."), 404); } /** @@ -184,19 +183,20 @@ class Ansel_Api extends Horde_Registry_Api $parts = explode('/', $path); if (count($parts) < 3) { - return PEAR::raiseError("Gallery does not exist"); + throw new Horde_Exception_NotFound("Gallery does not exist"); } $image_name = array_pop($parts); $gallery_id = end($parts); if (!$GLOBALS['ansel_storage']->galleryExists($gallery_id)) { - return PEAR::raiseError("Gallery does not exist"); + throw new Horde_Exception_NotFound("Gallery does not exist"); } $gallery = $GLOBALS['ansel_storage']->getGallery($gallery_id); if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - return PEAR::raiseError(_("Access denied adding photos to \"%s\".")); + throw new Horde_Exception_PermissionDenied(_("Access denied adding photos to \"%s\".")); } - return $gallery->addImage(array('image_type' => $content_type, + return $gallery->addImage(array( + 'image_type' => $content_type, 'image_filename' => $image_name, 'image_caption' => '', 'data' => $content)); @@ -215,8 +215,11 @@ class Ansel_Api extends Horde_Registry_Api return false; } - $image = $GLOBALS['ansel_storage']->getImage($image_id); - if (!$image || is_a($image, 'PEAR_Error')) { + try { + if (!($image = $GLOBALS['ansel_storage']->getImage($image_id))) { + return false; + } + } catch (Ansel_Exception $e) { return false; } @@ -301,8 +304,8 @@ class Ansel_Api extends Horde_Registry_Api * @return mixed An array of image/gallery data || PEAR_Error */ public function saveImage($app = null, $gallery_id, $image, $default = false, - $gallery_data = null, $encoding = null, $slug = null, - $compression = 'none', $skiphook = false) + $gallery_data = null, $encoding = null, $slug = null, + $compression = 'none', $skiphook = false) { $image_data = null; @@ -326,24 +329,18 @@ class Ansel_Api extends Horde_Registry_Api } if (is_null($image_data) && getimagesize($image['file']) === false) { - return PEAR::raiseError(_("The file you uploaded does not appear to be a valid photo.")); + throw new InvalidArgumentException(_("The file you uploaded does not appear to be a valid photo.")); } if (empty($slug) && empty($gallery_id)) { - return PEAR::raiseError(_("A gallery to add this photo to is required.")); + throw new InvalidArgumentException(_("A gallery to add this photo to is required.")); } if (!empty($slug)) { $gallery = $GLOBALS['ansel_storage']->getGalleryBySlug($slug); - if (is_a($gallery, 'PEAR_Error')) { - return $gallery; - } } elseif ($GLOBALS['ansel_storage']->galleryExists($gallery_id)) { $gallery = $GLOBALS['ansel_storage']->getGallery($gallery_id); - if (is_a($gallery, 'PEAR_Error')) { - return $gallery; - } } if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - return PEAR::raiseError(sprintf(_("Access denied adding photos to \"%s\"."), $gallery->get('name'))); + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied adding photos to \"%s\"."), $gallery->get('name'))); } if (!empty($gallery_data)) { foreach ($gallery_data as $key => $value) { @@ -367,9 +364,6 @@ class Ansel_Api extends Horde_Registry_Api } $image_id = $gallery->addImage($image_data, $default); - if (is_a($image_id, 'PEAR_Error')) { - return $image_id; - } // Call the postupload hook if needed if (!empty($GLOBALS['conf']['hooks']['postupload']) && !$skiphook) { @@ -379,9 +373,9 @@ class Ansel_Api extends Horde_Registry_Api } return array('image_id' => (int)$image_id, - 'gallery_id' => (int)$gallery->id, - 'gallery_slug' => $gallery->get('slug'), - 'image_count' => (int)$gallery->countImages()); + 'gallery_id' => (int)$gallery->id, + 'gallery_slug' => $gallery->get('slug'), + 'image_count' => (int)$gallery->countImages()); } /** @@ -409,7 +403,7 @@ class Ansel_Api extends Horde_Registry_Api { /* Check global Ansel permissions */ if (!$GLOBALS['injector']->getInstance('Horde_Perms')->getPermissions('ansel')) { - return PEAR::raiseError(_("Access denied deleting galleries.")); + throw new Horde_Exception_PermissionDenied(_("Access denied deleting galleries.")); } /* If no app is given use Ansel's own gallery which is initialized in @@ -419,15 +413,10 @@ class Ansel_Api extends Horde_Registry_Api } $image = $GLOBALS['ansel_storage']->getImage($image_id); - if (is_a($image, 'PEAR_Error')) { - return $image; - } $gallery = $GLOBALS['ansel_storage']->getGallery($image->gallery); - if (is_a($gallery, 'PEAR_Error') || - !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { - - return PEAR::raiseError(sprintf(_("Access denied deleting photos from \"%s\"."), $gallery->get('name'))); - } + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied deleting photos from \"%s\"."), $gallery->get('name'))); + } return $gallery->removeImage($image); } @@ -450,8 +439,9 @@ class Ansel_Api extends Horde_Registry_Api if (!(Horde_Auth::isAdmin() || (!$GLOBALS['injector']->getInstance('Horde_Perms')->exists('ansel') && Horde_Auth::getAuth()) || $GLOBALS['injector']->getInstance('Horde_Perms')->hasPermission('ansel', Horde_Auth::getAuth(), Horde_Perms::EDIT))) { - return PEAR::raiseError(_("Access denied creating new galleries.")); - } + + throw new Horde_Exception_PermissionDenied(_("Access denied creating new galleries.")); + } if (!is_null($app)) { $GLOBALS['ansel_storage'] = new Ansel_Storage($app); @@ -467,9 +457,7 @@ class Ansel_Api extends Horde_Registry_Api } $gallery = $GLOBALS['ansel_storage']->createGallery($attributes, $permobj, $parent); - if (is_a($gallery, 'PEAR_Error')) { - return $gallery; - } + return $gallery->id; } @@ -485,7 +473,7 @@ class Ansel_Api extends Horde_Registry_Api { /* Check global Ansel permissions */ if (!$GLOBALS['injector']->getInstance('Horde_Perms')->getPermissions('ansel')) { - return PEAR::raiseError(_("Access denied deleting galleries.")); + throw new Horde_Exception_PermissionDenied(_("Access denied deleting galleries.")); } /* If no app is given use Ansel's own gallery which is initialized in @@ -495,12 +483,8 @@ class Ansel_Api extends Horde_Registry_Api } $gallery = $GLOBALS['ansel_storage']->getGallery($gallery_id); - if (is_a($gallery, 'PEAR_Error')) { - return PEAR::raiseError(sprintf(_("Access denied deleting gallery \"%s\"."), - $gallery->getMessage())); - } elseif (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { - return PEAR::raiseError(sprintf(_("Access denied deleting gallery \"%s\"."), - $gallery->get('name'))); + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied deleting gallery \"%s\"."), $gallery->get('name'))); } else { $imageList = $gallery->listImages(); if ($imageList) { @@ -508,14 +492,8 @@ class Ansel_Api extends Horde_Registry_Api $gallery->removeImage($id); } } - $result = $GLOBALS['ansel_storage']->removeGallery($gallery); - if (!is_a($result, 'PEAR_Error')) { - return true; - } else { - return PEAR::raiseError(sprintf(_("There was a problem deleting %s: %s"), - $gallery->get('name'), - $result->getMessage())); - } + + return $GLOBALS['ansel_storage']->removeGallery($gallery); } } @@ -536,16 +514,15 @@ class Ansel_Api extends Horde_Registry_Api $GLOBALS['ansel_storage'] = new Ansel_Storage($app); } - if (!empty($slug)) { - $gallery = $GLOBALS['ansel_storage']->getGalleryBySlug($slug); - } else { - $gallery = $GLOBALS['ansel_storage']->getGallery($gallery_id); - } - - if (is_a($gallery, 'PEAR_Error')) { - return 0; - } else { + try { + if (!empty($slug)) { + $gallery = $GLOBALS['ansel_storage']->getGalleryBySlug($slug); + } else { + $gallery = $GLOBALS['ansel_storage']->getGallery($gallery_id); + } return (int)$gallery->countImages(); + } catch (Ansel_Exception $e) { + return 0; } } @@ -574,11 +551,7 @@ class Ansel_Api extends Horde_Registry_Api $gallery = $GLOBALS['ansel_storage']->getGallery($gallery_id); } - if (is_a($gallery, 'PEAR_Error')) { - return $gallery; - } else { - return $gallery->getDefaultImage($style); - } + return $gallery->getDefaultImage($style); } /** @@ -619,55 +592,39 @@ class Ansel_Api extends Horde_Registry_Api * @return string The image path. */ public function getImageContent($image_id, $view = 'screen', $style = null, - $app = null, $encoding = null, $compression = 'none') + $app = null, $encoding = null, + $compression = 'none') { - /* If no app is given use Ansel's own gallery which is initialized in - base.php */ + /* If no app is given use Ansel's own gallery which is initialized in base.php */ if (!is_null($app)) { $GLOBALS['ansel_storage'] = new Ansel_Storage($app); } - // Get image + /* Get image and gallery */ $image = $GLOBALS['ansel_storage']->getImage($image_id); - if (is_a($image, 'PEAR_Error')) { - return $image; - } - - // Get gallery $gallery = $GLOBALS['ansel_storage']->getGallery($image->gallery); - if (is_a($gallery, 'PEAR_Error')) { - return $gallery; - } - // Check age and password + /* Check age and password */ if (!$gallery->hasPasswd() || !$gallery->isOldEnough()) { - return PEAR::raiseError(_("Locked galleries are not viewable via the api.")); + throw new Horde_Exception(_("Locked galleries are not viewable via the api.")); } if ($view == 'full') { - // Check permissions for full view + /* Check permissions for full view */ if (!$gallery->canDownload()) { - return PEAR::RaiseError(sprintf(_("Access denied downloading photos from \"%s\"."), $gallery->get('name'))); + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied downloading photos from \"%s\"."), $gallery->get('name'))); } - try { - $data = $GLOBALS['ansel_vfs']->read($image->getVFSPath('full'), - $image->getVFSName('full')); + $data = $GLOBALS['ansel_vfs']->read($image->getVFSPath('full'), $image->getVFSName('full')); } catch (VFS_Exception $e) { - return PEAR::raiseError($data->getMessage()); + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Ansel_Exception($e->getMessage()); } } else { - // Load View $result = $image->load($view, $style); - - // Return image content $data = $image->_image->raw(); } - if (is_a($data, 'PEAR_Error')) { - return $data; - } - return $this->_getImageData($data, $encoding, $compression, false); } @@ -689,24 +646,18 @@ class Ansel_Api extends Horde_Registry_Api * @return array An array of gallery information. */ public function listGalleries($app = null, $perm = Horde_Perms::SHOW, - $parent = null, - $allLevels = true, $from = 0, $count = 0, - $attributes = null, $sort_by = null, $direction = 0) + $parent = null, $allLevels = true, $from = 0, + $count = 0, $attributes = null, $sort_by = null, + $direction = 0) { /* If no app is given use Ansel's own gallery which is initialized in base.php */ if (!is_null($app)) { $GLOBALS['ansel_storage'] = new Ansel_Storage($app); } - - $galleries = $GLOBALS['ansel_storage']->listGalleries( $perm, $attributes, $parent, $allLevels, $from, $count, $sort_by, $direction); - if (is_a($galleries, 'PEAR_Error')) { - return $galleries; - } - $return = array(); foreach ($galleries as $gallery) { $return[$gallery->id] = array_merge($gallery->data, array('crumbs' => $gallery->getGalleryCrumbData())); @@ -736,10 +687,6 @@ class Ansel_Api extends Horde_Registry_Api $results = $GLOBALS['ansel_storage']->getGalleries($ids); } - if (is_a($results, 'PEAR_Error')) { - return $results; - } - /* We can't just return the results of the getGalleries call - we need to ensure the caller has at least Horde_Perms::READ on the galleries. */ $galleries = array(); @@ -826,22 +773,13 @@ class Ansel_Api extends Horde_Registry_Api } else { $gallery = $GLOBALS['ansel_storage']->getGallery($gallery_id); } - if (is_a($gallery, 'PEAR_Error')) { - return $gallery; - } $images = $gallery->listImages(); - if (is_a($images, 'PEAR_Error')) { - return $images; - } $counter = 0; $imagelist = array(); foreach ($images as $id) { $image = $GLOBALS['ansel_storage']->getImage($id); - if (is_a($image, 'PEAR_Error')) { - return $image; - } $imagelist[$id]['name'] = $image->filename; $imagelist[$id]['caption'] = $image->caption; $imagelist[$id]['type'] = $image->type; @@ -969,20 +907,18 @@ class Ansel_Api extends Horde_Registry_Api * @return mixed An array of results | PEAR_Error */ public function searchTags($names, $max = 10, $from = 0, - $resource_type = 'all', $user = null, $raw = false, - $app = null) + $resource_type = 'all', $user = null, $raw = false, + $app = null) { if (!is_null($app)) { $GLOBALS['ansel_storage'] = new Ansel_Storage($app); } else { $app = 'ansel'; } - - $results = Ansel_Tags::searchTags($names, $max, $from, $resource_type, - $user); + $results = Ansel_Tags::searchTags($names, $max, $from, $resource_type, $user); /* Check for error or if we requested the raw data array */ - if (is_a($results, 'PEAR_Error') || $raw) { + if ($raw) { return $results; } @@ -1018,7 +954,6 @@ class Ansel_Api extends Horde_Registry_Api } } - return $return; } diff --git a/ansel/lib/Application.php b/ansel/lib/Application.php index 2610d243e..b5f60283c 100644 --- a/ansel/lib/Application.php +++ b/ansel/lib/Application.php @@ -68,10 +68,6 @@ class Ansel_Application extends Horde_Registry_Application // Create db, share, and vfs instances. $GLOBALS['ansel_db'] = Ansel::getDb(); - if (is_a($GLOBALS['ansel_db'], 'PEAR_Error')) { - throw new Horde_Exception_Prior($GLOBALS['ansel_db']); - } - $GLOBALS['ansel_storage'] = new Ansel_Storage(); $GLOBALS['ansel_vfs'] = Ansel::getVFS(); diff --git a/ansel/lib/Block/gallery.php b/ansel/lib/Block/gallery.php index 6134d049e..02ede55a1 100644 --- a/ansel/lib/Block/gallery.php +++ b/ansel/lib/Block/gallery.php @@ -47,8 +47,9 @@ class Horde_Block_ansel_gallery extends Horde_Block { function _title() { - $gallery = $this->_getGallery(); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $this->_getGallery(); + } catch (Horde_Exception $e) { return Horde::link(Ansel::getUrlFor('view', array('view' => 'List'), true)) . _("Gallery") . ''; } @@ -74,8 +75,9 @@ class Horde_Block_ansel_gallery extends Horde_Block { } function _content() { - $gallery = $this->_getGallery(); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $this->_getGallery(); + } catch (Horde_Exception $e) { return $gallery->getMessage(); } @@ -111,7 +113,7 @@ class Horde_Block_ansel_gallery extends Horde_Block { function _getGallery($retry = false) { // Make sure we haven't already selected a gallery. - if (is_a($this->_gallery, 'Ansel_Gallery')) { + if ($this->_gallery instanceof Ansel_Gallery) { return $this->_gallery; } @@ -135,11 +137,10 @@ class Horde_Block_ansel_gallery extends Horde_Block { } if (empty($this->_gallery)) { - return PEAR::raiseError(_("Gallery does not exist.")); - } elseif (is_a($this->_gallery, 'PEAR_Error') || - !$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) || + throw new Horde_Exception_NotFound(_("Gallery does not exist.")); + } elseif (!$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) || !$this->_gallery->isOldEnough() || $this->_gallery->hasPasswd()) { - return PEAR::raiseError(_("Access denied viewing this gallery.")); + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this gallery.")); } // Return the gallery. diff --git a/ansel/lib/Block/my_galleries.php b/ansel/lib/Block/my_galleries.php index 49931f2a2..70e620c69 100644 --- a/ansel/lib/Block/my_galleries.php +++ b/ansel/lib/Block/my_galleries.php @@ -39,13 +39,14 @@ class Horde_Block_ansel_my_galleries extends Horde_Block { { Horde::addScriptFile('tooltips.js', 'horde'); /* Get the top level galleries */ - $galleries = $GLOBALS['ansel_storage']->listGalleries( - Horde_Perms::EDIT, Horde_Auth::getAuth(), null, false, 0, - empty($this->_params['limit']) ? 0 : $this->_params['limit'], - 'last_modified', 1); + try { + $galleries = $GLOBALS['ansel_storage']->listGalleries( + Horde_Perms::EDIT, Horde_Auth::getAuth(), null, false, 0, + empty($this->_params['limit']) ? 0 : $this->_params['limit'], + 'last_modified', 1); - if (is_a($galleries, 'PEAR_Error')) { - return $galleries->getMessage(); + } catch (Ansel_Exception $e) { + return $e->getMessage(); } $preview_url = Horde::applicationUrl('preview.php'); diff --git a/ansel/lib/Block/recent_comments.php b/ansel/lib/Block/recent_comments.php index 5ba569e51..78d528ac3 100644 --- a/ansel/lib/Block/recent_comments.php +++ b/ansel/lib/Block/recent_comments.php @@ -41,8 +41,9 @@ class Horde_Block_ansel_recent_comments extends Horde_Block { function _title() { if ($this->_params['gallery'] != 'all') { - $gallery = $this->_getGallery(); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $this->_getGallery(); + } catch (Horde_Exception $e) { return Horde::link( Ansel::getUrlFor('view', array('view' => 'List'), true)) . _("Gallery") . ''; @@ -76,9 +77,10 @@ class Horde_Block_ansel_recent_comments extends Horde_Block { $image_ids[] = $thread['forum_name']; } } else { - $gallery = $this->_getGallery(); - if (is_a($gallery, 'PEAR_Error')) { - return $gallery->getMessage(); + try { + $gallery = $this->_getGallery(); + } catch (Horde_Exception $e) { + return $e->getMessage(); } $results = array(); $image_ids = $gallery->listImages(); @@ -100,8 +102,8 @@ class Horde_Block_ansel_recent_comments extends Horde_Block { . ''; foreach ($results as $comment) { - $image = &$ansel_storage->getImage($comment['image_id']); - if (!is_a($image, 'PEAR_Error')) { + try { + $image = &$ansel_storage->getImage($comment['image_id']); $url = Ansel::getUrlFor('view', array('view' => 'Image', 'gallery' => abs($image->gallery), @@ -119,7 +121,7 @@ class Horde_Block_ansel_recent_comments extends Horde_Block { . ''; - } + } catch (Horde_Exception $e) {} } $html .= '
    ' . _("Date") . '' . _("Image") . '' . _("Subject") . '' . _("By") . '
    ' . $comment['message_subject'] . '' . $comment['message_author'] . '
    '; @@ -129,7 +131,7 @@ class Horde_Block_ansel_recent_comments extends Horde_Block { function _getGallery() { // Make sure we haven't already selected a gallery. - if (is_a($this->_gallery, 'Ansel_Gallery')) { + if ($this->_gallery instanceof Ansel_Gallery) { return $this->_gallery; } @@ -142,10 +144,9 @@ class Horde_Block_ansel_recent_comments extends Horde_Block { } if (empty($this->_gallery)) { - return PEAR::raiseError(_("Gallery does not exist.")); - } elseif (is_a($this->_gallery, 'PEAR_Error') || - !$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - return PEAR::raiseError(_("Access denied viewing this gallery.")); + throw new Horde_Exception_NotFount(_("Gallery does not exist.")); + } elseif (!$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this gallery.")); } // Return a reference to the gallery. diff --git a/ansel/lib/Block/recently_added.php b/ansel/lib/Block/recently_added.php index aa0779368..f205b78f8 100644 --- a/ansel/lib/Block/recently_added.php +++ b/ansel/lib/Block/recently_added.php @@ -45,8 +45,9 @@ class Horde_Block_ansel_recently_added extends Horde_Block { function _title() { if ($this->_params['gallery'] != 'all') { - $gallery = $this->_getGallery(); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $this->_getGallery(); + } catch (Exception $e) { return Horde::link( Ansel::getUrlFor('view', array('view' => 'List'), true)) . _("Gallery") . ''; @@ -84,10 +85,11 @@ class Horde_Block_ansel_recently_added extends Horde_Block { // Retrieve the images, but protect against very large values for // limit. - $results = $GLOBALS['ansel_storage']->getRecentImages( + try { + $results = $GLOBALS['ansel_storage']->getRecentImages( $galleries, min($this->_params['limit'], 100)); - if (is_a($results, 'PEAR_Error')) { - return $results->getMessage(); + } catch (Ansel_Exception $e) { + return $e->getMessage(); } $preview_url = Horde::applicationUrl('preview.php', true); $header = array(_("Date"), _("Photo"), _("Gallery")); @@ -116,9 +118,6 @@ function previewImage(e, image_id) { HEADER; foreach ($results as $image) { - if (is_a($image, 'PEAR_Error')) { - continue; - } $gallery = $GLOBALS['ansel_storage']->getGallery($image->gallery); // Don't show locked galleries in the block. @@ -168,28 +167,26 @@ HEADER; function _getGallery() { - // Make sure we haven't already selected a gallery. - if (is_a($this->_gallery, 'Ansel_Gallery')) { + /* Make sure we haven't already selected a gallery. */ + if ($this->_gallery instanceof Ansel_Gallery) { return $this->_gallery; } - // Get the gallery object and cache it. + /* Get the gallery object and cache it. */ if (isset($this->_params['gallery']) && $this->_params['gallery'] != '__random') { - $this->_gallery = $GLOBALS['ansel_storage']->getGallery( - $this->_params['gallery']); + $this->_gallery = $GLOBALS['ansel_storage']->getGallery($this->_params['gallery']); } else { $this->_gallery = $GLOBALS['ansel_storage']->getRandomGallery(); } if (empty($this->_gallery)) { - return PEAR::raiseError(_("Gallery does not exist.")); - } elseif (is_a($this->_gallery, 'PEAR_Error') || - !$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - return PEAR::raiseError(_("Access denied viewing this gallery.")); + throw new Horde_Exception_NotFound(_("Gallery not found.")); + } elseif (!$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this gallery.")); } - // Return a reference to the gallery. + /* Return a reference to the gallery. */ return $this->_gallery; } diff --git a/ansel/lib/Block/recently_added_geodata.php b/ansel/lib/Block/recently_added_geodata.php index 996ebde3d..d911b4737 100644 --- a/ansel/lib/Block/recently_added_geodata.php +++ b/ansel/lib/Block/recently_added_geodata.php @@ -52,17 +52,16 @@ class Horde_Block_ansel_recently_added_geodata extends Horde_Block { Horde::addScriptFile('http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/1.1/src/markermanager.js', 'ansel', array('external' => true)); Horde::addScriptFile('googlemap.js'); if ($this->_params['gallery'] != 'all') { - $gallery = $this->_getGallery(); - if (is_a($gallery, 'PEAR_Error')) { - return Horde::link( - Ansel::getUrlFor('view', array('view' => 'List'), true)) + try { + $gallery = $this->_getGallery(); + } catch (Exception $e) { + return Horde::link(Ansel::getUrlFor('view', array('view' => 'List'), true)) . _("Gallery") . ''; } // Build the gallery name. if (isset($this->_params['gallery'])) { - $name = @htmlspecialchars($gallery->get('name'), ENT_COMPAT, - Horde_Nls::getCharset()); + $name = @htmlspecialchars($gallery->get('name'), ENT_COMPAT, Horde_Nls::getCharset()); } $style = $gallery->getStyle(); @@ -89,15 +88,13 @@ class Horde_Block_ansel_recently_added_geodata extends Horde_Block { $galleries = $this->_params['gallery']; } - $images = $GLOBALS['ansel_storage']->getRecentImagesGeodata(null, 0, min($this->_params['limit'], 100)); - if (is_a($images, 'PEAR_Error')) { - return $images->getMessage(); + try { + $images = $GLOBALS['ansel_storage']->getRecentImagesGeodata(null, 0, min($this->_params['limit'], 100)); + } catch (Ansel_Exception $e) { + return $e->getMessage(); } $images = array_reverse($images); foreach ($images as $key => $image) { - if (is_a($image, 'PEAR_Error')) { - continue; - } $id = $image['image_id']; $gallery = $GLOBALS['ansel_storage']->getGallery($image['gallery_id']); @@ -144,12 +141,10 @@ EOT; function _getGallery() { - // Make sure we haven't already selected a gallery. - if (is_a($this->_gallery, 'Ansel_Gallery')) { + if ($this->_gallery instanceof Ansel_Gallery) { return $this->_gallery; } - // Get the gallery object and cache it. if (isset($this->_params['gallery']) && $this->_params['gallery'] != '__random') { $this->_gallery = $GLOBALS['ansel_storage']->getGallery( @@ -159,13 +154,11 @@ EOT; } if (empty($this->_gallery)) { - return PEAR::raiseError(_("Gallery does not exist.")); - } elseif (is_a($this->_gallery, 'PEAR_Error') || - !$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { - return PEAR::raiseError(_("Access denied viewing this gallery.")); + throw new Horde_Exception_NotFound(_("Gallery not found.")); + } elseif (!$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { + throw new Horde_Exception_PermissionDenied(_("Access denied viewing this gallery.")); } - // Return a reference to the gallery. return $this->_gallery; } diff --git a/ansel/lib/Faces/Base.php b/ansel/lib/Faces/Base.php index 52943554f..9ba5293c6 100644 --- a/ansel/lib/Faces/Base.php +++ b/ansel/lib/Faces/Base.php @@ -196,6 +196,9 @@ class Ansel_Faces_Base * Count faces * * @param array $info Array of select criteria + * + * @return integer The count of faces + * @throws Ansel_Exception */ protected function _countFaces($info) { @@ -209,7 +212,12 @@ class Ansel_Faces_Base . ' ) AND f.gallery_id = s.share_id' . (isset($info['filter']) ? ' AND ' . $info['filter'] : ''); - return $GLOBALS['ansel_db']->queryOne($sql); + $result = $GLOBALS['ansel_db']->queryOne($sql); + if ($result instanceof PEAR_Error) { + throw new Ansel_Exception($result); + } + + return $result; } /** @@ -516,15 +524,17 @@ class Ansel_Faces_Base * @param boolen $create Create images or store data? * * @return array Faces found + * @throws Horde_Exception_PermissionDenied + * @throws Ansel_Exception */ - public function getFromPicture(&$image, $create = false) + public function getFromPicture($image, $create = false) { // get image if ID is passed - if (!is_a($image, 'Ansel_Image')) { - $image = &$GLOBALS['ansel_storage']->getImage($image); + if (!($image instanceof Ansel_Image)) { + $image = $GLOBALS['ansel_storage']->getImage($image); $gallery = $GLOBALS['ansel_storage']->getGallery($image->gallery); if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - throw new Horde_Exception('Access denied editing the photo.'); + throw new Horde_Exception_PermissionDenied('Access denied editing the photo.'); } } @@ -535,7 +545,7 @@ class Ansel_Faces_Base } // Clean up any existing faces we may have had in this image. - $result = Ansel_Faces::delete($image); + Ansel_Faces::delete($image); // Process faces $fids = array(); @@ -543,7 +553,7 @@ class Ansel_Faces_Base // Create Face id $face_id = $GLOBALS['ansel_db']->nextId('ansel_faces'); if ($face_id instanceof PEAR_Error) { - throw new Horde_Exception_Prior($face_id); + throw new Ansel_Exception($face_id); } // Store face id db @@ -555,16 +565,15 @@ class Ansel_Faces_Base $q = $GLOBALS['ansel_db']->prepare($sql, null, MDB2_PREPARE_MANIP); if ($q instanceof PEAR_Error) { - throw new Horde_Exception_Prior($q); + throw new Ansel_Exception($q); } $result = $q->execute($params); $q->free(); if ($result instanceof PEAR_Error) { - throw new Horde_Exception_Prior($result); + throw new Ansel_Exception($result); } if ($create) { - // Process image - $result = $this->_createView($face_id, $image, $rect); + $this->_createView($face_id, $image, $rect); // Clear any loaded views to save on memory usage. $image->reset(); $this->saveSignature($image->id, $face_id); @@ -780,7 +789,7 @@ class Ansel_Faces_Base } $face = $result->fetchRow(MDB2_FETCHMODE_ASSOC); - if (is_a($face, 'PEAR_Error')) { + if ($face instanceof PEAR_Error) { throw new Horde_Exception_Prior($face); } diff --git a/ansel/lib/Gallery.php b/ansel/lib/Gallery.php index 11950b056..60aabc943 100644 --- a/ansel/lib/Gallery.php +++ b/ansel/lib/Gallery.php @@ -34,6 +34,13 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical $this->_setModeHelper($mode); } + /** + * Helper for accessing the gallery id + * + * @param string $property + * + * @return mixed + */ public function __get($property) { switch ($property) { @@ -47,6 +54,7 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical /** * Check for special capabilities of this gallery. * + * @return boolean */ public function hasFeature($feature) { @@ -106,21 +114,15 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical /** * Saves any changes to this object to the backend permanently. * - * @TODO: this needs to stay public b/c Horde_Share_Object (which this extends) - * is not yet ported to PHP5. This will be fixed when we no longer - * extend Horde_Share_Object here. - * - * @return mixed true || PEAR_Error on failure. + * @return boolean */ - public function _save() + protected function _save() { // Check for invalid characters in the slug. if (!empty($this->data['attribute_slug']) && preg_match('/[^a-zA-Z0-9_@]/', $this->data['attribute_slug'])) { - // TODO: Need to keep the pear error here since Horde_Share still - // uses them. - return PEAR::raiseError( + throw new InvalidArgumentException( sprintf(_("Could not save gallery, the slug, \"%s\", contains invalid characters."), $this->data['attribute_slug'])); } @@ -128,13 +130,15 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical // Check for slug uniqueness $slugGalleryId = $GLOBALS['ansel_storage']->slugExists($this->data['attribute_slug']); if ($slugGalleryId > 0 && $slugGalleryId <> $this->id) { - return PEAR::raiseError(sprintf(_("Could not save gallery, the slug, \"%s\", already exists."), - $this->data['attribute_slug'])); + throw InvalidArgumentException( + sprintf(_("Could not save gallery, the slug, \"%s\", already exists."), + $this->data['attribute_slug'])); } if ($GLOBALS['conf']['ansel_cache']['usecache']) { $GLOBALS['cache']->expire('Ansel_Gallery' . $this->id); } + return parent::_save(); } @@ -142,28 +146,42 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical * Update the gallery image count. * * @param integer $images Number of images in action - * @param boolean $add Action to take (add or remove) - * @param integer $gallery_id Gallery id to update images for + * @param boolean $add True if adding, false if removing + * @param integer $gallery_id Gallery id to update images for, if not self + * + * @return boolean true on success + * @throws Ansel_Exception */ public function updateImageCount($images, $add = true, $gallery_id = null) { - // We do the query directly here to avoid having to instantiate a - // gallery object just to increment/decrement one value in the table. - // TODO: Change this - should always use the appropriate object, not - // direct manipulation of the share table... - $sql = 'UPDATE ' . $this->_shareOb->getTable() - . ' SET attribute_images = attribute_images ' - . ($add ? ' + ' : ' - ') . $images . ' WHERE share_id = ' - . ($gallery_id ? $gallery_id : $this->id); - - // Make sure to update the local value as well, so it doesn't get - // overwritten by any other updates from ->set() calls. if (is_null($gallery_id) || $gallery_id === $this->id) { + /* Updating self */ if ($add) { $this->data['attribute_images'] += $images; } else { $this->data['attribute_images'] -= $images; } + try { + $this->save(); + } catch (Horde_Share_Exception $e) { + Horde::logMessage($e->getMessage, 'ERR'); + throw Ansel_Exception($e); + } + } else { + /* Updating other gallery */ + $g = $GLOBALS['ansel_storage']->getGallery($gallery_id); + $count = $g->get('images'); + if ($add) { + $count += $images; + } else { + $count['attribute_images'] -= $images; + } + try { + $g->save(); + } catch (Horde_Share_Exception $e) { + Horde::logMessage($e->getMessage, 'ERR'); + throw Ansel_Exception($e); + } } /* Need to expire the cache for the gallery that was changed */ @@ -172,17 +190,20 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical $GLOBALS['cache']->expire('Ansel_Gallery' . $id); } - return $this->_shareOb->getWriteDb()->exec($sql); + return true; } /** * Adds an Ansel_Image object to this gallery. * - * @param $image Ansel_Image + * @param Ansel_Image $image The ansel image object to add + * @param boolean $default Set this image as the gallery's key image. + * + * @return integer The new image id */ public function addImageObject(Ansel_Image $image, $default = false) { - // Make sure it's taken as a new image + /* Make sure it's taken as a new image */ $image->id = null; $image->gallery = $this->getId(); $image->sort = $this->countImages(); @@ -190,8 +211,10 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical $this->updateImageCount(1); /* Should this be the default image? */ - $this->data['attribute_default'] = $image->id; - $this->clearStacks(); + if ($default) { + $this->data['attribute_default'] = $image->id; + $this->clearStacks(); + } /* Save all changes to the gallery */ $this->save(); @@ -351,18 +374,18 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical * @param Ansel_Gallery $gallery The gallery to copy images to. * * @return integer The number of images copied - * @throws Horde_Exception + * @throws Ansel_Exception */ public function copyImagesTo($images, $gallery) { if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - throw new Horde_Exception(sprintf(_("Access denied copying photos to \"%s\"."), $gallery->get('name'))); + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied copying photos to \"%s\"."), $gallery->get('name'))); } $db = $this->_shareOb->getWriteDb(); $imgCnt = 0; foreach ($images as $imageId) { - $img = &$this->getImage($imageId); + $img = $this->getImage($imageId); // Note that we don't pass the tags when adding the image..see below $newId = $gallery->addImage(array( 'image_caption' => $img->caption, @@ -376,13 +399,13 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical // for each tag - just write the data into the DB ourselves. $tags = $img->getTags(); $query = $this->_shareOb->getWriteDb()->prepare('INSERT INTO ansel_images_tags (image_id, tag_id) VALUES(' . $newId . ',?);'); - if (is_a($query, 'PEAR_Error')) { - throw new Horde_Exception($query->getMessage()); + if ($query instanceof PEAR_Error) { + throw new Ansel_Exception($query); } foreach ($tags as $tag_id => $tag_name) { $result = $query->execute($tag_id); - if (is_a($result, 'PEAR_Error')) { - throw new Horde_Exception($result->getMessge()); + if ($result instanceof PEAR_Error) { + throw new Ansel_Exception($result); } } $query->free(); @@ -394,12 +417,12 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical $exif = $db->queryAll('SELECT attr_name, attr_value FROM ansel_image_attributes WHERE image_id = ' . (int) $imageId . ';',null, MDB2_FETCHMODE_ASSOC); if (is_array($exif) && count($exif) > 0) { $insert = $db->prepare('INSERT INTO ansel_image_attributes (image_id, attr_name, attr_value) VALUES (?, ?, ?)'); - if (is_a($insert, 'PEAR_Error')) { + if ($insert instanceof PEAR_Error) { throw new Horde_Exception($insert->getMessage()); } foreach ($exif as $attr){ $result = $insert->execute(array($newId, $attr['attr_name'], $attr['attr_value'])); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { throw new Horde_Exception($result->getMessage()); } } @@ -840,7 +863,7 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical public function setParent($parent) { /* Make sure we have a gallery object */ - if (!is_null($parent) && !is_a($parent, 'Ansel_Gallery')) { + if (!is_null($parent) && !($parent instanceof Ansel_Gallery)) { $parent = $GLOBALS['ansel_storage']->getGallery($parent); } @@ -856,11 +879,7 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical } /* Call the parent class method */ - $result = parent::setParent($parent); - if (is_a($result, 'PEAR_Error')) { - // Horde_Share still uses PEAR_Error - throw new Horde_Exception($result->getMessage()); - } + parent::setParent($parent); /* Tell the parent the good news */ if (!is_null($parent) && !$parent->get('has_subgalleries')) { @@ -918,7 +937,7 @@ class Ansel_Gallery extends Horde_Share_Object_Sql_Hierarchical } $result = $query->execute(array($data[$driver_key], $this->id)); $query->free(); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { throw new Horde_Exception($result->getMessage()); } diff --git a/ansel/lib/GalleryMode/Date.php b/ansel/lib/GalleryMode/Date.php index f3c294b06..bf277f82c 100644 --- a/ansel/lib/GalleryMode/Date.php +++ b/ansel/lib/GalleryMode/Date.php @@ -11,8 +11,8 @@ * @author Michael J. Rubinsky * @package Ansel */ -class Ansel_GalleryMode_Date { - +class Ansel_GalleryMode_Date +{ /** * @var Ansel_Gallery */ @@ -157,9 +157,6 @@ class Ansel_GalleryMode_Date { if (!is_array($this->_subGalleries)) { /* Get a list of all the subgalleries */ $subs = $GLOBALS['ansel_storage']->listGalleries(Horde_Perms::SHOW, null, $this->_gallery); - if (is_a($subs, 'PEAR_Error')) { - return $subs; - } $this->_subGalleries = array_keys($subs); } } @@ -199,9 +196,6 @@ class Ansel_GalleryMode_Date { if (!count($this->_date) || empty($this->_date['year'])) { /* All available images - grouped by year */ $images = $ansel_storage->listImages($this->_gallery->id, 0, 0, array('image_id', 'image_original_date'), $gallery_where); - if (is_a($images, 'PEAR_Error')) { - return $images; - } $dates = array(); foreach ($images as $key => $image) { $dates[date('Y', $image['image_original_date'])][] = $key; @@ -307,17 +301,16 @@ class Ansel_GalleryMode_Date { } $images= $ansel_storage->listImages($this->_gallery->id, $from, $to, 'image_id', $where, 'image_sort'); $results = $ansel_storage->getImages(array('ids' => $images, 'preserve' => true)); - if (!is_a($results, 'PEAR_Error')) { - if ($this->_gallery->get('has_subgalleries')) { - $images = array(); - foreach ($results as $id => $image) { - $image->gallery = $this->_gallery->id; - $images[$id] = $image; - } - $children[$cache_key] = $images; - } else { - $children[$cache_key] = $results; + + if ($this->_gallery->get('has_subgalleries')) { + $images = array(); + foreach ($results as $id => $image) { + $image->gallery = $this->_gallery->id; + $images[$id] = $image; } + $children[$cache_key] = $images; + } else { + $children[$cache_key] = $results; } return $children[$cache_key]; @@ -351,7 +344,6 @@ class Ansel_GalleryMode_Date { return $results; } - /** * Return the count of this gallery's children * @@ -370,14 +362,9 @@ class Ansel_GalleryMode_Date { function countGalleryChildren($perm = Horde_Perms::SHOW, $galleries_only = false, $noauto = true) { $results = $this->getGalleryChildren($this->_date, 0, 0, $noauto); - if (is_a($results, 'PEAR_Error')) { - return $results; - } - return count($results); } - /** * Lists a slice of the image ids in this gallery. * In Date mode, this only makes sense if we are currently viewing a @@ -422,9 +409,9 @@ class Ansel_GalleryMode_Date { function moveImagesTo($images, $gallery) { if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - return PEAR::raiseError(sprintf(_("Access denied moving photos to \"%s\"."), $newGallery->get('name'))); + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied moving photos to \"%s\"."), $newGallery->get('name'))); } elseif (!$this->_gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::DELETE)) { - return PEAR::raiseError(sprintf(_("Access denied removing photos from \"%s\"."), $gallery->get('name'))); + throw new Horde_Exception_PermissionDenied(sprintf(_("Access denied removing photos from \"%s\"."), $gallery->get('name'))); } /* Sanitize image ids, and see if we're removing our default image. */ @@ -453,9 +440,11 @@ class Ansel_GalleryMode_Date { } /* Bulk update the images to their new gallery_id */ + // @TODO: Move this to Ansel_Storage:: $result = $this->_gallery->getShareOb()->getWriteDb()->exec('UPDATE ansel_images SET gallery_id = ' . $gallery->id . ' WHERE image_id IN (' . implode(',', $ids) . ')'); - if (is_a($result, 'PEAR_Error')) { - return $result; + if ($result instanceof PEAR_Error) { + Horde::logMessage($result, 'ERR'); + throw new Ansel_Exception($result); } /* Update the gallery counts for each affected gallery */ @@ -485,18 +474,13 @@ class Ansel_GalleryMode_Date { * @param mixed $image An image_id or Ansel_Image object to delete. * @param boolean $isStack Image is a stack image (doesn't update count). * - * @return mixed boolean || PEAR_Error + * @return boolean */ function removeImage($image, $isStack) { - /* Make sure $image is an Ansel_Image; if not, try loading it. */ - if (!is_a($image, 'Ansel_Image')) { - $img = &$GLOBALS['ansel_storage']->getImage($image); - if (is_a($img, 'PEAR_Error')) { - return $img; - } - $image = $img; + if (!($image instanceof Ansel_Image)) { + $image = $GLOBALS['ansel_storage']->getImage($image); } /* Make sure the image is in this gallery. */ @@ -526,6 +510,7 @@ class Ansel_GalleryMode_Date { } catch (VFS_Exception $e) {} /* Delete from SQL. */ + // @TODO: Move to Horde_Storage $this->_gallery->getShareOb()->getWriteDb()->exec('DELETE FROM ansel_images WHERE image_id = ' . (int)$image->id); /* Remove any attributes */ @@ -555,10 +540,8 @@ class Ansel_GalleryMode_Date { if (($GLOBALS['conf']['comments']['allow'] == 'all' || ($GLOBALS['conf']['comments']['allow'] == 'authenticated' && Horde_Auth::getAuth())) && $GLOBALS['registry']->hasMethod('forums/deleteForum')) { - $result = $GLOBALS['registry']->call('forums/deleteForum', - array('ansel', $image->id)); - - if (is_a($result, 'PEAR_Error')) { + $result = $GLOBALS['registry']->call('forums/deleteForum', array('ansel', $image->id)); + if ($result instanceof PEAR_Error) { Horde::logMessage($result, 'ERR'); return false; } diff --git a/ansel/lib/GalleryMode/Normal.php b/ansel/lib/GalleryMode/Normal.php index 7c2737143..4054cc862 100644 --- a/ansel/lib/GalleryMode/Normal.php +++ b/ansel/lib/GalleryMode/Normal.php @@ -78,9 +78,11 @@ class Ansel_GalleryMode_Normal { /* Now grab any images if we still have room */ if (($to - count($galleries) > 0) || ($from == 0 && $to == 0) && $this->_gallery->data['attribute_images']) { - $images = $this->getImages(max(0, $from - $num_galleries), $to - count($galleries)); - if (is_a($images, 'PEAR_Error')) { - Horde::logMessage($images, 'ERR'); + + try { + $images = $this->getImages(max(0, $from - $num_galleries), $to - count($galleries)); + } catch (Ansel_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); $images = array(); } } else { @@ -276,7 +278,7 @@ class Ansel_GalleryMode_Normal { $result = $GLOBALS['registry']->call('forums/deleteForum', array('ansel', $image->id)); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, 'ERR'); return false; } diff --git a/ansel/lib/Image.php b/ansel/lib/Image.php index b4107825d..04c6bf2d5 100644 --- a/ansel/lib/Image.php +++ b/ansel/lib/Image.php @@ -513,7 +513,7 @@ class Ansel_Image Implements Iterator * * @TODO: Move all SQL queries to Ansel_Storage:: * - * @return The image id + * @return integer image id * @throws Ansel_Exception */ public function save() diff --git a/ansel/lib/Storage.php b/ansel/lib/Storage.php index df1c4df1e..621f0975b 100644 --- a/ansel/lib/Storage.php +++ b/ansel/lib/Storage.php @@ -293,7 +293,7 @@ class Ansel_Storage * @return Ansel_Gallery object * @throws Horde_Exception_NotFound */ - public function &getGalleryBySlug($slug, $overrides = array()) + public function getGalleryBySlug($slug, $overrides = array()) { $id = $this->slugExists($slug); if ($id) { @@ -313,7 +313,7 @@ class Ansel_Storage * @return Ansel_Gallery * @throws Ansel_Exception */ - public function &getGallery($gallery_id, $overrides = array()) + public function getGallery($gallery_id, $overrides = array()) { // avoid cache server hits if (isset($this->_galleries[$gallery_id]) && !count($overrides)) { @@ -523,7 +523,7 @@ class Ansel_Storage /* If we have an id, then it's an existing image.*/ if ($image->id) { $update = $this->_db->prepare('UPDATE ansel_images SET image_filename = ?, image_type = ?, image_caption = ?, image_sort = ?, image_original_date = ?, image_latitude = ?, image_longitude = ?, image_location = ?, image_geotag_date = ? WHERE image_id = ?'); - if (is_a($update, 'PEAR_Error')) { + if ($update instanceof PEAR_Error) { Horde::logMessage($update, 'ERR'); throw new Ansel_Exception($update); } @@ -537,7 +537,7 @@ class Ansel_Storage $image->location, $image->geotag_timestamp, $image->id)); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($update, 'ERR'); throw new Ansel_Exception($result); } @@ -553,13 +553,13 @@ class Ansel_Storage /* Get the next image_id */ $image_id = $this->_db->nextId('ansel_images'); - if (is_a($image_id, 'PEAR_Error')) { + if ($image_id instanceof PEAR_Error) { throw new Ansel_Exception($image_id); } /* Prepare the SQL statement */ $insert = $this->_db->prepare('INSERT INTO ansel_images (image_id, gallery_id, image_filename, image_type, image_caption, image_uploaded_date, image_sort, image_original_date, image_latitude, image_longitude, image_location, image_geotag_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); - if (is_a($insert, 'PEAR_Error')) { + if ($insert instanceof PEAR_Error) { Horde::logMessage($insert, 'ERR'); throw new Ansel_Exception($insert); } @@ -578,7 +578,7 @@ class Ansel_Storage $image->location, (empty($image->lat) ? 0 : $image->uploaded))); $insert->free(); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, 'ERR'); throw new Ansel_Exception($result); } diff --git a/ansel/lib/Tags.php b/ansel/lib/Tags.php index 7f40ae64b..40f795a4e 100644 --- a/ansel/lib/Tags.php +++ b/ansel/lib/Tags.php @@ -19,10 +19,11 @@ /** * Static helper class for writing/reading tag values * + * @TODO: Move tag storage to Content_Tagger * @static */ -class Ansel_Tags { - +class Ansel_Tags +{ /** * Write out the tags for a specific resource. * @@ -30,9 +31,11 @@ class Ansel_Tags { * @param array $tags An array of tags. * @param string $resource_type The type of resource (image or gallery) * - * @return mixed True | PEAR_Error + * @return boolean True on success + * @throws InvalidArgumentException + * @throws Ansel_Exception */ - function writeTags($resource_id, $tags, $resource_type = 'image') + static public function writeTags($resource_id, $tags, $resource_type = 'image') { // First, make sure all tag names exist in the DB. $tagkeys = array(); @@ -40,7 +43,7 @@ class Ansel_Tags { foreach ($tags as $tag) { if (!empty($tag)) { if (!preg_match("/^[a-zA-Z0-9%_+.!*',()~-]*$/", $tag)) { - return PEAR::raiseError(_("Invalid characters in tag")); + throw new InvalidArgumentException('Invalid characters in tag.'); } $tag = Horde_String::lower(trim($tag)); $sql = $GLOBALS['ansel_db']->prepare('SELECT tag_id FROM ansel_tags WHERE tag_name = ?'); @@ -52,8 +55,9 @@ class Ansel_Tags { $id = $GLOBALS['ansel_db']->nextId('ansel_tags'); $result = $insert->execute(array($id, Horde_String::convertCharset($tag, Horde_Nls::getCharset(), $GLOBALS['conf']['sql']['charset']))); $tagkeys[] = $id; - } elseif (is_a($results, 'PEAR_Error')) { - return $results; + } elseif ($results instanceof PEAR_Error) { + Horde::logMessage($results->getMessage(), 'ERR'); + throw new Ansel_Exception($results); } else { $tagkeys[] = $results['tag_id']; } @@ -85,20 +89,20 @@ class Ansel_Tags { /** * Retrieve the tags for a specified resource. * - * @param int $resource_id The resource to get tags for. + * @param integer $resource_id The resource to get tags for. * @param string $resource_type The type of resource (gallery or image) * - * @return mixed An array of tags | PEAR_Error + * @return mixed An array of tags */ - function readTags($resource_id, $resource_type = 'image') + static public function readTags($resource_id, $resource_type = 'image') { if ($resource_type == 'image') { $stmt = $GLOBALS['ansel_db']->prepare('SELECT ansel_tags.tag_id, tag_name FROM ansel_tags INNER JOIN ansel_images_tags ON ansel_images_tags.tag_id = ansel_tags.tag_id WHERE ansel_images_tags.image_id = ?'); } else { $stmt = $GLOBALS['ansel_db']->prepare('SELECT ansel_tags.tag_id, tag_name FROM ansel_tags INNER JOIN ansel_galleries_tags ON ansel_galleries_tags.tag_id = ansel_tags.tag_id WHERE ansel_galleries_tags.gallery_id = ?'); } - if (is_a($stmt, 'PEAR_Error')) { - return $stmt; + if ($stmt instanceof PEAR_Error) { + throw new Ansel_Exception($stmt); } Horde::logMessage('SQL query by Ansel_Tags::readTags ' . $stmt->query, 'DEBUG'); $result = $stmt->execute((int)$resource_id); @@ -121,9 +125,9 @@ class Ansel_Tags { * will be included. * @param integer $limit Limit the number of tags returned to this value. * - * @return mixed An array containing tag_name, and total | PEAR_Error + * @return Array An array containing tag_name, and total */ - function listTagInfo($tags = null, $limit = 500) + static public function listTagInfo($tags = null, $limit = 500) { global $conf; // Only return the full list if $tags is omitted, not if @@ -169,10 +173,10 @@ class Ansel_Tags { * @param string $user Limit the result set to resources * owned by this user. * - * @return mixed An array of image_ids and galery_ids objects | PEAR_Error + * @return array An array of image_ids and gallery_ids */ - function searchTagsById($ids, $max = 0, $from = 0, - $resource_type = 'all', $user = null) + static public function searchTagsById($ids, $max = 0, $from = 0, + $resource_type = 'all', $user = null) { if (!is_array($ids) || !count($ids)) { return array('galleries' => array(), 'images' => array()); @@ -203,29 +207,27 @@ class Ansel_Tags { Horde::logMessage('SQL query by Ansel_Tags::searchTags: ' . $sql, 'DEBUG'); $GLOBALS['ansel_db']->setLimit($max, $from); $images = $GLOBALS['ansel_db']->queryCol($sql); - if (is_a($images, 'PEAR_Error')) { + if ($images instanceof PEAR_Error) { Horde::logMessage($images, 'ERR'); $results['images'] = array(); } else { /* Check permissions and filter on $user if required */ $imgs = array(); foreach ($images as $id) { - $img = &$GLOBALS['ansel_storage']->getImage($id); - if (is_a($img, 'PEAR_Error')) { - break; - } - $gal = $GLOBALS['ansel_storage']->getGallery($img->gallery); - if (!is_a($gal, 'PEAR_Error')) { + try { + $img = $GLOBALS['ansel_storage']->getImage($id); + $gal = $GLOBALS['ansel_storage']->getGallery($img->gallery); $owner = $gal->get('owner'); if ($gal->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) && (!isset($user) || (isset($user) && $owner && $owner == $user))) { $imgs[] = $id; } - } else { - Horde::logMessage($gal, 'ERR'); + } catch (Ansel_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + break; } } - $results['images'] = $imgs; + $results['images'] = $imgs; } } @@ -240,14 +242,15 @@ class Ansel_Tags { $GLOBALS['ansel_db']->setLimit($max, $from); $galleries = $GLOBALS['ansel_db']->queryCol($sql); - if (is_a($galleries, 'PEAR_Error')) { + if ($galleries instanceof PEAR_Error) { Horde::logMessage($galleries, 'ERR'); } else { /* Check perms */ foreach ($galleries as $id) { - $gallery = $GLOBALS['ansel_storage']->getGallery($id); - if (is_a($gallery, 'PEAR_Error')) { - Horde::logMessage($gallery, 'ERR'); + try { + $gallery = $GLOBALS['ansel_storage']->getGallery($id); + } catch (Ansel_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); continue; } if ($gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) && (!isset($user) || (isset($user) && $gallery->get('owner') && $gallery->get('owner') == $user))) { @@ -277,10 +280,10 @@ class Ansel_Tags { * @param string $user Limit the result set to resources owned by * specified user. * - * @return mixed An array of image_ids and gallery_ids | PEAR_Error + * @return mixed An array of image_ids and gallery_ids */ - function searchTags($names = array(), $max = 10, $from = 0, - $resource_type = 'all', $user = null) + static public function searchTags($names = array(), $max = 10, $from = 0, + $resource_type = 'all', $user = null) { /* Get the tag_ids */ $ids = Ansel_Tags::getTagIds($names); @@ -296,7 +299,7 @@ class Ansel_Tags { * * @return mixed A hash of tag_id -> tag_name | PEAR_Error */ - function getRelatedTags($ids) + static public function getRelatedTags($ids) { if (!count($ids)) { return array(); @@ -354,7 +357,7 @@ class Ansel_Tags { * * @return string The URL for this tag and action */ - function getTagLinks($tags, $action = 'add', $owner = null) + static public function getTagLinks($tags, $action = 'add', $owner = null) { $results = array(); foreach ($tags as $id => $taginfo) { @@ -378,9 +381,9 @@ class Ansel_Tags { * * @param array $tags An array of tag_names * - * @return mixed An array of tag_names => tag_ids | PEAR_Error + * @return array An array of tag_names => tag_ids */ - function getTagIds($tags) + static public function getTagIds($tags) { if (!count($tags)) { return array(); @@ -399,9 +402,13 @@ class Ansel_Tags { } /** + * Get the tag names from ids * + * @param array $ids An array of tag ids + * + * @return array A hash of tag_id => tag_names */ - function getTagNames($ids) + static public function getTagNames($ids) { if (!count($ids)) { return array(); @@ -421,8 +428,11 @@ class Ansel_Tags { /** * Retrieve an Ansel_Tags_Search object + * + * @return Ansel_Tags_Search + * @TODO: refactor into Ansel_Search */ - function getSearch($tags = null, $owner = null) + static public function getSearch($tags = null, $owner = null) { return new Ansel_Tags_Search($tags, $owner); } @@ -430,12 +440,12 @@ class Ansel_Tags { /** * Clear the session cache */ - function clearSearch() + static public function clearSearch() { unset($_SESSION['ansel_tags_search']); } - function clearCache() + static public function clearCache() { if ($GLOBALS['conf']['ansel_cache']['usecache']) { $GLOBALS['cache']->expire(Horde_Auth::getAuth() . '__anseltagsearches'); @@ -445,6 +455,10 @@ class Ansel_Tags { /** * Class that represents a slice of a tag search + * + * @TODO: Move this to Ansel_Search_Tags + * + * */ class Ansel_Tags_Search { @@ -482,7 +496,7 @@ class Ansel_Tags_Search { /** * Fetch the matching resources that should appear on the current page * - * @return Array of Ansel_Images and Ansel_Galleries | PEAR_Error + * @return Array of Ansel_Images and Ansel_Galleries */ function getSlice($page, $perpage) { @@ -498,11 +512,6 @@ class Ansel_Tags_Search { $gstart, 'galleries', $this->_owner); - - if (is_a($gresults, 'PEAR_Error')) { - return $gresults; - } - $galleries = array(); foreach ($gresults['galleries'] as $gallery) { $galleries[] = $GLOBALS['ansel_storage']->getGallery($gallery); @@ -517,9 +526,6 @@ class Ansel_Tags_Search { $istart, 'images', $this->_owner); - if (is_a($iresults, 'PEAR_Error')) { - return $iresults; - } $images = count($iresults['images']) ? array_values($GLOBALS['ansel_storage']->getImages(array('ids' => $iresults['images']))) : array(); if (($conf['comments']['allow'] == 'all' || ($conf['comments']['allow'] == 'authenticated' && Horde_Auth::getAuth())) && @@ -527,7 +533,7 @@ class Ansel_Tags_Search { $ids = array_keys($images); $ccounts = $GLOBALS['registry']->call('forums/numMessagesBatch', array($ids, 'ansel')); - if (!is_a($ccounts, 'PEAR_Error')) { + if (!($ccounts instanceof PEAR_Error)) { foreach ($images as $image) { $image->commentCount = (!empty($ccounts[$image->id]) ? $ccounts[$image->id] : 0); } diff --git a/ansel/lib/View/Base.php b/ansel/lib/View/Base.php index 9092fa1dd..77a49d4c7 100644 --- a/ansel/lib/View/Base.php +++ b/ansel/lib/View/Base.php @@ -124,7 +124,7 @@ abstract class Ansel_View_Base * @return Ansel_Gallery The requested Ansel_Gallery object * @throws Horde_Exception */ - protected function &_getGallery($galleryId = null, $slug = '') + protected function _getGallery($galleryId = null, $slug = '') { if (is_null($galleryId) && empty($slug)) { $galleryId = !empty($this->_params['gallery_id']) ? $this->_params['gallery_id'] : null; @@ -137,13 +137,11 @@ abstract class Ansel_View_Base // If we have a slug, use it. if (!empty($slug)) { - $gallery = &$GLOBALS['ansel_storage']->getGalleryBySlug($slug); + $gallery = $GLOBALS['ansel_storage']->getGalleryBySlug($slug); } else { - $gallery = &$GLOBALS['ansel_storage']->getGallery($galleryId); + $gallery = $GLOBALS['ansel_storage']->getGallery($galleryId); } - if (is_a($gallery, 'PEAR_Error')) { - throw new Horde_Exception($gallery->getMessage()); - } elseif (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::READ)) { throw new Horde_Exception(sprintf(_("Access denied to gallery \"%s\"."), $gallery->get('name'))); } diff --git a/ansel/lib/View/GalleryRenderer/Base.php b/ansel/lib/View/GalleryRenderer/Base.php index a88bd2beb..a95a08fb8 100644 --- a/ansel/lib/View/GalleryRenderer/Base.php +++ b/ansel/lib/View/GalleryRenderer/Base.php @@ -163,7 +163,7 @@ abstract class Ansel_View_GalleryRenderer_Base //$image_ids = $this->view->gallery->listImages($this->pagestart, $this->pagestart + $this->perpage); $ids = array(); foreach ($this->children as $child) { - if (is_a($child, 'Ansel_Image')) { + if ($child instanceof Ansel_Image) { $ids[] = $child->id; } } diff --git a/ansel/lib/View/Image.php b/ansel/lib/View/Image.php index 65bed7c6f..7cdbdd8a3 100644 --- a/ansel/lib/View/Image.php +++ b/ansel/lib/View/Image.php @@ -245,7 +245,7 @@ class Ansel_View_Image extends Ansel_View_Base array('ansel', $this->resource->id, 'commentCallback', true, null, $url)); - if (is_a($comments, 'PEAR_Error')) { + if ($comments instanceof PEAR_Error) { Horde::logMessage($comments, 'DEBUG'); $comments = array(); } diff --git a/ansel/lib/Widget/OwnerFaces.php b/ansel/lib/Widget/OwnerFaces.php index 100997023..054e5abd5 100644 --- a/ansel/lib/Widget/OwnerFaces.php +++ b/ansel/lib/Widget/OwnerFaces.php @@ -25,11 +25,10 @@ class Ansel_Widget_OwnerFaces extends Ansel_Widget_Base $this->_faces = Ansel_Faces::factory(); $this->_owner = $this->_view->gallery->get('owner'); - //@TODO: Remove the PEAR_Error check when Faces is refactored. try { $this->_count = $this->_faces->countOwnerFaces($this->_owner); - } catch (Horde_Exception $e) {} - if (is_a($this->_count, 'PEAR_error')) { + } catch (Horde_Exception $e) { + Horde::logMessage($e->getMessage, 'ERR'); $this->_count = 0; } diff --git a/ansel/lib/Widget/SimilarPhotos.php b/ansel/lib/Widget/SimilarPhotos.php index 6a57ee067..74175d3ea 100755 --- a/ansel/lib/Widget/SimilarPhotos.php +++ b/ansel/lib/Widget/SimilarPhotos.php @@ -70,14 +70,13 @@ class Ansel_Widget_SimilarPhotos extends Ansel_Widget_Base break; } if ($imgId != $this->_view->resource->id) { - $rImg = &$ansel_storage->getImage($imgId); - if (is_a($rImg, 'PEAR_Error')) { - continue; - } - $rGal = $ansel_storage->getGallery($rImg->gallery); - if (is_a($rGal, 'PEAR_Error')) { + try { + $rImg = $ansel_storage->getImage($imgId); + $rGal = $ansel_storage->getGallery($rImg->gallery); + } catch (Ansel_Exception $e) { continue; } + $title = sprintf(_("%s from %s"), $rImg->filename, $rGal->get('name')); $html .= Horde::link( Ansel::getUrlFor('view', diff --git a/ansel/map_edit.php b/ansel/map_edit.php index db4c835a7..93564436c 100644 --- a/ansel/map_edit.php +++ b/ansel/map_edit.php @@ -23,15 +23,9 @@ if (empty($image_id)) { Horde::fatal(_("An error has occured retrieving the image. Details have been logged."), __FILE__, __LINE__, true); } $image = $ansel_storage->getImage($image_id); -if (is_a($image, 'PEAR_Error')) { - Horde::fatal(_("An error has occured retrieving the image. Details have been logged."), __FILE__, __LINE__, true); -} $gallery = $ansel_storage->getGallery($image->gallery); -if (is_a($gallery, 'PEAR_Error')) { - Horde::fatal(_("An error has occured retrieving the image. Details have been logged."), __FILE__, __LINE__, true); -} if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - Horde::fatal(_("Not Authorized. Details have been logged for the server administrator."), __FILE__, __LINE__, true); + throw new Horde_Exception_PermissionDenied(_("Not Authorized. Details have been logged for the server administrator.")); } /* Determine if we already have a geotag or are we tagging it for the 1st time */ diff --git a/ansel/preview.php b/ansel/preview.php index c63d941e3..2611114ea 100644 --- a/ansel/preview.php +++ b/ansel/preview.php @@ -12,21 +12,18 @@ require_once dirname(__FILE__) . '/lib/Application.php'; Horde_Registry::appInit('ansel'); $imageId = Horde_Util::getFormData('image'); -$image = &$ansel_storage->getImage($imageId); -if (is_a($image, 'PEAR_Error')) { - Horde::logMessage($image, 'ERR'); +try { + $image = $ansel_storage->getImage($imageId); + $gal = $ansel_storage->getGallery(abs($image->gallery)); + $img = Ansel::getImageUrl($imageId, 'thumb', false); +} catch (Ansel_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); exit; } -$gal = $ansel_storage->getGallery(abs($image->gallery)); -if (is_a($gal, 'PEAR_Error')) { - Horde::logMessage($image, 'ERR'); - exit; -} -$img = Ansel::getImageUrl($imageId, 'thumb', false); -if (!is_a($img, 'PEAR_Error') && - $gal->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) && - !$gal->hasPasswd() && - $gal->isOldEnough()) { +if ($gal->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) && + !$gal->hasPasswd() && + $gal->isOldEnough()) { + echo '' . htmlspecialchars($image->filename) . ''; } else { echo ''; diff --git a/ansel/protect.php b/ansel/protect.php index d47edc79f..56485c62d 100644 --- a/ansel/protect.php +++ b/ansel/protect.php @@ -12,9 +12,10 @@ require_once dirname(__FILE__) . '/lib/Application.php'; Horde_Registry::appInit('ansel'); $vars = Horde_Variables::getDefaultVariables(); -$gallery = $ansel_storage->getGallery($vars->get('gallery')); -if (is_a($gallery, 'PEAR_Error')) { - $notification->push($gallery->getMessage()); +try { + $gallery = $ansel_storage->getGallery($vars->get('gallery')); +} catch (Ansel_Exception $e) { + $notification->push($e->getMessage()); header('Location: ' . Horde::applicationUrl('list.php')); exit; } diff --git a/ansel/report.php b/ansel/report.php index 00dc50087..7f24641fe 100644 --- a/ansel/report.php +++ b/ansel/report.php @@ -16,9 +16,9 @@ Horde_Registry::appInit('ansel'); $title = _("Do you really want to report this gallery?"); $gallery_id = (int)Horde_Util::getFormData('gallery'); - -$gallery = $ansel_storage->getGallery($gallery_id); -if (is_a($gallery, 'PEAR_Error')) { +try { + $gallery = $ansel_storage->getGallery($gallery_id); +} catch (Ansel_Exception $e) { $notification->push($gallery->getMessage()); header('Location: ' . Horde::applicationUrl('view.php?view=List', true)); exit; @@ -34,9 +34,9 @@ if (($image_id = Horde_Util::getFormData('image')) !== null) { } else { $style = $gallery->getStyle(); $return_url = Ansel::getUrlFor('view', - array('gallery' => $gallery_id, - 'view' => 'Gallery'), - true); + array('gallery' => $gallery_id, + 'view' => 'Gallery'), + true); } $vars = Horde_Variables::getDefaultVariables(); @@ -61,9 +61,7 @@ $gallery_id = Horde_Util::getFormData('id'); if ($form->validate()) { if (Horde_Util::getFormData('submitbutton') == _("Report")) { - require ANSEL_BASE . '/lib/Report.php'; $report = Ansel_Report::factory(); - $body = _("Gallery Name") . ': ' . $gallery->get('name') . "\n" . _("Gallery Description") . ': ' . $gallery->get('desc') . "\n" . _("Gallery Id") . ': ' . $gallery->id . "\n" @@ -71,12 +69,11 @@ if ($form->validate()) { . _("Report reason") . ': ' . $vars->get('reason') . "\n" . $return_url; - $result = $report->report($body); - if (is_a($result, 'PEAR_Error')) { - $notification->push(_("Gallery was not reported.") . ' ' . - $result->getMessage(), 'horde.error'); - } else { + try { + $result = $report->report($body); $notification->push(_("Gallery was reported."), 'horde.success'); + } catch (Horde_Exception $e) { + $notification->push(_("Gallery was not reported.") . ' ' . $result->getMessage(), 'horde.error'); } } else { $notification->push(_("Gallery was not reported."), 'horde.warning'); diff --git a/ansel/scripts/all_images_exif_to_tags.php b/ansel/scripts/all_images_exif_to_tags.php index 2664075e6..36856927e 100755 --- a/ansel/scripts/all_images_exif_to_tags.php +++ b/ansel/scripts/all_images_exif_to_tags.php @@ -16,7 +16,7 @@ Horde_Registry::appInit('ansel', array('authentication' => 'none', 'cli' => true $ret = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), 'hu:p:f:', array('help', 'username=', 'password=', 'fields=')); -if (is_a($ret, 'PEAR_Error')) { +if ($ret instanceof PEAR_Error) { $cli->fatal($ret->getMessage()); } @@ -73,22 +73,15 @@ if (!Horde_Auth::isAdmin()) { // Get the list of image ids that have exif data. $sql = 'SELECT DISTINCT image_id from ansel_image_attributes;'; $results = $GLOBALS['ansel_db']->query($sql); -if (is_a($results, 'PEAR_Error')) { +if ($results instanceof PEAR_Error) { $cli->fatal($results->getMessage()); } $image_ids = $results->fetchAll(MDB2_FETCHMODE_ASSOC); $results->free(); foreach (array_values($image_ids) as $image_id) { $image = $ansel_storage->getImage($image_id['image_id']); - if (!is_a($image, 'PEAR_Error')) { - $results = $image->exifToTags($exif_fields); - if (is_a($results, 'PEAR_Error')) { - $cli->message(sprintf(_("Could not extract exif fields from %s: %s"), $image_id['image_id'], $results->getMessage()), 'cli.error'); - } - $cli->message(sprintf(_("Extracted exif fields from %s"), $image->filename), 'cli.success'); - } else { - $cli->message(sprintf(_("Could not extract exif fields from %s: %s"), $image_id['image_id'], $image->getMessage()), 'cli.error'); - } + $results = $image->exifToTags($exif_fields); + $cli->message(sprintf(_("Extracted exif fields from %s"), $image->filename), 'cli.success'); } $cli->message(_("Done")); exit; diff --git a/ansel/scripts/ansel.php b/ansel/scripts/ansel.php index 83d585dc9..2cb26cee9 100755 --- a/ansel/scripts/ansel.php +++ b/ansel/scripts/ansel.php @@ -19,7 +19,7 @@ $ret = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), 'create=', 'gallery=', 'add=', 'dir=', 'caption=')); -if (is_a($ret, 'PEAR_Error')) { +if ($ret instanceof PEAR_Error) { $error = _("Couldn't read command-line options."); Horde::logMessage($error, 'DEBUG'); $cli->fatal($error); @@ -108,10 +108,8 @@ if (!empty($galleryId)) { $cli->fatal($error); } else { $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_Error') || - !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - $error = sprintf(_("Access denied adding photos to \"%s\"."), - $galleryId); + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { + $error = sprintf(_("Access denied adding photos to \"%s\"."), $galleryId); Horde::logMessage($error, 'WARN'); $cli->fatal($error); } @@ -129,48 +127,36 @@ if (!empty($createGallery)) { $attributes = array('name' => $gallery_name, 'desc' => $gallery_desc, 'owner' => $gallery_owner); - $gallery = $ansel_storage->createGallery($attributes, null, $parent); - if (is_a($gallery, 'PEAR_Error')) { + try { + $gallery = $ansel_storage->createGallery($attributes, null, $parent); + } catch (Ansel_Exception $e) { $galleryId = null; $error = sprintf(_("The gallery \"%s\" couldn't be created: %s"), $gallery_name, $gallery->getMessage()); Horde::logMessage($error, 'ERR'); $cli->fatal($error); - } else { - $msg = sprintf(_("The gallery \"%s\" was created successfully."), - $gallery_name); - Horde::logMessage($msg, 'DEBUG'); - $cli->message($msg, 'cli.success'); } + $msg = sprintf(_("The gallery \"%s\" was created successfully."), $gallery_name); + Horde::logMessage($msg, 'DEBUG'); + $cli->message($msg, 'cli.success'); } // List galleries/images. if (!empty($list)) { if (!empty($gallery)) { $images = $gallery->listImages(); - if (is_a($images, 'PEAR_Error')) { - $cli->fatal($images->getMessage()); - } - $cli->message(sprintf(_("Listing photos in %s"), $gallery->get('name')), 'cli.success'); $cli->writeln(); $images = array_keys($images); foreach ($images as $id) { - $image = &$ansel_storage->getImage($id); + $image = $ansel_storage->getImage($id); $cli->writeln(str_pad($image->filename, 30) . $image->getVFSPath() . '/' . $id); } } else { $galleries = $GLOBALS['ansel_storage']->listGalleries(); - if (is_a($galleries, 'PEAR_Error')) { - $error = _("Couldn't list galleries."); - Horde::logMessage($error, 'DEBUG'); - $cli->fatal($error); - } - $cli->message(_("Listing Gallery/Name"), 'cli.success'); $cli->writeln(); - foreach ($galleries as $id => $gallery) { $name = $gallery->get('name'); $msg = "$id/$name"; @@ -181,35 +167,25 @@ if (!empty($list)) { } // Add an image from the filesystem. -if (!empty($file) && isset($gallery) && !is_a($gallery, 'PEAR_Error')) { - $image = &Ansel::getImageFromFile($file, array('caption' => $caption)); - if (is_a($image, 'PEAR_Error')) { - Horde::logMessage($image, 'WARN'); - $cli->fatal($image->getMessage()); - } - - $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message'); - $image_id = $gallery->addImage($image); - if (is_a($image_id, 'PEAR_Error')) { +if (!empty($file) && isset($gallery)) { + try { + $image = Ansel::getImageFromFile($file, array('caption' => $caption)); + $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message'); + $image_id = $gallery->addImage($image); + } catch (Ansel_Exception $e) { $error = sprintf(_("There was a problem adding the photo \"%s\" to gallery \"%s\": %s"), - basename($file), $galleryId, $image_id->getMessage()); + basename($file), $galleryId, $e->getMessage()); Horde::logMessage($error, 'ERR'); $cli->fatal($error); } - - $msg = sprintf(_("Successfully added photo \"%s\" to gallery \"%s\"."), - basename($file), $galleryId); + $msg = sprintf(_("Successfully added photo \"%s\" to gallery \"%s\"."), basename($file), $galleryId); $cli->message($msg, 'cli.success'); Horde::logMessage($msg, 'NOTICE'); } // Add all images from a directory on the filesystem. -if (!empty($dir) && isset($gallery) && !is_a($gallery, 'PEAR_Error')) { +if (!empty($dir) && isset($gallery)) { $msg = addDirToGallery($dir, $gallery); - if (is_a($msg, 'PEAR_Error')) { - Horde::logMessage($msg, 'ERR'); - $cli->fatal($msg->getMessage()); - } if ($msg) { $msg = sprintf(ngettext("Successfully added %d photo (%s) to gallery \"%s\" from \"%s\".", "Successfully added %d photos (%s) to gallery \"%s\" from \"%s\".", count($msg)), count($msg), join(', ', $msg), $galleryId, $dir); @@ -264,23 +240,15 @@ function addDirToGallery($dir = '', &$gallery) // Process each file and upload to the gallery. $added_images = array(); foreach ($files_array as $file) { - $image = Ansel::getImageFromFile($dir . '/' . $file); - if (is_a($image, 'PEAR_Error')) { - Horde::logMessage($image, 'WARN'); + try { + $image = Ansel::getImageFromFile($dir . '/' . $file); + $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message'); + $image_id = $gallery->addImage($image); + } catch (Ansel_Exception $e) { + Horde::logMessage($e->getMessage(), 'WARN'); $cli->message($image->getMessage(), 'cli.error'); continue; } - - $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message'); - $image_id = $gallery->addImage($image); - if (is_a($image_id, 'PEAR_Error')) { - $error = sprintf(_("There was a problem adding the photo \"%s\" to gallery \"%s\"."), - $file, $galleryId); - Horde::logMessage($error, 'ERR'); - $cli->message($image_id->getMessage(), 'cli.error'); - continue; - } - $added_images[] = $file; } diff --git a/ansel/scripts/garbage_collection.php b/ansel/scripts/garbage_collection.php index 94a94182b..9e4e7aa96 100755 --- a/ansel/scripts/garbage_collection.php +++ b/ansel/scripts/garbage_collection.php @@ -19,7 +19,7 @@ $move = $verbose = false; $ret = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), 'mv', array('move', 'verbose')); -if (is_a($ret, 'PEAR_Error')) { +if ($ret instanceof PEAR_Error) { die("Couldn't read command-line options.\n"); } list($opts, $args) = $ret; diff --git a/ansel/scripts/recursive_import.php b/ansel/scripts/recursive_import.php index e8b9b9c9e..2e4f73964 100755 --- a/ansel/scripts/recursive_import.php +++ b/ansel/scripts/recursive_import.php @@ -16,7 +16,7 @@ Horde_Registry::appInit('ansel', array('authentication' => 'none', 'cli' => true $ret = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), 'hu:p:lc:g:a:d:k', array('help', 'username=', 'password=', 'dir=', 'keep')); -if (is_a($ret, 'PEAR_Error')) { +if ($ret instanceof PEAR_Error) { $cli->fatal($ret->getMessage()); } @@ -77,11 +77,9 @@ if (empty($dir)) { Horde_Nls::setCharset('utf-8'); $gallery_id = processDirectory($dir); -if (!$keepEmpties && !is_a($gallery_id, 'PEAR_Error')) { +if (!$keepEmpties) { $gallery = $ansel_storage->getGallery($gallery_id); - if (!is_a($gallery, 'PEAR_Error')) { - emptyGalleryCheck($gallery); - } + emptyGalleryCheck($gallery); } exit; @@ -134,11 +132,7 @@ function processDirectory($dir, $parent = null) $name = basename($dir); $cli->message(sprintf(_("Creating gallery: \"%s\""), $name), 'cli.message'); $gallery = $GLOBALS['ansel_storage']->createGallery(array('name' => $name), null, $parent); - if (is_a($gallery, 'PEAR_Error')) { - $cli->fatal(sprintf(_("The gallery \"%s\" couldn't be created: %s"), $name, $gallery->getMessage())); - } else { - $cli->message(sprintf(_("The gallery \"%s\" was created successfully."), $name), 'cli.success'); - } + $cli->message(sprintf(_("The gallery \"%s\" was created successfully."), $name), 'cli.success'); // Read all the files into an array. $files = array(); @@ -163,18 +157,8 @@ function processDirectory($dir, $parent = null) $added_images = array(); foreach ($files as $file) { $image = Ansel::getImageFromFile($dir . '/' . $file); - if (is_a($image, 'PEAR_Error')) { - $cli->message($image->getMessage(), 'cli.error'); - continue; - } - $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message'); $image_id = $gallery->addImage($image); - if (is_a($image_id, 'PEAR_Error')) { - $cli->message($image_id->getMessage(), 'cli.error'); - continue; - } - $added_images[] = $file; } diff --git a/ansel/scripts/remote_import.php b/ansel/scripts/remote_import.php index 4303c3c22..ef32cd0f6 100755 --- a/ansel/scripts/remote_import.php +++ b/ansel/scripts/remote_import.php @@ -19,7 +19,7 @@ Horde_Registry::appInit('ansel', array('authentication' => 'none', 'cli' => true $ret = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), 'hu:p:g:s:d:kr:zl', array('help', 'username=', 'password=', 'gallery=', 'slug=', 'dir=', 'keep', 'remotehost=', 'gzip', 'lzf')); -if (is_a($ret, 'PEAR_Error')) { +if ($ret instanceof PEAR_Error) { $cli->fatal($ret->getMessage()); } @@ -162,14 +162,8 @@ function processDirectory($dir, $parent = null, $gallery_id = null, $slug = null null, is_null($slug) ? null : array($slug), ); - $result = Horde_RPC::request('jsonrpc', $rpc_endpoint, $method, $params, $rpc_auth); - if (is_a($result, 'PEAR_Error')) { - $cli->fatal($result->getMessage()); - } + $result = Horde_Rpc::request('jsonrpc', $rpc_endpoint, $method, $params, $rpc_auth); $result = $result->result; - if (is_a($result, 'PEAR_Error')) { - $cli->fatal($result->getMessage()); - } if (empty($result)) { $cli->fatal(sprintf(_("Gallery %s not found."), (empty($slug) ? $gallery_id : $slug))); } @@ -188,16 +182,9 @@ function processDirectory($dir, $parent = null, $gallery_id = null, $slug = null $cli->message(sprintf(_("Creating gallery: \"%s\""), $name), 'cli.message'); $method = 'images.createGallery'; $params = array(null, array('name' => $name), null, $parent); - $result = Horde_RPC::request('jsonrpc', $rpc_endpoint, $method, $params, $rpc_auth); - if (is_a($result, 'PEAR_Error')) { - $cli->fatal($result->getMessage()); - } + $result = Horde_Rpc::request('jsonrpc', $rpc_endpoint, $method, $params, $rpc_auth); $gallery_id = $result->result; - if (is_a($gallery_id, 'PEAR_Error')) { - $cli->fatal(sprintf(_("The gallery \"%s\" couldn't be created: %s"), $name, $gallery_id->getMessage())); - } else { - $cli->message(sprintf(_("The gallery \"%s\" was created successfully."), $name), 'cli.success'); - } + $cli->message(sprintf(_("The gallery \"%s\" was created successfully."), $name), 'cli.success'); } /* Get the files and directories */ @@ -230,28 +217,14 @@ function processDirectory($dir, $parent = null, $gallery_id = null, $slug = null $added_images = array(); foreach ($files as $file) { $image = getImageFromFile($dir . '/' . $file, $compress); - if (is_a($image, 'PEAR_Error')) { - $cli->message($image->getMessage(), 'cli.error'); - continue; - } - $cli->message(sprintf(_("Storing photo \"%s\"..."), $file), 'cli.message'); $method = 'images.saveImage'; $params = array(null, $gallery_id, $image, false, null, 'binhex', $slug, $compress); - $result = Horde_RPC::request('jsonrpc', $rpc_endpoint, $method, $params, $rpc_auth); - if (is_a($result, 'PEAR_Error')) { - $cli->fatal($result->getMessage()); - } - - if (!is_a($result, 'stdClass')) { + $result = Horde_Rpc::request('jsonrpc', $rpc_endpoint, $method, $params, $rpc_auth); + if (!($result instanceof stdClass)) { $cli->fatal(sprintf(_("There was an unspecified error. The server returned: %s"), print_r($result, true))); } $image_id = $result->result; - if (is_a($image_id, 'PEAR_Error')) { - $cli->message($image_id->getMessage(), 'cli.error'); - continue; - } - $added_images[] = $file; } diff --git a/ansel/scripts/upgrades/2008-09-16_add_original_date_values.php b/ansel/scripts/upgrades/2008-09-16_add_original_date_values.php index 2f64db118..07d125916 100755 --- a/ansel/scripts/upgrades/2008-09-16_add_original_date_values.php +++ b/ansel/scripts/upgrades/2008-09-16_add_original_date_values.php @@ -24,7 +24,7 @@ foreach ($results as $image) { } $sql = 'UPDATE ansel_images SET image_original_date = ' . (int)$datetime . ' WHERE image_id = ' . (int)$image['image_id']; $result = $ansel_db->exec($sql); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { $cli->fatal($result->getMessage()); } $cli->message(sprintf("Image %d updated.", $image['image_id']), 'cli.message'); diff --git a/ansel/xppublish.php b/ansel/xppublish.php index 099420027..b3fd9bf5f 100644 --- a/ansel/xppublish.php +++ b/ansel/xppublish.php @@ -82,11 +82,11 @@ if ($cmd == 'select') { if (!$galleryId || !$ansel_storage->galleryExists($galleryId)) { $error = _("Invalid gallery specified.") . "
    \n"; } else { - $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_ERROR')) { - $error = _("There was an error accessing the gallery"); - } else { + try { + $gallery = $ansel_storage->getGallery($galleryId); $error = false; + } catch (Ansel_Exception $e) { + $error = _("There was an error accessing the gallery"); } } @@ -113,16 +113,16 @@ if ($cmd == 'new') { $gallery_desc = Horde_Util::getFormData('gallery_desc'); if ($create) { /* Creating a new gallery. */ - $gallery = $ansel_storage->createGallery(array('name' => $gallery_name, - 'desc' => $gallery_desc)); - if (is_a($gallery, 'PEAR_Error')) { - $error = sprintf(_("The gallery \"%s\" couldn't be created: %s"), $gallery_name, $gallery->getMessage()); - Horde::logMessage($error, 'ERR'); - } else { + try { + $gallery = $ansel_storage->createGallery( + array('name' => $gallery_name, 'desc' => $gallery_desc)); $galleryId = $gallery->id; $msg = sprintf(_("The gallery \"%s\" was created successfully."), $gallery_name); Horde::logMessage($msg, 'DEBUG'); - } + } catch (Ansel_Exception $e) { + $error = sprintf(_("The gallery \"%s\" couldn't be created: %s"), $gallery_name, $e->getMessage()); + Horde::logMessage($error, 'ERR'); + } } else { if (empty($galleryId) && $prefs->getValue('autoname')) { $galleryId = md5(microtime()); @@ -169,11 +169,15 @@ if ($cmd == 'add') { if (!$galleryId || !$ansel_storage->galleryExists($galleryId)) { $error = _("Invalid gallery specified.") . "
    \n"; } else { - $gallery = $ansel_storage->getGallery($galleryId); - if (is_a($gallery, 'PEAR_ERROR') || !$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { - $error = sprintf(_("Access denied adding photos to \"%s\"."), $gallery->get('name')); - } else { - $error = false; + try { + $gallery = $ansel_storage->getGallery($galleryId); + if (!$gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::EDIT)) { + $error = sprintf(_("Access denied adding photos to \"%s\"."), $gallery->get('name')); + } else { + $error = false; + } + } catch (Ansel_Exception $e) { + $error = _("There was an error accessing the gallery"); } } if (!$name || $error) { @@ -181,20 +185,18 @@ if ($cmd == 'add') { } else { try { $GLOBALS['browser']->wasFileUploaded('imagefile', _("photo")); - $image = &Ansel::getImageFromFile($file, array('image_filename' => $name)); - if (is_a($image, 'PEAR_Error')) { - $error = $image->getMessage(); - } else { - $gallery = $ansel_storage->getGallery($galleryId); + try { + $image = Ansel::getImageFromFile($file, array('image_filename' => $name)); + } catch (Ansel_Exception $e) { + $error = $e->getMessage(); + } + + $gallery = $ansel_storage->getGallery($galleryId); + try { $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(); - } + $error = false; + } catch (Ansel_Exception $e) { + $error = _("There was a problem uploading the photo."); } } catch (Horde_Browser_Exception $e) { $error = $e->getMessage(); -- 2.11.0