2010-08-16 04:49:29 +00:00
< ? php
function install_post ( & $a ) {
global $db ;
2010-12-03 01:31:48 +00:00
$urlpath = $a -> get_path ();
2010-08-16 04:49:29 +00: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' ]));
2011-07-08 02:12:35 +00:00
$adminmail = notags ( trim ( $_POST [ 'adminmail' ]));
2010-08-16 04:49:29 +00:00
require_once ( " dba.php " );
2011-03-22 02:33:06 +00:00
unset ( $db );
2010-12-22 21:55:44 +00:00
$db = new dba ( $dbhost , $dbuser , $dbpass , $dbdata , true );
2010-08-16 04:49:29 +00:00
2011-12-03 08:08:04 +00:00
if ( get_db_errno ()) {
2011-03-22 00:34:29 +00:00
unset ( $db );
2010-11-18 10:59:59 +00:00
$db = new dba ( $dbhost , $dbuser , $dbpass , '' , true );
2011-12-03 08:08:04 +00:00
if ( ! get_db_errno ()) {
2010-11-18 10:59:59 +00:00
$r = q ( " CREATE DATABASE '%s' " ,
dbesc ( $dbdata )
);
2011-03-22 00:34:29 +00:00
if ( $r ) {
unset ( $db );
2010-12-22 21:55:44 +00:00
$db = new dba ( $dbhost , $dbuser , $dbpass , $dbdata , true );
2011-03-22 00:34:29 +00:00
}
2010-11-18 10:59:59 +00:00
}
2011-12-03 08:08:04 +00:00
if ( get_db_errno ()) {
2010-11-18 10:59:59 +00:00
notice ( t ( 'Could not create/connect to database.' ) . EOL );
return ;
}
2010-08-16 04:49:29 +00:00
}
2010-11-18 10:59:59 +00:00
2011-05-23 09:39:57 +00:00
info ( t ( 'Connected to database.' ) . EOL );
2010-08-16 04:49:29 +00:00
2011-05-11 11:37:13 +00:00
$tpl = get_intltext_template ( 'htconfig.tpl' );
2010-08-16 04:49:29 +00:00
$txt = replace_macros ( $tpl , array (
'$dbhost' => $dbhost ,
'$dbuser' => $dbuser ,
'$dbpass' => $dbpass ,
'$dbdata' => $dbdata ,
'$timezone' => $timezone ,
2010-12-03 01:31:48 +00:00
'$urlpath' => $urlpath ,
2011-07-08 02:12:35 +00:00
'$phpath' => $phpath ,
'$adminmail' => $adminmail
2010-08-16 04:49:29 +00:00
));
2011-07-08 02:12:35 +00:00
2010-08-16 04:49:29 +00:00
$result = file_put_contents ( '.htconfig.php' , $txt );
if ( ! $result ) {
2011-10-20 21:55:40 +00:00
$a -> data [ 'txt' ] = $txt ;
2010-08-16 04:49:29 +00:00
}
$errors = load_database ( $db );
2010-11-06 12:07:10 +00:00
2011-03-22 00:34:29 +00:00
if ( $errors )
$a -> data [ 'db_failed' ] = true ;
else
$a -> data [ 'db_installed' ] = true ;
2010-08-16 04:49:29 +00:00
2011-03-22 00:34:29 +00:00
return ;
2010-08-16 04:49:29 +00:00
}
2011-12-03 08:08:04 +00:00
function get_db_errno () {
if ( class_exists ( 'mysqli' ))
return mysqli_connect_errno ();
else
return mysql_errno ();
}
2010-08-16 04:49:29 +00:00
function install_content ( & $a ) {
2011-03-22 00:34:29 +00:00
global $db ;
2010-11-18 10:59:59 +00:00
$o = '' ;
2011-03-22 00:34:29 +00:00
if ( x ( $a -> data , 'db_installed' )) {
$o .= '<h2>' . t ( 'Proceed with Installation' ) . '</h2>' ;
$o .= '<p style="font-size: 130%;">' ;
2011-12-03 08:08:04 +00:00
$o .= t ( 'Your Friendica site database has been installed.' ) . EOL ;
2011-03-22 00:34:29 +00:00
$o .= t ( 'IMPORTANT: You will need to [manually] setup a scheduled task for the poller.' ) . EOL ;
$o .= t ( 'Please see the file "INSTALL.txt".' ) . EOL ;
$o .= '<br />' ;
$o .= '<a href="' . $a -> get_baseurl () . '/register' . '">' . t ( 'Proceed to registration' ) . '</a>' ;
$o .= '</p>' ;
return $o ;
}
if ( x ( $a -> data , 'db_failed' )) {
$o .= t ( 'Database import failed.' ) . EOL ;
$o .= t ( 'You may need to import the file "database.sql" manually using phpmyadmin or mysql.' ) . EOL ;
$o .= t ( 'Please see the file "INSTALL.txt".' ) . EOL ;
return $o ;
}
if ( $db && $db -> connected ) {
$r = q ( " SELECT COUNT(*) as `total` FROM `user` " );
if ( $r && count ( $r ) && $r [ 0 ][ 'total' ]) {
notice ( t ( 'Permission denied.' ) . EOL );
return '' ;
}
}
2010-11-18 10:59:59 +00:00
2011-12-03 08:08:04 +00:00
info ( t ( 'Welcome to Friendica.' ) . EOL );
2010-08-16 04:49:29 +00:00
2010-11-18 10:59:59 +00:00
check_funcs ();
2010-08-16 04:49:29 +00:00
$o .= check_htconfig ();
if ( strlen ( $o ))
return $o ;
2011-10-20 21:55:40 +00:00
if ( strlen ( $a -> data [ 'txt' ])) {
2010-08-16 04:49:29 +00:00
$o .= manual_config ( $a );
return ;
}
$o .= check_php ( $phpath );
2010-11-25 23:33:32 +00:00
$o .= check_keys ();
2010-08-16 04:49:29 +00:00
require_once ( 'datetime.php' );
2011-05-11 11:37:13 +00:00
$tpl = get_markup_template ( 'install_db.tpl' );
2010-08-16 04:49:29 +00:00
$o .= replace_macros ( $tpl , array (
2011-12-03 08:08:04 +00:00
'$lbl_01' => t ( 'Friendica Social Communications Server' ),
2011-04-08 06:10:43 +00:00
'$lbl_02' => t ( 'Installation' ),
2011-12-03 08:08:04 +00:00
'$lbl_03' => t ( 'In order to install Friendica we need to know how to connect to your database.' ),
2011-04-08 06:10:43 +00:00
'$lbl_04' => t ( 'Please contact your hosting provider or site administrator if you have questions about these settings.' ),
2011-07-28 04:48:02 +00:00
'$lbl_05' => t ( 'The database you specify below should already exist. If it does not, please create it before continuing.' ),
2011-04-08 06:10:43 +00:00
'$lbl_06' => t ( 'Database Server Name' ),
'$lbl_07' => t ( 'Database Login Name' ),
'$lbl_08' => t ( 'Database Login Password' ),
'$lbl_09' => t ( 'Database Name' ),
'$lbl_10' => t ( 'Please select a default timezone for your website' ),
2011-07-28 04:48:02 +00:00
'$lbl_11' => t ( 'Site administrator email address. Your account email address must match this in order to use the web admin panel.' ),
2010-12-03 01:31:48 +00:00
'$baseurl' => $a -> get_baseurl (),
2010-08-16 04:49:29 +00: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' ])),
2011-07-08 02:12:35 +00:00
'$phpath' => $phpath ,
2011-07-13 06:14:38 +00:00
'$adminmail' => notags ( trim ( $_POST [ 'adminmail' ]))
2010-08-16 04:49:29 +00:00
));
return $o ;
}
function check_php ( & $phpath ) {
2010-11-06 12:07:10 +00:00
$o = '' ;
2010-08-16 04:49:29 +00:00
$phpath = trim ( shell_exec ( 'which php' ));
if ( ! strlen ( $phpath )) {
2010-11-25 23:33:32 +00: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 04:49:29 +00:00
}
return $o ;
2010-11-25 23:33:32 +00:00
2010-08-16 04:49:29 +00:00
}
2010-11-25 23:33:32 +00: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
2010-12-22 22:07:27 +00:00
if ( ! $res ) {
$o .= t ( 'Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys' ) . EOL ;
$o .= t ( 'If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".' ) . EOL ;
}
2010-11-25 23:33:32 +00:00
return $o ;
}
2010-11-18 10:59:59 +00:00
function check_funcs () {
2010-11-18 23:57:09 +00: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 10:59:59 +00: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' ))
2011-06-05 05:48:43 +00:00
notice ( t ( 'Error: mysqli PHP module required but not installed.' ) . EOL );
if ( ! function_exists ( 'mb_strlen' ))
notice ( t ( 'Error: mb_string PHP module required but not installed.' ) . EOL );
2011-10-20 21:55:40 +00:00
if (( x ( $_SESSION , 'sysmsg' )) && is_array ( $_SESSION [ 'sysmsg' ]) && count ( $_SESSION [ 'sysmsg' ]))
2010-12-22 21:55:44 +00:00
notice ( t ( 'Please see the file "INSTALL.txt".' ) . EOL );
2010-11-18 10:59:59 +00:00
}
2010-08-16 04:49:29 +00:00
function check_htconfig () {
if ((( file_exists ( '.htconfig.php' )) && ( ! is_writable ( '.htconfig.php' )))
|| ( ! is_writable ( '.' ))) {
2010-11-06 12:07:10 +00: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 21:55:44 +00: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 04:49:29 +00:00
}
2010-11-16 03:27:15 +00:00
return $o ;
2010-08-16 04:49:29 +00:00
}
function manual_config ( & $a ) {
2011-10-20 21:55:40 +00:00
$data = htmlentities ( $a -> data [ 'txt' ]);
2010-11-06 12:07:10 +00: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-16 03:27:15 +00:00
$o .= " <textarea rows= \" 24 \" cols= \" 80 \" > $data </textarea> " ;
2010-11-06 12:07:10 +00:00
return $o ;
2010-08-16 04:49:29 +00: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 07:00:01 +00:00
}
2010-08-16 04:49:29 +00:00
return $errors ;
2011-05-23 09:39:57 +00:00
}