Skip to content

Instantly share code, notes, and snippets.

@joelmandell
Created November 2, 2015 21:10
Show Gist options
  • Save joelmandell/0eea0fb35751ce1777b2 to your computer and use it in GitHub Desktop.
Save joelmandell/0eea0fb35751ce1777b2 to your computer and use it in GitHub Desktop.
CodeIgniter - BlogController kodsnutt
<?php
class Blog extends Controller {
function Blog($page=0)
{
parent::Controller();
$this->load->library('session');
$this->load->database();
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->library('auth');
}
function page($page=0)
{
$this->load->model('BlogModel');
$data['text_desc'] = $this->BlogModel->description();
$data['title'] = "Blogg";
if($page==0)
{
$data['text'] = $this->BlogModel->list_latest_items(15);
$data['text_sidebar'] = $this->BlogModel->microblog();
} else {
$data['text'] = $this->BlogModel->list_latest_items(15);
$data['text_sidebar'] = $this->BlogModel->microblog();
}
$this->load->view('mandell_view2', $data);
}
function index($page=0)
{
$this->page($page);
}
function show_item($id=0)
{
if($id==0) Header("Location: ../");
if(is_numeric($id))
{
$this->load->model('BlogModel');
$data['text'] = $this->BlogModel->list_item($id);
$data['text'] .= $this->BlogModel->list_comments($id);
$data['text'] .= $this->BlogModel->write_comment($id);
$data['text_desc'] = strip_tags($this->BlogModel->preamble($id));
$data['title'] = $this->BlogModel->title($id);
$data['text_sidebar'] = $this->BlogModel->generate_links_to_blog(5);
$this->load->view('mandell_view2', $data);
} else {
Header("Location: ../");
}
}
function create_item()
{
if($this->auth->get_session_user_status()=="true")
{
$this->db->query("INSERT INTO blog_items(title,preamble,body,date) VALUES(".$this->db->escape($this->input->post("title")).",".$this->db->escape($this->input->post("preamble")).",".$this->db->escape($this->input->post('body')).",NOW())");
$query=$this->db->query("SELECT * FROM blog_items ORDER BY id DESC LIMIT 1");
$query->row()->id;
redirect("../../blog/show_item/".$query->row()->id."", "refresh");
} else {
die("Hack attempt!");
exit;
}
}
function create_comment($blogid=0)
{
$this->load->model('BlogModel');
if($blogid!=0)
{
$data['text']="";
$this->form_validation->set_error_delimiters('<div class="space">', '</div>');
$this->form_validation->set_rules('nick', 'nick', 'callback_nick_check');
$this->form_validation->set_rules('cap', 'säkerhets kod', 'callback_captcha_check');
$this->form_validation->set_rules('msg', 'meddelande', 'callback_msg_check');
if ($this->form_validation->run() == FALSE)
{
$data['text'] .= validation_errors();
$data['text'] .= $this->BlogModel->write_comment($blogid);
$this->load->view('mandell_view2', $data);
} else {
$query = $this->db->query("INSERT INTO blog_comments(blogid,publisher,text,date) VALUES(".$this->db->escape($blogid).",".$this->db->escape($this->input->post('nick')).",".strip_tags($this->db->escape($this->input->post('msg'))).",NOW())");
redirect("../../blog/show_item/".$blogid."", "refresh");
}
} else {
//FUNCTION ARGUMENT ($blogid) CANNOT BE 0.
}
}
function captcha_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('captcha_check', 'Du glömmde %s.');
return FALSE;
} else {
if(md5($this->input->post('cap'))==$this->session->userdata('security_code'))
{
return TRUE;
} else {
$this->form_validation->set_message('captcha_check', 'Felaktig %s.');
return FALSE;
}
}
}
function msg_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('msg_check', 'Du glömmde %s.');
return FALSE;
} else {
return TRUE;
}
}
function nick_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('nick_check', 'Du glömmde %s.');
return FALSE;
} else {
if($str == 'Joel Mandell')
{
$this->form_validation->set_message('nick_check', 'Detta namnet används av Administratören för denna sidan.');
return FALSE;
} else {
return TRUE;
}
}
}
function visa_kommentarer($blogid=0)
{
if($blogid!=0)
{
$this->load->model('Blog');
$data['text'] = $this->Blog->list_comments($blogid);
$this->load->view('mandell_view2', $data);
} else {
}
}
function admin_functions()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment