OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / NOTIFICATION.php
index 1d708d1..e7f3d38 100644 (file)
@@ -1,3 +1,404 @@
+<<<<<<< HEAD
+<?php\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2002-2012 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+/**\r
+ * Class used to represent a collection of e-mail addresses, to which a\r
+ * message can be sent (e.g. comment or karma vote notification).\r
+ *\r
+ * @license http://nucleuscms.org/license.txt GNU General Public License\r
+ * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
+ * @version $Id: NOTIFICATION.php 1534 2011-06-22 06:13:23Z sakamocchi $\r
+ */\r
+class Notification\r
+{\r
+       static private $charset;\r
+       static private $scheme = 'B';\r
+       \r
+       /**\r
+        * NOTIFICATION::address_validation()\r
+        * Validating the string as address\r
+        * \r
+        * FIXME: this is just migrated from globalfunctions.php\r
+        *  we should confirm this regular expression refering to RFC 5322\r
+        * \r
+        * @link        http://www.ietf.org/rfc/rfc5322.txt\r
+        * @see         3.4. Address Specification\r
+        * \r
+        * @static\r
+        * @param       String  $address        Address\r
+        * @return      Boolean valid or not\r
+        */\r
+       static public function address_validation($address)\r
+       {\r
+               return (boolean) preg_match('#^(?!\\.)(?:\\.?[-a-zA-Z0-9!\\#$%&\'*+/=?^_`{|}~]+)+@(?!\\.)(?:\\.?(?!-)[-a-zA-Z0-9]+(?<!-)){2,}$#', $address);\r
+       }\r
+       \r
+       /**\r
+        * NOTIFICATION::get_mail_footer()\r
+        * Return mail footer with Nucleus CMS singnature\r
+        * \r
+        * @static\r
+        * @param       void\r
+        * @return      String  Message body with \r
+        */\r
+       static public function get_mail_footer()\r
+       {\r
+               $message  = "\n";\r
+               $message .= "\n";\r
+               $message .= "-----------------------------\n";\r
+               $message .=  "   Powered by Nucleus CMS\n";\r
+               $message .=  "(http://www.nucleuscms.org/)\n";\r
+               return $message;\r
+       }\r
+       \r
+       /**\r
+        * NOTIFICATION::mail\r
+        * Send mails with headers including 7bit-encoded multibyte string\r
+        * \r
+        * @static\r
+        * @param       string  $to             receivers including singlebyte and multibyte strings, based on RFC 5322\r
+        * @param       string  $subject        subject including singlebyte and multibyte strings\r
+        * @param       string  $message        message including singlebyte and multibyte strings\r
+        * @param       string  $from           senders including singlebyte and multibyte strings, based on RFC 5322\r
+        * @param       string(B/Q)     $scheme 7bit-encoder scheme based on RFC 2047\r
+        * @return      boolean accepted delivery or not\r
+        */\r
+       static public function mail($to, $subject, $message, $from, $charset, $scheme='B')\r
+       {\r
+               self::$charset = $charset;\r
+               self::$scheme = $scheme;\r
+               \r
+               $to = self::mailbox_list_encoder($to);\r
+               $subject = self::seven_bit_characters_encoder($subject);\r
+               $from = 'From: ' . self::mailbox_list_encoder($from);\r
+               \r
+               /*\r
+                * All of 7bit character encoding derives from ISO/IEC 646\r
+                * So we can decide the body's encoding bit count by this regular expression.\r
+                * \r
+                */\r
+               $bitcount = '8bit';\r
+               if ( preg_match('#\A[\x00-\x7f]*\z#', $message) )\r
+               {\r
+                       $bitcount = '7bit';\r
+               }\r
+               \r
+               $header  = 'Content-Type: text/html; charset=' . self::$charset . "; format=flowed; delsp=yes\n";\r
+               $header .= "Content-Transfer-Encoding: {$bitcount}\n";\r
+               $header .= "X-Mailer: Nucleus CMS NOTIFICATION class\n";\r
+               \r
+               return mail($to, $subject, $message, "{$from}\n{$header}");\r
+       }\r
+       \r
+       /**\r
+        * NOTIFICATION::mailbox_list_encoder\r
+        * Encode multi byte strings included in mailbox.\r
+        * The format of mailbox is based on RFC 5322, which obsoletes RFC 2822\r
+        * \r
+        * @link        http://www.ietf.org/rfc/rfc5322.txt\r
+        * @see         3.4. Address Specification\r
+        * \r
+        * @static\r
+        * @param       string  $mailbox_list           mailbox list\r
+        * @return      string  encoded string  \r
+        * \r
+        */\r
+       static private function mailbox_list_encoder ($mailbox_list)\r
+       {\r
+               $encoded_mailboxes = array();\r
+               $mailboxes = preg_split('#,#', $mailbox_list);\r
+               foreach ( $mailboxes as $mailbox )\r
+               {\r
+                       if ( preg_match("#^([^,]+)?<([^,]+)?@([^,]+)?>$#", $mailbox, $match) )\r
+                       {\r
+                               $display_name = self::seven_bit_characters_encoder(trim($match[1]));\r
+                               $local_part = trim($match[2]);\r
+                               $domain = trim($match[3]);\r
+                               $encoded_mailboxes[] = "{$display_name} <{$local_part}@{$domain}>";\r
+                       }\r
+                       else if ( preg_match("#([^,]+)?@([^,]+)?#", $mailbox) )\r
+                       {\r
+                               $encoded_mailboxes[] = $mailbox;\r
+                       }\r
+                       else\r
+                       {\r
+                               continue;\r
+                       }\r
+               }\r
+               if ( $encoded_mailboxes == array() )\r
+               {\r
+                       return FALSE;\r
+               }\r
+               return implode(',', $encoded_mailboxes);\r
+       }\r
+       \r
+       /**\r
+        * NOTIFICATION::seven_bit_characters_encoder\r
+        * Encoder into 7bit ASCII expression for Non-ASCII Text based on RFC 2047.\r
+        * \r
+        * @link http://www.ietf.org/rfc/rfc2047.txt\r
+        * @see 2. Syntax of encoded-words\r
+        * \r
+        * NOTE: RFC 2047 has a ambiguousity for dealing with 'linear-white-space'.\r
+        *  This causes a trouble related to line breaking between single byte and multi-byte strings.\r
+        *  To avoid this, single byte string is encoded as well as multi byte string here.\r
+        * \r
+        * NOTE: RFC 2231 also defines the way to use non-ASCII characters in MIME header.\r
+        * http://www.ietf.org/rfc/rfc2231.txt\r
+        * \r
+        * NOTE: iconv extension give the same functions as this in PHP5\r
+        * iconv_mime_encode():\r
+        * http://www.php.net/manual/en/function.iconv-mime-encode.php\r
+        * \r
+        * @static\r
+        * @param       string  $charset        Character set encoding\r
+        * @param       string  $type   type of 7 bit encoding, should be 'B' or 'Q'\r
+        * @param       string  $string Target string with header field\r
+        * @return      string  encoded string\r
+        * \r
+        */\r
+       static private function seven_bit_characters_encoder($string)\r
+       {\r
+               $header = chr(13) . chr(10) . chr(32) . '=?' . self::$charset . '?' . self::$scheme . '?';\r
+               $footer = "?=";\r
+               $restriction = 78 - strlen($header) - strlen($footer) ;\r
+               \r
+               $encoded_words = array();\r
+               for ( $i = 0; $i < i18n::strlen($string); $i++ )\r
+               {\r
+                       if ( self::$scheme == 'B' )\r
+                       {\r
+                               if ( $i == 0 )\r
+                               {\r
+                                       $letters = '';\r
+                               }\r
+                               \r
+                               $letter = i18n::substr($string, $i, 1);\r
+                               $expected_length = strlen($letters) + strlen($letter) * 4 / 3;\r
+                               \r
+                               if ( $expected_length > $restriction )\r
+                               {\r
+                                       $encoded_text = self::b_encoder($letters);\r
+                                       $encoded_words[] = "{$header}{$encoded_text}{$footer}";\r
+                                       $letters = '';\r
+                               }\r
+                               \r
+                               $letters .= $letter;\r
+                               \r
+                               if ( $i == i18n::strlen($string) - 1 )\r
+                               {\r
+                                       $encoded_text = self::b_encoder($letters);\r
+                                       $encoded_words[] = "{$header}{$encoded_text}{$footer}";\r
+                                       break;\r
+                               }\r
+                               continue;\r
+                       }\r
+                       else\r
+                       {\r
+                               if ( $i == 0 )\r
+                               {\r
+                                       $encoded_text = '';\r
+                               }\r
+                               \r
+                               $encoded_letter = self::q_encoder(i18n::substr($string, $i, 1));\r
+                               $expected_length = strlen($encoded_text) + strlen($encoded_letter);\r
+                               \r
+                               if ( $expected_length > $restriction )\r
+                               {\r
+                                       $encoded_words[] = "{$header}{$encoded_text}{$footer}";\r
+                                       $letters = '';\r
+                               }\r
+                               \r
+                               $encoded_text .= $encoded_letter;\r
+                               \r
+                               if ( $i == i18n::strlen($string) - 1 )\r
+                               {\r
+                                       $encoded_words[] = "{$header}{$encoded_text}{$footer}";\r
+                                       break;\r
+                               }\r
+                               continue;\r
+                       }\r
+               }\r
+               \r
+               return implode('', $encoded_words);\r
+       }\r
+       \r
+       /**\r
+        * NOTIFICATION::b_encoder()\r
+        * \r
+        * B encoder according to RFC 2047.\r
+        * The "B" encoding is identical to the "BASE64" encoding defined by RFC 4648.\r
+        * \r
+        * @link http://www.ietf.org/rfc/rfc4648.txt\r
+        * @see 6.8. Base64 Content-Transfer-Encoding\r
+        * \r
+        * NOTE: According to RFC 4648\r
+        * (1)  The final quantum of encoding input is an integral multiple of 24 bits;\r
+        *              here, the final unit of encoded output will be an integral multiple\r
+        *              of 4 characters with no "=" padding.\r
+        * (2)  The final quantum of encoding input is exactly 8 bits; here,\r
+        *              the final unit of encoded output will be two characters followed\r
+        *              by two "=" padding characters.\r
+        * (3)  The final quantum of encoding input is exactly 16 bits; here,\r
+        *              the final unit of encoded output will be three characters followed\r
+        *              by one "=" padding character.\r
+        * \r
+        * @static\r
+        * @param       string  $target targetted string\r
+        * @return      string  encoded string\r
+        */\r
+       static private function b_encoder($target)\r
+       {\r
+               return base64_encode($target);\r
+       }\r
+       \r
+       /**\r
+        * NOTIFICATION::q_encoder()\r
+        * \r
+        * Q encoder according to RFC 2047.\r
+        * The "Q" encoding is similar to "Quoted-Printable" content-transfer-encoding defined in RFC 2045,\r
+        *  but the "Q" encoding and the "Quoted-Printable" are different a bit.\r
+        * \r
+        * @link http://www.ietf.org/rfc/rfc2047.txt\r
+        * @see 4.2. The "Q" encoding\r
+        * \r
+        * NOTE: According to RFC 2047\r
+        * (1)  Any 8-bit value may be represented by a "=" followed by two hexadecimal digits.\r
+        *              For example, if the character set in use were ISO-8859-1,\r
+        *              the "=" character would thus be encoded as "=3D", and a SPACE by "=20".\r
+        *              (Upper case should be used for hexadecimal digits "A" through "F".)\r
+        * (2)  The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be\r
+        *              represented as "_" (underscore, ASCII 95.).\r
+        *              (This character may not pass through some internetwork mail gateways,\r
+        *              but its use will greatly enhance readability of "Q" encoded data\r
+        *              with mail readers that do not support this encoding.)\r
+        *              Note that the "_" always represents hexadecimal 20,\r
+        *              even if the SPACE character occupies a different code position\r
+        *              in the character set in use.\r
+        * (3)  8-bit values which correspond to printable ASCII characters\r
+        *              other than "=", "?", and "_" (underscore), MAY be represented as those characters.\r
+        *              (But see section 5 for restrictions.)\r
+        *              In particular, SPACE and TAB MUST NOT be represented as themselves within encoded words.\r
+        * \r
+        * @static\r
+        * @param       string  $target targetted string\r
+        * @return      string  encoded string\r
+        */\r
+       static private function q_encoder($target)\r
+       {\r
+               $string = '';\r
+               \r
+               for ( $i = 0; $i < strlen($target); $i++ )\r
+               {\r
+                       $letter = substr ($target, $i, 1);\r
+                       $order = ord($letter);\r
+                       \r
+                       // Printable ASCII characters without "=", "?", "_"\r
+                       if ((33 <= $order && $order <= 60)\r
+                        || (62 == $order)\r
+                        || (64 <= $order && $order <= 94)\r
+                        || (96 <= $order && $order <= 126))\r
+                       {\r
+                               $string .= strtoupper(dechex($order));\r
+                       }\r
+                       // Space shuold be encoded as the same strings as "_"\r
+                       else if ($order == 32)\r
+                       {\r
+                               $string .= '_';\r
+                       }\r
+                       // Other characters\r
+                       else\r
+                       {\r
+                               $string .= '=' . strtoupper(dechex($order));\r
+                       }\r
+               }\r
+               \r
+               return $string;\r
+       }\r
+       \r
+       /**\r
+        * NOTICE: Deprecated\r
+        * NOTIFICATION::$addresses\r
+        * \r
+        * @deprecated\r
+        */\r
+       private $addresses = array();\r
+       \r
+       /**\r
+        * NOTICE: Deprecated\r
+        * takes one string as argument, containing multiple e-mail addresses\r
+        * separated by semicolons\r
+        * eg: site@demuynck.org;nucleus@demuynck.org;foo@bar.com\r
+        * \r
+        * @deprecated\r
+        */\r
+       function __construct($addresses)\r
+       {\r
+               $this->addresses = preg_split('#;#' , $addresses);\r
+       }\r
+       \r
+       /**\r
+        * NOTICE: Deprecated\r
+        * NOTIFICATION::validAddresses()\r
+        * \r
+        * returns true if all addresses are valid\r
+        * \r
+        * @deprecated\r
+        * @param       Void\r
+        * @return      Boolean\r
+        */\r
+       function validAddresses()\r
+       {\r
+               foreach ( $this->addresses as $address )\r
+               {\r
+                       if ( !self::address_validation(trim($address)) )\r
+                       {\r
+                               return 0;\r
+                       }\r
+               }\r
+               return 1;\r
+       }\r
+       \r
+       /**\r
+        * NOTICE: Deprecated\r
+        * NOTIFICATION::notify()\r
+        * \r
+        * Sends email messages to all the email addresses\r
+        * \r
+        * @deprecated\r
+        * @param       String  $title  \r
+        * @param       String  $message        \r
+        * @param       String  $from   \r
+        * @return      Void\r
+        */\r
+       function notify($title, $message, $from)\r
+       {\r
+               global $member;\r
+               $addresses = array();\r
+               \r
+               foreach ($this->addresses as $address)\r
+               {\r
+                       if ( $member->isLoggedIn() && ($member->getEmail() == $address) )\r
+                       {\r
+                               continue;\r
+                       }\r
+                       $addresses[] = $address;\r
+               }\r
+               \r
+               self::mail(implode(',', $addresses), $title, $message , $from);\r
+               return;\r
+       }\r
+}\r
+=======
 <?php
 /*
  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
@@ -397,3 +798,4 @@ class Notification
                return;
        }
 }
+>>>>>>> skinnable-master