Moved $imap_error_titles into sqimap_asearch_error_box because that's the only place...
[squirrelmail.git] / functions / strings.php
index 4ff54b898bfeb95e145ed5deb259da559bf41aa0..6db1afa8364b9f5b67cc82734d9fa168535a337e 100644 (file)
@@ -9,7 +9,7 @@
  * This code provides various string manipulation functions that are
  * used by the rest of the Squirrelmail code.
  *
- * $Id$
+ * @version $Id$
  * @package squirrelmail
  */
 
@@ -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,6 +467,7 @@ 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
  */
@@ -462,6 +476,8 @@ function quoteimap($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
  */
@@ -555,7 +576,91 @@ function sq_fwrite($fp, $string) {
        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