<?php
/**
 * Main index page for guestbook. 
 * 
 * This application is designed for website generated by RVSiteBuilder.
 * Unauthorized copy of the script is prohibited.
 * 
 * @version 1.00
 * @author pairote@rvglobalsoft.com
 * @copyright Copyright &copy; http://www.rvglobalsoft.com/index.php/rvsitebuilder/sitebuilderbilling/action/terms
 * @package rvsitebuilder
 */
 require_once('include.php');
 
// Disable surbl, captcha requirement
$aConfig['captcha'] = 0;
$aConfig['surbl_query_spam_protection'] = 0;
$aConfig['field_ip'] = 0;


///fixed require_once cannot ini_set('include_path') : apiruk
if (defined('RVSLIB_PEAR_PATH')) {
	require_once(RVSLIB_PEAR_PATH . 'HTML/QuickForm.php');
	require_once (RVSLIB_PEAR_PATH . "HTML/QuickForm/Renderer/QuickHtml.php");
} else {
	require_once('HTML/QuickForm.php');
	require_once ("HTML/QuickForm/Renderer/QuickHtml.php");
}
///END:apiruk

// Instantiate the HTML_QuickForm object
$form = new HTML_QuickForm('guestForm');
// get our render
$renderer =& new HTML_QuickForm_Renderer_QuickHtml();
 
 // create the elements
require_once('shareform.php');
createElements($form, $aConfig, $aLang);
_createElements($form, $aConfig, $aLang);

// set their values
_setValues($form, $aConfig, $aLang);

// Do the magic of creating the form.  NOTE: order is important here: this must
// be called after creating the form elements, but before rendering them.
$form->accept($renderer);

// Try to validate a form
$aError = array(); 
if(isset($_POST['post'])) {
    $valid = true;
    // QuickForm validation
    $valid= ($form->validate() && $valid ===true)? true:false;
    
    // Validate admin password
    $valid= (_validateAdminPass($aError, $aLang, $aConfig) && $valid ===true)? true:false;
}

// Output the form
if(!isset($valid) || !$valid) {
    // Make our form table using some of the widget functions.
    $registeredOption = $form->toArray();
    $data = _pageHead($aConfig, $aLang,$aError);    
    $data .= _rendererFormTable($renderer, $registeredOption, $aConfig, $aLang,$aError);    
    $data .= rendererFormTable($renderer, $registeredOption, $aConfig, $aLang,$aError);
    
    // Wrap the form and any remaining elements (i.e. hidden elements) into the form tags.
    echo '<div align="center">';
    echo $renderer->toHtml($data);
    echo '</div>';
}
else {
    # If the form validates then freeze the data
    $form->freeze();
    
    // Insert data into db text file
    $form->process('process_data', false);
    showMessage($aConfig, $aLang);
}

// end edit page
function _createElements(&$form, &$aConfig, $aLang)
{
    $form->addElement('password', 'comfirmPassword', null, array('size' => 20, 'maxlength' => 255));
    $form->applyFilter('comfirmPassword', 'trim');
    $form->addRule('comfirmPassword', $aLang['admin password warning'], 'required', null, 'client');    
    
    $key = (isset($_GET['key']))? $_GET['key'] : $_POST['key'];    
    $form->addElement('hidden', 'key', $key);
    $form->addElement('hidden', 'date');     
    $form->addElement('hidden', 'ip');     
    $form->addElement('hidden', 'post');         
}

function _setValues(&$form, $aConfig, $aLang)
{
    // Load data from the guestbook file and set back to the form
    $dataFile = file(SCRIPT_PATH . 'data/dataAsRaw.txt');
    $key = (isset($_GET['key']))? $_GET['key'] : $_POST['key'];    
    foreach ($dataFile as $line) { 
        if (ereg("^$key", $line)) {
			$line = preg_replace('/\n$/', '', $line);        
            list($id, $date, $name, $email, $location, $website, $icq, $aim, $yahoo, $skype, $msn, $msg, $ip, $option1, $option2, $option3, $option4, $option5) = explode("\t", $line);
            break;
        }
    }            
    
    $msg = str_replace("<br />", "\n", $msg);
    $aDefault = array('field_name' => $name,
                               'field_email' => $email,
                               'field_location' => $location,
                               'field_website' => $website,
                               'field_icq' => $icq,
                               'field_aim' => $aim,
                               'field_yahoo' => $yahoo,
                               'field_skype' => $skype,
                               'field_msn' => $msn,
                               'field_msg' => $msg,
                               'field_optional1' => $option1,
                               'field_optional2' => $option2,
                               'field_optional3' => $option3,
                               'field_optional4' => $option4,
                               'field_optional5' => $option5,
                               );
                               
                           		
    $form->setDefaults($aDefault);    
   
    $constants = array('comfirmPassword' => '', 
                                 'key' => $key,
                                 'date' => $date,
                                 'ip' => $ip,
                                 'post' => '2');
    $form->setConstants($constants);    
    $form->setJsWarnings ($aLang['Pre JS warning'], $aLang['Post JS warning']);
}
 
