]> git.mxchange.org Git - friendica.git/blob - library/spam/install/setup_berkeleydb.php
Merge pull request #321 from beardy-unixer/master
[friendica.git] / library / spam / install / setup_berkeleydb.php
1 <?php
2
3 #   Copyright (C) 2010 Tobias Leupold <tobias.leupold@web.de>
4 #
5 #   This file is part of the b8 package
6 #
7 #   This program is free software; you can redistribute it and/or modify it
8 #   under the terms of the GNU Lesser General Public License as published by
9 #   the Free Software Foundation in version 2.1 of the License.
10 #
11 #   This program is distributed in the hope that it will be useful, but
12 #   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 #   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 #   License for more details.
15 #
16 #   You should have received a copy of the GNU Lesser General Public License
17 #   along with this program; if not, write to the Free Software Foundation,
18 #   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19
20 echo <<<END
21 <?xml version="1.0" encoding="UTF-8"?>
22
23 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
24    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
25
26 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
27
28 <head>
29
30 <title>b8 Berkeley DB setup</title>
31
32 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
33
34 <meta name="dc.creator" content="Tobias Leupold" />
35 <meta name="dc.rights" content="Copyright (c) by Tobias Leupold" />
36
37 </head>
38
39 <body>
40
41 <div>
42
43 <h1>b8 Berkeley DB setup</h1>
44
45
46 END;
47
48 $failed = FALSE;
49
50 if(isset($_POST['handler'])) {
51
52         $dbfile = $_POST['dbfile'];
53         $dbfile_directory = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF']);
54
55         echo "<h2>Creating database</h2>\n\n";
56
57         echo "<p>\n";
58
59         echo "Checking database file name &hellip; ";
60
61         if($dbfile == "") {
62                 echo "<span style=\"color:red;\">Please provide the name of the database file!</span><br />\n";
63                 $failed = TRUE;
64         }
65         else
66                 echo "$dbfile<br />\n";
67
68         if(!$failed) {
69
70                 echo "Touching/Creating " . htmlentities($dbfile) . " &hellip; ";
71
72                 if(touch($dbfile) === FALSE) {
73                         echo "<span style=\"color:red;\">Failed to touch the database file. Please check the given filename and/or fix the permissions of $dbfile_directory.</span><br />\n";
74                         $failed = TRUE;
75                 }
76                 else
77                         echo "done<br />\n";
78
79         }
80
81         if(!$failed) {
82
83                 echo "Setting file permissions to 0666 &hellip ";
84
85                 if(chmod($dbfile, 0666) === FALSE) {
86                         echo "<span style=\"color:red;\">Failed to change the permissions of $dbfile_directory/$dbfile. Please adjust them manually.</span><br />\n";
87                         $failed = TRUE;
88                 }
89                 else
90                         echo "done<br />\n";
91
92         }
93
94         if(!$failed) {
95
96                 echo "Checking if the given file is empty &hellip ";
97
98                 if(filesize($dbfile) > 0) {
99                         echo "<span style=\"color:red;\">$dbfile_directory/$dbfile is not empty. Can't create a new database. Please delete/empty this file or give another filename.</span><br />\n";
100                         $failed = TRUE;
101                 }
102                 else
103                         echo "it is<br />\n";
104
105         }
106
107         if(!$failed) {
108
109                 echo "Connecting to $dbfile &hellip; ";
110
111                 $db = dba_open($dbfile, "c", $_POST['handler']);
112
113                 if($db === FALSE) {
114                         echo "<span style=\"color:red;\">Could not connect to the database!</span><br />\n";
115                         $failed = TRUE;
116                 }
117                 else
118                         echo "done<br />\n";
119
120         }
121
122         if(!$failed) {
123
124                 echo "Storing necessary internal variables &hellip ";
125
126                 $internals = array(
127                         "bayes*dbversion" => "2",
128                         "bayes*texts.ham" => "0",
129                         "bayes*texts.spam" => "0"
130                 );
131
132                 foreach($internals as $key => $value) {
133                         if(dba_insert($key, $value, $db) === FALSE) {
134                                 echo "<span style=\"color:red;\">Failed to insert data!</span><br />\n";
135                                 $failed = TRUE;
136                                 break;
137                         }
138                 }
139
140                 if(!$failed)
141                         echo "done<br />\n";
142
143         }
144
145         if(!$failed) {
146
147                 echo "Trying to read data from the database &hellip ";
148
149                 $dbversion = dba_fetch("bayes*dbversion", $db);
150
151                 if($dbversion != "2") {
152                         echo "<span style=\"color:red;\">Failed to read data!</span><br />\n";
153                         $failed = TRUE;
154                 }
155                 else
156                         echo "success<br />\n";
157         }
158
159         if(!$failed) {
160
161                 dba_close($db);
162
163                 echo "</p>\n\n";
164                 echo "<p style=\"color:green;\">Successfully created a new b8 database!</p>\n\n";
165                 echo "<table>\n";
166                 echo "<tr><td>Filename:</td><td>$dbfile_directory/$dbfile</td></tr>\n";
167                 echo "<tr><td>DBA handler:</td><td>{$_POST['handler']}</td><tr>\n";
168                 echo "</table>\n\n";
169                 echo "<p>Move this file to it's destination directory (default: the base directory of b8) to use it with b8. Be sure to use the right DBA handler in b8's configuration.";
170
171         }
172
173         echo "</p>\n\n";
174
175 }
176
177 if($failed === TRUE or !isset($_POST['handler'])) {
178
179 echo <<<END
180 <form action="{$_SERVER['PHP_SELF']}" method="post">
181
182 <h2>DBA Handler</h2>
183
184 <p>
185 The following table shows all available DBA handlers. Please choose the "Berkeley DB" one.
186 </p>
187
188 <table>
189 <tr><td></td><td><b>Handler</b></td><td><b>Description</b></td></tr>
190
191 END;
192
193 foreach(dba_handlers(TRUE) as $name => $version) {
194
195         $checked = "";
196
197         if(!isset($_POST['handler'])) {
198                 if(strpos($version, "Berkeley") !== FALSE )
199                         $checked = " checked=\"checked\"";
200         }
201         else {
202                 if($_POST['handler'] == $name)
203                         $checked = " checked=\"checked\"";
204         }
205
206         echo "<tr><td><input type=\"radio\" name=\"handler\" value=\"$name\"$checked /></td><td>$name</td><td>$version</td></tr>\n";
207
208 }
209
210 echo <<<END
211 </table>
212
213 <h2>Database file</h2>
214
215 <p>
216 Please the name of the desired database file. It will be created in the directory where this script is located.
217 </p>
218
219 <p>
220 <input type="text" name="dbfile" value="wordlist.db" />
221 </p>
222
223 <p>
224 <input type="submit" value="Create the database" />
225 </p>
226
227 </form>
228
229
230 END;
231
232 }
233
234 ?>
235
236 </div>
237
238 </body>
239
240 </html>