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
| // Are we working with any thumbnails?
$thumbnails = '';
if ($this->param('Thumbnails') >= 1) {
// Extract all images from the article.
preg_match_all('/<img [^>]*>/i', $article->introtext . $article->fulltext, $matches); $matches = $matches[0];
// Loop through the thumbnails.
for ($thumbnail = 0; $thumbnail < $this->param('Thumbnails'); $thumbnail++) {
if (!isset($matches[$thumbnail])) break;
// Remove the image from $text
$text = str_replace($matches[$thumbnail], '', $text);
// See if we need to remove styling.
if ($this->param('Thumbnails_Class') != '') {
// Remove style, class, width, border, and height attributes.
$matches[$thumbnail] = preg_replace('/(style|class|width|height|border) ?= ?[\'"][^\'"]*[\'"]/i', '', $matches[$thumbnail]);
// Add CSS.
$matches[$thumbnail] = preg_replace('@/?>$@', 'class="' . $this->param('Thumbnails_Class') . '" />', $matches[$thumbnail]);
}
// Make this thumbnail a link.
$matches[$thumbnail] = "<a href='" . JRoute::_("index.php?option=com_content&id={$article->slug}") . "'>{$matches[$thumbnail]}</a>";
// Add to the list of thumbnails.
$thumbnails .= $matches[$thumbnail];
}
}
if (strlen(strip_tags($text)) > $max_chars) {
if ($this->param('Strip_Formatting') == 1) {
// First, remove all new lines
$text = preg_replace("/\r\n|\r|\n/", "", $text);
// Next, replace <br /> tags with \n
$text = preg_replace("/<BR[^>]*>/i", "\n", $text);
// Replace <p> tags with \n\n
$text = preg_replace("/<P[^>]*>/i", "\n\n", $text);
// Strip all tags
$text = strip_tags($text);
// Truncate
$text = substr($text, 0, $max_chars);
// Pop off the last word in case it got cut in the middle
$text = preg_replace("/[.,!?:;]? [^ ]*$/", "", $text);
// Add ... to the end of the article.
$text = trim($text) . "...";
// Replace \n with <br />
$text = str_replace("\n", "<br />", $text);
} else {
// Truncate
$text = substr($text, 0, $max_chars);
// Pop off the last word in case it got cut in the middle
$text = preg_replace("/[.,!?:;]? [^ ]*$/", "", $text);
// Pop off the last tag, if it got cut in the middle.
$text = preg_replace('/<[^>]*$/', '', $text);
// Add ... to the end of the article if the last character is a letter or a number.
if (preg_match('/\w/', substr($text, -1))) $text = trim($text) . "...";
// Use Tidy to repair any bad XHTML (unclosed tags etc)
$tidy = new tidy(); $text = $tidy->repairString($text, array('show-body-only'=>true, 'output-xhtml'=>true), 'utf8');
}
// Add a "read more" link.
$article->readmore = true;
}
// If we have thumbnails, add it to $text.
$text = $thumbnails . $text;
// If Developer Mode is turned on, add some stuff.
if ($this->param('Developer_Mode') == 1) {
$text = ''
. '<div style="height:150px;width:100%;overflow:auto;">'
. '<b>Developer information:</b><br /><pre>'
. 'Developers: uncomment the next line in the code to display $GLOBALS. If you see this message and do not know what it means, you should turn Developer_Mode off in the Auto Read More configuration.'
. (htmlspecialchars(print_r($GLOBALS, 1))) // by default, this is commented out for security. Only uncomment it if you know what you are doing.
. '</pre></div>'
. $text
;
}
// Set $article->text.
$article->text = $text;
}
function param($name) {
static $plugin, $pluginParams;
if (!isset($plugin)) {
$plugin =& JPluginHelper::getPlugin('content', 'AutoReadMore');
$pluginParams = new JParameter( $plugin->params );
}
return $pluginParams->get($name);
}
} |