Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Imldeployment
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
Imldeployment
Commits
43b5d42e
Commit
43b5d42e
authored
10 months ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
messenger class: php8 only; added variable types
parent
3c9e168a
No related branches found
No related tags found
1 merge request
!66
php8 only; added variable types; short array syntax; remove glyphicons
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
public_html/deployment/classes/messenger.class.php
+37
-26
37 additions, 26 deletions
public_html/deployment/classes/messenger.class.php
with
37 additions
and
26 deletions
public_html/deployment/classes/messenger.class.php
+
37
−
26
View file @
43b5d42e
...
@@ -4,53 +4,59 @@
...
@@ -4,53 +4,59 @@
* send messenger notifications
* send messenger notifications
*
*
* @author hahn
* @author hahn
*
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types
*/
*/
class
messenger
{
class
messenger
{
/**
/**
* config array for messengers
* config array for messengers
* @var
type
* @var
array
*/
*/
protected
$_aCfg
=
array
()
;
protected
array
$_aCfg
=
[]
;
/**
/**
* content of messagetext to send
* content of messagetext to send
* @var string
* @var string
*/
*/
protected
$_sMessage
=
''
;
protected
string
$_sMessage
=
''
;
/**
/**
* Constructor
*
*
* @example
* @example
* $oMessenger = new messenger(
array(
* $oMessenger = new messenger(
[
* 'slack'=>
array(
* 'slack'=>
[
* 'incomingurl'=>[WebHook url],
* 'incomingurl'=>[WebHook url],
* 'user'=>[visible username in slack],
* 'user'=>[visible username in slack],
* 'icon'=>[Slack Icon], // hm, does not seem to work
* 'icon'=>[Slack Icon], // hm, does not seem to work
*
)
,
*
]
,
* 'email'=>
array(
* 'email'=>
[
* 'from'=>[senders e-mail]
* 'from'=>[senders e-mail]
* 'to'=>[email-address(es)] // multiple emails must be delimited with ";"
* 'to'=>[email-address(es)] // multiple emails must be delimited with ";"
*
)
*
]
*
)
);
*
]
);
* @param array $aCfg config array for notification targets
* @param array $aCfg config array for notification targets
* @return boolean
*/
*/
public
function
__construct
(
$aCfg
)
{
public
function
__construct
(
array
$aCfg
)
{
$this
->
_aCfg
=
$aCfg
;
$this
->
_aCfg
=
$aCfg
;
return
true
;
}
}
/**
/**
* send an email if _aCfg['email']['to'] exists
* Send an email if _aCfg['email']['to'] exists
* @return bool
*/
*/
private
function
_sendEmail
(){
private
function
_sendEmail
():
bool
{
if
(
isset
(
$this
->
_aCfg
[
'email'
][
'to'
])
&&
$this
->
_aCfg
[
'email'
][
'to'
])
{
if
(
isset
(
$this
->
_aCfg
[
'email'
][
'to'
])
&&
$this
->
_aCfg
[
'email'
][
'to'
])
{
preg_match
(
'/^(.*)\n/'
,
$this
->
_sMessage
,
$aTmp
);
preg_match
(
'/^(.*)\n/'
,
$this
->
_sMessage
,
$aTmp
);
$sSubject
=
$aTmp
[
0
];
$sSubject
=
$aTmp
[
0
];
return
mail
(
return
mail
(
$this
->
_aCfg
[
'email'
][
'to'
],
$this
->
_aCfg
[
'email'
][
'to'
],
$sSubject
,
$this
->
_sMessage
,
$sSubject
,
$this
->
_sMessage
,
"From: "
.
$this
->
_aCfg
[
'email'
][
'from'
]
.
"
\r\n
"
.
"From: "
.
$this
->
_aCfg
[
'email'
][
'from'
]
.
"
\r\n
"
.
"Reply-To: "
.
$this
->
_aCfg
[
'email'
][
'from'
]
.
"
\r\n
"
"Reply-To: "
.
$this
->
_aCfg
[
'email'
][
'from'
]
.
"
\r\n
"
);
);
...
@@ -59,15 +65,19 @@ class messenger {
...
@@ -59,15 +65,19 @@ class messenger {
}
}
/**
/**
* send a message to slack if _aCfg['slack']['incomingurl'] exists
* Send a message to slack if _aCfg['slack']['incomingurl'] exists
* @return bool|string
*/
*/
private
function
_sendToSlack
(){
private
function
_sendToSlack
():
bool
|
string
{
if
(
isset
(
$this
->
_aCfg
[
'slack'
][
'incomingurl'
])
&&
$this
->
_aCfg
[
'slack'
][
'incomingurl'
])
{
if
(
isset
(
$this
->
_aCfg
[
'slack'
][
'incomingurl'
])
&&
$this
->
_aCfg
[
'slack'
][
'incomingurl'
])
{
require_once
(
__DIR__
.
'/../../vendor/shooker/shooker.php'
);
require_once
(
__DIR__
.
'/../../vendor/shooker/shooker.php'
);
$shkr
=
new
Shooker
();
$shkr
=
new
Shooker
();
$shkr
->
setupIncoming
(
$this
->
_aCfg
[
'slack'
][
'incomingurl'
]);
$shkr
->
setupIncoming
(
$this
->
_aCfg
[
'slack'
][
'incomingurl'
]);
$sUser
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'user'
])
?
$this
->
_aCfg
[
'slack'
][
'user'
]
:
false
);
$sUser
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'user'
])
?
$this
->
_aCfg
[
'slack'
][
'user'
]
:
false
);
$sIcon
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'icon'
])
?
$this
->
_aCfg
[
'slack'
][
'icon'
]
:
false
);
$sIcon
=
(
isset
(
$this
->
_aCfg
[
'slack'
][
'icon'
])
?
$this
->
_aCfg
[
'slack'
][
'icon'
]
:
false
);
// returns bool or string with response from Slack API
return
$shkr
->
sendMessage
(
$this
->
_sMessage
,
$sUser
,
$sIcon
);
return
$shkr
->
sendMessage
(
$this
->
_sMessage
,
$sUser
,
$sIcon
);
}
}
return
false
;
return
false
;
...
@@ -77,13 +87,14 @@ class messenger {
...
@@ -77,13 +87,14 @@ class messenger {
/**
/**
* send a message to all targets
* send a message to all targets
* @param string $sMessage
* @param string $sMessage
* @return void
*/
*/
public
function
sendMessage
(
$sMessage
)
{
public
function
sendMessage
(
string
$sMessage
):
void
{
$this
->
_sMessage
=
$sMessage
;
$this
->
_sMessage
=
$sMessage
;
// echo '<pre>'.print_r($this->_aCfg, 1).'</pre>'.$sMessage.'<br>';
// echo '<pre>'.print_r($this->_aCfg, 1).'</pre>'.$sMessage.'<br>';
$this
->
_sendEmail
();
$this
->
_sendEmail
();
$this
->
_sendToSlack
();
$this
->
_sendToSlack
();
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment