OSDN Git Service

b096da01afb7a0bc7c804a917464a6e4286f4c59
[nucleus-jp/nucleus-next.git] / nucleus / libs / PLUGINADMIN.php
1 <?php\r
2 \r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2012 The Nucleus Group\r
6  *\r
7  * This program is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License\r
9  * as published by the Free Software Foundation; either version 2\r
10  * of the License, or (at your option) any later version.\r
11  * (see nucleus/documentation/index.html#license for more info)\r
12  */\r
13 /**\r
14  * code to make it easier to create plugin admin areas\r
15  *\r
16  * @license http://nucleuscms.org/license.txt GNU General Public License\r
17  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
18  * @version $Id: PLUGINADMIN.php 1626 2012-01-09 15:46:54Z sakamocchi $\r
19  */\r
20 \r
21 class PluginAdmin\r
22 {\r
23         public $strFullName;    // NP_SomeThing\r
24         public $plugin;         // ref. to plugin object\r
25         public $bValid;         // evaluates to true when object is considered valid\r
26         public $admin;                  // ref to an admin object\r
27 \r
28         private $skinContents;  // PluginAdmin contents\r
29         private $extrahead;             // extrahead\r
30 \r
31         public function __construct($pluginName)\r
32         {\r
33                 global $manager, $DIR_LIBS;\r
34                 \r
35                 if ( !class_exists('Admin', FALSE) )\r
36                 {\r
37                         include($DIR_LIBS . 'ADMIN.php');\r
38                 }\r
39                 \r
40                 $this->strFullName = "NP_{$pluginName}";\r
41                 \r
42                 // check if plugin exists and is installed\r
43                 if ( !$manager->pluginInstalled($this->strFullName) )\r
44                 {\r
45                         doError(_ERROR_INVALID_PLUGIN);\r
46                         return;\r
47                 }\r
48                 \r
49                 $this->plugin = &$manager->getPlugin($this->strFullName);\r
50                 $this->bValid =  $this->plugin;\r
51                 \r
52                 if ( !$this->bValid )\r
53                 {\r
54                         doError(_ERROR_INVALID_PLUGIN);\r
55                         return;\r
56                 }\r
57                 \r
58                 Admin::initialize();\r
59                 Admin::$action = "plugin_{$pluginName}";\r
60                 \r
61                 return;\r
62         }\r
63         \r
64         /**\r
65          * PluginAdmin::start()\r
66          * \r
67          * @param       string  $extraHead      child elements for header element\r
68          * @return      void\r
69          */\r
70         public function start($extraHead = '')\r
71         {\r
72                 global $CONF;\r
73                 $this->extrahead = $extraHead . '<base href="' . Entity::hsc($CONF['AdminURL']) . '" />' . "\n";\r
74                 ob_start();\r
75                 return;\r
76         }\r
77         \r
78         /**\r
79          * PluginAdmin::end()\r
80          * \r
81          * @param       void\r
82          * @return      void\r
83          */\r
84         public function end()\r
85         {\r
86                 $this->AddTicketByJS();\r
87                 $contents = ob_get_contents();\r
88                 ob_end_clean();\r
89                 $this->skinContents = '<%pagehead%>' . $contents . '<%pagefoot%>';\r
90                 Admin::action_PluginAdmin($this->skinContents, $this->extrahead);\r
91                 return;\r
92         }\r
93         \r
94         /**\r
95          * PluginAdmin::_AddTicketByJS()\r
96          * Add ticket when not used in plugin's admin page\r
97          * to avoid CSRF.\r
98          * \r
99          * @param       void\r
100          * @return      void\r
101          */\r
102         private function AddTicketByJS()\r
103         {\r
104                 global $CONF,$ticketforplugin;\r
105                 if ( !($ticket = $ticketforplugin['ticket']) )\r
106                 {\r
107                         return;\r
108                 }\r
109                 $ticket=Entity::hsc($ticket);\r
110 \r
111 ?><script type="text/javascript">\r
112 /*<![CDATA[*/\r
113 /* Add tickets for available links (outside blog excluded) */\r
114 for (i=0;document.links[i];i++){\r
115   if (document.links[i].href.indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0\r
116     && !(document.links[i].href.indexOf('//',0)<0)) continue;\r
117   if ((j=document.links[i].href.indexOf('?',0))<0) continue;\r
118   if (document.links[i].href.indexOf('ticket=',j)>=0) continue;\r
119   document.links[i].href=document.links[i].href.substring(0,j+1)+'ticket=<?php echo $ticket; ?>&'+document.links[i].href.substring(j+1);\r
120 }\r
121 /* Add tickets for forms (outside blog excluded) */\r
122 for (i=0;document.forms[i];i++){\r
123   /* check if ticket is already used */\r
124   for (j=0;document.forms[i].elements[j];j++) {\r
125     if (document.forms[i].elements[j].name=='ticket') {\r
126       j=-1;\r
127       break;\r
128     }\r
129   }\r
130   if (j==-1) continue;\r
131  \r
132   /* check if the modification works */\r
133   try{document.forms[i].innerHTML+='';}catch(e){\r
134     /* Modificaion falied: this sometime happens on IE */\r
135     if (!document.forms[i].action.name && document.forms[i].method.toUpperCase()=="POST") {\r
136       /* <input name="action"/> is not used for POST method*/\r
137       if (document.forms[i].action.indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0\r
138         && !(document.forms[i].action.indexOf('//',0)<0)) continue;\r
139       if (0<(j=document.forms[i].action.indexOf('?',0))) if (0<document.forms[i].action.indexOf('ticket=',j)) continue;\r
140       if (j<0) document.forms[i].action+='?'+'ticket=<?php echo $ticket; ?>';\r
141       else document.forms[i].action+='&'+'ticket=<?php echo $ticket; ?>';\r
142       continue;\r
143     }\r
144     document.write('<?php echo _PLUGINADMIN_TICKETS_JAVASCRIPT ?>');\r
145     j=document.forms[i].outerHTML;\r
146     while (j!=j.replace('<','&lt;')) j=j.replace('<','&lt;');\r
147     document.write('<p>'+j+'</p>');\r
148     continue;\r
149   }\r
150   /* check the action paramer in form tag */\r
151   /* note that <input name="action"/> may be used here */\r
152   j=document.forms[i].innerHTML;\r
153   document.forms[i].innerHTML='';\r
154   if ((document.forms[i].action+'').indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0\r
155       && !((document.forms[i].action+'').indexOf('//',0)<0)) {\r
156     document.forms[i].innerHTML=j;\r
157     continue;\r
158   }\r
159   /* add ticket */\r
160   document.forms[i].innerHTML=j+'<input type="hidden" name="ticket" value="<?php echo $ticket; ?>"/>';\r
161 }\r
162 /*]]>*/\r
163 </script><?php\r
164         return;\r
165         }\r
166 }\r
167 \r