-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrename.php
137 lines (126 loc) · 2.6 KB
/
rename.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
<?php
require_once(__DIR__ . "/global.php");
session_start();
HtmlHeader("rename");
if (IsLoggedIn())
{
global $username;
$username = $_SESSION['Username'];
LoginIndex();
}
else
{
NotLoginIndex();
}
fok();
function NotLoginIndex()
{
?>
<div class="login-page">
<div class="form">
<form action="login.php" class="login-form">
<a>Please login first.</br></a>
<button type="submit">login</button>
</form>
</div>
</div>
<?php
}
function LoginIndex()
{
global $username;
$id=0;
$error="";
if (!empty($_GET['id']))
{
$id = isset($_GET['id'])? $_GET['id'] : 0;
if (!is_numeric($id))
{
$error="erronous id";
}
$id = (int)$id;
}
$cert_file=CERT_PATH . $username . "-" . $id . ".ovpn";
if ($error === "")
{
if (!file_exists($cert_file))
{
$error = "no config file with this id=$id";
}
}
if (!empty($_POST['name']))
{
$name = isset($_POST['name'])? $_POST['name'] : '';
$name = (string)$name;
$error = "";
if (strlen($name) > 20)
{
$error = "Maximum name length is 20 characters.";
}
if (!preg_match('/^[a-z0-9]+$/i', $name))
{
$errror = "Only numbers and letters allowed.";
}
if ($error !== "")
{
?>
<div class="login-page">
<div class="form">
<form action="rename.php?id=<?php echo $id; ?>" class="login-form" method="post">
<a style="color:red"><?php echo $error; ?></br></a>
<button type="submit">okay</button>
</form>
</div>
</div>
<?php
fok();
}
$db = new PDO(DATABASE_PATH);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare('UPDATE Accounts SET Config' . $id .' = ? WHERE Username = ?;');
$stmt->execute(array($name, $_SESSION['Username']));
$aConfig = $_SESSION['Config']; // load old cfg
$aConfig[$id] = $name; // update
$_SESSION['Config'] = $aConfig; // save
?>
<div class="login-page">
<div class="form">
<form action="index.php" class="login-form">
<a>Updated config name to ' <?php echo $name; ?> '</br></a>
<button type="submit">okay</button>
</form>
</div>
</div>
<?php
fok();
}
if ($error === "")
{
?>
<div class="login-page">
<div class="form">
<form method="post" action="rename.php?id=<?php echo $id; ?>" class="login-form">
<input id="name" name="name" type="text" placeholder="new config name"/>
<button type="submit">update</button>
<p class="message">Old name was ok? <a href="index.php">back</a></p>
</form>
</div>
</div>
<?php
}
else
{
?>
<div class="login-page">
<div class="form">
<form action="index.php" class="login-form">
<a>Error: <?php echo $error; ?></br></a>
<button type="submit">back</button>
</form>
</div>
</div>
<?php
}
fok();
}
?>