1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Application
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Application helper functions
14 *
15 * @since 1.5
16 */
17 class JApplicationHelper
18 {
19 /**
20 * Client information array
21 *
22 * @var array
23 * @since 1.6
24 */
25 protected static $_clients = array();
26
27 /**
28 * Return the name of the request component [main component]
29 *
30 * @param string $default The default option
31 *
32 * @return string Option (e.g. com_something)
33 *
34 * @since 1.6
35 */
36 public static function getComponentName($default = null)
37 {
38 static $option;
39
40 if ($option)
41 {
42 return $option;
43 }
44
45 $input = JFactory::getApplication()->input;
46 $option = strtolower($input->get('option'));
47
48 if (empty($option))
49 {
50 $option = $default;
51 }
52
53 $input->set('option', $option);
54
55 return $option;
56 }
57
58 /**
59 * Provides a secure hash based on a seed
60 *
61 * @param string $seed Seed string.
62 *
63 * @return string A secure hash
64 *
65 * @since 3.2
66 */
67 public static function getHash($seed)
68 {
69 return md5(JFactory::getConfig()->get('secret') . $seed);
70 }
71
72 /**
73 * This method transliterates a string into a URL
74 * safe string or returns a URL safe UTF-8 string
75 * based on the global configuration
76 *
77 * @param string $string String to process
78 * @param string $language Language to transliterate to if unicode slugs are disabled
79 *
80 * @return string Processed string
81 *
82 * @since 3.2
83 */
84 public static function stringURLSafe($string, $language = '')
85 {
86 if (JFactory::getConfig()->get('unicodeslugs') == 1)
87 {
88 $output = JFilterOutput::stringURLUnicodeSlug($string);
89 }
90 else
91 {
92 if ($language === '*' || $language === '')
93 {
94 $languageParams = JComponentHelper::getParams('com_languages');
95 $language = $languageParams->get('site');
96 }
97 $output = JFilterOutput::stringURLSafe($string, $language);
98 }
99
100 return $output;
101 }
102
103 /**
104 * Gets information on a specific client id. This method will be useful in
105 * future versions when we start mapping applications in the database.
106 *
107 * This method will return a client information array if called
108 * with no arguments which can be used to add custom application information.
109 *
110 * @param integer $id A client identifier
111 * @param boolean $byName If True, find the client by its name
112 *
113 * @return mixed Object describing the client or false if not known
114 *
115 * @since 1.5
116 */
117 public static function getClientInfo($id = null, $byName = false)
118 {
119 // Only create the array if it is empty
120 if (empty(self::$_clients))
121 {
122 $obj = new stdClass;
123
124 // Site Client
125 $obj->id = 0;
126 $obj->name = 'site';
127 $obj->path = JPATH_SITE;
128 self::$_clients[0] = clone $obj;
129
130 // Administrator Client
131 $obj->id = 1;
132 $obj->name = 'administrator';
133 $obj->path = JPATH_ADMINISTRATOR;
134 self::$_clients[1] = clone $obj;
135
136 // Installation Client
137 $obj->id = 2;
138 $obj->name = 'installation';
139 $obj->path = JPATH_INSTALLATION;
140 self::$_clients[2] = clone $obj;
141 }
142
143 // If no client id has been passed return the whole array
144 if ($id === null)
145 {
146 return self::$_clients;
147 }
148
149 // Are we looking for client information by id or by name?
150 if (!$byName)
151 {
152 if (isset(self::$_clients[$id]))
153 {
154 return self::$_clients[$id];
155 }
156 }
157 else
158 {
159 foreach (self::$_clients as $client)
160 {
161 if ($client->name == strtolower($id))
162 {
163 return $client;
164 }
165 }
166 }
167
168 return;
169 }
170
171 /**
172 * Adds information for a client.
173 *
174 * @param mixed $client A client identifier either an array or object
175 *
176 * @return boolean True if the information is added. False on error
177 *
178 * @since 1.6
179 */
180 public static function addClientInfo($client)
181 {
182 if (is_array($client))
183 {
184 $client = (object) $client;
185 }
186
187 if (!is_object($client))
188 {
189 return false;
190 }
191
192 $info = self::getClientInfo();
193
194 if (!isset($client->id))
195 {
196 $client->id = count($info);
197 }
198
199 self::$_clients[$client->id] = clone $client;
200
201 return true;
202 }
203
204 /**
205 * Parse a XML install manifest file.
206 *
207 * XML Root tag should be 'install' except for languages which use meta file.
208 *
209 * @param string $path Full path to XML file.
210 *
211 * @return array XML metadata.
212 *
213 * @since 1.5
214 * @deprecated 4.0 Use JInstaller::parseXMLInstallFile instead.
215 */
216 public static function parseXMLInstallFile($path)
217 {
218 JLog::add('JApplicationHelper::parseXMLInstallFile is deprecated. Use JInstaller::parseXMLInstallFile instead.', JLog::WARNING, 'deprecated');
219
220 return JInstaller::parseXMLInstallFile($path);
221 }
222
223 /**
224 * Parse a XML language meta file.
225 *
226 * XML Root tag for languages which is meta file.
227 *
228 * @param string $path Full path to XML file.
229 *
230 * @return array XML metadata.
231 *
232 * @since 1.5
233 * @deprecated 4.0 Use JInstaller::parseXMLInstallFile instead.
234 */
235 public static function parseXMLLangMetaFile($path)
236 {
237 JLog::add('JApplicationHelper::parseXMLLangMetaFile is deprecated. Use JInstaller::parseXMLInstallFile instead.', JLog::WARNING, 'deprecated');
238
239 // Check if meta file exists.
240 if (!file_exists($path))
241 {
242 return false;
243 }
244
245 // Read the file to see if it's a valid component XML file
246 $xml = simplexml_load_file($path);
247
248 if (!$xml)
249 {
250 return false;
251 }
252
253 /*
254 * Check for a valid XML root tag.
255 *
256 * Should be 'metafile'.
257 */
258 if ($xml->getName() !== 'metafile')
259 {
260 unset($xml);
261
262 return false;
263 }
264
265 $data = array();
266
267 $data['name'] = (string) $xml->name;
268 $data['type'] = $xml->attributes()->type;
269
270 $data['creationDate'] = ((string) $xml->creationDate) ?: JText::_('JLIB_UNKNOWN');
271 $data['author'] = ((string) $xml->author) ?: JText::_('JLIB_UNKNOWN');
272
273 $data['copyright'] = (string) $xml->copyright;
274 $data['authorEmail'] = (string) $xml->authorEmail;
275 $data['authorUrl'] = (string) $xml->authorUrl;
276 $data['version'] = (string) $xml->version;
277 $data['description'] = (string) $xml->description;
278 $data['group'] = (string) $xml->group;
279
280 return $data;
281 }
282 }
283