diff --git a/packages/web/lib/fog/system.class.php b/packages/web/lib/fog/system.class.php index befd9c73ab..3450236c92 100644 --- a/packages/web/lib/fog/system.class.php +++ b/packages/web/lib/fog/system.class.php @@ -53,7 +53,7 @@ private static function _versionCompare() public function __construct() { self::_versionCompare(); - define('FOG_VERSION', '1.5.10.1734'); + define('FOG_VERSION', '1.5.10.1735'); define('FOG_SCHEMA', 273); define('FOG_BCACHE_VER', 141); define('FOG_CLIENT_VERSION', '0.13.0'); diff --git a/packages/web/lib/plugins/ntfy/class/ntfy.class.php b/packages/web/lib/plugins/ntfy/class/ntfy.class.php new file mode 100644 index 0000000000..2b7c42eedc --- /dev/null +++ b/packages/web/lib/plugins/ntfy/class/ntfy.class.php @@ -0,0 +1,50 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * The ntfy database and object definer + * + * @category Ntfy + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class Ntfy extends FOGController +{ + /** + * The ntfy table. + * + * @var string + */ + protected $databaseTable = 'ntfy'; + /** + * The database fields and commonized items. + * + * @var array + */ + protected $databaseFields = array( + 'id' => 'nID', + 'serverURL' => 'nServerURL', + 'topicEndpoint' => 'nTopicEndpoint', + 'credentials' => 'nCredentials', + ); + /** + * The required fields + * + * @var array + */ + protected $databaseFieldsRequired = array( + 'serverURL', + 'topicEndpoint', + ); +} diff --git a/packages/web/lib/plugins/ntfy/class/ntfyexception.class.php b/packages/web/lib/plugins/ntfy/class/ntfyexception.class.php new file mode 100644 index 0000000000..66909f24b4 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/class/ntfyexception.class.php @@ -0,0 +1,24 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Exception class for ntfy + * + * @category NtfyException + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class NtfyException extends Exception +{ +} diff --git a/packages/web/lib/plugins/ntfy/class/ntfyextends.class.php b/packages/web/lib/plugins/ntfy/class/ntfyextends.class.php new file mode 100644 index 0000000000..21c3995a94 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/class/ntfyextends.class.php @@ -0,0 +1,110 @@ + + * @license http://opensource.org/license/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * The base class of ntfy elements + * + * Extends the ntfy elements into the event class. + * + * @category NtfyExtends + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/license/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +abstract class NtfyExtends extends Event +{ + /** + * The name + * + * @var string + */ + protected $name; + /** + * The description + * + * @var string + */ + protected $description; + /** + * The event loop + * + * @var mixed + */ + protected static $eventloop; + /** + * The elements to use + * + * @var mixed + */ + protected static $elements; + /** + * The short description + * + * @var mixed + */ + protected static $shortdesc; + /** + * The message + * + * @var mixed + */ + protected static $message; + /** + * The item is active + * + * @var bool + */ + public $active; + /** + * Initialize the class item + * + * @return void + */ + public function __construct() + { + parent::__construct(); + self::$eventloop = function (&$Ntfy) { + self::getClass( + 'NtfyHandler', + $Ntfy->get('serverURL'), + $Ntfy->get('topicEndpoint') + )->pushNote( + '', + sprintf( + '%s %s', + self::$elements['HostName'], + _(self::$shortdesc) + ), + _(self::$message) + ); + }; + } + /** + * Perform action + * + * @param string $event the event to enact + * @param mixed $data the data + * + * @return void + */ + public function onEvent($event, $data) + { + self::$elements = $data; + array_map( + self::$eventloop, + (array)self::getClass('NtfyManager')->find() + ); + } +} diff --git a/packages/web/lib/plugins/ntfy/class/ntfyhandler.class.php b/packages/web/lib/plugins/ntfy/class/ntfyhandler.class.php new file mode 100644 index 0000000000..caed676725 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/class/ntfyhandler.class.php @@ -0,0 +1,143 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Ntfy handler + * + * @category NtfyHandler + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class NtfyHandler extends Ntfy +{ + private $_curlCallback; + /** + * The associated link + endpoint to send curl request to + * + * @var string + */ + private $topicURL; + /** + * Ntfy constructor. + * + * @throws NtfyException + */ + public function __construct($serverURL, $topicEndpoint) + { + $this->topicURL = $serverURL . '/' . $topicEndpoint; + if (!function_exists('curl_init')) { + throw new NftyException( + 'cURL library is not loaded.' + ); + } + } + /** + * Push a note. + * + * @param string $recipient The recipient. + * @param string $title The note's title. + * @param string $body The note's message. + * + * @return object Response. + * @throws NtfyException + */ + public function pushNote( + $recipient, + $title, + $body = null + ) { + $data = array(); + NtfyHandler::_parseRecipient( + $recipient, + $data + ); + $data['type'] = 'note'; + $data['title'] = $title; + $data['body'] = $body; + + return $this->_curlRequest( + $this->topicURL, + 'POST', + $data + ); + } + /** + * Add a callback function that will be invoked + * right before executing each cURL request. + * + * @param callable $callback The callback function. + * + * @return void + */ + public function addCurlCallback(callable $callback) + { + $this->_curlCallback = $callback; + } + /** + * Parse recipient. + * + * @param string $recipient Recipient string. + * @param array $data Data array to populate with + * the correct recipient parameter. + * + * @return void + */ + private static function _parseRecipient($recipient, array &$data) + { + if (!empty($recipient)) { + if (filter_var($recipient, FILTER_VALIDATE_EMAIL) !== false) { + $data[email] = $recipient; + } else { + if (substr($recipient, 0, 1) == "#") { + $data[channel_tag] = substr($recipient, 1); + } else { + $data[device_iden] = $recipient; + } + } + } + } + /** + * Send a request to a remote server using cURL. + * + * @param string $url URL to send the request to. + * @param string $method HTTP method. + * @param array $data Query data. + * @param bool $sendAsJSON Send the request as JSON. + * @param bool $auth Use the API key to authenticate + * + * @return object Response. + * @throws NtfyException + */ + private function _curlRequest( + $url, + $method, + $data = null, + $sendAsJSON = false, + $auth = true + ) { + $data = self::$FOGURLRequests->process( + $url, + $method, + $data, + $sendAsJSON, + ( + $auth ? + $this->_apiKey : + false + ), + $this->_curlCallback + ); + return json_decode($data[0]); + } +} \ No newline at end of file diff --git a/packages/web/lib/plugins/ntfy/class/ntfymanager.class.php b/packages/web/lib/plugins/ntfy/class/ntfymanager.class.php new file mode 100644 index 0000000000..a99b396dca --- /dev/null +++ b/packages/web/lib/plugins/ntfy/class/ntfymanager.class.php @@ -0,0 +1,93 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Manager class for ntfy + * + * @category NtfyManager + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class NtfyManager extends FOGManagerController +{ + /** + * The base table name. + * + * @var string + */ + public $tablename = 'ntfy'; + /** + * Installs the database for the plugin. + * + * @param string $name the name of the plugin. + * + * @return bool + */ + public function install() + { + $this->uninstall(); + + $fields = array( + 'nID', + 'nServerURL', + 'nTopicEndpoint', + 'nCredentials' + ); + $types = array( + 'INTEGER', + 'VARCHAR(255)', + 'VARCHAR(255)', + 'VARCHAR(255)' + ); + $notnulls = array( + false, + false, + false, + false + ); + $defaults = array( + false, + false, + false, + false + ); + $keys = array( + 'nID' + ); + $sql = Schema::createTable( + $this->tablename, + true, + $fields, + $types, + $notnulls, + $defaults, + $keys, + 'InnoDB', + 'utf8', + 'nID', + 'nID' + ); + + return self::$DB->query($sql); + } + // /** + // * Uninstalls. + // * + // * @return bool + // */ + // public function uninstall() + // { + // return true; + // } +} diff --git a/packages/web/lib/plugins/ntfy/config/plugin.config.php b/packages/web/lib/plugins/ntfy/config/plugin.config.php new file mode 100644 index 0000000000..024470cc24 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/config/plugin.config.php @@ -0,0 +1,28 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Plugin configuration file. + * + * @category Config + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +$fog_plugin = array(); +$fog_plugin['name'] = 'ntfy'; +$fog_plugin['description'] = 'Adds ntfy notifications ' + . 'using either ntfy.sh default server or self-hosted server.'; +$fog_plugin['menuicon'] = 'fa fa-comment fa-fw'; +$fog_plugin['menuicon_hover'] = null; +$fog_plugin['entrypoint'] = 'html/run.php'; diff --git a/packages/web/lib/plugins/ntfy/events/imagecomplete_ntfy.event.php b/packages/web/lib/plugins/ntfy/events/imagecomplete_ntfy.event.php new file mode 100644 index 0000000000..04191bbe15 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/events/imagecomplete_ntfy.event.php @@ -0,0 +1,74 @@ + + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Pushes notification on image completion. + * + * @category ImageComplete_Ntfy + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class ImageComplete_Ntfy extends NtfyExtends +{ + /** + * Name of this event. + * + * @var string + */ + protected $name = 'ImageComplete_Ntfy'; + /** + * Description of this event. + * + * @var string + */ + protected $description = 'Triggers when a host finishes imaging'; + /** + * Active flag. + * + * @var bool + */ + public $active = true; + /** + * Initialize object. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + self::$EventManager + ->register( + 'HOST_IMAGE_COMPLETE', + $this + ) + ->register( + 'HOST_IMAGEUP_COMPLETE', + $this + ); + } + /** + * Perform action when event met. + * + * @param string $event The event to perform from. + * @param mixed $data The data to send. + * + * @return void + */ + public function onEvent($event, $data) + { + self::$message = 'This host has finished imaging.'; + self::$shortdesc = 'Imaging Complete'; + parent::onEvent($event, $data); + } +} diff --git a/packages/web/lib/plugins/ntfy/events/imagefail_ntfy.event.php b/packages/web/lib/plugins/ntfy/events/imagefail_ntfy.event.php new file mode 100644 index 0000000000..4cecf1e0e9 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/events/imagefail_ntfy.event.php @@ -0,0 +1,70 @@ + + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Pushes notification on imaging failure. + * + * @category ImageFail_Ntfy + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class ImageFail_Ntfy extends NtfyExtends +{ + /** + * The name of the event. + * + * @var string + */ + protected $name = 'ImageFail_Ntfy'; + /** + * The description of the event. + * + * @var string + */ + protected $description = 'Triggers when a host fails imaging'; + /** + * Active flag. + * + * @var bool + */ + public $active = true; + /** + * Initialize object + * + * @return void + */ + public function __construct() + { + parent::__construct(); + self::$EventManager + ->register( + 'HOST_IMAGE_FAIL', + $this + ); + } + /** + * Perform action when event met. + * + * @param string $event The event to perform from. + * @param mixed $data The data to send. + * + * @return void + */ + public function onEvent($event, $data) + { + self::$message = 'This host has failed to image'; + self::$shortdesc = 'Failed'; + parent::onEvent($event, $data); + } +} diff --git a/packages/web/lib/plugins/ntfy/events/loginfailure_ntfy.event.php b/packages/web/lib/plugins/ntfy/events/loginfailure_ntfy.event.php new file mode 100644 index 0000000000..0cd8ff1ab3 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/events/loginfailure_ntfy.event.php @@ -0,0 +1,76 @@ + + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Pushes notification on login failure. + * + * @category LogonFailure_Ntfy + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class LoginFailure_Ntfy extends NtfyExtends +{ + /** + * The name of the event. + * + * @var string + */ + protected $name = 'LoginFailure_Ntfy'; + /** + * The description of the event. + * + * @var string + */ + protected $description = 'Triggers when a an invalid login occurs'; + /** + * Active flag. + * + * @var bool + */ + public $active = true; + /** + * Initialize object. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + self::$EventManager + ->register( + 'LoginFail', + $this + ); + } + /** + * Perform action when event met. + * + * @param string $event The event to perform from. + * @param mixed $data The data to send. + * + * @return void + */ + public function onEvent($event, $data) + { + self::$message = 'If you see repeatedly, please check your security'; + self::$shortdesc = sprintf( + '%s %s. %s: %s', + $data['Failure'], + _('failed to login'), + _('Remote address attempting to login'), + filter_input(INPUT_SERVER, 'REMOTE_ADDR') + ); + parent::onEvent($event, $data); + } +} diff --git a/packages/web/lib/plugins/ntfy/events/snapincomplete_ntfy.event.php b/packages/web/lib/plugins/ntfy/events/snapincomplete_ntfy.event.php new file mode 100644 index 0000000000..5316970709 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/events/snapincomplete_ntfy.event.php @@ -0,0 +1,73 @@ + + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Pushes notification on snapin completion. + * + * @category SnapinComplete_Ntfy + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class SnapinComplete_Ntfy extends NtfyExtends +{ + /** + * The name of the event. + * + * @var string + */ + protected $name = 'SnapinComplete_Ntfy'; + /** + * The description of the event. + * + * @var string + */ + protected $description = 'Triggers when a host completes snapin taskings'; + /** + * The active flag. + * + * @var bool + */ + public $active = true; + /** + * Initialize object. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + self::$EventManager + ->register( + 'HOST_SNAPIN_COMPLETE', + $this + ); + } + /** + * Perform action when event met. + * + * @param string $event The event to perform from. + * @param mixed $data The data to send. + * + * @return void + */ + public function onEvent($event, $data) + { + self::$message = sprintf( + 'Host %s has completed snapin tasking.', + $data['Host']->get('name') + ); + self::$shortdesc = 'Snapin(s) Complete'; + parent::onEvent($event, $data); + } +} diff --git a/packages/web/lib/plugins/ntfy/events/snapintaskcomplete_ntfy.event.php b/packages/web/lib/plugins/ntfy/events/snapintaskcomplete_ntfy.event.php new file mode 100644 index 0000000000..7892ef0a07 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/events/snapintaskcomplete_ntfy.event.php @@ -0,0 +1,77 @@ + + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Pushes notification on image completion. + * + * @category ImageComplete_Ntfy + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class SnapinTaskComplete_Ntfy extends NtfyExtends +{ + /** + * Name of the event. + * + * @var string + */ + protected $name = 'SnapinTaskComplete_Ntfy'; + /** + * Description of the event. + * + * @var string + */ + protected $description = 'Triggers when a host completes snapin task'; + /** + * Active flag. + * + * @var bool + */ + public $active = true; + /** + * Initialize object + * + * @return void + */ + public function __construct() + { + parent::__construct(); + self::$EventManager + ->register( + 'HOST_SNAPINTASK_COMPLETE', + $this + ); + } + /** + * Perform action when event met. + * + * @param string $event The event to perform from. + * @param mixed $data The data to send. + * + * @return void + */ + public function onEvent($event, $data) + { + self::$message = sprintf( + 'The snapin has completed installation on %s with status code: %s', + $data['Host']->get('name'), + $data['SnapinTask']->get('return') + ); + self::$shortdesc = sprintf( + '%s completed', + $data['Snapin']->get('name') + ); + parent::onEvent($event, $data); + } +} diff --git a/packages/web/lib/plugins/ntfy/hooks/addntfymenuitem.hook.php b/packages/web/lib/plugins/ntfy/hooks/addntfymenuitem.hook.php new file mode 100644 index 0000000000..04f1d1c03e --- /dev/null +++ b/packages/web/lib/plugins/ntfy/hooks/addntfymenuitem.hook.php @@ -0,0 +1,141 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Adds the ntfy menu item to the menu. + * + * @category AddNtfyMenuItem + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class AddNtfyMenuItem extends Hook +{ + /** + * the name of this hook + * + * @var string + */ + public $name = 'AddNtfyMenuItem'; + /** + * the description of this hook + * + * @var string + */ + public $description = 'Add Menu Item for ntfy'; + /** + * active flag + * + * @var bool + */ + public $active = true; + /** + * node enacted upon + * + * @var string + */ + public $node = 'ntfy'; + /** + * Constructor + * + * @return void + */ + public function __construct() + { + parent::__construct(); + self::$HookManager + ->register( + 'MAIN_MENU_DATA', + array( + $this, + 'menuData' + ) + ) + ->register( + 'SEARCH_PAGES', + array( + $this, + 'addSearch' + ) + ) + ->register( + 'PAGES_WITH_OBJECTS', + array( + $this, + 'addPageWithObject' + ) + ); + } + /** + * Insert ntfy menu item + * + * @param array $arguments to be altered + * + * @return void + */ + public function menuData($arguments) + { + if (!in_array($this->node, (array)self::$pluginsinstalled)) { + return; + } + self::arrayInsertAfter( + 'task', + $arguments['main'], + $this->node, + array( + _('ntfy Accounts'), + 'fa fa-comment' + ) + ); + } + /** + * Inserts the pages with objects element + * + * @param array $arguments the arguments to alter + * + * @return void + */ + public function addPageWithObject($arguments) + { + if (!in_array($this->node, (array)self::$pluginsinstalled)) { + return; + } + if (!isset($arguments['PagesWithObjects'])) { + return; + } + array_push( + $arguments['PagesWithObjects'], + $this->node + ); + } + /** + * Inserts the search + * + * @param array $arguments the arguments to alter + * + * @return void + */ + public function addSearch($arguments) + { + if (!in_array($this->node, (array)self::$pluginsinstalled)) { + return; + } + if (!isset($arguments['searchPages'])) { + return; + } + array_push( + $arguments['searchPages'], + $this->node + ); + } +} \ No newline at end of file diff --git a/packages/web/lib/plugins/ntfy/js/fog.ntfy.js b/packages/web/lib/plugins/ntfy/js/fog.ntfy.js new file mode 100644 index 0000000000..073a8747a9 --- /dev/null +++ b/packages/web/lib/plugins/ntfy/js/fog.ntfy.js @@ -0,0 +1,27 @@ +$(function() { + checkboxToggleSearchListPages(); + validatorOpts = { + submitHandler: submithandlerfunc, + rules: { + serverURL: { + required: true, + minlength: 1, + maxlength: 255 + }, + topicEndpoint: { + required: true, + minlength: 1, + maxlength: 255 + } + } + }; + setupTimeoutElement('#add', 'input[name="serverURL"], input[name="topicEndpoint"]', 1000); + $('.action-boxes').on('submit',function() { + var checked = $('input.toggle-action:checked'); + var ntfyIDArray = new Array(); + for (var i = 0,len = checked.size();i < len;i++) { + ntfyIDArray[ntfyIDArray.length] = checked.eq(i).attr('value'); + } + $('input[name="ntfyIDArray"]').val(ntfyIDArray.join(',')); + }); +}); \ No newline at end of file diff --git a/packages/web/lib/plugins/ntfy/pages/ntfymanagementpage.class.php b/packages/web/lib/plugins/ntfy/pages/ntfymanagementpage.class.php new file mode 100644 index 0000000000..d7549f13be --- /dev/null +++ b/packages/web/lib/plugins/ntfy/pages/ntfymanagementpage.class.php @@ -0,0 +1,273 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/** + * Page presenter for Ntfy plugin + * + * @category NtfyManagementPage + * @package FOGProject + * @author Tony Lam + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class NtfyManagementPage extends FOGPage +{ + /** + * The node name + * + * @var string + */ + public $node = 'ntfy'; + /** + * The initializer for the page. + * + * @param string $name the name of the page + * + * @return void + */ + public function __construct($name = '') + { + $this->name = _('ntfy Management'); + parent::__construct($this->name); + $this->menu = array( + 'list' => sprintf( + self::$foglang['ListAll'], + _('ntfy Accounts') + ), + 'add' => _('Link ntfy Account'), + ); + global $id; + if ($id) { + unset($this->subMenu); + } + $this->headerData = array( + '', + _('Server URL'), + _('Topic Endpoint'), + ); + $this->templates = array( + '', + '${serverURL}', + '${topicEndpoint}', + ); + $this->attributes = array( + array( + 'class' => 'parser-false filter-false', + 'width' => 16 + ), + array(), + array() + ); + /** + * Lambda function to return data either by list or search. + * + * @param object $ntfy the object to use + * + * @return void + */ + self::$returnData = function (&$ntfy) { + $this->data[] = array( + 'serverURL' => $ntfy->serverURL, + 'topicEndpoint' => $ntfy->topicEndpoint, + 'id' => $ntfy->id, + ); + unset($ntfy); + }; + } + /** + * Presents for creating a new link + * + * @return void + */ + public function add() + { + unset( + $this->data, + $this->form, + $this->span, + $this->headerData, + $this->templates, + $this->attributes + ); + + $this->title = _('Add Pub/Sub Topic'); + $this->attributes = array( + array('class' => 'col-xs-4'), + array('class' => 'col-xs-8 form-group'), + ); + $this->templates = array( + '${field}', + '${input}', + ); + $server_value = filter_input( + INPUT_POST, + 'serverURL', + FILTER_DEFAULT, + array( + 'options' => array( + 'default' => 'https://ntfy.sh' + ) + ) + ); + $topic_endpoint = filter_input( + INPUT_POST, + 'topicEndpoint' + ); + $fields = array( + '' => '
' + . '' + . '
', + '' => '
' + . '' + . '
', + '' => '' + ); + array_walk($fields, $this->fieldsToData); + self::$HookManager + ->processEvent( + 'NTFY_ADD', + array( + 'data' => &$this->data, + 'templates' => &$this->templates, + 'attributes' => &$this->attributes, + 'headerData' => &$this->headerData + ) + ); + + echo '
'; + echo '
'; + echo '
'; + echo '

'; + echo $this->title; + echo '

'; + echo '
'; + echo '
'; + echo '
'; + $this->render(12); + echo '
'; + echo '
'; + echo '
'; + echo '
'; + } + /** + * Actually performs the post to ntfy + * Altnerative version of addPost, testing a modified copy from slackmanagement + * + * @return void + */ + public function addPost() + { + try { + $server_url = trim( + filter_input( + INPUT_POST, + 'serverURL' + ) + ); + $topic_endpoint = trim( + filter_input( + INPUT_POST, + 'topicEndpoint' + ) + ); + // $usertype = preg_match( + // '/^[@]/', + // $user + // ); + // $channeltype = preg_match( + // '/^[#]/', + // $user + // ); + // if (!$usertype && !$channeltype) { + // throw new Exception( + // sprintf( + // '%s @ %s # %s!', + // _('Must use an'), + // _('or'), + // _('to signify if this is a user or channel to send to') + // ) + // ); + // } + // if (!$token) { + // throw new Exception( + // _('Please enter an access token') + // ); + // } + $ntfy_topic = self::getClass('Ntfy') + ->set('serverURL', $server_url) + ->set('topicEndpoint', $topic_endpoint); + + $conditions = array( + 'serverURL' => $server_url, + 'topicEndpoint' => $topic_endpoint + ); + $existing = self::getClass('NtfyManager') + ->find($conditions); + + if($existing) { + throw new Exception( + _('Account already linked') + ); + } + if (!$ntfy_topic->save()) { + throw new Exception( + _('Failed to create') + ); + } + + self::getClass( + 'NtfyHandler', + $server_url, + $topic_endpoint + )->pushNote( + '', + 'FOG', + 'Account linked' + ); + $msg = json_encode( + array( + 'msg' => _('Account successfully added!'), + 'title' => _('Link Ntfy Account Success') + ) + ); + } catch (Exception $e) { + $msg = json_encode( + array( + 'error' => $e->getMessage(), + 'title' => _('Link Ntfy Account Fail') + ) + ); + } + unset($Slack); + echo $msg; + exit; + } +} \ No newline at end of file