Exemplo n.º 1
0
 function call($arguments = null)
 {
     Console::debugEx(LOG_DEBUG1, __CLASS__, "Delegate invocation");
     foreach ($this->callable as $classes) {
         @call_user_func_array($classes, (array) $arguments);
     }
 }
Exemplo n.º 2
0
 function exception(Exception $e)
 {
     logger::emerg("Unhandled exception: (%s) %s in %s:%d", get_class($e), $e->getMessage(), str_replace(BASE_PATH, '', $e->getFile()), $e->getLine());
     Console::debugEx(0, get_class($e), "Unhandled exception: (%s) %s in %s:%d", get_class($e), $e->getMessage(), str_replace(BASE_PATH, '', $e->getFile()), $e->getLine());
     $f = file($e->getFile());
     foreach ($f as $i => $line) {
         $mark = $i + 1 == $e->getLine() ? '=> ' : '   ';
         $f[$i] = sprintf('  %05d. %s', $i + 1, $mark) . $f[$i];
         $f[$i] = str_replace("\n", "", $f[$i]);
     }
     $first = $e->getLine() - 4;
     if ($first < 0) {
         $first = 0;
     }
     $last = $e->getLine() + 3;
     if ($last >= count($f)) {
         $last = count($f) - 1;
     }
     $source = join("\n", array_slice($f, $first, $last - $first));
     Console::debugEx(0, get_class($e), Console::backtrace(0, $e->getTrace(), true));
     Console::debugEx(LOG_LOG, "Exception", "Source dump of %s:\n%s", str_replace(BASE_PATH, '', $e->getFile()), $source);
     $rv = 1;
     logger::emerg("Exiting with return code %d after exception.", $rv);
     Console::debugEx(LOG_BASIC, __CLASS__, "Exiting with return code %d after exception.", $rv);
 }
Exemplo n.º 3
0
 function loadView($view)
 {
     $path = base::expand($view, '/views');
     Console::debugEx(LOG_BASIC, __CLASS__, "Attempting to invoke view from %s", $path);
     if (file_exists($path)) {
         Console::debugEx(LOG_BASIC, __CLASS__, "Invoking as Pure PHP View");
         $this->path = $path;
     } else {
         throw new ViewNotFoundException("The view " . $view . " could not be found");
     }
 }
Exemplo n.º 4
0
 function run($app = 'app')
 {
     MvcApplication::$app = $app;
     Console::debugEx(LOG_VERBOSE, __CLASS__, 'Invoking router...');
     // Create new router and invoke it
     response::setStatus(200);
     $router = config::get(self::KEY_MVC_ROUTER, 'DefaultRouter');
     $r = new $router();
     $r->route();
     return 0;
 }
Exemplo n.º 5
0
 /**
  * @brief Invoke a controller method
  *
  * @param string $controller
  * @param string $method
  * @param array $arguments
  * @return int Exit code
  */
 static function invoke($controller = null, $method = null, array $arguments = null)
 {
     if (strpos($controller, '/') !== false) {
         $cfull = explode('/', $controller);
         $cpath = join('/', array_slice($cfull, 0, count($cfull) - 1)) . '/controllers';
         $controller = $cfull[count($cfull) - 1];
     } else {
         $cpath = 'controllers';
     }
     if (!$controller) {
         $controller = 'default';
     }
     // config
     if (!$method) {
         $method = 'index';
     }
     // config
     if (config::get(controller::KEY_TRANSLATE, false) == true) {
         $method = str_replace('-', '_', $method);
     }
     $controller = strtolower($controller);
     $method = strtolower($method);
     $ctlpath = base::apppath() . '/' . $cpath . '/' . $controller . '.php';
     Console::debugEx(LOG_VERBOSE, __CLASS__, 'Invoking %s:%s (%s)', $controller, $method, $ctlpath);
     $cc = $controller . 'Controller';
     if (!class_exists($cc)) {
         if (file_exists($ctlpath)) {
             require $ctlpath;
         } else {
             throw new NavigationException("Could not find controller class " . $controller);
             return RETURN_ERROR;
         }
     }
     $cr = new ReflectionClass($cc);
     if ($cr->hasMethod($method)) {
         $mr = $cr->getMethod($method);
         $args = array();
         for ($n = 0; $n < $mr->getNumberOfParameters(); $n++) {
             if ($n < count($arguments)) {
                 $args[$n] = $arguments[$n];
             } else {
                 $args[$n] = null;
             }
         }
         $arguments = $args;
     }
     $ci = new $cc();
     if (!$ci->__request($method, (array) $arguments)) {
         return RETURN_ERROR;
     } else {
         return RETURN_SUCCESS;
     }
 }
