-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.platon.php
372 lines (287 loc) · 9.71 KB
/
script.platon.php
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
defined('_JEXEC') or die('Restricted access');
/**
* VirtueMart script file
*
* This file is executed during install/upgrade and uninstall
*
* @author Patrick Kohl, Max Milbers
* @package VirtueMart
*/
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
@ini_set( 'memory_limit', '32M' );
@ini_set( 'max_execution_time', '120' );
// hack to prevent defining these twice in 1.6 installation
if (!defined('_VM_SCRIPT_INCLUDED')) {
define('_VM_SCRIPT_INCLUDED', true);
class com_virtuemart_allinoneInstallerScript {
public function preflight(){
//$this->vmInstall();
}
public function install(){
//$this->vmInstall();
}
public function discover_install(){
//$this->vmInstall();
}
public function postflight () {
$this->vmInstall();
}
public function vmInstall () {
jimport('joomla.filesystem.file');
jimport('joomla.installer.installer');
$this->createIndexFolder(JPATH_ROOT .DS. 'plugins'.DS.'vmpayment');
$this->path = JInstaller::getInstance()->getPath('extension_administrator');
$this->installPlugin('Platon', 'plugin','platon', 'vmpayment');
$task = JRequest::getCmd('task');
if($task!='updateDatabase'){
echo "<H3>Installing Platon Plugin Success.</h3>";
} else {
echo "<H3>Updated Virtuemart Plugin tables</h3>";
}
return true;
}
/**
* Installs a vm plugin into the database
*
*/
private function installPlugin($name, $type, $element, $group){
$task = JRequest::getCmd('task');
if($task!='updateDatabase'){
$data = array();
if(version_compare(JVERSION,'1.7.0','ge')) {
// Joomla! 1.7 code here
$table = JTable::getInstance('extension');
$data['enabled'] = 1;
$data['access'] = 1;
$tableName = '#__extensions';
$idfield = 'extension_id';
} elseif(version_compare(JVERSION,'1.6.0','ge')) {
// Joomla! 1.6 code here
$table = JTable::getInstance('extension');
$data['enabled'] = 1;
$data['access'] = 1;
$tableName = '#__extensions';
$idfield = 'extension_id';
} else {
// Joomla! 1.5 code here
$table = JTable::getInstance('plugin');
$data['published'] = 1;
$data['access'] = 0;
$tableName = '#__plugins';
$idfield = 'id';
}
$data['name'] = $name;
$data['type'] = $type;
$data['element'] = $element;
$data['folder'] = $group;
$data['client_id'] = 0;
$src= $this->path .DS. 'plugins' .DS. $group .DS.$element;
$db = JFactory::getDBO();
$q = 'SELECT '.$idfield.' FROM `'.$tableName.'` WHERE `name` = "'.$name.'" ';
$db->setQuery($q);
$count = $db->loadResult();
//We write only in the table, when it is not installed already
if(empty($count)){
// $table->load($count);
if(version_compare(JVERSION,'1.6.0','ge')) {
$data['manifest_cache'] = json_encode(JApplicationHelper::parseXMLInstallFile($src.DS.$element.'.xml'));
}
if(!$table->bind($data)){
$app = JFactory::getApplication();
$app -> enqueueMessage('VMInstaller table->bind throws error for '.$name.' '.$type.' '.$element.' '.$group);
}
if(!$table->check($data)){
$app = JFactory::getApplication();
$app -> enqueueMessage('VMInstaller table->check throws error for '.$name.' '.$type.' '.$element.' '.$group);
}
if(!$table->store($data)){
$app = JFactory::getApplication();
$app -> enqueueMessage('VMInstaller table->store throws error for '.$name.' '.$type.' '.$element.' '.$group);
}
$errors = $table->getErrors();
foreach($errors as $error){
$app = JFactory::getApplication();
$app -> enqueueMessage( get_class( $this ).'::store '.$error);
}
}
}
if(version_compare(JVERSION,'1.7.0','ge')) {
// Joomla! 1.7 code here
$dst= JPATH_ROOT . DS . 'plugins' .DS. $group.DS.$element;
} elseif(version_compare(JVERSION,'1.6.0','ge')) {
// Joomla! 1.6 code here
$dst= JPATH_ROOT . DS . 'plugins' .DS. $group.DS.$element;
} else {
// Joomla! 1.5 code here
$dst= JPATH_ROOT . DS . 'plugins' .DS. $group;
}
if($task!='updateDatabase'){
$this->recurse_copy( $src ,$dst );
}
if($group!='search') {
$this->updatePluginTable($name, $type, $element, $group, $dst);
} else {
if(version_compare(JVERSION,'1.6.0','ge')){
$this->updatePluginTable($name, $type, $element, $group, $dst);
}
}
}
public function updatePluginTable($name, $type, $element, $group, $dst){
$app = JFactory::getApplication();
//Update Tables
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
if (class_exists( 'VmConfig' )){
$pluginfilename = $dst.DS.$element.'.php';
require ($pluginfilename);
//plgVmpaymentPaypal
$pluginClassname = 'plg'.ucfirst($group).ucfirst($element);
//Let's get the global dispatcher
$dispatcher = JDispatcher::getInstance();
$config = array('type'=>$group,'name'=>$group,'params'=>'');
$plugin = new $pluginClassname($dispatcher,$config);;
// $updateString = $plugin->getVmPluginCreateTableSQL();
//if(function_exists($plugin->getTableSQLFields)){
$_psType = substr($group, 2);
$tablename = '#__virtuemart_'.$_psType .'_plg_'. $element;
$db = JFactory::getDBO();
$query='SHOW TABLES LIKE "%'.str_replace('#__','',$tablename).'"' ;
$db->setQuery($query);
$result = $db->loadResult();
//$app -> enqueueMessage( get_class( $this ).':: '.$query.' '.$result);
if ( $result) {
$SQLfields = $plugin->getTableSQLFields();
$loggablefields = $plugin->getTableSQLLoggablefields();
$tablesFields=array_merge($SQLfields,$loggablefields);
$update[$tablename]= array($tablesFields, array(),array());
$app -> enqueueMessage( get_class( $this ).':: VirtueMart2 update '.$tablename);
if(!class_exists('GenericTableUpdater')) require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'tableupdater.php');
$updater = new GenericTableUpdater();
$updater->updateMyVmTables($update);
}
//}
// } else {
// $app = JFactory::getApplication();
// $app -> enqueueMessage( get_class( $plugin ).':: VirtueMart2 function getTableSQLFields not found');
// }
} else {
$app = JFactory::getApplication();
$app -> enqueueMessage( get_class( $this ).':: VirtueMart2 must be installed, or the tables cant be updated '.$error);
}
}
/**
* @author Max Milbers
* @param string $tablename
* @param string $fields
* @param string $command
*/
private function alterTable($tablename,$fields,$command='CHANGE'){
if(empty($this->db)){
$this->db = JFactory::getDBO();
}
$query = 'SHOW COLUMNS FROM `'.$tablename.'` ';
$this->db->setQuery($query);
$columns = $this->db->loadResultArray(0);
foreach($fields as $fieldname => $alterCommand){
if(in_array($fieldname,$columns)){
$query = 'ALTER TABLE `'.$tablename.'` '.$command.' COLUMN `'.$fieldname.'` '.$alterCommand;
$this->db->setQuery($query);
$this->db->query();
}
}
}
/**
*
* @author Max Milbers
* @param string $table
* @param string $field
* @param string $fieldType
* @return boolean This gives true back, WHEN it altered the table, you may use this information to decide for extra post actions
*/
private function checkAddFieldToTable($table,$field,$fieldType){
$query = 'SHOW COLUMNS FROM `'.$table.'` ';
$this->db->setQuery($query);
$columns = $this->db->loadResultArray(0);
if(!in_array($field,$columns)){
$query = 'ALTER TABLE `'.$table.'` ADD '.$field.' '.$fieldType;
$this->db->setQuery($query);
if(!$this->db->query()){
$app = JFactory::getApplication();
$app->enqueueMessage('Install checkAddFieldToTable '.$this->db->getErrorMsg() );
return false;
} else {
return true;
}
}
return false;
}
/**
* copy all $src to $dst folder and remove it
*
* @author Max Milbers
* @param String $src path
* @param String $dst path
* @param String $type modules, plugins, languageBE, languageFE
*/
private function recurse_copy($src,$dst ) {
$dir = opendir($src);
$this->createIndexFolder($dst);
if(is_resource($dir)){
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src .DS. $file) ) {
$this->recurse_copy($src .DS. $file,$dst .DS. $file);
}
else {
if(JFile::exists($dst .DS. $file)){
if(!JFile::delete($dst .DS. $file)){
$app = JFactory::getApplication();
$app -> enqueueMessage('Couldnt delete '.$dst .DS. $file);
}
}
if(!JFile::move($src .DS. $file,$dst .DS. $file)){
$app = JFactory::getApplication();
$app -> enqueueMessage('Couldnt move '.$src .DS. $file.' to '.$dst .DS. $file);
}
}
}
}
closedir($dir);
if (is_dir($src)) JFolder::delete($src);
} else {
$app = JFactory::getApplication();
$app -> enqueueMessage('Couldnt read dir '.$dir.' source '.$src);
}
}
public function uninstall() {
return true;
}
/**
* creates a folder with empty html file
*
* @author Max Milbers
*
*/
public function createIndexFolder($path){
if(JFolder::create($path)) {
if(!JFile::exists($path .DS. 'index.html')){
JFile::copy(JPATH_ROOT.DS.'components'.DS.'index.html', $path .DS. 'index.html');
}
return true;
}
return false;
}
}
// PLZ look in #vminstall.php# to add your plugin and module
function com_install(){
if(!version_compare(JVERSION,'1.6.0','ge')) {
$vmInstall = new com_virtuemart_allinoneInstallerScript();
$vmInstall->vmInstall();
}
return true;
}
function com_uninstall(){
return true;
}
} //if defined
// pure php no tag