OSDN Git Service

FIX: NP_AttachをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Paint / paint / Parser_PaintBBS.php
1 <?php\r
2 // vim: tabstop=2:shiftwidth=2\r
3 \r
4 /**\r
5   * Parser_PaintBBS.php ($Revision: 1.33 $)\r
6   * \r
7   * by hsur ( http://blog.cles.jp/np_cles )\r
8   * $Id: Parser_PaintBBS.php,v 1.33 2010/06/06 11:44:19 hsur Exp $\r
9 */\r
10 \r
11 /*\r
12   * Copyright (C) 2005-2010 CLES. All rights reserved.\r
13   *\r
14   * This program is free software; you can redistribute it and/or\r
15   * modify it under the terms of the GNU General Public License\r
16   * as published by the Free Software Foundation; either version 2\r
17   * of the License, or (at your option) any later version.\r
18   * \r
19   * This program is distributed in the hope that it will be useful,\r
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
22   * GNU General Public License for more details.\r
23   * \r
24   * You should have received a copy of the GNU General Public License\r
25   * along with this program; if not, write to the Free Software\r
26   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA\r
27   * \r
28   * In addition, as a special exception, cles( http://blog.cles.jp/np_cles ) gives\r
29   * permission to link the code of this program with those files in the PEAR\r
30   * library that are licensed under the PHP License (or with modified versions\r
31   * of those files that use the same license as those files), and distribute\r
32   * linked combinations including the two. You must obey the GNU General Public\r
33   * License in all respects for all of the code used other than those files in\r
34   * the PEAR library that are licensed under the PHP License. If you modify\r
35   * this file, you may extend this exception to your version of the file,\r
36   * but you are not obligated to do so. If you do not wish to do so, delete\r
37   * this exception statement from your version.\r
38 */\r
39 \r
40 \r
41 class Parser_PaintBBS extends PaintPlugin {\r
42         function Parser_PaintBBS(){\r
43                 $this->id = '$Id: Parser_PaintBBS.php,v 1.33 2010/06/06 11:44:19 hsur Exp $';\r
44                 $this->name = 'PaintBBS Parser';\r
45                 $this->enable = true;\r
46                 $this->offset = 0;\r
47                 $this->postdata = null;\r
48         }\r
49         \r
50         function _read($length, &$err){\r
51                 $data = substr( $this->postdata, $this->offset, $length );\r
52                 $this->offset += strlen($data);\r
53                 $err = false;\r
54                 if( strlen($data) != $length ) $err = true;\r
55                 return $data;\r
56         }\r
57                         \r
58         function parse(){\r
59                 global $manager;\r
60                 $plugin = $manager->getPlugin('NP_Paint');\r
61 \r
62                 ini_set("always_populate_raw_post_data", "1");\r
63                 global $HTTP_RAW_POST_DATA;\r
64                 $this->postdata = $HTTP_RAW_POST_DATA;;\r
65                 \r
66                 if(! $this->postdata ){\r
67                         $plugin->_info(_PAINT_Parser_useinput);\r
68                         if( $_SERVER['CONTENT_LENGTH'] ) $length = $_SERVER['CONTENT_LENGTH'];\r
69                         if( $_ENV['CONTENT_LENGTH'] ) $length = $_ENV['CONTENT_LENGTH'];\r
70                         $input = fopen("php://input", "rb");\r
71                         if( $length ){\r
72                                 $this->postdata = fread($input, $length);\r
73                         } else {\r
74                                 $plugin->_warn(_PAINT_Parser_contentLengthNotFound);\r
75                                 while (!feof($input)) {\r
76                                   $this->postdata .= fread($input, 8192);\r
77                                 }\r
78                         }\r
79                         fclose($input);\r
80                 }\r
81                                 \r
82                 //TODO: debug\r
83 /*\r
84                 $test = fopen("/tmp/postdata.dat","wb");\r
85                 fwrite($test, $this->postdata);\r
86                 fclose($test);\r
87                 $plugin->_info('post length: ' . strlen($this->postdata) );\r
88 */\r
89 \r
90                 $data = Array(\r
91                         'magic' => null,\r
92                         'header' => null,\r
93                         'images' => Array(),\r
94                         'size' => Array(),\r
95                 );\r
96                 $err = false;\r
97                 \r
98                 // magic char\r
99                 $data['magic'] = $this->_read(1, $err);\r
100                 if($err) return 'Cannot read magic char';\r
101                 if( $data['magic'] == chr(0x00) ) return 'magic char is INVALID. (maybe poo=true?) ->' . $data['magic'];\r
102                 $plugin->_info('post magic: ' . $data['magic'] );\r
103 \r
104                 // header\r
105                 $headerSize = $this->_read(8, $err);\r
106                 if($err) return 'Cannot read header size';\r
107                 if( ! is_numeric($headerSize) ) return 'Header size is INVALID';\r
108                 \r
109                 $data['header'] = $this->_read(intval($headerSize), $err);\r
110                 if($err) return 'Cannot read header';\r
111                 \r
112                 // image\r
113                 $imageSize = $this->_read(8, $err);\r
114                 if($err) return 'Cannot read image size';\r
115                 if( ! is_numeric($imageSize) ) return 'Image size is INVALID';\r
116                 \r
117                 $pad = $this->_read(2, $err);// '\r\n'\r
118                 if($err) return 'Cannot read padding';\r
119                 if(! $pad === "\r\n" ) return 'Cannot find padding';\r
120 \r
121                 $imageData = $this->_read(intval($imageSize), $err);\r
122                 if($err) return 'Cannot read image ' . "(expected: $imageSize, actual:" . strlen($imageData) . ")";\r
123                 $plugin->_info("ImageSize (expected: $imageSize, actual:" . strlen($imageData) . ")");\r
124                 \r
125                 $data['images'][] = $imageData;\r
126                 $data['size'][] = strlen($imageData);\r
127 \r
128                 // thumb\r
129                 while(true){\r
130                         $thumbSize = $this->_read(8, $err);\r
131                         if($err) break;\r
132                         if( ! is_numeric($thumbSize) ) break;\r
133                         \r
134                         if( $thumbSize != 0 ){\r
135                                 $imageData = $this->_read($thumbSize, $err);\r
136                                 if($err) break;\r
137                                 $data['images'][] = $imageData;\r
138                                 $data['size'][] = strlen($imageData);\r
139                                 $plugin->_info("AnimeSize (expected: $thumbSize, actual:" . strlen($imageData) . ")");\r
140                         }\r
141                 }\r
142                 \r
143                 return $data;\r
144         }\r
145 }\r