. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * NotificationTarget Class * * @since 0.84 **/ class NotificationTarget extends CommonDBChild { public $prefix = ''; // From CommonDBChild static public $itemtype = 'Notification'; static public $items_id = 'notifications_id'; public $table = 'glpi_notificationtargets'; public $notification_targets = []; public $notification_targets_labels = []; public $notificationoptions = 0; // Tags which have data in HTML : do not try to clean them public $html_tags = []; // Data from the objet which can be used by the template // See https://forge.indepnet.net/projects/5/wiki/NotificationTemplatesTags public $data = []; public $tag_descriptions = []; // From CommonDBTM public $dohistory = true; //Array to store emails by notification public $target = []; public $entity = ''; //Object which raises the notification event public $obj = null; //Object which is associated with the event public $target_object = []; // array of event name => event label public $events = []; public $options = []; public $raiseevent = ''; private $allow_response = true; private $mode = null; private $event = null; const TAG_LANGUAGE = 'lang'; const TAG_VALUE = 'tag'; const TAG_FOR_ALL_EVENTS = 0; const ANONYMOUS_USER = 0; const GLPI_USER = 1; const EXTERNAL_USER = 2; /** * @param string $entity (default '') * @param string $event (default '') * @param mixed $object (default null) * @param array $options Options **/ function __construct($entity = '', $event = '', $object = null, $options = []) { if ($entity === '') { $this->entity = (isset($_SESSION['glpiactive_entity'])?$_SESSION['glpiactive_entity']:0); } else { $this->entity = $entity; } if ($object) { if ($object instanceof CommonDBTM && isset($object->fields['id'])) { // Reread to avoid slashes issue $object->getFromDB($object->fields['id']); } $this->obj = $object; $this->getObjectItem($event); } $this->raiseevent = $event; $this->options = $options; $this->addNotificationTargets($this->entity); $this->addAdditionalTargets($event); // add new target by plugin unset($this->data); Plugin::doHook('item_add_targets', $this); asort($this->notification_targets); } static function getTable($classname = null) { return parent::getTable(__CLASS__); } /** * Retrieve an item from the database for a specific target * * @param integer $notifications_id notification ID * @param string $type type of the target to retrive * @param integer $ID ID of the target to retrieve * * @since 0.85 * * @return boolean **/ function getFromDBForTarget($notifications_id, $type, $ID) { if ($this->getFromDBByCrit([ $this->getTable() . '.notifications_id' => $notifications_id, $this->getTable() . '.items_id' => $ID, $this->getTable() . '.type' => $type ])) { return true; } return false; } /** * Validate send before doing it (may be overloaded : exemple for private tasks or followups) * * @since 0.84 (new parameter) * * @param string $event notification event * @param array $infos destination of the notification * @param boolean $notify_me notify me on my action ? * ($infos contains users_id to check if the target is me) * (false by default) * @param mixed $emitter if this action is executed by the cron, we can * supply the id of the user (or the email if this * is an anonymous user with no account) who * triggered the event so it can be used instead of * getLoginUserID * * @return boolean **/ function validateSendTo($event, array $infos, $notify_me = false, $emitter = null) { $users_id = Session::getLoginUserID(false); // Override session ID with emitter ID if supplied if (is_int($emitter)) { // We have an ID, we can use it directly $users_id = $emitter; } else if (is_string($emitter)) { // We have an email, we need to check that the users_id is -1 which // is the value used for anonymous user and compare the emails if (isset($infos['users_id']) && $infos['users_id'] == -1 && isset($infos['email']) && $infos['email'] == $emitter ) { $users_id = -1; } } if (!$notify_me) { if (isset($infos['users_id']) // Check login user and not event launch by crontask && ($infos['users_id'] === $users_id)) { return false; } } return true; } /** * @param $event (default '') **/ function getSubjectPrefix($event = '') { $perso_tag = trim(Entity::getUsedConfig('notification_subject_tag', $this->getEntity(), '', '')); if (empty($perso_tag)) { $perso_tag = "GLPI"; } return "[$perso_tag] "; } /** * Get header to add to content **/ function getContentHeader() { return ''; } /** * Get footer to add to content **/ function getContentFooter() { return ''; } /** * @since 0.84 * * @return message id for notification **/ function getMessageID() { return ''; } static function getTypeName($nb = 0) { return _n('Recipient', 'Recipients', $nb); } protected function computeFriendlyName() { if (isset($this->notification_targets_labels[$this->getField("type")] [$this->getField("items_id")])) { return $this->notification_targets_labels[$this->getField("type")] [$this->getField("items_id")]; } return ''; } /** * Get a notificationtarget class by giving the object which raises the event * * @param $item the object which raises the event * @param $event the event which will be used (default '') * @param $options array of options * * @return NotificationTarget|false **/ static function getInstance($item, $event = '', $options = []) { $itemtype = $item->getType(); if ($plug = isPluginItemType($itemtype)) { // plugins case $name = 'Plugin'.$plug['plugin'].'NotificationTarget'.$plug['class']; } else if (strpos($itemtype, "\\" ) != false) { // namespace case $ns_parts = explode("\\", $itemtype); $classname = array_pop($ns_parts); $name = implode("\\", $ns_parts)."\\NotificationTarget$classname"; } else { // simple class (without namespace) $name = "NotificationTarget$itemtype"; } $entity = 0; if (class_exists($name)) { //Entity ID exists in the options array if (isset($options['entities_id'])) { $entity = $options['entities_id']; } else if (method_exists($item, 'getEntityID') && $item->getEntityID() >= 0) { //Item which raises the event contains an entityID $entity = $item->getEntityID(); } return new $name($entity, $event, $item, $options); } return false; } /** * Get a notificationtarget class by giving an itemtype * * @param $itemtype the itemtype of the object which raises the event * @param $event the event which will be used (default '') * @param $options array of options * * @return a notificationtarget class or false **/ static function getInstanceByType($itemtype, $event = '', $options = []) { if (($itemtype) && ($item = getItemForItemtype($itemtype))) { return self::getInstance($item, $event, $options); } return false; } /** * @param $notification Notification object **/ function showForNotification(Notification $notification) { if (!Notification::canView()) { return false; } if ($notification->getField('itemtype') != '') { $notifications_id = $notification->fields['id']; $canedit = $notification->can($notifications_id, UPDATE); if ($canedit) { echo "