]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
Rebuilt HTTPClient class as an extension of PEAR HTTP_Request2 package, adding redire...
[quix0rs-gnu-social.git] / install.php
1
2 <?php
3 /**
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2009, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @category Installation
21  * @package  Installation
22  *
23  * @author   Adrian Lang <mail@adrianlang.de>
24  * @author   Brenda Wallace <shiny@cpan.org>
25  * @author   Brett Taylor <brett@webfroot.co.nz>
26  * @author   Brion Vibber <brion@pobox.com>
27  * @author   CiaranG <ciaran@ciarang.com>
28  * @author   Craig Andrews <candrews@integralblue.com>
29  * @author   Eric Helgeson <helfire@Erics-MBP.local>
30  * @author   Evan Prodromou <evan@status.net>
31  * @author   Robin Millette <millette@controlyourself.ca>
32  * @author   Sarven Capadisli <csarven@status.net>
33  * @author   Tom Adams <tom@holizz.com>
34  * @license  GNU Affero General Public License http://www.gnu.org/licenses/
35  * @version  0.9.x
36  * @link     http://status.net
37  */
38
39 define('INSTALLDIR', dirname(__FILE__));
40
41 $external_libraries=array(
42     array(
43         'name'=>'gettext',
44         'url'=>'http://us.php.net/manual/en/book.gettext.php',
45         'check_function'=>'gettext'
46     ),
47     array(
48         'name'=>'PEAR',
49         'url'=>'http://pear.php.net/',
50         'deb'=>'php-pear',
51         'include'=>'PEAR.php',
52         'check_class'=>'PEAR'
53     ),
54     array(
55         'name'=>'DB',
56         'pear'=>'DB',
57         'url'=>'http://pear.php.net/package/DB',
58         'deb'=>'php-db',
59         'include'=>'DB/common.php',
60         'check_class'=>'DB_common'
61     ),
62     array(
63         'name'=>'DB_DataObject',
64         'pear'=>'DB_DataObject',
65         'url'=>'http://pear.php.net/package/DB_DataObject',
66         'include'=>'DB/DataObject.php',
67         'check_class'=>'DB_DataObject'
68     ),
69     array(
70         'name'=>'Console_Getopt',
71         'pear'=>'Console_Getopt',
72         'url'=>'http://pear.php.net/package/Console_Getopt',
73         'include'=>'Console/Getopt.php',
74         'check_class'=>'Console_Getopt'
75     ),
76     array(
77         'name'=>'Facebook API',
78         'url'=>'http://developers.facebook.com/',
79         'include'=>'facebook/facebook.php',
80         'check_class'=>'Facebook'
81     ),
82     array(
83         'name'=>'htmLawed',
84         'url'=>'http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed',
85         'include'=>'htmLawed/htmLawed.php',
86         'check_function'=>'htmLawed'
87     ),
88     array(
89         'name'=>'HTTP_Request',
90         'pear'=>'HTTP_Request',
91         'url'=>'http://pear.php.net/package/HTTP_Request',
92         'deb'=>'php-http-request',
93         'include'=>'HTTP/Request.php',
94         'check_class'=>'HTTP_Request'
95     ),
96     array(
97         'name'=>'HTTP_Request2',
98         'pear'=>'HTTP_Request2',
99         'url'=>'http://pear.php.net/package/HTTP_Request2',
100         'include'=>'HTTP/Request2.php',
101         'check_class'=>'HTTP_Request2'
102     ),
103     array(
104         'name'=>'Mail',
105         'pear'=>'Mail',
106         'url'=>'http://pear.php.net/package/Mail',
107         'deb'=>'php-mail',
108         'include'=>'Mail.php',
109         'check_class'=>'Mail'
110     ),
111     array(
112         'name'=>'Mail_mimeDecode',
113         'pear'=>'Mail_mimeDecode',
114         'url'=>'http://pear.php.net/package/Mail_mimeDecode',
115         'deb'=>'php-mail-mimedecode',
116         'include'=>'Mail/mimeDecode.php',
117         'check_class'=>'Mail_mimeDecode'
118     ),
119     array(
120         'name'=>'Mime_Type',
121         'pear'=>'Mime_Type',
122         'url'=>'http://pear.php.net/package/Mime_Type',
123         'include'=>'MIME/Type.php',
124         'check_class'=>'Mime_Type'
125     ),
126     array(
127         'name'=>'Net_URL_Mapper',
128         'pear'=>'Net_URL_Mapper',
129         'url'=>'http://pear.php.net/package/Net_URL_Mapper',
130         'include'=>'Net/URL/Mapper.php',
131         'check_class'=>'Net_URL_Mapper'
132     ),
133     array(
134         'name'=>'Net_Socket',
135         'pear'=>'Net_Socket',
136         'url'=>'http://pear.php.net/package/Net_Socket',
137         'deb'=>'php-net-socket',
138         'include'=>'Net/Socket.php',
139         'check_class'=>'Net_Socket'
140     ),
141     array(
142         'name'=>'Net_SMTP',
143         'pear'=>'Net_SMTP',
144         'url'=>'http://pear.php.net/package/Net_SMTP',
145         'deb'=>'php-net-smtp',
146         'include'=>'Net/SMTP.php',
147         'check_class'=>'Net_SMTP'
148     ),
149     array(
150         'name'=>'Net_URL',
151         'pear'=>'Net_URL',
152         'url'=>'http://pear.php.net/package/Net_URL',
153         'deb'=>'php-net-url',
154         'include'=>'Net/URL.php',
155         'check_class'=>'Net_URL'
156     ),
157     array(
158         'name'=>'Net_URL2',
159         'pear'=>'Net_URL2',
160         'url'=>'http://pear.php.net/package/Net_URL2',
161         'include'=>'Net/URL2.php',
162         'check_class'=>'Net_URL2'
163     ),
164     array(
165         'name'=>'Services_oEmbed',
166         'pear'=>'Services_oEmbed',
167         'url'=>'http://pear.php.net/package/Services_oEmbed',
168         'include'=>'Services/oEmbed.php',
169         'check_class'=>'Services_oEmbed'
170     ),
171     array(
172         'name'=>'Stomp',
173         'url'=>'http://stomp.codehaus.org/PHP',
174         'include'=>'Stomp.php',
175         'check_class'=>'Stomp'
176     ),
177     array(
178         'name'=>'System_Command',
179         'pear'=>'System_Command',
180         'url'=>'http://pear.php.net/package/System_Command',
181         'include'=>'System/Command.php',
182         'check_class'=>'System_Command'
183     ),
184     array(
185         'name'=>'XMPPHP',
186         'url'=>'http://code.google.com/p/xmpphp',
187         'include'=>'XMPPHP/XMPP.php',
188         'check_class'=>'XMPPHP_XMPP'
189     ),
190     array(
191         'name'=>'PHP Markdown',
192         'url'=>'http://www.michelf.com/projects/php-markdown/',
193         'include'=>'markdown.php',
194         'check_class'=>'Markdown_Parser'
195     ),
196     array(
197         'name'=>'OAuth',
198         'url'=>'http://code.google.com/p/oauth-php',
199         'include'=>'OAuth.php',
200         'check_class'=>'OAuthRequest'
201     ),
202     array(
203         'name'=>'Validate',
204         'pear'=>'Validate',
205         'url'=>'http://pear.php.net/package/Validate',
206         'include'=>'Validate.php',
207         'check_class'=>'Validate'
208     )
209 );
210 $dbModules = array(
211     'mysql' => array(
212         'name' => 'MySQL',
213         'check_module' => 'mysql', // mysqli?
214         'installer' => 'mysql_db_installer',
215     ),
216     'pgsql' => array(
217         'name' => 'PostgreSQL',
218         'check_module' => 'pgsql',
219         'installer' => 'pgsql_db_installer',
220     ),
221 );
222
223 /**
224  * the actual installation.
225  * If call libraries are present, then install
226  *
227  * @return void
228  */
229 function main()
230 {
231     if (!checkPrereqs()) {
232         return;
233     }
234
235     if (!empty($_GET['checklibs'])) {
236         showLibs();
237     } else {
238         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
239             handlePost();
240         } else {
241             showForm();
242         }
243     }
244 }
245
246 /**
247  * checks if an external libary is present
248  *
249  * @param string $external_library Name of library
250  *
251  * @return boolean indicates if library present
252  */
253 function haveExternalLibrary($external_library)
254 {
255     if (isset($external_library['include']) && !haveIncludeFile($external_library['include'])) {
256         return false;
257     }
258     if (isset($external_library['check_function']) && ! function_exists($external_library['check_function'])) {
259         return false;
260     }
261     if (isset($external_library['check_class']) && ! class_exists($external_library['check_class'])) {
262         return false;
263     }
264     return true;
265 }
266
267 // Attempt to include a PHP file and report if it worked, while
268 // suppressing the annoying warning messages on failure.
269 function haveIncludeFile($filename) {
270     $old = error_reporting(error_reporting() & ~E_WARNING);
271     $ok = include_once($filename);
272     error_reporting($old);
273     return $ok;
274 }
275
276 /**
277  * Check if all is ready for installation
278  *
279  * @return void
280  */
281 function checkPrereqs()
282 {
283     $pass = true;
284
285     if (file_exists(INSTALLDIR.'/config.php')) {
286          printf('<p class="error">Config file &quot;config.php&quot; already exists.</p>');
287         $pass = false;
288     }
289
290     if (version_compare(PHP_VERSION, '5.2.3', '<')) {
291         printf('<p class="error">Require PHP version 5.2.3 or greater.</p>');
292         $pass = false;
293     }
294
295     $reqs = array('gd', 'curl',
296                   'xmlwriter', 'mbstring','tidy');
297
298     foreach ($reqs as $req) {
299         if (!checkExtension($req)) {
300             printf('<p class="error">Cannot load required extension: <code>%s</code></p>', $req);
301             $pass = false;
302         }
303     }
304     // Make sure we have at least one database module available
305     global $dbModules;
306     $missingExtensions = array();
307     foreach ($dbModules as $type => $info) {
308         if (!checkExtension($info['check_module'])) {
309             $missingExtensions[] = $info['check_module'];
310         }
311     }
312
313     if (count($missingExtensions) == count($dbModules)) {
314         $req = implode(', ', $missingExtensions);
315         printf('<p class="error">Cannot find mysql or pgsql extension. You need one or the other.');
316         $pass = false;
317     }
318
319     if (!is_writable(INSTALLDIR)) {
320         printf('<p class="error">Cannot write config file to: <code>%s</code></p>', INSTALLDIR);
321         printf('<p>On your server, try this command: <code>chmod a+w %s</code>', INSTALLDIR);
322         $pass = false;
323     }
324
325     // Check the subdirs used for file uploads
326     $fileSubdirs = array('avatar', 'background', 'file');
327     foreach ($fileSubdirs as $fileSubdir) {
328         $fileFullPath = INSTALLDIR."/$fileSubdir/";
329         if (!is_writable($fileFullPath)) {
330             printf('<p class="error">Cannot write to %s directory: <code>%s</code></p>', $fileSubdir, $fileFullPath);
331             printf('<p>On your server, try this command: <code>chmod a+w %s</code></p>', $fileFullPath);
332             $pass = false;
333         }
334     }
335
336     return $pass;
337 }
338
339 /**
340  * Checks if a php extension is both installed and loaded
341  *
342  * @param string $name of extension to check
343  *
344  * @return boolean whether extension is installed and loaded
345  */
346 function checkExtension($name)
347 {
348     if (extension_loaded($name)) {
349         return true;
350     } elseif (function_exists('dl') && ini_get('enable_dl') && !ini_get('safe_mode')) {
351         // dl will throw a fatal error if it's disabled or we're in safe mode.
352         // More fun, it may not even exist under some SAPIs in 5.3.0 or later...
353         $soname = $name . '.' . PHP_SHLIB_SUFFIX;
354         if (PHP_SHLIB_SUFFIX == 'dll') {
355             $soname = "php_" . $soname;
356         }
357         return @dl($soname);
358     } else {
359         return false;
360     }
361 }
362
363 /**
364  * Show list of libraries
365  *
366  * @return void
367  */
368 function showLibs()
369 {
370     global $external_libraries;
371     $present_libraries=array();
372     $absent_libraries=array();
373     foreach ($external_libraries as $external_library) {
374         if (haveExternalLibrary($external_library)) {
375             $present_libraries[]=$external_library;
376         } else {
377             $absent_libraries[]=$external_library;
378         }
379     }
380     echo<<<E_O_T
381     <div class="instructions">
382         <p>StatusNet comes bundled with a number of libraries required for the application to work. However, it is best that you use PEAR or you distribution to manage
383         libraries instead, as they tend to provide security updates faster, and may offer improved performance.</p>
384         <p>On Debian based distributions, such as Ubuntu, use a package manager (such as &quot;aptitude&quot;, &quot;apt-get&quot;, and &quot;synaptic&quot;) to install the package listed.</p>
385         <p>On RPM based distributions, such as Red Hat, Fedora, CentOS, Scientific Linux, Yellow Dog Linux and Oracle Enterprise Linux, use a package manager (such as &quot;yum&quot;, &quot;apt-rpm&quot;, and &quot;up2date&quot;) to install the package listed.</p>
386         <p>On servers without a package manager (such as Windows), or if the library is not packaged for your distribution, you can use PHP's PEAR to install the library. Simply run &quot;pear install &lt;name&gt;&quot;.</p>
387     </div>
388     <h2>Absent Libraries</h2>
389     <ul id="absent_libraries">
390 E_O_T;
391     foreach ($absent_libraries as $library) {
392         echo '<li>';
393         if (isset($library['url'])) {
394             echo '<a href="'.$library['url'].'">'.htmlentities($library['name']).'</a>';
395         } else {
396             echo htmlentities($library['name']);
397         }
398         echo '<ul>';
399         if (isset($library['deb'])) {
400             echo '<li class="deb package">deb: <a href="apt:' . urlencode($library['deb']) . '">' . htmlentities($library['deb']) . '</a></li>';
401         }
402         if (isset($library['rpm'])) {
403             echo '<li class="rpm package">rpm: ' . htmlentities($library['rpm']) . '</li>';
404         }
405         if (isset($library['pear'])) {
406             echo '<li class="pear package">pear: ' . htmlentities($library['pear']) . '</li>';
407         }
408         echo '</ul>';
409     }
410     echo<<<E_O_T
411     </ul>
412     <h2>Installed Libraries</h2>
413     <ul id="present_libraries">
414 E_O_T;
415     foreach ($present_libraries as $library) {
416         echo '<li>';
417         if (isset($library['url'])) {
418             echo '<a href="'.$library['url'].'">'.htmlentities($library['name']).'</a>';
419         } else {
420             echo htmlentities($library['name']);
421         }
422         echo '</li>';
423     }
424     echo<<<E_O_T
425     </ul>
426 E_O_T;
427 }
428
429 function showForm()
430 {
431     global $dbModules;
432     $dbRadios = '';
433     $checked = 'checked="checked" '; // Check the first one which exists
434     foreach ($dbModules as $type => $info) {
435         if (checkExtension($info['check_module'])) {
436             $dbRadios .= "<input type=\"radio\" name=\"dbtype\" id=\"dbtype-$type\" value=\"$type\" $checked/> $info[name]<br />\n";
437             $checked = '';
438         }
439     }
440     echo<<<E_O_T
441         </ul>
442     </dd>
443 </dl>
444 <dl id="page_notice" class="system_notice">
445     <dt>Page notice</dt>
446     <dd>
447         <div class="instructions">
448             <p>Enter your database connection information below to initialize the database.</p>
449             <p>StatusNet bundles a number of libraries for ease of installation. <a href="?checklibs=true">You can see what bundled libraries you are using, versus what libraries are installed on your server.</a>
450         </div>
451     </dd>
452 </dl>
453 <form method="post" action="install.php" class="form_settings" id="form_install">
454     <fieldset>
455         <legend>Connection settings</legend>
456         <ul class="form_data">
457             <li>
458                 <label for="sitename">Site name</label>
459                 <input type="text" id="sitename" name="sitename" />
460                 <p class="form_guide">The name of your site</p>
461             </li>
462             <li>
463                 <label for="fancy-enable">Fancy URLs</label>
464                 <input type="radio" name="fancy" id="fancy-enable" value="enable" checked='checked' /> enable<br />
465                 <input type="radio" name="fancy" id="fancy-disable" value="" /> disable<br />
466                 <p class="form_guide" id='fancy-form_guide'>Enable fancy (pretty) URLs. Auto-detection failed, it depends on Javascript.</p>
467             </li>
468             <li>
469                 <label for="host">Hostname</label>
470                 <input type="text" id="host" name="host" />
471                 <p class="form_guide">Database hostname</p>
472             </li>
473             <li>
474
475                 <label for="dbtype">Type</label>
476                 $dbRadios
477                 <p class="form_guide">Database type</p>
478             </li>
479
480             <li>
481                 <label for="database">Name</label>
482                 <input type="text" id="database" name="database" />
483                 <p class="form_guide">Database name</p>
484             </li>
485             <li>
486                 <label for="username">Username</label>
487                 <input type="text" id="username" name="username" />
488                 <p class="form_guide">Database username</p>
489             </li>
490             <li>
491                 <label for="password">Password</label>
492                 <input type="password" id="password" name="password" />
493                 <p class="form_guide">Database password (optional)</p>
494             </li>
495         </ul>
496         <input type="submit" name="submit" class="submit" value="Submit" />
497     </fieldset>
498 </form>
499
500 E_O_T;
501 }
502
503 function updateStatus($status, $error=false)
504 {
505     echo '<li' . ($error ? ' class="error"': '' ) . ">$status</li>";
506 }
507
508 function handlePost()
509 {
510     $host     = $_POST['host'];
511     $dbtype   = $_POST['dbtype'];
512     $database = $_POST['database'];
513     $username = $_POST['username'];
514     $password = $_POST['password'];
515     $sitename = $_POST['sitename'];
516     $fancy    = !empty($_POST['fancy']);
517     $server = $_SERVER['HTTP_HOST'];
518     $path = substr(dirname($_SERVER['PHP_SELF']), 1);
519
520     echo <<<STR
521     <dl class="system_notice">
522         <dt>Page notice</dt>
523         <dd>
524             <ul>
525 STR;
526     $fail = false;
527
528     if (empty($host)) {
529         updateStatus("No hostname specified.", true);
530         $fail = true;
531     }
532
533     if (empty($database)) {
534         updateStatus("No database specified.", true);
535         $fail = true;
536     }
537
538     if (empty($username)) {
539         updateStatus("No username specified.", true);
540         $fail = true;
541     }
542
543     if (empty($sitename)) {
544         updateStatus("No sitename specified.", true);
545         $fail = true;
546     }
547
548     if ($fail) {
549         showForm();
550         return;
551     }
552
553     global $dbModules;
554     $db = call_user_func($dbModules[$dbtype]['installer'], $host, $database, $username, $password);
555
556     if (!$db) {
557         // database connection failed, do not move on to create config file.
558         return false;
559     }
560
561     updateStatus("Writing config file...");
562     $res = writeConf($sitename, $server, $path, $fancy, $db);
563
564     if (!$res) {
565         updateStatus("Can't write config file.", true);
566         showForm();
567         return;
568     }
569
570     /*
571         TODO https needs to be considered
572     */
573     $link = "http://".$server.'/'.$path;
574
575     updateStatus("StatusNet has been installed at $link");
576     updateStatus("You can visit your <a href='$link'>new StatusNet site</a>.");
577 }
578
579 function Pgsql_Db_installer($host, $database, $username, $password)
580 {
581     $connstring = "dbname=$database host=$host user=$username";
582
583     //No password would mean trust authentication used.
584     if (!empty($password)) {
585         $connstring .= " password=$password";
586     }
587     updateStatus("Starting installation...");
588     updateStatus("Checking database...");
589     $conn = pg_connect($connstring);
590
591     if ($conn ===false) {
592         updateStatus("Failed to connect to database: $connstring");
593         showForm();
594         return false;
595     }
596
597     //ensure database encoding is UTF8
598     $record = pg_fetch_object(pg_query($conn, 'SHOW server_encoding'));
599     if ($record->server_encoding != 'UTF8') {
600         updateStatus("StatusNet requires UTF8 character encoding. Your database is ". htmlentities($record->server_encoding));
601         showForm();
602         return false;
603     }
604
605     updateStatus("Running database script...");
606     //wrap in transaction;
607     pg_query($conn, 'BEGIN');
608     $res = runDbScript(INSTALLDIR.'/db/statusnet_pg.sql', $conn, 'pgsql');
609
610     if ($res === false) {
611         updateStatus("Can't run database script.", true);
612         showForm();
613         return false;
614     }
615     foreach (array('sms_carrier' => 'SMS carrier',
616                 'notice_source' => 'notice source',
617                 'foreign_services' => 'foreign service')
618           as $scr => $name) {
619         updateStatus(sprintf("Adding %s data to database...", $name));
620         $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn, 'pgsql');
621         if ($res === false) {
622             updateStatus(sprintf("Can't run %d script.", $name), true);
623             showForm();
624             return false;
625         }
626     }
627     pg_query($conn, 'COMMIT');
628
629     if (empty($password)) {
630         $sqlUrl = "pgsql://$username@$host/$database";
631     } else {
632         $sqlUrl = "pgsql://$username:$password@$host/$database";
633     }
634
635     $db = array('type' => 'pgsql', 'database' => $sqlUrl);
636
637     return $db;
638 }
639
640 function Mysql_Db_installer($host, $database, $username, $password)
641 {
642     updateStatus("Starting installation...");
643     updateStatus("Checking database...");
644
645     $conn = mysql_connect($host, $username, $password);
646     if (!$conn) {
647         updateStatus("Can't connect to server '$host' as '$username'.", true);
648         showForm();
649         return false;
650     }
651     updateStatus("Changing to database...");
652     $res = mysql_select_db($database, $conn);
653     if (!$res) {
654         updateStatus("Can't change to database.", true);
655         showForm();
656         return false;
657     }
658     updateStatus("Running database script...");
659     $res = runDbScript(INSTALLDIR.'/db/statusnet.sql', $conn);
660     if ($res === false) {
661         updateStatus("Can't run database script.", true);
662         showForm();
663         return false;
664     }
665     foreach (array('sms_carrier' => 'SMS carrier',
666                 'notice_source' => 'notice source',
667                 'foreign_services' => 'foreign service')
668           as $scr => $name) {
669         updateStatus(sprintf("Adding %s data to database...", $name));
670         $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
671         if ($res === false) {
672             updateStatus(sprintf("Can't run %d script.", $name), true);
673             showForm();
674             return false;
675         }
676     }
677
678     $sqlUrl = "mysqli://$username:$password@$host/$database";
679     $db = array('type' => 'mysql', 'database' => $sqlUrl);
680     return $db;
681 }
682
683 function writeConf($sitename, $server, $path, $fancy, $db)
684 {
685     // assemble configuration file in a string
686     $cfg =  "<?php\n".
687             "if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }\n\n".
688
689             // site name
690             "\$config['site']['name'] = '$sitename';\n\n".
691
692             // site location
693             "\$config['site']['server'] = '$server';\n".
694             "\$config['site']['path'] = '$path'; \n\n".
695
696             // checks if fancy URLs are enabled
697             ($fancy ? "\$config['site']['fancy'] = true;\n\n":'').
698
699             // database
700             "\$config['db']['database'] = '{$db['database']}';\n\n".
701             ($db['type'] == 'pgsql' ? "\$config['db']['quote_identifiers'] = true;\n\n":'').
702             "\$config['db']['type'] = '{$db['type']}';\n\n";
703     // write configuration file out to install directory
704     $res = file_put_contents(INSTALLDIR.'/config.php', $cfg);
705
706     return $res;
707 }
708
709 /**
710  * Install schema into the database
711  *
712  * @param string $filename location of database schema file
713  * @param dbconn $conn     connection to database
714  * @param string $type     type of database, currently mysql or pgsql
715  *
716  * @return boolean - indicating success or failure
717  */
718 function runDbScript($filename, $conn, $type = 'mysqli')
719 {
720     $sql = trim(file_get_contents($filename));
721     $stmts = explode(';', $sql);
722     foreach ($stmts as $stmt) {
723         $stmt = trim($stmt);
724         if (!mb_strlen($stmt)) {
725             continue;
726         }
727         // FIXME: use PEAR::DB or PDO instead of our own switch
728         switch ($type) {
729         case 'mysqli':
730             $res = mysql_query($stmt, $conn);
731             if ($res === false) {
732                 $error = mysql_error();
733             }
734             break;
735         case 'pgsql':
736             $res = pg_query($conn, $stmt);
737             if ($res === false) {
738                 $error = pg_last_error();
739             }
740             break;
741         default:
742             updateStatus("runDbScript() error: unknown database type ". $type ." provided.");
743         }
744         if ($res === false) {
745             updateStatus("ERROR ($error) for SQL '$stmt'");
746             return $res;
747         }
748     }
749     return true;
750 }
751
752 ?>
753 <?php echo"<?"; ?> xml version="1.0" encoding="UTF-8" <?php echo "?>"; ?>
754 <!DOCTYPE html
755 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
756        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
757 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
758     <head>
759         <title>Install StatusNet</title>
760         <link rel="shortcut icon" href="favicon.ico"/>
761         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.8" media="screen, projection, tv"/>
762         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/base/css/ie.css?version=0.8" /><![endif]-->
763         <!--[if lte IE 6]><link rel="stylesheet" type="text/css" theme/base/css/ie6.css?version=0.8" /><![endif]-->
764         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/default/css/ie.css?version=0.8" /><![endif]-->
765         <script src="js/jquery.min.js"></script>
766         <script src="js/install.js"></script>
767     </head>
768     <body id="install">
769         <div id="wrap">
770             <div id="header">
771                 <address id="site_contact" class="vcard">
772                     <a class="url home bookmark" href=".">
773                         <img class="logo photo" src="theme/default/logo.png" alt="StatusNet"/>
774                         <span class="fn org">StatusNet</span>
775                     </a>
776                 </address>
777             </div>
778             <div id="core">
779                 <div id="content">
780                     <h1>Install StatusNet</h1>
781 <?php main(); ?>
782                 </div>
783             </div>
784         </div>
785     </body>
786 </html>