Commit cbb42475 authored by lphuberdeau's avatar lphuberdeau

[MOD] Begining refactoring for the mail-in feature (calling code not included yet)

parent dc5199a1
......@@ -3009,6 +3009,17 @@ lib/core/Tiki/Event/Function/EventTrigger.php -text
lib/core/Tiki/Event/Lib.php -text
lib/core/Tiki/Event/Manager.php -text
lib/core/Tiki/Event/index.php -text
lib/core/Tiki/MailIn/Account.php -text
lib/core/Tiki/MailIn/Action/ActionInterface.php -text
lib/core/Tiki/MailIn/Action/ArticlePut.php -text
lib/core/Tiki/MailIn/Action/DirectFactory.php -text
lib/core/Tiki/MailIn/Action/FactoryInterface.php -text
lib/core/Tiki/MailIn/Action/SubjectPrefixFactory.php -text
lib/core/Tiki/MailIn/Exception/MailInException.php -text
lib/core/Tiki/MailIn/Exception/TransportException.php -text
lib/core/Tiki/MailIn/Source/Message.php -text
lib/core/Tiki/MailIn/Source/Pop3.php -text
lib/core/Tiki/MailIn/Source/SourceInterface.php -text
lib/core/Tiki/MemoryLimit.php -text
lib/core/Tiki/PageCache.php -text
lib/core/Tiki/Profile.php -text
......
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn;
class Account
{
private $source;
private $actionFactory;
private $accountAddress;
private $anonymousAllowed;
private $adminAllowed;
private $sendResponses;
private $discardAfter;
public static function fromDb(array $acc)
{
$account = new self;
$account->source = new Source\Pop3($acc['pop'], $acc['port'], $acc['username'], $acc['pass']);
switch ($acc['type']) {
case 'article-put':
$this->factory = new Action\DirectFactory('Tiki\MailIn\Action\ArticlePut', array(
'topic' => $acc['article_topicId'],
'type' => $acc['article_type'],
));
break;
case 'wiki-put':
$this->factory = new Action\DirectFactory('Tiki\MailIn\Action\WikiPut');
break;
case 'wiki-get':
$this->factory = new Action\DirectFactory('Tiki\MailIn\Action\WikiGet');
break;
case 'wiki-append':
$this->factory = new Action\DirectFactory('Tiki\MailIn\Action\WikiAppend');
break;
case 'wiki-prepend':
$this->factory = new Action\DirectFactory('Tiki\MailIn\Action\WikiPrepend');
break;
case 'wiki':
$this->factory = new Action\SubjectPrefixFactory(array(
'GET:' => new Action\DirectFactory('Tiki\MailIn\Action\WikiGet'),
'APPEND:' => new Action\DirectFactory('Tiki\MailIn\Action\WikiAppend'),
'PREPEND:' => new Action\DirectFactory('Tiki\MailIn\Action\WikiPrepend'),
'PUT:' => new Action\DirectFactory('Tiki\MailIn\Action\WikiPut'),
'' => new Action\DirectFactory('Tiki\MailIn\Action\WikiPut'),
));
break;
default:
throw new Exception\MailInException("Action factory not found.");
}
$account->accountAddress = $acc['account'];
$account->anonymousAllowed = $acc['anonymous'] == 'y';
$account->adminAllowed = $acc['admin'] == 'y';
$account->sendResponses = $acc['respond_email'] == 'y';
$account->discardAfter = $acc['discard_after'];
return $account;
}
private function __construct()
{
}
function getMessages()
{
return $this->source->getMessage();
}
function isAnyoneAllowed()
{
return $this->anonymous;
}
function canReceive(Source\Message $message)
{
$user = $message->getAssociatedUser();
$perms = TikiLib::lib('tiki')->get_user_permission_accessor($user, null, null);
if (! $user) {
return $this->anonymousAllowed;
} elseif ($perms->admin) {
return $this->adminAllowed;
} else {
$userlib = TikiLib::lib('user');
return $perms->send_mailin;
}
}
function getAction(Source\Message $message)
{
return $this->actionFactory->createAction($this, $message);
}
function prepareMessage(Source\Message $message)
{
// TODO : This is rather primitive and implies we control the message source, need to make smarter
if ($this->discardAfter) {
$body = $message->getBody();
$pos = strpos($body, $this->discardAfter);
if ($pos !== false) {
$body = substr($body, 0, $pos);
$message->setBody($body);
}
}
}
function sendFailureResponse(Source\Message $message)
{
if (! $this->sendResponses) {
return;
}
global $prefs;
$l = $prefs['language'];
$mail = new TikiMail();
$mail->setFrom($this->accountAddress);
$mail->setSubject(tra('Tiki mail-in auto-reply', $l));
$mail->setText(tra("Sorry, you can't use this feature.", $l));
$mail->send(array($message->getFromAddress()), 'mail');
}
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Action;
use Tiki\MailIn\Account;
use Tiki\MailIn\Source\Message;
interface ActionInterface
{
function isEnabled();
function isAllowed(Account $account, Message $message);
function execute(Account $account, Message $message);
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Action;
use Tiki\MailIn\Account;
use Tiki\MailIn\Source\Message;
class ArticlePut extends ActionInterface
{
private $topicId;
private $type;
function __construct(array $params)
{
$this->topicId = isset($params['topic']) ? intval($params['topic']) : 0;
$this->type = isset($params['type']) ? intval($params['type']) : null;
}
function isEnabled()
{
global $prefs;
return $prefs['feature_articles'] == 'y';
}
function isAllowed(Account $account, Message $message)
{
$user = $message->getAssociatedUser();
$perms = TikiLib::lib('tiki')->get_user_permission_accessor($user, 'topic', $this->topicId);
if (! $perms->submit_article && ! $perms->edit_submission) {
return false;
}
return true;
}
function execute(Account $account, Message $message)
{
$artlib = TikiLib::lib('art');
$tikilib = TikiLib::lib('tiki');
$title = $message->getSubject();
$heading = $message->getBody();
$topicId = $this->topicId;
$userm = $message->getAssociatedUser();
$authorName = $userm;
$body = '';
$publishDate = $tikilib->now;
$cur_time = explode(',', $tikilib->date_format('%Y,%m,%d,%H,%M,%S', $publishDate));
$expireDate = $tikilib->make_time($cur_time[3], $cur_time[4], $cur_time[5], $cur_time[1], $cur_time[2], $cur_time[0] + 1);
$subId = 0;
$type = $this->type;
$useImage = 'n';
$image_x = '';
$image_y = '';
$imgname = '';
$imgsize = '';
$imgtype = '';
$imgdata = '';
$topline = '';
$subtitle = '';
$linkto = '';
$image_caption = '';
$lang = '';
$rating = 7;
$isfloat = 'n';
$subid = $artlib->replace_submission($title, $authorName, $topicId, $useImage, $imgname, $imgsize, $imgtype, $imgdata, $heading, $body, $publishDate, $expireDate, $userm, $subId, $image_x, $image_y, $type, $topline, $subtitle, $linkto, $image_caption, $lang, $rating, $isfloat);
$perms = TikiLib::lib('tiki')->get_user_permission_accessor($user, 'topic', $this->topicId);
if ($perms->autoapprove_submission) {
$artlib->approve_submission($subid);
}
}
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Action;
use Tiki\MailIn\Account;
use Tiki\MailIn\Source\Message;
class DirectFactory implements FactoryInterface
{
private $class;
private $parameters;
function __construct($class, array $parameters = [])
{
$this->class = $class;
$this->parameters = $parameters;
}
function createAction(Account $account, Message $message)
{
$class = $this->class;
return new $class($this->parameters);
}
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Action;
use Tiki\MailIn\Account;
use Tiki\MailIn\Source\Message;
interface FactoryInterface
{
function createAction(Account $account, Message $message);
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Action;
use Tiki\MailIn\Account;
use Tiki\MailIn\Source\Message;
use Tiki\MailIn\Exception\MailInException;
class SubjectPrefixFactory implements FactoryInterface
{
private $config;
function __construct($config)
{
$this->config = $config;
}
function createAction(Account $account, Message $message)
{
$subject = $message->getSubject();
foreach ($this->config as $prefix => $factory) {
if (strpos($subject, $prefix) === 0) {
$subject = trim(substr($subject, strlen($prefix)));
$message->setSubject($subject);
return $factory->createAction($account, $message);
}
}
throw new MailInException(tr("Unable to find suitable action."));
}
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Exception;
class MailInException extends \Exception
{
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Exception;
class TransportException extends MailInException
{
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Source;
class Message
{
private $id;
private $deleteCallback;
private $messageId;
private $from;
private $subject;
private $body;
private $associatedUser;
function __construct($id, $deleteCallback)
{
$this->id = $id;
$this->deleteCallback = $deleteCallback;
}
function setMessageId($messageId)
{
$this->messageId = $messageId;
}
function setRawFrom($from)
{
$this->from = $from;
if ($email = $this->getFromAddress()) {
$userlib = TikiLib::lib('user');
$this->associatedUser = $userlib->get_user_by_email($email_from);
}
}
function getFromAddress()
{
preg_match('/<?([-!#$%&\'*+\.\/0-9=?A-Z^_`a-z{|}~][email protected][-!#$%&\'*+\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\.\/0-9=?A-Z^_`a-z{|}~]+)>?/', $this->from, $mail);
return $mail[1];
}
function getAssociatedUser()
{
return $this->associatedUser;
}
function delete()
{
if ($this->deleteCallback) {
$callback = $this->deleteCallback;
$callback();
$this->deleteCallback = null;
}
}
function setSubject($subject)
{
$this->subject = $subject;
}
function getSubject()
{
return $this->subject;
}
function setBody($body)
{
$this->body = $body;
}
function getBody()
{
return $this->body;
}
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Source;
class Pop3 implements SourceInterface
{
private $host;
private $port;
private $username;
private $password;
function __construct($host, $port, $username, $password)
{
$this->host = $host;
$this->port = (int) $port;
$this->username = $username;
$this->password = $password;
}
function getMessages()
{
$pop = $this->connect();
$messageCount = $pop->numMsg();
$mimelib = new mime();
for ($i = 1; $i <= $messageCount; ++$i) {
$headers = $this->getParsedHeaders($i);
$body = $pop3->getMsg($i);
if (! $headers) {
continue; // Headers not parsable, skip message
}
$info = $mimelib->decode($message);
$message = new Message($i, function () use ($pop, $i) {
$pop->deleteMsg($i);
});
$message->setMessageId(preg_replace(['<', ']'], '', $aux['Message-ID']));
$message->setRawFrom(isset($aux['From']) ? $aux['From'] : $aux['Return-path']);
$message->setSubject($info['header']['subject']);
$message->setBody($this->getBody($body));
}
$pop->disconnect();
}
private function connect()
{
$pop = new Net_Pop3;
if (! $pop->connect($this->host, $this->port)) {
throw new TransportException(tr("Failed to connect to POP3 account on %0:%1", $this->host, $this->port));
}
if (false === $pop->login($this->username, $this->password, "USER")) {
throw new TransportException(tr("Login failed for POP3 account on %0:%1 for user %2", $this->host, $this->password, $this->username));
}
return $pop;
}
private function getBody($output)
{
if (isset($output['text'][0])) {
$body = $output["text"][0];
} elseif (isset($output['parts'][0])) {
$body = $this->getBody($output['parts'][0]);
} else {
$body = '';
}
return $body;
}
}
<?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\MailIn\Source;
interface SourceInterface
{
function getMessages();
}
......@@ -3615,24 +3615,33 @@ class TikiLib extends TikiDb_Bridge
* @return bool
*/
function user_has_perm_on_object($usertocheck,$object,$objtype,$perm1,$perm2=null,$perm3=null)
{
$accessor = $tikilib->get_user_permission_accessor($usertocheck, $objtype, $object);
$chk1 = $perm1 != null ? $accessor->$perm1 : true;
$chk2 = $perm2 != null ? $accessor->$perm2 : true;
$chk3 = $perm3 != null ? $accessor->$perm3 : true;
return $chk1 && $chk2 && $chk3;
}
function get_user_permission_accessor($usertocheck, $type = null, $object = null)
{
global $user;
// Do not override perms for current users otherwise security tokens won't work
if ($usertocheck != $user) {
$groups = $this->get_user_groups($usertocheck);
if ($type && $object) {
$context = array( 'type' => $objtype, 'object' => $object );
$accessor = Perms::get($context);
} else {
$accessor = Perms::get();
}
$context = array( 'type' => $objtype, 'object' => $object );
$accessor = Perms::get($context);
// Do not override perms for current users otherwise security tokens won't work
if ($usertocheck != $user) {
$groups = $this->get_user_groups($usertocheck);
$accessor->setGroups($groups);
}
$chk1 = $perm1 != null ? $accessor->$perm1 : true;
$chk2 = $perm2 != null ? $accessor->$perm2 : true;
$chk3 = $perm3 != null ? $accessor->$perm3 : true;
return $chk1 && $chk2 & $chk3;
return $accessor;
}
/* get all the perm of an object either in a table or global+smarty set
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment