/**
  * Initialize the stream from the given path.
  * Concretely, transform ajxp.webdav:// into webdav://
  *
  * @param string $path
  * @return mixed Real path or -1 if currentListing contains the listing : original path converted to real path
  */
 protected static function initPath($path, $streamType, $storeOpenContext = false, $skipZip = false)
 {
     $url = AJXP_Utils::safeParseUrl($path);
     $repoId = $url["host"];
     $repoObject = ConfService::getRepositoryById($repoId);
     if (!isset($repoObject)) {
         $e = new Exception("Cannot find repository with id " . $repoId);
         self::$lastException = $e;
         throw $e;
     }
     if (self::$cloudContext == null) {
         self::$cloudContext = stream_context_create(array("swiftfs" => array('username' => $repoObject->getOption("USERNAME"), 'password' => $repoObject->getOption("PASSWORD"), 'tenantid' => $repoObject->getOption("TENANT_ID"), 'endpoint' => $repoObject->getOption("ENDPOINT"), 'openstack.swift.region' => $repoObject->getOption("REGION"))));
     }
     $baseContainer = $repoObject->getOption("CONTAINER");
     $p = "swiftfs://" . $baseContainer . str_replace("//", "/", $url["path"]);
     return $p;
 }
 /**
  * Initialize the stream from the given path.
  * Concretely, transform ajxp.webdav:// into webdav://
  *
  * @param string $path
  * @return mixed Real path or -1 if currentListing contains the listing : original path converted to real path
  */
 protected static function initPath($path, $streamType, $storeOpenContext = false, $skipZip = false)
 {
     $url = AJXP_Utils::safeParseUrl($path);
     $repoId = $url["host"];
     $repoObject = ConfService::getRepositoryById($repoId);
     if (!isset($repoObject)) {
         $e = new Exception("Cannot find repository with id " . $repoId);
         self::$lastException = $e;
         throw $e;
     }
     $path = $url["path"];
     $host = $repoObject->getOption("HOST");
     $hostParts = parse_url($host);
     if ($hostParts["scheme"] == "https" && !extension_loaded("openssl")) {
         $e = new Exception("Warning you must have the openssl PHP extension loaded to connect an https server!");
         self::$lastException = $e;
         throw $e;
     }
     $credentials = AJXP_Safe::tryLoadingCredentialsFromSources($hostParts, $repoObject);
     $user = $credentials["user"];
     $password = $credentials["password"];
     if ($user != null && $password != null) {
         $host = ($hostParts["scheme"] == "https" ? "webdavs" : "webdav") . "://{$user}:{$password}@" . $hostParts["host"];
         if (isset($hostParts["port"])) {
             $host .= ":" . $hostParts["port"];
         }
     } else {
         $host = str_replace(array("http", "https"), array("webdav", "webdavs"), $host);
     }
     // MAKE SURE THERE ARE NO // OR PROBLEMS LIKE THAT...
     $basePath = $repoObject->getOption("PATH");
     if ($basePath[strlen($basePath) - 1] == "/") {
         $basePath = substr($basePath, 0, -1);
     }
     if ($basePath[0] != "/") {
         $basePath = "/{$basePath}";
     }
     $path = AJXP_Utils::securePath($path);
     if ($path[0] == "/") {
         $path = substr($path, 1);
     }
     // SHOULD RETURN webdav://host_server/uri/to/webdav/folder
     AJXP_Logger::debug(__CLASS__, __FUNCTION__, $host . $basePath . "/" . $path);
     return $host . $basePath . "/" . $path;
 }
