spinstall.php 28.2 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 - 2019 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 14 15 16 17
 * 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.
 * See http://www.gnu.org/licenses/gpl.html and https://www.sigsiu.net/licenses.
 *
 * 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 24
/**
 * Class com_sobiproInstallerScript
 */
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
	 * @return  boolean  True on success
75 76 77 78 79
	 * @param JAdapterInstance $adapter
	 *
	 * @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


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

179 180 181 182
		try {
			$db->setQuery( 'DELETE FROM `#__sobipro_permissions` WHERE `pid` = 5;' );
			$db->execute();
		}
Sigrid Suski's avatar
Sigrid Suski committed
183 184 185 186 187
		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);
"
		);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		$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
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		$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' );
222
			$db->execute();
Sigrid Suski's avatar
Sigrid Suski committed
223 224 225 226 227
			$db->setQuery( 'ALTER TABLE #__sobipro_language CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' );
			$db->execute();
		}
		catch ( Exception $x ) {
		}
228 229 230
		try {
			$db->setQuery( 'ALTER TABLE #__sobipro_language CHANGE `sValue` `sValue` MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;' );
			$db->execute();
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		}
		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 {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
			$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 ) {
		}

Sigrid Suski's avatar
Sigrid Suski committed
265 266

		// Table __sobipro_field_data
267 268 269 270 271 272 273 274 275 276 277 278
		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
279
					$db->setQuery( 'ALTER TABLE #__sobipro_field_data ENGINE = MYISAM;' );
280 281
					$db->execute();
				}
Sigrid Suski's avatar
Sigrid Suski committed
282 283 284 285 286 287 288 289
				catch ( Exception $x ) {
				}
				try {
					$db->setQuery( 'ALTER TABLE  `#__sobipro_field_data` ADD FULLTEXT  `baseData` (`baseData`);' );
					$db->execute();
				}
				catch ( Exception $x ) {
				}
290
			}
Sigrid Suski's avatar
Sigrid Suski committed
291 292
		}
		catch ( Exception $x ) {
293 294
		}

Sigrid Suski's avatar
Sigrid Suski committed
295
		// Table __sobipro_language
296 297 298 299 300 301 302 303 304 305 306
		$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
307
				$db->setQuery( 'ALTER TABLE #__sobipro_language ENGINE = MYISAM;' );
308 309
				$db->execute();
			}
Sigrid Suski's avatar
Sigrid Suski committed
310 311 312 313 314 315 316 317
			catch ( Exception $x ) {
			}
			try {
				$db->setQuery( 'ALTER TABLE  `#__sobipro_language` ADD FULLTEXT  `sValue` (`sValue`);' );
				$db->execute();
			}
			catch ( Exception $x ) {
			}
318 319
		}

Sigrid Suski's avatar
Sigrid Suski committed
320 321 322 323 324
		$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
325 326 327 328 329 330 331 332 333 334 335
		$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
336
				$db->setQuery( 'ALTER TABLE #__sobipro_history CHANGE `change` `changeAction` VARCHAR(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;' );
337
				$db->execute();
Sigrid Suski's avatar
Sigrid Suski committed
338 339
			}
			catch ( Exception $x ) {
340 341 342
			}
		}

Sigrid Suski's avatar
Sigrid Suski committed
343
		// Table __sobipro_category
344 345 346
		$result = $db->setQuery( "SHOW COLUMNS FROM `#__sobipro_category` LIKE 'allFields'" );
		$col = $db->loadResult();
		if ( $col != 'allFields' ) {
347 348 349 350 351
			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
352 353
			}
			catch ( Exception $x ) {
354 355 356
			}
		}

357 358 359
		$result = $db->setQuery( "SHOW COLUMNS FROM `#__sobipro_category` LIKE 'section'" );
		$col = $db->loadResult();
		if ( $col != 'section' ) {
360
			$categories = null;
361
			try {
362
				$db->setQuery( 'ALTER TABLE `#__sobipro_category` ADD `section` INT(11) AFTER `id`;' );
363
				$db->execute();
364 365 366 367 368 369 370 371 372
				$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();
373 374 375
				foreach ( $categories as $i => $id ) {
					$parent = SPFactory::config()->getParentPath( $id );
					$sid = ( is_array( $parent ) && count( $parent ) ) ? $parent[ 0 ] : 0;
376
					try {
377
						$db->setQuery( "UPDATE `#__sobipro_category` SET `section` = {$sid} WHERE `id` = {$id};" );
378 379 380
						$db->execute();
					}
					catch ( Exception $x ) {
381
					}
382

383 384
				}
			}
385

386 387
		}

