/** * Handles database insertion if the user tries to submit a new week. * * @param $db * The database reference. */ function week_post_handler(&$db) { if (isset($_POST)) { return; } global $success_message; global $error_message; global $WEEK_TYPE_DICT; if (!check_post_values_set(array('week_number', 'week_type', 'week_season', 'week_start_date', 'week_end_date'))) { $error_message = "Missing expected POST week values."; return; } if (!is_valid_number($_POST['week_number'])) { $error_message = "Illegal week number detected."; return; } $week_number = intval($_POST['week_number']); if (!is_valid_number($_POST['week_season'])) { $error_message = "Season is invalid."; return; } $week_season_id = intval($_POST['week_season']); if (!is_valid_number($_POST['week_type'])) { $error_message = "Unexpected week type."; return; } $week_type = intval($_POST['week_type']); if ($week_type < 0 || $week_type >= count($WEEK_TYPE_DICT)) { $error_message = "Unexpected week type number enumeration (outside range)."; return; } if (!is_valid_date($_POST['week_start_date'])) { $error_message = "Invalid start date, is it in YYYY-MM-DD form?"; return; } $start_date = $_POST['week_start_date']; if (!is_valid_date($_POST['week_end_date'])) { $error_message = "Invalid end date, is it in YYYY-MM-DD form?"; return; } $end_date = $_POST['week_end_date']; if ($start_date > $end_date) { $error_message = "Start date is after the end date."; return; } if (!week_in_season_range($start_date, $end_date, $db)) { $error_message = "This week is not in any season date range. Cannot figure out what season it belongs to."; return; } try { $stmt = $db->prepare('INSERT INTO weeks(fk_season_id, number, type, start_date, end_date) VALUES(:sid, :wnum, :wtype, :sdate, :edate)'); $stmt->execute(array("sid" => $week_season_id, "wnum" => $week_number, "wtype" => $week_type, "sdate" => $start_date, "edate" => $end_date)); $success_message = "Successful week addition."; } catch (PDOException $e) { $error_message = "Error adding week: " . $e->getMessage(); } }
/** * The main page of the blog, show the most recent blog psots */ public function index() { if ($this->session->userdata('confirmation')) { $this->session->unset_userdata('confirmation'); } $posts = $this->post_model->get_posts(); if ($posts['count'] == 0) { // if there are no posts we don't want to load the regular posts view file or we'll get an error $data['view_file'] = 'posts/no-posts'; } else { $data['posts'] = $posts['list']; // ------------------------------------------------------------------------ // Pagination // ------------------------------------------------------------------------ // config for the pagination of the content (posts) $data['posts_per_page'] = 3; $offset = $this->uri->segment(3); $data['offset'] = (bool) $offset === FALSE ? '' : $offset; // If the offset is invalid or NULL (in which case the user goes back to the first page anyway) // the user is sent back to the first page and a feedback message is displayed if ((!is_valid_number($data['offset']) || !array_key_exists($data['offset'], $posts['list'])) && !empty($data['offset'])) { $this->session->set_flashdata('notice', 'Invalid Request'); redirect('posts/index/0'); } $this->load->library('pagination'); $config['base_url'] = site_url('/posts/index'); $config['total_rows'] = $posts['count']; $config['per_page'] = $data['posts_per_page']; $config['num_links'] = 10; $config['uri_segment'] = 3; $config['full_tag_open'] = '<div class="pagination-links">'; $config['full_tag_close'] = '</div>'; $this->pagination->initialize($config); $data['pagination_links'] = $this->pagination->create_links(); // Dynamically generate the posts pagination everytime the user clicks on a pagination link $data['posts'] = paginate($posts['list'], $posts['count'], $data['posts_per_page'], $data['offset']); // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // Breadcrumbs // ------------------------------------------------------------------------ // the page number segment of the breadcrumbs will only appear if there is at least two pages if ($posts['count'] > $config['per_page']) { $_seg_title = 'page ' . get_page_number($data['offset'], $data['posts_per_page']); $_seg_url = 'posts/index/' . $data['offset']; $breadcrumbs = $this->azbraz->new_segment($_seg_title, $_seg_url); } else { $breadcrumbs = ''; } $data['breadcrumbs'] = $this->azbraz->generate($breadcrumbs); // ------------------------------------------------------------------------ $data['view_file'] = 'posts/index'; } $this->load->view($this->main_view, $data); }
/** * Get one user from the database * * @access public * @param int $user_id * @return object **/ public function get_user($user_id = NULL) { if (empty($user_id) || !is_valid_number($user_id)) { return NULL; } $this->db->select('users.id, users.username, users.email'); $query = $this->db->get_where('users', array('id' => $user_id), 1); if ($query->num_rows == 1) { return $query->row(); } return NULL; }
/** * Handles database insertion if the user tries to submit a new map. * * @param $db * The database reference. */ function map_post_handler(&$db) { if (isset($_POST)) { return; } global $success_message; global $error_message; if (!check_post_values_set(array('map_file_id', 'map_pack', 'map_name', 'map_number'))) { $error_message = "Missing expected POST map values."; return; } if (!is_valid_number($_POST['map_file_id'])) { $error_message = "Illegal file ID number detected."; return; } $fk_file_id = intval($_POST['map_file_id']); if (!is_valid_number($_POST['map_number'])) { $error_message = "Illegal map number detected."; return; } $map_number = intval($_POST['map_number']); if ($map_number < 0) { $error_message = "Cannot have a negative map number."; return; } if (!preg_match('/[-a-zA-Z0-9_.! ]+/', $_POST['map_name'])) { $error_message = "Map name must only contain letters, numbers, spaces, or any of: ,.!_-"; return; } if ($_POST['map_name'] >= MAX_MAP_NAME_LENGTH) { $error_message = "Map name too long (must be less than " . MAX_MAP_NAME_LENGTH . " characters)."; return; } $map_name = $_POST['map_name']; if (!preg_match('/[-a-zA-Z0-9_.! ]+/', $_POST['map_pack'])) { $error_message = "Map pack must only contain letters, numbers, spaces, or any of: ,.!_-"; return; } if ($_POST['map_pack'] >= MAX_MAP_NAME_LENGTH) { $error_message = "Map pack too long (must be less than " . MAX_MAP_PACK_LENGTH . " characters)."; return; } $map_pack = $_POST['map_pack']; try { $stmt = $db->prepare('INSERT INTO maps(fk_file_image_id, pack, name, number) VALUES(:fkfile, :pack, :name, :num)'); $stmt->execute(array("fkfile" => $fk_file_id, "pack" => $map_pack, "name" => $map_name, "num" => $map_number)); $success_message = "Successful map addition."; } catch (PDOException $e) { $error_message = "Error adding map: " . $e->getMessage(); } }
/** * Confirm a critical change. This can be the deletion of a user for instance. * * @param string $action * @param int $user_id */ public function confirm($action = NULL, $user_id = NULL) { // Check if the user is valid if (empty($action) || empty($user_id) || !is_valid_number($user_id) || !is_valid_action($action) || $this->user_model->get_user($user_id) === NULL) { $this->session->set_flashdata('notice', 'Invalid Request'); redirect('admin/users/index'); } $data['question'] = 'Are you sure you want to delete the following user?'; $data['user'] = $this->user_model->get_user($user_id); $data['action'] = $action; $this->session->set_userdata(array('confirmation' => TRUE)); // ------------------------------------------------------------------------ // Breadcrums // ------------------------------------------------------------------------ $_seg_title = ucfirst($action) . ' User#' . $user_id . ': Confirmation'; $_seg_url = 'admin/users/confirm/' . $action . '/' . $user_id; $breadcrumbs = $this->azbraz->new_segment($_seg_title, $_seg_url); $data['breadcrumbs'] = $this->azbraz->generate(); // ------------------------------------------------------------------------ $data['view_file'] = 'admin/users/confirm'; $this->load->view($this->main_admin_view, $data); }
/** * Confirm a critical change. The can be the deletion of a post for instance. * * @param string $action : the "critical change" * @param int $post_id */ public function confirm($action = NULL, $post_id = NULL) { // Check if the post id is valid if (empty($action) || empty($post_id) || !is_valid_number($post_id) || !is_valid_action($action) || $this->post_model->get_post($post_id, 'all') === NULL) { $this->session->set_flashdata('notice', 'Invalid Request'); redirect('admin/posts'); } $data['question'] = 'Are you sure you want to ' . $action . ' the following post?'; $data['post'] = $this->post_model->get_post($post_id, 'all'); $data['action'] = $action; $this->session->set_userdata(array('confirmation' => TRUE)); // ------------------------------------------------------------------------ // Breadcrumbs // ------------------------------------------------------------------------ $_seg_title = ucfirst($action) . ' Post#' . $post_id . ': Confirmation'; $_seg_url = 'admin/posts/confirm/' . $action . '/' . $post_id; $breadcrumbs = $this->azbraz->new_segment($_seg_title, $_seg_url); $data['breadcrumbs'] = $this->azbraz->generate($breadcrumbs); // ------------------------------------------------------------------------ $data['view_file'] = 'admin/posts/confirm'; $this->load->view($this->main_admin_view, $data); }
</div> <div> <?php echo form_label('Website', 'website'); ?> <?php echo form_input('website', set_value('website')); ?> </div> <div> <?php echo form_textarea('body', ''); ?> </div> <?php // if the user tries to reply to another comment we need to store // the id of the parent comment in a hidden field if ($this->uri->segment(5) !== FALSE && is_valid_number($this->uri->segment(5))) { echo form_hidden('reply_to', $this->uri->segment(5)); } ?> <p><?php echo form_submit('submit', 'Submit'); ?> </p> </fieldset> </form>
$action = $_GET['action']; } else { $action = 'view'; } switch ($action) { case 'view': $cart = cart_get_items(); break; case 'add': $product_id = $_GET['product_id']; $quantity = $_GET['quantity']; $product = get_product($product_id); // validate the quantity entry if (empty($quantity)) { display_error('You must enter a quantity.'); } elseif (!is_valid_number($quantity, 1)) { display_error('Quantity must be 1 or more.'); } cart_add_item($product_id, $quantity); $cart = cart_get_items(); break; case 'update': $items = $_POST['items']; foreach ($items as $product_id => $quantity) { if ($quantity == 0) { cart_remove_item($product_id); } else { cart_update_item($product_id, $quantity); } } $cart = cart_get_items();
function is_number_negative() { $params = func_get_args(); $count = func_num_args(); $value = null; if (_get_value($params, $count, $value)) { if (is_valid_number($value) && $value < 0) { return true; } } return false; }
/** * Adds a new custom segment at the end of the breadcrumbs array * * @access public * @param string $title : the title of the segment * @param string $url : the url of the segment the segment is referring to * @param array $breadcrumbs : the breadcrumbs array. If set to NULL this method will just return one segment array without merging it to anything * @return array : the new breadcrumbs array with the custom segment at the end or just the segment array */ public function new_segment($title, $url, $breadcrumbs = array()) { if (!is_valid_string($title) && !is_valid_ci_url($url) && !is_valid_slug($url) && !is_valid_number($url) && !is_array($breadcrumbs) && !empty($breadcrumbs)) { if (!empty($breadcrumbs)) { return FALSE; } return $breadcrumbs; } if (!empty($breadcrumbs) && !is_valid_ci_url($url)) { $last = $breadcrumbs[count($breadcrumbs) - 1]['url']; $url = strip_trailing_slash($last) . '/' . $url; } $segment = array('title' => $title, 'url' => $url); $breadcrumbs[] = $segment; return $breadcrumbs; }
/** * Get the minimum info about one comment from the database * Intended to be used with the submit_ham and submit_spam methods * * @todo - rename that f*****g method, its name sucks * @access private * @param int $comment_id * @return array **/ private function _get_minimum_comment_data($comment_id) { if (!is_valid_number($comment_id)) { return NULL; } $this->db->select('author_name, author_email, author_website, body'); $query = $this->db->get_where('comments', array('id' => $comment_id), 1); if ($query->num_rows() != 1) { return NULL; } return $query->row_array(); }
/** * Create a temporary array of items out of a bigger array * so that the correct items are displayed on each page * * @param array $items * @param int $number_of_items_per_page * @param string $offset * @return array **/ public function paginate($items, $number_of_items_per_page, $offset) { if (!is_valid_number($offset)) { if (empty($offset)) { $first_item_to_display = 0; } else { return NULL; } } else { $first_item_to_display = $offset; } // if items is empty, array_slice will return an error, so we will just return the unchanged items variable in that case. if (empty($items) || !isset($items)) { return $items; } $page_items = array_slice($items, $first_item_to_display, $number_of_items_per_page); return $page_items; }
public function confirm($action = NULL, $comment_id = NULL) { if (empty($action) || empty($comment_id) || !is_valid_number($comment_id) || !is_valid_action($action) || $this->comment_model->get_comment($comment_id) === NULL) { $this->session->set_flashdata('notice', 'Invalid Request'); redirect('admin/comments'); } $data['view_file'] = 'admin/comments/confirm'; $data['question'] = 'Are you sure you want to delete the following comment?'; $data['comment'] = $this->comment_model->get_comment($comment_id); $this->load->view('admin/main', $data); }
/** * Gets the page number the user is on * * @todo - extend CI's Pagination class * @param int $offset * @param int $number_of_posts_per_page * @return int **/ function get_page_number($offset, $number_of_posts_per_page) { if (!is_valid_number($offset)) { if (empty($offset)) { return 1; } else { return NULL; } } else { return $offset / $number_of_posts_per_page + 1; } }
public function toggle_status($post_id, $status) { $this->firephp->fb($post_id); // DEBUG <- if (!is_valid_number($post_id)) { log_message('error', 'post_model.php:145 : The post id you passed is not a valid number!'); return FALSE; } if ($status === 'active') { $new_status = array('active' => 0); } elseif ($status === 'inactive') { $new_status = array('active' => 1); } $this->db->where('id', (int) $post_id); $this->db->update('posts', $new_status); return TRUE; }