Please report this to the site administrator.';
}
//$display .= 'Download speed =' . curl_getInfo( $ch, CURLINFO_SPEED_DOWNLOAD ) . ' ';
curl_close( $ch );
// IF we are using ISO-8859-1
// THEN convert from UTF-8 so that the pound sign and German chars display properly
if ( $LANG_CHARSET == 'iso-8859-1' )
{
$results = utf8_decode( $results );
}
$display .= $results;
$display .= '
';
return $display;
}
function printAdminPage()
{
if (!function_exists( 'curl_init' ) )
{
echo '
Your HTTP server does not support curl. The Amazon Search plugin will not work.
';
return;
}
global $wpdb;
$display = '';
$_AMZ_PREFS = $this->getAdminOptions();
// Check for and save updated prefs
if ( isset( $_POST['Save'] ) )
{
check_admin_referer('amz_search-admin_options');
$new_prefs = $_POST['new_prefs'];
// workaround for HTML being braindead - it only passes checkboxes which are 'on'
$_AMZ_PREFS['use_text_links'] = false;
$_AMZ_PREFS['use_ip2nation'] = false;
foreach ( $new_prefs as $pref => $value )
{
if (isset($new_prefs[$pref]))
{
$_AMZ_PREFS[$pref] = $new_prefs[$pref];
}
}
update_option($this->_adminOptionsName, $_AMZ_PREFS);
$results = $wpdb->get_results('SELECT * FROM ' . $this->_db_table, ARRAY_A);
foreach ($results as $server_info)
{
$associate_id = trim( $_POST['usertag'][$server_info['id']] );
$sql = 'UPDATE ' . $this->_db_table . " SET user_tag='$associate_id' WHERE id='{$server_info['id']}'";
$wpdb->query($sql);
}
$display .= '
Settings Updated.
';
}
$display .= '';
echo $display;
}
function _buildServerOptionList( $name, $selected )
{
$options = '';
return $options;
}
function _buildCategoryOptionList( $name, $selected )
{
$category = '';
return $category;
}
// Make sure we have a valid server
function _validServer( $a_server )
{
switch ( $a_server )
{
case 'com':
case 'ca':
case 'uk':
case 'fr':
case 'de':
case 'jp':
$clean_server = $a_server;
break;
case '':
$clean_server = 'com';
break;
default:
die( 'Invalid search data' );
}
return( $clean_server );
}
// Make sure we have a valid category
function _validCategory( $category )
{
switch ( $category )
{
case 'Blended':
case 'Books':
case 'DVD':
case 'Music':
case 'Software':
$clean_category = $category;
break;
case '':
$clean_category = 'Blended';
break;
default:
die( 'Invalid search data' );
}
return( $clean_category );
}
// Clean up and return a keyword
function _validKeyword( $keyword )
{
$p = strip_tags( $keyword );
$p = trim( $p );
$p = attribute_escape( $p );
return( $p );
}
// my own nonce functions because I don't want to echo the stuff immediately...
function _nonce_field($action = -1, $name = "_wpnonce", $referer = true)
{
$name = attribute_escape($name);
$ret = '';
if ( $referer )
$ret .= $this->_referer_field();
return $ret;
}
function _referer_field()
{
$ref = attribute_escape($_SERVER['REQUEST_URI']);
$ret = '';
if ( wp_get_original_referer() )
{
$original_ref = attribute_escape(stripslashes(wp_get_original_referer()));
$ret .= '';
}
return $ret;
}
// Get the amazon server for a given IP
function getAmazonServerFromIP( $default_server )
{
global $wpdb;
if ($wpdb->get_var("SHOW TABLES LIKE 'ip2nation'") != 'ip2nation')
return $default_server;
$sql = 'SELECT country FROM ip2nation
WHERE
ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
ORDER BY
ip DESC
LIMIT 0,1';
$row = $wpdb->get_row( $sql, ARRAY_A );
// Choose the best amazon location based on geography and language...
// This could be done more efficiently in a database, but for now this is easier until it's more complete.
// I could use some help here: some of my categorization may be incorrect or incomplete - please let me
// know of any glaring problems.
switch ($row['country'])
{
case 'us': // United States
case 'mx': // Mexico
case 'an': // Netherlands Antilles
case 'nz': // New Zealand
case 'kn': // Saint Kitts and Nevis
case 'lc': // Saint Lucia
case 'vg': // Virgin Islands (British)
case 'vi': // Virgin Islands (U.S.)
return 'com';
case 'uk': // United Kingdom
case 'gb': // Great Britain (UK)
case 'gi': // Gibraltar
case 'ie': // Ireland
return 'uk';
// Not necessarily English-speaking, but maybe order from UK?
case 'eu': // Europe
case 'dk': // Denmark
case 'fi': // Finland
case 'gr': // Greece
case 'is': // Iceland
case 'nl': // Netherlands
case 'no': // Norway
case 'za': // South Africa
case 'se': // Sweden
return 'uk';
case 'ca': // Canada
case 'pm': // St. Pierre and Miquelon
return 'ca';
case 'de': // Germany
case 'at': // Austria
case 'li': // Liechtenstein
case 'na': // Namibia
case 'ch': // Switzerland
return 'de';
// Not necessarily German-speaking, but maybe order from Germany?
case 'cz': // Czech Republic
case 'pl': // Poland
case 'sk': // Slovak Republic
return 'de';
case 'fr': // France
case 'be': // Belgium
case 'bj': // Benin
case 'bf': // Burkina Faso
case 'bi': // Burundi
case 'cm': // Cameroon
case 'cf': // Central African Republic
case 'td': // Chad
case 'km': // Comoros
case 'cg': // Congo
case 'dj': // Djibouti
case 'ga': // Gabon
case 'gp': // Guadeloupe
case 'gf': // French Guiana
case 'pf': // French Polynesia
case 'tf': // French Southern Territories
case 'ht': // Haiti
case 'ci': // Ivory Coast
case 'lu': // Luxembourg
case 'mg': // Madagascar
case 'ml': // Mali
case 'mq': // Martinique
case 'yt': // Mayotte
case 'mc': // Monaco
case 'nc': // New Caledonia
case 'ne': // Niger
case 're': // Reunion
case 'sn': // Senegal
case 'sc': // Seychelles
case 'tg': // Togo
case 'vu': // Vanuatu
case 'wf': // Wallis and Futuna Islands
return 'fr';
case 'jp':
return 'jp';
default:
break;
}
return $default_server;
}
// Convert a string of the form "a=b c=d" into an associative array
// From here:
function parseNameValues($text)
{
$values = array();
if (preg_match_all('/([^=\s]+)=("(?P[^"]+)"|' . '\'(?P[^\']+)\'|(?P.+?)\b)/', $text, $matches, PREG_SET_ORDER))
{
foreach ($matches as $match)
$values[strtolower(trim($match[1]))] = trim(@$match['value1'] . @$match['value2'] . @$match['value3']);
}
return $values;
}
// Links are of the form: [amazon isbn=0123456789]Title[/amazon]
// You may also use asin in place of isbn.
// These will be converted to amazon links pointing at your default server using your associate ID for that server.
function createLinks( $content )
{
if ( preg_match_all($this->_linkRegEx, $content, $matches) )
{
$_AMZ_PREFS = $this->getAdminOptions();
$server_code = $_AMZ_PREFS['default_server'];
if ( $_AMZ_PREFS['use_ip2nation'] )
$server_code = $this->getAmazonServerFromIP( $server_code );
$server_info = $this->getServerInfo( $server_code );
$amazon_server = $server_info['server'];
$ass_id = $server_info['user_tag'];
for ($i=0; $i < count($matches[0]); $i++)
{
$match = $matches[0][$i]; // the whole text that was matched
$args = strip_tags( trim($matches[1][$i]) ); // of the form "a=b c=d"
$title = $matches[2][$i]; // the title of the link
if (empty($args) || empty($title))
continue;
$nameValues = $this->parseNameValues( $args );
$asin = $nameValues['asin'];
$isbn = $nameValues['isbn'];
// check if we should use ISBN
if ( empty($asin) )
$asin = $isbn;
if ( empty($asin) )
continue;
$newlink = '' . $title . '';
$content = str_replace($match, $newlink, $content);
}
}
return $content;
}
}
}
if (class_exists('AMZ_Search'))
{
$gAMZ_search = new AMZ_Search();
}
// Initialize the admin panel
function AMZ_Search_adminPage()
{
global $gAMZ_search;
if (!isset($gAMZ_search))
return;
if (function_exists('add_options_page'))
add_options_page('Amazon Search', 'Amazon Search', 9, basename(__FILE__), array(&$gAMZ_search, 'printAdminPage'));
}
// Initialize the widget
function AMZ_Search_initWidgets()
{
function widget_amz_search($args)
{
global $gAMZ_search;
extract($args);
echo $before_widget;
$gAMZ_search->printSearchBox();
echo $after_widget;
}
register_sidebar_widget('Amazon Search', 'widget_amz_search');
}
function AMZ_Search_results()
{
global $gAMZ_search;
$gAMZ_search->storeCleanRequestData();
function AMZ_search()
{
global $gAMZ_search;
return $gAMZ_search->getSearchResults();
}
include('wp-content/plugins/amazon-search/templates/search.php');
exit;
}
// Handle all our main registrations and actions
if (isset($gAMZ_search))
{
$_AMZ_PREFS = $gAMZ_search->getAdminOptions();
register_activation_hook( __FILE__, array(&$gAMZ_search, 'db_install') );
// add handling of [amazon][/amazon] links
if ( $_AMZ_PREFS['use_text_links'] )
add_filter('the_content', array(&$gAMZ_search, 'createLinks'), 1);
add_action('wp_head', array(&$gAMZ_search, 'addHeaderCode'), 1);
add_action('admin_menu', 'AMZ_Search_adminPage');
add_action('activate_amz-search/amz-search.php', array(&$gAMZ_search, 'init'));
if (function_exists( 'curl_init' ) )
{
if ( isset($_REQUEST['amz_search']) )
add_action('template_redirect', 'AMZ_Search_results');
add_action('widgets_init','AMZ_Search_initWidgets');
}
}
?>