| 1 |
<?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
class Archive_Core { |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
protected $paths; |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
protected $driver; |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
* Loads the archive driver. |
|---|
| 22 |
* |
|---|
| 23 |
* @throws Kohana_Exception |
|---|
| 24 |
* @param string type of archive to create |
|---|
| 25 |
* @return void |
|---|
| 26 |
*/ |
|---|
| 27 |
public function __construct($type = NULL) |
|---|
| 28 |
{ |
|---|
| 29 |
$type = empty($type) ? 'zip' : $type; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
$driver = 'Archive_'.ucfirst($type).'_Driver'; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
if ( ! Kohana::auto_load($driver)) |
|---|
| 36 |
throw new Kohana_Exception('core.driver_not_found', $type, get_class($this)); |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
$this->driver = new $driver(); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
if ( ! ($this->driver instanceof Archive_Driver)) |
|---|
| 43 |
throw new Kohana_Exception('core.driver_implements', $type, get_class($this), 'Archive_Driver'); |
|---|
| 44 |
|
|---|
| 45 |
Log::add('debug', 'Archive Library initialized'); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* Adds files or directories, recursively, to an archive. |
|---|
| 50 |
* |
|---|
| 51 |
* @param string file or directory to add |
|---|
| 52 |
* @param string name to use for the given file or directory |
|---|
| 53 |
* @param bool add files recursively, used with directories |
|---|
| 54 |
* @return object |
|---|
| 55 |
*/ |
|---|
| 56 |
public function add($path, $name = NULL, $recursive = NULL) |
|---|
| 57 |
{ |
|---|
| 58 |
|
|---|
| 59 |
$path = str_replace('\\', '/', $path); |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
empty($name) and $name = $path; |
|---|
| 63 |
|
|---|
| 64 |
if (file_exists($path) AND is_dir($path)) |
|---|
| 65 |
{ |
|---|
| 66 |
|
|---|
| 67 |
$path = rtrim($path, '/').'/'; |
|---|
| 68 |
$name = rtrim($name, '/').'/'; |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
$this->paths[] = array($path, $name); |
|---|
| 72 |
|
|---|
| 73 |
if ($recursive === TRUE) |
|---|
| 74 |
{ |
|---|
| 75 |
$dir = opendir($path); |
|---|
| 76 |
while (($file = readdir($dir)) !== FALSE) |
|---|
| 77 |
{ |
|---|
| 78 |
|
|---|
| 79 |
if (substr($file, 0, 1) === '.') |
|---|
| 80 |
continue; |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
$this->add($path.$file, $name.$file, TRUE); |
|---|
| 84 |
} |
|---|
| 85 |
closedir($dir); |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
else |
|---|
| 89 |
{ |
|---|
| 90 |
$this->paths[] = array($path, $name); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
return $this; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
* Creates an archive and saves it into a file. |
|---|
| 98 |
* |
|---|
| 99 |
* @throws Kohana_Exception |
|---|
| 100 |
* @param string archive filename |
|---|
| 101 |
* @return boolean |
|---|
| 102 |
*/ |
|---|
| 103 |
public function save($filename) |
|---|
| 104 |
{ |
|---|
| 105 |
|
|---|
| 106 |
$directory = pathinfo($filename, PATHINFO_DIRNAME); |
|---|
| 107 |
|
|---|
| 108 |
if ( ! is_writable($directory)) |
|---|
| 109 |
throw new Kohana_Exception('archive.directory_unwritable', $directory); |
|---|
| 110 |
|
|---|
| 111 |
if (file_exists($filename)) |
|---|
| 112 |
{ |
|---|
| 113 |
|
|---|
| 114 |
if ( ! is_writable($filename)) |
|---|
| 115 |
throw new Kohana_Exception('archive.filename_conflict', $filename); |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
unlink($filename); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
return $this->driver->create($this->paths, $filename); |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
* Creates a raw archive file and returns it. |
|---|
| 126 |
* |
|---|
| 127 |
* @return string |
|---|
| 128 |
*/ |
|---|
| 129 |
public function create() |
|---|
| 130 |
{ |
|---|
| 131 |
return $this->driver->create($this->paths); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
* Forces a download of a created archive. |
|---|
| 136 |
* |
|---|
| 137 |
* @param string name of the file that will be downloaded |
|---|
| 138 |
* @return void |
|---|
| 139 |
*/ |
|---|
| 140 |
public function download($filename) |
|---|
| 141 |
{ |
|---|
| 142 |
download::force($filename, $this->driver->create($this->paths)); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
} |
|---|