/tags/rel-1_4_18/functions/html.php

# · PHP · 185 lines · 115 code · 26 blank · 44 comment · 24 complexity · 015a87d79c6038895f1a67ef92ca2554 MD5 · raw file

  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 &copy; 1999-2009 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id: html.php 13549 2009-04-15 22:00:49Z jervfors $
  12. * @package squirrelmail
  13. * @since 1.3.0
  14. */
  15. /**
  16. * Generate html tags
  17. *
  18. * @param string $tag Tag to output
  19. * @param string $val Value between tags
  20. * @param string $align Alignment (left, center, etc)
  21. * @param string $bgcolor Back color in hexadecimal
  22. * @param string $xtra Extra options
  23. * @return string HTML ready for output
  24. */
  25. function html_tag( $tag, // Tag to output
  26. $val = '', // Value between tags
  27. $align = '', // Alignment
  28. $bgcolor = '', // Back color
  29. $xtra = '' ) { // Extra options
  30. GLOBAL $languages, $squirrelmail_language;
  31. $align = strtolower( $align );
  32. $bgc = '';
  33. $tag = strtolower( $tag );
  34. if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
  35. $dir = $languages[$squirrelmail_language]['DIR'];
  36. } else {
  37. $dir = 'ltr';
  38. }
  39. if ( $dir == 'ltr' ) {
  40. $rgt = 'right';
  41. $lft = 'left';
  42. } else {
  43. $rgt = 'left';
  44. $lft = 'right';
  45. }
  46. if ( $bgcolor <> '' ) {
  47. $bgc = " bgcolor=\"$bgcolor\"";
  48. }
  49. switch ( $align ) {
  50. case '':
  51. $alg = '';
  52. break;
  53. case 'right':
  54. $alg = " align=\"$rgt\"";
  55. break;
  56. case 'left':
  57. $alg = " align=\"$lft\"";
  58. break;
  59. default:
  60. $alg = " align=\"$align\"";
  61. break;
  62. }
  63. $ret = "<$tag";
  64. if ( $dir <> 'ltr' ) {
  65. $ret .= " dir=\"$dir\"";
  66. }
  67. $ret .= $bgc . $alg;
  68. if ( $xtra <> '' ) {
  69. $ret .= " $xtra";
  70. }
  71. if ( $val <> '' ) {
  72. $ret .= ">$val</$tag>\n";
  73. } else {
  74. $ret .= '>' . "\n";
  75. }
  76. return( $ret );
  77. }
  78. /**
  79. * This function is used to add, modify or delete GET variables in a URL.
  80. * It is especially useful when $url = $PHP_SELF
  81. *
  82. * Set $val to NULL to remove $var from $url.
  83. * To ensure compatibility with older versions, use $val='0' to set $var to 0.
  84. *
  85. * @param string $url url that must be modified
  86. * @param string $var GET variable name
  87. * @param string $val variable value
  88. * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
  89. *
  90. * @return string $url modified url
  91. *
  92. * @since 1.3.0
  93. *
  94. */
  95. function set_url_var($url, $var, $val=null, $link=true) {
  96. $url = str_replace('&amp;','&',$url);
  97. if (strpos($url, '?') === false) {
  98. $url .= '?';
  99. }
  100. list($uri, $params) = explode('?', $url, 2);
  101. $newpar = array();
  102. $params = explode('&', $params);
  103. foreach ($params as $p) {
  104. if (trim($p)) {
  105. $p = explode('=', $p);
  106. $newpar[$p[0]] = (isset($p[1]) ? $p[1] : '');
  107. }
  108. }
  109. if (is_null($val)) {
  110. unset($newpar[$var]);
  111. } else {
  112. $newpar[$var] = $val;
  113. }
  114. if (!count($newpar)) {
  115. return $uri;
  116. }
  117. $url = $uri . '?';
  118. foreach ($newpar as $name => $value) {
  119. $url .= "$name=$value&";
  120. }
  121. $url = substr($url, 0, -1);
  122. if ($link) {
  123. $url = str_replace('&','&amp;',$url);
  124. }
  125. return $url;
  126. }
  127. /* Temporary test function to proces template vars with formatting.
  128. * I use it for viewing the message_header (view_header.php) with
  129. * a sort of template.
  130. */
  131. function echo_template_var($var, $format_ar = array() ) {
  132. $frm_last = count($format_ar) -1;
  133. if (isset($format_ar[0])) echo $format_ar[0];
  134. $i = 1;
  135. switch (true) {
  136. case (is_string($var)):
  137. echo $var;
  138. break;
  139. case (is_array($var)):
  140. $frm_a = array_slice($format_ar,1,$frm_last-1);
  141. foreach ($var as $a_el) {
  142. if (is_array($a_el)) {
  143. echo_template_var($a_el,$frm_a);
  144. } else {
  145. echo $a_el;
  146. if (isset($format_ar[$i])) {
  147. echo $format_ar[$i];
  148. }
  149. $i++;
  150. }
  151. }
  152. break;
  153. default:
  154. break;
  155. }
  156. if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
  157. echo $format_ar[$frm_last];
  158. }
  159. }