Commit 05a0e3ee authored by Sebastian Kummer's avatar Sebastian Kummer

Merge pull request #141 in ZP/z-push from feature/ZP-828-imap-contributions to develop

* commit 'be492b1d':
  ZP-828 Fixed estimatedDataSize.
  ZP-828 Update autoloader maps.
  ZP-828 Move Auth/SASL, Mail and Net to backend/imap. If other packages use these at a later stage, we can move them back again and build lib packages.
  ZP-828 Pull request #260 Z-Push-contrib. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Change back getMSTZnameFromTZName to private as is not called outside TimeZoneUtil. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Remove access keyword for extra include files. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Use __constructor for PHP 7 compatibility. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Copy-paste error. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Remove extra unset that doesn't make any difference. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Move secondary content type check inside primary. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Allow new time proposal when receiving canceled events. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Added legal information header. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Show last IMAP error on EmptyFolder. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Change default sensitivity to Normal, following Zarafa backend. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 Remove assigning variables to themselves. Released under the Affero GNU General Public License (AGPL) version 3.
  ZP-828 IMAP contributions. Released under the Affero GNU General Public License (AGPL) version 3.
parents 42bf4255 be492b1d
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@php.net> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Client implementation of various SASL mechanisms
*
* @author Richard Heyes <richard@php.net>
* @access public
* @version 1.0
* @package Auth_SASL
*/
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Auth_SASL-1.0.6.tgz
*
*
*/
//require_once('PEAR.php');
class Auth_SASL
{
/**
* Factory class. Returns an object of the request
* type.
*
* @param string $type One of: Anonymous
* Plain
* CramMD5
* DigestMD5
* SCRAM-* (any mechanism of the SCRAM family)
* Types are not case sensitive
*/
function &factory($type)
{
switch (strtolower($type)) {
case 'anonymous':
$filename = 'include/Auth/SASL/Anonymous.php';
$classname = 'Auth_SASL_Anonymous';
break;
case 'login':
$filename = 'include/Auth/SASL/Login.php';
$classname = 'Auth_SASL_Login';
break;
case 'plain':
$filename = 'include/Auth/SASL/Plain.php';
$classname = 'Auth_SASL_Plain';
break;
case 'external':
$filename = 'include/Auth/SASL/External.php';
$classname = 'Auth_SASL_External';
break;
case 'crammd5':
// $msg = 'Deprecated mechanism name. Use IANA-registered name: CRAM-MD5.';
// trigger_error($msg, E_USER_DEPRECATED);
case 'cram-md5':
$filename = 'include/Auth/SASL/CramMD5.php';
$classname = 'Auth_SASL_CramMD5';
break;
case 'digestmd5':
// $msg = 'Deprecated mechanism name. Use IANA-registered name: DIGEST-MD5.';
// trigger_error($msg, E_USER_DEPRECATED);
case 'digest-md5':
// $msg = 'DIGEST-MD5 is a deprecated SASL mechanism as per RFC-6331. Using it could be a security risk.';
// trigger_error($msg, E_USER_NOTICE);
$filename = 'include/Auth/SASL/DigestMD5.php';
$classname = 'Auth_SASL_DigestMD5';
break;
default:
$scram = '/^SCRAM-(.{1,9})$/i';
if (preg_match($scram, $type, $matches))
{
$hash = $matches[1];
$filename = 'include/Auth/SASL/SCRAM.php';
$classname = 'Auth_SASL_SCRAM';
$parameter = $hash;
break;
}
return Auth_SASL::raiseError('Invalid SASL mechanism type');
break;
}
require_once($filename);
if (isset($parameter))
$obj = new $classname($parameter);
else
$obj = new $classname();
return $obj;
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
static function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "Auth_SASL error: ". $message);
return false;
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@php.net> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Implmentation of ANONYMOUS SASL mechanism
*
* @author Richard Heyes <richard@php.net>
* @access public
* @version 1.0
* @package Auth_SASL
*/
class Auth_SASL_Anonymous extends Auth_SASL_Common
{
/**
* Not much to do here except return the token supplied.
* No encoding, hashing or encryption takes place for this
* mechanism, simply one of:
* o An email address
* o An opaque string not containing "@" that can be interpreted
* by the sysadmin
* o Nothing
*
* We could have some logic here for the second option, but this
* would by no means create something interpretable.
*
* @param string $token Optional email address or string to provide
* as trace information.
* @return string The unaltered input token
*/
function getResponse($token = '')
{
return $token;
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@php.net> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Common functionality to SASL mechanisms
*
* @author Richard Heyes <richard@php.net>
* @access public
* @version 1.0
* @package Auth_SASL
*/
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Auth_SASL-1.0.6.tgz
*
*
*/
class Auth_SASL_Common
{
/**
* Function which implements HMAC MD5 digest
*
* @param string $key The secret key
* @param string $data The data to hash
* @param bool $raw_output Whether the digest is returned in binary or hexadecimal format.
*
* @return string The HMAC-MD5 digest
*/
function _HMAC_MD5($key, $data, $raw_output = FALSE)
{
if (strlen($key) > 64) {
$key = pack('H32', md5($key));
}
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
$k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
$k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
$inner = pack('H32', md5($k_ipad . $data));
$digest = md5($k_opad . $inner, $raw_output);
return $digest;
}
/**
* Function which implements HMAC-SHA-1 digest
*
* @param string $key The secret key
* @param string $data The data to hash
* @param bool $raw_output Whether the digest is returned in binary or hexadecimal format.
* @return string The HMAC-SHA-1 digest
* @author Jehan <jehan.marmottard@gmail.com>
* @access protected
*/
protected function _HMAC_SHA1($key, $data, $raw_output = FALSE)
{
if (strlen($key) > 64) {
$key = sha1($key, TRUE);
}
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
$k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
$k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
$inner = pack('H40', sha1($k_ipad . $data));
$digest = sha1($k_opad . $inner, $raw_output);
return $digest;
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "SCRAM error: ". $message);
return false;
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@php.net> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Implmentation of CRAM-MD5 SASL mechanism
*
* @author Richard Heyes <richard@php.net>
* @access public
* @version 1.0
* @package Auth_SASL
*/
class Auth_SASL_CramMD5 extends Auth_SASL_Common
{
/**
* Implements the CRAM-MD5 SASL mechanism
* This DOES NOT base64 encode the return value,
* you will need to do that yourself.
*
* @param string $user Username
* @param string $pass Password
* @param string $challenge The challenge supplied by the server.
* this should be already base64_decoded.
*
* @return string The string to pass back to the server, of the form
* "<user> <digest>". This is NOT base64_encoded.
*/
function getResponse($user, $pass, $challenge)
{
return $user . ' ' . $this->_HMAC_MD5($pass, $challenge);
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@php.net> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Implmentation of DIGEST-MD5 SASL mechanism
*
* @author Richard Heyes <richard@php.net>
* @access public
* @version 1.0
* @package Auth_SASL
*/
class Auth_SASL_DigestMD5 extends Auth_SASL_Common
{
/**
* Provides the (main) client response for DIGEST-MD5
* requires a few extra parameters than the other
* mechanisms, which are unavoidable.
*
* @param string $authcid Authentication id (username)
* @param string $pass Password
* @param string $challenge The digest challenge sent by the server
* @param string $hostname The hostname of the machine you're connecting to
* @param string $service The servicename (eg. imap, pop, acap etc)
* @param string $authzid Authorization id (username to proxy as)
* @return string The digest response (NOT base64 encoded)
* @access public
*/
function getResponse($authcid, $pass, $challenge, $hostname, $service, $authzid = '')
{
$challenge = $this->_parseChallenge($challenge);
$authzid_string = '';
if ($authzid != '') {
$authzid_string = ',authzid="' . $authzid . '"';
}
if (!empty($challenge)) {
$cnonce = $this->_getCnonce();
$digest_uri = sprintf('%s/%s', $service, $hostname);
$response_value = $this->_getResponseValue($authcid, $pass, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $authzid);
if ($challenge['realm']) {
return sprintf('username="%s",realm="%s"' . $authzid_string .
',nonce="%s",cnonce="%s",nc=00000001,qop=auth,digest-uri="%s",response=%s,maxbuf=%d', $authcid, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $response_value, $challenge['maxbuf']);
} else {
return sprintf('username="%s"' . $authzid_string . ',nonce="%s",cnonce="%s",nc=00000001,qop=auth,digest-uri="%s",response=%s,maxbuf=%d', $authcid, $challenge['nonce'], $cnonce, $digest_uri, $response_value, $challenge['maxbuf']);
}
} else {
return $this->raiseError('Invalid digest challenge');
}
}
/**
* Parses and verifies the digest challenge*
*
* @param string $challenge The digest challenge
* @return array The parsed challenge as an assoc
* array in the form "directive => value".
* @access private
*/
function _parseChallenge($challenge)
{
$tokens = array();
while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $challenge, $matches)) {
// Ignore these as per rfc2831
if ($matches[1] == 'opaque' OR $matches[1] == 'domain') {
$challenge = substr($challenge, strlen($matches[0]) + 1);
continue;
}
// Allowed multiple "realm" and "auth-param"
if (!empty($tokens[$matches[1]]) AND ($matches[1] == 'realm' OR $matches[1] == 'auth-param')) {
if (is_array($tokens[$matches[1]])) {
$tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
} else {
$tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
}
// Any other multiple instance = failure
} elseif (!empty($tokens[$matches[1]])) {
$tokens = array();
break;
} else {
$tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
}
// Remove the just parsed directive from the challenge
$challenge = substr($challenge, strlen($matches[0]) + 1);
}
/**
* Defaults and required directives
*/
// Realm
if (empty($tokens['realm'])) {
$tokens['realm'] = "";
}
// Maxbuf
if (empty($tokens['maxbuf'])) {
$tokens['maxbuf'] = 65536;
}
// Required: nonce, algorithm
if (empty($tokens['nonce']) OR empty($tokens['algorithm'])) {
return array();
}
return $tokens;
}
/**
* Creates the response= part of the digest response
*
* @param string $authcid Authentication id (username)
* @param string $pass Password
* @param string $realm Realm as provided by the server
* @param string $nonce Nonce as provided by the server
* @param string $cnonce Client nonce
* @param string $digest_uri The digest-uri= value part of the response
* @param string $authzid Authorization id
* @return string The response= part of the digest response
* @access private
*/
function _getResponseValue($authcid, $pass, $realm, $nonce, $cnonce, $digest_uri, $authzid = '')
{
if ($authzid == '') {
$A1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce);
} else {
$A1 = sprintf('%s:%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce, $authzid);
}
$A2 = 'AUTHENTICATE:' . $digest_uri;
return md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($A1), $nonce, $cnonce, md5($A2)));
}
/**
* Creates the client nonce for the response
*
* @return string The cnonce value
* @access private
*/
function _getCnonce()
{
if (@file_exists('/dev/urandom') && $fd = @fopen('/dev/urandom', 'r')) {
return base64_encode(fread($fd, 32));
} elseif (@file_exists('/dev/random') && $fd = @fopen('/dev/random', 'r')) {
return base64_encode(fread($fd, 32));
} else {
$str = '';
for ($i=0; $i<32; $i++) {
$str .= chr(mt_rand(0, 255));
}
return base64_encode($str);
}
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2008 Christoph Schulz |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Christoph Schulz <develop@kristov.de> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Implmentation of EXTERNAL SASL mechanism
*
* @author Christoph Schulz <develop@kristov.de>
* @access public
* @version 1.0.3
* @package Auth_SASL
*/
class Auth_SASL_External extends Auth_SASL_Common
{
/**
* Returns EXTERNAL response
*
* @param string $authcid Authentication id (username)
* @param string $pass Password
* @param string $authzid Autorization id
* @return string EXTERNAL Response
*/
function getResponse($authcid, $pass, $authzid = '')
{
return $authzid;
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@php.net> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* This is technically not a SASL mechanism, however
* it's used by Net_Sieve, Net_Cyrus and potentially
* other protocols , so here is a good place to abstract
* it.
*
* @author Richard Heyes <richard@php.net>
* @access public
* @version 1.0
* @package Auth_SASL
*/
class Auth_SASL_Login extends Auth_SASL_Common
{
/**
* Pseudo SASL LOGIN mechanism
*
* @param string $user Username
* @param string $pass Password
* @return string LOGIN string
*/
function getResponse($user, $pass)
{
return sprintf('LOGIN %s %s', $user, $pass);
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@php.net> |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Implmentation of PLAIN SASL mechanism
*
* @author Richard Heyes <richard@php.net>
* @access public
* @version 1.0
* @package Auth_SASL
*/
class Auth_SASL_Plain extends Auth_SASL_Common
{
/**
* Returns PLAIN response
*
* @param string $authcid Authentication id (username)
* @param string $pass Password
* @param string $authzid Autorization id
* @return string PLAIN Response
*/
function getResponse($authcid, $pass, $authzid = '')
{
return $authzid . chr(0) . $authcid . chr(0) . $pass;
}
}
\ No newline at end of file
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2011 Jehan |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Jehan <jehan.marmottard@gmail.com |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
* Implementation of SCRAM-* SASL mechanisms.
* SCRAM mechanisms have 3 main steps (initial response, response to the server challenge, then server signature
* verification) which keep state-awareness. Therefore a single class instanciation must be done and reused for the whole
* authentication process.
*
* @author Jehan <jehan.marmottard@gmail.com>
* @access public
* @version 1.0
* @package Auth_SASL
*/
class Auth_SASL_SCRAM extends Auth_SASL_Common
{
/**
* Construct a SCRAM-H client where 'H' is a cryptographic hash function.
*
* @param string $hash The name cryptographic hash function 'H' as registered by IANA in the "Hash Function Textual
* Names" registry.
* @link http://www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xml "Hash Function Textual
* Names"
* format of core PHP hash function.
* @access public
*/
function __construct($hash)
{
// Though I could be strict, I will actually also accept the naming used in the PHP core hash framework.
// For instance "sha1" is accepted, while the registered hash name should be "SHA-1".
$hash = strtolower($hash);
$hashes = array('md2' => 'md2',
'md5' => 'md5',
'sha-1' => 'sha1',
'sha1' => 'sha1',
'sha-224' > 'sha224',
'sha224' > 'sha224',
'sha-256' => 'sha256',
'sha256' => 'sha256',
'sha-384' => 'sha384',
'sha384' => 'sha384',
'sha-512' => 'sha512',
'sha512' => 'sha512');
if (function_exists('hash_hmac') && isset($hashes[$hash]))
{
$this->hash = create_function('$data', 'return hash("' . $hashes[$hash] . '", $data, TRUE);');
$this->hmac = create_function('$key,$str,$raw', 'return hash_hmac("' . $hashes[$hash] . '", $str, $key, $raw);');
}
elseif ($hash == 'md5')
{
$this->hash = create_function('$data', 'return md5($data, true);');
$this->hmac = array($this, '_HMAC_MD5');
}
elseif (in_array($hash, array('sha1', 'sha-1')))
{
$this->hash = create_function('$data', 'return sha1($data, true);');
$this->hmac = array($this, '_HMAC_SHA1');
}
else
return $this->raiseError('Invalid SASL mechanism type');
}
/**
* Provides the (main) client response for SCRAM-H.
*
* @param string $authcid Authentication id (username)
* @param string $pass Password
* @param string $challenge The challenge sent by the server.
* If the challenge is NULL or an empty string, the result will be the "initial response".
* @param string $authzid Authorization id (username to proxy as)
* @return string|false The response (binary, NOT base64 encoded)
* @access public
*/
public function getResponse($authcid, $pass, $challenge = NULL, $authzid = NULL)
{
$authcid = $this->_formatName($authcid);
if (empty($authcid))
{
return false;
}
if (!empty($authzid))
{
$authzid = $this->_formatName($authzid);
if (empty($authzid))
{
return false;
}
}
if (empty($challenge))
{
return $this->_generateInitialResponse($authcid, $authzid);
}
else
{
return $this->_generateResponse($challenge, $pass);
}
}
/**
* Prepare a name for inclusion in a SCRAM response.
*
* @param string $username a name to be prepared.
* @return string the reformated name.
* @access private
*/
private function _formatName($username)
{
// TODO: prepare through the SASLprep profile of the stringprep algorithm.
// See RFC-4013.
$username = str_replace('=', '=3D', $username);
$username = str_replace(',', '=2C', $username);
return $username;
}
/**
* Generate the initial response which can be either sent directly in the first message or as a response to an empty
* server challenge.
*
* @param string $authcid Prepared authentication identity.
* @param string $authzid Prepared authorization identity.
* @return string The SCRAM response to send.
* @access private
*/
private function _generateInitialResponse($authcid, $authzid)
{
$init_rep = '';
$gs2_cbind_flag = 'n,'; // TODO: support channel binding.
$this->gs2_header = $gs2_cbind_flag . (!empty($authzid)? 'a=' . $authzid : '') . ',';
// I must generate a client nonce and "save" it for later comparison on second response.
$this->cnonce = $this->_getCnonce();
// XXX: in the future, when mandatory and/or optional extensions are defined in any updated RFC,
// this message can be updated.
$this->first_message_bare = 'n=' . $authcid . ',r=' . $this->cnonce;
return $this->gs2_header . $this->first_message_bare;
}
/**
* Parses and verifies a non-empty SCRAM challenge.
*
* @param string $challenge The SCRAM challenge
* @return string|false The response to send; false in case of wrong challenge or if an initial response has not
* been generated first.
* @access private
*/
private function _generateResponse($challenge, $password)
{
// XXX: as I don't support mandatory extension, I would fail on them.
// And I simply ignore any optional extension.
$server_message_regexp = "#^r=([\x21-\x2B\x2D-\x7E]+),s=((?:[A-Za-z0-9/+]{4})*(?:[A-Za-z0-9]{3}=|[A-Xa-z0-9]{2}==)?),i=([0-9]*)(,[A-Za-z]=[^,])*$#";
if (!isset($this->cnonce, $this->gs2_header)
|| !preg_match($server_message_regexp, $challenge, $matches))
{
return false;
}
$nonce = $matches[1];
$salt = base64_decode($matches[2]);
if (!$salt)
{
// Invalid Base64.
return false;
}
$i = intval($matches[3]);
$cnonce = substr($nonce, 0, strlen($this->cnonce));
if ($cnonce <> $this->cnonce)
{
// Invalid challenge! Are we under attack?
return false;
}
$channel_binding = 'c=' . base64_encode($this->gs2_header); // TODO: support channel binding.
$final_message = $channel_binding . ',r=' . $nonce; // XXX: no extension.
// TODO: $password = $this->normalize($password); // SASLprep profile of stringprep.
$saltedPassword = $this->hi($password, $salt, $i);
$this->saltedPassword = $saltedPassword;
$clientKey = call_user_func($this->hmac, $saltedPassword, "Client Key", TRUE);
$storedKey = call_user_func($this->hash, $clientKey, TRUE);
$authMessage = $this->first_message_bare . ',' . $challenge . ',' . $final_message;
$this->authMessage = $authMessage;
$clientSignature = call_user_func($this->hmac, $storedKey, $authMessage, TRUE);
$clientProof = $clientKey ^ $clientSignature;
$proof = ',p=' . base64_encode($clientProof);
return $final_message . $proof;
}
/**
* SCRAM has also a server verification step. On a successful outcome, it will send additional data which must
* absolutely be checked against this function. If this fails, the entity which we are communicating with is probably
* not the server as it has not access to your ServerKey.
*
* @param string $data The additional data sent along a successful outcome.
* @return bool Whether the server has been authenticated.
* If false, the client must close the connection and consider to be under a MITM attack.
* @access public
*/
public function processOutcome($data)
{
$verifier_regexp = '#^v=((?:[A-Za-z0-9/+]{4})*(?:[A-Za-z0-9]{3}=|[A-Xa-z0-9]{2}==)?)$#';
if (!isset($this->saltedPassword, $this->authMessage)
|| !preg_match($verifier_regexp, $data, $matches))
{
// This cannot be an outcome, you never sent the challenge's response.
return false;
}
$verifier = $matches[1];
$proposed_serverSignature = base64_decode($verifier);
$serverKey = call_user_func($this->hmac, $this->saltedPassword, "Server Key", true);
$serverSignature = call_user_func($this->hmac, $serverKey, $this->authMessage, TRUE);
return ($proposed_serverSignature === $serverSignature);
}
/**
* Hi() call, which is essentially PBKDF2 (RFC-2898) with HMAC-H() as the pseudorandom function.
*
* @param string $str The string to hash.
* @param string $hash The hash value.
* @param int $i The iteration count.
* @access private
*/
private function hi($str, $salt, $i)
{
$int1 = "\0\0\0\1";
$ui = call_user_func($this->hmac, $str, $salt . $int1, true);
$result = $ui;
for ($k = 1; $k < $i; $k++)
{
$ui = call_user_func($this->hmac, $str, $ui, true);
$result = $result ^ $ui;
}
return $result;
}
/**
* Creates the client nonce for the response
*
* @return string The cnonce value
* @access private
* @author Richard Heyes <richard@php.net>
*/
private function _getCnonce()
{
// TODO: I reused the nonce function from the DigestMD5 class.
// I should probably make this a protected function in Common.
if (@file_exists('/dev/urandom') && $fd = @fopen('/dev/urandom', 'r')) {
return base64_encode(fread($fd, 32));
} elseif (@file_exists('/dev/random') && $fd = @fopen('/dev/random', 'r')) {
return base64_encode(fread($fd, 32));
} else {
$str = '';
for ($i=0; $i<32; $i++) {
$str .= chr(mt_rand(0, 255));
}
return base64_encode($str);
}
}
}
\ No newline at end of file
<?php
/**
* PEAR's Mail:: interface.
*
* PHP versions 4 and 5
*
* LICENSE:
*
* Copyright (c) 2002-2007, Richard Heyes
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* o The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category Mail
* @package Mail
* @author Chuck Hagenbuch <chuck@horde.org>
* @copyright 1997-2010 Chuck Hagenbuch
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Mail.php 307489 2011-01-14 19:06:57Z alec $
* @link http://pear.php.net/package/Mail/
*/
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail-1.2.0.tgz
* SVN trunk version r333509
*
*
*/
/**
* PEAR's Mail:: interface. Defines the interface for implementing
* mailers under the PEAR hierarchy, and provides supporting functions
* useful in multiple mailer backends.
*
* @access public
* @version $Revision: 307489 $
* @package Mail
*/
class Mail
{
/**
* Line terminator used for separating header lines.
* @var string
*/
var $sep = "\r\n";
/**
* Provides an interface for generating Mail:: objects of various
* types
*
* @param string $driver The kind of Mail:: object to instantiate.
* @param array $params The parameters to pass to the Mail:: object.
* @return object Mail a instance of the driver class or if fails a PEAR Error
* @access public
*/
static function &factory($driver, $params = array())
{
$driver = strtolower($driver);
$class = 'Mail_' . $driver;
if (class_exists($class)) {
$mailer = new $class($params);
return $mailer;
} else {
return Mail::raiseError('Unable to find class for driver ' . $driver);
}
}
/**
* Implements Mail::send() function using php's built-in mail()
* command.
*
* @param mixed $recipients Either a comma-seperated list of recipients
* (RFC822 compliant), or an array of recipients,
* each RFC822 valid. This may contain recipients not
* specified in the headers, for Bcc:, resending
* messages, etc.
*
* @param array $headers The array of headers to send with the mail, in an
* associative array, where the array key is the
* header name (ie, 'Subject'), and the array value
* is the header value (ie, 'test'). The header
* produced from those values would be 'Subject:
* test'.
*
* @param string $body The full text of the message body, including any
* Mime parts, etc.
*
* @return mixed Returns true on success, or a PEAR_Error
* containing a descriptive error message on
* failure.
*
* @access public
* @deprecated use Mail_mail::send instead
*/
function send($recipients, $headers, $body)
{
if (!is_array($headers)) {
return Mail::raiseError('$headers must be an array');
}
$result = $this->_sanitizeHeaders($headers);
//if (is_a($result, 'PEAR_Error')) {
if ($result === false) {
return $result;
}
// if we're passed an array of recipients, implode it.
if (is_array($recipients)) {
$recipients = implode(', ', $recipients);
}
// get the Subject out of the headers array so that we can
// pass it as a seperate argument to mail().
$subject = '';
if (isset($headers['Subject'])) {
$subject = $headers['Subject'];
unset($headers['Subject']);
}
// flatten the headers out.
list(, $text_headers) = Mail::prepareHeaders($headers);
return mail($recipients, $subject, $body, $text_headers);
}
/**
* Sanitize an array of mail headers by removing any additional header
* strings present in a legitimate header's value. The goal of this
* filter is to prevent mail injection attacks.
*
* @param array $headers The associative array of headers to sanitize.
*
* @access private
*/
function _sanitizeHeaders(&$headers)
{
foreach ($headers as $key => $value) {
$headers[$key] =
preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
null, $value);
}
return true;
}
/**
* Take an array of mail headers and return a string containing
* text usable in sending a message.
*
* @param array $headers The array of headers to prepare, in an associative
* array, where the array key is the header name (ie,
* 'Subject'), and the array value is the header
* value (ie, 'test'). The header produced from those
* values would be 'Subject: test'.
*
* @return mixed Returns false if it encounters a bad address,
* otherwise returns an array containing two
* elements: Any From: address found in the headers,
* and the plain text version of the headers.
* @access private
*/
function prepareHeaders($headers)
{
$lines = array();
$from = null;
foreach ($headers as $key => $value) {
if (strcasecmp($key, 'From') === 0) {
$parser = new Mail_RFC822();
$addresses = $parser->parseAddressList($value, 'localhost', false);
//if (is_a($addresses, 'PEAR_Error')) {
if ($addresses === false) {
return $addresses;
}
$from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
// Reject envelope From: addresses with spaces.
if (strstr($from, ' ')) {
return false;
}
$lines[] = $key . ': ' . $value;
} elseif (strcasecmp($key, 'Received') === 0) {
$received = array();
if (is_array($value)) {
foreach ($value as $line) {
$received[] = $key . ': ' . $line;
}
}
else {
$received[] = $key . ': ' . $value;
}
// Put Received: headers at the top. Spam detectors often
// flag messages with Received: headers after the Subject:
// as spam.
$lines = array_merge($received, $lines);
} else {
// If $value is an array (i.e., a list of addresses), convert
// it to a comma-delimited string of its elements (addresses).
if (is_array($value)) {
$value = implode(', ', $value);
}
$lines[] = $key . ': ' . $value;
}
}
return array($from, join($this->sep, $lines));
}
/**
* Take a set of recipients and parse them, returning an array of
* bare addresses (forward paths) that can be passed to sendmail
* or an smtp server with the rcpt to: command.
*
* @param mixed Either a comma-seperated list of recipients
* (RFC822 compliant), or an array of recipients,
* each RFC822 valid.
*
* @return mixed An array of forward paths (bare addresses) or a PEAR_Error
* object if the address list could not be parsed.
* @access private
*/
function parseRecipients($recipients)
{
// if we're passed an array, assume addresses are valid and
// implode them before parsing.
if (is_array($recipients)) {
$recipients = implode(', ', $recipients);
}
// Parse recipients, leaving out all personal info. This is
// for smtp recipients, etc. All relevant personal information
// should already be in the headers.
$parser = new Mail_RFC822();
$addresses = $parser->parseAddressList($recipients, 'localhost', false);
// If parseAddressList() returned a PEAR_Error object, just return it.
//if (is_a($addresses, 'PEAR_Error')) {
if ($addresses === false) {
return $addresses;
}
$recipients = array();
if (is_array($addresses)) {
foreach ($addresses as $ob) {
$recipients[] = $ob->mailbox . '@' . $ob->host;
}
}
// Remove duplicated
$recipients = array_unique($recipients);
return $recipients;
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
static function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "Mail error: ". $message);
return false;
}
}
<?php
/**
* internal PHP-mail() implementation of the PEAR Mail:: interface.
*
* PHP versions 4 and 5
*
* LICENSE:
*
* Copyright (c) 2010 Chuck Hagenbuch
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* o The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category Mail
* @package Mail
* @author Chuck Hagenbuch <chuck@horde.org>
* @copyright 2010 Chuck Hagenbuch
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: mail.php 294747 2010-02-08 08:18:33Z clockwerx $
* @link http://pear.php.net/package/Mail/
*/
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail-1.2.0.tgz
*
*
*/
/**
* internal PHP-mail() implementation of the PEAR Mail:: interface.
* @package Mail
* @version $Revision: 294747 $
*/
class Mail_mail extends Mail {
/**
* Any arguments to pass to the mail() function.
* @var string
*/
var $_params = '';
/**
* Constructor.
*
* Instantiates a new Mail_mail:: object based on the parameters
* passed in.
*
* @param array $params Extra arguments for the mail() function.
*/
function __construct($params = null)
{
// The other mail implementations accept parameters as arrays.
// In the interest of being consistent, explode an array into
// a string of parameter arguments.
if (is_array($params)) {
$this->_params = join(' ', $params);
} else {
$this->_params = $params;
}
/* Because the mail() function may pass headers as command
* line arguments, we can't guarantee the use of the standard
* "\r\n" separator. Instead, we use the system's native line
* separator. */
if (defined('PHP_EOL')) {
$this->sep = PHP_EOL;
} else {
$this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
}
}
/**
* Implements Mail_mail::send() function using php's built-in mail()
* command.
*
* @param mixed $recipients Either a comma-seperated list of recipients
* (RFC822 compliant), or an array of recipients,
* each RFC822 valid. This may contain recipients not
* specified in the headers, for Bcc:, resending
* messages, etc.
*
* @param array $headers The array of headers to send with the mail, in an
* associative array, where the array key is the
* header name (ie, 'Subject'), and the array value
* is the header value (ie, 'test'). The header
* produced from those values would be 'Subject:
* test'.
*
* @param string $body The full text of the message body, including any
* Mime parts, etc.
*
* @return mixed Returns true on success, or a PEAR_Error
* containing a descriptive error message on
* failure.
*
* @access public
*/
function send($recipients, $headers, $body)
{
if (!is_array($headers)) {
return Mail_mail::raiseError('$headers must be an array');
}
$result = $this->_sanitizeHeaders($headers);
//if (is_a($result, 'PEAR_Error')) {
if ($result === false) {
return $result;
}
// If we're passed an array of recipients, implode it.
if (is_array($recipients)) {
$recipients = implode(', ', $recipients);
}
// Get the Subject out of the headers array so that we can
// pass it as a seperate argument to mail().
$subject = '';
if (isset($headers['Subject'])) {
$subject = $headers['Subject'];
unset($headers['Subject']);
}
// Also remove the To: header. The mail() function will add its own
// To: header based on the contents of $recipients.
unset($headers['To']);
// Flatten the headers out.
$headerElements = $this->prepareHeaders($headers);
//if (is_a($headerElements, 'PEAR_Error')) {
if ($headerElements === false) {
return $headerElements;
}
list(, $text_headers) = $headerElements;
// We only use mail()'s optional fifth parameter if the additional
// parameters have been provided and we're not running in safe mode.
if (empty($this->_params) || ini_get('safe_mode')) {
$result = mail($recipients, $subject, $body, $text_headers);
} else {
$result = mail($recipients, $subject, $body, $text_headers,
$this->_params);
}
// If the mail() function returned failure, we need to create a
// PEAR_Error object and return it instead of the boolean result.
if ($result === false) {
$result = Mail_mail::raiseError('mail() returned failure');
}
return $result;
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
static function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "Mail<mail> error: ". $message);
return false;
}
}
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Chuck Hagenbuch <chuck@horde.org> |
// +----------------------------------------------------------------------+
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail-1.2.0.tgz
*
*
*/
/**
* Sendmail implementation of the PEAR Mail:: interface.
* @access public
* @package Mail
* @version $Revision: 294744 $
*/
class Mail_sendmail extends Mail {
/**
* The location of the sendmail or sendmail wrapper binary on the
* filesystem.
* @var string
*/
var $sendmail_path = '/usr/sbin/sendmail';
/**
* Any extra command-line parameters to pass to the sendmail or
* sendmail wrapper binary.
* @var string
*/
var $sendmail_args = '-i';
/**
* Constructor.
*
* Instantiates a new Mail_sendmail:: object based on the parameters
* passed in. It looks for the following parameters:
* sendmail_path The location of the sendmail binary on the
* filesystem. Defaults to '/usr/sbin/sendmail'.
*
* sendmail_args Any extra parameters to pass to the sendmail
* or sendmail wrapper binary.
*
* If a parameter is present in the $params array, it replaces the
* default.
*
* @param array $params Hash containing any parameters different from the
* defaults.
* @access public
*/
function __construct($params)
{
if (isset($params['sendmail_path'])) {
$this->sendmail_path = $params['sendmail_path'];
}
if (isset($params['sendmail_args'])) {
$this->sendmail_args = $params['sendmail_args'];
}
/*
* Because we need to pass message headers to the sendmail program on
* the commandline, we can't guarantee the use of the standard "\r\n"
* separator. Instead, we use the system's native line separator.
*/
if (defined('PHP_EOL')) {
$this->sep = PHP_EOL;
} else {
$this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
}
}
/**
* Implements Mail::send() function using the sendmail
* command-line binary.
*
* @param mixed $recipients Either a comma-seperated list of recipients
* (RFC822 compliant), or an array of recipients,
* each RFC822 valid. This may contain recipients not
* specified in the headers, for Bcc:, resending
* messages, etc.
*
* @param array $headers The array of headers to send with the mail, in an
* associative array, where the array key is the
* header name (ie, 'Subject'), and the array value
* is the header value (ie, 'test'). The header
* produced from those values would be 'Subject:
* test'.
*
* @param string $body The full text of the message body, including any
* Mime parts, etc.
*
* @return mixed Returns true on success, or a PEAR_Error
* containing a descriptive error message on
* failure.
* @access public
*/
function send($recipients, $headers, $body)
{
if (!is_array($headers)) {
return Mail_sendmail::raiseError('$headers must be an array');
}
$result = $this->_sanitizeHeaders($headers);
//if (is_a($result, 'PEAR_Error')) {
if ($result === false) {
return $result;
}
$recipients = $this->parseRecipients($recipients);
//if (is_a($recipients, 'PEAR_Error')) {
if ($recipients === false) {
return $recipients;
}
$recipients = implode(' ', array_map('escapeshellarg', $recipients));
$headerElements = $this->prepareHeaders($headers);
//if (is_a($headerElements, 'PEAR_Error')) {
if ($headerElements === false) {
return $headerElements;
}
list($from, $text_headers) = $headerElements;
/* Since few MTAs are going to allow this header to be forged
* unless it's in the MAIL FROM: exchange, we'll use
* Return-Path instead of From: if it's set. */
if (!empty($headers['Return-Path'])) {
$from = $headers['Return-Path'];
}
if (!isset($from)) {
return Mail_sendmail::raiseError('No from address given.');
} elseif (strpos($from, ' ') !== false ||
strpos($from, ';') !== false ||
strpos($from, '&') !== false ||
strpos($from, '`') !== false) {
return Mail_sendmail::raiseError('From address specified with dangerous characters.');
}
$from = escapeshellarg($from); // Security bug #16200
$mail = @popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f$from -- $recipients", 'w');
if (!$mail) {
return Mail_sendmail::raiseError('Failed to open sendmail [' . $this->sendmail_path . '] for execution.');
}
// Write the headers following by two newlines: one to end the headers
// section and a second to separate the headers block from the body.
fputs($mail, $text_headers . $this->sep . $this->sep);
fputs($mail, $body);
$result = pclose($mail);
if (version_compare(phpversion(), '4.2.3') == -1) {
// With older php versions, we need to shift the pclose
// result to get the exit code.
$result = $result >> 8 & 0xFF;
}
if ($result != 0) {
return Mail_sendmail::raiseError('sendmail returned error code ' . $result,
$result);
}
return true;
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
static function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "Mail<sendmail> error: ". $message);
return false;
}
}
<?php
/**
* SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class.
*
* PHP versions 4 and 5
*
* LICENSE:
*
* Copyright (c) 2010, Chuck Hagenbuch
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* o The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request
* @author Jon Parise <jon@php.net>
* @author Chuck Hagenbuch <chuck@horde.org>
* @copyright 2010 Chuck Hagenbuch
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: smtp.php 307488 2011-01-14 19:00:54Z alec $
* @link http://pear.php.net/package/Mail/
*/
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail-1.2.0.tgz
*
*
*/
/** Error: Failed to create a Net_SMTP object */
define('PEAR_MAIL_SMTP_ERROR_CREATE', 10000);
/** Error: Failed to connect to SMTP server */
define('PEAR_MAIL_SMTP_ERROR_CONNECT', 10001);
/** Error: SMTP authentication failure */
define('PEAR_MAIL_SMTP_ERROR_AUTH', 10002);
/** Error: No From: address has been provided */
define('PEAR_MAIL_SMTP_ERROR_FROM', 10003);
/** Error: Failed to set sender */
define('PEAR_MAIL_SMTP_ERROR_SENDER', 10004);
/** Error: Failed to add recipient */
define('PEAR_MAIL_SMTP_ERROR_RECIPIENT', 10005);
/** Error: Failed to send data */
define('PEAR_MAIL_SMTP_ERROR_DATA', 10006);
/**
* SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class.
* @access public
* @package Mail
* @version $Revision: 307488 $
*/
class Mail_smtp extends Mail {
/**
* SMTP connection object.
*
* @var object
* @access private
*/
var $_smtp = null;
/**
* The list of service extension parameters to pass to the Net_SMTP
* mailFrom() command.
* @var array
*/
var $_extparams = array();
/**
* The SMTP host to connect to.
* @var string
*/
var $host = 'localhost';
/**
* The port the SMTP server is on.
* @var integer
*/
var $port = 25;
/**
* Should SMTP authentication be used?
*
* This value may be set to true, false or the name of a specific
* authentication method.
*
* If the value is set to true, the Net_SMTP package will attempt to use
* the best authentication method advertised by the remote SMTP server.
*
* @var mixed
*/
var $auth = false;
/**
* The username to use if the SMTP server requires authentication.
* @var string
*/
var $username = '';
/**
* The password to use if the SMTP server requires authentication.
* @var string
*/
var $password = '';
/**
* Hostname or domain that will be sent to the remote SMTP server in the
* HELO / EHLO message.
*
* @var string
*/
var $localhost = 'localhost';
/**
* SMTP connection timeout value. NULL indicates no timeout.
*
* @var integer
*/
var $timeout = null;
/**
* Turn on Net_SMTP debugging?
*
* @var boolean $debug
*/
var $debug = false;
/**
* Indicates whether or not the SMTP connection should persist over
* multiple calls to the send() method.
*
* @var boolean
*/
var $persist = false;
/**
* Use SMTP command pipelining (specified in RFC 2920) if the SMTP server
* supports it. This speeds up delivery over high-latency connections. By
* default, use the default value supplied by Net_SMTP.
* @var bool
*/
var $pipelining;
/**
* Require verification of SSL certificate used.
* @var bool
*/
var $verify_peer = true;
/**
* Require verification of peer name
* @var bool
*/
var $verify_peer_name = true;
/**
* Allow self-signed certificates. Requires verify_peer
* @var bool
*/
var $allow_self_signed = false;
/**
* Constructor.
*
* Instantiates a new Mail_smtp:: object based on the parameters
* passed in. It looks for the following parameters:
* host The server to connect to. Defaults to localhost.
* port The port to connect to. Defaults to 25.
* auth SMTP authentication. Defaults to none.
* username The username to use for SMTP auth. No default.
* password The password to use for SMTP auth. No default.
* localhost The local hostname / domain. Defaults to localhost.
* timeout The SMTP connection timeout. Defaults to none.
* verp Whether to use VERP or not. Defaults to false.
* DEPRECATED as of 1.2.0 (use setMailParams()).
* debug Activate SMTP debug mode? Defaults to false.
* persist Should the SMTP connection persist?
* pipelining Use SMTP command pipelining
*
* If a parameter is present in the $params array, it replaces the
* default.
*
* @param array Hash containing any parameters different from the
* defaults.
* @access public
*/
function __construct($params)
{
if (isset($params['host'])) $this->host = $params['host'];
if (isset($params['port'])) $this->port = $params['port'];
if (isset($params['auth'])) $this->auth = $params['auth'];
if (isset($params['username'])) $this->username = $params['username'];
if (isset($params['password'])) $this->password = $params['password'];
if (isset($params['localhost'])) $this->localhost = $params['localhost'];
if (isset($params['timeout'])) $this->timeout = $params['timeout'];
if (isset($params['debug'])) $this->debug = (bool)$params['debug'];
if (isset($params['persist'])) $this->persist = (bool)$params['persist'];
if (isset($params['pipelining'])) $this->pipelining = (bool)$params['pipelining'];
if (isset($params['verify_peer'])) $this->verify_peer = (bool)$params['verify_peer'];
if (isset($params['verify_peer_name'])) $this->verify_peer_name = (bool)$params['verify_peer_name'];
if (isset($params['allow_self_signed'])) $this->allow_self_signed = (bool)$params['allow_self_signed'];
// Deprecated options
if (isset($params['verp'])) {
$this->addServiceExtensionParameter('XVERP', is_bool($params['verp']) ? null : $params['verp']);
}
register_shutdown_function(array(&$this, '_Mail_smtp'));
}
/**
* Destructor implementation to ensure that we disconnect from any
* potentially-alive persistent SMTP connections.
*/
function _Mail_smtp()
{
$this->disconnect();
}
/**
* Implements Mail::send() function using SMTP.
*
* @param mixed $recipients Either a comma-seperated list of recipients
* (RFC822 compliant), or an array of recipients,
* each RFC822 valid. This may contain recipients not
* specified in the headers, for Bcc:, resending
* messages, etc.
*
* @param array $headers The array of headers to send with the mail, in an
* associative array, where the array key is the
* header name (e.g., 'Subject'), and the array value
* is the header value (e.g., 'test'). The header
* produced from those values would be 'Subject:
* test'.
*
* @param string $body The full text of the message body, including any
* MIME parts, etc.
*
* @return mixed Returns true on success, or a PEAR_Error
* containing a descriptive error message on
* failure.
* @access public
*/
function send($recipients, $headers, $body)
{
/* If we don't already have an SMTP object, create one. */
$result = &$this->getSMTPObject();
//if (PEAR::isError($result)) {
if ($result === false) {
return $result;
}
if (!is_array($headers)) {
return Mail_smtp::raiseError('$headers must be an array');
}
$this->_sanitizeHeaders($headers);
$headerElements = $this->prepareHeaders($headers);
//if (is_a($headerElements, 'PEAR_Error')) {
if ($headerElements === false) {
$this->_smtp->rset();
return $headerElements;
}
list($from, $textHeaders) = $headerElements;
/* Since few MTAs are going to allow this header to be forged
* unless it's in the MAIL FROM: exchange, we'll use
* Return-Path instead of From: if it's set. */
if (!empty($headers['Return-Path'])) {
$from = $headers['Return-Path'];
}
if (!isset($from)) {
$this->_smtp->rset();
return Mail_smtp::raiseError('No From: address has been provided',
PEAR_MAIL_SMTP_ERROR_FROM);
}
$params = null;
if (!empty($this->_extparams)) {
foreach ($this->_extparams as $key => $val) {
$params .= ' ' . $key . (is_null($val) ? '' : '=' . $val);
}
}
//if (PEAR::isError($res = $this->_smtp->mailFrom($from, ltrim($params)))) {
if (($res = $this->_smtp->mailFrom($from, ltrim($params))) === false) {
$error = $this->_error("Failed to set sender: $from", $res);
$this->_smtp->rset();
return Mail_smtp::raiseError($error, PEAR_MAIL_SMTP_ERROR_SENDER);
}
$recipients = $this->parseRecipients($recipients);
//if (is_a($recipients, 'PEAR_Error')) {
if ($recipients === false) {
$this->_smtp->rset();
return $recipients;
}
foreach ($recipients as $recipient) {
$res = $this->_smtp->rcptTo($recipient);
//if (is_a($res, 'PEAR_Error')) {
if ($res === false) {
$error = $this->_error("Failed to add recipient: $recipient", $res);
$this->_smtp->rset();
return Mail_smtp::raiseError($error, PEAR_MAIL_SMTP_ERROR_RECIPIENT);
}
}
/* Send the message's headers and the body as SMTP data. */
$res = $this->_smtp->data($body, $textHeaders);
list(,$args) = $this->_smtp->getResponse();
if (preg_match("/Ok: queued as (.*)/", $args, $queued)) {
$this->queued_as = $queued[1];
}
/* we need the greeting; from it we can extract the authorative name of the mail server we've really connected to.
* ideal if we're connecting to a round-robin of relay servers and need to track which exact one took the email */
$this->greeting = $this->_smtp->getGreeting();
//if (is_a($res, 'PEAR_Error')) {
if ($res === false) {
$error = $this->_error('Failed to send data', $res);
$this->_smtp->rset();
return Mail_smtp::raiseError($error, PEAR_MAIL_SMTP_ERROR_DATA);
}
/* If persistent connections are disabled, destroy our SMTP object. */
if ($this->persist === false) {
$this->disconnect();
}
return true;
}
/**
* Connect to the SMTP server by instantiating a Net_SMTP object.
*
* @return mixed Returns a reference to the Net_SMTP object on success, or
* a PEAR_Error containing a descriptive error message on
* failure.
*
* @since 1.2.0
* @access public
*/
function &getSMTPObject()
{
if (is_object($this->_smtp) !== false) {
return $this->_smtp;
}
$this->_smtp = &new Net_SMTP($this->host,
$this->port,
$this->localhost,
$this->pipelining,
0, //timeout
null, //socket_options
$this->verify_peer,
$this->verify_peer_name,
$this->allow_self_signed);
/* If we still don't have an SMTP object at this point, fail. */
if (is_object($this->_smtp) === false) {
return Mail_smtp::raiseError('Failed to create a Net_SMTP object',
PEAR_MAIL_SMTP_ERROR_CREATE);
}
/* Configure the SMTP connection. */
if ($this->debug) {
$this->_smtp->setDebug(true);
}
/* Attempt to connect to the configured SMTP server. */
//if (PEAR::isError($res = $this->_smtp->connect($this->timeout))) {
if (($res = $this->_smtp->connect($this->timeout)) === false) {
$error = $this->_error('Failed to connect to ' .
$this->host . ':' . $this->port,
$res);
return Mail_smtp::raiseError($error, PEAR_MAIL_SMTP_ERROR_CONNECT);
}
/* Attempt to authenticate if authentication has been enabled. */
if ($this->auth) {
$method = is_string($this->auth) ? $this->auth : '';
//if (PEAR::isError($res = $this->_smtp->auth($this->username, $this->password, $method))) {
if (($res = $this->_smtp->auth($this->username, $this->password, $method)) === false) {
$error = $this->_error("$method authentication failure",
$res);
$this->_smtp->rset();
return Mail_smtp::raiseError($error, PEAR_MAIL_SMTP_ERROR_AUTH);
}
}
return $this->_smtp;
}
/**
* Add parameter associated with a SMTP service extension.
*
* @param string Extension keyword.
* @param string Any value the keyword needs.
*
* @since 1.2.0
* @access public
*/
function addServiceExtensionParameter($keyword, $value = null)
{
$this->_extparams[$keyword] = $value;
}
/**
* Disconnect and destroy the current SMTP connection.
*
* @return boolean True if the SMTP connection no longer exists.
*
* @since 1.1.9
* @access public
*/
function disconnect()
{
/* If we have an SMTP object, disconnect and destroy it. */
if (is_object($this->_smtp) && $this->_smtp->disconnect()) {
$this->_smtp = null;
}
/* We are disconnected if we no longer have an SMTP object. */
return ($this->_smtp === null);
}
/**
* Build a standardized string describing the current SMTP error.
*
* @param string $text Custom string describing the error context.
* @param object $error Reference to the current PEAR_Error object.
*
* @return string A string describing the current SMTP error.
*
* @since 1.1.7
* @access private
*/
function _error($text, &$error)
{
/* Split the SMTP response into a code and a response string. */
list($code, $response) = $this->_smtp->getResponse();
/* Build our standardized error string. */
return $text
// . ' [SMTP: ' . $error->getMessage()
. ' [SMTP: '
. " (code: $code, response: $response)]";
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
static function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "Mail<smtp> error: ". $message);
return false;
}
}
<?php
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Chuck Hagenbuch <chuck@horde.org> |
// | Jon Parise <jon@php.net> |
// | Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> |
// +----------------------------------------------------------------------+
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Net_SMTP-1.6.2.tgz
* https://github.com/pear/Net_SMTP Commit 558b92f5c2ecbb857094a3926a100e51211a08c2 2014/03/09
*
*
*/
//require_once 'PEAR.php';
//require_once 'PEAR/Exception.php';
/**
* Provides an implementation of the SMTP protocol using PEAR's
* Net_Socket:: class.
*
* @package Net_SMTP
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jon Parise <jon@php.net>
* @author Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
*
* @example basic.php A basic implementation of the Net_SMTP package.
*/
class Net_SMTP
{
/**
* The server to connect to.
*
* @var string
*/
public $host = 'localhost';
/**
* The port to connect to.
*
* @var int
*/
public $port = 25;
/**
* The value to give when sending EHLO or HELO.
*
* @var string
*/
public $localhost = 'localhost';
/**
* List of supported authentication methods, in preferential order.
*
* @var array
*/
public $auth_methods = array();
/**
* Use SMTP command pipelining (specified in RFC 2920) if the SMTP
* server supports it.
*
* When pipeling is enabled, rcptTo(), mailFrom(), sendFrom(),
* somlFrom() and samlFrom() do not wait for a response from the
* SMTP server but return immediately.
*
* @var bool
*/
public $pipelining = false;
/**
* Number of pipelined commands.
*
* @var int
*/
protected $_pipelined_commands = 0;
/**
* Should debugging output be enabled?
*
* @var boolean
*/
protected $_debug = false;
/**
* Debug output handler.
*
* @var callback
*/
protected $_debug_handler = null;
/**
* The socket resource being used to connect to the SMTP server.
*
* @var resource
*/
protected $_socket = null;
/**
* Array of socket options that will be passed to Net_Socket::connect().
*
* @see stream_context_create()
*
* @var array
*/
protected $_socket_options = null;
/**
* The socket I/O timeout value in seconds.
*
* @var int
*/
protected $_timeout = 0;
/**
* The most recent server response code.
*
* @var int
*/
protected $_code = -1;
/**
* The most recent server response arguments.
*
* @var array
*/
protected $_arguments = array();
/**
* Stores the SMTP server's greeting string.
*
* @var string
*/
protected $_greeting = null;
/**
* Stores detected features of the SMTP server.
*
* @var array
*/
protected $_esmtp = array();
/**
* Require verification of SSL certificate used.
*
* @var bool
*/
protected $_verify_peer;
/**
* Require verification of peer name
*
* @var bool
*/
protected $_verify_peer_name;
/**
* Allow self-signed certificates. Requires verify_peer
*
* @var bool
*/
protected $_allow_self_signed;
/**
* Instantiates a new Net_SMTP object, overriding any defaults
* with parameters that are passed in.
*
* If you have SSL support in PHP, you can connect to a server
* over SSL using an 'ssl://' prefix:
*
* // 465 is a common smtps port.
* $smtp = new Net_SMTP('ssl://mail.host.com', 465);
* $smtp->connect();
*
* @param string $host The server to connect to.
* @param integer $port The port to connect to.
* @param string $localhost The value to give when sending EHLO or HELO.
* @param boolean $pipeling Use SMTP command pipelining
* @param integer $timeout Socket I/O timeout in seconds.
* @param array $socket_options Socket stream_context_create() options.
* @param boolean $verify_peer Require verification of SSL certificate used
* @param boolean $verify_peer_name Require verification of peer name
* @param boolean $allow_self_signed Allow self-signed certificates. Requires verify_peer
*/
public function __construct($host = null, $port = null, $localhost = null,
$pipelining = false, $timeout = 0,
$socket_options = null,
$verify_peer = true, $verify_peer_name = true, $allow_self_signed = false)
{
if (isset($host)) {
$this->host = $host;
}
if (isset($port)) {
$this->port = $port;
}
if (isset($localhost)) {
$this->localhost = $localhost;
}
$this->pipelining = $pipelining;
$this->_socket = new Net_Socket();
$this->_socket_options = $socket_options;
// SSL connection, we need to modify the socket_options
if (strpos($this->host, "ssl://") === 0) {
if ($this->_socket_options == null)
$this->_socket_options = array();
if (!array_key_exists('ssl', $this->_socket_options))
$this->_socket_options['ssl'] = array();
$this->_socket_options['ssl']['verify_peer'] = $verify_peer;
$this->_socket_options['ssl']['allow_self_signed'] = $allow_self_signed;
// This option was introduced in 5.6
if (version_compare(phpversion(), "5.6.0", ">="))
$this->_socket_options['ssl']['verify_peer_name'] = $verify_peer_name;
}
$this->_timeout = $timeout;
// We also need this for use in the STARTTLS command
$this->_verify_peer = $verify_peer;
$this->_verify_peer_name = $verify_peer_name;
$this->_allow_self_signed = $allow_self_signed;
/* Include the Auth_SASL package. If the package is available, we
* enable the authentication methods that depend upon it. */
$this->setAuthMethod('CRAM-MD5', array($this, '_authCram_MD5'));
$this->setAuthMethod('DIGEST-MD5', array($this, '_authDigest_MD5'));
/* These standard authentication methods are always available. */
$this->setAuthMethod('LOGIN', array($this, '_authLogin'), false);
$this->setAuthMethod('PLAIN', array($this, '_authPlain'), false);
}
/**
* Set the socket I/O timeout value in seconds plus microseconds.
*
* @param integer $seconds Timeout value in seconds.
* @param integer $microseconds Additional value in microseconds.
*/
public function setTimeout($seconds, $microseconds = 0)
{
return $this->_socket->setTimeout($seconds, $microseconds);
}
/**
* Set the value of the debugging flag.
*
* @param boolean $debug New value for the debugging flag.
*/
public function setDebug($debug, $handler = null)
{
$this->_debug = $debug;
$this->_debug_handler = $handler;
}
/**
* Write the given debug text to the current debug output handler.
*
* @param string $message Debug message text.
*/
protected function _debug($message)
{
if ($this->_debug) {
if ($this->_debug_handler) {
call_user_func_array($this->_debug_handler,
array(&$this, $message));
} else {
ZLog::Write(LOGLEVEL_DEBUG, "Net_SMTP DEBUG: ". $message);
}
}
}
/**
* Send the given string of data to the server.
*
* @param string $data The string of data to send.
*
* @return integer The number of bytes that were actually written.
* @throws PEAR_Exception
*/
protected function _send($data)
{
$this->_debug("Send: $data");
$result = $this->_socket->write($data);
if ($result === false) {
// return Net_SMTP::raiseError('Failed to write to socket: ' . $result->getMessage(),
// $result);
return Net_SMTP::raiseError('Failed to write to socket: ');
}
return $result;
}
/**
* Send a command to the server with an optional string of
* arguments. A carriage return / linefeed (CRLF) sequence will
* be appended to each command string before it is sent to the
* SMTP server - an error will be thrown if the command string
* already contains any newline characters. Use _send() for
* commands that must contain newlines.
*
* @param string $command The SMTP command to send to the server.
* @param string $args A string of optional arguments to append
* to the command.
*
* @return integer The number of bytes that were actually written.
* @throws PEAR_Exception
*/
protected function _put($command, $args = '')
{
if (!empty($args)) {
$command .= ' ' . $args;
}
if (strcspn($command, "\r\n") !== strlen($command)) {
return Net_SMTP::raiseError('Commands cannot contain newlines');
}
return $this->_send($command . "\r\n");
}
/**
* Read a reply from the SMTP server. The reply consists of a response
* code and a response message.
*
* @see getResponse
*
* @param mixed $valid The set of valid response codes. These
* may be specified as an array of integer
* values or as a single integer value.
* @param bool $later Do not parse the response now, but wait
* until the last command in the pipelined
* command group
*
* @throws PEAR_Exception
*/
protected function _parseResponse($valid, $later = false)
{
$this->_code = -1;
$this->_arguments = array();
if ($later) {
++$this->_pipelined_commands;
return;
}
for ($i = 0; $i <= $this->_pipelined_commands; ++$i) {
while ($line = $this->_socket->readLine()) {
$this->_debug("Recv: $line");
/* If we receive an empty line, the connection was closed. */
if (empty($line)) {
$this->disconnect();
return Net_SMTP::raiseError('Connection was closed',
// null, PEAR_ERROR_RETURN);
null, 1);
}
/* Read the code and store the rest in the arguments array. */
$code = substr($line, 0, 3);
$this->_arguments[] = trim(substr($line, 4));
/* Check the syntax of the response code. */
if (is_numeric($code)) {
$this->_code = (int)$code;
} else {
$this->_code = -1;
break;
}
/* If this is not a multiline response, we're done. */
if (substr($line, 3, 1) != '-') {
break;
}
}
}
$this->_pipelined_commands = 0;
/* Compare the server's response code with the valid code/codes. */
if ((is_int($valid) && ($this->_code === $valid)) ||
(is_array($valid) && in_array($this->_code, $valid, true))) {
return;
}
return Net_SMTP::raiseError('Invalid response code received from server',
// $this->_code, PEAR_ERROR_RETURN);
$this->_code, 1);
}
/**
* Issue an SMTP command and verify its response.
*
* @param string $command The SMTP command string or data.
* @param mixed $valid The set of valid response codes. These
* may be specified as an array of integer
* values or as a single integer value.
*
* @throws PEAR_Exception
*/
public function command($command, $valid)
{
//if (PEAR::isError($error = $this->_put($command))) {
if (($error = $this->_put($command)) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse($valid))) {
if (($error = $this->_parseResponse($valid)) === false) {
return $error;
}
return true;
}
/**
* Return a 2-tuple containing the last response from the SMTP server.
*
* @return array A two-element array: the first element contains the
* response code as an integer and the second element
* contains the response's arguments as a string.
*/
public function getResponse()
{
return array($this->_code, join("\n", $this->_arguments));
}
/**
* Return the SMTP server's greeting string.
*
* @return string A string containing the greeting string, or null if a
* greeting has not been received.
*/
public function getGreeting()
{
return $this->_greeting;
}
/**
* Attempt to connect to the SMTP server.
*
* @param int $timeout The timeout value (in seconds) for the
* socket connection attempt.
* @param bool $persistent Should a persistent socket connection
* be used?
*
* @throws PEAR_Exception
*/
public function connect($timeout = null, $persistent = false)
{
$this->_greeting = null;
$result = $this->_socket->connect($this->host, $this->port,
$persistent, $timeout,
$this->_socket_options);
//if (PEAR::isError($result)) {
if ($result === false) {
// return Net_SMTP::raiseError('Failed to connect socket: ' .
// $result->getMessage());
return Net_SMTP::raiseError('Failed to connect socket: ');
}
/*
* Now that we're connected, reset the socket's timeout value for
* future I/O operations. This allows us to have different socket
* timeout values for the initial connection (our $timeout parameter)
* and all other socket operations.
*/
if ($this->_timeout > 0) {
//if (PEAR::isError($error = $this->setTimeout($this->_timeout))) {
if (($error = $this->setTimeout($this->_timeout)) === false) {
return $error;
}
}
//if (PEAR::isError($error = $this->_parseResponse(220))) {
if (($error = $this->_parseResponse(220)) === false) {
return $error;
}
/* Extract and store a copy of the server's greeting string. */
list(, $this->_greeting) = $this->getResponse();
//if (PEAR::isError($error = $this->_negotiate())) {
if (($error = $this->_negotiate()) === false) {
return $error;
}
return true;
}
/**
* Attempt to disconnect from the SMTP server.
*
* @throws PEAR_Exception
*/
public function disconnect()
{
//if (PEAR::isError($error = $this->_put('QUIT'))) {
if (($error = $this->_put('QUIT')) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(221))) {
if (($error = $this->_parseResponse(221)) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_socket->disconnect())) {
if (($error = $this->_socket->disconnect()) === false) {
return Net_SMTP::raiseError('Failed to disconnect socket: ' .
$error->getMessage());
}
return true;
}
/**
* Attempt to send the EHLO command and obtain a list of ESMTP
* extensions available, and failing that just send HELO.
*
* @throws PEAR_Exception
*/
protected function _negotiate()
{
//if (PEAR::isError($error = $this->_put('EHLO', $this->localhost))) {
if (($error = $this->_put('EHLO', $this->localhost)) === false) {
return $error;
}
//if (PEAR::isError($this->_parseResponse(250))) {
if (($this->_parseResponse(250)) === false) {
/* If the EHLO failed, try the simpler HELO command. */
//if (PEAR::isError($error = $this->_put('HELO', $this->localhost))) {
if (($error = $this->_put('HELO', $this->localhost)) === false) {
return $error;
}
//if (PEAR::isError($this->_parseResponse(250))) {
if (($this->_parseResponse(250)) === false) {
return Net_SMTP::raiseError('HELO was not accepted: ', $this->_code,
// PEAR_ERROR_RETURN);
1);
}
return true;
}
foreach ($this->_arguments as $argument) {
$verb = strtok($argument, ' ');
$arguments = substr($argument, strlen($verb) + 1,
strlen($argument) - strlen($verb) - 1);
$this->_esmtp[$verb] = $arguments;
}
if (!isset($this->_esmtp['PIPELINING'])) {
$this->pipelining = false;
}
return true;
}
/**
* Returns the name of the best authentication method that the server
* has advertised.
*
* @return mixed Returns a string containing the name of the best
* supported authentication method.
* @throws PEAR_Exception
*/
protected function _getBestAuthMethod()
{
$available_methods = explode(' ', $this->_esmtp['AUTH']);
foreach ($this->auth_methods as $method => $callback) {
if (in_array($method, $available_methods)) {
return $method;
}
}
return Net_SMTP::raiseError('No supported authentication methods',
// null, PEAR_ERROR_RETURN);
null, 1);
}
/**
* Attempt to do SMTP authentication.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
* @param string $method The requested authentication method. If none is
* specified, the best supported method will be
* used.
* @param bool $tls Flag indicating whether or not TLS should be
* attempted.
* @param string $authz An optional authorization identifier. If
* specified, this identifier will be used as the
* authorization proxy.
*
* @throws PEAR_Exception
*/
public function auth($uid, $pwd, $method = '', $tls = true, $authz = '')
{
/* We can only attempt a TLS connection if one has been requested,
* we're running PHP 5.1.0 or later, have access to the OpenSSL
* extension, are connected to an SMTP server which supports the
* STARTTLS extension, and aren't already connected over a secure
* (SSL) socket connection. */
if ($tls && version_compare(PHP_VERSION, '5.1.0', '>=') &&
extension_loaded('openssl') && isset($this->_esmtp['STARTTLS']) &&
strncasecmp($this->host, 'ssl://', 6) !== 0) {
/* Start the TLS connection attempt. */
//if (PEAR::isError($result = $this->_put('STARTTLS'))) {
if (($result = $this->_put('STARTTLS')) === false) {
return $result;
}
//if (PEAR::isError($result = $this->_parseResponse(220))) {
if (($result = $this->_parseResponse(220)) === false) {
return $result;
}
//if (PEAR::isError($result = $this->_socket->enableCrypto(true, STREAM_CRYPTO_METHOD_TLS_CLIENT))) {
if (($result = $this->_socket->enableCrypto(true, STREAM_CRYPTO_METHOD_TLS_CLIENT, $this->_verify_peer, $this->_verify_peer_name, $this->_allow_self_signed)) === false) {
return $result;
} elseif ($result !== true) {
return Net_SMTP::raiseError('STARTTLS failed');
}
/* Send EHLO again to recieve the AUTH string from the
* SMTP server. */
$this->_negotiate();
}
if (empty($this->_esmtp['AUTH'])) {
return Net_SMTP::raiseError('SMTP server does not support authentication');
}
/* If no method has been specified, get the name of the best
* supported method advertised by the SMTP server. */
if (empty($method)) {
//if (PEAR::isError($method = $this->_getBestAuthMethod())) {
if (($method = $this->_getBestAuthMethod()) === false) {
return $method;
}
} else {
$method = strtoupper($method);
}
if (!array_key_exists($method, $this->auth_methods)) {
return Net_SMTP::raiseError("$method is not a supported authentication method");
}
if (!is_callable($this->auth_methods[$method], false)) {
return Net_SMTP::raiseError("$method authentication method cannot be called");
}
if (is_array($this->auth_methods[$method])) {
list($object, $method) = $this->auth_methods[$method];
$result = $object->{$method}($uid, $pwd, $authz, $this);
} else {
$func = $this->auth_methods[$method];
$result = $func($uid, $pwd, $authz, $this);
}
/* If an error was encountered, return the PEAR_Error object. */
//if (PEAR::isError($result)) {
if ($result === false) {
return $result;
}
return true;
}
/**
* Add a new authentication method.
*
* @param string $name The authentication method name (e.g. 'PLAIN')
* @param mixed $callback The authentication callback (given as the name
* of a function or as an (object, method name)
* array).
* @param bool $prepend Should the new method be prepended to the list
* of available methods? This is the default
* behavior, giving the new method the highest
* priority.
*
* @throws PEAR_Exception
*/
public function setAuthMethod($name, $callback, $prepend = true)
{
if (!is_string($name)) {
return Net_SMTP::raiseError('Method name is not a string');
}
if (!is_string($callback) && !is_array($callback)) {
return Net_SMTP::raiseError('Method callback must be string or array');
}
if (is_array($callback) &&
(!is_object($callback[0]) || !is_string($callback[1]))) {
return Net_SMTP::raiseError('Bad mMethod callback array');
}
if ($prepend) {
$this->auth_methods = array_merge(array($name => $callback),
$this->auth_methods);
} else {
$this->auth_methods[$name] = $callback;
}
return true;
}
/**
* Authenticates the user using the DIGEST-MD5 method.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
* @param string $authz The optional authorization proxy identifier.
*
* @throws PEAR_Exception
*/
protected function _authDigest_MD5($uid, $pwd, $authz = '')
{
//if (PEAR::isError($error = $this->_put('AUTH', 'DIGEST-MD5'))) {
if (($error = $this->_put('AUTH', 'DIGEST-MD5')) === false) {
return $error;
}
/* 334: Continue authentication request */
//if (PEAR::isError($error = $this->_parseResponse(334))) {
if (($error = $this->_parseResponse(334)) === false) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
$challenge = base64_decode($this->_arguments[0]);
$digest = Auth_SASL::factory('digest-md5');
$auth_str = base64_encode($digest->getResponse($uid, $pwd, $challenge,
$this->host, "smtp",
$authz));
//if (PEAR::isError($error = $this->_put($auth_str))) {
if (($error = $this->_put($auth_str)) === false) {
return $error;
}
/* 334: Continue authentication request */
//if (PEAR::isError($error = $this->_parseResponse(334))) {
if (($error = $this->_parseResponse(334)) === false) {
return $error;
}
/* We don't use the protocol's third step because SMTP doesn't
* allow subsequent authentication, so we just silently ignore
* it. */
//if (PEAR::isError($error = $this->_put(''))) {
if (($error = $this->_put('')) === false) {
return $error;
}
/* 235: Authentication successful */
//if (PEAR::isError($error = $this->_parseResponse(235))) {
if (($error = $this->_parseResponse(235)) === false) {
return $error;
}
return true;
}
/**
* Authenticates the user using the CRAM-MD5 method.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
* @param string $authz The optional authorization proxy identifier.
*
* @throws PEAR_Exception
*/
protected function _authCRAM_MD5($uid, $pwd, $authz = '')
{
//if (PEAR::isError($error = $this->_put('AUTH', 'CRAM-MD5'))) {
if (($error = $this->_put('AUTH', 'CRAM-MD5')) === false) {
return $error;
}
/* 334: Continue authentication request */
//if (PEAR::isError($error = $this->_parseResponse(334))) {
if (($error = $this->_parseResponse(334)) === false) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
$challenge = base64_decode($this->_arguments[0]);
$cram = Auth_SASL::factory('cram-md5');
$auth_str = base64_encode($cram->getResponse($uid, $pwd, $challenge));
//if (PEAR::isError($error = $this->_put($auth_str))) {
if (($error = $this->_put($auth_str)) === false) {
return $error;
}
/* 235: Authentication successful */
//if (PEAR::isError($error = $this->_parseResponse(235))) {
if (($error = $this->_parseResponse(235)) === false) {
return $error;
}
return true;
}
/**
* Authenticates the user using the LOGIN method.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
* @param string $authz The optional authorization proxy identifier.
*
* @throws PEAR_Exception
*/
protected function _authLogin($uid, $pwd, $authz = '')
{
//if (PEAR::isError($error = $this->_put('AUTH', 'LOGIN'))) {
if (($error = $this->_put('AUTH', 'LOGIN')) === false) {
return $error;
}
/* 334: Continue authentication request */
//if (PEAR::isError($error = $this->_parseResponse(334))) {
if (($error = $this->_parseResponse(334)) === false) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
//if (PEAR::isError($error = $this->_put(base64_encode($uid)))) {
if (($error = $this->_put(base64_encode($uid))) === false) {
return $error;
}
/* 334: Continue authentication request */
//if (PEAR::isError($error = $this->_parseResponse(334))) {
if (($error = $this->_parseResponse(334)) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_put(base64_encode($pwd)))) {
if (($error = $this->_put(base64_encode($pwd))) === false) {
return $error;
}
/* 235: Authentication successful */
//if (PEAR::isError($error = $this->_parseResponse(235))) {
if (($error = $this->_parseResponse(235)) === false) {
return $error;
}
return true;
}
/**
* Authenticates the user using the PLAIN method.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
* @param string $authz The optional authorization proxy identifier.
*
* @throws PEAR_Exception
*/
protected function _authPlain($uid, $pwd, $authz = '')
{
//if (PEAR::isError($error = $this->_put('AUTH', 'PLAIN'))) {
if (($error = $this->_put('AUTH', 'PLAIN')) === false) {
return $error;
}
/* 334: Continue authentication request */
//if (PEAR::isError($error = $this->_parseResponse(334))) {
if (($error = $this->_parseResponse(334)) === false) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
$auth_str = base64_encode($authz . chr(0) . $uid . chr(0) . $pwd);
//if (PEAR::isError($error = $this->_put($auth_str))) {
if (($error = $this->_put($auth_str)) === false) {
return $error;
}
/* 235: Authentication successful */
//if (PEAR::isError($error = $this->_parseResponse(235))) {
if (($error = $this->_parseResponse(235)) === false) {
return $error;
}
return true;
}
/**
* Send the HELO command.
*
* @param string The domain name to say we are.
*
* @throws PEAR_Exception
*/
public function helo($domain)
{
//if (PEAR::isError($error = $this->_put('HELO', $domain))) {
if (($error = $this->_put('HELO', $domain)) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(250))) {
if (($error = $this->_parseResponse(250)) === false) {
return $error;
}
return true;
}
/**
* Return the list of SMTP service extensions advertised by the server.
*
* @return array The list of SMTP service extensions.
*/
public function getServiceExtensions()
{
return $this->_esmtp;
}
/**
* Send the MAIL FROM: command.
*
* @param string $sender The sender (reverse path) to set.
* @param string $params String containing additional MAIL parameters,
* such as the NOTIFY flags defined by RFC 1891
* or the VERP protocol.
*
* @throws PEAR_Exception
*/
public function mailFrom($sender, $params = null)
{
$args = "FROM:<$sender>";
if (is_string($params) && strlen($params)) {
$args .= ' ' . $params;
}
//if (PEAR::isError($error = $this->_put('MAIL', $args))) {
if (($error = $this->_put('MAIL', $args)) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(250, $this->pipelining))) {
if (($error = $this->_parseResponse(250, $this->pipelining)) === false) {
return $error;
}
return true;
}
/**
* Send the RCPT TO: command.
*
* @param string $recipient The recipient (forward path) to add.
* @param string $params String containing additional RCPT parameters,
* such as the NOTIFY flags defined by RFC 1891.
*
* @throws PEAR_Exception
*/
public function rcptTo($recipient, $params = null)
{
$args = "TO:<$recipient>";
if (is_string($params) && strlen($params)) {
$args .= ' ' . $params;
}
//if (PEAR::isError($error = $this->_put('RCPT', $args))) {
if (($error = $this->_put('RCPT', $args)) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(array(250, 251), $this->pipelining))) {
if (($error = $this->_parseResponse(array(250, 251), $this->pipelining)) === false) {
return $error;
}
return true;
}
/**
* Quote the data so that it meets SMTP standards.
*
* This is provided as a separate public function to facilitate
* easier overloading for the cases where it is desirable to
* customize the quoting behavior.
*
* @param string &$data The message text to quote. The string must be
* passed by reference, and the text will be
* modified in place.
*/
public function quotedata(&$data)
{
/* Because a single leading period (.) signifies an end to the
* data, legitimate leading periods need to be "doubled" ('..').
* Also: change Unix (\n) and Mac (\r) linefeeds into CRLF's
* (\r\n). */
$data = preg_replace(
array('/^\./m', '/(?:\r\n|\n|\r(?!\n))/'),
array('..', "\r\n"),
$data
);
}
/**
* Send the DATA command.
*
* @param mixed $data The message data, either as a string or an open
* file resource.
* @param string $headers The message headers. If $headers is provided,
* $data is assumed to contain only body data.
*
* @throws PEAR_Exception
*/
public function data($data, $headers = null)
{
/* Verify that $data is a supported type. */
if (!is_string($data) && !is_resource($data)) {
return Net_SMTP::raiseError('Expected a string or file resource');
}
/* Start by considering the size of the optional headers string. We
* also account for the addition 4 character "\r\n\r\n" separator
* sequence. */
$size = is_null($headers) ? 0 : strlen($headers) + 4;
if (is_resource($data)) {
$stat = fstat($data);
if ($stat === false) {
return Net_SMTP::raiseError('Failed to get file size');
}
$size += $stat['size'];
} else {
$size += strlen($data);
}
/* RFC 1870, section 3, subsection 3 states "a value of zero indicates
* that no fixed maximum message size is in force". Furthermore, it
* says that if "the parameter is omitted no information is conveyed
* about the server's fixed maximum message size". */
$limit = isset($this->_esmtp['SIZE']) ? $this->_esmtp['SIZE'] : 0;
if ($limit > 0 && $size >= $limit) {
$this->disconnect();
return Net_SMTP::raiseError('Message size exceeds server limit');
}
/* Initiate the DATA command. */
//if (PEAR::isError($error = $this->_put('DATA'))) {
if (($error = $this->_put('DATA')) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(354))) {
if (($error = $this->_parseResponse(354)) === false) {
return $error;
}
/* If we have a separate headers string, send it first. */
if (!is_null($headers)) {
$this->quotedata($headers);
//if (PEAR::isError($result = $this->_send($headers . "\r\n\r\n"))) {
if (($result = $this->_send($headers . "\r\n\r\n")) === false) {
return $result;
}
}
/* Now we can send the message body data. */
if (is_resource($data)) {
/* Stream the contents of the file resource out over our socket
* connection, line by line. Each line must be run through the
* quoting routine. */
while (strlen($line = fread($data, 8192)) > 0) {
/* If the last character is an newline, we need to grab the
* next character to check to see if it is a period. */
while (!feof($data)) {
$char = fread($data, 1);
$line .= $char;
if ($char != "\n") {
break;
}
}
$this->quotedata($line);
//if (PEAR::isError($result = $this->_send($line))) {
if (($result = $this->_send($line)) === false) {
return $result;
}
}
} else {
/* Break up the data by sending one chunk (up to 512k) at a time.
* This approach reduces our peak memory usage. */
for ($offset = 0; $offset < $size;) {
$end = $offset + 512000;
/* Ensure we don't read beyond our data size or span multiple
* lines. quotedata() can't properly handle character data
* that's split across two line break boundaries. */
if ($end >= $size) {
$end = $size;
} else {
for (; $end < $size; $end++) {
if ($data[$end] != "\n") {
break;
}
}
}
/* Extract our chunk and run it through the quoting routine. */
$chunk = substr($data, $offset, $end - $offset);
$this->quotedata($chunk);
/* If we run into a problem along the way, abort. */
//if (PEAR::isError($result = $this->_send($chunk))) {
if (($result = $this->_send($chunk)) === false) {
return $result;
}
/* Advance the offset to the end of this chunk. */
$offset = $end;
}
}
/* Finally, send the DATA terminator sequence. */
//if (PEAR::isError($result = $this->_send("\r\n.\r\n"))) {
if (($result = $this->_send("\r\n.\r\n")) === false) {
return $result;
}
/* Verify that the data was successfully received by the server. */
//if (PEAR::isError($error = $this->_parseResponse(250, $this->pipelining))) {
if (($error = $this->_parseResponse(250, $this->pipelining)) === false) {
return $error;
}
return true;
}
/**
* Send the SEND FROM: command.
*
* @param string $path The reverse path to send.
*
* @throws PEAR_Exception
*/
public function sendFrom($path)
{
//if (PEAR::isError($error = $this->_put('SEND', "FROM:<$path>"))) {
if (($error = $this->_put('SEND', "FROM:<$path>")) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(250, $this->pipelining))) {
if (($error = $this->_parseResponse(250, $this->pipelining)) === false) {
return $error;
}
return true;
}
/**
* Send the SOML FROM: command.
*
* @param string $path The reverse path to send.
*
* @throws PEAR_Exception
*/
public function somlFrom($path)
{
//if (PEAR::isError($error = $this->_put('SOML', "FROM:<$path>"))) {
if (($error = $this->_put('SOML', "FROM:<$path>")) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(250, $this->pipelining))) {
if (($error = $this->_parseResponse(250, $this->pipelining)) === false) {
return $error;
}
return true;
}
/**
* Send the SAML FROM: command.
*
* @param string $path The reverse path to send.
*
* @throws PEAR_Exception
*/
public function samlFrom($path)
{
//if (PEAR::isError($error = $this->_put('SAML', "FROM:<$path>"))) {
if (($error = $this->_put('SAML', "FROM:<$path>")) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(250, $this->pipelining))) {
if (($error = $this->_parseResponse(250, $this->pipelining)) === false) {
return $error;
}
return true;
}
/**
* Send the RSET command.
*
* @throws PEAR_Exception
*/
public function rset()
{
//if (PEAR::isError($error = $this->_put('RSET'))) {
if (($error = $this->_put('RSET')) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(250, $this->pipelining))) {
if (($error = $this->_parseResponse(250, $this->pipelining)) === false) {
return $error;
}
return true;
}
/**
* Send the VRFY command.
*
* @param string $string The string to verify
*
* @throws PEAR_Exception
*/
public function vrfy($string)
{
/* Note: 251 is also a valid response code */
//if (PEAR::isError($error = $this->_put('VRFY', $string))) {
if (($error = $this->_put('VRFY', $string)) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(array(250, 252)))) {
if (($error = $this->_parseResponse(array(250, 252))) === false) {
return $error;
}
return true;
}
/**
* Send the NOOP command.
*
* @throws PEAR_Exception
*/
public function noop()
{
//if (PEAR::isError($error = $this->_put('NOOP'))) {
if (($error = $this->_put('NOOP')) === false) {
return $error;
}
//if (PEAR::isError($error = $this->_parseResponse(250))) {
if (($error = $this->_parseResponse(250)) === false) {
return $error;
}
return true;
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
static function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "Net_SMTP error: ". $message);
return false;
}
}
<?php
/**
* Net_Socket
*
* PHP Version 4
*
* Copyright (c) 1997-2013 The PHP Group
*
* This source file is subject to version 2.0 of the PHP license,
* that is bundled with this package in the file LICENSE, and is
* available at through the world-wide-web at
* http://www.php.net/license/2_02.txt.
* If you did not receive a copy of the PHP license and are unable to
* obtain it through the world-wide-web, please send a note to
* license@php.net so we can mail you a copy immediately.
*
* Authors: Stig Bakken <ssb@php.net>
* Chuck Hagenbuch <chuck@horde.org>
*
* @category Net
* @package Net_Socket
* @author Stig Bakken <ssb@php.net>
* @author Chuck Hagenbuch <chuck@horde.org>
* @copyright 1997-2003 The PHP Group
* @license http://www.php.net/license/2_02.txt PHP 2.02
* @link http://pear.php.net/packages/Net_Socket
*/
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError(), and defining OS_WINDOWS
*
* Reference implementation used:
* http://download.pear.php.net/package/Net_Socket-1.0.14.tgz
*
*
*/
//require_once 'PEAR.php';
if (substr(PHP_OS, 0, 3) == 'WIN') {
define('OS_WINDOWS', true);
} else {
define('OS_WINDOWS', false);
}
define('NET_SOCKET_READ', 1);
define('NET_SOCKET_WRITE', 2);
define('NET_SOCKET_ERROR', 4);
/**
* Generalized Socket class.
*
* @category Net
* @package Net_Socket
* @author Stig Bakken <ssb@php.net>
* @author Chuck Hagenbuch <chuck@horde.org>
* @copyright 1997-2003 The PHP Group
* @license http://www.php.net/license/2_02.txt PHP 2.02
* @link http://pear.php.net/packages/Net_Socket
*/
//class Net_Socket extends PEAR
class Net_Socket
{
/**
* Socket file pointer.
* @var resource $fp
*/
var $fp = null;
/**
* Whether the socket is blocking. Defaults to true.
* @var boolean $blocking
*/
var $blocking = true;
/**
* Whether the socket is persistent. Defaults to false.
* @var boolean $persistent
*/
var $persistent = false;
/**
* The IP address to connect to.
* @var string $addr
*/
var $addr = '';
/**
* The port number to connect to.
* @var integer $port
*/
var $port = 0;
/**
* Number of seconds to wait on socket operations before assuming
* there's no more data. Defaults to no timeout.
* @var integer|float $timeout
*/
var $timeout = null;
/**
* Number of bytes to read at a time in readLine() and
* readAll(). Defaults to 2048.
* @var integer $lineLength
*/
var $lineLength = 2048;
/**
* The string to use as a newline terminator. Usually "\r\n" or "\n".
* @var string $newline
*/
var $newline = "\r\n";
/**
* Connect to the specified port. If called when the socket is
* already connected, it disconnects and connects again.
*
* @param string $addr IP address or host name (may be with protocol prefix).
* @param integer $port TCP port number.
* @param boolean $persistent (optional) Whether the connection is
* persistent (kept open between requests
* by the web server).
* @param integer $timeout (optional) Connection socket timeout.
* @param array $options See options for stream_context_create.
*
* @access public
*
* @return boolean|PEAR_Error True on success or a PEAR_Error on failure.
*/
function connect($addr, $port = 0, $persistent = null,
$timeout = null, $options = null)
{
if (is_resource($this->fp)) {
@fclose($this->fp);
$this->fp = null;
}
if (!$addr) {
return $this->raiseError('$addr cannot be empty');
} else if (strspn($addr, ':.0123456789') == strlen($addr)) {
$this->addr = strpos($addr, ':') !== false ? '['.$addr.']' : $addr;
} else {
$this->addr = $addr;
}
$this->port = $port % 65536;
if ($persistent !== null) {
$this->persistent = $persistent;
}
$openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
$errno = 0;
$errstr = '';
$old_track_errors = @ini_set('track_errors', 1);
if ($timeout <= 0) {
$timeout = @ini_get('default_socket_timeout');
}
if ($options && function_exists('stream_context_create')) {
$context = stream_context_create($options);
// Since PHP 5 fsockopen doesn't allow context specification
if (function_exists('stream_socket_client')) {
$flags = STREAM_CLIENT_CONNECT;
if ($this->persistent) {
$flags = STREAM_CLIENT_PERSISTENT;
}
$addr = $this->addr . ':' . $this->port;
$fp = stream_socket_client($addr, $errno, $errstr,
$timeout, $flags, $context);
} else {
$fp = @$openfunc($this->addr, $this->port, $errno,
$errstr, $timeout, $context);
}
} else {
$fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout);
}
if (!$fp) {
if ($errno == 0 && !strlen($errstr) && isset($php_errormsg)) {
$errstr = $php_errormsg;
}
@ini_set('track_errors', $old_track_errors);
return $this->raiseError($errstr, $errno);
}
@ini_set('track_errors', $old_track_errors);
$this->fp = $fp;
$this->setTimeout();
return $this->setBlocking($this->blocking);
}
/**
* Disconnects from the peer, closes the socket.
*
* @access public
* @return mixed true on success or a PEAR_Error instance otherwise
*/
function disconnect()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
@fclose($this->fp);
$this->fp = null;
return true;
}
/**
* Set the newline character/sequence to use.
*
* @param string $newline Newline character(s)
* @return boolean True
*/
function setNewline($newline)
{
$this->newline = $newline;
return true;
}
/**
* Find out if the socket is in blocking mode.
*
* @access public
* @return boolean The current blocking mode.
*/
function isBlocking()
{
return $this->blocking;
}
/**
* Sets whether the socket connection should be blocking or
* not. A read call to a non-blocking socket will return immediately
* if there is no data available, whereas it will block until there
* is data for blocking sockets.
*
* @param boolean $mode True for blocking sockets, false for nonblocking.
*
* @access public
* @return mixed true on success or a PEAR_Error instance otherwise
*/
function setBlocking($mode)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$this->blocking = $mode;
stream_set_blocking($this->fp, (int)$this->blocking);
return true;
}
/**
* Sets the timeout value on socket descriptor,
* expressed in the sum of seconds and microseconds
*
* @param integer $seconds Seconds.
* @param integer $microseconds Microseconds, optional.
*
* @access public
* @return mixed True on success or false on failure or
* a PEAR_Error instance when not connected
*/
function setTimeout($seconds = null, $microseconds = null)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
if ($seconds === null && $microseconds === null) {
$seconds = (int) $this->timeout;
$microseconds = (int) (($this->timeout - $seconds) * 1000000);
} else {
$this->timeout = $seconds + $microseconds/1000000;
}
if ($this->timeout > 0) {
return stream_set_timeout($this->fp, (int) $seconds, (int) $microseconds);
}
else {
return false;
}
}
/**
* Sets the file buffering size on the stream.
* See php's stream_set_write_buffer for more information.
*
* @param integer $size Write buffer size.
*
* @access public
* @return mixed on success or an PEAR_Error object otherwise
*/
function setWriteBuffer($size)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$returned = stream_set_write_buffer($this->fp, $size);
if ($returned == 0) {
return true;
}
return $this->raiseError('Cannot set write buffer.');
}
/**
* Returns information about an existing socket resource.
* Currently returns four entries in the result array:
*
* <p>
* timed_out (bool) - The socket timed out waiting for data<br>
* blocked (bool) - The socket was blocked<br>
* eof (bool) - Indicates EOF event<br>
* unread_bytes (int) - Number of bytes left in the socket buffer<br>
* </p>
*
* @access public
* @return mixed Array containing information about existing socket
* resource or a PEAR_Error instance otherwise
*/
function getStatus()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
return stream_get_meta_data($this->fp);
}
/**
* Get a specified line of data
*
* @param int $size Reading ends when size - 1 bytes have been read,
* or a newline or an EOF (whichever comes first).
* If no size is specified, it will keep reading from
* the stream until it reaches the end of the line.
*
* @access public
* @return mixed $size bytes of data from the socket, or a PEAR_Error if
* not connected. If an error occurs, FALSE is returned.
*/
function gets($size = null)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
if (is_null($size)) {
return @fgets($this->fp);
} else {
return @fgets($this->fp, $size);
}
}
/**
* Read a specified amount of data. This is guaranteed to return,
* and has the added benefit of getting everything in one fread()
* chunk; if you know the size of the data you're getting
* beforehand, this is definitely the way to go.
*
* @param integer $size The number of bytes to read from the socket.
*
* @access public
* @return $size bytes of data from the socket, or a PEAR_Error if
* not connected.
*/
function read($size)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
return @fread($this->fp, $size);
}
/**
* Write a specified amount of data.
*
* @param string $data Data to write.
* @param integer $blocksize Amount of data to write at once.
* NULL means all at once.
*
* @access public
* @return mixed If the socket is not connected, returns an instance of
* PEAR_Error.
* If the write succeeds, returns the number of bytes written.
* If the write fails, returns false.
* If the socket times out, returns an instance of PEAR_Error.
*/
function write($data, $blocksize = null)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
if (is_null($blocksize) && !OS_WINDOWS) {
$written = @fwrite($this->fp, $data);
// Check for timeout or lost connection
if (!$written) {
$meta_data = $this->getStatus();
if (!is_array($meta_data)) {
return $meta_data; // PEAR_Error
}
if (!empty($meta_data['timed_out'])) {
return $this->raiseError('timed out');
}
}
return $written;
} else {
if (is_null($blocksize)) {
$blocksize = 1024;
}
$pos = 0;
$size = strlen($data);
while ($pos < $size) {
$written = @fwrite($this->fp, substr($data, $pos, $blocksize));
// Check for timeout or lost connection
if (!$written) {
$meta_data = $this->getStatus();
if (!is_array($meta_data)) {
return $meta_data; // PEAR_Error
}
if (!empty($meta_data['timed_out'])) {
return $this->raiseError('timed out');
}
return $written;
}
$pos += $written;
}
return $pos;
}
}
/**
* Write a line of data to the socket, followed by a trailing newline.
*
* @param string $data Data to write
*
* @access public
* @return mixed fwrite() result, or PEAR_Error when not connected
*/
function writeLine($data)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
return fwrite($this->fp, $data . $this->newline);
}
/**
* Tests for end-of-file on a socket descriptor.
*
* Also returns true if the socket is disconnected.
*
* @access public
* @return bool
*/
function eof()
{
return (!is_resource($this->fp) || feof($this->fp));
}
/**
* Reads a byte of data
*
* @access public
* @return 1 byte of data from the socket, or a PEAR_Error if
* not connected.
*/
function readByte()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
return ord(@fread($this->fp, 1));
}
/**
* Reads a word of data
*
* @access public
* @return 1 word of data from the socket, or a PEAR_Error if
* not connected.
*/
function readWord()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$buf = @fread($this->fp, 2);
return (ord($buf[0]) + (ord($buf[1]) << 8));
}
/**
* Reads an int of data
*
* @access public
* @return integer 1 int of data from the socket, or a PEAR_Error if
* not connected.
*/
function readInt()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$buf = @fread($this->fp, 4);
return (ord($buf[0]) + (ord($buf[1]) << 8) +
(ord($buf[2]) << 16) + (ord($buf[3]) << 24));
}
/**
* Reads a zero-terminated string of data
*
* @access public
* @return string, or a PEAR_Error if
* not connected.
*/
function readString()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$string = '';
while (($char = @fread($this->fp, 1)) != "\x00") {
$string .= $char;
}
return $string;
}
/**
* Reads an IP Address and returns it in a dot formatted string
*
* @access public
* @return Dot formatted string, or a PEAR_Error if
* not connected.
*/
function readIPAddress()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$buf = @fread($this->fp, 4);
return sprintf('%d.%d.%d.%d', ord($buf[0]), ord($buf[1]),
ord($buf[2]), ord($buf[3]));
}
/**
* Read until either the end of the socket or a newline, whichever
* comes first. Strips the trailing newline from the returned data.
*
* @access public
* @return All available data up to a newline, without that
* newline, or until the end of the socket, or a PEAR_Error if
* not connected.
*/
function readLine()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$line = '';
$timeout = time() + $this->timeout;
while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
$line .= @fgets($this->fp, $this->lineLength);
if (substr($line, -1) == "\n") {
return rtrim($line, $this->newline);
}
}
return $line;
}
/**
* Read until the socket closes, or until there is no more data in
* the inner PHP buffer. If the inner buffer is empty, in blocking
* mode we wait for at least 1 byte of data. Therefore, in
* blocking mode, if there is no data at all to be read, this
* function will never exit (unless the socket is closed on the
* remote end).
*
* @access public
*
* @return string All data until the socket closes, or a PEAR_Error if
* not connected.
*/
function readAll()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$data = '';
while (!feof($this->fp)) {
$data .= @fread($this->fp, $this->lineLength);
}
return $data;
}
/**
* Runs the equivalent of the select() system call on the socket
* with a timeout specified by tv_sec and tv_usec.
*
* @param integer $state Which of read/write/error to check for.
* @param integer $tv_sec Number of seconds for timeout.
* @param integer $tv_usec Number of microseconds for timeout.
*
* @access public
* @return False if select fails, integer describing which of read/write/error
* are ready, or PEAR_Error if not connected.
*/
function select($state, $tv_sec, $tv_usec = 0)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
$read = null;
$write = null;
$except = null;
if ($state & NET_SOCKET_READ) {
$read[] = $this->fp;
}
if ($state & NET_SOCKET_WRITE) {
$write[] = $this->fp;
}
if ($state & NET_SOCKET_ERROR) {
$except[] = $this->fp;
}
if (false === ($sr = stream_select($read, $write, $except,
$tv_sec, $tv_usec))) {
return false;
}
$result = 0;
if (count($read)) {
$result |= NET_SOCKET_READ;
}
if (count($write)) {
$result |= NET_SOCKET_WRITE;
}
if (count($except)) {
$result |= NET_SOCKET_ERROR;
}
return $result;
}
/**
* Turns encryption on/off on a connected socket.
*
* @param bool $enabled Set this parameter to true to enable encryption
* and false to disable encryption.
* @param integer $type Type of encryption. See stream_socket_enable_crypto()
* for values.
* @param boolean $verify_peer Require verification of SSL certificate used
* @param boolean $verify_peer_name Require verification of peer name
* @param boolean $allow_self_signed Allow self-signed certificates. Requires verify_peer
*
* @see http://se.php.net/manual/en/function.stream-socket-enable-crypto.php
* @access public
* @return false on error, true on success and 0 if there isn't enough data
* and the user should try again (non-blocking sockets only).
* A PEAR_Error object is returned if the socket is not
* connected
*/
function enableCrypto($enabled, $type, $verify_peer, $verify_peer_name, $allow_self_signed)
{
if (version_compare(phpversion(), "5.1.0", ">=")) {
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
// 5.6.0 Added verify_peer_name. verify_peer default changed to TRUE.
if (version_compare(phpversion(), "5.6.0", ">="))
stream_context_set_option($this->fp, array('ssl' => array('verify_peer' => $verify_peer, 'verify_peer_name' => $verify_peer_name, 'allow_self_signed' => $allow_self_signed)));
else
stream_context_set_option($this->fp, array('ssl' => array('verify_peer' => $verify_peer, 'allow_self_signed' => $allow_self_signed)));
return @stream_socket_enable_crypto($this->fp, $enabled, $type);
} else {
$msg = 'Net_Socket::enableCrypto() requires php version >= 5.1.0';
return $this->raiseError($msg);
}
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
function raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "Net_Socket error: ". $message);
return false;
}
}
BackendIMAP - NOTES
===================
This backend support the Search operation in the mailbox.
Since the IMAP search operation is pretty slow, with a medium/big mailbox, or with a lots of folders,
the mobile device will timeout the operation before this is completed on server.
I'm using Dovecot + FTS-SOLR plugin so the real search is done against an Apache SOLR server.
It reduces a 1-2 minutes search to 1-5 seconds, and the response is given to the mobile device in time.
CHANGESSINK
===========
It supports ChangesSink method that will detect and send faster changes to your device.
SMTP
====
You can choice between 3 methods for send mails: mail (php mail), sendmail (native binary), smtp (php smtp direct connection).
Remember to configure it in the config.php
"mail" is a sendmail wrapper in Linux.
MBCONVERT
=========
A lot of messages come with wrong encoding, to them to look better with any device you can pre-convert them to UTF-8.
You will need to install the php-mbstring module
\ No newline at end of file
REQUIREMENTS:
php-imap
php-mbstring (optional but recommended)
libawl-php
IMAP server (Dovecot, Courier...)
\ No newline at end of file
*Drenalina SRL (www.drenalina.com)* sponsored the development of the following features in the BackendIMAP, any existing bug it's my fault not theirs ;-)
Thank you very much for helping to improve it!!
- Meeting invitations and attendees
\ No newline at end of file
......@@ -54,23 +54,174 @@ define('IMAP_PORT', 143);
// best cross-platform compatibility (see http://php.net/imap_open for options)
define('IMAP_OPTIONS', '/notls/norsh');
// overwrite the "from" header if it isn't set when sending emails
// options: 'username' - the username will be set (usefull if your login is equal to your emailaddress)
// 'domain' - the value of the "domain" field is used
// '@mydomain.com' - the username is used and the given string will be appended
define('IMAP_DEFAULTFROM', '');
// copy outgoing mail to this folder. If not set z-push will try the default folders
define('IMAP_SENTFOLDER', '');
// Mark messages as read when moving to Trash.
// BE AWARE that you will lose the unread flag, but some mail clients do this so the Trash folder doesn't get boldened
define('IMAP_AUTOSEEN_ON_DELETE', false);
// IMPORTANT: BASIC IMAP FOLDERS [ask your mail admin]
// We can have diferent cases (case insensitive):
// 1.
// inbox
// sent
// drafts
// trash
// 2.
// inbox
// common.sent
// common.drafts
// common.trash
// 3.
// common.inbox
// common.sent
// common.drafts
// common.trash
// 4.
// common
// common.sent
// common.drafts
// common.trash
//
// gmail is a special case, where the default folders are under the [gmail] prefix and the folders defined by the user are under INBOX.
// This configuration seems to work:
// define('IMAP_FOLDER_PREFIX', '');
// define('IMAP_FOLDER_INBOX', 'INBOX');
// define('IMAP_FOLDER_SENT', '[Gmail]/Sent');
// define('IMAP_FOLDER_DRAFTS', '[Gmail]/Drafts');
// define('IMAP_FOLDER_TRASH', '[Gmail]/Trash');
// define('IMAP_FOLDER_SPAM', '[Gmail]/Spam');
// define('IMAP_FOLDER_ARCHIVE', '[Gmail]/All Mail');
// Since I know you won't configure this, I will raise an error unless you do.
// When configured set this to true to remove the error
define('IMAP_FOLDER_CONFIGURED', false);
// Folder prefix is the common part in your names (3, 4)
define('IMAP_FOLDER_PREFIX', '');
// Inbox will have the preffix preppend (3 & 4 to true)
define('IMAP_FOLDER_PREFIX_IN_INBOX', false);
// Inbox folder name (case doesn't matter) - (empty in 4)
define('IMAP_FOLDER_INBOX', 'INBOX');
// forward messages inline (default false - as attachment)
define('IMAP_INLINE_FORWARD', false);
// Sent folder name (case doesn't matter)
define('IMAP_FOLDER_SENT', 'SENT');
// use imap_mail() to send emails (default) - if false mail() is used
define('IMAP_USE_IMAPMAIL', true);
// Draft folder name (case doesn't matter)
define('IMAP_FOLDER_DRAFT', 'DRAFTS');
// Trash folder name (case doesn't matter)
define('IMAP_FOLDER_TRASH', 'TRASH');
// Spam folder name (case doesn't matter). Only showed as special by iOS devices
define('IMAP_FOLDER_SPAM', 'SPAM');
// Archive folder name (case doesn't matter). Only showed as special by iOS devices
define('IMAP_FOLDER_ARCHIVE', 'ARCHIVE');
// forward messages inline (default true - inlined)
define('IMAP_INLINE_FORWARD', true);
/* BEGIN fmbiete's contribution r1527, ZP-319 */
// list of folders we want to exclude from sync. Names, or part of it, separated by |
// example: dovecot.sieve|archive|spam
define('IMAP_EXCLUDED_FOLDERS', '');
/* END fmbiete's contribution r1527, ZP-319 */
// overwrite the "from" header with some value
// options:
// '' - do nothing, use the From header
// 'username' - the username will be set (usefull if your login is equal to your emailaddress)
// 'domain' - the value of the "domain" field is used
// 'sql' - the username will be the result of a sql query. REMEMBER TO INSTALL PHP-PDO AND PHP-DATABASE
// 'ldap' - the username will be the result of a ldap query. REMEMBER TO INSTALL PHP-LDAP!!
// '@mydomain.com' - the username is used and the given string will be appended
define('IMAP_DEFAULTFROM', '');
// DSN: formatted PDO connection string
// mysql:host=xxx;port=xxx;dbname=xxx
// USER: username to DB
// PASSWORD: password to DB
// OPTIONS: array with options needed
// QUERY: query to execute
// FIELDS: columns in the query
// FROM: string that will be the from, replacing the column names with the values
define('IMAP_FROM_SQL_DSN', '');
define('IMAP_FROM_SQL_USER', '');
define('IMAP_FROM_SQL_PASSWORD', '');
define('IMAP_FROM_SQL_OPTIONS', serialize(array(PDO::ATTR_PERSISTENT => true)));
define('IMAP_FROM_SQL_QUERY', "select first_name, last_name, mail_address from users where mail_address = '#username@#domain'");
define('IMAP_FROM_SQL_FIELDS', serialize(array('first_name', 'last_name', 'mail_address')));
define('IMAP_FROM_SQL_FROM', '#first_name #last_name <#mail_address>');
define('IMAP_FROM_SQL_FULLNAME', '#first_name #last_name');
// SERVER: ldap server
// SERVER_PORT: ldap port
// USER: dn to use for connecting
// PASSWORD: password
// QUERY: query to execute
// FIELDS: columns in the query
// FROM: string that will be the from, replacing the field names with the values
define('IMAP_FROM_LDAP_SERVER', 'localhost');
define('IMAP_FROM_LDAP_SERVER_PORT', '389');
define('IMAP_FROM_LDAP_USER', 'cn=zpush,ou=servers,dc=zpush,dc=org');
define('IMAP_FROM_LDAP_PASSWORD', 'password');
define('IMAP_FROM_LDAP_BASE', 'dc=zpush,dc=org');
define('IMAP_FROM_LDAP_QUERY', '(mail=#username@#domain)');
define('IMAP_FROM_LDAP_FIELDS', serialize(array('givenname', 'sn', 'mail')));
define('IMAP_FROM_LDAP_FROM', '#givenname #sn <#mail>');
define('IMAP_FROM_LDAP_FULLNAME', '#givenname #sn');
// Method used for sending mail
// mail => mail() php function
// sendmail => sendmail executable
// smtp => direct connection against SMTP
define('IMAP_SMTP_METHOD', 'mail');
global $imap_smtp_params;
// SMTP Parameters
// mail : no params
$imap_smtp_params = array();
// sendmail
//$imap_smtp_params = array('sendmail_path' => '/usr/bin/sendmail', 'sendmail_args' => '-i');
// smtp
// "host" - The server to connect. Default is localhost.
// "port" - The port to connect. Default is 25.
// "auth" - Whether or not to use SMTP authentication. Default is FALSE.
// "username" - The username to use for SMTP authentication. "imap_username" for using the same username as the imap server
// "password" - The password to use for SMTP authentication. "imap_password" for using the same password as the imap server
// "localhost" - The value to give when sending EHLO or HELO. Default is localhost
// "timeout" - The SMTP connection timeout. Default is NULL (no timeout).
// "verp" - Whether to use VERP or not. Default is FALSE.
// "debug" - Whether to enable SMTP debug mode or not. Default is FALSE.
// "persist" - Indicates whether or not the SMTP connection should persist over multiple calls to the send() method.
// "pipelining" - Indicates whether or not the SMTP commands pipelining should be used.
// "verify_peer" - Require verification of SSL certificate used. Default is TRUE.
// "verify_peer_name" - Require verification of peer name. Default is TRUE.
// "allow_self_signed" - Allow self-signed certificates. Requires verify_peer. Default is FALSE.
//$imap_smtp_params = array('host' => 'localhost', 'port' => 25, 'auth' => false);
// If you want to use SSL with port 25 or port 465 you must preppend "ssl://" before the hostname or IP of your SMTP server
// IMPORTANT: To use SSL you must use PHP 5.1 or later, install openssl libs and use ssl:// within the host variable
// IMPORTANT: To use SSL with PHP 5.6 you should set verify_peer, verify_peer_name and allow_self_signed
//$imap_smtp_params = array('host' => 'ssl://localhost', 'port' => 465, 'auth' => true, 'username' => 'imap_username', 'password' => 'imap_password');
// If you are using IMAP_SMTP_METHOD = mail or sendmail and your sent messages are not correctly displayed you can change this to "\n".
// BUT, it doesn't comply with RFC 2822 and will break if using smtp method
define('MAIL_MIMEPART_CRLF', "\r\n");
// A file containing file mime types->extension mappings.
// SELINUX users: make sure the file has a security context accesible by your apache/php-fpm process
define('SYSTEM_MIME_TYPES_MAPPING', '/etc/mime.types');
// Use BackendCalDAV for Meetings. You cannot hope to get that functionality working without a caldav backend.
define('IMAP_MEETING_USE_CALDAV', false);
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
<?php
/**
* The Mail_mimePart class is used to create MIME E-mail messages
*
* This class enables you to manipulate and build a mime email
* from the ground up. The Mail_Mime class is a userfriendly api
* to this class for people who aren't interested in the internals
* of mime mail.
* This class however allows full control over the email.
*
* Compatible with PHP versions 4 and 5
*
* LICENSE: This LICENSE is in the BSD license style.
* Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org>
* Copyright (c) 2003-2006, PEAR <pear-group@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the authors, nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* @category Mail
* @package Mail_Mime
* @author Richard Heyes <richard@phpguru.org>
* @author Cipriano Groenendal <cipri@php.net>
* @author Sean Coates <sean@php.net>
* @author Aleksander Machniak <alec@php.net>
* @copyright 2003-2006 PEAR <pear-group@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Mail_mime
*/
/**
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail_Mime-1.8.9.tgz
*
*
*/
/**
* The Mail_mimePart class is used to create MIME E-mail messages
*
* This class enables you to manipulate and build a mime email
* from the ground up. The Mail_Mime class is a userfriendly api
* to this class for people who aren't interested in the internals
* of mime mail.
* This class however allows full control over the email.
*
* @category Mail
* @package Mail_Mime
* @author Richard Heyes <richard@phpguru.org>
* @author Cipriano Groenendal <cipri@php.net>
* @author Sean Coates <sean@php.net>
* @author Aleksander Machniak <alec@php.net>
* @copyright 2003-2006 PEAR <pear-group@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/Mail_mime
*/
class Mail_mimePart
{
/**
* The encoding type of this part
*
* @var string
* @access private
*/
var $_encoding;
/**
* An array of subparts
*
* @var array
* @access private
*/
var $_subparts;
/**
* The output of this part after being built
*
* @var string
* @access private
*/
var $_encoded;
/**
* Headers for this part
*
* @var array
* @access private
*/
var $_headers;
/**
* The body of this part (not encoded)
*
* @var string
* @access private
*/
var $_body;
/**
* The location of file with body of this part (not encoded)
*
* @var string
* @access private
*/
var $_body_file;
/**
* The end-of-line sequence
*
* @var string
* @access private
*/
var $_eol = "\r\n";
/**
* Constructor.
*
* Sets up the object.
*
* @param string $body The body of the mime part if any.
* @param array $params An associative array of optional parameters:
* content_type - The content type for this part eg multipart/mixed
* encoding - The encoding to use, 7bit, 8bit,
* base64, or quoted-printable
* charset - Content character set
* cid - Content ID to apply
* disposition - Content disposition, inline or attachment
* filename - Filename parameter for content disposition
* description - Content description
* name_encoding - Encoding of the attachment name (Content-Type)
* By default filenames are encoded using RFC2231
* Here you can set RFC2047 encoding (quoted-printable
* or base64) instead
* filename_encoding - Encoding of the attachment filename (Content-Disposition)
* See 'name_encoding'
* headers_charset - Charset of the headers e.g. filename, description.
* If not set, 'charset' will be used
* eol - End of line sequence. Default: "\r\n"
* headers - Hash array with additional part headers. Array keys can be
* in form of <header_name>:<parameter_name>
* body_file - Location of file with part's body (instead of $body)
*
* @access public
*/
function Mail_mimePart($body = '', $params = array())
{
if (!empty($params['eol'])) {
$this->_eol = $params['eol'];
} else if (defined('MAIL_MIMEPART_CRLF')) { // backward-copat.
$this->_eol = MAIL_MIMEPART_CRLF;
}
// Additional part headers
if (!empty($params['headers']) && is_array($params['headers'])) {
$headers = $params['headers'];
}
foreach ($params as $key => $value) {
switch ($key) {
case 'encoding':
$this->_encoding = $value;
$headers['Content-Transfer-Encoding'] = $value;
break;
case 'cid':
$headers['Content-ID'] = '<' . $value . '>';
break;
case 'location':
$headers['Content-Location'] = $value;
break;
case 'body_file':
$this->_body_file = $value;
break;
// for backward compatibility
case 'dfilename':
$params['filename'] = $value;
break;
}
}
// Default content-type
if (empty($params['content_type'])) {
$params['content_type'] = 'text/plain';
}
// Content-Type
$headers['Content-Type'] = $params['content_type'];
if (!empty($params['charset'])) {
$charset = "charset={$params['charset']}";
// place charset parameter in the same line, if possible
if ((strlen($headers['Content-Type']) + strlen($charset) + 16) <= 76) {
$headers['Content-Type'] .= '; ';
} else {
$headers['Content-Type'] .= ';' . $this->_eol . ' ';
}
$headers['Content-Type'] .= $charset;
// Default headers charset
if (!isset($params['headers_charset'])) {
$params['headers_charset'] = $params['charset'];
}
}
// header values encoding parameters
$h_charset = !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII';
$h_language = !empty($params['language']) ? $params['language'] : null;
$h_encoding = !empty($params['name_encoding']) ? $params['name_encoding'] : null;
if (!empty($params['filename'])) {
$headers['Content-Type'] .= ';' . $this->_eol;
$headers['Content-Type'] .= $this->_buildHeaderParam(
'name', $params['filename'], $h_charset, $h_language, $h_encoding
);
}
// Content-Disposition
if (!empty($params['disposition'])) {
$headers['Content-Disposition'] = $params['disposition'];
if (!empty($params['filename'])) {
$headers['Content-Disposition'] .= ';' . $this->_eol;
$headers['Content-Disposition'] .= $this->_buildHeaderParam(
'filename', $params['filename'], $h_charset, $h_language,
!empty($params['filename_encoding']) ? $params['filename_encoding'] : null
);
}
// add attachment size
$size = $this->_body_file ? filesize($this->_body_file) : strlen($body);
if ($size) {
$headers['Content-Disposition'] .= ';' . $this->_eol . ' size=' . $size;
}
}
if (!empty($params['description'])) {
$headers['Content-Description'] = $this->encodeHeader(
'Content-Description', $params['description'], $h_charset, $h_encoding,
$this->_eol
);
}
// Search and add existing headers' parameters
foreach ($headers as $key => $value) {
$items = explode(':', $key);
if (count($items) == 2) {
$header = $items[0];
$param = $items[1];
if (isset($headers[$header])) {
$headers[$header] .= ';' . $this->_eol;
}
$headers[$header] .= $this->_buildHeaderParam(
$param, $value, $h_charset, $h_language, $h_encoding
);
unset($headers[$key]);
}
}
// Default encoding
if (!isset($this->_encoding)) {
$this->_encoding = '7bit';
}
// Assign stuff to member variables
$this->_encoded = array();
$this->_headers = $headers;
$this->_body = $body;
}
/**
* Encodes and returns the email. Also stores
* it in the encoded member variable
*
* @param string $boundary Pre-defined boundary string
*
* @return An associative array containing two elements,
* body and headers. The headers element is itself
* an indexed array. On error returns PEAR error object.
* @access public
*/
function encode($boundary=null)
{
$encoded =& $this->_encoded;
if (count($this->_subparts)) {
$boundary = $boundary ? $boundary : '=_' . md5(rand() . microtime());
$eol = $this->_eol;
$this->_headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
$encoded['body'] = '';
for ($i = 0; $i < count($this->_subparts); $i++) {
$encoded['body'] .= '--' . $boundary . $eol;
$tmp = $this->_subparts[$i]->encode();
if ($this->_isError($tmp)) {
return $tmp;
}
foreach ($tmp['headers'] as $key => $value) {
$encoded['body'] .= $key . ': ' . $value . $eol;
}
$encoded['body'] .= $eol . $tmp['body'] . $eol;
}
$encoded['body'] .= '--' . $boundary . '--' . $eol;
} else if ($this->_body) {
$encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding);
} else if ($this->_body_file) {
// Temporarily reset magic_quotes_runtime for file reads and writes
if ($magic_quote_setting = get_magic_quotes_runtime()) {
@ini_set('magic_quotes_runtime', 0);
}
$body = $this->_getEncodedDataFromFile($this->_body_file, $this->_encoding);
if ($magic_quote_setting) {
@ini_set('magic_quotes_runtime', $magic_quote_setting);
}
if ($this->_isError($body)) {
return $body;
}
$encoded['body'] = $body;
} else {
$encoded['body'] = '';
}
// Add headers to $encoded
$encoded['headers'] =& $this->_headers;
return $encoded;
}
/**
* Encodes and saves the email into file. File must exist.
* Data will be appended to the file.
*
* @param string $filename Output file location
* @param string $boundary Pre-defined boundary string
* @param boolean $skip_head True if you don't want to save headers
*
* @return array An associative array containing message headers
* or PEAR error object
* @access public
* @since 1.6.0
*/
function encodeToFile($filename, $boundary=null, $skip_head=false)
{
if (file_exists($filename) && !is_writable($filename)) {
$err = $this->_raiseError('File is not writeable: ' . $filename);
return $err;
}
if (!($fh = fopen($filename, 'ab'))) {
$err = $this->_raiseError('Unable to open file: ' . $filename);
return $err;
}
// Temporarily reset magic_quotes_runtime for file reads and writes
if ($magic_quote_setting = get_magic_quotes_runtime()) {
@ini_set('magic_quotes_runtime', 0);
}
$res = $this->_encodePartToFile($fh, $boundary, $skip_head);
fclose($fh);
if ($magic_quote_setting) {
@ini_set('magic_quotes_runtime', $magic_quote_setting);
}
return $this->_isError($res) ? $res : $this->_headers;
}
/**
* Encodes given email part into file
*
* @param string $fh Output file handle
* @param string $boundary Pre-defined boundary string
* @param boolean $skip_head True if you don't want to save headers
*
* @return array True on sucess or PEAR error object
* @access private
*/
function _encodePartToFile($fh, $boundary=null, $skip_head=false)
{
$eol = $this->_eol;
if (count($this->_subparts)) {
$boundary = $boundary ? $boundary : '=_' . md5(rand() . microtime());
$this->_headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
}
if (!$skip_head) {
foreach ($this->_headers as $key => $value) {
fwrite($fh, $key . ': ' . $value . $eol);
}
$f_eol = $eol;
} else {
$f_eol = '';
}
if (count($this->_subparts)) {
for ($i = 0; $i < count($this->_subparts); $i++) {
fwrite($fh, $f_eol . '--' . $boundary . $eol);
$res = $this->_subparts[$i]->_encodePartToFile($fh);
if ($this->_isError($res)) {
return $res;
}
$f_eol = $eol;
}
fwrite($fh, $eol . '--' . $boundary . '--' . $eol);
} else if ($this->_body) {
fwrite($fh, $f_eol . $this->_getEncodedData($this->_body, $this->_encoding));
} else if ($this->_body_file) {
fwrite($fh, $f_eol);
$res = $this->_getEncodedDataFromFile(
$this->_body_file, $this->_encoding, $fh
);
if ($this->_isError($res)) {
return $res;
}
}
return true;
}
/**
* Adds a subpart to current mime part and returns
* a reference to it
*
* @param string $body The body of the subpart, if any.
* @param array $params The parameters for the subpart, same
* as the $params argument for constructor.
*
* @return Mail_mimePart A reference to the part you just added. In PHP4, it is
* crucial if using multipart/* in your subparts that
* you use =& in your script when calling this function,
* otherwise you will not be able to add further subparts.
* @access public
*/
function &addSubpart($body, $params)
{
$this->_subparts[] = $part = new Mail_mimePart($body, $params);
return $part;
}
/**
* Returns encoded data based upon encoding passed to it
*
* @param string $data The data to encode.
* @param string $encoding The encoding type to use, 7bit, base64,
* or quoted-printable.
*
* @return string
* @access private
*/
function _getEncodedData($data, $encoding)
{
switch ($encoding) {
case 'quoted-printable':
return $this->_quotedPrintableEncode($data);
break;
case 'base64':
return rtrim(chunk_split(base64_encode($data), 76, $this->_eol));
break;
case '8bit':
case '7bit':
default:
return $data;
}
}
/**
* Returns encoded data based upon encoding passed to it
*
* @param string $filename Data file location
* @param string $encoding The encoding type to use, 7bit, base64,
* or quoted-printable.
* @param resource $fh Output file handle. If set, data will be
* stored into it instead of returning it
*
* @return string Encoded data or PEAR error object
* @access private
*/
function _getEncodedDataFromFile($filename, $encoding, $fh=null)
{
if (!is_readable($filename)) {
$err = $this->_raiseError('Unable to read file: ' . $filename);
return $err;
}
if (!($fd = fopen($filename, 'rb'))) {
$err = $this->_raiseError('Could not open file: ' . $filename);
return $err;
}
$data = '';
switch ($encoding) {
case 'quoted-printable':
while (!feof($fd)) {
$buffer = $this->_quotedPrintableEncode(fgets($fd));
if ($fh) {
fwrite($fh, $buffer);
} else {
$data .= $buffer;
}
}
break;
case 'base64':
while (!feof($fd)) {
// Should read in a multiple of 57 bytes so that
// the output is 76 bytes per line. Don't use big chunks
// because base64 encoding is memory expensive
$buffer = fread($fd, 57 * 9198); // ca. 0.5 MB
$buffer = base64_encode($buffer);
$buffer = chunk_split($buffer, 76, $this->_eol);
if (feof($fd)) {
$buffer = rtrim($buffer);
}
if ($fh) {
fwrite($fh, $buffer);
} else {
$data .= $buffer;
}
}
break;
case '8bit':
case '7bit':
default:
while (!feof($fd)) {
$buffer = fread($fd, 1048576); // 1 MB
if ($fh) {
fwrite($fh, $buffer);
} else {
$data .= $buffer;
}
}
}
fclose($fd);
if (!$fh) {
return $data;
}
}
/**
* Encodes data to quoted-printable standard.
*
* @param string $input The data to encode
* @param int $line_max Optional max line length. Should
* not be more than 76 chars
*
* @return string Encoded data
*
* @access private
*/
function _quotedPrintableEncode($input , $line_max = 76)
{
$eol = $this->_eol;
/*
// imap_8bit() is extremely fast, but doesn't handle properly some characters
if (function_exists('imap_8bit') && $line_max == 76) {
$input = preg_replace('/\r?\n/', "\r\n", $input);
$input = imap_8bit($input);
if ($eol != "\r\n") {
$input = str_replace("\r\n", $eol, $input);
}
return $input;
}
*/
$lines = preg_split("/\r?\n/", $input);
$escape = '=';
$output = '';
while (list($idx, $line) = each($lines)) {
$newline = '';
$i = 0;
while (isset($line[$i])) {
$char = $line[$i];
$dec = ord($char);
$i++;
if (($dec == 32) && (!isset($line[$i]))) {
// convert space at eol only
$char = '=20';
} elseif ($dec == 9 && isset($line[$i])) {
; // Do nothing if a TAB is not on eol
} elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) {
$char = $escape . sprintf('%02X', $dec);
} elseif (($dec == 46) && (($newline == '')
|| ((strlen($newline) + strlen("=2E")) >= $line_max))
) {
// Bug #9722: convert full-stop at bol,
// some Windows servers need this, won't break anything (cipri)
// Bug #11731: full-stop at bol also needs to be encoded
// if this line would push us over the line_max limit.
$char = '=2E';
}
// Note, when changing this line, also change the ($dec == 46)
// check line, as it mimics this line due to Bug #11731
// EOL is not counted
if ((strlen($newline) + strlen($char)) >= $line_max) {
// soft line break; " =\r\n" is okay
$output .= $newline . $escape . $eol;
$newline = '';
}
$newline .= $char;
} // end of for
$output .= $newline . $eol;
unset($lines[$idx]);
}
// Don't want last crlf
$output = substr($output, 0, -1 * strlen($eol));
return $output;
}
/**
* Encodes the parameter of a header.
*
* @param string $name The name of the header-parameter
* @param string $value The value of the paramter
* @param string $charset The characterset of $value
* @param string $language The language used in $value
* @param string $encoding Parameter encoding. If not set, parameter value
* is encoded according to RFC2231
* @param int $maxLength The maximum length of a line. Defauls to 75
*
* @return string
*
* @access private
*/
function _buildHeaderParam($name, $value, $charset=null, $language=null,
$encoding=null, $maxLength=75
) {
// RFC 2045:
// value needs encoding if contains non-ASCII chars or is longer than 78 chars
if (!preg_match('#[^\x20-\x7E]#', $value)) {
$token_regexp = '#([^\x21\x23-\x27\x2A\x2B\x2D'
. '\x2E\x30-\x39\x41-\x5A\x5E-\x7E])#';
if (!preg_match($token_regexp, $value)) {
// token
if (strlen($name) + strlen($value) + 3 <= $maxLength) {
return " {$name}={$value}";
}
} else {
// quoted-string
$quoted = addcslashes($value, '\\"');
if (strlen($name) + strlen($quoted) + 5 <= $maxLength) {
return " {$name}=\"{$quoted}\"";
}
}
}
// RFC2047: use quoted-printable/base64 encoding
if ($encoding == 'quoted-printable' || $encoding == 'base64') {
return $this->_buildRFC2047Param($name, $value, $charset, $encoding);
}
// RFC2231:
$encValue = preg_replace_callback(
'/([^\x21\x23\x24\x26\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E])/',
array($this, '_encodeReplaceCallback'), $value
);
$value = "$charset'$language'$encValue";
$header = " {$name}*={$value}";
if (strlen($header) <= $maxLength) {
return $header;
}
$preLength = strlen(" {$name}*0*=");
$maxLength = max(16, $maxLength - $preLength - 3);
$maxLengthReg = "|(.{0,$maxLength}[^\%][^\%])|";
$headers = array();
$headCount = 0;
while ($value) {
$matches = array();
$found = preg_match($maxLengthReg, $value, $matches);
if ($found) {
$headers[] = " {$name}*{$headCount}*={$matches[0]}";
$value = substr($value, strlen($matches[0]));
} else {
$headers[] = " {$name}*{$headCount}*={$value}";
$value = '';
}
$headCount++;
}
$headers = implode(';' . $this->_eol, $headers);
return $headers;
}
/**
* Encodes header parameter as per RFC2047 if needed
*
* @param string $name The parameter name
* @param string $value The parameter value
* @param string $charset The parameter charset
* @param string $encoding Encoding type (quoted-printable or base64)
* @param int $maxLength Encoded parameter max length. Default: 76
*
* @return string Parameter line
* @access private
*/
function _buildRFC2047Param($name, $value, $charset,
$encoding='quoted-printable', $maxLength=76
) {
// WARNING: RFC 2047 says: "An 'encoded-word' MUST NOT be used in
// parameter of a MIME Content-Type or Content-Disposition field",
// but... it's supported by many clients/servers
$quoted = '';
if ($encoding == 'base64') {
$value = base64_encode($value);
$prefix = '=?' . $charset . '?B?';
$suffix = '?=';
// 2 x SPACE, 2 x '"', '=', ';'
$add_len = strlen($prefix . $suffix) + strlen($name) + 6;
$len = $add_len + strlen($value);
while ($len > $maxLength) {
// We can cut base64-encoded string every 4 characters
$real_len = floor(($maxLength - $add_len) / 4) * 4;
$_quote = substr($value, 0, $real_len);
$value = substr($value, $real_len);
$quoted .= $prefix . $_quote . $suffix . $this->_eol . ' ';
$add_len = strlen($prefix . $suffix) + 4; // 2 x SPACE, '"', ';'
$len = strlen($value) + $add_len;
}
$quoted .= $prefix . $value . $suffix;
} else {
// quoted-printable
$value = $this->encodeQP($value);
$prefix = '=?' . $charset . '?Q?';
$suffix = '?=';
// 2 x SPACE, 2 x '"', '=', ';'
$add_len = strlen($prefix . $suffix) + strlen($name) + 6;
$len = $add_len + strlen($value);
while ($len > $maxLength) {
$length = $maxLength - $add_len;
// don't break any encoded letters
if (preg_match("/^(.{0,$length}[^\=][^\=])/", $value, $matches)) {
$_quote = $matches[1];
}
$quoted .= $prefix . $_quote . $suffix . $this->_eol . ' ';
$value = substr($value, strlen($_quote));
$add_len = strlen($prefix . $suffix) + 4; // 2 x SPACE, '"', ';'
$len = strlen($value) + $add_len;
}
$quoted .= $prefix . $value . $suffix;
}
return " {$name}=\"{$quoted}\"";
}
/**
* Encodes a header as per RFC2047
*
* @param string $name The header name
* @param string $value The header data to encode
* @param string $charset Character set name
* @param string $encoding Encoding name (base64 or quoted-printable)
* @param string $eol End-of-line sequence. Default: "\r\n"
*
* @return string Encoded header data (without a name)
* @access public
* @since 1.6.1
*/
function encodeHeader($name, $value, $charset='ISO-8859-1',
$encoding='quoted-printable', $eol="\r\n"
) {
// Structured headers
$comma_headers = array(
'from', 'to', 'cc', 'bcc', 'sender', 'reply-to',
'resent-from', 'resent-to', 'resent-cc', 'resent-bcc',
'resent-sender', 'resent-reply-to',
'mail-reply-to', 'mail-followup-to',
'return-receipt-to', 'disposition-notification-to',
);
$other_headers = array(
'references', 'in-reply-to', 'message-id', 'resent-message-id',
);
$name = strtolower($name);
if (in_array($name, $comma_headers)) {
$separator = ',';
} else if (in_array($name, $other_headers)) {
$separator = ' ';
}
if (!$charset) {
$charset = 'ISO-8859-1';
}
// Structured header (make sure addr-spec inside is not encoded)
if (!empty($separator)) {
// Simple e-mail address regexp
$email_regexp = '([^\s<]+|("[^\r\n"]+"))@\S+';
$parts = Mail_mimePart::_explodeQuotedString("[\t$separator]", $value);
$value = '';
foreach ($parts as $part) {
$part = preg_replace('/\r?\n[\s\t]*/', $eol . ' ', $part);
$part = trim($part);
if (!$part) {
continue;
}
if ($value) {
$value .= $separator == ',' ? $separator . ' ' : ' ';
} else {
$value = $name . ': ';
}
// let's find phrase (name) and/or addr-spec
if (preg_match('/^<' . $email_regexp . '>$/', $part)) {
$value .= $part;
} else if (preg_match('/^' . $email_regexp . '$/', $part)) {
// address without brackets and without name
$value .= $part;
} else if (preg_match('/<*' . $email_regexp . '>*$/', $part, $matches)) {
// address with name (handle name)
$address = $matches[0];
$word = str_replace($address, '', $part);
$word = trim($word);
// check if phrase requires quoting
if ($word) {
// non-ASCII: require encoding
if (preg_match('#([^\s\x21-\x7E]){1}#', $word)) {
if ($word[0] == '"' && $word[strlen($word)-1] == '"') {
// de-quote quoted-string, encoding changes
// string to atom
$search = array("\\\"", "\\\\");
$replace = array("\"", "\\");
$word = str_replace($search, $replace, $word);
$word = substr($word, 1, -1);
}
// find length of last line
if (($pos = strrpos($value, $eol)) !== false) {
$last_len = strlen($value) - $pos;
} else {
$last_len = strlen($value);
}
$word = Mail_mimePart::encodeHeaderValue(
$word, $charset, $encoding, $last_len, $eol
);
} else if (($word[0] != '"' || $word[strlen($word)-1] != '"')
&& preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $word)
) {
// ASCII: quote string if needed
$word = '"'.addcslashes($word, '\\"').'"';
}
}
$value .= $word.' '.$address;
} else {
// addr-spec not found, don't encode (?)
$value .= $part;
}
// RFC2822 recommends 78 characters limit, use 76 from RFC2047
$value = wordwrap($value, 76, $eol . ' ');
}
// remove header name prefix (there could be EOL too)
$value = preg_replace(
'/^'.$name.':('.preg_quote($eol, '/').')* /', '', $value
);
} else {
// Unstructured header
// non-ASCII: require encoding
if (preg_match('#([^\s\x21-\x7E]){1}#', $value)) {
if ($value[0] == '"' && $value[strlen($value)-1] == '"') {
// de-quote quoted-string, encoding changes
// string to atom
$search = array("\\\"", "\\\\");
$replace = array("\"", "\\");
$value = str_replace($search, $replace, $value);
$value = substr($value, 1, -1);
}
$value = Mail_mimePart::encodeHeaderValue(
$value, $charset, $encoding, strlen($name) + 2, $eol
);
} else if (strlen($name.': '.$value) > 78) {
// ASCII: check if header line isn't too long and use folding
$value = preg_replace('/\r?\n[\s\t]*/', $eol . ' ', $value);
$tmp = wordwrap($name.': '.$value, 78, $eol . ' ');
$value = preg_replace('/^'.$name.':\s*/', '', $tmp);
// hard limit 998 (RFC2822)
$value = wordwrap($value, 998, $eol . ' ', true);
}
}
return $value;
}
/**
* Explode quoted string
*
* @param string $delimiter Delimiter expression string for preg_match()
* @param string $string Input string
*
* @return array String tokens array
* @access private
*/
function _explodeQuotedString($delimiter, $string)
{
$result = array();
$strlen = strlen($string);
for ($q=$p=$i=0; $i < $strlen; $i++) {
if ($string[$i] == "\""
&& (empty($string[$i-1]) || $string[$i-1] != "\\")
) {
$q = $q ? false : true;
} else if (!$q && preg_match("/$delimiter/", $string[$i])) {
$result[] = substr($string, $p, $i - $p);
$p = $i + 1;
}
}
$result[] = substr($string, $p);
return $result;
}
/**
* Encodes a header value as per RFC2047
*
* @param string $value The header data to encode
* @param string $charset Character set name
* @param string $encoding Encoding name (base64 or quoted-printable)
* @param int $prefix_len Prefix length. Default: 0
* @param string $eol End-of-line sequence. Default: "\r\n"
*
* @return string Encoded header data
* @access public
* @since 1.6.1
*/
function encodeHeaderValue($value, $charset, $encoding, $prefix_len=0, $eol="\r\n")
{
// #17311: Use multibyte aware method (requires mbstring extension)
if ($result = Mail_mimePart::encodeMB($value, $charset, $encoding, $prefix_len, $eol)) {
return $result;
}
// Generate the header using the specified params and dynamicly
// determine the maximum length of such strings.
// 75 is the value specified in the RFC.
$encoding = $encoding == 'base64' ? 'B' : 'Q';
$prefix = '=?' . $charset . '?' . $encoding .'?';
$suffix = '?=';
$maxLength = 75 - strlen($prefix . $suffix);
$maxLength1stLine = $maxLength - $prefix_len;
if ($encoding == 'B') {
// Base64 encode the entire string
$value = base64_encode($value);
// We can cut base64 every 4 characters, so the real max
// we can get must be rounded down.
$maxLength = $maxLength - ($maxLength % 4);
$maxLength1stLine = $maxLength1stLine - ($maxLength1stLine % 4);
$cutpoint = $maxLength1stLine;
$output = '';
while ($value) {
// Split translated string at every $maxLength
$part = substr($value, 0, $cutpoint);
$value = substr($value, $cutpoint);
$cutpoint = $maxLength;
// RFC 2047 specifies that any split header should
// be separated by a CRLF SPACE.
if ($output) {
$output .= $eol . ' ';
}
$output .= $prefix . $part . $suffix;
}
$value = $output;
} else {
// quoted-printable encoding has been selected
$value = Mail_mimePart::encodeQP($value);
// This regexp will break QP-encoded text at every $maxLength
// but will not break any encoded letters.
$reg1st = "|(.{0,$maxLength1stLine}[^\=][^\=])|";
$reg2nd = "|(.{0,$maxLength}[^\=][^\=])|";
if (strlen($value) > $maxLength1stLine) {
// Begin with the regexp for the first line.
$reg = $reg1st;
$output = '';
while ($value) {
// Split translated string at every $maxLength
// But make sure not to break any translated chars.
$found = preg_match($reg, $value, $matches);
// After this first line, we need to use a different
// regexp for the first line.
$reg = $reg2nd;
// Save the found part and encapsulate it in the
// prefix & suffix. Then remove the part from the
// $value_out variable.
if ($found) {
$part = $matches[0];
$len = strlen($matches[0]);
$value = substr($value, $len);
} else {
$part = $value;
$value = '';
}
// RFC 2047 specifies that any split header should
// be separated by a CRLF SPACE
if ($output) {
$output .= $eol . ' ';
}
$output .= $prefix . $part . $suffix;
}
$value = $output;
} else {
$value = $prefix . $value . $suffix;
}
}
return $value;
}
/**
* Encodes the given string using quoted-printable
*
* @param string $str String to encode
*
* @return string Encoded string
* @access public
* @since 1.6.0
*/
function encodeQP($str)
{
// Bug #17226 RFC 2047 restricts some characters
// if the word is inside a phrase, permitted chars are only:
// ASCII letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
// "=", "_", "?" must be encoded
$regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';
$str = preg_replace_callback(
$regexp, array('Mail_mimePart', '_qpReplaceCallback'), $str
);
return str_replace(' ', '_', $str);
}
/**
* Encodes the given string using base64 or quoted-printable.
* This method makes sure that encoded-word represents an integral
* number of characters as per RFC2047.
*
* @param string $str String to encode
* @param string $charset Character set name
* @param string $encoding Encoding name (base64 or quoted-printable)
* @param int $prefix_len Prefix length. Default: 0
* @param string $eol End-of-line sequence. Default: "\r\n"
*
* @return string Encoded string
* @access public
* @since 1.8.0
*/
function encodeMB($str, $charset, $encoding, $prefix_len=0, $eol="\r\n")
{
if (!function_exists('mb_substr') || !function_exists('mb_strlen')) {
return;
}
$encoding = $encoding == 'base64' ? 'B' : 'Q';
// 75 is the value specified in the RFC
$prefix = '=?' . $charset . '?'.$encoding.'?';
$suffix = '?=';
$maxLength = 75 - strlen($prefix . $suffix);
// A multi-octet character may not be split across adjacent encoded-words
// So, we'll loop over each character
// mb_stlen() with wrong charset will generate a warning here and return null
$length = mb_strlen($str, $charset);
$result = '';
$line_length = $prefix_len;
if ($encoding == 'B') {
// base64
$start = 0;
$prev = '';
for ($i=1; $i<=$length; $i++) {
// See #17311
$chunk = mb_substr($str, $start, $i-$start, $charset);
$chunk = base64_encode($chunk);
$chunk_len = strlen($chunk);
if ($line_length + $chunk_len == $maxLength || $i == $length) {
if ($result) {
$result .= "\n";
}
$result .= $chunk;
$line_length = 0;
$start = $i;
} else if ($line_length + $chunk_len > $maxLength) {
if ($result) {
$result .= "\n";
}
if ($prev) {
$result .= $prev;
}
$line_length = 0;
$start = $i - 1;
} else {
$prev = $chunk;
}
}
} else {
// quoted-printable
// see encodeQP()
$regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';
for ($i=0; $i<=$length; $i++) {
$char = mb_substr($str, $i, 1, $charset);
// RFC recommends underline (instead of =20) in place of the space
// that's one of the reasons why we're not using iconv_mime_encode()
if ($char == ' ') {
$char = '_';
$char_len = 1;
} else {
$char = preg_replace_callback(
$regexp, array('Mail_mimePart', '_qpReplaceCallback'), $char
);
$char_len = strlen($char);
}
if ($line_length + $char_len > $maxLength) {
if ($result) {
$result .= "\n";
}
$line_length = 0;
}
$result .= $char;
$line_length += $char_len;
}
}
if ($result) {
$result = $prefix
.str_replace("\n", $suffix.$eol.' '.$prefix, $result).$suffix;
}
return $result;
}
/**
* Callback function to replace extended characters (\x80-xFF) with their
* ASCII values (RFC2047: quoted-printable)
*
* @param array $matches Preg_replace's matches array
*
* @return string Encoded character string
* @access private
*/
function _qpReplaceCallback($matches)
{
return sprintf('=%02X', ord($matches[1]));
}
/**
* Callback function to replace extended characters (\x80-xFF) with their
* ASCII values (RFC2231)
*
* @param array $matches Preg_replace's matches array
*
* @return string Encoded character string
* @access private
*/
function _encodeReplaceCallback($matches)
{
return sprintf('%%%02X', ord($matches[1]));
}
/**
* PEAR::isError implementation
*
* @param mixed $data Object
*
* @return bool True if object is an instance of PEAR_Error
* @access private
*/
function _isError($data)
{
// PEAR::isError() is not PHP 5.4 compatible (see Bug #19473)
//if (is_object($data) && is_a($data, 'PEAR_Error')) {
// return true;
//}
//return false;
return $data === false;
}
/**
* Z-Push helper for error logging
* removing PEAR dependency
*
* @param string debug message
* @return boolean always false as there was an error
* @access private
*/
function _raiseError($message) {
ZLog::Write(LOGLEVEL_ERROR, "mimePart error: ". $message);
return false;
}
} // End of class
<?php
/***********************************************
* File : mime_calendar.php
* Project : Z-Push
* Descr : Functions for using within the IMAP backend
*
* Created : 2015
*
* Copyright 2015 - 2016 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation with the following additional
* term according to sec. 7:
*
* According to sec. 7 of the GNU Affero General Public License, version 3,
* the terms of the AGPL are supplemented with the following terms:
*
* "Zarafa" is a registered trademark of Zarafa B.V.
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
* The licensing of the Program under the AGPL does not imply a trademark license.
* Therefore any rights, title and interest in our trademarks remain entirely with us.
*
* However, if you propagate an unmodified version of the Program you are
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
* Furthermore you may use our trademarks where it is necessary to indicate
* the intended purpose of a product or service provided you use it in accordance
* with honest practices in industrial or commercial matters.
* If you want to propagate modified versions of the Program under the name "Z-Push",
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/
function create_calendar_dav($data) {
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->create_calendar_dav(): Creating calendar event");
if (defined('IMAP_MEETING_USE_CALDAV') && IMAP_MEETING_USE_CALDAV) {
$caldav = new BackendCalDAV();
if ($caldav->Logon(Request::GetAuthUser(), Request::GetAuthDomain(), Request::GetAuthPassword())) {
$etag = $caldav->CreateUpdateCalendar($data);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->create_calendar_dav(): Calendar created with etag '%s' and data <%s>", $etag, $data));
$caldav->Logoff();
}
else {
ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->create_calendar_dav(): Error connecting with BackendCalDAV");
}
}
}
function delete_calendar_dav($uid) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->delete_calendar_dav('%s'): Deleting calendar event", $uid));
if ($uid === false) {
ZLog::Write(LOGLEVEL_WARN, "BackendIMAP->delete_calendar_dav(): UID not found; report the full calendar object to developers");
}
else {
if (defined('IMAP_MEETING_USE_CALDAV') && IMAP_MEETING_USE_CALDAV) {
$caldav = new BackendCalDAV();
if ($caldav->Logon(Request::GetAuthUser(), Request::GetAuthDomain(), Request::GetAuthPassword())) {
$events = $caldav->FindCalendar($uid);
if (count($events) == 1) {
$href = $events[0]["href"];
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->delete_calendar_dav(): found event with href '%s', deleting", $href));
// Delete event
$res = $caldav->DeleteCalendar($href);
if ($res) {
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->delete_calendar_dav(): event deleted");
}
else {
ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->delete_calendar_dav(): error removing event, we will end with zombie events");
}
$caldav->Logoff();
}
else {
ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->delete_calendar_dav(): event not found, we will end with zombie events");
}
}
else {
ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->delete_calendar_dav(): Error connecting with BackendCalDAV");
}
}
}
}
function update_calendar_attendee($uid, $mailto, $status) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->update_calendar_attendee('%s', '%s', '%s'): Updating calendar event attendee", $uid, $mailto, $status));
$updated = false;
if ($uid === false) {
ZLog::Write(LOGLEVEL_WARN, "BackendIMAP->update_calendar_attendee(): UID not found; report the full calendar object to developers");
}
else {
if (defined('IMAP_MEETING_USE_CALDAV') && IMAP_MEETING_USE_CALDAV) {
$caldav = new BackendCalDAV();
if ($caldav->Logon(Request::GetAuthUser(), Request::GetAuthDomain(), Request::GetAuthPassword())) {
$events = $caldav->FindCalendar($uid);
if (count($events) == 1) {
$href = $events[0]["href"];
$etag = $events[0]["etag"];
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->update_calendar_attendee(): found event with href '%s' etag '%s'; updating", $href, $etag));
// Get Attendee status
$old_status = "";
if (strcasecmp($old_status, $status) != 0) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->update_calendar_attendee(): Before <%s>", $events[0]["data"]));
$ical = new iCalComponent();
$ical->ParseFrom($events[0]["data"]);
$ical->SetCPParameterValue("VEVENT", "ATTENDEE", "PARTSTAT", strtoupper($status), $mailto);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->update_calendar_attendee(): After <%s>", $ical->Render()));
$etag = $caldav->CreateUpdateCalendar($ical->Render(), $href, $etag);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->update_calendar_attendee(): Calendar updated with etag '%s'", $etag));
// Update new status
$updated = true;
}
$caldav->Logoff();
}
else {
ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->update_calendar_attendee(): event not found or duplicated event");
}
}
else {
ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->update_calendar_attendee(): Error connecting with BackendCalDAV");
}
}
}
return $updated;
}
/**
* Detect if one message has one VCALENDAR part
*
* @param Mail_mimeDecode $message
* @return boolean
*/
function has_calendar_object($message) {
if (is_calendar($message)) {
return true;
}
else {
if(isset($message->parts)) {
for ($i = 0; $i < count($message->parts); $i++) {
if (is_calendar($message->parts[$i])) {
return true;
}
}
}
}
return false;
}
/**
* Detect if the message-part is VCALENDAR
* Content-Type: text/calendar;
*
* @param Mail_mimeDecode $message
* @return boolean
*/
function is_calendar($message) {
return isset($message->ctype_primary) && isset($message->ctype_secondary) && $message->ctype_primary == "text" && $message->ctype_secondary == "calendar";
}
/**
* Converts a text/calendar part into SyncMeetingRequest
* This is called on received messages, it's not called for events generated from the mobile
*
* @param $part MIME part
* @param $output SyncMail object
* @param $is_sent_folder boolean
*/
function parse_meeting_calendar($part, &$output, $is_sent_folder) {
$ical = new iCalComponent();
$ical->ParseFrom($part->body);
ZLog::Write(LOGLEVEL_WBXML, sprintf("BackendIMAP->parse_meeting_calendar(): %s", $part->body));
// Get UID
$uid = false;
$props = $ical->GetPropertiesByPath("VEVENT/UID");
if (count($props) > 0) {
$uid = $props[0]->Value();
}
$method = false;
$props = $ical->GetPropertiesByPath("VCALENDAR/METHOD");
if (count($props) > 0) {
$method = strtolower($props[0]->Value());
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->parse_meeting_calendar(): Using method from vcalendar object: %s", $method));
}
else {
if (isset($part->ctype_parameters["method"])) {
$method = strtolower($part->ctype_parameters["method"]);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->parse_meeting_calendar(): Using method from mime part object: %s", $method));
}
}
if ($method === false) {
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->parse_meeting_calendar() - No method header, please report it to the developers"));
$output->messageclass = "IPM.Appointment";
}
else {
switch ($method) {
case "cancel":
$output->messageclass = "IPM.Schedule.Meeting.Canceled";
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): Event canceled, removing calendar object");
delete_calendar_dav($uid);
break;
case "counter":
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): Counter received");
$output->messageclass = "IPM.Schedule.Meeting.Resp.Tent";
$output->meetingrequest->disallownewtimeproposal = 0;
break;
case "reply":
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): Reply received");
$props = $ical->GetPropertiesByPath('VEVENT/ATTENDEE');
for ($i = 0; $i < count($props); $i++) {
$mailto = $props[$i]->Value();
$props_params = $props[$i]->Parameters();
$status = strtolower($props_params["PARTSTAT"]);
if (!$is_sent_folder) {
// Only evaluate received replies, not sent
$res = update_calendar_attendee($uid, $mailto, $status);
}
else {
$res = true;
}
if ($res) {
// Only set messageclass for replies changing my calendar object
switch ($status) {
case "accepted":
$output->messageclass = "IPM.Schedule.Meeting.Resp.Pos";
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): Update attendee -> accepted");
break;
case "needs-action":
$output->messageclass = "IPM.Schedule.Meeting.Resp.Tent";
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): Update attendee -> needs-action");
break;
case "tentative":
$output->messageclass = "IPM.Schedule.Meeting.Resp.Tent";
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): Update attendee -> tentative");
break;
case "declined":
$output->messageclass = "IPM.Schedule.Meeting.Resp.Neg";
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): Update attendee -> declined");
break;
default:
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->parse_meeting_calendar() - Unknown reply status <%s>, please report it to the developers", $status));
$output->messageclass = "IPM.Appointment";
break;
}
}
}
$output->meetingrequest->disallownewtimeproposal = 1;
break;
case "request":
$output->messageclass = "IPM.Schedule.Meeting.Request";
$output->meetingrequest->disallownewtimeproposal = 0;
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar(): New request");
// New meeting, we don't create it now, because we need to confirm it first, but if we don't create it we won't see it in the calendar
break;
default:
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->parse_meeting_calendar() - Unknown method <%s>, please report it to the developers", strtolower($part->headers["method"])));
$output->messageclass = "IPM.Appointment";
$output->meetingrequest->disallownewtimeproposal = 0;
break;
}
}
$props = $ical->GetPropertiesByPath('VEVENT/DTSTAMP');
if (count($props) == 1) {
$output->meetingrequest->dtstamp = TimezoneUtil::MakeUTCDate($props[0]->Value());
}
$props = $ical->GetPropertiesByPath('VEVENT/UID');
if (count($props) == 1) {
$output->meetingrequest->globalobjid = $props[0]->Value();
}
$props = $ical->GetPropertiesByPath('VEVENT/DTSTART');
if (count($props) == 1) {
$output->meetingrequest->starttime = TimezoneUtil::MakeUTCDate($props[0]->Value());
if (strlen($props[0]->Value()) == 8) {
$output->meetingrequest->alldayevent = 1;
}
}
$props = $ical->GetPropertiesByPath('VEVENT/DTEND');
if (count($props) == 1) {
$output->meetingrequest->endtime = TimezoneUtil::MakeUTCDate($props[0]->Value());
if (strlen($props[0]->Value()) == 8) {
$output->meetingrequest->alldayevent = 1;
}
}
$props = $ical->GetPropertiesByPath('VEVENT/ORGANIZER');
if (count($props) == 1) {
$output->meetingrequest->organizer = str_ireplace("MAILTO:", "", $props[0]->Value());
}
$props = $ical->GetPropertiesByPath('VEVENT/LOCATION');
if (count($props) == 1) {
$output->meetingrequest->location = $props[0]->Value();
}
$props = $ical->GetPropertiesByPath('VEVENT/CLASS');
if (count($props) == 1) {
switch ($props[0]->Value()) {
case "PUBLIC":
$output->meetingrequest->sensitivity = "0";
break;
case "PRIVATE":
$output->meetingrequest->sensitivity = "2";
break;
case "CONFIDENTIAL":
$output->meetingrequest->sensitivity = "3";
break;
default:
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->parse_meeting_calendar() - Unknown VEVENT/CLASS '%s'. Using 0", $props[0]->Value()));
$output->meetingrequest->sensitivity = "0";
break;
}
}
else {
ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->parse_meeting_calendar() - No sensitivity class. Using 0");
$output->meetingrequest->sensitivity = "0";
}
// Get $tz from first timezone
$props = $ical->GetPropertiesByPath("VTIMEZONE/TZID");
if (count($props) > 0) {
// TimeZones shouldn't have dots
$tzname = str_replace(".", "", $props[0]->Value());
$tz = TimezoneUtil::GetFullTZFromTZName($tzname);
}
else {
$tz = TimezoneUtil::GetFullTZ();
}
$output->meetingrequest->timezone = base64_encode(TimezoneUtil::GetSyncBlobFromTZ($tz));
// Fixed values
$output->meetingrequest->instancetype = 0;
$output->meetingrequest->responserequested = 1;
$output->meetingrequest->busystatus = 2;
// TODO: reminder
$output->meetingrequest->reminder = "";
}
/**
* Modify a text/calendar part to transform it in a reply
*
* @param $part MIME part
* @param $response Response numeric value
* @param $condition_value string
* @return string MIME text/calendar
*/
function reply_meeting_calendar($part, $response, $username) {
$status_attendee = "ACCEPTED"; // 1 or default is ACCEPTED
$status_event = "CONFIRMED";
switch ($response) {
case 1:
$status_attendee = "ACCEPTED";
$status_event = "CONFIRMED";
break;
case 2:
$status_attendee = $status_event = "TENTATIVE";
break;
case 3:
// We won't hit this case ever, because we won't create an event if we are rejecting it
$status_attendee = "DECLINED";
$status_event = "CANCELLED";
break;
}
$ical = new iCalComponent();
$ical->ParseFrom($part->body);
$ical->SetPValue("METHOD", "REPLY");
$ical->SetCPParameterValue("VEVENT", "STATUS", $status_event, null);
// Update my information as attendee, but only mine
$ical->SetCPParameterValue("VEVENT", "ATTENDEE", "PARTSTAT", $status_attendee, sprintf("MAILTO:%s", $username));
$ical->SetCPParameterValue("VEVENT", "ATTENDEE", "RSVP", null, sprintf("MAILTO:%s", $username));
return $ical->Render();
}
<?php
/***********************************************
* File : mime_encode.php
* Project : Z-Push
* Descr : Functions for using within the IMAP backend
*
* Created : 2014
*
* Copyright 2014 - 2016 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation with the following additional
* term according to sec. 7:
*
* According to sec. 7 of the GNU Affero General Public License, version 3,
* the terms of the AGPL are supplemented with the following terms:
*
* "Zarafa" is a registered trademark of Zarafa B.V.
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
* The licensing of the Program under the AGPL does not imply a trademark license.
* Therefore any rights, title and interest in our trademarks remain entirely with us.
*
* However, if you propagate an unmodified version of the Program you are
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
* Furthermore you may use our trademarks where it is necessary to indicate
* the intended purpose of a product or service provided you use it in accordance
* with honest practices in industrial or commercial matters.
* If you want to propagate modified versions of the Program under the name "Z-Push",
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/
/**
* Add extra parts (not text; inlined or attached parts) to a mimepart object.
*
* @param Mail_mimePart $email reference to the object
* @param array $parts array of parts
*
* @return void
*/
function add_extra_sub_parts(&$email, $parts) {
if (isset($parts)) {
foreach ($parts as $part) {
$new_part = null;
// Only if it's an attachment we will add the text parts, because all the inline/no disposition have been already added
if (isset($part->disposition) && $part->disposition == "attachment") {
// it's an attachment
$new_part = add_sub_part($email, $part);
}
else {
if (isset($part->ctype_primary) && $part->ctype_primary != "text" && $part->ctype_primary != "multipart") {
// it's not a text part or a multipart
$new_part = add_sub_part($email, $part);
}
}
if (isset($part->parts)) {
// We add sub-parts to the new part (if any), not to the main message. Recursive calling
if ($new_part === null) {
add_extra_sub_parts($email, $part->parts);
}
else {
add_extra_sub_parts($new_part, $part->parts);
}
}
}
}
}
/**
* Add a subpart to a mimepart object.
*
* @param Mail_mimePart $email reference to the object
* @param object $part message part
*
* @return void
*/
function add_sub_part(&$email, $part) {
//http://tools.ietf.org/html/rfc4021
$new_part = null;
$params = array();
$params['content_type'] = '';
if (isset($part) && isset($email)) {
if (isset($part->ctype_primary)) {
$params['content_type'] = $part->ctype_primary;
if (isset($part->ctype_secondary)) {
$params['content_type'] .= '/' . $part->ctype_secondary;
}
}
if (isset($part->ctype_parameters)) {
foreach ($part->ctype_parameters as $k => $v) {
if(strcasecmp($k, 'boundary') != 0) {
$params['content_type'] .= '; ' . $k . '=' . $v;
}
}
}
if (isset($part->disposition)) {
$params['disposition'] = $part->disposition;
}
//FIXME: dfilename => filename
if (isset($part->d_parameters)) {
$params['headers_charset'] = 'utf-8';
foreach ($part->d_parameters as $k => $v) {
$params[$k] = $v;
}
}
foreach ($part->headers as $k => $v) {
switch($k) {
case "content-description":
$params['description'] = $v;
break;
case "content-type":
case "content-disposition":
case "content-transfer-encoding":
// Do nothing, we already did
break;
case "content-id":
$params['cid'] = str_replace('<', '', str_replace('>', '', $v));
break;
default:
$params[$k] = $v;
break;
}
}
// If not exist body, the part will be multipart/alternative, so we don't add encoding
if (!isset($params['encoding']) && isset($part->body)) {
$params['encoding'] = 'base64';
}
// We could not have body; recursive messages
$new_part = $email->addSubPart(isset($part->body) ? $part->body : "", $params);
unset($params);
}
// return the new part
return $new_part;
}
/**
* Add a subpart to a mimepart object.
*
* @param Mail_mimePart $email reference to the object
* @param object $part message part
*
* @return void
*/
function change_charset_and_add_subparts(&$email, $part) {
if (isset($part)) {
$new_part = null;
if (isset($part->ctype_parameters['charset'])) {
$part->ctype_parameters['charset'] = 'UTF-8';
$new_part = add_sub_part($email, $part);
}
else {
// We don't add the charset because it could be a non-text part
$new_part = add_sub_part($email, $part);
}
if (isset($part->parts)) {
foreach ($part->parts as $subpart) {
// Subparts are added to the part, not the main message
change_charset_and_add_subparts($new_part, $subpart);
}
}
}
}
/**
* Creates a MIME message from a decoded MIME message, reencoding and fixing the text.
*
* @param array $message array returned from Mail_mimeDecode->decode
*
* @return string MIME message
*/
function build_mime_message($message) {
$finalEmail = new Mail_mimePart(isset($message->body) ? $message->body : "", array('headers' => $message->headers));
if (isset($message->parts)) {
foreach ($message->parts as $part) {
change_charset_and_add_subparts($finalEmail, $part);
}
}
$mimeHeaders = Array();
$mimeHeaders['headers'] = Array();
$is_mime = false;
foreach ($message->headers as $key => $value) {
switch($key) {
case 'content-type':
$new_value = $message->ctype_primary . "/" . $message->ctype_secondary;
$is_mime = (strcasecmp($message->ctype_primary, 'multipart') == 0);
if (isset($message->ctype_parameters)) {
foreach ($message->ctype_parameters as $ckey => $cvalue) {
switch($ckey) {
case 'charset':
$new_value .= '; charset="UTF-8"';
break;
case 'boundary':
// Do nothing, we are encoding also the headers
break;
default:
$new_value .= '; ' . $ckey . '="' . $cvalue . '"';
break;
}
}
}
$mimeHeaders['content_type'] = $new_value;
break;
case 'content-transfer-encoding':
if (strcasecmp($value, "base64") == 0 || strcasecmp($value, "binary") == 0) {
$mimeHeaders['encoding'] = "base64";
}
else {
$mimeHeaders['encoding'] = "8bit";
}
break;
case 'content-id':
$mimeHeaders['cid'] = $value;
break;
case 'content-location':
$mimeHeaders['location'] = $value;
break;
case 'content-disposition':
$mimeHeaders['disposition'] = $value;
break;
case 'content-description':
$mimeHeaders['description'] = $value;
break;
default:
if (is_array($value)) {
foreach($value as $v) {
$mimeHeaders['headers'][$key] = $v;
}
}
else {
$mimeHeaders['headers'][$key] = $value;
}
break;
}
}
$finalEmail = new Mail_mimePart(isset($message->body) ? $message->body : "", $mimeHeaders);
unset($mimeHeaders);
if (isset($message->parts)) {
foreach ($message->parts as $part) {
change_charset_and_add_subparts($finalEmail, $part);
}
}
$boundary = '=_' . md5(rand() . microtime());
$finalEmail = $finalEmail->encode($boundary);
$headers = "";
$mimePart = new Mail_mimePart();
foreach ($finalEmail['headers'] as $key => $value) {
if (is_array($value)) {
foreach ($values as $ikey => $ivalue) {
$headers .= $key . ": " . $mimePart->encodeHeader($key, $ivalue, "utf-8", "base64") . "\n";
}
}
else {
$headers .= $key . ": " . $mimePart->encodeHeader($key, $value, "utf-8", "base64") . "\n";
}
}
unset($mimePart);
if ($is_mime) {
$built_message = "$headers\nThis is a multi-part message in MIME format.\n".$finalEmail['body'];
}
else {
$built_message = "$headers\n".$finalEmail['body'];
}
unset($headers);
unset($finalEmail);
return $built_message;
}
/**
* Detect if the message-part is SMIME
*
* @param Mail_mimeDecode $message
* @return boolean
*/
function is_smime($message) {
$res = false;
if (isset($message->ctype_primary) && isset($message->ctype_secondary)) {
$smime_types = array(array("multipart", "signed"), array("application", "pkcs7-mime"), array("application", "x-pkcs7-mime"), array("multipart", "encrypted"));
for ($i = 0; $i < count($smime_types) && !$res; $i++) {
$res = ($message->ctype_primary == $smime_types[$i][0] && $message->ctype_secondary == $smime_types[$i][1]);
}
}
return $res;
}
/**
* Detect if the message-part is SMIME, encrypted but not signed
* #190, KD 2015-06-04
*
* @param Mail_mimeDecode $message
* @return boolean
*/
function is_encrypted($message) {
$res = false;
if (is_smime($message) && !($message->ctype_primary == "multipart" && $message->ctype_secondary == "signed")) {
$res = true;
}
return $res;
}
/**
* Detect if the message is multipart.
* #198, KD 2015-06-15
*
* @param Mail_mimeDecode $message
* @return boolean
*/
function is_multipart($message) {
return isset($message->ctype_primary) && $message->ctype_primary == "multipart";
}
\ No newline at end of file
<?php
/***********************************************
* File : user_identity.php
* Project : Z-Push
* Descr : Functions for using within the IMAP backend
*
* Created : 2014
*
* Copyright 2014 - 2016 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation with the following additional
* term according to sec. 7:
*
* According to sec. 7 of the GNU Affero General Public License, version 3,
* the terms of the AGPL are supplemented with the following terms:
*
* "Zarafa" is a registered trademark of Zarafa B.V.
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
* The licensing of the Program under the AGPL does not imply a trademark license.
* Therefore any rights, title and interest in our trademarks remain entirely with us.
*
* However, if you propagate an unmodified version of the Program you are
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
* Furthermore you may use our trademarks where it is necessary to indicate
* the intended purpose of a product or service provided you use it in accordance
* with honest practices in industrial or commercial matters.
* If you want to propagate modified versions of the Program under the name "Z-Push",
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/
/**
* Returns the default value for "From"
*
* @return string
*/
function getDefaultFromValue($username, $domain) {
$v = "";
if (defined('IMAP_DEFAULTFROM')) {
switch (IMAP_DEFAULTFROM) {
case 'username':
$v = $username;
break;
case 'domain':
$v = $domain;
break;
case 'ldap':
$v = getIdentityFromLdap($username, $domain, IMAP_FROM_LDAP_FROM, true);
break;
case 'sql':
$v = getIdentityFromSql($username, $domain, IMAP_FROM_SQL_FROM, true);
break;
case 'passwd':
$v = getIdentityFromPasswd($username, $domain, 'FROM', true);
break;
default:
$v = $username . IMAP_DEFAULTFROM;
break;
}
}
return $v;
}
/**
* Return the default value for "FullName"
*
* @param string $username Username
* @return string
*/
function getDefaultFullNameValue($username, $domain) {
$v = $username;
if (defined('IMAP_DEFAULTFROM')) {
switch (IMAP_DEFAULTFROM) {
case 'ldap':
$v = getIdentityFromLdap($username, $domain, IMAP_FROM_LDAP_FULLNAME, false);
break;
case 'sql':
$v = getIdentityFromSql($username, $domain, IMAP_FROM_SQL_FULLNAME, false);
break;
case 'passwd':
$v = getIdentityFromPasswd($username, $domain, 'FULLNAME', false);
break;
}
}
return $v;
}
/**
* Generate the "From"/"FullName" value stored in a LDAP server
*
* @params string $username username value
* @params string $domain domain value
* @params string $identity pattern to fill with ldap values
* @params boolean $encode if the result should be encoded as a header
* @return string
*/
function getIdentityFromLdap($username, $domain, $identity, $encode = true) {
$ret_value = $username;
$ldap_conn = null;
try {
$ldap_conn = ldap_connect(IMAP_FROM_LDAP_SERVER, IMAP_FROM_LDAP_SERVER_PORT);
if ($ldap_conn) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromLdap() - Connected to LDAP"));
ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
$ldap_bind = ldap_bind($ldap_conn, IMAP_FROM_LDAP_USER, IMAP_FROM_LDAP_PASSWORD);
if ($ldap_bind) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromLdap() - Authenticated in LDAP"));
$filter = str_replace('#username', $username, str_replace('#domain', $domain, IMAP_FROM_LDAP_QUERY));
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromLdap() - Searching From with filter: %s", $filter));
$search = ldap_search($ldap_conn, IMAP_FROM_LDAP_BASE, $filter, unserialize(IMAP_FROM_LDAP_FIELDS));
$items = ldap_get_entries($ldap_conn, $search);
if ($items['count'] > 0) {
$ret_value = $identity;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromLdap() - Found entry in LDAP. Generating From"));
// We get the first object. It's your responsability to make the query unique
foreach (unserialize(IMAP_FROM_LDAP_FIELDS) as $field) {
$ret_value = str_replace('#'.$field, $items[0][$field][0], $ret_value);
}
if ($encode) {
$ret_value = encodeFrom($ret_value);
}
}
else {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromLdap() - No entry found in LDAP"));
}
}
else {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromLdap() - Not authenticated in LDAP server"));
}
}
else {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromLdap() - Not connected to LDAP server"));
}
}
catch(Exception $ex) {
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->getIdentityFromLdap() - Error getting From value from LDAP server: %s", $ex));
}
if ($ldap_conn != null) {
ldap_close($ldap_conn);
}
return $ret_value;
}
/**
* Generate the "From" value stored in a SQL Database
*
* @params string $username username value
* @params string $domain domain value
* @return string
*/
function getIdentityFromSql($username, $domain, $identity, $encode = true) {
$ret_value = $username;
$dbh = $sth = $record = null;
try {
$dbh = new PDO(IMAP_FROM_SQL_DSN, IMAP_FROM_SQL_USER, IMAP_FROM_SQL_PASSWORD, unserialize(IMAP_FROM_SQL_OPTIONS));
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromSql() - Connected to SQL Database"));
$sql = str_replace('#username', $username, str_replace('#domain', $domain, IMAP_FROM_SQL_QUERY));
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromSql() - Searching From with filter: %s", $sql));
$sth = $dbh->prepare($sql);
$sth->execute();
$record = $sth->fetch(PDO::FETCH_ASSOC);
if ($record) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromSql() - Found entry in SQL Database. Generating From"));
$ret_value = $identity;
foreach (unserialize(IMAP_FROM_SQL_FIELDS) as $field) {
$ret_value = str_replace('#'.$field, $record[$field], $ret_value);
}
if ($encode) {
$ret_value = encodeFrom($ret_value);
}
}
else {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromSql() - No entry found in SQL Database"));
}
}
catch(PDOException $ex) {
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->getIdentityFromSql() - Error getting From value from SQL Database: %s", $ex));
}
$dbh = $sth = $record = null;
return $ret_value;
}
/**
* Generate the "From" value from the local posix passwd database
*
* @params string $username username value
* @params string $domain domain value
* @return string
*/
function getIdentityFromPasswd($username, $domain, $identity, $encode = true) {
$ret_value = $username;
try {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromPasswd() - Fetching info for user %s", $username));
$local_user = posix_getpwnam($username);
if ($local_user) {
$tmp = $local_user['gecos'];
$tmp = explode(',', $tmp);
$name = $tmp[0];
unset($tmp);
switch ($identity) {
case 'FROM':
if (strlen($domain) > 0) {
$ret_value = sprintf("%s <%s@%s>", $name, $username, $domain);
} else {
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->getIdentityFromPasswd() - No domain passed. Cannot construct From address."));
}
break;
case 'FULLNAME':
$ret_value = sprintf("%s", $name);
break;
}
if ($encode) {
$ret_value = encodeFrom($ret_value);
}
} else {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getIdentityFromPasswd() - No entry found in Password database"));
}
}
catch(Exception $ex) {
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->getIdentityFromPasswd() - Error getting From value from passwd database: %s", $ex));
}
return $ret_value;
}
/**
* Encode the From value as Base64
*
* @param string $from From value
* @return string
*/
function encodeFrom($from) {
$items = explode("<", $from);
$name = trim($items[0]);
return "=?UTF-8?B?" . base64_encode($name) . "?= <" . $items[1];
}
\ No newline at end of file
......@@ -1224,15 +1224,35 @@ class TimezoneUtil {
*
* @param string $name internal timezone name
*
* @access public
* @access private
* @return string
*/
static public function getMSTZnameFromTZName($name) {
static private function getMSTZnameFromTZName($name) {
foreach (self::$mstzones as $mskey => $msdefs) {
if ($name == $msdefs[0])
return $msdefs[1];
}
// Not found? Then retrieve the correct TZName first and try again.
// That's ugly and needs a proper fix. But for now this method can convert
// - Europe/Berlin
// - W Europe Standard Time
// to "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna"
// which is more correct than the hardcoded default of (GMT+00:00...)
$tzName = '';
foreach (self::$phptimezones as $tzn => $phptzs) {
if (in_array($name, $phptzs)) {
$tzName = $tzn;
break;
}
}
if ($tzName != '') {
foreach (self::$mstzones as $mskey => $msdefs) {
if ($tzName == $msdefs[0])
return $msdefs[1];
}
}
ZLog::Write(LOGLEVEL_WARN, sprintf("TimezoneUtil::getMSTZnameFromTZName() no MS name found for '%s'. Returning '(GMT) Greenwich Mean Time: Dublin, Edinburgh, Lisbon, London'", $name));
return self::$mstzones["085"][1];
}
......@@ -1349,6 +1369,6 @@ class TimezoneUtil {
if (preg_match('/\/[.[:word:]]+\/\w+\/(\w+)\/([\w\/]+)/', $timezone, $matches)) {
return $matches[1] . "/" . $matches[2];
}
return TimezoneUtil::getMSTZnameFromTZName(trim($timezone, '"'));
return self::getMSTZnameFromTZName(trim($timezone, '"'));
}
}
......@@ -7,6 +7,15 @@ $baseDir = dirname($vendorDir);
return array(
'ASDevice' => $baseDir . '/lib/core/asdevice.php',
'Auth_SASL' => $baseDir . '/backend/imap/Auth/SASL.php',
'Auth_SASL_Anonymous' => $baseDir . '/backend/imap/Auth/SASL/Anonymous.php',
'Auth_SASL_Common' => $baseDir . '/backend/imap/Auth/SASL/Common.php',
'Auth_SASL_CramMD5' => $baseDir . '/backend/imap/Auth/SASL/CramMD5.php',
'Auth_SASL_DigestMD5' => $baseDir . '/backend/imap/Auth/SASL/DigestMD5.php',
'Auth_SASL_External' => $baseDir . '/backend/imap/Auth/SASL/External.php',
'Auth_SASL_Login' => $baseDir . '/backend/imap/Auth/SASL/Login.php',
'Auth_SASL_Plain' => $baseDir . '/backend/imap/Auth/SASL/Plain.php',
'Auth_SASL_SCRAM' => $baseDir . '/backend/imap/Auth/SASL/SCRAM.php',
'AuthenticationRequiredException' => $baseDir . '/lib/exceptions/authenticationrequiredexception.php',
'Backend' => $baseDir . '/lib/default/backend.php',
'BackendCalDAV' => $baseDir . '/backend/caldav/caldav.php',
......@@ -72,11 +81,18 @@ return array(
'MAPIProvider' => $baseDir . '/backend/kopano/mapiprovider.php',
'MAPIStreamWrapper' => $baseDir . '/backend/kopano/mapistreamwrapper.php',
'MAPIUtils' => $baseDir . '/backend/kopano/mapiutils.php',
'Mail' => $baseDir . '/backend/imap/Mail.php',
'Mail_RFC822' => $baseDir . '/include/z_RFC822.php',
'Mail_mail' => $baseDir . '/backend/imap/Mail/mail.php',
'Mail_mimeDecode' => $baseDir . '/include/mimeDecode.php',
'Mail_mimePart' => $baseDir . '/backend/imap/mimePart.php',
'Mail_sendmail' => $baseDir . '/backend/imap/Mail/sendmail.php',
'Mail_smtp' => $baseDir . '/backend/imap/Mail/smtp.php',
'MeetingResponse' => $baseDir . '/lib/request/meetingresponse.php',
'Meetingrequest' => $baseDir . '/backend/kopano/mapi/class.meetingrequest.php',
'MoveItems' => $baseDir . '/lib/request/moveitems.php',
'Net_SMTP' => $baseDir . '/backend/imap/Net/SMTP.php',
'Net_Socket' => $baseDir . '/backend/imap/Net/Socket.php',
'NoHierarchyCacheAvailableException' => $baseDir . '/lib/exceptions/nohierarchycacheavailableexception.php',
'NoPostRequestException' => $baseDir . '/lib/exceptions/nopostrequestexception.php',
'NotImplementedException' => $baseDir . '/lib/exceptions/notimplementedexception.php',
......
......@@ -14,6 +14,15 @@ class ComposerStaticInitd6749fc2fb9944bbe86b2b7d79a7852f
public static $classMap = array (
'ASDevice' => __DIR__ . '/../..' . '/lib/core/asdevice.php',
'Auth_SASL' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL.php',
'Auth_SASL_Anonymous' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/Anonymous.php',
'Auth_SASL_Common' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/Common.php',
'Auth_SASL_CramMD5' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/CramMD5.php',
'Auth_SASL_DigestMD5' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/DigestMD5.php',
'Auth_SASL_External' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/External.php',
'Auth_SASL_Login' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/Login.php',
'Auth_SASL_Plain' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/Plain.php',
'Auth_SASL_SCRAM' => __DIR__ . '/../..' . '/backend/imap/Auth/SASL/SCRAM.php',
'AuthenticationRequiredException' => __DIR__ . '/../..' . '/lib/exceptions/authenticationrequiredexception.php',
'Backend' => __DIR__ . '/../..' . '/lib/default/backend.php',
'BackendCalDAV' => __DIR__ . '/../..' . '/backend/caldav/caldav.php',
......@@ -79,11 +88,18 @@ class ComposerStaticInitd6749fc2fb9944bbe86b2b7d79a7852f
'MAPIProvider' => __DIR__ . '/../..' . '/backend/kopano/mapiprovider.php',
'MAPIStreamWrapper' => __DIR__ . '/../..' . '/backend/kopano/mapistreamwrapper.php',
'MAPIUtils' => __DIR__ . '/../..' . '/backend/kopano/mapiutils.php',
'Mail' => __DIR__ . '/../..' . '/backend/imap/Mail.php',
'Mail_RFC822' => __DIR__ . '/../..' . '/include/z_RFC822.php',
'Mail_mail' => __DIR__ . '/../..' . '/backend/imap/Mail/mail.php',
'Mail_mimeDecode' => __DIR__ . '/../..' . '/include/mimeDecode.php',
'Mail_mimePart' => __DIR__ . '/../..' . '/backend/imap/mimePart.php',
'Mail_sendmail' => __DIR__ . '/../..' . '/backend/imap/Mail/sendmail.php',
'Mail_smtp' => __DIR__ . '/../..' . '/backend/imap/Mail/smtp.php',
'MeetingResponse' => __DIR__ . '/../..' . '/lib/request/meetingresponse.php',
'Meetingrequest' => __DIR__ . '/../..' . '/backend/kopano/mapi/class.meetingrequest.php',
'MoveItems' => __DIR__ . '/../..' . '/lib/request/moveitems.php',
'Net_SMTP' => __DIR__ . '/../..' . '/backend/imap/Net/SMTP.php',
'Net_Socket' => __DIR__ . '/../..' . '/backend/imap/Net/Socket.php',
'NoHierarchyCacheAvailableException' => __DIR__ . '/../..' . '/lib/exceptions/nohierarchycacheavailableexception.php',
'NoPostRequestException' => __DIR__ . '/../..' . '/lib/exceptions/nopostrequestexception.php',
'NotImplementedException' => __DIR__ . '/../..' . '/lib/exceptions/notimplementedexception.php',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment