<?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');
    ///fixed require_once cannot ini_set('include_path') : apiruk
    if (defined('RVSLIB_PEAR_PATH')) {
    	require_once(RVSLIB_PEAR_PATH . 'Pager/Pager.php');
    } else {
    	require_once('Pager/Pager.php');
    }
	///END:apiruk
    $aData = array();
    if(is_file(SCRIPT_PATH . 'data/dataAsRaw.txt')){
        $aData = file(SCRIPT_PATH . 'data/dataAsRaw.txt');
    }
    $aData = array_reverse($aData);

   $params = array(
        'mode'      =>  'Sliding',
        'perPage'   =>    $aConfig['comment_per_page'],
        'delta'     =>    1,
        'itemData'  =>    $aData,
        'prevImg'   =>    'Previous' . ' <<',
        'nextImg'   =>  'Next' . '>> ',
        'linkClass' => 'pagelink',
        #'curPageLinkClassName' => '',
        'spacesBeforeSeparator' => 1,
        'spacesAfterSeparator' => 1,
    );
        
    $pager = & Pager::factory($params);
    $aShowData  = $pager->getPageData();
    $pager->getLinks();
    $showPager = $pager->links;      
    $totalItems = $pager->numItems();
    list($startOffset, $endOffset) = $pager->getOffsetByPageId();
    
    showTableHead($params['perPage'], $totalItems, $showPager, $aConfig, $aLang);    
    $count=0;
    
    foreach ($aShowData as $line) {
        list($id, $date, $name, $email, $location, $website, $icq, $aim, $yahoo, $skype, $msn, $msg, $ip, $option1, $option2, $option3, $option4, $option5) = explode("\t", $line);
        $option5 = preg_replace('/\n/i', '' ,$option5);
        $tabId = $count%2 + 1;
        $currentId = $totalItems - ($startOffset + $count) + 1;
        
        $name =  htmlspecialchars($name);
        $email =  htmlspecialchars($email);      
        $location =  htmlspecialchars($location);      
        $website =  htmlspecialchars($website);      
        $icq =  htmlspecialchars($icq);      
        $aim =  htmlspecialchars($aim);      
        $yahoo =  htmlspecialchars($yahoo);      
        $skype =  htmlspecialchars($skype);      
        $msn =  htmlspecialchars($msn);      
        $ip =  htmlspecialchars($ip);
        $msg = str_replace("<br />", "\n", $msg);
        $msg = htmlspecialchars($msg);
        $msg = nl2br($msg);
          
        echo "            <tr class=\"bgcolortab" . $tabId . "\">\n" . 
                "                <td valign=\"top\" class=\"margintab\"><b><font class=\"choicecolor\">$currentId</font></b> $name\n";
        
        if($aConfig['field_email'] && $email) { 
            echo '<br />' . mailto_encode($email, '', 'signlink');
        }
        if($aConfig['field_website'] && $website) { 
            if(!preg_match("/^http/i", $website)) {
            	$website = 'http://' . $website;
            }
            
            echo "<br /><a href=\"" . $website . "\" class=\"signlink\" target=\"_blank\">" . $website . "</a>\n";
        }        
        
        if($aConfig['field_location'] && $location) {         
            echo "<br />Location: $location\n";
        }

        if($aConfig['field_ip'] && $ip) { 
            echo "<br />IP: $ip\n";
        }       
        
        echo "<br />\n";
                
        if($aConfig['field_icq'] && $icq) {            
            echo ' <a href="http://web.icq.com/whitepages/message_me?uin=' . $icq . '&action=message" title="ICQ with ' . $icq . '" target="_blank">';
            echo "<img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/field_icq.gif\" alt=\"\" width=\"15\" height=\"16\" border=\"0\" />";
            echo '</a>';
        }
        
        if($aConfig['field_aim'] && $aim) {                
            echo ' <a href="aim:goim?screenname=' . $aim . '" title="iChat/AIM with ' . $aim . '" target="_blank">';
            echo "<img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/field_aim.gif\" alt=\"\" width=\"13\" height=\"16\" border=\"0\" />";
            echo '</a>';
        }
        if($aConfig['field_yahoo'] && $yahoo) {           
            echo ' <a href="ymsgr:sendIM?' . $yahoo . '" title="Yahoo! Messenger with ' . $yahoo . '" target="_blank">';
            echo "<img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/field_yahoo.gif\" alt=\"\" width=\"15\" height=\"17\" border=\"0\" />";
            echo '</a>';
        }
        if($aConfig['field_skype'] && $skype) {                           
            echo ' <a href="skype:' . $skype . '?call" title="Call ' . $skype . '!" target="_blank">';
            echo "<img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/field_skype.gif\" alt=\"\" width=\"16\" height=\"16\" border=\"0\" />";
            echo '</a>';
        }
        if($aConfig['field_msn'] && $msn) {                 
            $sEncmail = '';
            for($i=0; $i<strlen($msn); $i++) {
                $sEncmail .= "&#".ord(substr($msn,$i,1));
            } 
            
            echo ' <a href="http://members.msn.com/' . $sEncmail . '" title="MSN with ' . $sEncmail . '" target="_blank">';
            echo "<img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/field_msn.gif\" alt=\"\" width=\"15\" height=\"14\" border=\"0\" />";
            echo '</a>';
        }                
        
        echo "<br />\n";
                                
            echo "              </td>\n" . 
                "                <td valign=\"top\" class=\"margintab\">\n" . 
                "\n" . 
                "                <table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"color:#000000;\">\n" . 
                "                    <tr>\n" . 
                "                        <td><img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/pagetop.gif\" alt=\"\" width=\"6\" height=\"7\" /> " . 
                strftime($aConfig['date_format'], $date) . " <a href=\"?page=edit&key=" . $id . "\"><img\n" .                  
                "                            src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/pencil.gif\" alt=\"\" width=\"12\" height=\"12\" border=\"0\" title=\"edit\" /></a> " .
                "                        <a href=\"?page=delete&name=" . urlencode($name) . "&key=" . $id . "\"><img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/delete.gif\" alt=\"\"\n" . 
                "                            width=\"9\" height=\"9\" border=\"0\" title=\"delete\" /></a>";
            echo "                     </td>\n" . 
                "                    </tr>\n" . 
                "                    <tr>\n" . 
                "                        <td height=\"5\"></td>\n" . 
                "                    </tr>\n" . 
                "                    <tr>\n" . 
                "                        <td class=\"bglinecolor\" height=\"1\"></td>\n" . 
                "                    </tr>\n" . 
                "                    <tr>\n" . 
                "                        <td height=\"5\"></td>\n" . 
                "                    </tr>\n" . 
                "                    <tr>\n" . 
                "                        <td><img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/openc.gif\" alt=\"\" width=\"16\" height=\"14\" /></td>\n" . 
                "                    </tr>\n" . 
                "                    <tr>\n";
             if($aConfig['field_optional1'] || $aConfig['field_optional2'] || $aConfig['field_optional3'] 
                || $aConfig['field_optional4'] || $aConfig['field_optional5']) {  
                $msg .= '<br>';             	
             }                
             if($aConfig['field_optional1'] && $option1) {  
                $msg .= '<br>' . $aConfig['field_optional1_text'] . ": " .  $option1;
             }      
             if($aConfig['field_optional2']  && $option2) {  
                $msg .=  '<br>' . $aConfig['field_optional2_text'] . ": " .  $option2;
             }     
             if($aConfig['field_optional3']  && $option3) {  
                $msg .=  '<br>' . $aConfig['field_optional3_text'] . ": " .  $option3;
             }            
             if($aConfig['field_optional4'] && $option4) {  
                $msg .=  '<br>' . $aConfig['field_optional4_text'] . ": " .  $option4;
             }            
             if($aConfig['field_optional5'] && $option5 ) {  
                $msg .=  '<br>' . $aConfig['field_optional5_text'] . ": " .  $option5;
             } 
            $msg = insertSmileys($msg);                                                                   
            echo "                    <td class=\"margintab2\">" . nl2br(textWrap($msg, $aConfig['max_word_per_line'])) . "</td>\n";                
            echo "                </tr>\n" . 
                "                    <tr>\n" . 
                "                        <td align=\"right\"><img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/closec.gif\" alt=\"\" width=\"16\" height=\"14\" /></td>\n" . 
                "                    </tr>\n" . 
                "                </table>\n" . 
                "\n" . 
                "                </td>\n" . 
                "            </tr>\n" . 
                "            <tr>\n" . 
                "                <td colspan=\"2\"></td>\n" . 
                "            </tr>";
        $count++;
    }

