OSDN Git Service

Merge branch 'master' into develop
[sie/sie.git] / tool / sie_php / sie.php
1 <?php\r
2 \r
3 /**\r
4  * sie.php\r
5  *\r
6  * Copyright (c) 2008-2010 revulo\r
7  *\r
8  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
9  * of this software and associated documentation files (the "Software"), to\r
10  * deal in the Software without restriction, including without limitation the\r
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or\r
12  * sell copies of the Software, and to permit persons to whom the Software is\r
13  * furnished to do so, subject to the following conditions:\r
14  *\r
15  * The above copyright notice and this permission notice shall be included in\r
16  * all copies or substantial portions of the Software.\r
17  *\r
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\r
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\r
24  * IN THE SOFTWARE.\r
25  *\r
26  * @package     sie\r
27  * @author      revulo <revulon@gmail.com>\r
28  * @copyright   2008-2010 revulo\r
29  * @license     http://www.opensource.org/licenses/mit-license.php  MIT License\r
30  * @version     Release: 0.4\r
31  * @link        http://www.revulo.com/SVG/SIE.html\r
32  * @link        http://sie.sourceforge.jp/\r
33  */\r
34 \r
35 // Filename of SIE JavaScript library.\r
36 define('SIE_JS', 'sie.js');\r
37 \r
38 // Get the requested URL.\r
39 $url = $_SERVER['REQUEST_URI'];\r
40 \r
41 // Trim the query string from the URL.\r
42 $pos = strrpos($url, '?');\r
43 if ($pos !== false) {\r
44     $url = substr($url, 0, $pos);\r
45 }\r
46 \r
47 // Get the path to the requested file.\r
48 $file = rawurldecode($_SERVER['DOCUMENT_ROOT'] . $url);\r
49 \r
50 // Check the file extension.\r
51 $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));\r
52 if ($extension !== 'svg' && $extension !== 'svgz') {\r
53     header('HTTP/1.1 403 Forbidden');\r
54     exit;\r
55 }\r
56 \r
57 // Return a 404 status code if the SVG file is not found.\r
58 if (is_readable($file) === false) {\r
59     header('HTTP/1.1 404 Not Found');\r
60     exit;\r
61 }\r
62 \r
63 // If the request is an Ajax request, output the raw contents.\r
64 if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {\r
65     header('Content-Disposition: inline');\r
66     header('Content-Length: ' . filesize($file));\r
67     header('Content-Type: image/svg+xml');\r
68     readfile($file);\r
69     exit;\r
70 }\r
71 \r
72 // If the requested URL contains a question mark, download the file.\r
73 if ($pos !== false) {\r
74     header('Content-Type: application/octet-stream');\r
75     header('Content-Disposition: attachment');\r
76     header('Content-Length: ' . filesize($file));\r
77     readfile($file);\r
78     exit;\r
79 }\r
80 \r
81 // Get the relative-path reference to the requested file.\r
82 $url = basename($url);\r
83 \r
84 // Decode the file name.\r
85 $name = rawurldecode($url);\r
86 $name = mb_convert_encoding($name, 'UTF-8', 'auto');\r
87 $name = htmlspecialchars($name);\r
88 \r
89 // Get uncompressed file contents.\r
90 if ($extension === 'svgz') {\r
91     $data = file_get_contents('compress.zlib://' . $file); \r
92 } else {\r
93     $data = file_get_contents($file);\r
94 }\r
95 \r
96 // Strip <script> tags from the file contents.\r
97 $data = preg_replace('/<script.*?\/script>/is', '', $data);\r
98 \r
99 // Get the absolute-path reference to the JavaScript file.\r
100 $javascript = dirname($_SERVER['SCRIPT_NAME']) . '/' . SIE_JS;\r
101 \r
102 // Disable browser caching.\r
103 header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');\r
104 \r
105 ?>\r
106 <html>\r
107 <head>\r
108 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\r
109 </head>\r
110 <body>\r
111 \r
112 <div style="color: #777; margin-bottom: 1em;">\r
113 This is an image converted by <a href="http://sie.sourceforge.jp/">SIE</a>.<br />\r
114 Original file: <a href="<?php echo $url ?>?" title="Download <?php echo $name ?>"><?php echo $name ?></a>\r
115 </div>\r
116 \r
117 <script type="image/svg+xml" width="1000" height="1000">\r
118 <?php echo $data ?>\r
119 </script>\r
120 <script type="text/javascript" src="<?php echo $javascript ?>"></script>\r
121 \r
122 </body>\r
123 </html>\r