/** * The initial call of the CCServer get all Superglobals * and assigns them to itself as an holder. * * @return void */ public static function _init() { // create new instance from default input CCServer::$_instance = CCIn::create($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER); // unset default http holder to safe mem //unset( $_GET, $_POST, $_COOKIE, $_SERVER, $_FILES ); }
/** * CCValidator::post tests */ public function test_post() { CCIn::instance(new CCIn_Instance(array(), array('agb' => 1), array(), array(), array())); $validator = CCValidator::post(array('agb' => (bool) CCIn::post('agb'))); $this->assertTrue($validator instanceof CCValidator); $this->assertInternalType('bool', $validator->data('agb')); $this->assertTrue($validator->data('agb')); }
/** * Sign up action * * @return CCResponse */ public function action_sign_up() { // When the user is already authenticated we redirect him home. if (CCAuth::valid()) { return CCRedirect::to('/'); } $this->theme->topic = __(':action.topic'); $this->view = $this->theme->view('auth/sign_up.view'); // create a new user object as data holder $user = new User(); // bind the newly created user object to our view $this->view->bind('user', $user); if (CCIn::method('post')) { // Lets assign the email and the password to our // user object using the stirct assign method wich // will ignore all other post values in the assing process. $user->strict_assign(array('email', 'password'), CCIn::all('post')); $validator = CCValidator::post(); // assign the labels to the validator this way we get // correct translated error messages. $validator->label(array('email' => __('model/user.label.email'), 'password' => __('model/user.label.password'), 'password_match' => __('model/user.label.password_match'))); // does the user already exist $validator->set('same_email', User::find('email', $user->email)); $validator->message(__(':action.message.email_in_use'), 'negative', 'same_email'); // validate the other fields $validator->rules('email', 'required', 'email'); $validator->rules('password', 'required', 'min:6'); $validator->rules('password_match', 'required', 'match:password'); // when the data passes the validation if ($validator->success()) { // because the user input is correct we can now save the // object to the database and sign the user in. $user->save(); CCAuth::sign_in($user); UI\Alert::flash('success', __(':action.message.success')); return CCRedirect::to('/'); } else { UI\Alert::add('danger', $validator->errors()); } } }
/** * Execute the Request * * @param array $action * @param array $params * * @return self */ public function perform() { // set the input if (!is_null($this->input)) { CCIn::instance($this->input); } else { CCIn::instance(CCServer::instance()); } // set current request static::$_current =& $this; // route is invalid show 404 if (!$this->route instanceof CCRoute) { $this->route = CCRouter::resolve('#404'); } /* * call wake events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('wake', $this->route->uri) as $callback) { if (($return = CCContainer::call($callback)) instanceof CCResponse) { $this->response = $return; return $this; } } /* * a closure */ if (!is_array($this->route->callback) && is_callable($this->route->callback)) { // execute and capture the output ob_start(); // run the closure $return = call_user_func_array($this->route->callback, $this->route->params); // catch the output $output = ob_get_clean(); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the captured output $return = CCResponse::create($output); } } elseif (is_callable($this->route->callback)) { // execute the callback and get the return $return = call_user_func_array($this->route->callback, array($this->route->action, $this->route->params)); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the return as string $return = CCResponse::create((string) $return); } } else { $return = CCResponse::error(404); } // set the response $this->response = $return; /* * call sleep events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('sleep', $this->route->uri) as $callback) { if ($return = CCContainer::call($callback, $this->response) instanceof CCResponse) { $this->response = $return; return $this; } } return $this; }
/** * get the path of an uplaoded file * * @param string $key * @return string|false */ public static function upload_path($key) { return CCArr::get('tmp_name', CCIn::file($key, array('tmp_name' => false))); }
/** * Get the current url * * @param array $params * @param bool $retain Should we keep the get parameters? * @return string */ public static function current($params = array(), $retain = false) { return static::to(CCIn::uri(), $params, $retain); }
/** * Redirect to next parameter * also sanitize the parameter we only allow internal redirects * * @return CCResponse */ public static function next() { return static::full(parse_url(CCIn::get('next'), PHP_URL_PATH)); }
/** * test assignment * generates an clean Input instance * you can pass custom params for testing */ public function fakeServerData($add_get = array(), $add_post = array(), $add_server = array()) { $add_get = array_merge(array('foo' => 32, 'hello' => 'world', 'id' => '453'), $add_get); $add_post = array_merge(array('foo' => 32, 'hello' => 'world', 'id' => '453', 'some' => 'Other other String'), $add_post); $add_server = array_merge(array('HTTP_HOST' => 'local.ccf2.com', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9) AppleWebKit/537.71 (KHTML, like Gecko) Version/7.0 Safari/537.71', 'HTTP_ACCEPT_LANGUAGE' => 'en-us', 'HTTP_CACHE_CONTROL' => 'max-age=0', 'HTTP_CONNECTION' => 'keep-alive', 'SERVER_SOFTWARE' => 'Apache/2.4.4 (Unix) PHP/5.4.16 OpenSSL/1.0.1e mod_perl/2.0.8-dev Perl/v5.16.3', 'SERVER_NAME' => 'local.ccf2.com', 'SERVER_ADDR' => '127.0.0.1', 'SERVER_PORT' => '80', 'REMOTE_ADDR' => '123.121.123.121', 'REQUEST_SCHEME' => 'http', 'REMOTE_PORT' => '51749', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'REQUEST_METHOD' => 'POST', 'QUERY_STRING' => '', 'REQUEST_URI' => '/', 'SCRIPT_NAME' => '/index.php', 'PHP_SELF' => '/index.php', 'REQUEST_TIME' => time(), 'CLANCATS_ENV' => 'development'), $add_server); CCIn::instance(new CCIn_Instance($add_get, $add_post, array(), array(), $add_server)); }
/** * CCUrl::active tests */ public function test_active() { // fake some data CCIn::instance(new CCIn_Instance(array('param' => 1, 'test' => 'tee'), array(), array(), array(), array('REQUEST_URI' => '//foo/bar//file.xml?param=1&test=tee'))); $this->assertFalse(CCUrl::active('/')); $this->assertTrue(CCUrl::active('/foo/bar')); $this->assertFalse(CCUrl::active('/foo/boo')); $this->assertTrue(CCUrl::active('http://example.com/foo/bar')); $this->assertTrue(CCUrl::active('foo')); $this->assertTrue(CCUrl::active('foo/bar/file.xml?sdfsd')); }
/** * 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 the user and optinal also set the resore keys * * @param Auth\User $user * @param bool $keep_login * @return bool */ public function sign_in(\Auth\User $user, $keep_login = true) { // set the session key so the session knows we are logged in $this->session->set($this->config->session_key, $user->{$this->config->user_key}); // update the current user object $this->user = $user; // update the last login timestamp $this->user->last_login = time(); // pass the user trough the events to allow modifications // of the user object at sign in $this->user = \CCEvent::pass('auth.sign_in', $this->user); // save the user object to the database $this->user->save(); // set the restore keys to keep the login // after the session ends if ($keep_login) { $restore_id_cookie = $this->config->get('restore.id_cookie'); $restore_token_cookie = $this->config->get('restore.token_cookie'); $restore_lifetime = $this->config->get('restore.lifetime'); $restore_id = $this->session->get($this->config->session_key); $restore_token = $this->restore_key($this->user); CCCookie::set($restore_id_cookie, $restore_id, $restore_lifetime); CCCookie::set($restore_token_cookie, $restore_token, $restore_lifetime); // try to get the current login $login = $this->select_logins()->where('restore_id', $restore_id)->where('restore_token', $restore_token); // prepare the login data $login_data = array('restore_id' => $restore_id, 'restore_token' => $restore_token, 'last_login' => time(), 'client_agent' => \CCIn::client()->agent); // pass the login data trough the events $login_data = \CCEvent::pass('auth.store_login', $login_data); // if there is no such login create a new one if (!$login->run()) { \DB::insert($this->config->get('logins.table'), $login_data)->run($this->config->get('logins.handler')); } else { \DB::update($this->config->get('logins.table'), $login_data)->where('restore_id', $restore_id)->where('restore_token', $restore_token)->run($this->config->get('logins.handler')); } } // and finally we are authenticated return $this->authenticated = true; }
/** * 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(); }
/** * Does the current session fingerprint match a parameter * * When no parameter is given we use GET->s as default parameter * * @param string $fingerprint * @return string */ public function valid_fingerprint($fingerprint = null) { if (is_null($fingerprint)) { $fingerprint = \CCIn::get(\ClanCats::$config->get('session.default_fingerprint_parameter'), false); } return $this->fingerprint === $fingerprint; }
if ($queries < 1) { $queries = 1; } if ($queries > 500) { $queries = 500; } $worlds = array(); for ($i = 0; $i < $queries; ++$i) { $world = DB::select('World')->find(mt_rand(1, 10000)); $world->id = intval($world->id); $world->randomNumber = intval($world->randomNumber); $worlds[] = $world; } return CCResponse::create(json_encode($worlds), 200)->header('Content-Type', 'application/json'); }, 'updates' => function () { $queries = CCIn::get('queries', 1); if ($queries < 1) { $queries = 1; } if ($queries > 500) { $queries = 500; } $worlds = array(); for ($i = 0; $i < $queries; ++$i) { $id = mt_rand(1, 10000); DB::update('World')->set('randomNumber', mt_rand(1, 10000))->where('id', $id)->run(); $world = DB::select('World')->find($id); $world->id = intval($world->id); $world->randomNumber = intval($world->randomNumber); $worlds[] = $world; }