$email” will be printed after the “This will print before debug messages.\n\n”message.. In the next section, we look at how we can incorporate all of these classes to cre-ate an abstract
Trang 1$email”) will be printed after the “This will print before debug messages.
\n\n”message.
In the next section, we look at how we can incorporate all of these classes to cre-ate an abstract PHP application class.
Creating an Abstract Application Class
The code in Listing 4-4 uses class.DBI.php, class.ErrorHandler.php, and
class.Debugger.phpto create an abstract PHP application class.
Listing 4-4: class.PHPApplication.php
<?php /*
*
* PHPApplication class
*
* @author <php@evoknow.com>
* @access public
*
* Version 1.0.1
*/
if (defined(“DEBUGGER_LOADED”) && ! empty($DEBUGGER_CLASS)) {
include_once $DEBUGGER_CLASS;
}
//require_once ‘lib.session_handler.php’;
class PHPApplication {
function PHPApplication($param = null) {
global $ON, $OFF, $TEMPLATE_DIR;
Continued
Trang 2Listing 4-4 (Continued)
global $MESSAGES, $DEFAULT_LANGUAGE,
$REL_APP_PATH,
$REL_TEMPLATE_DIR;
// initialize application
$this->app_name = $this->setDefault($param[‘app_name’], null);
$this->app_version = $this->setDefault($param[‘app_version’], null);
$this->app_type = $this->setDefault($param[‘app_type’], null);
$this->app_db_url = $this->setDefault($param[‘app_db_url’], null);
$this->debug_mode= $this->setDefault($param[‘app_debugger’], null);
$this->auto_connect = $this->setDefault($param[‘app_auto_connect’], TRUE);
$this->auto_chk_session =
$this->setDefault($param[‘app_auto_chk_session’], TRUE);
$this->auto_authorize =
$this->setDefault($param[‘app_auto_authorize’], TRUE);
>session_ok =
$this->setDefault($param[‘app_auto_authorize’], FALSE);
$this->error = array();
$this->authorized= FALSE;
$this->language = $DEFAULT_LANGUAGE;
$this->base_url = sprintf(“%s%s”, $this->get_server(),
$REL_TEMPLATE_DIR);
$this->app_path = $REL_APP_PATH;
$this->template_dir = $TEMPLATE_DIR;
$this->messages = $MESSAGES;
// If debuggger is ON then create a debugger object
if (defined(“DEBUGGER_LOADED”) && $this->debug_mode == $ON) {
if (empty($param[‘debug_color’])) {
$param[‘debug_color’] = ‘red’;
}
$this->debugger = new Debugger(array(‘color’ =>
$param[‘debug_color’],
‘prefix’ => $this->app_name,
‘buffer’ => $OFF));
Trang 3$this->has_error = null;
$this->set_error_handler();
// start session
if (strstr($this->get_type(), ‘WEB’)) {
session_start();
$this->user_id = (! empty($_SESSION[“SESSION_USER_ID”])) ?
$_SESSION[“SESSION_USER_ID”] : null;
$this->user_name = (! empty($_SESSION[“SESSION_USERNAME”])) ?
$_SESSION[“SESSION_USERNAME”]: null;;
$this->user_email = (! empty($_SESSION[“SESSION_USERNAME”])) ?
$_SESSION[“SESSION_USERNAME”]: null;;
$this->set_url();
if ($this->auto_chk_session) $this->check_session();
if (! empty(>app_db_url) && >auto_connect && !
$this->connect())
{
$this->alert(‘APP_FAILED’);
}
if ($this->auto_authorize && ! $this->authorize()) {
$this->alert(‘UNAUTHORIZED_ACCESS’);
}
} }
function getEMAIL() {
return $this->user_email;
}
Continued
Trang 4Listing 4-4 (Continued)
function getNAME() {
list($name, $host) = explode(‘’, $this->getEMAIL());
return ucwords($name);
}
function check_session() {
if ($this->session_ok == TRUE) {
return TRUE;
}
if (!empty($this->user_name)) {
$this->session_ok = TRUE;
} else {
$this->session_ok = FALSE;
$this->reauthenticate();
}
return $this->session_ok;
}
function reauthenticate() {
global $AUTHENTICATION_URL;
header(“Location: $AUTHENTICATION_URL?url=$this->self_url”);
}
function getBaseURL() {
return $this->base_url;
}
Trang 5$this->set_url();
return $this->server;
}
function getAppPath() {
return $this->app_path;
}
function getFQAP() {
// get fully qualified application path
return sprintf(“%s%s”,$this->server, $this->app_path);
}
function getFQAN($thisApp = null) {
return sprintf(“%s/%s”, $this->getFQAP(), $thisApp);
}
function getTemplateDir() {
return $this->template_dir;
}
function set_url() {
$row_protocol = $this->getEnvironment(‘SERVER_PROTOCOL’);
$port = $this->getEnvironment(‘SERVER_PORT’);
if ($port == 80) {
$port = null;
} else {
$port = ‘:’ $port;
}
$protocol = strtolower(substr($row_protocol,0, strpos($row_protocol,’/’)));
Continued