<commit_before><?php
error_reporting(E_ALL);

class Halcyon {
  const DEBUG = false;

  public $_templateDirectory = "views";
  const CONTROLLER_DIR = "app";

  //
  // Don't touch anything beyond this line :-)
  //
  // Really.
  //
  
  const RESPONSE_JSON = 1;
  const RESPONSE_TEMPLATED = 2;

  private static $_halcyonInstance = null;

  public $_controllerLoaded = false;


  public $_responseType = Halcyon::RESPONSE_TEMPLATED;
  public $_templateLayout = "layout";
  public $_templateBody = null;
  public $_responseContentType = "text/html; charset=utf-8";
  
  
  public static function run() {
    $instance = self::getInstance();
    
    return $instance;
  }


  public static function getInstance() {
    if (self::$_halcyonInstance === null) {
      self::$_halcyonInstance = new Halcyon();
    }

    return self::$_halcyonInstance;
  }


  public static function jsonResponse() {
    self::getInstance()->_responseType = Halcyon::RESPONSE_JSON;
  }


  public static function templatedResponse() {
    self::getInstance()->_responseType = Halcyon::RESPONSE_TEMPLATED;
  }


  public static function suppressLayout() {
    self::getInstance()->_templateLayout = null;
  }


  public static function setTemplateDirectory($base) {
    self::getInstance()->_templateDirectory = $base;
  }

  public static function setLayout($layout) {
    self::getInstance()->_templateLayout = $layout;
  }

  public static function setBody($body) {
    self::getInstance()->_templateBody = $body;
  }


  public static function requireSession() {
    if (!isset($_SESSION["user"])) {
      header("Location: /session/login/");
      exit;
    }

    return true;
  }

  
  public static function requireGroup($group) {
    Halcyon::requireSession();
    
    if ($_SESSION["user"]->group != $group) {
      header("Location: /session/login/");
      exit;
    }

    return true;
  }

  
  public function __construct() {
    session_start();
    $this->_query = $_SERVER["QUERY_STRING"];

    // Clean up variables - URL
    $url = $_SERVER["REDIRECT_URL"];

    if (substr($url, -1) == "/") {
      $url = substr($url, 0, -1);
    }

    $scriptcount = count(explode("/", $_SERVER["SCRIPT_NAME"]));
    
    $explodedurl = explode("/", $url);
    $countexplodedurl = count($explodedurl);
      
    $sliced = array_slice($explodedurl, 0 + $scriptcount - 1);

    $this->_url = join("/", $sliced);
  }

  
  // General handler function
  public function handleRequest() {
    $this->loadController();

    if (!$this->_controllerLoaded) {
      header("HTTP/1.0 404 Not Found");

      echo "Error 404 :-(";
      
    } else {
      $this->_handler = new $this->_controllerClassname();

      $this->_controllerReturnValue = call_user_func_array(array($this->_handler, $this->_calleeMethod), $this->_calleeParams);

      $this->decorateOutput();
    }
  }


  // Decorate the output of the controller as per controller's request...
  private function decorateOutput() {
    if ($this->_responseType == Halcyon::RESPONSE_JSON) {
      header("Content-Type: application/json");
      echo json_encode($this->_controllerReturnValue);
      
    } elseif ($this->_responseType == Halcyon::RESPONSE_TEMPLATED) {
      header("Content-Type: " . $this->_responseContentType);
      // We're throwing away the return value...

      $templatefilename = null;
      
      if ($this->_templateBody === null) {
	$this->_templateBody = ucfirst($this->_calleeMethod);
	
	$templatefilename = $this->_templateDirectory . "/" .
	  $this->_controllerFilename . $this->_templateBody . ".php";

      } else {
	$templatefilename = $this->_templateDirectory . "/" .
	  $this->_templateBody . ".php";
      }

      if ($templatefilename !== null) {
	$body = ___captureFileOutput($this->_handler, $templatefilename);


	if ($this->_templateLayout !== null) {
	  $values = (array) $this->_handler;
	  $values["___body___"] = $body;

	  $layout = ___captureFileOutput($values, $this->_templateDirectory . "/" . $this->_templateLayout . ".php");

	  echo $layout;
	  
	} else {
	  echo $body;
	}
      }
    }
  }



