<?php
  require_once("Crypt.php");

  set_time_limit ( 900 );

  function VerifyUser( $data, &$key )
  {
    //pevne zadane id, pre ucel testovacieho servera
    
    //pevme zadany sukromny kluc, pre ucel testovacieho servera
    $plainData = AsymDecrypt( $data, "202700adbd85e2d7182720c3a0ee19c1", "30db31542ace0f7d37a629ee5eba28cb" );
    
    $idHash = substr( $plainData, 0, 32 );
    $key = substr( $plainData, 32, 32 );
    
    $id = md5($_POST["idp"]."1234567890");

    if ( strncmp( $idHash, $id, 32 ) != 0 )
      return false;
    
    return true;
  }

  function ReturnContent()
  {
    if ( VerifyUser( base64_decode( $_POST["data"] ), $key ) )
    {
      $contents = "";
      switch ( $_POST["action"] )
      {
        case "users": $contents = file_get_contents("/home/probatur/public_html/admin/exchange/users.xml");
        //case "users": $contents = file_get_contents("../admin/exchange/users.xml");
        break;
        
        case "courses": $contents = file_get_contents("/home/probatur/public_html/admin/exchange/courses.xml");
        //case "courses": $contents = file_get_contents("../admin/exchange/courses.xml");
        break;
        
        case "exams": 
        {
          if ( file_exists("/home/probatur/public_html/admin/exchange/exams".$_POST["param1"].".xml") )
            $contents = file_get_contents("/home/probatur/public_html/admin/exchange/exams".$_POST["param1"].".xml");
          else
            $contents = file_get_contents("/home/probatur/public_html/admin/exchange/emptyexams.xml");
        }
        break;
      }
      
      if ( !empty( $contents ) )
      {
        $td = mcrypt_module_open("rijndael-256", "", "ofb", "");
        if ( mcrypt_generic_init($td, $key, $_POST["iv"] ) != -1 )
        {
          $contents = mcrypt_generic($td, $contents);
          mcrypt_generic_deinit($td);
          mcrypt_module_close($td);
        }
        else
          $contents = ""; 
      }
      return $contents;
    }
    header("HTTP/1.0 403 Forbidden");
  }
  echo ReturnContent();
?>
