Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Yasinsky committed Mar 5, 2013
1 parent 4f79aa4 commit 2ed13de
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 154 deletions.
4 changes: 2 additions & 2 deletions mail.oopzy.com/code/srv/smtp2redis/smtp2redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ private function event_error($buffer, $error, $id)

//just to see whats up on errors, it really should be stringed in extension
$event_errors = array(
'EVBUFFER_READ' => 1,
//'EVBUFFER_READ' => 1,
'EVBUFFER_WRITE' => 2,
'EVBUFFER_EOF' => 16,
//'EVBUFFER_EOF' => 16,
'EVBUFFER_ERROR' => 32,
'EVBUFFER_TIMEOUT' => 64
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@
$dt['from'] = $emailParser->getHeader('from');
$dt['dt'] = $emailParser->getHeader('Date');
$dt['subject'] = $emailParser->getSubject();
$ttl = $redis->ttl($keys[$key]);

$dt['debug'] = $data['debug'];

$dt['expirein'] = date('i:s' ,$ttl );

$emailDeliveredToHeader = $emailParser->getHeader('Delivered-To');

$dt['body'] = $emailParser->getPlainBody();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
<?php
/*
* This returns a json array of boxes for given starting string
*/

require_once APP_PATH. 'models/oopzy_model.php';
/*String*/ function _save_message()
{
$ref = $_POST['ref'];
$to = $_POST['to'];

$tmp = explode(':',$ref);
$tmp = explode('@',$tmp[0]);
$box = $tmp[0];

$toemail = $_POST['to'];

$redis = new Redis();

try{

$redis->connect($GLOBALS['REDISHOST'], $GLOBALS['REDISPORT']);
//put request in a queue

echo json_encode(array($ref,$to));
die;
$oopzy = new Oopzy_model();

$message = '';

//see if link is established already and we can process
if( !$oopzy->is_linked($box, $toemail) )
{
//send verification emai;
$oopzy->verify_email($toemail, $box);

$message = utils::get_message('MSG_SAVE_MESSAGE_VERIFY');
}

$oopzy->do_remail_queue($ref, $box, $toemail);

//set autoremail
//if box quality is low not much we can do box_quality($box)
//also here where we can set $key request

}
catch(Exception $e)
{
echo json_encode(array('error' => $e->getMessage()));
die;
$message = $e->getMessage();
}

utils::error_echo_die($message);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
require_once APP_PATH. 'models/oopzy_model.php';
/*
* This returns a json array of boxes for given starting string
*/
/*String*/ function _verify_email($obj='',$box='')
/*void*/ function _verify_email($obj='',$box='')
{

if(isset($_POST['obj']))
Expand All @@ -16,129 +17,13 @@
$box = urldecode($box);
}

if(empty($obj))
{
utils::error_echo_die($GLOBALS['MESSAGES']['MSG_VERIFY_INVALID']);
}
$verify = new Oopzy_model();

//if obj is email init verification
if(filter_var($obj, FILTER_VALIDATE_EMAIL))
if(!$verify->verify_email($obj, $box))
{
if(stristr($obj, $GLOBALS['host_name']))
{
//we do not verify ourselves
utils::error_echo_die($GLOBALS['MESSAGES']['MSG_VERIFY_NOSELF']) ;
}

if(empty($box))
{
utils::error_echo_die($GLOBALS['MESSAGES']['MSG_VERIFY_INVALID_BOX']);
}

$quality = utils::box_quality($box);

if($quality < 3)
{
utils::error_echo_die($GLOBALS['MESSAGES']['MSG_VERIFY_LOW_BOX_QUALITY'] . "[$quality]");
}

$token = md5($obj . time().$GLOBALS['PRIVATE_KEY']);
$url = 'http://' . $GLOBALS['www_host_name'] . '/verify_email/' . $token;
$message = sprintf($GLOBALS['MESSAGES']['MSG_VERIFY_BODY'], $obj, $url);
$message_html = sprintf($GLOBALS['MESSAGES']['MSG_VERIFY_BODY_HTML'], $obj, $url, $url);
$subject = $GLOBALS['MESSAGES']['MSG_VERIFY_SUBJECT'];

//set request to redis

$redis = new Redis();

try{
$redis->connect($GLOBALS['REDISHOST'], $GLOBALS['REDISPORT']);
$res_redis = $redis->setex($token, 60*10 , serialize( array('to'=>$obj,'for'=>$box)) ); //10 min ttl

require_once APP_PATH. 'vendors/swift/lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('andrew@yasinsky.com')
->setPassword('mus1kpus1k');


// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create the message
$msg = Swift_Message::newInstance()

// Give the message a subject
->setSubject($subject)

// Set the From address with an associative array
->setFrom(array('nobody@' . $GLOBALS['host_name'] => 'Info@' . $GLOBALS['host_name']))

// Set the To addresses with an associative array
->setTo(array($obj))

// Give it a body
->setBody($message)

// And optionally an alternative body
->addPart($message_html, 'text/html')
;

$res_mail = $mailer->send($msg);

if($res_redis && $res_mail)
{
utils::error_echo_die("");
}
}
catch(Exception $e)
{
utils::error_echo_die('Exception: ' . $e->getMessage());
}

utils::error_echo_die($verify->get_error());
}
else
{
//lets see if it is token
if(strlen($obj)!=32)
{
//not token
utils::error_echo_die($GLOBALS['MESSAGES']['MSG_VERIFY_INVALID']);
}

$redis = new Redis();

try{
$redis->connect($GLOBALS['REDISHOST'], $GLOBALS['REDISPORT']);
$res = $redis->get($obj );


if($res)
{
$res = unserialize($res);

if(isset($res['to']) && isset($res['for']))
{

//otherwise start list
$key = md5($res['for'] . $res['to']);

$res_redis = $redis->setex($key, 60*60*24*30 , 1 ); //set for 30 days

utils::error_echo_die('');
}
}

utils::error_echo_die($GLOBALS['MESSAGES']['MSG_VERIFY_INVALID_TOKEN']);

}
catch(Exception $e)
{
utils::error_echo_die('Exception: ' . $e->getMessage());
}

//ok it is token lets see if we have request for on
}
//TODO , Token redirect
utils::error_echo_die("");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
require_once APP_PATH. 'models/oopzy_model.php';
/*
* This returns a json array of boxes for given starting string
*/
/*void*/ function _verify_token($obj='')
{

if(isset($_POST['obj']))
{
$obj = urldecode($_POST['obj']);
}
else
{
$obj = urldecode($obj);
}

$verify = new Oopzy_model();

if(!$verify->verify_token($obj))
{
utils::error_echo_die($verify->get_error());
}

//TODO , Token redirect

utils::error_echo_die("");
}
34 changes: 33 additions & 1 deletion mail.oopzy.com/code/var/mail.oopzy.com/app/helpers/utils.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
<?php
Class utils
{
public function json_indent($json) {

public function get_message($key)
{
if(isset($GLOBALS['MESSAGES'][$key]))
{
return $GLOBALS['MESSAGES'][$key];
}
else
{
return $key;
}
}

public function get_redis($key=false)
{

$redis = new Redis();

try{
//here where we get proper connection

$redis->connect($GLOBALS['REDISHOST'], $GLOBALS['REDISPORT']);
return $redis;

}
catch(Exception $e)
{
throw new Exception($e->getMessage());
}
}

public function json_indent($json) {

$result = '';
$pos = 0;
Expand Down Expand Up @@ -55,6 +86,7 @@ public function json_indent($json) {
public function box_quality($box)
{
$quality = 0;

//if the password length is less than 6, return message.
if (strlen($box) < 6)
{
Expand Down
Loading

0 comments on commit 2ed13de

Please sign in to comment.