spinstall.php 29.1 KB
Newer Older
1 2
<?php
/**
Sigrid Suski's avatar
Sigrid Suski committed
3 4 5 6 7 8 9
 * @package: SobiPro Component for Joomla!
 *
 * @author
 * Name: Sigrid Suski & Radek Suski, Sigsiu.NET GmbH
 * Email: sobi[at]sigsiu.net
 * Url: https://www.Sigsiu.NET
 *
10
 * @copyright Copyright (C) 2006 - 2020 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved.
11
 * @license GNU/GPL Version 3
Sigrid Suski's avatar
Sigrid Suski committed
12 13
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation, and under the additional terms according section 7 of GPL v3.
14
 * See https://www.gnu.org/licenses/gpl.html and https://www.sigsiu.net/licenses.
Sigrid Suski's avatar
Sigrid Suski committed
15 16 17
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 19 20 21
 */

defined( '_JEXEC' ) || exit( 'Restricted access' );

22
/**
23
 * @modified 22 January 2020 by Sigrid Suski
24
 */
25 26
class com_sobiproInstallerScript
{
27

28
	/**
29
	 * Called before any type of action
30
	 *
31 32
	 * @param string $route Which action is happening (install|uninstall|discover_install|update)
	 * @param JAdapterInstance $adapter The object responsible for running this script
33
	 *
34
	 * @return  boolean  True on success
35
	 */
36
//	public function preflight( $route, JAdapterInstance $adapter )
37 38 39 40
	/**
	 * @param string $route
	 * @param JAdapterInstance $adapter
	 */
41
	public function preflight( $route, $adapter )
42
	{
43 44
		if ( $adapter instanceof JInstallerAdapterComponent ) {
			$this->installPlugins( $adapter->getParent()->get( 'paths' ) );
45
		}
46 47
		elseif ( $adapter instanceof JInstallerComponent ) {
			$this->installPlugins( $adapter->getParent->get( '_paths' ) );
48 49
		}

50
		if ( $route != 'uninstall' ) {
51
			// Installing component manifest file version
52
			$this->release = $adapter->getManifest()->version;
53 54 55 56 57 58
			// Show the essential information at the install/update back-end
			echo '<h2>Installing SobiPro version ' . $this->release . ' ...</h2>';
		}
	}

	/**
59
	 * Called after any type of action
60
	 *
61 62
	 * @param string $route Which action is happening (install|uninstall|discover_install|update)
	 * @param JAdapterInstance $adapter The object responsible for running this script
63
	 *
64
	 * @return  boolean  True on success
65
	 */
66 67
	public function postflight( $route, JAdapterInstance $adapter )
	{
68 69
//		echo '<p>' . JText::_( 'COM_HELLOWORLD_POSTFLIGHT_' . $route . '_TEXT' ) . '</p>';
//
70
	}
71 72 73


	/**
74 75
	 * @param JAdapterInstance $adapter
	 *
76
	 * @return  boolean  True on success
77 78 79
	 * @return bool
	 * @throws SPException
	 * @throws \Sobi\Error\Exception
80
	 */
81
	public function update( JAdapterInstance $adapter )
82
//	public function update( $parent )
83 84 85 86 87 88 89
	{
		if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'usr', 'locale' ] ) ) ) {
			JFolder::delete( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'usr', 'locale' ] ) );
		}
		if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'services', 'installers', 'schemas', 'application.xsd' ] ) ) ) {
			JFolder::delete( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'services', 'installers', 'schemas', 'application.xsd' ] ) );
		}
90 91 92 93 94 95 96
//		try {
//			if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'media' ] ) ) ) {
//				JFolder::delete( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'media' ] ) );
//			}
//		}
//		catch ( Exception $x ) {
//		}
97 98 99 100

		if ( !( file_exists( implode( '/', [ JPATH_ROOT, 'images', 'sobipro', 'categories' ] ) ) ) ) {
			JFolder::create( implode( '/', [ JPATH_ROOT, 'images', 'sobipro', 'categories' ] ) );
		}
101 102

		// copy category images from media to images folder
103 104 105 106 107 108 109
		$srcpath = JPATH_ROOT . '/media/sobipro/icons';
		if ( file_exists( $srcpath ) ) {
			$files = scandir( $srcpath );

			$dest = JPATH_ROOT . '/images/sobipro/categories';
			if ( count( $files ) ) {
				foreach ( $files as $file ) {
110
					if ( $file != '.' && $file != '..' && $file != 'Mime' ) {
111 112 113 114 115 116 117 118 119 120
						if ( is_dir( $srcpath . '/' . $file ) ) {
							JFolder::copy( $srcpath . '/' . $file, $dest . '/' . $file, '', true );
						}
						elseif ( !( file_exists( $dest . '/' . $file ) ) ) {
							JFile::copy( $srcpath . '/' . $file, $dest . '/' . $file );
						}
					}
				}
			}
		}
