<?php 
//+-------------------------------------------------------------------------------------------------------
//+ Copyright Floopo.com
//+ helper.profile.php
//+ Edit users profile
//+
//+-------------------------------------------------------------------------------------------------------
//+ main_font_color                  - #353535;/*Customize-Main-Font-Color*/
//+ $title_color                     - #2e9fd9;/*Customize-Headers*/
//+ $hyper_link_color                - #137BFB;/*Customize-Hyperlinks*/
//+ $background_color                - #d7e9fb;/*Customize-MainBody-Background-Color*/
//+ $border_color                    - #bad7f4;/*Customize-Border*/
//+ $content_background_color        - #FFFFFF;/*Customize-Content-Background-Color*/
//+ $contrast_background_color       - #edf4fe;/*Customize-Contrast-background-Color*/
//+ $background_image                - url(../images/wrapper_background.png);/*Customize-MainBody-Background-Image*/
//+--------------------------------------------------------------------------------------------------------

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

$show_cp = 1;

//___Set some basics______________________
define('CUSTOMPROFILE',BASEPATH.'/plugins/customprofile/');
$default_css = BASEPATH.'/themes/'.$config['theme'].'/css/members_custom_copy.css'; //default css file in theme
$images_dir = CUSTOMPROFILE.'member_images/';
$members_css_dir = CUSTOMPROFILE.'member_css/';
$members_css = CUSTOMPROFILE.'member_css/'.$env_user_name.'.css';


//___CHECK IF SUBMIT______________________________________________________________________________________________________________________

