<?php
/**
 * Guestbook-Updater Tool
 *
 * Do not add this file to API-documentation,
 * it's just a tool - not an API ...
 *
 * @package yana
 * @ignore
 */
?>
<html>

<head>
    <title>Update Guestbook to version 2.8</title>
</head>

<body>

    <h1>Update Guestbook to version 2.8</h1>
    <h2>How to:</h2>
    <ol>
        <li>First rename your old &quot;config/&quot; directory to &quot;backup/&quot;.</li>
        <li>create backup copies of all files you may want to keep</li>
        <li>delete all directories except &quot;backup/&quot</li>
        <li>download and install the new version in this directory</li>
        <li>copy this Update script to the yana base directory</li>
        <li>ONLY AFTER you have done all this: run this Update :-)</li>
    </ol>
    <h2>What it does:</h2>
    <p>
        This script is used to convert entries from version 2.6 and 2.7 to the new version 2.8 and (possibly) beyond.
        You need at least PHP 4.3.x or higher. Otherwise this script will not be able to work properly.
    </p>
    <p>
        Your personal settings and user information (passwords et cetera) are NOT converted.
    </p>
    <p style="font-weight: bold;">
        This script may overwrite existing files. Be sure to <u>CREATE BACKUP COPIES</u> of all your files <u>BEFORE</u> you start.
    </p>

<?php

/* Create Start-Button */
if (!isset($_GET['start']) || $_GET['start'] !== 'true') {
    die('<a target="_self" href="?start=true">Click here to run this script now!</a>');
} else {
    print '<h2>Progress:</h2>';
    print '<p style="color: red; font-size: x-large; font-weight: bold;">This may take a while! Please be patient!</p>';
}

/**
 * @ignore
 */
require_once('library.php');

$db = new filedb("guestbook");
convert($db);

echo '<h1>End of Script! Please check your files.</h1>';

/**
 * reads dat-files in $dir and its sub-directories and exports them to the guestbook file-database
 *
 * @param  bufferedDbStream &$db database connection
 * @param  string $dir (optional) base directory to start scan
 * @param  string $filter (optional) file extension to scan, may contain multiple extension seperated by '|'
 * @return void
 * @uses   convert($db); convert($db, './', '.dat'); convert('./config/', '.dat|.config|.sml');
 */
