sources.php
9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
// namespace administrator\components\com_jmap\framework\files;
/**
* @package JMAP::FRAMEWORK::administrator::components::com_jmap
* @subpackage framework
* @subpackage file
* @author Joomla! Extensions Store
* @copyright (C) 2015 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined('_JEXEC') or die('Restricted access');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');
/**
* Multilanguage fallback utility class
*
* @package JMAP::FRAMEWORK::administrator::components::com_jmap
* @subpackage framework
* @subpackage file
* @since 3.0
*/
class JMapFileSources extends JObject {
/**
* Database connector
*
* @access private
* @var Object
*/
private $dbo;
/**
* Application object
*
* @access private
* @var Object
*/
private $app;
/**
* Variables in request array
*
* @access protected
* @var Object
*/
protected $requestArray;
/**
* Variables in request array name
*
* @access protected
* @var Object
*/
protected $requestName;
/**
* Store uploaded file to cache folder,
* fully manage error messages and ask for database insert
*
* @access public
* @return boolean
*/
public function import() {
// Get file info
$file = $this->app->input->files->get('datasourceimport', null, 'raw');
$tmpFile = $file['tmp_name'];
$tmpFileName = $file['name'];
try {
if(!$tmpFile || !$tmpFileName) {
throw new JMapException(JText::_('COM_JMAP_NOFILE_SELECTED'), 'error');
}
$tmpFileSize = $file['size'];
$allowedFileSize = 2 * 1024 * 1024; // MB->Bytes
if($tmpFileSize > $allowedFileSize) {
throw new JMapException(JText::_('COM_JMAP_SIZE_ERROR') .' Max 2MB.', 'error');
}
$tmpFileExtension = @array_pop(explode('.', $tmpFileName));
if($tmpFileExtension != 'json') {
throw new JMapException(JText::_('COM_JMAP_EXT_ERROR'), 'error');
}
// Deserialize contents
$fileContents = file_get_contents($tmpFile);
if($fileContents) {
$objectToRestore = json_decode($fileContents);
}
if(!is_array($objectToRestore)) {
throw new JMapException(JText::_('COM_JMAP_INVALID_IMPORT_DATA'), 'error');
}
// Prepare the values array
$dbQueryArray = array();
foreach ($objectToRestore as $dataSource) {
// Check if the data source is of type plugin and if the plugin is installed, otherwise skip and warn user
if($dataSource->type == 'plugin' && !JFolder::exists(JPATH_COMPONENT_ADMINISTRATOR . '/plugins/' . strtolower($dataSource->name))) {
$this->app->enqueueMessage(JText::sprintf('COM_JMAP_NOPLUGIN_INSTALLED_IMPORTED_DATASOURCE', $dataSource->name));
continue;
}
$dbSourceArray = array();
$dbSourceArray[] = $this->dbo->quote($dataSource->type);
$dbSourceArray[] = $this->dbo->quote(strip_tags($dataSource->name));
$dbSourceArray[] = $this->dbo->quote(strip_tags($dataSource->description));
$dbSourceArray[] = (int)$dataSource->published;
$dbSourceArray[] = $this->dbo->quote($dataSource->sqlquery);
$dbSourceArray[] = $this->dbo->quote($dataSource->sqlquery_managed);
$dbSourceArray[] = $this->dbo->quote($dataSource->params);
// Source imploded assignment
$dbQueryArray[] = implode(',', $dbSourceArray);
}
// Check if some valid data to import are available
if(!count($dbQueryArray)) {
throw new JMapException(JText::_('COM_JMAP_NO_IMPORT_DATA_FOUND'), 'warning');
}
// Final sources imploded assignment
$implodedSources = '(' . implode('),(', $dbQueryArray) . ')';
$queryImport = "INSERT INTO" . $this->dbo->quoteName('#__jmap') .
"\n (" .
"\n" . $this->dbo->quoteName('type') . "," .
"\n" . $this->dbo->quoteName('name') . "," .
"\n" . $this->dbo->quoteName('description') . "," .
"\n" . $this->dbo->quoteName('published') . "," .
"\n" . $this->dbo->quoteName('sqlquery') . "," .
"\n" . $this->dbo->quoteName('sqlquery_managed') . "," .
"\n" . $this->dbo->quoteName('params') . ")" .
"\n VALUES " . $implodedSources;
$this->dbo->setQuery($queryImport);
$this->dbo->execute();
if($this->dbo->getErrorNum()) {
throw new JMapException(JText::sprintf('COM_JMAP_DBERROR_IMPORT_DATA', $this->dbo->getErrorMsg()), 'error');
}
}
catch(JMapException $e) {
$this->setError($e);
return false;
} catch (Exception $e) {
$jmapException = new JMapException($e->getMessage(), 'error');
$this->setError($jmapException);
return false;
}
return true;
}
/**
* Download uploaded file message
*
* @access public
* @return boolean
*/
public function export($ids = null) {
// Load data from DB
try {
$query = "SELECT * FROM #__jmap" .
"\n WHERE id IN ( " . implode(',', $ids) . ")";
$this->dbo->setQuery($query);
$resultInfo = $this->dbo->loadObjectList();
if(!$resultInfo) {
if(!$resultInfo) {
throw new JMapException(JText::_('COM_JMAP_ERROR_NODATA_TOEXPORT'), 'error');
}
}
// Serialize data to export
$dataToExport = json_encode($resultInfo);
} catch(JMapException $e) {
$this->setError($e);
return false;
} catch (Exception $e) {
$jmapException = new JMapException($e->getMessage(), 'error');
$this->setError($jmapException);
return false;
}
$fsize = strlen($dataToExport);
$cont_dis = 'attachment';
$mimeType = 'application/json';
// required for IE, otherwise Content-disposition is ignored
if (ini_get ( 'zlib.output_compression' )) {
ini_set ( 'zlib.output_compression', 'Off' );
}
header ( "Pragma: public" );
header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header ( "Expires: 0" );
header ( "Content-Transfer-Encoding: binary" );
header ( 'Content-Disposition:' . $cont_dis . ';' . ' filename="datasources.json";' . ' size=' . $fsize . ';' ); //RFC2183
header ( "Content-Type: " . $mimeType ); // MIME type
header ( "Content-Length: " . $fsize );
if (! ini_get ( 'safe_mode' )) { // set_time_limit doesn't work in safe mode
@set_time_limit ( 0 );
}
// No encoding - we aren't using compression... (RFC1945)
//header("Content-Encoding: none");
//header("Vary: none");
echo $dataToExport;
exit();
}
/**
* Install a plugin like data source with source code and manifest zip package
*
* @access public
* @return boolean
*/
public function install() {
// Get file info
$file = $this->app->input->files->get('datasourceinstallplugin', null, 'raw');
$tmpFile = $file['tmp_name'];
$tmpFileName = $file['name'];
try {
if(!$tmpFile || !$tmpFileName) {
throw new JMapException(JText::_('COM_JMAP_NOFILE_SELECTED'), 'error');
}
$tmpFileSize = $file['size'];
$allowedFileSize = 2 * 1024 * 1024; // MB->Bytes
if($tmpFileSize > $allowedFileSize) {
throw new JMapException(JText::_('COM_JMAP_SIZE_ERROR') .' Max 2MB.', 'error');
}
$tmpFileExtension = @array_pop(explode('.', $tmpFileName));
if($tmpFileExtension != 'zip') {
throw new JMapException(JText::_('COM_JMAP_EXT_PLUGIN_ERROR'), 'error');
}
// Get the folder name to create, if existing delete it
$folderPluginName = strtolower(@array_shift(explode('.', $tmpFileName)));
$folderPluginPath = JPATH_COMPONENT_ADMINISTRATOR . '/plugins/' . $folderPluginName;
if(JFolder::exists($folderPluginPath)) {
JFolder::delete($folderPluginPath);
}
JFolder::create($folderPluginPath);
// Copy the zip archive to the target directory
if(!move_uploaded_file($tmpFile, $folderPluginPath . '/' . $tmpFileName)) {
throw new JMapException(JText::_('COM_JMAP_UPLOAD_ERROR'), 'error');
}
// Unpack the zip archive
JArchive::extract($folderPluginPath . '/' . $tmpFileName, $folderPluginPath);
// Delete the original zip archive
JFile::delete($folderPluginPath . '/' . $tmpFileName);
// Parse the XML manifest file of the plugin
$manifestFiles = glob( $folderPluginPath . '/*.xml');
if(!count($manifestFiles)) {
throw new JMapException(JText::_('COM_JMAP_ERROR_MANIFEST_NOTFOUND'), 'error');
}
$pluginManifestFile = $manifestFiles[0];
$pluginManifest = simplexml_load_file($pluginManifestFile);
// Get plugin name and descriptions
$pluginName = $pluginManifest->name;
$pluginDescription = $pluginManifest->description;
// Get plugin params
$pluginParams = $pluginManifest->config->fields->fieldset->field;
// Inject everything in the POST request and prepare for the save task
$this->requestArray[$this->requestName]['name'] = ucfirst($pluginName);
$this->requestArray[$this->requestName]['description'] = (string)$pluginDescription;
foreach ($pluginParams as $field) {
// Store in superglobal POST array
$fieldName = (string)$field->attributes()->name;
$fieldValue = (string)$field->attributes()->default;
$this->requestArray[$this->requestName]['params'][$fieldName] = $fieldValue;
}
// Check if plugin folder name is the same as specified in the manifest
if($folderPluginName != strtolower($pluginName)) {
// Delete if previously created folder so always overwrite on multiple installations
if(JFolder::exists(JPATH_COMPONENT_ADMINISTRATOR . '/plugins/' . strtolower($pluginName))) {
JFolder::delete(JPATH_COMPONENT_ADMINISTRATOR . '/plugins/' . strtolower($pluginName));
}
rename($folderPluginPath, JPATH_COMPONENT_ADMINISTRATOR . '/plugins/' . strtolower($pluginName));
}
}
catch(JMapException $e) {
$this->setError($e);
return false;
} catch (Exception $e) {
$jmapException = new JMapException($e->getMessage(), 'error');
$this->setError($jmapException);
return false;
}
return true;
}
/**
* Class constructor
*
* @access public
* @param Object $dbo
* @param Object $app
* @return Object &
*/
public function __construct($dbo, $app) {
// DB connector
$this->dbo = $dbo;
// Application
$this->app = $app;
$this->requestArray = &$GLOBALS;
$this->requestName = '_' . strtoupper('post');
}
}