Duffer Derek

Current Path : /var/www/podiomigration.bitkit.dk/httpdocs/src/classes/Repository/Utils/
Upload File :
Current File : /var/www/podiomigration.bitkit.dk/httpdocs/src/classes/Repository/Utils/LinkReplace.php

<?php
/**
 * Created by PhpStorm.
 * User: jis
 * Date: 7/4/17
 * Time: 11:28 AM
 */

namespace App\Repository\Utils;


use App\Repository\General\DB;
use PDO;
use Podio;

class LinkReplace
{
    public function filterURLFromString($identifier, $string)
    {
        $repo = new PodioAuthRepo();
        if ($repo->authenticate($identifier)) {
            Log::log("createItemComments - $identifier  started ", null, "info");
            print_r($string);
            echo '<br /><br />****************************************************<br /><br />';
            list($oldURLs, $newURLs) = $this->getURLsfromText($string);
            $newURLs = $this->getSortWithSlash($newURLs);
            $newTextString = $this->findAndReplaceURLs($string, $oldURLs, $newURLs);
            print_r($newTextString);
        }

//        $auth = new authController();
//        if ($auth->isPodioAuthenticated()) {
//
//            $itemID = 388518396;
//            $resp = Podio::get("/item/" . $itemID)->json_body();
//            if ($resp) {
//                echo '<pre>';
//                foreach ($resp['fields'] as $fields) {
//                    if ($fields['external_id'] == 'link-text-field') {
//                        $string = $fields['values'][0]['value'];
//                        print_r($string);
//                        echo '<br /><br />****************************************************<br /><br />';
//                        list($oldURLs, $newURLs) = $this->getURLsfromText($string);
//                        $newURLs = $this->getSortWithSlash($newURLs);
//                        $newTextString = $this->findAndReplaceURLs($string, $oldURLs, $newURLs);
//
//                        print_r($newTextString);
//                    }
//                }
//            }
//        }

    }

    public function getURLsfromText($string, $identifier)
    {
        $regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
        preg_match_all($regex, $string, $matches);
        $urls = $matches[0];
        $newURLs = array();
        $oldURLs = array();
        foreach ($urls as $url) {
//            echo 'Old URL: ';
//            echo $url . '<br />';
            $parseURL = parse_url($url);
            if (isset($parseURL['path'])) {
                $path_parts = explode('/', $parseURL['path']);
                $pathParts = array_values(array_filter($path_parts, function ($var) {
                    return $var !== '';
                }));
                $newPathParts = $this->getNewURL($parseURL, $pathParts, $identifier);
                if (isset($parseURL['fragment'])) {// for views and wigets , url part after #
                    $fragment_parts = explode('/', $parseURL['fragment']);
                    $fragmentParts = array_values(array_filter($fragment_parts, function ($var) {
                        return $var !== '';
                    }));
                    $newFragments = $this->getNewURLFragments($fragmentParts);
                }
                $newURL = $parseURL['scheme'] . '://' . $parseURL['host'] . '/' . implode("/", $newPathParts);
                if (isset($newFragments) && count($newFragments)) {
                    $newURL .= '#/' . implode("/", $newFragments);

                }
                array_push($newURLs, $newURL);
                array_push($oldURLs, $url);
            }
        }
        return array($oldURLs, $newURLs);
    }

