Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
sg_mail
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TYPO3
sg_mail
Commits
34f2f125
Commit
34f2f125
authored
5 years ago
by
Torsten Oppermann
Browse files
Options
Downloads
Patches
Plain Diff
[TASK] Code cleanups
parent
ff427916
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Classes/Service/MailTemplateService.php
+2
-1
2 additions, 1 deletion
Classes/Service/MailTemplateService.php
Classes/Service/PlaintextService.php
+123
-128
123 additions, 128 deletions
Classes/Service/PlaintextService.php
with
125 additions
and
129 deletions
Classes/Service/MailTemplateService.php
+
2
−
1
View file @
34f2f125
...
...
@@ -44,7 +44,6 @@ use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use
TYPO3\CMS\Extbase\Utility\LocalizationUtility
;
use
TYPO3\CMS\Fluid\View\StandaloneView
;
use
TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
;
use
TYPO3\CMS\Frontend\Page\PageRepository
;
/**
* MailTemplateService
...
...
@@ -799,6 +798,8 @@ class MailTemplateService {
}
return
$success
;
}
return
NULL
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
Classes/Service/PlaintextService.php
+
123
−
128
View file @
34f2f125
...
...
@@ -24,144 +24,139 @@
*/
declare
(
strict_types
=
1
);
namespace
SGalinski\SgMail\Service
;
/**
* Class PlaintextService
*/
class
PlaintextService
{
class
PlaintextService
{
/**
* Function makePlain() removes html tags and add linebreaks
* Easy generate a plain email bodytext from a html bodytext
*
* @param string $content HTML Mail bodytext
* @return string $content
*/
public
function
makePlain
(
$content
)
{
$content
=
$this
->
removeInvisibleElements
(
$content
);
$content
=
$this
->
removeLinebreaksAndTabs
(
$content
);
$content
=
$this
->
addLineBreaks
(
$content
);
$content
=
$this
->
addSpaceToTableCells
(
$content
);
$content
=
$this
->
extractLinkForPlainTextContent
(
$content
);
$content
=
$this
->
removeTags
(
$content
);
$array
=
[
'<br >'
,
'<br>'
,
'<br/>'
,
'<br />'
];
return
trim
(
str_replace
(
$array
,
PHP_EOL
,
$content
));
}
/**
* Function makePlain() removes html tags and add linebreaks
* Easy generate a plain email bodytext from a html bodytext
*
* @param array|string $content HTML Mail bodytext
* @return string $content
*/
public
function
makePlain
(
$content
):
string
{
$content
=
$this
->
removeInvisibleElements
(
$content
);
$content
=
$this
->
removeLinebreaksAndTabs
(
$content
);
$content
=
$this
->
addLineBreaks
(
$content
);
$content
=
$this
->
addSpaceToTableCells
(
$content
);
$content
=
$this
->
extractLinkForPlainTextContent
(
$content
);
$content
=
$this
->
removeTags
(
$content
);
$array
=
[
'<br >'
,
'<br>'
,
'<br/>'
,
'<br />'
];
return
trim
(
str_replace
(
$array
,
PHP_EOL
,
$content
));
}
/**
* Remove all invisible elements
*
* @param string $content
* @return string
*/
protected
function
removeInvisibleElements
(
$content
)
{
$content
=
preg_replace
(
[
'/<head[^>]*?>.*?<\/head>/siu'
,
'/<style[^>]*?>.*?<\/style>/siu'
,
'/<script[^>]*?>.*?<\/script>/siu'
,
'/<object[^>]*?>.*?<\/object>/siu'
,
'/<embed[^>]*?>.*?<\/embed>/siu'
,
'/<applet[^>]*?>.*?<\/applet>/siu'
,
'/<noframes[^>]*?>.*?<\/noframes>/siu'
,
'/<noscript[^>]*?>.*?<\/noscript>/siu'
,
'/<noembed[^>]*?>.*?<\/noembed>/siu'
,
],
[
''
,
''
,
''
,
''
,
''
,
''
,
''
,
''
,
''
],
$content
);
return
$content
;
}
/**
* Remove all invisible elements
*
* @param string $content
* @return string
*/
protected
function
removeInvisibleElements
(
$content
):
string
{
$content
=
preg_replace
(
[
'/<head[^>]*?>.*?<\/head>/siu'
,
'/<style[^>]*?>.*?<\/style>/siu'
,
'/<script[^>]*?>.*?<\/script>/siu'
,
'/<object[^>]*?>.*?<\/object>/siu'
,
'/<embed[^>]*?>.*?<\/embed>/siu'
,
'/<applet[^>]*?>.*?<\/applet>/siu'
,
'/<noframes[^>]*?>.*?<\/noframes>/siu'
,
'/<noscript[^>]*?>.*?<\/noscript>/siu'
,
'/<noembed[^>]*?>.*?<\/noembed>/siu'
,
],
[
''
,
''
,
''
,
''
,
''
,
''
,
''
,
''
,
''
],
$content
);
return
$content
;
}
/**
* Remove linebreaks and tabs
*
* @param string $content
* @return string
*/
protected
function
removeLinebreaksAndTabs
(
$content
)
{
$content
=
trim
(
str_replace
([
"
\n
"
,
"
\r
"
,
"
\t
"
],
''
,
$content
));
return
$content
;
}
/**
* Remove linebreaks and tabs
*
* @param string $content
* @return string
*/
protected
function
removeLinebreaksAndTabs
(
$content
):
string
{
$content
=
trim
(
str_replace
([
"
\n
"
,
"
\r
"
,
"
\t
"
],
''
,
$content
));
return
$content
;
}
/**
* add linebreaks on some parts (</p> => </p><br>)
*
* @param string $content
* @return array
*/
protected
function
addLineBreaks
(
$content
)
{
$tags2LineBreaks
=
[
'</p>'
,
'</tr>'
,
'<ul>'
,
'</li>'
,
'</h1>'
,
'</h2>'
,
'</h3>'
,
'</h4>'
,
'</h5>'
,
'</h6>'
,
'</div>'
,
'</legend>'
,
'</fieldset>'
,
'</dd>'
,
'</dt>'
];
return
str_replace
(
$tags2LineBreaks
,
'</p><br>'
,
$content
);
}
/**
* add linebreaks on some parts (</p> => </p><br>)
*
* @param string $content
* @return array
*/
protected
function
addLineBreaks
(
$content
):
array
{
$tags2LineBreaks
=
[
'</p>'
,
'</tr>'
,
'<ul>'
,
'</li>'
,
'</h1>'
,
'</h2>'
,
'</h3>'
,
'</h4>'
,
'</h5>'
,
'</h6>'
,
'</div>'
,
'</legend>'
,
'</fieldset>'
,
'</dd>'
,
'</dt>'
];
return
str_replace
(
$tags2LineBreaks
,
'</p><br>'
,
$content
);
}
/**
* Add a space character to a table cell
*
* @param string $content
* @return string
*/
protected
function
addSpaceToTableCells
(
$content
)
{
return
str_replace
([
'</td>'
,
'</th>'
],
'</td> '
,
$content
);
}
/**
* Add a space character to a table cell
*
* @param string $content
* @return string
*/
protected
function
addSpaceToTableCells
(
$content
):
string
{
return
str_replace
([
'</td>'
,
'</th>'
],
'</td> '
,
$content
);
}
/**
* Remove all tags but keep br and address
*
* @param string $content
* @return string
*/
protected
function
removeTags
(
$content
)
{
return
strip_tags
(
$content
,
'<br><address>'
);
}
/**
* Remove all tags but keep br and address
*
* @param string $content
* @return string
*/
protected
function
removeTags
(
$content
):
string
{
return
strip_tags
(
$content
,
'<br><address>'
);
}
/**
* Extract uri from href attributes and decode it
*
* replace links
* <a href="xyz">LINK</a>
* ->
* LINK [xyz]
*
* @param string $content
* @return string
*/
protected
function
extractLinkForPlainTextContent
(
$content
)
{
$pattern
=
'/<a[^>]+href\s*=\s*["\']([^"\']+)["\'][^>]*>(.*?)<\/a>/misu'
;
return
preg_replace_callback
(
$pattern
,
function
(
$matches
)
{
return
$matches
[
2
]
.
' ['
.
htmlspecialchars_decode
(
$matches
[
1
])
.
']'
;
},
$content
);
}
/**
* Extract uri from href attributes and decode it
*
* replace links
* <a href="xyz">LINK</a>
* ->
* LINK [xyz]
*
* @param string $content
* @return string
*/
protected
function
extractLinkForPlainTextContent
(
$content
):
string
{
$pattern
=
'/<a[^>]+href\s*=\s*["\']([^"\']+)["\'][^>]*>(.*?)<\/a>/misu'
;
return
preg_replace_callback
(
$pattern
,
function
(
$matches
)
{
return
$matches
[
2
]
.
' ['
.
htmlspecialchars_decode
(
$matches
[
1
])
.
']'
;
},
$content
);
}
}
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