html.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * html.php
  4. *
  5. * The idea is to inlcude here some functions to make easier
  6. * the right to left implementation by "functionize" some
  7. * html outputs.
  8. *
  9. * @copyright 1999-2025 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id$
  12. * @package squirrelmail
  13. * @since 1.3.0
  14. */
  15. /**
  16. * Generates a hyperlink
  17. *
  18. * @param string $uri The target link location
  19. * @param string $text The link text
  20. * @param string $target The location where the link should
  21. * be opened (OPTIONAL; default not used)
  22. * @param string $onclick The onClick JavaScript handler (OPTIONAL;
  23. * default not used)
  24. * @param string $class The CSS class name (OPTIONAL; default
  25. * not used)
  26. * @param string $id The ID name (OPTIONAL; default not used)
  27. * @param string $name The anchor name (OPTIONAL; default not used)
  28. * @param array $aAttribs Any extra attributes: this must be an
  29. * associative array, where keys will be
  30. * added as the attribute name, and values
  31. * (which are optional - should be null if
  32. * none should be used) will be placed in
  33. * double quotes (pending template implementation)
  34. * as the attribute value (OPTIONAL; default empty).
  35. *
  36. * @return string The desired hyperlink tag.
  37. *
  38. * @since 1.5.2
  39. *
  40. */
  41. function create_hyperlink($uri, $text, $target='', $onclick='',
  42. $class='', $id='', $name='', $aAttribs=array()) {
  43. global $oTemplate;
  44. $oTemplate->assign('uri', $uri);
  45. $oTemplate->assign('text', $text);
  46. $oTemplate->assign('target', $target);
  47. $oTemplate->assign('onclick', $onclick);
  48. $oTemplate->assign('class', $class);
  49. $oTemplate->assign('id', $id);
  50. $oTemplate->assign('name', $name);
  51. $oTemplate->assign('aAttribs', $aAttribs);
  52. return $oTemplate->fetch('hyperlink.tpl');
  53. }
  54. /**
  55. * Generates an image tag
  56. *
  57. * @param string $src The image source path
  58. * @param string $alt Alternate link text (OPTIONAL; default
  59. * not used)
  60. * @param string $width The width the image should be shown in
  61. * (OPTIONAL; default not used)
  62. * @param string $height The height the image should be shown in
  63. * (OPTIONAL; default not used)
  64. * @param string $border The image's border attribute value
  65. * (OPTIONAL; default not used)
  66. * @param string $class The CSS class name (OPTIONAL; default
  67. * not used)
  68. * @param string $id The ID name (OPTIONAL; default not used)
  69. * @param string $onclick The onClick JavaScript handler (OPTIONAL;
  70. * default not used)
  71. * @param string $title The image's title attribute value
  72. * (OPTIONAL; default not used)
  73. * @param string $align The image's alignment attribute value
  74. * (OPTIONAL; default not used)
  75. * @param string $hspace The image's hspace attribute value
  76. * (OPTIONAL; default not used)
  77. * @param string $vspace The image's vspace attribute value
  78. * (OPTIONAL; default not used)
  79. * @param string $text_alternative A text replacement for the entire
  80. * image tag, to be used at the
  81. * discretion of the template set,
  82. * if for some reason the image tag
  83. * cannot or should not be produced
  84. * (OPTIONAL; default not used)
  85. * @param array $aAttribs Any extra attributes: this must be an
  86. * associative array, where keys will be
  87. * added as the attribute name, and values
  88. * (which are optional - should be null if
  89. * none should be used) will be placed in
  90. * double quotes (pending template implementation)
  91. * as the attribute value (OPTIONAL; default empty).
  92. *
  93. * @return string The desired hyperlink tag.
  94. *
  95. * @since 1.5.2
  96. *
  97. */
  98. function create_image($src, $alt='', $width='', $height='',
  99. $border='', $class='', $id='', $onclick='',
  100. $title='', $align='', $hspace='', $vspace='',
  101. $text_alternative='', $aAttribs=array()) {
  102. global $oTemplate;
  103. $oTemplate->assign('src', $src);
  104. $oTemplate->assign('alt', $alt);
  105. $oTemplate->assign('width', $width);
  106. $oTemplate->assign('height', $height);
  107. $oTemplate->assign('border', $border);
  108. $oTemplate->assign('class', $class);
  109. $oTemplate->assign('id', $id);
  110. $oTemplate->assign('onclick', $onclick);
  111. $oTemplate->assign('title', $title);
  112. $oTemplate->assign('align', $align);
  113. $oTemplate->assign('hspace', $hspace);
  114. $oTemplate->assign('vspace', $vspace);
  115. $oTemplate->assign('text_alternative', $text_alternative);
  116. $oTemplate->assign('aAttribs', $aAttribs);
  117. return $oTemplate->fetch('image.tpl');
  118. }
  119. /**
  120. * Generates a label tag
  121. *
  122. * @param string $value The contents that belong inside the label
  123. * @param string $for The ID to which the label applies (OPTIONAL;
  124. * default not used)
  125. * @param array $aAttribs Any extra attributes: this must be an
  126. * associative array, where keys will be
  127. * added as the attribute name, and values
  128. * (which are optional - should be null if
  129. * none should be used) will be placed in
  130. * double quotes (pending template implementation)
  131. * as the attribute value (OPTIONAL; default empty).
  132. *
  133. * @return string The desired label tag.
  134. *
  135. * @since 1.5.2
  136. *
  137. */
  138. function create_label($value, $for='', $aAttribs=array()) {
  139. global $oTemplate;
  140. $oTemplate->assign('text', $value);
  141. $oTemplate->assign('for', $for);
  142. $oTemplate->assign('aAttribs', $aAttribs);
  143. return $oTemplate->fetch('label.tpl');
  144. }
  145. /**
  146. * Generates a span tag
  147. *
  148. * @param string $value The contents that belong inside the span
  149. * @param string $class The CSS class name (OPTIONAL; default
  150. * not used)
  151. * @param string $id The ID name (OPTIONAL; default not used)
  152. * @param array $aAttribs Any extra attributes: this must be an
  153. * associative array, where keys will be
  154. * added as the attribute name, and values
  155. * (which are optional - should be null if
  156. * none should be used) will be placed in
  157. * double quotes (pending template implementation)
  158. * as the attribute value (OPTIONAL; default empty).
  159. *
  160. * @return string The desired span tag.
  161. *
  162. * @since 1.5.2
  163. *
  164. */
  165. function create_span($value, $class='', $id='', $aAttribs=array()) {
  166. global $oTemplate;
  167. $oTemplate->assign('value', $value);
  168. $oTemplate->assign('class', $class);
  169. $oTemplate->assign('id', $id);
  170. $oTemplate->assign('aAttribs', $aAttribs);
  171. return $oTemplate->fetch('span.tpl');
  172. }
  173. /**
  174. * Generates an opening body tag
  175. *
  176. * @param string $onload Body onload JavaScript handler code
  177. * (OPTIONAL; default not used)
  178. * @param string $class The CSS class name (OPTIONAL; default
  179. * not used)
  180. * @param array $aAttribs Any extra attributes: this must be an
  181. * associative array, where keys will be
  182. * added as the attribute name, and values
  183. * (which are optional - should be null if
  184. * none should be used) will be placed in
  185. * double quotes (pending template implementation)
  186. * as the attribute value (OPTIONAL; default empty).
  187. *
  188. * @return string The desired body tag.
  189. *
  190. * @since 1.5.2
  191. *
  192. */
  193. function create_body($onload='', $class='', $aAttribs=array()) {
  194. global $oTemplate;
  195. $oTemplate->assign('onload', $onload);
  196. $oTemplate->assign('class', $class);
  197. $oTemplate->assign('aAttribs', $aAttribs);
  198. return $oTemplate->fetch('body.tpl');
  199. }
  200. /**
  201. * Generates html tags
  202. //FIXME: This should not be used anywhere in the core, or we should convert this to use templates. We should not be assuming HTML output.
  203. *
  204. * @param string $tag Tag to output
  205. * @param string $val Value between tags
  206. * @param string $align Alignment (left, center, etc)
  207. * @param string $bgcolor Back color in hexadecimal
  208. * @param string $xtra Extra options
  209. * @return string HTML ready for output
  210. * @since 1.3.0
  211. */
  212. function html_tag( $tag, // Tag to output
  213. $val = '', // Value between tags
  214. $align = '', // Alignment
  215. $bgcolor = '', // Back color
  216. $xtra = '' ) { // Extra options
  217. GLOBAL $languages, $squirrelmail_language;
  218. $align = strtolower( $align );
  219. $bgc = '';
  220. $tag = strtolower( $tag );
  221. if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
  222. $dir = $languages[$squirrelmail_language]['DIR'];
  223. } else {
  224. $dir = 'ltr';
  225. }
  226. if ( $dir == 'ltr' ) {
  227. $rgt = 'right';
  228. $lft = 'left';
  229. } else {
  230. $rgt = 'left';
  231. $lft = 'right';
  232. }
  233. if ( $bgcolor <> '' ) {
  234. $bgc = " bgcolor=\"$bgcolor\"";
  235. }
  236. switch ( $align ) {
  237. case '':
  238. $alg = '';
  239. break;
  240. case 'right':
  241. $alg = " align=\"$rgt\"";
  242. break;
  243. case 'left':
  244. $alg = " align=\"$lft\"";
  245. break;
  246. default:
  247. $alg = " align=\"$align\"";
  248. break;
  249. }
  250. $ret = "<$tag";
  251. if ( $dir <> 'ltr' ) {
  252. $ret .= " dir=\"$dir\"";
  253. }
  254. $ret .= $bgc . $alg;
  255. if ( $xtra <> '' ) {
  256. $ret .= " $xtra";
  257. }
  258. if ( $val <> '' ) {
  259. $ret .= ">$val</$tag>\n";
  260. } else {
  261. $ret .= '>'. "\n";
  262. }
  263. return( $ret );
  264. }
  265. /**
  266. * This function is used to add, modify or delete more than
  267. * one GET variable at a time in a URL. This simply takes
  268. * an array of variables (key/value pairs) and passes them
  269. * one at a time to {@link set_url_var}.
  270. *
  271. * Note that the value for any one of the variables may be
  272. * an array, and it will be handled properly.
  273. *
  274. * As with set_url_var, any of the variable values may be
  275. * set to NULL to remove it from the URI.
  276. *
  277. * Also, to ensure compatibility with older versions, use
  278. * $val='0' to set $var to 0.
  279. *
  280. * @param string $uri URI that must be modified
  281. * @param array $values List of GET variable names and their values
  282. * @param boolean $sanitize Controls sanitizing of ampersand in URIs
  283. *
  284. * @return string The modified URI
  285. *
  286. * @since 1.5.2
  287. *
  288. */
  289. function set_uri_vars($uri, $values, $sanitize=TRUE) {
  290. foreach ($values as $key => $value)
  291. if (is_array($value)) {
  292. $i = 0;
  293. foreach ($value as $val)
  294. $uri = set_url_var($uri, $key . '[' . $i++ . ']', $val, $sanitize);
  295. }
  296. else
  297. $uri = set_url_var($uri, $key, $value, $sanitize);
  298. return $uri;
  299. }
  300. /**
  301. * This function is used to add, modify or delete GET variables in a URL.
  302. * It is especially useful when $url = $PHP_SELF
  303. *
  304. * Set $val to NULL to remove $var from $url.
  305. * To ensure compatibility with older versions, use $val='0' to set $var to 0.
  306. *
  307. * @param string $url url that must be modified
  308. * @param string $var GET variable name
  309. * @param string $val variable value (CANNOT be an array)
  310. * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
  311. * @param boolean $treat_as_array When TRUE, if $var is an array (it occurs one
  312. * or more times with square brackets after it,
  313. * e.g. "var[1]"), the whole array will be removed
  314. * (when $val is NULL) or the given value will be
  315. * added to the next array slot (@since 1.4.23/1.5.2)
  316. *
  317. * @return string $url modified url
  318. *
  319. * @since 1.3.0
  320. *
  321. */
  322. function set_url_var($url, $var, $val=null, $link=true, $treat_as_array=false) {
  323. $url = str_replace('&amp;','&',$url);
  324. if (strpos($url, '?') === false) {
  325. $url .= '?';
  326. }
  327. list($uri, $params) = explode('?', $url, 2);
  328. $newpar = array();
  329. $params = explode('&', $params);
  330. $array_names = array();
  331. foreach ($params as $p) {
  332. if (trim($p)) {
  333. $p = explode('=', $p);
  334. $newpar[$p[0]] = (isset($p[1]) ? $p[1] : '');
  335. if ($treat_as_array && preg_match('/(.*)\[(\d+)]$/', $p[0], $matches)) {
  336. if (!isset($array_names[$matches[1]])) $array_names[$matches[1]] = array();
  337. $array_names[$matches[1]][$matches[2]] = $p[1];
  338. }
  339. }
  340. }
  341. if (is_null($val)) {
  342. if ($treat_as_array && !empty($array_names[$var])) {
  343. foreach ($array_names[$var] as $key => $ignore)
  344. unset($newpar[$var . '[' . $key . ']']);
  345. } else {
  346. unset($newpar[$var]);
  347. }
  348. } else {
  349. if ($treat_as_array && !empty($array_names[$var])) {
  350. $max_key = 0;
  351. foreach ($array_names[$var] as $key => $ignore)
  352. if ($key >= $max_key) $max_key = $key + 1;
  353. $newpar[$var . '[' . $max_key . ']'] = $val;
  354. } else {
  355. $newpar[$var] = $val;
  356. }
  357. }
  358. if (!count($newpar)) {
  359. return $uri;
  360. }
  361. $url = $uri . '?';
  362. foreach ($newpar as $name => $value) {
  363. $url .= "$name=$value&";
  364. }
  365. $url = substr($url, 0, -1);
  366. if ($link) {
  367. $url = str_replace('&','&amp;',$url);
  368. }
  369. return $url;
  370. }