From 4bef36aa6bf81afee7abd5d037e47026f6e2b08c Mon Sep 17 00:00:00 2001 From: Walter Hupfeld Date: Sat, 16 Mar 2024 17:33:46 +0100 Subject: [PATCH] shapefile --- vendor/Shapefile/File/FileInterface.php | 108 ++++++++++++++ vendor/Shapefile/File/StreamResourceFile.php | 143 +++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 vendor/Shapefile/File/FileInterface.php create mode 100644 vendor/Shapefile/File/StreamResourceFile.php diff --git a/vendor/Shapefile/File/FileInterface.php b/vendor/Shapefile/File/FileInterface.php new file mode 100644 index 0000000..df5f02f --- /dev/null +++ b/vendor/Shapefile/File/FileInterface.php @@ -0,0 +1,108 @@ +flag_resource = is_resource($file); + + if ($this->flag_resource) { + $this->handle = $file; + if (get_resource_type($this->handle) !== 'stream' || !stream_get_meta_data($this->handle)['seekable']) { + throw new ShapefileException(Shapefile::ERR_FILE_RESOURCE_NOT_VALID); + } + if ((!$write_access && !$this->isReadable()) || ($write_access && !$this->isWritable())) { + throw new ShapefileException(Shapefile::ERR_FILE_PERMISSIONS); + } + } else { + $this->handle = @fopen($file, $write_access ? 'c+b' : 'rb'); + if ($this->handle === false) { + throw new ShapefileException(Shapefile::ERR_FILE_OPEN); + } + } + } + + /** + * Destructor. + * + * Closes file if it was NOT passed as resource handle. + */ + public function __destruct() + { + if (!$this->flag_resource) { + fclose($this->handle); + } + } + + + /** + * Gets canonicalized absolute pathname. + */ + public function getFilepath() + { + return realpath(stream_get_meta_data($this->handle)['uri']); + } + + + public function isReadable() + { + return in_array(stream_get_meta_data($this->handle)['mode'], ['rb', 'r+b', 'w+b', 'x+b', 'c+b']); + } + + public function isWritable() + { + return in_array(stream_get_meta_data($this->handle)['mode'], ['r+b', 'wb', 'w+b', 'xb', 'x+b', 'cb', 'c+b']); + } + + + public function truncate($size) + { + ftruncate($this->handle, $size); + } + + public function getSize() + { + return fstat($this->handle)['size']; + } + + + public function getPointer() + { + return ftell($this->handle); + } + + public function setPointer($position) + { + fseek($this->handle, $position, SEEK_SET); + } + + public function resetPointer() + { + fseek($this->handle, 0, SEEK_END); + } + + public function setOffset($offset) + { + fseek($this->handle, $offset, SEEK_CUR); + } + + + public function read($length) + { + return @fread($this->handle, $length); + } + + public function write($data) + { + if (@fwrite($this->handle, $data) === false) { + return false; + } + return true; + } +}