<?php
//+-----------------------------------------------+//
//+ Copyright Floopo.com
//+ images.php
//+ Display and adding new images via web
//+-----------------------------------------------+//

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

//____DEFAULT TEMPLATE_____
$inner_template = TEMPLATES.'tpl_index.htm';


//____MYSQL CONNECTION____
//$quickmysql_debug = 1;
$dbase = new QuickMysql(DB_NAME,DB_USER,DB_PASS,DB_HOST);


//___PRESETS_____

//___ARE WE VIEWING OR EDITING_______________
$action = Sanatize($_GET['action']); //get action to load
if($action == '') {
    $action = Sanatize($_POST['action']);
}

$stream_type = Sanatize($_GET['type']); //updates, following_updates etc


//___START_______________

//If we are editing or adding new text - Load helper
if($action == 'add') {

    include_once (BASEPATH.'/com/com_images/helper.php');

} else {

    //____DETERMINE THE STREAM______________
    $incoming_stream = GetStream();
$incoming_stream = strtolower($incoming_stream);

    //___MEMBER STEAM___________________________
if(IsMemberActive($incoming_stream) && MembersPrivacyCheck($incoming_stream)) {


        //Determine type of stream to load (set in calling query)
        switch($stream_type) {

                //_______updates from people I am following_____
            case 'following_images':
                $paginate = array($config['page_limits'],Sanatize($_GET['page']),'  >>','<<  '); //(limit, current page, next, previous)
                $pagi_addurl_pre = "$incoming_stream/images/following/&";
                $stream = $dbase->PaginatedRecordLoop("SELECT following.followers_user_name AS fol_user, content_images.text_message, content_images.filename, content_images.content_id, member_profile.avatar, content_images.date_added, member_profile.user_name
                                                FROM following
                                                JOIN content_images ON following.user_name = content_images.user_name
                                                JOIN member_profile ON following.user_name =  member_profile.user_name 
                                                WHERE following.followers_user_name = '$incoming_stream'
                                                ORDER BY content_images.content_id DESC");
                $pagination = str_replace('&page=', '', $dbase->pagination_simple_html); //SEO trimming
                (count($stream) > 0) ? $images_avatar = 1 : $no_content = 1; //image block style
                break;

                //_______updates from favorites_____
            case 'favorite_images':
                $paginate = array($config['page_limits'],Sanatize($_GET['page']),'  >>','<<  '); //(limit, current page, next, previous)
                $pagi_addurl_pre = "$incoming_stream/images/favorites/&";
                $stream = $dbase->PaginatedRecordLoop("SELECT favorite_images.user_name AS fav_user, content_images.text_message, content_images.filename, content_images.content_id, member_profile.avatar, content_images.date_added, member_profile.user_name
                                                    FROM favorite_images
                                                    LEFT JOIN content_images ON favorite_images.content_id = content_images.content_id
                                                    LEFT JOIN member_profile ON content_images.user_name = member_profile.user_name
                                                    WHERE favorite_images.user_name = '$incoming_stream'
                                                    ORDER BY favorite_images.indexer DESC");

                $pagination = str_replace('&page=', '', $dbase->pagination_simple_html); //SEO trimming
                (count($stream) > 0) ? $images_avatar = 1 : $no_content = 1; //image block style
                break;


                //_______Replies to me _____
             case 'replies':

                 //check if logged in
                 ($env_user_name == '')? @header('Location: '.$env_base_url) : NULL;
                 $paginate = array($config['page_limits'],Sanatize($_GET['page']),'  >>','<<  '); //(limit, current page, next, previous)
                 $pagi_addurl_pre = "$incoming_stream/replies/images/&";
                 $stream = $dbase->PaginatedRecordLoop("SELECT content_images.user_name, content_images.content_id, content_images.content_public, content_images.date_added, content_images.text_message, content_images.filename, member_profile.avatar, member_profile.user_name
                                            FROM content_images
                                            JOIN member_profile ON content_images.user_name =  member_profile.user_name 
                                            WHERE content_images.content_public = 1 
                                            AND content_images.text_message LIKE ('%@$env_user_name%')
                                            ORDER BY content_id DESC");
                 $pagination = str_replace('&page=', '', $dbase->pagination_simple_html); //SEO trimming
                 (count($stream) > 0) ? $images_avatar = 1 : $no_content = 1; //image block style
                 break;
            
            
                //_______My updates________
            default:
                $paginate = array($config['page_limits'],Sanatize($_GET['page']),'  >>','<<  '); //(limit, current page, next, previous)
                $pagi_addurl_pre = "$incoming_stream/images/&";
                $stream = $dbase->PaginatedRecordLoop("SELECT * 
                                                FROM content_images
                                                WHERE user_name = '$incoming_stream'
                                                ORDER BY content_id DESC");
                $pagination = str_replace('&page=', '', $dbase->pagination_simple_html); //SEO trimming
                (count($stream) > 0) ? $images_plain = 1 : $no_content = 1; //image block style
                break;


        }

        //_______________MEMBERS GLOBAL DATA______________________________________
        $membersdata = new MembersGlobalData($incoming_stream);

        $web_profiles_array = $membersdata->WebProfiles(); // web profile

        $following_array = $membersdata->GetFollowing(); // people I am followings
        $count_following = @count($following_array);

        $followers_array = $membersdata->GetFollowers(); // people following me
        $count_followers = @count($followers_array);

        $count_updates = $membersdata->CountMembersUpdates(); //count updates

        $members_profile = $membersdata->MembersProfile(); //welcome message

        //--------------------------------------------------------------------------



    } else {

         //____this is a vlaid stream but private___________
        if(IsMemberActive($incoming_stream)){
		    $_SESSION["notice"] = 6;
            @header('Location: '.$env_base_url.'/notice'); //notice codes can be added in com_notice
            die();
        }

        //______if this was an 'invalid' stream____________
        if($incoming_stream != '' && $incoming_stream != 'public') {
        header('Location: pages/notfound'); //this will load /com/com_pages/pages.php?load=notfound
            die();
        }


        //_______Destroy any search_________________________
        if($_GET['search'] != 1){
        $_SESSION['search_query'] = '';
        }

        //______Seach Query_______________________________________
        if($_POST['search'] != ''){
        $search_string = Sanatize(mysql_real_escape_string($_POST['search']));
        $_SESSION['search_query'] = "AND content_images.text_message LIKE '%$search_string%'";
        }
    
        $search_query = $_SESSION['search_query'];
    
    
        //_____PUBLIC STREAM______________________________________
        $paginate = array($config['page_limits'],Sanatize($_GET['page']),'  >>','<<  '); //(limit, current page, next, previous)
        $pagi_addurl_pre = 'public/images/&';
        
         //search url
         if($search_query != ''){
         $pagi_addurl_pre = 'public/images/search/&';
         }
    
        $stream = $dbase->PaginatedRecordLoop("SELECT content_images.user_name, content_images.content_id, content_images.content_public, content_images.date_added, content_images.text_message, content_images.filename, member_profile.avatar, member_profile.user_name
                                            FROM content_images
                                            JOIN member_profile ON content_images.user_name =  member_profile.user_name 
                                            WHERE content_images.content_public = 1 $search_query ORDER BY content_id DESC");

        $incoming_stream = 'public';
        $pagination = str_replace('&page=', '', $dbase->pagination_simple_html); //SEO trimming
        
		//Set up the view
        $welcome_message = $stream['welcome_message'];
        (count($stream) > 0) ? $images_avatar = 1 : $no_content = 1; //text block style
    }
}


//____VISIBILITY________________________
$visibility = new Visibility('text');
$visible = $visibility->ComText(); //could create own method in lib_visibility but at the moment its much the same as text updates
//Explode array back to vars
foreach($visible as $key => $value) {
    $$key = $value;
}



//____DISPLAY ANY OUTPUT_________________________
$TBS = new clsTinyButStrong;
$TBS->NoErr = true;
if($action == '') {
    $TBS->LoadTemplate(TPL_WRAPPER);
} else {
    $TBS->LoadTemplate($template);
}
$TBS->MergeField('formdata',$_POST);
$TBS->MergeField('config',$config);
$TBS->MergeField('profile',$members_profile);
$TBS->MergeField('lang',$lang);
$TBS->MergeBlock('blk1',$stream);
$TBS->MergeBlock('blk2',$web_profiles_array);
$TBS->MergeBlock('blk3',$following_array);
$TBS->Render = TBS_OUTPUT;
$TBS->Show();
@mysql_close(); ?>