Created
October 22, 2015 18:53
-
-
Save Sama34/12eb28c03adeec8cbc1c to your computer and use it in GitHub Desktop.
OUGC Get Preview Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************************************************************** | |
* | |
* OUGC Get Preview Plugin | |
* Author: Omar Gonzalez | |
* Copyright: © 2015 Omar Gonzalez | |
* | |
* Website: http://omarg.me | |
* | |
* Shorts a message to look like a preview. | |
* | |
*************************************************************************** | |
**************************************************************************** | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
****************************************************************************/ | |
// Die if IN_MYBB is not defined, for security reasons. | |
defined('IN_MYBB') or die('Direct initialization of this file is not allowed.'); | |
// Plugin API | |
function ougc_getpreview_info() | |
{ | |
return array( | |
'name' => 'OUGC Get Preview', | |
'description' => 'Shorts a message to look like a preview.', | |
'website' => 'http://omarg.me', | |
'author' => 'Omar G.', | |
'authorsite' => 'http://omarg.me', | |
'version' => '1.0', | |
'versioncode' => 1000, | |
'compatibility' => '18*', | |
'codename' => 'ougc_getpreview' | |
); | |
} | |
/** | |
* @ https://gist.github.com/Sama34/10012654/edit | |
**/ | |
function ougc_getpreview($message, $maxlen=100, $stripquotes=true, $stripmycode=true) | |
{ | |
// Attempt to remove quotes, skip if going to strip MyCode | |
if($stripquotes && !$stripmycode) | |
{ | |
$message = preg_replace(array( | |
'#\[quote=([\"\']|"|)(.*?)(?:\\1)(.*?)(?:[\"\']|")?\](.*?)\[/quote\](\r\n?|\n?)#esi', | |
'#\[quote\](.*?)\[\/quote\](\r\n?|\n?)#si', | |
'#\[quote\]#si', | |
'#\[\/quote\]#si' | |
), '', $message); | |
} | |
// Attempt to remove any MyCode | |
if($stripmycode) | |
{ | |
global $parser; | |
if(!is_object($parser)) | |
{ | |
require_once MYBB_ROOT.'inc/class_parser.php'; | |
$parser = new postParser; | |
} | |
$message = $parser->parse_message($message, array( | |
'allow_html' => 0, | |
'allow_mycode' => 1, | |
'allow_smilies' => 0, | |
'allow_imgcode' => 1, | |
'filter_badwords' => 1, | |
'nl2br' => 0 | |
)); | |
// before stripping tags, try converting some into spaces | |
$message = preg_replace(array( | |
'~\<(?:img|hr).*?/\>~si', | |
'~\<li\>(.*?)\</li\>~si' | |
), array(' ', "\n* $1"), $message); | |
$message = unhtmlentities(strip_tags($message)); | |
} | |
// convert \xA0 to spaces (reverse ) | |
$message = trim(preg_replace(array('~ {2,}~', "~\n{2,}~"), array(' ', "\n"), strtr($message, array("\xA0" => ' ', "\r" => '', "\t" => ' ')))); | |
// newline fix for browsers which don't support them | |
$message = preg_replace("~ ?\n ?~", " \n", $message); | |
// Shorten the message if too long | |
if(my_strlen($message) > $maxlen) | |
{ | |
$message = my_substr($message, 0, $maxlen-1).'...'; | |
} | |
return htmlspecialchars_uni($message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment