From 38fc06245847cf4af9ef0bd6178e8254045b1f5c Mon Sep 17 00:00:00 2001 From: Synchro Date: Fri, 29 Nov 2013 12:07:44 +0100 Subject: [PATCH 1/2] It's not the responsibility of callers to format debug output Generate debug output before throwing exceptions --- class.phpmailer.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/class.phpmailer.php b/class.phpmailer.php index 2f6831a81..85fc75eb6 100644 --- a/class.phpmailer.php +++ b/class.phpmailer.php @@ -638,8 +638,7 @@ protected function edebug($str) break; case 'echo': default: - //Just echoes exactly what was received - echo $str; + echo $str."\n"; } } @@ -759,20 +758,20 @@ protected function addAnAddress($kind, $address, $name = '') { if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) { $this->setError($this->lang('Invalid recipient array') . ': ' . $kind); + $this->edebug($this->lang('Invalid recipient array') . ': ' . $kind); if ($this->exceptions) { throw new phpmailerException('Invalid recipient array: ' . $kind); } - $this->edebug($this->lang('Invalid recipient array') . ': ' . $kind); return false; } $address = trim($address); $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim if (!$this->validateAddress($address)) { $this->setError($this->lang('invalid_address') . ': ' . $address); + $this->edebug($this->lang('invalid_address') . ': ' . $address); if ($this->exceptions) { throw new phpmailerException($this->lang('invalid_address') . ': ' . $address); } - $this->edebug($this->lang('invalid_address') . ': ' . $address); return false; } if ($kind != 'Reply-To') { @@ -804,10 +803,10 @@ public function setFrom($address, $name = '', $auto = true) $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim if (!$this->validateAddress($address)) { $this->setError($this->lang('invalid_address') . ': ' . $address); + $this->edebug($this->lang('invalid_address') . ': ' . $address); if ($this->exceptions) { throw new phpmailerException($this->lang('invalid_address') . ': ' . $address); } - $this->edebug($this->lang('invalid_address') . ': ' . $address); return false; } $this->From = $address; @@ -1037,10 +1036,10 @@ public function postSend() } } catch (phpmailerException $e) { $this->setError($e->getMessage()); + $this->edebug($e->getMessage()); if ($this->exceptions) { throw $e; } - $this->edebug($e->getMessage() . "\n"); } return false; } @@ -2036,10 +2035,10 @@ public function addAttachment($path, $name = '', $encoding = 'base64', $type = ' } catch (phpmailerException $e) { $this->setError($e->getMessage()); + $this->edebug($e->getMessage()); if ($this->exceptions) { throw $e; } - $this->edebug($e->getMessage() . "\n"); return false; } return true; From 379b700e77c6e4f13dea9685d9fd30159f799348 Mon Sep 17 00:00:00 2001 From: Synchro Date: Mon, 16 Dec 2013 14:28:57 +0100 Subject: [PATCH 2/2] Add config for scrutinizer --- .gitignore | 1 + .scrutinizer.yml | 126 +++++++++++++++ .travis.yml | 8 +- PHPMailerAutoload.php | 5 +- README.md | 2 + class.phpmailer.php | 7 +- docs/generatedocs.sh | 4 +- language/phpmailer.lang-no.php | 2 +- test/bootstrap.php | 5 + test/phpmailerLangTest.php | 283 ++------------------------------- test/phpmailerTest.php | 12 +- travis.phpunit.xml.dist | 35 ++++ 12 files changed, 200 insertions(+), 290 deletions(-) create mode 100644 .scrutinizer.yml create mode 100644 test/bootstrap.php create mode 100644 travis.phpunit.xml.dist diff --git a/.gitignore b/.gitignore index 88d292247..989164d65 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ docs/phpdoc/ test/message.txt test/testbootstrap.php .idea +build/ diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 000000000..afdb8844e --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,126 @@ +before_commands: + - "composer install --prefer-source" + +tools: + # Code Coverage + external_code_coverage: + enabled: true + timeout: 300 + filter: + excluded_paths: + - 'docs/*' + - 'examples/*' + - 'extras/*' + - 'test/*' + - 'vendor/*' + + + php_code_coverage: + enabled: false + test_command: phpunit + filter: + excluded_paths: + - 'docs/*' + - 'examples/*' + - 'extras/*' + - 'test/*' + - 'vendor/*' + + + # Code Sniffer + php_code_sniffer: + enabled: true + command: phpcs + config: + standard: PSR2 + filter: + excluded_paths: + - 'docs/*' + - 'examples/*' + - 'extras/*' + - 'test/*' + - 'vendor/*' + + + # Copy/Paste Detector + php_cpd: + enabled: true + command: phpcpd + excluded_dirs: + - docs + - examples + - extras + - test + - vendor + + + # PHP CS Fixer (http://http://cs.sensiolabs.org/). + php_cs_fixer: + enabled: true + command: php-cs-fixer + config: + level: psr2 + filter: + excluded_paths: + - 'docs/*' + - 'examples/*' + - 'extras/*' + - 'test/*' + - 'vendor/*' + + + # Analyzes the size and structure of a PHP project. + php_loc: + enabled: true + command: phploc + excluded_dirs: + - docs + - examples + - extras + - test + - vendor + + + # PHP Mess Detector (http://phpmd.org). + php_mess_detector: + enabled: true + command: phpmd + config: + rulesets: + - codesize + - unusedcode + - naming + - design + filter: + excluded_paths: + - 'docs/*' + - 'examples/*' + - 'extras/*' + - 'test/*' + - 'vendor/*' + + + # Analyzes the size and structure of a PHP project. + php_pdepend: + enabled: true + command: pdepend + excluded_dirs: + - docs + - examples + - extras + - test + - vendor + + # Runs Scrutinizer's PHP Analyzer Tool + php_analyzer: + enabled: true + filter: + excluded_paths: + - 'docs/*' + - 'examples/*' + - 'extras/*' + - 'test/*' + - 'vendor/*' + + # Security Advisory Checker + sensiolabs_security_checker: true diff --git a/.travis.yml b/.travis.yml index 28f0099f9..c51f0f3b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ before_install: before_script: - sudo service postfix stop - smtp-sink -d "%d.%H.%M.%S" localhost:2500 1000 & + - mkdir -p build/logs - cd test - cp testbootstrap-dist.php testbootstrap.php - chmod +x fakesendmail.sh @@ -16,5 +17,10 @@ before_script: - sudo cp fakesendmail.sh /var/qmail/bin/sendmail - sudo cp fakesendmail.sh /usr/sbin/sendmail - echo 'sendmail_path = "/usr/sbin/sendmail -t -i "' | sudo tee "/home/travis/.phpenv/versions/`php -i|grep "PHP Version"|head -n 1|grep -o -P '\d+\.\d+\.\d+.*'`/etc/conf.d/sendmail.ini" + - pwd + - ls -al script: - - phpunit phpmailerTest + - phpunit --configuration ../travis.phpunit.xml.dist +after_script: + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover ../build/logs/clover.xml diff --git a/PHPMailerAutoload.php b/PHPMailerAutoload.php index be0dad253..6f7dd305b 100644 --- a/PHPMailerAutoload.php +++ b/PHPMailerAutoload.php @@ -38,7 +38,10 @@ function PHPMailerAutoload($classname) spl_autoload_register('PHPMailerAutoload'); } } else { - //Fall back to traditional autoload for old PHP versions + /** + * Fall back to traditional autoload for old PHP versions + * @param string $classname The name of the class to load + */ function __autoload($classname) { PHPMailerAutoload($classname); diff --git a/README.md b/README.md index adfcda4d3..8bcd31d8f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # PHPMailer - A full-featured email creation and transfer class for PHP Build status: [![Build Status](https://travis-ci.org/Synchro/PHPMailer.png)](https://travis-ci.org/Synchro/PHPMailer) +[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/Synchro/PHPMailer/badges/quality-score.png?s=320d104c5f7ae0bdb10abd98ea04e0cb107fe905)](https://scrutinizer-ci.com/g/Synchro/PHPMailer/) +[![Code Coverage](https://scrutinizer-ci.com/g/Synchro/PHPMailer/badges/coverage.png?s=4a55049051633406197503e4f66cc8eeb224c9a4)](https://scrutinizer-ci.com/g/Synchro/PHPMailer/) ## Class Features diff --git a/class.phpmailer.php b/class.phpmailer.php index 85fc75eb6..f6badc8a5 100644 --- a/class.phpmailer.php +++ b/class.phpmailer.php @@ -396,11 +396,7 @@ class PHPMailer * The function that handles the result of the send email action. * It is called out by send() for each email sent. * - * Value can be: - * - 'function_name' for function names - * - 'Class::Method' for static method calls - * - array($object, 'Method') for calling methods on $object - * See http://php.net/is_callable manual page for more details. + * Value can be any php callable: http://www.php.net/is_callable * * Parameters: * bool $result result of the send action @@ -410,7 +406,6 @@ class PHPMailer * string $subject the subject * string $body the email body * string $from email address of sender - * * @type string */ public $action_function = ''; diff --git a/docs/generatedocs.sh b/docs/generatedocs.sh index f4d065f37..8f24269fe 100755 --- a/docs/generatedocs.sh +++ b/docs/generatedocs.sh @@ -1,5 +1,5 @@ #!/bin/sh # Regenerate PHPMailer documentation # Run from within the docs folder -rm -rf phpdocs/* -phpdoc --directory .. --target ./phpdoc --ignore test/,examples/,extras/,test_script/ --sourcecode --force --title PHPMailer +rm -rf phpdoc/* +phpdoc --directory .. --target ./phpdoc --ignore test/,examples/,extras/,test_script/ --sourcecode --force --title PHPMailer --template="clean" diff --git a/language/phpmailer.lang-no.php b/language/phpmailer.lang-no.php index e6975203e..db1e327ec 100644 --- a/language/phpmailer.lang-no.php +++ b/language/phpmailer.lang-no.php @@ -21,4 +21,4 @@ $PHPMAILER_LANG['signing'] = 'Signeringsfeil: '; $PHPMAILER_LANG['smtp_connect_failed'] = 'SMTP Connect() feilet.'; $PHPMAILER_LANG['smtp_error'] = 'SMTP-serverfeil: '; -$PHPMAILER_LANG['variable_set'] = 'Kan ikke sette eller resette variabelen: '; \ No newline at end of file +$PHPMAILER_LANG['variable_set'] = 'Kan ikke sette eller resette variabelen: '; diff --git a/test/bootstrap.php b/test/bootstrap.php new file mode 100644 index 000000000..056c16d1e --- /dev/null +++ b/test/bootstrap.php @@ -0,0 +1,5 @@ + * @copyright 2004 - 2009 Andy Prevost + * @copyright 2010 Marcus Bointon * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License */ -require 'PHPUnit/Autoload.php'; +require_once 'PHPUnit/Autoload.php'; +require_once '../PHPMailerAutoload.php'; /** * PHPMailer - PHP email transport unit test class @@ -21,33 +23,12 @@ class PHPMailerLangTest extends PHPUnit_Framework_TestCase { /** - * Holds the default phpmailer instance. + * Holds a phpmailer instance. * @private * @var PHPMailer */ public $Mail; - /** - * Holds the SMTP mail host. - * @public - * @var string - */ - public $Host = ""; - - /** - * Holds the change log. - * @private - * @var string[] - */ - public $ChangeLog = array(); - - /** - * Holds the note log. - * @private - * @var string[] - */ - public $NoteLog = array(); - /** * @var string Default include path */ @@ -58,231 +39,13 @@ class PHPMailerLangTest extends PHPUnit_Framework_TestCase */ public function setUp() { - - if (file_exists('./testbootstrap.php')) { - include './testbootstrap.php'; //Overrides go in here - } - require_once $this->INCLUDE_DIR . 'class.phpmailer.php'; $this->Mail = new PHPMailer; - - $this->Mail->Priority = 3; - $this->Mail->Encoding = "8bit"; - $this->Mail->CharSet = "iso-8859-1"; - if (array_key_exists('mail_from', $_REQUEST)) { - $this->Mail->From = $_REQUEST['mail_from']; - } else { - $this->Mail->From = 'unit_test@phpmailer.example.com'; - } - $this->Mail->FromName = "Unit Tester"; - $this->Mail->Sender = ""; - $this->Mail->Subject = "Unit Test"; - $this->Mail->Body = ""; - $this->Mail->AltBody = ""; - $this->Mail->WordWrap = 0; - if (array_key_exists('mail_host', $_REQUEST)) { - $this->Mail->Host = $_REQUEST['mail_host']; - } else { - $this->Mail->Host = 'mail.example.com'; - } - if (array_key_exists('mail_port', $_REQUEST)) { - $this->Mail->Port = $_REQUEST['mail_port']; - } else { - $this->Mail->Port = 25; - } - $this->Mail->Helo = "localhost.localdomain"; - $this->Mail->SMTPAuth = false; - $this->Mail->Username = ""; - $this->Mail->Password = ""; - $this->Mail->PluginDir = $this->INCLUDE_DIR; - $this->Mail->addReplyTo("no_reply@phpmailer.example.com", "Reply Guy"); - $this->Mail->Sender = "unit_test@phpmailer.example.com"; - - if (strlen($this->Mail->Host) > 0) { - $this->Mail->Mailer = "smtp"; - } else { - $this->Mail->Mailer = "mail"; - $this->Mail->Sender = "unit_test@phpmailer.example.com"; - } - - if (array_key_exists('mail_to', $_REQUEST)) { - $this->setAddress($_REQUEST['mail_to'], 'Test User', 'to'); - } - if (array_key_exists('mail_cc', $_REQUEST) and strlen($_REQUEST['mail_cc']) > 0) { - $this->setAddress($_REQUEST['mail_cc'], 'Carbon User', 'cc'); - } - } - - /** - * Run after each test is completed. - */ - public function tearDown() - { - // Clean global variables - $this->Mail = null; - $this->ChangeLog = array(); - $this->NoteLog = array(); - } - - - /** - * Build the body of the message in the appropriate format. - * @private - * @returns void - */ - public function buildBody() - { - $this->CheckChanges(); - - // Determine line endings for message - if ($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0) { - $eol = "
"; - $bullet = "
  • "; - $bullet_start = "
      "; - $bullet_end = "
    "; - } else { - $eol = "\n"; - $bullet = " - "; - $bullet_start = ""; - $bullet_end = ""; - } - - $ReportBody = ""; - - $ReportBody .= "---------------------" . $eol; - $ReportBody .= "Unit Test Information" . $eol; - $ReportBody .= "---------------------" . $eol; - $ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol; - $ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol; - - if (strlen($this->Mail->Host) > 0) { - $ReportBody .= "Host: " . $this->Mail->Host . $eol; - } - - // If attachments then create an attachment list - $attachments = $this->Mail->getAttachments(); - if (count($attachments) > 0) { - $ReportBody .= "Attachments:" . $eol; - $ReportBody .= $bullet_start; - foreach ($attachments as $attachment) { - $ReportBody .= $bullet . "Name: " . $attachment[1] . ", "; - $ReportBody .= "Encoding: " . $attachment[3] . ", "; - $ReportBody .= "Type: " . $attachment[4] . $eol; - } - $ReportBody .= $bullet_end . $eol; - } - - // If there are changes then list them - if (count($this->ChangeLog) > 0) { - $ReportBody .= "Changes" . $eol; - $ReportBody .= "-------" . $eol; - - $ReportBody .= $bullet_start; - for ($i = 0; $i < count($this->ChangeLog); $i++) { - $ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" . - $this->ChangeLog[$i][1] . "]" . $eol; - } - $ReportBody .= $bullet_end . $eol . $eol; - } - - // If there are notes then list them - if (count($this->NoteLog) > 0) { - $ReportBody .= "Notes" . $eol; - $ReportBody .= "-----" . $eol; - - $ReportBody .= $bullet_start; - for ($i = 0; $i < count($this->NoteLog); $i++) { - $ReportBody .= $bullet . $this->NoteLog[$i] . $eol; - } - $ReportBody .= $bullet_end; - } - - // Re-attach the original body - $this->Mail->Body .= $eol . $eol . $ReportBody; - } - - /** - * Check which default settings have been changed for the report. - * @private - * @returns void - */ - public function checkChanges() - { - if ($this->Mail->Priority != 3) { - $this->addChange("Priority", $this->Mail->Priority); - } - if ($this->Mail->Encoding != "8bit") { - $this->addChange("Encoding", $this->Mail->Encoding); - } - if ($this->Mail->CharSet != "iso-8859-1") { - $this->addChange("CharSet", $this->Mail->CharSet); - } - if ($this->Mail->Sender != "") { - $this->addChange("Sender", $this->Mail->Sender); - } - if ($this->Mail->WordWrap != 0) { - $this->addChange("WordWrap", $this->Mail->WordWrap); - } - if ($this->Mail->Mailer != "mail") { - $this->addChange("Mailer", $this->Mail->Mailer); - } - if ($this->Mail->Port != 25) { - $this->addChange("Port", $this->Mail->Port); - } - if ($this->Mail->Helo != "localhost.localdomain") { - $this->addChange("Helo", $this->Mail->Helo); - } - if ($this->Mail->SMTPAuth) { - $this->addChange("SMTPAuth", "true"); - } - } - - /** - * Add a changelog entry. - * @access private - * @param string $sName - * @param string $sNewValue - * @return void - */ - public function addChange($sName, $sNewValue) - { - $this->ChangeLog[] = array($sName, $sNewValue); - } - - /** - * Adds a simple note to the message. - * @public - * @param string $sValue - * @return void - */ - public function addNote($sValue) - { - $this->NoteLog[] = $sValue; - } - - /** - * Adds all of the addresses - * @access public - * @param string $sAddress - * @param string $sName - * @param string $sType - * @return boolean - */ - public function setAddress($sAddress, $sName = '', $sType = 'to') - { - switch ($sType) { - case 'to': - return $this->Mail->addAddress($sAddress, $sName); - case 'cc': - return $this->Mail->addCC($sAddress, $sName); - case "bcc": - return $this->Mail->addBCC($sAddress, $sName); - } - return false; } /** * Test language files for missing and excess translations * All languages are compared with English + * @group languages */ public function testTranslations() { @@ -312,29 +75,3 @@ public function testTranslations() $this->assertEmpty($err, $err); } } - -/** - * This is a sample form for setting appropriate test values through a browser - * These values can also be set using a file called testbootstrap.php (not in svn) in the same folder as this script - * which is probably more useful if you run these tests a lot - * - * - *

    phpmailer Unit Test

    - * By entering a SMTP hostname it will automatically perform tests with SMTP. - * - *
    - * - * From Address: "/> - *
    - * To Address: "/> - *
    - * Cc Address: "/> - *
    - * SMTP Hostname: "/> - *

    - * - * - *

    - * - * - */ diff --git a/test/phpmailerTest.php b/test/phpmailerTest.php index e3ee66a2c..5c1e2d624 100644 --- a/test/phpmailerTest.php +++ b/test/phpmailerTest.php @@ -1,10 +1,7 @@ * @copyright 2004 - 2009 Andy Prevost + * @copyright 2010 Marcus Bointon * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License */ -require 'PHPUnit/Autoload.php'; -require '../PHPMailerAutoload.php'; +require_once 'PHPUnit/Autoload.php'; +require_once '../PHPMailerAutoload.php'; /** * PHPMailer - PHP email transport unit test class @@ -990,6 +988,8 @@ public function testQmailSend() $this->Mail->Subject = $subject . ': qmail'; $this->Mail->IsQmail(); $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo); + } else { + $this->markTestSkipped('Qmail is not installed'); } } diff --git a/travis.phpunit.xml.dist b/travis.phpunit.xml.dist new file mode 100644 index 000000000..61a9ca544 --- /dev/null +++ b/travis.phpunit.xml.dist @@ -0,0 +1,35 @@ + + + + + ./test/ + + + + + ./extras + + + + + languages + + + + + + + +