1: <?php
2: /**
3: * This file contains the Extractor for plugin archive files
4: *
5: * @package Plugin
6: * @subpackage PluginManager
7: * @author Frederic Schneider
8: * @copyright four for business AG <www.4fb.de>
9: * @license http://www.contenido.org/license/LIZENZ.txt
10: * @link http://www.4fb.de
11: * @link http://www.contenido.org
12: */
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: /**
17: * Extractor for plugin archive files
18: *
19: * @package Plugin
20: * @subpackage PluginManager
21: * @author Frederic Schneider
22: */
23: class PimPluginArchiveExtractor {
24:
25: /**
26: * The extractor initializer
27: *
28: * @var int
29: */
30: protected $_extractor = 0;
31:
32: /**
33: * The temp dir
34: *
35: * @var string
36: */
37: protected $tempDir = '';
38:
39: /**
40: * The archive file
41: *
42: * @var string
43: */
44: protected $_source = '';
45:
46: /**
47: * The destination path
48: *
49: * @var string
50: */
51: protected $_destination = '';
52:
53: /**
54: * The absolute path
55: *
56: * @var string
57: */
58: protected $_absPath = '';
59:
60: /**
61: * Constructor of ArchiveExtractor, load the file list
62: *
63: * @param string $source path to the temp directory
64: * @param string $filename name of zip archive
65: *
66: * @throws cException if the source file does not exists
67: */
68: public function __construct($source, $filename) {
69: $cfg = cRegistry::getConfig();
70:
71: // initialzing ziparchive
72: $this->_extractor = new ZipArchive();
73:
74: // path to temp directory
75: $this->tempDir = $source;
76:
77: // temp directory with zip archive
78: $this->_source = (string) $source . (string) $filename;
79:
80: if (file_exists($source)) {
81: // generate absolute path to the plugin manager directory
82: $this->_absPath = $cfg['path']['contenido'] . $cfg['path']['plugins'] . 'pim' . DIRECTORY_SEPARATOR;
83:
84: // open the zip archive
85: $this->_extractor->open($this->_source);
86: } else {
87: throw new cException('Source file does not exists');
88: }
89: }
90:
91: public function closeArchive() {
92: $this->_extractor->close();
93: }
94:
95: /**
96: * Sets the path where the extractor extracts the archive files
97: *
98: * @param string $destination string
99: *
100: * @throws cException if the destination path can not set (directory is not writable)
101: * @throws cException if the defined destination already exists
102: */
103: public function setDestinationPath($destination) {
104: if (!is_dir($destination)) {
105: $makeDirectory = mkdir($destination, cDirHandler::getDefaultPermissions());
106: if ($makeDirectory != true) {
107: throw new cException('Can not set destination path: directoy is not writable');
108: }
109: $this->_destination = (string) $destination;
110: } else {
111: throw new cException('Destination already exists');
112: }
113: }
114:
115: /**
116: * Extracts the whole archive
117: *
118: * @throws cException if the extraction failed
119: */
120: public function extractArchive() {
121: if ($this->_destination != '') {
122: $this->_extractor->extractTo($this->_destination);
123: } else {
124: throw new cException('Extraction failed: no destination path setted');
125: }
126: }
127:
128: /**
129: * Extracts a specific file from archive and return its content to use it in
130: * a variable
131: *
132: * @param string $filename
133: * @param bool $content [optional] whether to return the content or just the
134: * dir and filename of the extracted file
135: * @return string content of extracted file or dir and filename of extracted File
136: */
137: public function extractArchiveFileToVariable($filename, $content = true) {
138: $filename = (string) $filename;
139: $this->_extractor->extractTo($this->tempDir, $filename);
140:
141: if ($content) {
142: return file_get_contents($this->tempDir . $filename);
143: } else {
144: return $this->tempDir . $filename;
145: }
146: }
147:
148: /**
149: * Destory temporary plugin files (plugin.xml, plugin_install.sql and files
150: * at CONTENIDO temp dir)
151: *
152: * @throws cInvalidArgumentException
153: */
154: public function destroyTempFiles() {
155:
156: // remove plugin.xml if exists
157: if (cFileHandler::exists($this->tempDir . 'plugin.xml')) {
158: cFileHandler::remove($this->tempDir . 'plugin.xml');
159: }
160:
161: // remove plugin_install.sql if exists
162: if (cFileHandler::exists($this->tempDir . 'plugin_install.sql')) {
163: cFileHandler::remove($this->tempDir . 'plugin_install.sql');
164: }
165:
166: // remove temporary plugin dir if exists
167: if (cFileHandler::exists($this->_source)) {
168: cFileHandler::remove($this->_source);
169: }
170: }
171:
172: }
173: