2010-08-16 00:49:29 -04:00
< ? php
function install_post ( & $a ) {
global $db ;
2010-12-02 20:31:48 -05:00
$urlpath = $a -> get_path ();
2010-08-16 00:49:29 -04:00
$dbhost = notags ( trim ( $_POST [ 'dbhost' ]));
$dbuser = notags ( trim ( $_POST [ 'dbuser' ]));
$dbpass = notags ( trim ( $_POST [ 'dbpass' ]));
$dbdata = notags ( trim ( $_POST [ 'dbdata' ]));
$timezone = notags ( trim ( $_POST [ 'timezone' ]));
$phpath = notags ( trim ( $_POST [ 'phpath' ]));
require_once ( " dba.php " );
2010-12-22 16:55:44 -05:00
$db = new dba ( $dbhost , $dbuser , $dbpass , $dbdata , true );
2010-08-16 00:49:29 -04:00
2010-08-17 21:44:13 -04:00
if ( mysqli_connect_errno ()) {
2010-11-18 05:59:59 -05:00
$db = new dba ( $dbhost , $dbuser , $dbpass , '' , true );
if ( ! mysql_connect_errno ()) {
$r = q ( " CREATE DATABASE '%s' " ,
dbesc ( $dbdata )
);
if ( $r )
2010-12-22 16:55:44 -05:00
$db = new dba ( $dbhost , $dbuser , $dbpass , $dbdata , true );
2010-11-18 05:59:59 -05:00
}
if ( mysqli_connect_errno ()) {
notice ( t ( 'Could not create/connect to database.' ) . EOL );
return ;
}
2010-08-16 00:49:29 -04:00
}
2010-11-18 05:59:59 -05:00
notice ( t ( 'Connected to database.' ) . EOL );
2010-08-16 00:49:29 -04:00
2010-09-22 21:00:19 -04:00
$tpl = load_view_file ( 'view/htconfig.tpl' );
2010-08-16 00:49:29 -04:00
$txt = replace_macros ( $tpl , array (
'$dbhost' => $dbhost ,
'$dbuser' => $dbuser ,
'$dbpass' => $dbpass ,
'$dbdata' => $dbdata ,
'$timezone' => $timezone ,
2010-12-02 20:31:48 -05:00
'$urlpath' => $urlpath ,
2010-08-16 00:49:29 -04:00
'$phpath' => $phpath
));
$result = file_put_contents ( '.htconfig.php' , $txt );
if ( ! $result ) {
$a -> data = $txt ;
}
$errors = load_database ( $db );
if ( ! $errors ) {
// Our sessions normally are stored in the database. But as we have only managed
// to get it bootstrapped milliseconds ago, we have to apply a bit of trickery so
// that you'll see the following important notice (which is stored in the session).
session_write_close ();
2010-11-06 08:07:10 -04:00
2010-08-16 00:49:29 -04:00
require_once ( 'session.php' );
session_start ();
2010-11-06 08:07:10 -04:00
session_regenerate_id ();
2010-08-16 00:49:29 -04:00
$_SESSION [ 'sysmsg' ] = '' ;
notice ( t ( 'Database import succeeded.' ) . EOL
2010-09-29 21:11:23 -04:00
. t ( 'IMPORTANT: You will need to [manually] setup a scheduled task for the poller.' ) . EOL
2010-12-22 16:55:44 -05:00
. t ( 'Please see the file "INSTALL.txt".' ) . EOL );
2010-11-15 21:16:57 -05:00
goaway ( $a -> get_baseurl () . '/register' );
2010-08-16 00:49:29 -04:00
}
else {
$db = null ; // start fresh
notice ( t ( 'Database import failed.' ) . EOL
. t ( 'You may need to import the file "database.sql" manually using phpmyadmin or mysql.' ) . EOL
2010-12-22 16:55:44 -05:00
. t ( 'Please see the file "INSTALL.txt".' ) . EOL );
2010-08-16 00:49:29 -04:00
}
}
function install_content ( & $a ) {
2010-11-18 05:59:59 -05:00
$o = '' ;
2010-11-02 23:17:46 -04:00
notice ( t ( 'Welcome to Friendika.' ) . EOL );
2010-08-16 00:49:29 -04:00
2010-11-18 05:59:59 -05:00
check_funcs ();
2010-08-16 00:49:29 -04:00
$o .= check_htconfig ();
if ( strlen ( $o ))
return $o ;
if ( strlen ( $a -> data )) {
$o .= manual_config ( $a );
return ;
}
$o .= check_php ( $phpath );
2010-11-25 18:33:32 -05:00
$o .= check_keys ();
2010-08-16 00:49:29 -04:00
require_once ( 'datetime.php' );
2010-09-22 21:00:19 -04:00
$tpl = load_view_file ( 'view/install_db.tpl' );
2010-08-16 00:49:29 -04:00
$o .= replace_macros ( $tpl , array (
2010-12-02 20:31:48 -05:00
'$baseurl' => $a -> get_baseurl (),
2010-08-16 00:49:29 -04:00
'$tzselect' => (( x ( $_POST , 'timezone' )) ? select_timezone ( $_POST [ 'timezone' ]) : select_timezone ()),
'$submit' => t ( 'Submit' ),
'$dbhost' => (( x ( $_POST , 'dbhost' )) ? notags ( trim ( $_POST [ 'dbhost' ])) : 'localhost' ),
'$dbuser' => notags ( trim ( $_POST [ 'dbuser' ])),
'$dbpass' => notags ( trim ( $_POST [ 'dbpass' ])),
'$dbdata' => notags ( trim ( $_POST [ 'dbdata' ])),
'$phpath' => $phpath
));
return $o ;
}
function check_php ( & $phpath ) {
2010-11-06 08:07:10 -04:00
$o = '' ;
2010-08-16 00:49:29 -04:00
$phpath = trim ( shell_exec ( 'which php' ));
if ( ! strlen ( $phpath )) {
2010-11-25 18:33:32 -05:00
$o .= t ( 'Could not find a command line version of PHP in the web server PATH.' ) . EOL ;
$o .= t ( 'This is required. Please adjust the configuration file .htconfig.php accordingly.' ) . EOL ;
}
if ( strlen ( $phpath )) {
$str = autoname ( 8 );
$cmd = " $phpath testargs.php $str " ;
$result = trim ( shell_exec ( $cmd ));
if ( $result != $str ) {
$o .= t ( 'The command line version of PHP on your system does not have "register_argc_argv" enabled.' ) . EOL ;
$o .= t ( 'This is required for message delivery to work.' ) . EOL ;
}
2010-08-16 00:49:29 -04:00
}
return $o ;
2010-11-25 18:33:32 -05:00
2010-08-16 00:49:29 -04:00
}
2010-11-25 18:33:32 -05:00
function check_keys () {
$o = '' ;
$res = false ;
if ( function_exists ( 'openssl_pkey_new' ))
$res = openssl_pkey_new ( array (
'digest_alg' => 'sha1' ,
'private_key_bits' => 4096 ,
'encrypt_key' => false ));
// Get private key
if ( ! $res )
$o .= t ( 'Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys' ) . EOL ;
return $o ;
}
2010-11-18 05:59:59 -05:00
function check_funcs () {
2010-11-18 18:57:09 -05:00
if (( function_exists ( 'apache_get_modules' )) && ( ! in_array ( 'mod_rewrite' , apache_get_modules ())))
notice ( t ( 'Error: Apache webserver mod-rewrite module is required but not installed.' ) . EOL );
2010-11-18 05:59:59 -05:00
if ( ! function_exists ( 'curl_init' ))
notice ( t ( 'Error: libCURL PHP module required but not installed.' ) . EOL );
if ( ! function_exists ( 'imagecreatefromjpeg' ))
notice ( t ( 'Error: GD graphics PHP module with JPEG support required but not installed.' ) . EOL );
if ( ! function_exists ( 'openssl_public_encrypt' ))
notice ( t ( 'Error: openssl PHP module required but not installed.' ) . EOL );
if ( ! function_exists ( 'mysqli_connect' ))
notice ( t ( 'Error: mysqli PHP module required but not installed.' ) . EOL );
2010-11-18 18:57:09 -05:00
if (( x ( $_SESSION , 'sysmsg' )) && strlen ( $_SESSION [ 'sysmsg' ]))
2010-12-22 16:55:44 -05:00
notice ( t ( 'Please see the file "INSTALL.txt".' ) . EOL );
2010-11-18 05:59:59 -05:00
}
2010-08-16 00:49:29 -04:00
function check_htconfig () {
if ((( file_exists ( '.htconfig.php' )) && ( ! is_writable ( '.htconfig.php' )))
|| ( ! is_writable ( '.' ))) {
2010-11-06 08:07:10 -04:00
$o = t ( 'The web installer needs to be able to create a file called ".htconfig.php" in the top folder of your web server and it is unable to do so.' );
$o .= t ( 'This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.' );
$o .= t ( 'Please check with your site documentation or support people to see if this situation can be corrected.' );
2010-12-22 16:55:44 -05:00
$o .= t ( 'If not, you may be required to perform a manual installation. Please see the file "INSTALL.txt" for instructions.' );
2010-08-16 00:49:29 -04:00
}
2010-11-15 22:27:15 -05:00
return $o ;
2010-08-16 00:49:29 -04:00
}
function manual_config ( & $a ) {
2010-11-15 22:27:15 -05:00
$data = htmlentities ( $a -> data );
2010-11-06 08:07:10 -04:00
$o = t ( 'The database configuration file ".htconfig.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.' );
2010-11-15 22:27:15 -05:00
$o .= " <textarea rows= \" 24 \" cols= \" 80 \" > $data </textarea> " ;
2010-11-06 08:07:10 -04:00
return $o ;
2010-08-16 00:49:29 -04:00
}
function load_database ( $db ) {
$str = file_get_contents ( 'database.sql' );
$arr = explode ( ';' , $str );
$errors = 0 ;
foreach ( $arr as $a ) {
if ( strlen ( trim ( $a ))) {
$r = @ $db -> q ( trim ( $a ));
if ( ! $r ) {
notice ( t ( 'Errors encountered creating database tables.' ) . $a . EOL );
$errors ++ ;
}
}
2010-11-22 02:00:01 -05:00
}
2010-08-16 00:49:29 -04:00
return $errors ;
}