<?php
/*
Name: Droppy - Online file sharing
Author: Proxibolt
File: config.php
Support: http://support.proxibolt.com
*/

//Starting session
if(!isset($_SESSION)) {
    session_start();
}

ob_start();

$debug_mode = false;

if($debug_mode) {
    // If the debug mode is set to true

    //Turning off error reporting.
    error_reporting(E_ALL);
    ini_set('log_errors', 1);
    ini_set('error_log', dirname(__FILE__) . '/../error.log');
    ini_set('display_errors', 0);
}
else
{
    // If the debug mode is set to false

    error_reporting(0);
    ini_set('log_errors', 0);
    ini_set('display_errors', 0);
}

//Include database config
require_once dirname(__FILE__) . '/db.php';

// Let Droppy set my ini settings
$set_ini_settings = false; // Enabled = true | Disabled = false

//Delete data in the upload table older than 1 month
$delete_old_data = 'NO';

/*__________________________________________________________________________________________________________________________________________________*/
/*_________________________________________Do not change anything below if you don't know what you're doing_________________________________________*/
/*__________________________________________________________________________________________________________________________________________________*/
$table_uploads      = 'droppy_uploads';
$table_files        = 'droppy_files';
$table_downloads    = 'droppy_downloads';
$table_settings     = 'droppy_settings';
$table_accounts     = 'droppy_accounts';
$table_templates    = 'droppy_templates';
$table_social       = 'droppy_social';
$table_backgrounds  = 'droppy_backgrounds';
$table_receivers    = 'droppy_receivers';
$table_language     = 'droppy_language';
$table_users        = 'droppy_users';
$table_plugins      = 'droppy_plugins';
$table_updates      = 'droppy_updates';
$table_themes       = 'droppy_themes';
$table_pages        = 'droppy_pages';
$table_popups       = 'droppy_popups';
$table_log          = 'droppy_log';

$check_uploads_table = $mysqli->query("SELECT * FROM $table_uploads");
$check_files_table = $mysqli->query("SELECT * FROM $table_files");
$check_downloads_table = $mysqli->query("SELECT * FROM $table_downloads");
$check_settings_table = $mysqli->query("SELECT * FROM $table_settings");
$check_accounts_table = $mysqli->query("SELECT * FROM $table_accounts");
$check_templates_table = $mysqli->query("SELECT * FROM $table_templates");
$check_social_table = $mysqli->query("SELECT * FROM $table_social");
$check_backgrounds_table = $mysqli->query("SELECT * FROM $table_backgrounds");
$check_themes_table = $mysqli->query("SELECT * FROM $table_themes");

