| 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 |
/** |
| 15 |
* Implements the abstract methods to fix an update. |
| 16 |
* |
| 17 |
* Never use exit() and die() functions to stop the flow. |
| 18 |
* Return false instead to break process safely. |
| 19 |
*/ |
| 20 |
class VikBookingUpdateFixer |
| 21 |
{ |
| 22 |
/** |
| 23 |
* The current version. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $version; |
| 28 |
|
| 29 |
/** |
| 30 |
* Class constructor. |
| 31 |
*/ |
| 32 |
public function __construct($version) |
| 33 |
{ |
| 34 |
$this->version = $version; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* This method is called before the SQL installation. |
| 39 |
* |
| 40 |
* @return boolean True to proceed with the update, otherwise false to stop. |
| 41 |
*/ |
| 42 |
public function beforeInstallation() |
| 43 |
{ |
| 44 |
/** |
| 45 |
* Make sure all the necessary (and new) directories have been created. |
| 46 |
* |
| 47 |
* @since 1.5.0 |
| 48 |
*/ |
| 49 |
VikBookingUpdateManager::installUploadBackup(); |
| 50 |
|
| 51 |
if (version_compare($this->version, '1.5.0', '<')) { |
| 52 |
/** |
| 53 |
* For those upgrading to VBO 1.5.0 we need to move the customer upload |
| 54 |
* document directories to the new location. Basically, they have been |
| 55 |
* moved to the new dir /customerdocs inside the plugin's upload dir of WP. |
| 56 |
* |
| 57 |
* @since 1.5.0 |
| 58 |
*/ |
| 59 |
$dbo = JFactory::getDbo(); |
| 60 |
|
| 61 |
$old_custdocs_path = str_replace(DIRECTORY_SEPARATOR . 'customerdocs', '', VBO_CUSTOMERS_PATH); |
| 62 |
$new_custdocs_path = VBO_CUSTOMERS_PATH; |
| 63 |
|
| 64 |
$q = "SELECT `docsfolder` FROM `#__vikbooking_customers` WHERE `docsfolder` IS NOT NULL"; |
| 65 |
$dbo->setQuery($q); |
| 66 |
$dbo->execute(); |
| 67 |
if ($dbo->getNumRows()) { |
| 68 |
$doc_folders = $dbo->loadObjectList(); |
| 69 |
foreach ($doc_folders as $doc_folder) { |
| 70 |
if (empty($doc_folder->docsfolder)) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
// move directory to new path |
| 74 |
$from_dir = $old_custdocs_path . DIRECTORY_SEPARATOR . $doc_folder->docsfolder; |
| 75 |
$to_dir = $new_custdocs_path . DIRECTORY_SEPARATOR . $doc_folder->docsfolder; |
| 76 |
if (JFolder::copy($from_dir, $to_dir, $path = '', $force = true)) { |
| 77 |
JFolder::delete($from_dir); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if (version_compare($this->version, '1.5.11', '<') && !VikBookingLiteManager::guessPro()) |
| 84 |
{ |
| 85 |
$dbo = JFactory::getDbo(); |
| 86 |
// truncate the payment gateways table |
| 87 |
$dbo->setQuery("TRUNCATE TABLE `#__vikbooking_gpayments`"); |
| 88 |
$dbo->execute(); |
| 89 |
} |
| 90 |
|
| 91 |
if (version_compare($this->version, '1.6.0', '<')) |
| 92 |
{ |
| 93 |
if (!class_exists('VikBooking')) { |
| 94 |
require_once VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikbooking.php'; |
| 95 |
} |
| 96 |
|
| 97 |
// normalize translation records table name |
| 98 |
VikBooking::getTranslator()->normalizeTnTableNames(); |
| 99 |
} |
| 100 |
|
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* This method is called after the SQL installation. |
| 106 |
* |
| 107 |
* @return boolean True to proceed with the update, otherwise false to stop. |
| 108 |
*/ |
| 109 |
public function afterInstallation() |
| 110 |
{ |
| 111 |
/** |
| 112 |
* Unpublish overrides and obtain tracking list. |
| 113 |
* |
| 114 |
* @since 1.6.5 |
| 115 |
*/ |
| 116 |
$track = $this->deactivateBreakingOverrides(); |
| 117 |
|
| 118 |
// register breaking changes, if any |
| 119 |
VikBookingInstaller::registerBreakingChanges($track); |
| 120 |
|
| 121 |
if (version_compare($this->version, '1.6.1', '<')) |
| 122 |
{ |
| 123 |
// resolve conflicting update checks for older VCM versions (1.8.4) |
| 124 |
if (defined('VIKCHANNELMANAGER_SOFTWARE_VERSION') && version_compare(VIKCHANNELMANAGER_SOFTWARE_VERSION, '1.8.5', '<')) |
| 125 |
{ |
| 126 |
$old_vcm_data = file_get_contents(VCM_ADMIN_PATH . DIRECTORY_SEPARATOR . 'controller.php'); |
| 127 |
$old_vcm_data = str_replace("'forcecheck'", "'force_check'", $old_vcm_data); |
| 128 |
file_put_contents(VCM_ADMIN_PATH . DIRECTORY_SEPARATOR . 'controller.php', $old_vcm_data); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns a list of possible overrides that may |
| 137 |
* break the site for backward compatibility errors. |
| 138 |
* |
| 139 |
* @return array The list of overrides, grouped by client. |
| 140 |
* |
| 141 |
* @since 1.6.5 |
| 142 |
*/ |
| 143 |
protected function getBreakingOverrides() |
| 144 |
{ |
| 145 |
// define initial overrides lookup |
| 146 |
$lookup = [ |
| 147 |
'admin' => [], |
| 148 |
'site' => [], |
| 149 |
'layouts' => [], |
| 150 |
'widgets' => [], |
| 151 |
]; |
| 152 |
|
| 153 |
// check whether the current version (before the update) |
| 154 |
// was prior than 1.6.5 version |
| 155 |
if (version_compare($this->version, '1.6.5', '<')) |
| 156 |
{ |
| 157 |
// multitasking layout must be updated to support the Service Worker |
| 158 |
$lookup['layouts'][] = VBO_ADMIN_PATH . '/layouts/sidepanel/multitasking.php'; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* NOTE: it is possible to use the code below to automatically deactivate all the existing overrides: |
| 163 |
* `$lookup = JModel::getInstance('vikbooking', 'overrides', 'admin')->getAllOverrides();` |
| 164 |
*/ |
| 165 |
|
| 166 |
return $lookup; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Helper function used to deactivate any overrides that |
| 171 |
* may corrupt the system because of breaking changes. |
| 172 |
* |
| 173 |
* @return array The list of unpublished overrides. |
| 174 |
* |
| 175 |
* @since 1.6.5 |
| 176 |
*/ |
| 177 |
protected function deactivateBreakingOverrides() |
| 178 |
{ |
| 179 |
// load list of breaking overrides |
| 180 |
$lookup = $this->getBreakingOverrides(); |
| 181 |
|
| 182 |
$track = []; |
| 183 |
|
| 184 |
// get models to manage the overrides |
| 185 |
$listModel = JModel::getInstance('vikbooking', 'overrides', 'admin'); |
| 186 |
$itemModel = JModel::getInstance('vikbooking', 'override', 'admin'); |
| 187 |
|
| 188 |
foreach ($lookup as $client => $files) |
| 189 |
{ |
| 190 |
// do not need to load the whole tree in case |
| 191 |
// the client doesn't report any files |
| 192 |
if ($files) |
| 193 |
{ |
| 194 |
$tree = $listModel->getTree($client); |
| 195 |
|
| 196 |
foreach ($files as $file) |
| 197 |
{ |
| 198 |
// clean file path |
| 199 |
$file = JPath::clean($file); |
| 200 |
|
| 201 |
// check whether the specified file is supported |
| 202 |
if ($node = $listModel->isSupported($tree, $file)) |
| 203 |
{ |
| 204 |
// skip in case the path has been already unpublished |
| 205 |
if (in_array($node['override'], isset($track[$client]) ? $track[$client] : [])) |
| 206 |
{ |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
// override found, check whether we have an existing |
| 211 |
// and published override |
| 212 |
if ($node['has'] && $node['published']) |
| 213 |
{ |
| 214 |
// deactivate the override |
| 215 |
if ($itemModel->publish($node['override'], 0)) |
| 216 |
{ |
| 217 |
if (!isset($track[$client])) |
| 218 |
{ |
| 219 |
$track[$client] = []; |
| 220 |
} |
| 221 |
|
| 222 |
// track the unpublished file for later use |
| 223 |
$track[$client][] = $node['override']; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return $track; |
| 232 |
} |
| 233 |
} |
| 234 |
|