OSDN Git Service

ver 0.8.1
[nucleus-jp/nucleus-plugins.git] / trunk / sqlite / nucleus / sqlite / sqlite.php
1 <?php
2     /***************************************
3     * SQLite-MySQL wrapper for Nucleus     *
4     *                           ver 0.8.1  *
5     * Written by Katsumi                   *
6     ***************************************/
7 //
8 //  The licence of this script is GPL
9 //
10 //                ACKOWLEDGMENT
11 //
12 //  I thank all the people of Nucleus JP forum 
13 //  who discussed this project. Especially, I 
14 //  thank kosugiatkips, mekyo, and nakahara21 
15 //  for ideas of some part of code.
16 //  I also thank Jon Jensen for his generous
17 //  acceptance for using his PHP code in this
18 //  script.
19 //
20 //  The features that are supported by this script but not
21 //  generally by SQLite are as follows:
22 //
23 //  CREATE TABLE IF NOT EXISTS, auto_increment,
24 //  DROP TABLE IF EXISTS, ALTER TABLE, 
25 //  INSERT INTO ... SET xx=xx, xx=xx,
26 //  REPLACE INTO ... SET xx=xx, xx=xx,
27 //  SHOW KEYS FROM, SHOW INDEX FROM,
28 //  SHOW FIELDS FROM, SHOW COLUMNS FROM,
29 //  CREATE TABLE ... KEYS xxx (xxx,xxx)
30 //  SHOW TABLES LIKE, TRUNCATE TABLE
31 //  SHOW TABLES
32 //
33 // Release note:
34 //  Version 0.8.0
35 //    -This is the first established version and
36 //     exactly the same as ver 0.7.8b.
37 //
38 //  Version 0.8.1
39 //    -Execute "PRAGMA short_column_names=1" first.
40 //    -Avoid loading outside php file in some specfic environment.
41 //    -Avoid executing multiple queries using ";" as delimer.
42
43 global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_SESSION_VARS;
44 $aVarsToCheck = array('HTTP_GET_VARS', 'HTTP_POST_VARS', 'HTTP_COOKIE_VARS', 'HTTP_ENV_VARS', 'HTTP_SESSION_VARS', 'HTTP_POST_FILES', 'HTTP_SERVER_VARS', 'GLOBALS', 'argv', 'argc', '_GET', '_POST', '_COOKIE', '_ENV', '_SESSION', '_SERVER', '_FILES', 'DIR_LIBS');
45
46 foreach ($aVarsToCheck as $varName)
47 {
48         if (phpversion() >= '4.1.0')
49         {
50                 if (   isset($_GET[$varName])
51                         || isset($_POST[$varName])
52                         || isset($_COOKIE[$varName])
53                         || isset($_ENV[$varName])
54                         || isset($_SESSION[$varName])
55                         || isset($_FILES[$varName])
56                 ){
57                         die('Sorry. An error occurred.');
58                 }
59         } else {
60                 if (   isset($HTTP_GET_VARS[$varName])
61                         || isset($HTTP_POST_VARS[$varName])
62                         || isset($HTTP_COOKIE_VARS[$varName])
63                         || isset($HTTP_ENV_VARS[$varName])
64                         || isset($HTTP_SESSION_VARS[$varName])
65                         || isset($HTTP_POST_FILES[$varName])
66                 ){
67                         die('Sorry. An error occurred.');
68                 }
69         }
70 }
71
72 // Initializiation stuff
73 if (!isset($DIR_NUCLEUS)) $DIR_NUCLEUS=realpath('nucleus/');
74 if (substr($DIR_NUCLEUS,-1)!='/') $DIR_NUCLEUS.='/';
75 if (!file_exists($DIR_NUCLEUS.'sqlite/sqliteconfig.php')) exit;
76 include ($DIR_NUCLEUS.'sqlite/sqliteconfig.php');
77 $SQLITE_DBHANDLE=sqlite_open($SQLITECONF['DBFILENAME']);
78 if (!file_exists($DIR_NUCLEUS.'sqlite/sqlitequeryfunctions.php')) exit;
79 include ($DIR_NUCLEUS.'sqlite/sqlitequeryfunctions.php');
80 $SQLITECONF['VERSION']='0.8.1';
81
82 function sqlite_createQueryFunction($queryf,$globalf){
83         global $SQLITE_DBHANDLE;
84         if (function_exists($globalf)) sqlite_create_function($SQLITE_DBHANDLE,$queryf,$globalf);
85 }
86
87 //Following thing may work if MySQL is NOT installed in server.
88 if (!function_exists('mysql_query')) {
89         define ("MYSQL_ASSOC", SQLITE_ASSOC);
90         define ("MYSQL_BOTH", SQLITE_BOTH);
91         define ("MYSQL_NUM", SQLITE_NUM);
92         function mysql_connect(){
93                 global $SQLITECONF;
94                 $SQLITECONF['OVERRIDEMODE']=true;
95                 $args=func_get_args();
96                 return call_user_func_array('nucleus_mysql_connect',$args);
97         }
98         foreach (array('mysql_affected_rows','mysql_change_user','mysql_client_encoding','mysql_close',
99                 'mysql_create_db','mysql_data_seek','mysql_db_name','mysql_db_query','mysql_drop_db','mysql_errno',
100                 'mysql_error','mysql_escape_string','mysql_fetch_array','mysql_fetch_assoc','mysql_fetch_field','mysql_fetch_lengths',
101                 'mysql_fetch_object','mysql_fetch_row','mysql_field_flags','mysql_field_len','mysql_field_name','mysql_field_seek',
102                 'mysql_field_table','mysql_field_type','mysql_free_result','mysql_get_client_info','mysql_get_host_info',
103                 'mysql_get_proto_info','mysql_get_server_info','mysql_info','mysql_insert_id','mysql_list_dbs',
104                 'mysql_list_fields','mysql_list_processes','mysql_list_tables','mysql_num_fields','mysql_num_rows','mysql_numrows',
105                 'mysql_pconnect','mysql_ping','mysql_query','mysql_real_escape_string','mysql_result','mysql_select_db',
106                 'mysql_stat','mysql_tablename','mysql_thread_id','mysql_unbuffered_query')
107                  as $value) eval(
108                 "function $value(){\n".
109                 "  \$args=func_get_args();\n".
110                 "  return call_user_func_array('nucleus_$value',\$args);\n".
111                 "}\n");
112 }
113
114 // Empty object for mysql_fetch_object().
115 class SQLITE_OBJECT {}
116
117 function sqlite_ReturnWithError($text='Not supported',$more=''){
118         // Show warning when error_reporting() is set.
119         if (!(error_reporting() & E_WARNING)) return false;
120         
121         // Seek the file and line that originally called sql function.
122         $a=debug_backtrace();
123         foreach($a as $key=>$btrace) {
124                 if (!($templine=$btrace['line'])) continue;
125                 if (!($tempfile=$btrace['file'])) continue;
126                 $file=str_replace("\\",'/',$file);
127                 if (!$line && !$file && strpos($tempfile,'/sqlite.php')===false && strpos($tempfile,'/sqlitequeryfunctions.php')===false) {
128                         $line=$templine;
129                         $file=$tempfile;
130                 }
131                 echo "\n<!--$tempfile line:$templine-->\n";
132         }
133         echo "Warning from SQLite-MySQL wrapper: $text<br />\n";
134         if ($line && $file) echo "in <b>$file</b> on line <b>$line</b><br />\n";
135         echo $more;
136         return false;
137 }
138 function sqlite_DebugMessage($text=''){
139         global $SQLITECONF;
140         if (!$SQLITECONF['DEBUGREPORT']) return;
141         if ($text) $SQLITECONF['DEBUGMESSAGE'].="\n".$text."\n";
142         if (headers_sent()) {
143                 echo '<!--sqlite_DebugMessage'.$SQLITECONF['DEBUGMESSAGE'].'sqlite_DebugMessage-->';
144                 unset($SQLITECONF['DEBUGMESSAGE']);
145         }
146 }
147
148 // nucleus_mysql_XXXX() functions follow.
149 // These functions will be defined only when SQLite is installed in server.
150 if (function_exists('sqlite_open')) {
151
152 function nucleus_mysql_connect($p1=null,$p2=null,$p3=null,$p4=null,$p5=null){
153         // All prameters are ignored.
154         global $SQLITE_DBHANDLE,$SQLITECONF;
155         if (!$SQLITE_DBHANDLE) $SQLITE_DBHANDLE=sqlite_open($SQLITECONF['DBFILENAME']);
156         // Initialization queries.
157         foreach($SQLITECONF['INITIALIZE'] as $value) nucleus_mysql_query($value);
158         return $SQLITE_DBHANDLE;
159 }
160
161 function nucleus_mysql_close($p1=null){
162         global $SQLITE_DBHANDLE;
163         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
164         $SQLITE_DBHANDLE='';
165         return sqlite_close ($dbhandle);
166 }
167
168 function nucleus_mysql_select_db($p1,$p2=null){
169         // SQLite does not support multiple databases in a file.
170         // So this function do nothing and always returns true.
171         // Note: mysql_select_db() function returns true/false,
172         // not link-ID.
173         return true;
174 }
175
176 function nucleus_mysql_query($p1,$p2=null,$unbuffered=false){//echo htmlspecialchars($p1)."<br />\n";
177         global $SQLITE_DBHANDLE,$SQLITECONF;
178         if (!($dbhandle=$p2)) $dbhandle=$SQLITE_DBHANDLE;
179         $query=trim($p1);
180         if (strpos($query,"\xEF\xBB\xBF")===0) $query=substr($query,3);// UTF-8 stuff
181         if (substr($query,-1)==';') $query=substr($query,0,strlen($query)-1);
182         
183         // Escape style is changed from MySQL type to SQLite type here.
184         // This is important to avoid possible SQL-injection.
185         $strpositions=array();// contains the data show where the strings are (startposition => endposition)
186         if (strpos($query,'`')!==false || strpos($query,'"')!==false || strpos($query,"'")!==false)
187                 $strpositions=sqlite_changeQuote($query);
188         //echo "<br />".htmlspecialchars($p1)."<br /><br />\n".htmlspecialchars($query)."<hr />\n";
189
190         // Debug mode
191         if ($SQLITECONF['DEBUGMODE']) $query=sqlite_mysql_query_debug($query);
192         
193         // Anyway try it.
194         if ($unbuffered) {
195                 if ($ret=@sqlite_unbuffered_query($dbhandle,$query)) return $ret;
196         } else {
197                 if ($ret=@sqlite_query($dbhandle,$query)) return $ret;
198         }
199         
200         // Error occured. Query must be translated.
201         return sqlite_mysql_query_sub($dbhandle,$query,$strpositions,$p1,$unbuffered);
202 }
203 function sqlite_mysql_query_sub($dbhandle,$query,$strpositions=array(),$p1=null,$unbuffered=false){//echo htmlspecialchars($p1)."<br />\n";
204         // Query translation is needed, especially when changing the data in database.
205         // So far, this routine is written for 'CREATE TABLE','DROP TABLE', 'INSERT INTO',
206         // 'SHOW TABLES LIKE', 'SHOW KEYS FROM', 'SHOW INDEX FROM'
207         // and several functions used in query.
208         // How about 'UPDATE' ???
209         global $SQLITE_DBHANDLE,$SQLITECONF;
210         $beforetrans=time()+microtime();
211         if (!$p1) $p1=$query;
212         $morequeries=array();
213         $uquery=strtoupper($query);
214         if (strpos($uquery,'CREATE TABLE')===0 || ($temptable=(strpos($uquery,'CREATE TEMPORARY TABLE')===0))) {
215                 if (!($i=strpos($query,'('))) return sqlite_ReturnWithError('nucleus_mysql_query: '.$p1);
216                 //check if the command is 'CREATE TABLE IF NOT EXISTS'
217                 if (strpos(strtoupper($uquery),'CREATE TABLE IF NOT EXISTS')===0) {
218                         $tablename=trim(substr($query,26,$i-26));
219                         if (substr($tablename,0,1)!="'") $tablename="'$tablename'";
220                         $res=sqlite_query($dbhandle,"SELECT tbl_name FROM sqlite_master WHERE tbl_name=$tablename LIMIT 1");
221                         if (nucleus_mysql_num_rows($res)) return true;
222                 } else {
223                         $tablename=trim(substr($query,12,$i-12));
224                         if (substr($tablename,0,1)!="'") $tablename="'$tablename'";
225                 }
226                 
227                 $query=trim(substr($query,$i+1));
228                 for ($i=strlen($query);0<$i;$i--) if ($query[$i]==')') break;
229                 $query=substr($query,0,$i);
230                 $auto_increment=false;
231                 $commands=sqlite_splitByComma($query);
232                 $query=' (';
233                 $first=true;
234                 foreach($commands as $key => $value) {
235                         if (strpos(strtolower($value),'auto_increment')==strlen($value)-14) $auto_increment=true;
236                         $isint=preg_match('/int\(([0-9]*?)\)/i',$value);
237                         $isint=$isint | preg_match('/tinyint\(([0-9]*?)\)/i',$value);
238                         $value=preg_replace('/int\(([0-9]*?)\)[\s]+unsigned/i','int($1)',$value);
239                         $value=preg_replace('/int\([0-9]*?\)[\s]+NOT NULL[\s]+auto_increment$/i',' INTEGER NOT NULL PRIMARY KEY',$value);
240                         $value=preg_replace('/int\([0-9]*?\)[\s]+auto_increment$/i',' INTEGER PRIMARY KEY',$value);
241                         if ($auto_increment) $value=preg_replace('/^PRIMARY KEY(.*?)$/i','',$value);
242                         while (preg_match('/PRIMARY KEY[\s]*\((.*)\([0-9]+\)(.*)\)/i',$value)) // Remove '(100)' from 'PRIMARY KEY (`xxx` (100))'
243                                 $value=preg_replace('/PRIMARY KEY[\s]*\((.*)\([0-9]+\)(.*)\)/i','PRIMARY KEY ($1 $2)',$value);
244                         
245                         // CREATE KEY queries for SQLite (corresponds to KEY 'xxxx'('xxxx', ...) of MySQL
246                         if (preg_match('/^FULLTEXT KEY(.*?)$/i',$value,$matches)) {
247                                 array_push($morequeries,'CREATE INDEX '.str_replace('('," ON $tablename (",$matches[1]));
248                                 $value='';
249                         } else if (preg_match('/^UNIQUE KEY(.*?)$/i',$value,$matches)) {
250                                 array_push($morequeries,'CREATE UNIQUE INDEX '.str_replace('('," ON $tablename (",$matches[1]));
251                                 $value='';
252                         } else if (preg_match('/^KEY(.*?)$/i',$value,$matches)) {
253                                 array_push($morequeries,'CREATE INDEX '.str_replace('('," ON $tablename (",$matches[1]));
254                                 $value='';
255                         }
256                         
257                         // Check if 'DEFAULT' is set when 'NOT NULL'
258                         $uvalue=strtoupper($value);
259                         if (strpos($uvalue,'NOT NULL')!==false && 
260                                         strpos($uvalue,'DEFAULT')===false &&
261                                         strpos($uvalue,'INTEGER NOT NULL PRIMARY KEY')===false) {
262                                 if ($isint) $value.=" DEFAULT 0";
263                                 else $value.=" DEFAULT ''";
264                         }
265                         
266                         if ($value) {
267                                 if ($first) $first=false;
268                                 else $query.=',';
269                                  $query.=' '.$value;
270                         }
271                 }
272                 $query.=' )';
273                 if ($temptable) $query='CREATE TEMPORARY TABLE '.$tablename.$query;
274                 else $query='CREATE TABLE '.$tablename.$query;
275                 //echo "<br />".htmlspecialchars($p1)."<br /><br />\n".htmlspecialchars($query)."<hr />\n";
276         } else if (strpos($uquery,'DROP TABLE IF EXISTS')===0) {
277                 if (!($i=strpos($query,';'))) $i=strlen($query);
278                 $tablename=trim(substr($query,20,$i-20));
279                 if (substr($tablename,0,1)!="'") $tablename="'$tablename'";
280                 $res=sqlite_query($dbhandle,"SELECT tbl_name FROM sqlite_master WHERE tbl_name=$tablename LIMIT 1");
281                 if (!nucleus_mysql_num_rows($res)) return true;
282                 $query='DROP TABLE '.$tablename;
283         } else if (strpos($uquery,'ALTER TABLE ')===0) {
284                 $query=trim(substr($query,11));
285                 if ($i=strpos($query,' ')) {
286                         $tablename=trim(substr($query,0,$i));
287                         $query=trim(substr($query,$i));
288                         $ret =sqlite_altertable($tablename,$query,$dbhandle);
289                         if (!$ret) sqlite_ReturnWithError('SQL error',"<br /><i>".nucleus_mysql_error()."</i><br />".htmlspecialchars($p1)."<br /><br />\n".htmlspecialchars("ALTER TABLE $tablename $query")."<hr />\n");
290                         return $ret;
291                 }
292                 // Syntax error
293                 $query=='DROP TABLE '.$query;
294         } else if (strpos($uquery,'INSERT INTO ')===0 || strpos($uquery,'REPLACE INTO ')===0 ||
295                         strpos($uquery,'INSERT IGNORE INTO ')===0 || strpos($uquery,'REPLACE IGNORE INTO ')===0) {
296                 $buff=str_replace(' IGNORE ',' OR IGNORE ',substr($uquery,0,($i=strpos($uquery,' INTO ')+6)));
297                 $query=trim(substr($query,$i));
298                 if ($i=strpos($query,' ')) {
299                         $buff.=trim(substr($query,0,$i+1));
300                         $query=trim(substr($query,$i));
301                 }
302                 if ($i=strpos($query,' ')) {
303                         if (strpos(strtoupper($query),'SET')===0) {
304                                 $query=trim(substr($query,3));
305                                 $commands=sqlite_splitByComma($query);
306                                 $query=' VALUES(';
307                                 $buff.=' (';
308                                 foreach($commands as $key=>$value){
309                                         //echo "[".htmlspecialchars($value)."]";
310                                         if (0<$key) {
311                                                 $buff.=', ';
312                                                 $query.=', ';
313                                         }
314                                         if ($i=strpos($value,'=')) {
315                                                 $buff.=trim(substr($value,0,$i));
316                                                 $query.=substr($value,$i+1);
317                                         }
318                                 }
319                                 $buff.=')';
320                                 $query.=')';
321                         } else {
322                                 $beforevalues='';
323                                 $commands=sqlite_splitByComma($query);
324                                 $query='';
325                                 foreach($commands as $key=>$value){
326                                         if ($beforevalues=='' && preg_match('/^(.*)\)\s+VALUES\s+\(/i',$value,$matches)) {
327                                                 $beforevalues=$buff.' '.$query.$matches[1].')';
328                                         }
329                                         if (0<$key) $query.=$beforevalues.' VALUES ';// supports multiple insertion
330                                         $query.=$value.';';
331                                 }
332                         }
333                 }
334                 $query=$buff.' '.$query;
335         } else if (strpos($uquery,'SHOW TABLES LIKE ')===0) {
336                 $query='SELECT name FROM sqlite_master WHERE type=\'table\' AND name LIKE '.substr($query,17);
337         } else if (strpos($uquery,'SHOW TABLES')===0) {
338                 $query='SELECT name FROM sqlite_master WHERE type=\'table\'';
339         } else if (strpos($uquery,'SHOW KEYS FROM ')===0) {
340                 $query=sqlite_showKeysFrom(trim(substr($query,15)),$dbhandle);
341         } else if (strpos($uquery,'SHOW INDEX FROM ')===0) {
342                 $query=sqlite_showKeysFrom(trim(substr($query,16)),$dbhandle);
343         } else if (strpos($uquery,'SHOW FIELDS FROM ')===0) {
344                 $query=sqlite_showFieldsFrom(trim(substr($query,17)),$dbhandle);
345         } else if (strpos($uquery,'SHOW COLUMNS FROM ')===0) {
346                 $query=sqlite_showFieldsFrom(trim(substr($query,18)),$dbhandle);
347         } else if (strpos($uquery,'TRUNCATE TABLE ')===0) {
348                 $query='DELETE FROM '.substr($query,15);
349         } else sqlite_modifyQueryForUserFunc($query,$strpositions);
350
351         //Throw query again.
352         $aftertrans=time()+microtime();
353         if ($unbuffered) {
354                 $ret=sqlite_unbuffered_query($dbhandle,$query);
355         } else {
356                 $ret=sqlite_query($dbhandle,$query);
357         }
358
359         $afterquery=time()+microtime();
360         if ($SQLITECONF['MEASURESPEED']) sqlite_DebugMessage("translated query:$query\n".
361                 'translation: '.($aftertrans-$beforetrans).'sec, query: '.($afterquery-$aftertrans).'sec');
362         if (!$ret) sqlite_ReturnWithError('SQL error',"<br /><i>".nucleus_mysql_error()."</i><br />".htmlspecialchars($p1)."<br /><br />\n".htmlspecialchars($query)."<hr />\n");
363         foreach ($morequeries as $value) if ($value) @sqlite_query($dbhandle,$value);
364         return $ret;
365 }
366 function sqlite_changeQuote(&$query){
367         // This function is most important.
368         // When you modify this function, do it very carefully.
369         // Otherwise, you may allow crackers to do SQL-injection.
370         // This function returns array that shows where the strings are.
371         $sarray=array();
372         $ret='';
373         $qlen=strlen($query);
374         for ($i=0;$i<$qlen;$i++) {
375                 // Go to next quote
376                 if (($i1=strpos($query,'"',$i))===false) $i1=$qlen;
377                 if (($i2=strpos($query,"'",$i))===false) $i2=$qlen;
378                 if (($i3=strpos($query,'`',$i))===false) $i3=$qlen;
379                 if ($i1==$qlen && $i2==$qlen && $i3==$qlen) {
380                         $ret.=($temp=substr($query,$i));
381                         if (strstr($temp,';')) exit('Warning: try to use more than two queries?');
382                         break;
383                 }
384                 if ($i2<($j=$i1)) $j=$i2;
385                 if ($i3<$j) $j=$i3;
386                 $ret.=($temp=substr($query,$i,$j-$i));
387                 $c=$query[($i=$j)]; // $c keeps the type of quote.
388                 if (strstr($temp,';')) exit('Warning: try to use more than two queries?');
389                 
390                 // Check between quotes.
391                 // $j shows the begging positioin.
392                 // $i will show the ending position.
393                 $j=(++$i);
394                 while ($i<$qlen) {
395                         if (($i1=strpos($query,$c,$i))===false) $i1=$qlen;
396                         if (($i2=strpos($query,"\\",$i))===false) $i2=$qlen;
397                         if ($i2<$i1) {
398                                 // \something. Skip two characters.
399                                 $i=$i2+2;
400                                 continue;
401                         } if ($i1<($qlen-1) && $query[$i1+1]==$c) {
402                                 // "", '' or ``.  Skip two characters.
403                                 $i=$i1+2;
404                                 continue;
405                         } else {// OK. Reached the end position
406                                 $i=$i1;
407                                 break;
408                         }
409                 }
410                 $i1=strlen($ret);
411                 $ret.="'".sqlite_changeslashes(substr($query,$j,$i-$j));
412                 if ($i<$qlen) $ret.="'"; //else Syntax error in query.
413                 $i2=strlen($ret);
414                 $sarray[$i1]=$i2;
415         }//echo htmlspecialchars($query).'<br />'.htmlspecialchars($ret).'<br />';
416         $query=$ret;
417         return $sarray;
418 }
419 function sqlite_splitByComma($query) {
420         // The query is splitted by comma and the data will be put into an array.
421         // The commas in quoted strings are ignored.
422         $commands=array();
423         $i=0;
424         $in=false;
425         while ($query) {
426                 if ($query[$i]=="'") {
427                         $i++;
428                         while ($i<strlen($query)) {
429                                 if ($query[$i++]!="'") continue;
430                                 if ($query[$i]!="'") break;
431                                 $i++;
432                         }
433                         continue;
434                 } else if ($query[$i]=='(') $in=true;
435                 else if ($query[$i]==')') $in=false;
436                 else if ($query[$i]==',' && (!$in)) {
437                         $commands[]=trim(substr($query,0,$i));
438                         $query=trim(substr($query,$i+1));
439                         $i=0;
440                         continue;
441                 } // Do NOT add 'else' statement here! '$i++' is important in the following line.
442                 if (strlen($query)<=($i++)) break;
443         }
444         if ($query) $commands[]=$query;
445         return $commands;
446 }
447 function sqlite_changeslashes(&$text){
448         // By SQLite, "''" is used in the quoted string instead of "\'".
449         // In addition, only "'" seems to be allowed for perfect quotation of string.
450         // This routine is used for the conversion from MySQL type to SQL type.
451         // Do NOT use stripslashes() but use stripcslashes().  Otherwise, "\r\n" is not converted.
452         if ($text==='') return '';
453         return (sqlite_escape_string (stripcslashes((string)$text)));
454 }
455 function sqlite_altertable($table,$alterdefs,$dbhandle){
456         // This function originaly came from Jon Jensen's PHP class, SQLiteDB.
457         // There are some modifications by Katsumi.
458         $table=str_replace("'",'',$table);
459         if (!$alterdefs) return false;
460         $result = sqlite_query($dbhandle,"SELECT sql,name,type FROM sqlite_master WHERE tbl_name = '".$table."' ORDER BY type DESC");
461         if(!sqlite_num_rows($result)) return sqlite_ReturnWithError('no such table: '.$table);
462         $row = sqlite_fetch_array($result); //table sql
463         if (function_exists('microtime')) $tmpname='t'.str_replace('.','',str_replace(' ','',microtime()));
464         else $tmpname = 't'.rand(0,999999).time();
465         $origsql = trim(preg_replace("/[\s]+/"," ",str_replace(",",", ",preg_replace("/[\(]/","( ",$row['sql'],1))));
466         $createtemptableSQL = 'CREATE TEMPORARY '.substr(trim(preg_replace("'".$table."'",$tmpname,$origsql,1)),6);
467         $createindexsql = array();
468         $i = 0;
469         $defs = preg_split("/[,]+/",$alterdefs,-1,PREG_SPLIT_NO_EMPTY);
470         $prevword = $table;
471         $oldcols = preg_split("/[,]+/",substr(trim($createtemptableSQL),strpos(trim($createtemptableSQL),'(')+1),-1,PREG_SPLIT_NO_EMPTY);
472         $newcols = array();
473         for($i=0;$i<sizeof($oldcols);$i++){
474                 $colparts = preg_split("/[\s]+/",$oldcols[$i],-1,PREG_SPLIT_NO_EMPTY);
475                 $oldcols[$i] = $colparts[0];
476                 $newcols[$colparts[0]] = $colparts[0];
477         }
478         $newcolumns = '';
479         $oldcolumns = '';
480         reset($newcols);
481         while(list($key,$val) = each($newcols)){
482                 if (strtoupper($val)!='PRIMARY' && strtoupper($key)!='PRIMARY' &&
483                     strtoupper($val)!='UNIQUE'  && strtoupper($key)!='UNIQUE'){
484                         $newcolumns .= ($newcolumns?', ':'').$val;
485                         $oldcolumns .= ($oldcolumns?', ':'').$key;
486                 }
487         }
488         $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table;
489         $dropoldsql = 'DROP TABLE '.$table;
490         $createtesttableSQL = $createtemptableSQL;
491         foreach($defs as $def){
492                 $defparts = preg_split("/[\s]+/",$def,-1,PREG_SPLIT_NO_EMPTY);
493                 $action = strtolower($defparts[0]);
494                 switch($action){
495                 case 'modify':
496                         // Modification does not mean anything for SQLite, so just return true.
497                         // But this command will be supported in future???
498                         break;
499                 case 'add':
500                         if(($i=sizeof($defparts)) <= 2) return sqlite_ReturnWithError('near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').'": syntax error');
501                         
502                         // ignore if there is already such table
503                         $exists=false;
504                         foreach($oldcols as $value) if (str_replace("'",'',$defparts[1])==str_replace("'",'',$value)) $exists=true;
505                         if ($exists) break;
506                         
507                         // Ignore 'AFTER xxxx' statement.
508                         // Maybe this feature will be supprted later.
509                         if (4<=$i && strtoupper($defparts[$i-2])=='AFTER') 
510                                 unset($defparts[$i-1],$defparts[$i-2]);
511                         
512                         $createtesttableSQL = substr($createtesttableSQL,0,strlen($createtesttableSQL)-1).',';
513                         for($i=1;$i<sizeof($defparts);$i++) $createtesttableSQL.=' '.$defparts[$i];
514                         $createtesttableSQL.=')';
515                         break;
516                 case 'change':
517                         if(sizeof($defparts) <= 3) return sqlite_ReturnWithError('near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').($defparts[2]?' '.$defparts[2]:'').'": syntax error');
518                         if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){
519                                 if($newcols[$defparts[1]] != $defparts[1]){
520                                         sqlite_ReturnWithError('unknown column "'.$defparts[1].'" in "'.$table.'"');
521                                         return false;
522                                 }
523                                 $newcols[$defparts[1]] = $defparts[2];
524                                 $nextcommapos = strpos($createtesttableSQL,',',$severpos);
525                                 $insertval = '';
526                                 for($i=2;$i<sizeof($defparts);$i++) $insertval.=' '.$defparts[$i];
527                                 if($nextcommapos) $createtesttableSQL = substr($createtesttableSQL,0,$severpos).$insertval.substr($createtesttableSQL,$nextcommapos);
528                                 else $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1)).$insertval.')';
529                         } else  return sqlite_ReturnWithError('unknown column "'.$defparts[1].'" in "'.$table.'"');
530                         break;
531                 case 'drop':
532                         if(sizeof($defparts) < 2) return sqlite_ReturnWithError('near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').'": syntax error');
533                         if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){
534                                 $nextcommapos = strpos($createtesttableSQL,',',$severpos);
535                                 if($nextcommapos) $createtesttableSQL = substr($createtesttableSQL,0,$severpos).substr($createtesttableSQL,$nextcommapos + 1);
536                                 else $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1) - 1).')';
537                                 unset($newcols[$defparts[1]]);
538                         } else  return sqlite_ReturnWithError('unknown column "'.$defparts[1].'" in "'.$table.'"');
539                         break;
540                 default:
541                         return sqlite_ReturnWithError('near "'.$prevword.'": syntax error');
542                         break;
543                 }
544                 $prevword = $defparts[sizeof($defparts)-1];
545         }
546
547         //this block of code generates a test table simply to verify that the columns specifed are valid in an sql statement
548         //this ensures that no reserved words are used as columns, for example
549         if (!sqlite_query($dbhandle,$createtesttableSQL)) return false;
550         $droptempsql = 'DROP TABLE '.$tmpname;
551         sqlite_query($dbhandle,$droptempsql);
552         //end block
553
554         $createnewtableSQL = 'CREATE '.substr(trim(preg_replace("'".$tmpname."'",$table,$createtesttableSQL,1)),17);
555         $newcolumns = '';
556         $oldcolumns = '';
557         reset($newcols);
558         while(list($key,$val) = each($newcols)) {
559                 if (strtoupper($val)!='PRIMARY' && strtoupper($key)!='PRIMARY' &&
560                     strtoupper($val)!='UNIQUE'  && strtoupper($key)!='UNIQUE'){
561                         $newcolumns .= ($newcolumns?', ':'').$val;
562                         $oldcolumns .= ($oldcolumns?', ':'').$key;
563                 }
564         }
565         $copytonewsql = 'INSERT INTO '.$table.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$tmpname;
566
567         sqlite_query($dbhandle,$createtemptableSQL); //create temp table
568         sqlite_query($dbhandle,$copytotempsql); //copy to table
569         sqlite_query($dbhandle,$dropoldsql); //drop old table
570
571         sqlite_query($dbhandle,$createnewtableSQL); //recreate original table
572         sqlite_query($dbhandle,$copytonewsql); //copy back to original table
573         sqlite_query($dbhandle,$droptempsql); //drop temp table
574         return true;
575 }
576 function sqlite_showKeysFrom($tname,$dbhandle) {
577         // This function is for supporing 'SHOW KEYS FROM' and 'SHOW INDEX FROM'.
578         // For making the same result as obtained by MySQL, temporary table is made.
579         $tname=str_replace("'",'',$tname);
580         
581         // Create a temporary table for making result
582         if (function_exists('microtime')) $tmpname='t'.str_replace('.','',str_replace(' ','',microtime()));
583         else $tmpname = 't'.rand(0,999999).time();
584         sqlite_query($dbhandle,"CREATE TEMPORARY TABLE $tmpname ('Table', 'Non_unique', 'Key_name', 'Seq_in_index',".
585                 " 'Column_name', 'Collation', 'Cardinality', 'Sub_part', 'Packed', 'Null', 'Index_type', 'Comment')"); 
586         
587         // First, get the sql query when the table created
588         $res=sqlite_query($dbhandle,"SELECT sql FROM sqlite_master WHERE tbl_name = '$tname' ORDER BY type DESC");
589         $a=nucleus_mysql_fetch_assoc($res);
590         $tablesql=$a['sql'];
591         
592         // Check if each columns are unique
593         $notnull=array();
594         foreach(sqlite_splitByComma(substr($tablesql,strpos($tablesql,'(')+1)) as $value) {
595                 $name=str_replace("'",'',substr($value,0,strpos($value,' ')));
596                 if (strpos(strtoupper($value),'NOT NULL')!==false) $notnull[$name]='';
597                 else $notnull[$name]='YES';
598         }
599         
600         // Get the primary key (and check if it is unique???).
601         if (preg_match('/[^a-zA-Z_\']([\S]+)[^a-zA-Z_\']+INTEGER NOT NULL PRIMARY KEY/i',$tablesql,$matches)) {
602                 $pkey=str_replace("'",'',$matches[1]);
603                 $pkeynull='';
604         } else if (preg_match('/[^a-zA-Z_\']([\S]+)[^a-zA-Z_\']+INTEGER PRIMARY KEY/i',$tablesql,$matches)) {
605                 $pkey=str_replace("'",'',$matches[1]);
606                 $pkeynull='YES';
607         } else if (preg_match('/PRIMARY KEY[\s]*?\(([^\)]+)\)/i',$tablesql,$matches)) {
608                 $pkey=null;// PRIMARY KEY ('xxx'[,'xxx'])
609                 foreach(explode(',',$matches[1]) as $key=>$value) {
610                         $value=str_replace("'",'',trim($value));
611                         $key++;
612                         $cardinality=nucleus_mysql_num_rows(sqlite_query($dbhandle,"SELECT '$value' FROM '$tname'"));
613                         sqlite_query($dbhandle,"INSERT INTO $tmpname ('Table', 'Non_unique', 'Key_name', 'Seq_in_index',".
614                                 " 'Column_name', 'Collation', 'Cardinality', 'Sub_part', 'Packed', 'Null', 'Index_type', 'Comment')".
615                                 " VALUES ('$tname', '0', 'PRIMARY', '$key',".
616                                 " '$value', 'A', '$cardinality', null, null, '', 'BTREE', '')"); 
617                 }
618         } else $pkey=null;
619         
620         // Check the index.
621         $res=sqlite_query($dbhandle,"SELECT sql,name FROM sqlite_master WHERE type = 'index' and tbl_name = '$tname' ORDER BY type DESC");
622         while ($a=nucleus_mysql_fetch_assoc($res)) {
623                 if (!($sql=$a['sql'])) {// Primary key
624                         if ($pkey && strpos(strtolower($a['name']),'autoindex')) {
625                                 $cardinality=nucleus_mysql_num_rows(sqlite_query($dbhandle,"SELECT $pkey FROM '$tname'"));
626                                 sqlite_query($dbhandle,"INSERT INTO $tmpname ('Table', 'Non_unique', 'Key_name', 'Seq_in_index',".
627                                         " 'Column_name', 'Collation', 'Cardinality', 'Sub_part', 'Packed', 'Null', 'Index_type', 'Comment')".
628                                         " VALUES ('$tname', '0', 'PRIMARY', '1',".
629                                         " '$pkey', 'A', '$cardinality', null, null, '$pkeynull', 'BTREE', '')"); 
630                                 $pkey=null;
631                         }
632                 } else {// Non-primary key
633                         if (($name=str_replace("'",'',$a['name'])) && preg_match('/\(([\s\S]+)\)/',$sql,$matches)) {
634                                 foreach(explode(',',$matches[1]) as $key=>$value) {
635                                         $columnname=str_replace("'",'',$value);
636                                         if (strpos(strtoupper($sql),'CREATE UNIQUE ')===0) $nonunique='0';
637                                         else $nonunique='1';
638                                         $cardinality=nucleus_mysql_num_rows(sqlite_query($dbhandle,"SELECT $columnname FROM '$tname'"));
639                                         sqlite_query($dbhandle,"INSERT INTO $tmpname ('Table', 'Non_unique', 'Key_name', 'Seq_in_index',".
640                                                 " 'Column_name', 'Collation', 'Cardinality', 'Sub_part', 'Packed', 'Null', 'Index_type', 'Comment')".
641                                                 " VALUES ('$tname', '$nonunique', '$name', '".(string)($key+1)."',".
642                                                 " '$columnname', 'A', '$cardinality', null, null, '$notnull[$columnname]', 'BTREE', '')"); 
643                                 }
644                         }
645                 }
646         }
647         if ($pkey) { // The case that the key (index) is not defined.
648                 $cardinality=nucleus_mysql_num_rows(sqlite_query($dbhandle,"SELECT $pkey FROM '$tname'"));
649                 sqlite_query($dbhandle,"INSERT INTO $tmpname ('Table', 'Non_unique', 'Key_name', 'Seq_in_index',".
650                         " 'Column_name', 'Collation', 'Cardinality', 'Sub_part', 'Packed', 'Null', 'Index_type', 'Comment')".
651                         " VALUES ('$tname', '0', 'PRIMARY', '1',".
652                         " '$pkey', 'A', '$cardinality', null, null, '$pkeynull', 'BTREE', '')"); 
653                 $pkey=null;
654         }
655         
656         // return the final query to show the keys in MySQL style (using temporary table).
657         return "SELECT * FROM $tmpname";
658 }
659 function sqlite_showFieldsFrom($tname,$dbhandle){
660         // This function is for supporing 'SHOW FIELDS FROM' and 'SHOW COLUMNS FROM'.
661         // For making the same result as obtained by MySQL, temporary table is made.
662         $tname=str_replace("'",'',$tname);
663         
664         // First, get the sql query when the table created
665         $res=sqlite_query($dbhandle,"SELECT sql FROM sqlite_master WHERE tbl_name = '$tname' ORDER BY type DESC");
666         $a=nucleus_mysql_fetch_assoc($res);
667         $tablesql=trim($a['sql']);
668         if (preg_match('/^[^\(]+\(([\s\S]*?)\)$/',$tablesql,$matches)) $tablesql=$matches[1];
669         $tablearray=array();
670         foreach(sqlite_splitByComma($tablesql) as $value) {
671                 $value=trim($value);
672                 if ($i=strpos($value,' ')) {
673                         $name=str_replace("'",'',substr($value,0,$i));
674                         $value=trim(substr($value,$i));
675                         if (substr($value,-1)==',') $value=substr($value,strlen($value)-1);
676                         $tablearray[$name]=$value;
677                 }
678         }
679         
680         // Check if INDEX has been made for the parameter 'MUL' in 'KEY' column
681         $multi=array();
682         $res=sqlite_query($dbhandle,"SELECT name FROM sqlite_master WHERE type = 'index' and tbl_name = '$tname' ORDER BY type DESC");
683         while ($a=nucleus_mysql_fetch_assoc($res)) $multi[str_replace("'",'',$a['name'])]='MUL';
684         
685         // Create a temporary table for making result
686         if (function_exists('microtime')) $tmpname='t'.str_replace('.','',str_replace(' ','',microtime()));
687         else $tmpname = 't'.rand(0,999999).time();
688         sqlite_query($dbhandle,"CREATE TEMPORARY TABLE $tmpname ('Field', 'Type', 'Null', 'Key', 'Default', 'Extra')"); 
689         
690         // Check the table
691         foreach($tablearray as $field=>$value) {
692                 if (strtoupper($field)=='PRIMARY') continue;//PRIMARY KEY('xx'[,'xx'])
693                 $uvalue=strtoupper($value.' ');
694                 $key=(string)$multi[$field];
695                 if ($uvalue=='INTEGER NOT NULL PRIMARY KEY ' || $uvalue=='INTEGER PRIMARY KEY ') {
696                         $key='PRI';
697                         $extra='auto_increment';
698                 } else $extra='';
699                 if ($i=strpos($uvalue,' ')) {
700                         $type=substr($value,0,$i);
701                         if (strpos($type,'(') && ($i=strpos($value,')')))
702                                 $type=substr($value,0,$i+1);
703                 } else $type='';
704                 if (strtoupper($type)=='INTEGER') $type='int(11)';
705                 if (strpos($uvalue,'NOT NULL')===false) $null='YES';
706                 else {
707                         $null='';
708                         $value=preg_replace('/NOT NULL/i','',$value);
709                         $uvalue=strtoupper($value);
710                 }
711                 if ($i=strpos($uvalue,'DEFAULT')) {
712                         $default=trim(substr($value,$i+7));
713                         if (strtoupper($default)=='NULL') {
714                                 $default="";
715                                 $setdefault="";
716                         } else {
717                                 if (substr($default,0,1)=="'") $default=substr($default,1,strlen($default)-2);
718                                 $default="'".$default."',";
719                                 $setdefault="'Default',";
720                         }
721                 } else if ($null!='YES' && $extra!='auto_increment') {
722                         if (strpos(strtolower($type),'int')===false) $default="'',";
723                         else $default="'0',";
724                         $setdefault="'Default',";
725                 } else {
726                         $default="";
727                         $setdefault="";
728                 }
729                 sqlite_query($dbhandle,"INSERT INTO '$tmpname' ('Field', 'Type', 'Null', 'Key', $setdefault 'Extra')".
730                         " VALUES ('$field', '$type', '$null', '$key', $default '$extra')");
731         }
732         
733         // return the final query to show the keys in MySQL style (using temporary table).
734         return "SELECT * FROM $tmpname";
735 }
736 function sqlite_mysql_query_debug(&$query){
737         // The debug mode is so far used for checking query difference like "SELECT i.itime, ....".
738         // This must be chaged to "SELECT i.itime as itime,..." for SQLite.
739         // (This feature is not needed any more after the version 0.8.1 (see intialization query))
740         $uquery=strtoupper($query);
741         if (strpos($uquery,"SELECT ")!==0) return $query;
742         if (($i=strpos($uquery," FROM "))===false) return $query;
743         $select=sqlite_splitByComma(substr($query,7,$i-7));
744         $query=substr($query,$i);
745         $ret='';
746         foreach($select as $value){
747                 if (preg_match('/^([a-z_]+)\.([a-z_]+)$/i',$value,$matches)) {
748                         $value=$value." as ".$matches[2];
749                         $t=$matches[0]."=>$value\n";
750                         $a=debug_backtrace();
751                         foreach($a as $key=>$btrace) {
752                                 if (!($templine=$btrace['line'])) continue;
753                                 if (!($tempfile=$btrace['file'])) continue;
754                                 $tempfile=preg_replace('/[\s\S]*?[\/\\\\]([^\/\\\\]+)$/','$1',$tempfile);
755                                 $t.="$tempfile line:$templine\n";
756                         }
757                         sqlite_DebugMessage($t);
758                 }
759                 if ($ret) $ret.=', ';
760                 $ret.=$value;
761         }
762         return "SELECT $ret $query";
763 }
764
765 function nucleus_mysql_list_tables($p1=null,$p2=null) {
766         global $SQLITE_DBHANDLE,$MYSQL_DATABASE;
767         return sqlite_query($SQLITE_DBHANDLE,"SELECT name as Tables_in_$MYSQL_DATABASE FROM sqlite_master WHERE type='table'");
768 }
769 function nucleus_mysql_listtables($p1=null,$p2=null) { return nucleus_mysql_list_tables($p1,$p2);}
770
771 function nucleus_mysql_affected_rows($p1=null){
772         global $SQLITE_DBHANDLE;
773         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
774         return sqlite_changes($dbhandle);
775 }
776
777 function nucleus_mysql_error($p1=null){
778         global $SQLITE_DBHANDLE;
779         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
780         return sqlite_error_string ( sqlite_last_error ($dbhandle) );
781 }
782
783 function nucleus_mysql_fetch_array($p1,$p2=SQLITE_BOTH){
784         return sqlite_fetch_array ($p1,$p2);
785 }
786
787 function nucleus_mysql_fetch_assoc($p1){
788         return sqlite_fetch_array($p1,SQLITE_ASSOC);
789 }
790
791 function nucleus_mysql_fetch_object($p1,$p2=SQLITE_BOTH){
792         if (is_array($ret=sqlite_fetch_array ($p1,$p2))) {
793                 $o=new SQLITE_OBJECT;
794                 foreach ($ret as $key=>$value) {
795                         if (strstr($key,'.')) {// Remove table name.
796                                 $key=preg_replace('/^(.+)\."(.+)"$/','"$2"',$key);
797                                 $key=preg_replace('/^(.+)\.([^.^"]+)$/','$2',$key);
798                         }
799                         $o->$key=$value;
800                 }
801                 return $o;
802         } else return false;
803 }
804
805 function nucleus_mysql_fetch_row($p1){
806         return sqlite_fetch_array($p1,SQLITE_NUM);
807 }
808
809 function nucleus_mysql_field_name($p1,$p2){
810         return sqlite_field_name ($p1,$p2);
811 }
812
813 function nucleus_mysql_free_result($p1){
814         // ???? Cannot find corresponding function of SQLite.
815         // Maybe SQLite is NOT used for the high spec server
816         // that need mysql_free_result() function because of
817         // many SQL-queries in a script.
818         return true;
819 }
820
821 function nucleus_mysql_insert_id($p1=null){
822         global $SQLITE_DBHANDLE;
823         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
824         return sqlite_last_insert_rowid ($dbhandle);
825 }
826
827 function nucleus_mysql_num_fields($p1){
828         return sqlite_num_fields ($p1);
829 }
830
831 function nucleus_mysql_num_rows($p1){
832         return sqlite_num_rows ($p1);
833 }
834 function nucleus_mysql_numrows($p1){
835         return sqlite_num_rows ($p1);
836 }
837
838 function nucleus_mysql_result($p1,$p2,$p3=null){
839         if ($p3) return sqlite_ReturnWithError('nucleus_mysql_result');
840         if (!$p2) return sqlite_fetch_single ($p1);
841         $a=sqlite_fetch_array ($p1);
842         return $a[$p2];
843 }
844
845 function nucleus_mysql_unbuffered_query($p1,$p2=null){
846         return nucleus_mysql_query($p1,$p2,true);
847 }
848
849 function nucleus_mysql_client_encoding($p1=null){
850         return sqlite_libencoding();
851 }
852
853 function nucleus_mysql_data_seek($p1,$p2) {
854         return sqlite_seek($p1,$p2);
855 }
856
857 function nucleus_mysql_errno ($p1=null){
858         global $SQLITE_DBHANDLE;
859         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
860         return sqlite_last_error($dbhandle);
861 }
862
863 function nucleus_mysql_escape_string ($p1){
864         // The "'" will be changed to "''".
865         // This way works for both MySQL and SQLite when single quotes are used for string.
866         // Note that single quote is always used in this wrapper.
867         // If a plugin is made on SQLite-Nucleus and such plugin will be used for MySQL-Nucleus,
868         // nucleus_mysql_escape_string() will be changed to mysql_escape_string() and
869         // this routine won't be used, so this way won't be problem.
870         return sqlite_escape_string($p1);
871 }
872
873 function nucleus_mysql_real_escape_string ($p1,$p2=null){
874         //addslashes used here.
875         return addslashes($p1);
876 }
877
878 function nucleus_mysql_create_db ($p1,$p2=null){
879         // All prameters are ignored.
880         // Returns always true;
881         return true;
882 }
883
884 function nucleus_mysql_pconnect($p1=null,$p2=null,$p3=null,$p4=null,$p5=null){
885         global $SQLITE_DBHANDLE,$SQLITECONF;
886         sqlite_close ($SQLITE_DBHANDLE);
887         $SQLITE_DBHANDLE=sqlite_popen($SQLITECONF['DBFILENAME']);
888         return ($SQLITE['DBHANDLE']=$SQLITE_DBHANDLE);
889 }
890
891 function nucleus_mysql_fetch_field($p1,$p2=null){
892         if ($p2) return sqlite_ReturnWithError('nucleus_mysql_fetch_field');
893         // Only 'name' is supported.
894         $o=new SQLITE_OBJECT;
895         $o->name=array();
896         if(is_array($ret=sqlite_fetch_array ($p1,SQLITE_ASSOC )))
897                 foreach ($ret as $key=>$value) {
898                         if (is_string($key)) array_push($o->name,$key);
899                 }
900         return $o;
901
902 }
903
904 }//if (function_exists('sqlite_open'))
905
906 // This function is called instead of _execute_queries() in backp.php
907 function sqlite_restore_execute_queries(&$query){
908         global $DIR_NUCLEUS,$DIR_LIBS,$DIR_PLUGINS,$CONF;
909         
910         // Skip until the first "#" or "--"
911         if (($i=strpos($query,"\n#"))===false) $i=strlen($query);
912         if (($j=strpos($query,"\n--"))===false) $j=strlen($query);
913         if ($i<$j) $query=substr($query,$i+1);
914         else  $query=substr($query,$j+1);
915         
916         // Save the query to temporary file in sqlite directory.
917         if (function_exists('microtime')) {
918                 $prefix=preg_replace('/[^0-9]/','',microtime());
919         } else {
920                 srand(time());
921                 $prefix=(string)rand(0,999999);
922         }
923         $tmpname=tempnam($DIR_NUCLEUS.'sqlite/',"tmp$prefix");
924         if (!($handle=@fopen($tmpname,'w'))) return 'Cannot save temporary DB file.';
925         fwrite($handle,$query);
926         fclose($handle);
927         $tmpname=preg_replace('/[\s\S]*?[\/\\\\]([^\/\\\\]+)$/','$1',$tmpname);
928         
929         // Read the option from NP_SQLite
930         if (!class_exists('NucleusPlugin')) { include($DIR_LIBS.'PLUGIN.php');}
931         if (!class_exists('NP_SQLite')) { include($DIR_PLUGINS.'NP_SQLite.php'); }
932         $p=new NP_SQLite();
933         if (!($numatonce=@$p->getOption('numatonce'))) $numatonce=20;
934         if (!($refreshwait=@$p->getOption('refreshwait'))) $refreshwait=1;
935         
936         // Start process.
937         $url="plugins/sqlite/restore.php?dbfile=$tmpname&numatonce=$numatonce&refreshwait=$refreshwait";
938         header('HTTP/1.0 301 Moved Permanently');
939         header('Location: '.$url);
940         exit('<html><body>Moved Permanently</body></html>');
941 }
942
943 ?>