121
		// copy category images from media to images folder
122 123 124 125
		$srcpath = JPATH_ROOT . '/media/sobipro/images';
		if ( file_exists( $srcpath ) ) {
			$files = scandir( $srcpath );

126
			if ( $files != 'clustermarker' ) {
127 128 129 130 131 132 133 134 135 136
				$dest = JPATH_ROOT . '/images/sobipro/categories';
				if ( count( $files ) ) {
					foreach ( $files as $file ) {
						if ( $file != '.' && $file != '..' && $file != 'clustermarker' ) {
							if ( is_dir( $srcpath . '/' . $file ) ) {
								JFolder::copy( $srcpath . '/' . $file, $dest . '/' . $file, '', true );
							}
							elseif ( !( file_exists( $dest . '/' . $file ) ) ) {
								JFile::copy( $srcpath . '/' . $file, $dest . '/' . $file );
							}
137 138 139 140 141 142
						}
					}
				}
			}
		}

143
		$dest = JPATH_ROOT . '/images/sobipro/categories/image.png';
144
		if ( !( file_exists( $dest ) ) && file_exists( JPATH_ROOT . '/components/com_sobipro/tmp/install/image.png' ) ) {
145
			JFile::move( JPATH_ROOT . '/components/com_sobipro/tmp/install/image.png', JPATH_ROOT . '/images/sobipro/categories/image.png' );
146 147
		}

148
		$db = JFactory::getDBO();
149
		$db->setQuery( 'CREATE TABLE IF NOT EXISTS `#__sobipro_view_cache` (`cid` INT(11) NOT NULL AUTO_INCREMENT, `section` INT(11) NOT NULL, `sid` INT(11) NOT NULL, `fileName` VARCHAR(100) NOT NULL, `task` VARCHAR(100) NOT NULL, `site` INT(11) NOT NULL, `request` VARCHAR(190) NOT NULL, `language` VARCHAR(15) NOT NULL, `template` VARCHAR(150) NOT NULL, `configFile` TEXT NOT NULL, `userGroups` VARCHAR(190) NOT NULL, `created` DATETIME NOT NULL, PRIMARY KEY (`cid`), KEY `sid` (`sid`), KEY `section` (`section`), KEY `language` (`language`), KEY `task` (`task`), KEY `request` (`request`), KEY `site` (`site`), KEY `userGroups` (`userGroups`)) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;' );
150 151
		$db->execute();

Sigrid Suski's avatar
Sigrid Suski committed
152
		$db->setQuery( 'CREATE TABLE IF NOT EXISTS `#__sobipro_view_cache_relation` (`cid` INT(11) NOT NULL, `sid` INT(11) NOT NULL, PRIMARY KEY (`cid`, `sid`)) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;' );
153 154
		$db->execute();

Sigrid Suski's avatar
Sigrid Suski committed
155
		$db->setQuery( 'CREATE TABLE IF NOT EXISTS `#__sobipro_user_group` (`description` TEXT, `gid` INT(11) NOT NULL AUTO_INCREMENT, `enabled` INT(11) NOT NULL, `pid` INT(11) NOT NULL, `groupName` VARCHAR(150) NOT NULL, PRIMARY KEY (`gid`)) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT = 5000;' );
156 157
		$db->execute();

Sigrid Suski's avatar
Sigrid Suski committed
158
		$db->setQuery( 'CREATE TABLE IF NOT EXISTS `#__sobipro_counter` (`sid` INT(11) NOT NULL, `counter` INT(11) NOT NULL, `lastUpdate` DATETIME NOT NULL, PRIMARY KEY (`sid`)) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;' );
159 160
		$db->execute();

Sigrid Suski's avatar
Sigrid Suski committed
161
		$db->setQuery( 'CREATE TABLE IF NOT EXISTS `#__sobipro_history` (`revision` VARCHAR(150) NOT NULL, `changedAt` DATETIME NOT NULL, `uid` INT(11) NOT NULL, `userName` VARCHAR(150) NOT NULL, `userEmail` VARCHAR(150) NOT NULL, `changeAction` VARCHAR(150) NOT NULL, `site` ENUM (\'site\', \'adm\') NOT NULL, `sid` INT(11) NOT NULL, `changes` TEXT NOT NULL, `params` TEXT NOT NULL, `reason` TEXT NOT NULL, `language` VARCHAR(50) NOT NULL, PRIMARY KEY (`revision`)) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;' );
