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