OSDN Git Service

e1fce523e578bd96f3cdb6e5f676cf76993535f4
[nucleus-jp/nucleus-next.git] / nucleus / libs / PLUGINADMIN.php
1 <<<<<<< HEAD
2 <?php\r
3 \r
4 /*\r
5  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
6  * Copyright (C) 2002-2012 The Nucleus Group\r
7  *\r
8  * This program is free software; you can redistribute it and/or\r
9  * modify it under the terms of the GNU General Public License\r
10  * as published by the Free Software Foundation; either version 2\r
11  * of the License, or (at your option) any later version.\r
12  * (see nucleus/documentation/index.html#license for more info)\r
13  */\r
14 /**\r
15  * code to make it easier to create plugin admin areas\r
16  *\r
17  * @license http://nucleuscms.org/license.txt GNU General Public License\r
18  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
19  * @version $Id: PLUGINADMIN.php 1626 2012-01-09 15:46:54Z sakamocchi $\r
20  */\r
21 \r
22 class PluginAdmin\r
23 {\r
24         public $strFullName;            // NP_SomeThing\r
25         public $plugin;                 // ref. to plugin object\r
26         public $bValid;                 // evaluates to true when object is considered valid\r
27         public $admin;                          // ref to an admin object\r
28         \r
29         public function __construct($pluginName)\r
30         {\r
31                 global $manager, $DIR_LIBS;\r
32                 \r
33                 if ( !class_exists('Admin', FALSE) )\r
34                 {\r
35                         include($DIR_LIBS . 'ADMIN.php');\r
36                 }\r
37                 \r
38                 $this->strFullName = "NP_{$pluginName}";\r
39                 \r
40                 // check if plugin exists and is installed\r
41                 if ( !$manager->pluginInstalled($this->strFullName) )\r
42                 {\r
43                         doError(_ERROR_INVALID_PLUGIN);\r
44                 }\r
45                 \r
46                 $this->plugin =& $manager->getPlugin($this->strFullName);\r
47                 $this->bValid = $this->plugin;\r
48                 \r
49                 if ( !$this->bValid )\r
50                 {\r
51                         doError(_ERROR_INVALID_PLUGIN);\r
52                 }\r
53                 \r
54                 $this->admin = new Admin();\r
55                 $this->admin->action = "plugin_{$pluginName}";\r
56                 return;\r
57         }\r
58         \r
59         /**\r
60          * PluginAdmin::start()\r
61          * \r
62          * @param       string  $extraHead      child elements for header element\r
63          * @return      void\r
64          */\r
65         public function start($extraHead = '')\r
66         {\r
67                 global $CONF;\r
68                 $strBaseHref  = '<base href="' . Entity::hsc($CONF['AdminURL']) . '" />';\r
69                 $extraHead .= $strBaseHref;\r
70                 \r
71                 $this->admin->pagehead($extraHead);\r
72                 return;\r
73         }\r
74         \r
75         /**\r
76          * PluginAdmin::end()\r
77          * \r
78          * @param       void\r
79          * @return      void\r
80          */\r
81         public function end()\r
82         {\r
83                 $this->_AddTicketByJS();\r
84                 $this->admin->pagefoot();\r
85                 return;\r
86         }\r
87         \r
88         /**\r
89          * PluginAdmin::_AddTicketByJS()\r
90          * Add ticket when not used in plugin's admin page\r
91          * to avoid CSRF.\r
92          * \r
93          * @param       void\r
94          * @return      void\r
95          */\r
96         public function _AddTicketByJS()\r
97         {\r
98                 global $CONF,$ticketforplugin;\r
99                 if ( !($ticket=$ticketforplugin['ticket']) ) \r
100                 {\r
101                         return;\r
102                 }\r
103                 $ticket=Entity::hsc($ticket);\r
104 \r
105 ?><script type="text/javascript">\r
106 /*<![CDATA[*/\r
107 /* Add tickets for available links (outside blog excluded) */\r
108 for (i=0;document.links[i];i++){\r
109   if (document.links[i].href.indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0\r
110     && !(document.links[i].href.indexOf('//',0)<0)) continue;\r
111   if ((j=document.links[i].href.indexOf('?',0))<0) continue;\r
112   if (document.links[i].href.indexOf('ticket=',j)>=0) continue;\r
113   document.links[i].href=document.links[i].href.substring(0,j+1)+'ticket=<?php echo $ticket; ?>&'+document.links[i].href.substring(j+1);\r
114 }\r
115 /* Add tickets for forms (outside blog excluded) */\r
116 for (i=0;document.forms[i];i++){\r
117   /* check if ticket is already used */\r
118   for (j=0;document.forms[i].elements[j];j++) {\r
119     if (document.forms[i].elements[j].name=='ticket') {\r
120       j=-1;\r
121       break;\r
122     }\r
123   }\r
124   if (j==-1) continue;\r
125  \r
126   /* check if the modification works */\r
127   try{document.forms[i].innerHTML+='';}catch(e){\r
128     /* Modificaion falied: this sometime happens on IE */\r
129     if (!document.forms[i].action.name && document.forms[i].method.toUpperCase()=="POST") {\r
130       /* <input name="action"/> is not used for POST method*/\r
131       if (document.forms[i].action.indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0\r
132         && !(document.forms[i].action.indexOf('//',0)<0)) continue;\r
133       if (0<(j=document.forms[i].action.indexOf('?',0))) if (0<document.forms[i].action.indexOf('ticket=',j)) continue;\r
134       if (j<0) document.forms[i].action+='?'+'ticket=<?php echo $ticket; ?>';\r
135       else document.forms[i].action+='&'+'ticket=<?php echo $ticket; ?>';\r
136       continue;\r
137     }\r
138     document.write('<?php echo _PLUGINADMIN_TICKETS_JAVASCRIPT ?>');\r
139     j=document.forms[i].outerHTML;\r
140     while (j!=j.replace('<','&lt;')) j=j.replace('<','&lt;');\r
141     document.write('<p>'+j+'</p>');\r
142     continue;\r
143   }\r
144   /* check the action paramer in form tag */\r
145   /* note that <input name="action"/> may be used here */\r
146   j=document.forms[i].innerHTML;\r
147   document.forms[i].innerHTML='';\r
148   if ((document.forms[i].action+'').indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0\r
149       && !((document.forms[i].action+'').indexOf('//',0)<0)) {\r
150     document.forms[i].innerHTML=j;\r
151     continue;\r
152   }\r
153   /* add ticket */\r
154   document.forms[i].innerHTML=j+'<input type="hidden" name="ticket" value="<?php echo $ticket; ?>"/>';\r
155 }\r
156 /*]]>*/\r
157 </script><?php\r
158         return;\r
159         }\r
160 }\r
161 \r
162 =======
163 <?php
164
165 /*
166  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
167  * Copyright (C) 2002-2012 The Nucleus Group
168  *
169  * This program is free software; you can redistribute it and/or
170  * modify it under the terms of the GNU General Public License
171  * as published by the Free Software Foundation; either version 2
172  * of the License, or (at your option) any later version.
173  * (see nucleus/documentation/index.html#license for more info)
174  */
175 /**
176  * code to make it easier to create plugin admin areas
177  *
178  * @license http://nucleuscms.org/license.txt GNU General Public License
179  * @copyright Copyright (C) 2002-2012 The Nucleus Group
180  * @version $Id: PLUGINADMIN.php 1886 2012-06-17 08:27:27Z sakamocchi $
181  */
182
183 class PluginAdmin
184 {
185         public $strFullName;    // NP_SomeThing
186         public $plugin;         // ref. to plugin object
187         public $bValid;         // evaluates to true when object is considered valid
188         public $admin;                  // ref to an admin object
189
190         private $skinContents;  // PluginAdmin contents
191         private $extrahead;             // extrahead
192
193         public function __construct($pluginName)
194         {
195                 global $manager, $DIR_LIBS;
196                 
197                 if ( !class_exists('Admin', FALSE) )
198                 {
199                         include($DIR_LIBS . 'ADMIN.php');
200                 }
201                 
202                 $this->strFullName = "NP_{$pluginName}";
203                 
204                 // check if plugin exists and is installed
205                 if ( !$manager->pluginInstalled($this->strFullName) )
206                 {
207                         doError(_ERROR_INVALID_PLUGIN);
208                         return;
209                 }
210                 
211                 $this->plugin = &$manager->getPlugin($this->strFullName);
212                 $this->bValid =  $this->plugin;
213                 
214                 if ( !$this->bValid )
215                 {
216                         doError(_ERROR_INVALID_PLUGIN);
217                         return;
218                 }
219                 
220                 Admin::initialize();
221                 Admin::$action = "plugin_{$pluginName}";
222                 
223                 return;
224         }
225         
226         /**
227          * PluginAdmin::start()
228          * 
229          * @param       string  $extraHead      child elements for header element
230          * @return      void
231          */
232         public function start($extraHead = '')
233         {
234                 global $CONF;
235                 $this->extrahead = $extraHead . '<base href="' . Entity::hsc($CONF['AdminURL']) . '" />' . "\n";
236                 ob_start();
237                 return;
238         }
239         
240         /**
241          * PluginAdmin::end()
242          * 
243          * @param       void
244          * @return      void
245          */
246         public function end()
247         {
248                 $this->AddTicketByJS();
249                 $contents = ob_get_contents();
250                 ob_end_clean();
251                 $this->skinContents = '<%pagehead%>' . $contents . '<%pagefoot%>';
252                 Admin::action_PluginAdmin($this->skinContents, $this->extrahead);
253                 return;
254         }
255         
256         /**
257          * PluginAdmin::_AddTicketByJS()
258          * Add ticket when not used in plugin's admin page
259          * to avoid CSRF.
260          * 
261          * @param       void
262          * @return      void
263          */
264         private function AddTicketByJS()
265         {
266                 global $CONF,$ticketforplugin;
267                 if ( !($ticket = $ticketforplugin['ticket']) )
268                 {
269                         return;
270                 }
271                 $ticket=Entity::hsc($ticket);
272
273 ?><script type="text/javascript">
274 /*<![CDATA[*/
275 /* Add tickets for available links (outside blog excluded) */
276 for (i=0;document.links[i];i++){
277   if (document.links[i].href.indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0
278     && !(document.links[i].href.indexOf('//',0)<0)) continue;
279   if ((j=document.links[i].href.indexOf('?',0))<0) continue;
280   if (document.links[i].href.indexOf('ticket=',j)>=0) continue;
281   document.links[i].href=document.links[i].href.substring(0,j+1)+'ticket=<?php echo $ticket; ?>&'+document.links[i].href.substring(j+1);
282 }
283 /* Add tickets for forms (outside blog excluded) */
284 for (i=0;document.forms[i];i++){
285   /* check if ticket is already used */
286   for (j=0;document.forms[i].elements[j];j++) {
287     if (document.forms[i].elements[j].name=='ticket') {
288       j=-1;
289       break;
290     }
291   }
292   if (j==-1) continue;
293  
294   /* check if the modification works */
295   try{document.forms[i].innerHTML+='';}catch(e){
296     /* Modificaion falied: this sometime happens on IE */
297     if (!document.forms[i].action.name && document.forms[i].method.toUpperCase()=="POST") {
298       /* <input name="action"/> is not used for POST method*/
299       if (document.forms[i].action.indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0
300         && !(document.forms[i].action.indexOf('//',0)<0)) continue;
301       if (0<(j=document.forms[i].action.indexOf('?',0))) if (0<document.forms[i].action.indexOf('ticket=',j)) continue;
302       if (j<0) document.forms[i].action+='?'+'ticket=<?php echo $ticket; ?>';
303       else document.forms[i].action+='&'+'ticket=<?php echo $ticket; ?>';
304       continue;
305     }
306     document.write('<?php echo _PLUGINADMIN_TICKETS_JAVASCRIPT ?>');
307     j=document.forms[i].outerHTML;
308     while (j!=j.replace('<','&lt;')) j=j.replace('<','&lt;');
309     document.write('<p>'+j+'</p>');
310     continue;
311   }
312   /* check the action paramer in form tag */
313   /* note that <input name="action"/> may be used here */
314   j=document.forms[i].innerHTML;
315   document.forms[i].innerHTML='';
316   if ((document.forms[i].action+'').indexOf('<?php echo $CONF['PluginURL']; ?>',0)<0
317       && !((document.forms[i].action+'').indexOf('//',0)<0)) {
318     document.forms[i].innerHTML=j;
319     continue;
320   }
321   /* add ticket */
322   document.forms[i].innerHTML=j+'<input type="hidden" name="ticket" value="<?php echo $ticket; ?>"/>';
323 }
324 /*]]>*/
325 </script><?php
326         return;
327         }
328 }
329
330 >>>>>>> skinnable-master