162 163
		$db->execute();

Sigrid Suski's avatar
Sigrid Suski committed
164
		//1.1
165 166 167 168 169 170 171 172
		$db->setQuery( "UPDATE #__sobipro_field_option_selected SET `optValue` = REPLACE (`optValue`, '_', '-')" );
		$db->execute();

		$db->setQuery( "UPDATE #__sobipro_field_option SET `optValue` = REPLACE (`optValue`, '_', '-')" );
		$db->execute();

		$db->setQuery( "UPDATE #__sobipro_language SET  `sKey` = REPLACE(  `sKey` ,  '_',  '-' ) WHERE  `oType` =  'field_option'" );
		$db->execute();
Sigrid Suski's avatar
Sigrid Suski committed
173

174 175 176 177 178 179 180 181 182 183 184
		// 1.5.1
		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_field_option CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' );
			$db->execute();
			$db->setQuery( 'ALTER TABLE #__sobipro_field_option_selected CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' );
			$db->execute();
			$db->setQuery( 'ALTER TABLE #__sobipro_field_option_selected CHANGE `params` `params` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
		}
		catch ( Exception $x ) {
		}
Sigrid Suski's avatar
Sigrid Suski committed
185 186 187 188 189

		// Table __sobipro_permissions
		$db->setQuery( "UPDATE #__sobipro_permissions SET value =  '*' WHERE  pid = 18;" );
		$db->execute();

190 191 192 193
		try {
			$db->setQuery( 'DELETE FROM `#__sobipro_permissions` WHERE `pid` = 5;' );
			$db->execute();
		}
Sigrid Suski's avatar
Sigrid Suski committed
194 195 196 197 198
		catch ( Exception $x ) {
		}
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_permissions` (`pid`, `subject`, `action`, `value`, `site`, `published`) VALUES (86, 'entry', '*', '*', 'adm', 1), (87, 'category', '*', '*', 'adm', 1), (88, 'section', '*', '*', 'adm', 1), (89, 'section', 'access', '*', 'adm', 1), (90, 'section', 'configure', '*', 'adm', 1), (91, 'section', 'delete', '*', 'adm', 0), (92, 'category', 'edit', '*', 'adm', 1), (93, 'category', 'add', '*', 'adm', 1), (94, 'category', 'delete', '*', 'adm', 1), (95, 'entry', 'edit', '*', 'adm', 1), (96, 'entry', 'add', '*', 'adm', 1), (97, 'entry', 'delete', '*', 'adm', 1), (98, 'entry', 'approve', '*', 'adm', 1), (99, 'entry', 'publish', '*', 'adm', 1);
"
		);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		$db->execute();

		$db->setQuery( 'SHOW INDEX FROM  #__sobipro_permissions' );
		$cols = $db->loadAssocList();
		$skip = false;
		foreach ( $cols as $col ) {
			if ( $col[ 'Key_name' ] == 'uniquePermission' ) {
				$skip = true;
				continue;
			}
		}
		if ( !( $skip ) ) {
			$db->setQuery( 'ALTER TABLE `#__sobipro_permissions` ADD UNIQUE  `uniquePermission` (  `subject` ,  `action` ,  `value` ,  `site` );' );
			$db->execute();
		}

Sigrid Suski's avatar
Sigrid Suski committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_permissions` (`pid`, `subject`, `action`, `value`, `site`, `published`) VALUES (NULL, 'section', 'search', '*', 'front', 1), (NULL, 'entry', 'delete', 'own', 'front', 1),(NULL, 'entry', 'delete', '*', 'front', 1), (NULL, 'entry', 'manage', 'own', 'front', 1), (NULL, 'entry', 'access', 'expired_own', 'front', 1),  (NULL, 'entry', 'access', 'expired_any', 'front', 1);;;" );
		$db->execute();

//		$db->setQuery( 'SELECT pid FROM `#__sobipro_permissions` WHERE subject = "section" AND  action = "search";' );
//		$pid = $db->loadResult();
//
//		$db->setQuery( 'SELECT rid FROM #__sobipro_permissions_rules' );
//		$rids = $db->loadRowList();
//		if ( count( $rids ) ) {
//			$insert = array();
//			foreach ( $rids as $rid ) {
//
//			}
//		}


		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_field_data CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' );
233
			$db->execute();
Sigrid Suski's avatar
Sigrid Suski committed
234 235 236 237 238
			$db->setQuery( 'ALTER TABLE #__sobipro_language CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' );
			$db->execute();
		}
		catch ( Exception $x ) {
		}
