Uname:Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64

403WebShell
403Webshell
Server IP : 13.126.101.145  /  Your IP : 216.73.217.37
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64
User : www-data ( 33)
PHP Version : 8.3.17
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/html/rentals_updated/wp-content/plugins/api_manager/includes/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/rentals_updated/wp-content/plugins/api_manager/includes/class-sap-fleet-endpoint.php
<?php

// if (!defined('ABSPATH')) exit; // Exit if accessed directly

// class SAP_Fleet_Endpoint {

//     // Constructor to register the custom endpoint
//     public function __construct() {
//         add_action('init', [$this, 'register_custom_endpoint']);
//     }

//     // Register a custom endpoint
//     public function register_custom_endpoint() {
//         add_rewrite_rule('^sap-fleet-endpoint/?$', 'index.php?sap_fleet_endpoint=1', 'top');
//         add_filter('query_vars', [$this, 'add_query_var']);
//         add_action('template_redirect', [$this, 'handle_custom_endpoint']);
//     }

//     // Add custom query var for detection
//     public function add_query_var($query_vars) {
//         $query_vars[] = 'sap_fleet_endpoint';
//         return $query_vars;
//     }

//     // Helper function to find product by SKU
//     private function get_product_id_by_sku($sku) {
//         global $wpdb;
//         $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku));
//         return $product_id;
//     }

//     // Handle requests to the custom endpoint
//     public function handle_custom_endpoint() {
//         if (get_query_var('sap_fleet_endpoint') != 1) {
//             return;
//         }

//         // Basic Auth credentials
//         $valid_username = 'RMS_USER'; // Replace with your username
//         $valid_password = 'RMS_iTroSys123!'; // Replace with your password

//         // Basic Authentication
//         if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
//             header('WWW-Authenticate: Basic realm="Restricted Area"');
//             header('HTTP/1.0 401 Unauthorized');
//             echo json_encode(['error' => 'Unauthorized: Missing credentials']);
//             exit;
//         }

//         if ($_SERVER['PHP_AUTH_USER'] !== $valid_username || $_SERVER['PHP_AUTH_PW'] !== $valid_password) {
//             header('HTTP/1.0 401 Unauthorized');
//             echo json_encode(['error' => 'Unauthorized: Invalid credentials']);
//             exit;
//         }

//         // Parse the incoming data
//         $request_body = file_get_contents('php://input');
//         $data = json_decode($request_body, true);

//         // Validate required fields
//         if (empty($data['fleet_action']) || empty($data['asset_id'])) {
//             header('HTTP/1.0 400 Bad Request');
//             echo json_encode(['error' => 'Invalid data: Missing fleet_action or asset_id']);
//             exit;
//         }

//         // Sanitize data
//         $fleet_action = strtolower(sanitize_text_field($data['fleet_action']));
//         $asset_id = sanitize_text_field($data['asset_id']);

//         // Handle based on fleet_action
//         if ($fleet_action === 'rollin') {
//             // Validate additional required fields for rollin
//             if (empty($data['equipment_name']) || empty($data['equipment_number']) || 
//                 empty($data['equipment_serial_number']) || empty($data['fleet_type'])) {
//                 header('HTTP/1.0 400 Bad Request');
//                 echo json_encode(['error' => 'Invalid data: Missing required fields for rollin']);
//                 exit;
//             }

//             // Sanitize additional data
//             $equipment_name = sanitize_text_field($data['equipment_name']);
//             $equipment_number = sanitize_text_field($data['equipment_number']);
//             $equipment_serial_number = sanitize_text_field($data['equipment_serial_number']);
//             $fleet_type = sanitize_text_field($data['fleet_type']);

//             // Check if product with this SKU already exists
//             $existing_product_id = $this->get_product_id_by_sku($asset_id);
//             if ($existing_product_id) {
//                 header('HTTP/1.0 400 Bad Request');
//                 echo json_encode(['error' => 'Product with this SKU already exists']);
//                 exit;
//             }

