<?php

/*--------------------------+
 |      FAST FRAMEWORK      |
 |--------------------------|
 | a cms builder.           |
 |                          |
 | visit                    |
 | http://fastframework.org |
 | to learn more            |
 +--------------------------+


   -------------
   COPYRIGHT (c)

   2004 - 2021 Martin Sprekelsen <martin@sprekelsen.de>,
   2008 - 2021 Sintesi Labs Design Group EWIV/GEIE <in@sintesilabs.eu>

   This sourcecode is property of the author and protected by copyright.

   The use of this software is strictly limited
   to the purpose defined in your license-plan.
   Feel free to contact the author for licensing-information.
   Please respect the copyright.


   -------
   CREDITS

   Idea &
   Concept:   Martin Sprekelsen <martin@sprekelsen.de>
   Coding:    Martin Sprekelsen <martin@sprekelsen.de>
   Realised:  with kind support of Sintesi Labs Design Group EWIV/GEIE
   Thanks to: Nicole Sargenti <nicole@sintesilabs.eu> for infinite support
              Kirill Myshkin <kirill@sintesilabs.eu> for code and concepts
              Bastian Goecke <goecke@sintesilabs.de> for initial stimulus
              Early adopters: Pille, Wolfi, Bastian, Ben


   ---------
   FILE INFO

   $Revision: 14659 $
   $Date: 2021-07-27 14:44:26 +0200 (Di, 27 Jul 2021) $
   $Author: sintesilabs $

 */
error_reporting(E_ALL);
ini_set('display_errors', 1);


/*----------------------------------+
 | INIT                             |
 +----------------------------------*/

   ffBootloader::boot();



/*----------------------------------+
 | BACKWARDS-COMPATIBILITY          |
 +----------------------------------*/

   ffLegacy::load();



/*----------------------------------+
 | DISPATCH LOW-LEVEL CALLERS       |
 +----------------------------------*/

   ffCall::listen();



/*----------------------------------+
 | BUILD STRUCTURE TREE             |
 +----------------------------------*/

   $ffTree = new ffTree();           // init site structure

   $ffTree->load();                  // load saved structure

   $ffTree->userUpdates();           // listen to user updates

   $ffTree->build();                 // build structure-tree



/*----------------------------------+
 | OUTPUT                           |
 +----------------------------------*/

   (new ffSetup($ffTree))->view();    // build site and output view



/*----------------------------------+
 | BOOTLOADER                       |
 +----------------------------------*/

class ffBootloader{

	private static $sys = 'system';

	public static function boot() {

		// if ff is called from cli, it needs to set its cwd to its root dir first
		// https://stackoverflow.com/a/192145
		if (PHP_SAPI === 'cli') {
			chdir(dirname(__FILE__));
		}

		// ini-file in cwd may point to a custom system path
		$ini = getcwd() . '/ff.ini.php';
		if (is_file($ini)) {
			$ini = parse_ini_string(file_get_contents($ini));
			self::$sys = !isset($ini['ffSystemPath']) ?: rtrim($ini['ffSystemPath'], '/\\');
		}

		// init session
		self::load('ffSession');
		ffSession::start();

		// hand over to autoloader
		self::load('ffAutoloader');
		spl_autoload_register('ffAutoloader::autoload');
	}

	private static function load($class) {
		$path = self::$sys . '/plugins/ff/classes/' . $class . '.php';
		if (is_file($path)) {
			include_once($path);
			return;
		}

		http_response_code(404);
		die('ff is missing');
	}
}