Adding modifications from kritbit
This commit is contained in:
		@@ -1,15 +1,15 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
$config["DEFAULT_ROUTE"] = "main";
 | 
			
		||||
$config["USE_H20_TPL"] = true;
 | 
			
		||||
$config["SMTP_SERVER"] = "localhost";
 | 
			
		||||
$config["SMTP_USER"] = "";
 | 
			
		||||
$config["SMTP_PASS"] = "";
 | 
			
		||||
$config["SMTP_PORT"] = 25;
 | 
			
		||||
$config["SMTP_FROM"] = "HF-noreply@yoursite.com";
 | 
			
		||||
$config["DEBUG"] = true;
 | 
			
		||||
$config["USE_HF_SMTP"] = true;
 | 
			
		||||
$config["SITE_NAME"] = "default";
 | 
			
		||||
$config["SITE_URL"] = "";
 | 
			
		||||
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
$config["DEFAULT_ROUTE"] = "main";
 | 
			
		||||
$config["USE_H20_TPL"] = true;
 | 
			
		||||
$config["SMTP_SERVER"] = "localhost";
 | 
			
		||||
$config["SMTP_USER"] = "";
 | 
			
		||||
$config["SMTP_PASS"] = "";
 | 
			
		||||
$config["SMTP_PORT"] = 25;
 | 
			
		||||
$config["SMTP_FROM"] = "HF-noreply@yoursite.com";
 | 
			
		||||
$config["DEBUG"] = true;
 | 
			
		||||
$config["USE_HF_SMTP"] = true;
 | 
			
		||||
$config["SITE_NAME"] = "default";
 | 
			
		||||
$config["SITE_URL"] = "";
 | 
			
		||||
 | 
			
		||||
return $config;
 | 
			
		||||
