OSDN Git Service

NP_Paint v1.18
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Paint / paint / Parser_PaintBBS.php
diff --git a/trunk/NP_Paint/paint/Parser_PaintBBS.php b/trunk/NP_Paint/paint/Parser_PaintBBS.php
new file mode 100644 (file)
index 0000000..3a94922
--- /dev/null
@@ -0,0 +1,145 @@
+<?php\r
+// vim: tabstop=2:shiftwidth=2\r
+\r
+/**\r
+  * Parser_PaintBBS.php ($Revision: 1.33 $)\r
+  * \r
+  * by hsur ( http://blog.cles.jp/np_cles )\r
+  * $Id: Parser_PaintBBS.php,v 1.33 2010/06/06 11:44:19 hsur Exp $\r
+*/\r
+\r
+/*\r
+  * Copyright (C) 2005-2010 CLES. All rights reserved.\r
+  *\r
+  * This program is free software; you can redistribute it and/or\r
+  * modify it under the terms of the GNU General Public License\r
+  * as published by the Free Software Foundation; either version 2\r
+  * of the License, or (at your option) any later version.\r
+  * \r
+  * This program is distributed in the hope that it will be useful,\r
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+  * GNU General Public License for more details.\r
+  * \r
+  * You should have received a copy of the GNU General Public License\r
+  * along with this program; if not, write to the Free Software\r
+  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA\r
+  * \r
+  * In addition, as a special exception, cles( http://blog.cles.jp/np_cles ) gives\r
+  * permission to link the code of this program with those files in the PEAR\r
+  * library that are licensed under the PHP License (or with modified versions\r
+  * of those files that use the same license as those files), and distribute\r
+  * linked combinations including the two. You must obey the GNU General Public\r
+  * License in all respects for all of the code used other than those files in\r
+  * the PEAR library that are licensed under the PHP License. If you modify\r
+  * this file, you may extend this exception to your version of the file,\r
+  * but you are not obligated to do so. If you do not wish to do so, delete\r
+  * this exception statement from your version.\r
+*/\r
+\r
+\r
+class Parser_PaintBBS extends PaintPlugin {\r
+       function Parser_PaintBBS(){\r
+               $this->id = '$Id: Parser_PaintBBS.php,v 1.33 2010/06/06 11:44:19 hsur Exp $';\r
+               $this->name = 'PaintBBS Parser';\r
+               $this->enable = true;\r
+               $this->offset = 0;\r
+               $this->postdata = null;\r
+       }\r
+       \r
+       function _read($length, &$err){\r
+               $data = substr( $this->postdata, $this->offset, $length );\r
+               $this->offset += strlen($data);\r
+               $err = false;\r
+               if( strlen($data) != $length ) $err = true;\r
+               return $data;\r
+       }\r
+                       \r
+       function parse(){\r
+               global $manager;\r
+               $plugin = $manager->getPlugin('NP_Paint');\r
+\r
+               ini_set("always_populate_raw_post_data", "1");\r
+               global $HTTP_RAW_POST_DATA;\r
+               $this->postdata = $HTTP_RAW_POST_DATA;;\r
+               \r
+               if(! $this->postdata ){\r
+                       $plugin->_info(_PAINT_Parser_useinput);\r
+                       if( $_SERVER['CONTENT_LENGTH'] ) $length = $_SERVER['CONTENT_LENGTH'];\r
+                       if( $_ENV['CONTENT_LENGTH'] ) $length = $_ENV['CONTENT_LENGTH'];\r
+                       $input = fopen("php://input", "rb");\r
+                       if( $length ){\r
+                               $this->postdata = fread($input, $length);\r
+                       } else {\r
+                               $plugin->_warn(_PAINT_Parser_contentLengthNotFound);\r
+                               while (!feof($input)) {\r
+                                 $this->postdata .= fread($input, 8192);\r
+                               }\r
+                       }\r
+                       fclose($input);\r
+               }\r
+                               \r
+               //TODO: debug\r
+/*\r
+               $test = fopen("/tmp/postdata.dat","wb");\r
+               fwrite($test, $this->postdata);\r
+               fclose($test);\r
+               $plugin->_info('post length: ' . strlen($this->postdata) );\r
+*/\r
+\r
+               $data = Array(\r
+                       'magic' => null,\r
+                       'header' => null,\r
+                       'images' => Array(),\r
+                       'size' => Array(),\r
+               );\r
+               $err = false;\r
+               \r
+               // magic char\r
+               $data['magic'] = $this->_read(1, $err);\r
+               if($err) return 'Cannot read magic char';\r
+               if( $data['magic'] == chr(0x00) ) return 'magic char is INVALID. (maybe poo=true?) ->' . $data['magic'];\r
+               $plugin->_info('post magic: ' . $data['magic'] );\r
+\r
+               // header\r
+               $headerSize = $this->_read(8, $err);\r
+               if($err) return 'Cannot read header size';\r
+               if( ! is_numeric($headerSize) ) return 'Header size is INVALID';\r
+               \r
+               $data['header'] = $this->_read(intval($headerSize), $err);\r
+               if($err) return 'Cannot read header';\r
+               \r
+               // image\r
+               $imageSize = $this->_read(8, $err);\r
+               if($err) return 'Cannot read image size';\r
+               if( ! is_numeric($imageSize) ) return 'Image size is INVALID';\r
+               \r
+               $pad = $this->_read(2, $err);// '\r\n'\r
+               if($err) return 'Cannot read padding';\r
+               if(! $pad === "\r\n" ) return 'Cannot find padding';\r
+\r
+               $imageData = $this->_read(intval($imageSize), $err);\r
+               if($err) return 'Cannot read image ' . "(expected: $imageSize, actual:" . strlen($imageData) . ")";\r
+               $plugin->_info("ImageSize (expected: $imageSize, actual:" . strlen($imageData) . ")");\r
+               \r
+               $data['images'][] = $imageData;\r
+               $data['size'][] = strlen($imageData);\r
+\r
+               // thumb\r
+               while(true){\r
+                       $thumbSize = $this->_read(8, $err);\r
+                       if($err) break;\r
+                       if( ! is_numeric($thumbSize) ) break;\r
+                       \r
+                       if( $thumbSize != 0 ){\r
+                               $imageData = $this->_read($thumbSize, $err);\r
+                               if($err) break;\r
+                               $data['images'][] = $imageData;\r
+                               $data['size'][] = strlen($imageData);\r
+                               $plugin->_info("AnimeSize (expected: $thumbSize, actual:" . strlen($imageData) . ")");\r
+                       }\r
+               }\r
+               \r
+               return $data;\r
+       }\r
+}\r