bannerlib.php 10.3 KB
Newer Older
awcolley's avatar
awcolley committed
1
<?php
Jyhem's avatar
Jyhem committed
2
// (c) Copyright 2002-2014 by authors of the Tiki Wiki CMS Groupware Project
3
//
4 5 6
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
7

8
//this script may only be included - so its better to die if called directly.
9 10 11
if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
	header("location: index.php");
	exit;
12 13
}

14 15
class BannerLib extends TikiLib
{
lrargerich's avatar
lrargerich committed
16

17 18
	function select_banner_id($zone)
	{
19
		$map = array(0=>'sun', 1=>'mon', 2=>'tue', 3=>'wed', 4=>'thu', 5=>'fri', 6=>'sat');
20
		$dw = $map[$this->date_format("%w")];
21

sylvieg's avatar
sylvieg committed
22
		$hour = $this->date_format("%H"). $this->date_format("%M");
23 24 25 26
		$cookieName = "banner_$zone";
		$mid = '';
		$views = array();
		$bindvars = array('y', $hour, $hour, 'y', (int) $this->now, (int) $this->now, 'n', -1, -1, $zone);
27

28
		if (isset($_COOKIE[$cookieName])) {
29
			$views = json_decode($_COOKIE[$cookieName]);
30
			$mid = 'and (`bannerId` not in (' . implode(',', array_fill(0, count($views), '?')) . ') or ';
31 32 33
			foreach ($views as $bId=>$bView) {
				$bindvars[] = $bId;
			}
34

35 36 37 38 39 40 41
			foreach ($views as $bId=>$bView) {
				$mids[] = '(`bannerId` = ? and `maxUserImpressions` > ?)';
				$bindvars[] = $bId;
				$bindvars[] = $bView;
			}
			$mid .= implode('or', $mids).')';
		}
42

43
			$query = "select `bannerId` from `tiki_banners` where `$dw` = ? and  `hourFrom`<=? and `hourTo`>=? and" .
44 45
							" ( ((`useDates` = ?) and (`fromDate`<=? and `toDate`>=?)) or (`useDates` = ?) ) and" .
							" (`impressions`<`maxImpressions`  or `maxImpressions`=?) and" .
46
							"  (`clicks`<`maxClicks` or `maxClicks`=? or `maxClicks` is NULL)" .
47 48 49 50
							" and `zone`=? $mid and (`exceptInURIs` not like ? or `exceptInURIs` IS NULL)";

			$bindvars[] = '%#'.$_SERVER['REQUEST_URI'].'#%';
			$query1 = "$query and `onlyInURIs` like ? order by ".$this->convertSortMode('random');
51

52 53
			$result = $this->query($query1, array_merge($bindvars, array('%#'.$_SERVER['REQUEST_URI'].'#%')), 1, 0);
			if (!($res = $result->fetchRow())) {
54 55 56 57 58 59
			$query1 = "$query and (`onlyInURIs` is NULL or `onlyInURIs` =?) order by ".$this->convertSortMode('random');
			$bindvars[] = '';
			$result = $this->query($query1, $bindvars, 1, 0);
			if (!($res = $result->fetchRow())) {
				return false;
			}
60 61 62 63 64 65 66 67 68 69
			}
			$id = $res["bannerId"];

			// Increment banner impressions here
			if ($id) {
				$query = "update `tiki_banners` set `impressions` = `impressions` + 1 where `bannerId` = ?";
				$result = $this->query($query, array($id));
			}

			return $id;
70 71 72
	}


73
	function select_banner($zone, $target = '_blank', $id = '')
74
	{
75
		global $prefs, $tikilib;
76 77 78 79 80 81 82 83

		// Things to check
		// UseDates and dates
		// Hours
		// weekdays
		// zone
		// maxImpressions and impressions

84
		if (!empty($zone)) {
85
			$id = $this->select_banner_id($zone);
86
		}
87 88
		$res = $this->get_banner($id);
		$class = 'banner' . str_replace(' ', '_', $zone);
89

90
		$raw = '';
91

92
		switch ($res["which"]) {
93 94 95
			case 'useHTML':
				$raw = $res["HTMLData"];

96
				break;
97 98 99 100 101 102
			case 'useFlash':
				if ($prefs['javascript_enabled'] == 'y') {
					global $headerlib; include_once('lib/headerlib.php');
					$headerlib->add_jsfile('lib/swfobject/swfobject.js');
				}
				$raw = $tikilib->embed_flash(unserialize($res['HTMLData']));
103
				break;
104

105

106 107
			case 'useImage':
				$raw
108
					= "<div class='banner $class'><a target='$target' href='banner_click.php?id="
109
					. $res["bannerId"] . "&amp;url=" . urlencode($res["url"])
110
					. "'><img alt='banner' border='0' src=\"banner_image.php?id="
111
					. $res["bannerId"] . "\" /></a></div>";
112

113
				break;
114

115 116
			case 'useFixedURL':
				@$fp = fopen($res["fixedURLData"], "r");
117

118 119
				if ($fp) {
					$raw = '';
120

121 122 123 124
					while (!feof($fp)) {
						$raw .= fread($fp, 4096);
					}
					fclose($fp);
125 126
				}

127
				break;
128

129
			case 'useText':
130 131
				$raw = "<a target='$target' class='bannertext' href='banner_click.php?id=" . $res["bannerId"]
					. "&amp;url=" . urlencode($res["url"]) . "'>"
132
					. $res["textData"] . "</a>";
133

134
				break;
135 136
		}

137 138 139 140 141
		// Increment banner impressions done in select_banner_id()
		// Now to set view limiting cookie for user
		$cookieName = "banner_$zone";
		$views = array();
		if (isset($_COOKIE[$cookieName])) {
142
			$views = json_decode($_COOKIE[$cookieName]);
143
		}
144 145
		if ($res['maxUserImpressions'] > 0) {
			$views[$res['bannerId']] = isset($views[$res['bannerId']]) ? $views[$res['bannerId']]+1: 1;
146
			$expire = $res['useDates']? $res['toDate']: $tikilib->now+60*60*24*90; //90 days
147
			setcookie($cookieName, json_encode($views), $expire);
148
		}
149 150 151 152

		return $raw;
	}

153 154
	function add_click($bannerId)
	{
redflo's avatar
redflo committed
155
		$query = "update `tiki_banners` set `clicks` = `clicks` + 1 where `bannerId`=?";
156

157
		$result = $this->query($query, array((int)$bannerId));
158 159
	}

160 161
	function list_banners($offset = 0, $maxRecords = -1, $sort_mode = 'created_desc', $find = '', $user)
	{
162 163
		if ($user == 'admin') {
			$mid = '';
164
			$bindvars = array();
165
		} else {
redflo's avatar
redflo committed
166
			$mid = "where `client` = ?";
167
			$bindvars = array($user);
168 169 170 171
		}


		if ($find) {
redflo's avatar
redflo committed
172
			$findesc = '%' . $find . '%';
173
			$bindvars[] = $findesc;
174 175

			if ($mid) {
redflo's avatar
redflo committed
176
				$mid .= " and `url` like ? ";
177
			} else {
redflo's avatar
redflo committed
178
				$mid .= " where `url` like ? ";
179 180 181
			}
		}

182
		$query = "select * from `tiki_banners` $mid order by " . $this->convertSortMode($sort_mode);
redflo's avatar
redflo committed
183
		$query_cant = "select count(*) from `tiki_banners` $mid";
184 185
		$result = $this->query($query, $bindvars, $maxRecords, $offset);
		$cant = $this->getOne($query_cant, $bindvars);
186 187
		$ret = array();

redflo's avatar
redflo committed
188
		while ($res = $result->fetchRow()) {
189 190 191 192 193 194 195 196 197
			$ret[] = $res;
		}

		$retval = array();
		$retval["data"] = $ret;
		$retval["cant"] = $cant;
		return $retval;
	}

198
	function list_zones()
199
	{
redflo's avatar
redflo committed
200
		$query = "select `zone` from `tiki_zones`";
201

redflo's avatar
redflo committed
202
		$query_cant = "select count(*) from `tiki_zones`";
203 204
		$result = $this->query($query, array());
		$cant = $this->getOne($query_cant, array());
205 206
		$ret = array();

redflo's avatar
redflo committed
207
		while ($res = $result->fetchRow()) {
208 209 210 211 212 213 214 215 216
			$ret[] = $res;
		}

		$retval = array();
		$retval["data"] = $ret;
		$retval["cant"] = $cant;
		return $retval;
	}

217 218
	function remove_banner($bannerId)
	{
redflo's avatar
redflo committed
219
		$query = "delete from `tiki_banners` where `bannerId`=?";
220

221
		$result = $this->query($query, array($bannerId));
222 223
	}

224 225
	function get_banner($bannerId)
	{
redflo's avatar
redflo committed
226
		$query = "select * from `tiki_banners` where `bannerId`=?";
227

228
		$result = $this->query($query, array($bannerId));
229 230 231 232

		if (!$result->numRows())
			return false;

redflo's avatar
redflo committed
233
		$res = $result->fetchRow();
234 235 236
		return $res;
	}

237
	function replace_banner(
238 239 240 241 242 243 244 245 246
					$bannerId,
					$client,
					$url,
					$title = '',
					$alt = '',
					$use,
					$imageData,
					$imageType,
					$imageName,
247
					$HTMLData,
248 249 250 251 252 253 254 255 256 257 258 259 260
					$fixedURLData,
					$textData,
					$fromDate,
					$toDate,
					$useDates,
					$mon,
					$tue,
					$wed,
					$thu,
					$fri,
					$sat,
					$sun,
					$hourFrom,
261
					$hourTo,
262
					$maxImpressions,
263 264
					$maxClicks,
					$zone,
265 266
					$maxUserImpressions = -1,
					$onlyInURIs = null,
267
					$exceptInURIs = null
268
			)
269
	{
redflo's avatar
redflo committed
270
		$imageData = urldecode($imageData);
271 272 273
		//$imageData = '';

		if ($bannerId) {
redflo's avatar
redflo committed
274
			$query = "update `tiki_banners` set
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
				`client` = ?,
				`url` = ?,
				`title` = ?,
				`alt` = ?,
				`which` = ?,
				`imageData` = ?,
				`imageType` = ?,
				`imageName` = ?,
				`HTMLData` = ?,
				`fixedURLData` = ?,
				`textData` = ?,
				`fromDate` = ?,
				`toDate` = ?,
				`useDates` = ?,
				`created` = ?,
				`zone` = ?,
				`hourFrom` = ?,
				`hourTo` = ?,
				`mon` = ? ,`tue` = ?, `wed` = ?, `thu` = ?, `fri` = ?, `sat` = ?, `sun` = ?,
				`maxImpressions` = ?, `maxUserImpressions`=?, `maxClicks` = ?, `onlyInURIs`=?, `exceptInURIs`=? where `bannerId`=?";

			$bindvars = array(
								$client,
								$url,
								$title,
								$alt,
								$use,
								$imageData,
								$imageType,
								$imageName,
								$HTMLData,
306 307 308 309
								$fixedURLData,
								$textData,
								$fromDate,
								$toDate,
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
								$useDates,
								$this->now,
								$zone,
								$hourFrom,
								$hourTo,
								$mon,
								$tue,
								$wed,
								$thu,
								$fri,
								$sat,
								$sun,
								$maxImpressions,
								$maxUserImpressions,
								$maxClicks,
325 326
								$onlyInURIs,
								$exceptInURIs,
327 328 329 330 331 332 333 334
								$bannerId
			);

			$result = $this->query($query, $bindvars);

			/* invalid cache */
			global $tikilib, $tikidomain, $prefs;
			$bannercachefile = $prefs['tmpDir'];
335 336
			if ($tikidomain) {
				$bannercachefile .= "/$tikidomain";
337 338 339
			}
			$bannercachefile.= "/banner.".(int)$bannerId;
			unlink($bannercachefile);
340
		} else {
341 342
			$query = "insert into `tiki_banners`(`client`, `url`, `title`, `alt`, `which`, `imageData`, `imageType`, `HTMLData`," .
							" `fixedURLData`, `textData`, `fromDate`, `toDate`, `useDates`, `mon`, `tue`, `wed`, `thu`, `fri`, `sat`, `sun`," .
343
							" `hourFrom`, `hourTo`, `maxImpressions`,`maxUserImpressions`,`maxClicks`,`created`,`zone`,`imageName`," .
344 345 346 347 348 349 350 351 352 353 354 355
							" `impressions`,`clicks`, `onlyInURIs`, `exceptInURIs`)" .
							" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

			$bindvars = array(
									$client,
									$url,
									$title,
									$alt,
									$use,
									$imageData,
									$imageType,
									$HTMLData,
356 357 358 359 360
									$fixedURLData,
									$textData,
									$fromDate,
									$toDate,
									$useDates,
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
									$mon,
									$tue,
									$wed,
									$thu,
									$fri,
									$sat,
									$sun,
									$hourFrom,
									$hourTo,
									$maxImpressions,
									$maxUserImpressions,
									$maxClicks,
									$this->now,
									$zone,
									$imageName,
									0,
377 378
									0,
									$onlyInURIs,
379 380 381 382 383
									$exceptInURIs
			);


			$result = $this->query($query, $bindvars);
mose's avatar
mose committed
384
			$query = "select max(`bannerId`) from `tiki_banners` where `created`=?";
385
			$bannerId = $this->getOne($query, array((int)$this->now));
386 387 388 389 390
		}

		return $bannerId;
	}

391 392
	function banner_add_zone($zone)
	{
redflo's avatar
redflo committed
393
		$query = "delete from `tiki_zones` where `zone`=?";
394
		$this->query($query, array($zone), -1, -1, false);
redflo's avatar
redflo committed
395
		$query = "insert into `tiki_zones`(`zone`) values(?)";
396
		$result = $this->query($query, array($zone));
397 398 399
		return true;
	}

400 401
	function banner_get_zones()
	{
redflo's avatar
redflo committed
402
		$query = "select * from `tiki_zones`";
403

404
		$result = $this->query($query, array());
405 406
		$ret = array();

redflo's avatar
redflo committed
407
		while ($res = $result->fetchRow()) {
408 409 410 411 412 413
			$ret[] = $res;
		}

		return $ret;
	}

414 415
	function banner_remove_zone($zone)
	{
redflo's avatar
redflo committed
416
		$query = "delete from `tiki_zones` where `zone`=?";
417

418
		$result = $this->query($query, array($zone));
419 420 421

		return true;
	}
lrargerich's avatar
lrargerich committed
422
}
423
global $bannerlib; $bannerlib = new BannerLib;