HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux bsx-1-dev 6.8.0-101-generic #101-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 9 10:15:05 UTC 2026 x86_64
User: www-data (33)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/bsx/ass/admin/import.php
<?php


add_filter( 'wp_mail', 'kad_import' );

class Import {
    private $strct = [];
    
    public function __construct() {
        $this->strct = array(
            array('label'=>'Legal Name (Company)', 'name'=>'legal_name', 'where'=>'acf'),
            array('label'=>'Shortname', 'name'=>'shortname', 'where'=>'acf'),
            array('label'=>'Sector', 'name'=>'sector', 'where'=>'acf'),
            array('label'=>'Industry', 'name'=>'industry', 'where'=>'acf'),
            array('label'=>'Region1', 'name'=>'region1', 'where'=>'tax'),
            array('label'=>'Region2', 'name'=>'region2', 'where'=>'(tax)'),
            array('label'=>'ysym', 'name'=>'ysym', 'where'=>'acf'),
            array('label'=>'Benchmark Name', 'name'=>'ben', 'where'=>'arc'),
            array('label'=>'Exchange', 'name'=>'exchange2', 'where'=>'acf'),
            array('label'=>'Currency', 'name'=>'currency', 'where'=>'acf'),
            array('label'=>'Market Cap', 'name'=>'market_cap', 'where'=>'acf'),
            array('label'=>'Employees', 'name'=>'employees', 'where'=>'acf'),
            array('label'=>'Address', 'name'=>'address', 'where'=>'acf'),
            array('label'=>'Website', 'name'=>'website', 'where'=>'acf'),
            array('label'=>'Company Logo', 'name'=>'company_img', 'where'=>'img'),
            array('label'=>'Cassandra Name', 'name'=>'cassandra_name', 'where'=>'acf'),
            array('label'=>'Cassandra ID', 'name'=>'cass_id', 'where'=>'acf'),
            array('label'=>'Campaign Status', 'name'=>'campaign-status', 'where'=>'tax'),
            array('label'=>'Campaign Type', 'name'=>'campaign-type', 'where'=>'tax'),
            array('label'=>'Publish Date', 'name'=>'pub_date', 'where'=>'acf'),
            array('label'=>'Publish Time', 'name'=>'pub_time', 'where'=>'acf'),
            array('label'=>'Entry Date', 'name'=>'entry_date', 'where'=>'acf'),
            array('label'=>'Entry Time', 'name'=>'entry_time', 'where'=>'acf'),
            array('label'=>'Entry Price', 'name'=>'entry_price', 'where'=>'acf'),
            array('label'=>'Link to Report', 'name'=>'website_report', 'where'=>'acf'),
            array('label'=>'Follow-up', 'name'=>'fup', 'where'=>'acf'),
            array('label'=>'pdf', 'name'=>'pdf', 'where'=>'acf'),
            array('label'=>'Summary (Report)', 'name'=>'report_summary', 'where'=>'acf')
        );
    }    

    
    public function extract($msg) { 
        $msg = str_replace("\r", " ", $msg);
        $x = explode('<br>',nl2br($msg,false));
        error_log(print_r($x, true));

        $vars = [];
        foreach($x as $y) {
            $y = trim($y);
            if($y!='') {
                if(str_contains($y,':')) {
                    if(isset($v)) $vars[] = $v;
                    $v = $y;
                } else {
                    $v .= (' ' . $y);
                }
            }
        }
        $vars[] = $v;

        error_log(print_r($vars, true));

        $imported = [];
        $labels   = array_column($this->strct,'label');
        $names    = array_column($this->strct,'name');
        for($i = 0; $i < count($labels); $i++) {
            $pattern = preg_quote($labels[$i]);
            foreach($vars as $v) {
                $x = preg_match('/(?<=' . $pattern . ':).*$/', $v, $matches);
                if(empty($x)) continue;
                $imported[$names[$i]] = implode(';',$matches);
            }
        }
        error_log(print_r($imported,true));
        return $imported;
    }

    
}





class Cntr {
    public $postdata = [];
    public $id;
    public function __construct($vars) {
        $this->postdata = array(
            'post_type'   => 'contract',
            'post_title'  => $vars['ysym'],
            'post_status' => 'publish'
        );
        $this->id = wp_insert_post($this->postdata);

        update_field("field_6910dc9cfa018", $vars['ysym'], $this->id);

        $symex = explode('.', $vars['ysym']);
        update_field("field_6910dc5dfa014", $symex[0], $this->id);
        update_field("field_6910dc7dfa016", $symex[0], $this->id);

        $exch = $symex[1] ?? '';
        update_field("field_6910dc6ffa015", $exch, $this->id);
        update_field("field_6910dc89fa017", $vars['exchange2'], $this->id);

        update_field("field_69134fa4fd67f", $vars['shortname'], $this->id);
        update_field("field_6910dcddfa01a", $vars['currency'], $this->id);

        return $this->id;        
    }
}