Sigrid Suski's avatar
Sigrid Suski committed
388
		//Table __sobipro_registry
389 390 391
		$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
392
		$db->setQuery( "UPDATE `#__sobipro_registry` SET `params` = 'L15bXHdcLi1dK0BbXHdcLi1dK1wuW2EtekEtWl17MiwyNH0kLw==' WHERE `key` = 'email' " );
393 394 395 396 397 398 399 400 401
		$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
402
		$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;' );
403 404 405 406 407 408 409 410 411
		$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();

412
		$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;" );
413 414 415
		$db->execute();


Sigrid Suski's avatar
Sigrid Suski committed
416 417 418 419
		// 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();
420

421 422
		//1.4.6 (set it for section 1 if it does not exist)
		$db->setQuery( "INSERT IGNORE INTO `#__sobipro_config` ( `sKey` , `sValue` , `section` , `critical` , `cSection` ) VALUES ( '	icon_fonts_load',  '1',  1 , NULL , 'template' )" );
423 424 425 426 427
		$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
428
		}
429 430 431

		catch
		( Exception $x ) {
432
		}
433 434 435 436
		try {
			$db->setQuery( 'DELETE FROM `#__sobipro_config` WHERE `sKey` = "field_types_for_ordering"' );
			$db->execute();
		}
Sigrid Suski's avatar
Sigrid Suski committed
437
		catch ( Exception $x ) {
438 439
		}

440 441 442 443 444 445 446
		// 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 ) {
		}
447

448
		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' );
449

450 451 452 453 454 455
		$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>';
456

457 458
		return true;
	}
459 460


461
	/**
462
	 * Called on installation
463
	 *
464
	 * @param   JAdapterInstance $adapter The object responsible for running this script
465
	 *
466
	 * @return  boolean  True on success
467
	 */
468
	public function install( JAdapterInstance $adapter )
469
//	public function install( $parent )
470 471 472 473 474 475 476 477 478 479 480 481 482
	{
		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
483 484
				implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'tmp', 'SampleData', 'entries' ] ),
				implode( '/', [ JPATH_ROOT, 'images', 'sobipro', 'entries' ] )
485 486 487 488 489 490 491 492
			);
		}
		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' ] ) );
		}
493 494 495
//		if ( file_exists( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'media' ] ) ) ) {
//			JFolder::delete( implode( '/', [ JPATH_ROOT, 'components', 'com_sobipro', 'media' ] ) );
//		}
496
		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' );
497 498 499 500 501 502 503
		$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();
		}
504

505
		$this->installFramework();
506

507 508 509 510
		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>';
511 512 513

		return true;

514 515
	}

516 517 518
	/**
	 * @param $source
	 */
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	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();
		}
	}

	/**
535
	 * Called on uninstallation
536
	 *
537
	 * @param   JAdapterInstance $adapter The object responsible for running this script
538
	 */
539
	public function uninstall( JAdapterInstance $adapter )
540
//	public function uninstall( $parent )
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	{
		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>';

	}

557 558 559
	/**
	 * @param bool $extract
	 */
560 561 562
	protected function installFramework()
	{
		//Sobi Framework installation
563 564 565 566 567 568 569 570 571 572 573
		$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 );
					}
574 575 576
				}
			}
		}
577
//		$src = JPATH_ROOT . '/components/com_sobipro/Sobi.phar.tar.gz';
578 579
//		$framework = $libpath . "/Sobi-" . SOBI_FRAMEWORK_VERSION . ".phar.tar.gz";

580
//		if ( file_exists( $src ) ) {
581
//			JFile::copy( $src, $framework );
582 583
//			JFile::delete( $src );
//		}
584 585 586 587 588
//		else {
//			echo "<p style=\"font-size: 15px;color: #d70303;\">Framework file " . $src . " does not exist.</p>";
//
//			return false;
//		}
589

590 591 592
		// I am guessing that this was what cached the PHAR file. Let's see...
		if ( function_exists( 'opcache_reset' ) ) {
			opcache_reset();
593
			clearstatcache();
594 595
		}

596
		$fwPackage = JPATH_ROOT . '/components/com_sobipro/Sobi-Framework.zip';
597
//		@include_once 'phar://' . $framework . '/Framework.php';
598

599 600 601 602 603 604 605 606 607 608
//		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 );
//				}
609 610
				JFile::delete( $fwPackage );
			}
611 612
			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>';
613 614 615
			}
		}
		else {
616 617
//				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>';
618
		}
619 620 621 622 623 624
//		}
//		else {
//			if ( file_exists( $fwPackage ) ) {
//				JFile::delete( $fwPackage );
//			}
//		}
625 626
	}
}