<?php
/*
 * File: search.php
 * $Author: chuck $
 * $Revision: 1.26 $
 * $Date: 2000/11/11 18:21:45 $
 *
 * Turba: Copyright 2000 Charles J. Hagenbuch <chuck@horde.org>
 *
 * You should have received a copy of the GNU Public
 * License along with this package; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

function wapFormat($phoneNumber)
{
    $phoneNumber = preg_replace('|[ .()-]|', '', $phoneNumber);
    $phoneNumber = preg_replace('|ext(.)*|', '', $phoneNumber);
    
    return $phoneNumber;
}

require '../config/horde.php';
require './config/conf.php';
error_reporting($conf['debug_level']);

require_once '../lib/Registry.php';
require_once '../lib/Horde.php';
require_once '../lib/Browser.php';
require_once './lib/Turba.php';
require_once './lib/Source.php';
require './config/sources.php';

$browser = new Browser();
$registry = new Registry();

session_name($horde['session_name']);
@session_start();

/* Build the directory sources select widget. */
$source_options = '';
while (list($key, $entry) = each($cfgSources)) {
    $source_options .= "<option value=\"$key\">";
    $source_options .= $entry['title'] . "</option>\n";
}

if ($browser->wml) {
    $conf['paths']['templates'] .= '/wml';
}
$title = _("Directory Search");
require $conf['paths']['templates'] . '/common-header.inc';

/* find a source if there is one */
if (!isset($source) && isset($cfgSources) && is_array($cfgSources) && count($cfgSources) > 0) {
    reset($cfgSources);
    $source = key($cfgSources);
}

if(isset($source)) {
    $driver = new Turba_Source($source,
                               $cfgSources[$source]['title'],
                               $cfgSources[$source]['type'],
                               $cfgSources[$source]['params'],
                               $cfgSources[$source]['map'],
                               $cfgSources[$source]['public'],
                               $cfgSources[$source]['readonly']);

    $map = $driver->get_criterias();

    $glob_criterias = $driver->get_globals_criterias();

    if(count($glob_criterias) > 0) {
    	$newaddresses = $driver->search($glob_criterias, LASTNAME);
    }	
}


// has to be after for wap
require $conf['paths']['templates'] . '/search/adv_head.inc';

foreach($map as $criteria) {
	include($conf['paths']['templates'] . '/search/adv_row.inc');
}

require $conf['paths']['templates'] . '/search/results_head.inc';


if (empty($clear)) {
    
    if (isset($addresses) && is_array($addresses)) {
        for ($i = 0; $i < count($addresses); $i++) {
            $address = unserialize(base64_decode($addresses[$i]));
            if (is_array($address) && sizeof($address > 1)) {
                $displayed[$address['name'] . $address['email']] = 1;
                include $conf['paths']['templates'] . '/search/oldrow.inc';
            }
        }
    }
    
    if (isset($newaddresses) && is_object($newaddresses)) {
        include_once './lib/ListView.php';
        $display = new Turba_ListView($newaddresses, $conf['paths']['templates'] . '/search/row.inc');
        $display->display();
    }
}

require $conf['paths']['templates'] . '/search/foot.inc';
require $conf['paths']['templates'] . '/common-footer.inc';
if (!$browser->wml) $registry->shutdown();

?>