lib.util.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. class util
  23. {
  24. # Retourne une paire de l'élément suivant ou précédent d'un tableau à
  25. # un indice donné
  26. function getNextPrev($array,$key,$mode)
  27. {
  28. $keys = array_keys($array);
  29. $values = array_values($array);
  30. $p = array_flip($keys);
  31. $pos = !empty($p[$key]) ? $p[$key] : 0;
  32. if($mode == 'next') {
  33. return ($pos+1 < count($array)) ? array($keys[$pos+1] => $values[$pos+1]) : NULL;
  34. } else {
  35. return ($pos > 0) ? array($keys[$pos-1] => $values[$pos-1]) : NULL;
  36. }
  37. }
  38. # Obtenir l'host complet
  39. function getHost()
  40. {
  41. $server_name = $_SERVER['HTTP_HOST'];
  42. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
  43. {
  44. $scheme = 'https';
  45. $port = ($_SERVER['SERVER_PORT'] != '443') ? ':'.$_SERVER['SERVER_PORT'] : '';
  46. }
  47. else
  48. {
  49. $scheme = 'http';
  50. $port = ($_SERVER['SERVER_PORT'] != '80') ? ':'.$_SERVER['SERVER_PORT'] : '';
  51. }
  52. return $scheme.'://'.$server_name.$port;
  53. }
  54. # Obtenir l'url complète de la page
  55. function getPageURL($uri=NULL)
  56. {
  57. if ($uri == NULL) {
  58. $uri = str_replace('&','&amp;',$_SERVER['REQUEST_URI']);
  59. }
  60. return util::getHost().$uri;
  61. }
  62. # Couper une chaîne aux espaces
  63. function cutString($str,$l)
  64. {
  65. $s = preg_split('/([\s]+)/',$str,-1,PREG_SPLIT_DELIM_CAPTURE);
  66. $res = '';
  67. $L = 0;
  68. if (strlen($s[0]) >= $l) {
  69. return substr($s[0],0,$l);
  70. }
  71. foreach ($s as $v)
  72. {
  73. $L = $L+strlen($v);
  74. if ($L > $l) {
  75. break;
  76. } else {
  77. $res .= $v;
  78. }
  79. }
  80. return trim($res);
  81. }
  82. # Converti une chaîne Latin1 en UTF-8 et effectue la translation
  83. # des caractères litigieux.
  84. function latin1utf8($str)
  85. {
  86. $conv = array(
  87. chr(194).chr(128) => chr(226).chr(130).chr(172),
  88. chr(194).chr(130) => chr(226).chr(128).chr(154),
  89. chr(194).chr(131) => chr(198).chr(146),
  90. chr(194).chr(132) => chr(226).chr(128).chr(158),
  91. chr(194).chr(133) => chr(226).chr(128).chr(166),
  92. chr(194).chr(134) => chr(226).chr(128).chr(160),
  93. chr(194).chr(135) => chr(226).chr(128).chr(161),
  94. chr(194).chr(136) => chr(203).chr(134),
  95. chr(194).chr(137) => chr(226).chr(128).chr(176),
  96. chr(194).chr(138) => chr(197).chr(160),
  97. chr(194).chr(139) => chr(226).chr(128).chr(185),
  98. chr(194).chr(140) => chr(197).chr(146),
  99. chr(194).chr(145) => chr(226).chr(128).chr(152),
  100. chr(194).chr(146) => chr(226).chr(128).chr(153),
  101. chr(194).chr(147) => chr(226).chr(128).chr(156),
  102. chr(194).chr(148) => chr(226).chr(128).chr(157),
  103. chr(194).chr(149) => chr(226).chr(128).chr(162),
  104. chr(194).chr(150) => chr(226).chr(128).chr(147),
  105. chr(194).chr(151) => chr(226).chr(128).chr(148),
  106. chr(194).chr(152) => chr(203).chr(156),
  107. chr(194).chr(153) => chr(226).chr(132).chr(162),
  108. chr(194).chr(154) => chr(197).chr(161),
  109. chr(194).chr(155) => chr(226).chr(128).chr(186),
  110. chr(194).chr(156) => chr(197).chr(147),
  111. chr(194).chr(159) => chr(197).chr(184)
  112. );
  113. $str = utf8_encode($str);
  114. return str_replace(array_keys($conv),array_values($conv),$str);
  115. }
  116. /**
  117. Encodage d'une chaine en mime Quoted printable
  118. */
  119. function mimeEncode($s,$charset='UTF-8')
  120. {
  121. $s = preg_replace('/([^\x21-\x3c\x3e-\x7e])/e','"=".strtoupper(dechex(ord("\1")))',$s);
  122. return '=?'.$charset.'?Q?'.$s.'?=';
  123. }
  124. }
  125. ?>