2014-05-21 10:58:25 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link https://www.phptesting.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PHPCI\Plugin;
|
|
|
|
|
|
|
|
use PHPCI\Builder;
|
|
|
|
use PHPCI\Model\Build;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XMPP Notification - Send notification for successful or failure build
|
|
|
|
* @author Alexandre Russo <dev.github@ange7.com>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
|
|
|
class XMPP implements \PHPCI\Plugin
|
|
|
|
{
|
|
|
|
protected $directory;
|
|
|
|
protected $phpci;
|
|
|
|
protected $build;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string, username of sender account xmpp
|
|
|
|
*/
|
|
|
|
protected $username;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string, alias server of sender account xmpp
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string, password of sender account xmpp
|
|
|
|
*/
|
|
|
|
protected $password;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string, alias for sender
|
|
|
|
*/
|
|
|
|
protected $alias;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string, use tls
|
|
|
|
*/
|
|
|
|
protected $tls;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array, list of recipients xmpp accounts
|
|
|
|
*/
|
2014-05-21 11:24:17 +02:00
|
|
|
protected $recipients;
|
2014-05-21 10:58:25 +02:00
|
|
|
|
2014-05-21 11:16:05 +02:00
|
|
|
/**
|
|
|
|
* @var string, mask to format date
|
|
|
|
*/
|
2014-05-21 11:24:17 +02:00
|
|
|
protected $date_format;
|
2014-05-21 11:16:05 +02:00
|
|
|
|
2014-05-21 10:58:25 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param Builder $phpci
|
|
|
|
* @param Build $build
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = array())
|
|
|
|
{
|
|
|
|
$this->phpci = $phpci;
|
|
|
|
$this->build = $build;
|
|
|
|
|
2014-05-21 11:24:17 +02:00
|
|
|
$this->username = '';
|
|
|
|
$this->password = '';
|
|
|
|
$this->server = '';
|
|
|
|
$this->alias = '';
|
|
|
|
$this->recipients = array();
|
|
|
|
$this->tls = false;
|
2014-05-21 12:24:24 +02:00
|
|
|
$this->date_format = '%c';
|
2014-05-21 10:58:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set recipients list
|
|
|
|
*/
|
2014-07-11 15:07:27 +02:00
|
|
|
if (!empty($options['recipients'])) {
|
|
|
|
if (is_string($options['recipients'])) {
|
2014-05-21 10:58:25 +02:00
|
|
|
$this->recipients = array($options['recipients']);
|
2014-07-11 15:07:27 +02:00
|
|
|
} elseif (is_array($options['recipients'])) {
|
2014-05-21 10:58:25 +02:00
|
|
|
$this->recipients = $options['recipients'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setOptions($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set options configuration for plugin
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
protected function setOptions($options)
|
|
|
|
{
|
2014-07-11 15:07:27 +02:00
|
|
|
foreach (array('username', 'password', 'alias', 'tls', 'server', 'date_format') as $key) {
|
2014-05-21 10:58:25 +02:00
|
|
|
if (array_key_exists($key, $options)) {
|
|
|
|
$this->{$key} = $options[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get config format for sendxmpp config file
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getConfigFormat()
|
|
|
|
{
|
|
|
|
$conf = $this->username;
|
2014-07-11 15:07:27 +02:00
|
|
|
if (!empty($this->server)) {
|
2014-05-21 10:58:25 +02:00
|
|
|
$conf .= ';'.$this->server;
|
|
|
|
}
|
|
|
|
|
|
|
|
$conf .= ' '.$this->password;
|
|
|
|
|
2014-07-11 15:07:27 +02:00
|
|
|
if (!empty($this->alias)) {
|
2014-05-21 10:58:25 +02:00
|
|
|
$conf .= ' '.$this->alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-21 12:20:12 +02:00
|
|
|
* Find config file for sendxmpp binary (default is .sendxmpprc)
|
2014-05-21 10:58:25 +02:00
|
|
|
*/
|
2014-05-21 12:20:12 +02:00
|
|
|
public function findConfigFile()
|
2014-05-21 10:58:25 +02:00
|
|
|
{
|
2015-10-23 05:18:22 +02:00
|
|
|
if (file_exists($this->phpci->buildPath . DIRECTORY_SEPARATOR . '.sendxmpprc')) {
|
|
|
|
if (md5(file_get_contents($this->phpci->buildPath . DIRECTORY_SEPARATOR . '.sendxmpprc'))
|
|
|
|
!== md5($this->getConfigFormat())) {
|
2014-05-21 10:58:25 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send notification message.
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
2015-04-14 12:17:52 +02:00
|
|
|
$sendxmpp = $this->phpci->findBinary('sendxmpp');
|
2014-05-21 10:58:25 +02:00
|
|
|
|
2014-05-21 11:08:45 +02:00
|
|
|
/*
|
|
|
|
* Without recipients we can't send notification
|
|
|
|
*/
|
|
|
|
if (count($this->recipients) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:58:25 +02:00
|
|
|
/*
|
|
|
|
* Try to build conf file
|
|
|
|
*/
|
2015-10-23 05:18:22 +02:00
|
|
|
$config_file = $this->phpci->buildPath . DIRECTORY_SEPARATOR . '.sendxmpprc';
|
2014-07-11 15:07:27 +02:00
|
|
|
if (is_null($this->findConfigFile())) {
|
2015-03-28 15:50:40 +01:00
|
|
|
file_put_contents($config_file, $this->getConfigFormat());
|
|
|
|
chmod($config_file, 0600);
|
2014-05-21 10:58:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enabled ssl for connection
|
|
|
|
*/
|
|
|
|
$tls = '';
|
2014-07-11 15:07:27 +02:00
|
|
|
if ($this->tls) {
|
2014-05-21 10:58:25 +02:00
|
|
|
$tls = ' -t';
|
|
|
|
}
|
|
|
|
|
2015-10-23 05:18:22 +02:00
|
|
|
$message_file = $this->phpci->buildPath . DIRECTORY_SEPARATOR . uniqid('xmppmessage');
|
2014-07-11 15:07:27 +02:00
|
|
|
if ($this->buildMessage($message_file) === false) {
|
2014-05-21 12:24:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:58:25 +02:00
|
|
|
/*
|
|
|
|
* Send XMPP notification for all recipients
|
|
|
|
*/
|
2015-03-28 15:50:40 +01:00
|
|
|
$cmd = $sendxmpp . "%s -f %s -m %s %s";
|
2014-05-21 21:57:40 +02:00
|
|
|
$recipients = implode(' ', $this->recipients);
|
2014-05-21 10:58:25 +02:00
|
|
|
|
2015-03-28 15:50:40 +01:00
|
|
|
$success = $this->phpci->executeCommand($cmd, $tls, $config_file, $message_file, $recipients);
|
2014-05-21 21:57:40 +02:00
|
|
|
|
2014-05-21 12:25:05 +02:00
|
|
|
print $this->phpci->getLastOutput();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove temp message file
|
|
|
|
*/
|
|
|
|
$this->phpci->executeCommand("rm -rf ".$message_file);
|
|
|
|
|
|
|
|
return $success;
|
2014-05-21 10:58:25 +02:00
|
|
|
}
|
2014-05-21 11:16:05 +02:00
|
|
|
|
|
|
|
/**
|
2014-07-11 15:07:27 +02:00
|
|
|
* @param $message_file
|
|
|
|
* @return int
|
2014-05-21 11:16:05 +02:00
|
|
|
*/
|
2014-05-21 12:24:51 +02:00
|
|
|
protected function buildMessage($message_file)
|
2014-05-21 11:16:05 +02:00
|
|
|
{
|
2014-07-11 15:07:27 +02:00
|
|
|
if ($this->build->isSuccessful()) {
|
|
|
|
$message = "✔ [".$this->build->getProjectTitle()."] Build #" . $this->build->getId()." successful";
|
2014-05-21 11:16:05 +02:00
|
|
|
} else {
|
2014-07-11 15:07:27 +02:00
|
|
|
$message = "✘ [".$this->build->getProjectTitle()."] Build #" . $this->build->getId()." failure";
|
2014-05-21 11:16:05 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 11:24:17 +02:00
|
|
|
$message .= ' ('.strftime($this->date_format).')';
|
2014-05-21 12:24:51 +02:00
|
|
|
|
|
|
|
return file_put_contents($message_file, $message);
|
2014-05-21 11:16:05 +02:00
|
|
|
}
|
2014-05-21 10:58:25 +02:00
|
|
|
}
|