<?php

function openid_search($queryarray, $andor, $limit, $offset, $userid){
	global $xoopsDB;
	$sql = 'SELECT `openid`, `localid`, unix_timestamp(`created`) AS "created"  FROM ' . $xoopsDB->prefix('openid_localid') . ' WHERE 1=1';
	if ( $userid != 0 ) {
		$user = new XoopsUser();
		$uname = $user->getUnameFromId($userid);
		$user = NULL;
		unset($user);
		if ($uname) {
			$sql .= ' AND `localid`="' . addslashes($uname) . '"';
		}
	}
	// because count() returns 1 even if a supplied variable
	// is not an array, we must check if $querryarray is really an array
	if ( is_array($queryarray) && $count = count($queryarray) ) {
		$sql .= ' AND ((`openid` LIKE "%' . $queryarray[0] .'%" )';
		for($i=1;$i<$count;$i++){
			$sql .= ' ' . $andor;
			$sql .= ' (`openid` LIKE "%' . $queryarray[$i] .'%" )';
		}
		$sql .= ')';
	}
	$sql .= ' ORDER BY `created` DESC';
	$result = $xoopsDB->query($sql,$limit,$offset);
	$ret = array();
	$i = 0;
	$uids = array();
	
	$myts =& MyTextSanitizer::getInstance();
	
	$user_handler =& xoops_gethandler('user');
	
	while($myrow = $xoopsDB->fetchArray($result)){
 		if (! isset($uids[$myrow['localid']])) {
	 		$criteria = new CriteriaCompo(new Criteria('uname', $myrow['localid'] ));
			$users =& $user_handler->getObjects($criteria, false);
			$user = $users[0] ;
			unset( $users ) ;
			$uids[$myrow['localid']] = $user->getVar('uid');
		}

		$ret[$i]['image'] = "images/openid_small_logo_white.gif";
		$ret[$i]['link'] = '?redirect=' . rawurlencode($myrow['openid']);
		$ret[$i]['title'] = $myrow['openid'];
		$ret[$i]['time'] = $myrow['created'];
		$ret[$i]['uid'] = $uids[$myrow['localid']];
		$i++;
	}
	return $ret;
}
?>