if(!$check_uploads_table || !$check_files_table || !$check_downloads_table || !$check_settings_table || !$check_accounts_table || !$check_templates_table || !$check_social_table || !$check_backgrounds_table || $update == 'true') {
    //No data found
}
else
{
    $select_general = $mysqli->query("SELECT * FROM $table_settings");
    while($row = $select_general->fetch_assoc()) {
    	//General settings
    	$site_name                     = $row['site_name'];
    	$site_desc                     = $row['site_desc'];
    	$site_url                      = $row['site_url'];
        $lock_page                     = $row['lock_page'];
        $name_on_file                  = $row['name_on_file'];
    	$upload_dir                    = $row['upload_dir'];
    	$language                      = $row['language'];
    	$logo_path                     = $row['logo_path'];
    	$favicon_path                  = $row['favicon_path'];
    	$max_size                      = $row['max_size'];
    	$site_title                    = $row['site_title'];
    	$expire                        = $row['expire'];
        $bg_timer                      = $row['bg_timer'];
        $max_upload_files              = $row['max_files'];
        $background_url                = $row['bg_url'];
        if(!empty($row['blocked_types'])) {
            $disallowed_types          = $row['blocked_types'];
        }
        else
        {
            $disallowed_types          = '-';
        }
        $blocked_types_array           = explode(',', $row['blocked_types']);
        $blocked_types                 = array();
        $max_file_reports              = $row['max_file_reports'];
        foreach($blocked_types_array as $t => $type) {
            $blocked_types[]           = $type;
        }
        $email_to_name                 = $row['email_to_name']; 
        $terms_enabled                 = $row['terms_enabled'];
        $terms_text                    = $row['terms_text'];
        $about_text                    = $row['about_text'];
        $ad_1_enabled                  = $row['ad_1_enabled'];
        $ad_1_code                     = $row['ad_1_code'];
        $ad_2_enabled                  = $row['ad_2_enabled'];
        $ad_2_code                     = $row['ad_2_code'];
        $droppy_v                      = $row['version'];
        $default_destruct              = $row['default_destruct'];
        $default_sharetype             = $row['default_sharetype'];
        $default_email_to              = $row['default_email_to'];
        $password_enabled              = $row['password_enabled'];
        $analytics_code                = $row['analytics'];
        $accept_terms_enabled          = $row['accept_terms'];
        $purchase_code                 = $row['purchase_code'];
        $recaptcha_public_id           = '6LeLaygTAAAAAPXTHtzlDgfReJ1Fwztt_V9KrRd8';

        //Encryption setting
        $encrypt_files                 = $row['encrypt_files'];

    	//Mail settings
    	$email_server                  = $row['email_server'];
    	$email_from_email              = $row['email_from_email'];
    	$email_from_name               = $row['email_from_name'];
    	$smtp_host                     = $row['smtp_host'];
        $smtp_auth                     = $row['smtp_auth'];
    	$smtp_secure                   = $row['smtp_secure'];
    	$smtp_port                     = $row['smtp_port'];
    	$smtp_username                 = $row['smtp_username'];
    	$smtp_password                 = $row['smtp_password'];
    }

    $select_social = $mysqli->query("SELECT * FROM $table_social");
    while($row = $select_social->fetch_assoc()) {
    	$social_facebook               = $row['facebook'];
    	$social_twitter                = $row['twitter'];
    	$social_google                 = $row['google'];
    	$social_instagram              = $row['instagram'];
    	$social_github                 = $row['github'];
    	$social_tumblr                 = $row['tumblr'];
    	$social_pinterest              = $row['pinterest'];
    }

    //Getting the language file
    if(empty($_COOKIE['language'])) {
        $get_lang = $mysqli->query("SELECT * FROM $table_language WHERE path='$language'");
        while($lang = $get_lang->fetch_assoc()) {
            setcookie("language", $lang['name'], time()+3600*24*365*10, '/');
            require_once dirname(__FILE__) . '/language/' . $lang['path'];
            $language_file = $lang['path'];
        }  
    }
    else
    {
        $cookie_language = mysqli_real_escape_string($mysqli, $_COOKIE['language']);

        $get_lang = $mysqli->query("SELECT * FROM $table_language WHERE name='$cookie_language'");
        if($get_lang->num_rows == 1) {
            while($lang = $get_lang->fetch_assoc()) {
                require_once dirname(__FILE__) . '/language/' . $lang['path'];
                $language_file = $lang['path'];
            }
        }
        else
        {
            require_once dirname(__FILE__) . '/language/' . $language;
            $language_file = $language;
        }
    }
    
    //Get background
    $sql_bg = $mysqli->query("SELECT * FROM $table_backgrounds ORDER BY RAND() LIMIT 1");
    while($row = $sql_bg->fetch_assoc()) {
        $background = $row['src'];
        $back_url = $row['url'];
    }

    $plugin_sidebar_tabs    = '';
    $plugin_pages;
    $get_plugins = $mysqli->query("SELECT * FROM $table_plugins WHERE status='ready'");
    while($plugin = $get_plugins->fetch_assoc()) {
        include_once dirname(__FILE__) . '/../plugins/' . $plugin['path'] . 'plugin.php';
    }
    if(!$check_themes_table) {
        //Nop
    }
    else
    {
        $get_themes = $mysqli->query("SELECT * FROM $table_themes WHERE status='ready'");
        while($theme = $get_themes->fetch_assoc()) {
            $theme_path = 'resources/themes/' . $theme['path'];
        }
    }
}

//This function will handle all the emails that will be sent.
if (!function_exists('emailSender')) {
    function emailSender($mail_body,$mail_subject,$email_receiver,$reply_to_email = '')
    {
        //Getting some variables
        global $email_server, $mail, $smtp_host, $smtp_auth, $smtp_secure, $smtp_port, $smtp_username, $smtp_password, $email_from_email, $email_from_name;

        if($email_server == 'SMTP') {
            //Sending the email to the receivers of the file
            $mail             = new PHPMailer();

            $mail->IsSMTP(); // telling the class to use SMTP
            $mail->Host       = $smtp_host;
            $mail->SMTPAuth   = $smtp_auth;
            $mail->SMTPSecure = $smtp_secure;                       
            $mail->Port       = $smtp_port;                   
            $mail->Username   = $smtp_username;  
            $mail->Password   = $smtp_password;           

            $mail->From = $email_from_email;
            $mail->FromName = $email_from_name;

            if(!empty($reply_to_email)) {
                $mail->AddReplyTo($reply_to_email, '');
            }
            else
            {
                $mail->AddReplyTo($email_from_email, $email_from_name);
            }

            $mail->Subject    = $mail_subject;

            $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
            
            $mail->CharSet = 'UTF-8';

            $mail->MsgHTML($mail_body);
             
            $mail->AddAddress($email_receiver);

            if(!$mail->Send()) {
                error_log($mail->ErrorInfo);
            }
        }
        else
        {
            //Sending the email to the receivers of the file
            $mail             = new PHPMailer();       

            $mail->From = $email_from_email;
            $mail->FromName = $email_from_name;

            if(!empty($reply_to_email)) {
                $mail->AddReplyTo($reply_to_email, '');
            }
            else
            {
                $mail->AddReplyTo($email_from_email, $email_from_name);
            }

            $mail->Subject    = $mail_subject;

            $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
            
            $mail->CharSet = 'UTF-8';
            
            $mail->MsgHTML($mail_body);
             
            $mail->AddAddress($email_receiver);

            if(!$mail->Send()) {
                error_log($mail->ErrorInfo);
            }
        }
    }
}

