PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/www/delivery/ck.php

https://github.com/creath/revive-adserver
PHP | 3277 lines | 3224 code | 30 blank | 23 comment | 560 complexity | 5d81693d63e8c4f0ca566526653d46d1 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, AGPL-1.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | Revive Adserver |
  5. | http://www.revive-adserver.com |
  6. | |
  7. | Copyright: See the COPYRIGHT.txt file. |
  8. | License: GPLv2 or later, see the LICENSE.txt file. |
  9. +---------------------------------------------------------------------------+
  10. */
  11. /**
  12. * This is autogenerated file which contains all files from the "delivery_dev"
  13. * folder of Revive Adserver merged into a single output file. On systems
  14. * without a PHP opcode cache that is configured to not regularly check for
  15. * file updates, this autogenerated file can dramatically improve the
  16. * performance of Revive Adserver's delivery engine.
  17. *
  18. * !!!Warning!!!
  19. *
  20. * Do not edit this file. If you need to do any changes to any delivery file,
  21. * check out the source code from GitHub; make the necessary changes to the
  22. * file(s) in the "delivery_dev" folder; and regenerate the delivery files
  23. * using the script located in the "scripts/delivery" directory.
  24. */
  25. function parseDeliveryIniFile($configPath = null, $configFile = null, $sections = true)
  26. {
  27. if (!$configPath) {
  28. $configPath = MAX_PATH . '/var';
  29. }
  30. if ($configFile) {
  31. $configFile = '.' . $configFile;
  32. }
  33. $host = OX_getHostName();
  34. $configFileName = $configPath . '/' . $host . $configFile . '.conf.php';
  35. $conf = @parse_ini_file($configFileName, $sections);
  36. if (isset($conf['realConfig'])) {
  37. $realconf = @parse_ini_file(MAX_PATH . '/var/' . $conf['realConfig'] . '.conf.php', $sections);
  38. $conf = mergeConfigFiles($realconf, $conf);
  39. }
  40. if (!empty($conf)) {
  41. return $conf;
  42. } elseif ($configFile === '.plugin') {
  43. $pluginType = basename($configPath);
  44. $defaultConfig = MAX_PATH . '/plugins/' . $pluginType . '/default.plugin.conf.php';
  45. $conf = @parse_ini_file($defaultConfig, $sections);
  46. if ($conf !== false) {
  47. return $conf;
  48. }
  49. echo "Revive Adserver could not read the default configuration file for the {$pluginType} plugin";
  50. exit(1);
  51. }
  52. $configFileName = $configPath . '/default' . $configFile . '.conf.php';
  53. $conf = @parse_ini_file($configFileName, $sections);
  54. if (isset($conf['realConfig'])) {
  55. $conf = @parse_ini_file(MAX_PATH . '/var/' . $conf['realConfig'] . '.conf.php', $sections);
  56. }
  57. if (!empty($conf)) {
  58. return $conf;
  59. }
  60. if (file_exists(MAX_PATH . '/var/INSTALLED')) {
  61. echo "Revive Adserver has been installed, but no configuration file was found.\n";
  62. exit(1);
  63. }
  64. echo "Revive Adserver has not been installed yet -- please read the INSTALL.txt file.\n";
  65. exit(1);
  66. }
  67. if (!function_exists('mergeConfigFiles'))
  68. {
  69. function mergeConfigFiles($realConfig, $fakeConfig)
  70. {
  71. foreach ($fakeConfig as $key => $value) {
  72. if (is_array($value)) {
  73. if (!isset($realConfig[$key])) {
  74. $realConfig[$key] = array();
  75. }
  76. $realConfig[$key] = mergeConfigFiles($realConfig[$key], $value);
  77. } else {
  78. if (isset($realConfig[$key]) && is_array($realConfig[$key])) {
  79. $realConfig[$key][0] = $value;
  80. } else {
  81. if (isset($realConfig) && !is_array($realConfig)) {
  82. $temp = $realConfig;
  83. $realConfig = array();
  84. $realConfig[0] = $temp;
  85. }
  86. $realConfig[$key] = $value;
  87. }
  88. }
  89. }
  90. unset($realConfig['realConfig']);
  91. return $realConfig;
  92. }
  93. }
  94. function OX_getMinimumRequiredMemory($limit = null)
  95. {
  96. if ($limit == 'maintenance') {
  97. return 134217728; }
  98. return 134217728; }
  99. function OX_getMemoryLimitSizeInBytes() {
  100. $phpMemoryLimit = ini_get('memory_limit');
  101. if (empty($phpMemoryLimit) || $phpMemoryLimit == -1) {
  102. return -1;
  103. }
  104. $aSize = array(
  105. 'G' => 1073741824,
  106. 'M' => 1048576,
  107. 'K' => 1024
  108. );
  109. $phpMemoryLimitInBytes = $phpMemoryLimit;
  110. foreach($aSize as $type => $multiplier) {
  111. $pos = strpos($phpMemoryLimit, $type);
  112. if (!$pos) {
  113. $pos = strpos($phpMemoryLimit, strtolower($type));
  114. }
  115. if ($pos) {
  116. $phpMemoryLimitInBytes = substr($phpMemoryLimit, 0, $pos) * $multiplier;
  117. }
  118. }
  119. return $phpMemoryLimitInBytes;
  120. }
  121. function OX_checkMemoryCanBeSet()
  122. {
  123. $phpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
  124. if ($phpMemoryLimitInBytes == -1) {
  125. return true;
  126. }
  127. OX_increaseMemoryLimit($phpMemoryLimitInBytes + 1);
  128. $newPhpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
  129. $memoryCanBeSet = ($phpMemoryLimitInBytes != $newPhpMemoryLimitInBytes);
  130. @ini_set('memory_limit', $phpMemoryLimitInBytes);
  131. return $memoryCanBeSet;
  132. }
  133. function OX_increaseMemoryLimit($setMemory) {
  134. $phpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
  135. if ($phpMemoryLimitInBytes == -1) {
  136. return true;
  137. }
  138. if ($setMemory > $phpMemoryLimitInBytes) {
  139. if (@ini_set('memory_limit', $setMemory) === false) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. function setupConfigVariables()
  146. {
  147. $GLOBALS['_MAX']['MAX_DELIVERY_MULTIPLE_DELIMITER'] = '|';
  148. $GLOBALS['_MAX']['MAX_COOKIELESS_PREFIX'] = '__';
  149. $GLOBALS['_MAX']['thread_id'] = uniqid();
  150. $GLOBALS['_MAX']['SSL_REQUEST'] = false;
  151. if (
  152. (!empty($_SERVER['SERVER_PORT']) && !empty($GLOBALS['_MAX']['CONF']['openads']['sslPort']) && ($_SERVER['SERVER_PORT'] == $GLOBALS['_MAX']['CONF']['openads']['sslPort'])) ||
  153. (!empty($_SERVER['HTTPS']) && ((strtolower($_SERVER['HTTPS']) == 'on') || ($_SERVER['HTTPS'] == 1))) ||
  154. (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && (strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https')) ||
  155. (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && (strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) == 'on')) ||
  156. (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && (strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) == 'on')) ||
  157. (!empty($_SERVER['FRONT-END-HTTPS']) && (strtolower($_SERVER['FRONT-END-HTTPS']) == 'on'))
  158. ) {
  159. $GLOBALS['_MAX']['SSL_REQUEST'] = true;
  160. }
  161. $GLOBALS['_MAX']['MAX_RAND'] = isset($GLOBALS['_MAX']['CONF']['priority']['randmax']) ?
  162. $GLOBALS['_MAX']['CONF']['priority']['randmax'] : 2147483647;
  163. list($micro_seconds, $seconds) = explode(" ", microtime());
  164. $GLOBALS['_MAX']['NOW_ms'] = round(1000 *((float)$micro_seconds + (float)$seconds));
  165. if (substr($_SERVER['SCRIPT_NAME'], -11) != 'install.php') {
  166. $GLOBALS['serverTimezone'] = date_default_timezone_get();
  167. OA_setTimeZoneUTC();
  168. }
  169. }
  170. function setupServerVariables()
  171. {
  172. if (empty($_SERVER['REQUEST_URI'])) {
  173. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
  174. if (!empty($_SERVER['QUERY_STRING'])) {
  175. $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
  176. }
  177. }
  178. }
  179. function setupDeliveryConfigVariables()
  180. {
  181. if (!defined('MAX_PATH')) {
  182. define('MAX_PATH', dirname(__FILE__).'/../..');
  183. }
  184. if (!defined('OX_PATH')) {
  185. define('OX_PATH', MAX_PATH);
  186. }
  187. if (!defined('RV_PATH')) {
  188. define('RV_PATH', MAX_PATH);
  189. }
  190. if (!defined('LIB_PATH')) {
  191. define('LIB_PATH', MAX_PATH. DIRECTORY_SEPARATOR. 'lib'. DIRECTORY_SEPARATOR. 'OX');
  192. }
  193. if ( !(isset($GLOBALS['_MAX']['CONF']))) {
  194. $GLOBALS['_MAX']['CONF'] = parseDeliveryIniFile();
  195. }
  196. setupConfigVariables();
  197. }
  198. function OA_setTimeZone($timezone)
  199. {
  200. date_default_timezone_set($timezone);
  201. $GLOBALS['_DATE_TIMEZONE_DEFAULT'] = $timezone;
  202. }
  203. function OA_setTimeZoneUTC()
  204. {
  205. OA_setTimeZone('UTC');
  206. }
  207. function OA_setTimeZoneLocal()
  208. {
  209. $tz = !empty($GLOBALS['_MAX']['PREF']['timezone']) ? $GLOBALS['_MAX']['PREF']['timezone'] : 'GMT';
  210. OA_setTimeZone($tz);
  211. }
  212. function OX_getHostName()
  213. {
  214. if (!empty($_SERVER['HTTP_HOST'])) {
  215. $host = explode(':', $_SERVER['HTTP_HOST']);
  216. $host = $host[0];
  217. } else if (!empty($_SERVER['SERVER_NAME'])) {
  218. $host = explode(':', $_SERVER['SERVER_NAME']);
  219. $host = $host[0];
  220. }
  221. return $host;
  222. }
  223. function OX_getHostNameWithPort()
  224. {
  225. if (!empty($_SERVER['HTTP_HOST'])) {
  226. $host = $_SERVER['HTTP_HOST'];
  227. } else if (!empty($_SERVER['SERVER_NAME'])) {
  228. $host = $_SERVER['SERVER_NAME'];
  229. }
  230. return $host;
  231. }
  232. function setupIncludePath()
  233. {
  234. static $checkIfAlreadySet;
  235. if (isset($checkIfAlreadySet)) {
  236. return;
  237. }
  238. $checkIfAlreadySet = true;
  239. $oxPearPath = MAX_PATH . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'pear';
  240. $oxZendPath = MAX_PATH . DIRECTORY_SEPARATOR . 'lib';
  241. set_include_path($oxPearPath . PATH_SEPARATOR . $oxZendPath . PATH_SEPARATOR . get_include_path());
  242. }
  243. OX_increaseMemoryLimit(OX_getMinimumRequiredMemory());
  244. if (!defined('E_DEPRECATED')) {
  245. define('E_DEPRECATED', 0);
  246. }
  247. setupServerVariables();
  248. setupDeliveryConfigVariables();
  249. $conf = $GLOBALS['_MAX']['CONF'];
  250. $GLOBALS['_OA']['invocationType'] = array_search(basename($_SERVER['SCRIPT_FILENAME']), $conf['file']);
  251. if (!empty($conf['debug']['production'])) {
  252. error_reporting(E_ALL & ~(E_NOTICE | E_WARNING | E_DEPRECATED | E_STRICT));
  253. } else {
  254. error_reporting(E_ALL & ~(E_DEPRECATED | E_STRICT));
  255. }
  256. $file = '/lib/max/Delivery/common.php';
  257. $GLOBALS['_MAX']['FILES'][$file] = true;
  258. $file = '/lib/max/Delivery/cookie.php';
  259. $GLOBALS['_MAX']['FILES'][$file] = true;
  260. $GLOBALS['_MAX']['COOKIE']['LIMITATIONS']['arrCappingCookieNames'] = array();
  261. if (!is_callable('MAX_cookieSet')) {
  262. if (!empty($conf['cookie']['plugin']) && is_readable(MAX_PATH . "/plugins/cookieStorage/{$conf['cookie']['plugin']}.delivery.php")) {
  263. include MAX_PATH . "/plugins/cookieStorage/{$conf['cookie']['plugin']}.delivery.php";
  264. } else {
  265. function MAX_cookieSet($name, $value, $expire, $path = '/', $domain = null) { return MAX_cookieClientCookieSet($name, $value, $expire, $path, $domain); }
  266. function MAX_cookieUnset($name) { return MAX_cookieClientCookieUnset($name); }
  267. function MAX_cookieFlush() { return MAX_cookieClientCookieFlush(); }
  268. function MAX_cookieLoad() { return true; }
  269. }
  270. }
  271. function MAX_cookieAdd($name, $value, $expire = 0)
  272. {
  273. if (!isset($GLOBALS['_MAX']['COOKIE']['CACHE'])) {
  274. $GLOBALS['_MAX']['COOKIE']['CACHE'] = array();
  275. }
  276. $GLOBALS['_MAX']['COOKIE']['CACHE'][$name] = array($value, $expire);
  277. }
  278. function MAX_cookieSetViewerIdAndRedirect($viewerId) {
  279. $aConf = $GLOBALS['_MAX']['CONF'];
  280. MAX_cookieAdd($aConf['var']['viewerId'], $viewerId, _getTimeYearFromNow());
  281. MAX_cookieFlush();
  282. if ($GLOBALS['_MAX']['SSL_REQUEST']) {
  283. $url = MAX_commonConstructSecureDeliveryUrl(basename($_SERVER['SCRIPT_NAME']));
  284. } else {
  285. $url = MAX_commonConstructDeliveryUrl(basename($_SERVER['SCRIPT_NAME']));
  286. }
  287. $url .= "?{$aConf['var']['cookieTest']}=1&" . $_SERVER['QUERY_STRING'];
  288. MAX_header("Location: {$url}");
  289. exit;
  290. }
  291. function _getTimeThirtyDaysFromNow()
  292. {
  293. return MAX_commonGetTimeNow() + 2592000; }
  294. function _getTimeYearFromNow()
  295. {
  296. return MAX_commonGetTimeNow() + 31536000; }
  297. function _getTimeYearAgo()
  298. {
  299. return MAX_commonGetTimeNow() - 31536000; }
  300. function MAX_cookieUnpackCapping()
  301. {
  302. $conf = $GLOBALS['_MAX']['CONF'];
  303. $cookieNames = $GLOBALS['_MAX']['COOKIE']['LIMITATIONS']['arrCappingCookieNames'];
  304. if (!is_array($cookieNames))
  305. return;
  306. foreach ($cookieNames as $cookieName) {
  307. if (!empty($_COOKIE[$cookieName])) {
  308. if (!is_array($_COOKIE[$cookieName])) {
  309. $output = array();
  310. $data = explode('_', $_COOKIE[$cookieName]);
  311. foreach ($data as $pair) {
  312. list($name, $value) = explode('.', $pair);
  313. $output[$name] = $value;
  314. }
  315. $_COOKIE[$cookieName] = $output;
  316. }
  317. }
  318. if (!empty($_COOKIE['_' . $cookieName]) && is_array($_COOKIE['_' . $cookieName])) {
  319. foreach ($_COOKIE['_' . $cookieName] as $adId => $cookie) {
  320. if (_isBlockCookie($cookieName)) {
  321. $_COOKIE[$cookieName][$adId] = $cookie;
  322. } else {
  323. if (isset($_COOKIE[$cookieName][$adId])) {
  324. $_COOKIE[$cookieName][$adId] += $cookie;
  325. } else {
  326. $_COOKIE[$cookieName][$adId] = $cookie;
  327. }
  328. }
  329. MAX_cookieUnset("_{$cookieName}[{$adId}]");
  330. }
  331. }
  332. }
  333. }
  334. function _isBlockCookie($cookieName)
  335. {
  336. return in_array($cookieName, array(
  337. $GLOBALS['_MAX']['CONF']['var']['blockAd'],
  338. $GLOBALS['_MAX']['CONF']['var']['blockCampaign'],
  339. $GLOBALS['_MAX']['CONF']['var']['blockZone'],
  340. $GLOBALS['_MAX']['CONF']['var']['lastView'],
  341. $GLOBALS['_MAX']['CONF']['var']['lastClick'],
  342. $GLOBALS['_MAX']['CONF']['var']['blockLoggingClick'],
  343. ));
  344. }
  345. function MAX_cookieGetUniqueViewerId($create = true)
  346. {
  347. static $uniqueViewerId = null;
  348. if(!is_null($uniqueViewerId)) {
  349. return $uniqueViewerId;
  350. }
  351. $conf = $GLOBALS['_MAX']['CONF'];
  352. if (isset($_COOKIE[$conf['var']['viewerId']])) {
  353. $uniqueViewerId = $_COOKIE[$conf['var']['viewerId']];
  354. } elseif ($create) {
  355. $uniqueViewerId = md5(uniqid('', true)); $GLOBALS['_MAX']['COOKIE']['newViewerId'] = true;
  356. }
  357. return $uniqueViewerId;
  358. }
  359. function MAX_cookieGetCookielessViewerID()
  360. {
  361. if (empty($_SERVER['REMOTE_ADDR']) || empty($_SERVER['HTTP_USER_AGENT'])) {
  362. return '';
  363. }
  364. $cookiePrefix = $GLOBALS['_MAX']['MAX_COOKIELESS_PREFIX'];
  365. return $cookiePrefix . substr(md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']), 0, 32-(strlen($cookiePrefix)));
  366. }
  367. function MAX_Delivery_cookie_cappingOnRequest()
  368. {
  369. if (isset($GLOBALS['_OA']['invocationType']) &&
  370. ($GLOBALS['_OA']['invocationType'] == 'xmlrpc' || $GLOBALS['_OA']['invocationType'] == 'view')
  371. ) {
  372. return true;
  373. }
  374. return !$GLOBALS['_MAX']['CONF']['logging']['adImpressions'];
  375. }
  376. function MAX_Delivery_cookie_setCapping($type, $id, $block = 0, $cap = 0, $sessionCap = 0)
  377. {
  378. $conf = $GLOBALS['_MAX']['CONF'];
  379. $setBlock = false;
  380. if ($cap > 0) {
  381. $expire = MAX_commonGetTimeNow() + $conf['cookie']['permCookieSeconds'];
  382. if (!isset($_COOKIE[$conf['var']['cap' . $type]][$id])) {
  383. $value = 1;
  384. $setBlock = true;
  385. } else if ($_COOKIE[$conf['var']['cap' . $type]][$id] >= $cap) {
  386. $value = -$_COOKIE[$conf['var']['cap' . $type]][$id]+1;
  387. $setBlock = true;
  388. } else {
  389. $value = 1;
  390. }
  391. MAX_cookieAdd("_{$conf['var']['cap' . $type]}[{$id}]", $value, $expire);
  392. }
  393. if ($sessionCap > 0) {
  394. if (!isset($_COOKIE[$conf['var']['sessionCap' . $type]][$id])) {
  395. $value = 1;
  396. $setBlock = true;
  397. } else if ($_COOKIE[$conf['var']['sessionCap' . $type]][$id] >= $sessionCap) {
  398. $value = -$_COOKIE[$conf['var']['sessionCap' . $type]][$id]+1;
  399. $setBlock = true;
  400. } else {
  401. $value = 1;
  402. }
  403. MAX_cookieAdd("_{$conf['var']['sessionCap' . $type]}[{$id}]", $value, 0);
  404. }
  405. if ($block > 0 || $setBlock) {
  406. MAX_cookieAdd("_{$conf['var']['block' . $type]}[{$id}]", MAX_commonGetTimeNow(), _getTimeThirtyDaysFromNow());
  407. }
  408. }
  409. function MAX_cookieClientCookieSet($name, $value, $expire, $path = '/', $domain = null)
  410. {
  411. if (isset($GLOBALS['_OA']['invocationType']) && $GLOBALS['_OA']['invocationType'] == 'xmlrpc') {
  412. if (!isset($GLOBALS['_OA']['COOKIE']['XMLRPC_CACHE'])) {
  413. $GLOBALS['_OA']['COOKIE']['XMLRPC_CACHE'] = array();
  414. }
  415. $GLOBALS['_OA']['COOKIE']['XMLRPC_CACHE'][$name] = array($value, $expire);
  416. } else {
  417. @setcookie($name, $value, $expire, $path, $domain);
  418. }
  419. }
  420. function MAX_cookieClientCookieUnset($name)
  421. {
  422. $conf = $GLOBALS['_MAX']['CONF'];
  423. $domain = (!empty($conf['cookie']['domain'])) ? $conf['cookie']['domain'] : null;
  424. MAX_cookieSet($name, false, _getTimeYearAgo(), '/', $domain);
  425. MAX_cookieSet(str_replace('_', '%5F', urlencode($name)), false, _getTimeYearAgo(), '/', $domain);
  426. }
  427. function MAX_cookieClientCookieFlush()
  428. {
  429. $conf = $GLOBALS['_MAX']['CONF'];
  430. MAX_cookieSendP3PHeaders();
  431. if (!empty($GLOBALS['_MAX']['COOKIE']['CACHE'])) {
  432. reset($GLOBALS['_MAX']['COOKIE']['CACHE']);
  433. while (list($name,$v) = each ($GLOBALS['_MAX']['COOKIE']['CACHE'])) {
  434. list($value, $expire) = $v;
  435. if ($name == $conf['var']['viewerId']) {
  436. MAX_cookieClientCookieSet($name, $value, $expire, '/', (!empty($conf['cookie']['domain']) ? $conf['cookie']['domain'] : null));
  437. } else {
  438. MAX_cookieSet($name, $value, $expire, '/', (!empty($conf['cookie']['domain']) ? $conf['cookie']['domain'] : null));
  439. }
  440. }
  441. $GLOBALS['_MAX']['COOKIE']['CACHE'] = array();
  442. }
  443. $cookieNames = $GLOBALS['_MAX']['COOKIE']['LIMITATIONS']['arrCappingCookieNames'];
  444. if (!is_array($cookieNames))
  445. return;
  446. $maxCookieSize = !empty($conf['cookie']['maxCookieSize']) ? $conf['cookie']['maxCookieSize'] : 2048;
  447. foreach ($cookieNames as $cookieName) {
  448. if (empty($_COOKIE["_{$cookieName}"])) {
  449. continue;
  450. }
  451. switch ($cookieName) {
  452. case $conf['var']['blockAd'] :
  453. case $conf['var']['blockCampaign'] :
  454. case $conf['var']['blockZone'] : $expire = _getTimeThirtyDaysFromNow(); break;
  455. case $conf['var']['lastClick'] :
  456. case $conf['var']['lastView'] :
  457. case $conf['var']['capAd'] :
  458. case $conf['var']['capCampaign'] :
  459. case $conf['var']['capZone'] : $expire = _getTimeYearFromNow(); break;
  460. case $conf['var']['sessionCapCampaign'] :
  461. case $conf['var']['sessionCapAd'] :
  462. case $conf['var']['sessionCapZone'] : $expire = 0; break;
  463. }
  464. if (!empty($_COOKIE[$cookieName]) && is_array($_COOKIE[$cookieName])) {
  465. $data = array();
  466. foreach ($_COOKIE[$cookieName] as $adId => $value) {
  467. $data[] = "{$adId}.{$value}";
  468. }
  469. while (strlen(implode('_', $data)) > $maxCookieSize) {
  470. $data = array_slice($data, 1);
  471. }
  472. MAX_cookieSet($cookieName, implode('_', $data), $expire, '/', (!empty($conf['cookie']['domain']) ? $conf['cookie']['domain'] : null));
  473. }
  474. }
  475. }
  476. function MAX_cookieSendP3PHeaders() {
  477. if ($GLOBALS['_MAX']['CONF']['p3p']['policies']) {
  478. MAX_header("P3P: ". _generateP3PHeader());
  479. }
  480. }
  481. function _generateP3PHeader()
  482. {
  483. $conf = $GLOBALS['_MAX']['CONF'];
  484. $p3p_header = '';
  485. if ($conf['p3p']['policies']) {
  486. if ($conf['p3p']['policyLocation'] != '') {
  487. $p3p_header .= " policyref=\"".$conf['p3p']['policyLocation']."\"";
  488. }
  489. if ($conf['p3p']['policyLocation'] != '' && $conf['p3p']['compactPolicy'] != '') {
  490. $p3p_header .= ", ";
  491. }
  492. if ($conf['p3p']['compactPolicy'] != '') {
  493. $p3p_header .= " CP=\"".$conf['p3p']['compactPolicy']."\"";
  494. }
  495. }
  496. return $p3p_header;
  497. }
  498. $file = '/lib/max/Delivery/remotehost.php';
  499. $GLOBALS['_MAX']['FILES'][$file] = true;
  500. function MAX_remotehostSetInfo($run = false)
  501. {
  502. if (empty($GLOBALS['_OA']['invocationType']) || $run || ($GLOBALS['_OA']['invocationType'] != 'xmlrpc')) {
  503. MAX_remotehostProxyLookup();
  504. MAX_remotehostReverseLookup();
  505. MAX_remotehostSetGeoInfo();
  506. }
  507. }
  508. function MAX_remotehostProxyLookup()
  509. {
  510. $conf = $GLOBALS['_MAX']['CONF'];
  511. if ($conf['logging']['proxyLookup']) {
  512. OX_Delivery_logMessage('checking remote host proxy', 7);
  513. $proxy = false;
  514. if (!empty($_SERVER['HTTP_VIA']) || !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  515. $proxy = true;
  516. } elseif (!empty($_SERVER['REMOTE_HOST'])) {
  517. $aProxyHosts = array(
  518. 'proxy',
  519. 'cache',
  520. 'inktomi'
  521. );
  522. foreach ($aProxyHosts as $proxyName) {
  523. if (strpos($_SERVER['REMOTE_HOST'], $proxyName) !== false) {
  524. $proxy = true;
  525. break;
  526. }
  527. }
  528. }
  529. if ($proxy) {
  530. OX_Delivery_logMessage('proxy detected', 7);
  531. $aHeaders = array(
  532. 'HTTP_FORWARDED',
  533. 'HTTP_FORWARDED_FOR',
  534. 'HTTP_X_FORWARDED',
  535. 'HTTP_X_FORWARDED_FOR',
  536. 'HTTP_CLIENT_IP'
  537. );
  538. foreach ($aHeaders as $header) {
  539. if (!empty($_SERVER[$header])) {
  540. $ip = $_SERVER[$header];
  541. break;
  542. }
  543. }
  544. if (!empty($ip)) {
  545. foreach (explode(',', $ip) as $ip) {
  546. $ip = trim($ip);
  547. if (($ip != 'unknown') && (!MAX_remotehostPrivateAddress($ip))) {
  548. $_SERVER['REMOTE_ADDR'] = $ip;
  549. $_SERVER['REMOTE_HOST'] = '';
  550. $_SERVER['HTTP_VIA'] = '';
  551. OX_Delivery_logMessage('real address set to '.$ip, 7);
  552. break;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. }
  559. function MAX_remotehostReverseLookup()
  560. {
  561. if (empty($_SERVER['REMOTE_HOST'])) {
  562. if ($GLOBALS['_MAX']['CONF']['logging']['reverseLookup']) {
  563. $_SERVER['REMOTE_HOST'] = @gethostbyaddr($_SERVER['REMOTE_ADDR']);
  564. } else {
  565. $_SERVER['REMOTE_HOST'] = $_SERVER['REMOTE_ADDR'];
  566. }
  567. }
  568. }
  569. function MAX_remotehostSetGeoInfo()
  570. {
  571. if (!function_exists('parseDeliveryIniFile')) {
  572. }
  573. $aConf = $GLOBALS['_MAX']['CONF'];
  574. $type = (!empty($aConf['geotargeting']['type'])) ? $aConf['geotargeting']['type'] : null;
  575. if (!is_null($type) && $type != 'none') {
  576. $aComponent = explode(':', $aConf['geotargeting']['type']);
  577. if (!empty($aComponent[1]) && (!empty($aConf['pluginGroupComponents'][$aComponent[1]]))) {
  578. $GLOBALS['_MAX']['CLIENT_GEO'] = OX_Delivery_Common_hook('getGeoInfo', array(), $type);
  579. }
  580. }
  581. }
  582. function MAX_remotehostPrivateAddress($ip)
  583. {
  584. $ip = ip2long($ip);
  585. if (!$ip) return false;
  586. return (MAX_remotehostMatchSubnet($ip, '10.0.0.0', 8) ||
  587. MAX_remotehostMatchSubnet($ip, '172.16.0.0', 12) ||
  588. MAX_remotehostMatchSubnet($ip, '192.168.0.0', 16) ||
  589. MAX_remotehostMatchSubnet($ip, '127.0.0.0', 8)
  590. );
  591. }
  592. function MAX_remotehostMatchSubnet($ip, $net, $mask)
  593. {
  594. $net = ip2long($net);
  595. if (!is_integer($ip)) {
  596. $ip = ip2long($ip);
  597. }
  598. if (!$ip || !$net) {
  599. return false;
  600. }
  601. if (is_integer($mask)) {
  602. if ($mask > 32 || $mask <= 0)
  603. return false;
  604. elseif ($mask == 32)
  605. $mask = ~0;
  606. else
  607. $mask = ~((1 << (32 - $mask)) - 1);
  608. } elseif (!($mask = ip2long($mask))) {
  609. return false;
  610. }
  611. return ($ip & $mask) == ($net & $mask) ? true : false;
  612. }
  613. $file = '/lib/max/Delivery/log.php';
  614. $GLOBALS['_MAX']['FILES'][$file] = true;
  615. $file = '/lib/max/Dal/Delivery.php';
  616. $GLOBALS['_MAX']['FILES'][$file] = true;
  617. $file = '/lib/OA/Dal/Delivery.php';
  618. $GLOBALS['_MAX']['FILES'][$file] = true;
  619. function OA_Dal_Delivery_getAccountTZs()
  620. {
  621. $aConf = $GLOBALS['_MAX']['CONF'];
  622. $query = "
  623. SELECT
  624. value
  625. FROM
  626. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['application_variable'])."
  627. WHERE
  628. name = 'admin_account_id'
  629. ";
  630. $res = OA_Dal_Delivery_query($query);
  631. if (is_resource($res) && OA_Dal_Delivery_numRows($res)) {
  632. $adminAccountId = (int)OA_Dal_Delivery_result($res, 0, 0);
  633. } else {
  634. $adminAccountId = false;
  635. }
  636. $query = "
  637. SELECT
  638. a.account_id AS account_id,
  639. apa.value AS timezone
  640. FROM
  641. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['accounts'])." AS a JOIN
  642. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['account_preference_assoc'])." AS apa ON (apa.account_id = a.account_id) JOIN
  643. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['preferences'])." AS p ON (p.preference_id = apa.preference_id)
  644. WHERE
  645. a.account_type IN ('ADMIN', 'MANAGER') AND
  646. p.preference_name = 'timezone'
  647. ";
  648. $res = OA_Dal_Delivery_query($query);
  649. $aResult = array(
  650. 'adminAccountId' => $adminAccountId,
  651. 'aAccounts' => array()
  652. );
  653. if (is_resource($res)) {
  654. while ($row = OA_Dal_Delivery_fetchAssoc($res)) {
  655. $accountId = (int)$row['account_id'];
  656. if ($accountId === $adminAccountId) {
  657. $aResult['default'] = $row['timezone'];
  658. } else {
  659. $aResult['aAccounts'][$accountId] = $row['timezone'];
  660. }
  661. }
  662. }
  663. if (empty($aResult['default'])) {
  664. $aResult['default'] = 'UTC';
  665. }
  666. return $aResult;
  667. }
  668. function OA_Dal_Delivery_getZoneInfo($zoneid) {
  669. $aConf = $GLOBALS['_MAX']['CONF'];
  670. $zoneid = (int)$zoneid;
  671. $query = "
  672. SELECT
  673. z.zoneid AS zone_id,
  674. z.zonename AS name,
  675. z.delivery AS type,
  676. z.description AS description,
  677. z.width AS width,
  678. z.height AS height,
  679. z.chain AS chain,
  680. z.prepend AS prepend,
  681. z.append AS append,
  682. z.appendtype AS appendtype,
  683. z.forceappend AS forceappend,
  684. z.inventory_forecast_type AS inventory_forecast_type,
  685. z.block AS block_zone,
  686. z.capping AS cap_zone,
  687. z.session_capping AS session_cap_zone,
  688. z.show_capped_no_cookie AS show_capped_no_cookie_zone,
  689. z.ext_adselection AS ext_adselection,
  690. z.affiliateid AS publisher_id,
  691. a.agencyid AS agency_id,
  692. a.account_id AS trafficker_account_id,
  693. m.account_id AS manager_account_id
  694. FROM
  695. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['zones'])." AS z,
  696. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['affiliates'])." AS a,
  697. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['agency'])." AS m
  698. WHERE
  699. z.zoneid = {$zoneid}
  700. AND
  701. z.affiliateid = a.affiliateid
  702. AND
  703. a.agencyid = m.agencyid";
  704. $rZoneInfo = OA_Dal_Delivery_query($query);
  705. if (!is_resource($rZoneInfo)) {
  706. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : false;
  707. }
  708. $aZoneInfo = OA_Dal_Delivery_fetchAssoc($rZoneInfo);
  709. if (empty($aZoneInfo)) {
  710. return false;
  711. }
  712. $query = "
  713. SELECT
  714. p.preference_id AS preference_id,
  715. p.preference_name AS preference_name
  716. FROM
  717. {$aConf['table']['prefix']}{$aConf['table']['preferences']} AS p
  718. WHERE
  719. p.preference_name = 'default_banner_image_url'
  720. OR
  721. p.preference_name = 'default_banner_destination_url'";
  722. $rPreferenceInfo = OA_Dal_Delivery_query($query);
  723. if (!is_resource($rPreferenceInfo)) {
  724. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : false;
  725. }
  726. if (OA_Dal_Delivery_numRows($rPreferenceInfo) != 2) {
  727. return $aZoneInfo;
  728. }
  729. $aPreferenceInfo = OA_Dal_Delivery_fetchAssoc($rPreferenceInfo);
  730. $variableName = $aPreferenceInfo['preference_name'] . '_id';
  731. $$variableName = $aPreferenceInfo['preference_id'];
  732. $aPreferenceInfo = OA_Dal_Delivery_fetchAssoc($rPreferenceInfo);
  733. $variableName = $aPreferenceInfo['preference_name'] . '_id';
  734. $$variableName = $aPreferenceInfo['preference_id'];
  735. $query = "
  736. SELECT
  737. 'default_banner_destination_url_trafficker' AS item,
  738. apa.value AS value
  739. FROM
  740. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['account_preference_assoc'])." AS apa
  741. WHERE
  742. apa.account_id = {$aZoneInfo['trafficker_account_id']}
  743. AND
  744. apa.preference_id = $default_banner_destination_url_id
  745. UNION
  746. SELECT
  747. 'default_banner_destination_url_manager' AS item,
  748. apa.value AS value
  749. FROM
  750. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['account_preference_assoc'])." AS apa
  751. WHERE
  752. apa.account_id = {$aZoneInfo['manager_account_id']}
  753. AND
  754. apa.preference_id = $default_banner_destination_url_id
  755. UNION
  756. SELECT
  757. 'default_banner_image_url_trafficker' AS item,
  758. apa.value AS value
  759. FROM
  760. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['account_preference_assoc'])." AS apa
  761. WHERE
  762. apa.account_id = {$aZoneInfo['trafficker_account_id']}
  763. AND
  764. apa.preference_id = $default_banner_image_url_id
  765. UNION
  766. SELECT
  767. 'default_banner_image_url_manager' AS item,
  768. apa.value AS value
  769. FROM
  770. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['account_preference_assoc'])." AS apa
  771. WHERE
  772. apa.account_id = {$aZoneInfo['manager_account_id']}
  773. AND
  774. apa.preference_id = $default_banner_image_url_id
  775. UNION
  776. SELECT
  777. 'default_banner_image_url_admin' AS item,
  778. apa.value AS value
  779. FROM
  780. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['account_preference_assoc'])." AS apa,
  781. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['accounts'])." AS a
  782. WHERE
  783. apa.account_id = a.account_id
  784. AND
  785. a.account_type = 'ADMIN'
  786. AND
  787. apa.preference_id = $default_banner_image_url_id
  788. UNION
  789. SELECT
  790. 'default_banner_destination_url_admin' AS item,
  791. apa.value AS value
  792. FROM
  793. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['account_preference_assoc'])." AS apa,
  794. ".OX_escapeIdentifier($aConf['table']['prefix'].$aConf['table']['accounts'])." AS a
  795. WHERE
  796. apa.account_id = a.account_id
  797. AND
  798. a.account_type = 'ADMIN'
  799. AND
  800. apa.preference_id = $default_banner_destination_url_id";
  801. $rDefaultBannerInfo = OA_Dal_Delivery_query($query);
  802. if (!is_resource($rDefaultBannerInfo)) {
  803. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : false;
  804. }
  805. if (OA_Dal_Delivery_numRows($rDefaultBannerInfo) == 0) {
  806. if ($aConf['defaultBanner']['imageUrl'] != '') {
  807. $aZoneInfo['default_banner_image_url'] = $aConf['defaultBanner']['imageUrl'];
  808. }
  809. return $aZoneInfo;
  810. }
  811. $aDefaultImageURLs = array();
  812. $aDefaultDestinationURLs = array();
  813. while ($aRow = OA_Dal_Delivery_fetchAssoc($rDefaultBannerInfo)) {
  814. if (stristr($aRow['item'], 'default_banner_image_url')) {
  815. $aDefaultImageURLs[$aRow['item']] = $aRow['value'];
  816. } else if (stristr($aRow['item'], 'default_banner_destination_url')) {
  817. $aDefaultDestinationURLs[$aRow['item']] = $aRow['value'];
  818. }
  819. }
  820. $aTypes = array(
  821. 0 => 'admin',
  822. 1 => 'manager',
  823. 2 => 'trafficker'
  824. );
  825. foreach ($aTypes as $type) {
  826. if (isset($aDefaultImageURLs['default_banner_image_url_' . $type])) {
  827. $aZoneInfo['default_banner_image_url'] = $aDefaultImageURLs['default_banner_image_url_' . $type];
  828. }
  829. if (isset($aDefaultDestinationURLs['default_banner_destination_url_' . $type])) {
  830. $aZoneInfo['default_banner_destination_url'] = $aDefaultDestinationURLs['default_banner_destination_url_' . $type];
  831. }
  832. }
  833. return $aZoneInfo;
  834. }
  835. function OA_Dal_Delivery_getPublisherZones($publisherid) {
  836. $conf = $GLOBALS['_MAX']['CONF'];
  837. $publisherid = (int)$publisherid;
  838. $rZones = OA_Dal_Delivery_query("
  839. SELECT
  840. z.zoneid AS zone_id,
  841. z.affiliateid AS publisher_id,
  842. z.zonename AS name,
  843. z.delivery AS type
  844. FROM
  845. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['zones'])." AS z
  846. WHERE
  847. z.affiliateid={$publisherid}
  848. ");
  849. if (!is_resource($rZones)) {
  850. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : false;
  851. }
  852. while ($aZone = OA_Dal_Delivery_fetchAssoc($rZones)) {
  853. $aZones[$aZone['zone_id']] = $aZone;
  854. }
  855. return ($aZones);
  856. }
  857. function OA_Dal_Delivery_getZoneLinkedAds($zoneid) {
  858. $conf = $GLOBALS['_MAX']['CONF'];
  859. $zoneid = (int)$zoneid;
  860. $aRows = OA_Dal_Delivery_getZoneInfo($zoneid);
  861. $aRows['xAds'] = array();
  862. $aRows['ads'] = array();
  863. $aRows['lAds'] = array();
  864. $aRows['eAds'] = array();
  865. $aRows['count_active'] = 0;
  866. $aRows['zone_companion'] = false;
  867. $aRows['count_active'] = 0;
  868. $totals = array(
  869. 'xAds' => 0,
  870. 'ads' => 0,
  871. 'lAds' => 0
  872. );
  873. $query = "
  874. SELECT
  875. d.bannerid AS ad_id,
  876. d.campaignid AS placement_id,
  877. d.status AS status,
  878. d.description AS name,
  879. d.storagetype AS type,
  880. d.contenttype AS contenttype,
  881. d.pluginversion AS pluginversion,
  882. d.filename AS filename,
  883. d.imageurl AS imageurl,
  884. d.htmltemplate AS htmltemplate,
  885. d.htmlcache AS htmlcache,
  886. d.width AS width,
  887. d.height AS height,
  888. d.weight AS weight,
  889. d.seq AS seq,
  890. d.target AS target,
  891. d.url AS url,
  892. d.alt AS alt,
  893. d.statustext AS statustext,
  894. d.bannertext AS bannertext,
  895. d.adserver AS adserver,
  896. d.block AS block_ad,
  897. d.capping AS cap_ad,
  898. d.session_capping AS session_cap_ad,
  899. d.compiledlimitation AS compiledlimitation,
  900. d.acl_plugins AS acl_plugins,
  901. d.prepend AS prepend,
  902. d.append AS append,
  903. d.bannertype AS bannertype,
  904. d.alt_filename AS alt_filename,
  905. d.alt_imageurl AS alt_imageurl,
  906. d.alt_contenttype AS alt_contenttype,
  907. d.parameters AS parameters,
  908. d.transparent AS transparent,
  909. d.ext_bannertype AS ext_bannertype,
  910. az.priority AS priority,
  911. az.priority_factor AS priority_factor,
  912. az.to_be_delivered AS to_be_delivered,
  913. c.campaignid AS campaign_id,
  914. c.priority AS campaign_priority,
  915. c.weight AS campaign_weight,
  916. c.companion AS campaign_companion,
  917. c.block AS block_campaign,
  918. c.capping AS cap_campaign,
  919. c.session_capping AS session_cap_campaign,
  920. c.show_capped_no_cookie AS show_capped_no_cookie,
  921. c.clientid AS client_id,
  922. c.expire_time AS expire_time,
  923. c.revenue_type AS revenue_type,
  924. c.ecpm_enabled AS ecpm_enabled,
  925. c.ecpm AS ecpm,
  926. c.clickwindow AS clickwindow,
  927. c.viewwindow AS viewwindow,
  928. m.advertiser_limitation AS advertiser_limitation,
  929. a.account_id AS account_id,
  930. z.affiliateid AS affiliate_id,
  931. a.agencyid as agency_id
  932. FROM
  933. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['banners'])." AS d JOIN
  934. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['ad_zone_assoc'])." AS az ON (d.bannerid = az.ad_id) JOIN
  935. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['zones'])." AS z ON (az.zone_id = z.zoneid) JOIN
  936. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['campaigns'])." AS c ON (c.campaignid = d.campaignid) LEFT JOIN
  937. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['clients'])." AS m ON (m.clientid = c.clientid) LEFT JOIN
  938. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['agency'])." AS a ON (a.agencyid = m.agencyid)
  939. WHERE
  940. az.zone_id = {$zoneid}
  941. AND
  942. d.status <= 0
  943. AND
  944. c.status <= 0
  945. ";
  946. $rAds = OA_Dal_Delivery_query($query);
  947. if (!is_resource($rAds)) {
  948. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  949. }
  950. $aConversionLinkedCreatives = MAX_cacheGetTrackerLinkedCreatives();
  951. while ($aAd = OA_Dal_Delivery_fetchAssoc($rAds)) {
  952. $aAd['tracker_status'] = (!empty($aConversionLinkedCreatives[$aAd['ad_id']]['status'])) ? $aConversionLinkedCreatives[$aAd['ad_id']]['status'] : null;
  953. if ($aAd['campaign_priority'] == -1) {
  954. $aRows['xAds'][$aAd['ad_id']] = $aAd;
  955. $aRows['count_active']++;
  956. } elseif ($aAd['campaign_priority'] == 0) {
  957. $aRows['lAds'][$aAd['ad_id']] = $aAd;
  958. $aRows['count_active']++;
  959. } elseif ($aAd['campaign_priority'] == -2) {
  960. $aRows['eAds'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  961. $aRows['count_active']++;
  962. } else {
  963. $aRows['ads'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  964. $aRows['count_active']++;
  965. }
  966. if ($aAd['campaign_companion'] == 1) {
  967. $aRows['zone_companion'][] = $aAd['placement_id'];
  968. }
  969. }
  970. if (is_array($aRows['xAds'])) {
  971. $totals['xAds'] = _setPriorityFromWeights($aRows['xAds']);
  972. }
  973. if (is_array($aRows['ads'])) {
  974. $totals['ads'] = _getTotalPrioritiesByCP($aRows['ads']);
  975. }
  976. if (is_array($aRows['eAds'])) {
  977. $totals['eAds'] = _getTotalPrioritiesByCP($aRows['eAds']);
  978. }
  979. if (is_array($aRows['lAds'])) {
  980. $totals['lAds'] = _setPriorityFromWeights($aRows['lAds']);
  981. }
  982. $aRows['priority'] = $totals;
  983. return $aRows;
  984. }
  985. function OA_Dal_Delivery_getZoneLinkedAdInfos($zoneid) {
  986. $conf = $GLOBALS['_MAX']['CONF'];
  987. $zoneid = (int)$zoneid;
  988. $aRows['xAds'] = array();
  989. $aRows['ads'] = array();
  990. $aRows['lAds'] = array();
  991. $aRows['eAds'] = array();
  992. $aRows['zone_companion'] = false;
  993. $aRows['count_active'] = 0;
  994. $query =
  995. "SELECT "
  996. ."d.bannerid AS ad_id, " ."d.campaignid AS placement_id, " ."d.status AS status, " ."d.width AS width, "
  997. ."d.ext_bannertype AS ext_bannertype, "
  998. ."d.height AS height, "
  999. ."d.storagetype AS type, " ."d.contenttype AS contenttype, " ."d.weight AS weight, " ."d.adserver AS adserver, " ."d.block AS block_ad, " ."d.capping AS cap_ad, " ."d.session_capping AS session_cap_ad, " ."d.compiledlimitation AS compiledlimitation, " ."d.acl_plugins AS acl_plugins, " ."d.alt_filename AS alt_filename, " ."az.priority AS priority, " ."az.priority_factor AS priority_factor, " ."az.to_be_delivered AS to_be_delivered, " ."c.campaignid AS campaign_id, " ."c.priority AS campaign_priority, " ."c.weight AS campaign_weight, " ."c.companion AS campaign_companion, " ."c.block AS block_campaign, " ."c.capping AS cap_campaign, " ."c.session_capping AS session_cap_campaign, " ."c.show_capped_no_cookie AS show_capped_no_cookie, "
  1000. ."c.clientid AS client_id, " ."c.expire_time AS expire_time, "
  1001. ."c.revenue_type AS revenue_type, "
  1002. ."c.ecpm_enabled AS ecpm_enabled, "
  1003. ."c.ecpm AS ecpm, "
  1004. ."ct.status AS tracker_status, "
  1005. .OX_Dal_Delivery_regex("d.htmlcache", "src\\s?=\\s?[\\'\"]http:")." AS html_ssl_unsafe, "
  1006. .OX_Dal_Delivery_regex("d.imageurl", "^http:")." AS url_ssl_unsafe "
  1007. ."FROM "
  1008. .OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['banners'])." AS d JOIN "
  1009. .OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['ad_zone_assoc'])." AS az ON (d.bannerid = az.ad_id) JOIN "
  1010. .OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['campaigns'])." AS c ON (c.campaignid = d.campaignid) LEFT JOIN "
  1011. .OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['campaigns_trackers'])." AS ct ON (ct.campaignid = c.campaignid) "
  1012. ."WHERE "
  1013. ."az.zone_id = {$zoneid} "
  1014. ."AND "
  1015. ."d.status <= 0 "
  1016. ."AND "
  1017. ."c.status <= 0 ";
  1018. $rAds = OA_Dal_Delivery_query($query);
  1019. if (!is_resource($rAds)) {
  1020. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1021. }
  1022. while ($aAd = OA_Dal_Delivery_fetchAssoc($rAds)) {
  1023. if ($aAd['campaign_priority'] == -1) {
  1024. $aRows['xAds'][$aAd['ad_id']] = $aAd;
  1025. $aRows['count_active']++;
  1026. } elseif ($aAd['campaign_priority'] == 0) {
  1027. $aRows['lAds'][$aAd['ad_id']] = $aAd;
  1028. $aRows['count_active']++;
  1029. } elseif ($aAd['campaign_priority'] == -2) {
  1030. $aRows['eAds'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  1031. $aRows['count_active']++;
  1032. } else {
  1033. $aRows['ads'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  1034. $aRows['count_active']++;
  1035. }
  1036. if ($aAd['campaign_companion'] == 1) {
  1037. $aRows['zone_companion'][] = $aAd['placement_id']; }
  1038. }
  1039. return $aRows;
  1040. }
  1041. function OA_Dal_Delivery_getLinkedAdInfos($search, $campaignid = '', $lastpart = true) {
  1042. $conf = $GLOBALS['_MAX']['CONF'];
  1043. $campaignid = (int)$campaignid;
  1044. if ($campaignid > 0) {
  1045. $precondition = " AND d.campaignid = '".$campaignid."' ";
  1046. } else {
  1047. $precondition = '';
  1048. }
  1049. $aRows['xAds'] = array();
  1050. $aRows['ads'] = array();
  1051. $aRows['lAds'] = array();
  1052. $aRows['count_active'] = 0;
  1053. $aRows['zone_companion'] = false;
  1054. $aRows['count_active'] = 0;
  1055. $totals = array(
  1056. 'xAds' => 0,
  1057. 'ads' => 0,
  1058. 'lAds' => 0
  1059. );
  1060. $query = OA_Dal_Delivery_buildAdInfoQuery($search, $lastpart, $precondition);
  1061. $rAds = OA_Dal_Delivery_query($query);
  1062. if (!is_resource($rAds)) {
  1063. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1064. }
  1065. while ($aAd = OA_Dal_Delivery_fetchAssoc($rAds)) {
  1066. if ($aAd['campaign_priority'] == -1) {
  1067. $aAd['priority'] = $aAd['campaign_weight'] * $aAd['weight'];
  1068. $aRows['xAds'][$aAd['ad_id']] = $aAd;
  1069. $aRows['count_active']++;
  1070. $totals['xAds'] += $aAd['priority'];
  1071. } elseif ($aAd['campaign_priority'] == 0) {
  1072. $aAd['priority'] = $aAd['campaign_weight'] * $aAd['weight'];
  1073. $aRows['lAds'][$aAd['ad_id']] = $aAd;
  1074. $aRows['count_active']++;
  1075. $totals['lAds'] += $aAd['priority'];
  1076. } elseif ($aAd['campaign_priority'] == -2) {
  1077. $aRows['eAds'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  1078. $aRows['count_active']++;
  1079. } else {
  1080. $aRows['ads'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  1081. $aRows['count_active']++;
  1082. }
  1083. }
  1084. return $aRows;
  1085. }
  1086. function OA_Dal_Delivery_getLinkedAds($search, $campaignid = '', $lastpart = true) {
  1087. $conf = $GLOBALS['_MAX']['CONF'];
  1088. $campaignid = (int)$campaignid;
  1089. if ($campaignid > 0) {
  1090. $precondition = " AND d.campaignid = '".$campaignid."' ";
  1091. } else {
  1092. $precondition = '';
  1093. }
  1094. $aRows['xAds'] = array();
  1095. $aRows['ads'] = array();
  1096. $aRows['lAds'] = array();
  1097. $aRows['count_active'] = 0;
  1098. $aRows['zone_companion'] = false;
  1099. $aRows['count_active'] = 0;
  1100. $totals = array(
  1101. 'xAds' => 0,
  1102. 'ads' => 0,
  1103. 'lAds' => 0
  1104. );
  1105. $query = OA_Dal_Delivery_buildQuery($search, $lastpart, $precondition);
  1106. $rAds = OA_Dal_Delivery_query($query);
  1107. if (!is_resource($rAds)) {
  1108. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1109. }
  1110. $aConversionLinkedCreatives = MAX_cacheGetTrackerLinkedCreatives();
  1111. while ($aAd = OA_Dal_Delivery_fetchAssoc($rAds)) {
  1112. $aAd['tracker_status'] = (!empty($aConversionLinkedCreatives[$aAd['ad_id']]['status'])) ? $aConversionLinkedCreatives[$aAd['ad_id']]['status'] : null;
  1113. if ($aAd['campaign_priority'] == -1) {
  1114. $aAd['priority'] = $aAd['campaign_weight'] * $aAd['weight'];
  1115. $aRows['xAds'][$aAd['ad_id']] = $aAd;
  1116. $aRows['count_active']++;
  1117. $totals['xAds'] += $aAd['priority'];
  1118. } elseif ($aAd['campaign_priority'] == 0) {
  1119. $aAd['priority'] = $aAd['campaign_weight'] * $aAd['weight'];
  1120. $aRows['lAds'][$aAd['ad_id']] = $aAd;
  1121. $aRows['count_active']++;
  1122. $totals['lAds'] += $aAd['priority'];
  1123. } elseif ($aAd['campaign_priority'] == -2) {
  1124. $aRows['eAds'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  1125. $aRows['count_active']++;
  1126. } else {
  1127. $aRows['ads'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
  1128. $aRows['count_active']++;
  1129. }
  1130. }
  1131. if (isset($aRows['xAds']) && is_array($aRows['xAds'])) {
  1132. $totals['xAds'] = _setPriorityFromWeights($aRows['xAds']);
  1133. }
  1134. if (isset($aRows['ads']) && is_array($aRows['ads'])) {
  1135. if (isset($aRows['lAds']) && is_array($aRows['lAds']) && count($aRows['lAds']) > 0) {
  1136. $totals['ads'] = _getTotalPrioritiesByCP($aRows['ads'], true);
  1137. } else {
  1138. $totals['ads'] = _getTotalPrioritiesByCP($aRows['ads'], false);
  1139. }
  1140. }
  1141. if (is_array($aRows['eAds'])) {
  1142. $totals['eAds'] = _getTotalPrioritiesByCP($aRows['eAds']);
  1143. }
  1144. if (isset($aRows['lAds']) && is_array($aRows['lAds'])) {
  1145. $totals['lAds'] = _setPriorityFromWeights($aRows['lAds']);
  1146. }
  1147. $aRows['priority'] = $totals;
  1148. return $aRows;
  1149. }
  1150. function OA_Dal_Delivery_getAd($ad_id) {
  1151. $conf = $GLOBALS['_MAX']['CONF'];
  1152. $ad_id = (int)$ad_id;
  1153. $query = "
  1154. SELECT
  1155. d.bannerid AS ad_id,
  1156. d.campaignid AS placement_id,
  1157. d.status AS status,
  1158. d.description AS name,
  1159. d.storagetype AS type,
  1160. d.contenttype AS contenttype,
  1161. d.pluginversion AS pluginversion,
  1162. d.filename AS filename,
  1163. d.imageurl AS imageurl,
  1164. d.htmltemplate AS htmltemplate,
  1165. d.htmlcache AS htmlcache,
  1166. d.width AS width,
  1167. d.height AS height,
  1168. d.weight AS weight,
  1169. d.seq AS seq,
  1170. d.target AS target,
  1171. d.url AS url,
  1172. d.alt AS alt,
  1173. d.statustext AS statustext,
  1174. d.bannertext AS bannertext,
  1175. d.adserver AS adserver,
  1176. d.block AS block_ad,
  1177. d.capping AS cap_ad,
  1178. d.session_capping AS session_cap_ad,
  1179. d.compiledlimitation AS compiledlimitation,
  1180. d.acl_plugins AS acl_plugins,
  1181. d.prepend AS prepend,
  1182. d.append AS append,
  1183. d.bannertype AS bannertype,
  1184. d.alt_filename AS alt_filename,
  1185. d.alt_imageurl AS alt_imageurl,
  1186. d.alt_contenttype AS alt_contenttype,
  1187. d.parameters AS parameters,
  1188. d.transparent AS transparent,
  1189. d.ext_bannertype AS ext_bannertype,
  1190. c.campaignid AS campaign_id,
  1191. c.block AS block_campaign,
  1192. c.capping AS cap_campaign,
  1193. c.session_capping AS session_cap_campaign,
  1194. c.show_capped_no_cookie AS show_capped_no_cookie,
  1195. m.clientid AS client_id,
  1196. c.clickwindow AS clickwindow,
  1197. c.viewwindow AS viewwindow,
  1198. m.advertiser_limitation AS advertiser_limitation,
  1199. m.agencyid AS agency_id
  1200. FROM
  1201. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['banners'])." AS d,
  1202. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['campaigns'])." AS c,
  1203. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['clients'])." AS m
  1204. WHERE
  1205. d.bannerid={$ad_id}
  1206. AND
  1207. d.campaignid = c.campaignid
  1208. AND
  1209. m.clientid = c.clientid
  1210. ";
  1211. $rAd = OA_Dal_Delivery_query($query);
  1212. if (!is_resource($rAd)) {
  1213. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1214. } else {
  1215. return (OA_Dal_Delivery_fetchAssoc($rAd));
  1216. }
  1217. }
  1218. function OA_Dal_Delivery_getChannelLimitations($channelid) {
  1219. $conf = $GLOBALS['_MAX']['CONF'];
  1220. $channelid = (int)$channelid;
  1221. $rLimitation = OA_Dal_Delivery_query("
  1222. SELECT
  1223. acl_plugins,compiledlimitation
  1224. FROM
  1225. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['channel'])."
  1226. WHERE
  1227. channelid={$channelid}");
  1228. if (!is_resource($rLimitation)) {
  1229. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1230. }
  1231. $limitations = OA_Dal_Delivery_fetchAssoc($rLimitation);
  1232. return $limitations;
  1233. }
  1234. function OA_Dal_Delivery_getCreative($filename)
  1235. {
  1236. $conf = $GLOBALS['_MAX']['CONF'];
  1237. $rCreative = OA_Dal_Delivery_query("
  1238. SELECT
  1239. contents,
  1240. t_stamp
  1241. FROM
  1242. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['images'])."
  1243. WHERE
  1244. filename = '".OX_escapeString($filename)."'
  1245. ");
  1246. if (!is_resource($rCreative)) {
  1247. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1248. } else {
  1249. $aResult = OA_Dal_Delivery_fetchAssoc($rCreative);
  1250. $aResult['contents'] = OX_unescapeBlob($aResult['contents']);
  1251. $aResult['t_stamp'] = strtotime($aResult['t_stamp'] . ' GMT');
  1252. return ($aResult);
  1253. }
  1254. }
  1255. function OA_Dal_Delivery_getTracker($trackerid)
  1256. {
  1257. $conf = $GLOBALS['_MAX']['CONF'];
  1258. $trackerid = (int)$trackerid;
  1259. $rTracker = OA_Dal_Delivery_query("
  1260. SELECT
  1261. t.clientid AS advertiser_id,
  1262. t.trackerid AS tracker_id,
  1263. t.trackername AS name,
  1264. t.variablemethod AS variablemethod,
  1265. t.description AS description,
  1266. t.viewwindow AS viewwindow,
  1267. t.clickwindow AS clickwindow,
  1268. t.blockwindow AS blockwindow,
  1269. t.appendcode AS appendcode
  1270. FROM
  1271. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['trackers'])." AS t
  1272. WHERE
  1273. t.trackerid={$trackerid}
  1274. ");
  1275. if (!is_resource($rTracker)) {
  1276. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1277. } else {
  1278. return (OA_Dal_Delivery_fetchAssoc($rTracker));
  1279. }
  1280. }
  1281. function OA_Dal_Delivery_getTrackerLinkedCreatives($trackerid = null)
  1282. {
  1283. $aConf = $GLOBALS['_MAX']['CONF'];
  1284. $trackerid = (int)$trackerid;
  1285. $rCreatives = OA_Dal_Delivery_query("
  1286. SELECT
  1287. b.bannerid AS ad_id,
  1288. b.campaignid AS placement_id,
  1289. c.viewwindow AS view_window,
  1290. c.clickwindow AS click_window,
  1291. ct.status AS status,
  1292. t.type AS tracker_type
  1293. FROM
  1294. {$aConf['table']['prefix']}{$aConf['table']['banners']} AS b,
  1295. {$aConf['table']['prefix']}{$aConf['table']['campaigns_trackers']} AS ct,
  1296. {$aConf['table']['prefix']}{$aConf['table']['campaigns']} AS c,
  1297. {$aConf['table']['prefix']}{$aConf['table']['trackers']} AS t
  1298. WHERE
  1299. ct.trackerid=t.trackerid
  1300. AND c.campaignid=b.campaignid
  1301. AND b.campaignid = ct.campaignid
  1302. " . ((!empty($trackerid)) ? ' AND t.trackerid='.$trackerid : '') . "
  1303. ");
  1304. if (!is_resource($rCreatives)) {
  1305. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1306. } else {
  1307. $output = array();
  1308. while ($aRow = OA_Dal_Delivery_fetchAssoc($rCreatives)) {
  1309. $output[$aRow['ad_id']] = $aRow;
  1310. }
  1311. return $output;
  1312. }
  1313. }
  1314. function OA_Dal_Delivery_getTrackerVariables($trackerid)
  1315. {
  1316. $conf = $GLOBALS['_MAX']['CONF'];
  1317. $trackerid = (int)$trackerid;
  1318. $rVariables = OA_Dal_Delivery_query("
  1319. SELECT
  1320. v.variableid AS variable_id,
  1321. v.trackerid AS tracker_id,
  1322. v.name AS name,
  1323. v.datatype AS type,
  1324. purpose AS purpose,
  1325. reject_if_empty AS reject_if_empty,
  1326. is_unique AS is_unique,
  1327. unique_window AS unique_window,
  1328. v.variablecode AS variablecode
  1329. FROM
  1330. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['variables'])." AS v
  1331. WHERE
  1332. v.trackerid={$trackerid}
  1333. ");
  1334. if (!is_resource($rVariables)) {
  1335. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1336. } else {
  1337. $output = array();
  1338. while ($aRow = OA_Dal_Delivery_fetchAssoc($rVariables)) {
  1339. $output[$aRow['variable_id']] = $aRow;
  1340. }
  1341. return $output;
  1342. }
  1343. }
  1344. function OA_Dal_Delivery_getMaintenanceInfo()
  1345. {
  1346. $conf = $GLOBALS['_MAX']['CONF'];
  1347. $result = OA_Dal_Delivery_query("
  1348. SELECT
  1349. value AS maintenance_timestamp
  1350. FROM
  1351. ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['application_variable'])."
  1352. WHERE name = 'maintenance_timestamp'
  1353. ");
  1354. if (!is_resource($result)) {
  1355. return (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
  1356. } else {
  1357. $result = OA_Dal_Delivery_fetchAssoc($result);
  1358. return $result['maintenance_timestamp'];
  1359. }
  1360. }
  1361. function OA_Dal_Delivery_buildQuery($part, $lastpart, $precondition)
  1362. {
  1363. $conf = $GLOBALS['_MAX']['CONF'];
  1364. $aColumns = array(
  1365. 'd.bannerid AS ad_id',
  1366. 'd.campaignid AS placement_id',
  1367. 'd.status AS status',
  1368. 'd.description AS name',
  1369. 'd.storagetype AS type',
  1370. 'd.contenttype AS contenttype',
  1371. 'd.pluginversion AS pluginversion',
  1372. 'd.filename AS filename',
  1373. 'd.imageurl AS imageurl',
  1374. 'd.htmltemplate AS htmltemplate',
  1375. 'd.htmlcache AS htmlcache',
  1376. 'd.width AS width',
  1377. 'd.height AS height',
  1378. 'd.weight AS weight',
  1379. 'd.seq AS seq',
  1380. 'd.target AS target',
  1381. 'd.url AS url',
  1382. 'd.alt AS alt',
  1383. 'd.statustext AS statustext',
  1384. 'd.bannertext AS bannertext',
  1385. 'd.adserver AS adserver',
  1386. 'd.block AS block_ad',
  1387. 'd.capping AS cap_ad',
  1388. 'd.session_capping AS session_cap_ad',
  1389. 'd.compiledlimitation AS compiledlimitation',
  1390. 'd.acl_plugins AS acl_plugins',
  1391. 'd.prepend AS prepend',
  1392. 'd.append AS append',
  1393. 'd.bannertype AS bannertype',
  1394. 'd.alt_filename AS alt_filename',
  1395. 'd.alt_imageurl AS alt_imageurl',
  1396. 'd.alt_contenttype AS alt_contenttype',
  1397. 'd.parameters AS parameters',
  1398. 'd.transparent AS transparent',
  1399. 'd.ext_bannertype AS ext_bannertype',
  1400. 'az.priority AS priority',
  1401. 'az.priority_factor AS priority_factor',
  1402. 'az.to_be_delivered AS to_be_delivered',
  1403. 'm.campaignid AS campaign_id',
  1404. 'm.priority AS campaign_priority',
  1405. 'm.weight AS campaign_weight',
  1406. 'm.companion AS campaign_companion',
  1407. 'm.block AS block_campaign',
  1408. 'm.capping AS cap_campaign',
  1409. 'm.session_capping AS session_cap_campaign',
  1410. 'm.show_capped_no_cookie AS show_capped_no_cookie',
  1411. 'm.clickwindow AS clickwindow',
  1412. 'm.viewwindow AS viewwindow',
  1413. 'cl.clientid AS client_id',
  1414. 'm.expire_time AS expire_time',
  1415. 'm.revenue_type AS revenue_type',
  1416. 'm.ecpm_enabled AS ecpm_enabled',
  1417. 'm.ecpm AS ecpm',
  1418. 'cl.advertiser_limitation AS advertiser_limitation',
  1419. 'a.account_id AS account_id',
  1420. 'a.agencyid AS agency_id'
  1421. );
  1422. $aTables = array(
  1423. "".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['banners'])." AS d",
  1424. "JOIN ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['campaigns'])." AS m ON (d.campaignid = m.campaignid) ",
  1425. "JOIN ".OX_escapeIdentifier($conf['table']['prefix'].$conf['table']['clients'])." AS cl ON (m.clientid = cl.clienti…

Large files files are truncated, but you can click here to view the full file