//             // Create WooCommerce product
//             $product = new WC_Product_Simple();
            
//             // Set product name and SKU
//             $product->set_name($equipment_name);
//             $product->set_sku($asset_id);
            
//             // Set regular price
//             $product->set_regular_price(0);
            
//             // Set stock quantity
//             $product->set_manage_stock(true);
//             $product->set_stock_quantity(1);
//             $product->set_stock_status('instock');
            
//             // Set product meta data
//             $product->update_meta_data('equipment_number', $equipment_number);
//             $product->update_meta_data('equipment_serial_number', $equipment_serial_number);
            
//             // Save the product
//             $product_id = $product->save();

//             // Set the product category
//             if ($product_id) {
//                 // Get or create the "Gmmco" category
//                 $term = term_exists('Gmmco', 'product_cat');
//                 if (!$term) {
//                     $term = wp_insert_term('Gmmco', 'product_cat');
//                 }
                
//                 if (!is_wp_error($term)) {
//                     wp_set_object_terms($product_id, $term['term_id'], 'product_cat');
//                 }

//                 // Add fleet_type as purpose_of_listing post meta
//                 update_post_meta($product_id, 'purpose_of_listing', $fleet_type);

//                 header('Content-Type: application/json');
//                 echo json_encode([
//                     'success' => true,
//                     'message' => 'Product created successfully',
//                     'product_id' => $product_id
//                 ]);
//             } else {
//                 header('HTTP/1.0 500 Internal Server Error');
//                 echo json_encode(['error' => 'Failed to create product']);
//             }

//         } elseif ($fleet_action === 'rollout') {
//             // Find product by SKU (asset_id)
//             $product_id = $this->get_product_id_by_sku($asset_id);
            
//             if (!$product_id) {
//                 header('HTTP/1.0 404 Not Found');
//                 echo json_encode(['error' => 'Product not found with provided asset_id']);
//                 exit;
//             }

//             // Move product to trash
//            // $result = wp_trash_post($product_id);

//              // Set the product category
//              if ($product_id) {
//                 // Get or create the "Gmmco" category
//                 $term = term_exists('Rolled Out', 'product_cat');
//                 if (!$term) {
//                     $term = wp_insert_term('Rolled Out', 'product_cat');
//                 }
                
//                 if (!is_wp_error($term)) {
//                     wp_set_object_terms($product_id, $term['term_id'], 'product_cat');
//                 }

//                 // Add fleet_type as purpose_of_listing post meta
//                // update_post_meta($product_id, 'purpose_of_listing', $fleet_type);

//                 header('Content-Type: application/json');
//                 echo json_encode([
//                     'success' => true,
//                     'message' => 'Product Rolled Out successfully',
//                     'product_id' => $product_id
//                 ]);
//             } else {
//                 header('HTTP/1.0 500 Internal Server Error');
//                 echo json_encode(['error' => 'Failed to Roll Out product']);
//             }

//         } else {
//             header('HTTP/1.0 400 Bad Request');
//             echo json_encode(['error' => 'Invalid fleet_action. Must be either "rollin" or "rollout"']);
//         }

//         exit;
//     }
// }

// // Initialize the endpoint
// new SAP_Fleet_Endpoint();


if (!defined('ABSPATH')) exit; // Exit if accessed directly

class SAP_Fleet_Endpoint {

    // Constructor to register the custom endpoint
    public function __construct() {
        add_action('init', [$this, 'register_custom_endpoint']);
    }

    // Register a custom endpoint
    public function register_custom_endpoint() {
        add_rewrite_rule('^sap-fleet-endpoint/?$', 'index.php?sap_fleet_endpoint=1', 'top');
        add_filter('query_vars', [$this, 'add_query_var']);
        add_action('template_redirect', [$this, 'handle_custom_endpoint']);
    }

    // Add custom query var for detection
    public function add_query_var($query_vars) {
        $query_vars[] = 'sap_fleet_endpoint';
        return $query_vars;
    }

