PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.5
VikBooking Hotel Booking Engine & PMS v1.6.5
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / libraries / update / manager.php

manager.php in VikBooking Hotel Booking Engine & PMS 1.6.5, at libraries/update/manager.php

799 lines 22.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking - Libraries
4 * @subpackage update
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2018 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 JLoader::import('adapter.database.helper');
15 VikBookingLoader::import('update.changelog');
16 VikBookingLoader::import('update.license');
17
18 /**
19 * Class used to handle the upgrade of the plugin.
20 *
21 * @since 1.0
22 */
23 class VikBookingUpdateManager
24 {
25
26 /**
27 * For bc with PHP < 5.6, the private static var is now
28 * returned by this private method, because class properties
29 * do not support concatenation, and so we can't build the array.
30 *
31 * @return array the list of template files used as Options to temporary store modifications.
32 *
33 * @since 1.0.9
34 */
35 private static function getTemplateFiles()
36 {
37 return array(
38 VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'email_tmpl.php',
39 VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'error_form.php',
40 VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'invoices' . DIRECTORY_SEPARATOR . 'invoice_tmpl.php',
41 VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'invoices' . DIRECTORY_SEPARATOR . 'custom_invoice_tmpl.php',
42 VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'checkins' . DIRECTORY_SEPARATOR . 'checkin_tmpl.php',
43 VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'vikbooking_custom.css',
44 VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'vikbooking_backendcustom.css'
45 );
46 }
47
48 /**
49 * Checks if the current version should be updated.
50 *
51 * @param string $version The version to check.
52 *
53 * @return boolean True if should be updated, otherwise false.
54 */
55 public static function shouldUpdate($version)
56 {
57 if (is_null($version))
58 {
59 return false;
60 }
61
62 return version_compare($version, VIKBOOKING_SOFTWARE_VERSION, '<');
63 }
64
65 /**
66 * Executes the SQL file for the installation of the plugin.
67 *
68 * @return void
69 *
70 * @uses execSqlFile()
71 * @uses installAcl()
72 * @uses installProSettings()
73 * @uses installUploadBackup()
74 */
75 public static function install()
76 {
77 self::execSqlFile(VIKBOOKING_BASE . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR . 'install.mysql.utf8.sql');
78
79 $dbo = JFactory::getDbo();
80
81 // create the configuration record with the email address of the current user
82 $q = "INSERT INTO `#__vikbooking_config` (`param`,`setting`) VALUES ('adminemail', " . $dbo->q(JFactory::getUser()->email) . ");";
83 $dbo->setQuery($q);
84 $dbo->execute();
85
86 // footer must be disabled by default
87 $q = "UPDATE `#__vikbooking_config` SET `setting`='0' WHERE `param`='showfooter';";
88 $dbo->setQuery($q);
89 $dbo->execute();
90
91 // closing main text must not mention the name of the software
92 $q = "UPDATE `#__vikbooking_texts` SET `setting`='' WHERE `param`='closingmain';";
93 $dbo->setQuery($q);
94 $dbo->execute();
95
96 // truncate the payment gateways table
97 $dbo->setQuery("TRUNCATE TABLE `#__vikbooking_gpayments`");
98 $dbo->execute();
99
100 self::installAcl();
101 self::installProSettings();
102 self::installUploadBackup();
103 }
104
105 /**
106 * Executes the SQL file for the uninstallation of the plugin.
107 *
108 * @param boolean $drop True to drop the tables of VikBooking from the database.
109 *
110 * @return void
111 *
112 * @uses execSqlFile()
113 * @uses uninstallAcl()
114 * @uses uninstallProSettings()
115 * @uses uninstallTemplateContents()
116 * @uses uninstallUploadBackup()
117 */
118 public static function uninstall($drop = true)
119 {
120 if ($drop)
121 {
122 self::execSqlFile(VIKBOOKING_BASE . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR . 'uninstall.mysql.utf8.sql');
123 }
124
125 self::uninstallAcl();
126 self::uninstallProSettings();
127 self::uninstallTemplateContents();
128 self::uninstallUploadBackup();
129 }
130
131 /**
132 * Launches the process to finalise the update.
133 *
134 * @param string $version The current version.
135 *
136 * @uses getFixer()
137 * @uses installSql()
138 * @uses installAcl()
139 * @uses restoreTemplateFiles()
140 * @uses restoreUploadBackup()
141 */
142 public static function update($version)
143 {
144 $fixer = self::getFixer($version);
145
146 // trigger before installation routine
147
148 $res = $fixer->beforeInstallation();
149
150 if ($res === false)
151 {
152 return false;
153 }
154
155 // install SQL statements
156
157 $res = self::installSql($version);
158
159 if ($res === false)
160 {
161 return false;
162 }
163
164 // install ACL
165
166 $res = self::installAcl();
167
168 if ($res === false)
169 {
170 return false;
171 }
172
173 // move template files
174 self::restoreTemplateFiles();
175
176 // restore files uploaded
177 self::restoreUploadBackup();
178
179 // trigger after installation routine
180
181 $res = $fixer->afterInstallation();
182
183 return ($res === false ? false : true);
184 }
185
186 /**
187 * Reads from the Options if the content of a template file
188 * was previously modified. In this case, it restores the
189 * modifications onto the template file which is always
190 * overwritten during updates and upgrades.
191 *
192 * @return void
193 *
194 * @since 1.0.3
195 */
196 public static function restoreTemplateFiles()
197 {
198 jimport('joomla.filesystem.file');
199
200 foreach (self::getTemplateFiles() as $file)
201 {
202 // the file base name as used in the Options
203 $base_file = basename($file);
204 $base_file = substr($base_file, 0, strrpos($base_file, '.'));
205
206 // get override value from Options
207 $tmp_val = get_option('vikbooking_tmp_'.$base_file, null);
208 if (!is_null($tmp_val))
209 {
210 JFile::write($file, $tmp_val);
211 }
212 }
213 }
214
215 /**
216 * Updates an Option with the content of the modified
217 * template file for later use after updates/upgrades.
218 *
219 * @param string $path the template file full path or just its name
220 * @param string $content the raw content to put in the Option
221 *
222 * @return boolean
223 *
224 * @since 1.0.3
225 */
226 public static function storeTemplateContent($path, $content)
227 {
228 $base_path = basename($path);
229
230 foreach (self::getTemplateFiles() as $file)
231 {
232 // template file names are unique no matter of the directory
233 $base_file = basename($file);
234 if ($base_path == $base_file) {
235 // template file found
236 $base_file = substr($base_file, 0, strrpos($base_file, '.'));
237 // update the Option and return
238 update_option('vikbooking_tmp_'.$base_file, $content);
239
240 return true;
241 }
242 }
243
244 return false;
245 }
246
247 /**
248 * Removes some Options that were used to hold
249 * modifications made to the template files.
250 *
251 * @return void
252 */
253 public static function uninstallTemplateContents()
254 {
255 foreach (self::getTemplateFiles() as $file)
256 {
257 // the file base name as used in the Options
258 $base_file = basename($file);
259 $base_file = substr($base_file, 0, strrpos($base_file, '.'));
260
261 // remove entry from Options
262 delete_option('vikbooking_tmp_'.$base_file);
263 }
264 }
265
266 /**
267 * Returns a pair of key-values for each directory used
268 * for handling the backup of the uploaded files.
269 * All paths are missing the trailing directory separator.
270 * Gets the backup upload dir paths by default, or the
271 * real plugin dir paths if $real_path is true.
272 * Used during the installation, uninstallation and backup (post-update).
273 *
274 * @param boolean [$real_paths] If True, the real paths used by the plugin will be returned.
275 * The path of the backup folders will be returned otherwise.
276 *
277 * @return array An array of strings with the path of each dir
278 */
279 protected static function getUploadBackupDirs($real_paths = false)
280 {
281 if ($real_paths) {
282 // return the real upload dir paths used by the plugin
283 return array(
284 // main upload path for the images of the rooms, options, characteristics and such...
285 'front' => VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'uploads',
286 // upload dir path for the customers check-in documents
287 'checkins' => VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'checkins' . DIRECTORY_SEPARATOR . 'generated',
288 // upload dir path for the invoices in PDF format
289 'invoices' => VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'invoices' . DIRECTORY_SEPARATOR . 'generated',
290 // upload dir path for the scans of the ID/Documents of the customers
291 'idscans' => VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'idscans',
292 // dir path to extend the reports
293 'report' => VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'report',
294 // dir path to extend the SMS APIs
295 'smsapi' => VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'smsapi',
296 // dir path to custom language files
297 'languages' => VIKBOOKING_BASE . DIRECTORY_SEPARATOR . 'languages',
298 // upload dir path for the admin section, for custom logos and such... (we keep the smaller admin path at the end of the array)
299 'admin' => VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'resources',
300 );
301 }
302
303 // get the array information of the upload dir
304 $upload_dir = wp_upload_dir();
305 if (!is_array($upload_dir) || empty($upload_dir['basedir'])) {
306 return false;
307 }
308
309 // keep the keys of this array, as they are used as a map with other methods
310 return array(
311 // base directory that will contain the sub-directories divided by category
312 'base' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking',
313 // main upload path for the images of the rooms, options, characteristics and such...
314 'front' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'front',
315 // upload dir path for the the customers check-in documents
316 'checkins' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'checkins',
317 // upload dir path for the invoices in PDF format
318 'invoices' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'invoices',
319 // upload dir path for the scans of the ID/Documents of the customers
320 'idscans' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'idscans',
321 // dir path to extend the reports
322 'report' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'report',
323 // dir path to extend the SMS APIs
324 'smsapi' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'smsapi',
325 // dir path to custom language files
326 'languages' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'languages',
327 // upload dir path for the admin section, for custom logos and such...
328 'admin' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'admin',
329 // views overrides dir path: this dir should NOT be backed up, so this key should ONLY be in this array
330 'overrides' => $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'overrides',
331 // media dir path: this dir should NOT be backed up, so this key should ONLY be in this array
332 'media' => VBO_MEDIA_PATH,
333 // customer docs dir path: this dir should NOT be backed up, so this key should ONLY be in this array
334 'customerdocs' => VBO_CUSTOMERS_PATH,
335 );
336 }
337
338 /**
339 * Returns a list of keys for the upload back up directories
340 * only containing the keys for those dirs where the extendable
341 * PHP Classes are located by default in the plugin.
342 * Used during the installation, uninstallation and backup (post-update).
343 *
344 * @return array An array of strings with the keys of the paths of each dir supported
345 */
346 protected static function getExtendableDirsKeys()
347 {
348 // return the real upload dir paths used by the plugin
349 return array(
350 // dir path to extend the reports
351 'report',
352 // dir path to extend the SMS APIs
353 'smsapi',
354 );
355 }
356
357 /**
358 * Creates a directory called 'vikbooking' onto the defined upload
359 * directory for the website, for later use during the upload of files.
360 * All dirs are created by reading the instructions from getUploadBackupDirs().
361 * By default in /wp-content/uploads/vikbooking
362 *
363 * @return boolean True on success.
364 *
365 * @uses getUploadBackupDirs()
366 *
367 * @since 1.0.4
368 * @since 1.5.0 the visibility of this method has been changed to public for the Fixer class.
369 */
370 public static function installUploadBackup()
371 {
372 // import the Folder and File classes
373 JLoader::import('adapter.filesystem.folder');
374 JLoader::import('adapter.filesystem.file');
375
376 // create all necessary folders (if they don't exist)
377 $result = true;
378 foreach (self::getUploadBackupDirs() as $type => $path) {
379 $result = $result && JFolder::create($path);
380 if ($result) {
381 // create an empty HTML file to secure the directory from browsing
382 JFile::write($path . DIRECTORY_SEPARATOR . 'index.html', '<html></html>');
383 }
384 }
385
386 return $result;
387 }
388
389 /**
390 * Removes the directory 'vikbooking' used for the file upload.
391 * The dirs to be removed are read from the array in getUploadBackupDirs().
392 * This should happen only upon uninstallation of the Plugin.
393 *
394 * @return boolean True on success.
395 *
396 * @uses getUploadBackupDirs()
397 *
398 * @since 1.0.4
399 */
400 protected static function uninstallUploadBackup()
401 {
402 // import the Folder class
403 JLoader::import('adapter.filesystem.folder');
404
405 // delete all folders no longer needed (if they exist)
406 $result = true;
407 foreach (self::getUploadBackupDirs() as $type => $path) {
408 $result = JFolder::delete($path) && $result;
409 }
410
411 return $result;
412 }
413
414 /**
415 * Restores all the uploaded files from the directory 'vikbooking'
416 * and its sub-dirs, onto the original dirs of the Plugin.
417 *
418 * @return boolean True on success.
419 *
420 * @uses getUploadBackupDirs()
421 *
422 * @since 1.0.4
423 */
424 protected static function restoreUploadBackup()
425 {
426 // get the list of the backup directories
427 $backup_dirs = self::getUploadBackupDirs();
428
429 // get the list of the real upload directories
430 $real_dirs = self::getUploadBackupDirs(true);
431
432 // import the Folder and File classes
433 JLoader::import('adapter.filesystem.folder');
434 JLoader::import('adapter.filesystem.file');
435
436 $result = true;
437
438 foreach ($backup_dirs as $type => $path) {
439 // scan all files in this backup dir
440 $files = JFolder::files($path);
441 if (!isset($real_dirs[$type]) || !is_array($files) || !count($files)) {
442 continue;
443 }
444
445 // restore all backed-up files onto its real dir
446 foreach ($files as $file) {
447 $fname = basename($file);
448 // skip placeholder HTML file
449 if ($fname == 'index.html') {
450 continue;
451 }
452 // restore original file
453 $result = JFile::copy($path . DIRECTORY_SEPARATOR . $file, $real_dirs[$type] . DIRECTORY_SEPARATOR . $fname) && $result;
454 }
455 }
456
457 return $result;
458 }
459
460 /**
461 * Trigger called whenever a file has been uploaded.
462 * Checks if the destination path is recognized, and
463 * copies the uploaded/created file onto its backup folder.
464 * This is to have a mirror-file to be restored upon update.
465 *
466 * @param string $dest the path to the just uploaded file.
467 *
468 * @return boolean True on success.
469 *
470 * @uses getUploadBackupDirs()
471 * @uses installUploadBackup()
472 *
473 * @since 1.0.4
474 */
475 public static function triggerUploadBackup($dest)
476 {
477 // seek the key of the uploaded dir
478 $dir_key = false;
479 foreach (self::getUploadBackupDirs(true) as $key => $path) {
480 if (strpos($dest, $path) !== false) {
481 $dir_key = $key;
482 break;
483 }
484 }
485 if (!$dir_key) {
486 // could not recognize upload dir path from destination file
487 return false;
488 }
489
490 // always make sure the upload backup dirs are set for bc with those that installed a previous version of the plugin
491 if (!self::installUploadBackup()) {
492 // cannot proceed because backup folders are not set
493 return false;
494 }
495
496 // import the File class
497 JLoader::import('adapter.filesystem.file');
498
499 // get the backup upload dir path for this type of file
500 $backup_dirs = self::getUploadBackupDirs();
501 if (!isset($backup_dirs[$dir_key])) {
502 // the arrays returned by getUploadBackupDirs do not match (should NEVER happen)
503 return false;
504 }
505
506 // copy the file onto its backup dir
507 return JFile::copy($dest, $backup_dirs[$dir_key] . DIRECTORY_SEPARATOR . basename($dest));
508 }
509
510 /**
511 * This method should be called after importing a full backup
512 * copy from another Vik Booking installation. It recursively
513 * parses all the upload backup directories to mirror. This
514 * way, installing a future update will be safe.
515 *
516 * @return boolean true on success, false otherwise.
517 *
518 * @since 1.5.0
519 */
520 public static function triggerUploadFullMirroring()
521 {
522 // make sure all needed directories are available
523 if (!self::installUploadBackup()) {
524 // cannot proceed because backup folders are not set
525 return false;
526 }
527
528 // get all the backup upload directory paths
529 $backup_dirs = self::getUploadBackupDirs();
530
531 // copy result
532 $result = false;
533
534 // loop through all the real upload directories used by the plugin
535 foreach (self::getUploadBackupDirs(true) as $key => $path) {
536 if (!isset($backup_dirs[$key])) {
537 continue;
538 }
539 // copy all files onto their backup directory
540 foreach (JFolder::files($path) as $fname) {
541 $result = JFile::copy($path . DIRECTORY_SEPARATOR . $fname, $backup_dirs[$key] . DIRECTORY_SEPARATOR . $fname) || $result;
542 }
543 }
544
545 return $result;
546 }
547
548 /**
549 * Trigger called to back up all the files inside the path with the key
550 * passed. Useful to back up custom PHP files manually uploaded to extend
551 * the default Reports, Cron Jobs, SMS APIs to save a clone copy of these
552 * files onto a directory that will be later used for restoring the files.
553 * This method should be called every time a back-end View with a list of
554 * specific extendable classes is visited, to always update the back ups.
555 *
556 * @param string $key The key of the extendable class.
557 * @param string $filter An optional regex to apply on the file name to skip.
558 *
559 * @return boolean True on success.
560 *
561 * @uses getUploadBackupDirs()
562 * @uses installUploadBackup()
563 *
564 * @since 1.0.14
565 */
566 public static function triggerExtendableClassesBackup($key, $filter = '')
567 {
568 // get the list of the original directories
569 $orig_dirs = self::getUploadBackupDirs(true);
570
571 // get the list of the backup directories
572 $backup_dirs = self::getUploadBackupDirs();
573
574 // make sure the key exists in the dirs arrays
575 if (empty($key) || !isset($orig_dirs[$key]) || !isset($backup_dirs[$key])) {
576 return false;
577 }
578
579 // always make sure the backup dirs are set for bc with those that installed a previous version of the plugin
580 if (!self::installUploadBackup()) {
581 // cannot proceed because backup folders are not set
582 return false;
583 }
584
585 // import the Folder and File classes
586 JLoader::import('adapter.filesystem.folder');
587 JLoader::import('adapter.filesystem.file');
588
589 // scan all files in the original dir
590 $files = JFolder::files($orig_dirs[$key]);
591 if (!is_array($files) || !count($files)) {
592 return false;
593 }
594
595 $result = true;
596
597 // back-up all files of this dir onto its real dir
598 foreach ($files as $file) {
599 $fname = basename($file);
600 // skip placeholder HTML file
601 if ($fname == 'index.html') {
602 continue;
603 }
604 if (!empty($filter) && !preg_match($filter, $file)) {
605 // we skip this file as the filter matched its name
606 continue;
607 }
608 // clone original file
609 $result = JFile::copy($orig_dirs[$key] . DIRECTORY_SEPARATOR . $file, $backup_dirs[$key] . DIRECTORY_SEPARATOR . $fname) && $result;
610 }
611
612 return $result;
613 }
614
615 /**
616 * Get the script class to run the installation methods.
617 *
618 * @param string $version The current version.
619 *
620 * @return VikBookingUpdateFixer
621 */
622 protected static function getFixer($version)
623 {
624 VikBookingLoader::import('update.fixer');
625
626 return new VikBookingUpdateFixer($version);
627 }
628
629 /**
630 * Provides the installation of the ACL routines.
631 *
632 * @return boolean True on success, otherwise false.
633 */
634 protected static function installAcl()
635 {
636 JLoader::import('adapter.acl.access');
637 $actions = JAccess::getActions('vikbooking');
638
639 // No actions found!
640 // Probably, the main folder is not called "vikbooking".
641 if (!$actions)
642 {
643 return false;
644 }
645
646 $roles = array(
647 get_role('administrator'),
648 );
649
650 foreach ($roles as $role)
651 {
652 if ($role)
653 {
654 foreach ($actions as $action)
655 {
656 $cap = JAccess::adjustCapability($action->name, 'com_vikbooking');
657 $role->add_cap($cap, true);
658 }
659 }
660 }
661
662 return true;
663 }
664
665 /**
666 * Sets up the options for using the Pro version.
667 *
668 * @return void
669 */
670 protected static function installProSettings()
671 {
672 VikBookingChangelog::install();
673 VikBookingLicense::install();
674 }
675
676 /**
677 * Sets up the options for using the Pro version.
678 *
679 * @return void
680 */
681 protected static function uninstallProSettings()
682 {
683 VikBookingChangelog::uninstall();
684 VikBookingLicense::uninstall();
685 }
686
687 /**
688 * Provides the uninstallation of the ACL routines.
689 *
690 * @return boolean True on success, otherwise false.
691 */
692 protected static function uninstallAcl()
693 {
694 JLoader::import('adapter.acl.access');
695 $actions = JAccess::getActions('vikbooking');
696
697 // No actions found!
698 // Probably, something went wrong while installing the plugin.
699 if (!$actions)
700 {
701 return false;
702 }
703
704 $roles = array(
705 get_role('administrator'),
706 );
707
708 foreach ($roles as $role)
709 {
710 if ($role)
711 {
712 foreach ($actions as $action)
713 {
714 $cap = JAccess::adjustCapability($action->name, 'com_vikbooking');
715 $role->remove_cap($cap);
716 }
717 }
718 }
719
720 return true;
721 }
722
723 /**
724 * Run all the proper SQL files.
725 *
726 * @param string $version The current version.
727 *
728 * @return boolean True on success, otherwise false.
729 *
730 * @uses execSqlFile()
731 */
732 protected static function installSql($version)
733 {
734 $dbo = JFactory::getDbo();
735
736 $ok = true;
737
738 $sql_base = VIKBOOKING_BASE . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'mysql' . DIRECTORY_SEPARATOR;
739
740 try
741 {
742 foreach (glob($sql_base . '*.sql') as $file)
743 {
744 $name = basename($file);
745 $sql_v = substr($name, 0, strrpos($name, '.'));
746
747 if (version_compare($sql_v, $version, '>'))
748 {
749 // in case the SQL version is newer, execute the queries listed in the file
750 self::execSqlFile($file, $dbo);
751 }
752 }
753 }
754 catch (Exception $e)
755 {
756 $ok = false;
757 }
758
759 return $ok;
760 }
761
762 /**
763 * Executes all the queries contained in the specified file.
764 *
765 * @param string $file The SQL file to launch.
766 * @param JDatabase $dbo The database driver handler.
767 *
768 * @return void
769 */
770 protected static function execSqlFile($file, $dbo = null)
771 {
772 if (!is_file($file))
773 {
774 return;
775 }
776
777 if ($dbo === null)
778 {
779 $dbo = JFactory::getDbo();
780 }
781
782 $handle = fopen($file, 'r');
783
784 $bytes = '';
785 while (!feof($handle))
786 {
787 $bytes .= fread($handle, 8192);
788 }
789
790 fclose($handle);
791
792 foreach (JDatabaseHelper::splitSql($bytes) as $q)
793 {
794 $dbo->setQuery($q);
795 $dbo->execute();
796 }
797 }
798 }
799