<?php
// initial constants
define('ADMIN_SELECTED_PAGE', 'plugins');

// includes and security
include_once('../../../core/includes/master.inc.php');
include_once(DOC_ROOT . '/' . ADMIN_FOLDER_NAME . '/_local_auth.inc.php');

// load plugin details
$pluginId = (int) $_REQUEST['id'];
$plugin   = $db->getRow("SELECT * FROM plugin WHERE id = " . (int) $pluginId . " LIMIT 1");
if (!$plugin)
{
    adminFunctions::redirect(ADMIN_WEB_ROOT . '/plugin_manage.php?error=' . urlencode('There was a problem loading the plugin details.'));
}
define('ADMIN_PAGE_TITLE', $plugin['plugin_name'] . ' Plugin Settings');

// plugin object for functions
$pluginObj = pluginHelper::getInstance('ftpupload');

// connection types
$connectionTypes = array();
$connectionTypes['cpanel'] = 'cPanel (for WHM/cPanel servers only)';

// prepare variables
$plugin_enabled             = (int) $plugin['plugin_enabled'];
$connection_type            = 'cpanel';
$connection_cpanel_host     = _CONFIG_SITE_HOST_URL;
$connection_cpanel_user     = '';
$connection_cpanel_password = '';
$home_dir_path              = 'public_html/';
$ftp_account_quota          = 2000;
$paid_only                  = 0;
$show_ftp_tab               = 0;

// load existing settings
if (strlen($plugin['plugin_settings']))
{
    $plugin_settings = json_decode($plugin['plugin_settings'], true);
    if ($plugin_settings)
    {
        $connection_type            = $plugin_settings['connection_type'];
        $connection_cpanel_host     = $plugin_settings['connection_cpanel_host'];
        $connection_cpanel_user     = $plugin_settings['connection_cpanel_user'];
        $connection_cpanel_password = $plugin_settings['connection_cpanel_password'];
        $home_dir_path              = $plugin_settings['home_dir_path'];
        $ftp_account_quota          = $plugin_settings['ftp_account_quota'];
        $paid_only                  = $plugin_settings['paid_only'];
        $show_ftp_tab               = $plugin_settings['show_ftp_tab'];
    }
}

// handle page submissions
if (isset($_REQUEST['submitted']))
{
    // get variables
	$plugin_enabled             = (int)$_REQUEST['plugin_enabled'];
    $connection_type        = $_REQUEST['connection_type'];
    $connection_cpanel_host = strtolower(trim($_REQUEST['connection_cpanel_host']));
    $connection_cpanel_host = str_replace(array("http://", "https://", "www."), "", $connection_cpanel_host);
    $connection_cpanel_user     = trim($_REQUEST['connection_cpanel_user']);
    $connection_cpanel_password = trim($_REQUEST['connection_cpanel_password']);
    $home_dir_path              = trim($_REQUEST['home_dir_path']);
    if (substr($home_dir_path, strlen($home_dir_path) - 1, 1) != '/')
    {
        $home_dir_path = $home_dir_path . '/';
    }
    if (substr($home_dir_path, 0, 1) == '/')
    {
        $home_dir_path     = substr($home_dir_path, 1, strlen($home_dir_path) - 1);
    }
    $home_dir_path     = '/' . $home_dir_path;
    $ftp_account_quota = (int) trim($_REQUEST['ftp_account_quota']);
    $paid_only         = isset($_REQUEST['paid_only']) ? (int) $_REQUEST['paid_only'] : 0;
    $show_ftp_tab      = isset($_REQUEST['show_ftp_tab']) ? (int) $_REQUEST['show_ftp_tab'] : 0;

    // validate submission
    if (_CONFIG_DEMO_MODE == true)
    {
        adminFunctions::setError(adminFunctions::t("no_changes_in_demo_mode"));
    }

    // test ftp path
    if (strlen($home_dir_path) == 0)
    {
        adminFunctions::setError(adminFunctions::t("set_the_ftp_path", "Please set the path to store ftp accounts."));
    }

    // update the settings
    if (adminFunctions::isErrors() == false)
    {
        // compile new settings
        $settingsArr = array();
        $settingsArr['connection_type']            = $connection_type;
        $settingsArr['connection_cpanel_host']     = $connection_cpanel_host;
        $settingsArr['connection_cpanel_user']     = $connection_cpanel_user;
        $settingsArr['connection_cpanel_password'] = $connection_cpanel_password;
        $settingsArr['home_dir_path']              = $home_dir_path;
        $settingsArr['paid_only']                  = $paid_only;
        $settingsArr['ftp_account_quota']          = $ftp_account_quota;
        $settingsArr['show_ftp_tab']               = $show_ftp_tab;
        $settings                                  = json_encode($settingsArr);

        // update the user
        $dbUpdate = new DBObject("plugin", array("plugin_enabled", "plugin_settings"), 'id');
        $dbUpdate->plugin_enabled = $plugin_enabled;
        $dbUpdate->plugin_settings = $settings;
        $dbUpdate->id = $pluginId;
        $dbUpdate->update();

        adminFunctions::redirect(ADMIN_WEB_ROOT . '/plugin_manage.php?se=1');
    }
}

if(_CONFIG_DEMO_MODE == true)
{
    $connection_cpanel_password = '****************';
}

// page header
include_once(ADMIN_ROOT . '/_header.inc.php');
?>

<script>
    $(function() {
        // formvalidator
        $("#pluginForm").validationEngine();
    });
</script>

<div class="row clearfix">
    <div class="col_12">
        <div class="sectionLargeIcon" style="background: url(../assets/img/icons/128px.png) no-repeat;"></div>
        <div class="widget clearfix">
            <h2>Plugin Settings</h2>
            <div class="widget_inside">
                <?php echo adminFunctions::compileNotifications(); ?>
                <form method="POST" action="settings.php" name="pluginForm" id="pluginForm" autocomplete="off">
                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>Plugin State</h3>
                            <p>Whether the ftp upload plugin is available.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix alt-highlight">
                                    <label>Plugin Enabled:</label>
                                    <div class="input">
                                        <select name="plugin_enabled" id="plugin_enabled" class="medium validate[required]">
                                            <?php
                                            $enabledOptions = array(0 => 'No', 1 => 'Yes');
                                            foreach ($enabledOptions AS $k => $enabledOption)
                                            {
                                                echo '<option value="' . $k . '"';
                                                if ($plugin_enabled == $k)
                                                {
                                                    echo ' SELECTED';
                                                }
                                                echo '>' . $enabledOption . '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>FTP Connection Type</h3>
                            <p>Which FTP method to use.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix alt-highlight">
                                    <label>Method:</label>
                                    <div class="input">
                                        <select name="connection_type" id="connection_type">
                                            <?php
                                            foreach ($connectionTypes AS $k => $connectionType)
                                            {
                                                echo '<option value="' . $k . '"';
                                                if ($k == $connection_type)
                                                {
                                                    echo ' SELECTED';
                                                }
                                                echo '>' . $connectionType . '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="clearfix">
                                    <label>cPanel Domain Name:</label>
                                    <div class="input"><input id="connection_cpanel_host" name="connection_cpanel_host" type="text" class="large validate[required]" value="<?php echo adminFunctions::makeSafe($connection_cpanel_host); ?>"/></div>
                                </div>
                                <div class="clearfix alt-highlight">
                                    <label>cPanel Username:</label>
                                    <div class="input"><input id="connection_cpanel_user" name="connection_cpanel_user" type="text" class="large validate[required]" value="<?php echo adminFunctions::makeSafe($connection_cpanel_user); ?>"/></div>
                                </div>
                                <div class="clearfix">
                                    <label>cPanel Password:</label>
                                    <div class="input"><input id="connection_cpanel_password" name="connection_cpanel_password" type="password" class="large validate[required]" value="<?php echo adminFunctions::makeSafe($connection_cpanel_password); ?>"/></div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>FTP Account Details</h3>
                            <p>Storage path &amp; FTP account quota. The FTP quota does not affect their file hosting account, it's only a limit on the total file size they can upload via FTP at once.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix alt-highlight">
                                    <label>FTP Home Dir Path:</label>
                                    <div class="input"><input id="home_dir_path" name="home_dir_path" type="text" class="large validate[required]" value="<?php echo adminFunctions::makeSafe($home_dir_path); ?>"/>&nbsp;&nbsp;relative to your cPanel account root</div>
                                </div>
                                <div class="clearfix">
                                    <label>Account Quota: (in MB)</label>
                                    <div class="input"><input id="ftp_account_quota" name="ftp_account_quota" type="text" class="large validate[required]" value="<?php echo adminFunctions::makeSafe($ftp_account_quota); ?>"/>&nbsp;&nbsp;MB, 0 for unlimited</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix col_12">
                        <div class="col_4">
                            <h3>Display Settings</h3>
                            <p>Which types of accounts can have access. How to show the tab on the homepage. Whether to enable folder support via FTP.</p>
                        </div>
                        <div class="col_8 last">
                            <div class="form">
                                <div class="clearfix alt-highlight">
                                    <label>Only For Paid Users:</label>
                                    <div class="input"><input id="paid_only" name="paid_only" type="checkbox" value="1" <?php echo($paid_only == 1) ? 'CHECKED' : ''; ?>/>&nbsp;&nbsp;if ticked, only paid users will have the FTP upload option</div>
                                </div>
                                <div class="clearfix">
                                    <label>Always Show FTP Tab:</label>
                                    <div class="input"><input id="show_ftp_tab" name="show_ftp_tab" type="checkbox" value="1" <?php echo($show_ftp_tab == 1) ? 'CHECKED' : ''; ?>/>&nbsp;&nbsp;non-users will see the FTP tab on the homepage (prompt to register on click)</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="clearfix col_12">
                        <div class="col_4">&nbsp;</div>
                        <div class="col_8 last">
                            <div class="clearfix">
                                <div class="input no-label">
                                    <input type="submit" value="Submit" class="button blue">
                                    <input type="reset" value="Reset" class="button grey">
                                </div>
                            </div>
                        </div>
                    </div>

                    <input name="submitted" type="hidden" value="1"/>
                    <input name="id" type="hidden" value="<?php echo $pluginId; ?>"/>
                </form>
            </div>
        </div>   
    </div>
</div>

<?php
include_once(ADMIN_ROOT . '/_footer.inc.php');
?>