<?php 
//+-----------------------------------------------------+//
//+ Copyright floopo.com
//+ helper.index.php
//+ Installion helper (FTP Chmod) file
//+-----------------------------------------------------+//

//__________________________FTP CHMOD____________________________________________________________
if(isset($_POST['ftpchmod'])) {

    $show = 2;
    $setup_step1 = 'SetupDone';

    foreach($_POST as $key => $value) {
        $$key = $value;//create vars and assign values

        if($value == '') {//check if filled in
            $notification = 'Fill in all required fields';
            $error = true;
        }
    }

    //___connect to ftp______
    if(!$error) {

        // set up a connection or die
        $ftp_host = str_replace('http://www.','',$ftp_host);
        $ftp_host = str_replace('http://','',$ftp_host);
        $ftp_host = str_replace('www.','',$ftp_host);
        $ftp_host = trim($ftp_host);

        $conn_id = ftp_connect($ftp_host) or $notification = 'Failed to connect to FTP';

        // try to login
        if(!ftp_login($conn_id,$ftp_username,$ftp_password)) {
            $notification = 'Failed to connect to FTP';
            $connect_error = 1;
        }

        if(!$connect_error) {
            //remove starting /
            if(substr($path,0,1) == '/') {
                $path = substr($path,1);
            }

            //remove any trailing /
            if(substr($path,-1) == '/') {
                $path = substr($path,0,-1);
            }

            $path = trim($path);

            //cpanel users who may put 'www'
            if($path == 'www') {
                $path = 'public_html';
            }

            //chmod each one in trun
            $pass_count = 1;
            foreach($directories as $key => $value) {
                $file_path = $path.$value;
                $chmod = '0777';
                if(ftp_chmod($conn_id,eval("return({$chmod});"),$file_path) !== false) {
                    $pass_count += 1;
                }
            }
        }

    }

    //________check if all dir passed____________
    $error_count = 0;
    foreach($directories as $key => $value) {

        if(is_writable(BASEPATH.$value)) {
            $$key = 'ChmodPass';//css
        }
        else {
            $$key = 'ChmodFail';//css
            $error_count += 1;//error count
        }

    }

    if($error_count == 0) {//its all ok al done
        $show_chmod = 1;
        $show = 3;
        $setup_step1 = 'SetupDone';
    }

}

//__________________________MYSQL SETUP________________________________________________________________
if(isset($_POST['sqldump'])) {
    $show = 5;
    $setup_finish = 'SetupDone';

    //__________execute each sql_________________
    $file_content = file(BASEPATH.'/setup/setup.sql');
    $query = "";
    foreach($file_content as $sql_line) {
        if(trim($sql_line) != "" && strpos($sql_line,"--") === false) {
            $query .= $sql_line;
            if(preg_match("/;\s*$/",$sql_line)) {
                $result = @mysql_query($query) or $mysql_error = mysql_error();
                $query = "";
            }
        }
    }

    //turn each post into var
    foreach($_POST as $key => $value) {
        $$key = @mysql_real_escape_string($value);
    }

    //secure password
    $setup_password = md5($setup_password);

    //update system settings
    $sql = "UPDATE system_settings
        SET db_name = '$setup_mysqldb',
            db_host = '$setup_mysqlhost',
            db_user = '$setup_mysqluser',
            db_pass = '$setup_mysqlpass',
            site_url = '$setup_url'
        WHERE last_update = 0";
    @mysql_query($sql);

    //update admin password
    $sql = "UPDATE siteadmin
        SET password = '$setup_password'
        WHERE user_name = 'admin'";
    @mysql_query($sql);

    //if all is ok show finish page
    if(@mysql_error()) {
        $title = 'Installation Error!';
        $show = 5;

    } else {
        //________write to settings file______________________
        include_once (BASEPATH.'/setup/configbuilder.php');
        $myFile = BASEPATH.'/config/settings.php';
        @unlink($myFile);
        $fh = @fopen($myFile,'wb');
        @fwrite($fh,$config_line) or $error = true;
        @fclose($fh);

        //writing notification
        if($error || @mysql_error()) {
            $title = 'Installation Error!';
            $show = 5;
        } else {
        
        //__________yippiee!! all done_______
            $title = 'Installation Complete';
            $show = 6;
        }

    }

}
?>