html.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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-2006 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. *
  28. * @return string The desired hyperlink tag.
  29. *
  30. * @since 1.5.2
  31. *
  32. */
  33. function create_hyperlink($uri, $text, $target='', $onclick='', $class='', $id='') {
  34. global $oTemplate;
  35. $oTemplate->assign('uri', $uri);
  36. $oTemplate->assign('text', $text);
  37. $oTemplate->assign('target', $target);
  38. $oTemplate->assign('onclick', $onclick);
  39. $oTemplate->assign('class', $class);
  40. $oTemplate->assign('id', $id);
  41. return $oTemplate->fetch('hyperlink.tpl');
  42. }
  43. /**
  44. * Generates an image tag
  45. *
  46. * @param string $src The image source path
  47. * @param string $alt Alternate link text (OPTIONAL; default
  48. * not used)
  49. * @param string $width The width the image should be shown in
  50. * (OPTIONAL; default not used)
  51. * @param string $height The height the image should be shown in
  52. * (OPTIONAL; default not used)
  53. * @param string $border The image's border attribute value
  54. * (OPTIONAL; default not used)
  55. * @param string $class The CSS class name (OPTIONAL; default
  56. * not used)
  57. * @param string $id The ID name (OPTIONAL; default not used)
  58. * @param string $onclick The onClick JavaScript handler (OPTIONAL;
  59. * default not used)
  60. * @param string $title The image's title attribute value
  61. * (OPTIONAL; default not used)
  62. * @param string $align The image's alignment attribute value
  63. * (OPTIONAL; default not used)
  64. * @param string $hspace The image's hspace attribute value
  65. * (OPTIONAL; default not used)
  66. * @param string $vspace The image's vspace attribute value
  67. * (OPTIONAL; default not used)
  68. * @param string $text_alternative A text replacement for the entire
  69. * image tag, to be used at the
  70. * discretion of the template set,
  71. * if for some reason the image tag
  72. * cannot or should not be produced
  73. * (OPTIONAL; default not used)
  74. *
  75. * @return string The desired hyperlink tag.
  76. *
  77. * @since 1.5.2
  78. *
  79. */
  80. function create_image($src, $alt='', $width='', $height='',
  81. $border='', $class='', $id='', $onclick='',
  82. $title='', $align='', $hspace='', $vspace='',
  83. $text_alternative='') {
  84. global $oTemplate;
  85. $oTemplate->assign('src', $src);
  86. $oTemplate->assign('alt', $alt);
  87. $oTemplate->assign('width', $width);
  88. $oTemplate->assign('height', $height);
  89. $oTemplate->assign('border', $border);
  90. $oTemplate->assign('class', $class);
  91. $oTemplate->assign('id', $id);
  92. $oTemplate->assign('onclick', $onclick);
  93. $oTemplate->assign('title', $title);
  94. $oTemplate->assign('align', $align);
  95. $oTemplate->assign('hspace', $hspace);
  96. $oTemplate->assign('vspace', $vspace);
  97. $oTemplate->assign('text_alternative', $text_alternative);
  98. return $oTemplate->fetch('image.tpl');
  99. }
  100. /**
  101. * Generates html tags
  102. *
  103. * @param string $tag Tag to output
  104. * @param string $val Value between tags
  105. * @param string $align Alignment (left, center, etc)
  106. * @param string $bgcolor Back color in hexadecimal
  107. * @param string $xtra Extra options
  108. * @return string HTML ready for output
  109. * @since 1.3.0
  110. */
  111. function html_tag( $tag, // Tag to output
  112. $val = '', // Value between tags
  113. $align = '', // Alignment
  114. $bgcolor = '', // Back color
  115. $xtra = '' ) { // Extra options
  116. GLOBAL $languages, $squirrelmail_language;
  117. $align = strtolower( $align );
  118. $bgc = '';
  119. $tag = strtolower( $tag );
  120. if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
  121. $dir = $languages[$squirrelmail_language]['DIR'];
  122. } else {
  123. $dir = 'ltr';
  124. }
  125. if ( $dir == 'ltr' ) {
  126. $rgt = 'right';
  127. $lft = 'left';
  128. } else {
  129. $rgt = 'left';
  130. $lft = 'right';
  131. }
  132. if ( $bgcolor <> '' ) {
  133. $bgc = " bgcolor=\"$bgcolor\"";
  134. }
  135. switch ( $align ) {
  136. case '':
  137. $alg = '';
  138. break;
  139. case 'right':
  140. $alg = " align=\"$rgt\"";
  141. break;
  142. case 'left':
  143. $alg = " align=\"$lft\"";
  144. break;
  145. default:
  146. $alg = " align=\"$align\"";
  147. break;
  148. }
  149. $ret = "<$tag";
  150. if ( $dir <> 'ltr' ) {
  151. $ret .= " dir=\"$dir\"";
  152. }
  153. $ret .= $bgc . $alg;
  154. if ( $xtra <> '' ) {
  155. $ret .= " $xtra";
  156. }
  157. if ( $val <> '' ) {
  158. $ret .= ">$val</$tag>\n";
  159. } else {
  160. $ret .= '>'. "\n";
  161. }
  162. return( $ret );
  163. }
  164. /**
  165. * handy function to set url vars
  166. *
  167. * especially useful when $url = $PHP_SELF
  168. * @param string $url url that must be modified
  169. * @param string $var variable name
  170. * @param string $val variable value
  171. * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
  172. * @return string $url modified url
  173. * @since 1.3.0
  174. */
  175. function set_url_var($url, $var, $val=0, $link=true) {
  176. $k = '';
  177. $pat_a = array (
  178. '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
  179. '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
  180. '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
  181. '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
  182. );
  183. $url = str_replace('&amp;','&',$url);
  184. // FIXME: why switch is used instead of if () or one preg_match()
  185. switch (true) {
  186. case (preg_match($pat_a[0],$url,$regs)):
  187. $k = $regs[1];
  188. $v = $regs[2];
  189. break;
  190. case (preg_match($pat_a[1],$url,$regs)):
  191. $k = $regs[1];
  192. $v = $regs[2];
  193. break;
  194. case (preg_match($pat_a[2],$url,$regs)):
  195. $k = $regs[1];
  196. $v = $regs[2];
  197. break;
  198. case (preg_match($pat_a[3],$url,$regs)):
  199. $k = $regs[1];
  200. $v = $regs[2];
  201. break;
  202. default:
  203. if ($val) {
  204. if (strpos($url,'?')) {
  205. $url .= "&$var=$val";
  206. } else {
  207. $url .= "?$var=$val";
  208. }
  209. }
  210. break;
  211. }
  212. if ($k) {
  213. if ($val) {
  214. $rpl = "$k=$val";
  215. } else {
  216. $rpl = '';
  217. }
  218. if( substr($v,-1)=='&' ) {
  219. $rpl .= '&';
  220. }
  221. $pat = "/$k=$v/";
  222. $url = preg_replace($pat,$rpl,$url);
  223. }
  224. if ($link) {
  225. $url = str_replace('&','&amp;',$url);
  226. }
  227. return $url;
  228. }