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 int
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: * @param $source string path to the temp directory
66: * @param $filename string name of zip archive
67: * @throws cException if the source file does not exists
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: * @param $destination string
100: * @throws cException if the destination path can not set (directory is not
101: * writable)
102: * @throws cException if the defined destination already exists
103: */
104: public function setDestinationPath($destination) {
105: if (!is_dir($destination)) {
106: $makeDirectory = mkdir($destination, 0777);
107: if ($makeDirectory != true) {
108: throw new cException('Can not set destination path: directoy is not writable');
109: }
110: $this->_destination = (string) $destination;
111: } else {
112: throw new cException('Destination already exists');
113: }
114: }
115:
116: /**
117: * Extracts the whole archive
118: *
119: * @throws cException if the extraction failed
120: */
121: public function extractArchive() {
122: if ($this->_destination != '') {
123: $this->_extractor->extractTo($this->_destination);
124: } else {
125: throw new cException('Extraction failed: no destination path setted');
126: }
127: }
128:
129: /**
130: * Extracts a specific file from archive and return its content to use it in
131: * a variable
132: *
133: * @param $filename string
134: * @param $content bool [optional] whether to return the content or just the
135: * dir and filename of the extracted file
136: * @return string content of extracted file or dir and filename of extracted
137: * File
138: */
139: public function extractArchiveFileToVariable($filename, $content = true) {
140: $filename = (string) $filename;
141: $this->_extractor->extractTo($this->tempDir, $filename);
142:
143: if ($content) {
144: return file_get_contents($this->tempDir . $filename);
145: } else {
146: return $this->tempDir . $filename;
147: }
148: }
149:
150: /**
151: * Destory temporary plugin files (plugin.xml, plugin_install.sql and files
152: * at CONTENIDO temp dir)
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: