public function post() { if ($this->user->isUnVerified()) { $this->alert('Your account is not active now, plz check your mail and active.'); } if (!($article = Model::factory('Article')->find_one($this->input->param('id')))) { $this->alert('Article is not exists'); } $comment = Model::factory('Comment')->create(array('article_id' => $this->input->param('id'), 'text' => $this->input->data('text'), 'user_id' => $this->user->id)); try { ORM::get_db()->beginTransaction(); if (!$comment->save()) { $this->alert('Comment create error'); } $article->set_expr('comments_count', '`comments_count` + 1'); $article->save(); ORM::get_db()->commit(); } catch (\PDOException $e) { ORM::get_db()->rollBack(); // @TODO Logging $this->alert('Comment error because of the bad database'); } $this->app->emit('comment', $comment); $this->redirect('/p/' . $comment->article_id); }
public function post() { if (!$this->user->isOK()) { $this->alert('Your account is not ok, plz check active or contact the admin.'); } if (!$this->input->data('link') && !$this->input->data('content')) { $this->alert('At least one between url and content'); } // Check link format if ($this->input->data('link') && !filter_var($this->input->data('link'), FILTER_VALIDATE_URL)) { $this->alert('Url format is wrong'); } // Check url if exists if ($this->input->data('link') && Model::factory('Article')->where('link', $this->input->data('link'))->find_one()) { $this->alert('Url "' . $this->input->data('link') . '" is already exists'); } // Check title if (!$this->input->data('title')) { $this->alert('Title need'); } $article = Model::factory('Article')->create(array('title' => $this->input->data('title'), 'link' => $this->input->data('link'), 'content' => $this->input->data('content'), 'user_id' => $this->user->id, 'status' => Article::OK)); try { ORM::get_db()->beginTransaction(); if (!$article->save()) { $this->alert('Submit error'); } $this->user->set_expr('posts_count', '`posts_count` + 1'); $this->user->save(); ORM::get_db()->commit(); } catch (\PDOException $e) { ORM::get_db()->rollback(); $this->alert('Share error with error database'); } $this->redirect('/p/' . $article->id); }
public function get() { if (($page = $this->input->query('page', 1)) < 1) { $page = 1; } $limit = 20; $offset = ($page - 1) * $limit; $total = Model::factory('Article')->where('status', '1')->count(); $this->data['articles'] = Model::factory('Article')->where('status', '1')->order_by_desc('point')->offset($offset)->limit($limit)->find_many(); $this->data['latest_articles'] = array(); /** * 第一页加载最新的几篇文章 */ if ($page == 1) { $this->data['latest_articles'] = Model::factory('Article')->where('status', '1')->order_by_desc('created_at')->limit(5)->find_many(); if ($this->data['latest_articles'] && !$this->input->query('force_latest')) { $exists_articles_ids = array(); foreach ($this->data['articles'] as $article) { $exists_articles_ids[] = $article->id; } $this->data['latest_articles'] = array_filter($this->data['latest_articles'], function ($article) use($exists_articles_ids) { if (in_array($article->id, $exists_articles_ids)) { return false; } return true; }); } } $this->data['page'] = Html::makePage($this->input->uri(), 'page=(:num)', $page, $total, $limit); }
public function get() { $posts = Model::factory('Article')->where('status', '1')->order_by_desc('point')->limit(100)->find_many(); $xml = new \XMLWriter(); $xml->openMemory(); $xml->startDocument(); $xml->startElement('urlset'); foreach ($posts as $post) { $xml->startElement('url'); $xml->startElement('loc'); $xml->writeCdata($post->permalink()); $xml->endElement(); $xml->startElement('lastmod'); $xml->writeCdata(date(DATE_ATOM, strtotime($post->modified_at ? $post->modified_at : $post->created_at))); $xml->endElement(); $xml->startElement('changefreq'); $xml->writeCdata('always'); $xml->endElement(); $xml->startElement('priority'); $xml->writeCdata('1.0'); $xml->endElement(); $xml->endElement(); } $xml->endElement(); $this->data = $xml->outputMemory(); }
public function get() { $this->tpl = 'user.php'; $this->data['author'] = Model::factory('User')->find_one($this->input->params['id']); if (!$this->data['author']) { $this->alert('User is not exists'); } $this->title = $this->data['author']->name; }
public function run() { $articles = Model::factory('Article')->find_many(); foreach ($articles as $article) { $article->comments_count = $article->comments()->count(); $article->digg_count = Model::factory('UserDigg')->where('article_id', $article->id)->count(); $article->save(); } print Cli::text('Update article count ok' . PHP_EOL, 'green'); }
public function get() { if (($page = $this->input->query('page', 1)) < 1) { $page = 1; } $limit = 20; $offset = ($page - 1) * $limit; $total = Model::factory('Article')->where('status', '1')->count(); $this->data['articles'] = Model::factory('Article')->where('status', '1')->order_by_desc('created_at')->offset($offset)->limit($limit)->find_many(); $this->data['page'] = Html::makePage($this->input->uri(), 'page=(:num)', $page, $total, $limit); }
/** * Before logic */ protected function before() { $this->loadOrm(); $user_id = $this->input->session('login'); if ($user_id) { $this->user = Model::factory('User')->find_one($user_id); } if ($this->auth) { $this->auth(); } }
public function post() { if (!($article = Model::factory('Article')->find_one($this->input->params['id']))) { $this->alert('Article is not exists'); } if (!$this->user->isAdmin() && $article->user_id != $this->user->id) { $this->alert('Article only can be deleted by owner'); } $article->status = Article::DELETED; $article->save(); $this->redirect('/my/posts'); }
public function post() { $is_verify_user = $this->app->verify_user; // Check email if (!filter_var($this->input->data('email'), FILTER_VALIDATE_EMAIL)) { $this->alert('Email format is wrong'); } // Check user name rule if (!preg_match("/^[\\w]{4,20}\$/", $this->input->data('name'))) { $this->alert('User name only use a-z and 0-9, length must be 6-20'); } // Check password length if (strlen($this->input->data('password')) < 6) { $this->alert('Password length must be great than or equal 6'); } // Check if exists user name if (Model::factory('User')->where('name', $this->input->data('name'))->find_one()) { $this->alert('User already exists'); } // Check if exists user email if (Model::factory('User')->where('email', $this->input->data('email'))->find_one()) { $this->alert('Email already taken'); } // Create user /** @var $user \Model\User */ $user = Model::factory('User')->create(array('name' => $this->input->data('name'), 'password' => Crypt::makePassword($this->input->data('password'), $this->app->password_salt), 'email' => $this->input->data('email'), 'bio' => $this->input->data('bio'))); // If disable verify_user will set user verified automatic. if (!$is_verify_user) { $user->setVerified(); } try { ORM::get_db()->beginTransaction(); if (!$user->save()) { $this->alert('User create error'); } ORM::get_db()->commit(); } catch (\PDOException $e) { $this->alert('User register error because of the bad database'); //@TODO log ORM::get_db()->rollback(); } // login when success $this->input->session('login', $user->id); // Check if verify user if ($is_verify_user) { // Send verify email SendVerifyEmail::perform($user); $this->redirect('/account/welcome'); } else { $this->redirect('/'); } }
public function get() { $posts = Model::factory('Article')->where('status', '1')->order_by_desc('point')->limit(10)->find_many(); $feed = new FeedFactory(); $channel = new Channel(); $channel->title(config('site.title'))->description(config('site.default_meta'))->url(Url::site())->appendTo($feed); foreach ($posts as $post) { $item = new Item(); /** @var $post \Model\Article */ $item->title($post->title)->description(Html::fromMarkdown($post->content))->url($post->permalink())->pubDate(strtotime($post->created_at))->appendTo($channel); } $this->data = substr($feed, 0, -1); }
public function run() { $users = Model::factory('User')->find_many(); foreach ($users as $user) { $user->posts_count = $user->articles()->where('status', Article::OK)->count(); $user->digged_count = 0; foreach ($user->articles()->where('status', Article::OK)->find_many() as $article) { $user->digged_count += $article->digg_count; } $user->save(); } print Cli::text('Update user count ok' . PHP_EOL, 'green'); }
public function post() { if (!isset($this->input->auth)) { $this->alert('Authorize failed!'); } $auth = $this->input->auth['auth']; try { if ($passport = Model::factory('Passport')->where('provider', $auth['provider'])->where('uid', $auth['uid'])->find_one()) { $passport->access_token = $auth['credentials']['token']; $passport->expired_at = !empty($auth['credentials']['expires']) ? $auth['credentials']['expires'] : null; $passport->save(); } else { // 检查当前是否有登陆用户 if (!($user_id = $this->input->session('login'))) { // 没有用户创建一个 $user = Model::factory('User')->create(); $name = preg_replace('/\\s/', '', $auth['info']['name']); if (Model::factory('User')->where('name', $name)->find_one()) { $name .= rand(100, 999); } $user->name = $name; if (isset($auth['info']['description']) && ($bio = $auth['info']['description']) || isset($auth['raw']['description']) && ($bio = $auth['raw']['description'])) { $user->bio = $bio; } if (isset($auth['info']['email']) && ($email = $auth['info']['email'])) { $user->email = $email; } $user->status = User::OK; $user->save(); $user_id = $user->id; } // 绑定Passport $passport = Model::factory('Passport')->create(); $passport->provider = $auth['provider']; $passport->uid = $auth['uid']; $passport->display_name = isset($auth['info']['nickname']) ? $auth['info']['nickname'] : $auth['info']['name']; $passport->access_token = $auth['credentials']['token']; $passport->expired_at = !empty($auth['credentials']['expires']) ? $auth['credentials']['expires'] : null; $passport->user_id = $user_id; $passport->save(); if (!empty($user)) { $user->create_passport_id = $passport->id; $user->save(); } } } catch (\Exception $e) { $this->alert("Create user error: " . $e->getMessage()); } $this->input->session('login', $passport->user_id); $this->redirect('/'); }
public function get() { $this->tpl = 'account/verify.php'; $email = $this->app->cryptor->decrypt($this->input->query('code')); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->alert('Verify data is error'); } $user = Model::factory('User')->where('email', $email)->find_one(); if (!$user) { $this->alert('Verify email is not exists'); } if ($user->isUnVerified()) { $user->status = User::OK; $user->save(); } }
public function run() { $model = Model::factory('Article'); if (!$this->params['all']) { $model->where_gte('created_at', date('Y-m-d', strtotime('-1 month'))); } /** @var $articles \Model\Article[] */ $articles = $model->find_many(); foreach ($articles as $article) { $article->digg_count = $article->diggs()->count(); $article->comments_count = $article->comments()->count(); $article->point = $article->analysisPoint(); $article->save(); } print Cli::text('Analysis article point ok' . PHP_EOL, 'green'); }
public function post() { if (filter_var($this->input->data('name'), FILTER_VALIDATE_EMAIL)) { if (!($user = Model::factory('User')->where('email', $this->input->data('name'))->find_one())) { $this->alert('User email not found'); } } else { if (!($user = Model::factory('User')->where('name', $this->input->data('name'))->find_one())) { $this->alert('User name not found'); } } if ($user->password != Crypt::makePassword($this->input->data('password'), $this->app->password_salt)) { $this->alert('User name and password is not match'); } $this->input->session('login', $user->id); $this->redirect($this->input->query('continue') ? $this->input->query('continue') : '/'); }
public function get() { $this->tpl = 'article.php'; $this->data['article'] = Model::factory('Article')->find_one($this->input->params['id']); if (!$this->data['article']) { $this->alert('Article is not exists'); } // Un-normal Article is only display for his author if (!$this->data['article']->isOK() && (!$this->user || $this->data['article']->user_id != $this->user->id)) { $this->alert('Article is not ready'); } if ($this->is_robot) { $this->data['comments'] = $this->data['article']->comments()->order_by_desc('created_at')->find_many(); } $this->data['json']['pid'] = $this->input->params['id']; $this->title = $this->data['article']->title; }
public function post(Input $req) { // Check email if (!filter_var($req->data('email'), FILTER_VALIDATE_EMAIL)) { $this->alert('Email format is wrong'); } if ($password = $req->data('password')) { // Check password length if (strlen($req->data('password')) < 6) { $this->alert('Password length must be great than or equal 6'); } if ($req->data('password') != $req->data('re_password')) { $this->alert('Password dose not match'); } } $this->user->set('bio', $req->data('bio')); // Change password if ($password) { $this->user->password = Crypt::makePassword($password, $this->app->password_salt); } $send = false; // Change email if ($this->user->email != $req->data('email')) { // Check if exists user email if (Model::factory('User')->where('email', $req->data('email'))->find_one()) { $this->alert('Email already taken'); } $this->user->email = $req->data('email'); $this->user->status = User::UNVERIFIED; $send = true; } try { if (!$this->user->save()) { $this->alert('User create error'); } } catch (\PDOException $e) { $this->alert('User register error because of the bad database'); } if ($send) { // Send verify email SendVerifyEmail::perform($this->user); } $this->redirect('/u/' . $this->user->id); }
public function get() { if (($page = $this->input->query('page', 1)) < 1) { $page = 1; } $limit = 20; $offset = ($page - 1) * $limit; $user = $this->user; if ($this->params && $this->params['id']) { $user = Model::factory('User')->find_one($this->params['id']); if (!$user) { $this->alert("User is not exists"); } $this->title = $user->name . "'s comments"; } $total = $user->comments()->count(); $this->data['comments'] = $user->comments()->offset($offset)->limit($limit)->order_by_desc('created_at')->find_many(); $this->data['page'] = Html::makePage($this->input->uri(), 'page=(:num)', $page, $total, $limit); }
/** * Before logic */ protected function before() { $this->loadOrm(); $user_id = $this->input->session('login'); if ($user_id && ($this->user = Model::factory('User')->find_one($user_id))) { $this->output->cookie('nick', $this->user->name); } if ($this->auth) { $this->auth(); } // 检查搜索爬虫 if (preg_match('/bot|spider/i', $this->input->userAgent())) { $this->is_robot = true; } $this->data['user'] = $this->user; $this->data['id'] = strtolower(substr(get_called_class(), strlen('Route\\Web') + 1)); $this->data['title'] =& $this->title; $this->data['json'] = array('robot' => $this->is_robot, 'site_prefix' => Url::to('', null, true)); }
public function get() { switch ($this->input->param('type')) { case 'index': $this->data = Model::factory('Article')->where('status', '1')->order_by_desc('point')->limit(10)->find_array(); break; case 'latest': $this->data = Model::factory('Article')->where('status', '1')->order_by_desc('created_at')->limit(10)->find_array(); break; default: $this->error('Un-support alfred api "' . $this->input->param('type') . '"'); } // Map site links $this->data = array_map(function ($item) { if (!$item['link']) { $item['link'] = 'http://inews.io/p/' . $item['id']; } return $item; }, $this->data); }
public function post() { $this->tpl = 'submit.php'; if (!($article = Model::factory('Article')->find_one($this->input->params['id']))) { return $this->alert('Article is not exists'); } if (!$this->user->isAdmin() && $article->user_id != $this->user->id) { return $article->alert('Article only can be edit by owner'); } // not url or content $input = $this->input; if (!($input->data('title') && ($input->data('link') || $input->data('content')))) { return $this->redirect('/p/' . $article->id . '/edit'); } $article->title = $input->data('title'); $article->link = $input->data('link'); $article->content = $input->data('content'); $article->save(); $this->redirect('/p/' . $article->id); }
public function get() { if (!$this->user || !$this->user->isAdmin()) { $this->alert('Only admin can manage user'); } $user = Model::factory('User')->find_one($this->params['id']); if (!$user) { $this->alert('User is not exists'); } switch ($this->params['action']) { case 'suspend': $user->status = -1; $user->save(); break; case 'active': $user->status = 1; $user->save(); break; } $this->redirect('/u/' . $user->id); }
public function get() { if (!($article = Model::factory('Article')->find_one($this->input->params['id']))) { $this->alert('Article is not exists'); } if (Model::factory('UserDigg')->where('user_id', $this->user->id)->where('article_id', $this->input->params['id'])->find_one()) { $this->alert('You already digg this article'); } try { ORM::get_db()->beginTransaction(); if (!Model::factory('UserDigg')->create(array('user_id' => $this->user->id, 'article_id' => $article->id))->save()) { $this->alert('Digg create error'); } $article->set_expr('digg_count', '`digg_count` + 1'); $article->save(); ORM::get_db()->commit(); } catch (\PDOException $e) { ORM::get_db()->rollBack(); // @TODO Logging $this->alert('Digg error because of the bad database'); } $this->redirect($this->input->refer() ? $this->input->refer() : '/p/' . $this->input->param('id')); }
private function cancel($article) { if (!($user_digg = Model::factory('UserDigg')->where('user_id', $this->user->id)->where('article_id', $this->input->data('article_id'))->find_one())) { $this->error('You have not digg this article'); } try { ORM::get_db()->beginTransaction(); if (!$user_digg->delete()) { $this->error('Digg cancel error'); } $article->set_expr('digg_count', '`digg_count` - 1'); $article->save(); ORM::get_db()->exec("UPDATE `user` SET `digged_count` = `digged_count` - 1 WHERE `id` = '" . $article->user_id . "'"); ORM::get_db()->commit(); $article = $this->loadArticle(); $this->data['digg_count'] = $article->digg_count; } catch (\PDOException $e) { ORM::get_db()->rollBack(); // @TODO Logging $this->error('Digg cancel error because of the bad database'); } $this->ok('Digg canceled'); }
public function run() { $post = Model::factory('Article')->find_one($this->params['id']); if (!$post) { echo Cli::text('Article is not exists' . PHP_EOL, 'red'); } echo 'Article is ' . PHP_EOL . PHP_EOL . "\t" . Cli::text($post->title, 'green') . PHP_EOL . PHP_EOL; switch ($this->input->param('action')) { case 'delete': if (!Cli::confirm('Do you want to delete')) { return; } $post->status = Article::DELETED; $post->save(); break; case 'revert': $post->status = Article::OK; $post->save(); break; default: echo Cli::text('Error action!', 'red') . PHP_EOL; } echo 'Action done!' . PHP_EOL; }
public function delete() { $this->auth(); $comment = Model::factory('Comment')->find_one($this->params['id']); if (!$comment) { $this->error('Comment not exists'); } $article = Model::factory('Article')->find_one($comment->article_id); try { ORM::get_db()->beginTransaction(); if (!$comment->delete()) { $this->error('Comment delete fail'); } if ($article) { $article->set_expr('comments_count', '`comments_count` + 1'); $article->save(); } ORM::get_db()->commit(); } catch (\PDOException $e) { ORM::get_db()->rollBack(); $this->error('Comment delete error'); } $this->ok('Comment delete ok'); }
public function get() { $this->data['leaders'] = Model::factory('User')->where('status', '1')->order_by_desc('digged_count')->limit(18)->find_many(); }