'YahooDemo',
'type' => 'all',
'results' => 10,
'format' => 'html',
'language' => 'en',
'site' => $site );
$desc = "Options for Yahoo! Search plugin";
// autoload off since we only need options if searching
$autoload = 'no';
add_option($ys_optionsName, $options, $desc, $autoload);
return;
}
function ys_loadOptions() {
global $ys_options, $ys_optionsName;
$ys_options = get_option($ys_optionsName);
if ( $ys_options === false) {
ys_firstRun();
$ys_options = get_option($ys_optionsName);
}
return;
}
// hi-tech xml parsing
function ys_getTag($xml, $tag) {
$preg = "/<".$tag.">(.*?)<\/".$tag.">/si";
preg_match_all($preg, $xml, $matches);
foreach ( $matches[1] as $value ) {
$tags[] = $value;
}
return $tags;
}
// more hi-tech xml parsing
function ys_getAttr($xml, $attr) {
$preg = '/' . $attr . '\W*=\W*(\d+)/i';
preg_match($preg, $xml, $matches);
$attrs = $matches[1];
return $attrs;
}
// format the yahoo query from what we know
function ys_buildQuery() {
global $ys_service, $ys_options;
// only load when needed
ys_loadOptions();
$q == False;
if ( isset($_REQUEST['query']) ) {
$q = $ys_service . '?query='
. rawurlencode($_REQUEST['query']);
if ( ! empty($_REQUEST['start']) ) {
$q .= "&start=" . $_REQUEST['start'];
}
foreach ($ys_options as $key => $value) {
$q .= "&$key=$value";
}
}
return $q;
}
// handle paging
function ys_nextPrev($total, $start, $last) {
global $ys_options;
$resultspp = $ys_options['results'];
$encquery = rawurlencode($_REQUEST['query']);
if($start > 1) {
echo '« Previous Page ';
}
echo " — ";
if( $last < $total ) {
echo 'Next Page » ';
}
return;
}
// get it and print it
function ys_showResults() {
$query = ys_buildQuery();
$xml = file_get_contents($query);
$arTitle = ys_getTag($xml, 'Title');
$arSummary = ys_getTag($xml, 'Summary');
$arUrl = ys_getTag($xml, 'Url');
$arClick = ys_getTag($xml, 'ClickUrl');
$arMod = ys_getTag($xml, 'ModificationDate');
$first = ys_getAttr($xml, 'firstResultPosition');
$returned = ys_getAttr($xml, 'totalResultsReturned');
$total = ys_getAttr($xml, 'totalResultsAvailable');
if ( $returned ) {
$last = $first + $returned - 1;
} else {
$last = 0;
}
echo "
Found $total results, showing $first to $last \n";
$dateFormat = get_settings('date_format');
$timeFormat = get_settings('time_format');
for ( $i = 0; $i < $returned; $i++) {
$title = $arTitle[$i];
$summary = $arSummary[$i];
$url = $arUrl[$i];
$clickurl = $arClick[$i];
$lmod = $arMod[$i];
$lastmodified = date("$dateFormat \\a\\t $timeFormat", $lmod);
echo <<
$summary
Modified $lastmodified
RESULT;
}
ys_nextPrev($total, $first, $last);
return;
}
function yahooSearchForm() {
$query = "";
$showresults = 0;
if ( isset($_REQUEST['query']) ) {
$query = rawurldecode($_REQUEST['query']);
$showresults = 1;
}
// The "Powered by..." text is required by the TOS, you did read the TOS
// didn't you? Everyone's doing it; you should to.
echo <<Powered by Yahoo! Search
SEARCH1;
if ( $showresults ) {
ys_showResults();
}
return;
}
function ys_adminPanel() {
if ( function_exists('add_options_page') ) {
add_options_page('Yahoo! Search', 'Yahoo! Search',
9, basename(__FILE__),
'ys_optionsSubpanel');
}
return;
}
// print the given array index as options for
function ys_showAdminOptions($name) {
global $ys_options, $ys_optionsVals;
$current = $ys_options[$name];
$values = $ys_optionsVals[$name]['array'];
foreach ( $values as $value ) {
if ( $value == $current ) {
echo ''
. $value . ' ';
} else {
echo ''
. $value . ' ';
}
}
return;
}
// keepin' it real
function ys_validateInput() {
global $ys_optionsVals, $ys_optionsKeys, $ys_errInfo;
$ar = array();
foreach ( $ys_optionsKeys as $key ) {
if ( empty($_POST[$key]) ) {
return false;
}
}
$valid = 0;
foreach ( $ys_optionsKeys as $key ) {
$optVals = $ys_optionsVals[$key];
$optType = key($optVals);
$optValue = $optVals[$optType];
$input = $_POST[$key];
switch ($optType) {
case 'length':
list($min, $max) = $optValue;
$len = strlen($input);
if ( $len <= $max && $len >= $min ) {
$ar[$key] = $input;
$valid++;
} else {
$ys_errInfo .= "Option $key has incorrect length. ";
}
break;
case 'array':
if ( in_array($input, $optValue) ) {
$ar[$key] = $input;
$valid++;
} else {
$ys_errInfo .= "Option $key has invalid value. ";
}
break;
}
}
if ( count($ys_optionsKeys) != $valid ) {
$ar = false;
}
return $ar;
}
function ys_optionsUpdate() {
global $ys_optionsName, $ys_options;
$newOptions = ys_validateInput();
if ( $newOptions === false ) {
$rc = false;
} else {
update_option($ys_optionsName, $newOptions);
ys_loadOptions();
$rc = true;
}
return $rc;
}
function ys_optionsSubpanel() {
global $ys_options, $ys_errInfo;
ys_loadOptions();
if (isset($_POST['info_update'])) {
echo '';
$rc = ys_optionsUpdate();
if ( $rc === false ) {
echo "Sorry, failed to update options: $ys_errInfo";
} else {
echo "Options updated successfully.\n";
}
echo "
\n";
}
echo <<
Yahoo! Search Options
PANEL5;
return;
}
add_action('admin_menu', 'ys_adminPanel');
?>