Skip to content

Commit

Permalink
Messenger: detect emoji in text and display an error message (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-ichk authored May 13, 2024
1 parent c4f00b6 commit 139b6c5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ v27.0.00
Markbook: removed non-numeric entries from cumulative totals
Messenger: added search form and school year navigator to Manage Groups page for quicker group browsing
Messenger: added an index to the Message Target table to speed up the recipients query
Messenger: added the ability to remove emojis from the message body
Reports: updated report writing to also show left class enrolments with the Show Left Students tool
Reports: added uploadable Image as a field type in Manage Criteria Types
Reports: added a check for the correct .twig.html extension in Manage Assets
Expand Down
15 changes: 15 additions & 0 deletions modules/Messenger/messenger_manage_editProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

require_once '../../gibbon.php';

//Module includes
include './moduleFunctions.php';

$gibbonMessengerID = $_POST['gibbonMessengerID'] ?? '';
$sendTestEmail = $_POST['sendTestEmail'] ?? '';
$search = $_GET['search'] ?? '';
Expand Down Expand Up @@ -112,6 +115,14 @@
exit;
}

// Check for any emojis in the message
$containsEmoji = hasEmojis($data['body']);

// Remove any emojis from the message
if($containsEmoji) {
$data['body'] = removeEmoji($data['body']);
}

// Write to database
$updated = $messengerGateway->update($gibbonMessengerID, $data);
if (!$updated) {
Expand Down Expand Up @@ -144,6 +155,10 @@
exit;
}

if($containsEmoji) {
$URLSend .= '&return=warning3';
}

header("Location: {$URLSend}");
exit;
} elseif ($saveMode == 'Preview' && $data['messageWall'] == 'Y') {
Expand Down
15 changes: 15 additions & 0 deletions modules/Messenger/messenger_postProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

$_POST = $container->get(Validator::class)->sanitize($_POST, ['body' => 'HTML']);

//Module includes
include './moduleFunctions.php';

$address = $_POST['address'] ?? '';
$URL = $session->get('absoluteURL') . '/index.php?q=/modules/Messenger/messenger_manage_post.php&sidebar=true';
$URLSend = $session->get('absoluteURL') . '/index.php?q=/modules/Messenger/messenger_send.php&sidebar=true';
Expand Down Expand Up @@ -81,6 +84,14 @@
exit;
}

// Check for any emojis in the message
$containsEmoji = hasEmojis($data['body']);

// Remove any emojis from the message
if($containsEmoji) {
$data['body'] = removeEmoji($data['body']);
}

// Insert the message and get the ID
$gibbonMessengerID = $messengerGateway->insert($data);

Expand All @@ -106,6 +117,10 @@
exit;
}

if($containsEmoji) {
$URLSend .= '&return=warning3';
}

header("Location: {$URLSend}");
exit;
} elseif ($saveMode == 'Preview' && $data['messageWall'] == 'Y') {
Expand Down
11 changes: 11 additions & 0 deletions modules/Messenger/messenger_postQuickWallProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
$subject = $_POST['subject'] ?? '';
$body = stripslashes($_POST['body'] ?? '');

//check for any emojis in the message
$containsEmoji = hasEmojis($body);

//remove any emojis from the message
if($containsEmoji) {
$body = removeEmoji($body);
}

// Turn copy-pasted div breaks into paragraph breaks
$body = str_ireplace(['<div ', '<div>', '</div>'], ['<p ', '<p>', '</p>'], $body);

Expand Down Expand Up @@ -111,6 +119,9 @@
if ($partialFail == true) {
$URL .= '&return=warning1';
header("Location: {$URL}");
}else if($containsEmoji) {
$URL .= '&return=warning3';
header("Location: {$URL}");
} else {
//Success 0
$session->set('pageLoads', null);
Expand Down
14 changes: 14 additions & 0 deletions modules/Messenger/moduleFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ function getMessages($guid, $connection2, $mode = '', $date = '')
global $container;
return $container->get(MessengerGateway::class)->getMessages($mode, $date);
}

function hasEmojis($string) {

$regexEmoticons = '/[\x{1F300}-\x{1F5FF}\x{1F600}-\x{1F64F}\x{1F680}-\x{1F6FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}\x{1F900}-\x{1F9FF}\x{1F1E0}-\x{1F1FF}]/u';

return preg_match($regexEmoticons, $string) ? true : false;
}

function removeEmoji($text) {

$regexEmoticons = '/[\x{1F300}-\x{1F5FF}\x{1F600}-\x{1F64F}\x{1F680}-\x{1F6FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}\x{1F900}-\x{1F9FF}\x{1F1E0}-\x{1F1FF}]/u';

return preg_replace($regexEmoticons, '', $text);
}
1 change: 1 addition & 0 deletions src/View/Components/ReturnMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct()
'warning0' => __('Your optional extra data failed to save.'),
'warning1' => __('Your request was successful, but some data was not properly saved.'),
'warning2' => __('Your request was successful, but some data was not properly deleted.'),
'warning3' => __('Your request was successful but the emojis in your message have been removed'),
]);
}

Expand Down

0 comments on commit 139b6c5

Please sign in to comment.