1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_admin
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('_JEXEC') or die;
11
12/**
13 * Sysinfo View class for the Admin component
14 *
15 * @since  3.5
16 */
17class AdminViewSysinfo extends JViewLegacy
18{
19	/**
20	 * Execute and display a template script.
21	 *
22	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
23	 *
24	 * @return  mixed  A string if successful, otherwise an Error object.
25	 *
26	 * @since   3.5
27	 */
28	public function display($tpl = null)
29	{
30		// Access check.
31		if (!JFactory::getUser()->authorise('core.admin'))
32		{
33			throw new JAccessExceptionNotallowed(JText::_('JERROR_ALERTNOAUTHOR'), 403);
34		}
35
36		header('Content-Type: text/plain; charset=utf-8');
37		header('Content-Description: File Transfer');
38		header('Content-Disposition: attachment; filename="systeminfo-' . date('c') . '.txt"');
39		header('Cache-Control: must-revalidate');
40
41		$data = $this->getLayoutData();
42
43		$lines = array();
44
45		foreach ($data as $sectionName => $section)
46		{
47			$customRenderingMethod = 'render' . ucfirst($sectionName);
48
49			if (method_exists($this, $customRenderingMethod))
50			{
51				$lines[] = $this->$customRenderingMethod($section['title'], $section['data']);
52			}
53			else
54			{
55				$lines[] = $this->renderSection($section['title'], $section['data']);
56			}
57		}
58
59		echo str_replace(JPATH_ROOT, 'xxxxxx', implode("\n\n", $lines));
60
61		JFactory::getApplication()->close();
62	}
63
64	/**
65	 * Get the data for the view
66	 *
67	 * @return  array
68	 *
69	 * @since   3.5
70	 */
71	protected function getLayoutData()
72	{
73		$model = $this->getModel();
74
75		return array(
76			'info' => array(
77				'title' => JText::_('COM_ADMIN_SYSTEM_INFORMATION', true),
78				'data'  => $model->getSafeData('info')
79			),
80			'phpSettings' => array(
81				'title' => JText::_('COM_ADMIN_PHP_SETTINGS', true),
82				'data'  => $model->getSafeData('phpSettings')
83			),
84			'config' => array(
85				'title' => JText::_('COM_ADMIN_CONFIGURATION_FILE', true),
86				'data'  => $model->getSafeData('config')
87			),
88			'directories' => array(
89				'title' => JText::_('COM_ADMIN_DIRECTORY_PERMISSIONS', true),
90				'data'  => $model->getSafeData('directory', true)
91			),
92			'phpInfo' => array(
93				'title' => JText::_('COM_ADMIN_PHP_INFORMATION', true),
94				'data'  => $model->getSafeData('phpInfoArray')
95			),
96			'extensions' => array(
97				'title' => JText::_('COM_ADMIN_EXTENSIONS', true),
98				'data'  => $model->getSafeData('extensions')
99			)
100		);
101	}
102
103	/**
104	 * Render a section
105	 *
106	 * @param   string   $sectionName  Name of the section to render
107	 * @param   array    $sectionData  Data of the section to render
108	 * @param   integer  $level        Depth level for indentation
109	 *
110	 * @return  string
111	 *
112	 * @since   3.5
113	 */
114	protected function renderSection($sectionName, $sectionData, $level = 0)
115	{
116		$lines = array();
117
118		$margin = ($level > 0) ? str_repeat("\t", $level) : null;
119
120		$lines[] = $margin . '=============';
121		$lines[] = $margin . $sectionName;
122		$lines[] = $margin . '=============';
123		$level++;
124
125		foreach ($sectionData as $name => $value)
126		{
127			if (is_array($value))
128			{
129				if ($name == 'Directive')
130				{
131					continue;
132				}
133
134				$lines[] = '';
135				$lines[] = $this->renderSection($name, $value, $level);
136			}
137			else
138			{
139				if (is_bool($value))
140				{
141					$value = $value ? 'true' : 'false';
142				}
143
144				if (is_int($name) && ($name == 0 || $name == 1))
145				{
146					$name = ($name == 0 ? 'Local Value' : 'Master Value');
147				}
148
149				$lines[] = $margin . $name . ': ' . $value;
150			}
151		}
152
153		return implode("\n", $lines);
154	}
155
156	/**
157	 * Specific rendering for directories
158	 *
159	 * @param   string   $sectionName  Name of the section
160	 * @param   array    $sectionData  Directories information
161	 * @param   integer  $level        Starting level
162	 *
163	 * @return  string
164	 *
165	 * @since   3.5
166	 */
167	protected function renderDirectories($sectionName, $sectionData, $level = -1)
168	{
169		foreach ($sectionData as $directory => $data)
170		{
171			$sectionData[$directory] = $data['writable'] ? ' writable' : ' NOT writable';
172		}
173
174		return $this->renderSection($sectionName, $sectionData, $level);
175	}
176}
177