Moved $imap_error_titles into sqimap_asearch_error_box because that's the only place...
[squirrelmail.git] / functions / strings.php
index 2282bd782266bd42d0139f1fd619db4f58fcb517..6db1afa8364b9f5b67cc82734d9fa168535a337e 100644 (file)
@@ -3,13 +3,13 @@
 /**
  * strings.php
  *
- * Copyright (c) 1999-2003 The SquirrelMail Project Team
+ * Copyright (c) 1999-2004 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This code provides various string manipulation functions that are
  * used by the rest of the Squirrelmail code.
  *
- * $Id$
+ * @version $Id$
  * @package squirrelmail
  */
 
  * SquirrelMail version number -- DO NOT CHANGE
  */
 global $version;
-$version = '1.5.0';
+$version = '1.5.1 [CVS]';
 
 /**
  * SquirrelMail internal version number -- DO NOT CHANGE
  * $sm_internal_version = array (release, major, minor)
  */
 global $SQM_INTERNAL_VERSION;
-$SQM_INTERNAL_VERSION = array(1,5,0);
+$SQM_INTERNAL_VERSION = array(1,5,1);
 
 /**
  * There can be a circular issue with includes, where the $version string is
@@ -161,6 +161,8 @@ function readShortMailboxName($haystack, $needle) {
 }
 
 /**
+ * php_self
+ *
  * Creates an URL for the page calling this function, using either the PHP global
  * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added.
  *
@@ -187,6 +189,8 @@ function php_self () {
 
 
 /**
+ * get_location
+ *
  * Determines the location to forward to, relative to your server.
  * This is used in HTTP Location: redirects.
  * If this doesnt work correctly for you (although it should), you can
@@ -261,6 +265,8 @@ function get_location () {
 
 
 /**
+ * Encrypts password
+ *
  * These functions are used to encrypt the password before it is
  * stored in a cookie. The encryption key is generated by
  * OneTimePadCreate();
@@ -280,7 +286,9 @@ function OneTimePadEncrypt ($string, $epad) {
 }
 
 /**
- * Decrypt a password from the cookie, encrypted by OneTimePadEncrypt.
+ * Decrypts a password from the cookie
+ *
+ * Decrypts a password from the cookie, encrypted by OneTimePadEncrypt.
  * This uses the encryption key that is stored in the session.
  *
  * @param string string the string to decrypt
@@ -300,9 +308,11 @@ function OneTimePadDecrypt ($string, $epad) {
 
 
 /**
- * Randomize the mt_rand() function.  Toss this in strings or integers
- * and it will seed the generator appropriately. With strings, it is
- * better to get them long. Use md5() to lengthen smaller strings.
+ * Randomizes the mt_rand() function.
+ *
+ * Toss this in strings or integers and it will seed the generator 
+ * appropriately. With strings, it is better to get them long. 
+ * Use md5() to lengthen smaller strings.
  *
  * @param mixed val a value to seed the random number generator
  * @return void
@@ -329,6 +339,8 @@ function sq_mt_seed($Val) {
 
 
 /**
+ * Init random number generator
+ *
  * This function initializes the random number generator fairly well.
  * It also only initializes it once, so you don't accidentally get
  * the same 'random' numbers twice in one session.
@@ -370,6 +382,8 @@ function sq_mt_randomize() {
 }
 
 /**
+ * Creates encryption key
+ *
  * Creates an encryption key for encrypting the password stored in the cookie.
  * The encryption key itself is stored in the session.
  *
@@ -425,7 +439,6 @@ function show_readable_size($bytes) {
  *       4 = add numbers 0-9 to $chars
  * @return string the random string
  */
