| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * html.php |
| 5 | * |
| 6 | * The idea is to inlcude here some functions to make easier |
| 7 | * the right to left implementation by "functionize" some |
| 8 | * html outputs. |
| 9 | * |
| 10 | * @copyright 1999-2018 The SquirrelMail Project Team |
| 11 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 12 | * @version $Id$ |
| 13 | * @package squirrelmail |
| 14 | * @since 1.3.0 |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * Generates a hyperlink |
| 20 | * |
| 21 | * @param string $uri The target link location |
| 22 | * @param string $text The link text |
| 23 | * @param string $target The location where the link should |
| 24 | * be opened (OPTIONAL; default not used) |
| 25 | * @param string $onclick The onClick JavaScript handler (OPTIONAL; |
| 26 | * default not used) |
| 27 | * @param string $class The CSS class name (OPTIONAL; default |
| 28 | * not used) |
| 29 | * @param string $id The ID name (OPTIONAL; default not used) |
| 30 | * @param string $name The anchor name (OPTIONAL; default not used) |
| 31 | * @param array $aAttribs Any extra attributes: this must be an |
| 32 | * associative array, where keys will be |
| 33 | * added as the attribute name, and values |
| 34 | * (which are optional - should be null if |
| 35 | * none should be used) will be placed in |
| 36 | * double quotes (pending template implementation) |
| 37 | * as the attribute value (OPTIONAL; default empty). |
| 38 | * |
| 39 | * @return string The desired hyperlink tag. |
| 40 | * |
| 41 | * @since 1.5.2 |
| 42 | * |
| 43 | */ |
| 44 | function create_hyperlink($uri, $text, $target='', $onclick='', |
| 45 | $class='', $id='', $name='', $aAttribs=array()) { |
| 46 | |
| 47 | global $oTemplate; |
| 48 | |
| 49 | $oTemplate->assign('uri', $uri); |
| 50 | $oTemplate->assign('text', $text); |
| 51 | $oTemplate->assign('target', $target); |
| 52 | $oTemplate->assign('onclick', $onclick); |
| 53 | $oTemplate->assign('class', $class); |
| 54 | $oTemplate->assign('id', $id); |
| 55 | $oTemplate->assign('name', $name); |
| 56 | |
| 57 | $oTemplate->assign('aAttribs', $aAttribs); |
| 58 | |
| 59 | return $oTemplate->fetch('hyperlink.tpl'); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Generates an image tag |
| 66 | * |
| 67 | * @param string $src The image source path |
| 68 | * @param string $alt Alternate link text (OPTIONAL; default |
| 69 | * not used) |
| 70 | * @param string $width The width the image should be shown in |
| 71 | * (OPTIONAL; default not used) |
| 72 | * @param string $height The height the image should be shown in |
| 73 | * (OPTIONAL; default not used) |
| 74 | * @param string $border The image's border attribute value |
| 75 | * (OPTIONAL; default not used) |
| 76 | * @param string $class The CSS class name (OPTIONAL; default |
| 77 | * not used) |
| 78 | * @param string $id The ID name (OPTIONAL; default not used) |
| 79 | * @param string $onclick The onClick JavaScript handler (OPTIONAL; |
| 80 | * default not used) |
| 81 | * @param string $title The image's title attribute value |
| 82 | * (OPTIONAL; default not used) |
| 83 | * @param string $align The image's alignment attribute value |
| 84 | * (OPTIONAL; default not used) |
| 85 | * @param string $hspace The image's hspace attribute value |
| 86 | * (OPTIONAL; default not used) |
| 87 | * @param string $vspace The image's vspace attribute value |
| 88 | * (OPTIONAL; default not used) |
| 89 | * @param string $text_alternative A text replacement for the entire |
| 90 | * image tag, to be used at the |
| 91 | * discretion of the template set, |
| 92 | * if for some reason the image tag |
| 93 | * cannot or should not be produced |
| 94 | * (OPTIONAL; default not used) |
| 95 | * @param array $aAttribs Any extra attributes: this must be an |
| 96 | * associative array, where keys will be |
| 97 | * added as the attribute name, and values |
| 98 | * (which are optional - should be null if |
| 99 | * none should be used) will be placed in |
| 100 | * double quotes (pending template implementation) |
| 101 | * as the attribute value (OPTIONAL; default empty). |
| 102 | * |
| 103 | * @return string The desired hyperlink tag. |
| 104 | * |
| 105 | * @since 1.5.2 |
| 106 | * |
| 107 | */ |
| 108 | function create_image($src, $alt='', $width='', $height='', |
| 109 | $border='', $class='', $id='', $onclick='', |
| 110 | $title='', $align='', $hspace='', $vspace='', |
| 111 | $text_alternative='', $aAttribs=array()) { |
| 112 | |
| 113 | global $oTemplate; |
| 114 | |
| 115 | $oTemplate->assign('src', $src); |
| 116 | $oTemplate->assign('alt', $alt); |
| 117 | $oTemplate->assign('width', $width); |
| 118 | $oTemplate->assign('height', $height); |
| 119 | $oTemplate->assign('border', $border); |
| 120 | $oTemplate->assign('class', $class); |
| 121 | $oTemplate->assign('id', $id); |
| 122 | $oTemplate->assign('onclick', $onclick); |
| 123 | $oTemplate->assign('title', $title); |
| 124 | $oTemplate->assign('align', $align); |
| 125 | $oTemplate->assign('hspace', $hspace); |
| 126 | $oTemplate->assign('vspace', $vspace); |
| 127 | $oTemplate->assign('text_alternative', $text_alternative); |
| 128 | |
| 129 | $oTemplate->assign('aAttribs', $aAttribs); |
| 130 | |
| 131 | return $oTemplate->fetch('image.tpl'); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Generates a label tag |
| 138 | * |
| 139 | * @param string $value The contents that belong inside the label |
| 140 | * @param string $for The ID to which the label applies (OPTIONAL; |
| 141 | * default not used) |
| 142 | * @param array $aAttribs Any extra attributes: this must be an |
| 143 | * associative array, where keys will be |
| 144 | * added as the attribute name, and values |
| 145 | * (which are optional - should be null if |
| 146 | * none should be used) will be placed in |
| 147 | * double quotes (pending template implementation) |
| 148 | * as the attribute value (OPTIONAL; default empty). |
| 149 | * |
| 150 | * @return string The desired label tag. |
| 151 | * |
| 152 | * @since 1.5.2 |
| 153 | * |
| 154 | */ |
| 155 | function create_label($value, $for='', $aAttribs=array()) { |
| 156 | |
| 157 | global $oTemplate; |
| 158 | |
| 159 | $oTemplate->assign('text', $value); |
| 160 | $oTemplate->assign('for', $for); |
| 161 | |
| 162 | $oTemplate->assign('aAttribs', $aAttribs); |
| 163 | |
| 164 | return $oTemplate->fetch('label.tpl'); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Generates a span tag |
| 171 | * |
| 172 | * @param string $value The contents that belong inside the span |
| 173 | * @param string $class The CSS class name (OPTIONAL; default |
| 174 | * not used) |
| 175 | * @param string $id The ID name (OPTIONAL; default not used) |
| 176 | * @param array $aAttribs Any extra attributes: this must be an |
| 177 | * associative array, where keys will be |
| 178 | * added as the attribute name, and values |
| 179 | * (which are optional - should be null if |
| 180 | * none should be used) will be placed in |
| 181 | * double quotes (pending template implementation) |
| 182 | * as the attribute value (OPTIONAL; default empty). |
| 183 | * |
| 184 | * @return string The desired span tag. |
| 185 | * |
| 186 | * @since 1.5.2 |
| 187 | * |
| 188 | */ |
| 189 | function create_span($value, $class='', $id='', $aAttribs=array()) { |
| 190 | |
| 191 | global $oTemplate; |
| 192 | |
| 193 | $oTemplate->assign('value', $value); |
| 194 | $oTemplate->assign('class', $class); |
| 195 | $oTemplate->assign('id', $id); |
| 196 | |
| 197 | $oTemplate->assign('aAttribs', $aAttribs); |
| 198 | |
| 199 | return $oTemplate->fetch('span.tpl'); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * Generates an opening body tag |
| 206 | * |
| 207 | * @param string $onload Body onload JavaScript handler code |
| 208 | * (OPTIONAL; default not used) |
| 209 | * @param string $class The CSS class name (OPTIONAL; default |
| 210 | * not used) |
| 211 | * @param array $aAttribs Any extra attributes: this must be an |
| 212 | * associative array, where keys will be |
| 213 | * added as the attribute name, and values |
| 214 | * (which are optional - should be null if |
| 215 | * none should be used) will be placed in |
| 216 | * double quotes (pending template implementation) |
| 217 | * as the attribute value (OPTIONAL; default empty). |
| 218 | * |
| 219 | * @return string The desired body tag. |
| 220 | * |
| 221 | * @since 1.5.2 |
| 222 | * |
| 223 | */ |
| 224 | function create_body($onload='', $class='', $aAttribs=array()) { |
| 225 | |
| 226 | global $oTemplate; |
| 227 | |
| 228 | $oTemplate->assign('onload', $onload); |
| 229 | $oTemplate->assign('class', $class); |
| 230 | |
| 231 | $oTemplate->assign('aAttribs', $aAttribs); |
| 232 | |
| 233 | return $oTemplate->fetch('body.tpl'); |
| 234 | |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** |
| 239 | * Generates html tags |
| 240 | //FIXME: This should not be used anywhere in the core, or we should convert this to use templates. We should not be assuming HTML output. |
| 241 | * |
| 242 | * @param string $tag Tag to output |
| 243 | * @param string $val Value between tags |
| 244 | * @param string $align Alignment (left, center, etc) |
| 245 | * @param string $bgcolor Back color in hexadecimal |
| 246 | * @param string $xtra Extra options |
| 247 | * @return string HTML ready for output |
| 248 | * @since 1.3.0 |
| 249 | */ |
| 250 | function html_tag( $tag, // Tag to output |
| 251 | $val = '', // Value between tags |
| 252 | $align = '', // Alignment |
| 253 | $bgcolor = '', // Back color |
| 254 | $xtra = '' ) { // Extra options |
| 255 | |
| 256 | GLOBAL $languages, $squirrelmail_language; |
| 257 | |
| 258 | $align = strtolower( $align ); |
| 259 | $bgc = ''; |
| 260 | $tag = strtolower( $tag ); |
| 261 | |
| 262 | if ( isset( $languages[$squirrelmail_language]['DIR']) ) { |
| 263 | $dir = $languages[$squirrelmail_language]['DIR']; |
| 264 | } else { |
| 265 | $dir = 'ltr'; |
| 266 | } |
| 267 | |
| 268 | if ( $dir == 'ltr' ) { |
| 269 | $rgt = 'right'; |
| 270 | $lft = 'left'; |
| 271 | } else { |
| 272 | $rgt = 'left'; |
| 273 | $lft = 'right'; |
| 274 | } |
| 275 | |
| 276 | if ( $bgcolor <> '' ) { |
| 277 | $bgc = " bgcolor=\"$bgcolor\""; |
| 278 | } |
| 279 | |
| 280 | switch ( $align ) { |
| 281 | case '': |
| 282 | $alg = ''; |
| 283 | break; |
| 284 | case 'right': |
| 285 | $alg = " align=\"$rgt\""; |
| 286 | break; |
| 287 | case 'left': |
| 288 | $alg = " align=\"$lft\""; |
| 289 | break; |
| 290 | default: |
| 291 | $alg = " align=\"$align\""; |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | $ret = "<$tag"; |
| 296 | |
| 297 | if ( $dir <> 'ltr' ) { |
| 298 | $ret .= " dir=\"$dir\""; |
| 299 | } |
| 300 | $ret .= $bgc . $alg; |
| 301 | |
| 302 | if ( $xtra <> '' ) { |
| 303 | $ret .= " $xtra"; |
| 304 | } |
| 305 | |
| 306 | if ( $val <> '' ) { |
| 307 | $ret .= ">$val</$tag>\n"; |
| 308 | } else { |
| 309 | $ret .= '>'. "\n"; |
| 310 | } |
| 311 | |
| 312 | return( $ret ); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /** |
| 317 | * This function is used to add, modify or delete more than |
| 318 | * one GET variable at a time in a URL. This simply takes |
| 319 | * an array of variables (key/value pairs) and passes them |
| 320 | * one at a time to {@link set_url_var}. |
| 321 | * |
| 322 | * Note that the value for any one of the variables may be |
| 323 | * an array, and it will be handled properly. |
| 324 | * |
| 325 | * As with set_url_var, any of the variable values may be |
| 326 | * set to NULL to remove it from the URI. |
| 327 | * |
| 328 | * Also, to ensure compatibility with older versions, use |
| 329 | * $val='0' to set $var to 0. |
| 330 | * |
| 331 | * @param string $uri URI that must be modified |
| 332 | * @param array $values List of GET variable names and their values |
| 333 | * @param boolean $sanitize Controls sanitizing of ampersand in URIs |
| 334 | * |
| 335 | * @return string The modified URI |
| 336 | * |
| 337 | * @since 1.5.2 |
| 338 | * |
| 339 | */ |
| 340 | function set_uri_vars($uri, $values, $sanitize=TRUE) { |
| 341 | foreach ($values as $key => $value) |
| 342 | if (is_array($value)) { |
| 343 | $i = 0; |
| 344 | foreach ($value as $val) |
| 345 | $uri = set_url_var($uri, $key . '[' . $i++ . ']', $val, $sanitize); |
| 346 | } |
| 347 | else |
| 348 | $uri = set_url_var($uri, $key, $value, $sanitize); |
| 349 | return $uri; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /** |
| 354 | * This function is used to add, modify or delete GET variables in a URL. |
| 355 | * It is especially useful when $url = $PHP_SELF |
| 356 | * |
| 357 | * Set $val to NULL to remove $var from $url. |
| 358 | * To ensure compatibility with older versions, use $val='0' to set $var to 0. |
| 359 | * |
| 360 | * @param string $url url that must be modified |
| 361 | * @param string $var GET variable name |
| 362 | * @param string $val variable value (CANNOT be an array) |
| 363 | * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2) |
| 364 | * @param boolean $treat_as_array When TRUE, if $var is an array (it occurs one |
| 365 | * or more times with square brackets after it, |
| 366 | * e.g. "var[1]"), the whole array will be removed |
| 367 | * (when $val is NULL) or the given value will be |
| 368 | * added to the next array slot (@since 1.4.23/1.5.2) |
| 369 | * |
| 370 | * @return string $url modified url |
| 371 | * |
| 372 | * @since 1.3.0 |
| 373 | * |
| 374 | */ |
| 375 | function set_url_var($url, $var, $val=null, $link=true, $treat_as_array=false) { |
| 376 | $url = str_replace('&','&',$url); |
| 377 | |
| 378 | if (strpos($url, '?') === false) { |
| 379 | $url .= '?'; |
| 380 | } |
| 381 | |
| 382 | list($uri, $params) = explode('?', $url, 2); |
| 383 | |
| 384 | $newpar = array(); |
| 385 | $params = explode('&', $params); |
| 386 | $array_names = array(); |
| 387 | |
| 388 | foreach ($params as $p) { |
| 389 | if (trim($p)) { |
| 390 | $p = explode('=', $p); |
| 391 | $newpar[$p[0]] = (isset($p[1]) ? $p[1] : ''); |
| 392 | if ($treat_as_array && preg_match('/(.*)\[(\d+)]$/', $p[0], $matches)) { |
| 393 | if (!isset($array_names[$matches[1]])) $array_names[$matches[1]] = array(); |
| 394 | $array_names[$matches[1]][$matches[2]] = $p[1]; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if (is_null($val)) { |
| 400 | if ($treat_as_array && !empty($array_names[$var])) { |
| 401 | foreach ($array_names[$var] as $key => $ignore) |
| 402 | unset($newpar[$var . '[' . $key . ']']); |
| 403 | } else { |
| 404 | unset($newpar[$var]); |
| 405 | } |
| 406 | } else { |
| 407 | if ($treat_as_array && !empty($array_names[$var])) { |
| 408 | $max_key = 0; |
| 409 | foreach ($array_names[$var] as $key => $ignore) |
| 410 | if ($key >= $max_key) $max_key = $key + 1; |
| 411 | $newpar[$var . '[' . $max_key . ']'] = $val; |
| 412 | } else { |
| 413 | $newpar[$var] = $val; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (!count($newpar)) { |
| 418 | return $uri; |
| 419 | } |
| 420 | |
| 421 | $url = $uri . '?'; |
| 422 | foreach ($newpar as $name => $value) { |
| 423 | $url .= "$name=$value&"; |
| 424 | } |
| 425 | |
| 426 | $url = substr($url, 0, -1); |
| 427 | if ($link) { |
| 428 | $url = str_replace('&','&',$url); |
| 429 | } |
| 430 | |
| 431 | return $url; |
| 432 | } |
| 433 | |