@@ -1,42 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
class HF_Controller
 | 
			
		||||
{
 | 
			
		||||
    protected $config;
 | 
			
		||||
    protected $tpl;
 | 
			
		||||
    /** @var HF_Core  */
 | 
			
		||||
    protected $core;
 | 
			
		||||
 | 
			
		||||
    public function __construct($config, $core, $tpl = null)
 | 
			
		||||
    {
 | 
			
		||||
        $this->config = $config;
 | 
			
		||||
        $this->tpl = $tpl;
 | 
			
		||||
        $this->core = $core;
 | 
			
		||||
        $this->initdb();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function initdb()
 | 
			
		||||
    {
 | 
			
		||||
        if (isvarset($this->config["MYSQL_DBNAME"]) && isvarset($this->config["MYSQL_USER"]))
 | 
			
		||||
        {
 | 
			
		||||
            $pdo = new PDO(
 | 
			
		||||
                "mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}",
 | 
			
		||||
                $this->config['MYSQL_USER'],
 | 
			
		||||
                $this->config['MYSQL_PASS'],
 | 
			
		||||
                array(
 | 
			
		||||
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
 | 
			
		||||
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
 | 
			
		||||
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
 | 
			
		||||
                )
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            DB::$c = $pdo;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function loadRender($template, $parameters=array())
 | 
			
		||||
    {
 | 
			
		||||
        $this->tpl->loadTemplate($template);
 | 
			
		||||
        return $this->tpl->render($parameters);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,277 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
include "system/engine/controller.php";
 | 
			
		||||
include "system/engine/SMTP.php";
 | 
			
		||||
include "system/engine/exceptions.php";
 | 
			
		||||
 | 
			
		||||
class HF_Core
 | 
			
		||||
{
 | 
			
		||||
	private $class;
 | 
			
		||||
	private $method;
 | 
			
		||||
	private $classname;
 | 
			
		||||
	private $args = array();
 | 
			
		||||
    private $config = array();
 | 
			
		||||
    private $tpl;
 | 
			
		||||
    private $starttime;
 | 
			
		||||
	
 | 
			
		||||
	public function __construct()
 | 
			
		||||
	{
 | 
			
		||||
        $this->starttime = microtime(true);
 | 
			
		||||
        $config = include("system/engine/config-default.php");
 | 
			
		||||
        if (is_file("application/config.php"))
 | 
			
		||||
        {
 | 
			
		||||
            $newconfig = include("application/config.php");
 | 
			
		||||
        }
 | 
			
		||||
        $this->config = array_merge($config, $newconfig);
 | 
			
		||||
        if ($this->config["USE_H20_TPL"])
 | 
			
		||||
            $this->tpl = new H2o(null, array(
 | 
			
		||||
                "searchpath" => getcwd() . "/application/views/",
 | 
			
		||||
                "cache_dir" => "application/tmp/",
 | 
			
		||||
                'cache' => 'file'
 | 
			
		||||
            ));
 | 
			
		||||
        set_error_handler("HF_Core::error_handler");
 | 
			
		||||
        $this->findController();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    public function getRuntime()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->starttime - microtime(true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function siteURL()
 | 
			
		||||
    {
 | 
			
		||||
        if (isvarset($this->config["SITE_URL"]))
 | 
			
		||||
        {
 | 
			
		||||
            return $this->config["SITE_URL"];
 | 
			
		||||
        }
 | 
			
		||||
        $path = explode("/", $_SERVER["REQUEST_URI"]);
 | 
			
		||||
        $path = array_filter($path, 'strlen');
 | 
			
		||||
        if (count($path) == 0)
 | 
			
		||||
        {
 | 
			
		||||
            return  $_SERVER["HTTP_HOST"] . "/";
 | 
			
		||||
        } else {
 | 
			
		||||
            if (in_array($this->classname, $path))
 | 
			
		||||
            {
 | 
			
		||||
                $newpath = implode("/", array_splice($path, 0, -2));
 | 
			
		||||
                return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
 | 
			
		||||
            } else {
 | 
			
		||||
                $newpath = implode("/", $path);
 | 
			
		||||
                return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
	
 | 
			
		||||
	private function findController()
 | 
			
		||||
	{
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            if (isvarset($_SERVER["PATH_INFO"]))
 | 
			
		||||
            {
 | 
			
		||||
                $request = $_SERVER["PATH_INFO"];
 | 
			
		||||
                //$request = $_SERVER["PHP_SELF"];
 | 
			
		||||
                $splitreq = explode("/", $request);
 | 
			
		||||
                /*$request = "";
 | 
			
		||||
                for($i = 0; $i < count($splitreq); $i++)
 | 
			
		||||
                {
 | 
			
		||||
                    if ($splitreq[$i] == "index.php")
 | 
			
		||||
                    {
 | 
			
		||||
                        $request = implode("/", array_splice($splitreq, $i+1));
 | 
			
		||||
                    }
 | 
			
		||||
                }*/
 | 
			
		||||
                //print $request;
 | 
			
		||||
                //$request = substr($request, 1);
 | 
			
		||||
                //$request = substr($request, 0, -1);
 | 
			
		||||
            } else {
 | 
			
		||||
                $request = "";
 | 
			
		||||
            }
 | 
			
		||||
            if ($request == "" || $request == "/")
 | 
			
		||||
            {
 | 
			
		||||
                require("application/controllers/" . $this->config["DEFAULT_ROUTE"] . ".php");
 | 
			
		||||
                $this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            if ($request[strlen($request)-1] == "/")
 | 
			
		||||
                $request = substr($request, 0, -1);
 | 
			
		||||
            $arr = explode("/", $request);
 | 
			
		||||
            $path = "application/controllers/";
 | 
			
		||||
            for($i = 0; $i < count($arr); $i++)
 | 
			
		||||
            {
 | 
			
		||||
                if (is_file($path . $arr[$i] . ".php")) // found the controller
 | 
			
		||||
                {
 | 
			
		||||
                    include($path . $arr[$i] . ".php");
 | 
			
		||||
                    if ($i + 1 < count($arr)) // if there is a define after the controller name - this would be the method name
 | 
			
		||||
                    {
 | 
			
		||||
                        $this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], $arr[$i+1], array_slice ($arr, 2));
 | 
			
		||||
                    } else { // call index
 | 
			
		||||
                        $this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], "index");
 | 
			
		||||
                    }
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (is_dir($path . $arr[$i])) // controller is hidden deeper
 | 
			
		||||
                {
 | 
			
		||||
                    $path = $path . $arr[$i] . "/";
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                include($path . $this->config["DEFAULT_ROUTE"] . ".php");
 | 
			
		||||
                $this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
 | 
			
		||||
                //$this->load404Controller();
 | 
			
		||||
                break;
 | 
			
		||||
                // throw exception controller not found
 | 
			
		||||
            }
 | 
			
		||||
        } catch (Exception $e) {
 | 
			
		||||
            if ($this->config["DEBUG"])
 | 
			
		||||
                echo vdump($e, $this);
 | 
			
		||||
            else
 | 
			
		||||
                $this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
 | 
			
		||||
        }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    private function load404Controller()
 | 
			
		||||
    {
 | 
			
		||||
        if (is_file(getcwd() . "/application/status.php"))
 | 
			
		||||
        {
 | 
			
		||||
            include_once (getcwd() . "/application/status.php");
 | 
			
		||||
            $this->loadController(new status($this->config, $this, $this->tpl), "status", "Status404");
 | 
			
		||||
        } else {
 | 
			
		||||
            include_once(getcwd() . "/system/engine/status.php");
 | 
			
		||||
            $this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status404");
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function load500Controller()
 | 
			
		||||
    {
 | 
			
		||||
        if (is_file(getcwd() . "/application/status.php"))
 | 
			
		||||
        {
 | 
			
		||||
            include_once (getcwd() . "/application/status.php");
 | 
			
		||||
            $this->loadController(new status($this->config, $this, $this->tpl), "status", "Status500");
 | 
			
		||||
        } else {
 | 
			
		||||
            include_once (getcwd() . "/system/engine/status.php");
 | 
			
		||||
            $this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status500");
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function loadController($class, $classname, $method, $args = array())
 | 
			
		||||
    {
 | 
			
		||||
        $this->class = $class;
 | 
			
		||||
        $this->classname = $classname;
 | 
			
		||||
        $this->method = $method;
 | 
			
		||||
        $this->args = $args;
 | 
			
		||||
    }
 | 
			
		||||
	
 | 
			
		||||
	public function run($err=false)
 | 
			
		||||
	{
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            $call = new ReflectionMethod($this->classname, $this->method);
 | 
			
		||||
            if ($err)
 | 
			
		||||
            {
 | 
			
		||||
                $call->invokeArgs($this->class, $this->args);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $numOfReqPara = $call->getNumberOfRequiredParameters();
 | 
			
		||||
            $numOfOptPara = $call->getNumberOfParameters() - $numOfReqPara;
 | 
			
		||||
            $remainparas = count($this->args) - $numOfReqPara;
 | 
			
		||||
            if ($numOfReqPara == 0 || ($remainparas >= 0 && $remainparas <= $numOfOptPara))
 | 
			
		||||
            {
 | 
			
		||||
                $call->invokeArgs($this->class, $this->args);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                $this->load404Controller();
 | 
			
		||||
                $this->run(true);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } catch (ReflectionException $e)
 | 
			
		||||
        {
 | 
			
		||||
            if (strstr($e->getMessage(), "does not exist") !== false)
 | 
			
		||||
            {
 | 
			
		||||
                $this->load404Controller();
 | 
			
		||||
            } else {
 | 
			
		||||
                $this->load500Controller();
 | 
			
		||||
            }
 | 
			
		||||
            $this->run(true);
 | 
			
		||||
            if ($this->config["DEBUG"])
 | 
			
		||||
                echo vdump($e, $this);
 | 
			
		||||
            else
 | 
			
		||||
                $this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        } catch (Exception $e) {
 | 
			
		||||
            $this->load500Controller();
 | 
			
		||||
            $this->run(true);
 | 
			
		||||
            if ($this->config["DEBUG"])
 | 
			
		||||
                echo vdump($e, $this);
 | 
			
		||||
            else
 | 
			
		||||
                $this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
 | 
			
		||||
        }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    public function mail_admins($subject, $msg, $html = false)
 | 
			
		||||
    {
 | 
			
		||||
        if (array_key_exists("ADMINS", $this->config))
 | 
			
		||||
        {
 | 
			
		||||
            foreach($this->config["ADMINS"] as $email)
 | 
			
		||||
            {
 | 
			
		||||
                $this->mail_user($email, $subject, $msg, $html);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function mail_user($to, $subject, $msg, $html = false)
 | 
			
		||||
    {
 | 
			
		||||
        if ($this->config["USE_HF_SMTP"])
 | 
			
		||||
        {
 | 
			
		||||
            $smtp = new HF_SMTP($this->config["SMTP_FROM"], $to, $subject, $msg, $this->config["SMTP_SERVER"], $this->config["SMTP_USER"], $this->config["SMTP_PASS"], $this->config["SMTP_PORT"]);
 | 
			
		||||
            $smtp->send($html);
 | 
			
		||||
        } else {
 | 
			
		||||
            require_once "Mail.php";
 | 
			
		||||
            $smtp = null;
 | 
			
		||||
            if ($this->$this->config["SMTP_USER"] && $this->config["SMTP_PASS"])
 | 
			
		||||
                $smtp = Mail::factory('smtp', array(
 | 
			
		||||
                    "host" => $this->config["SMTP_SERVER"],
 | 
			
		||||
                    "port" => $this->config["SMTP_PORT"],
 | 
			
		||||
                    "auth" => true,
 | 
			
		||||
                    'username' => $this->config["SMTP_USER"],
 | 
			
		||||
                    'password' => $this->config["SMTP_PASS"]
 | 
			
		||||
                ));
 | 
			
		||||
            else
 | 
			
		||||
                $smtp = Mail::factory('smtp', array(
 | 
			
		||||
                    "host" => $this->config["SMTP_SERVER"],
 | 
			
		||||
                    "port" => $this->config["SMTP_PORT"]
 | 
			
		||||
                ));
 | 
			
		||||
            $headers = array ('From' => $this->config["SMTP_FROM"],
 | 
			
		||||
                'To' => $to,
 | 
			
		||||
                'Subject' => $subject);
 | 
			
		||||
            $smtp->send($to, $headers, $msg);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function error_handler($err_severity, $err_msg, $err_file, $err_line, array $err_context)
 | 
			
		||||
    {
 | 
			
		||||
        if (0 === error_reporting()) { return false;}
 | 
			
		||||
        switch($err_severity)
 | 
			
		||||
        {
 | 
			
		||||
            case E_ERROR:               throw new ErrorException            ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_WARNING:             throw new WarningException          ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_PARSE:               throw new ParseException            ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_NOTICE:              throw new NoticeException           ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_CORE_ERROR:          throw new CoreErrorException        ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_CORE_WARNING:        throw new CoreWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_COMPILE_ERROR:       throw new CompileErrorException     ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_COMPILE_WARNING:     throw new CoreWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_USER_ERROR:          throw new UserErrorException        ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_USER_WARNING:        throw new UserWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_USER_NOTICE:         throw new UserNoticeException       ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_STRICT:              throw new StrictException           ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_RECOVERABLE_ERROR:   throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_DEPRECATED:          throw new DeprecatedException       ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
            case E_USER_DEPRECATED:     throw new UserDeprecatedException   ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,18 +1,20 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
//Original idea by http://www.php.net/manual/en/function.set-error-handler.php#112881
 | 
			
		||||
 | 
			
		||||
class WarningException              extends ErrorException {}
 | 
			
		||||
class ParseException                extends ErrorException {}
 | 
			
		||||
class NoticeException               extends ErrorException {}
 | 
			
		||||
class CoreErrorException            extends ErrorException {}
 | 
			
		||||
class CoreWarningException          extends ErrorException {}
 | 
			
		||||
class CompileErrorException         extends ErrorException {}
 | 
			
		||||
class CompileWarningException       extends ErrorException {}
 | 
			
		||||
class UserErrorException            extends ErrorException {}
 | 
			
		||||
class UserWarningException          extends ErrorException {}
 | 
			
		||||
class UserNoticeException           extends ErrorException {}
 | 
			
		||||
class StrictException               extends ErrorException {}
 | 
			
		||||
class RecoverableErrorException     extends ErrorException {}
 | 
			
		||||
class DeprecatedException           extends ErrorException {}
 | 
			
		||||
class UserDeprecatedException       extends ErrorException {}
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace system\engine;
 | 
			
		||||
 | 
			
		||||
//Original idea by http://www.php.net/manual/en/function.set-error-handler.php#112881
 | 
			
		||||
 | 
			
		||||
class WarningException              extends \ErrorException {}
 | 
			
		||||
class ParseException                extends \ErrorException {}
 | 
			
		||||
class NoticeException               extends \ErrorException {}
 | 
			
		||||
class CoreErrorException            extends \ErrorException {}
 | 
			
		||||
class CoreWarningException          extends \ErrorException {}
 | 
			
		||||
class CompileErrorException         extends \ErrorException {}
 | 
			
		||||
class CompileWarningException       extends \ErrorException {}
 | 
			
		||||
class UserErrorException            extends \ErrorException {}
 | 
			
		||||
class UserWarningException          extends \ErrorException {}
 | 
			
		||||
class UserNoticeException           extends \ErrorException {}
 | 
			
		||||
class StrictException               extends \ErrorException {}
 | 
			
		||||
class RecoverableErrorException     extends \ErrorException {}
 | 
			
		||||
class DeprecatedException           extends \ErrorException {}
 | 
			
		||||
class UserDeprecatedException       extends \ErrorException {}
 | 
			
		||||
							
								
								
									
										24
									
								
								system/engine/hf_controller.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								system/engine/hf_controller.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace system\engine;
 | 
			
		||||
 | 
			
		||||
class HF_Controller
 | 
			
		||||
{
 | 
			
		||||
    protected $config;
 | 
			
		||||
    protected $tpl;
 | 
			
		||||
    protected $core;
 | 
			
		||||
 | 
			
		||||
    public function __construct($config, $core, $tpl = null)
 | 
			
		||||
    {
 | 
			
		||||
        $this->config = $config;
 | 
			
		||||
        $this->tpl = $tpl;
 | 
			
		||||
        $this->core = $core;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function loadRender($template, $parameters=array())
 | 
			
		||||
    {
 | 
			
		||||
        $this->tpl->loadTemplate($template);
 | 
			
		||||
        return $this->tpl->render($parameters);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										365
									
								
								system/engine/hf_core.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										365
									
								
								system/engine/hf_core.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,365 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
/*include "system/engine/hf_controller.php";
 | 
			
		||||
include "system/engine/smtp.php";
 | 
			
		||||
include "system/engine/exceptions.php";
 | 
			
		||||
include "system/engine/hf_model.php";*/
 | 
			
		||||
 | 
			
		||||
namespace system\engine;
 | 
			
		||||
 | 
			
		||||
use vendor\DB\DB;
 | 
			
		||||
 | 
			
		||||
include "exceptions.php";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class HF_Core
 | 
			
		||||
{
 | 
			
		||||
	private $class;
 | 
			
		||||
	private $method;
 | 
			
		||||
	private $classname;
 | 
			
		||||
	private $args = array();
 | 
			
		||||
	private $config = array();
 | 
			
		||||
	private $tpl;
 | 
			
		||||
	
 | 
			
		||||
	public function __construct($migrations=false)
 | 
			
		||||
	{
 | 
			
		||||
		$config = include("system/engine/config-default.php");
 | 
			
		||||
		if (is_file("application/config.php"))
 | 
			
		||||
		{
 | 
			
		||||
			$newconfig = include("application/config.php");
 | 
			
		||||
		}
 | 
			
		||||
		$this->config = array_merge($config, $newconfig);
 | 
			
		||||
		\vendor\DB\DB::$type = $config["DATABASE_TYPE"];
 | 
			
		||||
		if ($this->config["USE_H20_TPL"])
 | 
			
		||||
			$this->tpl = new \H2o(null, array(
 | 
			
		||||
				"searchpath" => getcwd() . "/application/views/",
 | 
			
		||||
				"cache_dir" => "application/tmp/",
 | 
			
		||||
				'cache' => 'file'
 | 
			
		||||
			));
 | 
			
		||||
		set_error_handler("\\system\\engine\\HF_Core::error_handler");
 | 
			
		||||
		//set_exception_handler("\\system\\engine\\HF_Core::exception_handler");
 | 
			
		||||
		if (!$migrations)
 | 
			
		||||
			$this->findController();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static function exception_handler($e) {
 | 
			
		||||
		echo "Hello";
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function siteURL()
 | 
			
		||||
	{
 | 
			
		||||
		if (isvarset($this->config["SITE_URL"]))
 | 
			
		||||
		{
 | 
			
		||||
			return $this->config["SITE_URL"];
 | 
			
		||||
		}
 | 
			
		||||
		$path = explode("/", $_SERVER["REQUEST_URI"]);
 | 
			
		||||
		$path = array_filter($path, 'strlen');
 | 
			
		||||
		if (count($path) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			return  $_SERVER["HTTP_HOST"] . "/";
 | 
			
		||||
		} else {
 | 
			
		||||
			if (in_array($this->classname, $path))
 | 
			
		||||
			{
 | 
			
		||||
				$newpath = implode("/", array_splice($path, 0, -2));
 | 
			
		||||
				return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
 | 
			
		||||
			} else {
 | 
			
		||||
				$newpath = implode("/", $path);
 | 
			
		||||
				return $_SERVER["HTTP_HOST"] . "/" . $newpath . "/";
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private function findController()
 | 
			
		||||
	{
 | 
			
		||||
		try
 | 
			
		||||
		{
 | 
			
		||||
			if (isvarset($_SERVER["PATH_INFO"]))
 | 
			
		||||
			{
 | 
			
		||||
				$request = $_SERVER["PATH_INFO"];
 | 
			
		||||
				//$request = $_SERVER["PHP_SELF"];
 | 
			
		||||
				$splitreq = explode("/", $request);
 | 
			
		||||
				/*$request = "";
 | 
			
		||||
				for($i = 0; $i < count($splitreq); $i++)
 | 
			
		||||
				{
 | 
			
		||||
					if ($splitreq[$i] == "index.php")
 | 
			
		||||
					{
 | 
			
		||||
						$request = implode("/", array_splice($splitreq, $i+1));
 | 
			
		||||
					}
 | 
			
		||||
				}*/
 | 
			
		||||
				//print $request;
 | 
			
		||||
				//$request = substr($request, 1);
 | 
			
		||||
				//$request = substr($request, 0, -1);
 | 
			
		||||
			} else {
 | 
			
		||||
				$request = "";
 | 
			
		||||
			}
 | 
			
		||||
			if ($request == "" || $request == "/")
 | 
			
		||||
			{
 | 
			
		||||
				require_once("application/controllers/" . $this->config["DEFAULT_ROUTE"] . ".php");
 | 
			
		||||
				$this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
			if ($request[strlen($request)-1] == "/")
 | 
			
		||||
				$request = substr($request, 0, -1);
 | 
			
		||||
			$arr = explode("/", $request);
 | 
			
		||||
			$path = "application/controllers/";
 | 
			
		||||
			for($i = 0; $i < count($arr); $i++)
 | 
			
		||||
			{
 | 
			
		||||
				if (is_file($path . $arr[$i] . ".php")) // found the controller
 | 
			
		||||
				{
 | 
			
		||||
					include_once($path . $arr[$i] . ".php");
 | 
			
		||||
					if ($i + 1 < count($arr)) // if there is a define after the controller name - this would be the method name
 | 
			
		||||
					{
 | 
			
		||||
						$this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], $arr[$i+1], array_slice ($arr, 3));
 | 
			
		||||
					} else { // call index
 | 
			
		||||
						$this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], "index");
 | 
			
		||||
					}
 | 
			
		||||
					return;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				if (is_dir($path . $arr[$i])) // controller is hidden deeper
 | 
			
		||||
				{
 | 
			
		||||
					$path = $path . $arr[$i] . "/";
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				include_once($path . $this->config["DEFAULT_ROUTE"] . ".php");
 | 
			
		||||
				$this->loadController(new $this->config["DEFAULT_ROUTE"]($this->config, $this, $this->tpl), $this->config["DEFAULT_ROUTE"], "index");
 | 
			
		||||
				//$this->load404Controller();
 | 
			
		||||
				break;
 | 
			
		||||
				// throw exception controller not found
 | 
			
		||||
			}
 | 
			
		||||
		} catch (\Exception $e) {
 | 
			
		||||
			if ($this->config["DEBUG"])
 | 
			
		||||
				echo vdump($e, $this);
 | 
			
		||||
			else
 | 
			
		||||
				$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function load404Controller()
 | 
			
		||||
	{
 | 
			
		||||
		if (is_file(getcwd() . "/application/status.php"))
 | 
			
		||||
		{
 | 
			
		||||
			include_once (getcwd() . "/application/status.php");
 | 
			
		||||
			$this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status404");
 | 
			
		||||
		} else {
 | 
			
		||||
			include_once(getcwd() . "/system/engine/status.php");
 | 
			
		||||
			$this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status404");
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function load500Controller()
 | 
			
		||||
	{
 | 
			
		||||
		if (is_file(getcwd() . "/application/status.php"))
 | 
			
		||||
		{
 | 
			
		||||
			include_once (getcwd() . "/application/status.php");
 | 
			
		||||
			$this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status500");
 | 
			
		||||
		} else {
 | 
			
		||||
			include_once (getcwd() . "/system/engine/status.php");
 | 
			
		||||
			$this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status500");
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function loadController($class, $classname, $method, $args = array())
 | 
			
		||||
	{
 | 
			
		||||
		$this->class = $class;
 | 
			
		||||
		$this->classname = $classname;
 | 
			
		||||
		$this->method = $method;
 | 
			
		||||
		$this->args = $args;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public function run($err=false)
 | 
			
		||||
	{
 | 
			
		||||
		try
 | 
			
		||||
		{
 | 
			
		||||
			$call = new \ReflectionMethod($this->classname, $this->method);
 | 
			
		||||
			if ($err)
 | 
			
		||||
			{
 | 
			
		||||
				$call->invokeArgs($this->class, $this->args);
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			$numOfReqPara = $call->getNumberOfRequiredParameters();
 | 
			
		||||
			$numOfOptPara = $call->getNumberOfParameters() - $numOfReqPara;
 | 
			
		||||
			$remainparas = count($this->args) - $numOfReqPara;
 | 
			
		||||
			if ($numOfReqPara == 0 || ($remainparas >= 0 && $remainparas <= $numOfOptPara))
 | 
			
		||||
			{
 | 
			
		||||
				$call->invokeArgs($this->class, $this->args);
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				$this->load404Controller();
 | 
			
		||||
				$this->run(true);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		catch (\ReflectionException $e)
 | 
			
		||||
		{
 | 
			
		||||
			if (strstr($e->getMessage(), "does not exist") !== false)
 | 
			
		||||
			{
 | 
			
		||||
				$this->load404Controller();
 | 
			
		||||
			} else {
 | 
			
		||||
				$this->load500Controller();
 | 
			
		||||
			}
 | 
			
		||||
			$this->run(true);
 | 
			
		||||
			if ($this->config["DEBUG"])
 | 
			
		||||
				echo vdump($e, $this);
 | 
			
		||||
			else
 | 
			
		||||
				$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		} catch (\Exception $e) {
 | 
			
		||||
			$this->load500Controller();
 | 
			
		||||
			$this->run(true);
 | 
			
		||||
			if ($this->config["DEBUG"])
 | 
			
		||||
				echo vdump($e, $this);
 | 
			
		||||
			else
 | 
			
		||||
				$this->mail_admins("[Exception - " . $this->config["SITE_NAME"] . "]", vdump($e, $this), true);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function mail_admins($subject, $msg, $html = false)
 | 
			
		||||
	{
 | 
			
		||||
		if (array_key_exists("ADMINS", $this->config))
 | 
			
		||||
		{
 | 
			
		||||
			foreach($this->config["ADMINS"] as $email)
 | 
			
		||||
			{
 | 
			
		||||
				$this->mail_user($email, $subject, $msg, $html);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function mail_user($to, $subject, $msg, $html = false)
 | 
			
		||||
	{
 | 
			
		||||
		if ($this->config["USE_HF_SMTP"])
 | 
			
		||||
		{
 | 
			
		||||
			$smtp = new HF_SMTP($this->config["SMTP_FROM"], $to, $subject, $msg, $this->config["SMTP_SERVER"], $this->config["SMTP_USER"], $this->config["SMTP_PASS"], $this->config["SMTP_PORT"]);
 | 
			
		||||
			$smtp->send($html);
 | 
			
		||||
		} else {
 | 
			
		||||
			require_once "Mail.php";
 | 
			
		||||
			$smtp = null;
 | 
			
		||||
			if ($this->$this->config["SMTP_USER"] && $this->config["SMTP_PASS"])
 | 
			
		||||
				$smtp = Mail::factory('smtp', array(
 | 
			
		||||
					"host" => $this->config["SMTP_SERVER"],
 | 
			
		||||
					"port" => $this->config["SMTP_PORT"],
 | 
			
		||||
					"auth" => true,
 | 
			
		||||
					'username' => $this->config["SMTP_USER"],
 | 
			
		||||
					'password' => $this->config["SMTP_PASS"]
 | 
			
		||||
				));
 | 
			
		||||
			else
 | 
			
		||||
				$smtp = Mail::factory('smtp', array(
 | 
			
		||||
					"host" => $this->config["SMTP_SERVER"],
 | 
			
		||||
					"port" => $this->config["SMTP_PORT"]
 | 
			
		||||
				));
 | 
			
		||||
			$headers = array ('From' => $this->config["SMTP_FROM"],
 | 
			
		||||
				'To' => $to,
 | 
			
		||||
				'Subject' => $subject);
 | 
			
		||||
			$smtp->send($to, $headers, $msg);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static function error_handler($err_severity, $err_msg, $err_file, $err_line, array $err_context)
 | 
			
		||||
	{
 | 
			
		||||
		if (0 === error_reporting()) { return false;}
 | 
			
		||||
		switch($err_severity)
 | 
			
		||||
		{
 | 
			
		||||
			case E_ERROR:               throw new \ErrorException            ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_WARNING:             throw new WarningException          ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_PARSE:               throw new ParseException            ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_NOTICE:              throw new NoticeException           ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_CORE_ERROR:          throw new CoreErrorException        ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_CORE_WARNING:        throw new CoreWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_COMPILE_ERROR:       throw new CompileErrorException     ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_COMPILE_WARNING:     throw new CoreWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_USER_ERROR:          throw new UserErrorException        ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_USER_WARNING:        throw new UserWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_USER_NOTICE:         throw new UserNoticeException       ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_STRICT:              throw new StrictException           ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_RECOVERABLE_ERROR:   throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_DEPRECATED:          throw new DeprecatedException       ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
			case E_USER_DEPRECATED:     throw new UserDeprecatedException   ($err_msg, 0, $err_severity, $err_file, $err_line);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function setupDatabaseConnection() {
 | 
			
		||||
		switch($this->config["DATABASE_TYPE"]) {
 | 
			
		||||
			case "SQLITE":
 | 
			
		||||
				DB::$c = new \PDO("sqlite:" . $this->config["DATABASE_FILE"]);
 | 
			
		||||
				break;
 | 
			
		||||
			case "MySQL":
 | 
			
		||||
				DB::$c = new \PDO(
 | 
			
		||||
						"mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}",
 | 
			
		||||
						$this->config['MYSQL_USER'],
 | 
			
		||||
						$this->config['MYSQL_PASS'],
 | 
			
		||||
						array(
 | 
			
		||||
								\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
 | 
			
		||||
								\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_OBJ,
 | 
			
		||||
								\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
 | 
			
		||||
						)
 | 
			
		||||
				);
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
		DB::$c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function runMigrations() {
 | 
			
		||||
        global $argv;
 | 
			
		||||
		$this->setupDatabaseConnection();
 | 
			
		||||
        DB::query("CREATE TABLE IF NOT EXISTS migrations (
 | 
			
		||||
							  id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
 | 
			
		||||
							  migration INTEGER,
 | 
			
		||||
							  ran_at DATETIME
 | 
			
		||||
				)");
 | 
			
		||||
        switch ($argv[1]) {
 | 
			
		||||
            case "show":
 | 
			
		||||
                foreach(DB::fetch("SELECT migration, ran_at FROM migrations") as $migration) {
 | 
			
		||||
                    echo $migration["migration"] . " => " . $migration["ran_at"] . PHP_EOL;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case "count":
 | 
			
		||||
                echo DB::column("SELECT COUNT(id) FROM migrations");
 | 
			
		||||
                break;
 | 
			
		||||
            case "run":
 | 
			
		||||
                $migrations = DB::fetch("SELECT migration FROM migrations");
 | 
			
		||||
                $migrationArray = [];
 | 
			
		||||
                foreach($migrations as $migration) {
 | 
			
		||||
                    $migrationArray[] = $migration["migration"];
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                foreach (glob("application/migrations/*.php") as $filename)
 | 
			
		||||
                {
 | 
			
		||||
                    if (!in_array($filename, $migrationArray)) {
 | 
			
		||||
						try {
 | 
			
		||||
							include $filename;
 | 
			
		||||
							DB::insert("migrations", ["migration" => $filename, "ran_at" => (new \DateTime())->format("Y-m-d")]);
 | 
			
		||||
						} catch (\Exception $e) {
 | 
			
		||||
							echo "[HF_Core] - Migration error - $e";
 | 
			
		||||
							exit(1);
 | 
			
		||||
						}
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case "clear":
 | 
			
		||||
                DB::query("DELETE FROM migrations");
 | 
			
		||||
                break;
 | 
			
		||||
            case "reset":
 | 
			
		||||
                switch($this->config["DATABASE_TYPE"]) {
 | 
			
		||||
                    case "SQLITE":
 | 
			
		||||
                        DB::$c = null;
 | 
			
		||||
                        unlink($this->config["DATABASE_FILE"]);
 | 
			
		||||
                        break;
 | 
			
		||||
                    case "MYSQL":
 | 
			
		||||
                        DB::query("DROP DATABASE " . $this->config['MYSQL_DBNAME']);
 | 
			
		||||
                        DB::query("CREATE DATABASE " . $this->config['MYSQL_DBNAME']);
 | 
			
		||||
                        break;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										77
									
								
								system/engine/hf_model.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								system/engine/hf_model.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,77 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace system\engine;
 | 
			
		||||
 | 
			
		||||
use \vendor\DB\DB;
 | 
			
		||||
 | 
			
		||||
abstract class HF_Model {
 | 
			
		||||
 | 
			
		||||
    public $id = null;
 | 
			
		||||
    public static function create($data) {
 | 
			
		||||
 | 
			
		||||
        $obj = new static();
 | 
			
		||||
        $function = new \ReflectionClass(get_called_class());
 | 
			
		||||
        $table = strtolower($function->getShortName());
 | 
			
		||||
 | 
			
		||||
        foreach(DB::getColumns($table) as $column) {
 | 
			
		||||
            if (isset($data[$column])) {
 | 
			
		||||
                $obj->$column = $data[$column];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return $obj;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function save() {
 | 
			
		||||
        $fieldMap = [];
 | 
			
		||||
        $function = new \ReflectionClass(get_called_class());
 | 
			
		||||
        $table = strtolower($function->getShortName());
 | 
			
		||||
        foreach(DB::getColumns($table) as $column) {
 | 
			
		||||
            $fieldMap[$column] = $this->$column;
 | 
			
		||||
        }
 | 
			
		||||
        if ($fieldMap["id"] == null) {
 | 
			
		||||
            DB::insert($table, $fieldMap);
 | 
			
		||||
        } else {
 | 
			
		||||
            $updateFields = $fieldMap;
 | 
			
		||||
            unset($updateFields["id"]);
 | 
			
		||||
            DB::update($table, $updateFields, $fieldMap["id"]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function update($data) {
 | 
			
		||||
        $function = new \ReflectionClass(get_called_class());
 | 
			
		||||
        $table = strtolower($function->getShortName());
 | 
			
		||||
        foreach(DB::getColumns($table) as $column) {
 | 
			
		||||
            if ($column == "id" || strpos($column, "_id") !== false) {
 | 
			
		||||
                continue; // Don't allow to override id
 | 
			
		||||
            }
 | 
			
		||||
            if (isset($data[$column])) {
 | 
			
		||||
                $this->$column = $data[$column];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function delete() {
 | 
			
		||||
        $function = new \ReflectionClass(get_called_class());
 | 
			
		||||
        $table = strtolower($function->getShortName());
 | 
			
		||||
        if ($this->id) {
 | 
			
		||||
            DB::query("DELETE FROM $table WHERE id = " . $this->id);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function deleteRelated($tables = []) {
 | 
			
		||||
        $function = new \ReflectionClass(get_called_class());
 | 
			
		||||
        $table = strtolower($function->getShortName());
 | 
			
		||||
        foreach($tables as $relatedTable) {
 | 
			
		||||
            DB::query("DELETE FROM $relatedTable WHERE $table" . "_id = " . $this->id);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function getByField($field, $value) {
 | 
			
		||||
        $function = new \ReflectionClass(get_called_class());
 | 
			
		||||
        $table = strtolower($function->getShortName());
 | 
			
		||||
        $fields = implode(", ", DB::getColumns($table));
 | 
			
		||||
        return DB::fetchObject("SELECT $fields FROM $table WHERE $field = ?", get_called_class(), [$value]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,80 +1,82 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
// Based on tutorial from here: https://portal.cyberhostpro.com/knowledgebase/170/PHP-Mail-Script-with-SMTP-Authentication.html
 | 
			
		||||
class HF_SMTP
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    private $from = "";
 | 
			
		||||
    private $to = "";
 | 
			
		||||
    private $subject = "";
 | 
			
		||||
    private $msg = "";
 | 
			
		||||
    private $user = null;
 | 
			
		||||
    private $password = null;
 | 
			
		||||
    private $port = 25;
 | 
			
		||||
    private $server = "localhost";
 | 
			
		||||
 | 
			
		||||
    private $smtpserverconn = null;
 | 
			
		||||
 | 
			
		||||
    public function __construct($from, $to, $subject, $msg, $server = "localhost", $user = null, $password = null, $port = 25)
 | 
			
		||||
    {
 | 
			
		||||
        $this->from = $from;
 | 
			
		||||
        $this->to = $to;
 | 
			
		||||
        $this->subject = $subject;
 | 
			
		||||
        $this->msg = $msg;
 | 
			
		||||
        $this->user = $user;
 | 
			
		||||
        $this->password = $password;
 | 
			
		||||
        $this->port = $port;
 | 
			
		||||
        $this->server = $server;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function send($html=false)
 | 
			
		||||
    {
 | 
			
		||||
        $err = null;
 | 
			
		||||
        $errstr = "";
 | 
			
		||||
        $this->smtpserverconn = fsockopen($this->server, $this->port, $err, $errstr, 100);
 | 
			
		||||
        $response = fgets($this->smtpserverconn, 515);
 | 
			
		||||
        if ($response === false)
 | 
			
		||||
        {
 | 
			
		||||
            throw new Exception("Could not connect to SMTP server!");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($this->user != null && $this->password != null)
 | 
			
		||||
        {
 | 
			
		||||
            $this->sendCommand("AUTH LOGIN");
 | 
			
		||||
            $this->sendCommand(base64_encode($this->user));
 | 
			
		||||
            $this->sendCommand(base64_encode($this->password));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand("HELO " . $_SERVER["SERVER_NAME"]);
 | 
			
		||||
        $this->sendCommand("MAIL FROM: " . $this->from);
 | 
			
		||||
        $this->sendCommand("RCPT TO: " . $this->to);
 | 
			
		||||
        $this->sendCommand("DATA");
 | 
			
		||||
 | 
			
		||||
        if ($html)
 | 
			
		||||
        {
 | 
			
		||||
            $this->sendCommand("MIME-Version: 1.0", false);
 | 
			
		||||
            $this->sendCommand("Content-type: text/html; charset=iso-8859-1", false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand("From: " . $this->from, false);
 | 
			
		||||
        $this->sendCommand("To: " . $this->to, false);
 | 
			
		||||
        $this->sendCommand("Subject: " . $this->subject, false);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand($this->msg, false);
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand("", false);
 | 
			
		||||
        $this->sendCommand(".", false);
 | 
			
		||||
        $this->sendCommand("QUIT");
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function sendCommand($command, $return = true)
 | 
			
		||||
    {
 | 
			
		||||
        fputs($this->smtpserverconn, $command . "\r\n");
 | 
			
		||||
        if ($return)
 | 
			
		||||
            return fgets($this->smtpserverconn, 515);
 | 
			
		||||
        else
 | 
			
		||||
            return null;
 | 
			
		||||
    }
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace system\engine;
 | 
			
		||||
 | 
			
		||||
// Based on tutorial from here: https://portal.cyberhostpro.com/knowledgebase/170/PHP-Mail-Script-with-SMTP-Authentication.html
 | 
			
		||||
class HF_SMTP
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    private $from = "";
 | 
			
		||||
    private $to = "";
 | 
			
		||||
    private $subject = "";
 | 
			
		||||
    private $msg = "";
 | 
			
		||||
    private $user = null;
 | 
			
		||||
    private $password = null;
 | 
			
		||||
    private $port = 25;
 | 
			
		||||
    private $server = "localhost";
 | 
			
		||||
 | 
			
		||||
    private $smtpserverconn = null;
 | 
			
		||||
 | 
			
		||||
    public function __construct($from, $to, $subject, $msg, $server = "localhost", $user = null, $password = null, $port = 25)
 | 
			
		||||
    {
 | 
			
		||||
        $this->from = $from;
 | 
			
		||||
        $this->to = $to;
 | 
			
		||||
        $this->subject = $subject;
 | 
			
		||||
        $this->msg = $msg;
 | 
			
		||||
        $this->user = $user;
 | 
			
		||||
        $this->password = $password;
 | 
			
		||||
        $this->port = $port;
 | 
			
		||||
        $this->server = $server;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function send($html=false)
 | 
			
		||||
    {
 | 
			
		||||
        $err = null;
 | 
			
		||||
        $errstr = "";
 | 
			
		||||
        $this->smtpserverconn = fsockopen($this->server, $this->port, $err, $errstr, 100);
 | 
			
		||||
        $response = fgets($this->smtpserverconn, 515);
 | 
			
		||||
        if ($response === false)
 | 
			
		||||
        {
 | 
			
		||||
            throw new Exception("Could not connect to SMTP server!");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($this->user != null && $this->password != null)
 | 
			
		||||
        {
 | 
			
		||||
            $this->sendCommand("AUTH LOGIN");
 | 
			
		||||
            $this->sendCommand(base64_encode($this->user));
 | 
			
		||||
            $this->sendCommand(base64_encode($this->password));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand("HELO " . $_SERVER["SERVER_NAME"]);
 | 
			
		||||
        $this->sendCommand("MAIL FROM: " . $this->from);
 | 
			
		||||
        $this->sendCommand("RCPT TO: " . $this->to);
 | 
			
		||||
        $this->sendCommand("DATA");
 | 
			
		||||
 | 
			
		||||
        if ($html)
 | 
			
		||||
        {
 | 
			
		||||
            $this->sendCommand("MIME-Version: 1.0", false);
 | 
			
		||||
            $this->sendCommand("Content-type: text/html; charset=iso-8859-1", false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand("From: " . $this->from, false);
 | 
			
		||||
        $this->sendCommand("To: " . $this->to, false);
 | 
			
		||||
        $this->sendCommand("Subject: " . $this->subject, false);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand($this->msg, false);
 | 
			
		||||
 | 
			
		||||
        $this->sendCommand("", false);
 | 
			
		||||
        $this->sendCommand(".", false);
 | 
			
		||||
        $this->sendCommand("QUIT");
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function sendCommand($command, $return = true)
 | 
			
		||||
    {
 | 
			
		||||
        fputs($this->smtpserverconn, $command . "\r\n");
 | 
			
		||||
        if ($return)
 | 
			
		||||
            return fgets($this->smtpserverconn, 515);
 | 
			
		||||
        else
 | 
			
		||||
            return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,15 +1,17 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
class HF_Status extends HF_Controller
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function Status404()
 | 
			
		||||
    {
 | 
			
		||||
        echo "Page not found!";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function Status500()
 | 
			
		||||
    {
 | 
			
		||||
        echo "System error";
 | 
			
		||||
    }
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace system\engine;
 | 
			
		||||
 | 
			
		||||
class HF_Status extends HF_Controller
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function Status404()
 | 
			
		||||
    {
 | 
			
		||||
        echo "Page not found!";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function Status500()
 | 
			
		||||
    {
 | 
			
		||||
        echo "System error";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user