<?php namespace TestCAS\Http\Controllers;

use Log;
use phpCAS;

class WelcomeController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Welcome Controller
    |--------------------------------------------------------------------------
    |
    | This controller renders the "marketing page" for the application and
    | is configured to only allow guests. Like most of the other sample
    | controllers, you are free to modify or remove it as you desire.
    |
    */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth.cas');
    }


    public function storePGT()
    {
        Log::info("Connecting to callback page");
        Log::info($_GET);

        if (!empty($_GET['pgtIou'])&&!empty($_GET['pgtId'])) {
          phpCAS::traceBegin();
          $pgt_iou=$_GET["pgtIou"];
          $pgt=$_GET["pgtId"];
          $fname="/home/vagrant/Code/storage/PGT/".$pgt_iou.'.plain';
          if (!file_exists($fname)) {
              touch($fname);
              // Chmod will fail on windows
              @chmod($fname, 0600);
              if ($f=fopen($fname, "w")) {
                  if (fputs($f, $pgt) === false) {
                      phpCAS::error('could not write PGT to `'.$fname.'\'');
                  }
                  phpCAS::trace('Successful write of PGT to `'.$fname.'\'');
                  fclose($f);
              } else {
                  phpCAS::error('could not open `'.$fname.'\'');
              }
          } else {
              phpCAS::error('File exists: `'.$fname.'\'');
          }
          phpCAS::traceEnd();
        }
    }

    /**
     * Show the application welcome screen to the user.
     *
     * @return Response
     */
    public function index()
    {
        Log::info("Connecting to index page");
        return view('welcome');
    }

}
