From: lakostis@mozilla.ru
To: cvs-commits@
Subject: [cvs commit] mozilla.ru/wiki/inc/classes
X-CVSROOT: /projects
Date: Thu,  8 Mar 2007 03:10:52 +0300 (MSK)
X-AV-Checked: ClamAV using ClamSMTP
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=koi8-r
Status: RO

Update of /projects/mozilla.ru/wiki/inc/classes
Update of /projects/mozilla.ru/wiki/install/tables

Modified Files:
	mail_helper.class.php utf8_helper.class.php comment.sql
Log Message:

  + fix mail_helper class (make it http://www.rfc-editor.org/rfc/rfc2047.txt compiliant).
  + fix comment table:
    + name is changed to char(50)
    + ip is changed to varchar(255) for cases where user have more than one ip.



Changed file: mail_helper.class.php, diff is included (3009 bytes)
Changed file: utf8_helper.class.php, diff is included (1929 bytes)
Changed file: comment.sql, diff is included (626 bytes)

Index: mozilla.ru/wiki/inc/classes/mail_helper.class.php
diff -u mozilla.ru/wiki/inc/classes/mail_helper.class.php:1.1.1.1 mozilla.ru/wiki/inc/classes/mail_helper.class.php:1.2
--- mozilla.ru/wiki/inc/classes/mail_helper.class.php:1.1.1.1	Fri Feb 16 00:03:40 2007
+++ mozilla.ru/wiki/inc/classes/mail_helper.class.php	Thu Mar  8 03:10:51 2007
@@ -27,6 +27,68 @@
 class mail_helper
 {
   /**
+   * \brief This does exactly what preg_quote() does in PHP 4-ish
+   *
+   * \param $str string for quote
+   * \delimiter quote delimiter
+   *
+   * \return encoded text if success
+   */
+   function phpbb_preg_quote($str, $delimiter)
+   {
+   
+    $text = preg_quote($str);
+    $text = str_replace($delimiter, '\\' . $delimiter, $text);
+    return $text;
+   }
+
+  /**
+   * \brief Encodes the given string for proper mail send
+   *
+   * \param str string for enconding
+   * \charset charset for encoding
+   *
+   * \return encoded str if sucess or original string if failure
+   */
+   function encode($str,$charset)
+   {
+		   if (($charset == 'ISO-8859-1') || (!$charset))
+		   {
+				   return $str;
+		   }
+
+		   if (preg_match('/[^\x20-\x7E]/', $str)) {
+				   $chunk_size = 47; /* floor((75 - strlen("=?UTF-8?B??=")) * 0.75); */
+				   $len = strlen($str);
+				   $output = '';
+				   while ($len > 0) {
+						   $chunk = utf8_helper::truncate($str, $chunk_size);
+						   $output .= ' =?UTF-8?B?'. base64_encode($chunk) ."?=\n";
+						   $c = strlen($chunk);
+						   $str = substr($str, $c);
+						   $len -= $c;
+				   }
+				   return trim($output);
+		   } else {
+				   /* define start delimimter, end delimiter and spacer */
+				   $end = "?=";
+				   $start = "=?$charset?B?";
+				   $spacer = "$end\r\n $start";
+
+				   /* determine length of encoded text within chunks and ensure length is even */
+				   $length = 75 - strlen($start) - strlen($end);
+				   $length = floor($length / 2) * 2;
+
+				   /* encode the string and split it into chunks with spacers after each chunk */
+				   $str = chunk_split(base64_encode($str), $length, $spacer);
+
+				   /* remove trailing spacer and add start and end delimiters */
+				   $str = preg_replace('#' . mail_helper::phpbb_preg_quote($spacer, '#') . '$#', '', $str);
+		   }
+		   return $start . $str . $end;
+   }
+
+  /**
    * \brief sends an email
    *
    * \param from from address
@@ -39,13 +101,21 @@
    */
   function send($from, $to, $charset, $subject, $message)
   {
+
+    /* Do a little spring cleaning
+     * idea stolen from PunBB */
+    $to = trim(preg_replace('#[\n\r]+#s', '', $to));
+    $subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
+    $from = trim(preg_replace('#[\n\r:]+#s', '', $from));
+
     $headers =
     'From: '.$from."\r\n".
     'Content-Type: text/plain;'."\r\n".
     "\t".'charset='.$charset."\r\n";
+    $subject = mail_helper::encode($subject,$charset);
 
     return @mail($to, $subject, $message, $headers);
   }
 }
 
-?>
\ No newline at end of file
+?>
Index: mozilla.ru/wiki/inc/classes/utf8_helper.class.php
diff -u mozilla.ru/wiki/inc/classes/utf8_helper.class.php:1.1.1.1 mozilla.ru/wiki/inc/classes/utf8_helper.class.php:1.2
--- mozilla.ru/wiki/inc/classes/utf8_helper.class.php:1.1.1.1	Fri Feb 16 00:03:40 2007
+++ mozilla.ru/wiki/inc/classes/utf8_helper.class.php	Thu Mar  8 03:10:51 2007
@@ -115,6 +115,50 @@
 
     return str_replace(array_keys($conv), array_values($conv), $str);
   }
+
+  /** 
+   * \brief Truncate a UTF-8-encoded string safely to a number of bytes. 
+   * 
+   * If the end position is in the middle of a UTF-8 sequence, it scans backwards 
+   * until the beginning of the byte sequence. 
+   * 
+   * Use this function whenever you want to chop off a string at an unsure 
+   * location. On the other hand, if you're sure that you're splitting on a 
+   * character boundary (e.g. after using strpos() or similar), you can safely use 
+   * substr() instead. 
+   *
+   * code taken from Drupal CMS (www.drupal.org)
+   * 
+   * \param $string 
+   *   The string to truncate. 
+   * \param $len 
+   *   An upper limit on the returned string length. 
+   * \param $wordsafe 
+   *   Flag to truncate at nearest space. Defaults to FALSE. 
+   * 
+   * \return 
+   *   The truncated string. 
+   */ 
+  function truncate($string, $len, $wordsafe = FALSE, $dots = FALSE) 
+  {
+    $slen = strlen($string); 
+    if ($slen <= $len) { 
+  		  return $string; 
+    } 
+    if ($wordsafe) { 
+  		  $end = $len; 
+  		  while (($string[--$len] != ' ') && ($len > 0)) {}; 
+  		  if ($len == 0) { 
+  				  $len = $end; 
+  		  } 
+    } 
+    if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) { 
+  		  return substr($string, 0, $len) . ($dots ? ' ...' : ''); 
+    } 
+    while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {}; 
+    return substr($string, 0, $len) . ($dots ? ' ...' : ''); 
+  } 
+
 }
 
 ?>

Index: mozilla.ru/wiki/install/tables/comment.sql
diff -u mozilla.ru/wiki/install/tables/comment.sql:1.1.1.1 mozilla.ru/wiki/install/tables/comment.sql:1.2
--- mozilla.ru/wiki/install/tables/comment.sql:1.1.1.1	Fri Feb 16 00:03:41 2007
+++ mozilla.ru/wiki/install/tables/comment.sql	Thu Mar  8 03:10:51 2007
@@ -4,9 +4,9 @@
   title              char(100) NOT NULL,
   content            LONGTEXT,
   timestamp          int(8),
-  ip                 char(15),
+  ip                 varchar(255),
 
-  name               char(20),
+  name               char(50),
   email              char(50),
 
   content_fk         int(6),

