Beispiel #1
0
 public function handle()
 {
     $this->page = Page::find_by_title($this->request->page);
     # Action tree
     if ($this->request->action_is('save')) {
         # TODO: validate request before saving
         if (strlen($this->request->post('wmd-input')) > self::MAX_BODY_LENGTH) {
             $this->t->flash('Page content is too long. Please shorten.', 'warning');
             $this->t->data('page-body', $this->request->post('wmd-input'));
         } else {
             $this->page->set('body', $this->request->post('wmd-input'));
             $this->page->save();
             NeechyResponse::redirect($this->page->url());
         }
     } elseif ($this->request->action_is('preview')) {
         $markdown = new Parsedown();
         $preview_html = $markdown->text($this->request->post('wmd-input'));
         $this->t->data('preview', $preview_html);
         $this->t->data('page-body', $this->request->post('wmd-input'));
     } elseif ($this->request->action_is('edit')) {
         $this->t->data('page-body', $this->request->post('wmd-input'));
     } else {
         $this->t->data('page-body', $this->page->field('body'));
     }
     # Partial variables
     $last_edited = sprintf('Last edited by %s on %s', $this->page->editor_link(), $this->page->field('created_at'));
     $page_title = NeechyTemplater::titleize_camel_case($this->page->get_title());
     # Render partial
     $this->t->data('action', $this->request->action);
     $this->t->data('page-title', $page_title);
     $this->t->data('last-edited', $last_edited);
     $content = $this->render_view('editor');
     # Return response
     return $this->respond($content);
 }
Beispiel #2
0
 public function handle()
 {
     if ($this->request->action_is('login')) {
         $login = new LoginValidator($this->request);
         if ($login->successful()) {
             $this->t->flash('You have been logged in.', 'success');
             $login->user->login();
             NeechyResponse::redirect($login->user->url());
         } else {
             $this->t->data('validation-errors', $login->errors);
             $this->t->data('login-name', $this->request->post('login-name'));
             $content = $this->render_view('login');
         }
         $this->t->data('alert', 'logging in');
     } elseif ($this->request->action_is('signup')) {
         $validator = new SignUpValidator($this->request);
         if ($validator->is_valid()) {
             $user = User::register($this->request->post('signup-name'), $this->request->post('signup-email'), $this->request->post('signup-pass'));
             $page = $this->save_user_page($user);
             NeechyResponse::redirect($page->url());
         } else {
             $this->t->data('validation-errors', $validator->errors);
             $this->t->data('signup-name', $this->request->post('signup-name'));
             $this->t->data('signup-email', $this->request->post('signup-email'));
             $content = $this->render_view('login');
         }
     } elseif ($this->request->page_is('logout')) {
         $this->t->flash('You have been logged out.', 'success');
         User::logout_current();
         $content = $this->render_view('login');
     } else {
         $content = $this->render_view('login');
     }
     return $this->respond($content);
 }
Beispiel #3
0
 public function redirect($handler, $page = null)
 {
     # Convenient wrapper for NeechyResponse::redirect. Also allows mocking for
     # testing, which is difficult with static methods. (See
     # http://stackoverflow.com/q/2357001/1093087).
     $url = NeechyPath::url($page, $handler);
     return NeechyResponse::redirect($url);
 }
Beispiel #4
0
<?php

require_once '../core/neechy/config.php';
require_once '../core/neechy/database.php';
require_once '../core/neechy/response.php';
# Parse params
$params = explode('/', $_SERVER["REQUEST_URI"]);
$action = count($params) > 2 ? $params[2] : null;
# Must init config for NeechyDatabase
NeechyConfig::init();
# Response
$json = array('params' => $params, 'action' => $action);
# Router
if ($action == 'database') {
    NeechyDatabase::reset();
    $json['result'] = 'ok';
} else {
    $json['warning'] = 'Invalid action.';
}
$response = new NeechyResponse(json_encode($json), 200);
$response->send_headers();
$response->render();
Beispiel #5
0
    private function dispatch_to_error($e)
    {
        $format = <<<STDERR

NEECHY CONSOLE ERROR:
%s


STDERR;
        $output = sprintf($format, $e->getMessage());
        $response = NeechyResponse::stderr($output);
        return $response;
    }