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