PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 15ms RepoModel.GetById 25ms app.codeStats 0ms

/users/CampaignTracker.php

http://showslow.googlecode.com/
PHP | 348 lines | 307 code | 34 blank | 7 comment | 26 complexity | 200f7dd48203f8d398846564ecbf551b MD5 | raw file
  1<?php
  2require_once(dirname(__FILE__).'/CookieStorage.php');
  3
  4/*
  5 * This file should be included on all pages if referer tracking is required
  6 */
  7
  8class CampaignTracker
  9{
 10	private static $referer = null;
 11	private static $campaign = null;
 12
 13	public static function recordCampaignVariables() {
 14		$campaign = array();
 15
 16		foreach (UserConfig::$campaign_variables as $variable => $urlparams) {
 17			foreach ($urlparams as $param) {
 18				if (array_key_exists($param, $_GET)) {
 19					$campaign[$variable] = $_GET[$param];
 20				}
 21			}
 22		}
 23
 24		if (count($campaign) == 0) {
 25			return;
 26		}
 27
 28		$storage = new MrClay_CookieStorage(array(
 29			'secret' => UserConfig::$SESSION_SECRET,
 30			'mode' => MrClay_CookieStorage::MODE_ENCRYPT,
 31			'path' => UserConfig::$SITEROOTURL,
 32			'expire' => 0,
 33			'httponly' => true
 34		));
 35
 36		if (!$storage->store(UserConfig::$entry_cmp_key, serialize($campaign))) {
 37			throw new Exception(implode("\n", $storage->errors));
 38		}
 39	}
 40
 41	// sets the referrer into a cookie
 42	public static function preserveReferer() {
 43		if (array_key_exists('HTTP_REFERER', $_SERVER)) {
 44			$referer = $_SERVER['HTTP_REFERER'];
 45
 46			// only set if referrer is not on this site
 47			if (strpos($referer, UserConfig::$SITEROOTFULLURL) === 0) {
 48				return;
 49			}
 50
 51			$storage = new MrClay_CookieStorage(array(
 52				'secret' => UserConfig::$SESSION_SECRET,
 53				'mode' => MrClay_CookieStorage::MODE_ENCRYPT,
 54				'path' => UserConfig::$SITEROOTURL,
 55				'expire' => 0,
 56				'httponly' => true
 57			));
 58
 59			if (!$storage->store(UserConfig::$entry_referer_key, $referer)) { 
 60				throw new Exception(implode("\n", $storage->errors));
 61			}
 62
 63			self::$referer = $referer;
 64		}
 65	}
 66
 67	public static function getReferer() {
 68		// use static one if we're on an entry page
 69		if (!is_null(self::$referer)) {
 70			return self::$referer;
 71		}
 72
 73		$storage = new MrClay_CookieStorage(array(
 74			'secret' => UserConfig::$SESSION_SECRET,
 75			'mode' => MrClay_CookieStorage::MODE_ENCRYPT,
 76			'path' => UserConfig::$SITEROOTURL,
 77			'httponly' => true
 78		));
 79
 80		return($storage->fetch(UserConfig::$entry_referer_key));
 81	}
 82
 83	public static function getCampaign() {
 84		// use static one if we're on an entry page
 85		if (!is_null(self::$campaign)) {
 86			return self::$campaign;
 87		}
 88
 89		$storage = new MrClay_CookieStorage(array(
 90			'secret' => UserConfig::$SESSION_SECRET,
 91			'mode' => MrClay_CookieStorage::MODE_ENCRYPT,
 92			'path' => UserConfig::$SITEROOTURL,
 93			'httponly' => true
 94		));
 95
 96		return(unserialize($storage->fetch(UserConfig::$entry_cmp_key)));
 97	}
 98
 99	public static function getCampaignSourceID($source) {
100		$source = mb_convert_encoding($source, 'UTF-8');
101
102		$db = UserConfig::getDB();
103
104		$cmp_source_id = null;
105		if ($stmt = $db->prepare('INSERT IGNORE INTO '.UserConfig::$mysql_prefix.'cmp_source (source) VALUES (?)'))
106		{
107			if (!$stmt->bind_param('s', $source))
108			{
109				throw new Exception("Can't bind parameter");
110			}
111			if (!$stmt->execute())
112			{
113				throw new Exception("Can't insert compaign source");
114			}
115			$stmt->close();
116		}
117		else
118		{
119			throw new Exception("Can't insert compaign source");
120		}
121
122		if ($stmt = $db->prepare('SELECT id FROM '.UserConfig::$mysql_prefix.'cmp_source
123						WHERE source = ?'))
124		{
125			if (!$stmt->bind_param('s', $source))
126			{
127				 throw new Exception("Can't bind parameter".$stmt->error);
128			}
129			if (!$stmt->execute())
130			{
131				throw new Exception("Can't execute statement: ".$stmt->error);
132			}
133			if (!$stmt->bind_result($cmp_source_id))
134			{
135				throw new Exception("Can't bind result: ".$stmt->error);
136			}
137
138			$stmt->fetch();
139			$stmt->close();
140		}
141		else
142		{
143			throw new Exception("Can't prepare statement: ".$db->error);
144		}
145
146		return $cmp_source_id;
147	}
148
149	public static function getCampaignMediumID($medium) {
150		$medium = mb_convert_encoding($medium, 'UTF-8');
151
152		$db = UserConfig::getDB();
153
154		$cmp_medium_id = null;
155		if ($stmt = $db->prepare('INSERT IGNORE INTO '.UserConfig::$mysql_prefix.'cmp_medium (medium) VALUES (?)'))
156		{
157			if (!$stmt->bind_param('s', $medium))
158			{
159				throw new Exception("Can't bind parameter");
160			}
161			if (!$stmt->execute())
162			{
163				throw new Exception("Can't insert compaign medium");
164			}
165			$stmt->close();
166		}
167		else
168		{
169			throw new Exception("Can't insert compaign medium");
170		}
171
172		if ($stmt = $db->prepare('SELECT id FROM '.UserConfig::$mysql_prefix.'cmp_medium
173						WHERE medium = ?'))
174		{
175			if (!$stmt->bind_param('s', $medium))
176			{
177				 throw new Exception("Can't bind parameter".$stmt->error);
178			}
179			if (!$stmt->execute())
180			{
181				throw new Exception("Can't execute statement: ".$stmt->error);
182			}
183			if (!$stmt->bind_result($cmp_medium_id))
184			{
185				throw new Exception("Can't bind result: ".$stmt->error);
186			}
187
188			$stmt->fetch();
189			$stmt->close();
190		}
191		else
192		{
193			throw new Exception("Can't prepare statement: ".$db->error);
194		}
195
196		return $cmp_medium_id;
197	}
198
199	public static function getCampaignKeywordsID($keywords) {
200		$keywords = mb_convert_encoding($keywords, 'UTF-8');
201
202		$db = UserConfig::getDB();
203
204		$cmp_keywords_id = null;
205		if ($stmt = $db->prepare('INSERT IGNORE INTO '.UserConfig::$mysql_prefix.'cmp_keywords (keywords) VALUES (?)'))
206		{
207			if (!$stmt->bind_param('s', $keywords))
208			{
209				throw new Exception("Can't bind parameter");
210			}
211			if (!$stmt->execute())
212			{
213				throw new Exception("Can't insert compaign keywords");
214			}
215			$stmt->close();
216		}
217		else
218		{
219			throw new Exception("Can't insert compaign keywords");
220		}
221
222		if ($stmt = $db->prepare('SELECT id FROM '.UserConfig::$mysql_prefix.'cmp_keywords
223						WHERE keywords = ?'))
224		{
225			if (!$stmt->bind_param('s', $keywords))
226			{
227				 throw new Exception("Can't bind parameter".$stmt->error);
228			}
229			if (!$stmt->execute())
230			{
231				throw new Exception("Can't execute statement: ".$stmt->error);
232			}
233			if (!$stmt->bind_result($cmp_keywords_id))
234			{
235				throw new Exception("Can't bind result: ".$stmt->error);
236			}
237
238			$stmt->fetch();
239			$stmt->close();
240		}
241		else
242		{
243			throw new Exception("Can't prepare statement: ".$db->error);
244		}
245
246		return $cmp_keywords_id;
247	}
248
249	public static function getCampaignContentID($content) {
250		$content = mb_convert_encoding($content, 'UTF-8');
251
252		$db = UserConfig::getDB();
253
254		$cmp_content_id = null;
255		if ($stmt = $db->prepare('INSERT IGNORE INTO '.UserConfig::$mysql_prefix.'cmp_content (content) VALUES (?)'))
256		{
257			if (!$stmt->bind_param('s', $content))
258			{
259				throw new Exception("Can't bind parameter");
260			}
261			if (!$stmt->execute())
262			{
263				throw new Exception("Can't insert compaign content");
264			}
265			$stmt->close();
266		}
267		else
268		{
269			throw new Exception("Can't insert compaign content");
270		}
271
272		if ($stmt = $db->prepare('SELECT id FROM '.UserConfig::$mysql_prefix.'cmp_content
273						WHERE content = ?'))
274		{
275			if (!$stmt->bind_param('s', $content))
276			{
277				 throw new Exception("Can't bind parameter".$stmt->error);
278			}
279			if (!$stmt->execute())
280			{
281				throw new Exception("Can't execute statement: ".$stmt->error);
282			}
283			if (!$stmt->bind_result($cmp_content_id))
284			{
285				throw new Exception("Can't bind result: ".$stmt->error);
286			}
287
288			$stmt->fetch();
289			$stmt->close();
290		}
291		else
292		{
293			throw new Exception("Can't prepare statement: ".$db->error);
294		}
295
296		return $cmp_content_id;
297	}
298
299	public static function getCampaignNameID($name) {
300		$name = mb_convert_encoding($name, 'UTF-8');
301
302		$db = UserConfig::getDB();
303
304		$cmp_name_id = null;
305		if ($stmt = $db->prepare('INSERT IGNORE INTO '.UserConfig::$mysql_prefix.'cmp (name) VALUES (?)'))
306		{
307			if (!$stmt->bind_param('s', $name))
308			{
309				throw new Exception("Can't bind parameter");
310			}
311			if (!$stmt->execute())
312			{
313				throw new Exception("Can't insert compaign name");
314			}
315			$stmt->close();
316		}
317		else
318		{
319			throw new Exception("Can't insert compaign name");
320		}
321
322		if ($stmt = $db->prepare('SELECT id FROM '.UserConfig::$mysql_prefix.'cmp
323						WHERE name = ?'))
324		{
325			if (!$stmt->bind_param('s', $name))
326			{
327				 throw new Exception("Can't bind parameter".$stmt->error);
328			}
329			if (!$stmt->execute())
330			{
331				throw new Exception("Can't execute statement: ".$stmt->error);
332			}
333			if (!$stmt->bind_result($cmp_name_id))
334			{
335				throw new Exception("Can't bind result: ".$stmt->error);
336			}
337
338			$stmt->fetch();
339			$stmt->close();
340		}
341		else
342		{
343			throw new Exception("Can't prepare statement: ".$db->error);
344		}
345
346		return $cmp_name_id;
347	}
348}