-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplg_flexbanner.php
283 lines (239 loc) · 10.7 KB
/
plg_flexbanner.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* @version $Id: plg_flexbanner.php,v 1.2 2010/04/29 17:44:09 carlos.delfino Exp $
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @Author: Carlos Delfino [email protected], based on work of keep - http://joomla.blog.hu - based on: Tobbworld.de - http://www.tobbworld.de
*
*
**/
/** Ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );
$mainframe->registerEvent( 'onPrepareContent', 'newBanner' );
jimport( 'joomla.plugin.plugin' );
/**
* este plugin existe apenas a titulo de inserção de funções para uso
* nos modulos, componentes, outro plugins e templates dos sistemas
* desenvolvidos pelo CSAT.
*
* @author Delfino
*
*/
class plgContentFlexbanner extends JPlugin {
private $lastmosaddbanner_params = array();
function __construct($subject, $config){
parent::__construct($subject, $config);
}
function onPrepareContent( &$article, &$params, $limitstart) {
$database =& JFactory::getDBO();
if ( JString::strpos( $article->text, 'flexbanner' ) === false ) {
return true;
}
$plugin =& JPluginHelper::getPlugin('content', 'plg_flexbanner');
if ( !JString::strpos( $article->text, "{flexbanner}") ) {
if (preg_match('{flexbanner:id=\d+(,\d+)*}', $article->text)) {
$article->text = $this->processWithId($article );
}
if (preg_match('{flexbanner:clientid=\d+(,\d+)*}', $article->text)) {
$article->text = $this->processWithClientId($article);
}
if (preg_match('{flexbanner:location=\d+(,\d+)*}', $article->text)) {
$article->text = $this->processWithCatId($article);
}
if (preg_match('{flexbanner:cat=\d+(,\d+)*}', $article->text)) {
$text = $article->text;
if (preg_match_all('/{flexbanner:location=\d+(,\d+)*}/', $text, $mosaddbanner_matches, PREG_PATTERN_ORDER) > 0) {
foreach ($mosaddbanner_matches[0] as $mosaddbanner_match) {
$mosaddbanner_output = "";
$mosaddbanner_match = str_replace("{flexbanner:location=", "", $mosaddbanner_match);
$mosaddbanner_match = str_replace("}", "", $mosaddbanner_match);
$mosaddbanner_params = array();
$mosaddbanner_params = explode(",", $mosaddbanner_match);
$database->setQuery(
"SELECT * FROM #__fabanner AS b
JOIN #__fabannerlocation as l ON b.bannerid = l.bannerid
WHERE l.locationid IN($mosaddbanner_match) AND b.published=1
ORDER BY rand() LIMIT 1" );
$numrows = $database->loadResult();
if($numrows >0 ) {
$banner = $database->loadObject( );
$text = preg_replace("/{flexbanner:location=\d+(,\d+)*}/", viewBannerWithId($banner->bannerid), $text, 1);
$article->text = str_replace ("{flexbanner}", "", $article->text);
} else {
$text = preg_replace ("/{flexbanner:location=\d+(,\d+)*}/", "", $text);
}
}
$article->text = $text;
}
}
return true;
} else {
$article->text = str_replace ("{flexbanner}", $this->viewbanner2(), $article->text);
}
}
function defaultBanner(){
return "<!-- no Banner -->";
}
function viewBannerWithId( $bannerId ) {
$database =& JFactory::getDBO();
$database->setQuery( "SELECT * FROM #__fabanner WHERE bannerid=$bannerId AND published=1" );
$numrows = $database->loadResult();
if($numrows == 0 ) {
return defaultBanner();
}
$banner = null;
if ($banner = $database->loadObject()) {
if ($numrows > 0) {
// Check if this impression is the last one and print the banner
if (($banner->maximpressions>0) && ($banner->maximpressions <= $banner->impressions)) {
return defaultBanner();
}
$banner->impmade++;
$database->setQuery( "UPDATE #__fabanner SET impressions=impressions+1 WHERE bannerid='$banner->bannerid'" );
if(!$database->query()) {
echo $database->stderr( true );
return;
}
if (trim( $banner->custombannercode )) {
return $banner->custombannercode;
} else if (eregi( "(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$", $banner->imageurl )) {
$imageurl = JURI::base().JRoute::_('images/banners/'.$banner->imageurl);
$banner1 = "<a href=\"index.php?option=com_flexbanner&task=click&bannerid=$banner->bannerid\" target=\"_blank\"><img src=\"$imageurl\" border=\"0\" /></a>";
return $banner1;
} else if (eregi("\.swf$", $banner->imageurl)) {
$imageurl = JURI::base().JRoute::_('images/banners/'.$banner->imageurl);
$banner2= "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" border=\"5\">
<param name=\"movie\" value=\"$imageurl\"><embed src=\"$imageurl\" loop=\"false\" pluginspage=\"http://www.macromedia.com/go/get/flashplayer\" type=\"application/x-shockwave-flash\"></embed></object>";
return $banner2;
}
}
} else {
return defaultBanner();
}
}
private function viewbanner2() {
$database =& JFactory::getDBO();
// é muito pessado fazer o count toda vez
// que ele encontrar um diretiva para exibir
// o banner, no nosso caso um artigo poderá
// causar a execução deste count mais de 30
// vezes por exibição
$database->setQuery( "SELECT count(*) AS numrows FROM #__fabanner WHERE published=1 AND ((imptotal>impmade) OR (imptotal=0))" );
$numrows = $database->loadResult();
if ($numrows == 0) {
return "";
}
$plugin =& JPluginHelper::getPlugin('content', 'plg_flexbanner');
$loop = $plugin->params->loop;
$database->setQuery( "SELECT * FROM #__fabanner WHERE published=1 AND ((imptotal>impmade) OR (imptotal=0)) ORDER BY rand() LIMIT 1" );
$banner = null;
if ($banner = $database->loadObject( )) {
if ($numrows > 0) {
// Check if this impression is the last one and print the banner
$database->setQuery( "UPDATE #__fabanner SET impmade=impmade+1 WHERE bannerid='$banner->bannerid'" );
$banner->impmade++;
if (trim( $banner->custombannercode )) {
return $banner->custombannercode;
} else if (eregi( "(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$", $banner->imageurl )) {
$imageurl = "/images/banners/$banner->imageurl";
$banner1 = "<a href=\"index.php?option=com_flexbanners&task=click&bannerid=$banner->bannerid\" target=\"_blank\"><img src=\"$imageurl\" border=\"0\" /></a>";
return $banner1;
} else if (eregi("\.swf$", $banner->imageurl)) {
$imageurl = "/images/banners/".$banner->imageurl;
$banner2= "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" border=\"5\">
<param name=\"movie\" value=\"$imageurl\"><embed src=\"$imageurl\" loop=\"$loop\" pluginspage=\"http://www.macromedia.com/go/get/flashplayer\" type=\"application/x-shockwave-flash\"></embed></object>";
return $banner2;
}
}
} else {
return "";
}
}
// funões de processamento e substituição
private function processWithLocationId($article) {
$text = $article->text;
if (preg_match_all('/{flexbanner:location=\d+(,\d+)*}/', $text, $mosaddbanner_matches, PREG_PATTERN_ORDER) > 0) {
foreach ($mosaddbanner_matches[0] as $mosaddbanner_match) {
$mosaddbanner_output = "";
$mosaddbanner_match = str_replace("{flexbanner:location=", "", $mosaddbanner_match);
$mosaddbanner_match = str_replace("}", "", $mosaddbanner_match);
$mosaddbanner_params = array();
$mosaddbanner_params = explode(",", $mosaddbanner_match);
$database->setQuery(
"SELECT * FROM #__fabanner AS b
JOIN #__fabannerlocation as l ON b.bannerid = l.bannerid
WHERE l.locationid IN($mosaddbanner_match) AND b.published=1
ORDER BY rand() LIMIT 1" );
$numrows = $database->loadResult();
if($numrows >0 ) {
$banner = $database->loadObject( );
$text = preg_replace("/{flexbanner:location=\d+(,\d+)*}/", viewBannerWithId($banner->bannerid), $text, 1);
$article->text = str_replace ("{flexbanner}", "", $article->text);
} else {
$text = preg_replace ("/{flexbanner:location=\d+(,\d+)*}/", "", $text);
}
}
$article->text = $text;
}
return $text;
}
private function processWithClientId($article) {
$text = $article->text;
if (preg_match_all('/{flexbanner:clientid=\d+(,\d+)*}/', $text, $mosaddbanner_matches, PREG_PATTERN_ORDER) > 0) {
foreach ($mosaddbanner_matches[0] as $mosaddbanner_match) {
$mosaddbanner_output = "";
$mosaddbanner_match = str_replace("{flexbanner:clientid=", "", $mosaddbanner_match);
$mosaddbanner_match = str_replace("}", "", $mosaddbanner_match);
$mosaddbanner_params = array();
$mosaddbanner_params = explode(",", $mosaddbanner_match);
$database->setQuery( "SELECT * FROM #__fabanner WHERE clientid IN($mosaddbanner_match) AND published=1 ORDER BY rand() LIMIT 1" );
$numrows = $database->loadResult();
if($numrows >0 ) {
$banner = $database->loadObject( );
$text = preg_replace("/{flexbanner:clientid=\d+(,\d+)*}/", viewBannerWithId($banner->bannerid), $text, 1);
$article->text = str_replace ("{flexbanner}", "", $article->text);
} else {
$text = preg_replace ("/{flexbanner:clientid=\d+(,\d+)*}/", "", $text);
}
}
$article->text = $text;
}
return $text;
}private function processWithId(&$article){
$text = $article->text;
if (preg_match_all('/{flexbanner:id=\d+(,\d+)*}/', $text, $mosaddbanner_matches, PREG_PATTERN_ORDER) > 0) {
foreach ($mosaddbanner_matches[0] as $mosaddbanner_match) {
$mosaddbanner_match = str_replace("{flexbanner:id=", "", $mosaddbanner_match);
$mosaddbanner_match = str_replace("}", "", $mosaddbanner_match);
$mosaddbanner_params = array();
$mosaddbanner_params = explode(",", $mosaddbanner_match);
$mosaddbanner_count = count($mosaddbanner_params);
if($mosaddbanner_count > 1){
//pega um novo banner ainda nãusado
// 1
for ($i = 0,$used = true;$i<$mosaddbanner_count && $used && $mosadbanner_count <= count($this->lastmosaddbanner_params); $i++ ){
$this->last = $mosaddbanner_params[array_rand($mosaddbanner_params)]; // 3
$used = in_array($last,$this->lastmosaddbanner_params); // 4
if($i+1<$mosaddbanner_count && $used && $mosadbanner_count <= count($this->lastmosaddbanner_params)){
$this->lastmosaddbanner_params = array();
}
}
if($i == $mosaddbanner_count){
continue;
}else if($used){
continue;
}
}else if($mosaddbanner_count == 1 && !in_array($last,$this->lastmosaddbanner_params)){
$last = $mosaddbanner_params[0];
}else{
continue;
}
$this->lastmosaddbanner_params[] = $last; // 5
$bannerHtml = viewBannerWithId($last);
$text = preg_replace("/{flexbanner:id=\d+(,\d+)*}/", $bannerHtml, $text, 1);
}
$text = preg_replace ("/{flexbanner(.+)*}/", "", $text);
return $text;
}
}
}
?>