Duffer Derek
<?php
/**
* Created by PhpStorm.
* User: jis
* Date: 30/3/17
* Time: 12:14 PM
*/
namespace App\Repository\Utils;
use App\Repository\General\DB;
use App\Repository\General\PodioConfig;
use App\Repository\Utils\ZipAndFileHandler;
use PDO;
use Podio;
use PodioSpace;
use PodioSpaceMember;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SimpleXMLElement;
use ZipArchive;
class TestRepo
{
private $customPodioAPI;
/**
* TestRepo constructor.
*/
public function __construct()
{
$this->customPodioAPI = new CustomPodioAPI();
$this->appId = '';
}
// http://logmein//test/{type}
public function testProcess($type, $param)
{
echo "<pre>";
switch ($type) {
case "test":
$this->test();
break;
}
}
public function testFunction()
{
$repo = new PodioAuthRepo();
if ($repo->authenticate(1)) {
$sql = "select `src_space_id`,`src_name`,`src_space_url` from `work_spaces`";
$STH = DB::prepare($sql);
$STH->execute();
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if ($result && count($result) > 0) {
foreach ($result as $space) {
echo "<pre>";
print_r($space);
$attributes = [
"name" => "OLD_" . $space["src_name"]
];
try {
PodioSpace::update((int)$space["src_space_id"], $attributes);
print_r($attributes);
} catch (\Exception $e) {
echo "<pre>";
print_r($e);
}
}
}
}
}
private function updateField()
{
$repo = new PodioAuthRepo();
if ($repo->authenticateSF(2)) {
$sql = "select `dest_app_id`,`dest_app_url` from `apps`";
$STH = DB::prepare($sql);
$STH->execute();
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if ($result && count($result) > 0) {
foreach ($result as $app) {
echo "<pre>";
$dateAttributes = [
"label" => "Item Created Date",
"hidden_create_view_edit" => TRUE,
"delta" => 100
];
$userAttributes = [
"label" => "Item Created User",
"hidden_create_view_edit" => TRUE,
"delta" => 99
];
try {
$fieldData = PodioAppField::get((int)$app["dest_app_id"], "item-created-date");
$dateFieldId = (int)$fieldData->field_id;
$fieldData = PodioAppField::get((int)$app["dest_app_id"], "item-created-user");
$userFieldId = (int)$fieldData->field_id;
} catch (\Exception $e) {
echo "<pre>";
print_r($e);
}
try {
PodioAppField::update((int)$app["dest_app_id"], $userFieldId, $userAttributes);
PodioAppField::update((int)$app["dest_app_id"], $dateFieldId, $dateAttributes);
} catch (\Exception $e) {
echo "<pre>";
print_r($e);
}
print_r($app["dest_app_url"]);
echo "<br>";
}
}
}
}
public function replaceExternalId(&$value)
{
if (strpos($value, 'item_value_') !== false) {
if (strpos($value, 'item_value_') !== false) {
preg_match_all('/item_value_([^*]*)/', $value, $match);
if ($match) {
foreach ($match[1] as $matchKey => $matchValue) {
if (strpos($matchValue, 'item_value_pfprepfield:') == false)
$value = str_replace($matchValue, TestRepo::getNewExternalId($matchValue), $value);
}
}
preg_match_all('/item_value_([^|]*)/', $value, $match);
if ($match) {
foreach ($match[1] as $matchKey => $matchValue) {
if (strpos($matchValue, 'item_value_pfprepfield:') == false)
$value = str_replace($matchValue, TestRepo::getNewExternalId($matchValue), $value);
}
}
}
// if (strpos($value, 'item_value_pfprepfield:') !== false) {
// preg_match_all('/item_value_pfprepfield:([^*]*)/', $value, $match);
// if ($match) {
// foreach ($match[1] as $matchKey => $matchValue) {
// $value = str_replace($matchValue, TestRepo::getNewExternalId($matchValue), $value);
// }
// }
// }
if (strpos($value, 'item_value_pfref:') !== false) {
preg_match_all('/item_value_pfref:([0-9]*)/', $value, $match);
if ($match) {
foreach ($match[1] as $matchKey => $matchValue) {
$value = str_replace($matchValue, TestRepo::getNewAppIds($matchValue), $value);
}
}
preg_match_all('/item_value_pfref:([0-9]*):([a-zA-Z-0-9]*)/', $value, $match);
if ($match) {
foreach ($match[2] as $matchKey => $matchValue) {
if ($matchValue !== "")
$value = str_replace($matchValue, TestRepo::getNewExternalIdWithApp($matchValue, $match[1][$matchKey]), $value);
}
}
}
if (strpos($value, 'item_value_pfdisplaypage') !== false) {
preg_match_all('/(\[\*item_value_pfdisplaypage:[[0-9]*\*\])/', $value, $match);
if ($match) {
foreach ($match[1] as $matchKey => $matchValue) {
$value = str_replace($matchValue, '', $value);
}
}
}
}
if (strpos($value, 'item_field') !== false) {
if (strpos($value, 'item_field_pfref') !== false) {
preg_match_all('/item_field_pfref:([0-9]*)/', $value, $match);
if ($match) {
foreach ($match[1] as $matchKey => $matchValue) {
$value = str_replace($matchValue, TestRepo::getNewAppIds($matchValue), $value);
}
}
}
}
}
public function test()
{
try {
echo "<pre>";
$dir = "/var/www/podiomigration.bitkit.dk/httpdocs/public/assets/flows/";
mkdir($dir);
$rootPath = realpath($dir);
$zip = new ZipArchive();
$zip->open("/var/www/podiomigration.bitkit.dk/httpdocs/public/assets/new.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
$getFilesAndDirectory = ZipAndFileHandler::GetFileList('/var/www/podiomigration.bitkit.dk/httpdocs/public/assets/flow-518455.xml.zip');
if ($getFilesAndDirectory) {
foreach ($getFilesAndDirectory['files'] as $fileKey => $file) {
$this->appId = '';
$fileName = basename($file);
$getXmlContents = $this->readXml($file);
$xmlArray = json_decode(json_encode((array)$getXmlContents), TRUE);
if (isset($xmlArray['flow'])) {
$flow = $xmlArray['flow'];
foreach ($flow as $key => $value) {
if (strpos(strtolower($key), 'app') !== false) {
if (!is_array($value)) {
if (preg_match('/^[0-9]+$/', $value)) {
$xmlArray['flow'][$key] = $this->getNewAppId($value);
}
}
if ($key == 'podioAppId') {
print_r($flow[$key]);
if ($flow[$key] != '')
$this->appId = $flow[$key];
else
$this->appId = null;
}
}
}
}
// echo "<br>";
if (isset($xmlArray['steps'])) {
foreach ($xmlArray['steps'] as $stepKey => $step) {
$stepArray = (unserialize(base64_decode($step['stepDetails'])));
print_r($stepArray);
echo "<br>";
array_walk_recursive($stepArray, array($this, 'replaceExternalId'));
// exit;
foreach ($stepArray as $key => $value) {
if (strpos(strtolower($key), 'field') !== false) {
if (!is_array($value)) {
if (preg_match('/^[0-9]+$/', $value)) {
$newValue = $this->getNewFieldId($value);
if ($newValue != $value && $newValue != '')
$stepArray[$key] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
} else {
foreach ($value as $insideKey => $insideVal) {
if (preg_match('/^[0-9]+$/', $insideVal)) {
$newValue = $this->getNewFieldId($insideVal);
if ($newValue != $value && $newValue != '')
$stepArray[$key][$insideKey] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
}
}
if (strpos(strtolower($key), 'app')) {
if (preg_match('/^[0-9]+$/', $value) !== false) {
$newValue = $this->getNewAppId($value);
if ($newValue != $value && $newValue != '')
$stepArray[$key] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
if (strpos(strtolower($key), 'user')) {
if (preg_match('/^[0-9]+$/', $value) !== false) {
$newValue = $this->getNewUserId($value);
if ($newValue != $value && $newValue != '')
$stepArray[$key] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
if (strpos(strtolower($key), 'view')) {
if (preg_match('/^[0-9]+$/', $value) !== false) {
$newValue = $this->getNewViewId($value);
if ($newValue != $value && $newValue != '')
$stepArray[$key] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
if ($key == "values") {
foreach ($stepArray["values"] as $valueKey => $keyVal) {
// print_r($stepArray["values"]);
// echo "<br>";
if (!is_array($keyVal)) {
if (strpos(strtolower($valueKey), 'field') !== false) {
if (preg_match('/^[0-9]+$/', $keyVal)) {
$newValue = $this->getNewFieldId($keyVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
if (strpos(strtolower($valueKey), 'app') !== false) {
if (preg_match('/^[0-9]+$/', $keyVal)) {
$newValue = $this->getNewAppId($keyVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
if (strpos(strtolower($valueKey), 'user') !== false) {
if (preg_match('/^[0-9]+$/', $keyVal)) {
$newValue = $this->getNewUserId($keyVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
if (strpos(strtolower($valueKey), 'view') !== false) {
if (preg_match('/^[0-9]+$/', $keyVal)) {
$newValue = $this->getNewViewId($keyVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey] = $newValue;
else {
unset($stepArray);
unset($xmlArray['steps'][$stepKey]);
continue;
}
}
}
} else {
foreach ($keyVal as $insideKey => $insideVal) {
if (!is_array($insideVal)) {
if (strpos(strtolower($insideKey), 'field') !== false) {
if (preg_match('/^[0-9]+$/', $insideVal)) {
$newValue = $this->getNewFieldId($insideVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey][$insideKey] = $newValue;
else {
unset($stepArray['values'][$valueKey]);
continue;
}
}
}
if (strpos(strtolower($insideKey), 'app') !== false) {
if (preg_match('/^[0-9]+$/', $insideVal)) {
$newValue = $this->getNewAppId($insideVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey][$insideKey] = $newValue;
else {
unset($stepArray['values'][$valueKey]);
continue;
}
}
}
if (strpos(strtolower($insideKey), 'user') !== false) {
if (preg_match('/^[0-9]+$/', $insideVal)) {
$newValue = $this->getNewUserId($insideVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey][$insideKey] = $newValue;
else {
unset($stepArray['values'][$valueKey]);
continue;
}
}
}
if (strpos(strtolower($insideKey), 'view') !== false) {
if (preg_match('/^[0-9]+$/', $insideVal)) {
$newValue = $this->getNewViewId($insideVal);
if ($newValue != $value && $newValue != '')
$stepArray['values'][$valueKey][$insideKey] = $newValue;
else {
unset($stepArray['values'][$valueKey]);
continue;
}
}
}
}
}
}
// print_r($stepArray["values"]);
// echo "<br>";
}
}
}
if (isset($stepArray)) {
print_r($stepArray);
echo "<br>";
$xmlArray['steps'][$stepKey]['stepDetails'] = base64_encode(serialize($stepArray));
// print_r(unserialize(base64_decode($xmlArray['steps'][$stepKey]['stepDetails'])));
}
}
}
exit;
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><root></root>');
$this->array_to_xml($xmlArray, $xml_data);
$xml_data->asXML("$dir/$fileName");
}
// exit;
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
// if (is_dir($getFilesAndDirectory['dir'])) {
// ZipAndFileHandler::recursiveDelete($getFilesAndDirectory['dir']);
// }
}
} catch (\Exception $e) {
print_r($e);
exit;
}
}
private function getNewID($key, $val)
{
}
private
function array_to_xml($data, &$xml_data)
{
foreach ($data as $key => $value) {
if (is_numeric($key)) {
$key = 'item' . $key; //dealing with <0/>..<n/> issues
}
if (is_array($value)) {
$subnode = $xml_data->addChild($key);
$this->array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key", htmlspecialchars("$value"));
}
}
}
// private function checkInArray($array)
// {
// if (is_array($array)) {
// foreach ($array as $key => $sub_array) {
// $result = $this->
// }
// }
// if (empty($array)) {
// return false;
// }
// return $array;
// }
public function getNewExternalId($src_field_external_id)
{
if ($this->appId != '') {
$sql1 = "select `dest_field_external_id` from `app_fields` WHERE `src_field_external_id`=:src_field_external_id AND `src_app_id` =:src_app_id";
$data = array("src_field_external_id" => $src_field_external_id, "src_app_id" => $this->appId);
} else {
$sql1 = "select `dest_field_external_id` from `app_fields` WHERE `src_field_external_id`=:src_field_external_id";
$data = array("src_field_external_id" => $src_field_external_id);
}
$STH1 = DB::prepare($sql1);
$STH1->execute($data);
$fieldResults = $STH1->fetchAll(PDO::FETCH_ASSOC);
return isset($fieldResults[0]) ? $fieldResults[0]['dest_field_external_id'] : $src_field_external_id;
}
public static function getNewExternalIdWithApp($src_field_external_id, $dest_app_id)
{
$sql1 = "select `dest_field_external_id` from `app_fields` WHERE `src_field_external_id`=:src_field_external_id AND `dest_app_id`=:dest_app_id";
$STH1 = DB::prepare($sql1);
$data = array("src_field_external_id" => $src_field_external_id, "dest_app_id" => $dest_app_id);
$STH1->execute($data);
$fieldResults = $STH1->fetchAll(PDO::FETCH_ASSOC);
return isset($fieldResults[0]) ? $fieldResults[0]['dest_field_external_id'] : $src_field_external_id;
}
public static function getNewAppIds($src_app_id)
{
$sql2 = "select dest_app_id from apps WHERE src_app_id=:src_app_id";
$STH2 = DB::prepare($sql2);
$data = array("src_app_id" => $src_app_id);
$STH2->execute($data);
$appResults = $STH2->fetchAll(PDO::FETCH_ASSOC);
return isset($appResults[0]) ? $appResults[0]['dest_app_id'] : $src_app_id;
}
private
function return_end_element($array)
{
$output = [];
array_walk_recursive($array, function (&$value, $key) use (&$output) {
if (!is_array($value)) {
if ($value) {
$output[$key] = $value;
}
}
});
return $output;
}
private
function getNewFieldId($src_field_id)
{
$sql1 = "select `dest_field_id`,`dest_field_external_id` from `app_fields` WHERE `src_field_id`=:src_field_id";
$STH1 = DB::prepare($sql1);
$data = array("src_field_id" => $src_field_id);
$STH1->execute($data);
$fieldResults = $STH1->fetchAll(PDO::FETCH_ASSOC);
return isset($fieldResults[0]) ? $fieldResults[0]['dest_field_id'] : $src_field_id;
}
private
function getNewAppId($src_app_id)
{
$sql2 = "select dest_app_id from apps WHERE src_app_id=:src_app_id";
$STH2 = DB::prepare($sql2);
$data = array("src_app_id" => $src_app_id);
$STH2->execute($data);
$appResults = $STH2->fetchAll(PDO::FETCH_ASSOC);
return isset($appResults[0]) ? $appResults[0]['dest_app_id'] : $src_app_id;
}
private
function getNewUserId($src_user_id)
{
$sql = "SELECT user_id FROM dest_workspace_members WHERE src_user_id=:src_user_id";
$STH = DB::prepare($sql);
$STH->execute(array(
'src_user_id' => $src_user_id,
));
$userResult = $STH->fetchAll(PDO::FETCH_ASSOC);
return isset($userResult[0]) ? $userResult[0]['user_id'] : $src_user_id;
}
private
function getNewViewId($src_view_id)
{
$sql = "SELECT created_view_id FROM app_views WHERE src_view_d=:src_view_d";
$STH = DB::prepare($sql);
$STH->execute(array(
'src_view_d' => $src_view_id,
));
$viewResult = $STH->fetchAll(PDO::FETCH_ASSOC);
return isset($viewResult[0]) ? $viewResult[0]['created_view_id'] : $src_view_id;
}
private
function readXml($file)
{
try {
$xml = (array)simplexml_load_file($file);
if ($xml) {
$output = $xml;
return $output;
}
} catch
(\Exception $e) {
return false;
}
}
private
function getUserReport()
{
echo "<pre>";
$key = PodioConfig::getKey1();
Podio::setup($key['client_id'], $key['client_secret']);
Podio::authenticate_with_password('podio@logmein.com', 'F69cb2q&fp&1');
$spaces = $this->customPodioAPI->podioGet(" /space/org/1249923/");
foreach ($spaces as $space) {
if ($space['name'] != "Employee Network") {
$spaceId = $space['space_id'];
$admins = $this->customPodioAPI->podioGet("/space/" . $spaceId . "/member/admin/");
foreach ($admins as $admin) {
$array = array(
'jis.jose+logmein1@phases.dk',
'podio@logmein.com');
if (!in_array($admin['user']['mail'], $array)) {
print_r($admin['user']['mail'] . ', ');
}
}
echo "<br>";
}
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists