Your IP : 216.73.216.240


Current Path : /home/juvelize/www/administrator/manifests/packages/articledetails/
Upload File :
Current File : /home/juvelize/www/administrator/manifests/packages/articledetails/installer.php

<?php
/**
 * @copyright	Copyright (C) 2011 Simplify Your Web, Inc. All rights reserved.
 * @license		GNU General Public License version 3 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Installer\InstallerHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Installer\Installer;

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');

/**
 * Script file of the Article Details package
 */
class pkg_articledetailsInstallerScript
{
	static $version = '6.2.2';
	static $minimum_needed_library_version = '1.6.19';
	static $available_languages = array('de-DE', 'en-GB', 'es-ES', 'fa-IR', 'fi-FI', 'fr-FR', 'it-IT', 'nl-NL', 'pt-BR', 'ru-RU', 'sl-SI', 'tr-TR');
	static $download_link = 'https://simplifyyourweb.com/downloads/syw-extension-library';
	static $changelog_link = 'https://simplifyyourweb.com/free-products/article-details/file/364-article-details';
	static $translation_link = 'https://simplifyyourweb.com/translators';

	/**
	 * Called before an install/update method
	 *
	 * @return  boolean  True on success
	 */
	public function preflight($type, $parent)
	{
	    if ($type == 'install' || $type == 'update') {	        
	        
	        // make sure we are under Joomla 3.8 or over
	        
	        if (version_compare(JVERSION, '3.8.0', 'lt') || version_compare(JVERSION, '3.11.0', 'gt')) {
	            // keep JFactory and JText so that if installed in Joomla < 3.8, it does not crash but shows the message
	            Factory::getApplication()->enqueueMessage(Text::sprintf('JOOMLA_REQUIRED_VERSION', '3.8', 'https://simplifyyourweb.com/free-products/article-details#downloads'), 'error');
	            return false;
	        }
	        
    		// check if syw library is present

    		if (!\JFolder::exists(JPATH_ROOT.'/libraries/syw')) {

    			if (!$this->installOrUpdatePackage($parent, 'lib_syw')) {
    				$message = Text::_('SYWLIBRARY_INSTALLFAILED').'<br /><a href="'.self::$download_link.'" target="_blank">'.Text::_('SYWLIBRARY_DOWNLOAD').'</a>';
    				Factory::getApplication()->enqueueMessage($message, 'error');
    				return false;
    			}

    			Factory::getApplication()->enqueueMessage(Text::sprintf('SYWLIBRARY_INSTALLED', self::$minimum_needed_library_version), 'message');

    		} else {
    		    jimport('syw.version', JPATH_LIBRARIES);

    			if (SYWVersion::isCompatible(self::$minimum_needed_library_version)) {

    				Factory::getApplication()->enqueueMessage(Text::_('SYWLIBRARY_COMPATIBLE'), 'message');

    			} else {

    				if (!$this->installOrUpdatePackage($parent, 'lib_syw')) {
    					$message = Text::_('SYWLIBRARY_UPDATEFAILED').'<br />'.Text::_('SYWLIBRARY_UPDATE');
    					Factory::getApplication()->enqueueMessage($message, 'error');
    					return false;
    				}

    				Factory::getApplication()->enqueueMessage(Text::sprintf('SYWLIBRARY_UPDATED', self::$minimum_needed_library_version), 'message');
    			}
    		}
	    }

		return true;
	}

	/**
	 * Called after an install/update method
	 *
	 * @return  boolean  True on success
	 */
	public function postflight($type, $parent)
	{
	    if ($type == 'install' || $type == 'update') {
     		echo '<p style="margin: 20px 0">';
    		echo '<img src="../plugins/content/articledetails/images/logo.png" />';
    		echo '<br /><br /><span class="label">'.Text::sprintf('PKG_ARTICLEDETAILS_VERSION', self::$version).'</span>';
    		echo '<br /><br />Olivier Buisard @ <a href="https://simplifyyourweb.com" target="_blank">Simplify Your Web</a>';
     		echo '</p>';

     		// language test

     		$current_language = Factory::getLanguage()->getTag();
     		if (!in_array($current_language, self::$available_languages)) {
     			Factory::getApplication()->enqueueMessage('The ' . Factory::getLanguage()->getName() . ' language is missing for this extension.<br /><a href="' . self::$translation_link . '" target="_blank">Please consider contributing to its translation</a>', 'notice');
     		}

     		// remove the old plugin update sites

     		$this->removeUpdateSite('plugin', 'articledetails', 'content', 'http://www.barejoomlatemplates.com/autoupdates/articledetails/articledetails-update.xml');
     		$this->removeUpdateSite('package', 'pkg_articledetails', '', 'http://www.barejoomlatemplates.com/autoupdates/articledetails/articledetails-pkg-update.xml');
	    }

 		// won't be an update on package first install, when moving from plugin to package
	    if ($type == 'update') {

	        // try Joomla 4 version

// 	        echo '<div class="alert alert-info" style="margin: 10px 0 20px 0; display: flex; align-items: center; padding: 8px 14px; max-width: 480px; box-sizing: border-box">';
// 	        echo '<img src="../plugins/content/articledetails/images/J!4_red.png" />';
// 	        echo '<p style="margin: 0 8px">';
// 	        echo '<span>' . Text::_('PKG_ARTICLEDETAILS_INFO_JOOMLA4RELEASENOTIFICATION') . '</span>';
// 	        echo '<br /><br /><a class="btn btn-info" href="https://simplifyyourweb.com/free-products/article-details/files/file/388-article-details" target="_blank">' . Text::_('PKG_ARTICLEDETAILS_INFO_JOOMLA4GOTOVERSION') . '</a>';
// 	        echo '</p>';
// 	        echo '</div>';

			// update warning

			Factory::getApplication()->enqueueMessage(Text::sprintf('PKG_ARTICLEDETAILS_WARNING_RELEASENOTES', self::$changelog_link), 'warning');

			// delete unnecessary files

			$files = array();

			$files[] = '/plugins/content/articledetails/fields/styleselect.php';
			$files[] = '/plugins/content/articledetails/images/glyphicons-halflings.png';
			$files[] = '/plugins/content/articledetails/images/preview.png';
			$files[] = '/plugins/content/articledetails/stylemaster.css.php';
			$files[] = '/plugins/content/articledetails/printmaster.css.php';
			$files[] = '/plugins/content/articledetails/style.css';
			$files[] = '/plugins/content/articledetails/print.css';

			foreach ($files as $file) {
			    if (\JFile::exists(JPATH_ROOT.$file) && !\JFile::delete(JPATH_ROOT.$file)) {
			        Factory::getApplication()->enqueueMessage(Text::sprintf('PKG_ARTICLEDETAILS_ERROR_DELETINGFILEFOLDER', $file), 'warning');
			    }
			}

			// remove old cached headers which may interfere with fixes, updates or new additions

			$filenames_to_delete = array();

			if (function_exists('glob')) {

				// remove old cached headers which may interfere with fixes, updates or new additions

				$filenames = glob(JPATH_SITE.'/cache/plg_content_articledetails/style_*.css');
				if ($filenames != false) {
				    $filenames_to_delete = array_merge($filenames_to_delete, $filenames);
				}

				$filenames = glob(JPATH_SITE.'/cache/plg_content_articledetails/print_*.css');
				if ($filenames != false) {
				    $filenames_to_delete = array_merge($filenames_to_delete, $filenames);
				}
			}

			foreach ($filenames_to_delete as $filename) {
			    if (\JFile::exists($filename) && !\JFile::delete($filename)) {
			        Factory::getApplication()->enqueueMessage(Text::sprintf('PKG_ARTICLEDETAILS_ERROR_DELETINGFILEFOLDER', $filename), 'warning');
			    }
			}

			// move old fields to new subforms

			$plugin = PluginHelper::getPlugin('content', 'articledetails');

			if (is_object($plugin) && !empty($plugin->params)) {

				$plugin_params = json_decode($plugin->params, true);

				$changes_made = false;

				// move social networks to subforms

				if (isset($plugin_params['share_pos_1'])) {

					$changes_made = true;

					$j = 0;
					$j_sub = 0;

					$social_network_blocs = array();

					while ($j < 6) {
						if (isset($plugin_params['share_pos_'.($j + 1)]) && $plugin_params['share_pos_'.($j + 1)] != 'none' && $plugin_params['share_pos_'.($j + 1)] != 'google' && $plugin_params['share_pos_'.($j + 1)] != 'stumbleupon') {
							$social_network_bloc = array();
							$social_network_bloc['social_network'] = $plugin_params['share_pos_'.($j + 1)];
							$social_network_bloc['other_network'] = '';
							$social_network_bloc['network_color'] = '';

							$social_network_blocs['social_networks'.$j_sub] = $social_network_bloc;
							$j_sub++;
						}
						$j++;
					}

					$j = 0;
					while ($j < 6) {
						unset($plugin_params['share_pos_'.($j + 1)]);
						$j++;
					}

					if (!empty($social_network_blocs)) {
						$plugin_params['social_networks'] = $social_network_blocs;
					}
				}

				// move detail fields

				if (isset($plugin_params['show_icons_b1'])) {

					$changes_made = true;

					$j = 0;
					$j_sub = 0;

					$info_blocs = array();

					while ($j < 3) {
						if (isset($plugin_params['info_b'.($j + 1)]) && $plugin_params['info_b'.($j + 1)] != 'none') {
							$info_bloc = array();
							$info_bloc['show_icons'] = isset($plugin_params['show_icons_b'.($j + 1)]) ? $plugin_params['show_icons_b'.($j + 1)] : 0;
							$info_bloc['icon'] = '';
							$info_bloc['prepend'] = isset($plugin_params['prepend_b'.($j + 1)]) ? $plugin_params['prepend_b'.($j + 1)] : '';
							$info_bloc['append'] = '';
							$info_bloc['info'] = $plugin_params['info_b'.($j + 1)];
							$info_bloc['extra_classes'] = isset($plugin_params['extra_classes_b'.($j + 1)]) ? $plugin_params['extra_classes_b'.($j + 1)] : '';
							$info_bloc['new_line'] = isset($plugin_params['new_line_b'.($j + 1)]) ? $plugin_params['new_line_b'.($j + 1)] : 0;
							$info_bloc['showing_in'] = isset($plugin_params['showing_in_b'.($j + 1)]) ? $plugin_params['showing_in_b'.($j + 1)] : '';
							$info_bloc['access'] = 1;

							$info_blocs['information_blocks'.$j_sub] = $info_bloc;
							$j_sub++;
						}
						$j++;
					}

					$j = 0;
					while ($j < 3) {
						unset($plugin_params['show_icons_b'.($j + 1)]);
						unset($plugin_params['prepend_b'.($j + 1)]);
						unset($plugin_params['info_b'.($j + 1)]);
						unset($plugin_params['extra_classes_b'.($j + 1)]);
						unset($plugin_params['new_line_b'.($j + 1)]);
						unset($plugin_params['showing_in_b'.($j + 1)]);
						$j++;
					}

					if (!empty($info_blocs)) {
						$plugin_params['before_title_information_blocks'] = $info_blocs;
					}
				}

				if (isset($plugin_params['show_icons_1'])) {

					$changes_made = true;

					$j = 0;
					$j_sub = 0;

					$info_blocs = array();

					while ($j < 9) {
						if (isset($plugin_params['info_'.($j + 1)]) && $plugin_params['info_'.($j + 1)] != 'none') {
							$info_bloc = array();
							$info_bloc['show_icons'] = isset($plugin_params['show_icons_'.($j + 1)]) ? $plugin_params['show_icons_'.($j + 1)] : 0;
							$info_bloc['icon'] = '';
							$info_bloc['prepend'] = isset($plugin_params['prepend_'.($j + 1)]) ? $plugin_params['prepend_'.($j + 1)] : '';
							$info_bloc['append'] = '';
							$info_bloc['info'] = $plugin_params['info_'.($j + 1)];
							$info_bloc['extra_classes'] = isset($plugin_params['extra_classes_'.($j + 1)]) ? $plugin_params['extra_classes_'.($j + 1)] : '';
							$info_bloc['new_line'] = isset($plugin_params['new_line_'.($j + 1)]) ? $plugin_params['new_line_'.($j + 1)] : 0;
							$info_bloc['showing_in'] = isset($plugin_params['showing_in_'.($j + 1)]) ? $plugin_params['showing_in_'.($j + 1)] : '';
							$info_bloc['access'] = 1;

							$info_blocs['information_blocks'.$j_sub] = $info_bloc;
							$j_sub++;
						}
						$j++;
					}

					$j = 0;
					while ($j < 9) {
						unset($plugin_params['show_icons_'.($j + 1)]);
						unset($plugin_params['prepend_'.($j + 1)]);
						unset($plugin_params['info_'.($j + 1)]);
						unset($plugin_params['extra_classes_'.($j + 1)]);
						unset($plugin_params['new_line_'.($j + 1)]);
						unset($plugin_params['showing_in_'.($j + 1)]);
						$j++;
					}

					if (!empty($info_blocs)) {
						$plugin_params['after_title_information_blocks'] = $info_blocs;
					}
				}

				if (isset($plugin_params['show_icons_foot1'])) {

					$changes_made = true;

					$j = 0;
					$j_sub = 0;

					$info_blocs = array();

					while ($j < 3) {
						if (isset($plugin_params['info_foot'.($j + 1)]) && $plugin_params['info_foot'.($j + 1)] != 'none') {
							$info_bloc = array();
							$info_bloc['show_icons'] = isset($plugin_params['show_icons_foot'.($j + 1)]) ? $plugin_params['show_icons_foot'.($j + 1)] : 0;
							$info_bloc['icon'] = '';
							$info_bloc['prepend'] = isset($plugin_params['prepend_foot'.($j + 1)]) ? $plugin_params['prepend_foot'.($j + 1)] : '';
							$info_bloc['append'] = '';
							$info_bloc['info'] = $plugin_params['info_foot'.($j + 1)];
							$info_bloc['extra_classes'] = isset($plugin_params['extra_classes_foot'.($j + 1)]) ? $plugin_params['extra_classes_foot'.($j + 1)] : '';
							$info_bloc['new_line'] = isset($plugin_params['new_line_foot'.($j + 1)]) ? $plugin_params['new_line_foot'.($j + 1)] : 0;
							$info_bloc['showing_in'] = 2;
							$info_bloc['access'] = 1;

							$info_blocs['information_blocks'.$j_sub] = $info_bloc;
							$j_sub++;
						}
						$j++;
					}

					$j = 0;
					while ($j < 3) {
						unset($plugin_params['show_icons_foot'.($j + 1)]);
						unset($plugin_params['prepend_foot'.($j + 1)]);
						unset($plugin_params['info_foot'.($j + 1)]);
						unset($plugin_params['extra_classes_foot'.($j + 1)]);
						unset($plugin_params['new_line_foot'.($j + 1)]);
						unset($plugin_params['showing_in_foot'.($j + 1)]);
						$j++;
					}

					if (!empty($info_blocs)) {
						$plugin_params['footer_information_blocks'] = $info_blocs;
					}
				}

				if ($changes_made) {

					$db = Factory::getDBO();

					$query = $db->getQuery(true);

					$query->update('#__extensions');
					$query->set($db->quoteName('params').'='.$db->quote(json_encode($plugin_params)));
					$query->where($db->quoteName('type').'='.$db->quote('plugin'));
					$query->where($db->quoteName('folder').'='.$db->quote('content'));
					$query->where($db->quoteName('element').'='.$db->quote('articledetails'));

					$db->setQuery($query);

					try {
						$db->execute();
					} catch (\RuntimeException $e) {
						Factory::getApplication()->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
						return false;
					}
				}
			}
		}

		return true;
	}