    private function getNewURL($parseURL, $pathParts, $identifier)
    {
        $newPathParts = $pathParts;

        switch ($parseURL['host']) {
            case 'podio.com':
                if ($pathParts[0] == 'tasks') {
                    if (isset($pathParts[1])) {
                        //have task id
                        $newTaskID = $this->getNewTaskID($pathParts[1]);//oldTaskID
                        $newPathParts[1] = $newTaskID;
                    } else {
                        //no need to continue
                    }
                } elseif ($pathParts[0] == 'users') {
                    //no need to continue

                } else {
                    //orgname
                    $newOrgID = $this->getNewOrgID($pathParts[0]);//oldOrgID
                    $newPathParts[0] = $newOrgID;
                    if (isset($pathParts[1])) {
                        //could be 'organization' or workspace ids
                        if ($pathParts[1] != 'organization') {
                            //workspace ids
                            $oldSpaceUrl = "https://podio.com/" . $pathParts[0] . "/" . $pathParts[1];
                            $newSpaceID = $this->getNewSpaceID($pathParts[1], $oldSpaceUrl, $identifier);//oldSpaceID
                            $newPathParts[1] = $newSpaceID;
                            if (isset($pathParts[2])) {
                                //might be contacts or apps , all others can be rejected
                                if ($pathParts[2] == 'contacts') {
                                    if (isset($pathParts[3])) {
                                        //contact ids
                                        $newContactID = $this->getNewContactID($pathParts[3]);//oldContactID
                                        $newPathParts[3] = $newContactID;
                                    } else {
                                        //no need to continue
                                    }
                                } elseif ($pathParts[2] == 'apps') {
                                    if (isset($pathParts[3])) {
                                        //app ids
                                        $oldAppUrl = "https://podio.com/" . $pathParts[0] . "/" . $pathParts[1] . "/apps/" . $pathParts[3];
                                        $newAppID = $this->getNewAppID($pathParts[3], $oldAppUrl, $identifier);//oldAppID
                                        $newPathParts[3] = $newAppID;
                                        if (isset($pathParts[4])) {
                                            if ($pathParts[4] == 'items') {
                                                $oldAppUrl = "https://podio.com/" . $pathParts[0] . "/" . $pathParts[1] . "/apps/" . $pathParts[3];
                                                $newItemID = $this->getNewItemID($pathParts[5], $oldAppUrl, $identifier);//oldItemID
                                                // https://podio.com/podio-data-copy-src/workspace-1/apps/test-app-4/items/1
                                                $str = "https://podio.com/" . $newOrgID . "/" . $newSpaceID . "/apps/" . $newAppID . "/items/1";

                                                //we're getting itemIDs and not item cunique id, so change url structure to https://podio.com/ORG_NAME/APP_NAME/item/ITEM_ID
                                                $newPathParts[1] = $newAppID;//set app id
                                                $newPathParts[2] = 'item';
                                                $newPathParts[3] = $newItemID;
                                                array_splice($newPathParts, 4, 2);//remove the next two lines
                                                array_splice($pathParts, 4, 2);
                                            }
                                        }
                                    } else {
                                        //no need to continue
                                    }
                                }
                            } else {
                                //no need to continue
                            }
                        } else {
                            //no need to continue
                        }
                    } else {
                        //no need to continue
                    }

                }
                break;
            case 'files.podio.com';
                if (isset($pathParts[0])) {
                    //file ids
                    $newFileID = $this->getNewFileID($pathParts[0]);//oldFileID
                    $newPathParts[0] = $newFileID;
                } else {
                    //no need to continue
                }
                break;
        }
        return array_replace($pathParts, $newPathParts);

    }

    public
    function getNewURLFragments($fragments)
    {
        return $fragments;
    }

    public
    function getNewTaskID($oldTaskID) // $identifier
    {

        $sql = "select dest_task_id  from `tasks` 
             where  src_task_id=:src_task_id limit 1"; //  `identifier`=:identifier and
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'src_task_id' => $oldTaskID,
        )); //   'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            return $result[0]['dest_task_id'];
        }
        //not found in old
        return $oldTaskID;
    }

    public
    function getNewOrgID($oldOrgID)
    {
        //get new id with the old one
        $oldOrganizations = array('logmein');
        $oldOrganizations = array('podio-data-copy-src');
        //find this in our organization array
        if (in_array($oldOrgID, $oldOrganizations)) {

            $newOrgID = 'citrix';
            $newOrgID = 'podio-data-copy-target';
            return $newOrgID;
        } else {
            //not found in old organizations array, could be another org or anything else
            return $oldOrgID;
        }

    }

    public
    function getNewSpaceID($oldSpaceID, $oldSpaceUrl, $identifier)
    {
        //get new id with the old one

        $sql = "select dest_space_url_label  from `work_spaces` 
             where  src_space_url=:src_space_url limit 1";  //  `identifier`=:identifier and
        $STH = DB::prepare($sql);
        $STH->execute(array(

            'src_space_url' => $oldSpaceUrl,
        )); //  'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            return $result[0]['dest_space_url_label'];
        }
        //not found in old
        return $oldSpaceID;
    }

    public
    function getNewAppID($oldAppID, $oldAppUrl, $identifier)
    {
        //get new id with the old one

        $sql = "select dest_app_url_label  from `apps` 
             where  src_app_url=:src_app_url limit 1"; //  `identifier`=:identifier and
        $STH = DB::prepare($sql);
        $STH->execute(array(

            'src_app_url' => $oldAppUrl,
        )); //    'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            return $result[0]['dest_app_url_label'];
        }
        //not found in old apps id
        return $oldAppID;


