public function action_twittercallback() { if (arr::get($_GET, 'denied', false)) { notes::error('Seems like you didn\'t want to log in with Twitter anyway. Feel free to try again if it was a mistake!'); site::redirect(); } $token = arr::get($_GET, 'oauth_token', false); $verifier = arr::get($_GET, 'oauth_verifier', false); if (!$token || !$verifier) { notes::error('Something went wrong in the process, and we didn\'t get the expected data back from Twitter. Please try again'); site::redirect(); } $connection = new TwitterOAuth(arr::get($this->creds, 'key'), arr::get($this->creds, 'secret'), Session::instance()->get_once('twitter_oauth_token'), Session::instance()->get_once('twitter_oauth_token_secret')); $token = $connection->getAccessToken($verifier); $oauth_token = arr::get($token, 'oauth_token', ''); $oauth_token_secret = arr::get($token, 'oauth_token_secret', ''); $user_id = arr::get($token, 'user_id', ''); $screen_name = arr::get($token, 'screen_name', ''); $oauth = ORM::factory('Oauth')->where('type', '=', 'twitter')->where('token', '=', $oauth_token)->find(); if ($oauth->loaded()) { try { $user = $oauth->user; user::force_login($user); } catch (exception $e) { if ($user->loaded()) { if (user::logged()) { // Random error, but user got logged in. We don't care, YOLO! } else { notes::error('Whoops! Something wen\'t wrong and we couldn\'t log you in. Please try again or send us a message if the problem persists.'); Kohana::$log->add(Log::ERROR, '1. Couldnt log user in: ' . $e->getMessage()); } } } site::redirect('write'); } else { try { $user = ORM::factory('User'); $user->username = $screen_name; $user->validation_required(false)->save(); $user->add_role('login'); $oauth = ORM::factory('Oauth'); $oauth->user_id = $user->id; $oauth->type = 'twitter'; $oauth->token = $oauth_token; $oauth->token_secret = $oauth_token_secret; $oauth->service_id = $user_id; $oauth->screen_name = $screen_name; $oauth->save(); user::force_login($user); } catch (exception $e) { Kohana::$log->add(Log::ERROR, '2. Couldnt create user: '******'Whoops! Something wen\'t wrong and we couldn\'t log you in. Please try again or send us a message if the problem persists.'); } site::redirect('/write'); } }
public function action_write() { $errors = false; $page = false; if (user::logged()) { $page = $this->request->param('page'); if ($_POST && strlen(arr::get($_POST, 'content', '')) > 0) { $content = arr::get($_POST, 'content', ''); if ($page->type == 'page') { $raw = $page->rawcontent(); if ($raw != "") { $content = $raw . "\n" . $content; } } else { if ($page->type == 'autosave') { $page->type = 'page'; } } try { $page->wordcount = site::count_words($content); $page->content = $content; if ($page->wordcount >= 750 && !(bool) $page->counted) { user::update_stats($page); $page->counted = 1; } $page->duration = $page->duration + (time() - arr::get($_POST, 'start', 999)); $page->update(); $oldsaves = ORM::factory('Page')->where('type', '=', 'autosave')->where('user_id', '=', user::get()->id)->find_all(); if ((bool) $oldsaves->count()) { foreach ($oldsaves as $old) { $old->delete(); } } achievement::check_all(user::get()); notes::success('Your page has been saved!'); //site::redirect('write/'.$page->day); } catch (ORM_Validation_Exception $e) { $errors = $e->errors('models'); } } } else { if ($_POST) { notes::error('You must be logged in to save your page. Please log in and submit again.'); } } $this->bind('errors', $errors); $this->bind('page', $page); $this->template->daystamp = $this->request->param('daystamp'); $this->template->page = $page; seo::instance()->title("Write Your Morning Pages"); seo::instance()->description("Morning Pages is about writing three pages of stream of consciousness thought every day. Become a better person by using MorninPages.net"); }
public function require_login($msg = true, $redirect = false) { if ($msg === true) { $msg = 'You must be logged in to see this page'; } if (!user::logged()) { if ($msg) { notes::error($msg); } if ($redirect) { site::redirect($redirect); } else { user::redirect('login'); } } }
public function action_delete() { $content = ORM::factory('Content', $this->request->param('id')); if (!$content->loaded()) { notes::error('Indholdet blev ikke fundet. Er det allerede blevet slettet?'); cms::redirect(''); } try { $type = $content->contenttype_id; $content->delete(); cms::redirect('content/index/' . $type); } catch (HTTP_Exception_Redirect $e) { throw $e; } catch (exception $e) { notes::error('Der opstod en fejl og indholdet kunne ikke slettes: ' . $e->getMessage()); cms::redirect('content/index/' . $content->id); } }
public function action_talk() { $tag = $this->request->param('tag'); $talk = $this->request->param('talk'); if (user::logged()) { // Iterate views if ($talk->user_id != user::get()->id) { $talk->views = $talk->views + 1; try { $talk->save(); } catch (ORM_Validation_Exception $e) { //var_dump($e->errors()); } } // Set when the user last saw the topic $user = user::get(); $viewed = $user->talkviews->where('talk_id', '=', $talk->id)->find(); if (!$viewed->loaded()) { $viewed->user_id = $user->id; $viewed->talk_id = $talk->id; } $viewed->last = time(); $viewed->save(); } $replies = $talk->replies->where('op', '!=', 1); $counter = $talk->replies->where('op', '!=', 1); $limit = Kohana::$config->load('talk')->get('pagination_limit'); $numreplies = $counter->count_all(); $numpages = ceil($numreplies / $limit); $page = (int) arr::get($_GET, 'page', 0); if ($_POST) { $this->require_login(); $reply = ORM::factory('Talkreply'); $reply->values($_POST); $reply->user_id = user::get()->id; $reply->talk_id = $talk->id; try { $reply->save(); $page = $numpages; $talk->last_reply = time(); $talk->save(); $subscriptions = $talk->subscriptions->find_all(); if ((bool) $subscriptions->count()) { foreach ($subscriptions as $subscription) { if ($subscription->user_id != $reply->user_id) { mail::create('talkreplyposted')->to($subscription->user->email)->tokenize(array('username' => $subscription->user->username, 'sendername' => $reply->user->username, 'title' => $talk->title, 'reply' => $reply->content, 'link' => HTML::anchor(URL::site($talk->url() . '?page=' . $page . '#comment-' . $reply->id, 'http'), $talk->title)))->send(); } } } $vote = ORM::factory('User_Talkvote'); $vote->type = 'talkreply'; $vote->user_id = user::get()->id; $vote->object_id = $reply->id; $vote->save(); notes::success('Your reply has been posted.'); site::redirect($talk->url() . '?page=' . $page . '#comment-' . $reply->id); } catch (ORM_Validation_Exception $e) { notes::error('Whoops! Your submission contained errors. Please review it and submit again'); $errors = $e->errors(); } } if ($page < 1) { $page = 1; } if ($page > $numpages) { $page = $numpages; } $replies = $replies->limit($limit); if ($page - 1 > 0) { $replies = $replies->offset($limit * ($page - 1)); } $replies = $replies->find_all(); $this->bind('tag', $tag); $this->bind('talk', $talk); $this->bind('replies', $replies); $this->bind('tags', ORM::factory('Talktag')->find_all()); $this->bind('numpages', $numpages); $this->bind('currentpage', $page); seo::instance()->title($talk->title); seo::instance()->description("Talk About Morning Pages, or anything else you might find interesting. Use this area to ask questions, make friends, or find out information about Morning Pages."); }
public function action_logout() { if (user::logout()) { notes::info('You have been logged out. See ya!'); user::redirect(''); } else { notes::error('An error occured and you could not be logged in.'); user::redirect(''); } }