blob: 5cec4597be80a36a776fb2a23f083ca17be35ea1 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev1e6b72c2012-01-06 19:09:22 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev1e6b72c2012-01-06 19:09:22 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter HTML Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/html_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('heading'))
53{
Timothy Warren01b129a2012-04-27 11:36:50 -040054 /**
55 * Heading
56 *
57 * Generates an HTML heading tag.
58 *
59 * @param string content
60 * @param int heading level
61 * @param string
62 * @return string
63 */
Greg Aker826429c2011-04-18 09:40:19 -050064 function heading($data = '', $h = '1', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Eric Barnesacedd2b2012-07-29 00:15:40 -040066 return '<h'.$h._stringify_attributes($attributes).'>'.$data.'</h'.$h.'>';
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
68}
69
70// ------------------------------------------------------------------------
71
Derek Allard2067d1a2008-11-13 22:59:24 +000072if ( ! function_exists('ul'))
73{
Timothy Warren01b129a2012-04-27 11:36:50 -040074 /**
75 * Unordered List
76 *
77 * Generates an HTML unordered list from an single or multi-dimensional array.
78 *
79 * @param array
80 * @param mixed
81 * @return string
82 */
Derek Allard2067d1a2008-11-13 22:59:24 +000083 function ul($list, $attributes = '')
84 {
85 return _list('ul', $list, $attributes);
86 }
87}
88
89// ------------------------------------------------------------------------
90
Derek Allard2067d1a2008-11-13 22:59:24 +000091if ( ! function_exists('ol'))
92{
Timothy Warren01b129a2012-04-27 11:36:50 -040093 /**
94 * Ordered List
95 *
96 * Generates an HTML ordered list from an single or multi-dimensional array.
97 *
98 * @param array
99 * @param mixed
100 * @return string
101 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 function ol($list, $attributes = '')
103 {
104 return _list('ol', $list, $attributes);
105 }
106}
107
108// ------------------------------------------------------------------------
109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110if ( ! function_exists('_list'))
111{
Timothy Warren01b129a2012-04-27 11:36:50 -0400112 /**
113 * Generates the list
114 *
115 * Generates an HTML ordered list from an single or multi-dimensional array.
116 *
117 * @param string
118 * @param mixed
119 * @param mixed
120 * @param int
121 * @return string
122 */
Rasmus Lerdorfe736f492013-05-18 10:25:03 -0400123 function _list($type = 'ul', $list = array(), $attributes = '', $depth = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
125 // If an array wasn't submitted there's nothing to do...
126 if ( ! is_array($list))
127 {
128 return $list;
129 }
130
131 // Set the indentation based on the depth
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200132 $out = str_repeat(' ', $depth)
133 // Write the opening list tag
134 .'<'.$type._stringify_attributes($attributes).">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136
Derek Jones4b9c6292011-07-01 17:40:48 -0500137 // Cycle through the list elements. If an array is
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // encountered we will recursively call _list()
139
140 static $_last_list_item = '';
141 foreach ($list as $key => $val)
142 {
143 $_last_list_item = $key;
144
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200145 $out .= str_repeat(' ', $depth + 2).'<li>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000146
147 if ( ! is_array($val))
148 {
149 $out .= $val;
150 }
151 else
152 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200153 $out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155
156 $out .= "</li>\n";
157 }
158
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200159 // Set the indentation for the closing tag and apply it
160 return $out.str_repeat(' ', $depth).'</'.$type.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162}
163
164// ------------------------------------------------------------------------
165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166if ( ! function_exists('img'))
167{
Timothy Warren01b129a2012-04-27 11:36:50 -0400168 /**
169 * Image
170 *
171 * Generates an <img /> element
172 *
173 * @param mixed
174 * @param bool
Eric Barnes62ab8b22012-07-28 14:57:04 -0400175 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400176 * @return string
177 */
Eric Barnes62ab8b22012-07-28 14:57:04 -0400178 function img($src = '', $index_page = FALSE, $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 if ( ! is_array($src) )
181 {
182 $src = array('src' => $src);
183 }
184
Derek Allarda0905f32010-07-05 08:11:33 -0400185 // If there is no alt attribute defined, set it to an empty string
186 if ( ! isset($src['alt']))
187 {
188 $src['alt'] = '';
189 }
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 $img = '<img';
192
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200193 foreach ($src as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev356bc662017-03-06 14:39:28 +0200195 if ($k === 'src' && ! preg_match('#^(data:[a-z,;])|(([a-z]+:)?(?<!data:)//)#i', $v))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 if ($index_page === TRUE)
198 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200199 $img .= ' src="'.get_instance()->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201 else
202 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200203 $img .= ' src="'.get_instance()->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
205 }
206 else
207 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300208 $img .= ' '.$k.'="'.$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210 }
211
Andrey Andreev6a64f852012-08-15 11:16:47 +0300212 return $img._stringify_attributes($attributes).' />';
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
214}
215
216// ------------------------------------------------------------------------
217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218if ( ! function_exists('doctype'))
219{
Timothy Warren01b129a2012-04-27 11:36:50 -0400220 /**
221 * Doctype
222 *
223 * Generates a page document type declaration
224 *
Przemyslaw Tomaszewski8711aeb2012-07-22 03:28:41 +0200225 * Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans,
226 * xhtml-frame, html4-strict, html4-trans, and html4-frame.
227 * All values are saved in the doctypes config file.
Timothy Warren01b129a2012-04-27 11:36:50 -0400228 *
229 * @param string type The doctype to be generated
230 * @return string
231 */
Derek Allard93bddd12009-04-14 19:27:38 +0000232 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Andrey Andreev06879112013-01-29 15:05:02 +0200234 static $doctypes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000235
Andrey Andreev06879112013-01-29 15:05:02 +0200236 if ( ! is_array($doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev06879112013-01-29 15:05:02 +0200238 if (file_exists(APPPATH.'config/doctypes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600239 {
240 include(APPPATH.'config/doctypes.php');
241 }
Eric Barnes92808342011-03-18 09:02:37 -0400242
Andrey Andreev06879112013-01-29 15:05:02 +0200243 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Andrey Andreev06879112013-01-29 15:05:02 +0200245 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
246 }
247
248 if (empty($_doctypes) OR ! is_array($_doctypes))
249 {
250 $doctypes = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 return FALSE;
252 }
Andrey Andreev06879112013-01-29 15:05:02 +0200253
254 $doctypes = $_doctypes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256
Andrey Andreev06879112013-01-29 15:05:02 +0200257 return isset($doctypes[$type]) ? $doctypes[$type] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
259}
260
261// ------------------------------------------------------------------------
262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263if ( ! function_exists('link_tag'))
264{
Timothy Warren01b129a2012-04-27 11:36:50 -0400265 /**
266 * Link
267 *
268 * Generates link to a CSS file
269 *
270 * @param mixed stylesheet hrefs or an array
271 * @param string rel
272 * @param string type
273 * @param string title
274 * @param string media
275 * @param bool should index_page be added to the css path
276 * @return string
277 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
279 {
280 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 $link = '<link ';
282
283 if (is_array($href))
284 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200285 foreach ($href as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Andrey Andreevd2e3a6f2014-03-12 16:22:46 +0200287 if ($k === 'href' && ! preg_match('#^([a-z]+:)?//#i', $v))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 if ($index_page === TRUE)
290 {
Derek Allard76763bb2009-09-16 11:25:20 +0000291 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293 else
294 {
Derek Allard76763bb2009-09-16 11:25:20 +0000295 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297 }
298 else
299 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300300 $link .= $k.'="'.$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 else
305 {
Emmanuel Grognet60f78b42014-05-16 08:25:15 +0200306 if (preg_match('#^([a-z]+:)?//#i', $href))
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Derek Allard292dcd82009-09-16 11:26:32 +0000308 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310 elseif ($index_page === TRUE)
311 {
Derek Allard76763bb2009-09-16 11:25:20 +0000312 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
314 else
315 {
Derek Allard76763bb2009-09-16 11:25:20 +0000316 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318
319 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
320
Alex Bilbie773ccc32012-06-02 11:11:08 +0100321 if ($media !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 $link .= 'media="'.$media.'" ';
324 }
325
Alex Bilbie773ccc32012-06-02 11:11:08 +0100326 if ($title !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $link .= 'title="'.$title.'" ';
329 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300332 return $link."/>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334}
335
336// ------------------------------------------------------------------------
337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338if ( ! function_exists('meta'))
339{
Timothy Warren01b129a2012-04-27 11:36:50 -0400340 /**
341 * Generates meta tags from an array of key/values
342 *
343 * @param array
344 * @param string
345 * @param string
346 * @param string
347 * @return string
348 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
350 {
351 // Since we allow the data to be passes as a string, a simple array
352 // or a multidimensional one, we need to do a little prepping.
353 if ( ! is_array($name))
354 {
355 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
356 }
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300357 elseif (isset($name['name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 // Turn single array into multidimensional
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300360 $name = array($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
362
363 $str = '';
364 foreach ($name as $meta)
365 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200366 $type = (isset($meta['type']) && $meta['type'] !== 'name') ? 'http-equiv' : 'name';
Andrey Andreevae31eb52012-05-17 14:54:15 +0300367 $name = isset($meta['name']) ? $meta['name'] : '';
368 $content = isset($meta['content']) ? $meta['content'] : '';
369 $newline = isset($meta['newline']) ? $meta['newline'] : "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000370
371 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
372 }
373
374 return $str;
375 }
376}
377
378// ------------------------------------------------------------------------
379
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200380if ( ! function_exists('br'))
381{
382 /**
383 * Generates HTML BR tags based on number supplied
384 *
Andrey Andreev59f04262014-02-26 19:04:36 +0200385 * @deprecated 3.0.0 Use str_repeat() instead
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200386 * @param int $count Number of times to repeat the tag
387 * @return string
388 */
389 function br($count = 1)
390 {
391 return str_repeat('<br />', $count);
392 }
393}
394
395// ------------------------------------------------------------------------
396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397if ( ! function_exists('nbs'))
398{
Timothy Warren01b129a2012-04-27 11:36:50 -0400399 /**
400 * Generates non-breaking space entities based on number supplied
401 *
Andrey Andreev59f04262014-02-26 19:04:36 +0200402 * @deprecated 3.0.0 Use str_repeat() instead
Timothy Warren01b129a2012-04-27 11:36:50 -0400403 * @param int
404 * @return string
405 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 function nbs($num = 1)
407 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200408 return str_repeat('&nbsp;', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410}