Plugin Directory

Changeset 3406150


Ignore:
Timestamp:
11/30/2025 12:11:33 PM (3 months ago)
Author:
owthub
Message:

Security Issue Fixed -> Changes in Public Request

File:
1 edited

Legend:

Unmodified
Added
Removed
  • library-management-system/trunk/public/class-library-management-system-public.php

    r3384422 r3406150  
    8888
    8989    // All books Shortcode Handler
    90     public function owt7_library_all_books_shortcode($template){
    91 
     90    public function owt7_library_all_books_shortcode($template) {
    9291        global $wpdb;
    9392
    94         $book_id = isset( $_REQUEST['bid'] ) ? trim( $_REQUEST['bid'] ) : "";
     93        $raw_bid = isset( $_REQUEST['bid'] ) ? trim( wp_unslash( $_REQUEST['bid'] ) ) : '';
     94        $book_id = 0;
    9595
    96         $book_id = base64_decode($book_id);
     96        if ( $raw_bid !== '' ) {
    9797
    98         if($book_id && intval($book_id) > 0){
    99             // Single book data
    100             $book = $wpdb->get_row(
    101                 "SELECT book.*, (SELECT category.name FROM ".$this->table_activator->owt7_library_tbl_category()." as category WHERE category.id = book.category_id LIMIT 1) as category_name, (SELECT bkcase.name FROM ".$this->table_activator->owt7_library_tbl_bookcase()." as bkcase WHERE bkcase.id = book.bookcase_id LIMIT 1) as bookcase_name, (SELECT section.name FROM ".$this->table_activator->owt7_library_tbl_bookcase_sections()." as section WHERE section.id = book.bookcase_section_id LIMIT 1) as section_name from " . $this->table_activator->owt7_library_tbl_books(). " as book WHERE book.id = {$book_id}"
    102             );
     98            $decoded = base64_decode( $raw_bid, true );
     99            if ( $decoded !== false && $decoded !== '' ) {
     100                $decoded = trim( $decoded );
     101                if ( ctype_digit( $decoded ) ) {
     102                    $book_id = intval( $decoded );
     103                } else {
     104                    $book_id = 0;
     105                }
     106            }
     107        }
    103108
    104             if(!empty($book)){
    105                 return $this->owt7_library_include_template_file("owt7_library_single_book", compact("book_id", "book"));
    106             }else{
    107                 return $this->owt7_library_include_template_file("errors/owt7_library_404_page");
     109        if ( $book_id > 0 ) {
     110            $sql = "
     111                SELECT book.*,
     112                    (SELECT category.name FROM " . $this->table_activator->owt7_library_tbl_category() . " AS category WHERE category.id = book.category_id LIMIT 1) AS category_name,
     113                    (SELECT bkcase.name FROM " . $this->table_activator->owt7_library_tbl_bookcase() . " AS bkcase WHERE bkcase.id = book.bookcase_id LIMIT 1) AS bookcase_name,
     114                    (SELECT section.name FROM " . $this->table_activator->owt7_library_tbl_bookcase_sections() . " AS section WHERE section.id = book.bookcase_section_id LIMIT 1) AS section_name
     115                FROM " . $this->table_activator->owt7_library_tbl_books() . " AS book
     116                WHERE book.id = %d
     117                LIMIT 1
     118            ";
     119
     120            $prepared = $wpdb->prepare( $sql, $book_id );
     121            $book = $wpdb->get_row( $prepared );
     122
     123            if ( ! empty( $book ) ) {
     124                return $this->owt7_library_include_template_file( "owt7_library_single_book", compact( "book_id", "book" ) );
     125            } else {
     126                return $this->owt7_library_include_template_file( "errors/owt7_library_404_page" );
    108127            }
    109         }else{
    110              
    111             $books_per_page = OWT7_LMS_DEFAULT_SHOW_BOOKS;
     128        } else {
     129            $books_per_page = (int) OWT7_LMS_DEFAULT_SHOW_BOOKS;
     130            if ( $books_per_page <= 0 ) {
     131                $books_per_page = 10;
     132            }
    112133
    113             $current_page = isset($_GET['p_no']) ? (int)$_GET['p_no'] : OWT7_LMS_DEFAULT_PAGE_NUMBER;
    114             $offset = ($current_page - 1) * $books_per_page;
     134            $current_page = isset( $_GET['p_no'] ) ? intval( $_GET['p_no'] ) : (int) OWT7_LMS_DEFAULT_PAGE_NUMBER;
     135            if ( $current_page < 1 ) {
     136                $current_page = 1;
     137            }
    115138
    116             // All categories
    117             $categories = $wpdb->get_results(
    118                 "SELECT category.*, (SELECT count(*) FROM ".$this->table_activator->owt7_library_tbl_books()." as book WHERE book.category_id = category.id LIMIT 1) as total_books from " . $this->table_activator->owt7_library_tbl_category(). " as category WHERE status = 1"
    119             );
     139            $offset = ( $current_page - 1 ) * $books_per_page;
     140            if ( $offset < 0 ) {
     141                $offset = 0;
     142            }
    120143
    121             // Get Total Pages
    122             $all_books = $wpdb->get_var("SELECT COUNT(*) FROM {$this->table_activator->owt7_library_tbl_books()} WHERE status = 1");
     144            $categories_sql = "
     145                SELECT category.*,
     146                    (SELECT count(*) FROM " . $this->table_activator->owt7_library_tbl_books() . " AS book WHERE book.category_id = category.id LIMIT 1) AS total_books
     147                FROM " . $this->table_activator->owt7_library_tbl_category() . " AS category
     148                WHERE status = 1
     149            ";
     150            $categories = $wpdb->get_results( $categories_sql );
    123151
    124             // Books Per Page Query
    125             $books = $wpdb->get_results(
    126                 "SELECT book.*, (SELECT category.name FROM ".$this->table_activator->owt7_library_tbl_category()." as category WHERE category.id = book.category_id LIMIT 1) as category_name, (SELECT bkcase.name FROM ".$this->table_activator->owt7_library_tbl_bookcase()." as bkcase WHERE bkcase.id = book.bookcase_id LIMIT 1) as bookcase_name, (SELECT section.name FROM ".$this->table_activator->owt7_library_tbl_bookcase_sections()." as section WHERE section.id = book.bookcase_section_id LIMIT 1) as section_name from " . $this->table_activator->owt7_library_tbl_books(). " as book WHERE status = 1 LIMIT {$books_per_page} OFFSET {$offset}"
    127             );
     152            $all_books = (int) $wpdb->get_var( "SELECT COUNT(*) FROM " . $this->table_activator->owt7_library_tbl_books() . " WHERE status = 1" );
    128153
    129             // Calculate total pages
    130             $total_pages = ceil($all_books / $books_per_page);
     154            $books_sql = "
     155                SELECT book.*,
     156                    (SELECT category.name FROM " . $this->table_activator->owt7_library_tbl_category() . " AS category WHERE category.id = book.category_id LIMIT 1) AS category_name,
     157                    (SELECT bkcase.name FROM " . $this->table_activator->owt7_library_tbl_bookcase() . " AS bkcase WHERE bkcase.id = book.bookcase_id LIMIT 1) AS bookcase_name,
     158                    (SELECT section.name FROM " . $this->table_activator->owt7_library_tbl_bookcase_sections() . " AS section WHERE section.id = book.bookcase_section_id LIMIT 1) AS section_name
     159                FROM " . $this->table_activator->owt7_library_tbl_books() . " AS book
     160                WHERE status = 1
     161                LIMIT %d OFFSET %d
     162            ";
     163            $prepared_books_sql = $wpdb->prepare( $books_sql, $books_per_page, $offset );
     164            $books = $wpdb->get_results( $prepared_books_sql );
    131165
    132             return $this->owt7_library_include_template_file("owt7_library_books", compact("books", "categories", "total_pages", "current_page"));
     166            $total_pages = 0;
     167            if ( $books_per_page > 0 ) {
     168                $total_pages = (int) ceil( $all_books / $books_per_page );
     169            }
     170
     171            return $this->owt7_library_include_template_file( "owt7_library_books", compact( "books", "categories", "total_pages", "current_page" ) );
    133172        }
    134173    }
Note: See TracChangeset for help on using the changeset viewer.