html_helper.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * 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:
  17. *
  18. * 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
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter HTML Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/helpers/html_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('heading'))
  50. {
  51. /**
  52. * Heading
  53. *
  54. * Generates an HTML heading tag.
  55. *
  56. * @param string content
  57. * @param int heading level
  58. * @param string
  59. * @return string
  60. */
  61. function heading($data = '', $h = '1', $attributes = '')
  62. {
  63. return '<h'.$h._stringify_attributes($attributes).'>'.$data.'</h'.$h.'>';
  64. }
  65. }
  66. // ------------------------------------------------------------------------
  67. if ( ! function_exists('ul'))
  68. {
  69. /**
  70. * Unordered List
  71. *
  72. * Generates an HTML unordered list from an single or multi-dimensional array.
  73. *
  74. * @param array
  75. * @param mixed
  76. * @return string
  77. */
  78. function ul($list, $attributes = '')
  79. {
  80. return _list('ul', $list, $attributes);
  81. }
  82. }
  83. // ------------------------------------------------------------------------
  84. if ( ! function_exists('ol'))
  85. {
  86. /**
  87. * Ordered List
  88. *
  89. * Generates an HTML ordered list from an single or multi-dimensional array.
  90. *
  91. * @param array
  92. * @param mixed
  93. * @return string
  94. */
  95. function ol($list, $attributes = '')
  96. {
  97. return _list('ol', $list, $attributes);
  98. }
  99. }
  100. // ------------------------------------------------------------------------
  101. if ( ! function_exists('_list'))
  102. {
  103. /**
  104. * Generates the list
  105. *
  106. * Generates an HTML ordered list from an single or multi-dimensional array.
  107. *
  108. * @param string
  109. * @param mixed
  110. * @param mixed
  111. * @param int
  112. * @return string
  113. */
  114. function _list($type = 'ul', $list = array(), $attributes = '', $depth = 0)
  115. {
  116. // If an array wasn't submitted there's nothing to do...
  117. if ( ! is_array($list))
  118. {
  119. return $list;
  120. }
  121. // Set the indentation based on the depth
  122. $out = str_repeat(' ', $depth)
  123. // Write the opening list tag
  124. .'<'.$type._stringify_attributes($attributes).">\n";
  125. // Cycle through the list elements. If an array is
  126. // encountered we will recursively call _list()
  127. static $_last_list_item = '';
  128. foreach ($list as $key => $val)
  129. {
  130. $_last_list_item = $key;
  131. $out .= str_repeat(' ', $depth + 2).'<li>';
  132. if ( ! is_array($val))
  133. {
  134. $out .= $val;
  135. }
  136. else
  137. {
  138. $out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
  139. }
  140. $out .= "</li>\n";
  141. }
  142. // Set the indentation for the closing tag and apply it
  143. return $out.str_repeat(' ', $depth).'</'.$type.">\n";
  144. }
  145. }
  146. // ------------------------------------------------------------------------
  147. if ( ! function_exists('img'))
  148. {
  149. /**
  150. * Image
  151. *
  152. * Generates an <img /> element
  153. *
  154. * @param mixed
  155. * @param bool
  156. * @param mixed
  157. * @return string
  158. */
  159. function img($src = '', $index_page = FALSE, $attributes = '')
  160. {
  161. if ( ! is_array($src) )
  162. {
  163. $src = array('src' => $src);
  164. }
  165. // If there is no alt attribute defined, set it to an empty string
  166. if ( ! isset($src['alt']))
  167. {
  168. $src['alt'] = '';
  169. }
  170. $img = '<img';
  171. foreach ($src as $k => $v)
  172. {
  173. if ($k === 'src' && ! preg_match('#^(data:[a-z,;])|(([a-z]+:)?(?<!data:)//)#i', $v))
  174. {
  175. if ($index_page === TRUE)
  176. {
  177. $img .= ' src="'.get_instance()->config->site_url($v).'"';
  178. }
  179. else
  180. {
  181. $img .= ' src="'.get_instance()->config->base_url($v).'"';
  182. }
  183. }
  184. else
  185. {
  186. $img .= ' '.$k.'="'.$v.'"';
  187. }
  188. }
  189. return $img._stringify_attributes($attributes).' />';
  190. }
  191. }
  192. // ------------------------------------------------------------------------
  193. if ( ! function_exists('doctype'))
  194. {
  195. /**
  196. * Doctype
  197. *
  198. * Generates a page document type declaration
  199. *
  200. * Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans,
  201. * xhtml-frame, html4-strict, html4-trans, and html4-frame.
  202. * All values are saved in the doctypes config file.
  203. *
  204. * @param string type The doctype to be generated
  205. * @return string
  206. */
  207. function doctype($type = 'xhtml1-strict')
  208. {
  209. static $doctypes;
  210. if ( ! is_array($doctypes))
  211. {
  212. if (file_exists(APPPATH.'config/doctypes.php'))
  213. {
  214. include(APPPATH.'config/doctypes.php');
  215. }
  216. if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
  217. {
  218. include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
  219. }
  220. if (empty($_doctypes) OR ! is_array($_doctypes))
  221. {
  222. $doctypes = array();
  223. return FALSE;
  224. }
  225. $doctypes = $_doctypes;
  226. }
  227. return isset($doctypes[$type]) ? $doctypes[$type] : FALSE;
  228. }
  229. }
  230. // ------------------------------------------------------------------------
  231. if ( ! function_exists('link_tag'))
  232. {
  233. /**
  234. * Link
  235. *
  236. * Generates link to a CSS file
  237. *
  238. * @param mixed stylesheet hrefs or an array
  239. * @param string rel
  240. * @param string type
  241. * @param string title
  242. * @param string media
  243. * @param bool should index_page be added to the css path
  244. * @return string
  245. */
  246. function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
  247. {
  248. $CI =& get_instance();
  249. $link = '<link ';
  250. if (is_array($href))
  251. {
  252. foreach ($href as $k => $v)
  253. {
  254. if ($k === 'href' && ! preg_match('#^([a-z]+:)?//#i', $v))
  255. {
  256. if ($index_page === TRUE)
  257. {
  258. $link .= 'href="'.$CI->config->site_url($v).'" ';
  259. }
  260. else
  261. {
  262. $link .= 'href="'.$CI->config->base_url($v).'" ';
  263. }
  264. }
  265. else
  266. {
  267. $link .= $k.'="'.$v.'" ';
  268. }
  269. }
  270. }
  271. else
  272. {
  273. if (preg_match('#^([a-z]+:)?//#i', $href))
  274. {
  275. $link .= 'href="'.$href.'" ';
  276. }
  277. elseif ($index_page === TRUE)
  278. {
  279. $link .= 'href="'.$CI->config->site_url($href).'" ';
  280. }
  281. else
  282. {
  283. $link .= 'href="'.$CI->config->base_url($href).'" ';
  284. }
  285. $link .= 'rel="'.$rel.'" type="'.$type.'" ';
  286. if ($media !== '')
  287. {
  288. $link .= 'media="'.$media.'" ';
  289. }
  290. if ($title !== '')
  291. {
  292. $link .= 'title="'.$title.'" ';
  293. }
  294. }
  295. return $link."/>\n";
  296. }
  297. }
  298. // ------------------------------------------------------------------------
  299. if ( ! function_exists('meta'))
  300. {
  301. /**
  302. * Generates meta tags from an array of key/values
  303. *
  304. * @param array
  305. * @param string
  306. * @param string
  307. * @param string
  308. * @return string
  309. */
  310. function meta($name = '', $content = '', $type = 'name', $newline = "\n")
  311. {
  312. // Since we allow the data to be passes as a string, a simple array
  313. // or a multidimensional one, we need to do a little prepping.
  314. if ( ! is_array($name))
  315. {
  316. $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
  317. }
  318. elseif (isset($name['name']))
  319. {
  320. // Turn single array into multidimensional
  321. $name = array($name);
  322. }
  323. $str = '';
  324. foreach ($name as $meta)
  325. {
  326. $type = (isset($meta['type']) && $meta['type'] !== 'name') ? 'http-equiv' : 'name';
  327. $name = isset($meta['name']) ? $meta['name'] : '';
  328. $content = isset($meta['content']) ? $meta['content'] : '';
  329. $newline = isset($meta['newline']) ? $meta['newline'] : "\n";
  330. $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
  331. }
  332. return $str;
  333. }
  334. }
  335. // ------------------------------------------------------------------------
  336. if ( ! function_exists('br'))
  337. {
  338. /**
  339. * Generates HTML BR tags based on number supplied
  340. *
  341. * @deprecated 3.0.0 Use str_repeat() instead
  342. * @param int $count Number of times to repeat the tag
  343. * @return string
  344. */
  345. function br($count = 1)
  346. {
  347. return str_repeat('<br />', $count);
  348. }
  349. }
  350. // ------------------------------------------------------------------------
  351. if ( ! function_exists('nbs'))
  352. {
  353. /**
  354. * Generates non-breaking space entities based on number supplied
  355. *
  356. * @deprecated 3.0.0 Use str_repeat() instead
  357. * @param int
  358. * @return string
  359. */
  360. function nbs($num = 1)
  361. {
  362. return str_repeat('&nbsp;', $num);
  363. }
  364. }