# BBCodeParser
# bad words filter

    showTableFooter();
    if ($count ==0) {
        echo "<p>&nbsp;</p>";
        echo "<p>&nbsp;</p>";
        echo "<p>&nbsp;</p>";                          
    }    
    
// End script; don't run exit'    

function showTableHead($perPage, $totalItems, $showPager, $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=sign\" class=\"signlink\">" . $aLang['Sign the Guestbook'] . "</a>\n" . 
            "        </td>\n" . 
            "    </tr>\n" . 
            "    <tr>\n" . 
            "        <td><b>" . strftime($aConfig['date_format']) . "</b><br />\n" . 
            "              " . $aLang['Thank you for visiting my site.'] . "</td>\n" . 
            "    </tr>\n" . 
            "    <tr>\n" . 
            "        <td height=\"10\"></td>\n" . 
            "    </tr>\n" . 
            "    <tr>\n" . 
            "        <td>\n" . 
            "        <table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n" . 
            "            <tr>\n" . 
            "                <td class=\"shownum\">" .
            "                <img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/arrow11.gif\" alt=\"\" width=\"9\" height=\"7\" />" . $aLang['Total Records:'] . "<font class=\"redtext\">$totalItems</font>\n" . 
            "                <br><img src=\"" . SCRIPT_URL . $aConfig['theme'] . "/images/arrow11.gif\" alt=\"\" width=\"9\" height=\"7\" />" . $aLang['Viewed Per Page:'] ."<font class=\"redtext\">$perPage</font></td>\n" . 
            "                <td align=\"right\">" .
            $showPager .
            "               </td>\n" . 
            "            </tr>\n" . 
            "        </table>\n" . 
            "        </td>\n" . 
            "    </tr>\n" . 
            "    <tr>\n" . 
            "        <td height=\"5\"></td>\n" . 
            "    </tr>\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>\n" . 
            "\n" . 
            "        <table cellpadding=\"1\" cellspacing=\"1\" width=\"100%\">\n" . 
            "            <tr>\n" . 
            "                <td class=\"txtheadb\" width=\"25%\">" . $aLang['Name'] . "</td>\n" . 
            "                <td class=\"txtheadb\" width=\"75%\">" . $aLang['Comments'] . "</td>\n" . 
            "            </tr>\n" . 
            "            <tr>\n" . 
            "                <td class=\"bgtabcolor\" height=\"4\"></td>\n" . 
            "                <td class=\"bgtabcolor\" height=\"4\"></td>\n" . 
            "            </tr>\n" . 
            "            <tr>\n" . 
            "                <td class=\"bglinecolor\" height=\"1\"></td>\n" . 
            "                <td class=\"bglinecolor\" height=\"1\"></td>\n" . 
            "            </tr>\n" . 
            "            <tr>\n" . 
            "                <td colspan=\"2\"></td>\n" . 
            "            </tr>";
}

