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 $source string path to the temp directory
64: * @param $filename string name of zip archive
65: * @throws cException if the source file does not exists
66: */
67: public function __construct($source, $filename) {
68: $cfg = cRegistry::getConfig();
69:
70: // initialzing ziparchive
71: $this->_extractor = new ZipArchive();
72:
73: // path to temp directory
74: $this->tempDir = $source;
75:
76: // temp directory with zip archive
77: $this->_source = (string) $source . (string) $filename;
78:
79: if (file_exists($source)) {
80: // generate absolute path to the plugin manager directory
81: $this->_absPath = $cfg['path']['contenido'] . $cfg['path']['plugins'] . 'pim' . DIRECTORY_SEPARATOR;
82:
83: // open the zip archive
84: $this->_extractor->open($this->_source);
85: } else {
86: throw new cException('Source file does not exists');
87: }
88: }
89:
90: public function closeArchive() {
91: $this->_extractor->close();
92: }
93:
94: /**
95: * Sets the path where the extractor extracts the archive files
96: *
97: * @param $destination string
98: * @throws cException if the destination path can not set (directory is not
99: * writable)
100: * @throws cException if the defined destination already exists
101: */
102: public function setDestinationPath($destination) {
103: if (!is_dir($destination)) {
104: $makeDirectory = mkdir($destination, 0777);
105: if ($makeDirectory != true) {
106: throw new cException('Can not set destination path: directoy is not writable');
107: }
108: $this->_destination = (string) $destination;
109: } else {
110: throw new cException('Destination already exists');
111: }
112: }
113:
114: /**
115: * Extracts the whole archive
116: *
117: * @throws cException if the extraction failed
118: */
119: public function extractArchive() {
120: if ($this->_destination != '') {
121: $this->_extractor->extractTo($this->_destination);
122: } else {
123: throw new cException('Extraction failed: no destination path setted');
124: }
125: }
126:
127: /**
128: * Extracts a specific file from archive and return its content to use it in
129: * a variable
130: *
131: * @param $filename string
132: * @param $content bool [optional] whether to return the content or just the
133: * dir and filename of the extracted file
134: * @return string content of extracted file or dir and filename of extracted
135: * 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: public function destroyTempFiles() {
153:
154: // remove plugin.xml if exists
155: if (cFileHandler::exists($this->tempDir . 'plugin.xml')) {
156: cFileHandler::remove($this->tempDir . 'plugin.xml');
157: }
158:
159: // remove plugin_install.sql if exists
160: if (cFileHandler::exists($this->tempDir . 'plugin_install.sql')) {
161: cFileHandler::remove($this->tempDir . 'plugin_install.sql');
162: }
163:
164: // remove temporary plugin dir if exists
165: if (cFileHandler::exists($this->_source)) {
166: cFileHandler::remove($this->_source);
167: }
168: }
169:
170: }
171: