Blame view

administrator/components/com_admin/models/help.php 3.93 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_admin
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

use Joomla\String\StringHelper;

/**
 * Admin Component Help Model
 *
 * @since  1.6
 */
class AdminModelHelp extends JModelLegacy
{
	/**
	 * The search string
	 *
	 * @var    string
	 * @since  1.6
	 */
	protected $help_search = null;

	/**
	 * The page to be viewed
	 *
	 * @var    string
	 * @since  1.6
	 */
	protected $page = null;

	/**
	 * The ISO language tag
	 *
	 * @var    string
	 * @since  1.6
	 */
	protected $lang_tag = null;

	/**
	 * Table of contents
	 *
	 * @var    array
	 * @since  1.6
	 */
	protected $toc = null;

	/**
	 * URL for the latest version check
	 *
	 * @var    string
	 * @since  1.6
	 */
	protected $latest_version_check = null;

	/**
	 * Method to get the help search string
	 *
	 * @return  string  Help search string
	 *
	 * @since   1.6
	 */
	public function &getHelpSearch()
	{
		if (is_null($this->help_search))
		{
			$this->help_search = JFactory::getApplication()->input->getString('helpsearch');
		}

		return $this->help_search;
	}

	/**
	 * Method to get the page
	 *
	 * @return  string  The page
	 *
	 * @since   1.6
	 */
	public function &getPage()
	{
		if (is_null($this->page))
		{
			$this->page = JHelp::createUrl(JFactory::getApplication()->input->get('page', 'JHELP_START_HERE'));
		}

		return $this->page;
	}

	/**
	 * Method to get the lang tag
	 *
	 * @return  string  lang iso tag
	 *
	 * @since  1.6
	 */
	public function getLangTag()
	{
		if (is_null($this->lang_tag))
		{
			$this->lang_tag = JFactory::getLanguage()->getTag();

			if (!is_dir(JPATH_BASE . '/help/' . $this->lang_tag))
			{
				// Use English as fallback
				$this->lang_tag = 'en-GB';
			}
		}

		return $this->lang_tag;
	}

	/**
	 * Method to get the table of contents
	 *
	 * @return  array  Table of contents
	 */
	public function &getToc()
	{
		if (!is_null($this->toc))
		{
			return $this->toc;
		}

		// Get vars
		$lang_tag    = $this->getLangTag();
		$help_search = $this->getHelpSearch();

		// New style - Check for a TOC JSON file
		if (file_exists(JPATH_BASE . '/help/' . $lang_tag . '/toc.json'))
		{
			$data = json_decode(file_get_contents(JPATH_BASE . '/help/' . $lang_tag . '/toc.json'));

			// Loop through the data array
			foreach ($data as $key => $value)
			{
				$this->toc[$key] = JText::_('COM_ADMIN_HELP_' . $value);
			}

			// Sort the Table of Contents
			asort($this->toc);

			return $this->toc;
		}

		// Get Help files
		jimport('joomla.filesystem.folder');
		$files = JFolder::files(JPATH_BASE . '/help/' . $lang_tag, '\.xml$|\.html$');
		$this->toc = array();

		foreach ($files as $file)
		{
			$buffer = file_get_contents(JPATH_BASE . '/help/' . $lang_tag . '/' . $file);

			if (!preg_match('#<title>(.*?)</title>#', $buffer, $m))
			{
				continue;
			}

			$title = trim($m[1]);

			if (!$title)
			{
				continue;
			}

			// Translate the page title
			$title = JText::_($title);

			// Strip the extension
			$file = preg_replace('#\.xml$|\.html$#', '', $file);

			if ($help_search && StringHelper::strpos(StringHelper::strtolower(strip_tags($buffer)), StringHelper::strtolower($help_search)) === false)
			{
				continue;
			}

			// Add an item in the Table of Contents
			$this->toc[$file] = $title;
		}

		// Sort the Table of Contents
		asort($this->toc);

		return $this->toc;
	}

	/**
	 * Method to get the latest version check
	 *
	 * @return  string  Latest Version Check URL
	 */
	public function &getLatestVersionCheck()
	{
		if (!$this->latest_version_check)
		{
			$override = 'https://help.joomla.org/proxy/index.php?keyref=Help{major}{minor}:'
				. 'Joomla_Version_{major}_{minor}_{maintenance}/{langcode}&amp;lang={langcode}';
			$this->latest_version_check = JHelp::createUrl('JVERSION', false, $override);
		}

		return $this->latest_version_check;
	}
}