<?  // PHPmyGallery - backend core - create htaccess file if not exists

/* File description:

This file creates a .htaccess file for you
(needs the adminmenu directory CHMODed to 777).

### What this script does:
###
### Included from: adminmenu.index.php
### |
### | (Uses templates from admin/htaccess)
### +-  (User enters username and password, Write file, Display Result)
###
*/ 

// Get POST variables
$username  = $_POST['ht_username'];
$passwd1   = $_POST['ht_passwd1'];
$passwd2   = $_POST['ht_passwd2'];
$encType   = $_POST['ht_enctype'];

// Get directory for htaccess file
$directory  = $_SERVER['SCRIPT_FILENAME'];
$directory  = str_replace('index.php','',$directory);

// Init TPL vars
$pktpl_vars['text_formelementSize']      = 25;
$pktpl_vars['text_formelementMaxlength'] = 100;

// Display `.htaccess file saved` message
$passwords_empty = ($passwd1==''||$passwd2==''||$passwd1==' '||$passwd2==' ');
$passwords_equal = ($passwd1==$passwd2);
if ($saveit=='yes'&&$passwords_equal&&!$passwords_empty) { // $saveit is set from backend.core.php
    
    // Assign template variables
    $pktpl_vars['text_dirsfromroot'] = $directory;
    
    // Assign file contents to variables
    $ht_filename = '.htaccess';
    $ht_passwd_filename = '.htpasswd01';
    if ($encType=='Use crypt')
        $passwd2 = crypt($passwd2,substr($passwd2,0,2));
    $htpasswd01 = $username.':'.$passwd2;
    $htaccess = pktpl('adminmenu/htaccess_create/htaccessFile.txt'); // pktpl works with txt files too
    $htaccess = str_replace("\n\n","\n",$htaccess);
    
    // Write .ht~ files
    @chmod('./',0777);
    $fd = fopen($ht_passwd_filename,'w+'); // open file for writing (replace existing file if any)
    fwrite($fd,$htpasswd01); // write text
    fclose($fd); // close file
    
    $fd = fopen($ht_filename,'w+'); // open file for writing (replace existing file if any)
    fwrite($fd,$htaccess); // write text
    fclose($fd); // close file
    
    // Make templates
    $pktpl_vars['text_headline']                  = $l['htaccess_file_written'];
    $pktpl_vars['template_admin_successBoxLinks'] = $l['htaccess_file_written_help'];
    $pktpl_vars['template_admin_table']           = pktpl('adminmenu/admin_successBox.html');
} // if
// Display form to input username and password
else {
    // Form elements to use
    $htaccessform_items = array(
        'ht_username'=>array( 'text',    '',$l['username_help']),
        'ht_passwd1'=>array(  'password','',$l['enter_password_help']),
        'ht_passwd2'=>array(  'password','',$l['reenter_password_help']),
        'ht_enctype'=>array(  'select',  'Use crypt,Windows (no encryption)',$l['passwordcrypt_help']),
    );
    
    // Processing form elements
    $tmp = '';
    foreach($htaccessform_items as $k=>$v) {
        $value = (isset($_POST[$k])&&$v[0]!='password') ? $_POST[$k]: $v[1];
        
        // Assign template vars
        $pktpl_vars['text_help'] = $v[2];
        $pktpl_vars['text_formelementName'] = $k;
        $pktpl_vars['text_formelementValue'] = $value;
        
        // Different template for `text`, `select` and `password` elements
        if ($v[0]=='select') {
            $arr = explode(',',$v[1]);
            $pktpl_vars['template_forminputSelectOptions'] = '';
            foreach($arr as $option) {
                $pktpl_vars['text_optionCaption'] = $option;
                $pktpl_vars['text_optionValue']   = $option;
                $pktpl_vars['template_forminputSelectOptions'] .= pktpl('all_forminputSelectOptions.html');
            } // foreach
            $pktpl_vars['text_selectName'] = $k;
            $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputSelect.html');
        } // if
        elseif ($v[0]=='text') {
            $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputText.html');
        } // elseif
        elseif ($v[0]=='password') {
            $pktpl_vars['template_all_forminputText'] = pktpl('all_forminputPassword.html');
        } // elseif
        $tmp .= pktpl('adminmenu/htaccess_create/TableRows.html');
        
    } // end foreach();
    $pktpl_vars['template_admin_htaccessTableRows'] = $tmp;
    
    // Assign template vars
    $pktpl_vars['text_headline'] = $l['htaccess_headline'];
    if($saveit=='yes'&&!$passwords_equal||$saveit=='yes'&&$passwords_empty)
        $pktpl_vars['text_errortext'] = $l['passwords_not_the_same'];
    else
        $pktpl_vars['text_errortext'] = '';
    
    // Make templates
    $pktpl_vars['template_admin_htaccessTableHelprow']    = pktpl('adminmenu/htaccess_create/TableHelprow.html');
    $pktpl_vars['template_admin_htaccessTableHeaders']    = pktpl('adminmenu/htaccess_create/TableHeaders.html');
    $pktpl_vars['template_admin_htaccessTableHeadline']   = pktpl('adminmenu/htaccess_create/TableHeadline.html');
    $pktpl_vars['template_admin_htaccessTableFormsubmit'] = pktpl('adminmenu/htaccess_create/TableFormsubmit.html');
    
    $pktpl_vars['template_admin_table'] = pktpl('adminmenu/htaccess_create/Table.html');
} // else

?>