File: /www/exchange0old/cmws/scripts/sync-people.php
<?php
// script for syncing existing People 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->cmSyncPeopleEnable) {
echo "People Sync is disabled. Please first enable People 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_PEOPLE_ROLE_INVESTOR
);
$entityIdArray = array(
CM_SYNC_PEOPLE_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 People (investors) are synced
}
if (count($syncDataEntitiesArr) === 0) {
$syncDataEntitiesArr = $allDataEntitiesArr;
}
echo $cmSync->showMemoryUsage();
if ($cmSync->initLogFile(CM_SYNC_PEOPLE) === 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";
$peopleSyncList = $cmSync->getPeopleListForSync($dataEntity, $entityIdArray[$dataEntity]);
$countList = count($peopleSyncList);
$counter = 0;
echo "Record(s) to sync: " . $countList . "\n";
echo $cmSync->showMemoryUsage();
foreach ($peopleSyncList as $peopleEntry) {
echo "\nChecking " . $dataEntity . " ". ++$counter ." out of ". $countList . "; Ref. ID: " .
$peopleEntry['exchange_ref_id'] . " Name: " . $peopleEntry['people_name'] . "\n";
$badString = filter_var($peopleEntry['people_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')));
$peopleEntry['people_name'] = $badString;
$peopleEntry['first_name'] = filter_var($peopleEntry['first_name'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
$peopleEntry['first_name'] = trim(strip_tags(mb_convert_encoding(utf8_encode($peopleEntry['first_name']), 'UTF-8', 'UTF-8')));
$peopleEntry['last_name'] = filter_var($peopleEntry['last_name'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
$peopleEntry['last_name'] = trim(strip_tags(mb_convert_encoding(utf8_encode($peopleEntry['last_name']), 'UTF-8', 'UTF-8')));
/* search for record using all possible and available primary matching attributes */
$searchArr = array("filter_data" => array(
"people_name" => $peopleEntry['people_name']
));
// search and find the best matching People record
$matchedResultArr = $cmSync->matchPeopleRecord($searchArr, $peopleEntry,
$searchResultArr = $cmSync->findPeopleInstancesInCMDB($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->syncPeopleRecord($matchedResultArr, $peopleEntry, $liveSync) === true ? "Success\n" : "Error\n");
unset($searchArr);
echo "\n" . $cmSync->showMemoryUsage();
}
unset($peopleSyncList);
}