// Get email templates functions
if (!function_exists('requestTemplate')) {
    function requestTemplate($template_type, $set_lang) 
    {
        global $mysqli, $table_templates;
        //Get sender templates
        if($template_type == 'sender') {
            $select_sender = $mysqli->query("SELECT * FROM $table_templates WHERE type='sender' AND lang='$set_lang'");
            while($row = $select_sender->fetch_assoc()) {
                $mail_body = $row['msg'];
            }
        }
        elseif($template_type == 'sender_subject') {
            $select_sender_subject = $mysqli->query("SELECT * FROM $table_templates WHERE type='sender_subject' AND lang='$set_lang'");
            while($row = $select_sender_subject->fetch_assoc()) {
                $mail_subject = $row['msg'];
            }
        }
        //Get receiver templates
        elseif($template_type == 'receiver') {
            $select_receiver = $mysqli->query("SELECT * FROM $table_templates WHERE type='receiver' AND lang='$set_lang'");
            while($row = $select_receiver->fetch_assoc()) {
                $mail_body = $row['msg'];
            }
        }
        elseif($template_type == 'receiver_subject') {
            $select_receiver_subject = $mysqli->query("SELECT * FROM $table_templates WHERE type='receiver_subject' AND lang='$set_lang'");
            while($row = $select_receiver_subject->fetch_assoc()) {
                $mail_subject = $row['msg'];
            }
        }
        //Get destroyed templates
        elseif($template_type == 'destroyed') {
            $select_destroyed = $mysqli->query("SELECT * FROM $table_templates WHERE type='destroyed' AND lang='$set_lang'");
            while($row = $select_destroyed->fetch_assoc()) {
                $mail_body = $row['msg'];
            }
            
        }
        elseif($template_type == 'destroyed_subject') {
            $select_destroyed_subject = $mysqli->query("SELECT * FROM $table_templates WHERE type='destroyed_subject' AND lang='$set_lang'");
            while($row = $select_destroyed_subject->fetch_assoc()) {
                $mail_subject = $row['msg'];
            }
        }
        //Get downloaded templates.
        elseif($template_type == 'downloaded') {
            $select_downloaded = $mysqli->query("SELECT * FROM $table_templates WHERE type='downloaded' AND lang='$set_lang'");
            while($row = $select_downloaded->fetch_assoc()) {
                $mail_body = $row['msg'];
            }
        }
        elseif($template_type == 'downloaded_subject') {
            $select_downloaded_subject = $mysqli->query("SELECT * FROM $table_templates WHERE type='downloaded_subject' AND lang='$set_lang'");
            while($row = $select_downloaded_subject->fetch_assoc()) {
                $mail_subject = $row['msg'];
            }
        }

        if(isset($mail_body)) 
        {
            $return_template = '
            <html xmlns="http://www.w3.org/1999/xhtml" style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;">
                <head style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;">
                    <meta charset="utf-8" style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;"> 
                </head>
                <body bgcolor="#f6f6f6" style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;-webkit-font-smoothing: antialiased;-webkit-text-size-adjust: none;height: 100%;width: 100%!important;">
                    <table class="body-wrap" style="margin: 0;padding: 20px;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;width: 100%;">
                        <tr style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;">
                            <td class="container" bgcolor="#FFFFFF" style="margin: 0 auto!important;padding: 20px;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;border: 1px solid #f0f0f0;display: block!important;max-width: 600px!important;clear: both!important;">
                                <!-- content -->
                                <div class="content" style="margin: 0 auto;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;max-width: 600px;display: block;">
                                <table style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;width: 100%;">
                                    <tr style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;">
                                        <td style="margin: 0;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;">
                                            <div class="content" style="margin: 0 auto;padding: 0;font-family: &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;font-size: 100%;line-height: 1.6;max-width: 600px;display: block;">' . nl2br($mail_body) . '</div>
                                        </td>
                                    </tr>
                                </table>
                                </div>
                                <!-- /content -->
                            </td>
                        </tr>
                    </table>
                </body>
            </html>';
        }
        elseif(isset($mail_subject)) 
        {
            $return_template = $mail_subject;
        }

        return $return_template;
    }
}

// Search in multidimensional array.
function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}