function urlMaker($text, $wrap)
{
    $text = str_replace("\n", " \n ", $text);
    $words = explode(' ', $text);
    require_once 'geograph/gridsquare.class.php';
    $g_square = new GridSquare();
    $prefixes = $g_square->getGridPrefixes();
    for ($i = 0; $i < sizeof($words); $i++) {
        $word = $words[$i];
        //Trim below is necessary is the tag is placed at the begin of string
        $c = 0;
        $host = "http:\\/\\/" . str_replace('.', '\\.', $_SERVER['HTTP_HOST']);
        $words[$i] = preg_replace("/(^|\\[){$host}\\/photo\\/(\\d+)/", '\\1[[\\2]]', $words[$i]);
        $words[$i] = preg_replace("/(^|\\[){$host}\\/view\\.php\\?id=(\\d+)/", '\\1[[\\2]]', $words[$i]);
        $words[$i] = preg_replace("/^{$host}\\/gridref\\/([STNH]?[A-Z]{1}\\d{2,10}|[A-Z]{3}\\d{2,10})/", '[[\\1]]', $words[$i]);
        $words[$i] = preg_replace("/^(\\!?)([STNH]?[A-Z]{1}|[A-Z]{3})(\\d{2,10})([^\\w]?)\$/e", '((!"$1"&&strlen("$3")%2==0&&$prefixes["$2"])?"[[$2$3]]":"$2$3")."$4"', $words[$i]);
        //todo? strip the ! even if wont fire for another reason eg [b]on the !B5467[/b]
        if ($word != $words[$i]) {
        } elseif (strtolower(substr($words[$i], 0, strlen($_SERVER['HTTP_HOST']) + 24)) == 'http://' . $_SERVER['HTTP_HOST'] . '/mapbrowse.php?t=') {
            $c = 1;
            $word = '<a href=\\"' . trim($words[$i]) . '\\" target=\\"_new\\" rel=\\"nofollow\\">Geograph Map</a>';
        } elseif (strtolower(substr($words[$i], 0, 7)) == 'http://') {
            $c = 1;
            $word = '<a href=\\"' . trim($words[$i]) . '\\" target=\\"_new\\" rel=\\"nofollow\\">' . trim($word) . '</a>';
        } elseif (strtolower(substr($words[$i], 0, 8)) == 'https://') {
            $c = 1;
            $word = '<a href=\\"' . trim($words[$i]) . '\\" target=\\"_new\\" rel=\\"nofollow\\">' . trim($word) . '</a>';
        } elseif (strtolower(substr($words[$i], 0, 6)) == 'ftp://') {
            $c = 1;
            $word = '<a href=\\"' . trim($words[$i]) . '\\" target=\\"_new\\" rel=\\"nofollow\\">' . trim($word) . '</a>';
        } elseif (strtolower(substr($words[$i], 0, 4)) == 'ftp.') {
            $c = 1;
            $word = '<a href=\\"ftp://' . trim($words[$i]) . '\\" target=\\"_new\\" rel=\\"nofollow\\">' . trim($word) . '</a>';
        } elseif (strtolower(substr($words[$i], 0, 4)) == 'www.') {
            $c = 1;
            $word = '<a href="http://' . trim($words[$i]) . '\\" target=\\"_new\\" rel=\\"nofollow\\">' . trim($word) . '</a>';
        } elseif (strtolower(substr($words[$i], 0, 7)) == 'mailto:') {
            $c = 1;
            $word = '<a href=\\"' . trim($words[$i]) . '\\" rel=\\"nofollow\\">' . trim($word) . '</a>';
        }
        if ($c == 1) {
            $words[$i] = $word;
        }
        //$words[$i]=str_replace ("\n ", "\n", $words[$i]);
    }
    $ret = str_replace(" \n ", "\n", implode(' ', $words));
    return $ret;
}
 function getSQLParts()
 {
     global $CONF;
     extract($this->sql, EXTR_PREFIX_ALL ^ EXTR_REFS, 'sql');
     if (!empty($_GET['BBOX'])) {
         //we need to turn this off as it will break the caching of number of results!
         $CONF['search_count_first_page'] = 0;
         list($west, $south, $east, $north) = explode(',', trim(str_replace('e ', 'e+', $_GET['BBOX'])));
         if (!empty($_GET['LOOKAT'])) {
             // calculate the approx center of the view -- note that this is innaccurate if the user is not looking straight down
             $clong = ($east - $west) / 2 + $west;
             $clat = ($north - $south) / 2 + $south;
             list($long, $lat) = preg_split('/,|\\s/', str_replace('e ', 'e+', $_GET['LOOKAT']));
             $uplat = ($clat + $north) / 2.0;
             $lolat = ($south + $clat) / 2.0;
             //is the lookat point outside the central square of the BBOX (hence large tilt)
             if ($lat > $uplat) {
                 $diflat = $north - $lat;
             } elseif ($lat < $lolat) {
                 $diflat = $lat - $south;
             }
             $uplong = ($clong + $east) / 2.0;
             $lolong = ($west + $clong) / 2.0;
             if ($long > $uplong) {
                 $diflong = $east - $long;
             } elseif ($long < $lolong) {
                 $diflong = $long - $west;
             }
             //find a suitable 'distance' from an edge
             $dif = abs(max($diflat, $diflong));
             //if we have an off center view create a new square and recenter it 'in the foreground'
             if ($dif) {
                 function interpolate_part($one, $two, $fraction)
                 {
                     $big = $two - $one;
                     $small = $fraction * $big;
                     return $one + $small;
                 }
                 $linelength = sqrt(pow($lat - $clat, 2) + pow($long - $clong, 2));
                 $fraction = $dif / $linelength;
                 //find the point on the line between the lookat and the center point
                 $nlat = interpolate_part($lat, $clat, $fraction);
                 $nlong = interpolate_part($long, $clong, $fraction);
                 //and recenter the 'square' on that new point
                 $south = $nlat - $dif;
                 $north = $nlat + $dif;
                 $west = $nlong - $dif;
                 $east = $nlong + $dif;
             }
         }
         $rectangle = "'POLYGON(({$west} {$south},{$east} {$south},{$east} {$north},{$west} {$north},{$west} {$south}))'";
         $sql_where = "CONTAINS(GeomFromText({$rectangle}),point_ll)";
         $this->sphinx['impossible']++;
         //todo we might be able to transform it to a set of GR's?
     } else {
         $sql_where = '';
     }
     $x = $this->x;
     $y = $this->y;
     if (!empty($x) && !empty($y)) {
         if ($this->limit8 && $this->limit8 < 2000 && $this->limit8 > -2000) {
             //2000 is a special value for effectivly unlimted!
             $d = abs(intval($this->limit8));
             if ($sql_where) {
                 $sql_where .= ' and ';
             }
             if ($this->limit8 == 1) {
                 $sql_where .= "CONTAINS( GeomFromText('POINT({$x} {$y})'),point_xy )";
             } else {
                 $left = $x - $d;
                 $right = $x + $d - 1;
                 $top = $y + $d - 1;
                 $bottom = $y - $d;
                 $rectangle = "'POLYGON(({$left} {$bottom},{$right} {$bottom},{$right} {$top},{$left} {$top},{$left} {$bottom}))'";
                 $sql_where .= "CONTAINS(GeomFromText({$rectangle}),point_xy)";
                 if ($this->limit8 > 0) {
                     //shame cant use dist_sqd in the next line!
                     $sql_where .= " and ((gs.x - {$x}) * (gs.x - {$x}) + (gs.y - {$y}) * (gs.y - {$y})) < " . $d * $d;
                 }
             }
         }
         if ($this->limit8 && $this->limit8 <= 20 && $this->limit8 > -20) {
             //possible, but calculate it JIT
             $this->sphinx['x'] = $x;
             $this->sphinx['y'] = $y;
             $this->sphinx['d'] = $this->limit8;
             $this->sphinx['sort'] = "@geodist ASC, @relevance DESC, @id DESC";
         } else {
             $this->sphinx['impossible']++;
         }
         if ($this->limit8 == 1) {
             $sql_fields .= ", 0 as dist_sqd";
         } else {
             //not using "power(gs.x -$x,2) * power( gs.y -$y,2)" beucause is testing could be upto 2 times slower!
             $sql_fields .= ", ((gs.x - {$x}) * (gs.x - {$x}) + (gs.y - {$y}) * (gs.y - {$y})) as dist_sqd";
             $sql_order = ' dist_sqd ';
         }
     }
     if (($x == 0 && $y == 0 || $this->limit8) && $this->orderby) {
         switch ($this->orderby) {
             case 'random':
                 $sql_order = ' crc32(concat("' . $this->crt_timestamp_ts . '",gi.gridimage_id)) ';
                 $this->sphinx['compatible_order'] = 0;
                 $this->sphinx['sort'] = "@random";
                 break;
             case 'dist_sqd':
                 break;
             case 'relevance':
                 $sql_order = '';
                 $this->sphinx['sort'] = "@relevance DESC, @id DESC";
                 break;
             case 'imagetaken':
                 if ($sql_where) {
                     $sql_where .= ' and ';
                 }
                 $sql_where .= "imagetaken NOT LIKE '0000-%'";
                 //falls though...
             //falls though...
             default:
                 $sql_order = preg_replace('/[^\\w,\\(\\)]+/', ' ', $this->orderby);
                 switch (str_replace(' desc', '', $this->orderby)) {
                     case 'gridimage_id':
                     case 'submitted':
                         $this->sphinx['sort'] = '@id';
                         break;
                     case 'x':
                         $this->sphinx['compatible_order'] = 0;
                         $this->sphinx['sort'] = 'wgs84_long';
                         break;
                     case 'y':
                         $this->sphinx['compatible_order'] = 0;
                         $this->sphinx['sort'] = 'wgs84_lat';
                         break;
                     case 'imagetaken':
                         $this->sphinx['compatible_order'] = 0;
                         $this->sphinx['sort'] = 'takendays';
                         break;
                     case 'realname':
                     case 'title':
                     case 'imageclass':
                     case 'grid_reference':
                     default:
                         $this->sphinx['impossible']++;
                 }
                 if (!$this->sphinx['impossible'] && preg_match('/ desc$/', $this->orderby)) {
                     $this->sphinx['sort'] .= " DESC";
                 } else {
                     $this->sphinx['sort'] .= " ASC";
                 }
         }
         $sql_order = preg_replace('/^submitted/', 'gridimage_id', $sql_order);
     } elseif (empty($this->sphinx['sort']) || $this->sphinx['sort'] != "@geodist ASC, @relevance DESC, @id DESC") {
         //sphinx undefined is 'relevence' where mysql undefined is table order
         $this->sphinx['compatible_order'] = 0;
     }
     if ($this->breakby) {
         if (preg_match('/imagetaken_(year|month|decade)$/', $this->breakby) && strpos($sql_order, 'imagetaken') === FALSE) {
             switch ($this->breakby) {
                 case 'imagetaken_month':
                     $breakby = "SUBSTRING(imagetaken,1,7)";
                     break;
                 case 'imagetaken_year':
                     $breakby = "SUBSTRING(imagetaken,1,4)";
                     break;
                 case 'imagetaken_decade':
                     $breakby = "SUBSTRING(imagetaken,1,3)";
                     break;
             }
         } else {
             $breakby = preg_replace('/_(year|month|decade)$/', '', $this->breakby);
         }
         $breakby = preg_replace('/^submitted/', 'gridimage_id', $breakby);
         if (strpos($sql_order, ' desc') !== FALSE) {
             $breakby .= ' desc';
             $sorder2 = " DESC";
         } else {
             $sorder2 = " ASC";
         }
         switch (str_replace(' desc', '', $breakby)) {
             case 'gridimage_id':
             case 'submitted':
                 $sorder = '@id';
                 break;
             case 'x':
                 $this->sphinx['compatible_order'] = 0;
                 $sorder = 'wgs84_long';
                 break;
             case 'y':
                 $this->sphinx['compatible_order'] = 0;
                 $sorder = 'wgs84_lat';
                 break;
             case 'imagetaken':
                 $this->sphinx['compatible_order'] = 0;
                 $sorder = 'takendays';
                 break;
             case 'imageclass':
                 $sorder = 'classcrc';
                 break;
             case 'realname':
             case 'title':
             case 'grid_reference':
             default:
                 $this->sphinx['impossible']++;
         }
         if ($breakby != $sql_order && !preg_match('/^(\\w+)\\+$/i', $this->breakby)) {
             $sql_order = $breakby . ($sql_order ? ", {$sql_order}" : '');
             $this->sphinx['sort'] = "{$sorder} {$sorder2}" . ($this->sphinx['sort'] ? ", {$this->sphinx['sort']}" : '');
         }
     }
     $sql_where_start = $sql_where;
     $this->getSQLPartsFromText($this->searchtext);
     if (!empty($this->limit1)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         if (strpos($this->limit1, '!') === 0) {
             $sql_where .= 'gi.user_id != ' . preg_replace('/^!/', '', $this->limit1);
         } else {
             $sql_where .= 'gi.user_id = ' . $this->limit1;
         }
         $this->sphinx['filters']['user_id'] = $this->limit1;
     }
     if (!empty($this->limit2)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         $statuslist = "'" . implode("','", explode(',', $this->limit2)) . "'";
         $sql_where .= "moderation_status in ({$statuslist}) ";
         if ($this->limit2 == 'geograph') {
             $this->sphinx['filters']['status'] = $this->limit2;
         } elseif ($this->limit2 == 'accepted') {
             $this->sphinx['filters']['status'] = 'supplemental';
         }
     }
     if (!empty($this->limit3)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         $sql_where .= "imageclass = '" . addslashes($this->limit3 == '-' ? '' : $this->limit3) . "' ";
         //todo tags tags tags
         if ($this->limit3 == '-') {
             $this->sphinx['impossible']++;
         } else {
             #$this->sphinx['filters']['imageclass'] = "\"".$this->limit3."\"";
             $db = $this->_getDB();
             $this->sphinx['filters']['classcrc'] = array($db->GetOne('select crc32(' . $db->Quote($this->limit3) . ')'));
         }
     }
     if (!empty($this->limit4)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         $sql_where .= 'gs.reference_index = ' . $this->limit4 . ' ';
         if (empty($this->sphinx['d'])) {
             //no point adding this filter if querying on location!
             $square = new GridSquare();
             $prefixes = $square->getGridPrefixes($this->limit4);
             $this->sphinx['filters']['myriad'] = "(" . implode(' | ', $prefixes) . ")";
         }
     }
     if (!empty($this->limit5)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         $db = $this->_getDB();
         $prefix = $db->GetRow('select * from gridprefix where prefix=' . $db->Quote($this->limit5) . ' limit 1');
         $left = $prefix['origin_x'];
         $right = $prefix['origin_x'] + $prefix['width'] - 1;
         $top = $prefix['origin_y'] + $prefix['height'] - 1;
         $bottom = $prefix['origin_y'];
         $rectangle = "'POLYGON(({$left} {$bottom},{$right} {$bottom},{$right} {$top},{$left} {$top},{$left} {$bottom}))'";
         $sql_where .= "CONTAINS(GeomFromText({$rectangle}),point_xy)";
         if (empty($this->limit4)) {
             $sql_where .= ' and gs.reference_index = ' . $prefix['reference_index'] . ' ';
         }
         $this->sphinx['filters']['myriad'] = $this->limit5;
     }
     if (!empty($this->limit6)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         $dates = explode('^', $this->limit6);
         //if a 'to' search then we must make blank bits match the end!
         list($y, $m, $d) = explode('-', $dates[1]);
         if ($y > 0) {
             if ($m == 0) {
                 $m = 12;
             }
             if ($d == 0) {
                 $d = date('t', mktime(0, 0, 0, $m, 1, $y));
             }
             $dates[1] = "{$y}-{$m}-{$d}";
         }
         if ($dates[0]) {
             if (preg_match("/0{4}-([01]?[1-9]+|10)-/", $dates[0]) > 0) {
                 //month only
                 list($y, $m, $d) = explode('-', $dates[0]);
                 $sql_where .= "MONTH(submitted) = {$m} ";
                 $this->sphinx['impossible']++;
             } elseif (preg_match("/0{4}-0{2}-([01]?[1-9]+|10)/", $dates[0]) > 0) {
                 //day only ;)
                 list($y, $m, $d) = explode('-', $dates[0]);
                 $sql_where .= "submitted > DATE_SUB(NOW(),INTERVAL {$d} DAY)";
                 $this->sphinx['filters']['submitted'] = array(time() - 86400 * $d, time());
             } elseif ($dates[1]) {
                 if ($dates[0] == $dates[1]) {
                     //both the same
                     $sql_where .= "submitted LIKE '" . $dates[0] . "%' ";
                     $this->sphinx['filters']['submitted'] = array(strtotime($dates[0]), strtotime($dates[0] . " 23:59:59"));
                 } else {
                     //between
                     $sql_where .= "submitted BETWEEN '" . $dates[0] . "' AND DATE_ADD('" . $dates[1] . "',INTERVAL 1 DAY) ";
                     $this->sphinx['filters']['submitted'] = array(strtotime($dates[0]), strtotime($dates[1] . " 23:59:59"));
                 }
             } else {
                 //from
                 $sql_where .= "submitted >= '" . $dates[0] . "' ";
                 $this->sphinx['filters']['submitted'] = array(strtotime($dates[0]), time());
             }
         } else {
             //to
             $sql_where .= "submitted <= '" . $dates[1] . "' ";
             $this->sphinx['filters']['submitted'] = array(strtotime("2005-01-01"), strtotime($dates[1] . " 23:59:59"));
         }
     }
     if (!empty($this->limit7)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         $dates = explode('^', $this->limit7);
         $same = $dates[0] == $dates[1];
         //if a 'to' search then we must make blank bits match the end!
         list($y1, $m1, $d1) = explode('-', $dates[1]);
         if ($y1 > 0) {
             if ($m1 == 0) {
                 $m1 = 12;
             }
             if ($d1 == 0) {
                 $d1 = date('t', mktime(0, 0, 0, $m1, 1, $y1));
             }
             $dates[1] = sprintf('%04d-%02d-%02d', $y1, $m1, $d1);
         }
         if ($dates[0]) {
             list($y, $m, $d) = explode('-', $dates[0]);
             $days0 = $this->toDays($dates[0]);
             if (preg_match("/0{4}-([01]?[1-9]+|10)-/", $dates[0]) > 0) {
                 //month only
                 $sql_where .= "MONTH(imagetaken) = {$m} ";
                 $this->sphinx['impossible']++;
             } elseif (preg_match("/0{4}-0{2}-([01]?[1-9]+|10)/", $dates[0]) > 0) {
                 //day only ;)
                 $sql_where .= "imagetaken > DATE_SUB(NOW(),INTERVAL {$d} DAY)";
                 $start = $this->toDays("DATE_SUB(NOW(),INTERVAL {$d} DAY)");
                 $now = $this->toDays('NOW()');
                 $this->sphinx['filters']['takendays'] = array($start, $now);
             } elseif ($dates[1]) {
                 if ($same) {
                     //both the same
                     if ($m == 0) {
                         $sql_where .= "imagetaken LIKE '{$y}%' ";
                         $this->sphinx['filters']['takenyear'] = $y;
                     } elseif ($d == 0) {
                         $sql_where .= "imagetaken LIKE '" . sprintf('%04d-%02d', $y, $m) . "%' ";
                         $this->sphinx['filters']['takenmonth'] = sprintf('%04d%02d', $y, $m);
                     } else {
                         $sql_where .= "imagetaken LIKE '" . $dates[0] . "' ";
                         $this->sphinx['filters']['takenday'] = str_replace('-', '', $dates[0]);
                     }
                 } else {
                     //between
                     $sql_where .= "imagetaken BETWEEN '" . $dates[0] . "' AND '" . $dates[1] . "' ";
                     $days1 = $this->toDays($dates[1]);
                     $this->sphinx['filters']['takendays'] = array($days0, $days1);
                 }
             } else {
                 //from
                 $sql_where .= "imagetaken >= '" . $dates[0] . "' ";
                 $now = $this->toDays('NOW()');
                 $this->sphinx['filters']['takendays'] = array($days0, $now);
             }
         } else {
             //to
             $sql_where .= "imagetaken != '0000-00-00' AND imagetaken <= '" . $dates[1] . "' ";
             $days1 = $this->toDays($dates[1]);
             $this->sphinx['filters']['takendays'] = array(1, $days1);
             //1 is just so doesnt match 0
         }
     }
     if (!empty($this->limit9)) {
         if ($this->limit9 > 1) {
             if ($sql_where) {
                 $sql_where .= ' and ';
             }
             $sql_where .= "topic_id = {$this->limit9} ";
         }
         $sql_from .= " INNER JOIN gridimage_post gp ON(gi.gridimage_id=gp.gridimage_id) ";
         $this->sphinx['impossible']++;
     }
     if (!empty($this->limit10)) {
         if ($sql_where) {
             $sql_where .= ' and ';
         }
         $sql_where .= "route_id = {$this->limit10} and ftf=1 ";
         $sql_from .= " INNER JOIN route_item r ON(grid_reference=r.gridref) ";
         $this->sphinx['impossible']++;
     }
     if ($sql_where_start != $sql_where) {
         $this->issubsetlimited = true;
     }
     if (!empty($_GET['debug'])) {
         print "<pre>";
         print_r($this->sql);
         print_r($this->sphinx);
         print "</pre>";
     }
 }
Exemple #3
0
require_once 'geograph/gridsquare.class.php';
require_once 'geograph/imagelist.class.php';
require_once 'geograph/map.class.php';
require_once 'geograph/mapmosaic.class.php';
require_once 'geograph/rastermap.class.php';
include_messages('browse');
init_session();
$smarty = new GeographPage();
dieUnderHighLoad(4);
customGZipHandlerStart();
$square = new GridSquare();
if (isset($_GET['inner'])) {
    $template = 'browse_inner.tpl';
} else {
    $template = 'browse.tpl';
    $smarty->assign('prefixes', $square->getGridPrefixes());
    $smarty->assign('kmlist', $square->getKMList());
}
//we can be passed a gridreference as gridsquare/northings/eastings
//or just gridref. So lets initialise our grid square
$grid_given = false;
$grid_ok = false;
if (isset($_GET['nl'])) {
    $_SESSION['nl'] = intval($_GET['nl']);
} elseif (isset($_SESSION['nl'])) {
    $_GET['nl'] = intval($_SESSION['nl']);
}
if (isset($_GET['ht'])) {
    $_SESSION['ht'] = intval($_GET['ht']);
} elseif (isset($_SESSION['ht'])) {
    $_GET['ht'] = intval($_SESSION['ht']);
Exemple #4
0
function advanced_form(&$smarty, &$db, $is_cachable = false)
{
    global $CONF, $imagestatuses, $sortorders, $breakdowns, $USER;
    if ($_GET['form'] == 'first') {
        $template = 'search_first.tpl';
        $smarty->assign('forum_topic_numsquare', $CONF['forum_topic_numsquare']);
    } elseif ($_GET['form'] == 'cluster2') {
        $template = 'search_cluster2.tpl';
    } elseif ($_GET['form'] == 'check') {
        $template = 'search_check.tpl';
        if (!$_GET['i']) {
            $smarty->assign('user_name', "{$USER->user_id}:{$USER->realname}");
            $smarty->assign('glue', 'or');
            $smarty->assign('displayclass', 'spelling');
            $smarty->assign('seditpage_options', array('simple', 'small_redirect'));
        }
        $checks = array('gr' => ' A. 4-figure Subject Grid Reference', 'pg' => ' B. No Photographer Grid Reference', 'p6' => ' C. 4-figure Photographer Grid Reference', 'dir' => ' D. No View Direction', 'dat' => ' E. Incomplete Taken Date', 'com' => ' F. No Description', 'sho' => ' G. Description fewer than 10 words', 'dup' => ' H. Description same as Title');
        $smarty->assign_by_ref('checks', $checks);
        $editpage_options = array('simple' => ' Simplifed Edit Image Page', 'small_redirect' => ' Simplified Success Page');
        $smarty->assign_by_ref('editpage_options', $editpage_options);
        $glues = array('or' => 'Any', 'and' => 'All');
        $smarty->assign_by_ref('glues', $glues);
        global $displayclasses;
        unset($displayclasses['full']);
        unset($displayclasses['thumbs']);
        unset($displayclasses['slide']);
        unset($displayclasses['text']);
        $displayclasses['searchtext'] = "Text-based Sidebar (IE Only)";
    } elseif ($_GET['form'] == 'text') {
        $template = 'search_text.tpl';
        global $sortorders;
        unset($sortorders['imageclass']);
        unset($sortorders['realname']);
        unset($sortorders['title']);
        unset($sortorders['grid_reference']);
        $sortorders['random'] = "Random";
        unset($breakdowns['imageclass']);
        unset($breakdowns['realname']);
        unset($breakdowns['title']);
        unset($breakdowns['grid_reference']);
    } elseif (isset($_GET['Special'])) {
        $USER->mustHavePerm("admin");
        $template = 'search_admin_advanced.tpl';
    } else {
        $template = 'search_advanced.tpl';
    }
    if ($is_cachable && $smarty->caching) {
        $smarty->caching = 2;
        // lifetime is per cache
        $smarty->cache_lifetime = 3600 * 3;
        //3hr cache
    } else {
        $smarty->caching = 0;
        // NO caching
    }
    $smarty->assign('pagesizes', array(5, 10, 15, 20, 30, 50));
    if (!$is_cachable || !$smarty->is_cached($template, $is_cachable)) {
        function addkm($a)
        {
            return $a . "km";
        }
        if ($_GET['form'] == 'text' || $_GET['form'] == 'cluster2') {
            $d = array(1, 2, 3, 4, 5, 7, 8, 10, 20, 30);
            $d = array_combine($d, array_map('addkm', $d));
        } else {
            $d = array(1, 2, 3, 4, 5, 7, 8, 10, 20, 30, 40, 50, 71, 100, 2000);
            $d = array_combine($d, array_map('addkm', $d));
            $d += array(-5 => '5km square', -10 => '10km square', -50 => '50km square');
            $topicsraw = $db->GetAssoc("select gp.topic_id,concat(topic_title,' [',count(*),']') as title,forum_name from gridimage_post gp\n\t\t\t\t\tinner join geobb_topics using (topic_id)\n\t\t\t\t\tinner join geobb_forums using (forum_id)\n\t\t\t\t\tgroup by gp.topic_id \n\t\t\t\t\thaving count(*) > 4\n\t\t\t\t\torder by geobb_topics.forum_id desc,topic_title");
            $topics = array("1" => "Any Topic");
            $options = array();
            foreach ($topicsraw as $topic_id => $row) {
                if ($last != $row['forum_name'] && $last) {
                    $topics[$last] = $options;
                    $options = array();
                }
                $last = $row['forum_name'];
                $options[$topic_id] = $row['title'];
            }
            $topics[$last] = $options;
            $smarty->assign_by_ref('topiclist', $topics);
        }
        $smarty->assign_by_ref('distances', $d);
        $countylist = array();
        $recordSet =& $db->Execute("SELECT reference_index,county_id,name FROM loc_counties WHERE n > 0");
        while (!$recordSet->EOF) {
            $countylist[$CONF['references'][$recordSet->fields[0]]][$recordSet->fields[1]] = $recordSet->fields[2];
            $recordSet->MoveNext();
        }
        $recordSet->Close();
        $smarty->assign_by_ref('countylist', $countylist);
        require_once 'geograph/gridsquare.class.php';
        $square = new GridSquare();
        $smarty->assign('prefixes', $square->getGridPrefixes());
        $smarty->assign_by_ref('references', $CONF['references']);
        $smarty->assign_by_ref('sortorders', $sortorders);
        $smarty->assign_by_ref('imagestatuses', $imagestatuses);
        $smarty->assign_by_ref('breakdowns', $breakdowns);
    }
    $smarty->display($template, $is_cachable);
}