OSDN Git Service

tag: sqlite0904
[nucleus-jp/nucleus-plugins.git] / sqlite / tags / sqlite0904 / nucleus / plugins / NP_SQLite.php
1 <?php 
2    /***********************************
3    * NP_SQLite plugin  ver 0.8.0      *
4    *                                  *
5    * This plugin modifies the plugins *
6    *     when these are installed.    *
7    *                                  *
8    * Written by Katsumi               *
9    ***********************************/
10 //  The licence of this script is GPL
11 class NP_SQLite extends NucleusPlugin { 
12         function getName() { return 'NP_SQLite'; }
13         function getMinNucleusVersion() { return 320; }
14         function getAuthor()  { $this->install(); return 'Katsumi'; }
15         function getVersion() { global $SQLITECONF; return $SQLITECONF['VERSION']; }
16         function getURL() {return 'http://hp.vector.co.jp/authors/VA016157/';}
17         function getDescription() { return $this->showinfo(); } 
18         function supportsFeature($what) { return (int)($what=='SqlTablePrefix'); }
19         function getEventList() { return array('PreAddPlugin','PreSkinParse','QuickMenu'); }
20         function hasAdminArea() { return 1; }
21
22         function install(){
23                 if (!$this->getOption('allowsql')) {
24                         $this->createOption('allowsql',$this->translated('Allow SQL query during the management?'),'yesno','no');
25                 }
26                 if (!$this->getOption('numatonce')) {
27                         $this->createOption('numatonce',$this->translated('Number of SQL queries at once when DB restore.'),'text','20','datatype=numerical'); 
28                         $this->createOption('refreshwait',$this->translated('Wating tile (seconds) when DB restore.'),'text','1','datatype=numerical');
29                 }
30         }
31         
32         var $infostr;
33         function showinfo() {
34                 global $SQLITECONF;
35                 if ($this->infostr) echo "<b>".$this->infostr."</b><hr />\n";
36                 return $this->translated('NP_SQLite plugin. Filesize of DB is now ').filesize($SQLITECONF['DBFILENAME']).' bytes.';
37         }
38
39         function init(){
40         }
41
42         function event_QuickMenu(&$data){
43                 global $member;
44                 $this->_showDebugMessage();
45
46                 // only show to admins
47                 if (!($member->isLoggedIn() && $member->isAdmin())) return;
48
49                 array_push($data['options'], array(
50                                 'title' => 'SQLite',
51                                 'url' => $this->getAdminURL(),
52                                 'tooltip' => $this->translated('SQLite management')
53                         ) );
54         }
55         function event_PreSkinParse(&$data){
56                 $this->_showDebugMessage();
57         }
58         function _showDebugMessage(){
59                 global $SQLITECONF;
60                 if (isset($SQLITECONF['DEBUGMESSAGE'])) sqlite_DebugMessage();
61                 unset($SQLITECONF['DEBUGMESSAGE']);
62         }
63
64         function event_PreAddPlugin(&$data){
65                 // This event happens before loading "NP_XXXX.php" file.
66                 // Therefore, modification is possible here.
67                 $this->modify_plugin($data['file'],true);
68         }
69         function modify_plugin($pluginfile,$install=false){
70                 global $DIR_PLUGINS,$SQLITECONF;
71                 if ($SQLITECONF['OVERRIDEMODE']) return true;
72
73                 $admindir=$DIR_PLUGINS.strtolower(substr($pluginfile,3));
74                 $pluginfile=$DIR_PLUGINS.$pluginfile;
75                 
76                 // List up all PHP files.
77                 $phpfiles=array();
78                 array_push($phpfiles,$pluginfile.'.php');
79                 $this->seekPhpFiles($admindir,$phpfiles);
80                 
81                 // Modify the PHP files.
82                 $allok=true;
83                 foreach ($phpfiles as $file) {
84                         if (!$this->changeFunctions($file)) {
85                                 $allok=false;
86                         }
87                 }
88                 if ($allok) {
89                         if ($install) $this->infostr=$this->translated('Pluing was installed sucessfully');
90                         return true;
91                 }
92                 
93                 if (!$install) return false;
94                 if ($this->is_japanese()) {
95                         echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" /></head>';
96                         echo '<body><h3>PHP ¥Õ¥¡¥¤¥ë¤ò¼êÆ°¤ÇÊѹ¹¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£</h3>';
97                 } else echo '<html><body><h3>Need to modify PHP file(s) manually.</h3>';
98                 foreach ($phpfiles as $file) echo $this->show_Lines($file);
99                 exit ('</body></html>');
100         }
101
102         function seekPhpFiles($dir,&$phpfiles){
103                 if (!is_dir($dir)) return;
104                 $d = dir($dir);
105                 $dirpath=realpath($d->path);
106                 $dirs=array();
107                 if (substr($dirpath,-1)!='/' && substr($dirpath,-1)!="\\") $dirpath.='/';
108                 while (false !== ($entry = $d->read())) {
109                         if ($entry=='.' || $entry=='..') continue;
110                         if (is_file($dirpath.$entry) && substr($entry,-4)=='.php') array_push($phpfiles,realpath($dirpath.$entry));
111                         if (is_dir($dirpath.$entry))  array_push($dirs,realpath($dirpath.$entry));
112                 }
113                 $d->close();
114                 foreach($dirs as $dir) $this->seekPhpFiles($dir,$phpfiles);
115         }
116         function changeFunctions($file){
117                 if (!is_file($file=realpath($file))) return false;
118                 if (!is_readable($file)) return false;
119                 $before=$this->read_from_file($file);
120                 // Do this process until change does not occur.. 
121                 // Otherwise, sometime file is not completely modified.
122                 $after=$this->do_replace($before);
123                 if ($before!=$after) return $this->write_to_file($file,$after);
124                 return true;
125         }
126         function do_replace(&$text) {
127                 // Do this process until change does not occur.. 
128                 // Otherwise, sometime file is not completely modified.
129                 $after=$text;
130                 do $after=preg_replace('/([^_])mysql_([_a-z]+)([\s]*?)\(/','$1nucleus_mysql_$2(',($before=$after));
131                 while ($before!=$after);
132                 return $after;
133         }
134         function show_Lines($file){
135                 if (!is_file($file=realpath($file))) return '';
136                 if (!is_readable($file)) return '';
137                 $result='';
138                 $lines=file($file);
139                 $firsttime=true;
140                 foreach($lines as $num=>$before) {
141                         $after=$this->do_replace($before);
142                         if ($after!=$before) {
143                                 if ($firsttime) $result.="<hr />\nFile: <b>$file</b><br /><br />\n";
144                                 $firsttime=false;
145                                 $result.="Modify line <b>".($num+1)."</b> like: &quot;\n";
146                                 $result.=str_replace('nucleus_mysql_','<font color="#ff0000">nucleus_</font>mysql_',htmlspecialchars($after))."&quot;<br /><br />\n";
147                         }
148                 }
149                 return $result;
150         }
151         function read_from_file($file) {
152                 if (function_exists('file_get_contents') ) $ret=file_get_contents($file);
153                 else {
154                         ob_start();
155                         readfile($file);
156                         $ret=ob_get_contents();
157                         ob_end_clean();
158                 }
159                 return $ret;
160         }
161         function write_to_file($file,&$text){
162                 if (!$handle = @fopen($file, 'w')) return false;
163                 fwrite($handle,$text);
164                 fclose($handle);
165                 return true;
166         }
167         function is_japanese(){
168                 $language = str_replace( array('\\','/'), array('',''), getLanguageName());
169                 return (strpos($language,'japanese')===0);
170         }
171
172         // Language stuff
173         var $langArray;
174         function translated($english){
175                 if (!is_array($this->langArray)) {
176                         $this->langArray=array();
177                         $language=$this->getDirectory().'language/'.str_replace( array('\\','/'), array('',''), getLanguageName()).'.php';
178                         if (file_exists($language)) include($language);
179                 }
180                 if (!($ret=$this->langArray[$english])) $ret=$english;
181                 return $ret;
182         }
183
184 }
185 ?>