Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manh #2

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Vagrant::Config.run do |config|
config.vm.box_url = 'http://hglab.realworld.jp/boxes/CentOS-5.7-i386.box'
config.vm.network :hostonly, '33.33.33.99'
config.vm.provision :chef_solo do |chef|
chef.install = false
chef.cookbooks_path = 'cookbooks'
chef.roles_path = "roles"
chef.add_role "vagrant-codeigniter"
Expand Down
Empty file modified codeigniter/application/.htaccess
100644 → 100755
Empty file.
Empty file modified codeigniter/application/cache/.htaccess
100644 → 100755
Empty file.
Empty file modified codeigniter/application/cache/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/config/autoload.php
100644 → 100755
Empty file.
7 changes: 4 additions & 3 deletions codeigniter/application/config/config.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
| MUST set an encryption key. See the user guide for info.
|
*/
$config['encryption_key'] = '';
$config['encryption_key'] = 'realworld';

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -279,7 +279,7 @@
| COOKIE data is encountered
|
*/
$config['global_xss_filtering'] = FALSE;
$config['global_xss_filtering'] = TRUE;

/*
|--------------------------------------------------------------------------
Expand All @@ -293,7 +293,7 @@
| 'csrf_cookie_name' = The cookie name
| 'csrf_expire' = The number in seconds the token should expire.
*/
$config['csrf_protection'] = FALSE;
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
Expand Down Expand Up @@ -357,6 +357,7 @@
*/
$config['proxy_ips'] = '';

$config['javascript_location'] = 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"';

/* End of file config.php */
/* Location: ./application/config/config.php */
2 changes: 2 additions & 0 deletions codeigniter/application/config/constants.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
define('FOPEN_WRITE_CREATE_STRICT', 'xb');
define('FOPEN_READ_WRITE_CREATE_STRICT', 'x+b');

define(TWEET_LIMIT, 10);
define(CACHE_TIME_OUT, 300);

/* End of file constants.php */
/* Location: ./application/config/constants.php */
2 changes: 1 addition & 1 deletion codeigniter/application/config/database.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'codeigniter';
$db['default']['database'] = 'twitter';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
Expand Down
Empty file modified codeigniter/application/config/doctypes.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/config/foreign_chars.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/config/hooks.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/config/index.html
100644 → 100755
Empty file.
7 changes: 7 additions & 0 deletions codeigniter/application/config/memcached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['memcached'] = array(
'hostname' => '127.0.0.1',
'port' => 11211,
'weight' => 1
);
Empty file modified codeigniter/application/config/migration.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/config/mimes.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/config/profiler.php
100644 → 100755
Empty file.
10 changes: 7 additions & 3 deletions codeigniter/application/config/routes.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
Expand Down Expand Up @@ -40,7 +40,11 @@

$route['default_controller'] = "welcome";
$route['404_override'] = '';