	private function removeUpdateSite($type, $element, $folder = '', $location = '')
	{
	    $db = Factory::getDBO();

	    $query = $db->getQuery(true);

	    $query->select('extension_id');
	    $query->from('#__extensions');
	    $query->where($db->quoteName('type').'='.$db->quote($type));
	    $query->where($db->quoteName('element').'='.$db->quote($element));
	    if ($folder) {
	        $query->where($db->quoteName('folder').'='.$db->quote($folder));
	    }

	    $db->setQuery($query);

	    $extension_id = '';
	    try {
	        $extension_id = $db->loadResult();
	    } catch (\RuntimeException $e) {
	        Factory::getApplication()->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
	        return false;
	    }

	    if ($extension_id) {

	        $query->clear();

	        $query->select('update_site_id');
	        $query->from('#__update_sites_extensions');
	        $query->where($db->quoteName('extension_id').'='.$db->quote($extension_id));

	        $db->setQuery($query);

	        $updatesite_id = array(); // can have several results
	        try {
	            $updatesite_id = $db->loadColumn();
	        } catch (\RuntimeException $e) {
	            Factory::getApplication()->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
	            return false;
	        }

	        if (empty($updatesite_id)) {
	            return false;
	        } else if (count($updatesite_id) == 1) {

	            $query->clear();

	            $query->delete($db->quoteName('#__update_sites'));
	            $query->where($db->quoteName('update_site_id').' = '.$db->quote($updatesite_id[0]));

	            $db->setQuery($query);

	            try {
	                $db->execute();
	            } catch (\RuntimeException $e) {
	                Factory::getApplication()->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
	                return false;
	            }
	        } else { // several update sites exist for the same extension therefore we need to specify which to delete

	            if ($location) {
	                $query->clear();

	                $query->delete($db->quoteName('#__update_sites'));
	                $query->where($db->quoteName('update_site_id').' IN ('.implode(',', $updatesite_id).')');
	                $query->where($db->quoteName('location').' = '.$db->quote($location));

	                $db->setQuery($query);

	                try {
	                    $db->execute();
	                } catch (\RuntimeException $e) {
	                    Factory::getApplication()->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
	                    return false;
	                }
	            } else {
	                return false;
	            }
	        }
	    } else {
	        return false;
	    }

	    return true;
	}

	private function installOrUpdatePackage($installer, $package_name, $installation_type = 'install')
	{
	    // Get the path to the package

	    $sourcePath = $installer->getParent()->getPath('source');
	    $sourcePackage = $sourcePath . '/packages/'.$package_name.'.zip';

	    // Extract and install the package

	    $package = InstallerHelper::unpack($sourcePackage);
	    if ($package === false || (is_array($package) && $package['type'] === false)) {
	        return false;
	    }

	    $tmpInstaller = new Installer();

	    if ($installation_type === 'install') {
	        return $tmpInstaller->install($package['dir']);
	    } else {
	        return $tmpInstaller->update($package['dir']);
	    }
	}

	/**
	 * Called on installation
	 *
	 * @return  boolean  True on success
	 */
	public function install($parent) {}

	/**
	 * Called on update
	 *
	 * @return  boolean  True on success
	 */
	public function update($parent) {}

	/**
	 * Called on uninstallation
	 */
	public function uninstall($parent) {}

}
?>