*/ // must be run within Dokuwiki if (!defined('DOKU_INC')) { die(); } class syntax_plugin_amazonlight extends DokuWiki_Syntax_Plugin { /** @var array what regions to use for the different countries */ const REGIONS = [ 'us' => 'ws-na', 'ca' => 'ws-na', 'de' => 'ws-eu', 'gb' => 'ws-eu', 'fr' => 'ws-eu', 'jp' => 'ws-fe', ]; /** @inheritDoc */ public function getType() { return 'substition'; } /** @inheritDoc */ public function getPType() { return 'block'; } /** @inheritDoc */ public function getSort() { return 160; } /** * Connect lookup pattern to lexer. * * @param string $mode Parser mode */ public function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{amazon>[\w:\\- =]+\}\}', $mode, 'plugin_amazonlight'); } /** @inheritDoc */ public function handle($match, $state, $pos, Doku_Handler $handler) { $match = substr($match, 9, -2); list($ctry, $asin) = explode(':', $match, 2); // no country given? if (empty($asin)) { $asin = $ctry; $ctry = 'us'; } // default parameters... $params = array( 'imgw' => $this->getConf('imgw'), 'imgh' => $this->getConf('imgh'), 'price' => $this->getConf('showprice'), ); // ...can be overridden list($asin, $more) = explode(' ', $asin, 2); $params['asin'] = $asin; if (preg_match('/(\d+)x(\d+)/i', $more, $match)) { $params['imgw'] = $match[1]; $params['imgh'] = $match[2]; } if (preg_match('/noprice/i', $more, $match)) { $params['price'] = false; } elseif (preg_match('/(show)?price/i', $more, $match)) { $params['price'] = true; } // correct country given? if ($ctry === 'uk') $ctry = 'gb'; if (!preg_match('/^(us|gb|jp|de|fr|ca)$/', $ctry)) { $ctry = 'us'; } $params['country'] = $ctry; return $params; } /** @inheritDoc */ public function render($mode, Doku_Renderer $renderer, $data) { if ($mode !== 'xhtml') { return false; } $html = $this->output($data); if (!$html) { if ($data['country'] == 'de') { $renderer->interwikilink('Amazon', 'Amazon.de', 'amazon.de', $data['asin']); } else { $renderer->interwikilink('Amazon', 'Amazon', 'amazon', $data['asin']); } } $renderer->doc .= $html; return true; } /** * @param array $param * @return string */ protected function output($param) { global $conf; try { $data = $this->fetchData($param['asin'], $param['country']); } catch (\Exception $e) { msg(hsc($e->getMessage()), -1); return false; } $img = ml($data['img'], array('w' => $param['imgw'], 'h' => $param['imgh'])); ob_start(); echo '