function _processFile($dir, $f, &$xmlStructure)
{
    $filepath = "{$dir}/{$f}";
    // $fileURL = "http://".$_SERVER[SERVER_NAME].dirname($_SERVER[SCRIPT_NAME]).substr($dir, 1)."/$f";
    // what level are we at?
    $depth = strlen(preg_replace("/[^\\/]/", "", $dir));
    // for directories (including symbolic links)
    if (is_link($filepath) || is_dir($filepath)) {
        // we don't examine some certain folders
        if (preg_match(IGNORE_FOLDERS, $filepath)) {
            return;
        }
        $name = "subfolder";
        switch ($depth) {
            case 1:
                $name = "group";
                break;
            case 2:
                $name = "project";
                break;
        }
        $subnode = $xmlStructure->addChild($name);
        $subnode->addAttribute("name", $f);
        $subnode->addAttribute("depth", $depth);
        $subnode->addAttribute("path", $filepath);
        // resolve the link
        if (is_link($filepath)) {
            $filepath = resolvePath("{$dir}/" . readlink($filepath));
        }
        scanDirectory($filepath, $subnode);
        // for text contents, e.g. contact
    } else {
        if (preg_match("/\\.(txt|md)\$/", $f)) {
            if (is_readable($filepath)) {
                $text = trim(file_get_contents($filepath));
                if (strlen($text)) {
                    $subnode = $xmlStructure->addChild("text", htmlentities($text));
                    $subnode->addAttribute("type", preg_replace("/\\.(txt|md)/", "", $f));
                }
            }
            // content files
        } else {
            $subnode = $xmlStructure->addChild("file");
            $subnode->addAttribute("path", $filepath);
            $subnode->addAttribute("name", preg_replace("/\\.[^.]+\$/", "", $f));
        }
    }
}
Esempio n. 2
0
 private function getFileexplorerPath($paramName = 'node')
 {
     $path = preg_replace("/^\\/fileexplorer/", "", $this->getParam($paramName));
     $path = resolvePath(PIMCORE_DOCUMENT_ROOT . $path);
     if (strpos($path, PIMCORE_DOCUMENT_ROOT) !== 0) {
         throw new \Exception('operation permitted, permission denied');
     }
     return $path;
 }
/**
 * Scan all files from Magento root
 *
 * @param $path
 * @param array $ignorePath
 * @return array
 */
function scanFiles($path, $ignorePath = [])
{
    global $sampleDataSource;
    $results = [];
    foreach (glob($path . DIRECTORY_SEPARATOR . '*') as $filename) {
        $target = preg_replace('#^' . preg_quote($sampleDataSource) . "#", '', $filename);
        if (!in_array(resolvePath($target), $ignorePath)) {
            $results[] = $filename;
            if (is_dir($filename)) {
                $results = array_merge($results, scanFiles($filename, $ignorePath));
            }
        }
    }
    return $results;
}
 protected function addAsset()
 {
     $success = false;
     if (array_key_exists("Filedata", $_FILES)) {
         $filename = $_FILES["Filedata"]["name"];
         $sourcePath = $_FILES["Filedata"]["tmp_name"];
     } else {
         if ($this->getParam("type") == "base64") {
             $filename = $this->getParam("filename");
             $sourcePath = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/upload-base64" . uniqid() . ".tmp";
             $data = preg_replace("@^data:[^,]+;base64,@", "", $this->getParam("data"));
             File::put($sourcePath, base64_decode($data));
         }
     }
     if ($this->getParam("dir") && $this->getParam("parentId")) {
         // this is for uploading folders with Drag&Drop
         // param "dir" contains the relative path of the file
         $parent = Asset::getById($this->getParam("parentId"));
         $newPath = $parent->getFullPath() . "/" . trim($this->getParam("dir"), "/ ");
         // check if the path is outside of the asset directory
         $newRealPath = PIMCORE_ASSET_DIRECTORY . $newPath;
         $newRealPath = resolvePath($newRealPath);
         if (strpos($newRealPath, PIMCORE_ASSET_DIRECTORY) !== 0) {
             throw new \Exception("not allowed");
         }
         $maxRetries = 5;
         for ($retries = 0; $retries < $maxRetries; $retries++) {
             try {
                 $newParent = Asset\Service::createFolderByPath($newPath);
                 break;
             } catch (\Exception $e) {
                 if ($retries < $maxRetries - 1) {
                     $waitTime = rand(100000, 900000);
                     // microseconds
                     usleep($waitTime);
                     // wait specified time until we restart the transaction
                 } else {
                     // if the transaction still fail after $maxRetries retries, we throw out the exception
                     throw $e;
                 }
             }
         }
         $this->setParam("parentId", $newParent->getId());
     } else {
         if (!$this->getParam("parentId") && $this->getParam("parentPath")) {
             $parent = Asset::getByPath($this->getParam("parentPath"));
             if ($parent instanceof Asset\Folder) {
                 $this->setParam("parentId", $parent->getId());
             } else {
                 $this->setParam("parentId", 1);
             }
         } else {
             if (!$this->getParam("parentId")) {
                 // set the parent to the root folder
                 $this->setParam("parentId", 1);
             }
         }
     }
     $filename = File::getValidFilename($filename);
     if (empty($filename)) {
         throw new \Exception("The filename of the asset is empty");
     }
     $parentAsset = Asset::getById(intval($this->getParam("parentId")));
     // check for duplicate filename
     $filename = $this->getSafeFilename($parentAsset->getFullPath(), $filename);
     if ($parentAsset->isAllowed("create")) {
         if (!is_file($sourcePath) || filesize($sourcePath) < 1) {
             throw new \Exception("Something went wrong, please check upload_max_filesize and post_max_size in your php.ini and write permissions of /website/var/");
         }
         $asset = Asset::create($this->getParam("parentId"), array("filename" => $filename, "sourcePath" => $sourcePath, "userOwner" => $this->user->getId(), "userModification" => $this->user->getId()));
         $success = true;
         @unlink($sourcePath);
     } else {
         \Logger::debug("prevented creating asset because of missing permissions, parent asset is " . $parentAsset->getFullPath());
     }
     return array("success" => $success, "asset" => $asset);
 }
