00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2007 by Ivan Y. Khvostishkov * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00015 abstract class FileArchive 00016 { 00017 protected $cmdBinPath = null; 00018 protected $sourceFile = null; 00019 00020 abstract public function readFile($fileName); 00021 00022 public function __construct($cmdBinPath = null) 00023 { 00024 if ($cmdBinPath !== null) { 00025 if (!is_executable($cmdBinPath)) 00026 throw new WrongStateException( 00027 'cannot find executable '.$cmdBinPath 00028 ); 00029 00030 $this->cmdBinPath = $cmdBinPath; 00031 } 00032 } 00033 00037 public function open($sourceFile) 00038 { 00039 if (!is_readable($sourceFile)) 00040 throw new WrongStateException( 00041 'cannot open file '.$sourceFile 00042 ); 00043 00044 $this->sourceFile = $sourceFile; 00045 00046 return $this; 00047 } 00048 00049 protected function execStdoutOptions($options) 00050 { 00051 if (!$this->cmdBinPath) 00052 throw new WrongStateException( 00053 'nothing to exec' 00054 ); 00055 00056 $cmd = escapeshellcmd($this->cmdBinPath.' '.$options); 00057 00058 ob_start(); 00059 00060 $exitStatus = null; 00061 00062 passthru($cmd.' 2>/dev/null', $exitStatus); 00063 00064 $output = ob_get_clean(); 00065 00066 if ($exitStatus != 0) 00067 throw new ArchiverException( 00068 $this->cmdBinPath.' failed with error code = '.$exitStatus 00069 ); 00070 00071 return $output; 00072 } 00073 } 00074 ?>