<?php

function current_url()
{
    $pageURL = 'http';
    if (is_ssl()) {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    $exp       = explode('/', $pageURL);
    $crap      = array_pop($exp);
    $base_path = implode('/', $exp);

    return str_replace('/setup', '', $base_path);
}

function is_ssl()
{
    if (isset($_SERVER['HTTPS'])) {
        if ('on' == strtolower($_SERVER['HTTPS']))
            return true;
        if ('1' == $_SERVER['HTTPS'])
            return true;
    } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
        return true;
    }

    return false;
}

function secure_folder($folder)
{
    // .htaccess file
    $htfile     = $folder . "/.htaccess";
    $fh         = fopen($htfile, 'w');
    $final_data = "# Generated by Zenbership.\n";
    $final_data .= "# IMPORTANT SECURITY FILE! DO NOT ALTER OR DELETE!\n";
    $final_data .= "# Zenbership: http://www.zenbership.com/\n";
    $final_data .= "AuthUserFile " . $folder . "/.htpasswd\n";
    $final_data .= "AuthName \"Content Off Limits\"\n";
    $final_data .= "AuthType Basic\n";
    $final_data .= "require valid-user";
    fwrite($fh, $final_data);
    fclose($fh);
    // .htpasswd file
    $htfile1 = $folder . "/.htpasswd";
    $content = md5(uniqid(rand(), true)) . ":" . crypt(md5(uniqid(rand(), true)) . md5(time() . rand(10000, 99999999))) . "\n";
    $fh      = fopen($htfile1, 'w');
    fwrite($fh, $content);
    fclose($fh);
}

?>