| @@ -1,6 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 2 | + if(!function_exists('wpdocs_secure_url')){ | |
| 3 | + function wpdocs_secure_url($file_id=''){ | |
| 4 | + | |
| 5 | + $ret = add_query_arg( | |
| 6 | + array( | |
| 7 | + 'wpdocs_dl' => $file_id, | |
| 8 | + 'wpdocs_nonce' => wp_create_nonce('wpdocs_dl_'.$file_id), | |
| 9 | + ), | |
| 10 | + home_url('/') | |
| 11 | + ); | |
| 12 | + return $ret; | |
| 13 | + } | |
| 14 | + } | |
| 3 | 15 | if(!function_exists('wp_docs_get_memphis_dir_option_id')){ |
| 4 | 16 | function wp_docs_get_option_id($option_name){ |
| 5 | 17 | global $wpdb; |
| 6 | 18 | $option_name = esc_sql( $option_name ); |
| @@ -654,10 +666,30 @@ | ||
| 654 | 666 | //pree($ret); |
| 655 | 667 | |
| 656 | 668 | return $ret; |
| 657 | 669 | } |
| 670 | + function wpdocs_create_folder_post( $post_parent, $post_title = "New Folder" ) { | |
| 671 | + | |
| 672 | + if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { | |
| 673 | + return 0; | |
| 674 | + } | |
| 658 | 675 | |
| 659 | - function wpdocs_create_folder_post($post_parent, $post_title = "New Folder") | |
| 676 | + $post_parent = absint( $post_parent ); | |
| 677 | + $post_title = sanitize_text_field( $post_title ); | |
| 678 | + | |
| 679 | + $my_post = array( | |
| 680 | + 'post_title' => $post_title, | |
| 681 | + 'post_content' => '', | |
| 682 | + 'post_status' => 'hidden', | |
| 683 | + 'post_author' => get_current_user_id(), | |
| 684 | + 'post_type' => 'wpdocs_folder', | |
| 685 | + 'post_parent' => ( ( $post_parent > 0 && wpdocs_folder_exists( $post_parent ) ) ? $post_parent : 0 ), | |
| 686 | + 'post_category' => array(), | |
| 687 | + ); | |
| 688 | + | |
| 689 | + return wp_insert_post( $my_post ); | |
| 690 | + } | |
| 691 | + /*function wpdocs_create_folder_post($post_parent, $post_title = "New Folder") | |
| 660 | 692 | { |
| 661 | 693 | |
| 662 | 694 | $my_post = array( |
| 663 | 695 | 'post_title' => $post_title, |
| @@ -671,9 +703,9 @@ | ||
| 671 | 703 | |
| 672 | 704 | $dir_id = wp_insert_post($my_post); |
| 673 | 705 | |
| 674 | 706 | return $dir_id; |
| 675 | - } | |
| 707 | + }*/ | |
| 676 | 708 | |
| 677 | 709 | add_action('wp_ajax_wpdocs_create_folder', 'wpdocs_create_folder'); |
| 678 | 710 | |
| 679 | 711 | function wpdocs_create_folder() |
| @@ -809,11 +841,68 @@ | ||
| 809 | 841 | |
| 810 | 842 | } |
| 811 | 843 | } |
| 812 | 844 | |
| 845 | + /** | |
| 846 | + * Centralize the "can this user edit this folder?" decision. | |
| 847 | + * Admins always pass; otherwise require the folder to be owned by the user. | |
| 848 | + */ | |
| 849 | + function wpdocs_user_can_edit_folder( $dir_id ) { | |
| 850 | + if ( current_user_can( 'manage_options' ) ) { | |
| 851 | + return true; | |
| 852 | + } | |
| 853 | + | |
| 854 | + $dir = get_post( $dir_id ); | |
| 855 | + if ( ! $dir || 'wpdocs_folder' !== $dir->post_type ) { // adjust CPT slug | |
| 856 | + return false; | |
| 857 | + } | |
| 858 | + | |
| 859 | + return (int) $dir->post_author === get_current_user_id(); | |
| 860 | + } | |
| 861 | + | |
| 813 | 862 | add_action('wp_ajax_wpdocs_add_files', 'wpdocs_add_files'); |
| 814 | 863 | |
| 815 | - function wpdocs_add_files(){ | |
| 864 | + function wpdocs_add_files() { | |
| 865 | + | |
| 866 | + // 1) Capability FIRST — before nonce, before anything. | |
| 867 | + if ( ! is_user_logged_in() || ! current_user_can( 'upload_files' ) ) { | |
| 868 | + wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'wp-docs' ) ), 403 ); | |
| 869 | + } | |
| 870 | + | |
| 871 | + // 2) Nonce check. | |
| 872 | + if ( empty( $_POST['nonce'] ) | |
| 873 | + || ! wp_verify_nonce( | |
| 874 | + sanitize_wpdocs_data( wp_unslash( $_POST['nonce'] ) ), | |
| 875 | + 'wpdocs_update_options_nonce' | |
| 876 | + ) | |
| 877 | + ) { | |
| 878 | + wp_send_json_error( array( 'message' => __( 'Sorry, your nonce did not verify.', 'wp-docs' ) ), 403 ); | |
| 879 | + } | |
| 880 | + | |
| 881 | + // 3) Input validation. | |
| 882 | + $dir_id = isset( $_POST['dir_id'] ) ? absint( $_POST['dir_id'] ) : 0; | |
| 883 | + if ( ! $dir_id ) { | |
| 884 | + wp_send_json_error( array( 'message' => __( 'Invalid folder.', 'wp-docs' ) ), 400 ); | |
| 885 | + } | |
| 886 | + | |
| 887 | + // 4) Per-folder ownership / capability gate. | |
| 888 | + if ( ! wpdocs_user_can_edit_folder( $dir_id ) ) { | |
| 889 | + wp_send_json_error( array( 'message' => __( 'You cannot modify this folder.', 'wp-docs' ) ), 403 ); | |
| 890 | + } | |
| 891 | + | |
| 892 | + $files = isset( $_POST['files'] ) ? sanitize_wpdocs_data( $_POST['files'] ) : array(); | |
| 893 | + $files = is_array( $files ) ? $files : array( $files ); | |
| 894 | + $files = array_filter( array_map( 'absint', $files ) ); | |
| 895 | + | |
| 896 | + wpdocs_update_files_meta( $dir_id, $files ); | |
| 897 | + | |
| 898 | + $ret = ! empty( $files ) ? wpdocs_list_added_items( $dir_id ) : ''; | |
| 899 | + | |
| 900 | + echo $ret; | |
| 901 | + exit; | |
| 902 | + } | |
| 903 | + | |
| 904 | + /*function wpdocs_add_files_old(){ | |
| 816 | 905 | |
| 817 | 906 | $nonce = sanitize_wpdocs_data(wp_unslash($_POST['nonce'])); |
| 818 | 907 | |
| 819 | 908 | if (!empty($_POST) && isset($_POST['nonce']) && ! wp_verify_nonce( $nonce, 'wpdocs_update_options_nonce' ) ) |
| @@ -839,9 +928,9 @@ | ||
| 839 | 928 | } |
| 840 | 929 | |
| 841 | 930 | echo $ret; |
| 842 | 931 | exit; |
| 843 | - } | |
| 932 | + }*/ | |
| 844 | 933 | function wpdocs_list_added_items($dir) |
| 845 | 934 | { |
| 846 | 935 | |
| 847 | 936 | global $wpdocs_options, $icon_sub_path, $wpdocs_url; |
| @@ -930,15 +1019,9 @@ | ||
| 930 | 1019 | } |
| 931 | 1020 | $class = ''; |
| 932 | 1021 | |
| 933 | 1022 | |
| 934 | - $secured_file_url = add_query_arg( | |
| 935 | - array( | |
| 936 | - 'wpdocs_dl' => $file_id, | |
| 937 | - 'wpdocs_nonce' => wp_create_nonce('wpdocs_dl_'.$file_id), | |
| 938 | - ), | |
| 939 | - home_url('/') | |
| 940 | - ); | |
| 1023 | + $secured_file_url = wpdocs_secure_url($file_id); | |
| 941 | 1024 | |
| 942 | 1025 | $files_list[ $title ] = '<li data-id="' . esc_attr( $item ) . '" data-dir="' . esc_attr( $dir ) . '" title="' . esc_attr( $filename ) . '"> |
| 943 | 1026 | <a href="' . esc_url( $secured_file_url ) . '" target="_blank" class="file ' . esc_attr( $class ) . '"> ' . $icon_str . ' </a> |
| 944 | 1027 | <a class="ftitle" title="' . esc_attr( $title ) . '">' . esc_html( $title ) . '</a> |
| @@ -1478,15 +1561,9 @@ | ||
| 1478 | 1561 | //$class .= 'fa-file'; |
| 1479 | 1562 | break; |
| 1480 | 1563 | } |
| 1481 | 1564 | |
| 1482 | - $secured_file_url = add_query_arg( | |
| 1483 | - array( | |
| 1484 | - 'wpdocs_dl' => $file_id, | |
| 1485 | - 'wpdocs_nonce' => wp_create_nonce('wpdocs_dl_' . $file_id), | |
| 1486 | - ), | |
| 1487 | - home_url('/') | |
| 1488 | - ); | |
| 1565 | + $secured_file_url = wpdocs_secure_url($file_id); | |
| 1489 | 1566 | |
| 1490 | 1567 | $file_list_row = ' |
| 1491 | 1568 | |
| 1492 | 1569 | <div title="'.esc_attr($filename).'" class="col-4 col-md-3 is_file text-center is_shallow" style="cursor: pointer;" data-id="'.esc_attr($file).'"> |
| @@ -1571,15 +1648,9 @@ | ||
| 1571 | 1648 | //$class .= 'fa-file'; |
| 1572 | 1649 | break; |
| 1573 | 1650 | } |
| 1574 | 1651 | |
| 1575 | - $secured_file_url = add_query_arg( | |
| 1576 | - array( | |
| 1577 | - 'wpdocs_dl' => $file_id, | |
| 1578 | - 'wpdocs_nonce' => wp_create_nonce('wpdocs_dl_' . $file_id), | |
| 1579 | - ), | |
| 1580 | - home_url('/') | |
| 1581 | - ); | |
| 1652 | + $secured_file_url = wpdocs_secure_url($file_id); | |
| 1582 | 1653 | |
| 1583 | 1654 | $file_list_row = ' |
| 1584 | 1655 | |
| 1585 | 1656 | <div title="'.esc_attr($filename).'" class="col-4 col-md-3 is_file text-center is_deep" style="cursor: pointer;" data-id="'.esc_attr($file).'"> |
| @@ -1679,15 +1750,9 @@ | ||
| 1679 | 1750 | |
| 1680 | 1751 | |
| 1681 | 1752 | if(trim($file_url)){ |
| 1682 | 1753 | |
| 1683 | - $secured_file_url = add_query_arg( | |
| 1684 | - array( | |
| 1685 | - 'wpdocs_dl' => $file_id, | |
| 1686 | - 'wpdocs_nonce' => wp_create_nonce('wpdocs_dl_' . $file_id), | |
| 1687 | - ), | |
| 1688 | - home_url('/') | |
| 1689 | - ); | |
| 1754 | + $secured_file_url = wpdocs_secure_url($file_id); | |
| 1690 | 1755 | |
| 1691 | 1756 | $file_list_row = ' |
| 1692 | 1757 | <div title="'.esc_attr($filename).'" class="col-12 file_wrapper is_file" style="cursor: pointer;" data-id="'.$file.'"> |
| 1693 | 1758 | <figure class="figure file_view p-3"> |
| @@ -1883,15 +1948,9 @@ | ||
| 1883 | 1948 | extract($file_data); |
| 1884 | 1949 | //pree($ts); |
| 1885 | 1950 | |
| 1886 | 1951 | if(trim($icon_url)){ |
| 1887 | - $secured_file_url = add_query_arg( | |
| 1888 | - array( | |
| 1889 | - 'wpdocs_dl' => $file_id, | |
| 1890 | - 'wpdocs_nonce' => wp_create_nonce('wpdocs_dl_' . $file_id), | |
| 1891 | - ), | |
| 1892 | - home_url('/') | |
| 1893 | - ); | |
| 1952 | + $secured_file_url = wpdocs_secure_url($file_id); | |
| 1894 | 1953 | $files_list_row = ' |
| 1895 | 1954 | <tr title="'.esc_attr($filename).'" data-url="'.$secured_file_url.'" class="file_view file_link is_file" style="cursor: pointer;" data-id="'.$file.'"> |
| 1896 | 1955 | |
| 1897 | 1956 | <td> |
| @@ -1947,15 +2006,9 @@ | ||
| 1947 | 2006 | //pree($ts); |
| 1948 | 2007 | |
| 1949 | 2008 | if(trim($icon_url)){ |
| 1950 | 2009 | |
| 1951 | - $secured_file_url = add_query_arg( | |
| 1952 | - array( | |
| 1953 | - 'wpdocs_dl' => $file_id, | |
| 1954 | - 'wpdocs_nonce' => wp_create_nonce('wpdocs_dl_' . $file_id), | |
| 1955 | - ), | |
| 1956 | - home_url('/') | |
| 1957 | - ); | |
| 2010 | + $secured_file_url = wpdocs_secure_url($file_id); | |
| 1958 | 2011 | |
| 1959 | 2012 | $files_list_row = ' |
| 1960 | 2013 | <tr title="'.esc_attr($filename).'" data-url="'.$secured_file_url.'" class="file_view file_link is_file is_deep" style="cursor: pointer;" data-id="'.$file.'"> |
| 1961 | 2014 | |
| @@ -2142,8 +2195,53 @@ | ||
| 2142 | 2195 | add_action('wp_ajax_wpdocs_update_folder', 'wpdocs_update_folder'); |
| 2143 | 2196 | |
| 2144 | 2197 | function wpdocs_update_folder() { |
| 2145 | 2198 | |
| 2199 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 2200 | + wp_send_json_error( array( 'msg' => __( 'Unauthorized access.', 'wp-docs' ) ), 403 ); | |
| 2201 | + } | |
| 2202 | + | |
| 2203 | + if ( | |
| 2204 | + empty( $_POST['nonce'] ) || | |
| 2205 | + ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wpdocs_update_options_nonce' ) | |
| 2206 | + ) { | |
| 2207 | + wp_send_json_error( array( 'msg' => __( 'Sorry, your nonce did not verify.', 'wp-docs' ) ), 403 ); | |
| 2208 | + } | |
| 2209 | + | |
| 2210 | + $dir_id = absint( $_POST['dir_id'] ?? 0 ); | |
| 2211 | + $resource_id = base64_decode( sanitize_text_field( $_POST['resource_id'] ?? '' ) ); | |
| 2212 | + $new_name = sanitize_text_field( $_POST['new_name'] ?? '' ); | |
| 2213 | + | |
| 2214 | + if ( ! $dir_id || $resource_id != $dir_id || ! wpdocs_folder_exists( $dir_id ) ) { | |
| 2215 | + wp_send_json_error( array( 'msg' => __( 'Invalid folder ID or resource mismatch.', 'wp-docs' ) ), 400 ); | |
| 2216 | + } | |
| 2217 | + | |
| 2218 | + global $wpdb, $wpdocs_post_types, $wpdocs_post_status; | |
| 2219 | + | |
| 2220 | + $updated = $wpdb->query( | |
| 2221 | + $wpdb->prepare( | |
| 2222 | + "UPDATE $wpdb->posts | |
| 2223 | + SET post_title = %s | |
| 2224 | + WHERE ID = %d | |
| 2225 | + AND post_type IN ('" . implode( "','", array_map( 'esc_sql', $wpdocs_post_types ) ) . "') | |
| 2226 | + AND post_status = %s", | |
| 2227 | + htmlspecialchars_decode( $new_name ), | |
| 2228 | + $dir_id, | |
| 2229 | + $wpdocs_post_status | |
| 2230 | + ) | |
| 2231 | + ); | |
| 2232 | + | |
| 2233 | + wp_send_json_success( | |
| 2234 | + array( | |
| 2235 | + 'msg' => $updated | |
| 2236 | + ? __( 'Successfully updated.', 'wp-docs' ) | |
| 2237 | + : __( 'No changes were made. Input seems the same as before.', 'wp-docs' ), | |
| 2238 | + ) | |
| 2239 | + ); | |
| 2240 | + } | |
| 2241 | + | |
| 2242 | + /*function wpdocs_update_folder() { | |
| 2243 | + | |
| 2146 | 2244 | |
| 2147 | 2245 | if ( ! current_user_can('edit_posts') ) { |
| 2148 | 2246 | wp_send_json_error(['msg' => __('Unauthorized access.', 'wp-docs')]); |
| 2149 | 2247 | } |
| @@ -2189,14 +2287,35 @@ | ||
| 2189 | 2287 | $ret['msg'] = __('Invalid folder ID or resource mismatch.', 'wp-docs'); |
| 2190 | 2288 | } |
| 2191 | 2289 | |
| 2192 | 2290 | wp_send_json_success($ret); |
| 2193 | - } | |
| 2291 | + }*/ | |
| 2194 | 2292 | |
| 2195 | 2293 | |
| 2196 | 2294 | add_action('wp_ajax_wpdocs_delete_folder', 'wpdocs_delete_folder'); |
| 2197 | 2295 | |
| 2198 | - function wpdocs_delete_folder() | |
| 2296 | + function wpdocs_delete_folder() { | |
| 2297 | + | |
| 2298 | + if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { | |
| 2299 | + wp_send_json_error( array( 'msg' => __( 'Unauthorized user', 'wp-docs' ) ), 403 ); | |
| 2300 | + } | |
| 2301 | + | |
| 2302 | + if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_wpdocs_data( wp_unslash( $_POST['nonce'] ) ), 'wpdocs_update_options_nonce' ) ) { | |
| 2303 | + wp_send_json_error( array( 'msg' => __( 'Sorry, your nonce did not verify.', 'wp-docs' ) ), 403 ); | |
| 2304 | + } | |
| 2305 | + | |
| 2306 | + $dir_id = isset( $_POST['dir_id'] ) ? absint( $_POST['dir_id'] ) : 0; | |
| 2307 | + $resource_id = base64_decode( sanitize_wpdocs_data( $_POST['resource_id'] ?? '' ) ); | |
| 2308 | + | |
| 2309 | + if ( ! $dir_id || $dir_id != $resource_id || ! wpdocs_folder_exists( $dir_id ) ) { | |
| 2310 | + wp_send_json_error( array( 'msg' => __( 'Invalid folder.', 'wp-docs' ) ), 400 ); | |
| 2311 | + } | |
| 2312 | + | |
| 2313 | + wpdocs_recursive_delete_folder( $dir_id ); | |
| 2314 | + | |
| 2315 | + wp_send_json_success(); | |
| 2316 | + } | |
| 2317 | + /*function wpdocs_delete_folder() | |
| 2199 | 2318 | { |
| 2200 | 2319 | |
| 2201 | 2320 | if ( ! current_user_can( 'manage_options' ) ) { |
| 2202 | 2321 | wp_send_json_error( __( 'Unauthorized user', 'wp-docs' ) ); |
| @@ -2215,9 +2334,9 @@ | ||
| 2215 | 2334 | wpdocs_recursive_delete_folder($dir_id); |
| 2216 | 2335 | } |
| 2217 | 2336 | |
| 2218 | 2337 | exit; |
| 2219 | - } | |
| 2338 | + } */ | |
| 2220 | 2339 | |
| 2221 | 2340 | |
| 2222 | 2341 | |
| 2223 | 2342 | add_action('wp_ajax_wpdocs_delete_files', 'wpdocs_delete_files'); |
| @@ -2297,11 +2416,59 @@ | ||
| 2297 | 2416 | return update_post_meta($dir_id, 'wpdocs_items', $wpdocs_items); |
| 2298 | 2417 | } |
| 2299 | 2418 | } |
| 2300 | 2419 | } |
| 2420 | + | |
| 2421 | + function wpdocs_delete_files() { | |
| 2422 | + if ( ! is_user_logged_in() || ! current_user_can( 'delete_posts' ) ) { | |
| 2423 | + wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'wp-docs' ) ), 403 ); | |
| 2424 | + } | |
| 2425 | + | |
| 2426 | + if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_wpdocs_data( wp_unslash( $_POST['nonce'] ) ), 'wpdocs_update_options_nonce' ) ) { | |
| 2427 | + wp_send_json_error( array( 'message' => __( 'Sorry, your nonce did not verify.', 'wp-docs' ) ), 403 ); | |
| 2428 | + } | |
| 2429 | + | |
| 2430 | + $dir_id = isset( $_POST['dir_id'] ) ? absint( $_POST['dir_id'] ) : 0; | |
| 2431 | + $files = isset( $_POST['files'] ) ? (array) $_POST['files'] : array(); | |
| 2432 | + $files = array_values( array_unique( array_filter( array_map( 'absint', $files ) ) ) ); | |
| 2433 | + | |
| 2434 | + if ( ! $dir_id || ! wpdocs_folder_exists( $dir_id ) || empty( $files ) ) { | |
| 2435 | + wp_send_json_error( array( 'message' => __( 'Invalid request.', 'wp-docs' ) ), 400 ); | |
| 2436 | + } | |
| 2437 | + | |
| 2438 | + $is_admin = current_user_can( 'manage_options' ); | |
| 2439 | + $is_owner = wpdocs_user_can_edit_folder( $dir_id ); | |
| 2440 | + | |
| 2441 | + if ( ! $is_admin && ! $is_owner ) { | |
| 2442 | + wp_send_json_error( array( 'message' => __( 'You cannot delete files from this folder.', 'wp-docs' ) ), 403 ); | |
| 2443 | + } | |
| 2444 | + | |
| 2445 | + $allowed = array(); | |
| 2446 | + foreach ( $files as $file_id ) { | |
| 2447 | + $attachment = get_post( $file_id ); | |
| 2448 | + if ( ! $attachment || 'attachment' !== $attachment->post_type ) { | |
| 2449 | + continue; | |
| 2450 | + } | |
| 2451 | + if ( $is_admin || current_user_can( 'edit_post', $file_id ) ) { | |
| 2452 | + $allowed[] = $file_id; | |
| 2453 | + } | |
| 2454 | + } | |
| 2455 | + | |
| 2456 | + if ( empty( $allowed ) ) { | |
| 2457 | + wp_send_json_error( array( 'message' => __( 'None of the specified files can be deleted by you.', 'wp-docs' ) ), 403 ); | |
| 2458 | + } | |
| 2459 | + | |
| 2460 | + wpdocs_del_items_by_user( $dir_id, $allowed, get_current_user_id() ); | |
| 2461 | + | |
| 2462 | + $wpdocs_items = wpdocs_added_items( $dir_id ); | |
| 2463 | + $wpdocs_items = array_values( array_unique( array_diff( (array) $wpdocs_items, $allowed ) ) ); | |
| 2464 | + | |
| 2465 | + update_post_meta( $dir_id, 'wpdocs_items', $wpdocs_items ); | |
| 2466 | + | |
| 2467 | + wp_send_json_success( array( 'dir_id' => $dir_id, 'files' => $allowed ) ); | |
| 2468 | + } | |
| 2301 | 2469 | |
| 2302 | - | |
| 2303 | - | |
| 2470 | + /* | |
| 2304 | 2471 | function wpdocs_delete_files() |
| 2305 | 2472 | { |
| 2306 | 2473 | |
| 2307 | 2474 | $dir_id = sanitize_wpdocs_data($_POST['dir_id']); |
| @@ -2325,9 +2492,9 @@ | ||
| 2325 | 2492 | } |
| 2326 | 2493 | |
| 2327 | 2494 | |
| 2328 | 2495 | exit; |
| 2329 | - } | |
| 2496 | + } */ | |
| 2330 | 2497 | |
| 2331 | 2498 | function wpd_admin_footer(){ |
| 2332 | 2499 | |
| 2333 | 2500 | ?> |
| @@ -2623,11 +2790,46 @@ | ||
| 2623 | 2790 | } |
| 2624 | 2791 | } |
| 2625 | 2792 | |
| 2626 | 2793 | add_action('wp_ajax_wpdocs_update_view', 'wpdocs_update_view'); |
| 2627 | - add_action('wp_ajax_nopriv_wpdocs_update_view', 'wpdocs_update_view'); | |
| 2628 | - | |
| 2629 | - if(!function_exists('wpdocs_update_view')){ | |
| 2794 | + | |
| 2795 | + | |
| 2796 | + if ( ! function_exists( 'wpdocs_update_view' ) ) { | |
| 2797 | + function wpdocs_update_view() { | |
| 2798 | + | |
| 2799 | + if ( ! is_user_logged_in() || ! current_user_can( 'read' ) ) { | |
| 2800 | + wp_send_json_error( array( 'msg' => __( 'Unauthorized access.', 'wp-docs' ) ), 403 ); | |
| 2801 | + } | |
| 2802 | + | |
| 2803 | + if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_wpdocs_data( wp_unslash( $_POST['nonce'] ) ), 'wpdocs_update_options_nonce' ) ) { | |
| 2804 | + wp_send_json_error( array( 'msg' => __( 'Sorry, your nonce did not verify.', 'wp-docs' ) ), 403 ); | |
| 2805 | + } | |
| 2806 | + | |
| 2807 | + $uid = get_current_user_id(); | |
| 2808 | + $key = 'wpdocs_view_rl_' . $uid; | |
| 2809 | + $hits = (int) get_transient( $key ); | |
| 2810 | + | |
| 2811 | + if ( $hits >= 60 ) { | |
| 2812 | + wp_send_json_error( array( 'msg' => __( 'Too many requests.', 'wp-docs' ) ), 429 ); | |
| 2813 | + } | |
| 2814 | + set_transient( $key, $hits + 1, MINUTE_IN_SECONDS ); | |
| 2815 | + | |
| 2816 | + if ( isset( $_POST['update_view'] ) ) { | |
| 2817 | + | |
| 2818 | + $wpdocs_view = get_option( 'wpdocs_view', array() ); | |
| 2819 | + $wpdocs_view = is_array( $wpdocs_view ) ? $wpdocs_view : array(); | |
| 2820 | + | |
| 2821 | + $parent_dir = sanitize_wpdocs_data( $_POST['parent_dir'] ?? '' ); | |
| 2822 | + $view_val = sanitize_wpdocs_data( $_POST['update_view'] ); | |
| 2823 | + | |
| 2824 | + $wpdocs_view[ $parent_dir ] = $view_val; | |
| 2825 | + update_option( 'wpdocs_view', $wpdocs_view ); | |
| 2826 | + } | |
| 2827 | + | |
| 2828 | + wp_send_json_success(); | |
| 2829 | + } | |
| 2830 | + } | |
| 2831 | + /*if(!function_exists('wpdocs_update_view')){ | |
| 2630 | 2832 | function wpdocs_update_view(){ |
| 2631 | 2833 | |
| 2632 | 2834 | $nonce = sanitize_wpdocs_data(wp_unslash($_POST['nonce'])); |
| 2633 | 2835 | |
| @@ -2645,9 +2847,9 @@ | ||
| 2645 | 2847 | |
| 2646 | 2848 | } |
| 2647 | 2849 | exit; |
| 2648 | 2850 | } |
| 2649 | - } | |
| 2851 | + }*/ | |
| 2650 | 2852 | function wpdocs_init_session() { |
| 2651 | 2853 | if(!session_id()) { |
| 2652 | 2854 | session_start(); |
| 2653 | 2855 | } |
| @@ -2843,81 +3045,83 @@ | ||
| 2843 | 3045 | return $ret; |
| 2844 | 3046 | |
| 2845 | 3047 | } |
| 2846 | 3048 | } |
| 2847 | - function wpdocs_file_download(){ | |
| 2848 | - | |
| 2849 | - if(empty($_GET['wpdocs_dl'])){ | |
| 2850 | - return; | |
| 2851 | - } | |
| 2852 | - | |
| 2853 | - $file_id = absint( | |
| 2854 | - wp_unslash($_GET['wpdocs_dl']) | |
| 2855 | - ); | |
| 2856 | - | |
| 2857 | - if(!$file_id){ | |
| 2858 | - status_header(404); | |
| 3049 | + if(!function_exists('wpdocs_file_download')){ | |
| 3050 | + function wpdocs_file_download(){ | |
| 3051 | + | |
| 3052 | + if(empty($_GET['wpdocs_dl'])){ | |
| 3053 | + return; | |
| 3054 | + } | |
| 3055 | + | |
| 3056 | + $file_id = absint( | |
| 3057 | + wp_unslash($_GET['wpdocs_dl']) | |
| 3058 | + ); | |
| 3059 | + | |
| 3060 | + if(!$file_id){ | |
| 3061 | + status_header(404); | |
| 3062 | + exit; | |
| 3063 | + } | |
| 3064 | + | |
| 3065 | + $nonce = isset($_GET['wpdocs_nonce']) | |
| 3066 | + ? sanitize_text_field( | |
| 3067 | + wp_unslash($_GET['wpdocs_nonce']) | |
| 3068 | + ) | |
| 3069 | + : ''; | |
| 3070 | + | |
| 3071 | + if( | |
| 3072 | + !wp_verify_nonce( | |
| 3073 | + $nonce, | |
| 3074 | + 'wpdocs_dl_' . $file_id | |
| 3075 | + ) | |
| 3076 | + ){ | |
| 3077 | + status_header(403); | |
| 3078 | + exit; | |
| 3079 | + } | |
| 3080 | + | |
| 3081 | + // Adjust this according to where you store the file | |
| 3082 | + $file = get_attached_file($file_id); | |
| 3083 | + | |
| 3084 | + if( | |
| 3085 | + !$file || | |
| 3086 | + !file_exists($file) | |
| 3087 | + ){ | |
| 3088 | + status_header(404); | |
| 3089 | + exit; | |
| 3090 | + } | |
| 3091 | + | |
| 3092 | + $uploads = wp_get_upload_dir(); | |
| 3093 | + $base_real = realpath($uploads['basedir']); | |
| 3094 | + $file_real = realpath($file); | |
| 3095 | + | |
| 3096 | + if( | |
| 3097 | + !$base_real || | |
| 3098 | + !$file_real || | |
| 3099 | + strpos( | |
| 3100 | + $file_real, | |
| 3101 | + $base_real . DIRECTORY_SEPARATOR | |
| 3102 | + ) !== 0 | |
| 3103 | + ){ | |
| 3104 | + status_header(403); | |
| 3105 | + exit; | |
| 3106 | + } | |
| 3107 | + | |
| 3108 | + $mime = wp_check_filetype($file_real); | |
| 3109 | + $mime = !empty($mime['type']) | |
| 3110 | + ? $mime['type'] | |
| 3111 | + : 'application/octet-stream'; | |
| 3112 | + | |
| 3113 | + header('Content-Type: ' . $mime); | |
| 3114 | + header('Content-Length: ' . filesize($file_real)); | |
| 3115 | + header( | |
| 3116 | + 'Content-Disposition: inline; filename="' . | |
| 3117 | + basename($file_real) . | |
| 3118 | + '"' | |
| 3119 | + ); | |
| 3120 | + | |
| 3121 | + readfile($file_real); | |
| 2859 | 3122 | exit; |
| 2860 | 3123 | } |
| 2861 | - | |
| 2862 | - $nonce = isset($_GET['wpdocs_nonce']) | |
| 2863 | - ? sanitize_text_field( | |
| 2864 | - wp_unslash($_GET['wpdocs_nonce']) | |
| 2865 | - ) | |
| 2866 | - : ''; | |
| 2867 | - | |
| 2868 | - if( | |
| 2869 | - !wp_verify_nonce( | |
| 2870 | - $nonce, | |
| 2871 | - 'wpdocs_dl_' . $file_id | |
| 2872 | - ) | |
| 2873 | - ){ | |
| 2874 | - status_header(403); | |
| 2875 | - exit; | |
| 2876 | - } | |
| 2877 | - | |
| 2878 | - // Adjust this according to where you store the file | |
| 2879 | - $file = get_attached_file($file_id); | |
| 2880 | - | |
| 2881 | - if( | |
| 2882 | - !$file || | |
| 2883 | - !file_exists($file) | |
| 2884 | - ){ | |
| 2885 | - status_header(404); | |
| 2886 | - exit; | |
| 2887 | - } | |
| 2888 | - | |
| 2889 | - $uploads = wp_get_upload_dir(); | |
| 2890 | - $base_real = realpath($uploads['basedir']); | |
| 2891 | - $file_real = realpath($file); | |
| 2892 | - | |
| 2893 | - if( | |
| 2894 | - !$base_real || | |
| 2895 | - !$file_real || | |
| 2896 | - strpos( | |
| 2897 | - $file_real, | |
| 2898 | - $base_real . DIRECTORY_SEPARATOR | |
| 2899 | - ) !== 0 | |
| 2900 | - ){ | |
| 2901 | - status_header(403); | |
| 2902 | - exit; | |
| 2903 | - } | |
| 2904 | - | |
| 2905 | - $mime = wp_check_filetype($file_real); | |
| 2906 | - $mime = !empty($mime['type']) | |
| 2907 | - ? $mime['type'] | |
| 2908 | - : 'application/octet-stream'; | |
| 2909 | - | |
| 2910 | - header('Content-Type: ' . $mime); | |
| 2911 | - header('Content-Length: ' . filesize($file_real)); | |
| 2912 | - header( | |
| 2913 | - 'Content-Disposition: inline; filename="' . | |
| 2914 | - basename($file_real) . | |
| 2915 | - '"' | |
| 2916 | - ); | |
| 2917 | - | |
| 2918 | - readfile($file_real); | |
| 2919 | - exit; | |
| 2920 | 3124 | } |
| 2921 | 3125 | |
| 2922 | 3126 | add_action('init', 'wpdocs_dir_actions'); |
| 2923 | 3127 | if(!function_exists('wpdocs_dir_actions')){ |