Plugin Directory

Changeset 3198788


Ignore:
Timestamp:
11/28/2024 11:19:26 AM (16 months ago)
Author:
rachanaS
Message:

Security updates

Location:
sponsered-link/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sponsered-link/trunk/readme.txt

    r3187176 r3198788  
    33Tags: sponser link
    44
    5 Requires at least: 3.0.1
     5Requires at least: 4.7
    66
    7 Tested up to: 6.6.2
     7Tested up to: 6.7.1
    88
    99Stable tag: 6.0
  • sponsered-link/trunk/sponserlink.php

    r3187176 r3198788  
    9090add_shortcode( 'SponseredLink', 'show_sponser_fornt' );
    9191
    92 /*Add custom url*/
     92/* Add custom URL */
    9393add_action('wp_ajax_add_sponser', 'process_add_sponser');
    9494function process_add_sponser() {
    95         if ( empty($_POST) || !wp_verify_nonce($_POST['add-sponser-url'],'add_sponser') ) {
    96             echo 'You targeted the right function, but sorry, your nonce did not verify.';
    97             die();
    98         } else {
    99                 global $wpdb;
    100                 $table_name = $wpdb->prefix."sponser_link";
    101                 $title    =  sanitize_text_field($_REQUEST['title']);
    102                 $link     =  sanitize_text_field($_REQUEST['link']);
    103                 $created  =  time();
    104                 $publish  =  sanitize_text_field($_REQUEST['publish']);
    105                 $target  =  sanitize_text_field($_REQUEST['target']);
    106                 $upload     = wp_upload_bits($_FILES["image"]["name"], null, wp_remote_get($_FILES["image"]["tmp_name"]));
    107                 $wpdb->insert(
    108                     $table_name,
    109                     array(
    110                         'title'    => $title,
    111                         'link'     => $link,
    112                         'created'  => $created,
    113                         'publish'  => $publish,
    114                         'target'  => $target,
    115                         'image'      => $upload['url']
    116                     ),
    117                     array(
    118                         '%s',
    119                         '%s',
    120                         '%s',
    121                         '%s',
    122                         '%s',
    123                         '%s'
    124                     )
    125                 );
    126                 $displayUrl = $_SERVER['HTTP_REFERER'].'&addmsg=Added Successfully';
    127                 echo "<script type='text/javascript'>location.href = '" . esc_url($displayUrl). "';</script>";
    128                 die(0);
    129     }
    130 }
    131 /*Edit custom url*/
    132 
     95    // Verify nonce for security
     96    if ( empty($_POST) || !wp_verify_nonce($_POST['add-sponser-url'],'add_sponser') ) {
     97        echo 'You targeted the right function, but sorry, your nonce did not verify.';
     98        die();
     99    } else {
     100        global $wpdb;
     101        $table_name = $wpdb->prefix . "sponser_link";
     102       
     103        // Validation: Ensure title and link are non-empty and correct format
     104        $title = isset($_REQUEST['title']) ? sanitize_text_field($_REQUEST['title']) : ''; // sanitize text field for title
     105        $link = isset($_REQUEST['link']) ? esc_url_raw($_REQUEST['link']) : '';
     106        $publish = isset($_REQUEST['publish']) ? sanitize_text_field($_REQUEST['publish']) : '';
     107        $target = isset($_REQUEST['target']) ? sanitize_text_field($_REQUEST['target']) : '';
     108
     109        // Basic validation: Check if the title is too short or contains non-text characters
     110        if (strlen($title) < 5) {
     111            echo 'Title is too short, must be at least 5 characters.';
     112            die();
     113        }
     114
     115        // Regex to allow only letters and spaces (no numbers, no special characters)
     116        if (!preg_match('/^[a-zA-Z0-9\s]+$/', $title)) {
     117            echo 'Title contains invalid characters. Only letters and spaces are allowed.';
     118            die();
     119        }
     120
     121        // Handle file upload and validate the image file
     122        if ($_FILES["image"]["name"] != '') {
     123            // Validate image type (e.g., JPG, PNG, GIF)
     124            $allowed_types = array('image/jpeg', 'image/png', 'image/gif');
     125            $file_type = mime_content_type($_FILES["image"]["tmp_name"]);
     126            if (!in_array($file_type, $allowed_types)) {
     127                echo 'Invalid image type. Only JPG, PNG, or GIF allowed.';
     128                die();
     129            }
     130
     131            // Sanitize the file name to prevent issues
     132            $filename = sanitize_file_name($_FILES["image"]["name"]);
     133
     134            // Get the temporary file path
     135            $tmp_name = $_FILES["image"]["tmp_name"];
     136            $upload_overrides = array( 'test_form' => false );
     137           
     138            // Handle the upload
     139            if ($_FILES["image"]["error"] === UPLOAD_ERR_OK) {
     140
     141                // Create the uploaded file array
     142                $uploadedfile = array(
     143                    'name'     => $filename,
     144                    'type'     => $_FILES["image"]["type"],
     145                    'tmp_name' => $tmp_name,
     146                    'error'    => $_FILES["image"]["error"],
     147                    'size'     => $_FILES["image"]["size"]
     148                );
     149
     150                // Upload the file
     151                $upload = wp_handle_upload($uploadedfile, $upload_overrides);
     152
     153                // Check if the upload was successful
     154                if ($upload && !isset($upload['error'])) {
     155                    //echo esc_html("File uploaded successfully!");
     156                    //print_r($upload); // This will show the file path, URL, and other details
     157                    //echo esc_url($upload['url']);
     158                   
     159                } else {
     160                    echo esc_html("Upload failed.");
     161                    // Display specific upload error, safely escaped
     162                    echo esc_html("Error: " . $upload['error']);
     163                }
     164
     165            } else {
     166                echo esc_html("Error uploading file: " . $_FILES["image"]["error"]);
     167            }           
     168
     169            // Proceed with uploading the image
     170            //$upload = wp_upload_bits($_FILES["image"]["name"], null, file_get_contents($_FILES["image"]["tmp_name"]));
     171        } else {
     172            $upload['url'] = sanitize_text_field($_REQUEST['image_hidden']);
     173        }
     174
     175        // Insert into the database
     176        $wpdb->insert(
     177            $table_name,
     178            array(
     179                'title' => sanitize_text_field($title), // Ensure title is sanitized properly
     180                'link' => esc_url_raw($link),
     181                'created' => time(),
     182                'publish' => sanitize_text_field($publish),
     183                'target' => sanitize_text_field($target),
     184                'image' => esc_url_raw($upload['url'])
     185            ),
     186            array('%s', '%s', '%d', '%s', '%s', '%s')
     187        );
     188
     189        // Redirect with success message
     190        $displayUrl = $_SERVER['HTTP_REFERER'] . '&addmsg=Added Successfully';
     191        echo "<script type='text/javascript'>location.href = '" . esc_url($displayUrl) . "';</script>";
     192        die(0);
     193    }
     194}
     195
     196
     197/* Edit custom URL */
    133198add_action('wp_ajax_edit_sponser', 'process_edit_sponser');
    134 
    135 function process_edit_sponser(){
    136    
    137         if ( empty($_POST) || !wp_verify_nonce($_POST['edit-sponser-url'],'edit_sponser') ) {
    138             echo 'You targeted the right function, but sorry, your nonce did not verify.';
    139             die();
    140         } else {
    141             global $wpdb;
    142             $table_name = $wpdb->prefix."sponser_link";
    143             $title      = sanitize_text_field($_REQUEST['title']);
    144             $link       = sanitize_text_field($_REQUEST['link']);
    145             $publish    = sanitize_text_field($_REQUEST['publish']);
    146             $target  =  sanitize_text_field($_REQUEST['target']);
    147             if($_FILES["image"]["name"] == ''){
    148                 $upload['url']     =sanitize_text_field($_REQUEST['image_hidden']);
    149             }
    150             else{
    151                 $upload     = wp_upload_bits($_FILES["image"]["name"], null, wp_remote_get($_FILES["image"]["tmp_name"]));
     199function process_edit_sponser() {
     200    // Verify nonce for security
     201    if ( empty($_POST) || !wp_verify_nonce($_POST['edit-sponser-url'],'edit_sponser') ) {
     202        echo 'You targeted the right function, but sorry, your nonce did not verify.';
     203        die();
     204    } else {
     205        global $wpdb;
     206        $table_name = $wpdb->prefix . "sponser_link";
     207       
     208        // Validation: Ensure title and link are non-empty and correct format
     209        $title = isset($_REQUEST['title']) ? sanitize_text_field($_REQUEST['title']) : ''; // sanitize text field for title
     210        $link = isset($_REQUEST['link']) ? esc_url_raw($_REQUEST['link']) : '';
     211        $publish = isset($_REQUEST['publish']) ? sanitize_text_field($_REQUEST['publish']) : '';
     212        $target = isset($_REQUEST['target']) ? sanitize_text_field($_REQUEST['target']) : '';
     213
     214        // Basic validation: Check if the title is too short or contains non-text characters
     215        if (strlen($title) < 5) {
     216            echo 'Title is too short, must be at least 5 characters.';
     217            die();
     218        }
     219
     220        // Regex to allow only letters and spaces (no numbers, no special characters)
     221        if (!preg_match('/^[a-zA-Z0-9\s]+$/', $title)) {
     222            echo 'Title contains invalid characters. Only letters and spaces are allowed.';
     223            die();
     224        }
     225
     226        // Handle file upload and validate the image file
     227        if ($_FILES["image"]["name"] == '') {
     228            $upload['url'] = sanitize_text_field($_REQUEST['image_hidden']);
     229        } else {
     230            // Validate image type (e.g., JPG, PNG, GIF)
     231            $allowed_types = array('image/jpeg', 'image/png', 'image/gif');
     232            $file_type = mime_content_type($_FILES["image"]["tmp_name"]);
     233            if (!in_array($file_type, $allowed_types)) {
     234                echo 'Invalid image type. Only JPG, PNG, or GIF allowed.';
     235                die();
    152236            }
    153             $id         =  sanitize_text_field($_REQUEST['id']);
    154 
    155             $wpdb->update(
    156                 $table_name,
    157                 array(
    158                     'title'    => $title,   
    159                     'link'     => $link,
    160                     'publish'  => $publish,
    161                     'target'  => $target,
    162                     'image'      => $upload['url']
    163                 ),
    164                 array( 'id' =>  $id ),
    165                 array(
    166                     '%s',   
    167                     '%s',
    168                     '%s',
    169                     '%s',
    170                     '%s'
    171                 ),
    172                 array( '%d' )
    173             );
    174             $displayUrl2 = $_SERVER['HTTP_REFERER'];
    175             $Location22 = explode('&', $displayUrl2);
    176             echo "<script type='text/javascript'>location.href = '" . esc_url($Location22[0]).'&editmsg=Update Successfully'. "';</script>";
    177             die(0);
    178     }
    179 }
     237
     238
     239            // Sanitize the file name to prevent issues
     240            $filename = sanitize_file_name($_FILES["image"]["name"]);
     241
     242            // Get the temporary file path
     243            $tmp_name = $_FILES["image"]["tmp_name"];
     244            $upload_overrides = array( 'test_form' => false );
     245           
     246            // Handle the upload
     247            if ($_FILES["image"]["error"] === UPLOAD_ERR_OK) {
     248
     249                // Create the uploaded file array
     250                $uploadedfile = array(
     251                    'name'     => $filename,
     252                    'type'     => $_FILES["image"]["type"],
     253                    'tmp_name' => $tmp_name,
     254                    'error'    => $_FILES["image"]["error"],
     255                    'size'     => $_FILES["image"]["size"]
     256                );
     257
     258                // Upload the file
     259                $upload = wp_handle_upload($uploadedfile, $upload_overrides);
     260
     261                // Check if the upload was successful
     262                if ($upload && !isset($upload['error'])) {
     263                    //echo esc_html("File uploaded successfully!");
     264                    //print_r($upload); // This will show the file path, URL, and other details
     265                    $editImgUrl = esc_url_raw($upload['url']);
     266                   
     267                } else {
     268                    echo esc_html("Upload failed.");
     269                    // Display specific upload error, safely escaped
     270                    echo esc_html("Error: " . $upload['error']);
     271                }
     272
     273            } else {
     274                echo esc_html("Error uploading file: " . $_FILES["image"]["error"]);
     275            }
     276
     277
     278            // Proceed with uploading the image
     279            //$upload = wp_upload_bits($_FILES["image"]["name"], null, file_get_contents($_FILES["image"]["tmp_name"]));
     280        }
     281
     282        $id = sanitize_text_field($_REQUEST['id']);
     283       
     284        // Update the record in the database
     285        $wpdb->update(
     286            $table_name,
     287            array(
     288                'title' => sanitize_text_field($title), // Ensure title is sanitized properly
     289                'link' => esc_url_raw($link),
     290                'publish' => sanitize_text_field($publish),
     291                'target' => sanitize_text_field($target),
     292                'image' => $editImgUrl
     293            ),
     294            array('id' => $id),
     295            array('%s', '%s', '%s', '%s', '%s'),
     296            array('%d')
     297        );
     298
     299        // Redirect with success message
     300        $displayUrl = $_SERVER['HTTP_REFERER'] . '&editmsg=Edited Successfully';
     301        echo "<script type='text/javascript'>location.href = '" . esc_url($displayUrl) . "';</script>";
     302        die(0);
     303    }
     304}
     305
     306
    180307/*setting custom url*/
    181308
Note: See TracChangeset for help on using the changeset viewer.