//        $oldApps = array('app1', 'app2', 'test-app1-2', 'journalizer-app');
//        if (in_array($oldAppID, $oldApps)) {
//            $newAppID = $oldAppID . '_new';
//            return $newAppID;
//        } else {
//            //check if it is (int) id
//            $oldIntApps = array(1234, 3333, 12497578);
//            if (in_array($oldAppID, $oldIntApps)) {
//                $newAppID = $oldAppID . '_new';
//                return $newAppID;
//            } else {
//                //not found in old apps id
//                return $oldAppID;
//            }
//        }
    }

    public
    function getNewContactID($oldContactID)
    {
        return $oldContactID;
//        //get new id with the old one
//        $oldContacts = array(122, 100, 163221777);
//        if (in_array($oldContactID, $oldContacts)) {
//            $newContactID = $oldContactID . '_new';
//            return $newContactID;
//        } else {
//            //not found in old
//            return $oldContactID;
//        }
    }

    public
    function getNewItemID($oldItemID, $oldAppUrl, $identifier)
    {

        $sql = "select src_app_id  from `apps` 
             where src_app_url=:src_app_url limit 1"; //  `identifier`=:identifier and
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'src_app_url' => $oldAppUrl,
        )); // 'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            $src_app_id = $result[0]['src_app_id'];

            $sql = "select dest_item_id  from `items` 
             where  src_app_id=:src_app_id and src_app_item_id=:src_app_item_id limit 1"; //  `identifier`=:identifier and
            $STH = DB::prepare($sql);
            $STH->execute(array(
                'src_app_id' => $src_app_id,
                'src_app_item_id' => $oldItemID
            )); //'identifier' => $identifier,
            $result = $STH->fetchAll(PDO::FETCH_ASSOC);
            if ($result && count($result) > 0) {
                return $result[0]['dest_item_id'];
            }
        }

        //not found in old apps id
        return $oldItemID;
    }

    public
    function getNewFileID($oldFileID)
    {//  $identifier

        $sql = "select new_file_id  from `files` 
             where  file_id=:file_id limit 1 and new_file_id is not null"; // `identifier`=:identifier and
        $STH = DB::prepare($sql);
        $STH->execute(array(

            'file_id' => $oldFileID,
        )); // 'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            return $result[0]['new_file_id'];
        }

        //not found in old
        return $oldFileID;
    }

    public
    function findAndReplaceURLs($string, $oldURLs, $newURLs)
    {
////        $newText = str_replace($oldURLs, $newURLs, $string);
////        return $newText;
//        foreach ($oldURLs as $i=>$oldURL){
//            $string=str_replace($oldURL,$newURLs[$i],$string);
//        }
        foreach ($newURLs as $i => $newURL) {
            $string = str_replace($oldURLs[$i], $newURL, $string);
        }
        return $string;

    }

    public
    function getSortWithSlash($URLs)
    {
        uasort($URLs, function ($a, $b) {
            $letter = '/';
            $letterPositionsA = array_keys(array_intersect(str_split($a), array($letter)));
            $letterPositionsB = array_keys(array_intersect(str_split($b), array($letter)));
            return count($letterPositionsB) - count($letterPositionsA);
        });
        return $URLs;
    }
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists