-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.php
156 lines (139 loc) · 4.45 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
<?php
set_time_limit(0);
define('RAFFLES_ROOT', __DIR__.'/vendor/kwijibo/raffles/');
require 'vendor/kwijibo/raffles/lib/rafflesstore.php';
include 'vendor/autoload.php';
require 'helpers.php';
class SetupException extends Exception {}
function savePostToConfig(){
$config = array(
'name' => $_POST['name'],
'license' => $_POST['license'],
'prefixes' => array(),
'username' => $_POST['Username'],
);
$Password = trim($_POST['Password']);
if(!empty($Password)){
$config['password'] = md5($_POST['Password']);
} else {
$existingConfig = getConfig(0);
$config['password'] = $existingConfig->password;
}
if(isset($_POST['prefixes'])){
foreach($_POST['prefixes'] as $vocab){
$config['prefixes'][$vocab['prefix']] = $vocab['namespace'];
}
}
if(isset($_POST['add-vocab-field'])){
$config['prefixes']['prefix']='';
}
try {
file_put_contents(CONFIG_JSON_FILE, json_encode($config));
} catch(Exception $e){
throw new SetupException("The webserver does not have permission to write your config file in this directory. ");
}
}
function uploadData(){
if(!empty($_FILES['data-file']['name'])){
if($_FILES['data-file']['error']){
//handle error
switch($_FILES['data-file']['error']){
case 1:
case 2:
$errorMessage = 'The file uploaded is bigger than the upload_max_filesize directive in your php.ini file. Try making the value larger.';
break;
case 3:
$errorMessage='The uploaded file was only partially uploaded. ';
break;
case 4:
$errorMessage='No file was uploaded';
break;
case 5:
case 6:
$errorMessage='There is no temporary folder to write the file to.';
break;
case 7:
$errorMessage='Failed to write file to disk.';
break;
case 8:
$errorMessage='A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.';
break;
}
throw new SetupException($errorMessage);
} else {
$Store = new \Raffles\RafflesStore(RAFFLES_DATA_DIR);
$Store->reset();
$Store->indexPredicates = false;
$extension = false;
if(strpos($_FILES['data-file']['name'],'.')){
$file_name_parts = explode('.', $_FILES['data-file']['name']);
$extension = array_pop($file_name_parts);
}
$Store->loadDataFile($_FILES['data-file']['tmp_name'], $extension);
}
}
}
function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
$Config = getConfig();
/* authenticate*/
if(
!empty($Config->password) && !empty($Config->username)
&& (
(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
||
(
$Config->username !=$_SERVER['PHP_AUTH_USER'] ||
$Config->password !=md5($_SERVER['PHP_AUTH_PW'])
)
)
){
header('WWW-Authenticate: Basic realm="Trilby Setup"');
header('HTTP/1.0 401 Unauthorized');
echo 'You need to log in to access the setup page. If you have forgotton your password, delete "config.json" from your Trilby installation folder.';
exit;
}
if($_SERVER['REQUEST_METHOD']=='POST'){
if(empty($_POST)){
serveErrorPage('The file is too big for this server; try increasing the maximum post size and file upload settigns in php.ini, or at the command line, in your trilby directory, run: php bin/add-data.php {filename}');
}
try {
savePostToConfig();
uploadData();
} catch(SetupException $e){
serveErrorPage($e->getMessage());
}
redirect('_setup');
} else {
$maxfileSize = return_bytes(ini_get('upload_max_filesize'));
$Store = new \Raffles\RafflesStore(RAFFLES_DATA_DIR);
$ns = $Store->getNamespaces();
$Config = getConfig(false);
if(empty($Config->prefixes)){
$Config->prefixes = new StdClass();
}
foreach($ns as $nsUri => $prefix){
if(empty($Config->prefixes->$prefix)){
$Config->prefixes->$prefix =$nsUri;
}
}
$innerTemplate='setup.html';
$showMap = false;
$title = 'Setup';
$site_name = 'Trilby';
$licenses = getLicenses();
require 'templates/outer.html';
}
?>