2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: amazon.inc.php,v 1.15 2005/04/09 03:18:06 henoheno Exp $
4 // Id: amazon.inc.php,v 1.1 2003/07/24 13:00:00 ´×¼Ë
6 // Amazon plugin: Book-review maker via amazon.com/amazon.jp
9 // 2004-2005 PukiWiki Developers Team
10 // 2003 ´×¼Ë <raku@rakunet.org> (Original author)
15 // * 2004/04/03 PukiWiki Developer Team (arino <arino@users.sourceforge.jp>)
16 // - replace plugin_amazon_get_page().
17 // - PLUGIN_AMAZON_XML 'xml.amazon.com' -> 'xml.amazon.co.jp'
18 // * 0.6 URL ¤¬Â¸ºß¤·¤Ê¤¤¾ì¹ç¡¢No image ¤òɽ¼¨¡¢²èÁüÇÛÃ֤ʤɽ¤Àµ¡£
19 // ¥¤¥ó¥é¥¤¥ó¥×¥é¥°¥¤¥ó¤Î¸Æ¤Ó½Ð¤·Êý¤ò½¤Àµ¡£
20 // ASIN ÈÖ¹æÉôʬ¤ò¥Á¥§¥Ã¥¯¤¹¤ë¡£
21 // ²èÁü¡¢¥¿¥¤¥È¥ë¤Î¥¥ã¥Ã¥·¥å¤Ë¤è¤ë®ÅÙ¤ÎÂçÉý¥¢¥Ã¥×¡£
22 // * 0.7 ¥Ö¥Ã¥¯¥ì¥Ó¥å¡¼À¸À®¤Î¥Ç¥Ð¥Ã¥°¡¢Ç§¾ÚÌäÂê¤Î°ì±þ¤Î¥¯¥ê¥¢¡£
23 // * 0.8 amazon Á´¾¦ÉʤβèÁü¤òɽ¼¨¡£
24 // ¥¢¥½¥·¥¨¥¤¥È ID ¤ËÂбþ¡£
25 // * 0.9 RedHat9+php4.3.2+apache2.0.46 ¤Ç²èÁü¤¬ÅÓÃæ¤Þ¤Ç¤·¤«Æɤ߹þ¤Þ¤ì¤Ê¤¤ÌäÂê¤ËÂн衣
26 // ÆüËܸì¥Ú¡¼¥¸¤Î²¼¤Ë¥Ö¥Ã¥¯¥ì¥Ó¥å¡¼¤òºî¤í¤¦¤È¤¹¤ë¤Èʸ»ú²½¤±¤·¤Æºî¤ì¤Ê¤¤ÌäÂê¤Î²ò·è¡£
27 // ½ñÀҤǤʤ¯ CD ¤Ê¤É¡¢ASIN Éôʬ¤¬Ä¹¤¯¤Æ¤â¥¿¥¤¥È¥ë¤ò¤¦¤Þ¤¯½¦¤¦¤è¤¦¤Ë¤¹¤ë¡£
28 // ¼Ì±Æ¤Î¤ß¼è¤ê¹þ¤à¤Î¤Ç¤Ê¤±¤ì¤Ð¡¢B000002G6J.01 ¤È½ñ¤«¤º B000002G6J ¤È½ñ¤¤¤Æ¤â¼Ì±Æ¤¬½Ð¤ë¤è¤¦¤Ë¤¹¤ë¡£
29 // ASIN ¤ËÂбþ¤¹¤ë¥¥ã¥Ã¥·¥å²èÁü/¥¥ã¥Ã¥·¥å¥¿¥¤¥È¥ë¤ò¤½¤ì¤¾¤ìºï½ü¤¹¤ëµ¡Ç½Äɲá£
30 // proxy Âбþ(»î¸³Åª)¡£
31 // proxy ¼ÂÁõ¤Î²áÄø¤Ç°ìÈ̥桼¥¶¤Î¤¿¤á¤Î AID ¤Ï¤Ê¤¯¤È¤â¼«Æ°À¸À®¤µ¤ì¤ë¤³¤È¤¬¤ï¤«¤ê¡¢ºï½ü¤·¤¿¡£
32 // * 1.0 ¥Ö¥Ã¥¯¥ì¥Ó¥å¡¼¤Ç¤Ê¤¯¡¢¥ì¥Ó¥å¡¼¤È¤¹¤ë¡£
33 // ²èÁü¤Î¥¥ã¥Ã¥·¥å¤òºï½ü¤¹¤ë´ü¸Â¤òÀߤ±¤ë¡£
34 // ¥¿¥¤¥È¥ë¡¢¼Ì±Æ¤ò Web Services ¤Î XML ¥¢¥¯¥»¥¹¤ÎÊýË¡¤Ë¤è¤Ã¤Æ get ¤¹¤ë¤³¤È¤Ç»þ´Ö¤òû½Ì¤¹¤ë¡£
35 // ¥ì¥Ó¥å¡¼¥Ú¡¼¥¸À¸À®¤Î¥¿¥¤¥ß¥ó¥°¤Ë¤Ä¤¤¤ÆÃí¤òÆþ¤ì¤ë¡£
36 // * 1.1 ÊÔ½¸À©¸Â¤ò¤«¤±¤Æ¤¤¤ë¾ì¹ç¡¢Éô³°¼Ô¤¬¥ì¥Ó¥å¡¼¤òºî¤í¤¦¤È¤·¤Æ¡¢¥Ú¡¼¥¸¤Ï¤Ç¤¤Ê¤¤¤¬ ASIN4774110655.tit ¤Ê¤É¤Î¥¥ã¥Ã¥·¥å¤¬¤Ç¤¤ë¤Î¤ò²ò·è¡£
37 // ²èÁü¤ÎºÇ¸å¤¬ 01 ¤Î¾ì¹ç¡¢image ¤òºï½ü¤¹¤ë¤È noimage.jpg ¤È¤Ê¤Ã¤Æ¤·¤Þ¤¦¥Ð¥°¤ò½¤Àµ¡£
38 // 1.0 ¤ÇƳÆþ¤·¤¿ XML ¥¢¥¯¥»¥¹¤Ï¹â®¤À¤¬¡¢ÊÖ¤¹²èÁü¾ðÊ󤬥¦¥½¤Ê¤Î¤Ç¡¢09 ¤¬¤À¤á¤Ê¤é 01 ¤ò¥È¥é¥¤¤¹¤ë¡¢¤Ç»ÃÄêŪ¤Ë²ò·è¡£
41 // * Ãøºî¸¢¤¬´ØÏ¢¤¹¤ë°Ù¡¢www.amazon.co.jp ¤Î¥¢¥½¥·¥¨¥¤¥È¥×¥í¥°¥é¥à¤ò³Îǧ¤Î¾å¤´ÍøÍѲ¼¤µ¤¤¡£
42 // * ¥ì¥Ó¥å¡¼¤Ï¡¢amazon ¥×¥é¥°¥¤¥ó¤¬¸Æ¤Ó½Ð¤¹ÊÔ½¸²èÌ̤Ϥ⤦½ÐÍè¤Æ PukiWiki ¤ËÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢
43 // Ãæ»ß¤¹¤ë¤Ê¤éÁ´Ê¸¤òºï½ü¤·¤Æ¥Ú¡¼¥¸¤Î¹¹¿·¥Ü¥¿¥ó¤ò²¡¤¹¤³¤È¡£
44 // * ²¼¤Î PLUGIN_AMAZON_AID¡¢PROXY ¥µ¡¼¥Ð¤ÎÉôʬ¡¢expire ¤ÎÉôʬ¤òŬÅö¤ËÊÔ½¸¤·¤Æ»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤(¾¤Ï¤½¤Î¤Þ¤Þ¤Ç¤â Ok)¡£
46 // Thanks to: Reimy and PukiWiki Developers Team
49 /////////////////////////////////////////////////
52 // Amazon associate ID
53 //define('PLUGIN_AMAZON_AID',''); // None
54 define('PLUGIN_AMAZON_AID','');
56 // Expire caches per ? days
57 define('PLUGIN_AMAZON_EXPIRE_IMAGECACHE', 1);
58 define('PLUGIN_AMAZON_EXPIRE_TITLECACHE', 356);
60 // Alternative image for 'Image not found'
61 define('PLUGIN_AMAZON_NO_IMAGE', IMAGE_DIR . 'noimage.png');
67 define('PLUGIN_AMAZON_SHOP_URI', 'http://www.amazon.co.jp/exec/obidos/ASIN/');
69 // Amazon information inquiry (dev-t = default value in the manual)
70 define('PLUGIN_AMAZON_XML', 'http://xml.amazon.co.jp/onca/xml3?t=webservices-20&' .
71 'dev-t=GTYDRES564THU&type=lite&page=1&f=xml&locale=jp&AsinSearch=');
75 define('PLUGIN_AMAZON_SHOP_URI', 'http://www.amazon.com/exec/obidos/ASIN/');
77 // Amazon information inquiry (dev-t = default value in the manual)
78 define('PLUGIN_AMAZON_XML', 'http://xml.amazon.com/onca/xml3?t=webservices-20&' .
79 'dev-t=GTYDRES564THU&type=lite&page=1&f=xml&locale=us&AsinSearch=');
83 /////////////////////////////////////////////////
85 function plugin_amazon_init()
87 global $amazon_aid, $amazon_body;
89 if (PLUGIN_AMAZON_AID == '') {
92 $amazon_aid = PLUGIN_AMAZON_AID . '/';
95 -ºî¼Ô: [[¤³¤³ÊÔ½¸¤Î¤³¤È]]
105 // ¤Þ¤º¡¢¤³¤Î¥ì¥Ó¥å¡¼¤ò»ß¤á¤ë¾ì¹ç¡¢Á´Ê¸¤òºï½ü¤·¡¢¥Ú¡¼¥¸¤Î[¹¹¿·¥Ü¥¿¥ó]¤ò²¡¤·¤Æ¤¯¤À¤µ¤¤¡ª(PukiWiki ¤Ë¤Ï¤â¤¦ÅÐÏ¿¤µ¤ì¤Æ¤¤¤Þ¤¹)
106 // ³¤±¤ë¤Ê¤é¡¢¾å¤Î¡¢[[¤³¤³ÊÔ½¸¤Î¤³¤È]]Éôʬ¤ò³ç¸Ì¤ò´Þ¤á¤Æºï½ü¤·¡¢½ñ¤Ä¾¤·¤Æ¤¯¤À¤µ¤¤¡£
107 // ¤ªÌ¾Á°¡¢Éôʬ¤Ï¤´¼«Ê¬¤Î̾Á°¤ËÊѹ¹¤·¤Æ¤¯¤À¤µ¤¤¡£»ä¤À¤È¡¢´×¼Ë¡¢¤Ç¤¹¡£
108 // **¤ªÁ¦¤áÂоݡ¢¤è¤ê¾å¤Ï¡¢¿·¤·¤¤¹Ô¤òÄɲ䷤ʤ¤¤Ç¤¯¤À¤µ¤¤¡£Ìܼ¡ºîÀ®¤Ë»ÈÍѤ¹¤ë¤Î¤Ç¡£
109 // //¤Ç»Ï¤Þ¤ë¥³¥á¥ó¥È¹Ô¤Ï¡¢ºÇ½ªÅª¤ËÁ´Éô¥«¥Ã¥È¤·¤Æ¤¯¤À¤µ¤¤¡£Ìܼ¡¤¬Àµ¾ï¤ËºîÀ®¤Ç¤¤Ê¤¤²ÄǽÀ¤¬¤¢¤ê¤Þ¤¹¡£
114 function plugin_amazon_convert()
116 global $script, $vars, $asin, $asin_all;
118 if (func_num_args() > 3) {
119 if (PKWK_READONLY) return ''; // Show nothing
121 return '#amazon([ASIN-number][,left|,right]' .
122 '[,book-title|,image|,delimage|,deltitle|,delete])';
124 } else if (func_num_args() == 0) {
126 if (PKWK_READONLY) return ''; // Show nothing
128 $s_page = htmlspecialchars($vars['page']);
129 if ($s_page == '') $s_page = isset($vars['refer']) ? $vars['refer'] : '';
131 <form action="$script" method="post">
133 <input type="hidden" name="plugin" value="amazon" />
134 <input type="hidden" name="refer" value="$s_page" />
136 <input type="text" name="asin" size="30" value="" />
137 <input type="submit" value="¥ì¥Ó¥å¡¼ÊÔ½¸" /> (ISBN 10 ·å or ASIN 12 ·å)
144 $aryargs = func_get_args();
146 $align = strtolower($aryargs[1]);
147 if ($align == 'clear') return '<div style="clear:both"></div>'; // ²þ¹ÔÁÞÆþ
148 if ($align != 'left') $align = 'right'; // ÇÛÃÖ·èÄê
150 $asin_all = htmlspecialchars($aryargs[0]); // for XSS
151 if (is_asin() == FALSE && $align != 'clear') return FALSE;
153 if ($aryargs[2] != '') {
155 $title = $alt = htmlspecialchars($aryargs[2]); // for XSS
156 if ($alt == 'image') {
157 $alt = plugin_amazon_get_asin_title();
158 if ($alt == '') return FALSE;
160 } else if ($alt == 'delimage') {
161 if (unlink(CACHE_DIR . 'ASIN' . $asin . '.jpg')) {
162 return 'Image of ' . $asin . ' deleted...';
164 return 'Image of ' . $asin . ' NOT DELETED...';
166 } elseif ($alt == 'deltitle') {
167 if (unlink(CACHE_DIR . 'ASIN' . $asin . '.tit')) {
168 return 'Title of ' . $asin . ' deleted...';
170 return 'Title of ' . $asin . ' NOT DELETED...';
172 } elseif ($alt == 'delete') {
173 if ((unlink(CACHE_DIR . 'ASIN' . $asin . '.jpg') &&
174 unlink(CACHE_DIR . 'ASIN' . $asin . '.tit'))) {
175 return 'Title and Image of ' . $asin . ' deleted...';
177 return 'Title and Image of ' . $asin . ' NOT DELETED...';
182 $alt = $title = plugin_amazon_get_asin_title();
183 if ($alt == '') return FALSE;
186 return plugin_amazon_print_object($align, $alt, $title);
189 function plugin_amazon_action()
191 global $vars, $script, $edit_auth, $edit_auth_users;
192 global $amazon_body, $asin, $asin_all;
194 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
196 $s_page = isset($vars['refer']) ? $vars['refer'] : '';
197 $asin_all = isset($vars['asin']) ?
198 htmlspecialchars(rawurlencode(strip_bracket($vars['asin']))) : '';
201 $retvars['msg'] = '¥Ö¥Ã¥¯¥ì¥Ó¥å¡¼ÊÔ½¸';
202 $retvars['refer'] = & $s_page;
203 $retvars['body'] = plugin_amazon_convert();
207 $r_page = $s_page . '/' . $asin;
208 $r_page_url = rawurlencode($r_page);
209 $auth_user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
212 if ($edit_auth && ($auth_user == '' || ! isset($edit_auth_users[$auth_user]) ||
213 $edit_auth_users[$auth_user] != $_SERVER['PHP_AUTH_PW'])) {
214 // Edit-auth failed. Just look the page
215 header('Location: ' . get_script_uri() . '?' . $r_page_url);
217 $title = plugin_amazon_get_asin_title();
218 if ($title == '' || preg_match('#^/#', $s_page)) {
220 header('Location: ' . get_script_uri() . '?' . rawurlencode($s_page));
222 $body = '#amazon(' . $asin_all . ',,image)' . "\n" .
223 '*' . $title . "\n" . $amazon_body;
224 plugin_amazon_review_save($r_page, $body);
225 header('Location: ' . get_script_uri() .
226 '?cmd=edit&page=' . $r_page_url);
233 function plugin_amazon_inline()
235 global $amazon_aid, $asin, $asin_all;
237 list($asin_all) = func_get_args();
239 $asin_all = htmlspecialchars($asin_all); // for XSS
240 if (! is_asin()) return FALSE;
242 $title = plugin_amazon_get_asin_title();
246 return '<a href="' . PLUGIN_AMAZON_SHOP_URI .
247 $asin . '/' . $amazon_aid . 'ref=nosim">' . $title . '</a>' . "\n";
251 function plugin_amazon_print_object($align, $alt, $title)
254 global $asin, $asin_ext, $asin_all;
256 $url = plugin_amazon_cache_image_fetch(CACHE_DIR);
257 $url_shop = PLUGIN_AMAZON_SHOP_URI . $asin . '/' . $amazon_aid . 'ref=nosim';
258 $center = 'text-align:center';
262 $div = '<div style="float:' . $align . ';margin:16px 16px 16px 16px;' . $center . '">' . "\n";
263 $div .= ' <a href="' . $url_shop . '"><img src="' . $url . '" alt="' . $alt . '" /></a>' . "\n";
264 $div .= '</div>' . "\n";
267 // Show image and title
268 $div = '<div style="float:' . $align . ';padding:.5em 1.5em .5em 1.5em;' . $center . '">' . "\n";
269 $div .= ' <table style="width:110px;border:0;' . $center . '">' . "\n";
270 $div .= ' <tr><td style="' . $center . '">' . "\n";
271 $div .= ' <a href="' . $url_shop . '"><img src="' . $url . '" alt="' . $alt .'" /></a></td></tr>' . "\n";
272 $div .= ' <tr><td style="' . $center . '"><a href="' . $url_shop . '">' . $title . '</a></td></tr>' . "\n";
273 $div .= ' </table>' . "\n";
274 $div .= '</div>' . "\n";
279 function plugin_amazon_get_asin_title()
281 global $asin, $asin_ext, $asin_all;
283 if ($asin_all == '') return '';
285 $nocache = $nocachable = 0;
287 $url = PLUGIN_AMAZON_XML . $asin;
289 if (file_exists(CACHE_DIR) === FALSE || is_writable(CACHE_DIR) === FALSE) $nocachable = 1; // ¥¥ã¥Ã¥·¥åÉԲĤξì¹ç
291 if (($title = plugin_amazon_cache_title_fetch(CACHE_DIR)) == FALSE) {
292 $nocache = 1; // ¥¥ã¥Ã¥·¥å¸«¤Ä¤«¤é¤º
293 $body = plugin_amazon_get_page($url); // ¤·¤«¤¿¤Ê¤¤¤Î¤Ç¼è¤ê¤Ë¤¤¤¯
295 $body = mb_convert_encoding($body, SOURCE_ENCODING, 'UTF-8');
296 preg_match('/<ProductName>([^<]*)</', $body, $tmpary);
297 $title = trim($tmpary[1]);
299 // preg_match('#<ImageUrlMedium>http://images-jp.amazon.com/images/P/[^.]+\.(..)\.#',
301 // if ($tmpary[1] != '') {
302 // $asin_ext = $tmpary[1];
303 // $asin_all = $asin . $asin_ext;
310 if ($nocache == 1 && $nocachable != 1)
311 plugin_amazon_cache_title_save($title, CACHE_DIR);
316 // ¥¿¥¤¥È¥ë¥¥ã¥Ã¥·¥å¤¬¤¢¤ë¤«Ä´¤Ù¤ë
317 function plugin_amazon_cache_title_fetch($dir)
319 global $asin, $asin_ext, $asin_all;
321 $filename = $dir . 'ASIN' . $asin . '.tit';
324 if (! is_readable($filename)) {
326 } elseif (PLUGIN_AMAZON_EXPIRE_TITLECACHE * 3600 * 24 < time() - filemtime($filename)) {
330 if ($get_tit) return FALSE;
332 if (($fp = @fopen($filename, 'r')) === FALSE) return FALSE;
333 $title = fgets($fp, 4096);
334 // $tmp_ext = fgets($fp, 4096);
335 // if ($tmp_ext != '') $asin_ext = $tmp_ext;
338 if (strlen($title) > 0) {
345 // ²èÁü¥¥ã¥Ã¥·¥å¤¬¤¢¤ë¤«Ä´¤Ù¤ë
346 function plugin_amazon_cache_image_fetch($dir)
348 global $asin, $asin_ext, $asin_all;
350 $filename = $dir . 'ASIN' . $asin . '.jpg';
353 if (! is_readable($filename)) {
355 } elseif (PLUGIN_AMAZON_EXPIRE_IMAGECACHE * 3600 * 24 < time() - filemtime($filename)) {
360 $url = 'http://images-jp.amazon.com/images/P/' . $asin . '.' . $asin_ext . '.MZZZZZZZ.jpg';
361 if (! is_url($url)) return FALSE;
363 $body = plugin_amazon_get_page($url);
365 $tmpfile = $dir . 'ASIN' . $asin . '.jpg.0';
366 $fp = fopen($tmpfile, 'wb');
369 $size = getimagesize($tmpfile);
372 if ($body == '' || $size[1] <= 1) { // Ä̾ï¤Ï1¤¬Ê֤뤬ǰ¤Î¤¿¤á0¤Î¾ì¹ç¤â(reimy)
373 // ¥¥ã¥Ã¥·¥å¤ò PLUGIN_AMAZON_NO_IMAGE ¤Î¥³¥Ô¡¼¤È¤¹¤ë
374 if ($asin_ext == '09') {
375 $url = 'http://images-jp.amazon.com/images/P/' . $asin . '.01.MZZZZZZZ.jpg';
376 $body = plugin_amazon_get_page($url);
378 $tmpfile = $dir . 'ASIN' . $asin . '.jpg.0';
379 $fp = fopen($tmpfile, 'wb');
382 $size = getimagesize($tmpfile);
386 if ($body == '' || $size[1] <= 1) {
387 $fp = fopen(PLUGIN_AMAZON_NO_IMAGE, 'rb');
388 if (! $fp) return FALSE;
391 while (! feof($fp)) $body .= fread($fp, 4096);
395 plugin_amazon_cache_image_save($body, CACHE_DIR);
401 function plugin_amazon_cache_title_save($data, $dir)
403 global $asin, $asin_ext, $asin_all;
405 $filename = $dir . 'ASIN' . $asin . '.tit';
406 $fp = fopen($filename, 'w');
414 function plugin_amazon_cache_image_save($data, $dir)
416 global $asin, $asin_ext, $asin_all;
418 $filename = $dir . 'ASIN' . $asin . '.jpg';
419 $fp = fopen($filename, 'wb');
427 function plugin_amazon_review_save($page, $data)
429 global $asin, $asin_ext, $asin_all;
431 $filename = DATA_DIR . encode($page) . '.txt';
432 if (! is_readable($filename)) {
433 $fp = fopen($filename, 'w');
442 function plugin_amazon_get_page($url)
444 $data = http_request($url);
445 return ($data['rc'] == 200) ? $data['data'] : '';
451 global $asin, $asin_ext, $asin_all;
454 if (preg_match('/^([A-Z0-9]{10}).?([0-9][0-9])?$/', $asin_all, $tmpary) == FALSE) {
458 $asin_ext = isset($tmpary[2]) ? $tmpary[2] : '';
459 if ($asin_ext == '') $asin_ext = '09';
460 $asin_all = $asin . $asin_ext;