401 lines
11 KiB
PHP
401 lines
11 KiB
PHP
<?php
|
|
|
|
// include BaseModel if available
|
|
include_once(realpath(dirname(__FILE__)) . "/mfBaseModel.php");
|
|
|
|
class mfBaseController
|
|
{
|
|
protected $log;
|
|
protected $needlogin = false;
|
|
/** @var mfRequest */
|
|
protected $request;
|
|
private $mfAction;
|
|
/** @var FronkDB */
|
|
private $mfDBI;
|
|
/** @var Layout */
|
|
private $mfLayout;
|
|
private $mfMenu;
|
|
private $mfUser;
|
|
/** @var mixed|null Called module (Controller) */
|
|
protected $mod;
|
|
/** @var mixed|null Called action method in Controller */
|
|
protected $action;
|
|
|
|
public function __construct($params = NULL)
|
|
{
|
|
// load logging facility
|
|
$this->log = mfLoghandler::singleton();
|
|
|
|
if ($params === null || $params === false) {
|
|
$params = [];
|
|
}
|
|
|
|
if (is_array($params)) {
|
|
$this->mod = array_key_exists("mod", $params) ? $params['mod'] : null;
|
|
$this->action = array_key_exists("mod", $params) ? $params['action'] : null;
|
|
}
|
|
|
|
|
|
// run custom constructor init()
|
|
if (method_exists($this, "init")) {
|
|
$this->init($params);
|
|
}
|
|
|
|
if (!defined('MFUSELOGIN')) define('MFUSELOGIN', false);
|
|
if (!defined('MFUSEMENU')) define('MFUSEMENU', false);
|
|
|
|
$nologin = false;
|
|
if(defined("MFBASE_BYPASS_LOGIN")) {
|
|
$nologin = MFBASE_BYPASS_LOGIN;
|
|
}
|
|
|
|
if (MFUSELOGIN && !$nologin) {
|
|
// if protected area and not logged in, redirect to mfLogin
|
|
if ($this->needlogin == true) {
|
|
if (!mfLoginController::isLoggedIn()) {
|
|
// Save original request
|
|
//$params['mfLoginGet']=$_GET; // TODO: some better way to preserve request while logging in
|
|
$params["mfLoginUrl"] = $_SERVER['REQUEST_URI'];
|
|
if (array_key_exists("action", $params) && $params['action']) {
|
|
unset($params['action']);
|
|
}
|
|
|
|
if ($this->loginTemplate) {
|
|
$params['mfLoginTemplate'] = $this->loginTemplate;
|
|
}
|
|
// Redirect to Loginform
|
|
$temp = new mfLoginController($params);
|
|
return 0;
|
|
} else {
|
|
$this->mfUser = mfUser::singleton();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (MFUSEMENU) {
|
|
// Load mfMenu for menu display
|
|
$this->mfMenu = new mfMenuController();
|
|
if ($this->mfMenu) {
|
|
$this->layout()->set("MenuItems", $this->mfMenu);
|
|
}
|
|
}
|
|
|
|
// set default template variables
|
|
$this->layout()->set('mod', $this->mod);
|
|
$this->layout()->set('action', $this->action);
|
|
|
|
// include and run global init file
|
|
if (defined("MFBASECONTROLLER_INIT_INCLUDE_FILE")) {
|
|
include MFBASECONTROLLER_INIT_INCLUDE_FILE;
|
|
}
|
|
|
|
$this->request = new mfRequest($params);
|
|
|
|
// now run action
|
|
if (array_key_exists("action", $params) && $params['action']) {
|
|
$this->__call($params['action'], $params);
|
|
} else {
|
|
if (method_exists($this, "index")) {
|
|
$this->__call("index", $params);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function user()
|
|
{
|
|
if (!MFUSELOGIN) {
|
|
trigger_error("mvcfronk: Tried to access mfBaseController::user(), though MFUSELOGIN is set to false.", E_USER_WARNING);
|
|
return false;
|
|
}
|
|
if (!$this->mfUser) {
|
|
$this->mfUser = mfUser::singleton();
|
|
}
|
|
return $this->mfUser;
|
|
}
|
|
|
|
protected function layout()
|
|
{
|
|
if (!is_object($this->mfLayout)) {
|
|
$this->mfLayout = Layout::singleton();
|
|
}
|
|
return $this->mfLayout;
|
|
}
|
|
|
|
|
|
protected function db()
|
|
{
|
|
$args = func_get_args();
|
|
|
|
// if no arguments, just return a DB instance
|
|
if (!$args) {
|
|
// don't allow managed FronkDB instance, but new custom instance is allowed
|
|
if (!FRONKDB) {
|
|
return false;
|
|
}
|
|
if (!is_object($this->mfDBI)) {
|
|
$this->mfDBI = FronkDB::singleton();
|
|
|
|
}
|
|
return $this->mfDBI;
|
|
} else {
|
|
// else return a new instance
|
|
$dbhost = $args[0];
|
|
$dbuser = $args[1];
|
|
$dbpass = $args[2];
|
|
$dbname = $args[3];
|
|
return $this->getNewDBInstance($dbhost, $dbuser, $dbpass, $dbname);
|
|
}
|
|
|
|
}
|
|
|
|
public function reconnectDB() {
|
|
$this->db()->reconnect();
|
|
}
|
|
|
|
public function __call($name, $params)
|
|
{
|
|
$methodname = false;
|
|
if (method_exists($this, $name)) {
|
|
// check if callable
|
|
$methodname = $name;
|
|
} else { // if function doesn't exist, maybe it's an Action
|
|
$funcname = lcfirst($name);
|
|
if (!preg_match('/Action$/', $name)) {
|
|
$funcname .= "Action";
|
|
}
|
|
|
|
if (method_exists($this, $funcname)) {
|
|
$methodname = $funcname;
|
|
} else {
|
|
throw new Exception(get_class($this) . ": $name not found", 404);
|
|
}
|
|
}
|
|
|
|
try {
|
|
$ref = new ReflectionMethod($this, $methodname);
|
|
if (!$ref->isPublic() && !$ref->isProtected()) {
|
|
throw new Exception("Method $methodname cannot be called due to limited visibility");
|
|
}
|
|
} catch (Exception $e) {
|
|
throw $e;
|
|
}
|
|
|
|
return call_user_func(array($this, $methodname), $params);
|
|
}
|
|
|
|
public function __get($name)
|
|
{
|
|
if ($name == "db") {
|
|
return $this->db();
|
|
}
|
|
if ($name == "layout") {
|
|
return $this->layout();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
protected function logout()
|
|
{
|
|
mfLoginController::staticLogout();
|
|
$this->redirect(DEFAULT_ROUTE);
|
|
}
|
|
|
|
/*
|
|
* private internal functions
|
|
*/
|
|
|
|
private function getNewDBInstance($dbhost = false, $dbuser = false, $dbpass = false, $dbname = false)
|
|
{
|
|
if (!$dbhost) $dbhost = FRONKDB_DBHOST;
|
|
if (!$dbuser) $dbhost = FRONKDB_DBUSER;
|
|
if (!$dbpass) $dbhost = FRONKDB_DBPASS;
|
|
if (!$dbname) $dbname = FRONKDB_DBNAME;
|
|
|
|
return FronkDB::singleton($dbhost, $dbuser, $dbpass, $dbname);
|
|
}
|
|
|
|
/**
|
|
* @param $mod
|
|
* @param $action
|
|
* @param $params
|
|
* @param $anker
|
|
* @return never-returns
|
|
*/
|
|
public static function redirect($mod = false, $action = false, $params = false, $anker = false)
|
|
{
|
|
//var_dump($mod);
|
|
//var_dump($action);
|
|
|
|
$log = mfLoghandler::singleton();
|
|
|
|
if (MFUSEFANCYURLS && defined('MFFANCYBASEURL')) {
|
|
// use fancy urls
|
|
$url = MFFANCYBASEURL;
|
|
if ($mod) {
|
|
$url .= "/$mod";
|
|
if ($action) {
|
|
$url .= "/$action";
|
|
}
|
|
}
|
|
} else {
|
|
// no fancy urls
|
|
if (!$mod) {
|
|
$url = "?";
|
|
} elseif ($mod) {
|
|
$url = "?action=$mod";
|
|
if ($action) {
|
|
$url .= "_$action";
|
|
}
|
|
}
|
|
}
|
|
/*
|
|
if(is_array($params) && count($params)) {
|
|
foreach($params as $k => $v) {
|
|
$url.="&$k=$v";
|
|
}
|
|
}*/
|
|
|
|
if ($params) {
|
|
|
|
if (is_array($params) && count($params)) {
|
|
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
|
foreach ($params as $k => $v) {
|
|
$v = urlencode($v);
|
|
|
|
if ($k) {
|
|
$k = urlencode($k);
|
|
$url .= "$k=$v&";
|
|
} else {
|
|
$url .= "$v&";
|
|
}
|
|
}
|
|
|
|
$url = preg_replace('/&$/', '', $url);
|
|
} else {
|
|
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
|
$url .= $params;
|
|
}
|
|
}
|
|
if ($anker) {
|
|
$url .= "#$anker";
|
|
}
|
|
|
|
$url = preg_replace('#^/+#', "/", $url);
|
|
|
|
$log->debug("Redirecting to $url");
|
|
header("Location: $url");
|
|
exit;
|
|
}
|
|
|
|
public static function getUrl($mod, $action = null, $param = null)
|
|
{
|
|
if (!$mod) {
|
|
if (MFUSEFANCYURLS) return MFFANCYBASEURL;
|
|
return "";
|
|
}
|
|
|
|
|
|
if (MFUSEFANCYURLS) {
|
|
// use fancy urls
|
|
$url = MFFANCYBASEURL;
|
|
if ($mod) {
|
|
$url .= "/$mod";
|
|
if ($action) {
|
|
$url .= "/$action";
|
|
}
|
|
}
|
|
$url = preg_replace('#//#', '/', $url);
|
|
} else {
|
|
// no fancy urls
|
|
$url = "?action=$mod";
|
|
if ($action) {
|
|
$url .= "_$action";
|
|
}
|
|
}
|
|
if (is_array($param) && count($param)) {
|
|
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
|
$param_qs = http_build_query($param);
|
|
$url .= "$param_qs";
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
|
|
public static function returnJson($response)
|
|
{
|
|
if(mfResponse::isResponse($response)) {
|
|
$code = $response['code'];
|
|
$status = $response['status'];
|
|
$data = $response['data'];
|
|
|
|
$proto = "HTTP/1.0";
|
|
if($_SERVER["SERVER_PROTOCOL"]) {
|
|
$proto = $_SERVER["SERVER_PROTOCOL"];
|
|
}
|
|
|
|
header("$proto $code $status");
|
|
header("Content-type: application/json");
|
|
//http_response_code($code);
|
|
echo json_encode(["status" => $status, "result" => $data]);
|
|
exit;
|
|
}
|
|
if (is_array($response)) {
|
|
header("Content-Type: application/json");
|
|
echo json_encode($response);
|
|
exit;
|
|
} else {
|
|
throw new Exception("Response data not an array");
|
|
}
|
|
}
|
|
|
|
// Helper functions
|
|
|
|
public static function dateToTimestamp($date)
|
|
{
|
|
// extract day, month, year
|
|
if (!preg_match('/^(\d{1,2})\.(\d{1,2})\.(\d{2,4})/', $date, $d)) {
|
|
return false;
|
|
}
|
|
// extract time if available
|
|
if (preg_match('/(\d\d):(\d\d):(\d\d)$/', $date, $t)) {
|
|
if (!$t[3]) {
|
|
$t[3] = 0;
|
|
}
|
|
} else {
|
|
$t = array(0, 0, 0, 0);
|
|
}
|
|
// make and return timestamp
|
|
$ts = mktime($t[1], $t[2], $t[3], $d[2], $d[1], $d[3]);
|
|
return $ts;
|
|
}
|
|
|
|
public static function dateToDB($date, $type = 'l')
|
|
{
|
|
// get timestamp
|
|
$ts = self::dateToTimestamp($date);
|
|
|
|
// only proceed if timestamp conversion was successful
|
|
if (!$ts) {
|
|
return false;
|
|
}
|
|
|
|
// return date and time if long type requested
|
|
if ($type = 'l') {
|
|
$dbdate = date('Y-m-d H:i:s', $ts);
|
|
} else {
|
|
$dbdate = date('Y-m-d', $ts);
|
|
}
|
|
|
|
return $dbdate;
|
|
}
|
|
|
|
public static function sendError(string $message): void {
|
|
http_response_code(500);
|
|
self::returnJson(['success' => false, 'message' => $message, 'error' => $message]);
|
|
exit;
|
|
}
|
|
|
|
}
|