if(isset($_POST['update']) && $_POST['reset'] == '') {

    foreach($_POST as $key => $value) {
        $$key = Sanatize(mysql_real_escape_string($value));
    }

    //_______check if member has a css file____
    if(!is_file($members_css)) {

        //copy from default (ERROR code: 001)
        (@copy($default_css,$members_css)) ? null : $display_notification = $lang['generic_error'].'- 001';
    }

    //check if css dir is writable (ERROR code: 002)
    (is_writeable($members_css_dir)) ? $proceed1 = true : $display_notification = $lang['generic_error'].'- 002';

    //____Background Images___________

    //check if images dir is writeable (ERROR code: 003)
    (is_writeable($images_dir)) ? $proceed2 = true : $display_notification = $lang['generic_error'].'- 003';


    //________________Lets upload the image file____________________________
    if($_FILES) {
    
        //allowed file types
        $allowed_extensions = array('gif','jpg','png','jpeg');
        $allowed_file_types = array('image/gif','image/jpg','image/jpeg','image/pjpeg','image/x-png','image/png');

       
        //temp move files
        @copy(CUSTOMPROFILE.'member_images/'.$env_user_name.'.png',CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.png');
        @copy(CUSTOMPROFILE.'member_images/'.$env_user_name.'.jpg',CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.jpg');
        @copy(CUSTOMPROFILE.'member_images/'.$env_user_name.'.jpeg',CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.jpeg');


        //______file upload________________
        $new_filename = $env_user_name;
        $filesize_limit = 1000000;
        $destination_folder = $images_dir;//with or without trailing /
        $fileupload = new FileUpload($new_filename,$destination_folder,$filesize_limit, $allowed_extensions,$allowed_file_types);
        if($fileupload->obj_status){
         
	
        //lets delete backup files
        @unlink(CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.png');
        @unlink(CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.jpg');
        @unlink(CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.jpeg');
        
        $new_image = $fileupload->saved_file_name; //e.g. output : fredmarks.gif
		$background_image = true;
		
		
		
		}else{

		//if an error - restore backup files
        @copy(CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.png',CUSTOMPROFILE.'member_images/'.$env_user_name.'.png');
        @copy(CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.jpg',CUSTOMPROFILE.'member_images/'.$env_user_name.'.jpg');
        @copy(CUSTOMPROFILE.'member_images/'.$env_user_name.'-bak.jpeg',CUSTOMPROFILE.'member_images/'.$env_user_name.'.jpeg');
       
		}

    }

    //______________Let put it all together_________________________

    if($proceed1 == true && $proceed2 == true) {

        //Get members css file contents
        $cssfile = @fopen($members_css,"r");
        $css_content = @fread($cssfile,filesize($members_css));
        @fclose($cssfile);


        //main_font_color
        if($main_font_color) {
            $css_content = preg_replace("%#(.*?);.*/.Customize-Main-Font-Color./%","$main_font_color;/*Customize-Main-Font-Color*/",$css_content);
        }

        //title_color
        if($title_color) {
            $css_content = preg_replace("%#(.*?);.*/.Customize-Headers./%","$title_color;/*Customize-Headers*/",$css_content);
        }

        if($hyper_link_color) {
            //hyper_link_color
            $css_content = preg_replace("%#(.*?);.*/.Customize-Hyperlinks./%","$hyper_link_color;/*Customize-Hyperlinks*/",$css_content);
        }

        //background_color
        if($background_color) {
            $css_content = preg_replace("%#(.*?);.*/.Customize-MainBody-Background-Color./%","$background_color;/*Customize-MainBody-Background-Color*/",$css_content);
        }

        //border_color
        if($border_color) {
            $css_content = preg_replace("%#(.*?);.*/.Customize-Border./%","$border_color;/*Customize-Border*/",$css_content);
        }

        //content_background_color
        if($content_background_color) {
            $css_content = preg_replace("%#(.*?);.*/.Customize-Content-Background-Color./%","$content_background_color;/*Customize-Content-Background-Color*/",$css_content);
        }

        //contrast_background_color
        if($contrast_background_color) {
            $css_content = preg_replace("%#(.*?);.*/.Customize-Contrast-background-Color./%","$contrast_background_color;/*Customize-Contrast-background-Color*/",$css_content);
        }
        
        
        //contrast_background_color
        if($background_image) {
            $css_content = preg_replace("%url.*;/.Customize-MainBody-Background-Image./%",
			                            "url($env_base_url/plugins/customprofile/member_images/$new_image);/*Customize-MainBody-Background-Image*/",$css_content);
        }
        
        
        //___write to file_______
        $file = @fopen($members_css,"w");
        @fwrite($file,$css_content);
        @fclose($file);
    $display_notification = $lang['request_completed'];

    }
}

//_________-CHECK IF RESET_______________________________________________________________________________________________________________________
if($_POST['reset'] != '') {

    //delete css file
    @unlink($members_css);
    $display_notification = $lang['request_completed'];
}

//___LOAD CURRENT PROFILE________________________________
if(is_file($members_css)) {

    //Get members css file contents
    $cssfile = @fopen($members_css,"r");
    $css_content = @fread($cssfile,filesize($members_css));
    @fclose($cssfile);

    //___LOAD DEFAULT PROFILE________________________________
}
else {
    //Get members css file contents
    $cssfile = @fopen($default_css,"r");
    $css_content = @fread($cssfile,filesize($default_css));
    @fclose($cssfile);
}

//---------------------------------------------------------------------------------------------------------------------------------------------------
//main_font_color
(preg_match('%#(.*?);.*/.Customize-Main-Font-Color./%',$css_content,$matches)) ? $main_font_color = $matches[1] : $main_font_color = "";


//title_color
(preg_match('%#(.*?);.*/.Customize-Headers./%',$css_content,$matches)) ? $title_color = $matches[1] : $title_color = "";

//hyper_link_color
(preg_match('%#(.*?);.*/.Customize-Hyperlinks./%',$css_content,$matches)) ? $hyper_link_color = $matches[1] : $hyper_link_color = "";

//background_color
(preg_match('%#(.*?);.*/.Customize-MainBody-Background-Color./%',$css_content,$matches)) ? $background_color = $matches[1] : $background_color = "";

//border_color
(preg_match('%#(.*?);.*/.Customize-Border./%',$css_content,$matches)) ? $border_color = $matches[1] : $border_color = "";

//content_background_color
(preg_match('%#(.*?);.*/.Customize-Content-Background-Color./%',$css_content,$matches)) ? $content_background_color = $matches[1] : $content_background_color = "";

//contrast_background_color
(preg_match('%#(.*?);.*/.Customize-Contrast-background-Color./%',$css_content,$matches)) ? $contrast_background_color = $matches[1] : $contrast_background_color = "";

//---------------------------------------------------------------------------------------------------------------------------------------------------
?>