239 240 241
		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `sValue` `sValue` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
		}
		catch ( Exception $x ) {
		}
		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `sKey` `sKey` VARCHAR(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
		}
		catch ( Exception $x ) {
		}
		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `language` `language` VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `oType` `oType` VARCHAR(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
		}
		catch ( Exception $x ) {
		}
		try {
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `params` `params` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `options` `options` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `explanation` `explanation` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
		}
		catch ( Exception $x ) {
		}

		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_field_data CHANGE `baseData` `baseData` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
		}
		catch ( Exception $x ) {
		}

276 277 278 279 280 281 282
		// Table __sobipro_search
		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_search CHANGE `entriesResults` `entriesResults` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->setQuery( 'ALTER TABLE #__sobipro_search CHANGE `catsResults` `catsResults` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
		}
		catch ( Exception $x ) {
		}
Sigrid Suski's avatar
Sigrid Suski committed
283 284

		// Table __sobipro_field_data
285 286 287 288 289 290 291 292 293 294 295 296
		try {
			$db->setQuery( 'SHOW INDEX FROM  #__sobipro_field_data' );
			$cols = $db->loadAssocList();
			$skip = false;
			foreach ( $cols as $col ) {
				if ( $col[ 'Key_name' ] == 'baseData' ) {
					$skip = true;
					continue;
				}
			}
			if ( !( $skip ) ) {
				try {
Sigrid Suski's avatar
Sigrid Suski committed
297
					$db->setQuery( 'ALTER TABLE #__sobipro_field_data ENGINE = MYISAM;' );
298 299
					$db->execute();
				}
Sigrid Suski's avatar
Sigrid Suski committed
300 301 302 303 304 305 306 307
				catch ( Exception $x ) {
				}
				try {
					$db->setQuery( 'ALTER TABLE  `#__sobipro_field_data` ADD FULLTEXT  `baseData` (`baseData`);' );
					$db->execute();
				}
				catch ( Exception $x ) {
				}
308
			}
Sigrid Suski's avatar
Sigrid Suski committed
309 310
		}
		catch ( Exception $x ) {
311 312
		}

Sigrid Suski's avatar
Sigrid Suski committed
313
		// Table __sobipro_language
314 315 316 317 318 319 320 321 322 323 324
		$db->setQuery( 'SHOW INDEX FROM  #__sobipro_language' );
		$cols = $db->loadAssocList();
		$skip = false;
		foreach ( $cols as $col ) {
			if ( $col[ 'Key_name' ] == 'sValue' ) {
				$skip = true;
				continue;
			}
		}
		if ( !( $skip ) ) {
			try {
Sigrid Suski's avatar
Sigrid Suski committed
325
				$db->setQuery( 'ALTER TABLE #__sobipro_language ENGINE = MYISAM;' );
326 327
				$db->execute();
			}
Sigrid Suski's avatar
Sigrid Suski committed
328 329 330 331 332 333 334 335
			catch ( Exception $x ) {
			}
			try {
				$db->setQuery( 'ALTER TABLE  `#__sobipro_language` ADD FULLTEXT  `sValue` (`sValue`);' );
				$db->execute();
			}
			catch ( Exception $x ) {
			}
336 337
		}

Sigrid Suski's avatar
Sigrid Suski committed
338 339 340 341 342
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_language` (`sKey`, `sValue`, `section`, `language`, `oType`, `fid`, `id`, `params`, `options`, `explanation`) VALUES ('rejection-of-a-new-entry', 'Entry {entry.name} has been rejected as it does not comply with the rules.\n\n<br/>Rejected by {user.name}\n<br/>at {date%d F Y H:i:s}\n', 0, 'en-GB', 'rejections-templates', 0, 1, '', '', ''), ('rejection-of-changes', 'Changes in {entry.name} discarded as these changes violating rules.\n\n<br/>Rejected by {user.name}\n<br/>at {date%d F Y H:i:s}\n', 0, 'en-GB', 'rejections-templates', 0, 1, '', '', '');" );
		$db->execute();


		// Table __sobipro_history
343 344 345 346 347 348 349 350 351 352 353
		$db->setQuery( 'SHOW INDEX FROM  #__sobipro_history' );
		$cols = $db->loadAssocList();
		$skip = false;
		foreach ( $cols as $col ) {
			if ( $col[ 'Key_name' ] == 'changeAction' ) {
				$skip = true;
				continue;
			}
		}
		if ( !( $skip ) ) {
			try {
Sigrid Suski's avatar
Sigrid Suski committed
354
				$db->setQuery( 'ALTER TABLE #__sobipro_history CHANGE `change` `changeAction` VARCHAR(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;' );
355
				$db->execute();
Sigrid Suski's avatar
Sigrid Suski committed
356 357
			}
			catch ( Exception $x ) {
358 359 360
			}
		}

Sigrid Suski's avatar
Sigrid Suski committed
361
		// Table __sobipro_category
362 363 364
		$result = $db->setQuery( "SHOW COLUMNS FROM `#__sobipro_category` LIKE 'allFields'" );
		$col = $db->loadResult();
		if ( $col != 'allFields' ) {
365 366 367 368 369
			try {
				$db->setQuery( 'ALTER TABLE `#__sobipro_category` ADD `allFields` TINYINT(2) NOT NULL AFTER `showIcon`, ADD `entryFields` TEXT NOT NULL AFTER `allFields`;' );
				$db->execute();
				$db->setQuery( 'UPDATE `#__sobipro_category` SET `allFields` = 1' );
				$db->execute();
Sigrid Suski's avatar
Sigrid Suski committed
370 371
			}
			catch ( Exception $x ) {
372 373 374
			}
		}

