Plugin Directory

Changeset 3234611


Ignore:
Timestamp:
02/04/2025 11:06:22 AM (14 months ago)
Author:
topirank
Message:

Release version 1.0.4

Location:
topirank-integration
Files:
32 added
10 edited

Legend:

Unmodified
Added
Removed
  • topirank-integration/trunk/admin/admin-page.php

    r3220350 r3234611  
    1919        <div id="notification" class="notification"></div>
    2020        <div class="topirank_wrap">
    21             <?php topirank_handle_file_upload(); ?>
    2221            <form method="POST" enctype="multipart/form-data" action="<?php echo esc_url(admin_url('admin.php?page=topirank_process_upload')); ?>">
    23                 <?php wp_nonce_field('topirank_file_upload_action', 'topirank_file_upload_nonce'); ?>
    2422                <div class="topirank_main_content_line first_line">
    2523                    <div class="topirank_line_left_column">
     
    10098        </div>
    10199    </div>
    102     </div>
    103     </div>
    104100<?php
    105101}
  • topirank-integration/trunk/assets/css/topirank-normalize.css

    r3220350 r3234611  
    7272.topirank-styles table {
    7373  display: inline-table !important;
     74  border-width: 0;
    7475}
    7576
  • topirank-integration/trunk/includes/api-integration.php

    r3220350 r3234611  
    1616    ]);
    1717
     18    register_rest_route('topirank-integration', '/chunk-size', [
     19        'methods' => 'GET',
     20        'callback' => 'topirank_get_chunk_size',
     21        'permission_callback' => '__return_true',
     22    ]);
     23
    1824    register_rest_route('topirank-integration', '/ping', [
    1925        'methods' => 'GET',
     
    2430add_action('rest_api_init', 'topirank_custom_rest_route');
    2531
     32function topirank_get_chunk_size(WP_REST_Request $request)
     33{
     34    $upload_max_filesize = ini_get('upload_max_filesize');
     35    $post_max_size = ini_get('post_max_size');
     36    $max_upload_size = wp_convert_hr_to_bytes($upload_max_filesize);
     37    $max_post_size = wp_convert_hr_to_bytes($post_max_size);
     38    $default_chunk_size = min($max_upload_size, $max_post_size);
     39    return new WP_REST_Response(['chunk_size' => intval($default_chunk_size * 0.8)], 200);
     40}
    2641
    2742
     
    8095function topirank_handle_rest_file_upload(WP_REST_Request $request)
    8196{
     97
    8298    /* phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */
    8399
    84     if (!isset($_FILES['html_archive']) || empty($_FILES['html_archive'])) {
    85         return new WP_REST_Response(['message' => 'File not found.'], 400);
     100    $file = $_FILES['html_archive'];
     101
     102    $chunkIndex = intval($request->get_param('chunkIndex'));
     103    $totalChunks = intval($request->get_param('totalChunks'));
     104    $fileName = sanitize_file_name($request->get_param('fileName'));
     105    $replace = isset($request['replace']) && $request['replace'] === 'true';
     106    $selected_style = isset($request['style']) ? sanitize_text_field($request['style']) : 'not set';
     107
     108    if (!$file || $file['error'] !== UPLOAD_ERR_OK) {
     109        return new WP_REST_Response(['message' => 'Error uploading chunk.'], 400);
    86110    }
    87111
    88     $file = [
    89         'name'     => isset($_FILES['html_archive']['name']) ? sanitize_file_name(wp_unslash($_FILES['html_archive']['name'])) : '',
    90         'type'     => isset($_FILES['html_archive']['type']) ? sanitize_mime_type($_FILES['html_archive']['type']) : '',
    91         'tmp_name' => isset($_FILES['html_archive']['tmp_name']) ? sanitize_text_field($_FILES['html_archive']['tmp_name']) : '',
    92         'error'    => isset($_FILES['html_archive']['error']) ? intval($_FILES['html_archive']['error']) : 0,
    93         'size'     => isset($_FILES['html_archive']['size']) ? intval($_FILES['html_archive']['size']) : 0,
    94     ];
     112    $upload_dir = wp_upload_dir();
     113    $base_temp_dir = $upload_dir['basedir'] . '/topirank-temp';
    95114
    96     $replace = isset($request['replace']) && $request['replace'] == 'true';
    97     $selected_style = isset($request['style']) ? $request['style'] : 'not set';
     115    $temp_dir = $base_temp_dir . '/' . md5($fileName);
    98116
    99     if (!function_exists('wp_handle_upload')) {
    100         require_once ABSPATH . 'wp-admin/includes/file.php';
     117
     118    if (!file_exists($temp_dir)) {
     119        wp_mkdir_p($temp_dir);
    101120    }
    102121
    103     $file_extension = pathinfo($file['name'], PATHINFO_EXTENSION);
    104     if ($file_extension !== 'zip') {
    105         return new WP_REST_Response(['message' => 'Please upload a ZIP archive.'], 400);
     122    $temp_file_path = $temp_dir . '/' . $fileName . '.part';
     123
     124    $handle = fopen($temp_file_path, 'ab');
     125    if (!$handle) {
     126        return new WP_REST_Response(['message' => 'Error opening temp file.'], 500);
    106127    }
    107128
    108     $uploaded_file = wp_handle_upload($file, ['test_form' => false]);
     129    $chunk_data = file_get_contents($file['tmp_name']);
     130    fwrite($handle, $chunk_data);
     131    fclose($handle);
    109132
    110     if ($uploaded_file && !isset($uploaded_file['error'])) {
    111         $zip_path = $uploaded_file['file'];
    112         $upload_dir = wp_upload_dir();
     133    if ($chunkIndex + 1 === $totalChunks) {
     134        $final_path = $upload_dir['basedir'] . '/topirank-import-files/' . $fileName;
     135
     136        if (!file_exists(dirname($final_path))) {
     137            wp_mkdir_p(dirname($final_path));
     138        }
     139
     140        if (!rename($temp_file_path, $final_path)) {
     141            return new WP_REST_Response(['message' => 'Failed to finalize file upload.'], 500);
     142        }
     143
     144        $file_extension = pathinfo($final_path, PATHINFO_EXTENSION);
     145        if ($file_extension !== 'zip') {
     146            return new WP_REST_Response(['message' => 'Please upload a ZIP archive.'], 400);
     147        }
     148
    113149        $topirank_dir = $upload_dir['basedir'] . '/topirank-import-files';
    114         topirank_recursive_delete($topirank_dir);
    115150        if (!file_exists($topirank_dir)) {
    116151            wp_mkdir_p($topirank_dir);
    117152        }
    118153
    119         $extracted_path = $topirank_dir . '/' . basename($zip_path, '.zip');
     154        $extracted_path = $topirank_dir . '/' . basename($final_path, '.zip');
     155
     156        if (!function_exists('wp_handle_upload')) {
     157            require_once ABSPATH . 'wp-admin/includes/file.php';
     158        }
    120159
    121160        remove_filter('content_save_pre', 'wp_filter_post_kses');
    122161        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
    123162        ob_start();
    124         topirank_extract_and_create_posts($zip_path, $extracted_path, $replace, $selected_style);
     163        $process_result = topirank_extract_and_create_posts($final_path, $extracted_path, $replace, $selected_style);
    125164        ob_end_clean();
    126165        add_filter('content_save_pre', 'wp_filter_post_kses');
    127166        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
    128167
    129         topirank_recursive_delete($extracted_path);
    130         if (file_exists($zip_path)) {
    131             wp_delete_file($zip_path);
     168        if ($process_result) {
     169            wp_delete_file($final_path);
     170            topirank_recursive_delete($extracted_path);
     171        } else {
     172            return new WP_REST_Response(['message' => 'Failed to process the uploaded file.'], 500);
    132173        }
    133174
     175        $files = glob($temp_dir . '/*');
     176        foreach ($files as $file) {
     177            if (is_file($file)) {
     178                unlink($file);
     179            }
     180        }
     181        rmdir($temp_dir);
     182
    134183        return new WP_REST_Response(['message' => 'The file has been successfully uploaded and processed.'], 201);
    135     } else {
    136         return new WP_REST_Response(['message' => 'Error loading file: ' . $uploaded_file['error']], 500);
    137184    }
     185
     186    return new WP_REST_Response(['message' => 'Chunk uploaded successfully.'], 200);
    138187}
  • topirank-integration/trunk/includes/extract-and-create.php

    r3220350 r3234611  
    7272
    7373    topirank_process_folders($extracted_path, $archive_name, $replace, $selected_style, $check_delete);
     74
     75    return true;
    7476}
    7577
  • topirank-integration/trunk/includes/file-upload.php

    r3220350 r3234611  
    2424function topirank_handle_file_upload()
    2525{
    26     if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['html_archive'])) {
    27 
    28         if (
    29             !isset($_POST['topirank_file_upload_nonce']) ||
    30             !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['topirank_file_upload_nonce'])), 'topirank_file_upload_action')
    31         ) {
    32             wp_die(('Security check failed. Please try again.'));
     26    if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
     27        if (!isset($_FILES['html_archive']) || empty($_FILES['html_archive']['tmp_name'])) {
     28            echo '<div class="topirank_main_content_line without_space_between">
     29        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
     30<path d="M12 22C17.523 22 22 17.523 22 12C22 6.477 17.523 2 12 2C6.477 2 2 6.477 2 12C2 17.523 6.477 22 12 22Z" stroke="#CF2323" stroke-width="1.5" stroke-linejoin="round"/>
     31<path d="M14.8289 9.17188L9.17188 14.8289M9.17188 9.17188L14.8289 14.8289" stroke="#CF2323" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
     32</svg>
     33<div class="topirank_line_title">';
     34            esc_html_e('Invalid file upload.', 'topirank-integration');
     35            echo '</div>
     36</div>';
     37            return;
    3338        }
    3439
  • topirank-integration/trunk/includes/helpers.php

    r3220350 r3234611  
    55function topirank_create_posts_from_html($folder_path, $archive_name, $replace, $selected_style, &$check_delete)
    66{
     7    set_time_limit(0);
    78    $wp_filesystem = topirank_get_filesystem();
    89
  • topirank-integration/trunk/includes/init.php

    r3220350 r3234611  
    3737      exit;
    3838    }
    39 
    40     if (
    41       !isset($_POST['topirank_file_upload_nonce']) ||
    42       !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['topirank_file_upload_nonce'])), 'topirank_file_upload_action')
    43     ) {
    44       wp_die(('Security check failed. Please try again.'));
    45     }
    4639  }
    4740});
  • topirank-integration/trunk/includes/update-content-and-seo.php

    r3225179 r3234611  
    3535  $dom = new DOMDocument();
    3636
    37   $encoded_content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');
     37  $encoded_content = html_entity_decode(htmlspecialchars($content, ENT_QUOTES | ENT_HTML5, 'UTF-8'));
    3838  $dom->loadHTML($encoded_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    3939  libxml_clear_errors();
     
    155155      $dom = new DOMDocument();
    156156      libxml_use_internal_errors(true);
    157       $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
     157      $dom->loadHTML(html_entity_decode(htmlspecialchars($content, ENT_QUOTES | ENT_HTML5, 'UTF-8')), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    158158      libxml_clear_errors();
    159159    }
  • topirank-integration/trunk/readme.txt

    r3225179 r3234611  
    44Requires at least: 6.0
    55Tested up to: 6.7.1
    6 Stable tag: 1.0.3
     6Stable tag: 1.0.4
    77Requires PHP: 7.3
    88License: GPLv2 or later
     
    5858== Changelog ==
    5959
     60= 1.0.4 =
     61* Improved large file upload functionality via the application.
     62* Fixed parsing issues with HTML pages.
     63* Resolved timeout error issues.
     64* Optimized parsing speed for better performance.
     65* Fixed various SEO-related bugs.
     66
    6067= 1.0.3 =
    6168
  • topirank-integration/trunk/topirank-integration.php

    r3225180 r3234611  
    33Plugin Name: Topirank Integration
    44Description: Plugin for parsing pages
    5 Version: 1.0.3
     5Version: 1.0.4
    66Author: Topirank
    77License: GPLv2 or later
Note: See TracChangeset for help on using the changeset viewer.