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