375 376 377
		$result = $db->setQuery( "SHOW COLUMNS FROM `#__sobipro_category` LIKE 'section'" );
		$col = $db->loadResult();
		if ( $col != 'section' ) {
378
			$categories = null;
379
			try {
380
				$db->setQuery( 'ALTER TABLE `#__sobipro_category` ADD `section` INT(11) AFTER `id`;' );
381
				$db->execute();
382 383 384 385 386 387 388 389 390
				$db->setQuery( 'SELECT `id` FROM `#__sobipro_category` WHERE 1' );
				$categories = $db->loadColumn();
			}
			catch ( Exception $x ) {
			}

			if ( is_array( $categories ) && count( $categories ) ) {
				require_once( JPATH_SITE . '/components/com_sobipro/lib/sobi.php' );
				Sobi::Initialise();
391 392 393
				foreach ( $categories as $i => $id ) {
					$parent = SPFactory::config()->getParentPath( $id );
					$sid = ( is_array( $parent ) && count( $parent ) ) ? $parent[ 0 ] : 0;
394
					try {
395
						$db->setQuery( "UPDATE `#__sobipro_category` SET `section` = {$sid} WHERE `id` = {$id};" );
396 397 398
						$db->execute();
					}
					catch ( Exception $x ) {
399
					}
400

401 402
				}
			}
403

404 405
		}

Sigrid Suski's avatar
Sigrid Suski committed
406
		//Table __sobipro_registry
407 408 409
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_registry` (`section`, `key`, `value`, `params`, `description`, `options`) VALUES ('rejections-templates', 'rejection-of-a-new-entry', 'Rejection of a new entry', 'YTo0OntzOjE3OiJ0cmlnZ2VyLnVucHVibGlzaCI7YjoxO3M6MTc6InRyaWdnZXIudW5hcHByb3ZlIjtiOjA7czo5OiJ1bnB1Ymxpc2giO2I6MTtzOjc6ImRpc2NhcmQiO2I6MDt9', '', ''), ('rejections-templates', 'rejection-of-changes', 'Rejection of changes', 'YTo0OntzOjE3OiJ0cmlnZ2VyLnVucHVibGlzaCI7YjowO3M6MTc6InRyaWdnZXIudW5hcHByb3ZlIjtiOjE7czo5OiJ1bnB1Ymxpc2giO2I6MDtzOjc6ImRpc2NhcmQiO2I6MTt9', '', '');" );
		$db->execute();

Sigrid Suski's avatar
Sigrid Suski committed
410
		$db->setQuery( "UPDATE `#__sobipro_registry` SET `params` = 'L15bXHdcLi1dK0BbXHdcLi1dK1wuW2EtekEtWl17MiwyNH0kLw==' WHERE `key` = 'email' " );
411 412 413 414 415 416 417 418 419
		$db->execute();

		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_field_types` (`tid`, `fType`, `tGroup`, `fPos`) VALUES ('category', 'Category', 'special', 11);" );
		$db->execute();
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_field_types` (`tid`, `fType`, `tGroup`, `fPos`) VALUES ('info', 'Information', 'free_single_simple_data', 6);" );
		$db->execute();
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_field_types` (`tid`, `fType`, `tGroup`, `fPos`) VALUES ('button', 'Button', 'special', 5);" );
		$db->execute();

Sigrid Suski's avatar
Sigrid Suski committed
420
		$db->setQuery( 'CREATE TABLE IF NOT EXISTS `#__sobipro_field_url_clicks` (`date` DATETIME NOT NULL, `uid` INT(11) NOT NULL, `sid` INT(11) NOT NULL, `fid` VARCHAR(50) NOT NULL, `ip` VARCHAR(15) NOT NULL, `section` INT(11) NOT NULL, `browserData` TEXT NOT NULL, `osData` TEXT NOT NULL, `humanity` INT(3) NOT NULL, PRIMARY KEY (`date`, `sid`, `fid`, `ip`, `section`)) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;' );
