html.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 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 sould 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. * handy function to set url vars
  267. //FIXME: You call this documentation? :-) What does "set url vars" mean? Looks like it might take a fully-formed URI (possibly with a query string) and adds a variable/value pair to it(?)... Does $link work on only the var being added or the whole URI?
  268. *
  269. * especially useful when $url = $PHP_SELF
  270. *
  271. * @param string $url url that must be modified
  272. * @param string $var variable name
  273. * @param string $val variable value
  274. * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
  275. *
  276. * @return string $url modified url
  277. *
  278. * @since 1.3.0
  279. *
  280. */
  281. function set_url_var($url, $var, $val=0, $link=true) {
  282. $k = '';
  283. $pat_a = array (
  284. '/.+(\\&'.$var.')=(.*)\\&/AU', /* in the middle */
  285. '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
  286. '/.+(\\?'.$var.')=(.*)$/AU', /* at front and only var */
  287. '/.+(\\&'.$var.')=(.*)$/AU' /* at the end */
  288. );
  289. $url = str_replace('&amp;','&',$url);
  290. // FIXME: why switch is used instead of if () or one preg_match()
  291. switch (true) {
  292. case (preg_match($pat_a[0],$url,$regs)):
  293. $k = $regs[1];
  294. $v = $regs[2];
  295. break;
  296. case (preg_match($pat_a[1],$url,$regs)):
  297. $k = $regs[1];
  298. $v = $regs[2];
  299. break;
  300. case (preg_match($pat_a[2],$url,$regs)):
  301. $k = $regs[1];
  302. $v = $regs[2];
  303. break;
  304. case (preg_match($pat_a[3],$url,$regs)):
  305. $k = $regs[1];
  306. $v = $regs[2];
  307. break;
  308. default:
  309. if ($val) {
  310. if (strpos($url,'?')) {
  311. $url .= "&$var=$val";
  312. } else {
  313. $url .= "?$var=$val";
  314. }
  315. }
  316. break;
  317. }
  318. if ($k) {
  319. if ($val) {
  320. $rpl = "$k=$val";
  321. } else {
  322. $rpl = '';
  323. }
  324. if( substr($v,-1)=='&' ) {
  325. $rpl .= '&';
  326. }
  327. $pat = "/$k=$v/";
  328. $url = preg_replace($pat,$rpl,$url);
  329. }
  330. if ($link) {
  331. $url = str_replace('&','&amp;',$url);
  332. }
  333. return $url;
  334. }