Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrites Mail_Parse::parsePriority function #491

Merged
merged 3 commits into from
Jun 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions include/class.mailparse.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,36 @@ function getPriority(){
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){
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static keyword was added purely to pass the tests.. and as this function is only ever called statically, it makes sense too.
https://github.com/osTicket/osTicket-1.8/search?q=parsePriority&ref=cmdform


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){
Expand Down
5 changes: 5 additions & 0 deletions setup/test/run-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -60,6 +63,8 @@ function show_fails_on_ctrlc() {
$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();
Expand Down
112 changes: 112 additions & 0 deletions setup/test/tests/test.header_functions.php
Original file line number Diff line number Diff line change
@@ -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';
?>