HEX
Server: Apache/2.2.15 (CentOS)
System: Linux ip-10-0-2-146.eu-west-1.compute.internal 2.6.32-754.35.1.el6.centos.plus.x86_64 #1 SMP Sat Nov 7 11:33:42 UTC 2020 x86_64
User: root (0)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: //www/exchange0old/cmws/scripts/sync-org.php
<?php
// script for syncing existing Organisation data with Role Mapping from Exchange 2.0 to Credebt Machine

if (PHP_SAPI !== "cli") {
    echo "The script should be run only from CLI.";
    exit;
}

$memoryLimit = '1512M';
if (ini_set('memory_limit', $memoryLimit) === false) {
    echo "Unable to set the required memory limit: " . $memoryLimit . ".\n";
    exit;
}

if (is_null($argv)) {
    echo "Please enable \$argv variable in PHP configuration.";
    exit;
}

// required includes
require_once("../../exchange/Bin/constants.php");
require_once("../../exchange/Bin/init.php");
require_once(ROOT_FOLDER . "Bin/Security.php");
require_once(ROOT_FOLDER . "admin/classes/Model/base_actions.php");
require_once(ROOT_FOLDER . "admin/classes/Model/CMSync.php");

$cmSync = new CMSync(MYSQL_ENGINE_IMPROVED);
if (!$cmSync->cmSyncOrgEnable) {
    echo "Organisation Sync is disabled. Please first enable Organisation Sync in Exchange 2.0 Site Settings.\n";
    exit;
}

if (in_array(CM_SYNC_SCRIPT_ARG_CLEANLOG, $argv, true) === true) {
    $cmSync->cleanLogEvents($argv[0]);
    echo "Log Events have been removed from database.\n";
    exit;
}

$liveSync = true;
if (in_array(CM_SYNC_SCRIPT_ARG_PRECHECK, $argv, true) === true) {
    $liveSync = false;
    echo "The script will ONLY perform a remote database entity check without any real data sync.\n";
}

$entitiesArgArr = $argv;
array_shift($entitiesArgArr);
array_walk($entitiesArgArr, function (&$value) { $value = ucfirst(strtolower($value)); });

// define data entities to sync
$allDataEntitiesArr = array(
    CM_SYNC_ORG_ROLE_ORIGINATOR,
    CM_SYNC_ORG_ROLE_DEBTOR,
    CM_SYNC_ORG_ROLE_CREDITOR,
    CM_SYNC_ORG_ROLE_INTERMEDIARY,
    CM_SYNC_ORG_ROLE_INVESTOR
);

$entityIdArray = array(
    CM_SYNC_ORG_ROLE_ORIGINATOR => null,
    CM_SYNC_ORG_ROLE_DEBTOR => null,
    CM_SYNC_ORG_ROLE_CREDITOR => null,
    CM_SYNC_ORG_ROLE_INTERMEDIARY => null,
    CM_SYNC_ORG_ROLE_INVESTOR => null
);

$syncDataEntitiesArr = array();
foreach ($entitiesArgArr as $entityVal) {
    if (($pos = strpos($entityVal, ":")) !== false) {
        $entityValArr = explode(":", $entityVal);
        array_walk($entityValArr, function (&$value) { $value = trim($value); });
        list($valCheck, $idToSync) = $entityValArr;
    } else {
        $valCheck = $entityVal;
        $idToSync = null;
    }

    if (in_array($valCheck, $allDataEntitiesArr, true) === true) {
        $syncDataEntitiesArr[] = $valCheck;
        if (!is_null($idToSync)) {
            $entityIdArray[$valCheck] = $idToSync;
        }
    } elseif (strtolower($valCheck) !== CM_SYNC_SCRIPT_ARG_CLEANLOG
        &&  strtolower($valCheck) !== CM_SYNC_SCRIPT_ARG_PRECHECK) {
        echo "Incorrect command argument: " . strtolower($valCheck) . "\n";
        exit;
    }
    // by default all Organisations are synced
}

if (count($syncDataEntitiesArr) === 0) {
    $syncDataEntitiesArr = $allDataEntitiesArr;
}
echo $cmSync->showMemoryUsage();

if ($cmSync->initLogFile(CM_SYNC_ORG) === false) {
    echo "Log File write error: Unable to create log file. Please check folder permissions.\n";
    exit;
}

foreach ($syncDataEntitiesArr as $dataEntity) {
    echo "\nSyncing Entity Data for: ". $dataEntity . "\n";
    $orgSyncList = $cmSync->getOrganisationListForSync($dataEntity, $entityIdArray[$dataEntity]);
    $countList = count($orgSyncList);
    $counter = 0;
    echo "Record(s) to sync: " . $countList . "\n";
    echo $cmSync->showMemoryUsage();

    foreach ($orgSyncList as $orgEntry) {
        echo "\nChecking " . $dataEntity . " ". ++$counter ." out of ". $countList . "; Ref. ID: " . $orgEntry['exchange_ref_id'] . " Name: " .
            $orgEntry['organisation_name'] . "\n";

            $badString = filter_var($orgEntry['organisation_name'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
            $badString = trim(strip_tags(mb_convert_encoding(utf8_encode($badString), 'UTF-8', 'UTF-8')));
            $orgEntry['organisation_name'] = $badString;

        // search for record using all possible and available primary matching attributes
        $filterArr = array(
            "vat_number" => null,
            "registration_number" => !empty($orgEntry['registration_number']) ?
                trim($orgEntry['registration_number']) : null,
            "trade_name" => !empty($orgEntry['trade_name']) ? trim($orgEntry['trade_name']) : null,
            "organisation_name" => trim($orgEntry['organisation_name']),
            "country_id" => !empty($orgEntry['country_id']) ? $orgEntry['country_id'] : null
        );

        $searchArr = array("filter_data" => array_filter($filterArr));

        // search and find the best matching Organisation record
        $matchedResultArr = $cmSync->matchOrganisationRecord($searchArr, $orgEntry,
            $searchResultArr = $cmSync->findOrgInstancesInCMDB($searchArr));

        $roleFound = false;
        if (is_null($searchResultArr) || is_null($matchedResultArr)) {
            $totalFound = 0;
        } else {
            $totalFound = (int)$searchResultArr['total'];
            if (!is_null($matchedResultArr['cm_id_role_mapping'])) {
                $roleFound = true;
            }
        }
        echo "Found " . $totalFound . " possibly matching result(s) in Credebt Machine Database\n";
        if ($roleFound === true) {
            echo "Found matching Role Mapping in Credebt Machine Database\n";
        }
        echo "Syncing... \t",
            ($cmSync->syncOrganisationRecord($matchedResultArr, $orgEntry, $liveSync) === true ? "Success\n" : "Error\n");
        unset($filterArr, $searchArr);
        echo "\n" . $cmSync->showMemoryUsage();
    }
    unset($orgSyncList);
}