function  showTableFooter()
{
	echo "        </table>\n" . 
            "        </td>\n" . 
            "    </tr>\n" . 
            "</table>";
}
    
function insertSmileys($str) {
    // for each smileys
    include(SCRIPT_PATH . 'smilies.php');
    foreach ($aSmiley as $ascii => $img) {    
        $replace = "<img src=\"" . SCRIPT_URL . "smilies/" .  $img . "\" alt=\"$ascii\" title=\"" . str_replace('.gif', '' , $img) . "\" border=\"0\">";
        // replace it with the image tag
        $str = str_replace($ascii, $replace, $str);
    }
    return $str;
}

function textWrap($text, $size = 70) {
   $new_text = '';
   $text_1 = explode('>',$text);
   $sizeof = sizeof($text_1);
   for ($i=0; $i<$sizeof; ++$i) {
       $text_2 = explode('<',$text_1[$i]);
       if (!empty($text_2[0])) {
           $new_text .= preg_replace('#([^\n\r .]{'. $size .'})#i', '\\1  ', $text_2[0]);
       }
       if (!empty($text_2[1])) {
           $new_text .= '<' . $text_2[1] . '>'; 
       }
   }
   return $new_text;
}

function mailto_encode($sMail, $sText="", $sClass="", $aParams=Array())
{
    $sEncmail ="";
    for($i=0; $i<strlen($sMail); $i++) {
        $sEncmail .= "&#".ord(substr($sMail,$i,1));;
    } 
    
    # If no link text, the email adress is used as text
    if(!$sText) $sText = $sEncmail;
    
    # "Mailto: " is added to link
    $sEncmail = "&#77;&#97;&#105;&#108;&#116;&#111;&#58;&#32;" . $sEncmail;
    $sParams = "";
    foreach($aParams as $sKey=>$sValue) {
        if($sParams) $sParams .= "&$sKey=" . rawurlencode($sValue);
        else $sParams = "?$sKey=" . rawurlencode($sValue);
    }
    return "<a class='$sClass' href='$sEncmail$sParams'>$sText</a>";
}

?>