$route['twitter/login'] = 'user_controller/login';
$route['twitter/register'] = 'user_controller/register';
$route['twitter/logout'] = 'user_controller/logout';
$route['twitter/homepage'] = 'tweet_controller/homepage';
$route['twitter/tweet/(:num)'] = 'tweet_controller/tweet/$1';
$route['twitter/post'] = 'tweet_controller/post';
/* End of file routes.php */
/* Location: ./application/config/routes.php */
Empty file modified codeigniter/application/config/smileys.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/config/user_agents.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/controllers/index.html
100644 → 100755
Empty file.
70 changes: 70 additions & 0 deletions codeigniter/application/controllers/tweet_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tweet_controller extends CI_CONTROLLER
{
public function __construct()
{
parent::__construct();

$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('encrypt');
$this->load->library('security');
$this->load->library('javascript');
$this->load->library('javascript/jquery', FALSE);
$this->load->library('driver');

$this->load->driver('cache');

$this->load->model('tweet_model');
$this->load->model("user_model");

$this->load->helper(array('form','url', 'email', 'date', 'html'));
if (!$this->_is_login()) {
redirect('twitter/login');
}
}

private function _is_login()
{
return $this->session->userdata('username');
}

// ツイート・リストをもっと表示する
public function tweet($limit)
{
$user_id = $this->session->userdata('id');
$data['tweets'] = $this->tweet_model->get($user_id, $limit);
$this->load->view('tweet', $data);
}

// 新しいツイートを投稿する
public function post()
{
$user_id = $this->session->userdata('id');

$tweet = $this->input->post('tweet');
$tweet_length = strlen($tweet);
if (($tweet_length === 0) || ($tweet_length > 140)) {
return;
}

$this->tweet_model->insert($user_id, $tweet);
$data['tweets'] = $this->tweet_model->get($user_id, TWEET_LIMIT);
}

public function homepage()
{
$data['library_src'] = $this->jquery->script();

$data['name'] = $this->session->userdata('username');
$user_id = $this->session->userdata('id');
$data['user_id'] = $user_id;
$data['tweets'] = $this->tweet_model->get($user_id, TWEET_LIMIT);
$this->form_validation->set_rules('tweet', 'ツイート', 'trim|required|max_lenght[140]|min_length[0]');

if ($this->form_validation->run() === FALSE) {
$this->load->view('mainpage',$data);
}
}
}
115 changes: 115 additions & 0 deletions codeigniter/application/controllers/user_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_controller extends CI_CONTROLLER
{
public function __construct()
{
parent::__construct();

$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('encrypt');
$this->load->library('security');
$this->load->library('javascript');
$this->load->library('javascript/jquery', FALSE);
$this->load->library('input');
$this->load->library('driver');

$this->load->driver('cache');

$this->load->model('user_model');

$this->load->helper(array('form','url', 'email', 'date', 'html'));
}

public function login()
{
$this->form_validation->set_rules('mail', 'メールアドレス', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'パスワード', 'trim|required|min_length[6]');

if ($this->form_validation->run() === FALSE) {
$this->load->view('login');
} else {
$mail = $this->input->post("mail");
$password = $this->input->post("password");
$result = $this->user_model->get($mail, $password);

if ($result !== NULL) {
$sessiondata = array(
'username' => $result->name,
'mail' => $mail,
'id' => $result->user_id
);
$this->session->set_userdata($sessiondata);
$this->user_model->set_status($mail, 'ON');
redirect('twitter/homepage');
}

$this->session->set_flashdata('error_msg', '<div class="text-danger">メールアドレスまたはパスワードが違います!</div>');
redirect('twitter/login');
}
}

public function register()
{
$this->form_validation->set_rules('name', '名前', 'trim|required');
$this->form_validation->set_rules('mail', 'メールアドレス', 'trim|required|valid_email|callback_mail_unique');
$this->form_validation->set_rules('password', 'パスワード', 'trim|required|min_length[6]|user_password_unique');
$this->form_validation->set_rules('confirm_password', '確認パスワード', 'trim|required|min_length[6]|callback_password_confirm');

if ($this->form_validation->run() === FALSE) {
$this->load->view('register');
} else {
$name = $this->input->post("name");
$mail = $this->input->post("mail");
$password = $this->input->post("password");
$password = $this->encrypt->encode($password);

$this->load->model("user_model");
$this->user_model->insert($name, $mail, $password);

$sessiondata = array(
'username' => $name,
'mail' => $mail,
'id' => $result->user_id
);
$this->session->set_userdata($sessiondata);
$this->user_model->set_status($mail, 'ON');
redirect('twitter/homepage');
}
}

// 確認パスワードをチェック
public function password_confirm($confirm_password)
{
$password = $this->input->post("password");

if ($confirm_password === $password) {
return TRUE;
}

$this->form_validation->set_message('password_confirm', '確認パスワードは違います。');
return FALSE;
}

// 登録メールの存在をチェック
public function mail_unique($mail)
{
$is_mail_unique = $this->user_model->is_mail_unique($mail);

if ($is_mail_unique === FALSE) {
$this->form_validation->set_message('mail_unique', 'メールは存在しました!');
}

return $is_mail_unique;
}

public function logout()
{
$mail = $this->session->userdata('mail');
$this->user_model->set_status($mail, 'OFF');
$this->session->sess_destroy();
$this->cache->memcached->clean();
redirect('twitter/login');
}
}
Empty file modified codeigniter/application/controllers/welcome.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/core/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/errors/error_404.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/errors/error_db.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/errors/error_general.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/errors/error_php.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/errors/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/helpers/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/hooks/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/index.html
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions codeigniter/application/js/jquery.js