Esempio n. 5
0
function getSftpListing($directory, $host, $port, $username, $password)
{
    $directory = resolvePath($directory);
    $dir = $directory;
    // Parse directory to parts
    $parsed_dir = trim($dir, '/');
    $parts = empty($parsed_dir) ? array() : explode('/', $parsed_dir);
    // Find the path to the parent directory
    if (!empty($parts)) {
        $copy_of_parts = $parts;
        array_pop($copy_of_parts);
        if (!empty($copy_of_parts)) {
            $parent_directory = '/' . implode('/', $copy_of_parts);
        } else {
            $parent_directory = '/';
        }
    } else {
        $parent_directory = '';
    }
    // Initialise
    $connection = null;
    $sftphandle = null;
    // Open a connection
    if (!function_exists('ssh2_connect')) {
        return array('error' => AKText::_('SFTP_NO_SSH2'));
    }
    $connection = ssh2_connect($host, $port);
    if ($connection === false) {
        return array('error' => AKText::_('SFTP_WRONG_USER'));
    }
    if (!ssh2_auth_password($connection, $username, $password)) {
        return array('error' => AKText::_('SFTP_WRONG_USER'));
    }
    $sftphandle = ssh2_sftp($connection);
    if ($sftphandle === false) {
        return array('error' => AKText::_('SFTP_NO_FTP_SUPPORT'));
    }
    // Get a raw directory listing (hoping it's a UNIX server!)
    $list = array();
    $dir = ltrim($dir, '/');
    $handle = opendir("ssh2.sftp://{$sftphandle}/{$dir}");
    if (!is_resource($handle)) {
        return array('error' => AKText::_('SFTPBROWSER_ERROR_NOACCESS'));
    }
    while (($entry = readdir($handle)) !== false) {
        if (!is_dir("ssh2.sftp://{$sftphandle}/{$dir}/{$entry}")) {
            continue;
        }
        $list[] = $entry;
    }
    closedir($handle);
    if (!empty($list)) {
        asort($list);
    }
    return array('error' => '', 'list' => $list, 'breadcrumbs' => $parts, 'directory' => $directory, 'parent' => $parent_directory);
}
Esempio n. 6
0
/**
 * Returns the wwwroot or null string if it was impossible to get.
 *
 * @return String wwwroot or null string if it was impossible to get.
 */
function getWWWRoot($config)
{
    if (isset($config['preview.wwwroot']) && $config['preview.wwwroot']) {
        return getRealPath($config, 'preview.wwwroot');
    }
    // Check document root
    if (isset($_SERVER['DOCUMENT_ROOT'])) {
        return resolvePath($_SERVER['DOCUMENT_ROOT']);
    }
    // Try script file
    if (isset($_SERVER["SCRIPT_NAME"]) && isset($_SERVER["SCRIPT_FILENAME"])) {
        $path = str_replace(toUnixPath($_SERVER["SCRIPT_NAME"]), "", toUnixPath($_SERVER["SCRIPT_FILENAME"]));
        if (is_dir($path)) {
            return toOSPath($path);
        }
    }
    // If all else fails, try this.
    if (isset($_SERVER["SCRIPT_NAME"]) && isset($_SERVER["PATH_TRANSLATED"])) {
        $path = str_replace(toUnixPath($_SERVER["SCRIPT_NAME"]), "", str_replace("//", "/", toUnixPath($_SERVER["PATH_TRANSLATED"])));
        if (is_dir($path)) {
            return toOSPath($path);
        }
    }
    die("Could not resolve WWWROOT path, please set an absolute path in preview.wwwroot config option.");
    return null;
}