forked from cydrobolt/polr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.php
357 lines (334 loc) · 20.3 KB
/
setup.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
<!DOCTYPE html>
<!--
# Copyright (C) 2013-2015 Chaoyi Zha
# Polr is an open-source project licensed under the GPL.
# The above copyright notice and the following license are applicable to
# the entire project, unless explicitly defined otherwise.
# http://github.com/cydrobolt/polr
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
# See http://www.gnu.org/copyleft/gpl.html for the full text of the
# license.
#
-->
<html>
<head>
<meta charset="UTF-8">
<title>Polr Setup</title>
<link rel="stylesheet" href="css/install-bootstrap.css"/>
</head>
<body style="padding-top:60px">
<div class="navbar navbar-default navbar-fixed-top">
<a class="navbar-brand" href="index.php">Polr</a>
</div>
<div class='container-fluid push pushtop' style="text-align: left">
<span><h1 style="text-align:center">Polr Setup</h1></span><br />
<?php
@(include('config.php'));
include ('version.php');
require_once 'lib-password.php';
date_default_timezone_set('UTC');
$mysqlnd = function_exists('mysqli_fetch_all');
if (!$mysqlnd) {
echo "<br /><br /><p>Error: You don't seem to have the <code>MySQL native driver. </code>
<br />You should install it and restart your server in order to use Polr properly. <br />
On Ubuntu-based distros: <code>sudo apt-get install php5-mysqlnd</code><br />
On Fedora-based distros: <code>sudo yum install php-mysqlnd</code>, or if you get errors, <code>sudo yum remove php-mysql && yum install php-mysqlnd</code><br />
For most Windows computers, the native driver should come by default for PHP >= 5.4.
<br />For more information, click <a href='http://php.net/manual/en/mysqlnd.install.php'>here</a></p>";
die();
}
function rstr($length = 34) {
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
if (isset($ppass)) {
if (!isset($_POST['pw'])) {
require_once 'layout-headerlg.php';
echo "<h2>Enter setup password to proceed:</h2>";
echo "<form action='setup.php' method='post'><br /><input class='form-control' type='password' name='pw' /><br /><input type='submit' class='form-control' value='Log in' /></form>";
require_once 'layout-footerlg.php';
die();
} else if ($pwf = password_verify($_POST['pw'], $ppass)) {
echo "";
} else {
require_once 'layout-headerlg.php';
echo "Wrong password<br />";
echo "<h2>Enter setup password to proceed:</h2>";
echo "<form action='setup.php' method='post'><br /><input type='password' class='form-control' name='pw' /><br /><input type='submit' class='form-control' value='Log in' /></form>";
require_once 'layout-footerlg.php';
die();
}
}
if (isset($_POST['dbserver'])) {
$rstr = rstr(50);
function hashpass($pass, $salt = "") {
if (!$salt) {
$salt = rstr(60);
}
$opts = array(
'cost' => 10,
'salt' => $salt
);
$hashed = password_hash($pass, PASSWORD_BCRYPT, $opts);
return $hashed;
}
$nowdate = date('F d Y');
$ppass = hashpass($_POST['protpass']);
$hp = sha1(rstr(30));
$data = <<<PHP
<?php
\$host = "{$_POST['dbserver']}";
\$user = "{$_POST['dbuser']}";
\$passwd = "{$_POST['dbpass']}";
\$db = "{$_POST['dbname']}";
\$wsa = "{$_POST['appurl']}";
\$wsn = "{$_POST['appname']}";
\$wsb = "{$nowdate}";
\$ppass = '{$ppass}';
\$hp = "{$hp}";
\$regtype = "{$_POST['reg']}";
\$path = "{$_POST['path']}";
\$fpass = {$_POST['fpass']};
\$li_shorten_only = {$_POST['li_shorten_only']};
\$theme = "{$_POST['t']}";
\$ip = {$_POST['ipfetch']};
\$li_show_front = {$_POST['li_show_front']};
\$li_index_redirect = '{$_POST['li_index_redirect']}';
\$unstr = "{$rstr}";
PHP;
if (strlen($_POST['smtp-servers'])>1) {
$smtpSection = <<<PHP
\n
\$smtpCfg = array(
"servers" => "{$_POST['smtp-servers']}",
"from" => "{$_POST['smtp-from']}",
"username" => "{$_POST['smtp-username']}",
"password" => "{$_POST['smtp-password']}",
);
PHP;
$data .= $smtpSection;
}
$handle = fopen("config.php", 'w');
if (fwrite($handle, $data) === FALSE) {
die("<p class='alert alert-danger'>Could not write to <strong>config.php</strong>. Please make sure your file permissions are correct.
<br />Your webserver's user should have write permissions for the folder. Try looking up <code>chown</code>, <code>chgrp</code> and <code>chmod</code>. <a href='http://linuxcommand.org/lts0070.php'>Helpful link</a></p>. <strong>Aborting Setup</strong>");
} else {
echo "<p class='alert alert-success'>Successfully created <strong>config.php</strong> .</p>";
}
fclose($handle); //config.php
require_once('lib-core.php');
$path = $_POST['path'];
// check if path ends with / , validating it for .htaccess
if ( empty( $path ) ) {
echo "<p class='alert alert-success'>Path is empty, no <strong>.htaccess</strong> needs to be written.</p>";
} elseif ( !( ( strlen( $path) > 2) &&
( substr( $path, -1 ) == "/") ) ) {
echo "<p class='alert alert-danger'>Path needs trailing slash (<code>/</code>) and cannot be a slash only.</p>";
} else {
$data = "<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase $path
RewriteRule ^api$ api.php [L]
RewriteRule ^api/$ api.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)\?([a-zA-Z0-9]+)$ r.php?u=$1&lkey=$2 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/?$ r.php?u=$1 [L,QSA]
RewriteRule ^t-([a-zA-Z0-9]+)/?$ r.php?u=t-$1 [L,QSA]
RewriteRule ^/?\+([a-zA-Z0-9]+)$ stats.php?bv=$1 [L,QSA]
</IfModule>";
$handle = fopen('.htaccess', 'w');
if (fwrite($handle, $data) === FALSE) {
echo "<p class='alert alert-danger'>Could not write to <strong>.htaccess</strong> .</p>";
} else {
echo "<p class='alert alert-success'>Successfully created <strong>.htaccess</strong> .</p>";
}
fclose($handle); //.htaccess
// .nginx-config
$data = "# Polr experimental nginx configuration. Append this to your nginx config for effect.
# If you use Apache, ignore this file.
# Try `/etc/nginx/config.d/` if you have trouble finding the configuration
server {
listen 80;
server_name %SERVER_NAME%;
index index.php
location = $path/api {
rewrite ^(.*)$ $path/api.php;
}
location $path/ {
if (!-e \$request_filename){
rewrite ^$path/([a-zA-Z0-9]+)\?([a-zA-Z0-9]+)$ $path/r.php?u=$1&lkey=$2;
}
rewrite ^$path/([a-zA-Z0-9]+)/?$ $path/r.php?u=$1;
rewrite ^$path/?\+([a-zA-Z0-9]+)$ $path/stats.php?bv=$1;
}
location $path/t {
rewrite ^$path/t-([a-zA-Z0-9]+)/?$ $path/r.php?u=t-$1;
}
}";
$handle = fopen('.nginx-config', 'w');
if (fwrite($handle, $data) === FALSE) {
echo "<p class='alert alert-warning'>Could not write to <strong>.nginx-config</strong></p>";
} else {
echo "<p class='alert alert-success'>Successfully created <strong>.nginx-config</strong> .</p>";
}
fclose($handle); //.nginx-config
}
// Create Polr tables
// TODO: lib-core.php:95
// return actual result of query and implement Error message here instead of core~
sqlrun('
CREATE TABLE `api` (
`valid` tinyint(1) NOT NULL,
`email` varchar(50) NOT NULL,
`apikey` varchar(70) NOT NULL,
`quota` int(11) NOT NULL,
PRIMARY KEY (`apikey`),
UNIQUE KEY `email` (`email`),
KEY `valid` (`valid`)
);');
sqlrun('
CREATE TABLE `auth` (
`username` varchar(50) NOT NULL,
`password` text NOT NULL,
`email` varchar(65) NOT NULL,
`rkey` varchar(65) NOT NULL,
`role` varchar(37) NOT NULL,
`valid` tinyint(1) NOT NULL DEFAULT "0",
`uid` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`theme` varchar(100) NOT NULL,
`ip` tinytext NOT NULL,
KEY `valid` (`valid`),
KEY `email` (`email`),
KEY `username` (`username`)
);');
sqlrun('
CREATE TABLE `redirinfo` (
`rurl` text NOT NULL,
`rid` int(11) NOT NULL AUTO_INCREMENT,
`baseval` varchar(30) NOT NULL,
`ip` varchar(90) NOT NULL,
`iscustom` varchar(4) NOT NULL,
`user` tinytext NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`country` varchar(10) NOT NULL,
`lkey` tinytext NOT NULL,
`clicks` int(11) NOT NULL,
`pw` int(120) NOT NULL,
`etc` text,
`etc2` text,
PRIMARY KEY (`rid`),
KEY `baseval` (`baseval`),
KEY `ip` (`ip`),
KEY `iscustom` (`iscustom`)
);');
sqlrun('
CREATE TABLE `redirinfo-temp` (
`rurl` text NOT NULL,
`rid` int(11) NOT NULL AUTO_INCREMENT,
`baseval` varchar(30) NOT NULL,
`ip` varchar(90) NOT NULL,
`iscustom` varchar(4) NOT NULL,
`user` tinytext NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`country` tinytext NOT NULL,
`clicks` int(11) NOT NULL,
`pw` int(120) NOT NULL,
`etc` text,
`etc2` text,
PRIMARY KEY (`rid`),
KEY `baseval` (`baseval`),
KEY `ip` (`ip`),
KEY `iscustom` (`iscustom`)
);');
$acctpass = hashpass($_POST['acctpass']);
$nr = sha1(rstr(50));
sqlrun("INSERT INTO auth (username,email,password,rkey,valid,role,theme,ip) VALUES ('{$_POST['acct']}','{$_POST['acctemail']}','{$acctpass}','{$nr}','1','adm','{$site_theme}','{$ip}') ");
echo "You are now finished Polr Setup. You can now close this window, and login to your account <a href='index.php'>here</a> (login form @ top right). <br /><br />If you need help, click <a href=\"http://webchat.freenode.net/?channels=#polr\">here</a><br />"
. "<br /><br /><b>Clueless? Read the docs. <a href='https://github.com/Cydrobolt/polr/blob/master/README.md'>https://github.com/Cydrobolt/polr/blob/master/README.md</a></b>";
} else {
include('version.php');
echo "<form name=\"Config Creation\" style='margin:0 auto; width: 650px' method=\"post\" action=\"" . 'setup.php' . "\">";
// DB Config
echo "<b style=\"text-align:center\">Database Configuration</b><br />";
echo "Database Host: <input type=\"text\" class='form-control' style='width:650px' name=\"dbserver\" value=\"localhost\"><br />";
echo "Database User: <input type=\"text\" class='form-control' style='width:650px' name=\"dbuser\" value=\"root\"><br />";
echo "Database Pass: <input type=\"password\" class='form-control' style='width:650px' name=\"dbpass\" value=\"password\"><br />";
echo "Database Name: <input type=\"text\" class='form-control' style='width:650px' name=\"dbname\" value=\"polr\"><br />";
// App Config
echo "<br /><b style=\"text-align:center\">Application Settings</b><br />";
echo "Application Name: <input type=\"text\" class='form-control' style='width:650px' name=\"appname\" value=\"Polr\"><br />";
echo "Application URL (path to Polr, no http://, www., or trailing slash) : <input type=\"text\" style='width:650px' class='form-control' name=\"appurl\" value=\"yoursite.com\"><br />";
echo "Fetch ip through variable: <input type=\"text\" class='form-control' style='width:650px' name=\"ipfetch\" value=\"\$_SERVER['REMOTE_ADDR']\"><br />";
echo "Shortening Permissions: <select name='li_shorten_only' style='width:650px' class='form-control'>"
. "<option value='false' selected='selected'>All users can shorten URLs</option>"
. "<option value='true'>Only logged in users may shorten URLs</option>"
. "</select><br />";
echo "Show public interface: <select name='li_show_front' style='width:650px' class='form-control'>"
. "<option value='false' selected='selected'>Show public interface (default)</option>"
. "<option value='true'>Hide public interface (for private shorteners)</option>"
. "</select>";
echo "If public interface is hidden, redirect index page to: <input type=\"text\" class='form-control' style='width:650px' name=\"li_index_redirect\" placeholder=\"http://your-main-site.com\">"
. "<i>Notice: if this setting is enabled, you will need to go to http://PATHTOPOLR/login.php before you can access the index page. Leave the field blank to show a generic index page.</i>"
. "<br /><br />";
// Security/Account Config
echo "<br /><b style=\"text-align:center\">Admin Account Settings</b><br />";
echo "Setup Access Password: <input type=\"text\" class='form-control' style='width:650px' name=\"protpass\" value=\"password123\"><br />";
echo "Admin Account: <input type=\"text\" class='form-control' style='width:650px' style='width:650px' name=\"acct\" value=\"polr\"><br />";
echo "Admin Email: <input type=\"text\" class='form-control' style='width:650px' style='width:650px' name=\"acctemail\" value=\"[email protected]\"><br />";
echo "Admin Password: <input type=\"password\" style='width:650px' class='form-control' name=\"acctpass\" value=\"polr\"><br />";
// SMTP Config
echo "<br /><b style=\"text-align:center\">SMTP Settings</b><p>(leave blank if you are not using email verification/password recovery)</p>";
echo "SMTP Servers (semicolon separated): <input type=\"text\" class='form-control' style='width:650px' name=\"smtp-servers\" placeholder=\"smtp.gmail.com\"><br />";
echo "SMTP Username: <input type=\"text\" class='form-control' style='width:650px' name=\"smtp-username\" placeholder=\"[email protected]\"><br />";
echo "SMTP Password: <input type=\"password\" class='form-control' style='width:650px' name=\"smtp-password\" placeholder=\"password\"><br />";
echo "SMTP From: <input type=\"text\" class='form-control' style='width:650px' name=\"smtp-from\" placeholder=\"[email protected]\"><br />";
echo "<br /><b style=\"text-align:center\">Other Settings</b><br />Registration: <select name='reg' style='width:650px' class='form-control'>"
. "<option value='none'>No registration</option>"
. "<option value='email'>Email verification required</option>"
. "<option value='free'>No email verification required</option>"
. "<option value='regulated'>New registrations must be approved (do not choose, future option)</option>"
. "</select><br /><br />";
echo "Password Recovery: <select name='fpass' style='width:650px' class='form-control'>"
. "<option value='false'>No (default)</option>"
. "<option value='true'>Yes (could cause problems unless helper-mailsend.php/email is properly set up)</option>"
. "</select><br /><br />";
echo "Path relative to root (leave blank if /, if http://site.com/polr, then write /polr/): <input type=\"text\" class='form-control' style='width:650px' name=\"path\" placeholder=\"/polr/\" value=\"\"><br />";
echo "Theme (choose wisely, click <a href='https://github.com/Cydrobolt/polr/wiki/Themes-Screenshots'>here</a> for screenshots: <select name='t' style='width:650px' class='form-control'>"
. "<option value=''>Modern (default)</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/cyborg/bootstrap.min.css'>Midnight Black</option>"
. "<option value='/css/install-bootstrap.css'>Cheery (deprecated)</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/united/bootstrap.min.css'>Orange</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/simplex/bootstrap.min.css'>Crisp White</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/darkly/bootstrap.min.css'>Cloudy Night</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/cerulean/bootstrap.min.css'>Calm Skies</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/paper/bootstrap.min.css'>Android Material Design</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/superhero/bootstrap.min.css'>Blue Metro</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/sandstone/bootstrap.min.css'>Sandstone</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/cyborg/bootstrap.min.css'>Jet Black</option>"
. "<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/lumen/bootstrap.min.css'>Newspaper</option>"
. "</select><br /><br />";
if (isset($_POST['pw'])) {
echo "<input type='hidden' value='{$_POST['pw']}' name='pw' />";
}
echo "<div id='buttons' style='text-align:center'>";
if (isset($ppass)) {
echo "<input type=\"submit\" class=\"btn btn-success\" style='width:150px' value=\"Update\">";
} else {
echo "<input type=\"submit\" class=\"btn btn-success\" style='width:150px' value=\"Install\">";
}
echo " ";
echo "<input type=\"reset\" value=\"Clear Fields\" class=\"btn btn-warning\" style='width:150px'>";
echo "</div>";
echo "</form>";
echo "<br /><br /></div><div class='container' style='text-align:center'>"
. "<p><b>Please read the README.md file located in the root Polr directory. It contains essential and indispensable troubleshooting, installation, and support materials. <b/></p>";
echo "<br /><br />Polr is <a href='http://en.wikipedia.org/wiki/Open-source_software'>Open-Source software</a> licensed under the <a href='//www.gnu.org/copyleft/gpl.html'>GPL License</a>. By continuing to use Polr, you agree to the terms of the GPL License.";
echo "<div class=''>Polr Version $version released $reldate - <a href='//github.com/cydrobolt/polr'>Github</a></div></div><br /><span style='padding-left:4%'>© Copyright $relyear Chaoyi Zha & <a href='https://github.com/Cydrobolt/polr/graphs/contributors'>Other Polr Contributors</a></span>";
}
?>
</div>
</body>
</html>