421 422 423 424 425 426 427 428 429
		$db->execute();

		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_plugins` (`pid`, `name`, `version`, `description`, `author`, `authorURL`, `authorMail`, `enabled`, `type`, `depend`) VALUES ('category', 'Category', '1.4', NULL, 'Sigsiu.NET GmbH', 'https://www.sigsiu.net/', 'sobi@sigsiu.net', 1, 'field', '');" );
		$db->execute();
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_plugins` (`pid`, `name`, `version`, `description`, `author`, `authorURL`, `authorMail`, `enabled`, `type`, `depend`) VALUES ('info', 'Information', '1.4', NULL, 'Sigsiu.NET GmbH', 'https://www.sigsiu.net/', 'sobi@sigsiu.net', 1, 'field', '');" );
		$db->execute();
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_plugins` (`pid`, `name`, `version`, `description`, `author`, `authorURL`, `authorMail`, `enabled`, `type`, `depend`) VALUES ('button', 'Button', '1.4', NULL, 'Sigsiu.NET GmbH', 'https://www.sigsiu.net/', 'sobi@sigsiu.net', 1, 'field', '');" );
		$db->execute();

430
		$db->setQuery( "CREATE TABLE IF NOT EXISTS `#__sobipro_crawler` ( `url` VARCHAR(190) NOT NULL,`crid` INT(11) NOT NULL AUTO_INCREMENT,`state` TINYINT(1) NOT NULL, PRIMARY KEY (`crid`), UNIQUE KEY `url` (`url`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_unicode_ci;" );
431 432 433
		$db->execute();


Sigrid Suski's avatar
Sigrid Suski committed
434 435 436 437
		// Table __sobipro_config
		//removed in 1.4.6
//		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_config` ( `sKey` , `sValue` , `section` , `critical` , `cSection` ) VALUES ( 'engb_preload',  '1',  '0', NULL ,  'lang' )" );
//		$db->execute();
438

439
		//1.4.6 (set it for section 1 if it does not exist)
440
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_config` ( `sKey` , `sValue` , `section` , `critical` , `cSection` ) VALUES ( 'icon_fonts_load',  '1',  1 , NULL , 'template' )" );
441 442 443 444 445
		$db->execute();

		try {
			$db->setQuery( 'DELETE FROM `#__sobipro_config` WHERE `sKey` = "xml_raw" AND `section` = 0;' );
			$db->execute();
Sigrid Suski's avatar
Sigrid Suski committed
446
		}
447 448 449

		catch
		( Exception $x ) {
450
		}
451 452 453 454
		try {
			$db->setQuery( 'DELETE FROM `#__sobipro_config` WHERE `sKey` = "field_types_for_ordering"' );
			$db->execute();
		}
Sigrid Suski's avatar
Sigrid Suski committed
455
		catch ( Exception $x ) {
456 457
		}

458 459 460 461 462 463 464
		// 1.4.7.3
		try {
			$db->setQuery( 'ALTER TABLE `#__sobipro_field` CHANGE `notice` `notice` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;' );
			$db->execute();
		}
		catch ( Exception $x ) {
		}
465

466
		JFile::move( JPATH_ROOT . '/components/com_sobipro/etc/repos/sobipro_core/repository.1.5.xml', JPATH_ROOT . '/components/com_sobipro/etc/repos/sobipro_core/repository.xml' );
467

468 469 470 471 472 473
		$this->installFramework();

		echo '<div class="alert alert-info" style="margin-top: 20px;"><h3>Thank you for updating SobiPro!</h3><p>SobiPro is checking your system now, please see if there are errors or warnings. If the system check reports errors, your SobiPro installation will probably not work. If you see warnings, some functionality of SobiPro can be disturbed or malfunction. In these cases you should take a look to the <a href="https://www.sigsiu.net/sobipro/requirements"><strong>Requirements for SobiPro</strong></a> page on our website.</p>
<p>You can install languages directly from our <a href="index.php?option=com_sobipro&task=extensions.browse"><strong>Repository</strong></a> or download them from our <a href="https://www.sigsiu.net/center/languages"><strong>website</strong></a> and install it in the <a href="index.php?option=com_sobipro&task=extensions.installed"><strong>SobiPro Application Manager</strong></a>.</p></div>';

		echo '<iframe src="index.php?option=com_sobipro&task=requirements&init=1&tmpl=component" style="border: 1px solid #e0e0e0; border-radius: 5px; height: 900px; min-width: 1000px; width: 99%; margin-bottom: 50px; padding-left: 10px; padding-top: 10px;"></iframe>';
