html.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. * @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 span tag
  121. *
  122. * @param string $value The contents that belong inside the span
  123. * @param string $class The CSS class name (OPTIONAL; default
  124. * not used)
  125. * @param string $id The ID name (OPTIONAL; default not used)
  126. * @param array $aAttribs Any extra attributes: this must be an
  127. * associative array, where keys will be
  128. * added as the attribute name, and values
  129. * (which are optional - should be null if
  130. * none should be used) will be placed in
  131. * double quotes (pending template implementation)
  132. * as the attribute value (OPTIONAL; default empty).
  133. *
  134. * @return string The desired span tag.
  135. *
  136. * @since 1.5.2
  137. *
  138. */
  139. function create_span($value, $class='', $id='', $aAttribs=array()) {
  140. global $oTemplate;
  141. $oTemplate->assign('value', $value);
  142. $oTemplate->assign('class', $class);
  143. $oTemplate->assign('id', $id);
  144. $oTemplate->assign('aAttribs', $aAttribs);
  145. return $oTemplate->fetch('span.tpl');
  146. }
  147. /**
  148. * Generates html tags
  149. //FIXME: this should not be used anywhere in the core, or we should
  150. // convert this to use templates. We sould not be assuming HTML output.
  151. *
  152. * @param string $tag Tag to output
  153. * @param string $val Value between tags
  154. * @param string $align Alignment (left, center, etc)
  155. * @param string $bgcolor Back color in hexadecimal
  156. * @param string $xtra Extra options
  157. * @return string HTML ready for output
  158. * @since 1.3.0
  159. */
  160. function html_tag( $tag, // Tag to output
  161. $val = '', // Value between tags
  162. $align = '', // Alignment
  163. $bgcolor = '', // Back color
  164. $xtra = '' ) { // Extra options
  165. GLOBAL $languages, $squirrelmail_language;
  166. $align = strtolower( $align );
  167. $bgc = '';
  168. $tag = strtolower( $tag );
  169. if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
  170. $dir = $languages[$squirrelmail_language]['DIR'];
  171. } else {
  172. $dir = 'ltr';
  173. }
  174. if ( $dir == 'ltr' ) {
  175. $rgt = 'right';
  176. $lft = 'left';
  177. } else {
  178. $rgt = 'left';
  179. $lft = 'right';
  180. }
  181. if ( $bgcolor <> '' ) {
  182. $bgc = " bgcolor=\"$bgcolor\"";
  183. }
  184. switch ( $align ) {
  185. case '':
  186. $alg = '';
  187. break;
  188. case 'right':
  189. $alg = " align=\"$rgt\"";
  190. break;
  191. case 'left':
  192. $alg = " align=\"$lft\"";
  193. break;
  194. default:
  195. $alg = " align=\"$align\"";
  196. break;
  197. }
  198. $ret = "<$tag";
  199. if ( $dir <> 'ltr' ) {
  200. $ret .= " dir=\"$dir\"";
  201. }
  202. $ret .= $bgc . $alg;
  203. if ( $xtra <> '' ) {
  204. $ret .= " $xtra";
  205. }
  206. if ( $val <> '' ) {
  207. $ret .= ">$val</$tag>\n";
  208. } else {
  209. $ret .= '>'. "\n";
  210. }
  211. return( $ret );
  212. }
  213. /**
  214. * handy function to set url vars
  215. *
  216. * especially useful when $url = $PHP_SELF
  217. * @param string $url url that must be modified
  218. * @param string $var variable name
  219. * @param string $val variable value
  220. * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
  221. * @return string $url modified url
  222. * @since 1.3.0
  223. */
  224. function set_url_var($url, $var, $val=0, $link=true) {
  225. $k = '';
  226. $pat_a = array (
  227. '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
  228. '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
  229. '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
  230. '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
  231. );
  232. $url = str_replace('&amp;','&',$url);
  233. // FIXME: why switch is used instead of if () or one preg_match()
  234. switch (true) {
  235. case (preg_match($pat_a[0],$url,$regs)):
  236. $k = $regs[1];
  237. $v = $regs[2];
  238. break;
  239. case (preg_match($pat_a[1],$url,$regs)):
  240. $k = $regs[1];
  241. $v = $regs[2];
  242. break;
  243. case (preg_match($pat_a[2],$url,$regs)):
  244. $k = $regs[1];
  245. $v = $regs[2];
  246. break;
  247. case (preg_match($pat_a[3],$url,$regs)):
  248. $k = $regs[1];
  249. $v = $regs[2];
  250. break;
  251. default:
  252. if ($val) {
  253. if (strpos($url,'?')) {
  254. $url .= "&$var=$val";
  255. } else {
  256. $url .= "?$var=$val";
  257. }
  258. }
  259. break;
  260. }
  261. if ($k) {
  262. if ($val) {
  263. $rpl = "$k=$val";
  264. } else {
  265. $rpl = '';
  266. }
  267. if( substr($v,-1)=='&' ) {
  268. $rpl .= '&';
  269. }
  270. $pat = "/$k=$v/";
  271. $url = preg_replace($pat,$rpl,$url);
  272. }
  273. if ($link) {
  274. $url = str_replace('&','&amp;',$url);
  275. }
  276. return $url;
  277. }