OSDN Git Service

move sqlite
[nucleus-jp/nucleus-plugins.git] / sqlite / trunk / nucleus / sqlite / sqlite.php
1 <?php
2     /****************************************
3     * SQLite-MySQL wrapper for Nucleus      *
4     *                           ver 0.9.0.3 *
5     * Written by Katsumi                    *
6     ****************************************/
7
8 // Check SQLite installed
9
10 if (!function_exists('sqlite_open')) exit('Sorry, SQLite is not available from PHP (maybe, not installed in the server).');
11
12 // Initializiation stuff
13 require_once dirname(__FILE__) . '/sqliteconfig.php';
14 $SQLITE_DBHANDLE=sqlite_open($SQLITECONF['DBFILENAME']);
15 require_once dirname(__FILE__) . '/sqlitequeryfunctions.php';
16 $SQLITECONF['VERSION']='0.9.0.3';
17
18 //Following thing may work if MySQL is NOT installed in server.
19 if (!function_exists('mysql_query')) {
20         define ("MYSQL_ASSOC", SQLITE_ASSOC);
21         define ("MYSQL_BOTH", SQLITE_BOTH);
22         define ("MYSQL_NUM", SQLITE_NUM);
23         function mysql_connect(){
24                 global $SQLITECONF;
25                 $SQLITECONF['OVERRIDEMODE']=true;
26                 $args=func_get_args();
27                 return call_user_func_array('nucleus_mysql_connect',$args);
28         }
29         foreach (array('mysql_affected_rows','mysql_change_user','mysql_client_encoding','mysql_close',
30                 'mysql_create_db','mysql_data_seek','mysql_db_name','mysql_db_query','mysql_drop_db','mysql_errno',
31                 'mysql_error','mysql_escape_string','mysql_fetch_array','mysql_fetch_assoc','mysql_fetch_field','mysql_fetch_lengths',
32                 'mysql_fetch_object','mysql_fetch_row','mysql_field_flags','mysql_field_len','mysql_field_name','mysql_field_seek',
33                 'mysql_field_table','mysql_field_type','mysql_free_result','mysql_get_client_info','mysql_get_host_info',
34                 'mysql_get_proto_info','mysql_get_server_info','mysql_info','mysql_insert_id','mysql_list_dbs',
35                 'mysql_list_fields','mysql_list_processes','mysql_list_tables','mysql_num_fields','mysql_num_rows','mysql_numrows',
36                 'mysql_pconnect','mysql_ping','mysql_query','mysql_real_escape_string','mysql_result','mysql_select_db',
37                 'mysql_stat','mysql_tablename','mysql_thread_id','mysql_unbuffered_query')
38                  as $value) eval(
39                 "function $value(){\n".
40                 "  \$args=func_get_args();\n".
41                 "  return call_user_func_array('nucleus_$value',\$args);\n".
42                 "}\n");
43 }
44
45 // Empty object for mysql_fetch_object().
46 class SQLITE_OBJECT {}
47
48 function sqlite_ReturnWithError($text='Not supported',$more=''){
49         // Show warning when error_reporting() is set.
50         if (!(error_reporting() & E_WARNING)) return false;
51         
52         // Seek the file and line that originally called sql function.
53         $a=debug_backtrace();
54         foreach($a as $key=>$btrace) {
55                 if (!($templine=$btrace['line'])) continue;
56                 if (!($tempfile=$btrace['file'])) continue;
57                 $file=str_replace('\\','/',$file);
58                 if (!$line && !$file && strpos($tempfile,'/sqlite.php')===false && strpos($tempfile,'/sqlitequeryfunctions.php')===false) {
59                         $line=$templine;
60                         $file=$tempfile;
61                 }
62                 echo "\n<!--$tempfile line:$templine-->\n";
63         }
64         echo "Warning from SQLite-MySQL wrapper: $text<br />\n";
65         if ($line && $file) echo "in <b>$file</b> on line <b>$line</b><br />\n";
66         echo $more;
67         return false;
68 }
69 function sqlite_DebugMessage($text=''){
70         global $SQLITECONF;
71         if (!$SQLITECONF['DEBUGREPORT']) return;
72         if ($text) $SQLITECONF['DEBUGMESSAGE'].="\n".$text."\n";
73         if (headers_sent()) {
74                 echo '<!--sqlite_DebugMessage'.$SQLITECONF['DEBUGMESSAGE'].'sqlite_DebugMessage-->';
75                 unset($SQLITECONF['DEBUGMESSAGE']);
76         }
77 }
78
79 // nucleus_mysql_XXXX() functions follow.
80
81 function nucleus_mysql_connect($p1=null,$p2=null,$p3=null,$p4=null,$p5=null){
82         // All prameters are ignored.
83         global $SQLITE_DBHANDLE,$SQLITECONF;
84         if (!$SQLITE_DBHANDLE) $SQLITE_DBHANDLE=sqlite_open($SQLITECONF['DBFILENAME']);
85         // Initialization queries.
86         foreach($SQLITECONF['INITIALIZE'] as $value) nucleus_mysql_query($value);
87         // Unregister the function 'php' in sql query.
88         sqlite_create_function($SQLITE_DBHANDLE,'php','pi');
89         return $SQLITE_DBHANDLE;
90 }
91
92 function nucleus_mysql_close($p1=null){
93         global $SQLITE_DBHANDLE;
94         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
95         $SQLITE_DBHANDLE='';
96         return sqlite_close ($dbhandle);
97 }
98
99 function nucleus_mysql_select_db($p1,$p2=null){
100         // SQLite does not support multiple databases in a file.
101         // So this function do nothing and always returns true.
102         // Note: mysql_select_db() function returns true/false,
103         // not link-ID.
104         return true;
105 }
106
107 function nucleus_mysql_query($p1,$p2=null,$unbuffered=false){//echo htmlspecialchars($p1)."<br />\n";
108         global $SQLITE_DBHANDLE,$SQLITECONF;
109         if (!($dbhandle=$p2)) $dbhandle=$SQLITE_DBHANDLE;
110         $query=trim($p1);
111         if (strpos($query,"\xEF\xBB\xBF")===0) $query=substr($query,3);// UTF-8 stuff
112         if (substr($query,-1)==';') $query=substr($query,0,strlen($query)-1);
113         
114         // Escape style is changed from MySQL type to SQLite type here.
115         // This is important to avoid possible SQL-injection.
116         $strpositions=array();// contains the data show where the strings are (startposition => endposition)
117         if (strpos($query,'`')!==false || strpos($query,'"')!==false || strpos($query,"'")!==false)
118                 $strpositions=sqlite_changeQuote($query);
119         //echo "<br />".htmlspecialchars($p1)."<br /><br />\n".htmlspecialchars($query)."<hr />\n";
120
121         // Debug mode
122         if ($SQLITECONF['DEBUGMODE']) $query=sqlite_mysql_query_debug($query);
123         
124         // Anyway try it.
125         if ($unbuffered) {
126                 if ($ret=@sqlite_unbuffered_query($dbhandle,$query)) return $ret;
127         } else {
128                 if ($ret=@sqlite_query($dbhandle,$query)) return $ret;
129         }
130         
131         // Error occured. Query must be translated.
132         return sqlite_mysql_query_sub($dbhandle,$query,$strpositions,$p1,$unbuffered);
133 }
134 function sqlite_mysql_query_sub($dbhandle,$query,$strpositions=array(),$p1=null,$unbuffered=false){//echo htmlspecialchars($p1)."<br />\n";
135         // Query translation is needed, especially when changing the data in database.
136         // So far, this routine is written for 'CREATE TABLE','DROP TABLE', 'INSERT INTO',
137         // 'SHOW TABLES LIKE', 'SHOW KEYS FROM', 'SHOW INDEX FROM'
138         // and several functions used in query.
139         // How about 'UPDATE' ???
140         global $SQLITE_DBHANDLE,$SQLITECONF;
141         $beforetrans=time()+microtime();
142         if (!$p1) $p1=$query;
143         $morequeries=array();
144         $temptable=false;
145         $uquery=strtoupper($query);
146         if (strpos($uquery,'CREATE TABLE')===0 || ($temptable=(strpos($uquery,'CREATE TEMPORARY TABLE')===0))) {
147                 if (!($i=strpos($query,'('))) return sqlite_ReturnWithError('nucleus_mysql_query: '.$p1);
148                 //check if the command is 'CREATE TABLE IF NOT EXISTS'
149                 if (strpos(strtoupper($uquery),'CREATE TABLE IF NOT EXISTS')===0) {
150                         $tablename=trim(substr($query,26,$i-26));
151                         if (substr($tablename,0,1)!="'") $tablename="'$tablename'";
152                         $res=sqlite_query($dbhandle,"SELECT tbl_name FROM sqlite_master WHERE tbl_name=$tablename LIMIT 1");
153                         if (nucleus_mysql_num_rows($res)) return true;
154                 } else {
155                         $tablename=trim(substr($query,12,$i-12));
156                         if (substr($tablename,0,1)!="'") $tablename="'$tablename'";
157                 }
158                 $query=trim(substr($query,$i+1));
159                 for ($i=strlen($query);0<$i;$i--) if ($query[$i]==')') break;
160                 $query=substr($query,0,$i);
161                 $commands=_sqlite_divideByChar(',',$query);
162                 require_once(dirname(__FILE__) . '/sqlitealtertable.php');
163                 $query=sqlite_createtable_query($commands,$tablename,$temptable,$morequeries);
164         } else if (strpos($uquery,'DROP TABLE IF EXISTS')===0) {
165                 if (!($i=strpos($query,';'))) $i=strlen($query);
166                 $tablename=trim(substr($query,20,$i-20));
167                 if (substr($tablename,0,1)!="'") $tablename="'$tablename'";
168                 $res=sqlite_query($dbhandle,"SELECT tbl_name FROM sqlite_master WHERE tbl_name=$tablename LIMIT 1");
169                 if (!nucleus_mysql_num_rows($res)) return true;
170                 $query='DROP TABLE '.$tablename;
171         } else if (strpos($uquery,'ALTER TABLE ')===0) {
172                 $query=trim(substr($query,11));
173                 if ($i=strpos($query,' ')) {
174                         $tablename=trim(substr($query,0,$i));
175                         $query=trim(substr($query,$i));
176                         require_once(dirname(__FILE__) . '/sqlitealtertable.php');
177                         $ret =sqlite_altertable($tablename,$query,$dbhandle);
178                         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");
179                         return $ret;
180                 }
181                 // Else, syntax error
182         } else if (strpos($uquery,'RENAME TABLE ')===0) {
183                 require_once(dirname(__FILE__) . '/sqlitealtertable.php');
184                 return sqlite_renametable(_sqlite_divideByChar(',',substr($query,13)),$dbhandle);
185         } else if (strpos($uquery,'INSERT INTO ')===0 || strpos($uquery,'REPLACE INTO ')===0 ||
186                         strpos($uquery,'INSERT IGNORE INTO ')===0 || strpos($uquery,'REPLACE IGNORE INTO ')===0) {
187                 $buff=str_replace(' IGNORE ',' OR IGNORE ',substr($uquery,0,($i=strpos($uquery,' INTO ')+6)));
188                 $query=trim(substr($query,$i));
189                 if ($i=strpos($query,' ')) {
190                         $buff.=trim(substr($query,0,$i+1));
191                         $query=trim(substr($query,$i));
192                 }
193                 if ($i=strpos($query,' ')) {
194                         if (strpos(strtoupper($query),'SET')===0) {
195                                 $query=trim(substr($query,3));
196                                 $commands=_sqlite_divideByChar(',',$query);
197                                 $query=' VALUES(';
198                                 $buff.=' (';
199                                 foreach($commands as $key=>$value){
200                                         //echo "[".htmlspecialchars($value)."]";
201                                         if (0<$key) {
202                                                 $buff.=', ';
203                                                 $query.=', ';
204                                         }
205                                         if ($i=strpos($value,'=')) {
206                                                 $buff.=trim(substr($value,0,$i));
207                                                 $query.=substr($value,$i+1);
208                                         }
209                                 }
210                                 $buff.=')';
211                                 $query.=')';
212                         } else {
213                                 $beforevalues='';
214                                 $commands=_sqlite_divideByChar(',',$query);
215                                 $query='';
216                                 foreach($commands as $key=>$value){
217                                         if ($beforevalues=='' && preg_match('/^(.*)\)\s+VALUES\s+\(/i',$value,$matches)) {
218                                                 $beforevalues=$buff.' '.$query.$matches[1].')';
219                                         }
220                                         if (0<$key) $query.=$beforevalues.' VALUES ';// supports multiple insertion
221                                         $query.=$value.';';
222                                 }
223                         }
224                 }
225                 $query=$buff.' '.$query;
226         } else if (strpos($uquery,'SHOW TABLES LIKE ')===0) {
227                 $query='SELECT name FROM sqlite_master WHERE type=\'table\' AND name LIKE '.substr($query,17);
228         } else if (strpos($uquery,'SHOW TABLES')===0) {
229                 $query='SELECT name FROM sqlite_master WHERE type=\'table\'';
230         } else if (strpos($uquery,'SHOW KEYS FROM ')===0) {
231                 require_once(dirname(__FILE__) . '/sqlitealtertable.php');
232                 $query=sqlite_showKeysFrom(trim(substr($query,15)),$dbhandle);
233         } else if (strpos($uquery,'SHOW INDEX FROM ')===0) {
234                 require_once(dirname(__FILE__) . '/sqlitealtertable.php');
235                 $query=sqlite_showKeysFrom(trim(substr($query,16)),$dbhandle);
236         } else if (strpos($uquery,'SHOW FIELDS FROM ')===0) {
237                 require_once(dirname(__FILE__) . '/sqlitealtertable.php');
238                 $query=sqlite_showFieldsFrom(trim(substr($query,17)),$dbhandle);
239         } else if (strpos($uquery,'SHOW COLUMNS FROM ')===0) {
240                 require_once(dirname(__FILE__) . '/sqlitealtertable.php');
241                 $query=sqlite_showFieldsFrom(trim(substr($query,18)),$dbhandle);
242         } else if (strpos($uquery,'TRUNCATE TABLE ')===0) {
243                 $query='DELETE FROM '.substr($query,15);
244         } else if (preg_match('/^DESC \'([^\']+)\' \'([^\']+)\'$/',$query,$m)) {
245                 return nucleus_mysql_query("SHOW FIELDS FROM '$m[1]' LIKE '$m[2]'");
246         } else if (preg_match('/^DESC ([^\s]+) ([^\s]+)$/',$query,$m)) {
247                 return nucleus_mysql_query("SHOW FIELDS FROM '$m[1]' LIKE '$m[2]'");
248         } else SQLite_Functions::sqlite_modifyQueryForUserFunc($query,$strpositions);
249
250         //Throw query again.
251         $aftertrans=time()+microtime();
252         if ($unbuffered) {
253                 $ret=sqlite_unbuffered_query($dbhandle,$query);
254         } else {
255                 $ret=sqlite_query($dbhandle,$query);
256         }
257
258         $afterquery=time()+microtime();
259         if ($SQLITECONF['MEASURESPEED']) sqlite_DebugMessage("translated query:$query\n".
260                 'translation: '.($aftertrans-$beforetrans).'sec, query: '.($afterquery-$aftertrans).'sec');
261         if (!$ret) sqlite_ReturnWithError('SQL error',"<br /><i>".nucleus_mysql_error()."</i><br />".htmlspecialchars($p1)."<br /><br />\n".htmlspecialchars($query)."<hr />\n");
262         foreach ($morequeries as $value) if ($value) @sqlite_query($dbhandle,$value);
263         return $ret;
264 }
265 function sqlite_changeQuote(&$query){
266         // This function is most important.
267         // When you modify this function, do it very carefully.
268         // Otherwise, you may allow crackers to do SQL-injection.
269         // This function returns array that shows where the strings are.
270         $sarray=array();
271         $ret='';
272         $qlen=strlen($query);
273         for ($i=0;$i<$qlen;$i++) {
274                 // Go to next quote
275                 if (($i1=strpos($query,'"',$i))===false) $i1=$qlen;
276                 if (($i2=strpos($query,"'",$i))===false) $i2=$qlen;
277                 if (($i3=strpos($query,'`',$i))===false) $i3=$qlen;
278                 if ($i1==$qlen && $i2==$qlen && $i3==$qlen) {
279                         $temp=preg_replace('/[\s]+/',' ',substr($query,$i)); // Change all spacying to ' '.
280                         $ret.=($temp);
281                         if (strstr($temp,';')) exit('Warning: try to use more than two queries?');
282                         break;
283                 }
284                 if ($i2<($j=$i1)) $j=$i2;
285                 if ($i3<$j) $j=$i3;
286                 $temp=preg_replace('/[\s]+/',' ',substr($query,$i,$j-$i)); // Change all spacying to ' '.
287                 $ret.=($temp);
288                 $c=$query[($i=$j)]; // $c keeps the type of quote.
289                 if (strstr($temp,';')) exit('Warning: try to use more than two queries?');
290                 
291                 // Check between quotes.
292                 // $j shows the begging positioin.
293                 // $i will show the ending position.
294                 $j=(++$i);
295                 while ($i<$qlen) {
296                         if (($i1=strpos($query,$c,$i))===false) $i1=$qlen;
297                         if (($i2=strpos($query,"\\",$i))===false) $i2=$qlen;
298                         if ($i2<$i1) {
299                                 // \something. Skip two characters.
300                                 $i=$i2+2;
301                                 continue;
302                         } if ($i1<($qlen-1) && $query[$i1+1]==$c) {
303                                 // "", '' or ``.  Skip two characters.
304                                 $i=$i1+2;
305                                 continue;
306                         } else {// OK. Reached the end position
307                                 $i=$i1;
308                                 break;
309                         }
310                 }
311                 $i1=strlen($ret);
312                 $ret.="'".sqlite_changeslashes(substr($query,$j,$i-$j));
313                 if ($i<$qlen) $ret.="'"; //else Syntax error in query.
314                 $i2=strlen($ret);
315                 $sarray[$i1]=$i2;
316         }//echo htmlspecialchars($query).'<br />'.htmlspecialchars($ret).'<br />';
317         $query=$ret;
318         return $sarray;
319 }
320 function sqlite_changeslashes(&$text){
321         // By SQLite, "''" is used in the quoted string instead of "\'".
322         // In addition, only "'" seems to be allowed for perfect quotation of string.
323         // This routine is used for the conversion from MySQL type to SQL type.
324         // Do NOT use stripslashes() but use stripcslashes().  Otherwise, "\r\n" is not converted.
325         if ($text==='') return '';
326         return (sqlite_escape_string (stripcslashes((string)$text)));
327 }
328 function _sqlite_divideByChar($char,$query,$limit=-1){
329         if (!is_array($char)) $char=array($char);
330         $ret=array();
331         $query=trim($query);
332         $buff='';
333         while (strlen($query)){
334                 $i=strlen($query);
335                 foreach($char as $value){
336                         if (($j=strpos($query,$value))!==false) {
337                                 if ($j<$i) $i=$j;
338                         }
339                 }
340                 if (($j=strpos($query,'('))===false) $j=strlen($query);
341                 if (($k=strpos($query,"'"))===false) $k=strlen($query);
342                 if ($i<$j && $i<$k) {// ',' found
343                         $buff.=substr($query,0,$i);
344                         if (strlen($buff)) $ret[]=$buff;
345                         $query=trim(substr($query,$i+1));
346                         $buff='';
347                         $limit--;
348                         if ($limit==0) exit;
349                 } else if ($j<$i && $j<$k) {// '(' found
350                         if (($i=strpos($query,')',$j))===false) {
351                                 $buff.=$query;
352                                 if (strlen($buff)) $ret[]=$buff;
353                                 $query=$buff='';
354                         } else {
355                                 $buff.=substr($query,0,$i+1);
356                                 $query=substr($query,$i+1);
357                         }
358                 } else if ($k<$i && $k<$j) {// "'" found
359                         if (($i=strpos($query,"'",$k+1))===false) {
360                                 $buff.=$query;
361                                 if (strlen($buff)) $ret[]=$buff;
362                                 $query=$buff='';
363                         } else {
364                                 $buff.=substr($query,0,$i+1);
365                                 $query=substr($query,$i+1);
366                         }
367                 } else {// last column
368                         $buff.=$query;
369                         if (strlen($buff)) $ret[]=$buff;
370                         $query=$buff='';
371                 }
372         }
373         if (strlen($buff)) $ret[]=$buff;
374         return $ret;
375 }
376 function sqlite_mysql_query_debug(&$query){
377         // There is nothing to do here in this version.
378         return $query;
379 }
380
381 function nucleus_mysql_list_tables($p1=null,$p2=null) {
382         global $SQLITE_DBHANDLE,$MYSQL_DATABASE;
383         return sqlite_query($SQLITE_DBHANDLE,"SELECT name as Tables_in_$MYSQL_DATABASE FROM sqlite_master WHERE type='table'");
384 }
385 function nucleus_mysql_listtables($p1=null,$p2=null) { return nucleus_mysql_list_tables($p1,$p2);}
386
387 function nucleus_mysql_affected_rows($p1=null){
388         global $SQLITE_DBHANDLE;
389         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
390         return sqlite_changes($dbhandle);
391 }
392
393 function nucleus_mysql_error($p1=null){
394         global $SQLITE_DBHANDLE;
395         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
396         return sqlite_error_string ( sqlite_last_error ($dbhandle) );
397 }
398
399 function nucleus_mysql_fetch_array($p1,$p2=SQLITE_BOTH){
400         return sqlite_fetch_array ($p1,$p2);
401 }
402
403 function nucleus_mysql_fetch_assoc($p1){
404         return sqlite_fetch_array($p1,SQLITE_ASSOC);
405 }
406
407 function nucleus_mysql_fetch_object($p1,$p2=SQLITE_BOTH){
408         if (is_array($ret=sqlite_fetch_array ($p1,$p2))) {
409                 $o=new SQLITE_OBJECT;
410                 foreach ($ret as $key=>$value) {
411                         if (strstr($key,'.')) {// Remove table name.
412                                 $key=preg_replace('/^(.+)\."(.+)"$/','"$2"',$key);
413                                 $key=preg_replace('/^(.+)\.([^.^"]+)$/','$2',$key);
414                         }
415                         $o->$key=$value;
416                 }
417                 return $o;
418         } else return false;
419 }
420
421 function nucleus_mysql_fetch_row($p1){
422         return sqlite_fetch_array($p1,SQLITE_NUM);
423 }
424
425 function nucleus_mysql_field_name($p1,$p2){
426         return sqlite_field_name ($p1,$p2);
427 }
428
429 function nucleus_mysql_free_result($p1){
430         // ???? Cannot find corresponding function of SQLite.
431         // Maybe SQLite is NOT used for the high spec server
432         // that need mysql_free_result() function because of
433         // many SQL-queries in a script.
434         return true;
435 }
436
437 function nucleus_mysql_insert_id($p1=null){
438         global $SQLITE_DBHANDLE;
439         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
440         return sqlite_last_insert_rowid ($dbhandle);
441 }
442
443 function nucleus_mysql_num_fields($p1){
444         return sqlite_num_fields ($p1);
445 }
446
447 function nucleus_mysql_num_rows($p1){
448         return sqlite_num_rows ($p1);
449 }
450 function nucleus_mysql_numrows($p1){
451         return sqlite_num_rows ($p1);
452 }
453
454 function nucleus_mysql_result($p1,$p2,$p3=null){
455         if ($p3) return sqlite_ReturnWithError('nucleus_mysql_result');
456         if (!$p2) return sqlite_fetch_single ($p1);
457         $a=sqlite_fetch_array ($p1);
458         return $a[$p2];
459 }
460
461 function nucleus_mysql_unbuffered_query($p1,$p2=null){
462         return nucleus_mysql_query($p1,$p2,true);
463 }
464
465 function nucleus_mysql_client_encoding($p1=null){
466         return sqlite_libencoding();
467 }
468
469 function nucleus_mysql_data_seek($p1,$p2) {
470         return sqlite_seek($p1,$p2);
471 }
472
473 function nucleus_mysql_errno ($p1=null){
474         global $SQLITE_DBHANDLE;
475         if (!($dbhandle=$p1)) $dbhandle=$SQLITE_DBHANDLE;
476         return sqlite_last_error($dbhandle);
477 }
478
479 function nucleus_mysql_escape_string ($p1){
480         // The "'" will be changed to "''".
481         // This way works for both MySQL and SQLite when single quotes are used for string.
482         // Note that single quote is always used in this wrapper.
483         // If a plugin is made on SQLite-Nucleus and such plugin will be used for MySQL-Nucleus,
484         // nucleus_mysql_escape_string() will be changed to mysql_escape_string() and
485         // this routine won't be used, so this way won't be problem.
486         return sqlite_escape_string($p1);
487 }
488
489 function nucleus_mysql_real_escape_string ($p1,$p2=null){
490         //addslashes used here.
491         return addslashes($p1);
492 }
493
494 function nucleus_mysql_create_db ($p1,$p2=null){
495         // All prameters are ignored.
496         // Returns always true;
497         return true;
498 }
499
500 function nucleus_mysql_pconnect($p1=null,$p2=null,$p3=null,$p4=null,$p5=null){
501         global $SQLITE_DBHANDLE,$SQLITECONF;
502         sqlite_close ($SQLITE_DBHANDLE);
503         $SQLITE_DBHANDLE=sqlite_popen($SQLITECONF['DBFILENAME']);
504         return ($SQLITE['DBHANDLE']=$SQLITE_DBHANDLE);
505 }
506
507 function nucleus_mysql_fetch_field($p1,$p2=null){
508         if ($p2) return sqlite_ReturnWithError('nucleus_mysql_fetch_field');
509         // Only 'name' is supported.
510         $o=new SQLITE_OBJECT;
511         $o->name=array();
512         if(is_array($ret=sqlite_fetch_array ($p1,SQLITE_ASSOC )))
513                 foreach ($ret as $key=>$value) {
514                         if (is_string($key)) array_push($o->name,$key);
515                 }
516         return $o;
517
518 }
519
520 // This function is called instead of _execute_queries() in backp.php
521 function sqlite_restore_execute_queries(&$query){
522         global $DIR_NUCLEUS,$DIR_LIBS,$DIR_PLUGINS,$CONF;
523         
524         // Skip until the first "#" or "--"
525         if (($i=strpos($query,"\n#"))===false) $i=strlen($query);
526         if (($j=strpos($query,"\n--"))===false) $j=strlen($query);
527         if ($i<$j) $query=substr($query,$i+1);
528         else  $query=substr($query,$j+1);
529         
530         // Save the query to temporary file in sqlite directory.
531         if (function_exists('microtime')) {
532                 $prefix=preg_replace('/[^0-9]/','',microtime());
533         } else {
534                 srand(time());
535                 $prefix=(string)rand(0,999999);
536         }
537         $tmpname=tempnam($DIR_NUCLEUS.'sqlite/',"tmp$prefix");
538         if (!($handle=@fopen($tmpname,'w'))) return 'Cannot save temporary DB file.';
539         fwrite($handle,$query);
540         fclose($handle);
541         $tmpname=preg_replace('/[\s\S]*?[\/\\\\]([^\/\\\\]+)$/','$1',$tmpname);
542         
543         // Read the option from NP_SQLite
544         if (!class_exists('NucleusPlugin')) { include($DIR_LIBS.'PLUGIN.php');}
545         if (!class_exists('NP_SQLite')) { include($DIR_PLUGINS.'NP_SQLite.php'); }
546         $p=new NP_SQLite();
547         if (!($numatonce=@$p->getOption('numatonce'))) $numatonce=20;
548         if (!($refreshwait=@$p->getOption('refreshwait'))) $refreshwait=1;
549         
550         // Start process.
551         $url="plugins/sqlite/restore.php?dbfile=$tmpname&numatonce=$numatonce&refreshwait=$refreshwait";
552         header('HTTP/1.0 301 Moved Permanently');
553         header('Location: '.$url);
554         exit('<html><body>Moved Permanently</body></html>');
555 }
556
557 ?>