474

475 476
		return true;
	}
477 478


479
	/**
480
	 * Called on installation
481
	 *
482
	 * @param JAdapterInstance $adapter The object responsible for running this script
483
	 *
484
	 * @return  boolean  True on success
485
	 */
486
	public function install( JAdapterInstance $adapter )
487
//	public function install( $parent )
488 489 490 491 492 493 494 495 496 497 498 499 500
	{
		if ( !( file_exists( implode( '/', [ JPATH_ROOT, 'images', 'sobipro' ] ) ) ) ) {
			JFolder::create( implode( '/', [ JPATH_ROOT, 'images', 'sobipro' ] ) );
		}
		if ( !( file_exists( implode( '/', [ JPATH_ROOT, 'images', 'sobipro', 'categories' ] ) ) ) ) {
			JFolder::create( implode( '/', [ JPATH_ROOT, 'images', 'sobipro', 'categories' ] ) );

			if ( file_exists( JPATH_ROOT . '/components/com_sobipro/tmp/install/image.png' ) ) {
				JFile::move( JPATH_ROOT . '/components/com_sobipro/tmp/install/image.png', JPATH_ROOT . '/images/sobipro/categories/image.png' );
			}
		}
		if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'tmp', 'SampleData', 'entries' ] ) ) ) {
			JFolder::move(
Sigrid Suski's avatar
Sigrid Suski committed
501 502
				implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'tmp', 'SampleData', 'entries' ] ),
				implode( '/', [ JPATH_ROOT, 'images', 'sobipro', 'entries' ] )
503 504 505 506 507 508 509 510
			);
		}
		if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'usr', 'locale' ] ) ) ) {
			JFolder::delete( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'usr', 'locale' ] ) );
		}
		if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'services', 'installers', 'schemas', 'application.xsd' ] ) ) ) {
			JFolder::delete( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'services', 'installers', 'schemas', 'application.xsd' ] ) );
		}
511 512 513
//		if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'media' ] ) ) ) {
//			JFolder::delete( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'media' ] ) );
//		}
514
		JFile::move( JPATH_ROOT . '/components/com_sobipro/etc/repos/sobipro_core/repository.1.5.xml', JPATH_ROOT . '/components/com_sobipro/etc/repos/sobipro_core/repository.xml' );
515 516 517 518 519 520 521
		$db = JFactory::getDBO();
		$db->setQuery( 'SHOW COLUMNS FROM #__sobipro_field_data' );
		$cols = $db->loadAssocList( 'Field' );
		if ( !( isset( $cols[ 'editLimit' ] ) ) ) {
			$db->setQuery( 'ALTER TABLE  `#__sobipro_field_data` ADD  `editLimit` INT( 11 );' );
			$db->execute();
		}
522

523
		$this->installFramework();
524

525 526 527 528
		echo '<div class="alert alert-info" style="margin-top: 20px;"><h3>Welcome to SobiPro!</h3><p>SobiPro is checking your system now, please see if there are errors or warnings. If the system check reports errors, your SobiPro installation will probably not work. If you see warnings, some functionality of SobiPro can be disturbed or malfunction. In these cases you should take a look to the <a href="https://www.sigsiu.net/sobipro/requirements"><strong>Requirements for SobiPro</strong></a> page on our website.</p>
<p>You can install languages directly from our <a href="index.php?option=com_sobipro&task=extensions.browse"><strong>Repository</strong></a> or download them from our <a href="https://www.sigsiu.net/center/languages"><strong>website</strong></a> and install it in the <a href="index.php?option=com_sobipro&task=extensions.installed"><strong>SobiPro Application Manager</strong></a>.</p></div>';

		echo '<iframe src="index.php?option=com_sobipro&task=requirements&init=1&tmpl=component" style="border: 1px solid #e0e0e0; border-radius: 5px; height: 900px; min-width: 1000px; width: 99%; margin-bottom: 50px; padding-left: 10px; padding-top: 10px;"></iframe>';
529 530 531

		return true;

532 533
	}