function convert(&$db, $dir = './backup/', $sfilter = '.dat')
{
    $afilter = explode("|", $sfilter);
    foreach ($afilter as $a => $filter)
    {
        $list = dirlist($dir, $filter);
        foreach ($list as $a => $b)
        {
            echo "<h3>Processing file ${dir}${b}</h3>\n";
            $file = new textfile("${dir}${b}");
            $file->read();
            for ($i=0;$i<$file->length();$i++)
            {
                $row = $file->get($i, $file->DONT_UNTAINT_INPUT);
                $result = array();
                $result['profile_id'] = preg_replace("/".preg_quote($filter)."$/", "", $b);
                if ($result['profile_id'] === 'gbuch') {
                    $result['profile_id'] = 'default';
                }
                foreach ($row as $key => $element)
                {
                    if (is_string($element)) {
                      $element = preg_replace('/<br>|<br \/>/', '[br]', $element);
                      $element = preg_replace('/<wbr>/', '[wbr]', $element);
                    }
                    if (!empty($element)) {
                        switch (strtolower($key)) {
    
                            case 'guestbook_name':
                                $result['guestbook_name']    = $element;
                            break;
    
                            case 'name':
                                $result['guestbook_name']    = $element;
                            break;
    
                            case 'guestbook_ip':
                                $result['guestbook_ip']      = untaintInput($element, 'ip', 15);
                            break;
    
                            case 'remote_addr':
                                $result['guestbook_ip']      = untaintInput($element, 'ip', 15);
                            break;
    
                            case 'guestbook_message':
                                $result['guestbook_message'] = $element;
                            break;
    
                            case 'nachricht':
                                $result['guestbook_message'] = $element;
                            break;
    
                            case 'guestbook_mail':
                                $result['guestbook_mail']    = untaintInput($element, 'mail');
                            break;
    
                            case 'mail':
                                $result['guestbook_mail']    = untaintInput($element, 'mail');
                            break;
    
                            case 'guestbook_homepage':
                                $result['guestbook_homepage']= $element;
                            break;
    
                            case 'homepage':
                                $result['guestbook_homepage']= $element;
                            break;
    
                            case 'guestbook_hometown':
                                $result['guestbook_hometown']= $element;
                            break;
    
                            case 'wohnort':
                                $result['guestbook_hometown']= $element;
                            break;
    
                            case 'guestbook_messenger':
                                $result['guestbook_messenger']= $element;
                            break;
    
                            case 'messenger':
                                $result['guestbook_messenger']= $element;
                            break;
    
                            case 'icq':
                                $result['guestbook_messenger']= $element;
                                $result['guestbook_msgtyp']   = 'icq';
                            break;
    
                            case 'msn':
                                $result['guestbook_messenger']= $element;
                                $result['guestbook_msgtyp']   = 'msn';
                            break;
    
                            case 'yahoo':
                                $result['guestbook_messenger']= $element;
                                $result['guestbook_msgtyp']   = 'yahoo';
                            break;
    
                            case 'aol':
                                $result['guestbook_messenger']= $element;
                                $result['guestbook_msgtyp']   = 'aol';
                            break;
    
                            case 'guestbook_msgtyp':
                                $result['guestbook_msgtyp']   = $element;
                            break;
    
                            case 'msgtyp':
                                $result['guestbook_msgtyp']   = $element;
                            break;
    
                            case 'guestbook_is_registered':
                                $result['guestbook_is_registered'] = (($element) ? 1 : 0);
                            break;
    
                            case 'is_registered':
                                $result['guestbook_is_registered'] = (($element) ? 1 : 0);
                            break;
    
                            case 'guestbook_opinion':
                                $result['guestbook_opinion']       = $element;
                            break;
    
                            case 'meinung':
                                if (is_numeric($element) && $element > 0 && $element < 6) {
                                        $result['guestbook_opinion']= $element;
                                } else {
                                    switch ($element) {
                                        case 'sehr gut':
                                            $result['guestbook_opinion'] = 1;
                                        break;
                                        case 'gut':
                                            $result['guestbook_opinion'] = 2;
                                        break;
                                        case 'durchschnittlich':
                                            $result['guestbook_opinion'] = 3;
                                        break;
                                        case 'schlecht':
                                            $result['guestbook_opinion'] = 4;
                                        break;
                                        case 'sehr schlecht':
                                            $result['guestbook_opinion'] = 5;
                                        break;
                                        default:
                                        break;
                                    }
                                }
                            break;
    
                            case 'guestbook_comment':
                                $result['guestbook_comment']  = $element;
                            break;
    
                            case 'kommentar':
                                $result['guestbook_comment']  = $element;
                            break;
    
                            case 'guestbook_date':
                                if (is_numeric($element)) {
                                $result['guestbook_date']     = $element;
                                } elseif (!empty($element)) {
                                $date = explode(".", $element);
                                $result['guestbook_date']     = mktime(0,0,0,$date[1],$date[0],$date[2]);
                                }
                            break;
    
                            case 'datum':
                                if (is_numeric($element)) {
                                $result['guestbook_date']     = $element;
                                } elseif (!empty($element)) {
                                $date = explode(".", $element);
                                $result['guestbook_date']     = mktime(0,0,0,$date[1],$date[0],$date[2]);
                                }
                            break;
    
                            default:
                                echo "<p><b>Warning: unprocessed key ${key} dropped! Possible loss of information!</p>";
                            break;
                        } /* end switch $key */
                    } /* end if */
                } /* end foreach $row */
                if (count($result)>0) {
                    $test = $db->insert("guestbook.*", $result);
                    if ($test) {
                        $test = $db->write();
                    }
                    echo "<p><b>Updating row #${i} ".( ($test) ? '<span style="color:#0A0">successful</span>' : '<span style="color:#A00">failed</span>' ).":</b></p>\n";
                    foreach($result as $key => $element)
                    {
                        echo "<i>${key}</i>=".htmlentities("${element}; ");
                    }
                } else {
                    echo "<p style=\"color: #a00;\"><b>Not updating row #${i} since it is empty.</b></p>\n";
                }
            } /* end for $file */
            if (count($result)>0) {
                echo "<p><b>Updating database ".( ($test) ? '<span style="color:#0A0">successful</span>' : '<span style="color:#A00">failed</span>' )."</b></p>\n";
            } else {
                echo "<p style=\"color: #a00;\"><b>Update of file ".$file->getFilename()." aborted, since it doesn't seem to have the right version. Not saving changes. Hey Pal, you ain't running this script twice, ey? I told you not to.</p>\n";
            }
        } /* end foreach $list */
    } /* end foreach $filter */
    $list = subdirlist($dir);
    foreach($list as $a => $b)
    {
        @convert($db, $b, $sfilter);
    }
}

/**
 * lists all sub-directories in the directory $dir
 * sorted by name
 *
 * @param   string $dir base directory to start scan
 * @return  array
 * @uses    subdirlist('./');
 */
function subdirlist($dir)
{
    settype($dir, "string");

    $dirlist = array();
    if (is_dir($dir)) {
        $hdir = dir($dir);
        while($entry=$hdir->read()) {
            if (!preg_match("/^\.{1,2}$/i",$entry) && is_dir("${dir}${entry}")){
                $dirlist[count($dirlist)]="${dir}${entry}/";
            }
        }
        $hdir->close();
        sort($dirlist);
    }
    return $dirlist;

}

?>
</body>

</html>