  // Get an array of arrays (file, method, params), load each in turn and try to find a valid one
  private function loadController() {
    $candidates = $this->findControllerCandidates();

    $last = "";
    $loaded = false;<commit_msg>switch template files to use .tpl, fix backslash munging at the end of url, kill error reporting which was set to all<commit_after><?php
class Halcyon {
  const DEBUG = false;

  public $_templateDirectory = "views";
  const CONTROLLER_DIR = "app";

  //
  // Don't touch anything beyond this line :-)
  //
  // Really.
  //
  
  const RESPONSE_JSON = 1;
  const RESPONSE_TEMPLATED = 2;

  private static $_halcyonInstance = null;

  public $_controllerLoaded = false;


  public $_responseType = Halcyon::RESPONSE_TEMPLATED;
  public $_templateLayout = "layout";
  public $_templateBody = null;
  public $_responseContentType = "text/html; charset=utf-8";
  
  
  public static function run() {
    $instance = self::getInstance();
    
    return $instance;
  }


  public static function getInstance() {
    if (self::$_halcyonInstance === null) {
      self::$_halcyonInstance = new Halcyon();
    }

    return self::$_halcyonInstance;
  }


  public static function jsonResponse() {
    self::getInstance()->_responseType = Halcyon::RESPONSE_JSON;
  }


  public static function templatedResponse() {
    self::getInstance()->_responseType = Halcyon::RESPONSE_TEMPLATED;
  }


  public static function suppressLayout() {
    self::getInstance()->_templateLayout = null;
  }


  public static function setTemplateDirectory($base) {
    self::getInstance()->_templateDirectory = $base;
  }

  public static function setLayout($layout) {
    self::getInstance()->_templateLayout = $layout;
  }

  public static function setBody($body) {
    self::getInstance()->_templateBody = $body;
  }


  public static function requireSession() {
    if (!isset($_SESSION["user"])) {
      header("Location: /session/login/");
      exit;
    }

    return true;
  }

  
  public static function requireGroup($group) {
    Halcyon::requireSession();
    
    if ($_SESSION["user"]->group != $group) {
      header("Location: /session/login/");
      exit;
    }

    return true;
  }

  
  public function __construct() {
    session_start();
    $this->_query = $_SERVER["QUERY_STRING"];

    // Clean up variables - URL
    $url = $_SERVER["REDIRECT_URL"];

    $url = rtrim($url, '/');
    
    $scriptcount = count(explode("/", $_SERVER["SCRIPT_NAME"]));
    
    $explodedurl = explode("/", $url);
    $countexplodedurl = count($explodedurl);
      
    $sliced = array_slice($explodedurl, 0 + $scriptcount - 1);

    $this->_url = join("/", $sliced);
  }

  
  // General handler function
  public function handleRequest() {
    $this->loadController();

    if (!$this->_controllerLoaded) {
      header("HTTP/1.0 404 Not Found");

      echo "Error 404 :-(";
      
    } else {
      $this->_handler = new $this->_controllerClassname();

      $this->_controllerReturnValue = call_user_func_array(array($this->_handler, $this->_calleeMethod), $this->_calleeParams);

      $this->decorateOutput();
    }
  }


  // Decorate the output of the controller as per controller's request...
  private function decorateOutput() {
    if ($this->_responseType == Halcyon::RESPONSE_JSON) {
      header("Content-Type: application/json");
      echo json_encode($this->_controllerReturnValue);
      
    } elseif ($this->_responseType == Halcyon::RESPONSE_TEMPLATED) {
      header("Content-Type: " . $this->_responseContentType);
      // We're throwing away the return value...

      $templatefilename = null;
      
      if ($this->_templateBody === null) {
	$this->_templateBody = ucfirst($this->_calleeMethod);
	
	$templatefilename = $this->_templateDirectory . "/" .
	  $this->_controllerFilename . $this->_templateBody . ".tpl";

      } else {
	$templatefilename = $this->_templateDirectory . "/" .
	  $this->_templateBody . ".tpl";
      }

      if ($templatefilename !== null) {
	$body = ___captureFileOutput($this->_handler, $templatefilename);


	if ($this->_templateLayout !== null) {
	  $values = (array) $this->_handler;
	  $values["___body___"] = $body;

	  $layout = ___captureFileOutput($values, $this->_templateDirectory . "/" . $this->_templateLayout . ".tpl");

	  echo $layout;
	  
	} else {
	  echo $body;
	}
      }
    }
  }



  // Get an array of arrays (file, method, params), load each in turn and try to find a valid one
  private function loadController() {
    $candidates = $this->findControllerCandidates();

    $last = "";
    $loaded = false;
{'fraction_non_alphanumeric': 0.14804180544389572, 'fraction_numerical': 0.003330653497186172, 'mean_word_length': 3.184526669870255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '703363', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2594, 'n_words': 584}