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