Completed
Pull Request — development (#791)
by Nick
03:57
created

edithelper.inc.php ➔ html2plaintext()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 3
dl 0
loc 52
rs 9.0472
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/****************************************************************************
3
 * common functions for text editors
4
 *
5
 ****************************************************************************/
6
7
// used in both lib1 and lib2 code
8
9
require_once __DIR__ . '/smiley.inc.php';
10
11
12
/**
13
 * Do all the conversions needed to process HTML or plain text editor input,
14
 * for either storing it into the database or (when swiching modes)
15
 * re-displaying it in another editor mode.
16
 *
17
 * oldDescMode is the mode in which the editor was running which output the $text,
18
 * or 0 if the text came from the database with `htm_text` = 0.
19
 *
20
 * descMode    is == descMode if the user hit the editor's "save" button,
21
 * or the new mode if the user hit another mode button
22
 * @param mixed $oldDescMode
23
 * @param mixed $descMode
24
 * @param mixed $text
25
 * @param & $representText
26
 */
27
28
/**
29
 * @param $oldDescMode
30
 * @param $descMode
31
 * @param $text
32
 * @return mixed|string
33
 */
34
function processEditorInput($oldDescMode, $descMode, $text, &$representText)
35
{
36
    global $opt, $smiley;
37
38
    if ($descMode != 1) {
39
        if ($oldDescMode == 1) {
40
            // mode switch from plain text to HTML editor => convert HTML special chars
41
            $text = nl2br(htmlspecialchars($text));
42
            // .. and smilies
43
            $text = ' ' . $text . ' ';   // see Redmine #1103
44
            $text = str_replace($smiley['text'], $smiley['spaced_image'], $text);
45
            if (substr($text, 0, 1) == ' ') {
46
                $text = substr($text, 1);
47
            }
48
            if (substr($text, -1) == ' ') {
49
                $text = substr($text, 0, strlen($text) - 1);
50
            }
51
            $representText = $text;
52
        } else {
53
            // save HTML input => verify / tidy / filter;
54
            // also implemented in okapi/services/logs/submit.php
55
            $purifier = new OcHTMLPurifier($opt);
56
            $text = $purifier->purify($text);
57
            $representText = $text;
58
        }
59
    } else {
60
        if ($oldDescMode == 1) {
61
            // keep plain text for re-presenting to the user
62
            $representText = $text;
63
            // convert to HTML for storing to database
64
            // also implemented in okapi/services/logs/submit.php
65
            $text = nl2br(htmlspecialchars($text, ENT_COMPAT, 'UTF-8'));
66
            $text = str_replace('  ', '&nbsp; ', $text);   // can produce new '  ' ('&nbsp; ' + ' ')
67
            $text = str_replace('  ', '&nbsp; ', $text);
68
        } else {
69
            // mode switch from HTML editor to plain text, or decode HTML-encoded plain text
70
            $representText = html2plaintext($text, $oldDescMode = 0, 0);
71
        }
72
    }
73
74
    return $text;
75
}
76
77
78
// $texthtml0 is set if the text is from cache_desc.desc or cache_logs.text
79
// and text_html is 0, i.e. the text was edited in the "text" editor mode.
80
//
81
// If $wrap is > 0, longer lines will be wrapped to new lines.
82
83
/**
84
 * @param $text
85
 * @param $texthtml0
86
 * @param $wrap
87
 *
88
 * @return mixed|string
89
 */
90
function html2plaintext($text, $texthtml0, $wrap)
0 ignored issues
show
Unused Code introduced by
The parameter $wrap is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
{
92
    global $smiley;
93
94
    if ($texthtml0) {
95
        $text = str_replace(
96
            [
97
                '<p>',
98
                "\n",
99
                "\r",
100
            ],
101
            '',
102
            $text
103
        );
104
        $text = str_replace(
105
            [
106
                '<br />',
107
                '</p>',
108
            ],
109
            "\n",
110
            $text
111
        );
112
        $text = html_entity_decode($text, ENT_COMPAT, 'UTF-8');
113
    } else {
114
        // convert smileys ...
115
        $countSmileyImage = count($smiley['image']);
116
        for ($n = 0; $n < $countSmileyImage; $n++) {
117
            $text = mb_ereg_replace(
118
                '<img [^>]*?src=[^>]+?' . str_replace('.', '\.', $smiley['file'][$n]) . '[^>]+?>',
119
                '[s![' . $smiley['text'][$n] . ']!s]',
120
                $text
121
            );
122
            // the [s[ ]s] is needed to protect the spaces around the smileys
123
        }
124
125
        // REDMINE-1249: Missing log text in mail notification
126
        // simpler solution that converts html to text as the previous class html2text emptied the text completely
127
        // implementation for line wrap, url's and probably more is missing
128
        $text = preg_replace( "/\n\s+/", "\n", rtrim(html_entity_decode(strip_tags($text))));
129
130
        $text = str_replace(
131
            [
132
                '[s![',
133
                ']!s]',
134
            ],
135
            '',
136
            $text
137
        );
138
    }
139
140
    return $text;
141
}
142
143
144
/**
145
 * @return string
146
 */
147
function editorJsPath()
148
{
149
    return 'resource2/ocstyle/js/editor.js?ft=' . filemtime('resource2/ocstyle/js/editor.js');
150
}
151