2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: amazon.inc.php,v 1.11 2004/12/28 15:12:25 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 PukiWiki Developer 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 return '#amazon([ASIN-number][,left|,right]' .
120 '[,book-title|,image|,delimage|,deltitle|,delete])';
121 } else if (func_num_args() == 0) {
123 $s_page = htmlspecialchars($vars['page']);
124 if ($s_page == '') $s_page = $vars['refer'];
126 <form action="$script" method="post">
128 <input type="hidden" name="plugin" value="amazon" />
129 <input type="hidden" name="refer" value="$s_page" />
131 <input type="text" name="asin" size="30" value="" />
132 <input type="submit" value="¥ì¥Ó¥å¡¼ÊÔ½¸" /> (ISBN 10 ·å or ASIN 12 ·å)
139 $aryargs = func_get_args();
141 $align = strtolower($aryargs[1]);
142 if ($align == 'clear') return '<div style="clear:both"></div>'; // ²þ¹ÔÁÞÆþ
143 if ($align != 'left') $align = 'right'; // ÇÛÃÖ·èÄê
145 $asin_all = htmlspecialchars($aryargs[0]); // for XSS
146 if (is_asin() == FALSE && $align != 'clear') return FALSE;
148 if ($aryargs[2] != '') {
150 $title = $alt = htmlspecialchars($aryargs[2]); // for XSS
151 if ($alt == 'image') {
152 $alt = plugin_amazon_get_asin_title();
153 if ($alt == '') return FALSE;
155 } else if ($alt == 'delimage') {
156 if (unlink(CACHE_DIR . 'ASIN' . $asin . '.jpg')) {
157 return 'Image of ' . $asin . ' deleted...';
159 return 'Image of ' . $asin . ' NOT DELETED...';
161 } elseif ($alt == 'deltitle') {
162 if (unlink(CACHE_DIR . 'ASIN' . $asin . '.tit')) {
163 return 'Title of ' . $asin . ' deleted...';
165 return 'Title of ' . $asin . ' NOT DELETED...';
167 } elseif ($alt == 'delete') {
168 if ((unlink(CACHE_DIR . 'ASIN' . $asin . '.jpg') &&
169 unlink(CACHE_DIR . 'ASIN' . $asin . '.tit'))) {
170 return 'Title and Image of ' . $asin . ' deleted...';
172 return 'Title and Image of ' . $asin . ' NOT DELETED...';
177 $alt = $title = plugin_amazon_get_asin_title();
178 if ($alt == '') return FALSE;
181 return plugin_amazon_print_object($align, $alt, $title);
184 function plugin_amazon_action()
186 global $vars, $script, $edit_auth, $edit_auth_users;
187 global $amazon_body, $asin, $asin_all;
189 $asin_all = htmlspecialchars(rawurlencode(strip_bracket($vars['asin'])));
192 $retvars['msg'] = '¥Ö¥Ã¥¯¥ì¥Ó¥å¡¼ÊÔ½¸';
193 $retvars['refer'] = $vars['refer'];
194 $retvars['body'] = plugin_amazon_convert();
198 $s_page = $vars['refer'];
199 $r_page = $s_page . '/' . $asin;
200 $r_page_url = rawurlencode($r_page);
203 if ($edit_auth && (! isset($_SERVER['PHP_AUTH_USER']) ||
204 ! array_key_exists($_SERVER['PHP_AUTH_USER'], $edit_auth_users) ||
205 $edit_auth_users[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW'])) {
206 header('Location: ' . get_script_uri() . '?cmd=read&page=' . $r_page_url);
208 $title = plugin_amazon_get_asin_title();
209 if ($title == '' || preg_match('/^\//', $s_page)) {
210 header('Location: ' . get_script_uri() .
211 '?cmd=read&page=' . encode($s_page));
213 $body = '#amazon(' . $asin_all . ',,image)' . "\n" .
214 '*' . $title . "\n" . $amazon_body;
215 plugin_amazon_review_save($r_page, $body);
216 header('Location: ' . get_script_uri() . '?cmd=edit&page=' . $r_page_url);
222 function plugin_amazon_inline()
224 global $amazon_aid, $asin, $asin_all;
226 list($asin_all) = func_get_args();
228 $asin_all = htmlspecialchars($asin_all); // for XSS
229 if (! is_asin()) return FALSE;
231 $title = plugin_amazon_get_asin_title();
235 return '<a href="' . PLUGIN_AMAZON_SHOP_URI .
236 $asin . '/' . $amazon_aid . 'ref=nosim">' . $title . '</a>' . "\n";
240 function plugin_amazon_print_object($align, $alt, $title)
243 global $asin, $asin_ext, $asin_all;
245 $url = plugin_amazon_cache_image_fetch(CACHE_DIR);
246 $url_shop = PLUGIN_AMAZON_SHOP_URI . $asin . '/' . $amazon_aid . 'ref=nosim';
247 $center = 'text-align:center';
251 $div = '<div style="float:' . $align . ';margin:16px 16px 16px 16px;' . $center . '">' . "\n";
252 $div .= ' <a href="' . $url_shop . '"><img src="' . $url . '" alt="' . $alt . '" /></a>' . "\n";
253 $div .= '</div>' . "\n";
256 // Show image and title
257 $div = '<div style="float:' . $align . ';padding:.5em 1.5em .5em 1.5em;' . $center . '">' . "\n";
258 $div .= ' <table style="width:110px;border:0;' . $center . '">' . "\n";
259 $div .= ' <tr><td style="' . $center . '">' . "\n";
260 $div .= ' <a href="' . $url_shop . '"><img src="' . $url . '" alt="' . $alt .'" /></a></td></tr>' . "\n";
261 $div .= ' <tr><td style="' . $center . '"><a href="' . $url_shop . '">' . $title . '</a></td></tr>' . "\n";
262 $div .= ' </table>' . "\n";
263 $div .= '</div>' . "\n";
268 function plugin_amazon_get_asin_title()
270 global $asin, $asin_ext, $asin_all;
272 if ($asin_all == '') return '';
274 $nocache = $nocachable = 0;
276 $url = PLUGIN_AMAZON_XML . $asin;
278 if (file_exists(CACHE_DIR) === FALSE || is_writable(CACHE_DIR) === FALSE) $nocachable = 1; // ¥¥ã¥Ã¥·¥åÉԲĤξì¹ç
280 if (($title = plugin_amazon_cache_title_fetch(CACHE_DIR)) == FALSE) {
281 $nocache = 1; // ¥¥ã¥Ã¥·¥å¸«¤Ä¤«¤é¤º
282 $body = plugin_amazon_get_page($url); // ¤·¤«¤¿¤Ê¤¤¤Î¤Ç¼è¤ê¤Ë¤¤¤¯
284 $body = mb_convert_encoding($body, SOURCE_ENCODING, 'UTF-8');
285 preg_match('/<ProductName>([^<]*)</', $body, $tmpary);
286 $title = trim($tmpary[1]);
288 // preg_match('#<ImageUrlMedium>http://images-jp.amazon.com/images/P/[^.]+\.(..)\.#',
290 // if ($tmpary[1] != '') {
291 // $asin_ext = $tmpary[1];
292 // $asin_all = $asin . $asin_ext;
299 if ($nocache == 1 && $nocachable != 1)
300 plugin_amazon_cache_title_save($title, CACHE_DIR);
305 // ¥¿¥¤¥È¥ë¥¥ã¥Ã¥·¥å¤¬¤¢¤ë¤«Ä´¤Ù¤ë
306 function plugin_amazon_cache_title_fetch($dir)
308 global $asin, $asin_ext, $asin_all;
310 $filename = $dir . 'ASIN' . $asin . '.tit';
313 if (! is_readable($filename)) {
315 } elseif (PLUGIN_AMAZON_EXPIRE_TITLECACHE * 3600 * 24 < time() - filemtime($filename)) {
319 if ($get_tit) return FALSE;
321 if (($fp = @fopen($filename, 'r')) === FALSE) return FALSE;
322 $title = fgets($fp, 4096);
323 // $tmp_ext = fgets($fp, 4096);
324 // if ($tmp_ext != '') $asin_ext = $tmp_ext;
327 if (strlen($title) > 0) {
334 // ²èÁü¥¥ã¥Ã¥·¥å¤¬¤¢¤ë¤«Ä´¤Ù¤ë
335 function plugin_amazon_cache_image_fetch($dir)
337 global $asin, $asin_ext, $asin_all;
339 $filename = $dir . 'ASIN' . $asin . '.jpg';
342 if (! is_readable($filename)) {
344 } elseif (PLUGIN_AMAZON_EXPIRE_IMAGECACHE * 3600 * 24 < time() - filemtime($filename)) {
349 $url = 'http://images-jp.amazon.com/images/P/' . $asin . '.' . $asin_ext . '.MZZZZZZZ.jpg';
350 if (! is_url($url)) return FALSE;
352 $body = plugin_amazon_get_page($url);
354 $tmpfile = $dir . 'ASIN' . $asin . '.jpg.0';
355 $fp = fopen($tmpfile, 'wb');
358 $size = getimagesize($tmpfile);
361 if ($body == '' || $size[1] <= 1) { // Ä̾ï¤Ï1¤¬Ê֤뤬ǰ¤Î¤¿¤á0¤Î¾ì¹ç¤â(reimy)
362 // ¥¥ã¥Ã¥·¥å¤ò PLUGIN_AMAZON_NO_IMAGE ¤Î¥³¥Ô¡¼¤È¤¹¤ë
363 if ($asin_ext == '09') {
364 $url = 'http://images-jp.amazon.com/images/P/' . $asin . '.01.MZZZZZZZ.jpg';
365 $body = plugin_amazon_get_page($url);
367 $tmpfile = $dir . 'ASIN' . $asin . '.jpg.0';
368 $fp = fopen($tmpfile, 'wb');
371 $size = getimagesize($tmpfile);
375 if ($body == '' || $size[1] <= 1) {
376 $fp = fopen(PLUGIN_AMAZON_NO_IMAGE, 'rb');
377 if (! $fp) return FALSE;
380 while (! feof($fp)) $body .= fread($fp, 4096);
384 plugin_amazon_cache_image_save($body, CACHE_DIR);
390 function plugin_amazon_cache_title_save($data, $dir)
392 global $asin, $asin_ext, $asin_all;
394 $filename = $dir . 'ASIN' . $asin . '.tit';
395 $fp = fopen($filename, 'w');
403 function plugin_amazon_cache_image_save($data, $dir)
405 global $asin, $asin_ext, $asin_all;
407 $filename = $dir . 'ASIN' . $asin . '.jpg';
408 $fp = fopen($filename, 'wb');
416 function plugin_amazon_review_save($page, $data)
418 global $asin, $asin_ext, $asin_all;
420 $filename = DATA_DIR . encode($page) . '.txt';
421 if (! is_readable($filename)) {
422 $fp = fopen($filename, 'w');
431 function plugin_amazon_get_page($url)
433 $data = http_request($url);
434 return ($data['rc'] == 200) ? $data['data'] : '';
440 global $asin, $asin_ext, $asin_all;
443 if (preg_match('/^([A-Z0-9]{10}).?([0-9][0-9])?$/', $asin_all, $tmpary) == FALSE) {
447 $asin_ext = $tmpary[2];
448 if ($asin_ext == '') $asin_ext = '09';
449 $asin_all = $asin . $asin_ext;