534 535 536
	/**
	 * @param $source
	 */
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	protected function installPlugins( $source )
	{
		$source = $source[ 'source' ];
		$plugins = [ 'Header' ];
		$path = $source . '/Plugins';
		$installer = new JInstaller;
		$db = JFactory::getDBO();
		foreach ( $plugins as $plugin ) {
			$dir = $path . '/' . $plugin;
			$installer->install( $dir );
			$db->setQuery( "UPDATE #__extensions SET enabled =  '1' WHERE  element = 'sp{$plugin}';" );
			$db->execute();
		}
	}

	/**
553
	 * Called on uninstallation
554
	 *
555
	 * @param JAdapterInstance $adapter The object responsible for running this script
556
	 */
557
	public function uninstall( JAdapterInstance $adapter )
558
//	public function uninstall( $parent )
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	{
		echo '<h2>Un-Installing SobiPro ...</h2>';

		$db = JFactory::getDBO();
		$query = "show tables like '" . $db->getPrefix() . "sobipro_%'";
		$db->setQuery( $query );
		$tables = $db->loadColumn();
		foreach ( $tables as $table ) {
			$db->setQuery( "DROP TABLE {$table};" );
			$db->execute();
		}
		JFolder::delete( implode( '/', [ JPATH_ROOT, 'images', 'sobipro' ] ) );
		echo '<p style="margin-bottom: 50px;"><strong>Done! All SobiPro files and database tables have been removed from your system.</strong></p>';

	}

575 576 577
	/**
	 * @param bool $extract
	 */
578 579 580
	protected function installFramework()
	{
		//Sobi Framework installation
581 582 583 584 585 586 587 588 589 590 591
		$libpath = JPATH_ROOT . '/libraries/sobi';
		if ( !( file_exists( $libpath ) ) ) {
			JFolder::create( $libpath );
		}
		else {
			$files = scandir( $libpath );
			if ( count( $files ) ) {
				foreach ( $files as $file ) {
					if ( strstr( $file, '.tar.gz' ) || strstr( $file, '.php' ) ) {
						JFile::delete( $libpath . '/' . $file );
					}
592 593 594
				}
			}
		}
595
//		$src = JPATH_ROOT . '/components/com_sobipro/Sobi.phar.tar.gz';
596 597
//		$framework = $libpath . "/Sobi-" . SOBI_FRAMEWORK_VERSION . ".phar.tar.gz";

598
//		if ( file_exists( $src ) ) {
599
//			JFile::copy( $src, $framework );
600 601
//			JFile::delete( $src );
//		}
602 603 604 605 606
//		else {
//			echo "<p style=\"font-size: 15px;color: #d70303;\">Framework file " . $src . " does not exist.</p>";
//
//			return false;
//		}
607

608 609 610
		// I am guessing that this was what cached the PHAR file. Let's see...
		if ( function_exists( 'opcache_reset' ) ) {
			opcache_reset();
611
			clearstatcache();
612 613
		}

614
		$fwPackage = JPATH_ROOT . '/components/com_sobipro/Sobi-Framework.zip';
615
//		@include_once 'phar://' . $framework . '/Framework.php';
616

617 618 619 620 621 622 623 624 625 626
//		if ( !( class_exists( '\\Sobi\\Framework' ) ) ) {
		if ( file_exists( $fwPackage ) ) {
//				echo '<p style="font-size: 15px">It seems your server does not support PHAR, or PHAR is not usable.</p>';
			try {
				$arch = new Joomla\Archive\Zip();
				$arch->extract( $fwPackage, $libpath );
//					echo '<p style="font-size: 15px">Unpacked the Sobi Framework. Nevertheless, you should consider to install PHAR on your server.</p>';
//				if ( file_exists( $framework ) ) {
//					JFile::delete( $framework );
//				}
627 628
				JFile::delete( $fwPackage );
			}
629 630
			catch ( Exception $x ) {
				echo '<p style="font-size: 17px;color: #d70303;">Failed to unpack the Sobi Framework.<br/>Please install the Sobi Framework manually as described in <a href="https://www.sigsiu.net/center/sobipro-component/154-sobi-framework" style="text-decoration: underline; color: #d70303;">Install the Sobi Framework manually</a>.</p>';
631 632 633
			}
		}
		else {
634 635
//				echo '<p style="font-size: 15px;color: #d70303;">It seems your server does not support PHAR, or PHAR is not usable.<br/>Please install the Sobi Framework manually as described in <a href="https://www.sigsiu.net/center/sobipro-component/154-sobi-framework" style="text-decoration: underline; color: #d70303;">Install the Sobi Framework manually</a>.</p>';
			echo '<p style="font-size: 15px;color: #d70303;">Sobi Framework file ' . $fwPackage . ' not found in the installation package.</p>';
636
		}
637 638 639 640 641 642
//		}
//		else {
//			if ( file_exists( $fwPackage ) ) {
//				JFile::delete( $fwPackage );
//			}
//		}
643 644
	}
}