Adding modifications from kritbit
This commit is contained in:
parent
5b104d624b
commit
f08b2987e1
18
migrations.php
Normal file
18
migrations.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
spl_autoload_extensions(".php"); // comma-separated list
|
||||
spl_autoload_register();
|
||||
|
||||
foreach (glob("system/vendor/*.php") as $filename)
|
||||
{
|
||||
include $filename;
|
||||
}
|
||||
|
||||
if (!is_cli()) {
|
||||
die("This script must be ran from the command line");
|
||||
}
|
||||
|
||||
$core = new \system\engine\HF_Core(true);
|
||||
$core->runMigrations();
|
@ -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
|
||||
|
||||
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 {}
|
||||
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,5 +1,7 @@
|
||||
<?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
|
||||
{
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace system\engine;
|
||||
|
||||
class HF_Status extends HF_Controller
|
||||
{
|
||||
|
||||
|
36
system/vendor/DB.php
vendored
36
system/vendor/DB.php
vendored
@ -10,10 +10,15 @@
|
||||
* @license MIT License <http://www.opensource.org/licenses/mit-license.php>
|
||||
********************************** 80 Columns *********************************
|
||||
*/
|
||||
|
||||
namespace vendor\DB;
|
||||
|
||||
class DB
|
||||
{
|
||||
static $q,$c,$p,$i = '`';
|
||||
|
||||
static $type = "";
|
||||
|
||||
/**
|
||||
* Fetch a column offset from the result set (COUNT() queries)
|
||||
*
|
||||
@ -79,6 +84,21 @@ class DB
|
||||
return $statement->fetchAll(\PDO::FETCH_COLUMN, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all query result rows as object
|
||||
*
|
||||
* @param string $query query string
|
||||
* @param array $params query parameters
|
||||
* @return array
|
||||
*/
|
||||
static function fetchObject($query, $className, $params = NULL)
|
||||
{
|
||||
/** @var \PDOStatement $statement */
|
||||
if( ! $statement = DB::query($query, $params)) return null;
|
||||
|
||||
return $statement->fetchAll(\PDO::FETCH_CLASS, $className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and send a query returning the PDOStatement
|
||||
*
|
||||
@ -126,4 +146,20 @@ class DB
|
||||
))
|
||||
return $statement->rowCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array containing all field names
|
||||
* @param $table
|
||||
* @return array
|
||||
*/
|
||||
static function getColumns($table) {
|
||||
switch (self::$type) {
|
||||
case "SQLITE":
|
||||
return self::fetch("PRAGMA table_info($table)", null, 1);
|
||||
break;
|
||||
case "MySQL":
|
||||
return self::fetch("DESC $table", null, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
2
system/vendor/StackTracePrint.php
vendored
2
system/vendor/StackTracePrint.php
vendored
@ -10,7 +10,7 @@ function vdump() {
|
||||
|
||||
$ret = "<pre style='background: #eee; border: 1px solid #aaa; clear: both; overflow: auto; padding: 10px; text-align: left; margin-bottom: 5px'>";
|
||||
|
||||
$ret .- "<b>".htmlspecialchars(trim($code[$backtrace[0]['line']-1]))."</b>\n";
|
||||
$ret .= "<b>".htmlspecialchars(trim($code[$backtrace[0]['line']-1]))."</b>\n";
|
||||
|
||||
$ret .= "\n";
|
||||
|
||||
|
27
system/vendor/aes.php
vendored
Normal file
27
system/vendor/aes.php
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
// Source: http://stackoverflow.com/a/8232171/195722
|
||||
function aes_encrypt($key, $text) {
|
||||
$realKey = "";
|
||||
if (count($key) < 32) { //if it's less than 32 bits - pad it
|
||||
$realKey = str_pad($key, 32 - count($key) + 1);
|
||||
} else if (count($key) > 32) {
|
||||
throw new \Exception("Key is too long");
|
||||
} else {
|
||||
$realKey = $key;
|
||||
}
|
||||
|
||||
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $realKey, $text, MCRYPT_MODE_ECB));
|
||||
}
|
||||
|
||||
function aes_decrypt($key, $cipherText) {
|
||||
$realKey = "";
|
||||
if (count($key) < 32) { //if it's less than 32 bits - pad it
|
||||
$realKey = str_pad($key, 32 - count($key) + 1);
|
||||
} else if (count($key) > 32) {
|
||||
throw new \Exception("Key is too long");
|
||||
} else {
|
||||
$realKey = $key;
|
||||
}
|
||||
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $realKey, base64_decode($cipherText), MCRYPT_MODE_ECB);
|
||||
}
|
2
system/vendor/h2o.php
vendored
2
system/vendor/h2o.php
vendored
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
define('H2O_VERSION', '0.3');
|
||||
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
|
||||
defined('H2O_ROOT') or define('H2O_ROOT', dirname(__FILE__) . DS);
|
||||
@ -10,6 +11,7 @@ require H2O_ROOT.'h2o/tags.php';
|
||||
require H2O_ROOT.'h2o/errors.php';
|
||||
require H2O_ROOT.'h2o/filters.php';
|
||||
require H2O_ROOT.'h2o/context.php';
|
||||
require H2O_ROOT.'h2o/parser.php';
|
||||
|
||||
/**
|
||||
* Example:
|
||||
|
17
system/vendor/is_cli.php
vendored
Normal file
17
system/vendor/is_cli.php
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
|
||||
function is_cli()
|
||||
{
|
||||
if( defined('STDIN') )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
19
system/vendor/phpoauthlib2/LICENSE
vendored
Normal file
19
system/vendor/phpoauthlib2/LICENSE
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2015 Nathan Adams <adamsna@datanethost.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
149
system/vendor/phpoauthlib2/ccurl.php
vendored
Normal file
149
system/vendor/phpoauthlib2/ccurl.php
vendored
Normal file
@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace phpoauthlib2;
|
||||
|
||||
// Copied from PHP manual comment section:
|
||||
// http://php.net/manual/en/book.curl.php#90821
|
||||
// Modified to fit Inefero's/srchub needs
|
||||
// Now used for oauth lib
|
||||
class ccurl {
|
||||
protected $_useragent = 'phpoauthlib2 (http://www.srchub.org); https://srchub.org/p/phpoauthlib2 PHPOAuthLib';
|
||||
protected $_url;
|
||||
protected $_followlocation;
|
||||
protected $_timeout;
|
||||
protected $_maxRedirects;
|
||||
protected $_cookieFileLocation = './cookie.txt';
|
||||
protected $_post;
|
||||
protected $_postFields;
|
||||
protected $_referer ="";
|
||||
|
||||
// Get around some broken webservers *cough*IIS*cough*?
|
||||
// http://stackoverflow.com/questions/14459704/does-empty-expect-header-mean-anything
|
||||
protected $_header = array('Expect:');
|
||||
|
||||
protected $_session;
|
||||
protected $_webpage;
|
||||
protected $_includeHeader;
|
||||
protected $_noBody;
|
||||
protected $_status;
|
||||
protected $_binaryTransfer;
|
||||
public $authentication = 0;
|
||||
public $auth_name = '';
|
||||
public $auth_pass = '';
|
||||
|
||||
public function useAuth($use){
|
||||
$this->authentication = 0;
|
||||
if($use == true) $this->authentication = 1;
|
||||
}
|
||||
|
||||
public function setName($name){
|
||||
$this->auth_name = $name;
|
||||
}
|
||||
public function setPass($pass){
|
||||
$this->auth_pass = $pass;
|
||||
}
|
||||
|
||||
public function addHeader($head)
|
||||
{
|
||||
$this->_header[] = $head;
|
||||
}
|
||||
|
||||
public function __construct($url,$followlocation = true,$timeOut = 30,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false)
|
||||
{
|
||||
$this->_url = $url;
|
||||
$this->_followlocation = $followlocation;
|
||||
$this->_timeout = $timeOut;
|
||||
$this->_maxRedirects = $maxRedirecs;
|
||||
$this->_noBody = $noBody;
|
||||
$this->_includeHeader = $includeHeader;
|
||||
$this->_binaryTransfer = $binaryTransfer;
|
||||
|
||||
$this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt';
|
||||
|
||||
}
|
||||
|
||||
public function setReferer($referer){
|
||||
$this->_referer = $referer;
|
||||
}
|
||||
|
||||
public function setCookiFileLocation($path)
|
||||
{
|
||||
$this->_cookieFileLocation = $path;
|
||||
}
|
||||
|
||||
public function setPost ($postFields)
|
||||
{
|
||||
$this->_post = true;
|
||||
$this->_postFields = $postFields;
|
||||
}
|
||||
|
||||
public function setUserAgent($userAgent)
|
||||
{
|
||||
$this->_useragent = $userAgent;
|
||||
}
|
||||
|
||||
public function createCurl($url = 'nul')
|
||||
{
|
||||
if($url != 'nul'){
|
||||
$this->_url = $url;
|
||||
}
|
||||
|
||||
$s = curl_init();
|
||||
|
||||
curl_setopt($s,CURLOPT_URL,$this->_url);
|
||||
|
||||
// I understand the implications here - but this isn't a client application
|
||||
// if my ISP is performing MITM sniffing I have bigger fish to fry
|
||||
// also the security of a CA signed certificate is questionable at best
|
||||
// https://www.schneier.com/blog/archives/2012/02/verisign_hacked.html
|
||||
// Email me if you want to discus this adamsna@datanethost.net
|
||||
// NA - 12/10/2014
|
||||
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false);
|
||||
|
||||
curl_setopt($s,CURLOPT_HTTPHEADER,$this->_header);
|
||||
curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout);
|
||||
curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects);
|
||||
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
|
||||
curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation);
|
||||
curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
|
||||
curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);
|
||||
|
||||
if($this->authentication == 1){
|
||||
curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
|
||||
}
|
||||
if($this->_post)
|
||||
{
|
||||
//curl_setopt($s,CURLOPT_POST,true);
|
||||
curl_setopt($s, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);
|
||||
|
||||
}
|
||||
|
||||
if($this->_includeHeader)
|
||||
{
|
||||
curl_setopt($s,CURLOPT_HEADER,true);
|
||||
}
|
||||
|
||||
if($this->_noBody)
|
||||
{
|
||||
curl_setopt($s,CURLOPT_NOBODY,true);
|
||||
}
|
||||
|
||||
curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent);
|
||||
curl_setopt($s,CURLOPT_REFERER,$this->_referer);
|
||||
|
||||
$this->_webpage = curl_exec($s);
|
||||
$this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE);
|
||||
curl_close($s);
|
||||
|
||||
}
|
||||
|
||||
public function getHttpStatus()
|
||||
{
|
||||
return $this->_status;
|
||||
}
|
||||
|
||||
public function __tostring(){
|
||||
return $this->_webpage;
|
||||
}
|
||||
}
|
5
system/vendor/phpoauthlib2/cookie.txt
vendored
Normal file
5
system/vendor/phpoauthlib2/cookie.txt
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
# Netscape HTTP Cookie File
|
||||
# http://curl.haxx.se/docs/http-cookies.html
|
||||
# This file was generated by libcurl! Edit at your own risk.
|
||||
|
||||
#HttpOnly_.google.com TRUE / FALSE 1463331114 NID 73=wOGSMwy8vYCu7qJjpMYLVMQ_gYCNZXru2x8g0p5InvQIpj8X4P2uiXE8QdX50ZcpHvxCXo35XdDS8P5trQSsI0s3UmQ-tmG_01TTIVil6YeJIzFKqoGmKAdWo_0o8MOQ
|
24
system/vendor/phpoauthlib2/example.php
vendored
Normal file
24
system/vendor/phpoauthlib2/example.php
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use \phpoauthlib2\providers\GoogleAuthProvider;
|
||||
use \phpoauthlib2\OAuth;
|
||||
|
||||
require 'OAuth.php';
|
||||
require 'providers/GoogleAuthProvider.php';
|
||||
|
||||
$authProvider = new GoogleAuthProvider($_GET, [
|
||||
"client_id" => "apps.googleusercontent.com",
|
||||
"client_secret" => "<KEY>",
|
||||
"redirect_uri" => "http://example.com/phpoauthlib2/example.php"
|
||||
]);
|
||||
|
||||
$oauth = new OAuth($authProvider, $_GET);
|
||||
|
||||
$check = $oauth->check();
|
||||
|
||||
if ($check === true) {
|
||||
echo "Hello - " . $authProvider->getFirstName();
|
||||
echo "<br>Your email is - " . $authProvider->getEmail();
|
||||
} else {
|
||||
header("Location: " . $check);
|
||||
}
|
26
system/vendor/phpoauthlib2/oauth.php
vendored
Normal file
26
system/vendor/phpoauthlib2/oauth.php
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace system\vendor\phpoauthlib2;
|
||||
|
||||
class OAuth {
|
||||
protected $oauthProvider = null;
|
||||
protected $request = null;
|
||||
|
||||
public function __construct($provider, $request) {
|
||||
$this->oauthProvider = $provider;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function check() {
|
||||
if (isset($this->request["code"]) && !empty($this->request["code"])) {
|
||||
$this->oauthProvider->getProfile();
|
||||
return true;
|
||||
} else {
|
||||
return $this->oauthProvider->getLoginUrl();
|
||||
}
|
||||
}
|
||||
|
||||
public function getProfile() {
|
||||
return $this->oauthProvider->getProfile();
|
||||
}
|
||||
}
|
113
system/vendor/phpoauthlib2/oauthdataprovider.php
vendored
Normal file
113
system/vendor/phpoauthlib2/oauthdataprovider.php
vendored
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace system\vendor\phpoauthlib2;
|
||||
|
||||
require "ccurl.php";
|
||||
|
||||
use phpoauthlib2\ccurl;
|
||||
|
||||
class OAUTH_SCOPES {
|
||||
const EMAIL = "EMAIL";
|
||||
}
|
||||
|
||||
class OAuthDataProvider {
|
||||
protected $version = "2.0";
|
||||
protected $name = "undefined";
|
||||
protected $responseType = "code";
|
||||
protected $header = "Authorization: Bearer";
|
||||
protected $profile = "";
|
||||
protected $dialog = "";
|
||||
protected $nonce = null;
|
||||
protected $accessToken = null;
|
||||
|
||||
protected $state = "";
|
||||
protected $redirectURL = "";
|
||||
protected $scope = "";
|
||||
protected $clientId = "";
|
||||
protected $client_secret = "";
|
||||
|
||||
protected $request = null;
|
||||
protected $profileData = [];
|
||||
|
||||
public function __construct($profile, $dialog, $accessToken, $request, $header="Authorization: Bearer") {
|
||||
$this->profile = $profile;
|
||||
$this->dialog = $dialog;
|
||||
$this->accessToken = $accessToken;
|
||||
$this->header = $header;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function getLoginUrl() {
|
||||
$urlBuilder = [];
|
||||
$urlBuilder[] = "client_id=" . $this->clientId;
|
||||
$urlBuilder[] = "response_type=" . $this->responseType;
|
||||
$urlBuilder[] = "scope=" . $this->scope;
|
||||
$urlBuilder[] = "state=" . $this->state;
|
||||
$urlBuilder[] = "redirect_uri=" . urlencode($this->redirectURL);
|
||||
return $this->dialog . "?" . implode("&", $urlBuilder);
|
||||
}
|
||||
|
||||
protected function getToken() {
|
||||
$tokenBuilder = [];
|
||||
$tokenBuilder["client_id"] = $this->clientId;
|
||||
$tokenBuilder["client_secret"] = $this->client_secret;
|
||||
$tokenBuilder["grant_type"] = "authorization_code";
|
||||
$tokenBuilder["redirect_uri"] = htmlspecialchars($this->redirectURL);
|
||||
$tokenBuilder["code"] = $this->request["code"];
|
||||
$curl = new ccurl($this->accessToken);
|
||||
$curl->setPost($tokenBuilder);
|
||||
$curl->createCurl();
|
||||
return (string)$curl;
|
||||
}
|
||||
|
||||
protected function parseToken() {
|
||||
$token = $this->getToken();
|
||||
$convertedToken = json_decode($token, true);
|
||||
if (!$convertedToken) {
|
||||
$realToken = $token;
|
||||
} else {
|
||||
$realToken = $convertedToken["access_token"];
|
||||
}
|
||||
|
||||
return $realToken;
|
||||
}
|
||||
|
||||
public function getProfile() {
|
||||
$token = $this->parseToken();
|
||||
|
||||
$profileUrl = $this->profile . "=" . $token;
|
||||
$curl = new ccurl($profileUrl);
|
||||
$curl->addHeader($this->header . " " . $token);
|
||||
$curl->createCurl();
|
||||
$this->profileData = json_decode((string)$curl, true);
|
||||
return (string)$curl;
|
||||
}
|
||||
|
||||
public function getEmail() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getFirstName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getLastName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getGender() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRawProfile() {
|
||||
return $this->profileData;
|
||||
}
|
||||
|
||||
public function getSource() {
|
||||
return null;
|
||||
}
|
||||
}
|
70
system/vendor/phpoauthlib2/providers/facebookauthprovider.php
vendored
Normal file
70
system/vendor/phpoauthlib2/providers/facebookauthprovider.php
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace system\vendor\phpoauthlib2\providers;
|
||||
|
||||
require "OAuthDataProvider.php";
|
||||
|
||||
use phpoauthlib2\ccurl;
|
||||
use phpoauthlib2\OAUTH_SCOPES;
|
||||
use phpoauthlib2\OAuthDataProvider;
|
||||
|
||||
class FacebookAuthProvider extends OAuthDataProvider {
|
||||
|
||||
public function __construct($request, $conf, $scopes=[OAUTH_SCOPES::EMAIL]) {
|
||||
parent::__construct(
|
||||
"https://graph.facebook.com/me",
|
||||
"https://www.facebook.com/dialog/oauth",
|
||||
"https://graph.facebook.com/oauth/access_token",
|
||||
$request
|
||||
);
|
||||
|
||||
$this->client_secret = $conf["client_secret"];
|
||||
$this->redirectURL = $conf["redirect_uri"];
|
||||
$this->clientId = $conf["client_id"];
|
||||
$tempScopes = [];
|
||||
foreach($scopes as $scope) {
|
||||
switch ($scope) {
|
||||
case OAUTH_SCOPES::EMAIL:
|
||||
$tempScopes[] = "email";
|
||||
}
|
||||
}
|
||||
$tempScopes[] = "public_profile";
|
||||
$this->scope = implode(" ", $tempScopes);
|
||||
}
|
||||
|
||||
public function getEmail() {
|
||||
return $this->profileData["email"];
|
||||
}
|
||||
|
||||
public function getFirstName() {
|
||||
return $this->profileData["first_name"];
|
||||
}
|
||||
|
||||
public function getLastName() {
|
||||
return $this->profileData["last_name"];
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
return $this->profileData["id"];
|
||||
}
|
||||
|
||||
public function getSource() {
|
||||
return "FACEBOOK";
|
||||
}
|
||||
|
||||
public function parseToken() {
|
||||
$token = $this->getToken();
|
||||
return explode("=", $token)[1];
|
||||
}
|
||||
|
||||
public function getProfile() {
|
||||
$token = $this->parseToken();
|
||||
$profileUrl = $this->profile . "?fields=first_name,last_name,name,email,age_range&access_token=" . $token;
|
||||
$curl = new ccurl($profileUrl);
|
||||
$curl->createCurl();
|
||||
$ret = (string)$curl;
|
||||
$this->profileData = json_decode($ret, true);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
}
|
56
system/vendor/phpoauthlib2/providers/googleauthprovider.php
vendored
Normal file
56
system/vendor/phpoauthlib2/providers/googleauthprovider.php
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace system\vendor\phpoauthlib2\providers;
|
||||
|
||||
use phpoauthlib2\ccurl;
|
||||
use system\vendor\phpoauthlib2\OAUTH_SCOPES;
|
||||
use system\vendor\phpoauthlib2\OAuth;
|
||||
use system\vendor\phpoauthlib2\OAuthDataProvider;
|
||||
|
||||
class GoogleAuthProvider extends OAuthDataProvider {
|
||||
|
||||
public function __construct($request, $conf, $scopes=[OAUTH_SCOPES::EMAIL]) {
|
||||
parent::__construct(
|
||||
"https://www.googleapis.com/oauth2/v1/userinfo?access_token",
|
||||
"https://accounts.google.com/o/oauth2/auth",
|
||||
"https://accounts.google.com/o/oauth2/token", $request);
|
||||
|
||||
$this->client_secret = $conf["client_secret"];
|
||||
$this->redirectURL = $conf["redirect_uri"];
|
||||
$this->clientId = $conf["client_id"];
|
||||
|
||||
$tmpScopes = [];
|
||||
foreach($scopes as $scope) {
|
||||
switch ($scope) {
|
||||
case OAUTH_SCOPES::EMAIL:
|
||||
$tmpScopes[] = "https://www.googleapis.com/auth/userinfo.email";
|
||||
}
|
||||
}
|
||||
|
||||
$this->scope = implode(" ", $tmpScopes);
|
||||
}
|
||||
|
||||
public function getEmail() {
|
||||
return $this->profileData["email"];
|
||||
}
|
||||
|
||||
public function getFirstName() {
|
||||
return $this->profileData["given_name"];
|
||||
}
|
||||
|
||||
public function getLastName() {
|
||||
return $this->profileData["family_name"];
|
||||
}
|
||||
|
||||
public function getGender() {
|
||||
return $this->profileData["gender"];
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
return $this->profileData["id"];
|
||||
}
|
||||
|
||||
public function getSource() {
|
||||
return "GOOGLE";
|
||||
}
|
||||
}
|
86
system/vendor/phpoauthlib2/readme.md
vendored
Normal file
86
system/vendor/phpoauthlib2/readme.md
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
# phpoauthlib2
|
||||
|
||||
phpoauthlib2 is another OAuth 2.0 library for PHP. The goal of the project is to make it as easy as possible to integrate OAuth into your web application.
|
||||
|
||||
You can think of phpoauthlib2 as a combination of ideas from the following projects:
|
||||
|
||||
- PHPoAuthLib
|
||||
- li3_socialauth
|
||||
- oauth-4-laravel
|
||||
- PHPoAuthUserData
|
||||
|
||||
All wrapped in one simple library.
|
||||
|
||||
# Why phpoauthlib2?
|
||||
|
||||
Or more specifically why "2"?
|
||||
|
||||
For me this is version 2.0. Originally I developed this in private for PHP and it worked but after a refactoring to Django/Python (Python version coming soon to a pip repository near you) then I ported it back to PHP.
|
||||
|
||||
# How to use
|
||||
|
||||
It's very easy to use this library. Examine the following line -
|
||||
|
||||
$authProvider = new GoogleAuthProvider($_GET, [
|
||||
"client_id" => "apps.googleusercontent.com",
|
||||
"client_secret" => "<KEY>",
|
||||
"redirect_uri" => "http://example.com/phpoauthlib2/example.php"
|
||||
]);
|
||||
|
||||
client_id and client_secret are provided by the OAauth provider (in this case Google) and the redirect_uri is where you want to the user to end up on successful login. It should go without saying that client_id and client_secret should be kept private - you should avoid committing them a public place like github (yes - people have services running and monitoring for people who commit credentials. Don't believe me? Commit your Amazon AWS keys and see how fast people will spin up VMs). The library will handle the verification and present you with some simple base methods to extract data you might be interested in or the ability to work with the entire OAuth data.
|
||||
|
||||
$oauth = new OAuth($authProvider, $_GET);
|
||||
|
||||
OAuth is really a wrapper to do the verification check. In both this line and the previous one we are passing $_GET but phpoauthlib2 can accept any request array from your framework (provided your framework can emit the GET request as an array - which I know at least Symfony can do this).
|
||||
|
||||
$check = $oauth->check();
|
||||
|
||||
The check method will return true or a string. Not ideal but I couldn't think of any simpler way to do it (obviously not a problem in a lose typed language - but I don't personally like mixing return types). true indicates that the user successfully logged in and you have access to the user's information. A string indicates that you need to redirect them to the OAuth provider to login (the string itself is the redirect URL).
|
||||
|
||||
if ($check === true) {
|
||||
echo "Hello - " . $authProvider->getFirstName();
|
||||
echo "<br>Your email is - " . $authProvider->getEmail();
|
||||
} else {
|
||||
header("Location: " . $check);
|
||||
}
|
||||
|
||||
This library is designed to be very minimal - so you need to decide how to hook into the login subsystem of your web application. In the example file - it's checking to make sure that the login was successful and then can call $authProvider->getXXX (such as getFirstName and getEmail in this example) and the provider class will return those fields from the raw profile data so you don't have to worry about it.
|
||||
|
||||
The work flow to integrate to your system is usually:
|
||||
|
||||
if ($check === true) {
|
||||
$mySystem->login($authProvider->getEmail()); // which sets a cookie or session that they logged in with this specific user
|
||||
header("Location: http://example.com/yoursystem/user.php"); // The line above logs them in to your system - then immediately bounce back to your system and potentially send them straight to their user dashboard
|
||||
|
||||
The reasoning behind the getXXX methods is to provide some commonality between providers. That way you can present a OAuth login prompt for different services to the user and you can just call $provider->getEmail() to get their email without having to worry about the actual field that the OAuth provider decided to put it in.
|
||||
|
||||
If after you have verified the login was successful you may call
|
||||
|
||||
$provider->getRawProfile();
|
||||
|
||||
To return the raw return from the OAuth provider (which will be an associative array).
|
||||
|
||||
# Google
|
||||
|
||||
To get OAuth credentials for Google just go to this URL: https://console.developers.google.com/
|
||||
|
||||
And create a project (which is free) and go to APIs & auth -> Credentials.
|
||||
If you are creating a new project - it may complain that you need to setup the OAuth consent screen. Do this and return to the credentials section and you should be able to setup the project.
|
||||
|
||||
Add credentials -> OAuth 2.0 client ID
|
||||
Then select Web application
|
||||
|
||||
It is very important that you input a correct authorized redirect URI. This will be where the user will be sent back on successful login.
|
||||
|
||||
# License
|
||||
|
||||
I am licensing this under the MIT license. Which essentially grants you the right TDWTFYWWI (to do whatever the f you want with it) - assuming that you acknowledge that I don't provide a warranty.
|
||||
|
||||
# What this library is/is not
|
||||
|
||||
- This library is a simple interface to use PHP OAuth 2.0 in your web application.
|
||||
- This library is designed to be as flexible as possible to use in any framework.
|
||||
|
||||
- This library is not designed to hold your hand to secure your client_id, client_secret, or other data.
|
||||
- This library is not designed to be specific to a certain framework.
|
||||
- This library is not designed to be abstract. The only class you should ever have to extend is OAuthDataProvider - and that is to create a "provider" for different OAuth providers (which merely contains the URLs to send for login, where to query for user data and normalizing data).
|
Loading…
Reference in New Issue
Block a user