-
 function GenerateRandomString($size, $chars, $flags = 0) {
     if ($flags & 0x1) {
         $chars .= 'abcdefghijklmnopqrstuvwxyz';
@@ -454,14 +467,17 @@ function GenerateRandomString($size, $chars, $flags = 0) {
 
 /**
  * Escapes special characters for use in IMAP commands.
+ *
  * @param string the string to escape
  * @return string the escaped string
  */
 function quoteimap($str) {
-    return ereg_replace('(["\\])', '\\\\1', $str);
+    return preg_replace("/([\"\\\\])/", "\\\\$1", $str);
 }
 
 /**
+ * Trims array
+ *
  * Trims every element in the array, ie. remove the first char of each element
  * @param array array the array to trim
  */
@@ -482,6 +498,8 @@ function TrimArray(&$array) {
 }
 
 /**
+ * Create compose link
+ *
  * Returns a link to the compose-page, taking in consideration
  * the compose_in_new and javascript settings.
  * @param string url the URL to the compose page
@@ -520,11 +538,14 @@ function makeComposeLink($url, $text = null, $target='')
 }
 
 /**
+ * Print variable
+ *
  * sm_print_r($some_variable, [$some_other_variable [, ...]]);
+ *
  * Debugging function - does the same as print_r, but makes sure special
  * characters are converted to htmlentities first.  This will allow
  * values like <some@email.address> to be displayed.
- * The output is wrapped in <pre> and </pre> tags.
+ * The output is wrapped in <<pre>> and <</pre>> tags.
  *
  * @return void
  */
@@ -541,5 +562,105 @@ function sm_print_r() {
     print '</pre>';
 }
 
+/**
+ * version of fwrite which checks for failure
+ */
+function sq_fwrite($fp, $string) {
+       // write to file
+       $count = @fwrite($fp,$string);
+       // the number of bytes written should be the length of the string
+       if($count != strlen($string)) {
+               return FALSE;
+       }
+
+       return $count;
+}
+
+/**
+ * sq_get_html_translation_table
+ *
+ * Returns the translation table used by sq_htmlentities()
+ *
+ * @param integer $table html translation table. Possible values (without quotes):
+ *             <ul>
+ *                <li>HTML_ENTITIES - full html entities table defined by charset</li>
+ *                <li>HTML_SPECIALCHARS - html special characters table</li>
+ *             </ul>
+ * @param integer $quote_style quote encoding style. Possible values (without quotes):
+ *             <ul>
+ *                <li>ENT_COMPAT - (default) encode double quotes</li>
+ *                <li>ENT_NOQUOTES -  don't encode double or single quotes</li>
+ *                <li>ENT_QUOTES - encode double and single quotes</li>
+ *             </ul>
+ * @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value.
+ * @return array html translation array
+ */
+function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='us-ascii') {
+  global $default_charset;
+
+  if ($table == HTML_SPECIALCHARS) $charset='us-ascii';
+
+  // Start array with ampersand
+  $sq_html_ent_table = array( "&" => '&amp;' );
+
+  // < and >
+  $sq_html_ent_table = array_merge($sq_html_ent_table,
+                       array("<" => '&lt;',
+                             ">" => '&gt;')
+                       );
+  // double quotes
+  if ($quote_style == ENT_COMPAT)
+     $sq_html_ent_table = array_merge($sq_html_ent_table,
+                           array("\"" => '&quot;')
+                           );
+
+  // double and single quotes
+  if ($quote_style == ENT_QUOTES)
+     $sq_html_ent_table = array_merge($sq_html_ent_table,
+                           array("\"" => '&quot;',
+                             "'" => '&#39;')
+                           );
+
+  if ($charset=='auto') $charset=$default_charset;
+
+  // add entities that depend on charset
+  switch($charset){
+  case 'iso-8859-1':
+    include_once(SM_PATH . 'functions/htmlentities/iso-8859-1.php');
+    break;
+  case 'utf-8':
+    include_once(SM_PATH . 'functions/htmlentities/utf-8.php');
+    break;
+  case 'us-ascii':
+  default:
+    break;
+  }
+  // return table
+  return $sq_html_ent_table;
+}
+
+/**
+ * sq_htmlentities
+ *
+ * Convert all applicable characters to HTML entities.
+ * Minimal php requirement - v.4.0.5
+ *
+ * @param string $string string that has to be sanitized
+ * @param integer $quote_style quote encoding style. Possible values (without quotes):
+ *             <ul>
+ *                <li>ENT_COMPAT - (default) encode double quotes</li>
+ *                <li>ENT_NOQUOTES - don't encode double or single quotes</li>
+ *                <li>ENT_QUOTES - encode double and single quotes</li>
+ *             </ul>
+ * @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value.
+ * @return string sanitized string
+ */
+function sq_htmlentities($string,$quote_style=ENT_COMPAT,$charset='us-ascii') {
+  // get translation table
+  $sq_html_ent_table=sq_get_html_translation_table(HTML_ENTITIES,$quote_style,$charset);
+  // convert characters
+  return str_replace(array_keys($sq_html_ent_table),array_values($sq_html_ent_table),$string);
+}
+
 $PHP_SELF = php_self();
-?>
+?>
\ No newline at end of file