function process_data($values) 
{    
    $values['field_msg'] = nl2br($values['field_msg']);

    // Remove /t characters    
    $values = str_replace("\t", "", $values);
    $values = str_replace("\r", "", $values);    
    $values = str_replace("\n", "", $values);
        
    $key = $values['key'];
    $values['field_name'] = (isset($values['field_name'])) ? $values['field_name'] : '';       
    $values['field_email'] = (isset($values['field_email'])) ? $values['field_email'] : '';    
    $values['field_location'] = (isset($values['field_location'])) ? $values['field_location'] : '';
    $values['field_website'] = (isset($values['field_website'])) ? $values['field_website'] : '';
    $values['field_icq'] = (isset($values['field_icq'])) ? $values['field_icq'] : '';
    $values['field_aim'] = (isset($values['field_aim'])) ? $values['field_aim'] : '';
    $values['field_yahoo'] = (isset($values['field_yahoo'])) ? $values['field_yahoo'] : '';
    $values['field_email'] = (isset($values['field_email'])) ? $values['field_email'] : '';
    $values['field_skype'] = (isset($values['field_skype'])) ? $values['field_skype'] : '';
    $values['field_msn'] = (isset($values['field_msn'])) ? $values['field_msn'] : '';
    $values['field_msg'] = (isset($values['field_msg'])) ? $values['field_msg'] : '';       
    $values['field_ip'] = (isset($values['ip'])) ? $values['ip'] : '';        
    $values['field_optional1'] = (isset($values['field_optional1'])) ? $values['field_optional1'] : '';
    $values['field_optional2'] = (isset($values['field_optional2'])) ? $values['field_optional2'] : '';
    $values['field_optional3'] = (isset($values['field_optiona3'])) ? $values['field_optional3'] : '';
    $values['field_optional4'] = (isset($values['field_optional4'])) ? $values['field_optional4'] : '';
    $values['field_optional5'] = (isset($values['field_optional5'])) ? $values['field_optional5'] : '';

    $writeData = $values['key'];
    $writeData .= "\t" . $values['date'];
    $writeData .= "\t" . $values['field_name'];
    $writeData .= "\t" . $values['field_email'];    
    $writeData .= "\t" . $values['field_location'];    
    $writeData .= "\t" . $values['field_website'];    
    $writeData .= "\t" . $values['field_icq'];
    $writeData .= "\t" . $values['field_aim'];    
    $writeData .= "\t" . $values['field_yahoo'];        
    $writeData .= "\t" . $values['field_skype'];    
    $writeData .= "\t" . $values['field_msn'];    
    $writeData .= "\t" . $values['field_msg'];    
    $writeData .= "\t" . $values['field_ip'];
    $writeData .= "\t" . $values['field_optional1'];
    $writeData .= "\t" . $values['field_optional2'];
    $writeData .= "\t" . $values['field_optional3'];
    $writeData .= "\t" . $values['field_optional4'];
    $writeData .= "\t" . $values['field_optional5'];                    
    $writeData .= "\n" ;   
    
    $dataFile = file(SCRIPT_PATH . 'data/dataAsRaw.txt');  
    $file = fopen(SCRIPT_PATH . 'data/dataAsRaw.txt', 'w');
    foreach ($dataFile as $line) { 
        if (ereg("^$key", $line)) {
            fwrite($file, $writeData);
        }
        else {       
            fwrite($file, $line);
        }
    }
    fclose($file);          
    
}

function _rendererFormTable(&$renderer, $registeredOption, $aConfig, $aLang, $aError)
{  
     $data = '';
    foreach ($registeredOption['elements'] as $element => $aValue) {    
        if ($aValue['name'] == 'comfirmPassword') {
            $_noneed = $renderer->elementToHtml($aValue['name']);            
            if(isset($aError['comfirmPassword'])) {
            	$data .= $aError['comfirmPassword'];
            }
            $data .= '<p align="center">';
            $data .= $aLang['admin password'] . '<span style="color: rgb(255, 0, 0);">*</span>:';
            $data .= '<br><input type="password" name="comfirmPassword" value="">';
            $data .= '</p>';
            break;
        }
    }
  
    return $data;	
}

