<?php

/*  Alternate WebKit
(C) 2011-2014 by Alternate Tools
-----------------------------------------------------

File:         display_sysinfo.php
Description:  Administrative module to display system info

*/

    include "../modules/mod_admin.php";
    include "../modules/class_textfile.php";
    include "../modules/class_pagelist.php";
    include "../modules/class_pageimagelist.php";
    include "../modules/class_pagefilelist.php";

    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }

    // Cancel sysinfo?
    if (isset($_POST['sysinfo_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }
    // Reset visitors?
    if (isset($_POST['sysinfo_ResetVisitors']))
    {
        $cFile = new cTextFile;
        if ($cFile->SaveToFile($FRONT_COUNT_FILE, "0") == true && $cFile->SaveToFile($FRONT_IPLOG_FILE, "") == true)
        {
            log_message ('User ' . get_user_name() . ' reseted visitor counter');
            header('location: ' . $ADMIN_MAIN_SITE);
            return;
        }
        $errorString = get_translation('admin_sysinfo_errorvisitors') . '!';
    }
    // Reset all page views?
    if (isset($_POST['sysinfo_ResetViews']))
    {
        $pagList = new cPageList;
        $pagList->SetFolder($PAGES_FOLDER);
        $page = new cPage;
        $page->SetFolder($PAGES_FOLDER);
        $pagList->LoadPageList();
        $list = $pagList->GetPageList();
        $errorOccured = false;
        foreach ($list as $value)
        {
            if ($page->LoadPageFromId($value) == false)
            {
                $errorOccured = true;
                break;
            }
            if ($page->ResetViewCount() == false)
            {
                $errorOccured = true;
                break;
            }
        }
        if ($errorOccured == false)
        {
            log_message ('User ' . get_user_name() . ' reseted page views');
            header('location: ' . $ADMIN_MAIN_SITE);
            return;
        }
        $errorString = get_translation('admin_sysinfo_errorviews') . '!';
    }

    // Display
    $cFile = new cTextFile;
    print_admin_header(get_translation('admin_sysinfo_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-14', true, get_translation('admin_backtomain'));
    if ($errorString !="")
        print_error_message($errorString);

    echo_r ('<form method="post" action="">');

    // System
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%"><b>' . get_translation('admin_sysinfo_serverparameters') . ':</b></td>');
    echo_r ('    <td></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_servername') . ': </td>');
    echo_r ('    <td>' . $_SERVER['SERVER_NAME'] . '</td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_serveripaddress') . ': </td>');
    echo_r ('    <td>' . $_SERVER['SERVER_ADDR'] . '</td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_serversoftware') . ': </td>');
    echo_r ('    <td>' . $_SERVER['SERVER_SOFTWARE'] . '</td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_serverphpversion') . ': </td>');
    echo_r ('    <td>' . phpversion() . '</td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_serverdatetime') . ': </td>');
    echo_r ('    <td>' . date("Y-m-d H:i:s") . '</td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    echo_r ('<hr>');
    // Site
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%"><b>' . get_translation('admin_sysinfo_siteparameters') . ':</b></td>');
    echo_r ('    <td></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    // Visitors
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_visitors') . ': </td>');
    echo_r ('    <td>' . $cFile->LoadFromFile($FRONT_COUNT_FILE) . '</td>');
    echo_r ('  </tr>');
    // Pages count
    $pagList = new cPageList;
    $pagList->SetFolder($PAGES_FOLDER);
    $pagList->LoadPageList();
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_numpages') . ': </td>');
    echo_r ('    <td>' . count($pagList->GetPageList()) . '</td>');
    echo_r ('  </tr>');
    // Pages views
    $pageCount = 0;
    $page = new cPage;
    $page->SetFolder($PAGES_FOLDER);
    $pagList = new cPageList;
    $pagList->SetFolder($PAGES_FOLDER);
    $pagList->LoadPageList();
    $list = $pagList->GetPageList();
    $hightestCount = -1;
    $lowestCount = -1;
    $hightestCountName = "";
    $lowestCountName = "";
    foreach ($list as $value)
    {
         $page->LoadPageFromId($value);
         $pageCount += $page->GetViewCount();
         if ($lowestCount == -1)
         {
             $lowestCount = $page->GetViewCount();
             $lowestCountName = $page->GetTitle();
         }
         if ($page->GetViewCount() >  $hightestCount)
         {
             $hightestCount = $page->GetViewCount();
             $hightestCountName = $page->GetTitle();
         }
         if (($page->GetViewCount() < $lowestCount) && $page->GetType() != PAGETYPE_LINK)  // Links do not increase!!
         {
             $lowestCount = $page->GetViewCount();
             $lowestCountName = $page->GetTitle();
         }
    }
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_pageviews') . ': </td>');
    echo_r ('    <td>' . $pageCount . '</td>');
    echo_r ('  </tr>');
    if ($hightestCount > 0)
    {
        echo_r ('  <tr>');
        echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_highestpageview') . ': </td>');
        echo_r ('    <td>' . $hightestCountName . ' (' . $hightestCount . ')</td>');
        echo_r ('  </tr>');
    }
    if ($lowestCount > 0)
    {
        echo_r ('  <tr>');
        echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_lowestpageview') . ': </td>');
        echo_r ('    <td>' . $lowestCountName . ' (' . $lowestCount . ')</td>');
        echo_r ('  </tr>');
    }
    // Pageimages count
    $pagList = new cPageImageList;
    $pagList->SetFolder($PAGEIMAGES_FOLDER);
    $pagList->LoadPageImageList();
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_numimages') . ': </td>');
    echo_r ('    <td>' . count($pagList->GetPageImageList()) . '</td>');
    echo_r ('  </tr>');
    // Pagefiles count
    $pagList = new cPageFileList;
    $pagList->SetFolder($PAGEFILES_FOLDER);
    $pagList->LoadPageFileList();
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_numfiles') . ': </td>');
    echo_r ('    <td>' . count($pagList->GetPageFileList()) . '</td>');
    echo_r ('  </tr>');
    if (user_is_admin() == true)  // Reset option admins only
    {
        echo_r ('  <tr>');
        echo_r ('    <td>');
        echo_r ('    </td>');
        echo_r ('    <td>');
        echo_r ('    <p align="right"><input type="submit" class="bottom_btn_large" name="sysinfo_ResetVisitors" value="' . get_translation('admin_sysinfo_resetvisitors') . '">');
        echo_r ('    <input type="submit" class="bottom_btn_large" name="sysinfo_ResetViews" value="' . get_translation('admin_sysinfo_resetviews') . '"></p>');
        echo_r ('    </td>');
        echo_r ('  </tr>');
    }
    echo_r ('</table>');

    // Referers
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%"><b>' . get_translation('admin_sysinfo_referrers') . ': </b></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td>');
    echo_r ('    <textarea cols="120" rows="8" wrap="off">');
    $contentLog = $cFile->LoadFromFile($FRONT_LOG_FILE);
    $contentArray = explode(chr(10), $contentLog);
    $contentLine = array();
    $referers = array();
    for ($i = 0; $i < count($contentArray); $i ++)
    {
        $contentLine = explode("|", $contentArray[$i]);
        if (isset($contentLine[2]) == false)
            continue;
        if (in_array($contentLine[2], $referers) == false)
            $referers[] = $contentLine[2];
    }
    sort($referers);
    foreach ($referers as $referer)
        echo_r($referer);
    echo_r ('    </textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

    // User agents
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%"><b>' . get_translation('admin_sysinfo_useragents') . ': </b></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td>');
    echo_r ('    <textarea cols="120" rows="8" wrap="off">');
    $contentLog = $cFile->LoadFromFile($FRONT_LOG_FILE);
    $contentArray = explode(chr(10), $contentLog);
    $contentLine = array();
    $referers = array();
    for ($i = 0; $i < count($contentArray); $i ++)
    {
        $contentLine = explode("|", $contentArray[$i]);
        if (isset($contentLine[3]) == false)
            continue;
        if (in_array($contentLine[3], $referers) == false)
        {
            if (strstr($contentLine[3], "A new visitor") == "")
            $referers[] = $contentLine[3];
        }
    }
    sort($referers);
    foreach ($referers as $referer)
        echo_r($referer);
    echo_r ('    </textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

    // Languages
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%"><b>' . get_translation('admin_sysinfo_languages') . ': </b></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td>');
    echo_r ('    <textarea cols="120" rows="8" wrap="off">');
    $contentLog = $cFile->LoadFromFile($FRONT_LOG_FILE);
    $contentArray = explode(chr(10), $contentLog);
    $contentLine = array();
    $referers = array();
    for ($i = 0; $i < count($contentArray); $i ++)
    {
        $contentLine = explode("|", $contentArray[$i]);
        if (isset($contentLine[4]) == false)
            continue;
        if (in_array($contentLine[4], $referers) == false)
        {
            if (strstr($contentLine[4], "A new visitor") == "")
            $referers[] = $contentLine[4];
        }
    }
    sort($referers);
    foreach ($referers as $referer)
        echo_r($referer);
    echo_r ('    </textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

    // Software
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%"><b>' . get_translation('admin_sysinfo_software') . ': </b></td>');
    echo_r ('    <td></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_softwareversion') . ': </td>');
    echo_r ('    <td>' . $SOFTWARE_VERSION . ' - ' . $SOFTWARE_DATE . '</td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_sysinfo_softwarechanges') . ': </td>');
    echo_r ('    <td>');
    echo_r ('    <textarea cols="88" rows="5" wrap="off">');
    include $SOFTWARE_CHANGES;
    echo_r ('    </textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    echo_r ('</form>');


    echo_r('<br>');

    print_bottom_links("sysinfo_Cancel", get_translation('admin_ok'));

    print_admin_footer();
?>