Merge branch 'fronkdev' into 'master'
mfBaseController now throws exception when called method lacks visibility See merge request fronk/thetool!331
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// include BaseModel if available
|
// include BaseModel if available
|
||||||
include_once(realpath(dirname(__FILE__))."/mfBaseModel.php");
|
include_once(realpath(dirname(__FILE__)) . "/mfBaseModel.php");
|
||||||
|
|
||||||
class mfBaseController {
|
class mfBaseController
|
||||||
|
{
|
||||||
protected $log;
|
protected $log;
|
||||||
protected $needlogin = false;
|
protected $needlogin = false;
|
||||||
protected $request;
|
protected $request;
|
||||||
@@ -15,143 +16,165 @@ class mfBaseController {
|
|||||||
protected $mod;
|
protected $mod;
|
||||||
protected $action;
|
protected $action;
|
||||||
|
|
||||||
public function __construct($params = NULL) {
|
public function __construct($params = NULL)
|
||||||
|
{
|
||||||
// load logging facility
|
// load logging facility
|
||||||
$this->log = mfLoghandler::singleton();
|
$this->log = mfLoghandler::singleton();
|
||||||
|
|
||||||
if($params === null || $params === false) {
|
if ($params === null || $params === false) {
|
||||||
$params = [];
|
$params = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->mod = $params['mod'];
|
if (is_array($params)) {
|
||||||
$this->action = $params['action'];
|
$this->mod = array_key_exists("mod", $params) ? $params['mod'] : null;
|
||||||
|
$this->action = array_key_exists("mod", $params) ? $params['action'] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// run custom constructor init()
|
// run custom constructor init()
|
||||||
if(method_exists($this,"init")) {
|
if (method_exists($this, "init")) {
|
||||||
$this->init($params);
|
$this->init($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!defined('MFUSELOGIN')) define('MFUSELOGIN',false);
|
if (!defined('MFUSELOGIN')) define('MFUSELOGIN', false);
|
||||||
if(!defined('MFUSEMENU')) define('MFUSEMENU',false);
|
if (!defined('MFUSEMENU')) define('MFUSEMENU', false);
|
||||||
|
|
||||||
if(MFUSELOGIN) {
|
if (MFUSELOGIN) {
|
||||||
// if protected area and not logged in, redirect to mfLogin
|
// if protected area and not logged in, redirect to mfLogin
|
||||||
if($this->needlogin==true) {
|
if ($this->needlogin == true) {
|
||||||
if(!mfLoginController::isLoggedIn()) {
|
if (!mfLoginController::isLoggedIn()) {
|
||||||
// Save original request
|
// Save original request
|
||||||
//$params['mfLoginGet']=$_GET; // TODO: some better way to preserve request while logging in
|
//$params['mfLoginGet']=$_GET; // TODO: some better way to preserve request while logging in
|
||||||
$params["mfLoginUrl"]=$_SERVER['REQUEST_URI'];
|
$params["mfLoginUrl"] = $_SERVER['REQUEST_URI'];
|
||||||
if($params['action']) {
|
if (array_key_exists("action", $params) && $params['action']) {
|
||||||
unset($params['action']);
|
unset($params['action']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->loginTemplate) {
|
if ($this->loginTemplate) {
|
||||||
$params['mfLoginTemplate']=$this->loginTemplate;
|
$params['mfLoginTemplate'] = $this->loginTemplate;
|
||||||
}
|
}
|
||||||
// Redirect to Loginform
|
// Redirect to Loginform
|
||||||
$temp=new mfLoginController($params);
|
$temp = new mfLoginController($params);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
$this->mfUser=mfUser::singleton();
|
$this->mfUser = mfUser::singleton();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(MFUSEMENU) {
|
if (MFUSEMENU) {
|
||||||
// Load mfMenu for menu display
|
// Load mfMenu for menu display
|
||||||
$this->mfMenu=new mfMenuController();
|
$this->mfMenu = new mfMenuController();
|
||||||
if($this->mfMenu) {
|
if ($this->mfMenu) {
|
||||||
$this->layout()->set("MenuItems",$this->mfMenu);
|
$this->layout()->set("MenuItems", $this->mfMenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// set default template variables
|
// set default template variables
|
||||||
$this->layout()->set('mod',$this->mod);
|
$this->layout()->set('mod', $this->mod);
|
||||||
$this->layout()->set('action',$this->action);
|
$this->layout()->set('action', $this->action);
|
||||||
|
|
||||||
// include and run global init file
|
// include and run global init file
|
||||||
if(defined("MFBASECONTROLLER_INIT_INCLUDE_FILE")) {
|
if (defined("MFBASECONTROLLER_INIT_INCLUDE_FILE")) {
|
||||||
include MFBASECONTROLLER_INIT_INCLUDE_FILE;
|
include MFBASECONTROLLER_INIT_INCLUDE_FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->request = new mfRequest($params);
|
$this->request = new mfRequest($params);
|
||||||
|
|
||||||
// now run action
|
// now run action
|
||||||
if($params['action']) {
|
if (array_key_exists("action", $params) && $params['action']) {
|
||||||
$this->__call($params['action'],$params);
|
$this->__call($params['action'], $params);
|
||||||
} else {
|
} else {
|
||||||
if(method_exists($this,"index")) {
|
if (method_exists($this, "index")) {
|
||||||
$this->__call("index",$params);
|
$this->__call("index", $params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function user() {
|
protected function user()
|
||||||
if(!MFUSELOGIN) {
|
{
|
||||||
|
if (!MFUSELOGIN) {
|
||||||
trigger_error("mvcfronk: Tried to access mfBaseController::user(), though MFUSELOGIN is set to false.", E_USER_WARNING);
|
trigger_error("mvcfronk: Tried to access mfBaseController::user(), though MFUSELOGIN is set to false.", E_USER_WARNING);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(!$this->mfUser) {
|
if (!$this->mfUser) {
|
||||||
$this->mfUser=mfUser::singleton();
|
$this->mfUser = mfUser::singleton();
|
||||||
}
|
}
|
||||||
return $this->mfUser;
|
return $this->mfUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function layout() {
|
protected function layout()
|
||||||
if(!is_object($this->mfLayout)) {
|
{
|
||||||
|
if (!is_object($this->mfLayout)) {
|
||||||
$this->mfLayout = Layout::singleton();
|
$this->mfLayout = Layout::singleton();
|
||||||
}
|
}
|
||||||
return $this->mfLayout;
|
return $this->mfLayout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function db() {
|
protected function db()
|
||||||
$args=func_get_args();
|
{
|
||||||
|
$args = func_get_args();
|
||||||
|
|
||||||
// if no arguments, just return a DB instance
|
// if no arguments, just return a DB instance
|
||||||
if(!$args) {
|
if (!$args) {
|
||||||
// don't allow managed FronkDB instance, but new custom instance is allowed
|
// don't allow managed FronkDB instance, but new custom instance is allowed
|
||||||
if(!FRONKDB) {
|
if (!FRONKDB) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(!is_object($this->mfDBI)) {
|
if (!is_object($this->mfDBI)) {
|
||||||
$this->mfDBI=FronkDB::singleton();
|
$this->mfDBI = FronkDB::singleton();
|
||||||
|
|
||||||
}
|
}
|
||||||
return $this->mfDBI;
|
return $this->mfDBI;
|
||||||
} else {
|
} else {
|
||||||
// else return a new instance
|
// else return a new instance
|
||||||
$dbhost=$args[0];
|
$dbhost = $args[0];
|
||||||
$dbuser=$args[1];
|
$dbuser = $args[1];
|
||||||
$dbpass=$args[2];
|
$dbpass = $args[2];
|
||||||
$dbname=$args[3];
|
$dbname = $args[3];
|
||||||
return $this->getNewDBInstance($dbhost,$dbuser,$dbpass,$dbname);
|
return $this->getNewDBInstance($dbhost, $dbuser, $dbpass, $dbname);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __call($name,$params) {
|
public function __call($name, $params)
|
||||||
if(method_exists($this,$name)) {
|
{
|
||||||
return call_user_func(array($this, $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
|
} else { // if function doesn't exist, maybe it's an Action
|
||||||
$funcname=lcfirst($name);
|
$funcname = lcfirst($name);
|
||||||
if(!preg_match('/Action$/',$name)) {
|
if (!preg_match('/Action$/', $name)) {
|
||||||
$funcname.="Action";
|
$funcname .= "Action";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(method_exists($this,$funcname)) {
|
if (method_exists($this, $funcname)) {
|
||||||
return call_user_func(array($this, $funcname), $params);
|
$methodname = $funcname;
|
||||||
} else {
|
} else {
|
||||||
throw new Exception(get_class($this).": $name not found",404);
|
throw new Exception(get_class($this) . ": $name not found", 404);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get($name) {
|
try {
|
||||||
if($name == "db") {
|
$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();
|
return $this->db();
|
||||||
}
|
}
|
||||||
if($name == "layout") {
|
if ($name == "layout") {
|
||||||
return $this->layout();
|
return $this->layout();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +182,8 @@ class mfBaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function logout() {
|
protected function logout()
|
||||||
|
{
|
||||||
mfLoginController::staticLogout();
|
mfLoginController::staticLogout();
|
||||||
$this->redirect(DEFAULT_ROUTE);
|
$this->redirect(DEFAULT_ROUTE);
|
||||||
}
|
}
|
||||||
@@ -168,38 +192,40 @@ class mfBaseController {
|
|||||||
* private internal functions
|
* private internal functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private function getNewDBInstance($dbhost=false,$dbuser=false,$dbpass=false,$dbname=false) {
|
private function getNewDBInstance($dbhost = false, $dbuser = false, $dbpass = false, $dbname = false)
|
||||||
if(!$dbhost) $dbhost=FRONKDB_DBHOST;
|
{
|
||||||
if(!$dbuser) $dbhost=FRONKDB_DBUSER;
|
if (!$dbhost) $dbhost = FRONKDB_DBHOST;
|
||||||
if(!$dbpass) $dbhost=FRONKDB_DBPASS;
|
if (!$dbuser) $dbhost = FRONKDB_DBUSER;
|
||||||
if(!$dbname) $dbname=FRONKDB_DBNAME;
|
if (!$dbpass) $dbhost = FRONKDB_DBPASS;
|
||||||
|
if (!$dbname) $dbname = FRONKDB_DBNAME;
|
||||||
|
|
||||||
return FronkDB::singleton($dbhost,$dbuser,$dbpass,$dbname);
|
return FronkDB::singleton($dbhost, $dbuser, $dbpass, $dbname);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function redirect($mod=false,$action=false,$params=false,$anker=false) {
|
public static function redirect($mod = false, $action = false, $params = false, $anker = false)
|
||||||
|
{
|
||||||
//var_dump($mod);
|
//var_dump($mod);
|
||||||
//var_dump($action);
|
//var_dump($action);
|
||||||
|
|
||||||
$log = mfLoghandler::singleton();
|
$log = mfLoghandler::singleton();
|
||||||
|
|
||||||
if(MFUSEFANCYURLS && defined('MFFANCYBASEURL')) {
|
if (MFUSEFANCYURLS && defined('MFFANCYBASEURL')) {
|
||||||
// use fancy urls
|
// use fancy urls
|
||||||
$url=MFFANCYBASEURL;
|
$url = MFFANCYBASEURL;
|
||||||
if($mod) {
|
if ($mod) {
|
||||||
$url.="/$mod";
|
$url .= "/$mod";
|
||||||
if($action) {
|
if ($action) {
|
||||||
$url.="/$action";
|
$url .= "/$action";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// no fancy urls
|
// no fancy urls
|
||||||
if(!$mod) {
|
if (!$mod) {
|
||||||
$url="?";
|
$url = "?";
|
||||||
} elseif($mod) {
|
} elseif ($mod) {
|
||||||
$url="?action=$mod";
|
$url = "?action=$mod";
|
||||||
if($action) {
|
if ($action) {
|
||||||
$url.="_$action";
|
$url .= "_$action";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,14 +236,14 @@ class mfBaseController {
|
|||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
if($params) {
|
if ($params) {
|
||||||
|
|
||||||
if(is_array($params) && count($params)) {
|
if (is_array($params) && count($params)) {
|
||||||
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
||||||
foreach($params as $k => $v) {
|
foreach ($params as $k => $v) {
|
||||||
$v = urlencode($v);
|
$v = urlencode($v);
|
||||||
|
|
||||||
if($k) {
|
if ($k) {
|
||||||
$k = urlencode($k);
|
$k = urlencode($k);
|
||||||
$url .= "$k=$v&";
|
$url .= "$k=$v&";
|
||||||
} else {
|
} else {
|
||||||
@@ -231,8 +257,8 @@ class mfBaseController {
|
|||||||
$url .= $params;
|
$url .= $params;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if($anker) {
|
if ($anker) {
|
||||||
$url.="#$anker";
|
$url .= "#$anker";
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = preg_replace('#^/+#', "/", $url);
|
$url = preg_replace('#^/+#', "/", $url);
|
||||||
@@ -242,30 +268,31 @@ class mfBaseController {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getUrl($mod, $action=null, $param=null) {
|
public static function getUrl($mod, $action = null, $param = null)
|
||||||
if(!$mod) {
|
{
|
||||||
|
if (!$mod) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(MFUSEFANCYURLS) {
|
if (MFUSEFANCYURLS) {
|
||||||
// use fancy urls
|
// use fancy urls
|
||||||
$url=MFFANCYBASEURL;
|
$url = MFFANCYBASEURL;
|
||||||
if($mod) {
|
if ($mod) {
|
||||||
$url.="/$mod";
|
$url .= "/$mod";
|
||||||
if($action) {
|
if ($action) {
|
||||||
$url.="/$action";
|
$url .= "/$action";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$url = preg_replace('#//#','/',$url);
|
$url = preg_replace('#//#', '/', $url);
|
||||||
} else {
|
} else {
|
||||||
// no fancy urls
|
// no fancy urls
|
||||||
$url="?action=$mod";
|
$url = "?action=$mod";
|
||||||
if($action) {
|
if ($action) {
|
||||||
$url.="_$action";
|
$url .= "_$action";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(is_array($param) && count($param)) {
|
if (is_array($param) && count($param)) {
|
||||||
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
||||||
$param_qs = http_build_query($param);
|
$param_qs = http_build_query($param);
|
||||||
$url .= "$param_qs";
|
$url .= "$param_qs";
|
||||||
@@ -274,8 +301,9 @@ class mfBaseController {
|
|||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function returnJson($data) {
|
public static function returnJson($data)
|
||||||
if(is_array($data)) {
|
{
|
||||||
|
if (is_array($data)) {
|
||||||
header("Content-Type: application/json");
|
header("Content-Type: application/json");
|
||||||
echo json_encode($data);
|
echo json_encode($data);
|
||||||
exit;
|
exit;
|
||||||
@@ -286,38 +314,40 @@ class mfBaseController {
|
|||||||
|
|
||||||
// Helper functions
|
// Helper functions
|
||||||
|
|
||||||
public static function dateToTimestamp($date) {
|
public static function dateToTimestamp($date)
|
||||||
|
{
|
||||||
// extract day, month, year
|
// extract day, month, year
|
||||||
if (!preg_match('/^(\d{1,2})\.(\d{1,2})\.(\d{2,4})/',$date,$d)) {
|
if (!preg_match('/^(\d{1,2})\.(\d{1,2})\.(\d{2,4})/', $date, $d)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// extract time if available
|
// extract time if available
|
||||||
if (preg_match('/(\d\d):(\d\d):(\d\d)$/',$date,$t)) {
|
if (preg_match('/(\d\d):(\d\d):(\d\d)$/', $date, $t)) {
|
||||||
if (!$t[3]) {
|
if (!$t[3]) {
|
||||||
$t[3] = 0;
|
$t[3] = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$t = array(0,0,0);
|
$t = array(0, 0, 0);
|
||||||
}
|
}
|
||||||
// make and return timestamp
|
// make and return timestamp
|
||||||
$ts = mktime($t[1],$t[2],$t[3],$d[2],$d[1],$d[3]);
|
$ts = mktime($t[1], $t[2], $t[3], $d[2], $d[1], $d[3]);
|
||||||
return $ts;
|
return $ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function dateToDB($date,$type='l') {
|
public static function dateToDB($date, $type = 'l')
|
||||||
|
{
|
||||||
// get timestamp
|
// get timestamp
|
||||||
$ts = self::dateToTimestamp($date);
|
$ts = self::dateToTimestamp($date);
|
||||||
|
|
||||||
// only proceed if timestamp conversion was successful
|
// only proceed if timestamp conversion was successful
|
||||||
if(!$ts) {
|
if (!$ts) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return date and time if long type requested
|
// return date and time if long type requested
|
||||||
if($type = 'l') {
|
if ($type = 'l') {
|
||||||
$dbdate = date('Y-m-d H:i:s',$ts);
|
$dbdate = date('Y-m-d H:i:s', $ts);
|
||||||
} else {
|
} else {
|
||||||
$dbdate = date('Y-m-d',$ts);
|
$dbdate = date('Y-m-d', $ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dbdate;
|
return $dbdate;
|
||||||
|
|||||||
Reference in New Issue
Block a user