[ansel] Patch to add search capability
Heath S. Hendrickson
heath at outerspaceconsultants.com
Tue May 11 22:48:46 PDT 2004
It's a rather limited search functionality, but it works.
The following files were modified:
templates/menu/menu.inc (to add the search button, as it's not
gallery specific)
lib/Gallery.php (to add a simple function to call getByAttributes())
And the following new file was created:
search.php
First you need to realize that the Exif data as stored in the DataTree
is the "raw" Exif data, so searching it might not be all that intuitive
and it surely won't match with what you see in the Exif pretty-print
with each image. For now, though, I wanted to get this submitted so
that it could be vetted and hopefully committed before going any
further. That way others could try it out and give me some feedback.
The only thing that I haven't figured out how to do is to verify
permissions on the found images before displaying them in the final
result set. It's not critical, though, becasue the gallery permissions
should deny a user from seeing the image if they don't have the correct
permissions. It's just an information disclosure vulnerability, for
those of you keeping track. I suppose I could run each image's gallery
through a quick check, and use local caching to speed it up... I'll have
to think about it though, and it's getting late...
h
-------------- next part --------------
<?php
/**
* $Horde: ansel/reorder.php,v 1.2 2004/03/23 22:08:54 chuck Exp $
*
* Copyright 2001-2004 Chuck Hagenbuch <chuck at horde.org>
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*/
@define('ANSEL_BASE', dirname(__FILE__));
require_once ANSEL_BASE . '/lib/base.php';
// include this so we don't have to sync any new Exif fields if they get added
require_once ANSEL_BASE . '/lib/Exif.php';
require_once 'Horde/Form.php';
require_once 'Horde/Form/Renderer.php';
class SearchForm extends Horde_Form {
var $_useFormToken = false;
function SearchForm(&$vars, $title)
{
parent::Horde_Form($vars, $title);
$this->setButtons(_("Search"));
$this->addHidden('', 'actionID', 'text', false);
// this gets the known Exif fields
$fields = Ansel_ImageData::getFields();
// now add on any others that we may want
$fields += array('description' => _("Description"),
'date-uploaded' => _("Date Uploaded"),
'filename' => _("Filename"),
'gallery' => _("Gallery Name"));
$name = &$this->addVariable(_("Field"), 'field', 'enum', false, false, null, array($fields));
$ops = array('IS' => _("Is"),
'NOT' => _("Is Not"),
'BEGIN' => _("Begins With"),
'END' => _("Ends With"),
'LIKE' => _("Contains"),
'NOT LIKE' => _("Does Not Contain"));
$ops_val = &$this->addVariable(_("Operator"), 'op', 'enum', false, false, null, array($ops));
$w = &$this->addVariable(_("Value"), 'value', 'text', false, false, null);
}
}
$actionID = Util::getFormData('actionID');
require_once 'Horde/Variables.php';
$vars = &Variables::getDefaultVariables();
$title = 'Search for Images';
require ANSEL_TEMPLATES . '/common-header.inc';
Ansel::menu();
$vars->set('actionID', 'search');
$form = &Horde_Form::singleton('SearchForm', $vars, sprintf(_("Search for Images"), $title));
$renderer = &new Horde_Form_Renderer();
$form->renderActive($renderer, $vars, 'search.php', 'post', 'multipart/form-data');
// now display the results, if there are any. This won't do anything the first time it's called
switch ($actionID) {
case 'search':
// set up the search criteria to use
$attr_name = Util::getFormData('field');
$attr_op = Util::getFormData('op');
$attr_value = Util::getFormData('value');
// escape the SQL regex wildcards
$attr_value = str_replace(array("%", "_"), array("\%", "\_"), $attr_value);
// for the CONTAINS and DOES NOT CONTAIN cases, we add the SQL regex wildcards to the front and back of the value
if ($attr_op == 'LIKE' || $attr_op == 'NOT LIKE') {
$attr_value = '%' . $attr_value . '%';
} else if ($attr_op == 'BEGIN') {
$attr_op = 'LIKE';
$attr_value = $attr_value . '%';
} else if ($attr_op == 'END') {
$attr_op = 'LIKE';
$attr_value = '%' . $attr_value;
}
$crit['AND'] = array(array('field' => 'name',
'op' => '=',
'test' => $attr_name),
array('field' => 'value',
'op' => $attr_op,
'test' => $attr_value));
$imagelist = &$ansel_shares->searchAllImages($crit);
// now display the results
Horde::addScriptFile('popup.js', 'horde');
echo "<div class='text'>";
echo "<p class='header'>Found " . count($imagelist) . " matching images.</p>";
echo "<table width='100%' cellpadding='5' cellspacing='5'>";
echo "<tr class='item'><th class='item'>#</th><th class='item'>Image</th><th class='item'>Gallery</th><th class='item'>Description</th></tr>";
$num = 1;
foreach ($imagelist as $id => $name) {
$image = &$ansel_shares->getImage($name);
$thumb_url = Ansel::getImageUrl($id, 'thumb');
$link = Horde::link('', _("Thumbnail"), 'widget', '', 'popup(\'' . $thumb_url . '\',' . $conf['thumbnail']['width'] . ', ' . $conf['thumbnail']['height'] .'); return false;') . $image->get('filename') . '</a>';
$galleryName = $image->get('gallery');
$gallery = $ansel_shares->getShare($galleryName);
$galleryLink = Horde::link(Horde::applicationUrl(Util::addParameter('view.php', array('gallery' => $galleryName, 'page' => '1')))) . $gallery->get('name') . '</a>';
$description = $image->get('description');
echo '<tr class="item'. ($num % 2) . '"><td>' . $num . '</td><td>' . $link . '</td><td>' . $galleryLink . '</td><td>';
if (!empty($description)) {
echo $description;
} else {
echo ' ';
}
echo '</td></tr>';
$num++;
}
echo "</table>";
echo "</div>";
exit;
}
require $registry->getParam('templates', 'horde') . '/common-footer.inc';
-------------- next part --------------
Index: lib/Gallery.php
===================================================================
RCS file: /usr/local/horde/cvs/ansel/lib/Gallery.php,v
retrieving revision 1.58
diff -u -r1.58 Gallery.php
--- lib/Gallery.php 10 May 2004 03:26:48 -0000 1.58
+++ lib/Gallery.php 12 May 2004 04:04:07 -0000
@@ -144,6 +144,14 @@
}
/**
+ * Returns a list of images maching the specified criteria
+ */
+ function searchAllImages($criteria)
+ {
+ return $this->_datatree->getByAttributes($criteria, '-1', true, false);
+ }
+
+ /**
* Adds a new DataTreeObject_Gallery to the object backend
*
* @param object DataTreeObject_Gallery $gallery The gallery to add
@@ -292,6 +300,12 @@
// Update the modified flag.
$this->data['last-modified'] = time();
+ // Set the upload date the flag.
+ $this->data['date-uploaded'] = time();
+
+ // Set the order to the end of the gallery.
+ $this->setOrder($imageOb->getId(), $this->data['num-images']);
+
// Store changes to the gallery.
$this->update();
-------------- next part --------------
Index: templates/menu/menu.inc
===================================================================
RCS file: /usr/local/horde/cvs/ansel/templates/menu/menu.inc,v
retrieving revision 1.17
diff -u -r1.17 menu.inc
--- templates/menu/menu.inc 7 Apr 2004 15:18:44 -0000 1.17
+++ templates/menu/menu.inc 12 May 2004 03:39:41 -0000
@@ -36,6 +36,9 @@
echo Menu::createItem('#', _("Print"), 'print.gif', $registry->getParam('graphics', 'horde'), '', "open_print_win('$print_link'); return false;");
}
+/* The search menu item */
+echo Menu::createItem(Horde::applicationUrl('search.php'), _("Search"), 'search.gif');
+
/* Additional site-specific menu items. */
Menu::siteLinks();
More information about the ansel
mailing list