<?php
  // Syscheck script - by That Software Guy
  $version = "1.0.3";

  require('includes/application_top.php');
  require(DIR_WS_LANGUAGES . $_SESSION['language'] . '/' . 'syscheck.php');

  // List of directories which should not contain php files
  $no_php_dirs = array();
  $no_php_dirs[] = "../images";

  // List of known good files which contain calls to eval
  $eval_ok_files = array(); 

  $name = dirname(__FILE__); 
  $tok = explode("/", $name);
  $lasttok = sizeof($tok) - 1;
  $ADMINDIR = "../". $tok[$lasttok] . "/";

  $eval_ok_files[] = "../ipn_main_handler.php";
  $eval_ok_files[] = $ADMINDIR . "syscheck.php";
  $eval_ok_files[] = $ADMINDIR . "product_types.php";
  $eval_ok_files[] = $ADMINDIR . "modules.php";
  $eval_ok_files[] = $ADMINDIR . "configuration.php";
  $eval_ok_files[] = $ADMINDIR . "categories.php";
  $eval_ok_files[] = $ADMINDIR . "includes/classes/phplot.php";
  $eval_ok_files[] = $ADMINDIR . "includes/functions/general.php";
  $eval_ok_files[] = $ADMINDIR . "includes/functions/functions_customers.php";
  $eval_ok_files[] = $ADMINDIR . "includes/modules/newsletters/product_notification.php";
  $eval_ok_files[] = "../includes/functions/functions_customers.php";
  $eval_ok_files[] = "../includes/modules/payment/paypalwpp.php";
  $eval_ok_files[] = "../includes/modules/payment/paypal.php";
  $eval_ok_files[] = "../includes/modules/payment/paypaldp.php";
  $eval_ok_files[] = "../includes/modules/payment/nochex_apc.php";
  $eval_ok_files[] = "../includes/modules/payment/paypal/paypal_curl.php";
  $eval_ok_files[] = "../includes/modules/pages/download/header_php.php";
  // You may add files to this list.


  // Which tests to run? 
  $run_scan_admin_log = true; 
  $run_report_admin_users = true; 
  $run_writable_directories_test = true; 
  $run_writable_directories_test_on_linux = true; 
  $run_php_unexpected_files_test = true; 
  $run_php_eval_test = true; 
  $run_writable_files_test = true; 
  $run_writable_files_test_on_linux = true; 

