Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Add the following lines to your `composer.json` file and then run `php composer.
}
```

[Basic usage](docs/index.mkd)

## License

Apache License 2.0. See [LICENSE](LICENSE) for details.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.3.0"
"phpunit/phpunit": "~6"
},
"autoload": {
"classmap" : ["src/Spamassassin/"]
Expand Down
29 changes: 29 additions & 0 deletions docs/index.mkd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Using spamassasin PHP client

PHP spamassasin client will connect to existing
spam detection daemon and verify if message is a unwanted
spam or if it's a legit message the end user want to
receive.

To create instance of spamassasin PHP client, create
new instance of `Client` class - this will use default
params which should connect to local spamassasin:

```php
use Spamassassin\Client;

$client = new Client();
```

Then validate if message is market by spam by checking
`$result` variable of type `Spamassassin\Result`:

```php
$result = $client->process($message);
```

The most important properties here are:

* `isSpam` - whether spamassasin marked message as unwanted
* `score` - received score
* `threshold` - required score to be marked as spam
8 changes: 8 additions & 0 deletions src/Spamassassin/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Client

protected $hostname = 'localhost';
protected $port = '783';
protected $timeout = null;

protected $socketPath;
protected $socket;
Expand Down Expand Up @@ -60,6 +61,11 @@ protected function getSocket()
$socket = fsockopen($this->hostname, $this->port, $errno, $errstr);
}

if(!empty($this->timeout))
{
stream_set_timeout($socket, $this->timeout);
}

if (!$socket) {
throw new Exception(
"Could not connect to SpamAssassin: {$errstr}", $errno
Expand Down Expand Up @@ -215,6 +221,7 @@ protected function parseOutput($header, $message)

$result->score = (float) $matches[2];
$result->thresold = (float) $matches[3];
$result->threshold = (float) $matches[3];
} else {

/**
Expand All @@ -234,6 +241,7 @@ protected function parseOutput($header, $message)

$result->score = (float) $matches[2];
$result->thresold = (float) $matches[3];
$result->threshold = (float) $matches[3];
}

}
Expand Down
10 changes: 8 additions & 2 deletions src/Spamassassin/Client/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@ class Result

/**
* How many points the message must score to be considered spam
*
*
* @var float
*/
public $threshold;

/**
* Typo kept for backward compat.
* @see $threshold
*/
public $thresold;

/**
* Is it spam or not?
*
*
* @var boolean
*/
public $isSpam;
Expand Down
6 changes: 5 additions & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use PHPUnit\Framework\TestCase;
use Spamassassin\Client;

class BaseTestCase extends PHPUnit_Framework_TestCase
class BaseTestCase extends TestCase
{
/** @var Client */
protected $sa;
Expand All @@ -25,6 +27,8 @@ public function setUp()
);
}

$params['timeout'] = 3;

$params["protocolVersion"] = $GLOBALS["PHPUNIT_SA_PROTOCOL_VERSION"];
$this->params = $params;
$this->sa = new Client($params);
Expand Down
2 changes: 1 addition & 1 deletion tests/CheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function testShouldBeAbleToCheckSpamMessage()
$return = $this->sa->check($message);

$this->assertTrue($return->isSpam);
$this->assertEquals(5.0, $return->thresold);
$this->assertEquals(5.0, $return->threshold);
$this->assertTrue($return->score >= 1000);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/LearnDisabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function setUp()
public function testShouldThrowExceptionIfLearningIsDisabled()
{
$message = $this->getMessage('Spam_GTUBE.txt');
$this->setExpectedException('Spamassassin\Client\Exception');
$this->expectException('Spamassassin\Client\Exception');
$this->sa->learn($message, Client::LEARN_SPAM);
}

public function testShouldThrowExceptionWhenForgettingIfLearningIsDisabled()
{
$message = $this->getMessage('Spam_GTUBE.txt');
$this->setExpectedException('Spamassassin\Client\Exception');
$this->expectException('Spamassassin\Client\Exception');
$this->sa->learn($message, Client::LEARN_FORGET);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/MaxSizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MaxSizeTest extends BaseTestCase
{
public function testShouldThrowExceptionIfMessageExceedsMaxSize()
{
$this->setExpectedException('Spamassassin\Client\Exception');
$this->expectException('Spamassassin\Client\Exception');

$params = $this->params;

Expand Down
2 changes: 1 addition & 1 deletion tests/SpamReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public function testShouldReturnReportIfMessageIsSpam()
{
$report = $this->sa->getSpamReport($this->gtube);

$this->assertContains("Content preview:", $report->message);
$this->assertContains("1000.0", $report->message);
$this->assertContains("1000 GTUBE", $report->message);
$this->assertTrue($report->isSpam);
}
Expand Down