-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathphpmyadmin.sh
More file actions
175 lines (150 loc) · 5.8 KB
/
phpmyadmin.sh
File metadata and controls
175 lines (150 loc) · 5.8 KB
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
#!/bin/bash
################################################################################
# Script Name: phpmyadmin.sh
# Description: View and set domain/ip for phpmyadmin access.
# Usage: opencli phpmyadmin [set <domain_name> | ip]
# Author: Stefan Pejcic
# Created: 30.03.2026
# Last Modified: 23.04.2026
# Company: openpanel.com
# Copyright (c) openpanel.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
readonly CADDYFILE="/etc/openpanel/caddy/Caddyfile"
START_MARKER="# START PHPMYADMIN DOMAIN #"
END_MARKER="# END PHPMYADMIN DOMAIN #"
is_valid_domain() {
local domain="$1"
[[ "$domain" =~ ^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$ ]]
}
get_domain() {
grep -A1 "START PHPMYADMIN DOMAIN" "$CADDYFILE" | tail -n1 | awk '{print $1}'
}
remove_block() {
if grep -q "$START_MARKER" "$CADDYFILE" && grep -q "$END_MARKER" "$CADDYFILE"; then
sed -i "/$START_MARKER/,/$END_MARKER/d" "$CADDYFILE"
fi
}
set_domain() {
NEW_DOMAIN="$1"
if [ -z "$NEW_DOMAIN" ]; then
echo "Usage: opencli phpmyadmin set <domain>"
exit 1
fi
if [ "$NEW_DOMAIN" = "ip" ]; then
remove_block
update_env_for_existing_users "disable"
update_env_for_new_users "disable"
echo "Done"
return
fi
if ! is_valid_domain "$NEW_DOMAIN"; then
echo "ERROR: Invalid domain format. Please provide a valid domain."
exit 1
fi
if opencli domains-whoowns "$NEW_DOMAIN" | grep -q "not found in the database."; then
if [ -f "$CADDYFILE" ] || [ -f "$CONFIG_FILE" ]; then
if grep -q -E "^\s*$NEW_DOMAIN\s*\{" "$CADDYFILE" 2>/dev/null; then
echo "ERROR: $NEW_DOMAIN is already configured for another system service and can not be set for phpMyAdmin access."
exit 1
fi
fi
else
echo "ERROR: Domain $NEW_DOMAIN is already in used by an OpenPanel account and can not be set for phpMyAdmin access."
exit 1
fi
NEW_BLOCK=$(cat <<EOF
$START_MARKER
${NEW_DOMAIN}: {
@dynamicPort path_regexp port ^/(\\d+)(/.*)?\$
handle @dynamicPort {
uri strip_prefix /{http.regexp.port.1}
reverse_proxy localhost:{http.regexp.port.1}
}
handle {
respond "Please use your unique phpmyadmin path shown on OpenPanel > MySQL > phpMyAdmin page."
}
}
$END_MARKER
EOF
)
echo "Creating proxy for the $NEW_DOMAIN domain.."
if grep -q "$START_MARKER" "$CADDYFILE" && grep -q "$END_MARKER" "$CADDYFILE"; then
# edit
sed -i "/$START_MARKER/,/$END_MARKER/c\\
$NEW_BLOCK
" "$CADDYFILE"
echo "Updated existing PHPMYADMIN domain block."
else
# insert
echo -e "\n$NEW_BLOCK" >> "$CADDYFILE"
echo "Added new PHPMYADMIN domain block."
fi
update_env_for_existing_users "$NEW_DOMAIN"
update_env_for_new_users "$NEW_DOMAIN"
echo "Done"
}
update_env_for_existing_users() {
DOMAIN="$1"
echo "Updating PMA_ABSOLUTE_URI for all existing users..."
for file in /home/*/.env; do
context=$(basename "$(dirname "$file")")
if [ "$DOMAIN" = "disable" ]; then
sed -i -E 's|^PMA_ABSOLUTE_URI=.*|PMA_ABSOLUTE_URI=\"\"|' "$file"
else
port=$(grep -E '^PMA_PORT=' "$file" | sed -E 's/.*"([0-9]+):.*/\1/')
if [ -n "$port" ]; then
sed -i -E "s|^PMA_ABSOLUTE_URI=.*|PMA_ABSOLUTE_URI=\"https://$DOMAIN/${port}/\"|" "$file"
fi
fi
if docker --context "$context" compose ps --services 2>/dev/null | grep "^phpmyadmin$"; then
echo "Restarting running phpmyadmin service for docker context: $context"
docker --context "$context" compose down phpmyadmin &>/dev/null
docker --context "$context" compose up -d phpmyadmin &>/dev/null
fi
done
}
update_env_for_new_users() {
echo "Updating PMA_ABSOLUTE_URI in template for new users.."
local file="/etc/openpanel/docker/compose/1.0/.env"
if [ "$DOMAIN" = "disable" ]; then
sed -i -E 's|^PMA_ABSOLUTE_URI=.*|PMA_ABSOLUTE_URI=\"\"|' "$file"
else
sed -i -E "s|^PMA_ABSOLUTE_URI=.*|PMA_ABSOLUTE_URI=\"https://$DOMAIN/{PMA_PORT}/\"|" "$file"
fi
touch "/etc/openpanel/caddy/domains/$DOMAIN.conf"
if docker --context default compose ps -q caddy >/dev/null 2>&1; then
nohup docker --context default restart caddy >/dev/null 2>&1 &
disown
else
nohup bash -c "cd /root && docker --context default compose up -d caddy" >/dev/null 2>&1 &
disown
fi
}
if [ "$#" -eq 0 ]; then
get_domain
elif [ "$1" = "set" ]; then
set_domain "$2"
else
echo "Usage:"
echo " opencli phpmyadmin # Show current phpMyAdmin link"
echo " opencli phpmyadmin set <domain> # Set https://DOMAIN/PORT/ for phpMyAdmin access."
echo " opencli phpmyadmin set ip # Set http://IP:PORT for phpMyAdmin access."
fi