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