strings.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. <?php
  2. /**
  3. * strings.php
  4. *
  5. * This code provides various string manipulation functions that are
  6. * used by the rest of the SquirrelMail code.
  7. *
  8. * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /** @ignore */
  14. if (!defined('SM_PATH')) define('SM_PATH','../');
  15. /**
  16. * SquirrelMail version number -- DO NOT CHANGE
  17. */
  18. global $version;
  19. $version = '1.5.1 [CVS]';
  20. /**
  21. * SquirrelMail internal version number -- DO NOT CHANGE
  22. * $sm_internal_version = array (release, major, minor)
  23. */
  24. global $SQM_INTERNAL_VERSION;
  25. $SQM_INTERNAL_VERSION = array(1,5,1);
  26. /**
  27. * There can be a circular issue with includes, where the $version string is
  28. * referenced by the include of global.php, etc. before it's defined.
  29. * For that reason, bring in global.php AFTER we define the version strings.
  30. */
  31. include_once(SM_PATH . 'functions/global.php');
  32. /**
  33. * Include Compatibility plugin if available.
  34. */
  35. if (file_exists(SM_PATH . 'plugins/compatibility/functions.php'))
  36. include_once(SM_PATH . 'plugins/compatibility/functions.php');
  37. /**
  38. * Appends citation markers to the string.
  39. * Also appends a trailing space.
  40. *
  41. * @author Justus Pendleton
  42. * @param string $str The string to append to
  43. * @param int $citeLevel the number of markers to append
  44. * @return null
  45. * @since 1.5.1
  46. */
  47. function sqMakeCite (&$str, $citeLevel) {
  48. for ($i = 0; $i < $citeLevel; $i++) {
  49. $str .= '>';
  50. }
  51. if ($citeLevel != 0) {
  52. $str .= ' ';
  53. }
  54. }
  55. /**
  56. * Create a newline in the string, adding citation
  57. * markers to the newline as necessary.
  58. *
  59. * @author Justus Pendleton
  60. * @param string $str the string to make a newline in
  61. * @param int $citeLevel the citation level the newline is at
  62. * @param int $column starting column of the newline
  63. * @return null
  64. * @since 1.5.1
  65. */
  66. function sqMakeNewLine (&$str, $citeLevel, &$column) {
  67. $str .= "\n";
  68. $column = 0;
  69. if ($citeLevel > 0) {
  70. sqMakeCite ($str, $citeLevel);
  71. $column = $citeLevel + 1;
  72. } else {
  73. $column = 0;
  74. }
  75. }
  76. /**
  77. * Checks for spaces in strings - only used if PHP doesn't have native ctype support
  78. *
  79. * You might be able to rewrite the function by adding short evaluation form.
  80. *
  81. * possible problems:
  82. * - iso-2022-xx charsets - hex 20 might be part of other symbol. I might
  83. * be wrong. 0x20 is not used in iso-2022-jp. I haven't checked iso-2022-kr
  84. * and iso-2022-cn mappings.
  85. *
  86. * - no-break space (&nbsp;) - it is 8bit symbol, that depends on charset.
  87. * there are at least three different charset groups that have nbsp in
  88. * different places.
  89. *
  90. * I don't see any charset/nbsp options in php ctype either.
  91. *
  92. * @param string $string tested string
  93. * @return bool true when only whitespace symbols are present in test string
  94. * @since 1.5.1
  95. */
  96. function sm_ctype_space($string) {
  97. if ( preg_match('/^[\x09-\x0D]|^\x20/', $string) || $string=='') {
  98. return true;
  99. } else {
  100. return false;
  101. }
  102. }
  103. /**
  104. * Wraps text at $wrap characters. While sqWordWrap takes
  105. * a single line of text and wraps it, this function works
  106. * on the entire corpus at once, this allows it to be a little
  107. * bit smarter and when and how to wrap.
  108. *
  109. * @author Justus Pendleton
  110. * @param string $body the entire body of text
  111. * @param int $wrap the maximum line length
  112. * @return string the wrapped text
  113. * @since 1.5.1
  114. */
  115. function &sqBodyWrap (&$body, $wrap) {
  116. //check for ctype support, and fake it if it doesn't exist
  117. if (!function_exists('ctype_space')) {
  118. function ctype_space ($string) {
  119. return sm_ctype_space($string);
  120. }
  121. }
  122. // the newly wrapped text
  123. $outString = '';
  124. // current column since the last newline in the outstring
  125. $outStringCol = 0;
  126. $length = sq_strlen($body);
  127. // where we are in the original string
  128. $pos = 0;
  129. // the number of >>> citation markers we are currently at
  130. $citeLevel = 0;
  131. // the main loop, whenever we start a newline of input text
  132. // we start from here
  133. while ($pos < $length) {
  134. // we're at the beginning of a line, get the new cite level
  135. $newCiteLevel = 0;
  136. while (($pos < $length) && (sq_substr($body,$pos,1) == '>')) {
  137. $newCiteLevel++;
  138. $pos++;
  139. // skip over any spaces interleaved among the cite markers
  140. while (($pos < $length) && (sq_substr($body,$pos,1) == ' ')) {
  141. $pos++;
  142. }
  143. if ($pos >= $length) {
  144. break;
  145. }
  146. }
  147. // special case: if this is a blank line then maintain it
  148. // (i.e. try to preserve original paragraph breaks)
  149. // unless they occur at the very beginning of the text
  150. if ((sq_substr($body,$pos,1) == "\n" ) && (sq_strlen($outString) != 0)) {
  151. $outStringLast = $outString{sq_strlen($outString) - 1};
  152. if ($outStringLast != "\n") {
  153. $outString .= "\n";
  154. }
  155. sqMakeCite ($outString, $newCiteLevel);
  156. $outString .= "\n";
  157. $pos++;
  158. $outStringCol = 0;
  159. continue;
  160. }
  161. // if the cite level has changed, then start a new line
  162. // with the new cite level.
  163. if (($citeLevel != $newCiteLevel) && ($pos > ($newCiteLevel + 1)) && ($outStringCol != 0)) {
  164. sqMakeNewLine ($outString, 0, $outStringCol);
  165. }
  166. $citeLevel = $newCiteLevel;
  167. // prepend the quote level if necessary
  168. if ($outStringCol == 0) {
  169. sqMakeCite ($outString, $citeLevel);
  170. // if we added a citation then move the column
  171. // out by citelevel + 1 (the cite markers + the space)
  172. $outStringCol = $citeLevel + ($citeLevel ? 1 : 0);
  173. } else if ($outStringCol > $citeLevel) {
  174. // not a cite and we're not at the beginning of a line
  175. // in the output. add a space to separate the new text
  176. // from previous text.
  177. $outString .= ' ';
  178. $outStringCol++;
  179. }
  180. // find the next newline -- we don't want to go further than that
  181. $nextNewline = sq_strpos ($body, "\n", $pos);
  182. if ($nextNewline === FALSE) {
  183. $nextNewline = $length;
  184. }
  185. // Don't wrap unquoted lines at all. For now the textarea
  186. // will work fine for this. Maybe revisit this later though
  187. // (for completeness more than anything else, I think)
  188. if ($citeLevel == 0) {
  189. $outString .= sq_substr ($body, $pos, ($nextNewline - $pos));
  190. $outStringCol = $nextNewline - $pos;
  191. if ($nextNewline != $length) {
  192. sqMakeNewLine ($outString, 0, $outStringCol);
  193. }
  194. $pos = $nextNewline + 1;
  195. continue;
  196. }
  197. /**
  198. * Set this to false to stop appending short strings to previous lines
  199. */
  200. $smartwrap = true;
  201. // inner loop, (obviously) handles wrapping up to
  202. // the next newline
  203. while ($pos < $nextNewline) {
  204. // skip over initial spaces
  205. while (($pos < $nextNewline) && (ctype_space (sq_substr($body,$pos,1)))) {
  206. $pos++;
  207. }
  208. // if this is a short line then just append it and continue outer loop
  209. if (($outStringCol + $nextNewline - $pos) <= ($wrap - $citeLevel - 1) ) {
  210. // if this is the final line in the input string then include
  211. // any trailing newlines
  212. // echo substr($body,$pos,$wrap). "<br />";
  213. if (($nextNewline + 1 == $length) && (sq_substr($body,$nextNewline,1) == "\n")) {
  214. $nextNewline++;
  215. }
  216. // trim trailing spaces
  217. $lastRealChar = $nextNewline;
  218. while (($lastRealChar > $pos && $lastRealChar < $length) && (ctype_space (sq_substr($body,$lastRealChar,1)))) {
  219. $lastRealChar--;
  220. }
  221. // decide if appending the short string is what we want
  222. if (($nextNewline < $length && sq_substr($body,$nextNewline,1) == "\n") &&
  223. isset($lastRealChar)) {
  224. $mypos = $pos;
  225. //check the first word:
  226. while (($mypos < $length) && (sq_substr($body,$mypos,1) == '>')) {
  227. $mypos++;
  228. // skip over any spaces interleaved among the cite markers
  229. while (($mypos < $length) && (sq_substr($body,$mypos,1) == ' ')) {
  230. $mypos++;
  231. }
  232. }
  233. /*
  234. $ldnspacecnt = 0;
  235. if ($mypos == $nextNewline+1) {
  236. while (($mypos < $length) && ($body{$mypos} == ' ')) {
  237. $ldnspacecnt++;
  238. }
  239. }
  240. */
  241. $firstword = sq_substr($body,$mypos,sq_strpos($body,' ',$mypos) - $mypos);
  242. //if ($dowrap || $ldnspacecnt > 1 || ($firstword && (
  243. if (!$smartwrap || $firstword && (
  244. $firstword{0} == '-' ||
  245. $firstword{0} == '+' ||
  246. $firstword{0} == '*' ||
  247. sq_substr($firstword,0,1) == sq_strtoupper(sq_substr($firstword,0,1)) ||
  248. strpos($firstword,':'))) {
  249. $outString .= sq_substr($body,$pos,($lastRealChar - $pos+1));
  250. $outStringCol += ($lastRealChar - $pos);
  251. sqMakeNewLine($outString,$citeLevel,$outStringCol);
  252. $nextNewline++;
  253. $pos = $nextNewline;
  254. $outStringCol--;
  255. continue;
  256. }
  257. }
  258. $outString .= sq_substr ($body, $pos, ($lastRealChar - $pos + 1));
  259. $outStringCol += ($lastRealChar - $pos);
  260. $pos = $nextNewline + 1;
  261. continue;
  262. }
  263. $eol = $pos + $wrap - $citeLevel - $outStringCol;
  264. // eol is the tentative end of line.
  265. // look backwards for there for a whitespace to break at.
  266. // if it's already less than our current position then
  267. // our current line is already too long, break immediately
  268. // and restart outer loop
  269. if ($eol <= $pos) {
  270. sqMakeNewLine ($outString, $citeLevel, $outStringCol);
  271. continue;
  272. }
  273. // start looking backwards for whitespace to break at.
  274. $breakPoint = $eol;
  275. while (($breakPoint > $pos) && (! ctype_space (sq_substr($body,$breakPoint,1)))) {
  276. $breakPoint--;
  277. }
  278. // if we didn't find a breakpoint by looking backward then we
  279. // need to figure out what to do about that
  280. if ($breakPoint == $pos) {
  281. // if we are not at the beginning then end this line
  282. // and start a new loop
  283. if ($outStringCol > ($citeLevel + 1)) {
  284. sqMakeNewLine ($outString, $citeLevel, $outStringCol);
  285. continue;
  286. } else {
  287. // just hard break here. most likely we are breaking
  288. // a really long URL. could also try searching
  289. // forward for a break point, which is what Mozilla
  290. // does. don't bother for now.
  291. $breakPoint = $eol;
  292. }
  293. }
  294. // special case: maybe we should have wrapped last
  295. // time. if the first breakpoint here makes the
  296. // current line too long and there is already text on
  297. // the current line, break and loop again if at
  298. // beginning of current line, don't force break
  299. $SLOP = 6;
  300. if ((($outStringCol + ($breakPoint - $pos)) > ($wrap + $SLOP)) && ($outStringCol > ($citeLevel + 1))) {
  301. sqMakeNewLine ($outString, $citeLevel, $outStringCol);
  302. continue;
  303. }
  304. // skip newlines or whitespace at the beginning of the string
  305. $substring = sq_substr ($body, $pos, ($breakPoint - $pos));
  306. $substring = rtrim ($substring); // do rtrim and ctype_space have the same ideas about whitespace?
  307. $outString .= $substring;
  308. $outStringCol += sq_strlen ($substring);
  309. // advance past the whitespace which caused the wrap
  310. $pos = $breakPoint;
  311. while (($pos < $length) && (ctype_space (sq_substr($body,$pos,1)))) {
  312. $pos++;
  313. }
  314. if ($pos < $length) {
  315. sqMakeNewLine ($outString, $citeLevel, $outStringCol);
  316. }
  317. }
  318. }
  319. return $outString;
  320. }
  321. /**
  322. * Wraps text at $wrap characters
  323. *
  324. * Has a problem with special HTML characters, so call this before
  325. * you do character translation.
  326. *
  327. * Specifically, &amp;#039; comes up as 5 characters instead of 1.
  328. * This should not add newlines to the end of lines.
  329. *
  330. * @param string $line the line of text to wrap, by ref
  331. * @param int $wrap the maximum line lenth
  332. * @param string $charset name of charset used in $line string. Available since v.1.5.1.
  333. * @return void
  334. * @since 1.0
  335. */
  336. function sqWordWrap(&$line, $wrap, $charset='') {
  337. global $languages, $squirrelmail_language;
  338. // Use custom wrapping function, if translation provides it
  339. if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  340. function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap')) {
  341. if (mb_detect_encoding($line) != 'ASCII') {
  342. $line = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap', $line, $wrap);
  343. return;
  344. }
  345. }
  346. ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
  347. $beginning_spaces = $regs[1];
  348. if (isset($regs[2])) {
  349. $words = explode(' ', $regs[2]);
  350. } else {
  351. $words = '';
  352. }
  353. $i = 0;
  354. $line = $beginning_spaces;
  355. while ($i < count($words)) {
  356. /* Force one word to be on a line (minimum) */
  357. $line .= $words[$i];
  358. $line_len = strlen($beginning_spaces) + sq_strlen($words[$i],$charset) + 2;
  359. if (isset($words[$i + 1]))
  360. $line_len += sq_strlen($words[$i + 1],$charset);
  361. $i ++;
  362. /* Add more words (as long as they fit) */
  363. while ($line_len < $wrap && $i < count($words)) {
  364. $line .= ' ' . $words[$i];
  365. $i++;
  366. if (isset($words[$i]))
  367. $line_len += sq_strlen($words[$i],$charset) + 1;
  368. else
  369. $line_len += 1;
  370. }
  371. /* Skip spaces if they are the first thing on a continued line */
  372. while (!isset($words[$i]) && $i < count($words)) {
  373. $i ++;
  374. }
  375. /* Go to the next line if we have more to process */
  376. if ($i < count($words)) {
  377. $line .= "\n";
  378. }
  379. }
  380. }
  381. /**
  382. * Does the opposite of sqWordWrap()
  383. * @param string $body the text to un-wordwrap
  384. * @return void
  385. * @since 1.0
  386. */
  387. function sqUnWordWrap(&$body) {
  388. global $squirrelmail_language;
  389. if ($squirrelmail_language == 'ja_JP') {
  390. return;
  391. }
  392. $lines = explode("\n", $body);
  393. $body = '';
  394. $PreviousSpaces = '';
  395. $cnt = count($lines);
  396. for ($i = 0; $i < $cnt; $i ++) {
  397. preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
  398. $CurrentSpaces = $regs[1];
  399. if (isset($regs[2])) {
  400. $CurrentRest = $regs[2];
  401. } else {
  402. $CurrentRest = '';
  403. }
  404. if ($i == 0) {
  405. $PreviousSpaces = $CurrentSpaces;
  406. $body = $lines[$i];
  407. } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */
  408. && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */
  409. && strlen($CurrentRest)) { /* and there's a line to continue with */
  410. $body .= ' ' . $CurrentRest;
  411. } else {
  412. $body .= "\n" . $lines[$i];
  413. $PreviousSpaces = $CurrentSpaces;
  414. }
  415. }
  416. $body .= "\n";
  417. }
  418. /**
  419. * If $haystack is a full mailbox name and $needle is the mailbox
  420. * separator character, returns the last part of the mailbox name.
  421. *
  422. * @param string haystack full mailbox name to search
  423. * @param string needle the mailbox separator character
  424. * @return string the last part of the mailbox name
  425. * @since 1.0
  426. */
  427. function readShortMailboxName($haystack, $needle) {
  428. if ($needle == '') {
  429. $elem = $haystack;
  430. } else {
  431. $parts = explode($needle, $haystack);
  432. $elem = array_pop($parts);
  433. while ($elem == '' && count($parts)) {
  434. $elem = array_pop($parts);
  435. }
  436. }
  437. return( $elem );
  438. }
  439. /**
  440. * Find out where SquirrelMail lives and try to be smart about it.
  441. * The only problem would be when SquirrelMail lives in directories
  442. * called "src", "functions", or "plugins", but people who do that need
  443. * to be beaten with a steel pipe anyway.
  444. *
  445. * @return string the base uri of SquirrelMail installation.
  446. * @since 1.2.6
  447. */
  448. function sqm_baseuri(){
  449. global $base_uri, $PHP_SELF;
  450. /**
  451. * If it is in the session, just return it.
  452. */
  453. if (isset($base_uri)){
  454. return $base_uri;
  455. }
  456. $dirs = array('|src/.*|', '|plugins/.*|', '|functions/.*|');
  457. $repl = array('', '', '');
  458. $base_uri = preg_replace($dirs, $repl, $PHP_SELF);
  459. return $base_uri;
  460. }
  461. /**
  462. * get_location
  463. *
  464. * Determines the location to forward to, relative to your server.
  465. * This is used in HTTP Location: redirects.
  466. * If this doesnt work correctly for you (although it should), you can
  467. * remove all this code except the last two lines, and have it return
  468. * the right URL for your site, something like:
  469. *
  470. * http://www.example.com/squirrelmail/
  471. *
  472. * @return string the base url for this SquirrelMail installation
  473. * @since 1.0
  474. */
  475. function get_location () {
  476. global $imap_server_type;
  477. /* Get the path, handle virtual directories */
  478. if(strpos(php_self(), '?')) {
  479. $path = substr(php_self(), 0, strpos(php_self(), '?'));
  480. } else {
  481. $path = php_self();
  482. }
  483. $path = substr($path, 0, strrpos($path, '/'));
  484. if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) {
  485. return $full_url . $path;
  486. }
  487. /* Check if this is a HTTPS or regular HTTP request. */
  488. $proto = 'http://';
  489. /*
  490. * If you have 'SSLOptions +StdEnvVars' in your apache config
  491. * OR if you have HTTPS=on in your HTTP_SERVER_VARS
  492. * OR if you are on port 443
  493. */
  494. $getEnvVar = getenv('HTTPS');
  495. if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
  496. (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) ||
  497. (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) {
  498. $proto = 'https://';
  499. }
  500. /* Get the hostname from the Host header or server config. */
  501. if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) {
  502. if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) {
  503. $host = '';
  504. }
  505. }
  506. $port = '';
  507. if (! strstr($host, ':')) {
  508. if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) {
  509. if (($server_port != 80 && $proto == 'http://') ||
  510. ($server_port != 443 && $proto == 'https://')) {
  511. $port = sprintf(':%d', $server_port);
  512. }
  513. }
  514. }
  515. /* this is a workaround for the weird macosx caching that
  516. causes Apache to return 16080 as the port number, which causes
  517. SM to bail */
  518. if ($imap_server_type == 'macosx' && $port == ':16080') {
  519. $port = '';
  520. }
  521. /* Fallback is to omit the server name and use a relative */
  522. /* URI, although this is not RFC 2616 compliant. */
  523. $full_url = ($host ? $proto . $host . $port : '');
  524. sqsession_register($full_url, 'sq_base_url');
  525. return $full_url . $path;
  526. }
  527. /**
  528. * Encrypts password
  529. *
  530. * These functions are used to encrypt the password before it is
  531. * stored in a cookie. The encryption key is generated by
  532. * OneTimePadCreate();
  533. *
  534. * @param string $string the (password)string to encrypt
  535. * @param string $epad the encryption key
  536. * @return string the base64-encoded encrypted password
  537. * @since 1.0
  538. */
  539. function OneTimePadEncrypt ($string, $epad) {
  540. $pad = base64_decode($epad);
  541. if (strlen($pad)>0) {
  542. // make sure that pad is longer than string
  543. while (strlen($string)>strlen($pad)) {
  544. $pad.=$pad;
  545. }
  546. } else {
  547. // FIXME: what should we do when $epad is not base64 encoded or empty.
  548. }
  549. $encrypted = '';
  550. for ($i = 0; $i < strlen ($string); $i++) {
  551. $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
  552. }
  553. return base64_encode($encrypted);
  554. }
  555. /**
  556. * Decrypts a password from the cookie
  557. *
  558. * Decrypts a password from the cookie, encrypted by OneTimePadEncrypt.
  559. * This uses the encryption key that is stored in the session.
  560. *
  561. * @param string $string the string to decrypt
  562. * @param string $epad the encryption key from the session
  563. * @return string the decrypted password
  564. * @since 1.0
  565. */
  566. function OneTimePadDecrypt ($string, $epad) {
  567. $pad = base64_decode($epad);
  568. if (strlen($pad)>0) {
  569. // make sure that pad is longer than string
  570. while (strlen($string)>strlen($pad)) {
  571. $pad.=$pad;
  572. }
  573. } else {
  574. // FIXME: what should we do when $epad is not base64 encoded or empty.
  575. }
  576. $encrypted = base64_decode ($string);
  577. $decrypted = '';
  578. for ($i = 0; $i < strlen ($encrypted); $i++) {
  579. $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
  580. }
  581. return $decrypted;
  582. }
  583. /**
  584. * Randomizes the mt_rand() function.
  585. *
  586. * Toss this in strings or integers and it will seed the generator
  587. * appropriately. With strings, it is better to get them long.
  588. * Use md5() to lengthen smaller strings.
  589. *
  590. * @param mixed $val a value to seed the random number generator. mixed = integer or string.
  591. * @return void
  592. * @since 1.0
  593. */
  594. function sq_mt_seed($Val) {
  595. /* if mt_getrandmax() does not return a 2^n - 1 number,
  596. this might not work well. This uses $Max as a bitmask. */
  597. $Max = mt_getrandmax();
  598. if (! is_int($Val)) {
  599. $Val = crc32($Val);
  600. }
  601. if ($Val < 0) {
  602. $Val *= -1;
  603. }
  604. if ($Val == 0) {
  605. return;
  606. }
  607. mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
  608. }
  609. /**
  610. * Init random number generator
  611. *
  612. * This function initializes the random number generator fairly well.
  613. * It also only initializes it once, so you don't accidentally get
  614. * the same 'random' numbers twice in one session.
  615. *
  616. * @return void
  617. * @since 1.0
  618. */
  619. function sq_mt_randomize() {
  620. static $randomized;
  621. if ($randomized) {
  622. return;
  623. }
  624. /* Global. */
  625. sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER);
  626. sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER);
  627. sq_mt_seed((int)((double) microtime() * 1000000));
  628. sq_mt_seed(md5($remote_port . $remote_addr . getmypid()));
  629. /* getrusage */
  630. if (function_exists('getrusage')) {
  631. /* Avoid warnings with Win32 */
  632. $dat = @getrusage();
  633. if (isset($dat) && is_array($dat)) {
  634. $Str = '';
  635. foreach ($dat as $k => $v)
  636. {
  637. $Str .= $k . $v;
  638. }
  639. sq_mt_seed(md5($Str));
  640. }
  641. }
  642. if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) {
  643. sq_mt_seed(md5($unique_id));
  644. }
  645. $randomized = 1;
  646. }
  647. /**
  648. * Creates encryption key
  649. *
  650. * Creates an encryption key for encrypting the password stored in the cookie.
  651. * The encryption key itself is stored in the session.
  652. *
  653. * Pad must be longer or equal to encoded string length in 1.4.4/1.5.0 and older.
  654. * @param int $length optional, length of the string to generate
  655. * @return string the encryption key
  656. * @since 1.0
  657. */
  658. function OneTimePadCreate ($length=100) {
  659. sq_mt_randomize();
  660. $pad = '';
  661. for ($i = 0; $i < $length; $i++) {
  662. $pad .= chr(mt_rand(0,255));
  663. }
  664. return base64_encode($pad);
  665. }
  666. /**
  667. * Returns a string showing the size of the message/attachment.
  668. *
  669. * @param int $bytes the filesize in bytes
  670. * @return string the filesize in human readable format
  671. * @since 1.0
  672. */
  673. function show_readable_size($bytes) {
  674. $bytes /= 1024;
  675. $type = 'k';
  676. if ($bytes / 1024 > 1) {
  677. $bytes /= 1024;
  678. $type = 'M';
  679. }
  680. if ($bytes < 10) {
  681. $bytes *= 10;
  682. settype($bytes, 'integer');
  683. $bytes /= 10;
  684. } else {
  685. settype($bytes, 'integer');
  686. }
  687. return $bytes . '&nbsp;' . $type;
  688. }
  689. /**
  690. * Generates a random string from the character set you pass in
  691. *
  692. * @param int $size the length of the string to generate
  693. * @param string $chars a string containing the characters to use
  694. * @param int $flags a flag to add a specific set to the characters to use:
  695. * Flags:
  696. * 1 = add lowercase a-z to $chars
  697. * 2 = add uppercase A-Z to $chars
  698. * 4 = add numbers 0-9 to $chars
  699. * @return string the random string
  700. * @since 1.0
  701. */
  702. function GenerateRandomString($size, $chars, $flags = 0) {
  703. if ($flags & 0x1) {
  704. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  705. }
  706. if ($flags & 0x2) {
  707. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  708. }
  709. if ($flags & 0x4) {
  710. $chars .= '0123456789';
  711. }
  712. if (($size < 1) || (strlen($chars) < 1)) {
  713. return '';
  714. }
  715. sq_mt_randomize(); /* Initialize the random number generator */
  716. $String = '';
  717. $j = strlen( $chars ) - 1;
  718. while (strlen($String) < $size) {
  719. $String .= $chars{mt_rand(0, $j)};
  720. }
  721. return $String;
  722. }
  723. /**
  724. * Escapes special characters for use in IMAP commands.
  725. *
  726. * @param string $str the string to escape
  727. * @return string the escaped string
  728. * @since 1.0.3
  729. */
  730. function quoteimap($str) {
  731. return preg_replace("/([\"\\\\])/", "\\\\$1", $str);
  732. }
  733. /**
  734. * Trims array
  735. *
  736. * Trims every element in the array, ie. remove the first char of each element
  737. * @param array $array the array to trim
  738. * @since 1.2.0
  739. */
  740. function TrimArray(&$array) {
  741. foreach ($array as $k => $v) {
  742. global $$k;
  743. if (is_array($$k)) {
  744. foreach ($$k as $k2 => $v2) {
  745. $$k[$k2] = substr($v2, 1);
  746. }
  747. } else {
  748. $$k = substr($v, 1);
  749. }
  750. /* Re-assign back to array. */
  751. $array[$k] = $$k;
  752. }
  753. }
  754. /**
  755. * Create compose link
  756. *
  757. * Returns a link to the compose-page, taking in consideration
  758. * the compose_in_new and javascript settings.
  759. * @param string $url the URL to the compose page
  760. * @param string $text the link text, default "Compose"
  761. * @param string $target (since 1.4.3) url target
  762. * @return string a link to the compose page
  763. * @since 1.4.2
  764. */
  765. function makeComposeLink($url, $text = null, $target='') {
  766. global $compose_new_win,$javascript_on, $compose_width, $compose_height;
  767. if(!$text) {
  768. $text = _("Compose");
  769. }
  770. // if not using "compose in new window", make
  771. // regular link and be done with it
  772. if($compose_new_win != '1') {
  773. return makeInternalLink($url, $text, $target);
  774. }
  775. // build the compose in new window link...
  776. // if javascript is on, use onclick event to handle it
  777. if($javascript_on) {
  778. sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
  779. $compuri = $base_uri.$url;
  780. return "<a href=\"javascript:void(0)\" onclick=\"comp_in_new('$compuri','$compose_width','$compose_height')\">$text</a>";
  781. }
  782. // otherwise, just open new window using regular HTML
  783. return makeInternalLink($url, $text, '_blank');
  784. }
  785. /**
  786. * Print variable
  787. *
  788. * sm_print_r($some_variable, [$some_other_variable [, ...]]);
  789. *
  790. * Debugging function - does the same as print_r, but makes sure special
  791. * characters are converted to htmlentities first. This will allow
  792. * values like <some@email.address> to be displayed.
  793. * The output is wrapped in <<pre>> and <</pre>> tags.
  794. * Since 1.4.2 accepts unlimited number of arguments.
  795. * @since 1.4.1
  796. * @return void
  797. */
  798. function sm_print_r() {
  799. ob_start(); // Buffer output
  800. foreach(func_get_args() as $var) {
  801. print_r($var);
  802. echo "\n";
  803. // php has get_class_methods function that can print class methods
  804. if (is_object($var)) {
  805. // get class methods if $var is object
  806. $aMethods=get_class_methods(get_class($var));
  807. // make sure that $aMethods is array and array is not empty
  808. if (is_array($aMethods) && $aMethods!=array()) {
  809. echo "Object methods:\n";
  810. foreach($aMethods as $method) {
  811. echo '* ' . $method . "\n";
  812. }
  813. }
  814. echo "\n";
  815. }
  816. }
  817. $buffer = ob_get_contents(); // Grab the print_r output
  818. ob_end_clean(); // Silently discard the output & stop buffering
  819. print '<div align="left"><pre>';
  820. print htmlentities($buffer);
  821. print '</pre></div>';
  822. }
  823. /**
  824. * version of fwrite which checks for failure
  825. * @param resource $fp
  826. * @param string $string
  827. * @return number of written bytes. false on failure
  828. * @since 1.4.3
  829. */
  830. function sq_fwrite($fp, $string) {
  831. // write to file
  832. $count = @fwrite($fp,$string);
  833. // the number of bytes written should be the length of the string
  834. if($count != strlen($string)) {
  835. return FALSE;
  836. }
  837. return $count;
  838. }
  839. /**
  840. * sq_get_html_translation_table
  841. *
  842. * Returns the translation table used by sq_htmlentities()
  843. *
  844. * @param integer $table html translation table. Possible values (without quotes):
  845. * <ul>
  846. * <li>HTML_ENTITIES - full html entities table defined by charset</li>
  847. * <li>HTML_SPECIALCHARS - html special characters table</li>
  848. * </ul>
  849. * @param integer $quote_style quote encoding style. Possible values (without quotes):
  850. * <ul>
  851. * <li>ENT_COMPAT - (default) encode double quotes</li>
  852. * <li>ENT_NOQUOTES - don't encode double or single quotes</li>
  853. * <li>ENT_QUOTES - encode double and single quotes</li>
  854. * </ul>
  855. * @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value.
  856. * @return array html translation array
  857. * @since 1.5.1
  858. */
  859. function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='us-ascii') {
  860. global $default_charset;
  861. if ($table == HTML_SPECIALCHARS) $charset='us-ascii';
  862. // Start array with ampersand
  863. $sq_html_ent_table = array( "&" => '&amp;' );
  864. // < and >
  865. $sq_html_ent_table = array_merge($sq_html_ent_table,
  866. array("<" => '&lt;',
  867. ">" => '&gt;')
  868. );
  869. // double quotes
  870. if ($quote_style == ENT_COMPAT)
  871. $sq_html_ent_table = array_merge($sq_html_ent_table,
  872. array("\"" => '&quot;')
  873. );
  874. // double and single quotes
  875. if ($quote_style == ENT_QUOTES)
  876. $sq_html_ent_table = array_merge($sq_html_ent_table,
  877. array("\"" => '&quot;',
  878. "'" => '&#39;')
  879. );
  880. if ($charset=='auto') $charset=$default_charset;
  881. // add entities that depend on charset
  882. switch($charset){
  883. case 'iso-8859-1':
  884. include_once(SM_PATH . 'functions/htmlentities/iso-8859-1.php');
  885. break;
  886. case 'utf-8':
  887. include_once(SM_PATH . 'functions/htmlentities/utf-8.php');
  888. break;
  889. case 'us-ascii':
  890. default:
  891. break;
  892. }
  893. // return table
  894. return $sq_html_ent_table;
  895. }
  896. /**
  897. * sq_htmlentities
  898. *
  899. * Convert all applicable characters to HTML entities.
  900. * Minimal php requirement - v.4.0.5.
  901. *
  902. * Function is designed for people that want to use full power of htmlentities() in
  903. * i18n environment.
  904. *
  905. * @param string $string string that has to be sanitized
  906. * @param integer $quote_style quote encoding style. Possible values (without quotes):
  907. * <ul>
  908. * <li>ENT_COMPAT - (default) encode double quotes</li>
  909. * <li>ENT_NOQUOTES - don't encode double or single quotes</li>
  910. * <li>ENT_QUOTES - encode double and single quotes</li>
  911. * </ul>
  912. * @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value.
  913. * @return string sanitized string
  914. * @since 1.5.1
  915. */
  916. function sq_htmlentities($string,$quote_style=ENT_COMPAT,$charset='us-ascii') {
  917. // get translation table
  918. $sq_html_ent_table=sq_get_html_translation_table(HTML_ENTITIES,$quote_style,$charset);
  919. // convert characters
  920. return str_replace(array_keys($sq_html_ent_table),array_values($sq_html_ent_table),$string);
  921. }
  922. /**
  923. * Tests if string contains 8bit symbols.
  924. *
  925. * If charset is not set, function defaults to default_charset.
  926. * $default_charset global must be set correctly if $charset is
  927. * not used.
  928. * @param string $string tested string
  929. * @param string $charset charset used in a string
  930. * @return bool true if 8bit symbols are detected
  931. * @since 1.5.1 and 1.4.4
  932. */
  933. function sq_is8bit($string,$charset='') {
  934. global $default_charset;
  935. if ($charset=='') $charset=$default_charset;
  936. /**
  937. * Don't use \240 in ranges. Sometimes RH 7.2 doesn't like it.
  938. * Don't use \200-\237 for iso-8859-x charsets. This range
  939. * stores control symbols in those charsets.
  940. * Use preg_match instead of ereg in order to avoid problems
  941. * with mbstring overloading
  942. */
  943. if (preg_match("/^iso-8859/i",$charset)) {
  944. $needle='/\240|[\241-\377]/';
  945. } else {
  946. $needle='/[\200-\237]|\240|[\241-\377]/';
  947. }
  948. return preg_match("$needle",$string);
  949. }
  950. /**
  951. * Replacement of mb_list_encodings function
  952. *
  953. * This function provides replacement for function that is available only
  954. * in php 5.x. Function does not test all mbstring encodings. Only the ones
  955. * that might be used in SM translations.
  956. *
  957. * Supported strings are stored in session in order to reduce number of
  958. * mb_internal_encoding function calls.
  959. *
  960. * If you want to test all mbstring encodings - fill $list_of_encodings
  961. * array.
  962. * @return array list of encodings supported by php mbstring extension
  963. * @since 1.5.1 and 1.4.6
  964. */
  965. function sq_mb_list_encodings() {
  966. if (! function_exists('mb_internal_encoding'))
  967. return array();
  968. // php 5+ function
  969. if (function_exists('mb_list_encodings')) {
  970. $ret = mb_list_encodings();
  971. array_walk($ret,'sq_lowercase_array_vals');
  972. return $ret;
  973. }
  974. // don't try to test encodings, if they are already stored in session
  975. if (sqgetGlobalVar('mb_supported_encodings',$mb_supported_encodings,SQ_SESSION))
  976. return $mb_supported_encodings;
  977. // save original encoding
  978. $orig_encoding=mb_internal_encoding();
  979. $list_of_encoding=array(
  980. 'pass',
  981. 'auto',
  982. 'ascii',
  983. 'jis',
  984. 'utf-8',
  985. 'sjis',
  986. 'euc-jp',
  987. 'iso-8859-1',
  988. 'iso-8859-2',
  989. 'iso-8859-7',
  990. 'iso-8859-9',
  991. 'iso-8859-15',
  992. 'koi8-r',
  993. 'koi8-u',
  994. 'big5',
  995. 'gb2312',
  996. 'gb18030',
  997. 'windows-1251',
  998. 'windows-1255',
  999. 'windows-1256',
  1000. 'tis-620',
  1001. 'iso-2022-jp',
  1002. 'euc-cn',
  1003. 'euc-kr',
  1004. 'euc-tw',
  1005. 'uhc',
  1006. 'utf7-imap');
  1007. $supported_encodings=array();
  1008. foreach ($list_of_encoding as $encoding) {
  1009. // try setting encodings. suppress warning messages
  1010. if (@mb_internal_encoding($encoding))
  1011. $supported_encodings[]=$encoding;
  1012. }
  1013. // restore original encoding
  1014. mb_internal_encoding($orig_encoding);
  1015. // register list in session
  1016. sqsession_register($supported_encodings,'mb_supported_encodings');
  1017. return $supported_encodings;
  1018. }
  1019. /**
  1020. * Callback function used to lowercase array values.
  1021. * @param string $val array value
  1022. * @param mixed $key array key
  1023. * @since 1.5.1 and 1.4.6
  1024. */
  1025. function sq_lowercase_array_vals(&$val,$key) {
  1026. $val = strtolower($val);
  1027. }
  1028. /**
  1029. * Function returns number of characters in string.
  1030. *
  1031. * Returned number might be different from number of bytes in string,
  1032. * if $charset is multibyte charset. Detection depends on mbstring
  1033. * functions. If mbstring does not support tested multibyte charset,
  1034. * vanilla string length function is used.
  1035. * @param string $str string
  1036. * @param string $charset charset
  1037. * @since 1.5.1 and 1.4.6
  1038. * @return integer number of characters in string
  1039. */
  1040. function sq_strlen($str, $charset=null){
  1041. // default option
  1042. if (is_null($charset)) return strlen($str);
  1043. // lowercase charset name
  1044. $charset=strtolower($charset);
  1045. // use automatic charset detection, if function call asks for it
  1046. if ($charset=='auto') {
  1047. global $default_charset, $squirrelmail_language;
  1048. set_my_charset();
  1049. $charset=$default_charset;
  1050. if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
  1051. }
  1052. // Use mbstring only with listed charsets
  1053. $aList_of_mb_charsets=array('utf-8','big5','gb2312','gb18030','euc-jp','euc-cn','euc-tw','euc-kr');
  1054. // calculate string length according to charset
  1055. if (in_array($charset,$aList_of_mb_charsets) && in_array($charset,sq_mb_list_encodings())) {
  1056. $real_length = mb_strlen($str,$charset);
  1057. } else {
  1058. // own strlen detection code is removed because missing strpos,
  1059. // strtoupper and substr implementations break string wrapping.
  1060. $real_length=strlen($str);
  1061. }
  1062. return $real_length;
  1063. }
  1064. /**
  1065. * string padding with multibyte support
  1066. *
  1067. * @link http://www.php.net/str_pad
  1068. * @param string $string original string
  1069. * @param integer $width padded string width
  1070. * @param string $pad padding symbols
  1071. * @param integer $padtype padding type
  1072. * (internal php defines, see str_pad() description)
  1073. * @param string $charset charset used in original string
  1074. * @return string padded string
  1075. */
  1076. function sq_str_pad($string, $width, $pad, $padtype, $charset='') {
  1077. $charset = strtolower($charset);
  1078. $padded_string = '';
  1079. switch ($charset) {
  1080. case 'utf-8':
  1081. case 'big5':
  1082. case 'gb2312':
  1083. case 'euc-kr':
  1084. /*
  1085. * all multibyte charsets try to increase width value by
  1086. * adding difference between number of bytes and real length
  1087. */
  1088. $width = $width - sq_strlen($string,$charset) + strlen($string);
  1089. default:
  1090. $padded_string=str_pad($string,$width,$pad,$padtype);
  1091. }
  1092. return $padded_string;
  1093. }
  1094. /**
  1095. * Wrapper that is used to switch between vanilla and multibyte substr
  1096. * functions.
  1097. * @param string $string
  1098. * @param integer $start
  1099. * @param integer $length
  1100. * @param string $charset
  1101. * @return string
  1102. * @since 1.5.1
  1103. * @link http://www.php.net/substr
  1104. * @link http://www.php.net/mb_substr
  1105. */
  1106. function sq_substr($string,$start,$length,$charset='auto') {
  1107. // use automatic charset detection, if function call asks for it
  1108. if ($charset=='auto') {
  1109. global $default_charset, $squirrelmail_language;
  1110. set_my_charset();
  1111. $charset=$default_charset;
  1112. if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
  1113. }
  1114. $charset = strtolower($charset);
  1115. if (function_exists('mb_internal_encoding') &&
  1116. in_array($charset,sq_mb_list_encodings())) {
  1117. return mb_substr($string,$start,$length,$charset);
  1118. }
  1119. // TODO: add mbstring independent code
  1120. // use vanilla string functions as last option
  1121. return substr($string,$start,$length);
  1122. }
  1123. /**
  1124. * Wrapper that is used to switch between vanilla and multibyte strpos
  1125. * functions.
  1126. * @param string $haystack
  1127. * @param mixed $needle
  1128. * @param integer $offset
  1129. * @param string $charset
  1130. * @return string
  1131. * @since 1.5.1
  1132. * @link http://www.php.net/strpos
  1133. * @link http://www.php.net/mb_strpos
  1134. */
  1135. function sq_strpos($haystack,$needle,$offset,$charset='auto') {
  1136. // use automatic charset detection, if function call asks for it
  1137. if ($charset=='auto') {
  1138. global $default_charset, $squirrelmail_language;
  1139. set_my_charset();
  1140. $charset=$default_charset;
  1141. if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
  1142. }
  1143. $charset = strtolower($charset);
  1144. if (function_exists('mb_internal_encoding') &&
  1145. in_array($charset,sq_mb_list_encodings())) {
  1146. return mb_strpos($haystack,$needle,$offset,$charset);
  1147. }
  1148. // TODO: add mbstring independent code
  1149. // use vanilla string functions as last option
  1150. return strpos($haystack,$needle,$offset);
  1151. }
  1152. /**
  1153. * Wrapper that is used to switch between vanilla and multibyte strtoupper
  1154. * functions.
  1155. * @param string $string
  1156. * @param string $charset
  1157. * @return string
  1158. * @since 1.5.1
  1159. * @link http://www.php.net/strtoupper
  1160. * @link http://www.php.net/mb_strtoupper
  1161. */
  1162. function sq_strtoupper($string,$charset='auto') {
  1163. // use automatic charset detection, if function call asks for it
  1164. if ($charset=='auto') {
  1165. global $default_charset,$squirrelmail_language;
  1166. set_my_charset();
  1167. $charset=$default_charset;
  1168. if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
  1169. }
  1170. $charset = strtolower($charset);
  1171. if (function_exists('mb_strtoupper') &&
  1172. in_array($charset,sq_mb_list_encodings())) {
  1173. return mb_strtoupper($string,$charset);
  1174. }
  1175. // TODO: add mbstring independent code
  1176. // use vanilla string functions as last option
  1177. return strtoupper($string);
  1178. }
  1179. /**
  1180. * Counts 8bit bytes in string
  1181. * @param string $string tested string
  1182. * @return integer number of 8bit bytes
  1183. */
  1184. function sq_count8bit($string) {
  1185. $count=0;
  1186. for ($i=0; $i<strlen($string); $i++) {
  1187. if (ord($string[$i]) > 127) $count++;
  1188. }
  1189. return $count;
  1190. }
  1191. ?>