html.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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-2007 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 html tags
  175. //FIXME: this should not be used anywhere in the core, or we should
  176. // convert this to use templates. We sould not be assuming HTML output.
  177. *
  178. * @param string $tag Tag to output
  179. * @param string $val Value between tags
  180. * @param string $align Alignment (left, center, etc)
  181. * @param string $bgcolor Back color in hexadecimal
  182. * @param string $xtra Extra options
  183. * @return string HTML ready for output
  184. * @since 1.3.0
  185. */
  186. function html_tag( $tag, // Tag to output
  187. $val = '', // Value between tags
  188. $align = '', // Alignment
  189. $bgcolor = '', // Back color
  190. $xtra = '' ) { // Extra options
  191. GLOBAL $languages, $squirrelmail_language;
  192. $align = strtolower( $align );
  193. $bgc = '';
  194. $tag = strtolower( $tag );
  195. if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
  196. $dir = $languages[$squirrelmail_language]['DIR'];
  197. } else {
  198. $dir = 'ltr';
  199. }
  200. if ( $dir == 'ltr' ) {
  201. $rgt = 'right';
  202. $lft = 'left';
  203. } else {
  204. $rgt = 'left';
  205. $lft = 'right';
  206. }
  207. if ( $bgcolor <> '' ) {
  208. $bgc = " bgcolor=\"$bgcolor\"";
  209. }
  210. switch ( $align ) {
  211. case '':
  212. $alg = '';
  213. break;
  214. case 'right':
  215. $alg = " align=\"$rgt\"";
  216. break;
  217. case 'left':
  218. $alg = " align=\"$lft\"";
  219. break;
  220. default:
  221. $alg = " align=\"$align\"";
  222. break;
  223. }
  224. $ret = "<$tag";
  225. if ( $dir <> 'ltr' ) {
  226. $ret .= " dir=\"$dir\"";
  227. }
  228. $ret .= $bgc . $alg;
  229. if ( $xtra <> '' ) {
  230. $ret .= " $xtra";
  231. }
  232. if ( $val <> '' ) {
  233. $ret .= ">$val</$tag>\n";
  234. } else {
  235. $ret .= '>'. "\n";
  236. }
  237. return( $ret );
  238. }
  239. /**
  240. * handy function to set url vars
  241. *
  242. * especially useful when $url = $PHP_SELF
  243. * @param string $url url that must be modified
  244. * @param string $var variable name
  245. * @param string $val variable value
  246. * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
  247. * @return string $url modified url
  248. * @since 1.3.0
  249. */
  250. function set_url_var($url, $var, $val=0, $link=true) {
  251. $k = '';
  252. $pat_a = array (
  253. '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
  254. '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
  255. '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
  256. '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
  257. );
  258. $url = str_replace('&amp;','&',$url);
  259. // FIXME: why switch is used instead of if () or one preg_match()
  260. switch (true) {
  261. case (preg_match($pat_a[0],$url,$regs)):
  262. $k = $regs[1];
  263. $v = $regs[2];
  264. break;
  265. case (preg_match($pat_a[1],$url,$regs)):
  266. $k = $regs[1];
  267. $v = $regs[2];
  268. break;
  269. case (preg_match($pat_a[2],$url,$regs)):
  270. $k = $regs[1];
  271. $v = $regs[2];
  272. break;
  273. case (preg_match($pat_a[3],$url,$regs)):
  274. $k = $regs[1];
  275. $v = $regs[2];
  276. break;
  277. default:
  278. if ($val) {
  279. if (strpos($url,'?')) {
  280. $url .= "&$var=$val";
  281. } else {
  282. $url .= "?$var=$val";
  283. }
  284. }
  285. break;
  286. }
  287. if ($k) {
  288. if ($val) {
  289. $rpl = "$k=$val";
  290. } else {
  291. $rpl = '';
  292. }
  293. if( substr($v,-1)=='&' ) {
  294. $rpl .= '&';
  295. }
  296. $pat = "/$k=$v/";
  297. $url = preg_replace($pat,$rpl,$url);
  298. }
  299. if ($link) {
  300. $url = str_replace('&','&amp;',$url);
  301. }
  302. return $url;
  303. }