-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
RichText.php
168 lines (141 loc) · 5.66 KB
/
RichText.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\widgets;
use Yii;
use yii\helpers\Html;
use humhub\models\UrlOembed;
use humhub\libs\ParameterEvent;
/**
* RichText dis
*
* @author luke
*/
class RichText extends \humhub\components\Widget
{
/**
* @var string text to display
*/
public $text = "";
/**
* @var boolean
*/
public $encode = true;
/**
* @var boolean
*/
public $minimal = false;
/**
* @var boolean edit mode
*/
public $edit = false;
/**
* @var \humhub\components\ActiveRecord this richtext belongs to
*/
public $record = null;
/**
* @var int
*/
public $maxLength = 0;
/**
* @event \humhub\modules\search\events\ParameterEvent with parameter 'output'
*/
const EVENT_BEFORE_OUTPUT = 'beforeOutput';
public function run()
{
if ($this->encode) {
$this->text = Html::encode($this->text);
}
if (!$this->minimal) {
$maxOembedCount = 3; // Maximum OEmbeds
$oembedCount = 0; // OEmbeds used
$that = $this;
$this->text = preg_replace_callback('/(https?:\/\/.*?)(\s|$)/i', function ($match) use (&$oembedCount, &$maxOembedCount, &$that) {
if ($that->edit) {
return Html::a($match[0], Html::decode($match[0]), array('target' => '_blank'));
}
// Try use oembed
if ($maxOembedCount > $oembedCount) {
$oembed = UrlOembed::GetOembed($match[0]);
if ($oembed) {
$oembedCount++;
return $oembed;
}
}
return Html::a($match[1], Html::decode($match[1]), array('target' => '_blank')) . $match[2];
}, $this->text);
}
// get user and space details from guids
$this->text = self::translateMentioning($this->text, ($this->minimal) ? false : true);
// create image tag for emojis
$this->text = self::translateEmojis($this->text, ($this->minimal) ? false : true);
if ($this->maxLength != 0) {
$this->text = \humhub\libs\Helpers::truncateText($this->text, $this->maxLength);
}
$output = nl2br($this->text);
$this->trigger(self::EVENT_BEFORE_OUTPUT, new ParameterEvent(['output' => &$output]));
return $output;
}
/**
* Replace emojis from text to img tag
*
* @param string $text Contains the complete message
* @param string $show show smilies or remove it (for activities and notifications)
*/
public static function translateEmojis($text, $show = true)
{
$emojis = array(
"Relaxed", "Yum", "Relieved", "Hearteyes", "Cool", "Smirk",
"KissingClosedEyes", "StuckOutTongue", "StuckOutTongueWinkingEye", "StuckOutTongueClosedEyes", "Disappointed", "Frown",
"ColdSweat", "TiredFace", "Grin", "Sob", "Gasp", "Gasp2",
"Laughing", "Joy", "Sweet", "Satisfied", "Innocent", "Wink",
"Ambivalent", "Expressionless", "Sad", "Slant", "Worried", "Kissing",
"KissingHeart", "Angry", "Naughty", "Furious", "Cry", "OpenMouth",
"Fearful", "Confused", "Weary", "Scream", "Astonished", "Flushed",
"Sleeping", "NoMouth", "Mask", "Worried", "Smile", "Muscle",
"Facepunch", "ThumbsUp", "ThumbsDown", "Beers", "Cocktail", "Burger",
"PoultryLeg", "Party", "Cake", "Sun", "Fire", "Heart"
);
return preg_replace_callback('@;(\w*?);@', function($hit) use(&$show, &$emojis) {
if (in_array($hit[1], $emojis)) {
if ($show) {
return Html::img(Yii::getAlias("@web/img/emoji/" . $hit[1] . ".svg"), array('data-emoji-name' => $hit[0], 'class' => 'atwho-emoji', 'width' => '18', 'height' => '18', 'alt' => $hit[1]));
}
return '';
}
return $hit[0];
}, $text);
}
/**
* Translate guids from users to username
*
* @param strint $text Contains the complete message
* @param boolean $buildAnchors Wrap the username with a link to the profile, if it's true
*/
public static function translateMentioning($text, $buildAnchors = true)
{
return preg_replace_callback('@\@\-([us])([\w\-]*?)($|\s|\.|")@', function($hit) use(&$buildAnchors) {
if ($hit[1] == 'u') {
$user = \humhub\modules\user\models\User::findOne(['guid' => $hit[2]]);
if ($user !== null) {
if ($buildAnchors) {
return ' <span contenteditable="false"><a href="' . $user->getUrl() . '" target="_self" class="atwho-user" data-user-guid="@-u' . $user->guid . '">@' . Html::encode($user->getDisplayName()) . '</a></span>' . $hit[3];
}
return " @" . Html::encode($user->getDisplayName()) . $hit[3];
}
} elseif ($hit[1] == 's') {
$space = \humhub\modules\space\models\Space::findOne(['guid' => $hit[2]]);
if ($space !== null) {
if ($buildAnchors) {
return ' <span contenteditable="false"><a href="' . $space->getUrl() . '" target="_self" class="atwho-user" data-user-guid="@-s' . $space->guid . '">@' . Html::encode($space->name) . '</a></span>' . $hit[3];
}
return " @" . Html::encode($space->name) . $hit[3];
}
}
return $hit[0];
}, $text);
}
}