PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/LiftiumConfig.php

https://github.com/liftiuminc/liftium
PHP | 217 lines | 163 code | 37 blank | 17 comment | 25 complexity | 4265285803af630c4af4f7d1d6085428 MD5 | raw file
  1. <?php
  2. class LiftiumConfig{
  3. const cacheTimeout = 15;
  4. const cacheTimeout_tag = 30;
  5. public function getConfig($criteria = array()){
  6. $cache = LiftiumCache::getInstance();
  7. $AdTag = new AdTag();
  8. $cacheKey = __CLASS__ . ':' . __METHOD__ . ":" . md5(serialize($criteria)) . self::getCacheVersion();
  9. $object = $cache->get($cacheKey);
  10. if (!empty($object) && empty($_GET['purge'])){
  11. return $object;
  12. }
  13. // Cache miss, get from DB
  14. $object = new stdClass();
  15. // Pull tags
  16. $criteria['enabled'] = 1;
  17. $criteria['brand_safety_level_check'] = true;
  18. foreach ($AdTag->getSizes() as $size){
  19. $criteria['size'] = $size;
  20. $tags = AdTag::searchTags($criteria, false);
  21. foreach($tags as $tag_id){
  22. $tag = $this->loadTagFromId($tag_id);
  23. if (AdTag::isUnderDailyLimit($tag_id, @$tag['max_daily_impressions'])){
  24. $object->sizes[$size][] = $tag;
  25. } else {
  26. if (!empty($_GET['debug'])){
  27. echo "tag #$tag_id skipped because it's over daily limit";
  28. }
  29. }
  30. }
  31. }
  32. // Pull publisher level info
  33. $dbr = Framework::getDB("slave");
  34. $sql = "SELECT hoptime, site_name, brand_safety_level,
  35. beacon_throttle FROM publishers WHERE id = ?";
  36. $sth = $dbr->prepare($sql);
  37. $sth->execute(array($criteria['pubid']));
  38. $publisher = $sth->fetchObject();
  39. unset($sth);
  40. if (!empty($publisher)){
  41. $object->max_hop_time = $publisher->hoptime;
  42. $object->throttle = $publisher->beacon_throttle;
  43. $object->brand_safety_level = $publisher->brand_safety_level;
  44. $object->site_name = $publisher->site_name;
  45. }
  46. // Store in memcache for next time
  47. $cache->set($cacheKey, $object, 0, self::cacheTimeout);
  48. return $object;
  49. }
  50. public function loadTagFromId($tag_id){
  51. $cache = LiftiumCache::getInstance();
  52. $cacheKey = __CLASS__ . ':' . __METHOD__ . ':' . self::getCacheVersion() . ":$tag_id";
  53. $out = $cache->get($cacheKey);
  54. if (!empty($out) && empty($_GET['purge'])){
  55. return $out;
  56. }
  57. // Cache miss, get from DB
  58. $dbr = Framework::getDB("slave");
  59. static $sth_t;
  60. if (empty($sth_t)){
  61. $sql = "SELECT networks.network_name, tags.id AS tag_id, tags.network_id,
  62. tags.tag, tags.always_fill, tags.sample_rate,
  63. tags.frequency_cap AS freq_cap, tags.size, tags.pacing,
  64. tags.rejection_time as rej_time, tags.tier, tags.value, tags.floor,
  65. networks.tag_template, networks.pay_type, tags.max_daily_impressions
  66. FROM tags
  67. INNER JOIN networks ON tags.network_id = networks.id
  68. WHERE tags.id = ? LIMIT 1";
  69. $sth_t = $dbr->prepare($sql);
  70. }
  71. $sth_t->execute(array($tag_id));
  72. $out = $sth_t->fetch(PDO::FETCH_ASSOC);
  73. if ($out === false){
  74. return false;
  75. }
  76. // Get the tag options
  77. $dim = AdTag::getHeightWidthFromSize($out['size']);
  78. $tag_options = array(
  79. 'size' => $out['size'],
  80. 'width' => $dim['width'],
  81. 'height' => $dim['height']
  82. );
  83. static $sth_o;
  84. if (empty($sth_o)){
  85. $sql = "SELECT option_name, option_value FROM tag_options WHERE tag_id = ?";
  86. $sth_o = $dbr->prepare($sql);
  87. }
  88. $sth_o->execute(array($tag_id));
  89. while($row = $sth_o->fetch(PDO::FETCH_ASSOC)){
  90. $tag_options[$row['option_name']]=$row['option_value'];
  91. }
  92. // Abbreviate pay_type
  93. switch ($out['pay_type']){
  94. case "Per Impression" : $out['pay_type'] = "CPM"; break;
  95. case "Per Click" : $out['pay_type'] = "CPC"; break;
  96. case "Affliate" : $out['pay_type'] = "CPA"; break;
  97. }
  98. // If the tag exists already, pass it through the expander
  99. if (!empty($out['tag'])){
  100. $out['tag'] = AdTag::expandMacros($out['tag'], $tag_options);
  101. unset($out['tag_template']);
  102. } else if (!empty($out['tag_template'])){
  103. $out['tag'] = AdTag::expandMacros($out['tag_template'], $tag_options);
  104. unset($out['tag_template']);
  105. } else {
  106. $class = AdNetwork::getNetworkClass($out['network_id']);
  107. if ($class === false){
  108. $msg = "No tag template, network class or tag for tagid #{$out['tag_id']}";
  109. trigger_error($msg, E_USER_WARNING);
  110. $out['tag'] = $msg;
  111. } else {
  112. $AN = new $class();
  113. $out['tag'] = $AN->getAd($slotname, $size, $tag_options);
  114. }
  115. }
  116. // Make the tag smaller
  117. $out['tag'] = self::packTag($out['tag']);
  118. // Get the Targeting criteria
  119. $out['criteria'] = TargetingCriteria::getCriteriaForTag($tag_id);
  120. if (!empty($_GET['debug'])){
  121. print_r($out);
  122. }
  123. // Slim it down for compactness
  124. $slimout = array();
  125. foreach ($out as $key => $value){
  126. if (!empty($value)){
  127. $slimout[$key] = $value;
  128. }
  129. }
  130. // Store in memcache for next time
  131. $cache->set($cacheKey, $slimout, 0, self::cacheTimeout_tag);
  132. return $slimout;
  133. }
  134. static public function getCountryList(){
  135. $cache = LiftiumCache::getInstance();
  136. $cacheKey = __CLASS__ . ':' . __METHOD__ . ":" . self::cacheVersion . ":";
  137. $out = $cache->get($cacheKey);
  138. if (!empty($out) && empty($_GET['purge'])){
  139. return $out;
  140. }
  141. // Cache miss, get from DB
  142. $dbr = Framework::getDB("slave");
  143. $sql = "SELECT target_keyvalue FROM target_value WHERE
  144. target_key_id = (SELECT target_key_id FROM target_key WHERE target_keyname = 'Geography')
  145. ORDER BY length(target_keyvalue), target_keyvalue";
  146. $sth = $dbr->prepare($sql);
  147. $sth->execute();
  148. $out = array();
  149. while($row = $dbr->fetch(PDO::FETCH_ASSOC)){
  150. $out[] = $row['target_keyvalue'];
  151. }
  152. // Store in memcache for next time
  153. $cache->set($cacheKey, $out, 0, 99);
  154. return $out;
  155. }
  156. public static function clearCache(){
  157. trigger_error("clear cache not implented", E_USER_WARNING);
  158. //file_get_contents("http://athena-ads.wikia.com/athena/config/?purge=1&cb=" . mt_rand());
  159. }
  160. static private function getCacheVersion(){
  161. if (Framework::isDev()) {
  162. return mt_rand();
  163. } else {
  164. return "1.0r";
  165. }
  166. }
  167. static public function getLastUpdated() {
  168. $db = Framework::getDB("slave");
  169. $result = $db->query("SELECT UNIX_TIMESTAMP(MAX(updated_at)) as lastmod FROM tags;");
  170. $row = $result->fetch();
  171. return intval($row[0]);
  172. }
  173. // Make the tag smaller. Someday: Pack the javascript
  174. static public function packTag($tag) {
  175. // Cheap and easy: Remove the leading white space. That's never needed.
  176. $out = preg_replace('/^[ \t]+/m', '', $tag);
  177. return $out;
  178. }
  179. }
  180. ?>