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:
Frank Schubert
2024-04-16 12:59:34 +00:00
+50 -20
View File
@@ -3,7 +3,8 @@
// 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,7 +16,8 @@ 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();
@@ -23,8 +25,11 @@ class mfBaseController {
$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")) {
@@ -41,7 +46,7 @@ class mfBaseController {
// 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']);
} }
@@ -77,7 +82,7 @@ class mfBaseController {
$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")) {
@@ -86,7 +91,8 @@ class mfBaseController {
} }
} }
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;
@@ -97,7 +103,8 @@ class mfBaseController {
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();
} }
@@ -105,7 +112,8 @@ class mfBaseController {
} }
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
@@ -130,9 +138,12 @@ class mfBaseController {
} }
public function __call($name,$params) { public function __call($name, $params)
{
$methodname = false;
if (method_exists($this, $name)) { if (method_exists($this, $name)) {
return call_user_func(array($this, $name), $params); // 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)) {
@@ -140,14 +151,26 @@ class mfBaseController {
} }
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);
} }
} }
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;
} }
public function __get($name) { return call_user_func(array($this, $methodname), $params);
}
public function __get($name)
{
if ($name == "db") { if ($name == "db") {
return $this->db(); return $this->db();
} }
@@ -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,7 +192,8 @@ 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 (!$dbhost) $dbhost = FRONKDB_DBHOST;
if (!$dbuser) $dbhost = FRONKDB_DBUSER; if (!$dbuser) $dbhost = FRONKDB_DBUSER;
if (!$dbpass) $dbhost = FRONKDB_DBPASS; if (!$dbpass) $dbhost = FRONKDB_DBPASS;
@@ -177,7 +202,8 @@ class mfBaseController {
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);
@@ -242,7 +268,8 @@ 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 "";
} }
@@ -274,7 +301,8 @@ 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);
@@ -286,7 +314,8 @@ 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;
@@ -304,7 +333,8 @@ class mfBaseController {
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);