Commit 114c6b6044c2854142564a8f75009dede394ed2c
1 parent
55e5e9c5
Add File-like objects that can be handled as a file would, but might be
backed by other things (databases, a URL, a subversion repository, and so forth). git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3579 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
3 changed files
with
278 additions
and
0 deletions
lib/filelike/filelike.inc.php
0 โ 100644
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * $Id$ | |
| 4 | + * | |
| 5 | + * Interface for representing file-like operations (open, read, write, | |
| 6 | + * close) that may not deal with files on the filesystem (or URLs). | |
| 7 | + * | |
| 8 | + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com | |
| 9 | + * | |
| 10 | + * This program is free software; you can redistribute it and/or modify | |
| 11 | + * it under the terms of the GNU General Public License as published by | |
| 12 | + * the Free Software Foundation; either version 2 of the License, or | |
| 13 | + * (at your option) any later version. | |
| 14 | + * | |
| 15 | + * This program is distributed in the hope that it will be useful, | |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 | + * GNU General Public License for more details. | |
| 19 | + * | |
| 20 | + * You should have received a copy of the GNU General Public License | |
| 21 | + * along with this program; if not, write to the Free Software | |
| 22 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 23 | + * | |
| 24 | + * @version $Revision$ | |
| 25 | + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa | |
| 26 | + */ | |
| 27 | + | |
| 28 | +class KTFileLike { | |
| 29 | + var $bSupportChunking = false; | |
| 30 | + var $bIsFSFile = false; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Set up any resources needed to perform work. | |
| 34 | + */ | |
| 35 | + function open($mode = "r") { | |
| 36 | + return PEAR::raiseError('Not implemented'); | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Take care of getting rid of any active resources. | |
| 41 | + */ | |
| 42 | + function close() { | |
| 43 | + return PEAR::raiseError('Not implemented'); | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Behaves like fread | |
| 48 | + */ | |
| 49 | + function read($iBytes) { | |
| 50 | + return PEAR::raiseError('Not implemented'); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Behaves like fwrite | |
| 55 | + */ | |
| 56 | + function write($sData) { | |
| 57 | + return PEAR::raiseError('Not implemented'); | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Behaves like file_get_contents | |
| 62 | + */ | |
| 63 | + function get_contents() { | |
| 64 | + return PEAR::raiseError('Not implemented'); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Behaves like file_get_contents | |
| 69 | + */ | |
| 70 | + function put_contents() { | |
| 71 | + return PEAR::raiseError('Not implemented'); | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Behaves like feof | |
| 76 | + */ | |
| 77 | + function eof() { | |
| 78 | + return PEAR::raiseError('Not implemented'); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * If $bIsFSFile, returns the FSPath (for rename/move) | |
| 83 | + */ | |
| 84 | + function getFSPath() { | |
| 85 | + return PEAR::raiseError('Not implemented'); | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Behaves like filesize | |
| 90 | + */ | |
| 91 | + function filesize() { | |
| 92 | + return PEAR::raiseError('Not implemented'); | |
| 93 | + } | |
| 94 | + | |
| 95 | +} | |
| 96 | + | |
| 97 | +?> | ... | ... |
lib/filelike/filelikeutil.inc.php
0 โ 100644
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * $Id$ | |
| 4 | + * | |
| 5 | + * Utilities using file-like objects | |
| 6 | + * | |
| 7 | + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com | |
| 8 | + * | |
| 9 | + * This program is free software; you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation; either version 2 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program; if not, write to the Free Software | |
| 21 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 | + * | |
| 23 | + * @version $Revision$ | |
| 24 | + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa | |
| 25 | + */ | |
| 26 | + | |
| 27 | +class KTFileLikeUtil { | |
| 28 | + function copy_contents($oFrom, $oTo) { | |
| 29 | + if ($oFrom->bSupportChunking && $oTo->bSupportChunking) { | |
| 30 | + $res = $oFrom->open("r"); | |
| 31 | + if (PEAR::isError($res)) { | |
| 32 | + return $res; | |
| 33 | + } | |
| 34 | + $res = $oTo->open("w"); | |
| 35 | + if (PEAR::isError($res)) { | |
| 36 | + return $res; | |
| 37 | + } | |
| 38 | + while (!$oFrom->eof()) { | |
| 39 | + $res = $oFrom->read(8192); | |
| 40 | + if (PEAR::isError($res)) { | |
| 41 | + return $res; | |
| 42 | + } | |
| 43 | + $res = $oTo->write($res); | |
| 44 | + if (PEAR::isError($res)) { | |
| 45 | + return $res; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + $res = $oFrom->close(); | |
| 49 | + if (PEAR::isError($res)) { | |
| 50 | + return $res; | |
| 51 | + } | |
| 52 | + $res = $oTo->close(); | |
| 53 | + if (PEAR::isError($res)) { | |
| 54 | + return $res; | |
| 55 | + } | |
| 56 | + } else { | |
| 57 | + $oTo->put_contents($oFrom->get_contents()); | |
| 58 | + } | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + | |
| 62 | + function send_contents($oFrom, $bChunking = true) { | |
| 63 | + if ($oFrom->bSupportChunking && $bChunking) { | |
| 64 | + $res = $oFrom->open("r"); | |
| 65 | + if (PEAR::isError($res)) { | |
| 66 | + return $res; | |
| 67 | + } | |
| 68 | + while (!$oFrom->eof()) { | |
| 69 | + $res = $oFrom->read(8192); | |
| 70 | + if (PEAR::isError($res)) { | |
| 71 | + return $res; | |
| 72 | + } | |
| 73 | + print $res; | |
| 74 | + } | |
| 75 | + $res = $oFrom->close(); | |
| 76 | + if (PEAR::isError($res)) { | |
| 77 | + return $res; | |
| 78 | + } | |
| 79 | + } else { | |
| 80 | + print $oFrom->get_contents(); | |
| 81 | + } | |
| 82 | + } | |
| 83 | +} | |
| 84 | + | |
| 85 | +?> | ... | ... |
lib/filelike/fsfilelike.inc.php
0 โ 100644
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * $Id$ | |
| 4 | + * | |
| 5 | + * Filelike wrapper for normal files (and streams too) | |
| 6 | + * | |
| 7 | + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com | |
| 8 | + * | |
| 9 | + * This program is free software; you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation; either version 2 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program; if not, write to the Free Software | |
| 21 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 | + * | |
| 23 | + * @version $Revision$ | |
| 24 | + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa | |
| 25 | + */ | |
| 26 | + | |
| 27 | +require_once(KT_LIB_DIR . '/filelike/filelike.inc.php'); | |
| 28 | + | |
| 29 | +class KTFSFileLike extends KTFileLike { | |
| 30 | + var $bSupportChunking = true; | |
| 31 | + var $fh; | |
| 32 | + var $sFilename; | |
| 33 | + | |
| 34 | + var $bIsFSFile = true; | |
| 35 | + | |
| 36 | + function KTFSFileLike ($sFilename) { | |
| 37 | + $this->sFilename = $sFilename; | |
| 38 | + } | |
| 39 | + | |
| 40 | + function getFSPath() { | |
| 41 | + return $this->sFilename; | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Set up any resources needed to perform work. | |
| 46 | + */ | |
| 47 | + function open($mode = "r") { | |
| 48 | + $this->fh = fopen($this->sFilename, $mode); | |
| 49 | + if ($this->fh === false) { | |
| 50 | + $this->fh = null; | |
| 51 | + return PEAR::raiseError('Error opening file'); | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Take care of getting rid of any active resources. | |
| 57 | + */ | |
| 58 | + function close() { | |
| 59 | + if (is_null($this->fh)) { | |
| 60 | + return PEAR::raiseError('Not open'); | |
| 61 | + } | |
| 62 | + return fclose($this->fh); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Behaves like fread | |
| 67 | + */ | |
| 68 | + function read($iBytes) { | |
| 69 | + return fread($this->fh, $iBytes); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Behaves like fwrite | |
| 74 | + */ | |
| 75 | + function write($sData) { | |
| 76 | + return fwrite($this->fh, $sData); | |
| 77 | + } | |
| 78 | + | |
| 79 | + function get_contents() { | |
| 80 | + return file_get_contents($this->sFilename); | |
| 81 | + } | |
| 82 | + | |
| 83 | + function put_contents($sData) { | |
| 84 | + return file_put_contents($this->sFilename, $sData); | |
| 85 | + } | |
| 86 | + | |
| 87 | + function eof() { | |
| 88 | + return feof($this->fh); | |
| 89 | + } | |
| 90 | + | |
| 91 | + function filesize() { | |
| 92 | + return filesize($this->sFilename); | |
| 93 | + } | |
| 94 | +} | |
| 95 | + | |
| 96 | +?> | ... | ... |