diff options
Diffstat (limited to 'app/Core')
-rw-r--r-- | app/Core/Base.php | 6 | ||||
-rw-r--r-- | app/Core/EmailClient.php | 49 | ||||
-rw-r--r-- | app/Core/HttpClient.php | 75 |
3 files changed, 101 insertions, 29 deletions
diff --git a/app/Core/Base.php b/app/Core/Base.php index cb8e4487..6cb87cbc 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -11,6 +11,7 @@ use Pimple\Container; * @author Frederic Guillot * * @property \Core\Helper $helper + * @property \Core\EmailClient $emailClient * @property \Core\HttpClient $httpClient * @property \Core\Paginator $paginator * @property \Core\Request $request @@ -21,10 +22,11 @@ use Pimple\Container; * @property \Integration\GitlabWebhook $gitlabWebhook * @property \Integration\HipchatWebhook $hipchatWebhook * @property \Integration\Jabber $jabber - * @property \Integration\MailgunWebhook $mailgunWebhook - * @property \Integration\PostmarkWebhook $postmarkWebhook + * @property \Integration\Mailgun $mailgun + * @property \Integration\Postmark $postmark * @property \Integration\SendgridWebhook $sendgridWebhook * @property \Integration\SlackWebhook $slackWebhook + * @property \Integration\Smtp $smtp * @property \Model\Acl $acl * @property \Model\Action $action * @property \Model\Authentication $authentication diff --git a/app/Core/EmailClient.php b/app/Core/EmailClient.php new file mode 100644 index 00000000..b1986502 --- /dev/null +++ b/app/Core/EmailClient.php @@ -0,0 +1,49 @@ +<?php + +namespace Core; + +/** + * Mail client + * + * @package core + * @author Frederic Guillot + */ +class EmailClient extends Base +{ + /** + * Send a HTML email + * + * @access public + * @param string $email + * @param string $name + * @param string $subject + * @param string $html + */ + public function send($email, $name, $subject, $html) + { + $this->container['logger']->debug('Sending email to '.$email.' ('.MAIL_TRANSPORT.')'); + + $start_time = microtime(true); + $author = 'Kanboard'; + + if (Session::isOpen() && $this->userSession->isLogged()) { + $author = e('%s via Kanboard', $this->user->getFullname($this->session['user'])); + } + + switch (MAIL_TRANSPORT) { + case 'sendgrid': + $this->sendgrid->sendEmail($email, $name, $subject, $html, $author); + break; + case 'mailgun': + $this->mailgun->sendEmail($email, $name, $subject, $html, $author); + break; + case 'postmark': + $this->postmark->sendEmail($email, $name, $subject, $html, $author); + break; + default: + $this->smtp->sendEmail($email, $name, $subject, $html, $author); + } + + $this->container['logger']->debug('Email sent in '.round(microtime(true) - $start_time, 6).' seconds'); + } +} diff --git a/app/Core/HttpClient.php b/app/Core/HttpClient.php index 0803ec7a..2f280a1e 100644 --- a/app/Core/HttpClient.php +++ b/app/Core/HttpClient.php @@ -2,22 +2,20 @@ namespace Core; -use Pimple\Container; - /** * HTTP client * * @package core * @author Frederic Guillot */ -class HttpClient +class HttpClient extends Base { /** * HTTP connection timeout in seconds * * @var integer */ - const HTTP_TIMEOUT = 2; + const HTTP_TIMEOUT = 5; /** * Number of maximum redirections for the HTTP client @@ -31,46 +29,60 @@ class HttpClient * * @var string */ - const HTTP_USER_AGENT = 'Kanboard Webhook'; + const HTTP_USER_AGENT = 'Kanboard'; /** - * Container instance + * Send a POST HTTP request encoded in JSON * - * @access protected - * @var \Pimple\Container + * @access public + * @param string $url + * @param array $data + * @param array $headers + * @return string */ - protected $container; + public function postJson($url, array $data, array $headers = array()) + { + return $this->doRequest( + $url, + json_encode($data), + array_merge(array('Content-type: application/json'), $headers) + ); + } /** - * Constructor + * Send a POST HTTP request encoded in www-form-urlencoded * * @access public - * @param \Pimple\Container $container + * @param string $url + * @param array $data + * @param array $headers + * @return string */ - public function __construct(Container $container) + public function postForm($url, array $data, array $headers = array()) { - $this->container = $container; + return $this->doRequest( + $url, + http_build_query($data), + array_merge(array('Content-type: application/x-www-form-urlencoded'), $headers) + ); } /** - * Send a POST HTTP request + * Make the HTTP request * - * @access public + * @access private * @param string $url - * @param array $data + * @param array $content + * @param array $headers * @return string */ - public function post($url, array $data) + private function doRequest($url, $content, array $headers) { if (empty($url)) { return ''; } - $headers = array( - 'User-Agent: '.self::HTTP_USER_AGENT, - 'Content-Type: application/json', - 'Connection: close', - ); + $headers = array_merge(array('User-Agent: '.self::HTTP_USER_AGENT, 'Connection: close'), $headers); $context = stream_context_create(array( 'http' => array( @@ -79,16 +91,25 @@ class HttpClient 'timeout' => self::HTTP_TIMEOUT, 'max_redirects' => self::HTTP_MAX_REDIRECTS, 'header' => implode("\r\n", $headers), - 'content' => json_encode($data) + 'content' => $content ) )); - $response = @file_get_contents(trim($url), false, $context); + $stream = @fopen(trim($url), 'r', false, $context); + $response = ''; + + if (is_resource($stream)) { + $response = stream_get_contents($stream); + } + else { + $this->container['logger']->error('HttpClient: request failed'); + } if (DEBUG) { - $this->container['logger']->debug($url); - $this->container['logger']->debug(var_export($data, true)); - $this->container['logger']->debug($response); + $this->container['logger']->debug('HttpClient: url='.$url); + $this->container['logger']->debug('HttpClient: payload='.$content); + $this->container['logger']->debug('HttpClient: metadata='.var_export(@stream_get_meta_data($stream), true)); + $this->container['logger']->debug('HttpClient: response='.$response); } return $response; |