Add NAMESPACE support (not activated, but just needed to add the namespace on top of file)

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4630 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-10-24 14:36:00 +00:00
parent 3b2f0e6f82
commit 681863f187
13 changed files with 53 additions and 49 deletions

View File

@@ -1,7 +1,8 @@
<?php <?php
/** DomFramework /** DomFramework
@package domframework * @package domframework
@author Dominique Fournier <dominique@fournier38.fr> */ * @author Dominique Fournier <dominique@fournier38.fr>
*/
/** User authentication (abstract class) */ /** User authentication (abstract class) */
class auth class auth
@@ -143,7 +144,7 @@ class auth
*/ */
public function connect () public function connect ()
{ {
throw new Exception (dgettext("domframework", throw new \Exception (dgettext("domframework",
"No connect to authentication available"), "No connect to authentication available"),
405); 405);
} }

View File

@@ -1,7 +1,8 @@
<?php <?php
/** DomFramework /** DomFramework
@package domframework * @package domframework
@author Dominique Fournier <dominique@fournier38.fr> */ * @author Dominique Fournier <dominique@fournier38.fr>
*/
require_once ("domframework/auth.php"); require_once ("domframework/auth.php");
require_once ("domframework/authparams.php"); require_once ("domframework/authparams.php");
@@ -116,8 +117,8 @@ class authentication
// If the user is already connected, redirect to the main page of the site // If the user is already connected, redirect to the main page of the site
if (session_id () === "") if (session_id () === "")
session_start (); session_start ();
$auth = new \auth (); $auth = new auth ();
$pre = new \authparams (array ("session")); $pre = new authparams (array ("session"));
if (isset ($_SESSION["domframework"]["authentication"]["message"])) if (isset ($_SESSION["domframework"]["authentication"]["message"]))
$message = $_SESSION["domframework"]["authentication"]["message"]; $message = $_SESSION["domframework"]["authentication"]["message"];
else else
@@ -168,7 +169,7 @@ class authentication
$this->route->redirect ("/authentication/$url", ""); $this->route->redirect ("/authentication/$url", "");
} }
} }
$authparams = new \authparams (array ("post")); $authparams = new authparams (array ("post"));
$res = $this->verifAuth ($authparams->email, $authparams->password); $res = $this->verifAuth ($authparams->email, $authparams->password);
if (! is_array ($res)) if (! is_array ($res))
{ {
@@ -217,7 +218,7 @@ class authentication
if ($this->debug) if ($this->debug)
echo "=== entering verifAuthREST (restMethods=". echo "=== entering verifAuthREST (restMethods=".
print_r ($this->restMethods, true).")\n"; print_r ($this->restMethods, true).")\n";
$authparams = new \authparams ($this->restMethods); $authparams = new authparams ($this->restMethods);
$res = array ("email"=>"anonymous", "password"=>"anonymous"); $res = array ("email"=>"anonymous", "password"=>"anonymous");
if ($authparams->email !== "anonymous" && if ($authparams->email !== "anonymous" &&
$authparams->password !== "anonymous") $authparams->password !== "anonymous")
@@ -245,7 +246,7 @@ class authentication
if ($this->debug) if ($this->debug)
echo "=== entering verifAuthHTML (htmlMethods=". echo "=== entering verifAuthHTML (htmlMethods=".
print_r ($this->htmlMethods, true).")\n"; print_r ($this->htmlMethods, true).")\n";
$authparams = new \authparams ($this->htmlMethods); $authparams = new authparams ($this->htmlMethods);
// Don't ask to the provider if anonymous is known // Don't ask to the provider if anonymous is known
if ($authparams->email === "anonymous" || $authparams->email === null) if ($authparams->email === "anonymous" || $authparams->email === null)
{ {
@@ -328,7 +329,8 @@ class authentication
if (! is_array ($serversParam)) if (! is_array ($serversParam))
throw new \Exception ("Auth Server $key configuration error : ". throw new \Exception ("Auth Server $key configuration error : ".
"not an array", 500); "not an array", 500);
$authmethod = new $classname (); $class = __NAMESPACE__."\\$classname";
$authmethod = new $class ();
foreach ($serversParam as $param=>$value) foreach ($serversParam as $param=>$value)
{ {
if ($this->debug) if ($this->debug)

View File

@@ -1,7 +1,8 @@
<?php <?php
/** DomFramework /** DomFramework
@package domframework * @package domframework
@author Dominique Fournier <dominique@fournier38.fr> */ * @author Dominique Fournier <dominique@fournier38.fr>
*/
require_once ("domframework/auth.php"); require_once ("domframework/auth.php");
@@ -36,7 +37,7 @@ class authldap extends auth
function __construct () function __construct ()
{ {
if (!function_exists ("ldap_connect")) if (!function_exists ("ldap_connect"))
throw new Exception ("LDAP support unavailable in PHP", 500); throw new \Exception ("LDAP support unavailable in PHP", 500);
} }
/** Establish a connection to a LDAP server /** Establish a connection to a LDAP server
@@ -46,13 +47,13 @@ class authldap extends auth
{ {
$this->ldapconn = ldap_connect ($this->ldapserver, $this->ldapport); $this->ldapconn = ldap_connect ($this->ldapserver, $this->ldapport);
if (!$this->ldapconn) if (!$this->ldapconn)
throw new Exception ("Can't contact LDAP server", 500); throw new \Exception ("Can't contact LDAP server", 500);
ldap_set_option ($this->ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option ($this->ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($this->ldapconn, LDAP_OPT_TIMELIMIT, $this->ldaptimeout); ldap_set_option($this->ldapconn, LDAP_OPT_TIMELIMIT, $this->ldaptimeout);
$ldapbind = @ldap_bind ($this->ldapconn, $this->ldapauth, $this->ldappwd); $ldapbind = @ldap_bind ($this->ldapconn, $this->ldapauth, $this->ldappwd);
if (ldap_errno ($this->ldapconn) !== 0) if (ldap_errno ($this->ldapconn) !== 0)
throw new Exception ("Authentication error in pre-auth LDAP", 500); throw new \Exception ("Authentication error in pre-auth LDAP", 500);
} }
/** Try to authenticate the email/password of the user /** Try to authenticate the email/password of the user
@@ -64,15 +65,15 @@ class authldap extends auth
$search = ldap_search ($this->ldapconn, $this->ldapbase, $filter, $search = ldap_search ($this->ldapconn, $this->ldapbase, $filter,
array ($this->ldapfield)); array ($this->ldapfield));
if ($search === FALSE) if ($search === FALSE)
throw new Exception ("Unable to search in LDAP", 500); throw new \Exception ("Unable to search in LDAP", 500);
$info = ldap_get_entries ($this->ldapconn, $search); $info = ldap_get_entries ($this->ldapconn, $search);
if (!isset ($info["count"]) || $info["count"] !== 1 || !isset ($info[0]) || if (!isset ($info["count"]) || $info["count"] !== 1 || !isset ($info[0]) ||
!isset ($info[0]["dn"])) !isset ($info[0]["dn"]))
throw new Exception ("Unable to find the user : '$email'", 401); throw new \Exception ("Unable to find the user : '$email'", 401);
$dn = $info[0]["dn"]; $dn = $info[0]["dn"];
$ldapbind2 = @ldap_bind ($this->ldapconn, $dn, $password); $ldapbind2 = @ldap_bind ($this->ldapconn, $dn, $password);
if ($ldapbind2 !== TRUE) if ($ldapbind2 !== TRUE)
throw new Exception ("Bad password for '$email'", 401); throw new \Exception ("Bad password for '$email'", 401);
$this->ldapdnuser = $dn; $this->ldapdnuser = $dn;
} }
@@ -80,11 +81,11 @@ class authldap extends auth
public function getdetails () public function getdetails ()
{ {
if ($this->ldapdnuser === NULL) if ($this->ldapdnuser === NULL)
throw new Exception ("No user authenticated !", 401); throw new \Exception ("No user authenticated !", 401);
$search = ldap_search ($this->ldapconn, $this->ldapdnuser, $search = ldap_search ($this->ldapconn, $this->ldapdnuser,
$this->ldapfiltersearch); $this->ldapfiltersearch);
if ($search === FALSE) if ($search === FALSE)
throw new Exception ("Can not found the details for user", 401); throw new \Exception ("Can not found the details for user", 401);
$data = ldap_get_entries ($this->ldapconn, $search); $data = ldap_get_entries ($this->ldapconn, $search);
$res = array (); $res = array ();
if (isset ($data[0])) if (isset ($data[0]))
@@ -103,7 +104,7 @@ class authldap extends auth
@param string $newpassword The new password to be recorded */ @param string $newpassword The new password to be recorded */
public function changepassword ($oldpassword, $newpassword) public function changepassword ($oldpassword, $newpassword)
{ {
throw new Exception (dgettext("domframework", throw new \Exception (dgettext("domframework",
"The password can't be change for LDAP users"), 405); "The password can't be change for LDAP users"), 405);
} }
@@ -124,12 +125,12 @@ class authldap extends auth
public function listusers () public function listusers ()
{ {
if ($this->ldapconn === NULL) if ($this->ldapconn === NULL)
throw new Exception ("No established LDAP connection", 500); throw new \Exception ("No established LDAP connection", 500);
$search = ldap_search ($this->ldapconn,$this->ldapbase, $search = ldap_search ($this->ldapconn,$this->ldapbase,
$this->ldapfiltersearch, $this->ldapfiltersearch,
array ("mail","sn","givenname")); array ("mail","sn","givenname"));
if ($search === FALSE) if ($search === FALSE)
throw new Exception ("Unable to search the users in LDAP", 500); throw new \Exception ("Unable to search the users in LDAP", 500);
$info = ldap_get_entries ($this->ldapconn, $search); $info = ldap_get_entries ($this->ldapconn, $search);
$data = array (); $data = array ();
foreach ($info as $key=>$vals) foreach ($info as $key=>$vals)

View File

@@ -1,7 +1,8 @@
<?php <?php
/** DomFramework /** DomFramework
@package domframework * @package domframework
@author Dominique Fournier <dominique@fournier38.fr> */ * @author Dominique Fournier <dominique@fournier38.fr>
*/
/** Takes the email and the password of the user */ /** Takes the email and the password of the user */
class authparams class authparams

View File

@@ -25,7 +25,7 @@ class http
if (!isset ($userchoices[0])) if (!isset ($userchoices[0]))
{ {
if ($default === FALSE) if ($default === FALSE)
throw new Exception ("No user choice provided to bestChoice", 500); throw new \Exception ("No user choice provided to bestChoice", 500);
return $default; return $default;
} }
// Remove weights (provided by the order of the user) // Remove weights (provided by the order of the user)

View File

@@ -18,7 +18,7 @@ class lockfile
if (!is_dir (dirname ($this->storagelock))) if (!is_dir (dirname ($this->storagelock)))
@mkdir (dirname ($this->storagelock)); @mkdir (dirname ($this->storagelock));
if (!is_dir (dirname ($this->storagelock))) if (!is_dir (dirname ($this->storagelock)))
throw new Exception ("Can't create '".dirname ($this->storagelock). throw new \Exception ("Can't create '".dirname ($this->storagelock).
"' directory for lockfile", "' directory for lockfile",
500); 500);
$lockid = uniqid() . '_' . md5 (mt_rand()); $lockid = uniqid() . '_' . md5 (mt_rand());
@@ -39,7 +39,7 @@ class lockfile
usleep (100000); usleep (100000);
clearstatcache (TRUE, $this->storagelock); clearstatcache (TRUE, $this->storagelock);
if (microtime (TRUE) > $startTime + 10) if (microtime (TRUE) > $startTime + 10)
throw new Exception ("Can't have the authorization lock RW in 10s", throw new \Exception ("Can't have the authorization lock RW in 10s",
500); 500);
} }
@@ -56,7 +56,7 @@ class lockfile
{ {
usleep (100000); usleep (100000);
if (microtime (TRUE) > $startTime + 10) if (microtime (TRUE) > $startTime + 10)
throw new Exception ("Can't have the authorization lock RO in 10s", throw new \Exception ("Can't have the authorization lock RO in 10s",
500); 500);
} }
@@ -71,7 +71,7 @@ class lockfile
if (!is_dir (dirname ($this->storagelock))) if (!is_dir (dirname ($this->storagelock)))
@mkdir (dirname ($this->storagelock)); @mkdir (dirname ($this->storagelock));
if (!is_dir (dirname ($this->storagelock))) if (!is_dir (dirname ($this->storagelock)))
throw new Exception ("Can't create '".dirname ($this->storagelock)."'", throw new \Exception ("Can't create '".dirname ($this->storagelock)."'",
500); 500);
$lockid = uniqid() . '_' . md5 (mt_rand()); $lockid = uniqid() . '_' . md5 (mt_rand());
// Stale lock : 60s old min // Stale lock : 60s old min
@@ -89,7 +89,7 @@ class lockfile
usleep (100000); usleep (100000);
clearstatcache (TRUE, $this->storagelock); clearstatcache (TRUE, $this->storagelock);
if (microtime (TRUE) > $startTime + 10) if (microtime (TRUE) > $startTime + 10)
throw new Exception ("Can't have the authorization lock in 10s", 500); throw new \Exception ("Can't have the authorization lock in 10s", 500);
} }
touch ($this->storagelock."-".$lockid); touch ($this->storagelock."-".$lockid);

View File

@@ -10,6 +10,6 @@ class output
@param mixed $data The data to be displayed */ @param mixed $data The data to be displayed */
public function out ($data) public function out ($data)
{ {
throw new Exception ("No type of output selected"); throw new \Exception ("No type of output selected");
} }
} }

View File

@@ -53,7 +53,7 @@ class outputhtml extends output
if (isset ($resView["title"])) if (isset ($resView["title"]))
$title = $resView["title"]; $title = $resView["title"];
if (! isset ($resView["content"])) if (! isset ($resView["content"]))
throw new Exception (sprintf ( throw new \Exception (sprintf (
dgettext("domframework", dgettext("domframework",
"No data provided from view %s::%s"), "No data provided from view %s::%s"),
$viewClass,$viewMethod), $viewClass,$viewMethod),

View File

@@ -13,7 +13,7 @@ class outputjson extends output
function __construct () function __construct ()
{ {
if (!function_exists ("json_encode")) if (!function_exists ("json_encode"))
throw new Exception ("JSON support not available in PHP ! apt-get ". throw new \Exception ("JSON support not available in PHP ! apt-get ".
"install php5-json", 500); "install php5-json", 500);
} }

View File

@@ -32,7 +32,7 @@ class outputrest extends output
{ {
if (! isset ($variable["exceptionCode"])) if (! isset ($variable["exceptionCode"]))
$variable["exceptionCode"] = 200; $variable["exceptionCode"] = 200;
$rest = new \rest (); $rest = new rest ();
$rest->display ($data, $variable["exceptionCode"]); $rest->display ($data, $variable["exceptionCode"]);
} }
} }

View File

@@ -108,22 +108,22 @@ class ratelimitfile extends ratelimit
if (file_exists ($this->storageDir)) if (file_exists ($this->storageDir))
{ {
if (! is_dir ($this->storageDir)) if (! is_dir ($this->storageDir))
throw new Exception (sprintf (dgettext ("domframework", throw new \Exception (sprintf (dgettext ("domframework",
"storageDir '%s' is not a directory"), "storageDir '%s' is not a directory"),
$this->storageDir), 500); $this->storageDir), 500);
if (! is_readable ($this->storageDir)) if (! is_readable ($this->storageDir))
throw new Exception (sprintf (dgettext ("domframework", throw new \Exception (sprintf (dgettext ("domframework",
"storageDir '%s' is not readable"), "storageDir '%s' is not readable"),
$this->storageDir), 500); $this->storageDir), 500);
if (! is_writeable ($this->storageDir)) if (! is_writeable ($this->storageDir))
throw new Exception (sprintf (dgettext ("domframework", throw new \Exception (sprintf (dgettext ("domframework",
"storageDir '%s' is not writeable"), "storageDir '%s' is not writeable"),
$this->storageDir), 500); $this->storageDir), 500);
} }
else else
{ {
if (@mkdir ($this->storageDir, 0777, true) === false) if (@mkdir ($this->storageDir, 0777, true) === false)
throw new Exception (sprintf (dgettext ("domframework", throw new \Exception (sprintf (dgettext ("domframework",
"Can't create '%s' storageDir"), "Can't create '%s' storageDir"),
$this->storageDir), 500); $this->storageDir), 500);
} }

View File

@@ -27,14 +27,13 @@ class renderer
$this->output */ $this->output */
public function run () public function run ()
{ {
if (!@require_once ("domframework/output$this->output.php")) require_once ("domframework/outputhtml.php");
throw new \Exception ("Renderer '$this->output' not found", 500); $class = __NAMESPACE__."\\output$this->output";
$class = "output$this->output"; $obj = new $class ();
$reflection = new \ReflectionMethod ($class, "out"); $res = call_user_func_array (array ($obj, "out"),
$res = $reflection->invokeArgs (new $class, array ($this->result, $this->title,
array ($this->result, $this->title, $this->viewClass, $this->viewMethod, $this->layout,
$this->viewClass, $this->viewMethod, $this->layout, $this->replacement, $this->variable));
$this->replacement, $this->variable));
echo $res; echo $res;
} }

View File

@@ -28,7 +28,7 @@ class rest
} }
require_once ("domframework/output$type.php"); require_once ("domframework/output$type.php");
$constr = "output$type"; $constr = __NAMESPACE__."\\output$type";
$method = "out"; $method = "out";
$obj = new $constr (); $obj = new $constr ();
$obj->$method ($message); $obj->$method ($message);