Example #1
0
	/**
	 * Construct a new File object with the requested filename or URL.
	 *
	 * @param null $filename
	 * @param Filestore\FTP\FTPConnection|null $ftpobject
	 */
	public function __construct($filename = null, $ftpobject = null) {
		if($ftpobject !== null){
			$this->_ftp = $ftpobject;
		}
		else{
			$this->_ftp = \Core\ftp();
		}

		if($filename){
			$this->setFilename($filename);
		}
	}
Example #2
0
	/**
	 * Get the global FTP connection.
	 *
	 * Returns the FTP resource or false on failure.
	 *
	 * @deprecated 2011.11
	 * @return resource | false
	 */
	public static function FTP() {
		return \Core\ftp();
	}
Example #3
0
	/**
	 * Delete a directory and recursively any file inside it.
	 */
	public function delete() {
		$ftp    = \Core\ftp();
		$tmpdir = TMP_DIR;
		if ($tmpdir{0} != '/') $tmpdir = ROOT_PDIR . $tmpdir; // Needs to be fully resolved

		if(
			!$ftp || // FTP not enabled or
			(strpos($this->getPath(), $tmpdir) === 0) // Destination is a temporary directory.
		){
			$dirqueue = array($this->getPath());
			$x        = 0;
			do {
				$x++;
				foreach ($dirqueue as $k => $d) {
					$isempty = true;
					$dh      = opendir($d);
					if (!$dh) return false;
					while (($file = readdir($dh)) !== false) {
						if ($file == '.') continue;
						if ($file == '..') continue;
						$isempty = false;
						if (is_dir($d . $file)) $dirqueue[] = $d . $file . '/';
						else unlink($d . $file);
					}
					closedir($dh);
					if ($isempty) {
						rmdir($d);
						unset($dirqueue[$k]);
					}
				}

				$dirqueue = array_unique($dirqueue);
				krsort($dirqueue);
			}
			while (sizeof($dirqueue) && $x <= 10);
			return true;
		}
		else{
			// If there are children, drop into them and remove those too.
			// This is because directories need to be empty.
			foreach($this->ls() as $sub){
				if($sub instanceof Filestore\File) $sub->delete();
				else $sub->delete();
			}
			$path = $this->getPath();

			// Trim off the ROOT_PDIR since it'll be relative to the ftp root set in the config.
			if (strpos($path, ROOT_PDIR) === 0) $path = substr($path, strlen(ROOT_PDIR));

			// Prepend the ftp directory
			//$path = \ConfigHandler::Get('/core/ftp/path') . $path;

			return ftp_rmdir($ftp, $path);
		}
	}
Example #4
0
	/**
	 * Write a string to a file
	 *
	 * @link http://php.net/manual/en/function.file-put-contents.php
	 *
	 * @param string $filename <p>
	 * Path to the file where to write the data.
	 * </p>
	 * @param mixed  $data <p>
	 * The data to write. Can be either a string, an
	 * array or a stream resource.
	 * </p>
	 * <p>
	 * If data is a stream resource, the
	 * remaining buffer of that stream will be copied to the specified file.
	 * This is similar with using stream_copy_to_stream.
	 * </p>
	 * <p>
	 * You can also specify the data parameter as a single
	 * dimension array. This is equivalent to
	 * file_put_contents($filename, implode('', $array)).
	 * </p>
	 *
	 * @return bool Returns true on success or false on failure.
	 */
	public static function _PutContents($filename, $data) {
		$ftp    = \Core\ftp();
		$tmpdir = TMP_DIR;
		if ($tmpdir{0} != '/') $tmpdir = ROOT_PDIR . $tmpdir; // Needs to be fully resolved

		// Resolve it from its default.
		// This is provided from a config define, (probably).
		$mode = (defined('DEFAULT_FILE_PERMS') ? DEFAULT_FILE_PERMS : 0644);

		if (!$ftp) {
			$ret = file_put_contents($filename, $data);
			if ($ret === false) return $ret;
			chmod($filename, $mode);
			return $ret;
		}
		elseif (strpos($filename, $tmpdir) === 0) {
			// Tmp files should be written directly.
			$ret = file_put_contents($filename, $data);
			if ($ret === false) return $ret;
			chmod($filename, $mode);
			return $ret;
		}
		else {
			// Trim off the ROOT_PDIR since it'll be relative to the ftp root set in the config.
			if (strpos($filename, ROOT_PDIR) === 0) $filename = substr($filename, strlen(ROOT_PDIR));
			//$filename = ConfigHandler::Get('/core/ftp/path') . $filename;
			// FTP requires a filename, not data...
			$tmpfile = $tmpdir . 'ftpupload-' . Core::RandomHex(4);
			file_put_contents($tmpfile, $data);

			if (!ftp_put($ftp, $filename, $tmpfile, FTP_BINARY)) {
				// Well, delete the temp file anyway...
				unlink($tmpfile);
				return false;
			}

			if (!ftp_chmod($ftp, $mode, $filename)) return false;

			// woot... but cleanup the trash first.
			unlink($tmpfile);
			return true;
		}
	}
