/** * Dispatch a request. * * This will determine which module and action to use by request parameters * specified by the user. * * @param array $arg_options * @param array $req_args * @return void */ public function dispatch($arg_options = array(), $req_args = array()) { try { // Setup default arg_options and req_args arrays $opt_array = array("-m" => "module", "--module" => "module", "-a" => "action", "--action" => "action", "-h" => "help_flag", "--help" => "help_flag"); $arg_options = array_merge($opt_array, $arg_options); $req_array = array("module", "action"); $req_args = array_merge($req_array, $req_args); $this->parseArgs($arg_options); $moduleName = @$_REQUEST['module']; $actionName = @$_REQUEST['action']; // initialize the controller $this->initialize(); // get the application context $context = $this->getContext(); // Check to see if script is being run on console if (isset($_SERVER['_']) && !isset($_SERVER['HTTP_HOST'])) { $context->getUser()->setAuthenticated(true); $this->getContext()->getUser()->addCredential(MO_CONSOLE_CREDENTIAL); define("MO_IS_CONSOLE", true); } if ($moduleName == null) { // no module has been specified $moduleName = MO_DEFAULT_MODULE; } if ($actionName == null) { // no action has been specified if ($this->actionExists($moduleName, 'Index')) { // an Index action exists $actionName = 'Index'; } else { // use the default action $actionName = MO_DEFAULT_ACTION; } } if (!$this->hasRequiredArgs($req_args) || isset($_REQUEST['help_flag'])) { $this->getAction($moduleName, $actionName)->showHelpMessage(); } else { // make the first request $this->forward($moduleName, $actionName); } } catch (MojaviException $e) { $e->printStackTrace('console'); } catch (Exception $e) { // most likely an exception from a third-party library $e = new MojaviException($e->getMessage()); $e->printStackTrace('console'); } }
/** * Dispatch a request. * * This will determine which module and action to use by request parameters * specified by the user. * * @return void */ public function dispatch() { try { // initialize the controller $this->initialize(); // get the application context $context = $this->getContext(); // determine our module and action $moduleName = ucfirst($context->getRequest()->getParameter(MO_MODULE_ACCESSOR)); $actionName = ucfirst($context->getRequest()->getParameter(MO_ACTION_ACCESSOR)); $moduleName = preg_replace_callback("/_([a-zA-Z0-9])/", function ($matches) { return strtoupper($matches[1]); }, $moduleName); $moduleName = preg_replace_callback("/-([a-zA-Z0-9])/", function ($matches) { return strtoupper($matches[1]); }, $moduleName); $actionName = preg_replace_callback("/_([a-zA-Z0-9])/", function ($matches) { return strtoupper($matches[1]); }, $actionName); $actionName = preg_replace_callback("/-([a-zA-Z0-9])/", function ($matches) { return strtoupper($matches[1]); }, $actionName); if ($moduleName == null) { // no module has been specified $moduleName = MO_DEFAULT_MODULE; } if ($actionName == null) { // no action has been specified if ($this->actionExists($moduleName, 'Index')) { // an Index action exists $actionName = 'Index'; } else { // use the default action $actionName = MO_DEFAULT_ACTION; } } // make the first request $this->forward($moduleName, $actionName); } catch (MojaviException $e) { $e->printStackTrace(); } catch (Exception $e) { // most likely an exception from a third-party library $e = new MojaviException($e->getMessage()); $e->printStackTrace(); } }
/** * Dispatch a request. * * @param string A module name. * @param string An action name. * @param array An associative array of parameters to be set. * * @return void */ public function dispatch($moduleName, $actionName, $parameters = null) { try { // initialize the controller $this->initialize(); // set parameters if ($parameters != null) { $this->getContext()->getRequest()->setParametersByRef($parameters); } // make the first request $this->forward($moduleName, $actionName); } catch (MojaviException $e) { $e->printStackTrace(); } catch (Exception $e) { // most likely an exception from a third-party library $e = new MojaviException($e->getMessage()); $e->printStackTrace(); } }
/** * Execute an SQL Statement and return result to be handled by calling function. * * @param mixed PreparedStatement or KeyBasedPreparedStatement * @param string Name of connection to be used * @param resource $connection Connection resource handler * @return mixed Resource if query executed successfully, otherwise false */ public function executeInsert(PreparedStatement $ps, $name = 'default', &$con = NULL, $debug = self::DEBUG) { $retval = false; try { // Connect to database if (is_null($con)) { if (self::DEBUG) { LoggerManager::error(__METHOD__ . ":: Retrieving New DB Connection for '" . $name . "'..."); } $con = $this->getDatabaseConnection($name); } // Get the prepared query /* @var $sth PDOStatement */ $sth = $ps->getPreparedStatement($con); if ($debug) { LoggerManager::error(__METHOD__ . " :: " . $sth); } // Execute the query $sth->execute(); return $con->lastInsertId(); } catch (MojaviException $e) { LoggerManager::fatal($e->printStackTrace('')); throw $e; } catch (PDOException $e) { ob_start(); $sth->debugDumpParams(); $stmt = ob_get_clean(); $e = new MojaviException($e->getMessage()); LoggerManager::fatal($sth->queryString); LoggerManager::fatal($stmt); LoggerManager::fatal($e->printStackTrace('')); throw $e; } catch (Exception $e) { $e = new MojaviException($e->getMessage()); LoggerManager::fatal($e->printStackTrace('')); throw $e; } }
/** * Handles autoloading of classes that have been specified in autoload.ini. * * @param string A class name. * * @return void */ function __autoload($class) { // this static variable is generated by the $config file below static $classes; if (!isset($classes)) { try { // include the list of autoload classes $config = ConfigCache::checkConfig('config/autoload.ini'); require_once $config; } catch (MojaviException $e) { $e->printStackTrace(); } catch (Exception $e) { // unknown exception $e = new MojaviException($e->getMessage()); $e->printStackTrace(); } } if (isset($classes[$class])) { // class exists, let's include it require_once $classes[$class]; } else { $className = ltrim($class, '\\'); $fileName = ''; $namespace = ''; if (($lastNsPos = strripos($className, '\\')) !== false) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className); if (file_exists(MO_LIB_DIR . DIRECTORY_SEPARATOR . $fileName . '.php')) { require_once $fileName . '.php'; } else { // Split the class by underscores and look for it $class_file = str_replace('_', DIRECTORY_SEPARATOR, $class); if (file_exists(MO_LIB_DIR . DIRECTORY_SEPARATOR . $class_file . '.php')) { require_once MO_LIB_DIR . DIRECTORY_SEPARATOR . $class_file . '.php'; } else { if (false) { // Destroy the session completely session_destroy(); // unspecified class $error = 'Autoloading of class "%s" failed'; $error = sprintf($error, $class); $e = new AutoloadException($error); $e->printStackTrace(); // Clear the cache // Clearing the cache here can cause a bunch of bus errors in apache, so we don't do it anymore */ // ConfigCache::clear(); if (in_array(session_name(), $_COOKIE)) { if (file_exists("/tmp/sess_" . $_COOKIE[session_name()])) { try { unlink("/tmp/sess_" . $_COOKIE[session_name()]); } catch (Exception $e) { $e = new \Mojavi\Exception\MojaviException($e->getMessage()); $e->printStackTrace(""); } } } } } } } }
/** * Execute an SQL Statement and return result to be handled by calling function. * * @param mixed PreparedStatement or KeyBasedPreparedStatement * @param string Name of connection to be used * @param resource $connection Connection resource handler * @return mixed Resource if query executed successfully, otherwise false */ public function executeInsert(PreparedStatement $ps, $name = 'default', $con = NULL, $debug = self::DEBUG) { if ($ps instanceof PdoPreparedStatement) { return parent::executeInsert($ps, $name, $con, $debug); } $retval = false; try { // Connect to database if (is_null($con)) { if (self::DEBUG) { LoggerManager::debug(__METHOD__ . ":: Retrieving New DB Connection for '" . $name . "'..."); } $con = $this->getContext()->getDatabaseConnection($name); } if (function_exists('mysql_ping')) { if (!mysql_ping($con)) { $this->getContext()->getDatabaseManager()->getDatabase($name)->shutdown(); $con = $this->getContext()->getDatabaseConnection($name); } } else { throw new Exception('Missing php-mysql libraries on this server'); } // Get the prepared query $query = $ps->getPreparedStatement($con); if ($debug) { LoggerManager::error(__METHOD__ . " :: " . $ps->getDebugQueryString()); } // Execute the query $rs = mysql_query($query, $con); if (!$rs) { throw new Exception(mysql_error($con)); } else { return mysql_insert_id($con); } } catch (MojaviException $e) { $this->getErrors()->addError('error', new Error($e->getMessage())); LoggerManager::fatal($e->printStackTrace('')); throw $e; } catch (PDOException $e) { $e = new MojaviException($e->getMessage()); LoggerManager::fatal($e->printStackTrace('')); throw $e; } catch (Exception $e) { $this->getErrors()->addError('error', new Error($e->getMessage())); $e = new MojaviException($e->getMessage()); LoggerManager::fatal($e->printStackTrace('')); throw $e; } }
/** * Execute an SQL Statement and return result to be handled by calling function. * * @param mixed PreparedStatement or KeyBasedPreparedStatement * @param string Name of connection to be used * @param resource $connection Connection resource handler * @return mixed Resource if query executed successfully, otherwise false */ public function executeInsert(PreparedStatement $ps, $name = 'default', $con = NULL, $debug = self::DEBUG) { $retval = false; try { // Connect to database if (is_null($con)) { if (self::DEBUG) { LoggerManager::debug(__METHOD__ . ":: Retrieving New DB Connection for '" . $name . "'..."); } $con = $this->getContext()->getDatabaseConnection($name); } // Get the prepared query /* @var $sth PDOStatement */ $sth = $ps->getPreparedStatement($con); // Debug the query to the log if ($debug) { LoggerManager::error(__METHOD__ . " :: " . $ps->getDebugQueryString()); } // Execute the query $sth->execute(); // Return the last insert id $retval = $con->lastInsertId(); } catch (PDOException $e) { // If the MySQL server has gone away, try reconnecting, otherwise throw an exception if ($e->getMessage() == 'SQLSTATE[HY000]: General error: 2006 MySQL server has gone away') { try { $this->getContext()->getDatabaseManager()->getDatabase($name)->shutdown(); // Connect to database $con = $this->getContext()->getDatabaseConnection($name); // Get the prepared query /* @var $sth PDOStatement */ $sth = $ps->getPreparedStatement($con); if ($debug) { LoggerManager::error(__METHOD__ . " :: " . $ps->getDebugQueryString()); } // Execute the query $sth->execute(); $retval = $sth; } catch (Exception $e) { ob_start(); $sth->debugDumpParams(); $stmt = ob_get_clean(); $this->getErrors()->addError('error', new Error($e->getMessage() . ": " . $sth->queryString)); $e = new MojaviException($e->getMessage()); LoggerManager::fatal($sth->queryString); LoggerManager::fatal($stmt); LoggerManager::fatal($e->printStackTrace('')); throw $e; } } else { if (strpos($e->getMessage(), 'Lock wait timeout exceeded; try restarting transaction') !== false) { // If there was a lock on the transaction, then try it again before failing try { $this->getContext()->getDatabaseManager()->getDatabase($name)->shutdown(); // Connect to database $con = $this->getContext()->getDatabaseConnection($name); // Get the prepared query /* @var $sth PDOStatement */ $sth = $ps->getPreparedStatement($con); if ($debug) { LoggerManager::error(__METHOD__ . " :: " . $ps->getDebugQueryString()); } // Execute the query $sth->execute(); $retval = $sth; } catch (Exception $e) { ob_start(); $sth->debugDumpParams(); $stmt = ob_get_clean(); $this->getErrors()->addError('error', new Error($e->getMessage() . ": " . $sth->queryString)); $e = new MojaviException($e->getMessage()); LoggerManager::fatal($sth->queryString); LoggerManager::fatal($stmt); LoggerManager::fatal($e->printStackTrace('')); throw $e; } } else { ob_start(); $sth->debugDumpParams(); $stmt = ob_get_clean(); $this->getErrors()->addError('error', new Error($e->getMessage() . ": " . $sth->queryString)); $e = new MojaviException($e->getMessage()); LoggerManager::fatal($sth->queryString); LoggerManager::fatal($stmt); LoggerManager::fatal($e->printStackTrace('')); throw $e; } } } catch (MojaviException $e) { // Output Mojavi Exceptions to the log and throw the Exception $this->getErrors()->addError('error', new Error($e->getMessage())); LoggerManager::fatal($e->printStackTrace('')); throw $e; } catch (Exception $e) { // Output Normal Exceptions to the log and throw the Exception $this->getErrors()->addError('error', new Error($e->getMessage())); $e = new MojaviException($e->getMessage()); LoggerManager::fatal($e->printStackTrace('')); throw $e; } return $retval; }
/** * Retrieve a new Controller implementation instance. * * @param string A Controller implementation name. * * @return Controller A Controller implementation instance. * * @throws <b>FactoryException</b> If a new controller implementation * instance cannot be created. */ public static function newInstance($class) { try { if (!isset(self::$instance)) { // the class exists $object = new $class(); if (!$object instanceof Controller) { // the class name is of the wrong type $error = 'Class "%s" is not of the type Controller'; $error = sprintf($error, $class); throw new FactoryException($error); } // set our singleton instance self::$instance = $object; return $object; } else { $type = get_class(self::$instance); // an instance has already been created $error = 'A Controller implementation instance has already ' . 'been created'; throw new FactoryException($error); } } catch (MojaviException $e) { $e->printStackTrace(); } catch (Exception $e) { // most likely an exception from a third-party library $e = new MojaviException($e->getMessage()); $e->printStackTrace(); } }