forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_privileges.php
2557 lines (2329 loc) · 111 KB
/
server_privileges.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: */
/**
*
* @package PhpMyAdmin
*/
/**
*
*/
require_once 'libraries/common.inc.php';
/**
* Does the common work
*/
$GLOBALS['js_include'][] = 'server_privileges.js';
$GLOBALS['js_include'][] = 'functions.js';
$GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.16.custom.js';
$GLOBALS['js_include'][] = 'codemirror/lib/codemirror.js';
$GLOBALS['js_include'][] = 'codemirror/mode/mysql/mysql.js';
$_add_user_error = false;
require 'libraries/server_common.inc.php';
if ($GLOBALS['cfg']['AjaxEnable']) {
$conditional_class = 'ajax';
} else {
$conditional_class = '';
}
/**
* Sets globals from $_GET
*/
$get_params = array(
'checkprivs',
'dbname',
'hostname',
'initial',
'tablename',
'username'
);
foreach ($get_params as $one_get_param) {
if (isset($_GET[$one_get_param])) {
$GLOBALS[$one_get_param] = $_GET[$one_get_param];
}
}
/**
* Sets globals from $_POST
*/
$post_params = array(
'createdb-1',
'createdb-2',
'createdb-3',
'grant_count',
'hostname',
'pma_pw',
'pma_pw2',
'pred_hostname',
'pred_password',
'pred_username',
'username'
);
foreach ($post_params as $one_post_param) {
if (isset($_POST[$one_post_param])) {
$GLOBALS[$one_post_param] = $_POST[$one_post_param];
}
}
/**
* Sets globals from $_POST patterns, for privileges and max_* vars
*/
$post_patterns = array(
'/_priv$/i',
'/^max_/i'
);
foreach (array_keys($_POST) as $post_key) {
foreach ($post_patterns as $one_post_pattern) {
if (preg_match($one_post_pattern, $post_key)) {
$GLOBALS[$post_key] = $_POST[$post_key];
}
}
}
/**
* Messages are built using the message name
*/
$strPrivDescAllPrivileges = __('Includes all privileges except GRANT.');
$strPrivDescAlter = __('Allows altering the structure of existing tables.');
$strPrivDescAlterRoutine = __('Allows altering and dropping stored routines.');
$strPrivDescCreateDb = __('Allows creating new databases and tables.');
$strPrivDescCreateRoutine = __('Allows creating stored routines.');
$strPrivDescCreateTbl = __('Allows creating new tables.');
$strPrivDescCreateTmpTable = __('Allows creating temporary tables.');
$strPrivDescCreateUser = __('Allows creating, dropping and renaming user accounts.');
$strPrivDescCreateView = __('Allows creating new views.');
$strPrivDescDelete = __('Allows deleting data.');
$strPrivDescDropDb = __('Allows dropping databases and tables.');
$strPrivDescDropTbl = __('Allows dropping tables.');
$strPrivDescEvent = __('Allows to set up events for the event scheduler');
$strPrivDescExecute = __('Allows executing stored routines.');
$strPrivDescFile = __('Allows importing data from and exporting data into files.');
$strPrivDescGrant = __('Allows adding users and privileges without reloading the privilege tables.');
$strPrivDescIndex = __('Allows creating and dropping indexes.');
$strPrivDescInsert = __('Allows inserting and replacing data.');
$strPrivDescLockTables = __('Allows locking tables for the current thread.');
$strPrivDescMaxConnections = __('Limits the number of new connections the user may open per hour.');
$strPrivDescMaxQuestions = __('Limits the number of queries the user may send to the server per hour.');
$strPrivDescMaxUpdates = __('Limits the number of commands that change any table or database the user may execute per hour.');
$strPrivDescMaxUserConnections = __('Limits the number of simultaneous connections the user may have.');
$strPrivDescProcess = __('Allows viewing processes of all users');
$strPrivDescReferences = __('Has no effect in this MySQL version.');
$strPrivDescReload = __('Allows reloading server settings and flushing the server\'s caches.');
$strPrivDescReplClient = __('Allows the user to ask where the slaves / masters are.');
$strPrivDescReplSlave = __('Needed for the replication slaves.');
$strPrivDescSelect = __('Allows reading data.');
$strPrivDescShowDb = __('Gives access to the complete list of databases.');
$strPrivDescShowView = __('Allows performing SHOW CREATE VIEW queries.');
$strPrivDescShutdown = __('Allows shutting down the server.');
$strPrivDescSuper = __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.');
$strPrivDescTrigger = __('Allows creating and dropping triggers');
$strPrivDescUpdate = __('Allows changing data.');
$strPrivDescUsage = __('No privileges.');
/**
* Checks if a dropdown box has been used for selecting a database / table
*/
if (PMA_isValid($_REQUEST['pred_tablename'])) {
$tablename = $_REQUEST['pred_tablename'];
unset($pred_tablename);
} elseif (PMA_isValid($_REQUEST['tablename'])) {
$tablename = $_REQUEST['tablename'];
} else {
unset($tablename);
}
if (PMA_isValid($_REQUEST['pred_dbname'])) {
$dbname = $_REQUEST['pred_dbname'];
unset($pred_dbname);
} elseif (PMA_isValid($_REQUEST['dbname'])) {
$dbname = $_REQUEST['dbname'];
} else {
unset($dbname);
unset($tablename);
}
if (isset($dbname)) {
$db_and_table = PMA_backquote(PMA_unescape_mysql_wildcards($dbname)) . '.';
if (isset($tablename)) {
$db_and_table .= PMA_backquote($tablename);
} else {
$db_and_table .= '*';
}
} else {
$db_and_table = '*.*';
}
// check if given $dbname is a wildcard or not
if (isset($dbname)) {
//if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
if (preg_match('/(?<!\\\\)(?:_|%)/i', $dbname)) {
$dbname_is_wildcard = true;
} else {
$dbname_is_wildcard = false;
}
}
/**
* Checks if the user is allowed to do what he tries to...
*/
if (! $is_superuser) {
include 'libraries/server_links.inc.php';
echo '<h2>' . "\n"
. PMA_getIcon('b_usrlist.png')
. __('Privileges') . "\n"
. '</h2>' . "\n";
PMA_Message::error(__('No Privileges'))->display();
include 'libraries/footer.inc.php';
}
$random_n = mt_rand(0, 1000000); // a random number that will be appended to the id of the user forms
/**
* Escapes wildcard in a database+table specification
* before using it in a GRANT statement.
*
* Escaping a wildcard character in a GRANT is only accepted at the global
* or database level, not at table level; this is why I remove
* the escaping character. Internally, in mysql.tables_priv.Db there are
* no escaping (for example test_db) but in mysql.db you'll see test\_db
* for a db-specific privilege.
*
* @param string $dbname Database name
* @param string $tablename Table name
*
* @return string the escaped (if necessary) database.table
*/
function PMA_wildcardEscapeForGrant($dbname, $tablename)
{
if (! strlen($dbname)) {
$db_and_table = '*.*';
} else {
if (strlen($tablename)) {
$db_and_table = PMA_backquote(PMA_unescape_mysql_wildcards($dbname)) . '.';
$db_and_table .= PMA_backquote($tablename);
} else {
$db_and_table = PMA_backquote($dbname) . '.';
$db_and_table .= '*';
}
}
return $db_and_table;
}
/**
* Generates a condition on the user name
*
* @param string $initial the user's initial
*
* @return string the generated condition
*/
function PMA_rangeOfUsers($initial = '')
{
// strtolower() is used because the User field
// might be BINARY, so LIKE would be case sensitive
if (! empty($initial)) {
$ret = " WHERE `User` LIKE '" . PMA_sqlAddSlashes($initial, true) . "%'"
. " OR `User` LIKE '" . PMA_sqlAddSlashes(strtolower($initial), true) . "%'";
} else {
$ret = '';
}
return $ret;
} // end function
/**
* Extracts the privilege information of a priv table row
*
* @param array $row the row
* @param boolean $enableHTML add <dfn> tag with tooltips
*
* @global resource $user_link the database connection
*
* @return array
*/
function PMA_extractPrivInfo($row = '', $enableHTML = false)
{
$grants = array(
array(
'Select_priv',
'SELECT',
__('Allows reading data.')),
array(
'Insert_priv',
'INSERT',
__('Allows inserting and replacing data.')),
array(
'Update_priv',
'UPDATE',
__('Allows changing data.')),
array(
'Delete_priv',
'DELETE',
__('Allows deleting data.')),
array(
'Create_priv',
'CREATE',
__('Allows creating new databases and tables.')),
array(
'Drop_priv',
'DROP',
__('Allows dropping databases and tables.')),
array(
'Reload_priv',
'RELOAD',
__('Allows reloading server settings and flushing the server\'s caches.')),
array(
'Shutdown_priv',
'SHUTDOWN',
__('Allows shutting down the server.')),
array(
'Process_priv',
'PROCESS',
__('Allows viewing processes of all users')),
array(
'File_priv',
'FILE',
__('Allows importing data from and exporting data into files.')),
array(
'References_priv',
'REFERENCES',
__('Has no effect in this MySQL version.')),
array(
'Index_priv',
'INDEX',
__('Allows creating and dropping indexes.')),
array(
'Alter_priv',
'ALTER',
__('Allows altering the structure of existing tables.')),
array(
'Show_db_priv',
'SHOW DATABASES',
__('Gives access to the complete list of databases.')),
array(
'Super_priv',
'SUPER',
__('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.')),
array(
'Create_tmp_table_priv',
'CREATE TEMPORARY TABLES',
__('Allows creating temporary tables.')),
array(
'Lock_tables_priv',
'LOCK TABLES',
__('Allows locking tables for the current thread.')),
array(
'Repl_slave_priv',
'REPLICATION SLAVE',
__('Needed for the replication slaves.')),
array(
'Repl_client_priv',
'REPLICATION CLIENT',
__('Allows the user to ask where the slaves / masters are.')),
array(
'Create_view_priv',
'CREATE VIEW',
__('Allows creating new views.')),
array(
'Event_priv',
'EVENT',
__('Allows to set up events for the event scheduler')),
array(
'Trigger_priv',
'TRIGGER',
__('Allows creating and dropping triggers')),
// for table privs:
array(
'Create View_priv',
'CREATE VIEW',
__('Allows creating new views.')),
array(
'Show_view_priv',
'SHOW VIEW',
__('Allows performing SHOW CREATE VIEW queries.')),
// for table privs:
array(
'Show view_priv',
'SHOW VIEW',
__('Allows performing SHOW CREATE VIEW queries.')),
array(
'Create_routine_priv',
'CREATE ROUTINE',
__('Allows creating stored routines.')),
array(
'Alter_routine_priv',
'ALTER ROUTINE',
__('Allows altering and dropping stored routines.')),
array(
'Create_user_priv',
'CREATE USER',
__('Allows creating, dropping and renaming user accounts.')),
array(
'Execute_priv',
'EXECUTE',
__('Allows executing stored routines.')),
);
if (! empty($row) && isset($row['Table_priv'])) {
$row1 = PMA_DBI_fetch_single_row(
'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
'ASSOC', $GLOBALS['userlink']
);
$av_grants = explode('\',\'', substr($row1['Type'], 5, strlen($row1['Type']) - 7));
unset($row1);
$users_grants = explode(',', $row['Table_priv']);
foreach ($av_grants as $current_grant) {
$row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
}
unset($current_grant);
unset($av_grants);
unset($users_grants);
}
$privs = array();
$allPrivileges = true;
foreach ($grants as $current_grant) {
if ((! empty($row) && isset($row[$current_grant[0]]))
|| (empty($row) && isset($GLOBALS[$current_grant[0]]))
) {
if ((! empty($row) && $row[$current_grant[0]] == 'Y')
|| (empty($row)
&& ($GLOBALS[$current_grant[0]] == 'Y'
|| (is_array($GLOBALS[$current_grant[0]])
&& count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count']
&& empty($GLOBALS[$current_grant[0] . '_none']))))
) {
if ($enableHTML) {
$privs[] = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
} else {
$privs[] = $current_grant[1];
}
} elseif (! empty($GLOBALS[$current_grant[0]])
&& is_array($GLOBALS[$current_grant[0]])
&& empty($GLOBALS[$current_grant[0] . '_none'])) {
if ($enableHTML) {
$priv_string = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
} else {
$priv_string = $current_grant[1];
}
$privs[] = $priv_string . ' (`' . join('`, `', $GLOBALS[$current_grant[0]]) . '`)';
} else {
$allPrivileges = false;
}
}
}
if (empty($privs)) {
if ($enableHTML) {
$privs[] = '<dfn title="' . __('No privileges.') . '">USAGE</dfn>';
} else {
$privs[] = 'USAGE';
}
} elseif ($allPrivileges && (! isset($GLOBALS['grant_count']) || count($privs) == $GLOBALS['grant_count'])) {
if ($enableHTML) {
$privs = array('<dfn title="' . __('Includes all privileges except GRANT.') . '">ALL PRIVILEGES</dfn>');
} else {
$privs = array('ALL PRIVILEGES');
}
}
return $privs;
} // end of the 'PMA_extractPrivInfo()' function
/**
* Displays on which column(s) a table-specific privilege is granted
*
* @param array $columns
* @param array $row
* @param string $name_for_select
* @param string $priv_for_header
* @param string $name
* @param string $name_for_dfn
* @param string $name_for_current
*
* @return void
*/
function PMA_displayColumnPrivs($columns, $row, $name_for_select,
$priv_for_header, $name, $name_for_dfn, $name_for_current
) {
echo ' <div class="item" id="div_item_' . $name . '">' . "\n"
. ' <label for="select_' . $name . '_priv">' . "\n"
. ' <code><dfn title="' . $name_for_dfn . '">'
. $priv_for_header . '</dfn></code>' . "\n"
. ' </label><br />' . "\n"
. ' <select id="select_' . $name . '_priv" name="'
. $name_for_select . '[]" multiple="multiple" size="8">' . "\n";
foreach ($columns as $current_column => $current_column_privileges) {
echo ' <option value="' . htmlspecialchars($current_column) . '"';
if ($row[$name_for_select] == 'Y' || $current_column_privileges[$name_for_current]) {
echo ' selected="selected"';
}
echo '>' . htmlspecialchars($current_column) . '</option>' . "\n";
}
echo ' </select>' . "\n"
. ' <i>' . __('Or') . '</i>' . "\n"
. ' <label for="checkbox_' . $name_for_select
. '_none"><input type="checkbox"'
. (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
. ' name="' . $name_for_select . '_none" id="checkbox_'
. $name_for_select . '_none" title="' . _pgettext('None privileges', 'None') . '" />'
. _pgettext('None privileges', 'None') . '</label>' . "\n"
. ' </div>' . "\n";
} // end function
/**
* Displays the privileges form table
*
* @param string $db the database
* @param string $table the table
* @param boolean $submit wheather to display the submit button or not
*
* @global array $cfg the phpMyAdmin configuration
* @global ressource $user_link the database connection
*
* @return void
*/
function PMA_displayPrivTable($db = '*', $table = '*', $submit = true)
{
global $random_n;
if ($db == '*') {
$table = '*';
}
if (isset($GLOBALS['username'])) {
$username = $GLOBALS['username'];
$hostname = $GLOBALS['hostname'];
if ($db == '*') {
$sql_query = "SELECT * FROM `mysql`.`user`"
." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'"
." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "';";
} elseif ($table == '*') {
$sql_query = "SELECT * FROM `mysql`.`db`"
." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'"
." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'"
." AND '" . PMA_unescape_mysql_wildcards($db) . "'"
." LIKE `Db`;";
} else {
$sql_query = "SELECT `Table_priv`"
." FROM `mysql`.`tables_priv`"
." WHERE `User` = '" . PMA_sqlAddSlashes($username) . "'"
." AND `Host` = '" . PMA_sqlAddSlashes($hostname) . "'"
." AND `Db` = '" . PMA_unescape_mysql_wildcards($db) . "'"
." AND `Table_name` = '" . PMA_sqlAddSlashes($table) . "';";
}
$row = PMA_DBI_fetch_single_row($sql_query);
}
if (empty($row)) {
if ($table == '*') {
if ($db == '*') {
$sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;';
} elseif ($table == '*') {
$sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;';
}
$res = PMA_DBI_query($sql_query);
while ($row1 = PMA_DBI_fetch_row($res)) {
if (substr($row1[0], 0, 4) == 'max_') {
$row[$row1[0]] = 0;
} else {
$row[$row1[0]] = 'N';
}
}
PMA_DBI_free_result($res);
} else {
$row = array('Table_priv' => '');
}
}
if (isset($row['Table_priv'])) {
$row1 = PMA_DBI_fetch_single_row(
'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
'ASSOC', $GLOBALS['userlink']
);
// note: in MySQL 5.0.3 we get "Create View', 'Show view';
// the View for Create is spelled with uppercase V
// the view for Show is spelled with lowercase v
// and there is a space between the words
$av_grants = explode('\',\'', substr($row1['Type'], strpos($row1['Type'], '(') + 2, strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3));
unset($row1);
$users_grants = explode(',', $row['Table_priv']);
foreach ($av_grants as $current_grant) {
$row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
}
unset($row['Table_priv'], $current_grant, $av_grants, $users_grants);
// get collumns
$res = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote(PMA_unescape_mysql_wildcards($db)) . '.' . PMA_backquote($table) . ';');
$columns = array();
if ($res) {
while ($row1 = PMA_DBI_fetch_row($res)) {
$columns[$row1[0]] = array(
'Select' => false,
'Insert' => false,
'Update' => false,
'References' => false
);
}
PMA_DBI_free_result($res);
}
unset($res, $row1);
}
// t a b l e - s p e c i f i c p r i v i l e g e s
if (! empty($columns)) {
$res = PMA_DBI_query(
'SELECT `Column_name`, `Column_priv`'
.' FROM `mysql`.`columns_priv`'
.' WHERE `User`'
.' = \'' . PMA_sqlAddSlashes($username) . "'"
.' AND `Host`'
.' = \'' . PMA_sqlAddSlashes($hostname) . "'"
.' AND `Db`'
.' = \'' . PMA_sqlAddSlashes(PMA_unescape_mysql_wildcards($db)) . "'"
.' AND `Table_name`'
.' = \'' . PMA_sqlAddSlashes($table) . '\';'
);
while ($row1 = PMA_DBI_fetch_row($res)) {
$row1[1] = explode(',', $row1[1]);
foreach ($row1[1] as $current) {
$columns[$row1[0]][$current] = true;
}
}
PMA_DBI_free_result($res);
unset($res, $row1, $current);
echo '<input type="hidden" name="grant_count" value="' . count($row) . '" />' . "\n"
. '<input type="hidden" name="column_count" value="' . count($columns) . '" />' . "\n"
. '<fieldset id="fieldset_user_priv">' . "\n"
. ' <legend>' . __('Table-specific privileges')
. PMA_showHint(__('Note: MySQL privilege names are expressed in English'))
. '</legend>' . "\n";
// privs that are attached to a specific column
PMA_displayColumnPrivs(
$columns, $row, 'Select_priv', 'SELECT',
'select', __('Allows reading data.'), 'Select'
);
PMA_displayColumnPrivs(
$columns, $row, 'Insert_priv', 'INSERT',
'insert', __('Allows inserting and replacing data.'), 'Insert'
);
PMA_displayColumnPrivs(
$columns, $row, 'Update_priv', 'UPDATE',
'update', __('Allows changing data.'), 'Update'
);
PMA_displayColumnPrivs(
$columns, $row, 'References_priv', 'REFERENCES', 'references',
__('Has no effect in this MySQL version.'), 'References'
);
// privs that are not attached to a specific column
echo ' <div class="item">' . "\n";
foreach ($row as $current_grant => $current_grant_value) {
$grant_type = substr($current_grant, 0, (strlen($current_grant) - 5));
if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))) {
continue;
}
// make a substitution to match the messages variables;
// also we must substitute the grant we get, because we can't generate
// a form variable containing blanks (those would get changed to
// an underscore when receiving the POST)
if ($current_grant == 'Create View_priv') {
$tmp_current_grant = 'CreateView_priv';
$current_grant = 'Create_view_priv';
} elseif ($current_grant == 'Show view_priv') {
$tmp_current_grant = 'ShowView_priv';
$current_grant = 'Show_view_priv';
} else {
$tmp_current_grant = $current_grant;
}
echo ' <div class="item">' . "\n"
. ' <input type="checkbox"'
. (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
. ' name="' . $current_grant . '" id="checkbox_' . $current_grant
. '" value="Y" '
. ($current_grant_value == 'Y' ? 'checked="checked" ' : '')
. 'title="';
echo (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))])
? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]
: $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl']) . '"/>' . "\n";
echo ' <label for="checkbox_' . $current_grant
. '"><code><dfn title="'
. (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))])
? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]
: $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl'])
. '">' . strtoupper(substr($current_grant, 0, strlen($current_grant) - 5)) . '</dfn></code></label>' . "\n"
. ' </div>' . "\n";
} // end foreach ()
echo ' </div>' . "\n";
// for Safari 2.0.2
echo ' <div class="clearfloat"></div>' . "\n";
} else {
// g l o b a l o r d b - s p e c i f i c
//
$privTable_names = array(0 => __('Data'), 1 => __('Structure'), 2 => __('Administration'));
// d a t a
$privTable[0] = array(
array('Select', 'SELECT', __('Allows reading data.')),
array('Insert', 'INSERT', __('Allows inserting and replacing data.')),
array('Update', 'UPDATE', __('Allows changing data.')),
array('Delete', 'DELETE', __('Allows deleting data.'))
);
if ($db == '*') {
$privTable[0][] = array('File', 'FILE', __('Allows importing data from and exporting data into files.'));
}
// s t r u c t u r e
$privTable[1] = array(
array('Create', 'CREATE', ($table == '*' ? __('Allows creating new databases and tables.') : __('Allows creating new tables.'))),
array('Alter', 'ALTER', __('Allows altering the structure of existing tables.')),
array('Index', 'INDEX', __('Allows creating and dropping indexes.')),
array('Drop', 'DROP', ($table == '*' ? __('Allows dropping databases and tables.') : __('Allows dropping tables.'))),
array('Create_tmp_table', 'CREATE TEMPORARY TABLES', __('Allows creating temporary tables.')),
array('Show_view', 'SHOW VIEW', __('Allows performing SHOW CREATE VIEW queries.')),
array('Create_routine', 'CREATE ROUTINE', __('Allows creating stored routines.')),
array('Alter_routine', 'ALTER ROUTINE', __('Allows altering and dropping stored routines.')),
array('Execute', 'EXECUTE', __('Allows executing stored routines.')),
);
// this one is for a db-specific priv: Create_view_priv
if (isset($row['Create_view_priv'])) {
$privTable[1][] = array('Create_view', 'CREATE VIEW', __('Allows creating new views.'));
}
// this one is for a table-specific priv: Create View_priv
if (isset($row['Create View_priv'])) {
$privTable[1][] = array('Create View', 'CREATE VIEW', __('Allows creating new views.'));
}
if (isset($row['Event_priv'])) {
// MySQL 5.1.6
$privTable[1][] = array('Event', 'EVENT', __('Allows to set up events for the event scheduler'));
$privTable[1][] = array('Trigger', 'TRIGGER', __('Allows creating and dropping triggers'));
}
// a d m i n i s t r a t i o n
$privTable[2] = array(
array('Grant', 'GRANT', __('Allows adding users and privileges without reloading the privilege tables.')),
);
if ($db == '*') {
$privTable[2][] = array('Super', 'SUPER', __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.'));
$privTable[2][] = array('Process', 'PROCESS', __('Allows viewing processes of all users'));
$privTable[2][] = array('Reload', 'RELOAD', __('Allows reloading server settings and flushing the server\'s caches.'));
$privTable[2][] = array('Shutdown', 'SHUTDOWN', __('Allows shutting down the server.'));
$privTable[2][] = array('Show_db', 'SHOW DATABASES', __('Gives access to the complete list of databases.'));
}
$privTable[2][] = array('Lock_tables', 'LOCK TABLES', __('Allows locking tables for the current thread.'));
$privTable[2][] = array('References', 'REFERENCES', __('Has no effect in this MySQL version.'));
if ($db == '*') {
$privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', __('Allows the user to ask where the slaves / masters are.'));
$privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', __('Needed for the replication slaves.'));
$privTable[2][] = array('Create_user', 'CREATE USER', __('Allows creating, dropping and renaming user accounts.'));
}
echo '<input type="hidden" name="grant_count" value="'
. (count($privTable[0]) + count($privTable[1]) + count($privTable[2]) - (isset($row['Grant_priv']) ? 1 : 0))
. '" />' . "\n"
. '<fieldset id="fieldset_user_global_rights">' . "\n"
. ' <legend>' . "\n"
. ' '
. ($db == '*'
? __('Global privileges')
: ($table == '*'
? __('Database-specific privileges')
: __('Table-specific privileges'))) . "\n"
. ' (<a href="server_privileges.php?'
. $GLOBALS['url_query'] . '&checkall=1" onclick="setCheckboxes(\'addUsersForm_' . $random_n . '\', true); return false;">'
. __('Check All') . '</a> /' . "\n"
. ' <a href="server_privileges.php?'
. $GLOBALS['url_query'] . '" onclick="setCheckboxes(\'addUsersForm_' . $random_n . '\', false); return false;">'
. __('Uncheck All') . '</a>)' . "\n"
. ' </legend>' . "\n"
. ' <p><small><i>' . __('Note: MySQL privilege names are expressed in English') . '</i></small></p>' . "\n";
// Output the Global privilege tables with checkboxes
foreach ($privTable as $i => $table) {
echo ' <fieldset>' . "\n"
. ' <legend>' . __($privTable_names[$i]) . '</legend>' . "\n";
foreach ($table as $priv) {
echo ' <div class="item">' . "\n"
. ' <input type="checkbox"'
. ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0] . '_priv"'
. ' value="Y" title="' . $priv[2] . '"'
. ((! empty($GLOBALS['checkall']) || $row[$priv[0] . '_priv'] == 'Y') ? ' checked="checked"' : '')
. '/>' . "\n"
. ' <label for="checkbox_' . $priv[0] . '_priv"><code><dfn title="' . $priv[2] . '">'
. $priv[1] . '</dfn></code></label>' . "\n"
. ' </div>' . "\n";
}
echo ' </fieldset>' . "\n";
}
// The "Resource limits" box is not displayed for db-specific privs
if ($db == '*') {
echo ' <fieldset>' . "\n"
. ' <legend>' . __('Resource limits') . '</legend>' . "\n"
. ' <p><small><i>' . __('Note: Setting these options to 0 (zero) removes the limit.') . '</i></small></p>' . "\n"
. ' <div class="item">' . "\n"
. ' <label for="text_max_questions"><code><dfn title="'
. __('Limits the number of queries the user may send to the server per hour.') . '">MAX QUERIES PER HOUR</dfn></code></label>' . "\n"
. ' <input type="text" name="max_questions" id="text_max_questions" value="'
. $row['max_questions'] . '" size="11" maxlength="11" title="' . __('Limits the number of queries the user may send to the server per hour.') . '" />' . "\n"
. ' </div>' . "\n"
. ' <div class="item">' . "\n"
. ' <label for="text_max_updates"><code><dfn title="'
. __('Limits the number of commands that change any table or database the user may execute per hour.') . '">MAX UPDATES PER HOUR</dfn></code></label>' . "\n"
. ' <input type="text" name="max_updates" id="text_max_updates" value="'
. $row['max_updates'] . '" size="11" maxlength="11" title="' . __('Limits the number of commands that change any table or database the user may execute per hour.') . '" />' . "\n"
. ' </div>' . "\n"
. ' <div class="item">' . "\n"
. ' <label for="text_max_connections"><code><dfn title="'
. __('Limits the number of new connections the user may open per hour.') . '">MAX CONNECTIONS PER HOUR</dfn></code></label>' . "\n"
. ' <input type="text" name="max_connections" id="text_max_connections" value="'
. $row['max_connections'] . '" size="11" maxlength="11" title="' . __('Limits the number of new connections the user may open per hour.') . '" />' . "\n"
. ' </div>' . "\n"
. ' <div class="item">' . "\n"
. ' <label for="text_max_user_connections"><code><dfn title="'
. __('Limits the number of simultaneous connections the user may have.') . '">MAX USER_CONNECTIONS</dfn></code></label>' . "\n"
. ' <input type="text" name="max_user_connections" id="text_max_user_connections" value="'
. $row['max_user_connections'] . '" size="11" maxlength="11" title="' . __('Limits the number of simultaneous connections the user may have.') . '" />' . "\n"
. ' </div>' . "\n"
. ' </fieldset>' . "\n";
}
// for Safari 2.0.2
echo ' <div class="clearfloat"></div>' . "\n";
}
echo '</fieldset>' . "\n";
if ($submit) {
echo '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">' . "\n"
. ' <input type="submit" name="update_privs" value="' . __('Go') . '" />' . "\n"
. '</fieldset>' . "\n";
}
} // end of the 'PMA_displayPrivTable()' function
/**
* Displays the fields used by the "new user" form as well as the
* "change login information / copy user" form.
*
* @param string $mode are we creating a new user or are we just
* changing one? (allowed values: 'new', 'change')
*
* @global array $cfg the phpMyAdmin configuration
* @global ressource $user_link the database connection
*
* @return void
*/
function PMA_displayLoginInformationFields($mode = 'new')
{
// Get user/host name lengths
$fields_info = PMA_DBI_get_columns('mysql', 'user', null, true);
$username_length = 16;
$hostname_length = 41;
foreach ($fields_info as $val) {
if ($val['Field'] == 'User') {
strtok($val['Type'], '()');
$v = strtok('()');
if (is_int($v)) {
$username_length = $v;
}
} elseif ($val['Field'] == 'Host') {
strtok($val['Type'], '()');
$v = strtok('()');
if (is_int($v)) {
$hostname_length = $v;
}
}
}
unset($fields_info);
if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) {
$GLOBALS['pred_username'] = 'any';
}
echo '<fieldset id="fieldset_add_user_login">' . "\n"
. '<legend>' . __('Login Information') . '</legend>' . "\n"
. '<div class="item">' . "\n"
. '<label for="select_pred_username">' . "\n"
. ' ' . __('User name') . ':' . "\n"
. '</label>' . "\n"
. '<span class="options">' . "\n"
. ' <select name="pred_username" id="select_pred_username" title="' . __('User name') . '"' . "\n"
. ' onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">' . "\n"
. ' <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . __('Any user') . '</option>' . "\n"
. ' <option value="userdefined"' . ((! isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . __('Use text field') . ':</option>' . "\n"
. ' </select>' . "\n"
. '</span>' . "\n"
. '<input type="text" name="username" maxlength="'
. $username_length . '" title="' . __('User name') . '"'
. (empty($GLOBALS['username'])
? ''
: ' value="' . htmlspecialchars(
isset($GLOBALS['new_username'])
? $GLOBALS['new_username']
: $GLOBALS['username']
) . '"'
)
. ' onchange="pred_username.value = \'userdefined\';" />' . "\n"
. '</div>' . "\n"
. '<div class="item">' . "\n"
. '<label for="select_pred_hostname">' . "\n"
. ' ' . __('Host') . ':' . "\n"
. '</label>' . "\n"
. '<span class="options">' . "\n"
. ' <select name="pred_hostname" id="select_pred_hostname" title="' . __('Host') . '"' . "\n";
$_current_user = PMA_DBI_fetch_value('SELECT USER();');
if (! empty($_current_user)) {
$thishost = str_replace("'", '', substr($_current_user, (strrpos($_current_user, '@') + 1)));
if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
unset($thishost);
}
}
echo ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
. (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
. 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
unset($_current_user);
// when we start editing a user, $GLOBALS['pred_hostname'] is not defined
if (! isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
switch (strtolower($GLOBALS['hostname'])) {
case 'localhost':
case '127.0.0.1':
$GLOBALS['pred_hostname'] = 'localhost';
break;
case '%':
$GLOBALS['pred_hostname'] = 'any';
break;
default:
$GLOBALS['pred_hostname'] = 'userdefined';
break;
}
}
echo ' <option value="any"'
. ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
? ' selected="selected"' : '') . '>' . __('Any host')
. '</option>' . "\n"
. ' <option value="localhost"'
. ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost')
? ' selected="selected"' : '') . '>' . __('Local')
. '</option>' . "\n";
if (! empty($thishost)) {
echo ' <option value="thishost"'
. ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost')
? ' selected="selected"' : '') . '>' . __('This Host')
. '</option>' . "\n";
}
unset($thishost);
echo ' <option value="hosttable"'
. ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable')
? ' selected="selected"' : '') . '>' . __('Use Host Table')
. '</option>' . "\n"
. ' <option value="userdefined"'
. ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined')
? ' selected="selected"' : '')
. '>' . __('Use text field') . ':</option>' . "\n"
. ' </select>' . "\n"
. '</span>' . "\n"
. '<input type="text" name="hostname" maxlength="'
. $hostname_length . '" value="'
. htmlspecialchars(isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '')
. '" title="' . __('Host')
. '" onchange="pred_hostname.value = \'userdefined\';" />' . "\n"
. PMA_showHint(__('When Host table is used, this field is ignored and values stored in Host table are used instead.'))
. '</div>' . "\n"
. '<div class="item">' . "\n"
. '<label for="select_pred_password">' . "\n"
. ' ' . __('Password') . ':' . "\n"
. '</label>' . "\n"
. '<span class="options">' . "\n"
. ' <select name="pred_password" id="select_pred_password" title="'
. __('Password') . '"' . "\n"
. ' onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">' . "\n"
. ($mode == 'change' ? ' <option value="keep" selected="selected">' . __('Do not change the password') . '</option>' . "\n" : '')
. ' <option value="none"';
if (isset($GLOBALS['username']) && $mode != 'change') {
echo ' selected="selected"';
}
echo '>' . __('No Password') . '</option>' . "\n"
. ' <option value="userdefined"' . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>' . __('Use text field') . ':</option>' . "\n"
. ' </select>' . "\n"
. '</span>' . "\n"
. '<input type="password" id="text_pma_pw" name="pma_pw" title="' . __('Password') . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
. '</div>' . "\n"
. '<div class="item" id="div_element_before_generate_password">' . "\n"
. '<label for="text_pma_pw2">' . "\n"
. ' ' . __('Re-type') . ':' . "\n"
. '</label>' . "\n"
. '<span class="options"> </span>' . "\n"
. '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . __('Re-type') . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
. '</div>' . "\n"
// Generate password added here via jQuery
. '</fieldset>' . "\n";
} // end of the 'PMA_displayUserAndHostFields()' function
/**
* Returns all the grants for a certain user on a certain host
* Used in the export privileges for all users section
*
* @param string $user User name
* @param string $host Host name
*
* @return string containing all the grants text
*/
function PMA_getGrants($user, $host)
{
$grants = PMA_DBI_fetch_result("SHOW GRANTS FOR '" . PMA_sqlAddSlashes($user) . "'@'" . PMA_sqlAddSlashes($host) . "'");
$response = '';
foreach ($grants as $one_grant) {
$response .= $one_grant . ";\n\n";
}
return $response;
} // end of the 'PMA_getGrants()' function
/**
* Changes / copies a user, part I
*/
if (isset($_REQUEST['change_copy'])) {