-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstalluninstallscript.php
556 lines (484 loc) · 18.3 KB
/
installuninstallscript.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
/**
* @component J!Track Gallery (jtg) for Joomla! 2.5 and 3.x
*
* @package Com_Jtg
* @subpackage Backend
* @author Christophe Seguinot <christophe@jtrackgallery.net>
* @author Pfister Michael, JoomGPStracks <info@mp-development.de>
* @author Christian Knorr, InJooOSM <christianknorr@users.sourceforge.net>
* @copyright 2015 J!TrackGallery, InJooosm and joomGPStracks teams
*
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
* @link http://jtrackgallery.net/
*
*/
/* This file is based on Joomla script.php and corresponding com_flexicontent install script
Developper !!
Use file /installuninstall.php
Don't use /administrator/componentes/com_jtg//installuninstall.php which is copied by joomla during install
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Installer class for the jtg component
*
* @package Comjtg
* @subpackage Frontend
* @since 0.8
*/
class Com_JtgInstallerScript
{
/**
* $parent is the class calling this method.
* $type is the type of change (install, update or discover_install, not uninstall).
* preflight runs before anything else and while the extracted files are in the uploaded temp folder.
* If preflight returns false, Joomla will abort the update and undo everything already done.
*
* @param string $type the type of change (install, update or discover_install, not uninstall).
* @param object $parent the class calling this method.
*
* @return return_description
*/
public function preflight($type, $parent)
{
// Try to increment some limits
// Execution time 5 minutes
@set_time_limit(240);
// Continue execution if client disconnects
ignore_user_abort(true);
// Load english language file for 'com_jtg' component then override with current language file
JFactory::getLanguage()->load('com_jtg', JPATH_ADMINISTRATOR . '/components/com_jtg/language', 'en-GB', true);
JFactory::getLanguage()->load('com_jtg', JPATH_ADMINISTRATOR . '/components/com_jtg/language', null, true);
$jversion = new JVersion;
// Installing component manifest file version
$this->release = $parent->get('manifest')->version;
// File version of existing manifest file
$this->release_existing = $this->getParam('version');
// Manifest file minimum Joomla version
$this->minimum_joomla_release = $parent->get('manifest')->attributes()->version;
echo '<p> -- ' . JText::sprintf('COM_JTG_PREFLIGHT', $this->release) . '</p>';
if ($this->release_existing)
{
echo '<br /> ' . JText::sprintf('COM_JTG_PREFLIGHT_UPDATING', $this->release_existing, $this->release);
}
else
{
echo '<br /> ' . JText::sprintf('COM_JTG_PREFLIGHT_INSTALLING', $this->release);
}
echo '<br /> ' . JText::sprintf('COM_JTG_PREFLIGHT_MIN_JOOMLA', $this->minimum_joomla_release, $jversion->getShortVersion());
// Abort if the current Joomla release is older
if (version_compare($jversion->getShortVersion(), $this->minimum_joomla_release, 'lt'))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_JTG_PREFLIGHT_MIN_JOOMLA_ABORT', $this->minimum_joomla_release), 'Warning');
return false;
}
// Abort if the component being installed is not newer than the currently installed version
if ( $type == 'update' )
{
$oldRelease = $this->getParam('version');
if ( version_compare($this->release, $oldRelease, 'lt') )
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_JTG_PREFLIGHT_JTG_WRONG_VERSION', $oldRelease, $this->release), 'Warning');
return false;
// TODO this aborts the install process but generates and error in Joomla !!!
}
}
return true;
}
/**
* install runs after the database scripts are executed.
*
* If the extension is new, the install method is run.
* If install returns false, Joomla will abort the install and undo everything already done.
*
* @param object $parent the class calling this method.
*
* @return return_description
*/
public function install( $parent )
{
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
$doc = JFactory::getDocument();
?>
<br />
<img
src="<?php echo JUri::root() . 'components/com_jtg/assets/images/logo_JTG.png'; ?>"
alt="J!Track Gallery" />
<br />
<table class="adminlist" border="1" style="width:100%;">
<tbody>
<tr>
<td><?php JText::_('COM_JTG_INSTALL_LICENCE') ?></td>
</tr>
<tr>
<td><?php JText::_('COM_JTG_CREATE_FOLDERS') ?></td>
</tr>
</tbody>
</table>
<?php
$folders_to_create = array (
"images/jtrackgallery",
"images/jtrackgallery/cats",
"images/jtrackgallery/terrain",
"images/jtrackgallery/language",
"images/jtrackgallery/uploaded_tracks",
"images/jtrackgallery/uploaded_tracks/import"
);
$folders_to_chmod = array (
"images/jtrackgallery/cats",
"images/jtrackgallery/language",
"images/jtrackgallery/uploaded_tracks",
"images/jtrackgallery/uploaded_tracks/import"
);
echo '<table><tr><td colspan="3"><b>' . JText::_('COM_JTG_FILES_FOLDERS_TO_CREATE') . '</td></tr>';
echo '<tr><td>' . JText::_('COM_JTG_FILE') . '/' . JText::_('COM_JTG_FOLDER') . '</td><td>' . JText::_('COM_JTG_NAME') . '</td><td>' . JText::_('COM_JTG_STATE') . '</td></tr>';
foreach ( $folders_to_create AS $folder )
{
if (JFolder::exists(JPATH_SITE . '/' . $folder))
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' .
$folder . '</td><td><font color="green">' .
JText::_('COM_JTG_ALREADY_EXISTS') . '</font></td></tr>';
}
elseif (JFolder::create(JPATH_SITE . '/' . $folder))
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' .
$folder . '</td><td><font color="green">' .
JText::_('COM_JTG_CREATED') . '</font></td></tr>';
}
else
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' .
$folder . '</td><td><font color="red">' .
JText::_('COM_JTG_NOT_CREATED') . '</font></td></tr>';
}
}
foreach ( $folders_to_chmod AS $folder )
{
if ( JPath::canChmod(JPATH_SITE . '/' . $folder) AND (chmod(JPATH_SITE . '/' . $folder, 0777)))
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' .
$folder . '</td><td><font color="green">' .
JText::_('COM_JTG_CHMODDED') . '</font></td></tr>';
}
else
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' .
$folder . '</td><td><font color="red">' .
JText::_('COM_JTG_NOT_CHMODDED') . '</font></td></tr>';
}
}
// Copy Cats image
$src_folder_to_copy = JPATH_SITE . '/components/com_jtg/assets/images/cats';
$dest_folder_to_copy = JPATH_SITE . '/images/jtrackgallery/cats';
$files = JFolder::files($src_folder_to_copy);
// Copy file by file without erasing existing files
foreach ($files as $file)
{
if (!JFile::exists($dest_folder_to_copy . '/' . $file) )
{
JFile::copy($src_folder_to_copy . '/' . $file, $dest_folder_to_copy . '/' . $file);
}
}
// Copy additional.ini language files without erasing existing files
$src_folder_to_copy = JPATH_SITE . '/components/com_jtg/assets/language';
$dest_folder_to_copy = JPATH_SITE . '/images/jtrackgallery/language';
$folders = JFolder::folders($src_folder_to_copy, false);
// For each language folder
foreach ($folders as $folder)
{
$src_folder_to_copy2 = $src_folder_to_copy . '/' . $folder;
$dest_folder_to_copy2 = $dest_folder_to_copy . '/' . $folder;
$files = JFolder::files($src_folder_to_copy2);
JFolder::create($dest_folder_to_copy2);
foreach ($files as $file)
{
if (!JFile::exists($dest_folder_to_copy2 . '/' . $file) )
{
JFile::copy($src_folder_to_copy2 . '/' . $file, $dest_folder_to_copy2 . '/' . $file);
}
}
}
// Copy example tracks without erasing existing files
$src_folder_to_copy = JPATH_SITE . '/components/com_jtg/assets/uploaded_tracks';
$dest_folder_to_copy = JPATH_SITE . '/images/jtrackgallery/uploaded_tracks';
$files = JFolder::files($src_folder_to_copy);
// Copy file by file without erasing existing files
foreach ($files as $file)
{
if (!JFile::exists($dest_folder_to_copy . '/' . $file) )
{
JFile::copy($src_folder_to_copy . '/' . $file, $dest_folder_to_copy . '/' . $file);
}
}
// Copy example image gallery
$src_folder_to_copy = JPATH_SITE . '/components/com_jtg/assets/uploaded_tracks_images';
$dest_folder_to_copy = JPATH_SITE . '/images/jtrackgallery/uploaded_tracks_images';
$folders = JFolder::folders($src_folder_to_copy, 'track_*', false);
// For each uploaded_tracks_images folder
foreach ($folders as $folder)
{
JFolder::copy($src_folder_to_copy . '/' . $folder, $dest_folder_to_copy . '/' . $folder, $force = false);
}
// Copy difficulty level icons
$src_folder_to_copy = JPATH_SITE . '/components/com_jtg/assets/images/difficulty_level';
$dest_folder_to_copy = JPATH_SITE . '/images/jtrackgallery/difficulty_level';
JFolder::copy($src_folder_to_copy, $dest_folder_to_copy, $force = false);
echo '<tr><td colspan="3">' . JText::sprintf('COM_JTG_INSTALLED_VERSION', $this->release) . '</td></tr>';
// Update tracks user ID to current user id
$db = JFactory::getDBO();
$application = JFactory::getApplication();
$uid = JFactory::getUser($row->uid);
$db->setQuery("UPDATE `#__jtg_files` SET `uid` = '$uid' WHERE 1");
$db->execute();
// You can have the backend jump directly to the newly installed component configuration page
// $parent->getParent()->setRedirectURL('index.php?option=com_jtg');
echo '<tr><td colspan="3">';
echo '<font color="red" size="+1">' . JText::_('COM_JTG_HINTS') . '</font>';
echo JText::_('COM_JTG_HINTS_DETAILS');
echo '</td></tr>';
echo '</table>';
return true;
}
/**
* update runs after the database scripts are executed.
*
* If the extension exists, then the update method is run.
* If this returns false, Joomla will abort the update and undo everything already done.
*
* @param object $parent the class calling this method.
*
* @return return_description
*/
public function update( $parent )
{
// This is executed after upgrade.sql
// Upgrading from $oldRelease to $this->release
$oldRelease = $this->getParam('version');
// Bug in 0.9.22 1054 Unknown column 'usepace' in 'field list' SQL=INSERT INTO xxxx_jtg_cats
$db = JFactory::getDBO();
$columns = $db->getTableColumns('#__jtg_cats');
if(!isset($columns['usepace'])){
// The usepace row does not exits
$db->setQuery("ALTER TABLE `#__jtg_cats` ADD `usepace` TINYINT(1) NOT NULL DEFAULT '0' AFTER `ordering`; ");
$db->execute();
}
// If installed version is equal to then 0.9.21 ==> remove plugin plg_jtrackgallery_maps v0.1
$plg_folder = JPATH_SITE . '/plugins/content/plg_jtrackgallery_maps/';
if (JFolder::exists($plg_folder))
{
// Remove old plugin version
JFolder::delete($plg_folder);
$db = JFactory::getDBO();
$application = JFactory::getApplication();
$uid = JFactory::getUser($row->uid);
$db->setQuery("DELETE FROM `#__extensions` WHERE element ='plg_jtrackgallery_maps'");
$db->execute();
}
/*
* Move existing old image gallery
* from /images/jtrackgallery/track_xx (version<= 0.9.9)
* to /images/jtrackgallery/uploaded_tracks_images/track_xx (version> 0.9.9)
*/
$folders = JFolder::folders(JPATH_SITE . '/images/jtrackgallery', '^track*', false);
// Move entire folder (track_xx) if destination folder don't exist
foreach ($folders as $folder)
{
if (!JFolder::exists(JPATH_SITE . '/images/jtrackgallery/uploaded_tracks_images/' . $folder))
{
JFolder::move(JPATH_SITE . '/images/jtrackgallery/' . $folder, JPATH_SITE . '/images/jtrackgallery/uploaded_tracks_images/' . $folder);
}
}
/*
* If it does not exists:
* Create difficulty level icons folder and copy icons in it
*/
$dest_folder_to_copy = JPATH_SITE . '/images/jtrackgallery/difficulty_level';
if (!JFolder::exists($dest_folder_to_copy))
{
JFolder::copy(JPATH_SITE . '/components/com_jtg/assets/images/difficulty_level', $dest_folder_to_copy, '', $force = false);
}
echo '<p>' . JText::sprintf('COM_JTG_UPDATED', $this->release) . '</p>';
// You can have the backend jump directly to the newly updated component configuration page
// $parent->getParent()->setRedirectURL('index.php?option=com_jtg');
return true;
}
/**
* postflight is run after the extension is registered in the database.
*
* @param string $type the type of change (install, update or discover_install, not uninstall).
* @param object $parent the class calling this method.
*
* @return return_description
*/
public function postflight($type, $parent)
{
$db = JFactory::getDBO();
$application = JFactory::getApplication();
// Check if com_jtg is installed,
$query = 'SELECT COUNT(*) FROM #__extensions WHERE name = "com_jtg"';
$db->setQuery($query);
$db->execute();
$componentJtgIsInstalled = $db->loadResult();
if (( $type == 'install' ) and (! $componentJtgIsInstalled) )
{
// This a NON successfull install:
// TODO truncate all com_jtg tables
$application->enqueueMessage('SHOULD TRUNCATE ALL TABLES');
// DROP TABLE `#__jtg_cats`, `#__jtg_cats2`, `#__jtg_comments`, `#__jtg_config`, `#__jtg_files`, `#__jtg_files2`, `#__jtg_maps`, `#__jtg_terrains`, `#__jtg_users`, `#__jtg_votes`;
}
if (( $type == 'install' ) and ($componentJtgIsInstalled !== null) )
{
// This is a successful install (not an upgrade):
// affect sample tracks to current (admin) user
$user = JFactory::getUser();
$uid = $user->id;
$query = 'UPDATE #__jtg_files SET uid =' . $uid . ' WHERE 1';
$db->setQuery($query);
$db->execute();
// TODO Save default params directly in table
$query = 'UPDATE #__extensions SET params = ';
$query .= '\' {
"jtg_param_display_jtg_credits":"1",
"jtg_param_newest":"10",
"jtg_param_mostklicks":"10",
"jtg_param_best":"0",
"jtg_param_rand":"0",
"jtg_param_otherfiles":"0",
"jtg_param_lh":"1",
"jtg_param_disable_terrains":"0",
"jtg_param_vote_show_stars":"0",
"jtg_param_show_speedchart":"1",
"jtg_param_elevation_filter_min_ascent":"10",
"jtg_param_show_heightchart":"1",
"jtg_param_hide_track_info":"0",
"jtg_param_show_durationcalc":"1",
"jtg_param_show_layerswitcher":"1",
"jtg_param_show_panzoombar":"1",
"jtg_param_show_attribution":"1",
"jtg_param_show_mouselocation":"1",
"jtg_param_show_scale":"1",
"jtg_param_allow_mousemove":"1",
"jtg_param_allow_keymove":"0",
"jtg_param_offer_download_original":"1",
"jtg_param_offer_download_gpx":"1",
"jtg_param_offer_download_kml":"1",
"jtg_param_offer_download_tcx":"0",
"jtg_param_tracks":"0",
"jtg_param_cats":["-1"],
"jtg_param_user":["0"],
"jtg_param_usergroup":["-1"],
"jtg_param_terrain":["-1"],
"jtg_param_level_from":"1",
"jtg_param_level_to":"5",
"jtg_param_vote_from":"0",
"jtg_param_vote_to":"10",
"jtg_param_list_icon_max_height":"24"
}\'';
$query .= ' WHERE name = "com_jtg" AND type = "Component"';
$db->setQuery($query);
$db->execute();
}
return true;
}
/**
* function_description
*
* uninstall runs before any other action is taken (file removal or database processing).
*
* @param object $parent is the class calling this method
*
* @return always true
*/
public function uninstall( $parent )
{
// Set a simple message
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
$application = JFactory::getApplication();
$application->enqueueMessage(JText::_('COM_JTG_THANK_YOU_FOR_USING'));
echo '<p>' . JText::_('COM_JTG_UNINSTALLING') . '</p>';
$folders_to_delete = array ('images/jtrackgallery');
$files_to_delete = array ();
echo '<table><tr><td colspan="3"><b>' . JText::_('COM_JTG_FILES_FOLDERS_TO_DELETE') . '</td></tr>';
echo '<tr><td>' . JText::_('COM_JTG_FILE') . '/' . JText::_('COM_JTG_FOLDER') . '</td><td>' . JText::_('COM_JTG_NAME') . '</td><td>' . JText::_('COM_JTG_STATE') . '</td></tr>';
foreach ( $files_to_delete AS $file )
{
if (!JFile::exists($file))
{
echo '<tr><td>' . JText::_('COM_JTG_FILE') . '</td><td>' . $file . '</td><td><font color="green">' . JText::_('COM_JTG_NOT_EXISTS') . ' </font> </td></tr>';
}
elseif (JFile::delete($file))
{
echo '<tr><td>' . JText::_('COM_JTG_FILE') . '</td><td>' . $file . '</td><td><font color="green">' . JText::_('COM_JTG_DELETED') . ' </font> </td></tr>';
}
else
{
echo '<tr><td>' . JText::_('COM_JTG_FILE') . '</td><td>' . $file . '</td><td><font color="red">' . JText::_('COM_JTG_ERROR') . '/' . JText::_('COM_JTG_NOT_DELETED') . '</font> </td></tr>';
}
}
foreach ( $folders_to_delete AS $folder )
{
if (!JFolder::exists(JPATH_SITE . '/' . $folder))
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' . $folder . '</td><td><font color="green">' . JText::_('COM_JTG_NOT_EXISTS') . '</font> </td></tr>';
}
elseif (JFolder::delete(JPATH_SITE . '/' . $folder))
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' . $folder . '</td><td><font color="green">' . JText::_('COM_JTG_DELETED') . '</font> </td></tr>';
}
else
{
echo '<tr><td>' . JText::_('COM_JTG_FOLDER') . '</td><td>' . $folder . '</td><font color="red">' . JText::_('COM_JTG_ERROR') . JText::_('COM_JTG_NOT_DELETED') . '</font> </td></tr>';
}
}
echo '</table>';
return true;
}
/**
* get a variable from the manifest file (actually, from the manifest cache).
*
* @param string $name parameter name
*
* @return parameter value
*/
public function getParam( $name )
{
$db = JFactory::getDbo();
$db->setQuery('SELECT manifest_cache FROM #__extensions WHERE name = "com_jtg"');
$manifest = json_decode($db->loadResult(), true);
return $manifest[ $name ];
}
/**
* function_description
*
* sets parameter values in the component's row of the extension table
*
* @param array $param_array parameters array
*
* @return void
*/
public function setParams($param_array)
{
if ( count($param_array) > 0 )
{
// Read the existing component value(s)
$db = JFactory::getDbo();
$db->setQuery('SELECT params FROM #__extensions WHERE name = "com_jtg"');
$params = json_decode($db->loadResult(), true);
// Add the new variable(s) to the existing one(s)
foreach ( $param_array as $name => $value )
{
$params[ (string) $name ] = (string) $value;
}
// Store the combined new and existing values back as a JSON string
$paramsString = json_encode($params);
$db->setQuery('UPDATE #__extensions SET params = ' .
$db->quote($paramsString) .
' WHERE name = "com_jtg"' );
$db->execute();
}
}
}