Large diffs are not rendered by default.

Empty file modified codeigniter/application/language/english/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/calendar_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/date_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/db_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/email_lang.php
100644 → 100755
Empty file.
Empty file.
Empty file modified codeigniter/application/language/japanese/ftp_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/imglib_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/number_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/profiler_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/unit_test_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/language/japanese/upload_lang.php
100644 → 100755
Empty file.
Empty file modified codeigniter/application/libraries/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/logs/index.html
100644 → 100755
Empty file.
Empty file modified codeigniter/application/models/index.html
100644 → 100755
Empty file.
99 changes: 99 additions & 0 deletions codeigniter/application/models/tweet_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tweet_model extends CI_MODEL
{
public function __construct()
{
parent::__construct();

$this->load->library('driver');
$this->load->driver('cache');

$this->load->database();
}

// ツイートを読み出す
public function get($user_id, $limit)
{
$result = array();
$limit = (int) $limit;
$on_cache = $this->cache->memcached->get($limit);

if ($on_cache === FALSE) {
for ($key = TWEET_LIMIT; $key < $limit; $key += TWEET_LIMIT) {
$result = array_merge($result, $this->cache->memcached->get($key));
}
$last_tweets = $this->cache->memcached->get($limit - TWEET_LIMIT);
$last_tweet_id = $last_tweets[count($last_tweets) - 1]['tweet_id'];

$this->db->select('tweet.*, users.name');
$this->db->join('users', 'users.user_id = tweet.user_id');
$this->db->order_by('tweet_id', 'desc');
if ($last_tweet_id !== NULL) {
$this->db->where('tweet_id <', $last_tweet_id);
}
$this->db->where('tweet.user_id =', $user_id);
$add_to_cache = $this->db->get('tweet', TWEET_LIMIT)->result_array();
$this->cache->memcached->save($limit, $add_to_cache, CACHE_TIME_OUT);
$result = array_merge($result, $add_to_cache);
return $result;
}

for ($key = TWEET_LIMIT; $key <= $limit; $key += TWEET_LIMIT) {
$result = array_merge($result, $this->cache->memcached->get($key));
}
$tweet_num = count($result);
if ($tweet_num < $limit) {
$last_tweet_id = $result[$tweet_num - 1]['tweet_id'];
$this->db->select('tweet.*, users.name');
$this->db->join('users', 'users.user_id = tweet.user_id');
$this->db->order_by('tweet_id', 'desc');
$this->db->where('tweet_id <', $last_tweet_id);
$this->db->where('tweet.user_id =', $user_id);
$add_to_cache = $this->db->get('tweet', TWEET_LIMIT)->result_array();
$result = array_merge($result, $add_to_cache);
$add_to_cache = array_merge($this->cache->memcached->get($limit), $add_to_cache);
$this->cache->memcached->save($limit, $add_to_cache, CACHE_TIME_OUT);
}
return $result;
}

// ツイートを入力する
public function insert($user_id, $tweet)
{
$time = date('Y-m-d H:i:s', now());
$data = array(
'tweet_id' => NULL,
'user_id' => $user_id,
'tweet' => $tweet,
'time' => $time,
);
$this->db->insert('tweet', $data);

$this->db->select('tweet.*, users.name');
$this->db->select('users.name');
$this->db->from('users');
$this->db->where('time =', $time);
$temp = $this->db->get('tweet')->result_array();
$key = TWEET_LIMIT;

while (1) {
$cache = $this->cache->memcached->get($key);
if ($cache !== FALSE) {
$cache_length = count($cache);
if($cache_length === 10) {
$cache = array_merge($temp, $cache);
$temp = $cache[count($cache) - 1];
array_pop($cache);
$this->cache->memcached->save($key, $cache, CACHE_TIME_OUT);
$key += TWEET_LIMIT;
continue;
}
$this->cache->memcached->save($key, array_merge(array($temp), $cache), CACHE_TIME_OUT);
break;
}
$this->cache->memcached->save($key, array($temp), CACHE_TIME_OUT);
break;
}
}
}
Loading