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/LinkReplaceRepo.php

<?php

/**
 * Created by IntelliJ IDEA.
 * User: jis
 * Date: 17/4/17
 * Time: 6:05 PM
 */

namespace App\Repository\Utils;


use App\Config\Config;
use App\Repository\General\DB;
use App\Repository\General\Log;
use PDO;

class LinkReplaceRepo
{

    public function replaceLinks($string, $identifier)
    {
        try {
            return $this->replacePodioLink($string, $identifier);
        } catch (\Exception $e) {
            Log::logError($e . "", array($string), "replaceLinks");
        }
        return $string;
    }

    public function testLinkReplace()
    {
        $sql = "select *  from item_field_values where id=105056 ";
        $STH = DB::prepare($sql);
        $STH->execute();
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);

        if ($result && count($result) > 0) {
            $stringValue = $result[0]['value'];
            $newString = $this->replacePodioLink($stringValue, 1);
            echo $newString;
        }
    }

    public function replacePodioLink($stringValue, $identifier)
    {
        $regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
        preg_match_all($regex, $stringValue, $matches);
        $urls = $matches[0];
        foreach ($urls as $url) {
            $newUrl = $this->getNewUrl($url);
            if ($newUrl) {
                $stringValue = $this->str_replace_first($url, $newUrl, $stringValue);
            }
        }
        return $stringValue;

        //        $sql = "select *  from item_field_values where id=695 ";
        //        $STH = DB::prepare($sql);
        //        $STH->execute();
        //        $result = $STH->fetchAll(PDO::FETCH_ASSOC);

        //        if ($result && count($result) > 0) {
        //            $stringValue = $result[0]['value'];
    }

    private function getNewUrl($url)
    {
        $newUrl = null;
        $parseURL = parse_url($url);
        if (isset($parseURL['path'])) {
            $path = str_replace("//", "/", $parseURL['path']);
            $path_parts = explode('/', $path);
            if ($parseURL['host'] == 'podio.com') {
                if (is_array($path_parts) && count($path_parts) == 3 && $path_parts[1] == 'tasks') {
                    // this is a task
                    $newTaskID = $this->getNewTaskID($path_parts[2]);
                    if ($newTaskID) {
                        $newUrl = "https://podio.com/tasks/" . $newTaskID;
                    }
                } else if (is_array($path_parts) && count($path_parts) > 1 && $path_parts[1] == Config::$src_org) {
                    // check whether this is an item
                    $newUrl = $this->getNewItemUrl($path_parts);
                    if (!$newUrl) { // it is not an item
                        // check whether this is an app
                        $newUrl = $this->getNewAppUrl($path_parts, $url);
                        if (!$newUrl) { // it is not an app
                            // check whether this is a space
                            $newUrl = $this->getNewSpaceUrl($path_parts, $url);
                        }
                    }
                    if (!$newUrl) { // it may be a view
                        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 !== '';
                            }));
                            if ($fragmentParts && is_array($fragmentParts) && count($fragmentParts) >= 2 && $fragmentParts[0] == 'views') {
                                $newUrl = $this->getNewViewUrl($fragmentParts[1]);
                            }
                        }
                    }
                }
            } else if ($parseURL['host'] == 'files.podio.com') {
                if (is_array($path_parts) && count($path_parts) == 2) {
                    $newFileID = $this->getNewFileID($path_parts[1]);
                    $newUrl = "https://files.podio.com/" . $newFileID;
                }
            }
        } else {
            Log::log("getNewUrl path is missing", array("url" => $url));
        }
        return $newUrl;
    }


    private function getNewAppUrl($path_parts, $url)
    {
        if (is_array($path_parts) && count($path_parts) == 5) {
            $sql = "SELECT dest_app_url
FROM  `apps` WHERE src_app_url = :src_app_url AND dest_app_url IS NOT NULL limit 1"; //  `identifier`=:identifier and
            $STH = DB::prepare($sql);
            $STH->execute(array(
                'src_app_url' => $url,
            )); // 'identifier' => $identifier,
            $result = $STH->fetchAll(PDO::FETCH_ASSOC);
            if ($result && count($result) > 0) {
                return $result[0]['dest_app_url'];
            }
        } else if (is_array($path_parts) && count($path_parts) == 4) {
            // space apps url
            if ($path_parts[3] == 'apps') {
                $spaceURL = $this->str_lreplace("/apps", "", $url);
                $newSpaceUrl = $this->getNewSpaceUrlFromDB($spaceURL);
                if ($newSpaceUrl) {
                    return $newSpaceUrl . "/apps";
                }
            }
            // space tasks url
            if ($path_parts[3] == 'tasks') {
                $spaceURL = $this->str_lreplace("/tasks", "", $url);
                $newSpaceUrl = $this->getNewSpaceUrlFromDB($spaceURL);
                if ($newSpaceUrl) {
                    return $newSpaceUrl . "/tasks";
                }
            }

            // space files url
            if ($path_parts[3] == 'files') {
                $spaceURL = $this->str_lreplace("/files", "", $url);
                $newSpaceUrl = $this->getNewSpaceUrlFromDB($spaceURL);
                if ($newSpaceUrl) {
                    return $newSpaceUrl . "/files";
                }
            }

            // space members url
            if ($path_parts[3] == 'members') {
                $spaceURL = $this->str_lreplace("/members", "", $url);
                $newSpaceUrl = $this->getNewSpaceUrlFromDB($spaceURL);
                if ($newSpaceUrl) {
                    return $newSpaceUrl . "/members";
                }
            }

            // space calender url
            if ($path_parts[3] == 'calender') {
                $spaceURL = $this->str_lreplace("/calender", "", $url);
                $newSpaceUrl = $this->getNewSpaceUrlFromDB($spaceURL);
                if ($newSpaceUrl) {
                    return $newSpaceUrl . "/calender";
                }
            }
        }
        return null;
    }

    private function getNewSpaceUrl($path_parts, $url)
    {
        if (is_array($path_parts) && count($path_parts) == 3) {
            return $this->getNewSpaceUrlFromDB($url);
        }
        return null;
    }

    private function getNewSpaceUrlFromDB($url)
    {
        $sql = "SELECT dest_space_url
FROM  `work_spaces` WHERE src_space_url = :src_space_url AND dest_space_url IS NOT NULL limit 1"; //  `identifier`=:identifier and
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'src_space_url' => $url,
        )); // 'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            return $result[0]['dest_space_url'];
        }
        return null;
    }

    private function getNewViewUrl($viewID)
    {
        $sql = "SELECT * FROM  `app_views` WHERE  `src_view_d` =:src_view_d";
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'src_view_d' => $viewID
        ));
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            $view = $result[0];
            $sql = "SELECT `dest_app_url` FROM `apps` WHERE `src_app_id`=:src_app_id and dest_app_url IS NOT NULL";
            $STH = DB::prepare($sql);
            $STH->execute(array(
                'src_app_id' => $view['src_app_id']
            ));

            $result = $STH->fetchAll(PDO::FETCH_ASSOC);
            if ($result && count($result) > 0) {
                $destAppUrl = $result[0]['dest_app_url'];
                if (isset($view['created_view_id'])) { // have new view
                    return $destAppUrl . "#/views/" . $view['created_view_id'];
                } else { // new view is not created
                    return $destAppUrl;
                }
            }
        }
        return null;
    }

    private function getNewItemUrl($path_parts)
    {
        if (is_array($path_parts) && count($path_parts) == 7) {
            if ($path_parts[5] == 'items') { // this is an item url
                $path = $path_parts;
                unset($path[6]);
                unset($path[5]);
                $url = implode("/", $path);
                $appUrl = "https://podio.com" . $url;
                if ($path_parts[6] == 'new') {
                    $sql = "SELECT dest_app_url 
                      FROM  `apps` WHERE src_app_url = :src_app_url AND dest_app_url IS NOT NULL limit 1"; //  `identifier`=:identifier and
                    $STH = DB::prepare($sql);
                    $STH->execute(array(
                        'src_app_url' => $appUrl,
                    )); // 'identifier' => $identifier,
                    $result = $STH->fetchAll(PDO::FETCH_ASSOC);
                    if ($result && count($result) > 0) {
                        $destAppUrl = $result[0]['dest_app_url'];
                        return $destAppUrl . '/items/new';
                    }
                } else {
                    $newItemID = $this->getNewItemID($appUrl, $path_parts[6]);
                    if ($newItemID) {
                        // https://podio.com/JIS/JOSE/item/578384245
                        $oldSpaceUrlLabel = $path_parts[2];
                        $oldAppUrlLabel = $path_parts[4];
                        // todo replace new space and app url label
                        return "https://podio.com/" . $oldSpaceUrlLabel . "/" . $oldAppUrlLabel . "/item/" . $newItemID;
                    }
                }
            }
        }
        return null;
    }


    private function getNewItemID($appUrl, $oldAppItemId)
    {
        $sql = "SELECT t1 . dest_item_id
FROM  `items` AS t1
INNER JOIN apps AS t2 ON t1.src_app_id = t2.src_app_id
WHERE t2.src_app_url = :src_app_url
AND t1.src_app_item_id =:src_app_item_id"; //  `identifier`=:identifier and
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'src_app_url' => $appUrl,
            'src_app_item_id' => $oldAppItemId
        )); // 'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            return $result[0]['dest_item_id'];
        }

        return null;
    }


    private function getNewFileID($oldFileID)
    {
        $sql = "select new_file_id  from `files` 
             where  file_id=:file_id 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;
    }

    private 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 null;
    }

    function str_lreplace($search, $replace, $subject)
    {
        $pos = strrpos($subject, $search);

        if ($pos !== false) {
            $subject = substr_replace($subject, $replace, $pos, strlen($search));
        }

        return $subject;
    }


    function str_replace_first($from, $to, $subject)
    {
        $from = '/' . preg_quote($from, '/') . '/';
        return preg_replace($from, $to, $subject, 1);
    }

    public function getNewUser($id)
    {
        $sql = "SELECT user_id FROM dest_workspace_members WHERE src_user_id=:user_id";
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'user_id' => $id,
        )); //   'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            return $result[0]['user_id'];
        }
        return $id;
    }

    public function getNewUserProfile($id)
    {
        $sql = "SELECT profile_id FROM dest_workspace_members WHERE src_user_id=:user_id";
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'user_id' => $id,
        )); //   'identifier' => $identifier,
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if ($result && count($result) > 0) {
            if ($result[0]['profile_id'] == 198494431)
                return false;
            else
                return $result[0]['profile_id'];
        }
        return false;
    }
}

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