Skip to content

Commit

Permalink
Merge branch 'rubenCodeforges-refactoring'
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddjian committed Mar 14, 2015
2 parents fe7816d + 15ca6b5 commit 18bbec2
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 139 deletions.
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,15 @@ var_dump($validator->getResults());
?>
```

* Enabling logging : just handle a logPath to the options array

```php
//enable logging
$options = array('logPath' => __DIR__.'/log.txt');

//Handle $options to the constructor as third parameter
$validator = new ValidatorEmail($email, $from, $options);
```


* Avaialable default options

```php
array(
'domainMoreInfo' => false,
'delaySleep' => array(0),
'noCommIsValid' => 0,
'catchAllIsValid' => 1,
'logPath' => null
'catchAllIsValid' => 0,
'catchAllEnabled' => 1,
);
```

Expand Down
2 changes: 2 additions & 0 deletions lib/SmtpValidatorEmail/Helper/EmailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static function sortEmailsByDomain(array $emails){
}
return ($domainA < $domainB) ? -1 : 1;
});

return $emails;
}

Expand All @@ -26,6 +27,7 @@ public static function getGroupedEmails(array $emails){
for($i = 0 ; $i<count($emails); $i++) {
$groupedEmails[substr(strrchr($emails[$i], "@"), 1)][]= $emails[$i];
}

return $groupedEmails;
}
}
61 changes: 29 additions & 32 deletions lib/SmtpValidatorEmail/Helper/TransportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace SmtpValidatorEmail\Helper;


use SmtpValidatorEmail\Exception\ExceptionNoConnection;
use SmtpValidatorEmail\Helper\Interfaces\TransportInterface;
use SmtpValidatorEmail\Service\StatusManager;
use SmtpValidatorEmail\Smtp\Smtp;
Expand All @@ -15,55 +14,53 @@ class TransportHelper implements TransportInterface{

private $connected = false;

private $domain;

/**
* @param StatusManager $statusManager
* @param $users array
* @param array $options FromDomain and FromUsers keys
* @param $domain
*/
public function __construct (StatusManager $statusManager,$options) {
$this->smtp = new Smtp($statusManager,$options);
public function __construct (StatusManager $statusManager,$users,$options,$domain) {
$this->smtp = new Smtp($statusManager,$users,$options);
$this->domain = $domain;
}

/**
* @param $mxs
* @return int|null|String
*/
public function connect ($mxs) {

$status = null;

foreach($mxs as $host=>$priority){
// try connecting to the remote host
try {

$this->smtp->connect($host);

if ( $this->smtp->isConnect() ) {
$this->setHost($host);
$this->connected = true;
break;
}

} catch (ExceptionNoConnection $e) {
// unable to connect to host, so these addresses are invalid?
$status = 'unable to connect to host';
$connection = $this->smtp->connect($host,$this->domain);
if ( $connection == 'connected' ) {
$this->setHost($host);
$status = 1;
$this->connected = true;
$this->smtp->state['mail'] = true;
break;
}else{
$status = $connection;
}
}
return $status;
}

public function reconnect($from) {
$status = null;
try {
$this->smtp->connect($this->host);

if($this->smtp->isConnect()){
$this->smtp->helo();
if(!$this->smtp->mail($from)) {
return 'MAIL FROM not accepted';
}else {
$this->connected = true;
}
if($connection = $this->smtp->connect($this->host,$this->domain)=='connected'){
$this->smtp->helo();
if(!$this->smtp->mail($from)) {
return 'MAIL FROM not accepted';
}else {
$this->connected = true;
}
} catch (ExceptionNoConnection $e) {
// unable to connect to host, so these addresses are invalid?
$status = 'unable to connect to host';
}else {
$status = $connection;
}

return $status;
}

Expand Down
61 changes: 41 additions & 20 deletions lib/SmtpValidatorEmail/Helper/ValidationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ValidationHelper {
* @var Mx
*/
private $mxs;

/**
* @var Domain;
*/
Expand All @@ -37,14 +38,17 @@ class ValidationHelper {

/**
* @param StatusManager $statusManager
* @param $users array
* @param array $options
* @param array $from Domain and User
* @param $domain
*/
public function __construct(StatusManager $statusManager,$options,$from){
public function __construct(StatusManager $statusManager,$users,$options,$from,$domain){

$this->statusManager = $statusManager;
$this->options = $options;
$this->transport = new TransportHelper($this->statusManager, $from);

$this->users = $users;
$this->transport = new TransportHelper($this->statusManager, $users,$from,$domain);
}

/**
Expand All @@ -65,10 +69,9 @@ public function getDom(){
* @param $domain
* @param $users
*/
public function startValidation($domain,$users){
public function startValidation($domain){
$mx = new Mx();
$this->mxs = $mx->getEntries($domain);
$this->users = $users;
$this->dom = new Domain($domain);
$this->dom->addDescription(array('users' => $this->users));
$this->dom->addDescription(array('mxs' => $this->mxs));
Expand All @@ -81,10 +84,9 @@ public function establishConnection(){
}

try {
$result = $this->transport->connect($this->mxs);
$this->statusManager->setStatus($this->users, $this->dom, $result);
$this->transport->connect($this->mxs);
} catch (\Exception $e) {
$this->statusManager->setStatus($this->users, $this->dom, 0, 'could not connect to host '.$this->mxs);
$this->statusManager->setStatus($this->users, $this->dom, 0, 'could not connect to host ');
}
}

Expand All @@ -95,21 +97,22 @@ public function establishConnection(){
*/
public function catchAll() {

$isCatchallDomain = null;
if($this->options['catchAllEnabled']){
try{
$isCatchallDomain = $this->transport->getSmtp()->acceptsAnyRecipient($this->dom);

}catch (\Exception $e) {
$this->statusManager->setStatus($this->users, $this->dom, $this->options['catchAllIsValid'], 'error while on CatchAll test: '.$e );
}

// if a catchall domain is detected, and we consider
// accounts on such domains as invalid, mark all the
// users as invalid and move on
if ($isCatchallDomain) {
if (!$this->options['catchAllIsValid']) {
$this->statusManager->setStatus($this->users, $this->dom, $this->options['catchAllIsValid'],'catch all detected');
return true;
}else{
return false;
}
}
}
Expand All @@ -123,6 +126,7 @@ public function catchAll() {
*/
public function mailFrom($fromUser,$fromDomain) {
if (!($this->transport->getSmtp()->mail($fromUser. '@' . $fromDomain))) {

// MAIL FROM not accepted, we can't talk
$this->statusManager->setStatus($this->users, $this->dom, $this->options['noCommIsValid'],'MAIL FROM not accepted');
}
Expand All @@ -137,23 +141,43 @@ public function mailFrom($fromUser,$fromDomain) {
public function rcptEachUser($fromUser,$fromDomain){
$this->transport->getSmtp()->noop();
// rcpt to for each user
var_dump("user list :");
var_dump($this->users);
$iterator = 0;
$dynamicTimeout = 0;

if(count($this->users) >= $this->options['sameDomainLimit']){
$dynamicTimeout = count($this->users);
}

foreach ($this->users as $user) {
// TODO: An error from the SMTP couse a disconnect , need to implement a reconnect
var_dump("Checking user :".$user);

if(!$this->transport->getSmtp()->isConnect()){
$this->transport->reconnect($fromUser . '@' . $fromDomain);
var_dump("Connection lost. Reconnect.");
$this->establishConnection();
}

$address = $user . '@' . $this->dom->getDomain();
// Sets the results to an integer 0 ( failure ) or 1 ( success )

// TODO: Impliment setter
$this->statusManager->updateStatus($address,$this->transport->getSmtp()->rcpt($address));
// Sets the results to an integer 0 ( failure ) or 1 ( success )
$result = $this->transport->getSmtp()->rcpt($address);
var_dump("The adress $address, was checked , result: $result");
$this->statusManager->updateStatus($address,$result);

if ($this->statusManager->getStatus($address) == 1) {
if ($iterator == count($this->users)) {
// stop the loop
return true;
return 1;
}

$this->transport->getSmtp()->noop();

// if( $iterator > 0 && $iterator % 4 == 0 ){
// $this->transport->disconnect();
// sleep($dynamicTimeout);
// }

$iterator++;
}
}

Expand All @@ -169,8 +193,5 @@ public function closeConnection () {

public function getDomainInfo() {
// TODO: Create setter for domainMoreInfo
// if ($options['domainMoreInfo']) {
// $this->results->get[$dom->getDomain()] = $dom->getDescription();
// }
}
}
11 changes: 4 additions & 7 deletions lib/SmtpValidatorEmail/Helper/ValidatorInitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ class ValidatorInitHelper{
public function init($emails = array(), $sender, $options = array()) {
$defaultOptions = array(
'domainMoreInfo' => false,
'delaySleep' => array(0),
'delaySleep' => array(0,1),
'noCommIsValid' => 0,
'catchAllIsValid' => 1,
'catchAllIsValid' => 0,
'catchAllEnabled' => 1,
'sameDomainLimit' => 3,
'logPath' => null
'sameDomainLimit' => 5,
);

$emails = is_array($emails) ? EmailHelper::sortEmailsByDomain($emails) : $emails;

$this->options = array_merge($defaultOptions,$options);

$this->setSender($sender);
$this->setBags($emails);
$this->results = new Results();
Expand All @@ -63,7 +60,7 @@ public function init($emails = array(), $sender, $options = array()) {
public function setBags($emails) {
if (!empty($emails)) {
$emailBag = new BagHelper();
$emailBag->add((array)$emails);
$emailBag->add($emails);
$domainBag = $this->setEmailsDomains($emailBag);
$this->domains = $domainBag->all();
}
Expand Down
Loading

0 comments on commit 18bbec2

Please sign in to comment.