    // Helper function to find product by SKU
    private function get_product_id_by_sku($asset_id) {
        global $wpdb;
        $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $asset_id));
        return $product_id;
    }

    // Helper function to find product by equipment_number
    private function get_product_id_by_equipment_number($equipment_number) {
        global $wpdb;
        $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='equipment_number' AND meta_value='%s' LIMIT 1", $equipment_number));
        return $product_id;
    }

    // Create or update product attributes
    private function set_product_attributes($product_id, $attributes) {
        $product_attributes = array();
        
        foreach ($attributes as $key => $value) {
            $attribute_name = wc_sanitize_taxonomy_name($key);
            $taxonomy = 'pa_' . $attribute_name;
            
            // Check if the attribute taxonomy exists, if not create it
            if (!taxonomy_exists($taxonomy)) {
                register_taxonomy(
                    $taxonomy,
                    'product',
                    array(
                        'label' => ucfirst($key),
                        'rewrite' => array('slug' => $attribute_name),
                        'hierarchical' => true,
                    )
                );
            }
            
            // Get or create the term
            $term_slug = sanitize_title($value);
            $term = get_term_by('slug', $term_slug, $taxonomy);
            
            if (!$term) {
                $term = wp_insert_term($value, $taxonomy);
                if (is_wp_error($term)) {
                    continue;
                }
                $term_id = $term['term_id'];
            } else {
                $term_id = $term->term_id;
            }
            
            // Add the attribute to the product
            wp_set_object_terms($product_id, $term_id, $taxonomy);
            
            $product_attributes[$taxonomy] = array(
                'name' => $taxonomy,
                'value' => $value,
                'is_visible' => 1,
                'is_variation' => 0,
                'is_taxonomy' => 1
            );
        }
        
        // Update product attributes
        update_post_meta($product_id, '_product_attributes', $product_attributes);
    }

    // Handle requests to the custom endpoint
    public function handle_custom_endpoint() {
        if (get_query_var('sap_fleet_endpoint') != 1) {
            return;
        }

        // Basic Auth credentials
        $valid_username = 'RMS_USER'; // Replace with your username
        $valid_password = 'RMS_iTroSys123!'; // Replace with your password

        // Basic Authentication
        if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
            header('WWW-Authenticate: Basic realm="Restricted Area"');
            header('HTTP/1.0 401 Unauthorized');
            echo json_encode(['error' => 'Unauthorized: Missing credentials']);
            exit;
        }

        if ($_SERVER['PHP_AUTH_USER'] !== $valid_username || $_SERVER['PHP_AUTH_PW'] !== $valid_password) {
            header('HTTP/1.0 401 Unauthorized');
            echo json_encode(['error' => 'Unauthorized: Invalid credentials']);
            exit;
        }

        // Parse the incoming data
        $request_body = file_get_contents('php://input');
        $data = json_decode($request_body, true);

        // Validate required fields
        if (empty($data['fleet_action']) || empty($data['equipment_number'])) {
            header('HTTP/1.0 400 Bad Request');
            echo json_encode(['error' => 'Invalid data: Missing fleet_action or Ewuipment Number']);
            exit;
        }

        // Sanitize data
        $fleet_action = strtolower(sanitize_text_field($data['fleet_action']));
        $equipment_number = sanitize_text_field($data['equipment_number']);
        // Validate additional required fields for rollin
            // if (empty($data['equipment_name']) || empty($data['asset_id']) || 
            //     empty($data['equipment_serial_number']) || empty($data['fleet_type']) || 
            //     empty($data['product_region']) || empty($data['model_number']) || empty($data['sale_price']) || 
            //     empty($data['yard_location']) || empty($data['acquisition_date']) || 
            //     empty($data['acquisition_cost']) || empty($data['registration_number'] ||
            //     empty($data['status']) || empty($data['st3/st5']) ||
            //     empty($data['machine_serialno']) || empty($data['current_location'])))
            //      {            
            //     header('HTTP/1.0 400 Bad Request');
            //     echo json_encode(['error' => 'Invalid data: Missing required fields for rollin']);
            //     exit;
            // }

           
        // Handle based on fleet_action
        if ($fleet_action === 'rollin') {           
            if (empty($data['equipment_name']) || 
            empty($data['asset_id']) || 
            empty($data['fleet_type']) || 
            empty($data['yard_location']) ||
            empty($data['current_location']))
             {            
            header('HTTP/1.0 400 Bad Request');
            echo json_encode(['error' => 'Invalid data: Missing required fields for rollin']);
            exit;
        }
            // Sanitize additional data
            $equipment_name = sanitize_text_field($data['equipment_name']);
            $asset_id = sanitize_text_field($data['asset_id']);
            $equipment_serial_number = sanitize_text_field($data['equipment_serial_number']);
            $fleet_type = sanitize_text_field($data['fleet_type']);
            $product_region = sanitize_text_field($data['product_region']);
            $model_number = sanitize_text_field($data['model_number']);
            $yard_location = sanitize_text_field($data['yard_location']);
            $acquisition_date = sanitize_text_field($data['acquisition_date']);
            $acquisition_cost = sanitize_text_field($data['acquisition_cost']);
            $registration_number = sanitize_text_field($data['registration_number']);
            $status = sanitize_text_field($data['status']);
            $st3_st5 = sanitize_text_field($data['st3/st5']);
            $machine_serialno = sanitize_text_field($data['machine_serialno']);
           // $brand = sanitize_text_field($data['brand']);             
            $current_location = sanitize_text_field($data['current_location']);
            $sale_price = sanitize_text_field($data['sale_price']);


            
            // New fields
            // $yard_location = !empty($data['yard_location']) ? strtoupper(sanitize_text_field($data['yard_location'])) : '';
            // $current_location = !empty($data['current_location']) ? sanitize_text_field($data['current_location']) : '';

            // First check if product with equipment_number exists
            $existing_product_id = $this->get_product_id_by_equipment_number($equipment_number);
            
            if ($existing_product_id) {

                // Add fleet_type as purpose_of_listing post meta
                // update_post_meta($product_id, 'purpose_of_listing', $fleet_type);
                // Check if product with this SKU already exists
                $existing_sku_product_id = $this->get_product_id_by_sku($asset_id);
                // Update existing product
                $product = wc_get_product($existing_product_id);

                if (!empty($existing_sku_product_id)) {
                    // Notify customer but proceed with creating product with modified SKU
                    $sku_to_use = $asset_id.'-5';
                    if($existing_product_id !==  $existing_sku_product_id)
                      {
                         $product->set_sku($sku_to_use);
                         $response['notification'] = 'Product with SKU ' . $asset_id . ' already exists. Updated with SKU ' . $sku_to_use;

                      }else{
                        $product->set_sku($asset_id);                
                        }
                }
                else{
                    $product->set_sku($asset_id);                
                    }
                    if (!empty($response)) {
                    echo json_encode($response);
                    }                
                
                // Update product name
                $product->set_name($equipment_name);
                
                
                if($fleet_type === 'Sales'){
                    $product->set_regular_price($sale_price);
                    }
                    else{
                    $product->set_regular_price(0);    
                    }

                $product->set_status('draft');

                // Update meta data
                $product->update_meta_data('equipment_number', $equipment_number);
                $product->update_meta_data('equipment_serial_number', $equipment_serial_number);
                $product->update_meta_data('purpose_of_listing', $fleet_type);
                $product->update_meta_data('_product_region',$product_region);
                $product->update_meta_data('model_number',$model_number);
                $product->update_meta_data('yard_location',$yard_location);
                $product->update_meta_data('acquisition_date',$acquisition_date);
                $product->update_meta_data('acquisition_cost',$acquisition_cost);
                $product->update_meta_data('registration_number',$registration_number);
                $product->update_meta_data('status',$status);
                $product->update_meta_data('st3st5_field',$st3_st5);
                $product->update_meta_data('machine_serialno',$machine_serialno);
               // $product->update_meta_data('brand',$brand);                
                $product->update_meta_data('current_location', $current_location);

                // Save the product
                $product_id = $product->save();
                
                // Set the product category
                if ($product_id) {
                    $category_name = 'Gmmco';
                    $taxonomy = 'product_cat';
                
                    // Get the category by name
                    $term = get_term_by('name', $category_name, $taxonomy);
                
                    if (!$term) {
                        // Category doesn't exist, so create it
                        $term = wp_insert_term($category_name, $taxonomy);
                
                        // Ensure we get the correct term ID after inserting
                        if (!is_wp_error($term)) {
                            $term_id = $term['term_id'];
                        } else {
                            echo "Error creating category: " . $term->get_error_message();
                            return;
                        }
                    } else {
                        // Category exists, fetch the term ID
                        $term_id = $term->term_id;
                    }
                
                    // Assign the category to the product
                    wp_set_object_terms($product_id, [$term_id], $taxonomy);
                
                
                    // Add attributes
                    $attributes = array(
                        'yard-location' => $yard_location,
                        'purpose-of-listing' => $fleet_type
                    );
                    $this->set_product_attributes($product_id, $attributes);

                    // Add fleet_type as purpose_of_listing post meta
                    update_post_meta($product_id, 'purpose_of_listing', $fleet_type);

                    header('Content-Type: application/json');
                    $response = [
                        'success' => true,
                        'message' => 'Product Updated successfully',
                        'product_id' => $product_id
                    ];

                    // Add notification if SKU was modified
                    // if ($existing_sku_product_id !== $existing_product_id) {
                    //     $response['notification'] = 'Product with SKU ' . $asset_id . ' already exists. Created with SKU ' . $sku_to_use;
                    // }
                    echo json_encode($response);

                } else {
                    header('HTTP/1.0 500 Internal Server Error');
                    echo json_encode(['error' => 'Failed to update product']);
                }
              

            } else {   
               
                // Create WooCommerce product
                $product = new WC_Product_Simple();
                
                $product->set_name($equipment_name);
                // Set regular price
                if($fleet_type === 'Sales'){
                $product->set_regular_price($sale_price);
                }
                else{
                $product->set_regular_price(0);
                }
                // Set stock quantity
                $product->set_manage_stock(true);
                $product->set_stock_quantity(1);
                $product->set_stock_status('instock');
                
                $product->set_status('draft');

                // Set product meta data
               $product->update_meta_data('equipment_number', $equipment_number);
                $product->update_meta_data('equipment_serial_number', $equipment_serial_number);
                $product->update_meta_data('purpose_of_listing', $fleet_type);
                $product->update_meta_data('_product_region',$product_region);
                $product->update_meta_data('model_number',$model_number);
                $product->update_meta_data('yard_location',$yard_location);
                $product->update_meta_data('acquisition_date',$acquisition_date);
                $product->update_meta_data('acquisition_cost',$acquisition_cost);
                $product->update_meta_data('registration_number',$registration_number);
                $product->update_meta_data('status',$status);
                $product->update_meta_data('st3st5_field',$st3_st5);
                $product->update_meta_data('machine_serialno',$machine_serialno);
               // $product->update_meta_data('brand',$brand);                
                $product->update_meta_data('current_location', $current_location);
                 
                      // Check if product with this SKU already exists
                      $existing_sku_product_id = $this->get_product_id_by_sku($asset_id);   

                      // Set product name and SKU
                  if (!empty($existing_sku_product_id)) {
                      // Notify customer but proceed with creating product with modified SKU
                      $sku_to_use = $asset_id.'-5';
                      if($product_id !==  $existing_sku_product_id)
                        {
                           $product->set_sku($sku_to_use);
                           $response['notification'] = 'Product with SKU ' . $asset_id . ' already exists. Created with SKU ' . $sku_to_use;
  
                        }else{
                          $product->set_sku($asset_id);                
                          }
                  }
                  else{
                      $product->set_sku($asset_id); 
                 
                      }
                      if (!empty($response)) {
                      echo json_encode($response);
                      }
                // Save the product
                $product_id = $product->save();

                // Set the product category
                if ($product_id) {               

                    $category_name = 'Gmmco';
                    $taxonomy = 'product_cat';

                    // Get the category by name
                   $term = get_term_by('name', $category_name, $taxonomy);

                   if (!$term) {
                   // Category doesn't exist, so create it
                   $term = wp_insert_term($category_name, $taxonomy);

                   // Ensure we get the correct term ID after inserting
                   if (!is_wp_error($term)) {
                      $term_id = $term['term_id'];
                    } else {
                       echo "Error creating category: " . $term->get_error_message();
                      return;
                        }
                     } else {
                       // Category exists, fetch the term ID
                       $term_id = $term->term_id;
                    }

                        // Assign the category to the product
                       wp_set_object_terms($product_id, [$term_id], $taxonomy);
                
                    // Add attributes
                    $attributes = array(
                        'yard-location' => $yard_location,
                        'purpose-of-listing' => $fleet_type
                    );
                    $this->set_product_attributes($product_id, $attributes);

                    // Add fleet_type as purpose_of_listing post meta
                    update_post_meta($product_id, 'purpose_of_listing', $fleet_type);

                    header('Content-Type: application/json');
                    $response = [
                        'success' => true,
                        'message' => 'Product created successfully',
                        'product_id' => $product_id
                    ];
                    
                    echo json_encode($response);

                } else {
                    header('HTTP/1.0 500 Internal Server Error');
                    echo json_encode(['error' => 'Failed to create product']);
                }
            }

        } elseif ($fleet_action === 'rollout') {

            if (empty($data['asset_id']))
             {            
            header('HTTP/1.0 400 Bad Request');
            echo json_encode(['error' => 'Invalid data: Missing required fields for rollout']);
            exit;
             }
            
             $asset_id = sanitize_text_field($data['asset_id']);

            // First check if product with asset_id as SKU exists
            $product_sku_id = $this->get_product_id_by_sku($asset_id);
            $product_id = $this->get_product_id_by_equipment_number($equipment_number);

            if (empty($product_id) && empty($product_sku_id)) {
                 
                    header('HTTP/1.0 500 Internal Server Error');
                    echo json_encode(['error' => 'No Product found with Equipment number and Asset ID']);
                    exit;

                 }elseif(empty($product_id)) {
                    header('HTTP/1.0 500 Internal Server Error');
                    echo json_encode(['error' => 'No Product found with Equipment number']);
                    exit;
                 
                } elseif ($product_id) {
                // Get or create the "Rolled Out" category
                    $category_name = 'Rolled Out'; // Your category name
                    $taxonomy = 'product_cat';

                    // Get the category by name
                   $term = get_term_by('name', $category_name, $taxonomy);

                   if (!$term) {
                   // Category doesn't exist, so create it
                   $term = wp_insert_term($category_name, $taxonomy);

                   // Ensure we get the correct term ID after inserting
                   if (!is_wp_error($term)) {
                      $term_id = $term['term_id'];
                    } else {
                       echo "Error creating category: " . $term->get_error_message();
                      return;
                        }
                     } else {
                       // Category exists, fetch the term ID
                       $term_id = $term->term_id;
                    }

                        // Assign the category to the product
                       wp_set_object_terms($product_id, [$term_id], $taxonomy);


                header('Content-Type: application/json');
                echo json_encode([
                    'success' => true,
                    'message' => 'Product Rolled Out successfully',
                    'product_id' => $product_id
                ]);
            } else {
                header('HTTP/1.0 500 Internal Server Error');
                echo json_encode(['error' => 'Failed to Roll Out product']);
            }

        } else {
            header('HTTP/1.0 400 Bad Request');
            echo json_encode(['error' => 'Invalid fleet_action. Must be either "rollin" or "rollout"']);
        }

        exit;
    }
}

// Initialize the endpoint
new SAP_Fleet_Endpoint();

Youez - 2016 - github.com/yon3zu
LinuXploit