diff --git a/include/class.mailparse.php b/include/class.mailparse.php index 48a402dfb013e724f1b8eb9c1d683007fa5d896e..42389ebc08432b29f4959a8a644e8d9c1ff39f9e 100644 --- a/include/class.mailparse.php +++ b/include/class.mailparse.php @@ -456,22 +456,36 @@ class Mail_Parse { return Mail_Parse::parsePriority($this->getHeader()); } - function parsePriority($header=null){ - - $priority=0; - if($header && ($begin=strpos($header,'X-Priority:'))!==false){ - $begin+=strlen('X-Priority:'); - $xpriority=preg_replace("/[^0-9]/", "",substr($header, $begin, strpos($header,"\n",$begin) - $begin)); - if(!is_numeric($xpriority)) - $priority=0; - elseif($xpriority>4) - $priority=1; - elseif($xpriority>=3) - $priority=2; - elseif($xpriority>0) - $priority=3; - } - return $priority; + static function parsePriority($header=null){ + + if (! $header) + return 0; + // Test for normal "X-Priority: INT" style header & stringy version. + // Allows for Importance possibility. + $matching_char = ''; + if (preg_match ( '/priority: (\d|\w)/i', $header, $matching_char ) + || preg_match ( '/importance: (\d|\w)/i', $header, $matching_char )) { + switch ($matching_char[1]) { + case 'h' : + case 'H' :// high + case 'u': + case 'U': //Urgent + case 6 : + case 5 : + return 1; + case 'n' : // normal + case 'N' : + case 4 : + case 3 : + return 2; + case 'l' : // low + case 'L' : + case 2 : + case 1 : + return 3; + } + } + return 0; } function parseAddressList($address){ diff --git a/setup/test/run-tests.php b/setup/test/run-tests.php index ecd7bb3c73a4f092b8684c3a84f04a010e296d16..326f8be18f9ba349b2d908eab241be1f60d817d4 100644 --- a/setup/test/run-tests.php +++ b/setup/test/run-tests.php @@ -2,6 +2,9 @@ <?php if (php_sapi_name() != 'cli') exit(); +//Allow user to select suite +$selected_test = (isset($argv[1])) ? $argv[1] : false; + require_once "tests/class.test.php"; if (!function_exists('get_osticket_root_path')) { @@ -63,6 +66,8 @@ foreach (glob_recursive(dirname(__file__)."/tests/test.*.php") as $t) { $class = (include $t); if (!is_string($class)) continue; + if($selected_test && ($class != $selected_test)) + continue; $test = new $class(); echo "Running: " . $test->name . "\n"; $test->run(); diff --git a/setup/test/tests/test.header_functions.php b/setup/test/tests/test.header_functions.php new file mode 100644 index 0000000000000000000000000000000000000000..b64a1c400946e590e01a8742d47d9a09ffefbd46 --- /dev/null +++ b/setup/test/tests/test.header_functions.php @@ -0,0 +1,112 @@ +<?php +require_once "class.test.php"; +define('INCLUDE_DIR', realpath(dirname(__file__).'/../../../include').'/'); +define('PEAR_DIR', INCLUDE_DIR.'/pear/'); +require_once INCLUDE_DIR."class.mailparse.php"; + +abstract class Priorities { + const HIGH_PRIORITY = 1; + const NORMAL_PRIORITY = 2; + const LOW_PRIORITY = 3; + const NO_PRIORITY = 0; +} + +class TestHeaderFunctions extends Test { + var $name = "Email Header Function Algorithm Regression Tests."; + + function testMailParsePriority() { + $func_class_method = array('Mail_Parse','parsePriority'); + $strlen_base = strlen($this->h()); + + foreach ( array ( + 'X-Priority: isNAN' => Priorities::NO_PRIORITY, // input => output + 'X-Priority: 1' => Priorities::LOW_PRIORITY, + 'X-Priority: 2' => Priorities::LOW_PRIORITY, + 'X-Priority: 3' => Priorities::NORMAL_PRIORITY, + 'X-Priority: 4' => Priorities::NORMAL_PRIORITY, + 'X-Priority: 5' => Priorities::HIGH_PRIORITY, + 'X-Priority: 6' => Priorities::HIGH_PRIORITY, + 'No priority set' => Priorities::NO_PRIORITY, + 'Priority: normal' => Priorities::NORMAL_PRIORITY, + 'xyz-priority: high' => Priorities::HIGH_PRIORITY, + 'Priority: high' => Priorities::HIGH_PRIORITY, + 'priority: low' => Priorities::LOW_PRIORITY, + 'x-priority: 1000' => Priorities::LOW_PRIORITY, // only matches first 1, not the full 1000 + 'priority: 3' => Priorities::NORMAL_PRIORITY, + 'IPM-Importance: low' => Priorities::LOW_PRIORITY, + 'My-Importance: URGENT' => Priorities::HIGH_PRIORITY, + 'Urgency: High' => Priorities::NO_PRIORITY, //urgency doesn't match.. maybe it should? + 'X-Importance: 5' => Priorities::HIGH_PRIORITY, + '' => Priorities::NO_PRIORITY + ) as $priority => $response ) { + $this->assert(is_int($response), "Setup fail, function should only return Integer values"); + //get header + $header = $this->h($priority); + + if(strlen($priority)){ + $this->assert((strlen($header) > $strlen_base), "Setup fail, function h not returning correct string length"); + } + if (! (call_user_func_array ($func_class_method , array($header) ) == $response)){ + //TODO: make line number dynamic + $this->fail ( "class.mailparse.php", 351, "Algorithm mistake: $priority should return $response!" ); + }else{ + $this->pass(); + } + } + + } + + /** + * Generate some header text to test with. Allows insertion of a known header variable + * + * @param string $setPriority + * @return string + */ + function h($setPriority = "") { + return <<<HEADER +Delivered-To: clonemeagain@gmail.com +Received: by 10.69.18.42 with SMTP id gj10csp88238pbd; +Fri, 20 Dec 2013 10:08:25 -0800 (PST) +X-Received: by 10.224.13.80 with SMTP id b16mr16256982qaa.73.1387562904239; +Fri, 20 Dec 2013 10:08:24 -0800 (PST) +Return-Path: <noreply@github.com> +Received: from github-smtp2a-ext-cp1-prd.iad.github.net (github-smtp2-ext5.iad.github.net. [192.30.252.196]) +by mx.google.com with ESMTPS id k3si6568083qao.74.2013.12.20.10.08.23 +for <clonemeagain@gmail.com> +(version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); +Fri, 20 Dec 2013 10:08:23 -0800 (PST) +Received-SPF: pass (google.com: domain of noreply@github.com designates 192.30.252.196 as permitted sender) client-ip=192.30.252.196; +Authentication-Results: mx.google.com; +spf=pass (google.com: domain of noreply@github.com designates 192.30.252.196 as permitted sender) smtp.mail=noreply@github.com +Date: Fri, 20 Dec 2013 10:08:23 -0800 +From: Jared Hancock <notifications@github.com> +Reply-To: "osTicket/osTicket-1.8" <reply+i-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH@reply.github.com> +To: "osTicket/osTicket-1.8" <osTicket-1.8@noreply.github.com> +Cc: clonemeagain <clonemeagain@gmail.com> +Message-ID: <osTicket/osTicket-1.8/pull/336/issue_event/82864993@github.com> +In-Reply-To: <osTicket/osTicket-1.8/pull/336@github.com> +References: <osTicket/osTicket-1.8/pull/336@github.com> +Subject: Re: [osTicket-1.8] Landing page inline image correction (#336) +Mime-Version: 1.0 +Content-Type: multipart/alternative; +boundary="--==_mimepart_52b4879729712_d621217cfc567e3"; +charset=UTF-8 +Content-Transfer-Encoding: 7bit +Precedence: list +X-GitHub-Recipient: clonemeagain +X-GitHub-Reason: author +List-ID: osTicket/osTicket-1.8 <osTicket-1.8.osTicket.github.com> +List-Archive: https://github.com/osTicket/osTicket-1.8 +List-Post: <mailto:reply+i-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH@reply.github.com> +List-Unsubscribe: <mailto:unsub+i-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH@reply.github.com>, +<https://github.com/notifications/unsubscribe/BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISHBUNCHORANDOMGIBBERIBBERISHBUNCHORANDOMGIBBERIBBERISH> +X-Auto-Response-Suppress: All +X-GitHub-Recipient-Address: clonemeagain@gmail.com +$setPriority + +HEADER; + } + +} +return 'TestHeaderFunctions'; +?>