class Comp {
    public $postdata = [];
    public $id;
    public function __construct($vars, $ids) {
        $tax_input = array(
            'region'         => [$vars['region1'], $vars['region2']],
        );

        $this->postdata = array(
            'post_type'   => 'company',
            'post_title'  => $vars['legal_name'],
            'post_status' => 'publish',
            'tax_input'   => $tax_input
        );
        $this->id = wp_insert_post($this->postdata);

        update_field("field_68f5ed79355e7", $vars['legal_name'], $this->id);
        update_field("field_68f60646f47a6", $vars['sector'], $this->id);
        update_field("field_68f6065df47a7", $vars['industry'], $this->id);
        update_field("field_68f5ed9c355e8", floatval($vars['market_cap']), $this->id);
        update_field("field_6905465a19443", intval($vars['employees']), $this->id);
        update_field("field_68f5edb9355e9", $vars['website'], $this->id);
        update_field("field_68f6033772f20", $vars['address'], $this->id);

        update_field("field_69191ff1b9bad", $ids['cntr_id'], $this->id);
        
        
        $image = media_sideload_image(sanitize_url($vars['company_img']),0,
                                      $vars['shortname'],'id' );
        error_log(print_r($image,true));
        set_post_thumbnail( $this->id, $image );

        return $this->id;        
    }
}


class Camp {
    public $postdata = [];
    public $id;
    public function __construct($vars,$ids) {

        $tax_input = array(
            'campaign-status'=> [$vars['campaign-status']],
            'campaign-type'  => [$vars['campaign-type']]
        );

        $this->postdata = array(
            'post_type'   => 'campaign',
            'post_title'  => $vars['cassandra_name'] . " on $" . $vars['ysym'],
            'post_status' => 'publish',
            'tax_input'   => $tax_input
        );
        $this->id = wp_insert_post($this->postdata);

        $pub_date = $vars['pub_date'] . ' ' . $vars['pub_time'];
        update_field("field_68f60918953d2", $pub_date, $this->id);
        $entry_date = $vars['entry_date'] . ' ' . $vars['entry_time'];
        update_field("field_68f7ba3394c46", $entry_date, $this->id);
        update_field("field_68f6097a953d4", $vars['entry_price'], $this->id);

        $benchmark = get_posts(
            array(
                'post_type' => 'contract',
                'meta_key'  => 'ysym',
                'meta_value' => $vars['ben'],
                'post_status' => 'publish',
                'posts_per_page' => 1
            )
        );

        update_field("field_691917214ea32", $ids['cass_id'], $this->id);
        update_field("field_691913c54d6bd", $ids['cntr_id'], $this->id);
        update_field("field_6919107e4d6b8", $ids['comp_id'], $this->id);
        update_field("field_691914444d6be", $benchmark[0]->ID,  $this->id);
        
        return $this->id;        
    }
}



class Rprt {
    public $postdata = [];
    public $id;
    public function __construct($vars,$ids) {

        $this->postdata = array(
            'post_type'   => 'report',
            'post_title'  => $vars['cassandra_name'] . " ./. $" . $vars['ysym'],
            'post_status' => 'publish',
        );
        $this->id = wp_insert_post($this->postdata);

        $pub_date = $vars['pub_date'] . ' ' . $vars['pub_time'];
        update_field("field_69305623ae910", $pub_date, $this->id);

        update_field("field_693055deae90f", $ids['camp_id'], $this->id);

        update_field("field_69305665ae911", $vars['report_summary'] ?? '', $this->id);

        if(isset(vars['pdf'])){
            $pdf = $vars['pdf'] == 1 ? true : false;
        } else {
            $pdf = false;
        }
        update_field("field_693c9b3fc8136", $pdf, $this->id);

        if(isset($vars["fup"])) {
            $fup = $vars['fup'] == 1 ? true : false;
        } else {
            $fup = false;
        }
        update_field("field_6930557dae90e", $fup, $this->id);

        update_field("field_693056b736926", $vars['website_report'] ?? '', $this->id);
        

        return $this->id;        
    }
}



        
            



function kad_import( $args ) {
    $imp = new Import();
    $vars= $imp->extract($args['message']);
    $cass_id = $vars['cass_id'];
    $cntr_id = new Cntr($vars);
    $ids = [
        'cntr_id'=>$cntr_id,
        'cass_id'=>$cass_id
    ];
    error_log(print_r($ids,true));
    $comp_id = new Comp($vars, $ids);
    $ids['comp_id'] = $comp_id;
    error_log(print_r($ids,true));
    $camp_id = new Camp($vars,$ids); 
    $ids['camp_id'] = $camp_id;
    error_log(print_r($ids,true));
    $rprt_id = new Rprt($vars, $ids);
    return $args;
}