?>
<html>
<head>
<title>SysCheck for <?php echo STORE_NAME?></title>
</head>
<body>
<?php
echo "<h1>" . SYSCHECK_TITLE . "</h1>"; 
echo "<b>" . SYSCHECK_VERSION . " " . $version . "</b>";
echo "&nbsp;&nbsp;";
echo '<a href="http://www.thatsoftwareguy.com/zencart_syscheck.html" target="_blank">Help</a>'; 
echo "&nbsp;&nbsp;";
echo '<a href="http://www.thatsoftwareguy.com/donate.html" target="_blank">Donate</a>'; 
echo "<hr />";
?>
<?php
  global $db;

  // Report admin accessing addresses 
  if ($run_scan_admin_log) {
     $admin_iplist = $db->Execute("SELECT DISTINCT ip_address FROM " . TABLE_ADMIN_ACTIVITY_LOG); 
     echo "<h2>" . ADMIN_IP. "</h2>"; 
     if ($admin_iplist->RecordCount() > 0) { 
        while (!$admin_iplist->EOF) { 
            $hostname=gethostbyaddr($admin_iplist->fields['ip_address']);
            echo $admin_iplist->fields['ip_address'] . " -> " . $hostname . "<br />";
            $admin_iplist->MoveNext(); 
        }
     } else {
       echo NONE_FOUND . "<br />"; 
     }
  } else { 
     echo "<h2>" . ADMIN_IP. "</h2>"; 
     echo SKIP_ADMIN_IP_TEST. "<br />"; 
  }

  if ($run_report_admin_users) {
     echo '<h2>' .ADMIN_USERS  . '</h2>';
     $admin_userlist = $db->Execute("SELECT admin_name, admin_email FROM " . TABLE_ADMIN); 
     if ($admin_userlist->RecordCount() > 0) { 
        while (!$admin_userlist->EOF) { 
            echo $admin_userlist->fields['admin_name'] . ' ' .  
                 '(' . $admin_userlist->fields['admin_email'] . ') ' .  
                 "<br />"; 
            $admin_userlist->MoveNext(); 
        }
     } else {
       echo NONE_FOUND . "<br />"; 
     }

  } else { 
     echo '<h2>' .ADMIN_USERS  . '</h2>';
     echo SKIP_ADMIN_USERS_TEST . "<br />"; 
  }
 
  // Look for writable directories
  if ( ($run_writable_directories_test_on_linux) || 
       ($run_writable_directories_test) ) {
     $header_printed = false;
     $files = get_dirs(".."); 
     foreach ($files as $file) { 
            // Linux - check permissions of dirs
            if ($run_writable_directories_test_on_linux) {
               $perms = substr(sprintf('%o', fileperms($file)), -4);
               if ($perms == 0755) continue;  
               if ($perms == 755) continue;  
            }
            if (!$header_printed) {
               $header_printed = true;
               echo "<h2>" . DIRCHECK_TITLE . "</h2>"; 
            }
            if ($run_writable_directories_test_on_linux) {
               echo $file . " (" . $perms . ")" . "<br />";
            } else {
               echo $file . "<br />";
            }
     }
     if (!$header_printed) {
         $header_printed = true;
         echo "<h2>" . DIRCHECK_TITLE . "</h2>"; 
         echo NO_ISSUES . "<br />";
     }
  } else { 
    echo "<h2>" . DIRCHECK_TITLE . "</h2>"; 
    echo SKIP_RUN_WRITABLE_DIRECTORIES_TEST . "<br />";
  }

  // Look for PHP files which shouldn't be there
  if ($run_php_unexpected_files_test) { 
     $header_printed = false;
     $scriptlist = array("php", "py", "pl" , "cgi"); 
     foreach ($no_php_dirs as $dir) { 
        $files = pc_process_dir($dir); 
        foreach ($files as $file) { 
           $info = pathinfo($file); 
           if (in_array($info['extension'], $scriptlist)) {
               if (!$header_printed) {
                  $header_printed = true;
                  echo "<h2>" . NOPHP_TITLE . "</h2>"; 
               }
               echo $file . "<br />";
           }
        }
     }
     if (!$header_printed) {
         $header_printed = true;
         echo "<h2>" . NOPHP_TITLE . "</h2>"; 
         echo NO_ISSUES . "<br />";
     }
  } else { 
    echo "<h2>" . NOPHP_TITLE . "</h2>"; 
    echo SKIP_RUN_PHP_UNEXPECTED_FILES_TEST . "<br />"; 
  }

  // Scan all files for eval.
  if ($run_php_eval_test) {
     $header_printed = false;
     $files = pc_process_dir(".."); 
     $writable_files = array(); 
     foreach ($files as $file) { 
        $writable = true; 
        $perms = 0;
        // Linux - check permissions of files 
        if ($run_writable_files_test_on_linux) {
            $perms = substr(sprintf('%o', fileperms($file)), -4);
            if ($perms == 0644) $writable = false;
            if ($perms == 644) $writable = false;  
            if ($perms == 444) $writable = false;  
        }
        if ($writable) { 
           $writable_files[] = array('name' => $file, 
                                     'perms' => $perms); 
        }
        $info = pathinfo($file); 
        if ($info['extension'] == "php") {
            $contents = file_get_contents($file);
            if (strpos($contents, "eval") !== false) {
               if (in_array($file, $eval_ok_files)) continue;
               if (!$header_printed) {
                  $header_printed = true;
                  echo "<h2>" . EVAL_TITLE . "</h2>"; 
               }
               echo $file . "<br />"; 
            }
        }
     }
     if (!$header_printed) {
         $header_printed = true;
         echo "<h2>" . EVAL_TITLE . "</h2>"; 
         echo NO_ISSUES . "<br />";
     }
  } else {
    echo "<h2>" . EVAL_TITLE . "</h2>"; 
    echo SKIP_RUN_PHP_EVAL_TEST . "<br />"; 
  }

  // Check for writable files
  if ( ($run_writable_files_test) || 
       ($run_writable_files_test_on_linux) ) {
     $header_printed = false;
     if (sizeof($writable_files) > 0) {
         $header_printed = true;
         echo "<h2>" . FILECHECK_TITLE . "</h2>"; 
         foreach ($writable_files as $file) { 
               if ($run_writable_files_test_on_linux) {
                 echo $file['name'] . " (" . $file['perms']. ")" . "<br />";
               } else {
                 echo $file['name'] . "<br />";
               }
         }
     }
     if (!$header_printed) {
         echo "<h2>" . FILECHECK_TITLE . "</h2>"; 
         echo NO_ISSUES . "<br />";
     }
  } else {
    echo "<h2>" . FILECHECK_TITLE . "</h2>"; 
    echo SKIP_RUN_WRITABLE_FILES_TEST . "<br />"; 
  }

  echo '<br />';

  // End of report
  echo '<br />' . SYSCHECK_REPORT_END; 
  echo '<br /><br />' . '<a href="' . zen_href_link(FILENAME_DEFAULT) . '">' . SYSCHECK_BACK . '</a>'; 
?>
</body>
</html>
