/** * The Autoloader initialisation * * @return void */ public static function _init() { // is the profiler enabled? if (!(static::$_enabled = ClanCats::$config->get('profiler.enabled'))) { return; } // enable profiling only in development mode if (ClanCats::in_development()) { // add a hook to the resposne so that we can // append a table with the profiler data to the body CCEvent::mind('response.output', function ($output) { if (strpos($output, '</body>') === false) { return $output; } $table = \UI\Table::create(array('style' => array('width' => '100%'), 'cellpadding' => '5', 'class' => 'table debug-table debug-table-profiler')); $table->header(array('#', 'message', 'memory', 'time')); foreach (\CCProfiler::data() as $key => $item) { $table->row(array($key + 1, $item[0], $item[1], $item[2])); } // add the table before the body end return str_replace('</body>', $table . "\n</body>", $output); }); // also add an error inspector hook so that we can access the // profiler data in the error handler CCError_Inspector::info_callback('Profiler', function () { $table = array(); foreach (\CCProfiler::data() as $key => $check) { $table['#' . ($key + 1) . ': ' . $check[2]] = $check[0]; } return $table; }); } }
/** * install an orbit module * * @param array $params */ public function action_permissions($params) { $folders = \CCEvent::fire('ccdoctor.permissions'); if (!is_array($folders)) { $folders = array(); } // add storage directories foreach (\ClanCats::$config->get('storage.paths') as $folder) { $folders[] = $folder; } foreach ($folders as $folder) { $display_folder = \CCStr::replace($folder, array(CCROOT => '')); // create directory if not existing if (!is_dir($folder)) { if (!mkdir($folder, 0755, true)) { $this->error("doctor could not create folder at: {$display_folder}"); } } // check permissions $perm = substr(decoct(fileperms($folder)), 2); if ($perm < 755) { CCCli::line(CCCli::color($perm, 'red') . ' - ' . $display_folder . ' fixing with ' . CCCli::color('755', 'green')); if (!chmod($folder, 0755)) { CCCli::line("doctor - is not able to change permissions for: {$display_folder}", 'red'); } } elseif ($perm == 777) { CCCli::line(CCCli::color($perm, 'yellow') . ' - ' . $display_folder . ' warning! this can be dangerous.'); } else { $this->success('- ' . $display_folder, $perm); } } }
/** * initialize the ship * * @return void */ public function wake() { // wrap the assets \CCFinder::alias('CCAsset', __DIR__ . '/CCAsset' . EXT); // map the other classes \CCFinder::package(__DIR__ . '/', array('Packtacular' => 'Packtacular' . EXT, 'Packtacular\\Theme' => 'Theme' . EXT, 'lessc' => 'lib/lessc.inc' . EXT)); // add writeable directory hook \CCEvent::mind('ccdoctor.permissions', function () { return PUBLICPATH . \CCConfig::create("Packtacular::packtacular")->path; }); }
/** * Static init * When we are in development then we append the qurey log to body * * @codeCoverageIgnore * * @return void */ public static function _init() { if (\ClanCats::in_development()) { // add a hook to the main resposne \CCEvent::mind('response.output', function ($output) { if (strpos($output, '</body>') === false) { return $output; } $table = \UI\Table::create(array('style' => array('width' => '100%'), 'cellpadding' => '5', 'class' => 'table debug-table debug-table-db')); $table->header(array('#', 'query')); foreach (static::log() as $key => $item) { $table->row(array($key + 1, $item)); } return str_replace('</body>', $table . "\n</body>", $output); }); } }
/** * error class init * * @return void */ public static function _init() { // we capture non fatal errors only in dev environments if (ClanCats::in_development()) { // add a hook to the main resposne CCEvent::mind('response.output', function ($output) { if (strpos($output, '</body>') === false) { return $output; } $table = \UI\Table::create(array('style' => array('width' => '100%'), 'cellpadding' => '5', 'class' => 'table debug-table debug-table-errors')); $table->header(array('#', 'message', 'file')); foreach (\CCError::$non_fatals as $key => $item) { $table->row(array($key + 1, $item->getMessage(), CCStr::strip($item->getFile(), CCROOT) . ':' . $item->getLine())); } return str_replace('</body>', $table . "\n</body>", $output); }); } }
/** * Get full details for an Event object * @param string $url - url to an Event * @return Event */ public function getEventDetails($url) { $response = $this->CTCTRequest->makeRequest($url, 'GET'); $parsedResponse = simplexml_load_string($response['xml'], null, null, "http://www.w3.org/2005/Atom"); return new CCEvent(CCEvent::createStruct($parsedResponse)); }
/** * send response * means printing the response and setting the headers if set * * @param bool $headers * @return void */ public function send($headers = false) { if ($headers && headers_sent() && !CLI) { throw new CCException("CCResponse::send - cannot send header, header has already been send."); } if ($headers) { // status header header(CCIn::server('SERVER_PROTOCOL') . ' ' . $this->_status . ' ' . CCResponse::$messages[$this->_status]); // check if content type is already set if (!isset($this->_header['Content-Type'])) { $this->header('Content-Type', 'text/html; charset=' . ClanCats::$config->get('charset', 'utf-8')); } $this->header('X-Powered-By', 'ClanCatsFramework version: ' . ClanCats::VERSION); // set headers foreach ($this->_header as $key => $content) { header($key . ': ' . $content); } } // profiler CCProfiler::check('CCResponse - sending response'); // print the body echo CCEvent::pass('response.output', $this->body()); }
/** * Sign a user out * * @param id $user_id * @param string $name * @return false */ public function sign_out() { if (!$this->authenticated) { return false; } // remove the restore login \DB::delete($this->config->get('logins.table'))->where('restore_token', $this->restore_key($this->user))->run(); // logout the user $this->session->delete($this->config->session_key); // pass the user object through all user hooks $this->user = \CCEvent::pass('auth.sign_out', $this->user); $this->user->save(); $user_model = $this->config->user_model; // create new empty user $this->user = new $user_model(); return $this->authenticated = false; }
/** * Handler::sign_in keep login tests */ public function test_sign_in_keeper() { Auth\Handler::kill_instance('main'); $example_user = clone static::$current_user; $auth = Auth\Handler::create(); $auth->sign_in($example_user, false); $this->assertTrue($auth->user instanceof DB\Model); $this->assertEquals(static::$current_user->id, $auth->user->id); // test valid Auth\Handler::kill_instance('main'); $auth = Auth\Handler::create(); $this->assertTrue($auth->valid()); // lets create an keeper login now $this->create_keeper_login(); // lets test the login store event $this->assertEquals(null, $auth->login()->client_ip); $auth->session->destroy(); CCEvent::mind('auth.store_login', function ($data) { $data['client_ip'] = '127.0.0.1'; return $data; }); Auth\Handler::kill_instance('main'); $auth = Auth\Handler::create(); $this->assertTrue($auth->valid()); $this->assertEquals('127.0.0.1', $auth->login()->client_ip); // now lets modify some data to force restore failure // changing the the current client ip will force failure CCIn::instance(new CCIn_Instance(array(), array(), array(), array(), array('REMOTE_ADDR' => '192.168.1.42'))); $this->keeper_login_false(); // next lets modify the users password wich will force a failure $this->create_keeper_login(); $this->keeper_login_true(); static::$current_user->password = "******"; static::$current_user->save(); $this->keeper_login_false(); // modifiy the restore_id $this->create_keeper_login(); $this->keeper_login_true(); CCCookie::set('ccauth-restore-id', '34'); $this->keeper_login_false(); // modifiy the restore_token $this->create_keeper_login(); $this->keeper_login_true(); CCCookie::set('ccauth-restore-token', 'wrong'); $this->keeper_login_false(); // delete the user $this->create_keeper_login(); $this->keeper_login_true(); static::$current_user->delete(); $this->keeper_login_false(); // create him again static::$current_user->save(); }
/** * static initialisation * * @return void */ public static function _init() { CCEvent::after('CCF.shutdown', array(get_class(), 'write')); }
/** * Resolve an uri and get the route object * * @param string $uri * @return CCRoute */ public static function resolve($uri) { // cut unnecessary slashes and params if (substr($uri, -1) == '/') { $uri = substr($uri, 0, -1); } if (substr($uri, 0, 1) == '/') { $uri = substr($uri, 1); } $uri = CCStr::cut($uri, '?'); // create new route instance $route = new CCRoute($uri); // private route if (substr($uri, 0, 1) == '#') { if (!array_key_exists($uri, static::$privates)) { throw new CCException("CCRouter::resolve() - private route {$uri} could not be found."); } return static::configure($route, static::$privates[$uri]); } // pass the route trough the priority events if ($hook_route = CCEvent::pass('ccf.router.resolve.before', array($route, false))) { if ($hook_route[1] === true) { return static::configure($route, $hook_route[0]); } } // if the uri is empty root is requestet if (empty($uri)) { // try one slash also aka empty if (array_key_exists('', static::$routes)) { return static::configure($route, static::$routes['']); } return static::configure($route, static::$privates['#root']); } // simple static key route if (isset(static::$routes[$uri])) { return static::configure($route, static::$routes[$uri]); } // explode the uri $uri_parts = explode('/', $uri); // get the action $action = array_pop($uri_parts); // implode the uri without action $uri_without_action = implode('/', $uri_parts); // try the static key with an action if (isset($action)) { if (isset(static::$routes[$uri_without_action])) { if (CCController::has_action(static::$routes[$uri_without_action], $action)) { $route->action = $action; return static::configure($route, static::$routes[$uri_without_action]); } } } // dynamic keys foreach (static::$routes as $pattern => $dynamic_route) { // try the dynamic route only if it can be a pattern if (strpos($pattern, '[') !== false && strpos($pattern, ']') !== false) { // build an reqular expression $regx = '#^' . CCStr::replace($pattern, static::$filters) . '$#i'; // try to match the uri with regex if (preg_match($regx, $uri, $params)) { // remove the first param unset($params[0]); $route->action = null; $route->params = $params; return static::configure($route, $dynamic_route); } // try to match the uri without the action with the regex if (is_string($dynamic_route) && preg_match($regx, $uri_without_action, $params)) { if (CCController::has_action($dynamic_route, $action)) { // remove the first param unset($params[0]); $route->action = $action; $route->params = $params; return static::configure($route, $dynamic_route); } } } } /* * pass to events */ if ($hook_route = CCEvent::pass('ccf.router.resolve.after', array($route, false))) { if ($hook_route[1] === true) { return static::configure($route, $hook_route[0]); } } return false; }
* but also some mini helpers like _dd() for var_dump and die. */ require COREPATH . 'shortcuts' . EXT; /* *--------------------------------------------------------------- * shutdown handler *--------------------------------------------------------------- * * Register our shutdown handler so we can display custom error * messages and run events before shutdown like saving the * session ect.. */ register_shutdown_function(function () { // try to run all shutdown hooks try { \CCEvent::fire('CCF.shutdown'); } catch (\Exception $e) { } // run error shutdown to catch possible errors if (class_exists("\\CCError")) { \CCError::shutdown(); } }); /* *--------------------------------------------------------------- * exception handler *--------------------------------------------------------------- * * Register our handler for uncaught exceptions, so we can * handle them on our own. */
/** * Session constructor * * @param string $name * @param array $config */ protected function __construct($name, $config = null) { if (is_null($config)) { $config = \CCConfig::create('session')->get($name); // check for an alias. If you set a string // in your config file we use the config // with the passed key. if (is_string($config)) { $config = \CCConfig::create('session')->get($config); } } if (empty($config)) { throw new Exception("Session\\Manager::create - Invalid session manager (" . $name . ")."); } // also don't forget to set the name manager name becaue we need him later. $this->_name = $name; // keep the configuration array $this->_config = $config; // Setup the driver class. We simply use name // from the confif file and make the first letter // capital. example: Handler_Mysql, Handler_Sqlite etc. $driver_class = __NAMESPACE__ . "\\Manager_" . ucfirst($config['driver']); if (!class_exists($driver_class)) { throw new Exception("Session\\Manager::create - The driver (" . $driver_class . ") is invalid."); } $this->set_driver($driver_class); // try to get the id from cookie $this->id = $this->cookie_session_id(); // set the fingerprint $this->fingerprint = sha1($this->id); // Before reading we might have to kill old sessions using // the Garbage collector if (\CCArr::get('gc.enabled', $this->_config, true)) { if (mt_rand(1, \CCArr::get('gc.factor', $this->_config, 25)) == 1) { $this->gc(); } } // Register a shutdown event to write the session down // This should not happen on shutdown if we using command line if (!\ClanCats::is_cli()) { \CCEvent::mind('CCF.shutdown', array($this, 'write')); } // Now get the inital data from our driver $this->read(); }