1<?php
2/**
3 * amazon-Plugin: Searches for books in amazon
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Thomas Baumann <tom@tiri.li>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'inc/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12// require_once('/usr/share/pear/PHP/Compat/Function/file_put_contents.php');
13require_once('class.xmlreader.php');
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_amazon extends DokuWiki_Syntax_Plugin {
20   function getInfo(){
21      return array(
22          'author' => 'Thomas Baumann',
23          'email'  => 'tom@tiri.li',
24          'date'   => '2005-10-31',
25          'name'   => 'Amazon Plugin',
26          'desc'   => 'Search and display Amazon link',
27          'url'    => 'https://www.dokuwiki.org/plugin:amazon_heavy'
28      );
29  }
30  /**
31  * What kind of syntax are we?
32  */
33  function getType(){
34    return 'protected';
35  }
36
37  /**
38  * Where to sort in?
39  */
40  function getSort(){
41    return 400;
42  }
43
44
45    /**
46     * Connect pattern to lexer
47     */
48    function connectTo($mode) {
49        $this->Lexer->addEntryPattern('<amazon(?=.*\x3C/amazon\x3E)',$mode,'plugin_amazon');
50    }
51
52    function postConnect() {
53      $this->Lexer->addExitPattern('</amazon>','plugin_amazon');
54    }
55
56    /**
57     * Handle the match
58     */
59
60
61    function handle($match, $state, $pos) {
62      if ( $state == DOKU_LEXER_UNMATCHED ) {
63        $matches = preg_split('/>/u',$match,2);
64        $matches[0] = trim($matches[0]);
65        if ( trim($matches[0]) == '' ) {
66          $matches[0] = NULL;
67        }
68        return array($matches[1],$matches[0]);
69      }
70      return TRUE;
71    }
72    /**
73     * Create output
74     */
75    function render($mode, &$renderer, $data) {
76      global $conf;
77      if($mode == 'xhtml' && strlen($data[0]) > 1) {
78        if ( !is_dir($conf['mediadir'] . '/amazon') ) mkdir($conf['mediadir'] . '/amazon', 0777-$conf['dmask']);
79        $hash = md5(serialize($data[0]));
80        $filename = $conf['mediadir'] . '/amazon/'.$hash.'.xml';
81
82        if ( is_readable($filename) ) {
83          $xmlreader = new xmlreader ($filename);
84          $xml=$xmlreader->parse();
85          $feed = $xml['ProductInfo']['#']['Details'];
86          $renderer->doc .= $this->plugin_render($feed);
87          return true;
88        }
89
90        if (!$this->getUrl($filename, $data[0])) {
91          $xmlreader = new xmlreader ($filename);
92          $xml=$xmlreader->parse();
93          $feed = $xml['ProductInfo']['#']['Details'];
94          $renderer->doc .= $this->plugin_render($feed);
95        } else {
96          $renderer->doc .= '**ERROR RENDERING**';
97        }
98        return true;
99      }
100      return false;
101    }
102
103    function getUrl($filename, &$data) {
104      // $data enthaelt alles zwischen den <amazon>..</amazon> tags
105      global $conf;
106
107      // amazon array erzeugen
108      $amazon=array();
109      $tmp_amazon=explode("\n",$data);
110      foreach ($tmp_amazon as $line) {
111        if ($line) {
112	  list($var,$value)=split("=",$line);
113          $amazon[$var]=$value;
114        }
115      }
116
117      if ($amazon['keyword'] == "") $amazon['keyword']="wiki";
118      if ($amazon['mode'] == "") $amazon['mode']="books-de";
119      if ($amazon['text'] == "") $amazon['text']="&nbsp;";
120
121      $xmlurl=$conf['amazon_xml']."KeywordSearch=".rawurlencode($amazon['keyword'])."&dev-t=".$conf['amazon_dev']."&f=xml&locale=de&mode=".$amazon['mode']."&page=1&t=".$conf['amazon_id']."&type=".$amazon['type'];
122
123      $data=array();
124      $data[0]=$xmlurl;
125      $data[1]=$amazon['text'];
126
127      $retval = exec('/usr/bin/lynx --dump "'.$xmlurl.'" > '.$filename);
128
129      return 0;
130    }
131
132    // output text string
133    function plugin_render($feed) {
134          $html="<table>";
135          for ($i=0; $i<sizeof ($feed); $i++)
136          {
137            $item  = $feed[$i];
138            $html .= "<tr><td><a href=\"".$item["@"]["url"]."\">";
139            $html .= "<img src=\"".$item["#"]["ImageUrlSmall"][0]["#"]."\" alt=\"".$item["#"]["ProductName"][0]["#"]."\"></img></a></td>";
140            $html .= "<td><h2>".$item["#"]["ProductName"][0]["#"]."</h2>(Autor: ".$item["#"]["Authors"][0]["#"]["Author"][0]["#"].")<br />";
141            $html .= "&nbsp;<a href=\"".$item["@"]["url"]."\">";
142            $html .= "<b>Art.# ".$item["#"]["Asin"][0]["#"];
143            if ($item["#"]["Isbn"][0]["#"]) $html .= " (ISBN: ".$item["#"]["Isbn"][0]["#"].")";
144            $html .= "</b></a><br />";
145            if ($item["#"]["BrowseList"][0]["#"]["BrowseNode"][0]["#"]["BrowseName"][0]["#"])
146              $html .= "&nbsp;".$item["#"]["BrowseList"][0]["#"]["BrowseNode"][0]["#"]["BrowseName"][0]["#"]."<br />";
147            if ($item["#"]["Media"][0]["#"])
148              $html .= "&nbsp;".$item["#"]["Media"][0]["#"]."<br />";
149            if ($item["#"]["Reviews"][0]["#"]["CustomerReview"][0]["#"]["Comment"][0]["#"])
150              $html .= "&nbsp;".$item["#"]["Reviews"][0]["#"]["CustomerReview"][0]["#"]["Comment"][0]["#"]."<br />";
151/*  $amazon[]= $item["#"]["Asin"][0]["#"];
152    $amazon[]= $item["#"]["ProductName"][0]["#"];
153    $amazon[]= $item["#"]["ImageUrlSmall"][0]["#"];
154    $amazon[]= $item["#"]["OurPrice"][0]["#"];
155    $amazon[]= $item["#"]["BrowseList"][0]["#"]["BrowseNode"][0]["#"]["BrowseName"][0]["#"];
156    $amazon[]= $item["#"]["Isbn"][0]["#"];
157    $amazon[]= $item["#"]["Media"][0]["#"];
158    $amazon[]= $item["#"]["Reviews"][0]["#"]["CustomerReview"][0]["#"]["Comment"][0]["#"];
159*/
160        $html .= "</td></tr>\n";
161          }
162    $html .= "</table>";
163      return $html;
164    // p_render($format, p_get_instructions($text),$info);
165    }
166}
167