function _pageHead($aConfig, $aLang,$aError)
{
        $data = "<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"borderoutside\" width=\"90%\" align=\"center\">\n" . 
                "    <tr>\n" . 
                "        <td align=\"right\">" .
//                "        <img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/admin.gif\" alt=\"\" width=\"12\" height=\"12\" /> \n" . 
//                "        <a href=\"?page=admin\" class=\"signlink\">Administration</a> | \n" .
                "        <img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/signicn.gif\" alt=\"\" width=\"12\" height=\"12\" /> \n" . 
                "        <a href=\"?page=list\" class=\"signlink\">View the Guestbook</a>\n" . 
                "        </td>\n" . 
                "    </tr>\n" . 
                "    <tr>\n" . 
                "        <td><b>" . strftime($aConfig['date_format']) . "</b><br />\n" . 
                "        </td>\n" . 
                "    </tr>\n" . 
                "    <tr>\n" . 
                "        <td height=\"10\"></td>\n" . 
                "    </tr>\n" .
                "    <tr>\n" . 
                "        <td>\n" . 
                "        <table cellpadding=\"1\" cellspacing=\"1\" width=\"100%\">\n" . 
                "            <tr>\n" . 
                "                <td class=\"txtheadb\" align=\"left\">Edit the Guestbook:</td>\n" . 
                "            </tr>\n" . 
                "            <tr>\n" . 
                "                <td>";
        if (count($aError) > 0) {
            $data .= '<p align="center"><font color="red">';
            $data .= $aLang['Please fill in all the indicated fields and try again.'];
            $data .= '</font></p>';
        }                
        $data .="                 </td>\n" . 
                "            </tr>\n" .                 
                "            <tr>\n" . 
                "                <td class=\"bgtabcolor\" height=\"4\"></td>\n" . 
                "            </tr>\n" . 
                "            <tr>\n" . 
                "                <td class=\"bglinecolor\" height=\"1\"></td>\n" . 
                "            </tr>\n" . 
                "            <tr>\n" . 
                "                <td>\n" . 
                "                <table cellpadding=\"1\" cellspacing=\"1\" width=\"100%\">";   
    return $data;                
}

function _validateAdminPass(&$aError, $aLang, $aConfig)
{
   
    if (md5($_POST['comfirmPassword']) == $aConfig['apass'] )  {  
        $valid = true;
    }
    else {
        $aError['comfirmPassword'] = '<p align="center"><font color="red">' . $aLang['verify password failed'] . '</font></p>'; 
        $valid = false;
    }
    
    
    return $valid;	
}

function showMessage($aConfig, $aLang)
{
  echo "<table cellpadding=\"0\" cellspacing=\"0\" class=\"borderoutside\" width=\"90%\" align=\"center\">\n" . 
            "    <tr>\n" . 
            "        <td align=\"right\">" .
//            "        <img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/admin.gif\" alt=\"\" width=\"12\" height=\"12\" /> \n" . 
//            "        <a href=\"?page=admin\" class=\"signlink\">Administration</a> | \n" .
            "        <img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/signicn.gif\" alt=\"\" width=\"12\" height=\"12\" /> \n" . 
            "        <a href=\"?page=list\" class=\"signlink\">View the Guestbook</a>\n" . 
            "        </td>\n" . 
            "    </tr>\n" . 
            "    <tr>\n" . 
            "        <td><b>" . strftime($aConfig['date_format']) . "</b><br />\n" . 
            "        </td>\n" . 
            "    </tr>\n" . 
            "    <tr>\n" . 
            "        <td height=\"10\"></td>\n" . 
            "    </tr>\n" .
            "    <tr>\n" . 
            "        <td>\n" . 

            "        <table cellpadding=\"1\" cellspacing=\"1\" width=\"100%\">\n" . 
            "            <tr>\n" . 
            "                 <td class=\"bgtabcolor\" height=\"2\"><img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/spacer.gif\" alt=\"\" width=\"1\" height=\"2\" /></td>\n" . 
            "            </tr>\n" .             
            "            <tr>\n" . 
            "                <td class=\"txtheadb\">" . $aLang['Edited Guestbook'] . ":</td>\n" . 
            "            </tr>\n" . 
            "            <tr>\n" . 
            "                <td class=\"bgtabcolor\" height=\"4\"></td>\n" . 
            "            </tr>\n" . 
            "            <tr>\n" . 
            "                <td class=\"bglinecolor\" height=\"1\"></td>\n" . 
            "            </tr>\n" . 
            "            <tr>\n" . 
            "                <td></td>\n" . 
            "            </tr>" .
            "        </table>";     
              
        echo '<div align="center"><br /><br /><br />';        
        echo $aLang['Your comment was successfully updated.'];
        echo '<br /><a href="?page=list">' . $aLang['Click here to view your comment.'] . '</a>';
        echo '<br /><br /><br /></div>';
    
        echo '   </td>';
        echo '  </tr>'; 
        echo '</table>';    
}
  
?>