<?php
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ helper.settings.php
//+ Settings admin helper
//+-----------------------------------------------------+//

defined('BASEPATH') or die('You can not directly access this file');//check its within this framework

//____DEFAULT TEMPLATE_____________________________
$inner_template = ADMIN_TEMPLATES.'tpl_settings.htm';

//____Check for any Post_____________________

if(isset($_POST['update'])) {

    //__process the form
    $process = new FormProcessor();
    if(!$process->RequiredFields('site_name,
                                  site_url,
                                  db_name,
                                  db_host,
                                  site_description,
                                  splash_message,
                                  language,
                                  members_welcome_message,
                                  display_followers_block,
                                  display_members_name,
                                  page_limits,
                                  following_list,
                                  display_login_box,
                                  display_signup_splash,
                                  allow_registration,
                                  members_only,
                                  privacy_message')) {

        $notification = $lang['fill_in_all'];

    }
    else {

        //escape the data (any clean further? admin input safe??)
        foreach($_POST as $key => $value) {

         //no real espace for fckeditor
         if($key == 'site_description' || $key == 'splash_message'){
          $$key = $value;

        }else{
        
            $$key = Sanatize(@mysql_real_escape_string($value));
          }
       }
       
       /*
       
       //SECURITY CHECKS---------------------------------------------------------------
       $numeric = array('display_followers_block',
			            'display_login_box',
			            'display_signup_splash',
			            'display_members_name',
			            'allow_registration',
			            'members_only',
			            'page_limits',
			            'members_only',
                        'following_list');
                        
        foreach($numeric as $key => $value) {
        $$key = ((is_numeric($value))? $value : 0); //inavlid input - set to zero
        }     
       //-------------------------------------------------------------------------------
       */
       


        //__upate the db
        $dbase->UpdateRecord("UPDATE system_settings
                      SET
					  site_name= '$site_name',
					  site_description= '$site_description',
					  language= '$language',
					  members_welcome_message= '$members_welcome_message',
					  display_followers_block= $display_followers_block,
					  display_login_box= $display_login_box,
					  display_signup_splash= $display_signup_splash,
					  display_members_name= $display_members_name,
					  reserved_names= '$reserved_names',
					  allow_registration= $allow_registration,
                      debug_mode = 0,
					  members_only= $members_only,
					  page_limits= $page_limits,
					  site_title= '$site_title',
					  meta_title= '$meta_title',
					  meta_description= '$meta_description',
					  meta_keywords= '$meta_keywords',
					  privacy_message= '$privacy_message',
					  splash_message= '$splash_message',
					  db_name= '$db_name',
					  db_user= '$db_user',
					  db_host= '$db_host',
					  site_url= '$site_url',
					  following_list= $following_list
					  WHERE place_holder = 'yes'");



       //________write to settings file______________________
        if($_POST['db_password'] != ''){
        $db_passowrd = $_POST['db_password'];
        $dbase->UpdateRecord("UPDATE system_settings
                      SET
					  db_pass= '$db_passowrd'
					  WHERE place_holder = 'yes'");
        
        }

        //________write to settings file______________________
        if($_POST['new_password'] != ''){

        $new_password = md5($_POST['new_password']);
        $dbase->UpdateRecord("UPDATE siteadmin
                              SET
					          password= '$new_password'
                              WHERE
                              user_name='admin'");

        }


        //________write to settings file______________________
        include_once (BASEPATH.'/com/com_admin/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) {
            $notification = $lang['settings_writing_error'];

        }
        else {

            //general notification
            $notification = (($dbase->obj_status) ? $lang['request_completed'] : $lang['generic_error']);
        }

    }
}

//____Lets get current settings_____________________
$settings = $dbase->SelectRecord("SELECT *
                                  FROM system_settings");

//___Set live data________________________________
$live_data = ((isset($_POST['update'])) ? $_POST : $settings);

//____Lets set the pulldowns & general data_______________________
foreach($live_data as $key => $value) {

    //pull down stuff
    $new_var = $key.'_'.$value;
    $$new_var = 'selected';

    //field boxes stuff
    $$key = stripslashes($value);
}

//____Lets get the themes___________________________
$themesdir = @opendir(BASEPATH.'/themes/');
while(($file = @readdir($themesdir)) !== false) {
    $found_path = BASEPATH.'/themes/'.$file;
    $file_type = @filetype($found_path);//is it dir of file
    if($file != '.' && $file != '..' && $file != 'siteadmin' && $file_type == 'dir') {

        //set dbase selected theme
        $selected = (($file == $live_data['theme']) ? 'selected' : '');

        //create pull down options
        $themes_list = $themes_list.'<option value="'.$file.'" '.$selected.'>'.$file.'</option>';
    }
}

//____Lets get the languages________________________
$langdir = @opendir(BASEPATH.'/lang/');
while(($file = @readdir($langdir)) !== false) {
    $found_path = BASEPATH.'/lang/'.$file;
    $file_type = @filetype($found_path);//is it dir of file
    if($file != '.' && $file != '..' && $file_type == 'file') {

        //set dbase selected theme
        $file = str_replace('.php','',$file);
        
        $selected = (($file == $live_data['language']) ? 'selected' : '');

        //create pull down options
        $language_list = $language_list.'<option value="'.$file.'" '.$selected.'>'.$file.'</option>';
    }
}


    include (FCK_EDITOR);
    $sBasePath = BASEURL.'/thirdparty/fckeditor/';
    $oFCKeditor = new FCKeditor('site_description');
    $oFCKeditor->BasePath = $sBasePath;
    $oFCKeditor->CustomConfigurationsPath = "fckconfig.js";
    $oFCKeditor->ToolbarSet = 'BasicPlus';//OR manually set to 'Basic', 'BasicPlus' or 'Advanced'
    $oFCKeditor->SkinPath = $sBasePath + 'editor/skins/silver/';
    $oFCKeditor->Value = $site_description;
    $oFCKeditor->Width = '575';
    $oFCKeditor->Height = '175';
    $FCKeditor1 = $oFCKeditor->CreateHtml();
    $fck_site_description = $FCKeditor1;
    
    
    
    $oFCKeditor2 = new FCKeditor('splash_message');
    $oFCKeditor2->BasePath = $sBasePath;
    $oFCKeditor2->CustomConfigurationsPath = "fckconfig.js";
    $oFCKeditor2->ToolbarSet = 'BasicPlus';//OR manually set to 'Basic', 'BasicPlus' or 'Advanced'
    $oFCKeditor2->SkinPath = $sBasePath + 'editor/skins/silver/';
    $oFCKeditor2->Value = $splash_message;
    $oFCKeditor2->Width = '575';
    $oFCKeditor2->Height = '175';
    $FCKeditor2 = $oFCKeditor2->CreateHtml();
    $fck_splash_message = $FCKeditor2;
?>