function amazon_fetch($asin, $mode)
 {
     global $serendipity;
     if (!class_exists('serendipity_event_amazonchooser')) {
         return;
     }
     $country = trim($this->get_config('server'));
     list($country_url, $mode_list) = Amazon_country_code($country);
     $mode_names = Amazon_return_mode_array();
     if (!is_dir($serendipity['serendipityPath'] . 'templates_c/amazonget/')) {
         mkdir($serendipity['serendipityPath'] . 'templates_c/amazonget/');
     }
     if (!in_array($mode, $mode_list)) {
         $mode = "All";
     }
     $data = array();
     serendipity_plugin_api::hook_event('serendipity_event_amazonchooser_devinfo', $data);
     $AWSAccessKey = $data['dtoken'];
     $secretKey = $data['secretKey'];
     $AssociateTag = $data['aaid'];
     if (@(include_once "Cache/Lite.php")) {
         $cache_obj = new Cache_Lite(array('cacheDir' => $serendipity['serendipityPath'] . 'templates_c/amazonget/', 'automaticSerialization' => true, 'lifeTime' => 43200));
         $results = $cache_obj->get('amazonlookup' . $asin);
     }
     if (!$results['return_date']) {
         $results = Amazon_ItemLookup($AWSAccessKey, $AssociateTag, $secretKey, $mode, $asin, $country_url);
         if ($results['return_date'] && class_exists('Cache_Lite') && is_object($cache_obj)) {
             $cache_obj->save($results, 'amazonlookup' . $asin);
         }
     }
     if ($results['count'] == 0 || $results['return_count'] == 0) {
         $results['items'] = "";
     }
     return $results;
 }
 function event_hook($event, &$bag, &$eventData, $addData = null)
 {
     global $serendipity;
     $hooks =& $bag->get('event_hooks');
     if (isset($hooks[$event])) {
         switch ($event) {
             case 'backend_entry_toolbar_extended':
                 if (isset($eventData['backend_entry_toolbar_extended:textarea'])) {
                     $txtarea = $serendipity['version'][0] < '2' ? $eventData['backend_entry_toolbar_extended:textarea'] : $eventData['backend_entry_toolbar_extended:nugget'];
                 } else {
                     $txtarea = 'extended';
                 }
                 if (!$serendipity['wysiwyg']) {
                     $this->generate_button($txtarea, false);
                     return true;
                 } else {
                     return false;
                 }
                 break;
             case 'backend_entry_toolbar_body':
                 if (isset($eventData['backend_entry_toolbar_body:textarea'])) {
                     $txtarea = $serendipity['version'][0] < '2' ? $eventData['backend_entry_toolbar_body:textarea'] : $eventData['backend_entry_toolbar_body:nugget'];
                 } else {
                     $txtarea = 'body';
                 }
                 if (!$serendipity['wysiwyg']) {
                     $this->generate_button($txtarea, false);
                     return true;
                 } else {
                     return false;
                 }
                 break;
             case 'frontend_display':
                 foreach ($this->markup_elements as $temp) {
                     if (isset($eventData[$temp['element']]) && !$eventData['properties']['ep_disable_markup_' . $this->instance] && !isset($serendipity['POST']['properties']['disable_markup_' . $this->instance])) {
                         $element = $temp['element'];
                         $eventData[$element] = preg_replace_callback('/(?<!\\\\)\\[amazon_chooser\\](.*?),(.*?)\\[\\/amazon_chooser\\]/', array(&$this, 'get_amazon_item'), $eventData[$element]);
                     }
                 }
                 return true;
                 break;
             case 'backend_wysiwyg':
                 $link = serendipity_rewriteURL('plugin/amazonch') . ($serendipity['rewrite'] != 'none' ? '?' : '&amp;') . 'txtarea=' . ($serendipity['version'][0] > '1' ? 'amazonchooser' . $eventData['item'] : $eventData['jsname']);
                 $open = $serendipity['version'][0] > '1' ? 'serendipity.openPopup' : 'window.open';
                 $eventData['buttons'][] = array('id' => 'amazonchooser' . ($serendipity['version'][0] > '1' ? $eventData['item'] : $eventData['jsname']), 'name' => PLUGIN_EVENT_AMAZONCHOOSER_MEDIA_BUTTON, 'javascript' => 'function() { ' . $open . '(\'' . $link . '\', \'AmazonImageSel\', \'width=800,height=600,toolbar=no,scrollbars=1,scrollbars,resize=1,resizable=1\') }', 'img_url' => $serendipity['serendipityHTTPPath'] . ($serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '') . 'plugin/plugin_amazonchooser.gif', 'img_path' => 'serendipity_event_amazonchooser/serendipity_event_amazonchooser.gif', 'toolbar' => 'other');
                 //'img_path' deprecated, used by ckeditor plugin <= 4.1.0
                 return true;
                 break;
             case 'css_backend':
             case 'css':
                 $out = serendipity_getTemplateFile('serendipity_event_amazonchooser.css', 'serendipityPath');
                 if ($out && $out != 'serendipity_event_amazonchooser.css') {
                     $eventData .= file_get_contents($out);
                 } else {
                     $eventData .= file_get_contents(dirname(__FILE__) . '/serendipity_event_amazonchooser.css');
                 }
                 return true;
                 break;
             case 'serendipity_event_amazonchooser_button':
                 $eventData['button_out'] = $this->generate_button($eventData['textbox'], true);
                 return true;
                 break;
             case 'serendipity_event_amazonchooser_devinfo':
                 $eventData['dtoken'] = trim($this->get_config('dtoken'));
                 $eventData['secretKey'] = trim($this->get_config('secretKey'));
                 $eventData['aaid'] = trim($this->get_config('aaid'));
                 return true;
                 break;
             case 'external_plugin':
                 $uri_parts = explode('?', str_replace('&amp;', '&', $eventData));
                 $parts = explode('&', $uri_parts[0]);
                 $uri_part = $parts[0];
                 $parts = array_pop($parts);
                 if (count($parts) > 1) {
                     foreach ($parts as $key => $value) {
                         $val = explode('=', $value);
                         $_REQUEST[$val[0]] = $val[1];
                     }
                 } else {
                     $val = explode('=', $parts[0]);
                     $_REQUEST[$val[0]] = $val[1];
                 }
                 if (!isset($_REQUEST['txtarea'])) {
                     $parts = explode('&', $uri_parts[1]);
                     if (count($parts) > 1) {
                         foreach ($parts as $key => $value) {
                             $val = explode('=', $value);
                             $_REQUEST[$val[0]] = $val[1];
                         }
                     } else {
                         $val = explode('=', $parts[0]);
                         $_REQUEST[$val[0]] = $val[1];
                     }
                 }
                 switch ($uri_part) {
                     case 'amazonch-js':
                         header('Content-Type: text/javascript');
                         echo file_get_contents(dirname(__FILE__) . '/serendipity_event_amazonchooser.js');
                         break;
                     case 'plugin_amazonchooser.gif':
                         header('Content-Type: image/gif');
                         echo file_get_contents(dirname(__FILE__) . '/serendipity_event_amazonchooser.gif');
                         break;
                     case 'amazonch':
                         session_start();
                         include 'serendipity_config.inc.php';
                         if (IN_serendipity !== true) {
                             die("Don't hack!");
                         }
                         if (!is_object($serendipity['smarty'])) {
                             serendipity_smarty_init();
                         }
                         if ($_SESSION['serendipityAuthedUser'] !== true) {
                             die(HAVE_TO_BE_LOGGED_ON);
                         }
                         $country = trim($this->get_config('server'));
                         list($country_url, $mode) = Amazon_country_code($country);
                         $mode_names = Amazon_return_mode_array();
                         header('Content-Type: text/html; charset=' . LANG_CHARSET);
                         $tfile = serendipity_getTemplateFile('plugin_amazon_search.tpl', 'serendipityPath');
                         if (!$tfile || $tfile == 'plugin_amazon_search.tpl') {
                             $tfile = dirname(__FILE__) . '/plugin_amazon_search.tpl';
                         }
                         $tdisplayfile = serendipity_getTemplateFile('plugin_amazon_display.tpl', 'serendipityPath');
                         if (!$tdisplayfile || $tdisplayfile == 'plugin_amazon_display.tpl') {
                             $tdisplayfile = dirname(__FILE__) . '/plugin_amazon_display.tpl';
                         }
                         $serendipity['smarty']->assign(array('plugin_amazonchooser_css' => serendipity_rewriteURL('serendipity_admin.css'), 'plugin_amazonchooser_js' => serendipity_rewriteURL('plugin/amazonch-js')));
                         switch ($_REQUEST['step']) {
                             case '1':
                                 $page = 1;
                                 if (isset($_REQUEST['page'])) {
                                     $page = (int) $_REQUEST['page'];
                                 }
                                 if (isset($_REQUEST['simple']) && $_REQUEST['simple']) {
                                     $simple = "&amp;simple=1";
                                 } else {
                                     $simple = "";
                                 }
                                 $request_mode = trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['mode'])) : htmlspecialchars(rawurlencode($_REQUEST['mode']), ENT_COMPAT, LANG_CHARSET));
                                 if (in_array($_REQUEST['mode'], $mode)) {
                                     $results = $this->Amazon_Call("search", $request_mode, trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['keyword'])) : htmlspecialchars(rawurlencode($_REQUEST['keyword']), ENT_COMPAT, LANG_CHARSET)), $country_url, $page);
                                 } else {
                                     $results['return_count'] = 0;
                                     $results['count'] = 0;
                                     $results['error_message'] = PLUGIN_EVENT_AMAZONCHOOSER_INVALIDINDEX . ": " . trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['mode'])) : htmlspecialchars(rawurlencode($_REQUEST['mode']), ENT_COMPAT, LANG_CHARSET));
                                 }
                                 if ($page > 1) {
                                     $previous_page = $page - 1;
                                     $serendipity['smarty']->assign(array('plugin_amazonchooser_previouspage' => $previous_page));
                                 }
                                 if ($page < 400 && $results['return_count'] > 10) {
                                     $next_page = $page + 1;
                                     $serendipity['smarty']->assign(array('plugin_amazonchooser_nextpage' => $next_page));
                                 }
                                 $serendipity['smarty']->assign(array('plugin_amazonchooser_page' => "Search", 'plugin_amazonchooser_displaytemplate' => $tdisplayfile, 'plugin_amazonchooser_currentpage' => $page, 'plugin_amazonchooser_totalpages' => $results['totalpages'], 'plugin_amazonchooser_item_count' => $results['count'], 'plugin_amazonchooser_return_count' => $results['return_count'], 'plugin_amazonchooser_error_message' => $results['error_message'], 'plugin_amazonchooser_error_result' => $results['error_result'], 'plugin_amazonchooser_cache_time' => $results['return_date'], 'plugin_amazonchooser_items' => $results['items'], 'plugin_amazonchooser_search_url' => serendipity_rewriteURL('plugin/amazonch') . ($serendipity['rewrite'] != 'none' ? '?' : '&amp;') . 'txtarea=' . (function_exists('serendipity_specialchars') ? serendipity_specialchars($_REQUEST['txtarea']) : htmlspecialchars($_REQUEST['txtarea'], ENT_COMPAT, LANG_CHARSET)) . $simple . '&amp;keyword=' . trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['keyword'])) : htmlspecialchars(rawurlencode($_REQUEST['keyword']), ENT_COMPAT, LANG_CHARSET)) . '&amp;mode=' . $request_mode, 'plugin_amazonchooser_this_url' => serendipity_rewriteURL('plugin/amazonch') . ($serendipity['rewrite'] != 'none' ? '?' : '&amp;') . '&amp;mode=' . trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['mode'])) : htmlspecialchars(rawurlencode($_REQUEST['mode']), ENT_COMPAT, LANG_CHARSET)) . '&amp;txtarea=' . (function_exists('serendipity_specialchars') ? serendipity_specialchars($_REQUEST['txtarea']) : htmlspecialchars($_REQUEST['txtarea'], ENT_COMPAT, LANG_CHARSET)) . $simple . '&amp;step=1&amp;keyword=' . trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['keyword'])) : htmlspecialchars(rawurlencode($_REQUEST['keyword']), ENT_COMPAT, LANG_CHARSET)) . '&amp;page=', 'plugin_amazonchooser_select_url' => serendipity_rewriteURL('plugin/amazonch') . ($serendipity['rewrite'] != 'none' ? '?' : '&amp;') . '&amp;mode=' . trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['mode'])) : htmlspecialchars(rawurlencode($_REQUEST['mode']), ENT_COMPAT, LANG_CHARSET)) . $simple . '&amp;txtarea=' . (function_exists('serendipity_specialchars') ? serendipity_specialchars($_REQUEST['txtarea']) : htmlspecialchars($_REQUEST['txtarea'], ENT_COMPAT, LANG_CHARSET)) . '&amp;step=2&amp;asin='));
                                 break;
                             case '2':
                                 if (isset($_REQUEST['asin'])) {
                                     $result = $this->Amazon_Call("lookup", trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['mode'])) : htmlspecialchars(rawurlencode($_REQUEST['mode']), ENT_COMPAT, LANG_CHARSET)), trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['asin'])) : htmlspecialchars(rawurlencode($_REQUEST['asin']), ENT_COMPAT, LANG_CHARSET)), $country_url, $page);
                                 } else {
                                     $result['count'] = 0;
                                     $result['error_message'] = PLUGIN_EVENT_AMAZONCHOOSER_NOASIN;
                                 }
                                 if (isset($_REQUEST['simple']) && $_REQUEST['simple']) {
                                     $simple = 1;
                                 } else {
                                     $simple = "";
                                 }
                                 $serendipity['smarty']->assign(array('plugin_amazonchooser_page' => "Lookup", 'plugin_amazonchooser_displaytemplate' => $tdisplayfile, 'plugin_amazonchooser_txtarea' => $_REQUEST['txtarea'], 'plugin_amazonchooser_item_count' => $result['count'], 'plugin_amazonchooser_return_count' => $result['return_count'], 'plugin_amazonchooser_searchmode' => trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['mode'])) : htmlspecialchars(rawurlencode($_REQUEST['mode']), ENT_COMPAT, LANG_CHARSET)), 'plugin_amazonchooser_simple' => $simple, 'plugin_amazonchooser_error_message' => $result['error_message'], 'plugin_amazonchooser_cache_time' => $result['return_date'], 'plugin_amazonchooser_error_result' => $result['error_result'], 'thingy' => $result['items'][0]));
                                 break;
                             default:
                                 $defaultmode = rawurlencode($_REQUEST['mode']);
                                 $link = serendipity_rewriteURL('plugin/amazonch') . ($serendipity['rewrite'] != 'none' ? '?' : '&amp;');
                                 foreach ($mode as $type) {
                                     $mode_out[$type] = $mode_names[$type];
                                 }
                                 if (isset($_REQUEST['simple']) && $_REQUEST['simple']) {
                                     $simple = "1";
                                 } else {
                                     $simple = "0";
                                 }
                                 asort($mode_out);
                                 $serendipity['smarty']->assign(array('plugin_amazonchooser_page' => "default", 'plugin_amazonchooser_keyword' => rawurldecode($_REQUEST['keyword']), 'plugin_amazonchooser_link' => $link, 'plugin_amazonchooser_txtarea' => trim(function_exists('serendipity_specialchars') ? serendipity_specialchars(rawurlencode($_REQUEST['txtarea'])) : htmlspecialchars(rawurlencode($_REQUEST['txtarea']), ENT_COMPAT, LANG_CHARSET)), 'plugin_amazonchooser_simple' => $simple, 'plugin_amazonchooser_mode' => $mode_out, 'plugin_amazonchooser_defaultmode' => $defaultmode));
                                 break;
                         }
                         // use native API here - extends s9y version >= 1.3'
                         $content = $this->parseTemplate($tfile);
                         echo $content;
                 }
             default:
                 return false;
                 break;
         }
     } else {
         return false;
     }
 }