summaryrefslogtreecommitdiff
path: root/smartyplugins/outputfilter.groupslayout.php
blob: f3f747e223fe17c56fd04e5d7b7d9087d3383b4f (plain)
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
<?php
namespace Bitweaver\Plugins;

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Handler
 */
function group_replace_url_hander($matched) {
	$url = $matched[2];
	// Make sure it is not an off site url
	if( substr($url, 0, strlen(BIT_ROOT_URL)) == BIT_ROOT_URL ||
		substr($url, 0, strlen(BIT_ROOT_URI)) == BIT_ROOT_URI ) {
		// Make sure it is decoded
		$url = urldecode($url);
		// Figure out which way to express it
		if( strstr($url, '?') ) {
			$url = $url.'&group_layout_id='.$_REQUEST['group_layout_id'];
		}
		else {
			$url = $url.'?group_layout_id='.$_REQUEST['group_layout_id'];
		}
		// Return it in the right way.
		return $matched[1].$url.$matched[3];
	}

	return $matched[0];
}

/**
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     outputfilter.groupslayout.php
 * Type:     outputfilter
 * Name:     groupslayout
 * Version:  1.0
 * Date:     April 14, 2008
 * Purpose:
 * Install:  Drop into the plugin directory, call 
 *           $gBitSmarty->loadFilter('output','groupslayout');
 *           from application.
 * Author:   Nick Palmer <nick@slugardy.net> based on highlight filter by
 *           Greg Hinkle <ghinkl@users.sourceforge.net>
 *           and mose <mose@feu.org>
 * -------------------------------------------------------------
 */
function smarty_outputfilter_groupslayout( $source, &$gBitSmarty ) {
	if( empty($_REQUEST['group_layout_id']) || !is_numeric($_REQUEST['group_layout_id']) ) {
		return $source;
	}

	// get all text that needs to be
	$extractor = "|<div\s+id=.?content[^>]*>.*?<!--\s*end\s*#content\s*-->|is";
	preg_match_all( $extractor, $source, $match );
	$contents = $match[0];

	$ret = [];
	foreach( $contents as $key => $content ) {
		// if we picked something up, we rip it out and modify
		if( !empty( $content ) ) {
			$source = preg_replace( $extractor, "@@@SMARTY:TRIM:CONTENT@@@", $source );

			// Protect some parts
			$patterns = [
				"!<script[^>]+>.*?</script>!is"           => "@@@SMARTY:TRIM:SCRIPT@@@",
			];

			//			ksort( $patterns );

			foreach( $patterns as $pattern => $replace ) {
				preg_match_all( $pattern, $content, $match );
				$matches[$replace] = $match[0];
				$content = preg_replace( $pattern, $replace, $content );
			}

			// Now we can finally fix up the anchor tags
			$pattern = '#(<a\s+.*?href=[\"\'])(.+?)([\"\']\s*[^>]*>)#';
			$content = preg_replace_callback($pattern, group_replace_url_hander, $content);

			// Put the protected parts back in.
			foreach( $patterns as $pattern ) {
				foreach( $matches[$pattern] as $insert ) {
					$content = preg_replace( "!{$pattern}!", $insert, $content, 1 );
				}
			}

			$ret[] = $content;
		}
	}

	// insert the code back into the source
	foreach( $ret as $content ) {
		// Escape dollars in content so they don't back reference
		$content = preg_replace('!\$!','\\\$', $content);
		$source = preg_replace( "!@@@SMARTY:TRIM:CONTENT@@@!", $content, $source, 1 );
	}

	return $source;
}