PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.0.4
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.0.4
4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Utils / DBPermissions.php
wp-staging / Framework / Utils Last commit date
Cache 2 years ago ThirdParty 3 years ago DBPermissions.php 3 years ago Escape.php 3 years ago Math.php 5 years ago Sanitize.php 2 years ago ServerVars.php 3 years ago SlashMode.php 5 years ago Strings.php 2 years ago Times.php 3 years ago Urls.php 3 years ago WpDefaultDirectories.php 2 years ago
DBPermissions.php
134 lines
1 <?php
2
3 namespace WPStaging\Framework\Utils;
4
5 use wpdb;
6 use ArrayIterator;
7 use WPStaging\Framework\Security\Auth;
8
9 /**
10 * Class Check user permissions on DB
11 * @package WPStaging\Framework\Utils
12 */
13 class DBPermissions
14 {
15 /** @var wpdb */
16 protected $wpdb;
17
18 /** @var Auth */
19 private $auth;
20
21 public function __construct(wpdb $wpdb, Auth $auth)
22 {
23 $this->wpdb = $wpdb;
24 $this->auth = $auth;
25 }
26
27 /**
28 * Ajax check user permissions on DB before push process start
29 */
30 public function ajaxCheckDBPermissions()
31 {
32 if (!$this->isAuthenticated()) {
33 return;
34 }
35
36 $type = isset($_POST['type']) ? sanitize_text_field($_POST['type']) : '';
37 $grantsToCheck = ['CREATE', 'UPDATE', 'INSERT', 'DROP'];
38 if ($type === 'push') {
39 $grantsToCheck[] = 'ALTER';
40 }
41
42 if ($this->isAllowed(['ALL PRIVILEGES']) || $this->isAllowed($grantsToCheck)) {
43 wp_send_json_success();
44 }
45
46 $message = __("The database user might not have sufficient permissions to use the restore action. Continue the process anyway by clicking the 'Proceed' button or change the user's DB permissions and resume the process.<br/><br/> Required permissions are: CREATE, UPDATE, INSERT, DROP.", 'wp-staging');
47 if ($type === 'push') {
48 $message = __("The database user might not have sufficient permissions to use the push action. Continue the process anyway by clicking the 'Proceed' button or alter the user's DB permissions and resume the process.<br/><br/> Required permissions are: CREATE, UPDATE, ALTER, INSERT, DROP.", 'wp-staging');
49 }
50 $message = '<span id="wpstg-permission-info-output">' . $message . '</span>';
51 $message .= '<span id="wpstg-permission-info-data">' . $this->getDebugInfo() . '</span>';
52 $message .= '<br/><br/><button type="button" id="wpstg-db-permission-show-full-message" class="wpstg-link-btn wpstg-blue-primary">' . __("Show Full Message", "wp-staging") . '</button>';
53
54 wp_send_json_error([
55 'message' => wp_kses_post($message),
56 ]);
57 }
58
59 /**
60 * Check if the current user has the grants given in arguments.
61 *
62 * @param array $grantsToCheck
63 * @return bool
64 */
65 public function isAllowed($grantsToCheck)
66 {
67 $grants = $this->wpdb->get_results("SHOW GRANTS;");
68 if (empty($grants) || $this->wpdb->last_error) {
69 return false;
70 }
71
72 $hasGranted = array_filter($grants, function ($grant) use ($grantsToCheck) {
73 $grant = (new ArrayIterator($grant))->current();
74 if (stripos($grant, '`' . DB_NAME . '`') !== false || stripos($grant, '`' . $this->wpdb->esc_like(DB_NAME) . '`') !== false || stripos($grant, '*.*') !== false) {
75 foreach ($grantsToCheck as $value) {
76 if (!preg_match("/" . $value . "[,]/", $grant) && !preg_match("/" . $value . " ON/", $grant)) {
77 return false;
78 }
79 }
80 return true;
81 }
82 });
83
84 if (!empty($hasGranted)) {
85 return true;
86 }
87
88 return false;
89 }
90
91 /**
92 * @return string
93 */
94 public function getDebugInfo()
95 {
96 $data = '<textarea class="wpstg-permission-info-output wpstg-textbox" readonly="readonly" name="wpstg-permission-info" title="' . __('Please copy and paste this message and report it to us!', 'wp-staging') . '">';
97 $data .= PHP_EOL . __('DB Name: ', 'wp-staging') . DB_NAME;
98 $data .= PHP_EOL . __('DB User: ', 'wp-staging') . DB_USER;
99 $data .= PHP_EOL . __('DB Host: ', 'wp-staging') . DB_HOST;
100
101 $grants = $this->wpdb->get_results("SHOW GRANTS;");
102 if (empty($grants) || $this->wpdb->last_error) {
103 $data .= PHP_EOL . __('wpdb query error: ', 'wp-staging') . $this->wpdb->last_error;
104 return wp_kses_post($data);
105 }
106 $grantsHtml = '';
107 foreach ($grants as $grant) {
108 $grantsHtml .= PHP_EOL . (new ArrayIterator($grant))->current() . ';';
109 }
110
111 $data .= PHP_EOL . __('User Grants: ', 'wp-staging') . $grantsHtml;
112 $data .= '</textarea>';
113
114 return wp_kses_post($data);
115 }
116
117 /**
118 * @param wpdb $db
119 * @return void
120 */
121 public function setDB(wpdb $wpdb)
122 {
123 $this->wpdb = $wpdb;
124 }
125
126 /**
127 * @return bool Whether the current request is considered to be authenticated.
128 */
129 protected function isAuthenticated()
130 {
131 return $this->auth->isAuthenticatedRequest();
132 }
133 }
134