Exemplo n.º 6
0
 function __construct()
 {
     $this->cachefile = base::appPath() . '/.l2cache';
     $this->lockfile = base::appPath() . '/.l2lock';
     $this->hlockfile = fopen($this->lockfile, "w");
     flock($this->hlockfile, LOCK_EX);
     if (file_exists($this->cachefile)) {
         Console::debugEx(LOG_BASIC, __CLASS__, "Loading package cache from %s", $this->cachefile);
         $this->loadCache();
     } else {
         Console::debugEx(LOG_BASIC, __CLASS__, "Package cache not found");
         $this->initCache();
     }
 }
Exemplo n.º 7
0
 function __construct($matchtok, $str)
 {
     $md = explode(' ', $str);
     $mi = 0;
     $mo = array();
     Console::debugEx(LOG_DEBUG2, __CLASS__, "Parsing quotes in array for %s", $matchtok);
     Console::debugEx(LOG_DEBUG2, __CLASS__, " \$md = {'%s'}", join("','", $md));
     while ($mi < count($md)) {
         Console::debugEx(LOG_DEBUG2, __CLASS__, "Current token: %s", $md[$mi]);
         $qt = $md[$mi][0];
         if ($qt == '"' || $qt == "'") {
             $buf = array();
             while ($mi < count($md)) {
                 $str = $md[$mi];
                 $buf[] = $md[$mi++];
                 Console::debugEx(LOG_DEBUG2, __CLASS__, " -- Quoted token: %s (%s)", $str, $str[strlen($str) - 1]);
                 if ($str[strlen($str) - 2] == $qt) {
                     break;
                 }
             }
             $bufstr = join(' ', $buf);
             $bufstr = substr($bufstr, 1, strlen($bufstr) - 2);
             $mo[] = $bufstr;
             Console::debugEx(LOG_DEBUG2, __CLASS__, "Joined quoted statement: %s", $bufstr);
         } else {
             $mo[] = $md[$mi++];
         }
     }
     $md = $mo;
     Console::debugEx(LOG_DEBUG2, __CLASS__, " \$md = {'%s'}", join("','", $md));
     $mi = 0;
     $matchtoks = explode(' ', $matchtok);
     while ($mi < count($md)) {
         Console::debugEx(LOG_DEBUG1, __CLASS__, 'Parsing tokenized data for %s', $md[$mi]);
         $token = strtolower($md[$mi]);
         foreach ($matchtoks as $tok) {
             $ti = explode(':', $tok);
             if ($ti[0] == $token) {
                 Console::debugEx(LOG_DEBUG2, __CLASS__, "Matched token %s", $token);
                 $this->_tokens[$ti[0]] = join(' ', array_slice($md, $mi + 1, $ti[1]));
                 $mi += $ti[1];
                 break;
             }
         }
         $mi++;
     }
 }