Example #5
0
	/**
	 * Static function to act as Factory for the underlying Filestore system.
	 * This will parse the incoming URI and return the appropriate type based on Core settings and filetype.
	 *
	 * @param $uri
	 *
	 * @return File
	 */
	public static function File($uri) {

		// GOGO caching ;)
		if(isset(self::$_ResolveCache[$uri])){
			$resolved = self::$_ResolveCache[$uri]->getFilename();
			if(isset(self::$_Files[$resolved])){
				return self::$_Files[$resolved];
			}
		}

		// self::$_Files[$originaluri]

		//var_dump($uri);

		// base64 comes first.  If the filename is encoded in that, decode it first.
		if (strpos($uri, 'base64:') === 0){
			$uri = base64_decode(substr($uri, 7));
		}

		// Allow FTP files to be requested here!
		// This needs to be before the :// check, because technically FTP can be a remote file,
		// but it has extra functionality, (namely being able to write or perform other operations through FTP)
		if(strpos($uri, 'ftp://') === 0){
			// Don't cache remote files.
			return new Backends\FileFTP($uri);
		}


		if(strpos($uri, ROOT_PDIR) === 0){
			// No change needed ;)
		}
		elseif(strpos($uri, ROOT_URL_NOSSL) === 0){
			// If this is a local file, just the URL version.... allow that remap too!
			$uri = ROOT_PDIR . substr($uri, strlen(ROOT_URL_NOSSL));
		}
		elseif(strpos($uri, ROOT_URL_SSL) === 0){
			// If this is a local file, just the URL version.... allow that remap too!
			$uri = ROOT_PDIR . substr($uri, strlen(ROOT_URL_SSL));
		}

		// Allow remote files to be requested here too!
		if(strpos($uri, '://') !== false){
			// Don't cache remote files.
			return new Backends\FileRemote($uri);
		}




		if(
			strpos($uri, 'asset/') === 0 ||
			strpos($uri, 'assets/') === 0 ||
			strpos($uri, get_asset_path()) === 0
		){
			// Is this an asset request?
			$file = resolve_asset_file($uri);
		}
		elseif(
			strpos($uri, 'public/') === 0 ||
			strpos($uri, get_public_path()) === 0
		){
			// Is this a public request?
			$file = resolve_public_file($uri);
		}
		elseif(
			strpos($uri, 'private/') === 0 ||
			strpos($uri, get_private_path()) === 0
		){
			// Is this a private request?
			$file = resolve_private_file($uri);
		}
		elseif(
			strpos($uri, 'tmp/') === 0
		){
			// Is this a tmp request?
			$file = new Backends\FileLocal(get_tmp_path() . substr($uri, 4));
		}
		elseif(
			strpos($uri, get_tmp_path()) === 0 ||
			strpos($uri, '/tmp/') === 0
		){
			// tmp fully resolved?
			$file = new Backends\FileLocal($uri);
		}
		elseif(\Core\ftp() && EXEC_MODE == 'WEB'){
			// Umm.... ok
			// Still, try to use the FTP proxy files if it's enabled.
			$file = new Backends\FileFTP($uri);
		}
		else{
			// Screw it... regular file it is!
			$file = new Backends\FileLocal($uri);
		}

		// Cache this for future calls on this page load.
		self::$_Files[$file->getFilename()] = $file;
		return $file;
	}
Example #6
0
	protected function setUp(){
		$this->_ftp = \Core\ftp();
	}
Example #7
0
/**
 * Resolve a name for a public to an actual file.
 *
 * @param $filename
 *
 * @return \Core\Filestore\Directory
 *
 * @throws \Exception
 */
function resolve_public_directory($filename){
	$resolved = get_public_path();

	if (strpos($filename, 'public/') === 0) {
		// Allow "assets/blah" to be passed in
		$filename = substr($filename, 7);
	}
	elseif(strpos($filename, $resolved) === 0){
		// Allow the fully resolved name to be passed in
		$filename = substr($filename, strlen($resolved));
	}

	// I need to check the custom, current theme, and finally default locations for the file.
	$theme = \ConfigHandler::Get('/theme/selected');
	switch(CDN_TYPE){
		case 'local':
			if(\Core\ftp()){
				// FTP has its own sub-type.
				return new Backends\DirectoryFTP($resolved . $filename);
			}
			else{
				return new Backends\DirectoryLocal($resolved . $filename);
			}

			break;
		default:
			throw new \Exception('Unsupported CDN type: ' . CDN_TYPE);
			break;
	}
}