Changeset 622


Ignore:
Timestamp:
Feb 15, 2018 12:31:38 PM (6 years ago)
Author:
anonymous
Message:

Add Email() sandbox mode. Add Email->send() test. Fix minor bugs.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/version.txt

    r620 r622  
    1 2.2.0
     12.2.1-1
  • trunk/js/Msg.js

    r617 r622  
    5656        // Warn if the target doesn't exist.
    5757        if (!$(options.container).length) {
    58             console.warn('Strangecode.Msg container not found: ' + this.o.container);
     58            console.warn('Strangecode.Msg container not found: ' + options.container);
    5959        }
    6060    });
  • trunk/lib/Auth_SQL.inc.php

    r619 r622  
    826826            $db->query("
    827827                UPDATE " . $this->_params['db_table'] . " SET
    828                 blocked = '',
     828                blocked = NULL,
    829829                blocked_reason = ''
    830830                WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
  • trunk/lib/Email.inc.php

    r618 r622  
    7373        'wrap' => true,
    7474        'line_length' => 75,
     75
     76        'sandbox_mode' => null,
     77        'sandbox_to_addr' => null,
    7578    );
    7679
     
    8083    // String that contains the email body after replacements.
    8184    protected $_template_replaced;
     85
     86    // Email debug modes.
     87    const SANDBOX_MODE_REDIRECT = 1; // Send all mail to 'sandbox_to_addr'
     88    const SANDBOX_MODE_STDERR = 2; // Log all mail to stderr
    8289
    8390    /**
     
    359366            }
    360367            // If the envelope_sender_address was given as a header, move it to the correct place.
    361             if ('envelope_sender_address' == $key) {
     368            if ('envelope_sender_address' == strtolower($key)) {
    362369                $this->_params['envelope_sender_address'] = isset($this->_params['envelope_sender_address']) ? $this->_params['envelope_sender_address'] : $val;
     370                continue;
     371            }
     372            // If we're sending in sandbox mode, remove any headers with recipient addresses.
     373            if ($this->getParam('sandbox_mode') == self::SANDBOX_MODE_REDIRECT && in_array(strtolower($key), array('to', 'cc', 'bcc')) && mb_strpos($val, '@') !== false) {
     374                // Don't carry this into the $final_headers.
     375                $app->logMsg(sprintf('Skipping header in sandbox mode: %s=%s', $key, $val), LOG_DEBUG, __FILE__, __LINE__);
    363376                continue;
    364377            }
     
    384397            $app->logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);
    385398            return false;
     399        }
     400
     401        // Enter sandbox mode, if specified.
     402        switch ($this->getParam('sandbox_mode')) {
     403        case self::SANDBOX_MODE_REDIRECT:
     404            if (!$this->getParam('sandbox_to_addr')) {
     405                $app->logMsg(sprintf('Email sandbox_mode is SANDBOX_MODE_REDIRECT but sandbox_to_addr is not set.', null), LOG_ERR, __FILE__, __LINE__);
     406                break;
     407            }
     408            $final_to = $this->getParam('sandbox_to_addr');
     409            break;
     410
     411        case self::SANDBOX_MODE_STDERR:
     412            file_put_contents('php://stderr', sprintf("Subject: %s\nTo: %s\n%s\n\n%s", $this->getParam('subject'), $final_to, str_replace($this->getParam('crlf'), "\n", $final_headers), $final_body), FILE_APPEND);
     413            return true;
    386414        }
    387415
  • trunk/tests/EmailTest.php

    r468 r622  
    55 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    66 * Copyright 2001-2012 Strangecode, LLC
    7  * 
     7 *
    88 * This file is part of The Strangecode Codebase.
    99 *
     
    1212 * Free Software Foundation, either version 3 of the License, or (at your option)
    1313 * any later version.
    14  * 
     14 *
    1515 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1616 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1717 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1818 * details.
    19  * 
     19 *
    2020 * You should have received a copy of the GNU General Public License along with
    2121 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    8383//     }
    8484//
    85 //     function testsend()
    86 //     {
    87 //         $result   = $this->Email->send(PARAM);
    88 //         $expected = EXPECTED_VAL;
    89 //         $this->assertEquals($expected, $result);
    90 //     }
     85    function testsend()
     86    {
     87
     88        $this->Email = new Email(array(
     89            'to' => 'Rob Recipient <to@example.com>',
     90            'from' => 'Sam Sender <from@example.com>',
     91            'subject' => 'EmailTest',
     92            'headers' => array(
     93                'CC' => 'This header removed in REDIRECT sandbox mode <remove-me@example.com>',
     94                'X-Hello' => 'Hi there',
     95            ),
     96            'sandbox_mode' => Email::SANDBOX_MODE_REDIRECT,
     97            'sandbox_to_addr' => 'quinn@strangecode.com',
     98        ));
     99        $this->Email->setString('This is a {TEST}');
     100        $this->Email->replace(array(
     101            'test' => '– you guessed it – a test!'
     102        ));
     103        $result   = $this->Email->send();
     104        $expected = true;
     105        $this->assertEquals($expected, $result);
     106    }
    91107
    92108    function testvalidemail()
  • trunk/tests/run_tests.sh

    r602 r622  
    3838# Config options go in phpunit.xml
    3939# phpunit --tap | grep -v '^ok '
    40 phpunit --stderr || err "\nSomething went wrong (code $?). If there is no output above, check the php_error_log";
     40../vendor/phpunit/phpunit/composer/bin/phpunit --stderr || err "\nSomething went wrong (code $?). If there is no output above, check the php_error_log";
    4141
Note: See TracChangeset for help on using the changeset viewer.