Exemplo n.º 8
0
 /**
  * @brief Main router entry point
  *
  * Override this in your router to get full control over the way the
  * request is being fed to the router class.
  *
  * @return Mixed The result from the routerequest call
  */
 public function route()
 {
     Console::debugEx(LOG_VERBOSE, __CLASS__, 'Looking for event handlers before routing');
     // Invoke events first to see if anything is registered
     if (event::invoke(MvcEvent::EVENT_BEFORE_ROUTING, array('uri' => $this->_uri, 'segments' => $this->_urisegments, 'domain' => $this->_domain, 'secure' => $this->_secure)) == true) {
         return 0;
     }
     Console::debugEx(LOG_VERBOSE, __CLASS__, 'Examining static routes');
     // Determine if this is a hooked uri
     foreach (Router::$_staticroutes as $sr) {
         if (@preg_match('/' . $sr['match'] . '/', $this->_uri, $ret)) {
             call_user_func_array($sr['hook'], array_slice($ret, 1));
             return 0;
         }
     }
     Console::debugEx(LOG_VERBOSE, __CLASS__, 'Invoking the router');
     // Invoke the router
     return $this->routeRequest($this->_uri);
 }
Exemplo n.º 9
0
 /**
  *
  * @param string $pattern Sprintf-style pattern to query
  * @param string $vars Variables to assign to pattern
  * @return null Nothing
  */
 function exec($pattern, $vars = null)
 {
     $args = func_get_args();
     $sql = $this->conn->quote($args);
     $this->debug[] = $sql;
     Console::debugEx(LOG_DEBUG1, __CLASS__, "Execute: %s", $sql);
     Database::$counter++;
     Database::$queries['EXECUTING']++;
     $queryresult = $this->conn->execute($sql, $this->attribs);
     return null;
 }
Exemplo n.º 10
0
 protected function addField($field, $meta)
 {
     // TODO: Verify the meta format
     $md = explode(' ', $meta);
     $mi = 0;
     $mo = array();
     // Console::debugEx(LOG_DEBUG2,__CLASS__,"Parsing quotes in array for %s", $meta);
     // Console::debugEx(LOG_DEBUG2,__CLASS__," \$md = {'%s'}", join("','", $md));
     while ($mi < count($md)) {
         // Console::debugEx(LOG_DEBUG2,__CLASS__,"Current token: %s", $md[$mi]);
         if ($md[$mi][0] == '"') {
             $buf = array();
             while ($mi < count($md)) {
                 $str = $md[$mi];
                 $buf[] = $md[$mi++];
                 // Console::debugEx(LOG_DEBUG2,__CLASS__," -- Quoted token: %s (%s)", $str, $str[strlen($str)-1]);
                 if ($str[strlen($str) - 2] == '"') {
                     break;
                 }
             }
             $bufstr = join(' ', $buf);
             $bufstr = substr($bufstr, 1, strlen($bufstr) - 2);
             $mo[] = $bufstr;
             Console::debugEx(LOG_DEBUG2, __CLASS__, "Joined quoted statement: %s", $bufstr);
         } else {
             $mo[] = $md[$mi++];
         }
     }
     $md = $mo;
     // Console::debugEx(LOG_DEBUG2,__CLASS__," \$md = {'%s'}", join("','", $md));
     $ftype = null;
     $fdef = null;
     $freq = false;
     $fprot = false;
     $mi = 0;
     while ($mi < count($md)) {
         // Console::debugEx(LOG_DEBUG1,__CLASS__,'Parsing abstract model field %s: %s', $field, $md[$mi]);
         switch (strtolower($md[$mi])) {
             case 'string':
                 $ftype = 'STRING';
                 break;
             case 'int':
                 $ftype = 'INT';
                 break;
             case 'bool':
                 $ftype = 'BOOL';
                 break;
             case 'set':
                 $ftype = 'SET';
                 break;
             case 'enum':
                 $ftype = 'STRING';
                 break;
             case 'required':
                 $freq = true;
                 break;
             case 'protected':
                 $fprot = true;
                 break;
             case 'index':
                 $this->_index = $field;
                 break;
             case 'default':
                 $fdef = $md[++$mi];
                 break;
             case 'like':
                 $flike = $md[++$mi];
                 break;
             case 'in':
             case 'of':
                 $fin = $md[++$mi];
                 break;
             case 'format':
                 if ($ftype == 'INT' || $ftype == 'STRING') {
                     // Check format
                 } else {
                     Console::warn('Format declaration for key %s ignored', $field);
                 }
                 break;
             case 'auto':
                 if ($ftype == 'INT') {
                 } else {
                     Console::warn('Only INT can be auto fields');
                 }
                 $fauto = true;
                 break;
         }
         $mi++;
     }
     if ($ftype != null) {
         $this->_fields[$field] = array('type' => $ftype, 'required' => $freq, 'default' => $fdef, 'protected' => $fprot);
         return true;
     } else {
         Console::warn('Bad type specified for field %s in AbstractModel implementation', $field);
         Console::backtrace();
     }
     return false;
 }
