source: trunk/forum/includes/acp/acp_database.php

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 55.8 KB
Line 
1<?php
2/**
3*
4* @package acp
5* @version $Id$
6* @copyright (c) 2005 phpBB Group
7* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8*
9*/
10
11/**
12* @ignore
13*/
14if (!defined('IN_PHPBB'))
15{
16 exit;
17}
18
19/**
20* @package acp
21*/
22class acp_database
23{
24 var $u_action;
25
26 function main($id, $mode)
27 {
28 global $cache, $db, $user, $auth, $template, $table_prefix;
29 global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
30
31 $user->add_lang('acp/database');
32
33 $this->tpl_name = 'acp_database';
34 $this->page_title = 'ACP_DATABASE';
35
36 $action = request_var('action', '');
37 $submit = (isset($_POST['submit'])) ? true : false;
38
39 $template->assign_vars(array(
40 'MODE' => $mode
41 ));
42
43 switch ($mode)
44 {
45 case 'backup':
46
47 $this->page_title = 'ACP_BACKUP';
48
49 switch ($action)
50 {
51 case 'download':
52 $type = request_var('type', '');
53 $table = request_var('table', array(''));
54 $format = request_var('method', '');
55 $where = request_var('where', '');
56
57 if (!sizeof($table))
58 {
59 trigger_error($user->lang['TABLE_SELECT_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
60 }
61
62 $store = $download = $structure = $schema_data = false;
63
64 if ($where == 'store_and_download' || $where == 'store')
65 {
66 $store = true;
67 }
68
69 if ($where == 'store_and_download' || $where == 'download')
70 {
71 $download = true;
72 }
73
74 if ($type == 'full' || $type == 'structure')
75 {
76 $structure = true;
77 }
78
79 if ($type == 'full' || $type == 'data')
80 {
81 $schema_data = true;
82 }
83
84 @set_time_limit(1200);
85 @set_time_limit(0);
86
87 $time = time();
88
89 $filename = 'backup_' . $time . '_' . unique_id();
90 switch ($db->sql_layer)
91 {
92 case 'mysqli':
93 case 'mysql4':
94 case 'mysql':
95 $extractor = new mysql_extractor($download, $store, $format, $filename, $time);
96 break;
97
98 case 'sqlite':
99 $extractor = new sqlite_extractor($download, $store, $format, $filename, $time);
100 break;
101
102 case 'postgres':
103 $extractor = new postgres_extractor($download, $store, $format, $filename, $time);
104 break;
105
106 case 'oracle':
107 $extractor = new oracle_extractor($download, $store, $format, $filename, $time);
108 break;
109
110 case 'mssql':
111 case 'mssql_odbc':
112 $extractor = new mssql_extractor($download, $store, $format, $filename, $time);
113 break;
114
115 case 'firebird':
116 $extractor = new firebird_extractor($download, $store, $format, $filename, $time);
117 break;
118 }
119
120 $extractor->write_start($table_prefix);
121
122 foreach ($table as $table_name)
123 {
124 // Get the table structure
125 if ($structure)
126 {
127 $extractor->write_table($table_name);
128 }
129 else
130 {
131 // We might wanna empty out all that junk :D
132 switch ($db->sql_layer)
133 {
134 case 'sqlite':
135 case 'firebird':
136 $extractor->flush('DELETE FROM ' . $table_name . ";\n");
137 break;
138
139 case 'mssql':
140 case 'mssql_odbc':
141 $extractor->flush('TRUNCATE TABLE ' . $table_name . "GO\n");
142 break;
143
144 case 'oracle':
145 $extractor->flush('TRUNCATE TABLE ' . $table_name . "/\n");
146 break;
147
148 default:
149 $extractor->flush('TRUNCATE TABLE ' . $table_name . ";\n");
150 break;
151 }
152 }
153
154 // Data
155 if ($schema_data)
156 {
157 $extractor->write_data($table_name);
158 }
159 }
160
161 $extractor->write_end();
162
163 add_log('admin', 'LOG_DB_BACKUP');
164
165 if ($download == true)
166 {
167 exit;
168 }
169
170 trigger_error($user->lang['BACKUP_SUCCESS'] . adm_back_link($this->u_action));
171 break;
172
173 default:
174 include($phpbb_root_path . 'includes/functions_install.' . $phpEx);
175 $tables = get_tables($db);
176 asort($tables);
177 foreach ($tables as $table_name)
178 {
179 if (strlen($table_prefix) === 0 || stripos($table_name, $table_prefix) === 0)
180 {
181 $template->assign_block_vars('tables', array(
182 'TABLE' => $table_name
183 ));
184 }
185 }
186 unset($tables);
187
188 $template->assign_vars(array(
189 'U_ACTION' => $this->u_action . '&amp;action=download'
190 ));
191
192 $available_methods = array('gzip' => 'zlib', 'bzip2' => 'bz2');
193
194 foreach ($available_methods as $type => $module)
195 {
196 if (!@extension_loaded($module))
197 {
198 continue;
199 }
200
201 $template->assign_block_vars('methods', array(
202 'TYPE' => $type
203 ));
204 }
205
206 $template->assign_block_vars('methods', array(
207 'TYPE' => 'text'
208 ));
209 break;
210 }
211 break;
212
213 case 'restore':
214
215 $this->page_title = 'ACP_RESTORE';
216
217 switch ($action)
218 {
219 case 'submit':
220 $delete = request_var('delete', '');
221 $file = request_var('file', '');
222
223 if (!preg_match('#^backup_\d{10,}_[a-z\d]{16}\.(sql(?:\.(?:gz|bz2))?)$#', $file, $matches))
224 {
225 trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
226 }
227
228 $file_name = $phpbb_root_path . 'store/' . $matches[0];
229
230 if (!file_exists($file_name) || !is_readable($file_name))
231 {
232 trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
233 }
234
235 if ($delete)
236 {
237 if (confirm_box(true))
238 {
239 unlink($file_name);
240 add_log('admin', 'LOG_DB_DELETE');
241 trigger_error($user->lang['BACKUP_DELETE'] . adm_back_link($this->u_action));
242 }
243 else
244 {
245 confirm_box(false, $user->lang['DELETE_SELECTED_BACKUP'], build_hidden_fields(array('delete' => $delete, 'file' => $file)));
246 }
247 }
248 else
249 {
250 $download = request_var('download', '');
251
252 if ($download)
253 {
254 $name = $matches[0];
255
256 switch ($matches[1])
257 {
258 case 'sql':
259 $mimetype = 'text/x-sql';
260 break;
261 case 'sql.bz2':
262 $mimetype = 'application/x-bzip2';
263 break;
264 case 'sql.gz':
265 $mimetype = 'application/x-gzip';
266 break;
267 }
268
269 header('Pragma: no-cache');
270 header("Content-Type: $mimetype; name=\"$name\"");
271 header("Content-disposition: attachment; filename=$name");
272
273 @set_time_limit(0);
274
275 $fp = @fopen($file_name, 'rb');
276
277 if ($fp !== false)
278 {
279 while (!feof($fp))
280 {
281 echo fread($fp, 8192);
282 }
283 fclose($fp);
284 }
285
286 flush();
287 exit;
288 }
289
290 switch ($matches[1])
291 {
292 case 'sql':
293 $fp = fopen($file_name, 'rb');
294 $read = 'fread';
295 $seek = 'fseek';
296 $eof = 'feof';
297 $close = 'fclose';
298 $fgetd = 'fgetd';
299 break;
300
301 case 'sql.bz2':
302 $fp = bzopen($file_name, 'r');
303 $read = 'bzread';
304 $seek = '';
305 $eof = 'feof';
306 $close = 'bzclose';
307 $fgetd = 'fgetd_seekless';
308 break;
309
310 case 'sql.gz':
311 $fp = gzopen($file_name, 'rb');
312 $read = 'gzread';
313 $seek = 'gzseek';
314 $eof = 'gzeof';
315 $close = 'gzclose';
316 $fgetd = 'fgetd';
317 break;
318 }
319
320 switch ($db->sql_layer)
321 {
322 case 'mysql':
323 case 'mysql4':
324 case 'mysqli':
325 case 'sqlite':
326 while (($sql = $fgetd($fp, ";\n", $read, $seek, $eof)) !== false)
327 {
328 $db->sql_query($sql);
329 }
330 break;
331
332 case 'firebird':
333 $delim = ";\n";
334 while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
335 {
336 $query = trim($sql);
337 if (substr($query, 0, 8) === 'SET TERM')
338 {
339 $delim = $query[9] . "\n";
340 continue;
341 }
342 $db->sql_query($query);
343 }
344 break;
345
346 case 'postgres':
347 $delim = ";\n";
348 while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
349 {
350 $query = trim($sql);
351
352 if (substr($query, 0, 13) == 'CREATE DOMAIN')
353 {
354 list(, , $domain) = explode(' ', $query);
355 $sql = "SELECT domain_name
356 FROM information_schema.domains
357 WHERE domain_name = '$domain';";
358 $result = $db->sql_query($sql);
359 if (!$db->sql_fetchrow($result))
360 {
361 $db->sql_query($query);
362 }
363 $db->sql_freeresult($result);
364 }
365 else
366 {
367 $db->sql_query($query);
368 }
369
370 if (substr($query, 0, 4) == 'COPY')
371 {
372 while (($sub = $fgetd($fp, "\n", $read, $seek, $eof)) !== '\.')
373 {
374 if ($sub === false)
375 {
376 trigger_error($user->lang['RESTORE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
377 }
378 pg_put_line($db->db_connect_id, $sub . "\n");
379 }
380 pg_put_line($db->db_connect_id, "\\.\n");
381 pg_end_copy($db->db_connect_id);
382 }
383 }
384 break;
385
386 case 'oracle':
387 while (($sql = $fgetd($fp, "/\n", $read, $seek, $eof)) !== false)
388 {
389 $db->sql_query($sql);
390 }
391 break;
392
393 case 'mssql':
394 case 'mssql_odbc':
395 while (($sql = $fgetd($fp, "GO\n", $read, $seek, $eof)) !== false)
396 {
397 $db->sql_query($sql);
398 }
399 break;
400 }
401
402 $close($fp);
403
404 // Purge the cache due to updated data
405 $cache->purge();
406
407 add_log('admin', 'LOG_DB_RESTORE');
408 trigger_error($user->lang['RESTORE_SUCCESS'] . adm_back_link($this->u_action));
409 break;
410 }
411
412 default:
413 $methods = array('sql');
414 $available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2');
415
416 foreach ($available_methods as $type => $module)
417 {
418 if (!@extension_loaded($module))
419 {
420 continue;
421 }
422 $methods[] = $type;
423 }
424
425 $dir = $phpbb_root_path . 'store/';
426 $dh = @opendir($dir);
427
428 $backup_files = array();
429
430 if ($dh)
431 {
432 while (($file = readdir($dh)) !== false)
433 {
434 if (preg_match('#^backup_(\d{10,})_[a-z\d]{16}\.(sql(?:\.(?:gz|bz2))?)$#', $file, $matches))
435 {
436 if (in_array($matches[2], $methods))
437 {
438 $backup_files[gmdate("d-m-Y H:i:s", $matches[1])] = $file;
439 }
440 }
441 }
442 closedir($dh);
443 }
444
445 if (!empty($backup_files))
446 {
447 krsort($backup_files);
448
449 foreach ($backup_files as $name => $file)
450 {
451 $template->assign_block_vars('files', array(
452 'FILE' => $file,
453 'NAME' => $name,
454 'SUPPORTED' => true,
455 ));
456 }
457 }
458
459 $template->assign_vars(array(
460 'U_ACTION' => $this->u_action . '&amp;action=submit'
461 ));
462 break;
463 }
464 break;
465 }
466 }
467}
468
469/**
470* @package acp
471*/
472class base_extractor
473{
474 var $fh;
475 var $fp;
476 var $write;
477 var $close;
478 var $store;
479 var $download;
480 var $time;
481 var $format;
482 var $run_comp = false;
483
484 function base_extractor($download = false, $store = false, $format, $filename, $time)
485 {
486 $this->download = $download;
487 $this->store = $store;
488 $this->time = $time;
489 $this->format = $format;
490
491 switch ($format)
492 {
493 case 'text':
494 $ext = '.sql';
495 $open = 'fopen';
496 $this->write = 'fwrite';
497 $this->close = 'fclose';
498 $mimetype = 'text/x-sql';
499 break;
500 case 'bzip2':
501 $ext = '.sql.bz2';
502 $open = 'bzopen';
503 $this->write = 'bzwrite';
504 $this->close = 'bzclose';
505 $mimetype = 'application/x-bzip2';
506 break;
507 case 'gzip':
508 $ext = '.sql.gz';
509 $open = 'gzopen';
510 $this->write = 'gzwrite';
511 $this->close = 'gzclose';
512 $mimetype = 'application/x-gzip';
513 break;
514 }
515
516 if ($download == true)
517 {
518 $name = $filename . $ext;
519 header('Pragma: no-cache');
520 header("Content-Type: $mimetype; name=\"$name\"");
521 header("Content-disposition: attachment; filename=$name");
522
523 switch ($format)
524 {
525 case 'bzip2':
526 ob_start();
527 break;
528
529 case 'gzip':
530 if ((isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) && strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'msie') === false)
531 {
532 ob_start('ob_gzhandler');
533 }
534 else
535 {
536 $this->run_comp = true;
537 }
538 break;
539 }
540 }
541
542 if ($store == true)
543 {
544 global $phpbb_root_path;
545 $file = $phpbb_root_path . 'store/' . $filename . $ext;
546
547 $this->fp = $open($file, 'w');
548
549 if (!$this->fp)
550 {
551 trigger_error('FILE_WRITE_FAIL', E_USER_ERROR);
552 }
553 }
554 }
555
556 function write_end()
557 {
558 static $close;
559
560 if ($this->store)
561 {
562 if ($close === null)
563 {
564 $close = $this->close;
565 }
566 $close($this->fp);
567 }
568
569 // bzip2 must be written all the way at the end
570 if ($this->download && $this->format === 'bzip2')
571 {
572 $c = ob_get_clean();
573 echo bzcompress($c);
574 }
575 }
576
577 function flush($data)
578 {
579 static $write;
580 if ($this->store === true)
581 {
582 if ($write === null)
583 {
584 $write = $this->write;
585 }
586 $write($this->fp, $data);
587 }
588
589 if ($this->download === true)
590 {
591 if ($this->format === 'bzip2' || $this->format === 'text' || ($this->format === 'gzip' && !$this->run_comp))
592 {
593 echo $data;
594 }
595
596 // we can write the gzip data as soon as we get it
597 if ($this->format === 'gzip')
598 {
599 if ($this->run_comp)
600 {
601 echo gzencode($data);
602 }
603 else
604 {
605 ob_flush();
606 flush();
607 }
608 }
609 }
610 }
611}
612
613/**
614* @package acp
615*/
616class mysql_extractor extends base_extractor
617{
618 function write_start($table_prefix)
619 {
620 $sql_data = "#\n";
621 $sql_data .= "# phpBB Backup Script\n";
622 $sql_data .= "# Dump of tables for $table_prefix\n";
623 $sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
624 $sql_data .= "#\n";
625 $this->flush($sql_data);
626 }
627
628 function write_table($table_name)
629 {
630 global $db;
631 static $new_extract;
632
633 if ($new_extract === null)
634 {
635 if ($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '3.23.20', '>='))
636 {
637 $new_extract = true;
638 }
639 else
640 {
641 $new_extract = false;
642 }
643 }
644
645 if ($new_extract)
646 {
647 $this->new_write_table($table_name);
648 }
649 else
650 {
651 $this->old_write_table($table_name);
652 }
653 }
654
655 function write_data($table_name)
656 {
657 global $db;
658 if ($db->sql_layer === 'mysqli')
659 {
660 $this->write_data_mysqli($table_name);
661 }
662 else
663 {
664 $this->write_data_mysql($table_name);
665 }
666 }
667
668 function write_data_mysqli($table_name)
669 {
670 global $db;
671 $sql = "SELECT *
672 FROM $table_name";
673 $result = mysqli_query($db->db_connect_id, $sql, MYSQLI_USE_RESULT);
674 if ($result != false)
675 {
676 $fields_cnt = mysqli_num_fields($result);
677
678 // Get field information
679 $field = mysqli_fetch_fields($result);
680 $field_set = array();
681
682 for ($j = 0; $j < $fields_cnt; $j++)
683 {
684 $field_set[] = $field[$j]->name;
685 }
686
687 $search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
688 $replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
689 $fields = implode(', ', $field_set);
690 $sql_data = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
691 $first_set = true;
692 $query_len = 0;
693 $max_len = get_usable_memory();
694
695 while ($row = mysqli_fetch_row($result))
696 {
697 $values = array();
698 if ($first_set)
699 {
700 $query = $sql_data . '(';
701 }
702 else
703 {
704 $query .= ',(';
705 }
706
707 for ($j = 0; $j < $fields_cnt; $j++)
708 {
709 if (!isset($row[$j]) || is_null($row[$j]))
710 {
711 $values[$j] = 'NULL';
712 }
713 else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024))
714 {
715 $values[$j] = $row[$j];
716 }
717 else
718 {
719 $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
720 }
721 }
722 $query .= implode(', ', $values) . ')';
723
724 $query_len += strlen($query);
725 if ($query_len > $max_len)
726 {
727 $this->flush($query . ";\n\n");
728 $query = '';
729 $query_len = 0;
730 $first_set = true;
731 }
732 else
733 {
734 $first_set = false;
735 }
736 }
737 mysqli_free_result($result);
738
739 // check to make sure we have nothing left to flush
740 if (!$first_set && $query)
741 {
742 $this->flush($query . ";\n\n");
743 }
744 }
745 }
746
747 function write_data_mysql($table_name)
748 {
749 global $db;
750 $sql = "SELECT *
751 FROM $table_name";
752 $result = mysql_unbuffered_query($sql, $db->db_connect_id);
753
754 if ($result != false)
755 {
756 $fields_cnt = mysql_num_fields($result);
757
758 // Get field information
759 $field = array();
760 for ($i = 0; $i < $fields_cnt; $i++)
761 {
762 $field[] = mysql_fetch_field($result, $i);
763 }
764 $field_set = array();
765
766 for ($j = 0; $j < $fields_cnt; $j++)
767 {
768 $field_set[] = $field[$j]->name;
769 }
770
771 $search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
772 $replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
773 $fields = implode(', ', $field_set);
774 $sql_data = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
775 $first_set = true;
776 $query_len = 0;
777 $max_len = get_usable_memory();
778
779 while ($row = mysql_fetch_row($result))
780 {
781 $values = array();
782 if ($first_set)
783 {
784 $query = $sql_data . '(';
785 }
786 else
787 {
788 $query .= ',(';
789 }
790
791 for ($j = 0; $j < $fields_cnt; $j++)
792 {
793 if (!isset($row[$j]) || is_null($row[$j]))
794 {
795 $values[$j] = 'NULL';
796 }
797 else if ($field[$j]->numeric && ($field[$j]->type !== 'timestamp'))
798 {
799 $values[$j] = $row[$j];
800 }
801 else
802 {
803 $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
804 }
805 }
806 $query .= implode(', ', $values) . ')';
807
808 $query_len += strlen($query);
809 if ($query_len > $max_len)
810 {
811 $this->flush($query . ";\n\n");
812 $query = '';
813 $query_len = 0;
814 $first_set = true;
815 }
816 else
817 {
818 $first_set = false;
819 }
820 }
821 mysql_free_result($result);
822
823 // check to make sure we have nothing left to flush
824 if (!$first_set && $query)
825 {
826 $this->flush($query . ";\n\n");
827 }
828 }
829 }
830
831 function new_write_table($table_name)
832 {
833 global $db;
834
835 $sql = 'SHOW CREATE TABLE ' . $table_name;
836 $result = $db->sql_query($sql);
837 $row = $db->sql_fetchrow($result);
838
839 $sql_data = '# Table: ' . $table_name . "\n";
840 $sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
841 $this->flush($sql_data . $row['Create Table'] . ";\n\n");
842
843 $db->sql_freeresult($result);
844 }
845
846 function old_write_table($table_name)
847 {
848 global $db;
849
850 $sql_data = '# Table: ' . $table_name . "\n";
851 $sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
852 $sql_data .= "CREATE TABLE $table_name(\n";
853 $rows = array();
854
855 $sql = "SHOW FIELDS
856 FROM $table_name";
857 $result = $db->sql_query($sql);
858
859 while ($row = $db->sql_fetchrow($result))
860 {
861 $line = ' ' . $row['Field'] . ' ' . $row['Type'];
862
863 if (!is_null($row['Default']))
864 {
865 $line .= " DEFAULT '{$row['Default']}'";
866 }
867
868 if ($row['Null'] != 'YES')
869 {
870 $line .= ' NOT NULL';
871 }
872
873 if ($row['Extra'] != '')
874 {
875 $line .= ' ' . $row['Extra'];
876 }
877
878 $rows[] = $line;
879 }
880 $db->sql_freeresult($result);
881
882 $sql = "SHOW KEYS
883 FROM $table_name";
884
885 $result = $db->sql_query($sql);
886
887 $index = array();
888 while ($row = $db->sql_fetchrow($result))
889 {
890 $kname = $row['Key_name'];
891
892 if ($kname != 'PRIMARY')
893 {
894 if ($row['Non_unique'] == 0)
895 {
896 $kname = "UNIQUE|$kname";
897 }
898 }
899
900 if ($row['Sub_part'])
901 {
902 $row['Column_name'] .= '(' . $row['Sub_part'] . ')';
903 }
904 $index[$kname][] = $row['Column_name'];
905 }
906 $db->sql_freeresult($result);
907
908 foreach ($index as $key => $columns)
909 {
910 $line = ' ';
911
912 if ($key == 'PRIMARY')
913 {
914 $line .= 'PRIMARY KEY (' . implode(', ', $columns) . ')';
915 }
916 else if (strpos($key, 'UNIQUE') === 0)
917 {
918 $line .= 'UNIQUE ' . substr($key, 7) . ' (' . implode(', ', $columns) . ')';
919 }
920 else if (strpos($key, 'FULLTEXT') === 0)
921 {
922 $line .= 'FULLTEXT ' . substr($key, 9) . ' (' . implode(', ', $columns) . ')';
923 }
924 else
925 {
926 $line .= "KEY $key (" . implode(', ', $columns) . ')';
927 }
928
929 $rows[] = $line;
930 }
931
932 $sql_data .= implode(",\n", $rows);
933 $sql_data .= "\n);\n\n";
934
935 $this->flush($sql_data);
936 }
937}
938
939/**
940* @package acp
941*/
942class sqlite_extractor extends base_extractor
943{
944 function write_start($prefix)
945 {
946 $sql_data = "--\n";
947 $sql_data .= "-- phpBB Backup Script\n";
948 $sql_data .= "-- Dump of tables for $prefix\n";
949 $sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
950 $sql_data .= "--\n";
951 $sql_data .= "BEGIN TRANSACTION;\n";
952 $this->flush($sql_data);
953 }
954
955 function write_table($table_name)
956 {
957 global $db;
958 $sql_data = '-- Table: ' . $table_name . "\n";
959 $sql_data .= "DROP TABLE $table_name;\n";
960
961 $sql = "SELECT sql
962 FROM sqlite_master
963 WHERE type = 'table'
964 AND name = '" . $db->sql_escape($table_name) . "'
965 ORDER BY type DESC, name;";
966 $result = $db->sql_query($sql);
967 $row = $db->sql_fetchrow($result);
968 $db->sql_freeresult($result);
969
970 // Create Table
971 $sql_data .= $row['sql'] . ";\n";
972
973 $result = $db->sql_query("PRAGMA index_list('" . $db->sql_escape($table_name) . "');");
974
975 $ar = array();
976 while ($row = $db->sql_fetchrow($result))
977 {
978 $ar[] = $row;
979 }
980 $db->sql_freeresult($result);
981
982 foreach ($ar as $value)
983 {
984 if (strpos($value['name'], 'autoindex') !== false)
985 {
986 continue;
987 }
988
989 $result = $db->sql_query("PRAGMA index_info('" . $db->sql_escape($value['name']) . "');");
990
991 $fields = array();
992 while ($row = $db->sql_fetchrow($result))
993 {
994 $fields[] = $row['name'];
995 }
996 $db->sql_freeresult($result);
997
998 $sql_data .= 'CREATE ' . ($value['unique'] ? 'UNIQUE ' : '') . 'INDEX ' . $value['name'] . ' on ' . $table_name . ' (' . implode(', ', $fields) . ");\n";
999 }
1000
1001 $this->flush($sql_data . "\n");
1002 }
1003
1004 function write_data($table_name)
1005 {
1006 global $db;
1007 static $proper;
1008
1009 if (is_null($proper))
1010 {
1011 $proper = version_compare(PHP_VERSION, '5.1.3', '>=');
1012 }
1013
1014 if ($proper)
1015 {
1016 $col_types = sqlite_fetch_column_types($db->db_connect_id, $table_name);
1017 }
1018 else
1019 {
1020 $sql = "SELECT sql
1021 FROM sqlite_master
1022 WHERE type = 'table'
1023 AND name = '" . $table_name . "'";
1024 $table_data = sqlite_single_query($db->db_connect_id, $sql);
1025 $table_data = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', '', $table_data);
1026 $table_data = trim($table_data);
1027
1028 preg_match('#\((.*)\)#s', $table_data, $matches);
1029
1030 $table_cols = explode(',', trim($matches[1]));
1031 foreach ($table_cols as $declaration)
1032 {
1033 $entities = preg_split('#\s+#', trim($declaration));
1034 $column_name = preg_replace('/"?([^"]+)"?/', '\1', $entities[0]);
1035
1036 // Hit a primary key, those are not what we need :D
1037 if (empty($entities[1]) || (strtolower($entities[0]) === 'primary' && strtolower($entities[1]) === 'key'))
1038 {
1039 continue;
1040 }
1041 $col_types[$column_name] = $entities[1];
1042 }
1043 }
1044
1045 $sql = "SELECT *
1046 FROM $table_name";
1047 $result = sqlite_unbuffered_query($db->db_connect_id, $sql);
1048 $rows = sqlite_fetch_all($result, SQLITE_ASSOC);
1049 $sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES (';
1050 foreach ($rows as $row)
1051 {
1052 foreach ($row as $column_name => $column_data)
1053 {
1054 if (is_null($column_data))
1055 {
1056 $row[$column_name] = 'NULL';
1057 }
1058 else if ($column_data == '')
1059 {
1060 $row[$column_name] = "''";
1061 }
1062 else if (strpos($col_types[$column_name], 'text') !== false || strpos($col_types[$column_name], 'char') !== false || strpos($col_types[$column_name], 'blob') !== false)
1063 {
1064 $row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data));
1065 }
1066 }
1067 $this->flush($sql_insert . implode(', ', $row) . ");\n");
1068 }
1069 }
1070
1071 function write_end()
1072 {
1073 $this->flush("COMMIT;\n");
1074 parent::write_end();
1075 }
1076}
1077
1078/**
1079* @package acp
1080*/
1081class postgres_extractor extends base_extractor
1082{
1083 function write_start($prefix)
1084 {
1085 $sql_data = "--\n";
1086 $sql_data .= "-- phpBB Backup Script\n";
1087 $sql_data .= "-- Dump of tables for $prefix\n";
1088 $sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
1089 $sql_data .= "--\n";
1090 $sql_data .= "BEGIN TRANSACTION;\n";
1091 $this->flush($sql_data);
1092 }
1093
1094 function write_table($table_name)
1095 {
1096 global $db;
1097 static $domains_created = array();
1098
1099 $sql = "SELECT a.domain_name, a.data_type, a.character_maximum_length, a.domain_default
1100 FROM INFORMATION_SCHEMA.domains a, INFORMATION_SCHEMA.column_domain_usage b
1101 WHERE a.domain_name = b.domain_name
1102 AND b.table_name = '{$table_name}'";
1103 $result = $db->sql_query($sql);
1104 while ($row = $db->sql_fetchrow($result))
1105 {
1106 if (empty($domains_created[$row['domain_name']]))
1107 {
1108 $domains_created[$row['domain_name']] = true;
1109 //$sql_data = "DROP DOMAIN {$row['domain_name']};\n";
1110 $sql_data = "CREATE DOMAIN {$row['domain_name']} as {$row['data_type']}";
1111 if (!empty($row['character_maximum_length']))
1112 {
1113 $sql_data .= '(' . $row['character_maximum_length'] . ')';
1114 }
1115 $sql_data .= ' NOT NULL';
1116 if (!empty($row['domain_default']))
1117 {
1118 $sql_data .= ' DEFAULT ' . $row['domain_default'];
1119 }
1120 $this->flush($sql_data . ";\n");
1121 }
1122 }
1123
1124 $sql_data = '-- Table: ' . $table_name . "\n";
1125 $sql_data .= "DROP TABLE $table_name;\n";
1126 // PGSQL does not "tightly" bind sequences and tables, we must guess...
1127 $sql = "SELECT relname
1128 FROM pg_class
1129 WHERE relkind = 'S'
1130 AND relname = '{$table_name}_seq'";
1131 $result = $db->sql_query($sql);
1132 // We don't even care about storing the results. We already know the answer if we get rows back.
1133 if ($db->sql_fetchrow($result))
1134 {
1135 $sql_data .= "DROP SEQUENCE {$table_name}_seq;\n";
1136 $sql_data .= "CREATE SEQUENCE {$table_name}_seq;\n";
1137 }
1138 $db->sql_freeresult($result);
1139
1140 $field_query = "SELECT a.attnum, a.attname as field, t.typname as type, a.attlen as length, a.atttypmod as lengthvar, a.attnotnull as notnull
1141 FROM pg_class c, pg_attribute a, pg_type t
1142 WHERE c.relname = '" . $db->sql_escape($table_name) . "'
1143 AND a.attnum > 0
1144 AND a.attrelid = c.oid
1145 AND a.atttypid = t.oid
1146 ORDER BY a.attnum";
1147 $result = $db->sql_query($field_query);
1148
1149 $sql_data .= "CREATE TABLE $table_name(\n";
1150 $lines = array();
1151 while ($row = $db->sql_fetchrow($result))
1152 {
1153 // Get the data from the table
1154 $sql_get_default = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault
1155 FROM pg_attrdef d, pg_class c
1156 WHERE (c.relname = '" . $db->sql_escape($table_name) . "')
1157 AND (c.oid = d.adrelid)
1158 AND d.adnum = " . $row['attnum'];
1159 $def_res = $db->sql_query($sql_get_default);
1160 $def_row = $db->sql_fetchrow($def_res);
1161 $db->sql_freeresult($def_res);
1162
1163 if (empty($def_row))
1164 {
1165 unset($row['rowdefault']);
1166 }
1167 else
1168 {
1169 $row['rowdefault'] = $def_row['rowdefault'];
1170 }
1171
1172 if ($row['type'] == 'bpchar')
1173 {
1174 // Internally stored as bpchar, but isn't accepted in a CREATE TABLE statement.
1175 $row['type'] = 'char';
1176 }
1177
1178 $line = ' ' . $row['field'] . ' ' . $row['type'];
1179
1180 if (strpos($row['type'], 'char') !== false)
1181 {
1182 if ($row['lengthvar'] > 0)
1183 {
1184 $line .= '(' . ($row['lengthvar'] - 4) . ')';
1185 }
1186 }
1187
1188 if (strpos($row['type'], 'numeric') !== false)
1189 {
1190 $line .= '(';
1191 $line .= sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff));
1192 $line .= ')';
1193 }
1194
1195 if (isset($row['rowdefault']))
1196 {
1197 $line .= ' DEFAULT ' . $row['rowdefault'];
1198 }
1199
1200 if ($row['notnull'] == 't')
1201 {
1202 $line .= ' NOT NULL';
1203 }
1204
1205 $lines[] = $line;
1206 }
1207 $db->sql_freeresult($result);
1208
1209
1210 // Get the listing of primary keys.
1211 $sql_pri_keys = "SELECT ic.relname as index_name, bc.relname as tab_name, ta.attname as column_name, i.indisunique as unique_key, i.indisprimary as primary_key
1212 FROM pg_class bc, pg_class ic, pg_index i, pg_attribute ta, pg_attribute ia
1213 WHERE (bc.oid = i.indrelid)
1214 AND (ic.oid = i.indexrelid)
1215 AND (ia.attrelid = i.indexrelid)
1216 AND (ta.attrelid = bc.oid)
1217 AND (bc.relname = '" . $db->sql_escape($table_name) . "')
1218 AND (ta.attrelid = i.indrelid)
1219 AND (ta.attnum = i.indkey[ia.attnum-1])
1220 ORDER BY index_name, tab_name, column_name";
1221
1222 $result = $db->sql_query($sql_pri_keys);
1223
1224 $index_create = $index_rows = $primary_key = array();
1225
1226 // We do this in two steps. It makes placing the comma easier
1227 while ($row = $db->sql_fetchrow($result))
1228 {
1229 if ($row['primary_key'] == 't')
1230 {
1231 $primary_key[] = $row['column_name'];
1232 $primary_key_name = $row['index_name'];
1233 }
1234 else
1235 {
1236 // We have to store this all this info because it is possible to have a multi-column key...
1237 // we can loop through it again and build the statement
1238 $index_rows[$row['index_name']]['table'] = $table_name;
1239 $index_rows[$row['index_name']]['unique'] = ($row['unique_key'] == 't') ? true : false;
1240 $index_rows[$row['index_name']]['column_names'][] = $row['column_name'];
1241 }
1242 }
1243 $db->sql_freeresult($result);
1244
1245 if (!empty($index_rows))
1246 {
1247 foreach ($index_rows as $idx_name => $props)
1248 {
1249 $index_create[] = 'CREATE ' . ($props['unique'] ? 'UNIQUE ' : '') . "INDEX $idx_name ON $table_name (" . implode(', ', $props['column_names']) . ");";
1250 }
1251 }
1252
1253 if (!empty($primary_key))
1254 {
1255 $lines[] = " CONSTRAINT $primary_key_name PRIMARY KEY (" . implode(', ', $primary_key) . ")";
1256 }
1257
1258 // Generate constraint clauses for CHECK constraints
1259 $sql_checks = "SELECT conname as index_name, consrc
1260 FROM pg_constraint, pg_class bc
1261 WHERE conrelid = bc.oid
1262 AND bc.relname = '" . $db->sql_escape($table_name) . "'
1263 AND NOT EXISTS (
1264 SELECT *
1265 FROM pg_constraint as c, pg_inherits as i
1266 WHERE i.inhrelid = pg_constraint.conrelid
1267 AND c.conname = pg_constraint.conname
1268 AND c.consrc = pg_constraint.consrc
1269 AND c.conrelid = i.inhparent
1270 )";
1271 $result = $db->sql_query($sql_checks);
1272
1273 // Add the constraints to the sql file.
1274 while ($row = $db->sql_fetchrow($result))
1275 {
1276 if (!is_null($row['consrc']))
1277 {
1278 $lines[] = ' CONSTRAINT ' . $row['index_name'] . ' CHECK ' . $row['consrc'];
1279 }
1280 }
1281 $db->sql_freeresult($result);
1282
1283 $sql_data .= implode(", \n", $lines);
1284 $sql_data .= "\n);\n";
1285
1286 if (!empty($index_create))
1287 {
1288 $sql_data .= implode("\n", $index_create) . "\n\n";
1289 }
1290 $this->flush($sql_data);
1291 }
1292
1293 function write_data($table_name)
1294 {
1295 global $db;
1296 // Grab all of the data from current table.
1297 $sql = "SELECT *
1298 FROM $table_name";
1299 $result = $db->sql_query($sql);
1300
1301 $i_num_fields = pg_num_fields($result);
1302 $seq = '';
1303
1304 for ($i = 0; $i < $i_num_fields; $i++)
1305 {
1306 $ary_type[] = pg_field_type($result, $i);
1307 $ary_name[] = pg_field_name($result, $i);
1308
1309
1310 $sql = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault
1311 FROM pg_attrdef d, pg_class c
1312 WHERE (c.relname = '{$table_name}')
1313 AND (c.oid = d.adrelid)
1314 AND d.adnum = " . strval($i + 1);
1315 $result2 = $db->sql_query($sql);
1316 if ($row = $db->sql_fetchrow($result2))
1317 {
1318 // Determine if we must reset the sequences
1319 if (strpos($row['rowdefault'], "nextval('") === 0)
1320 {
1321 $seq .= "SELECT SETVAL('{$table_name}_seq',(select case when max({$ary_name[$i]})>0 then max({$ary_name[$i]})+1 else 1 end FROM {$table_name}));\n";
1322 }
1323 }
1324 }
1325
1326 $this->flush("COPY $table_name (" . implode(', ', $ary_name) . ') FROM stdin;' . "\n");
1327 while ($row = $db->sql_fetchrow($result))
1328 {
1329 $schema_vals = array();
1330
1331 // Build the SQL statement to recreate the data.
1332 for ($i = 0; $i < $i_num_fields; $i++)
1333 {
1334 $str_val = $row[$ary_name[$i]];
1335
1336 if (preg_match('#char|text|bool|bytea#i', $ary_type[$i]))
1337 {
1338 $str_val = str_replace(array("\n", "\t", "\r", "\b", "\f", "\v"), array('\n', '\t', '\r', '\b', '\f', '\v'), addslashes($str_val));
1339 $str_empty = '';
1340 }
1341 else
1342 {
1343 $str_empty = '\N';
1344 }
1345
1346 if (empty($str_val) && $str_val !== '0')
1347 {
1348 $str_val = $str_empty;
1349 }
1350
1351 $schema_vals[] = $str_val;
1352 }
1353
1354 // Take the ordered fields and their associated data and build it
1355 // into a valid sql statement to recreate that field in the data.
1356 $this->flush(implode("\t", $schema_vals) . "\n");
1357 }
1358 $db->sql_freeresult($result);
1359 $this->flush("\\.\n");
1360
1361 // Write out the sequence statements
1362 $this->flush($seq);
1363 }
1364
1365 function write_end()
1366 {
1367 $this->flush("COMMIT;\n");
1368 parent::write_end();
1369 }
1370}
1371
1372/**
1373* @package acp
1374*/
1375class mssql_extractor extends base_extractor
1376{
1377 function write_end()
1378 {
1379 $this->flush("COMMIT\nGO\n");
1380 parent::write_end();
1381 }
1382
1383 function write_start($prefix)
1384 {
1385 $sql_data = "--\n";
1386 $sql_data .= "-- phpBB Backup Script\n";
1387 $sql_data .= "-- Dump of tables for $prefix\n";
1388 $sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
1389 $sql_data .= "--\n";
1390 $sql_data .= "BEGIN TRANSACTION\n";
1391 $sql_data .= "GO\n";
1392 $this->flush($sql_data);
1393 }
1394
1395 function write_table($table_name)
1396 {
1397 global $db;
1398 $sql_data = '-- Table: ' . $table_name . "\n";
1399 $sql_data .= "IF OBJECT_ID(N'$table_name', N'U') IS NOT NULL\n";
1400 $sql_data .= "DROP TABLE $table_name;\n";
1401 $sql_data .= "GO\n";
1402 $sql_data .= "\nCREATE TABLE [$table_name] (\n";
1403 $rows = array();
1404
1405 $text_flag = false;
1406
1407 $sql = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IS_IDENTITY
1408 FROM INFORMATION_SCHEMA.COLUMNS
1409 WHERE TABLE_NAME = '$table_name'";
1410 $result = $db->sql_query($sql);
1411
1412 while ($row = $db->sql_fetchrow($result))
1413 {
1414 $line = "\t[{$row['COLUMN_NAME']}] [{$row['DATA_TYPE']}]";
1415
1416 if ($row['DATA_TYPE'] == 'text')
1417 {
1418 $text_flag = true;
1419 }
1420
1421 if ($row['IS_IDENTITY'])
1422 {
1423 $line .= ' IDENTITY (1 , 1)';
1424 }
1425
1426 if ($row['CHARACTER_MAXIMUM_LENGTH'] && $row['DATA_TYPE'] !== 'text')
1427 {
1428 $line .= ' (' . $row['CHARACTER_MAXIMUM_LENGTH'] . ')';
1429 }
1430
1431 if ($row['IS_NULLABLE'] == 'YES')
1432 {
1433 $line .= ' NULL';
1434 }
1435 else
1436 {
1437 $line .= ' NOT NULL';
1438 }
1439
1440 if ($row['COLUMN_DEFAULT'])
1441 {
1442 $line .= ' DEFAULT ' . $row['COLUMN_DEFAULT'];
1443 }
1444
1445 $rows[] = $line;
1446 }
1447 $db->sql_freeresult($result);
1448
1449 $sql_data .= implode(",\n", $rows);
1450 $sql_data .= "\n) ON [PRIMARY]";
1451
1452 if ($text_flag)
1453 {
1454 $sql_data .= " TEXTIMAGE_ON [PRIMARY]";
1455 }
1456
1457 $sql_data .= "\nGO\n\n";
1458 $rows = array();
1459
1460 $sql = "SELECT CONSTRAINT_NAME, COLUMN_NAME
1461 FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
1462 WHERE TABLE_NAME = '$table_name'";
1463 $result = $db->sql_query($sql);
1464 while ($row = $db->sql_fetchrow($result))
1465 {
1466 if (!sizeof($rows))
1467 {
1468 $sql_data .= "ALTER TABLE [$table_name] WITH NOCHECK ADD\n";
1469 $sql_data .= "\tCONSTRAINT [{$row['CONSTRAINT_NAME']}] PRIMARY KEY CLUSTERED \n\t(\n";
1470 }
1471 $rows[] = "\t\t[{$row['COLUMN_NAME']}]";
1472 }
1473 if (sizeof($rows))
1474 {
1475 $sql_data .= implode(",\n", $rows);
1476 $sql_data .= "\n\t) ON [PRIMARY] \nGO\n";
1477 }
1478 $db->sql_freeresult($result);
1479
1480 $index = array();
1481 $sql = "EXEC sp_statistics '$table_name'";
1482 $result = $db->sql_query($sql);
1483 while ($row = $db->sql_fetchrow($result))
1484 {
1485 if ($row['TYPE'] == 3)
1486 {
1487 $index[$row['INDEX_NAME']][] = '[' . $row['COLUMN_NAME'] . ']';
1488 }
1489 }
1490 $db->sql_freeresult($result);
1491
1492 foreach ($index as $index_name => $column_name)
1493 {
1494 $index[$index_name] = implode(', ', $column_name);
1495 }
1496
1497 foreach ($index as $index_name => $columns)
1498 {
1499 $sql_data .= "\nCREATE INDEX [$index_name] ON [$table_name]($columns) ON [PRIMARY]\nGO\n";
1500 }
1501 $this->flush($sql_data);
1502 }
1503
1504 function write_data($table_name)
1505 {
1506 global $db;
1507
1508 if ($db->sql_layer === 'mssql')
1509 {
1510 $this->write_data_mssql($table_name);
1511 }
1512 else
1513 {
1514 $this->write_data_odbc($table_name);
1515 }
1516 }
1517
1518 function write_data_mssql($table_name)
1519 {
1520 global $db;
1521 $ary_type = $ary_name = array();
1522 $ident_set = false;
1523 $sql_data = '';
1524
1525 // Grab all of the data from current table.
1526 $sql = "SELECT *
1527 FROM $table_name";
1528 $result = $db->sql_query($sql);
1529
1530 $retrieved_data = mssql_num_rows($result);
1531
1532 $i_num_fields = mssql_num_fields($result);
1533
1534 for ($i = 0; $i < $i_num_fields; $i++)
1535 {
1536 $ary_type[$i] = mssql_field_type($result, $i);
1537 $ary_name[$i] = mssql_field_name($result, $i);
1538 }
1539
1540 if ($retrieved_data)
1541 {
1542 $sql = "SELECT 1 as has_identity
1543 FROM INFORMATION_SCHEMA.COLUMNS
1544 WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
1545 $result2 = $db->sql_query($sql);
1546 $row2 = $db->sql_fetchrow($result2);
1547 if (!empty($row2['has_identity']))
1548 {
1549 $sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
1550 $ident_set = true;
1551 }
1552 $db->sql_freeresult($result2);
1553 }
1554
1555 while ($row = $db->sql_fetchrow($result))
1556 {
1557 $schema_vals = $schema_fields = array();
1558
1559 // Build the SQL statement to recreate the data.
1560 for ($i = 0; $i < $i_num_fields; $i++)
1561 {
1562 $str_val = $row[$ary_name[$i]];
1563
1564 if (preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
1565 {
1566 $str_quote = '';
1567 $str_empty = "''";
1568 $str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
1569 }
1570 else if (preg_match('#date|timestamp#i', $ary_type[$i]))
1571 {
1572 if (empty($str_val))
1573 {
1574 $str_quote = '';
1575 }
1576 else
1577 {
1578 $str_quote = "'";
1579 }
1580 }
1581 else
1582 {
1583 $str_quote = '';
1584 $str_empty = 'NULL';
1585 }
1586
1587 if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
1588 {
1589 $str_val = $str_empty;
1590 }
1591
1592 $schema_vals[$i] = $str_quote . $str_val . $str_quote;
1593 $schema_fields[$i] = $ary_name[$i];
1594 }
1595
1596 // Take the ordered fields and their associated data and build it
1597 // into a valid sql statement to recreate that field in the data.
1598 $sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
1599
1600 $this->flush($sql_data);
1601 $sql_data = '';
1602 }
1603 $db->sql_freeresult($result);
1604
1605 if ($retrieved_data && $ident_set)
1606 {
1607 $sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
1608 }
1609 $this->flush($sql_data);
1610 }
1611
1612 function write_data_odbc($table_name)
1613 {
1614 global $db;
1615 $ary_type = $ary_name = array();
1616 $ident_set = false;
1617 $sql_data = '';
1618
1619 // Grab all of the data from current table.
1620 $sql = "SELECT *
1621 FROM $table_name";
1622 $result = $db->sql_query($sql);
1623
1624 $retrieved_data = odbc_num_rows($result);
1625
1626 if ($retrieved_data)
1627 {
1628 $sql = "SELECT 1 as has_identity
1629 FROM INFORMATION_SCHEMA.COLUMNS
1630 WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
1631 $result2 = $db->sql_query($sql);
1632 $row2 = $db->sql_fetchrow($result2);
1633 if (!empty($row2['has_identity']))
1634 {
1635 $sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
1636 $ident_set = true;
1637 }
1638 $db->sql_freeresult($result2);
1639 }
1640
1641 $i_num_fields = odbc_num_fields($result);
1642
1643 for ($i = 0; $i < $i_num_fields; $i++)
1644 {
1645 $ary_type[$i] = odbc_field_type($result, $i + 1);
1646 $ary_name[$i] = odbc_field_name($result, $i + 1);
1647 }
1648
1649 while ($row = $db->sql_fetchrow($result))
1650 {
1651 $schema_vals = $schema_fields = array();
1652
1653 // Build the SQL statement to recreate the data.
1654 for ($i = 0; $i < $i_num_fields; $i++)
1655 {
1656 $str_val = $row[$ary_name[$i]];
1657
1658 if (preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
1659 {
1660 $str_quote = '';
1661 $str_empty = "''";
1662 $str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
1663 }
1664 else if (preg_match('#date|timestamp#i', $ary_type[$i]))
1665 {
1666 if (empty($str_val))
1667 {
1668 $str_quote = '';
1669 }
1670 else
1671 {
1672 $str_quote = "'";
1673 }
1674 }
1675 else
1676 {
1677 $str_quote = '';
1678 $str_empty = 'NULL';
1679 }
1680
1681 if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
1682 {
1683 $str_val = $str_empty;
1684 }
1685
1686 $schema_vals[$i] = $str_quote . $str_val . $str_quote;
1687 $schema_fields[$i] = $ary_name[$i];
1688 }
1689
1690 // Take the ordered fields and their associated data and build it
1691 // into a valid sql statement to recreate that field in the data.
1692 $sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
1693
1694 $this->flush($sql_data);
1695
1696 $sql_data = '';
1697
1698 }
1699 $db->sql_freeresult($result);
1700
1701 if ($retrieved_data && $ident_set)
1702 {
1703 $sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
1704 }
1705 $this->flush($sql_data);
1706 }
1707
1708}
1709
1710/**
1711* @package acp
1712*/
1713class oracle_extractor extends base_extractor
1714{
1715 function write_table($table_name)
1716 {
1717 global $db;
1718 $sql_data = '-- Table: ' . $table_name . "\n";
1719 $sql_data .= "DROP TABLE $table_name\n/\n";
1720 $sql_data .= "\nCREATE TABLE $table_name (\n";
1721
1722 $sql = "SELECT COLUMN_NAME, DATA_TYPE, DATA_PRECISION, DATA_LENGTH, NULLABLE, DATA_DEFAULT
1723 FROM ALL_TAB_COLS
1724 WHERE table_name = '{$table_name}'";
1725 $result = $db->sql_query($sql);
1726
1727 $rows = array();
1728 while ($row = $db->sql_fetchrow($result))
1729 {
1730 $line = ' "' . $row['column_name'] . '" ' . $row['data_type'];
1731
1732 if ($row['data_type'] !== 'CLOB')
1733 {
1734 if ($row['data_type'] !== 'VARCHAR2' && $row['data_type'] !== 'CHAR')
1735 {
1736 $line .= '(' . $row['data_precision'] . ')';
1737 }
1738 else
1739 {
1740 $line .= '(' . $row['data_length'] . ')';
1741 }
1742 }
1743
1744 if (!empty($row['data_default']))
1745 {
1746 $line .= ' DEFAULT ' . $row['data_default'];
1747 }
1748
1749 if ($row['nullable'] == 'N')
1750 {
1751 $line .= ' NOT NULL';
1752 }
1753 $rows[] = $line;
1754 }
1755 $db->sql_freeresult($result);
1756
1757 $sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME
1758 FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
1759 WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
1760 AND B.CONSTRAINT_TYPE = 'P'
1761 AND A.TABLE_NAME = '{$table_name}'";
1762 $result = $db->sql_query($sql);
1763
1764 $primary_key = array();
1765 $contraint_name = '';
1766 while ($row = $db->sql_fetchrow($result))
1767 {
1768 $constraint_name = '"' . $row['constraint_name'] . '"';
1769 $primary_key[] = '"' . $row['column_name'] . '"';
1770 }
1771 $db->sql_freeresult($result);
1772
1773 if (sizeof($primary_key))
1774 {
1775 $rows[] = " CONSTRAINT {$constraint_name} PRIMARY KEY (" . implode(', ', $primary_key) . ')';
1776 }
1777
1778 $sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME
1779 FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
1780 WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
1781 AND B.CONSTRAINT_TYPE = 'U'
1782 AND A.TABLE_NAME = '{$table_name}'";
1783 $result = $db->sql_query($sql);
1784
1785 $unique = array();
1786 $contraint_name = '';
1787 while ($row = $db->sql_fetchrow($result))
1788 {
1789 $constraint_name = '"' . $row['constraint_name'] . '"';
1790 $unique[] = '"' . $row['column_name'] . '"';
1791 }
1792 $db->sql_freeresult($result);
1793
1794 if (sizeof($unique))
1795 {
1796 $rows[] = " CONSTRAINT {$constraint_name} UNIQUE (" . implode(', ', $unique) . ')';
1797 }
1798
1799 $sql_data .= implode(",\n", $rows);
1800 $sql_data .= "\n)\n/\n";
1801
1802 $sql = "SELECT A.REFERENCED_NAME, C.*
1803 FROM USER_DEPENDENCIES A, USER_TRIGGERS B, USER_SEQUENCES C
1804 WHERE A.REFERENCED_TYPE = 'SEQUENCE'
1805 AND A.NAME = B.TRIGGER_NAME
1806 AND B.TABLE_NAME = '{$table_name}'
1807 AND C.SEQUENCE_NAME = A.REFERENCED_NAME";
1808 $result = $db->sql_query($sql);
1809
1810 $type = request_var('type', '');
1811
1812 while ($row = $db->sql_fetchrow($result))
1813 {
1814 $sql_data .= "\nDROP SEQUENCE \"{$row['referenced_name']}\"\n/\n";
1815 $sql_data .= "\nCREATE SEQUENCE \"{$row['referenced_name']}\"";
1816
1817 if ($type == 'full')
1818 {
1819 $sql_data .= ' START WITH ' . $row['last_number'];
1820 }
1821
1822 $sql_data .= "\n/\n";
1823 }
1824 $db->sql_freeresult($result);
1825
1826 $sql = "SELECT DESCRIPTION, WHEN_CLAUSE, TRIGGER_BODY
1827 FROM USER_TRIGGERS
1828 WHERE TABLE_NAME = '{$table_name}'";
1829 $result = $db->sql_query($sql);
1830 while ($row = $db->sql_fetchrow($result))
1831 {
1832 $sql_data .= "\nCREATE OR REPLACE TRIGGER {$row['description']}WHEN ({$row['when_clause']})\n{$row['trigger_body']}\n/\n";
1833 }
1834 $db->sql_freeresult($result);
1835
1836 $sql = "SELECT A.INDEX_NAME, B.COLUMN_NAME
1837 FROM USER_INDEXES A, USER_IND_COLUMNS B
1838 WHERE A.UNIQUENESS = 'NONUNIQUE'
1839 AND A.INDEX_NAME = B.INDEX_NAME
1840 AND B.TABLE_NAME = '{$table_name}'";
1841 $result = $db->sql_query($sql);
1842
1843 $index = array();
1844
1845 while ($row = $db->sql_fetchrow($result))
1846 {
1847 $index[$row['index_name']][] = $row['column_name'];
1848 }
1849
1850 foreach ($index as $index_name => $column_names)
1851 {
1852 $sql_data .= "\nCREATE INDEX $index_name ON $table_name(" . implode(', ', $column_names) . ")\n/\n";
1853 }
1854 $db->sql_freeresult($result);
1855 $this->flush($sql_data);
1856 }
1857
1858 function write_data($table_name)
1859 {
1860 global $db;
1861 $ary_type = $ary_name = array();
1862
1863 // Grab all of the data from current table.
1864 $sql = "SELECT *
1865 FROM $table_name";
1866 $result = $db->sql_query($sql);
1867
1868 $i_num_fields = ocinumcols($result);
1869
1870 for ($i = 0; $i < $i_num_fields; $i++)
1871 {
1872 $ary_type[$i] = ocicolumntype($result, $i + 1);
1873 $ary_name[$i] = ocicolumnname($result, $i + 1);
1874 }
1875
1876 $sql_data = '';
1877
1878 while ($row = $db->sql_fetchrow($result))
1879 {
1880 $schema_vals = $schema_fields = array();
1881
1882 // Build the SQL statement to recreate the data.
1883 for ($i = 0; $i < $i_num_fields; $i++)
1884 {
1885 // Oracle uses uppercase - we use lowercase
1886 $str_val = $row[strtolower($ary_name[$i])];
1887
1888 if (preg_match('#char|text|bool|raw|clob#i', $ary_type[$i]))
1889 {
1890 $str_quote = '';
1891 $str_empty = "''";
1892 $str_val = sanitize_data_oracle($str_val);
1893 }
1894 else if (preg_match('#date|timestamp#i', $ary_type[$i]))
1895 {
1896 if (empty($str_val))
1897 {
1898 $str_quote = '';
1899 }
1900 else
1901 {
1902 $str_quote = "'";
1903 }
1904 }
1905 else
1906 {
1907 $str_quote = '';
1908 $str_empty = 'NULL';
1909 }
1910
1911 if (empty($str_val) && $str_val !== '0')
1912 {
1913 $str_val = $str_empty;
1914 }
1915
1916 $schema_vals[$i] = $str_quote . $str_val . $str_quote;
1917 $schema_fields[$i] = '"' . $ary_name[$i] . '"';
1918 }
1919
1920 // Take the ordered fields and their associated data and build it
1921 // into a valid sql statement to recreate that field in the data.
1922 $sql_data = "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ")\n/\n";
1923
1924 $this->flush($sql_data);
1925 }
1926 $db->sql_freeresult($result);
1927 }
1928
1929 function write_start($prefix)
1930 {
1931 $sql_data = "--\n";
1932 $sql_data .= "-- phpBB Backup Script\n";
1933 $sql_data .= "-- Dump of tables for $prefix\n";
1934 $sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
1935 $sql_data .= "--\n";
1936 $this->flush($sql_data);
1937 }
1938}
1939
1940/**
1941* @package acp
1942*/
1943class firebird_extractor extends base_extractor
1944{
1945 function write_start($prefix)
1946 {
1947 $sql_data = "--\n";
1948 $sql_data .= "-- phpBB Backup Script\n";
1949 $sql_data .= "-- Dump of tables for $prefix\n";
1950 $sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
1951 $sql_data .= "--\n";
1952 $this->flush($sql_data);
1953 }
1954
1955 function write_data($table_name)
1956 {
1957 global $db;
1958 $ary_type = $ary_name = array();
1959
1960 // Grab all of the data from current table.
1961 $sql = "SELECT *
1962 FROM $table_name";
1963 $result = $db->sql_query($sql);
1964
1965 $i_num_fields = ibase_num_fields($result);
1966
1967 for ($i = 0; $i < $i_num_fields; $i++)
1968 {
1969 $info = ibase_field_info($result, $i);
1970 $ary_type[$i] = $info['type'];
1971 $ary_name[$i] = $info['name'];
1972 }
1973
1974 while ($row = $db->sql_fetchrow($result))
1975 {
1976 $schema_vals = $schema_fields = array();
1977
1978 // Build the SQL statement to recreate the data.
1979 for ($i = 0; $i < $i_num_fields; $i++)
1980 {
1981 $str_val = $row[strtolower($ary_name[$i])];
1982
1983 if (preg_match('#char|text|bool|varbinary|blob#i', $ary_type[$i]))
1984 {
1985 $str_quote = '';
1986 $str_empty = "''";
1987 $str_val = sanitize_data_generic(str_replace("'", "''", $str_val));
1988 }
1989 else if (preg_match('#date|timestamp#i', $ary_type[$i]))
1990 {
1991 if (empty($str_val))
1992 {
1993 $str_quote = '';
1994 }
1995 else
1996 {
1997 $str_quote = "'";
1998 }
1999 }
2000 else
2001 {
2002 $str_quote = '';
2003 $str_empty = 'NULL';
2004 }
2005
2006 if (empty($str_val) && $str_val !== '0')
2007 {
2008 $str_val = $str_empty;
2009 }
2010
2011 $schema_vals[$i] = $str_quote . $str_val . $str_quote;
2012 $schema_fields[$i] = '"' . $ary_name[$i] . '"';
2013 }
2014
2015 // Take the ordered fields and their associated data and build it
2016 // into a valid sql statement to recreate that field in the data.
2017 $sql_data = "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\n";
2018
2019 $this->flush($sql_data);
2020 }
2021 $db->sql_freeresult($result);
2022 }
2023
2024 function write_table($table_name)
2025 {
2026 global $db;
2027
2028 $sql_data = '-- Table: ' . $table_name . "\n";
2029 $sql_data .= "DROP TABLE $table_name;\n";
2030
2031 $data_types = array(7 => 'SMALLINT', 8 => 'INTEGER', 10 => 'FLOAT', 12 => 'DATE', 13 => 'TIME', 14 => 'CHARACTER', 27 => 'DOUBLE PRECISION', 35 => 'TIMESTAMP', 37 => 'VARCHAR', 40 => 'CSTRING', 261 => 'BLOB', 701 => 'DECIMAL', 702 => 'NUMERIC');
2032
2033 $sql_data .= "\nCREATE TABLE $table_name (\n";
2034
2035 $sql = 'SELECT DISTINCT R.RDB$FIELD_NAME as FNAME, R.RDB$NULL_FLAG as NFLAG, R.RDB$DEFAULT_SOURCE as DSOURCE, F.RDB$FIELD_TYPE as FTYPE, F.RDB$FIELD_SUB_TYPE as STYPE, F.RDB$FIELD_LENGTH as FLEN
2036 FROM RDB$RELATION_FIELDS R
2037 JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME
2038 LEFT JOIN RDB$FIELD_DIMENSIONS D ON R.RDB$FIELD_SOURCE = D.RDB$FIELD_NAME
2039 WHERE F.RDB$SYSTEM_FLAG = 0
2040 AND R.RDB$RELATION_NAME = \''. $table_name . '\'
2041 ORDER BY R.RDB$FIELD_POSITION';
2042 $result = $db->sql_query($sql);
2043
2044 $rows = array();
2045 while ($row = $db->sql_fetchrow($result))
2046 {
2047 $line = "\t" . '"' . $row['fname'] . '" ' . $data_types[$row['ftype']];
2048
2049 if ($row['ftype'] == 261 && $row['stype'] == 1)
2050 {
2051 $line .= ' SUB_TYPE TEXT';
2052 }
2053
2054 if ($row['ftype'] == 37 || $row['ftype'] == 14)
2055 {
2056 $line .= ' (' . $row['flen'] . ')';
2057 }
2058
2059 if (!empty($row['dsource']))
2060 {
2061 $line .= ' ' . $row['dsource'];
2062 }
2063
2064 if (!empty($row['nflag']))
2065 {
2066 $line .= ' NOT NULL';
2067 }
2068 $rows[] = $line;
2069 }
2070 $db->sql_freeresult($result);
2071
2072 $sql_data .= implode(",\n", $rows);
2073 $sql_data .= "\n);\n";
2074 $keys = array();
2075
2076 $sql = 'SELECT I.RDB$FIELD_NAME as NAME
2077 FROM RDB$RELATION_CONSTRAINTS RC, RDB$INDEX_SEGMENTS I, RDB$INDICES IDX
2078 WHERE (I.RDB$INDEX_NAME = RC.RDB$INDEX_NAME)
2079 AND (IDX.RDB$INDEX_NAME = RC.RDB$INDEX_NAME)
2080 AND (RC.RDB$RELATION_NAME = \''. $table_name . '\')
2081 ORDER BY I.RDB$FIELD_POSITION';
2082 $result = $db->sql_query($sql);
2083
2084 while ($row = $db->sql_fetchrow($result))
2085 {
2086 $keys[] = $row['name'];
2087 }
2088
2089 if (sizeof($keys))
2090 {
2091 $sql_data .= "\nALTER TABLE $table_name ADD PRIMARY KEY (" . implode(', ', $keys) . ');';
2092 }
2093
2094 $db->sql_freeresult($result);
2095
2096 $sql = 'SELECT I.RDB$INDEX_NAME as INAME, I.RDB$UNIQUE_FLAG as UFLAG, S.RDB$FIELD_NAME as FNAME
2097 FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON S.RDB$INDEX_NAME=I.RDB$INDEX_NAME
2098 WHERE (I.RDB$SYSTEM_FLAG IS NULL OR I.RDB$SYSTEM_FLAG=0)
2099 AND I.RDB$FOREIGN_KEY IS NULL
2100 AND I.RDB$RELATION_NAME = \''. $table_name . '\'
2101 AND I.RDB$INDEX_NAME NOT STARTING WITH \'RDB$\'
2102 ORDER BY S.RDB$FIELD_POSITION';
2103 $result = $db->sql_query($sql);
2104
2105 $index = array();
2106 while ($row = $db->sql_fetchrow($result))
2107 {
2108 $index[$row['iname']]['unique'] = !empty($row['uflag']);
2109 $index[$row['iname']]['values'][] = $row['fname'];
2110 }
2111
2112 foreach ($index as $index_name => $data)
2113 {
2114 $sql_data .= "\nCREATE ";
2115 if ($data['unique'])
2116 {
2117 $sql_data .= 'UNIQUE ';
2118 }
2119 $sql_data .= "INDEX $index_name ON $table_name(" . implode(', ', $data['values']) . ");";
2120 }
2121 $sql_data .= "\n";
2122
2123 $db->sql_freeresult($result);
2124
2125 $sql = 'SELECT D1.RDB$DEPENDENT_NAME as DNAME, D1.RDB$FIELD_NAME as FNAME, D1.RDB$DEPENDENT_TYPE, R1.RDB$RELATION_NAME
2126 FROM RDB$DEPENDENCIES D1
2127 LEFT JOIN RDB$RELATIONS R1 ON ((D1.RDB$DEPENDENT_NAME = R1.RDB$RELATION_NAME) AND (NOT (R1.RDB$VIEW_BLR IS NULL)))
2128 WHERE (D1.RDB$DEPENDED_ON_TYPE = 0)
2129 AND (D1.RDB$DEPENDENT_TYPE <> 3)
2130 AND (D1.RDB$DEPENDED_ON_NAME = \'' . $table_name . '\')
2131 UNION SELECT DISTINCT F2.RDB$RELATION_NAME, D2.RDB$FIELD_NAME, D2.RDB$DEPENDENT_TYPE, R2.RDB$RELATION_NAME FROM RDB$DEPENDENCIES D2, RDB$RELATION_FIELDS F2
2132 LEFT JOIN RDB$RELATIONS R2 ON ((F2.RDB$RELATION_NAME = R2.RDB$RELATION_NAME) AND (NOT (R2.RDB$VIEW_BLR IS NULL)))
2133 WHERE (D2.RDB$DEPENDENT_TYPE = 3)
2134 AND (D2.RDB$DEPENDENT_NAME = F2.RDB$FIELD_SOURCE)
2135 AND (D2.RDB$DEPENDED_ON_NAME = \'' . $table_name . '\')
2136 ORDER BY 1, 2';
2137 $result = $db->sql_query($sql);
2138 while ($row = $db->sql_fetchrow($result))
2139 {
2140 $sql = 'SELECT T1.RDB$DEPENDED_ON_NAME as GEN, T1.RDB$FIELD_NAME, T1.RDB$DEPENDED_ON_TYPE
2141 FROM RDB$DEPENDENCIES T1
2142 WHERE (T1.RDB$DEPENDENT_NAME = \'' . $row['dname'] . '\')
2143 AND (T1.RDB$DEPENDENT_TYPE = 2 AND T1.RDB$DEPENDED_ON_TYPE = 14)
2144 UNION ALL SELECT DISTINCT D.RDB$DEPENDED_ON_NAME, D.RDB$FIELD_NAME, D.RDB$DEPENDED_ON_TYPE
2145 FROM RDB$DEPENDENCIES D, RDB$RELATION_FIELDS F
2146 WHERE (D.RDB$DEPENDENT_TYPE = 3)
2147 AND (D.RDB$DEPENDENT_NAME = F.RDB$FIELD_SOURCE)
2148 AND (F.RDB$RELATION_NAME = \'' . $row['dname'] . '\')
2149 ORDER BY 1,2';
2150 $result2 = $db->sql_query($sql);
2151 $row2 = $db->sql_fetchrow($result2);
2152 $db->sql_freeresult($result2);
2153 $gen_name = $row2['gen'];
2154
2155 $sql_data .= "\nDROP GENERATOR " . $gen_name . ";";
2156 $sql_data .= "\nSET TERM ^ ;";
2157 $sql_data .= "\nCREATE GENERATOR " . $gen_name . "^";
2158 $sql_data .= "\nSET GENERATOR " . $gen_name . " TO 0^\n";
2159 $sql_data .= "\nCREATE TRIGGER {$row['dname']} FOR $table_name";
2160 $sql_data .= "\nBEFORE INSERT\nAS\nBEGIN";
2161 $sql_data .= "\n NEW.{$row['fname']} = GEN_ID(" . $gen_name . ", 1);";
2162 $sql_data .= "\nEND^\n";
2163 $sql_data .= "\nSET TERM ; ^\n";
2164 }
2165
2166 $this->flush($sql_data);
2167
2168 $db->sql_freeresult($result);
2169 }
2170}
2171
2172// get how much space we allow for a chunk of data, very similar to phpMyAdmin's way of doing things ;-) (hey, we only do this for MySQL anyway :P)
2173function get_usable_memory()
2174{
2175 $val = trim(@ini_get('memory_limit'));
2176
2177 if (preg_match('/(\\d+)([mkg]?)/i', $val, $regs))
2178 {
2179 $memory_limit = (int) $regs[1];
2180 switch ($regs[2])
2181 {
2182
2183 case 'k':
2184 case 'K':
2185 $memory_limit *= 1024;
2186 break;
2187
2188 case 'm':
2189 case 'M':
2190 $memory_limit *= 1048576;
2191 break;
2192
2193 case 'g':
2194 case 'G':
2195 $memory_limit *= 1073741824;
2196 break;
2197 }
2198
2199 // how much memory PHP requires at the start of export (it is really a little less)
2200 if ($memory_limit > 6100000)
2201 {
2202 $memory_limit -= 6100000;
2203 }
2204
2205 // allow us to consume half of the total memory available
2206 $memory_limit /= 2;
2207 }
2208 else
2209 {
2210 // set the buffer to 1M if we have no clue how much memory PHP will give us :P
2211 $memory_limit = 1048576;
2212 }
2213
2214 return $memory_limit;
2215}
2216
2217function sanitize_data_mssql($text)
2218{
2219 $data = preg_split('/[\n\t\r\b\f]/', $text);
2220 preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
2221
2222 $val = array();
2223
2224 foreach ($data as $value)
2225 {
2226 if (strlen($value))
2227 {
2228 $val[] = "'" . $value . "'";
2229 }
2230 if (sizeof($matches[0]))
2231 {
2232 $val[] = 'char(' . ord(array_shift($matches[0])) . ')';
2233 }
2234 }
2235
2236 return implode('+', $val);
2237}
2238
2239function sanitize_data_oracle($text)
2240{
2241// $data = preg_split('/[\0\n\t\r\b\f\'"\/\\\]/', $text);
2242// preg_match_all('/[\0\n\t\r\b\f\'"\/\\\]/', $text, $matches);
2243 $data = preg_split('/[\0\b\f\'\/]/', $text);
2244 preg_match_all('/[\0\r\b\f\'\/]/', $text, $matches);
2245
2246 $val = array();
2247
2248 foreach ($data as $value)
2249 {
2250 if (strlen($value))
2251 {
2252 $val[] = "'" . $value . "'";
2253 }
2254 if (sizeof($matches[0]))
2255 {
2256 $val[] = 'chr(' . ord(array_shift($matches[0])) . ')';
2257 }
2258 }
2259
2260 return implode('||', $val);
2261}
2262
2263function sanitize_data_generic($text)
2264{
2265 $data = preg_split('/[\n\t\r\b\f]/', $text);
2266 preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
2267
2268 $val = array();
2269
2270 foreach ($data as $value)
2271 {
2272 if (strlen($value))
2273 {
2274 $val[] = "'" . $value . "'";
2275 }
2276 if (sizeof($matches[0]))
2277 {
2278 $val[] = "'" . array_shift($matches[0]) . "'";
2279 }
2280 }
2281
2282 return implode('||', $val);
2283}
2284
2285// modified from PHP.net
2286function fgetd(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
2287{
2288 $record = '';
2289 $delim_len = strlen($delim);
2290
2291 while (!$eof($fp))
2292 {
2293 $pos = strpos($record, $delim);
2294 if ($pos === false)
2295 {
2296 $record .= $read($fp, $buffer);
2297 if ($eof($fp) && ($pos = strpos($record, $delim)) !== false)
2298 {
2299 $seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
2300 return substr($record, 0, $pos);
2301 }
2302 }
2303 else
2304 {
2305 $seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
2306 return substr($record, 0, $pos);
2307 }
2308 }
2309
2310 return false;
2311}
2312
2313function fgetd_seekless(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
2314{
2315 static $array = array();
2316 static $record = '';
2317
2318 if (!sizeof($array))
2319 {
2320 while (!$eof($fp))
2321 {
2322 if (strpos($record, $delim) !== false)
2323 {
2324 $array = explode($delim, $record);
2325 $record = array_pop($array);
2326 break;
2327 }
2328 else
2329 {
2330 $record .= $read($fp, $buffer);
2331 }
2332 }
2333 if ($eof($fp) && strpos($record, $delim) !== false)
2334 {
2335 $array = explode($delim, $record);
2336 $record = array_pop($array);
2337 }
2338 }
2339
2340 if (sizeof($array))
2341 {
2342 return array_shift($array);
2343 }
2344
2345 return false;
2346}
2347
2348?>
Note: See TracBrowser for help on using the repository browser.