forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.php
4874 lines (4438 loc) · 169 KB
/
Util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Hold the PMA\libraries\Util class
*
* @package PhpMyAdmin
*/
namespace PMA\libraries;
use PMA\libraries\plugins\ImportPlugin;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use stdClass;
use PMA\libraries\URL;
use PMA\libraries\Sanitize;
use PMA\libraries\Template;
use PhpMyAdmin\SqlParser\Utils\Error as ParserError;
if (! defined('PHPMYADMIN')) {
exit;
}
/**
* Misc functions used all over the scripts.
*
* @package PhpMyAdmin
*/
class Util
{
/**
* Checks whether configuration value tells to show icons.
*
* @param string $value Configuration option name
*
* @return boolean Whether to show icons.
*/
public static function showIcons($value)
{
return in_array($GLOBALS['cfg'][$value], array('icons', 'both'));
}
/**
* Checks whether configuration value tells to show text.
*
* @param string $value Configuration option name
*
* @return boolean Whether to show text.
*/
public static function showText($value)
{
return in_array($GLOBALS['cfg'][$value], array('text', 'both'));
}
/**
* Returns an HTML IMG tag for a particular icon from a theme,
* which may be an actual file or an icon from a sprite.
* This function takes into account the ActionLinksMode
* configuration setting and wraps the image tag in a span tag.
*
* @param string $icon name of icon file
* @param string $alternate alternate text
* @param boolean $force_text whether to force alternate text to be displayed
* @param boolean $menu_icon whether this icon is for the menu bar or not
* @param string $control_param which directive controls the display
*
* @return string an html snippet
*/
public static function getIcon(
$icon,
$alternate = '',
$force_text = false,
$menu_icon = false,
$control_param = 'ActionLinksMode'
) {
$include_icon = $include_text = false;
if (self::showIcons($control_param)) {
$include_icon = true;
}
if ($force_text
|| self::showText($control_param)
) {
$include_text = true;
}
// Sometimes use a span (we rely on this in js/sql.js). But for menu bar
// we don't need a span
$button = $menu_icon ? '' : '<span class="nowrap">';
if ($include_icon) {
$button .= self::getImage($icon, $alternate);
}
if ($include_icon && $include_text) {
$button .= ' ';
}
if ($include_text) {
$button .= $alternate;
}
$button .= $menu_icon ? '' : '</span>';
return $button;
}
/**
* Returns an HTML IMG tag for a particular image from a theme,
* which may be an actual file or an icon from a sprite
*
* @param string $image The name of the file to get
* @param string $alternate Used to set 'alt' and 'title' attributes
* of the image
* @param array $attributes An associative array of other attributes
*
* @return string an html IMG tag
*/
public static function getImage($image, $alternate = '', $attributes = array())
{
static $sprites; // cached list of available sprites (if any)
if (defined('TESTSUITE')) {
// prevent caching in testsuite
unset($sprites);
}
$is_sprite = false;
$alternate = htmlspecialchars($alternate);
// If it's the first time this function is called
if (! isset($sprites)) {
$sprites = array();
// Try to load the list of sprites
if (isset($_SESSION['PMA_Theme'])) {
$sprites = $_SESSION['PMA_Theme']->getSpriteData();
}
}
// Check if we have the requested image as a sprite
// and set $url accordingly
$class = str_replace(array('.gif','.png'), '', $image);
if (array_key_exists($class, $sprites)) {
$is_sprite = true;
$url = (defined('PMA_TEST_THEME') ? '../' : '') . 'themes/dot.gif';
} elseif (isset($GLOBALS['pmaThemeImage'])) {
$url = $GLOBALS['pmaThemeImage'] . $image;
} else {
$url = './themes/pmahomme/' . $image;
}
// set class attribute
if ($is_sprite) {
if (isset($attributes['class'])) {
$attributes['class'] = "icon ic_$class " . $attributes['class'];
} else {
$attributes['class'] = "icon ic_$class";
}
}
// set all other attributes
$attr_str = '';
foreach ($attributes as $key => $value) {
if (! in_array($key, array('alt', 'title'))) {
$attr_str .= " $key=\"$value\"";
}
}
// override the alt attribute
if (isset($attributes['alt'])) {
$alt = $attributes['alt'];
} else {
$alt = $alternate;
}
// override the title attribute
if (isset($attributes['title'])) {
$title = $attributes['title'];
} else {
$title = $alternate;
}
// generate the IMG tag
$template = '<img src="%s" title="%s" alt="%s"%s />';
$retval = sprintf($template, $url, $title, $alt, $attr_str);
return $retval;
}
/**
* Returns the formatted maximum size for an upload
*
* @param integer $max_upload_size the size
*
* @return string the message
*
* @access public
*/
public static function getFormattedMaximumUploadSize($max_upload_size)
{
// I have to reduce the second parameter (sensitiveness) from 6 to 4
// to avoid weird results like 512 kKib
list($max_size, $max_unit) = self::formatByteDown($max_upload_size, 4);
return '(' . sprintf(__('Max: %s%s'), $max_size, $max_unit) . ')';
}
/**
* Generates a hidden field which should indicate to the browser
* the maximum size for upload
*
* @param integer $max_size the size
*
* @return string the INPUT field
*
* @access public
*/
public static function generateHiddenMaxFileSize($max_size)
{
return '<input type="hidden" name="MAX_FILE_SIZE" value="'
. $max_size . '" />';
}
/**
* Add slashes before "_" and "%" characters for using them in MySQL
* database, table and field names.
* Note: This function does not escape backslashes!
*
* @param string $name the string to escape
*
* @return string the escaped string
*
* @access public
*/
public static function escapeMysqlWildcards($name)
{
return strtr($name, array('_' => '\\_', '%' => '\\%'));
} // end of the 'escapeMysqlWildcards()' function
/**
* removes slashes before "_" and "%" characters
* Note: This function does not unescape backslashes!
*
* @param string $name the string to escape
*
* @return string the escaped string
*
* @access public
*/
public static function unescapeMysqlWildcards($name)
{
return strtr($name, array('\\_' => '_', '\\%' => '%'));
} // end of the 'unescapeMysqlWildcards()' function
/**
* removes quotes (',",`) from a quoted string
*
* checks if the string is quoted and removes this quotes
*
* @param string $quoted_string string to remove quotes from
* @param string $quote type of quote to remove
*
* @return string unqoted string
*/
public static function unQuote($quoted_string, $quote = null)
{
$quotes = array();
if ($quote === null) {
$quotes[] = '`';
$quotes[] = '"';
$quotes[] = "'";
} else {
$quotes[] = $quote;
}
foreach ($quotes as $quote) {
if (mb_substr($quoted_string, 0, 1) === $quote
&& mb_substr($quoted_string, -1, 1) === $quote
) {
$unquoted_string = mb_substr($quoted_string, 1, -1);
// replace escaped quotes
$unquoted_string = str_replace(
$quote . $quote,
$quote,
$unquoted_string
);
return $unquoted_string;
}
}
return $quoted_string;
}
/**
* format sql strings
*
* @param string $sqlQuery raw SQL string
* @param boolean $truncate truncate the query if it is too long
*
* @return string the formatted sql
*
* @global array $cfg the configuration array
*
* @access public
* @todo move into PMA_Sql
*/
public static function formatSql($sqlQuery, $truncate = false)
{
global $cfg;
if ($truncate
&& mb_strlen($sqlQuery) > $cfg['MaxCharactersInDisplayedSQL']
) {
$sqlQuery = mb_substr(
$sqlQuery,
0,
$cfg['MaxCharactersInDisplayedSQL']
) . '[...]';
}
return '<code class="sql"><pre>' . "\n"
. htmlspecialchars($sqlQuery) . "\n"
. '</pre></code>';
} // end of the "formatSql()" function
/**
* Displays a link to the documentation as an icon
*
* @param string $link documentation link
* @param string $target optional link target
* @param boolean $bbcode optional flag indicating whether to output bbcode
*
* @return string the html link
*
* @access public
*/
public static function showDocLink($link, $target = 'documentation', $bbcode = false)
{
if($bbcode){
return "[a@$link@$target][dochelpicon][/a]";
}else{
return '<a href="' . $link . '" target="' . $target . '">'
. self::getImage('b_help.png', __('Documentation'))
. '</a>';
}
} // end of the 'showDocLink()' function
/**
* Get a URL link to the official MySQL documentation
*
* @param string $link contains name of page/anchor that is being linked
* @param string $anchor anchor to page part
*
* @return string the URL link
*
* @access public
*/
public static function getMySQLDocuURL($link, $anchor = '')
{
// Fixup for newly used names:
$link = str_replace('_', '-', mb_strtolower($link));
if (empty($link)) {
$link = 'index';
}
$mysql = '5.5';
$lang = 'en';
if (defined('PMA_MYSQL_INT_VERSION')) {
if (PMA_MYSQL_INT_VERSION >= 50700) {
$mysql = '5.7';
} elseif (PMA_MYSQL_INT_VERSION >= 50600) {
$mysql = '5.6';
} elseif (PMA_MYSQL_INT_VERSION >= 50500) {
$mysql = '5.5';
}
}
$url = 'https://dev.mysql.com/doc/refman/'
. $mysql . '/' . $lang . '/' . $link . '.html';
if (! empty($anchor)) {
$url .= '#' . $anchor;
}
return PMA_linkURL($url);
}
/**
* Displays a link to the official MySQL documentation
*
* @param string $link contains name of page/anchor that is being linked
* @param bool $big_icon whether to use big icon (like in left frame)
* @param string $anchor anchor to page part
* @param bool $just_open whether only the opening <a> tag should be returned
*
* @return string the html link
*
* @access public
*/
public static function showMySQLDocu(
$link,
$big_icon = false,
$anchor = '',
$just_open = false
) {
$url = self::getMySQLDocuURL($link, $anchor);
$open_link = '<a href="' . $url . '" target="mysql_doc">';
if ($just_open) {
return $open_link;
} elseif ($big_icon) {
return $open_link
. self::getImage('b_sqlhelp.png', __('Documentation')) . '</a>';
} else {
return self::showDocLink($url, 'mysql_doc');
}
} // end of the 'showMySQLDocu()' function
/**
* Returns link to documentation.
*
* @param string $page Page in documentation
* @param string $anchor Optional anchor in page
*
* @return string URL
*/
public static function getDocuLink($page, $anchor = '')
{
/* Construct base URL */
$url = $page . '.html';
if (!empty($anchor)) {
$url .= '#' . $anchor;
}
/* Check if we have built local documentation */
if (defined('TESTSUITE')) {
/* Provide consistent URL for testsuite */
return PMA_linkURL('https://docs.phpmyadmin.net/en/latest/' . $url);
} elseif (@file_exists('doc/html/index.html')) {
if (defined('PMA_SETUP')) {
return '../doc/html/' . $url;
} else {
return './doc/html/' . $url;
}
} else {
/* TODO: Should link to correct branch for released versions */
return PMA_linkURL('https://docs.phpmyadmin.net/en/latest/' . $url);
}
}
/**
* Displays a link to the phpMyAdmin documentation
*
* @param string $page Page in documentation
* @param string $anchor Optional anchor in page
* @param boolean $bbcode Optional flag indicating whether to output bbcode
*
* @return string the html link
*
* @access public
*/
public static function showDocu($page, $anchor = '', $bbcode = false)
{
return self::showDocLink(self::getDocuLink($page, $anchor), 'documentation', $bbcode);
} // end of the 'showDocu()' function
/**
* Displays a link to the PHP documentation
*
* @param string $target anchor in documentation
*
* @return string the html link
*
* @access public
*/
public static function showPHPDocu($target)
{
$url = PMA_getPHPDocLink($target);
return self::showDocLink($url);
} // end of the 'showPHPDocu()' function
/**
* Returns HTML code for a tooltip
*
* @param string $message the message for the tooltip
*
* @return string
*
* @access public
*/
public static function showHint($message)
{
if ($GLOBALS['cfg']['ShowHint']) {
$classClause = ' class="pma_hint"';
} else {
$classClause = '';
}
return '<span' . $classClause . '>'
. self::getImage('b_help.png')
. '<span class="hide">' . $message . '</span>'
. '</span>';
}
/**
* Displays a MySQL error message in the main panel when $exit is true.
* Returns the error message otherwise.
*
* @param string|bool $server_msg Server's error message.
* @param string $sql_query The SQL query that failed.
* @param bool $is_modify_link Whether to show a "modify" link or not.
* @param string $back_url URL for the "back" link (full path is
* not required).
* @param bool $exit Whether execution should be stopped or
* the error message should be returned.
*
* @return string
*
* @global string $table The current table.
* @global string $db The current database.
*
* @access public
*/
public static function mysqlDie(
$server_msg = '',
$sql_query = '',
$is_modify_link = true,
$back_url = '',
$exit = true
) {
global $table, $db;
/**
* Error message to be built.
* @var string $error_msg
*/
$error_msg = '';
// Checking for any server errors.
if (empty($server_msg)) {
$server_msg = $GLOBALS['dbi']->getError();
}
// Finding the query that failed, if not specified.
if ((empty($sql_query) && (!empty($GLOBALS['sql_query'])))) {
$sql_query = $GLOBALS['sql_query'];
}
$sql_query = trim($sql_query);
/**
* The lexer used for analysis.
* @var Lexer $lexer
*/
$lexer = new Lexer($sql_query);
/**
* The parser used for analysis.
* @var Parser $parser
*/
$parser = new Parser($lexer->list);
/**
* The errors found by the lexer and the parser.
* @var array $errors
*/
$errors = ParserError::get(array($lexer, $parser));
if (empty($sql_query)) {
$formatted_sql = '';
} elseif (count($errors)) {
$formatted_sql = htmlspecialchars($sql_query);
} else {
$formatted_sql = self::formatSql($sql_query, true);
}
$error_msg .= '<div class="error"><h1>' . __('Error') . '</h1>';
// For security reasons, if the MySQL refuses the connection, the query
// is hidden so no details are revealed.
if ((!empty($sql_query)) && (!(mb_strstr($sql_query, 'connect')))) {
// Static analysis errors.
if (!empty($errors)) {
$error_msg .= '<p><strong>' . __('Static analysis:')
. '</strong></p>';
$error_msg .= '<p>' . sprintf(
__('%d errors were found during analysis.'),
count($errors)
) . '</p>';
$error_msg .= '<p><ol>';
$error_msg .= implode(
ParserError::format(
$errors,
'<li>%2$s (near "%4$s" at position %5$d)</li>'
)
);
$error_msg .= '</ol></p>';
}
// Display the SQL query and link to MySQL documentation.
$error_msg .= '<p><strong>' . __('SQL query:') . '</strong>' . "\n";
$formattedSqlToLower = mb_strtolower($formatted_sql);
// TODO: Show documentation for all statement types.
if (mb_strstr($formattedSqlToLower, 'select')) {
// please show me help to the error on select
$error_msg .= self::showMySQLDocu('SELECT');
}
if ($is_modify_link) {
$_url_params = array(
'sql_query' => $sql_query,
'show_query' => 1,
);
if (strlen($table) > 0) {
$_url_params['db'] = $db;
$_url_params['table'] = $table;
$doedit_goto = '<a href="tbl_sql.php'
. URL::getCommon($_url_params) . '">';
} elseif (strlen($db) > 0) {
$_url_params['db'] = $db;
$doedit_goto = '<a href="db_sql.php'
. URL::getCommon($_url_params) . '">';
} else {
$doedit_goto = '<a href="server_sql.php'
. URL::getCommon($_url_params) . '">';
}
$error_msg .= $doedit_goto
. self::getIcon('b_edit.png', __('Edit'))
. '</a>';
}
$error_msg .= ' </p>' . "\n"
. '<p>' . "\n"
. $formatted_sql . "\n"
. '</p>' . "\n";
}
// Display server's error.
if (!empty($server_msg)) {
$server_msg = preg_replace(
"@((\015\012)|(\015)|(\012)){3,}@",
"\n\n",
$server_msg
);
// Adds a link to MySQL documentation.
$error_msg .= '<p>' . "\n"
. ' <strong>' . __('MySQL said: ') . '</strong>'
. self::showMySQLDocu('Error-messages-server')
. "\n"
. '</p>' . "\n";
// The error message will be displayed within a CODE segment.
// To preserve original formatting, but allow word-wrapping,
// a couple of replacements are done.
// All non-single blanks and TAB-characters are replaced with their
// HTML-counterpart
$server_msg = str_replace(
array(' ', "\t"),
array(' ', ' '),
$server_msg
);
// Replace line breaks
$server_msg = nl2br($server_msg);
$error_msg .= '<code>' . $server_msg . '</code><br/>';
}
$error_msg .= '</div>';
$_SESSION['Import_message']['message'] = $error_msg;
if (!$exit) {
return $error_msg;
}
/**
* If this is an AJAX request, there is no "Back" link and
* `Response()` is used to send the response.
*/
$response = Response::getInstance();
if ($response->isAjax()) {
$response->setRequestStatus(false);
$response->addJSON('message', $error_msg);
exit;
}
if (!empty($back_url)) {
if (mb_strstr($back_url, '?')) {
$back_url .= '&no_history=true';
} else {
$back_url .= '?no_history=true';
}
$_SESSION['Import_message']['go_back_url'] = $back_url;
$error_msg .= '<fieldset class="tblFooters">'
. '[ <a href="' . $back_url . '">' . __('Back') . '</a> ]'
. '</fieldset>' . "\n\n";
}
exit($error_msg);
}
/**
* Check the correct row count
*
* @param string $db the db name
* @param array $table the table infos
*
* @return int $rowCount the possibly modified row count
*
*/
private static function _checkRowCount($db, $table)
{
$rowCount = 0;
if ($table['Rows'] === null) {
// Do not check exact row count here,
// if row count is invalid possibly the table is defect
// and this would break the navigation panel;
// but we can check row count if this is a view or the
// information_schema database
// since Table::countRecords() returns a limited row count
// in this case.
// set this because Table::countRecords() can use it
$tbl_is_view = $table['TABLE_TYPE'] == 'VIEW';
if ($tbl_is_view || $GLOBALS['dbi']->isSystemSchema($db)) {
$rowCount = $GLOBALS['dbi']
->getTable($db, $table['Name'])
->countRecords();
}
}
return $rowCount;
}
/**
* returns array with tables of given db with extended information and grouped
*
* @param string $db name of db
* @param string $tables name of tables
* @param integer $limit_offset list offset
* @param int|bool $limit_count max tables to return
*
* @return array (recursive) grouped table list
*/
public static function getTableList(
$db,
$tables = null,
$limit_offset = 0,
$limit_count = false
) {
$sep = $GLOBALS['cfg']['NavigationTreeTableSeparator'];
if ($tables === null) {
$tables = $GLOBALS['dbi']->getTablesFull(
$db,
'',
false,
null,
$limit_offset,
$limit_count
);
if ($GLOBALS['cfg']['NaturalOrder']) {
uksort($tables, 'strnatcasecmp');
}
}
if (count($tables) < 1) {
return $tables;
}
$default = array(
'Name' => '',
'Rows' => 0,
'Comment' => '',
'disp_name' => '',
);
$table_groups = array();
foreach ($tables as $table_name => $table) {
$table['Rows'] = self::_checkRowCount($db, $table);
// in $group we save the reference to the place in $table_groups
// where to store the table info
if ($GLOBALS['cfg']['NavigationTreeEnableGrouping']
&& $sep && mb_strstr($table_name, $sep)
) {
$parts = explode($sep, $table_name);
$group =& $table_groups;
$i = 0;
$group_name_full = '';
$parts_cnt = count($parts) - 1;
while (($i < $parts_cnt)
&& ($i < $GLOBALS['cfg']['NavigationTreeTableLevel'])
) {
$group_name = $parts[$i] . $sep;
$group_name_full .= $group_name;
if (! isset($group[$group_name])) {
$group[$group_name] = array();
$group[$group_name]['is' . $sep . 'group'] = true;
$group[$group_name]['tab' . $sep . 'count'] = 1;
$group[$group_name]['tab' . $sep . 'group']
= $group_name_full;
} elseif (! isset($group[$group_name]['is' . $sep . 'group'])) {
$table = $group[$group_name];
$group[$group_name] = array();
$group[$group_name][$group_name] = $table;
$group[$group_name]['is' . $sep . 'group'] = true;
$group[$group_name]['tab' . $sep . 'count'] = 1;
$group[$group_name]['tab' . $sep . 'group']
= $group_name_full;
} else {
$group[$group_name]['tab' . $sep . 'count']++;
}
$group =& $group[$group_name];
$i++;
}
} else {
if (! isset($table_groups[$table_name])) {
$table_groups[$table_name] = array();
}
$group =& $table_groups;
}
$table['disp_name'] = $table['Name'];
$group[$table_name] = array_merge($default, $table);
}
return $table_groups;
}
/* ----------------------- Set of misc functions ----------------------- */
/**
* Adds backquotes on both sides of a database, table or field name.
* and escapes backquotes inside the name with another backquote
*
* example:
* <code>
* echo backquote('owner`s db'); // `owner``s db`
*
* </code>
*
* @param mixed $a_name the database, table or field name to "backquote"
* or array of it
* @param boolean $do_it a flag to bypass this function (used by dump
* functions)
*
* @return mixed the "backquoted" database, table or field name
*
* @access public
*/
public static function backquote($a_name, $do_it = true)
{
if (is_array($a_name)) {
foreach ($a_name as &$data) {
$data = self::backquote($data, $do_it);
}
return $a_name;
}
if (! $do_it) {
if (!(Context::isKeyword($a_name) & Token::FLAG_KEYWORD_RESERVED)
) {
return $a_name;
}
}
// '0' is also empty for php :-(
if (strlen($a_name) > 0 && $a_name !== '*') {
return '`' . str_replace('`', '``', $a_name) . '`';
} else {
return $a_name;
}
} // end of the 'backquote()' function
/**
* Adds backquotes on both sides of a database, table or field name.
* in compatibility mode
*
* example:
* <code>
* echo backquoteCompat('owner`s db'); // `owner``s db`
*
* </code>
*
* @param mixed $a_name the database, table or field name to
* "backquote" or array of it
* @param string $compatibility string compatibility mode (used by dump
* functions)
* @param boolean $do_it a flag to bypass this function (used by dump
* functions)
*
* @return mixed the "backquoted" database, table or field name
*
* @access public
*/
public static function backquoteCompat(
$a_name,
$compatibility = 'MSSQL',
$do_it = true
) {
if (is_array($a_name)) {
foreach ($a_name as &$data) {
$data = self::backquoteCompat($data, $compatibility, $do_it);
}
return $a_name;
}
if (! $do_it) {
if (!Context::isKeyword($a_name)) {
return $a_name;
}
}
// @todo add more compatibility cases (ORACLE for example)
switch ($compatibility) {
case 'MSSQL':
$quote = '"';
break;
default:
$quote = "`";
break;
}
// '0' is also empty for php :-(
if (strlen($a_name) > 0 && $a_name !== '*') {
return $quote . $a_name . $quote;
} else {
return $a_name;
}
} // end of the 'backquoteCompat()' function
/**
* Prepare the message and the query
* usually the message is the result of the query executed
*
* @param Message|string $message the message to display
* @param string $sql_query the query to display
* @param string $type the type (level) of the message
*
* @return string
*
* @access public
*/
public static function getMessage(
$message,
$sql_query = null,
$type = 'notice'
) {
global $cfg;
$retval = '';
if (null === $sql_query) {
if (! empty($GLOBALS['display_query'])) {
$sql_query = $GLOBALS['display_query'];
} elseif (! empty($GLOBALS['unparsed_sql'])) {
$sql_query = $GLOBALS['unparsed_sql'];
} elseif (! empty($GLOBALS['sql_query'])) {
$sql_query = $GLOBALS['sql_query'];
} else {
$sql_query = '';
}
}
$render_sql = $cfg['ShowSQL'] == true && ! empty($sql_query) && $sql_query !== ';';
if (isset($GLOBALS['using_bookmark_message'])) {
$retval .= $GLOBALS['using_bookmark_message']->getDisplay();
unset($GLOBALS['using_bookmark_message']);
}
if ($render_sql) {
$retval .= '<div class="result_query"'
. ' style="text-align: ' . $GLOBALS['cell_align_left'] . '"'
. '>' . "\n";
}
if ($message instanceof Message) {
if (isset($GLOBALS['special_message'])) {
$message->addText($GLOBALS['special_message']);
unset($GLOBALS['special_message']);
}
$retval .= $message->getDisplay();
} else {
$retval .= '<div class="' . $type . '">';
$retval .= Sanitize::sanitize($message);
if (isset($GLOBALS['special_message'])) {
$retval .= Sanitize::sanitize($GLOBALS['special_message']);
unset($GLOBALS['special_message']);
}
$retval .= '</div>';
}
if ($render_sql) {
$query_too_big = false;
$queryLength = mb_strlen($sql_query);