Exemplo n.º 11
0
 function __destruct()
 {
     Console::debugEx(LOG_BASIC, '(destructor)', "Memory allocated at shutdown: %0.3f KB (Total used: %0.3f KB, By code: %0.3f KB)", memory_get_usage() / 1024 / 1024, memory_get_usage(true) / 1024 / 1024, (memory_get_usage(true) - SYSTEM_INITIAL_MEMORY_USE) / 1024 / 1024);
 }
Exemplo n.º 12
0
 function query($sql, $attr = null)
 {
     Console::debugEx(LOG_DEBUG2, __CLASS__, "SQL Query: %s", $sql);
     $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $qt = new Timer(true);
     try {
         $query = $this->conn->query($sql);
     } catch (PDOException $e) {
         throw new DatabaseException($e->getMessage(), intval($e->getCode()), $e);
     }
     $qtt = $qt->stop();
     if (class_exists('OptimizationReport') && $qtt >= config::get(RuntimeOptimization::KEY_DBQUERYTIME)) {
         $msg = sprintf('<p>The following query took %5.1fs to complete:</p><pre>%s</pre>', $qtt, wordwrap($sql));
         if (class_exists('OptimizationReport')) {
             OptimizationReport::addOptimizationHint('Slow SQL Query', 'DB:00001', 'warning', $msg);
         }
     }
     if ($query) {
         if ($query->rowCount() > 0) {
             try {
                 $fetchmode = MYSQLI_BOTH;
                 if ($attr & QueryAttributes::QATTR_GET_ASSOC) {
                     $data = $query->fetchAll(MYSQLI_ASSOC);
                 } elseif ($attr & QueryAttributes::QATTR_GET_NUMERIC) {
                     $data = $query->fetchAll(MYSQLI_NUM);
                 } else {
                     $data = $query->fetchAll();
                 }
                 $ret = array('data' => $data, 'count' => $query->rowCount(), 'columns' => $query->columnCount(), 'error' => false);
             } catch (PDOException $e) {
                 $ret = array('data' => null, 'count' => $query->rowCount(), 'columns' => null, 'error' => false);
             }
         } else {
             $ret = array('data' => null, 'count' => $query->rowCount(), 'columns' => null, 'error' => false);
         }
     } else {
         $ei = $this->conn->errorInfo();
         Console::warn("Database error: %s (%s)", $ei[2], $ei[0]);
         $ret = array('data' => null, 'count' => 0, 'columns' => 0, 'error' => $ei[2]);
     }
     $this->autonumber = $this->conn->lastInsertId();
     return $ret;
 }
Exemplo n.º 13
0
 /**
  *
  * @param string $pattern Sprintf-style pattern to query
  * @param string $vars Variables to assign to pattern
  * @return int The number of affected rows
  */
 function updateRow($pattern, $vars = null)
 {
     $args = func_get_args();
     $sql = $this->conn->escapeString($args);
     $this->debug[] = $sql;
     Console::debugEx(LOG_DEBUG1, __CLASS__, "UpdateRow: %s", $sql);
     Database::$counter++;
     Database::$queries['UPDATING']++;
     $queryresult = $this->conn->query($sql);
     $affected = $queryresult['count'];
     return $affected;
 }