OSDN Git Service

NP_Paint v1.18
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Paint / paint / index.php
1 <?php\r
2 // vim: tabstop=2:shiftwidth=2\r
3 \r
4 /**\r
5   * index.php ($Revision: 1.99 $)\r
6   * \r
7   * by hsur ( http://blog.cles.jp/np_cles )\r
8   * $Id: index.php,v 1.99 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 define('PAINT_INI_URL', 'http://blog.cles.jp/support/paint.ini');\r
41 define('PAINT_PLUGIN_DIR',      dirname(__FILE__)."/applet");\r
42 \r
43 $strRel = '../../../';\r
44 include ($strRel.'config.php');\r
45 include ($DIR_LIBS.'PLUGINADMIN.php');\r
46 \r
47 require_once($DIR_PLUGINS . 'sharedlibs/sharedlibs.php');\r
48 require_once('cles/Feedback.php');\r
49 require_once('cles/Template.php');\r
50 require_once('pclzip.lib.php');\r
51 \r
52 if ($blogid) {\r
53         $isblogadmin = $member->isBlogAdmin($blogid);\r
54 } else\r
55         $isblogadmin = 0;\r
56 \r
57 if (!$member->isLoggedIn()) {\r
58         $oPluginAdmin = new PluginAdmin('Paint');\r
59         $oPluginAdmin->start();\r
60         echo '<p>' . _PAINT_NeedLogin . '</p>';\r
61         $oPluginAdmin->end();\r
62         exit;\r
63 }\r
64 \r
65 if (!($member->isAdmin() || $isblogadmin)) {\r
66         $oPluginAdmin = new PluginAdmin('Paint');\r
67         $oPluginAdmin->start();\r
68         echo "<p>"._ERROR_DISALLOWED."</p>";\r
69         $oPluginAdmin->end();\r
70         exit;\r
71 }\r
72 \r
73 $action = requestVar('action');\r
74 $aActionsNotToCheck = array(\r
75         '',\r
76         'report',\r
77 );\r
78 if (!in_array($action, $aActionsNotToCheck)) {\r
79         if (!$manager->checkTicket()) doError(_ERROR_BADTICKET);\r
80 }\r
81 \r
82 // create the admin area page\r
83 $oPluginAdmin = new PluginAdmin('Paint');\r
84 $oPluginAdmin->start();\r
85 $fb =& new cles_Feedback($oPluginAdmin);\r
86 \r
87 $templateEngine =& new cles_Template(dirname(__FILE__).'/template');\r
88 define('NP_PAINT_TEMPLATEDIR_INDEX', 'index');\r
89 $tplVars = array(\r
90         'indexurl' => serverVar('PHP_SELF'),\r
91         'optionurl' => $CONF['AdminURL'] . 'index.php?action=pluginoptions&amp;plugid=' . $oPluginAdmin->plugin->getid(),\r
92         'paintiniurl' => PAINT_INI_URL,\r
93         'paintplugindir' => PAINT_PLUGIN_DIR,\r
94         'ticket' => $manager->_generateTicket(),\r
95 );\r
96 \r
97 // get the plugin options; stored in the DB\r
98 //$pbl_config['enabled']       = $oPluginAdmin->plugin->getOption('enabled');\r
99 \r
100 // menu\r
101 $menu = $templateEngine->fetch('menu', NP_PAINT_TEMPLATEDIR_INDEX);\r
102 echo $templateEngine->fill($menu, $tplVars, false);\r
103 \r
104 //action\r
105 switch ($action) {\r
106         case 'help' :\r
107                 echo $templateEngine->fetch('help', NP_PAINT_TEMPLATEDIR_INDEX);\r
108                 break;\r
109                 \r
110         case 'install' :\r
111                 if( requestVar('func') == 'ini' ){\r
112                         $err = paint_plugin_download( PAINT_INI_URL, PAINT_PLUGIN_DIR);\r
113                         if($err){\r
114                                 echo '<h3>Error: ' . _PAINT_canNotReadFile . ':' .PAINT_INI_URL . '</h3>';\r
115                                 echo '<h4>Reason: '.$err.'</h4>';\r
116                                 break;\r
117                         }\r
118                 }\r
119                 \r
120                 echo "<h2>"._PAINT_iniDownload."</h2>";\r
121                 echo "<ul>\n";\r
122                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=install&func=ini"),ENT_QUOTES)."\">"._PAINT_doDownload."</a></li>\n";\r
123                 echo "</ul>\n";\r
124 \r
125                 if( ! is_readable(PAINT_PLUGIN_DIR . '/paint.ini') )\r
126                         break;\r
127                 \r
128                 $settings = parse_ini_file(PAINT_PLUGIN_DIR . '/paint.ini', true);\r
129                 if( ! $settings ){\r
130                         echo '<h3>Error: ' . _PAINT_canNotReadFile . ': ' .PAINT_INI_URL . '</h3>';\r
131                         break;\r
132                 }\r
133                 \r
134                 echo '<h2>' . _PAINT_appletinstall. "( {$settings['paint']['version']}[{$settings['paint']['updated']}] )</h2>";\r
135 \r
136                 if ($pluginName = getVar('plugin')) {\r
137                         $oPluginAdmin->plugin->_loadPlugin();\r
138                         $plugin = $oPluginAdmin->plugin->disabledPlugin[$pluginName];\r
139 \r
140                         if ($plugin) {\r
141                                 $zipurl = $settings[$pluginName]['zip'];\r
142                                 if($zipurl)\r
143                                         echo paint_plugin_install($zipurl);\r
144                                 else\r
145                                         echo '<h3>Error:' ._PAINT_canNotAutoInstall . '</h3>';\r
146 \r
147                                 } else {\r
148                                 echo '<h3>Error:'. _PAINT_noSuchPlugin . '</h3>';\r
149                         }\r
150                 }\r
151 \r
152                 $oPluginAdmin->plugin->_loadPlugin(false, true);\r
153                 ksort($oPluginAdmin->plugin->disabledPlugin);\r
154 \r
155                 echo '<h3>'._PAINT_autoInstall.'</h3>';\r
156                 echo "<ul>\n";\r
157                 foreach ($oPluginAdmin->plugin->disabledPlugin as $key => $plugin) {\r
158                         if ( $settings[$key]['zip'] )\r
159                                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=install&plugin=".$key),ENT_QUOTES)."\">".$plugin->getName()._PAINT_installSuffix."</a></li>\n";\r
160                         elseif ( $settings[$key]['file'] )\r
161                                 echo "<li><em>".$plugin->getName(). _PAINT_canNotAutoInstall ."<br /><a href=\"{$settings[$key]['file']}\">{$settings[$key]['file']}</a>"._PAINT_downloadSuffix."</em></li>\n";\r
162                         else\r
163                                 echo "<li><em>".$plugin->getName(). _PAINT_canNotAutoInstall . "</em></li>\n";\r
164                 }\r
165                 echo "</ul>\n";\r
166 \r
167                 break;\r
168 \r
169         case 'report' :\r
170                 $oPluginAdmin->plugin->_loadPlugin();\r
171                 $extra = '';\r
172                 foreach ($oPluginAdmin->plugin->parsers as $key => $name) {\r
173                         $extra .= 'parser_'.$key.', ';\r
174                 }\r
175                 foreach ($oPluginAdmin->plugin->applets as $key => $name) {\r
176                         $extra .= 'applet_'.$key.', ';\r
177                 }\r
178                 foreach ($oPluginAdmin->plugin->palettes as $key => $name) {\r
179                         $extra .= 'palette_'.$key.', ';\r
180                 }\r
181                 foreach ($oPluginAdmin->plugin->viewers as $key => $name) {\r
182                         $extra .= 'viewer_'.$key.', ';\r
183                 }\r
184                 $fb->printForm($extra);\r
185 \r
186                 break;\r
187 \r
188         default :\r
189                 break;\r
190 }\r
191 \r
192 echo "<br />";\r
193 \r
194 $oPluginAdmin->end();\r
195 \r
196 function paint_plugin_install($url) {\r
197         $err = paint_plugin_download($url, PAINT_PLUGIN_DIR);\r
198         if ($err)\r
199                 return $err;\r
200 \r
201         $file = PAINT_PLUGIN_DIR . '/' . basename($url);\r
202         $err = paint_plugin_extract($file, PAINT_PLUGIN_DIR);\r
203 \r
204         return $err;\r
205 }\r
206 \r
207 function paint_plugin_extract($file, $dir) {\r
208         $archive = new PclZip($file);\r
209         if ($archive->extract(PCLZIP_OPT_PATH, $dir, PCLZIP_OPT_SET_CHMOD, 0777) == 0) {\r
210                 return $archive->errorInfo(true);\r
211         }\r
212         return '';\r
213 }\r
214 \r
215 function paint_plugin_download($url, $dir) {\r
216         if (!@ is_writable($dir) || !@ is_dir($dir)) {\r
217                 return _Paint_directoryNotWriteable . $dir;\r
218         }\r
219 \r
220         $remote = fopen($url, "rb");\r
221         socket_set_timeout($remote, 10);\r
222         if (!$remote)\r
223                 return _PAINT_fileOpen_failure . ': '.$url;\r
224 \r
225         $file = $dir.'/'.basename($url);\r
226         $local = fopen($file, "wb");\r
227         socket_set_timeout($local, 5);\r
228         if (!$local)\r
229                 return _PAINT_fileOpen_failure . ': '.$file;\r
230 \r
231         while (!feof($remote)) {\r
232                 $buffer = fgets($remote, 4096);\r
233                 fputs($local, $buffer);\r
234         }\r
235 \r
236         fclose($remote);\r
237         fclose($local);\r
238 \r
239         return '';\r
240 }\r