OSDN Git Service

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