Example #3
0
 public function initPath($ajxpPath)
 {
     $repo = ConfService::getRepository();
     if (empty(self::$dropbox)) {
         $consumerKey = $repo->getOption('CONSUMER_KEY');
         $consumerSecret = $repo->getOption('CONSUMER_SECRET');
         self::$oauth = new Dropbox_OAuth_PEAR($consumerKey, $consumerSecret);
         self::$oauth->setToken($_SESSION["OAUTH_DROPBOX_TOKENS"]);
         self::$dropbox = new Dropbox_API(self::$oauth);
     }
     $basePath = $repo->getOption("PATH");
     if (empty($basePath)) {
         $basePath = "";
     }
     $parts = AJXP_Utils::safeParseUrl($ajxpPath);
     $path = $basePath . "/" . ltrim($parts["path"], "/");
     if ($path == "") {
         return "/";
     }
     return $path;
 }
 /**
  * Initialize the stream from the given path.
  * Concretely, transform ajxp.smb:// into smb://
  *
  * @param string $path
  * @return mixed Real path or -1 if currentListing contains the listing : original path converted to real path
  */
 protected static function initPath($path, $streamType, $storeOpenContext = false, $skipZip = false)
 {
     $url = AJXP_Utils::safeParseUrl($path);
     $repoId = $url["host"];
     $repoObject = ConfService::getRepositoryById($repoId);
     if (!isset($repoObject)) {
         throw new Exception("Cannot find repository with id " . $repoId);
     }
     $path = $url["path"];
     // Fix if the host is defined as //MY_HOST/path/to/folder
     $hostOption = AuthService::getFilteredRepositoryOption("access.smb", $repoObject, "HOST");
     $host = str_replace("//", "", $hostOption);
     $credentials = "";
     $safeCreds = AJXP_Safe::tryLoadingCredentialsFromSources($url, $repoObject);
     if ($safeCreds["user"] != "" && $safeCreds["password"] != "") {
         $login = $safeCreds["user"];
         $pass = $safeCreds["password"];
         $_SESSION["AJXP_SESSION_REMOTE_PASS"] = $pass;
         $credentials = "{$login}:{$pass}@";
         $domain = $repoObject->getOption("DOMAIN");
         if ($domain != "") {
             if (strcmp(substr($domain, -1), "/") === 0 || strcmp(substr($domain, -1), "\\") === 0) {
                 $credentials = $domain . $credentials;
             } else {
                 $credentials = $domain . "/" . $credentials;
             }
         }
     }
     $basePath = $repoObject->getOption("PATH");
     $fullPath = "smbclient://" . $credentials . $host . "/";
     //.$basePath."/".$path;
     if ($basePath != "") {
         $fullPath .= trim($basePath, "/\\");
     }
     if ($path != "") {
         $fullPath .= ($path[0] == "/" ? "" : "/") . $path;
     }
     return $fullPath;
 }
 /**
  * Initialize the stream from the given path.
  *
  * @param string $path
  * @param $streamType
  * @param bool $storeOpenContext
  * @param bool $skipZip
  * @return mixed Real path or -1 if currentListing contains the listing : original path converted to real path
  * @throws AJXP_Exception
  * @throws Exception
  */
 protected static function initPath($path, $streamType, $storeOpenContext = false, $skipZip = false)
 {
     $path = self::unPatchPathForBaseDir($path);
     $url = AJXP_Utils::safeParseUrl($path);
     $repoId = $url["host"];
     $test = trim($url["path"], "/");
     $atRoot = empty($test);
     $repoObject = ConfService::getRepositoryById($repoId);
     if (!isset($repoObject)) {
         throw new Exception("Cannot find repository with id " . $repoId);
     }
     $split = UserSelection::detectZip($url["path"]);
     $insideZip = false;
     if ($split && $streamType == "file" && $split[1] != "/") {
         $insideZip = true;
     }
     if ($split && $streamType == "dir") {
         $insideZip = true;
     }
     if ($skipZip) {
         $insideZip = false;
     }
     $resolveUser = null;
     if (isset($url["user"]) && AuthService::usersEnabled()) {
         $resolveUser = ConfService::getConfStorageImpl()->createUserObject($url["user"]);
     }
     $resolvedPath = realpath(SystemTextEncoding::toStorageEncoding($repoObject->getOption("PATH", false, $resolveUser)));
     //var_dump($path);
     //var_dump($skipZip);
     // Inside a zip : copy the file to a tmp file and return a reference to it
     if ($insideZip) {
         $zipPath = $split[0];
         $localPath = $split[1];
         require_once AJXP_BIN_FOLDER . "/pclzip.lib.php";
         //print($streamType.$path);
         if ($streamType == "file") {
             if (self::$crtZip == null || !is_array(self::$currentListingKeys)) {
                 $tmpDir = AJXP_Utils::getAjxpTmpDir() . DIRECTORY_SEPARATOR . md5(time() - rand());
                 mkdir($tmpDir);
                 $tmpFileName = $tmpDir . DIRECTORY_SEPARATOR . basename($localPath);
                 AJXP_Logger::debug(__CLASS__, __FUNCTION__, "Tmp file {$tmpFileName}");
                 register_shutdown_function(array("fsAccessWrapper", "removeTmpFile"), $tmpDir, $tmpFileName);
                 $crtZip = new PclZip(AJXP_Utils::securePath($resolvedPath . $repoObject->resolveVirtualRoots($zipPath)));
                 $content = $crtZip->listContent();
                 if (is_array($content)) {
                     foreach ($content as $item) {
                         $fName = AJXP_Utils::securePath($item["stored_filename"]);
                         if ($fName == $localPath || "/" . $fName == $localPath) {
                             $localPath = $fName;
                             break;
                         }
                     }
                 }
                 $crtZip->extract(PCLZIP_OPT_BY_NAME, $localPath, PCLZIP_OPT_PATH, $tmpDir, PCLZIP_OPT_REMOVE_ALL_PATH);
                 AJXP_Logger::debug(__CLASS__, __FUNCTION__, "Extracted " . $path . " to " . dirname($localPath));
                 if ($storeOpenContext) {
                     self::$crtZip = $crtZip;
                 }
                 return $tmpFileName;
             } else {
                 $key = basename($localPath);
                 if (array_key_exists($key, self::$currentListing)) {
                     self::$currentFileKey = $key;
                     return -1;
                 } else {
                     throw new AJXP_Exception("Cannot find key");
                 }
             }
         } else {
             $crtZip = new PclZip(AJXP_Utils::securePath($resolvedPath . $repoObject->resolveVirtualRoots($zipPath)));
             $liste = $crtZip->listContent();
             if (!is_array($liste)) {
                 $liste = array();
             }
             if ($storeOpenContext) {
                 self::$crtZip = $crtZip;
             }
             $folders = array();
             $files = array();
             $builtFolders = array();
             if ($localPath[strlen($localPath) - 1] != "/") {
                 $localPath .= "/";
             }
             foreach ($liste as $item) {
                 $stored = $item["stored_filename"];
                 if ($stored[0] != "/") {
                     $stored = "/" . $stored;
                 }
                 $pathPos = strpos($stored, $localPath);
                 if ($pathPos !== false) {
                     $afterPath = substr($stored, $pathPos + strlen($localPath));
                     if ($afterPath != "" && substr_count($afterPath, "/") < 2) {
                         $statValue = array();
                         if (substr_count($afterPath, "/") == 0) {
                             $statValue[2] = $statValue["mode"] = $item["folder"] ? 040555 : 0100555;
                             $statValue[7] = $statValue["size"] = $item["size"];
                             $statValue[8] = $statValue["atime"] = $item["mtime"];
                             $statValue[9] = $statValue["mtime"] = $item["mtime"];
                             $statValue[10] = $statValue["ctime"] = $item["mtime"];
                             if (strpos($afterPath, "/") == strlen($afterPath) - 1) {
                                 $afterPath = substr($afterPath, 0, strlen($afterPath) - 1);
                             }
                             //$statValue["filename"] = $zipPath.$localPath.$afterPath;
                             if ($item["folder"]) {
                                 $folders[$afterPath] = $statValue;
                             } else {
                                 $files[$afterPath] = $statValue;
                             }
                         } else {
                             $arr = explode("/", $afterPath);
                             $afterPath = array_shift($arr);
                             if (isset($folders[$afterPath]) || isset($builtFolders[$afterPath])) {
                                 continue;
                             }
                             $statValue[2] = $statValue["mode"] = 040555;
                             $statValue[7] = $statValue["size"] = 0;
                             $statValue[8] = $statValue["atime"] = $item["mtime"];
                             $statValue[9] = $statValue["mtime"] = $item["mtime"];
                             $statValue[10] = $statValue["ctime"] = $item["mtime"];
                             $builtFolders[$afterPath] = $statValue;
                         }
                     }
                 }
             }
             self::$currentListing = array_merge($folders, $builtFolders, $files);
             self::$currentListingKeys = array_keys(self::$currentListing);
             self::$currentListingIndex = 0;
             return -1;
         }
     } else {
         if ($atRoot) {
             $virtual = $repoObject->listVirtualRoots();
             if (count($virtual)) {
                 self::$currentListing = array();
                 foreach ($virtual as $rootKey => $rootValue) {
                     $statValue = array();
                     $statValue[2] = $statValue["mode"] = 040000;
                     //($rootValue["right"] == "rw" ? "00040000" : "00070000");
                     self::$currentListing[$rootKey] = $statValue;
                 }
                 self::$currentListingKeys = array_keys(self::$currentListing);
                 self::$currentListingIndex = 0;
                 return -1;
             }
         }
         return $resolvedPath . $repoObject->resolveVirtualRoots($url["path"]);
     }
 }
 protected function buildRealUrl($url)
 {
     if (!isset($this->user)) {
         $parts = $this->parseUrl($url);
     } else {
         // parseUrl already called before (rename case).
         $parts = AJXP_Utils::safeParseUrl($url);
     }
     $serverPath = AJXP_Utils::securePath("/{$this->path}/" . $parts["path"]);
     return "ftp" . ($this->secure ? "s" : "") . "://{$this->user}:{$this->password}@{$this->host}:{$this->port}" . $serverPath;
 }
 /**
  * Stats the given path.
  *
  * @param string $path
  * @param mixed $flags
  * @return array
  */
 public function url_stat($path, $flags)
 {
     $realPath = self::initPath($path);
     $stat = @stat($realPath);
     $parts = AJXP_Utils::safeParseUrl($path);
     $repoObject = ConfService::getRepositoryById($parts["host"]);
     AbstractAccessDriver::fixPermissions($stat, $repoObject, array($this, "detectRemoteUserId"));
     return $stat;
 }
 public static function getSshConnection($path, $repoObject = null)
 {
     if ($repoObject != null) {
         $url = array();
     } else {
         $url = AJXP_Utils::safeParseUrl($path);
         $repoId = $url["host"];
         $repoObject = ConfService::getRepositoryById($repoId);
     }
     $remote_serv = $repoObject->getOption("SERV");
     $remote_port = $repoObject->getOption("PORT");
     $credentials = AJXP_Safe::tryLoadingCredentialsFromSources($url, $repoObject);
     $remote_user = $credentials["user"];
     $remote_pass = $credentials["password"];
     $remote_base_path = $repoObject->getOption("PATH");
     $callbacks = array('disconnect' => "disconnectedSftp", 'ignore' => "ignoreSftp", 'debug' => "debugSftp", 'macerror' => "macerrorSftp");
     $connection = ssh2_connect($remote_serv, intval($remote_port), array(), $callbacks);
     ssh2_auth_password($connection, $remote_user, $remote_pass);
     return array($connection, $remote_base_path);
 }
 public function stream_open($path, $mode, $options, &$opened_path)
 {
     // parse URL
     $parts = AJXP_Utils::safeParseUrl($path);
     $this->repositoryId = $parts["host"];
     $mainCacheDir = defined('AJXP_SHARED_CACHE_DIR') ? AJXP_SHARED_CACHE_DIR : AJXP_CACHE_DIR;
     if (!isset(self::$delimiter) && file_exists($mainCacheDir . "/access.imap/mailbox_delim_" . $this->repositoryId)) {
         self::$delimiter = file_get_contents($mainCacheDir . "/access.imap/mailbox_delim_" . $this->repositoryId);
     }
     $this->path = substr($parts["path"], 1);
     //$this->mailbox = "INBOX";
     $pathParts = explode("/", $this->path);
     $pathParts = array_filter($pathParts, "rejectEmpty");
     if (count($pathParts) > 1) {
         $this->path = array_pop($pathParts);
         $this->mailbox = implode("/", $pathParts);
     } else {
         if (count($pathParts) == 1) {
             $this->mailbox = implode("/", $pathParts);
             $this->path = "";
         } else {
             $this->mailbox = "";
             $this->path = "";
         }
     }
     $this->fragment = $parts["fragment"];
     if (empty($this->path) && $mode !== 'np') {
         return false;
     }
     if (!empty($this->mailbox)) {
         $this->mailbox = mb_convert_encoding($this->mailbox, "UTF7-IMAP", SystemTextEncoding::getEncoding());
         $this->mailbox = str_replace("__delim__", isset(self::$delimiter) ? self::$delimiter : "/", $this->mailbox);
     }
     if (!empty($this->fragment) && strpos($this->fragment, "attachments") === 0 && strpos($this->fragment, "/") !== false) {
         // remove fragment
         $ar = explode("#", $path);
         $mailPath = array_shift($ar);
         $ar = explode("/", $this->fragment);
         $attachmentId = array_pop($ar);
         $this->currentAttachmentData = array("realPath" => $mailPath, "attachmentId" => $attachmentId);
         // EXTRACT ATTACHMENT AND RETURN
         require_once AJXP_INSTALL_PATH . "/plugins/editor.eml/class.EmlParser.php";
         $emlParser = new EmlParser("", "");
         $attachMeta = array();
         $this->data = $emlParser->getAttachmentBody($this->currentAttachmentData["realPath"], $this->currentAttachmentData["attachmentId"], true, $attachMeta);
         if (self::$attachmentsMetadata == null) {
             self::$attachmentsMetadata = array($attachMeta);
         }
         $this->currentAttachmentData["size"] = strlen($this->data);
         $this->pos = 0;
         $this->size = strlen($this->data);
         return true;
     }
     // open IMAP connection
     if (self::$currentStream != null) {
         AJXP_Logger::debug(__CLASS__, __FUNCTION__, "Using currently opened stream! " . print_r(self::$currentStream, true));
         $this->ih = self::$currentStream;
         // Rewind everything
         $this->dir_rewinddir();
         $this->stream_seek(0);
     } else {
         $repository = ConfService::getRepositoryById($this->repositoryId);
         $ssl = $repository->getOption("SSL") == "true" ? true : false;
         $this->pop3 = $repository->getOption("BOX_TYPE") == "pop3" ? true : false;
         $this->host = $repository->getOption("HOST");
         $this->port = $repository->getOption("PORT");
         $this->username = $repository->getOption("USER");
         $this->password = $repository->getOption("PASS");
         $server = "{" . $this->host . ":" . $this->port . "/" . ($this->pop3 ? "pop3/" : "") . ($ssl ? "ssl/novalidate-cert" : "novalidate-cert") . "}";
         self::$currentRef = $server;
         AJXP_Logger::debug(__CLASS__, __FUNCTION__, "Opening a new stream " . $server . " with mailbox '" . $this->mailbox . "'");
         try {
             $this->ih = imap_open($server . $this->mailbox, $this->username, $this->password, !$this->pop3 && empty($this->mailbox) ? OP_HALFOPEN : NULL, 1);
         } catch (Exception $e) {
             throw new Exception($e->getMessage() . " - imap errors  : " . print_r(imap_errors(), true), $e->getCode());
         }
         self::$currentStream = $this->ih;
         register_shutdown_function(array("imapAccessWrapper", "closeStreamFunc"));
     }
     if ($this->ih) {
         if (!empty($this->path)) {
             list($stats, ) = imap_fetch_overview($this->ih, $this->path);
             $this->size = $stats->size;
             $this->time = strtotime($stats->date);
         }
         return true;
     } else {
         return false;
     }
 }