/**
 * The function to get and return the ads for direct selection
 *
 * @param string  $search       The search string for this banner selection
 *                              Usually 'bannerid:123' or 'campaignid:123'
 * @param string  $campaignid   The campaign ID to fecth banners from, added in 2.3.32 to allow BC with 2.0
 * @param boolean $lastpart     Are there any other search strings left
 *
 * @return array|false          The array of ads matching the search criteria
 *                              or false on failure
 */
function OA_Dal_Delivery_getLinkedAds($search, $campaignid = '', $lastpart = true)
{
    $conf = $GLOBALS['_MAX']['CONF'];
    // Sanitise parameteres
    $campaignid = (int) $campaignid;
    if ($campaignid > 0) {
        $precondition = " AND d.campaignid = '" . $campaignid . "' ";
    } else {
        $precondition = '';
    }
    $aRows['xAds'] = array();
    $aRows['ads'] = array();
    $aRows['lAds'] = array();
    $aRows['count_active'] = 0;
    $aRows['zone_companion'] = false;
    $aRows['count_active'] = 0;
    $totals = array('xAds' => 0, 'ads' => 0, 'lAds' => 0);
    $query = OA_Dal_Delivery_buildQuery($search, $lastpart, $precondition);
    $rAds = OA_Dal_Delivery_query($query);
    if (!is_resource($rAds)) {
        return defined('OA_DELIVERY_CACHE_FUNCTION_ERROR') ? OA_DELIVERY_CACHE_FUNCTION_ERROR : null;
    }
    // Get creatives with conversions enabled
    $aConversionLinkedCreatives = MAX_cacheGetTrackerLinkedCreatives();
    while ($aAd = OA_Dal_Delivery_fetchAssoc($rAds)) {
        $aAd['tracker_status'] = !empty($aConversionLinkedCreatives[$aAd['ad_id']]['status']) ? $aConversionLinkedCreatives[$aAd['ad_id']]['status'] : null;
        // Is the creative from an override, contract or remnant campaign?
        if ($aAd['campaign_priority'] == -1) {
            // Creative is in an override campaign
            $aAd['priority'] = $aAd['campaign_weight'] * $aAd['weight'];
            $aRows['xAds'][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
            $totals['xAds'] += $aAd['priority'];
        } elseif ($aAd['campaign_priority'] == 0) {
            // Creative is in a remnant campaign
            $aAd['priority'] = $aAd['campaign_weight'] * $aAd['weight'];
            $aRows['lAds'][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
            $totals['lAds'] += $aAd['priority'];
        } elseif ($aAd['campaign_priority'] == -2) {
            // Creative is in an eCPM campaign
            $aRows['eAds'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
        } else {
            // Creative is in a contract campaign
            $aRows['ads'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
        }
    }
    // If there are override campaign creatives, sort by priority
    if (isset($aRows['xAds']) && is_array($aRows['xAds'])) {
        $totals['xAds'] = _setPriorityFromWeights($aRows['xAds']);
    }
    // If there are contract campaign creatives, prepare array of priority totals
    // to allow delivery to do the scaling work later
    if (isset($aRows['ads']) && is_array($aRows['ads'])) {
        // Are there any "lower" level creatives?
        if (isset($aRows['lAds']) && is_array($aRows['lAds']) && count($aRows['lAds']) > 0) {
            // "Lower" level creatives exist, INCLUDE the "blank" priority
            $totals['ads'] = _getTotalPrioritiesByCP($aRows['ads'], true);
        } else {
            // "Lower" level creatives do NOT exist, EXCLUDE the "blank" priority
            $totals['ads'] = _getTotalPrioritiesByCP($aRows['ads'], false);
        }
    }
    // eCPM remnant campaigns
    if (is_array($aRows['eAds'])) {
        $totals['eAds'] = _getTotalPrioritiesByCP($aRows['eAds']);
    }
    // If there are remnant campaign creatives, sort by priority
    if (isset($aRows['lAds']) && is_array($aRows['lAds'])) {
        $totals['lAds'] = _setPriorityFromWeights($aRows['lAds']);
    }
    $aRows['priority'] = $totals;
    return $aRows;
}
Example #2
0
/**
 * This function checks if the specified tracker connects back to a valid action
 *
 * @param integer $trackerid The ID of the tracker to look up in the database
 * @return mixed If action occurred: array indexed on [#s since action] => array('type' => <connection type>, 'id' => <creative ID>
 *               else false
 */
function MAX_trackerCheckForValidAction($trackerid)
{
    // Get all creatives that are linked to this tracker
    $aTrackerLinkedAds = MAX_cacheGetTrackerLinkedCreatives($trackerid);
    // This tracker is not linked to any creatives
    if (empty($aTrackerLinkedAds)) {
        return false;
    }
    // Note: Constants are not included n the delivery engine, the values below map to the values defined in constants.php
    $aPossibleActions = _getActionTypes();
    $now = MAX_commonGetTimeNow();
    $aConf = $GLOBALS['_MAX']['CONF'];
    $aMatchingActions = array();
    // Iterate over all creatives linked to this tracker...
    foreach ($aTrackerLinkedAds as $creativeId => $aLinkedInfo) {
        // Iterate over all possible actions (currently only "view" and "click")
        foreach ($aPossibleActions as $actionId => $action) {
            // If there is both a connection window set, and this creative has been actioned
            if (!empty($aLinkedInfo[$action . '_window']) && !empty($_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId])) {
                // Check for any custom data which a plugin may have stored in the cookie
                if (stristr($_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId], ' ')) {
                    list($value, $extra) = explode(' ', $_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId], 2);
                    $_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId] = $value;
                } else {
                    $extra = '';
                }
                list($lastAction, $zoneId) = explode('-', $_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId]);
                // Decode the base32 timestamp
                $lastAction = MAX_commonUnCompressInt($lastAction);
                // Calculate how long ago this action occurred
                $lastSeenSecondsAgo = $now - $lastAction;
                // If the action occurred within the window (and sanity check that it's > 0), record this as a matching action
                if ($lastSeenSecondsAgo <= $aLinkedInfo[$action . '_window'] && $lastSeenSecondsAgo > 0) {
                    // Index the matching array against the # seconds ago that the action occurred
                    $aMatchingActions[$lastSeenSecondsAgo] = array('action_type' => $actionId, 'tracker_type' => $aLinkedInfo['tracker_type'], 'status' => $aLinkedInfo['status'], 'cid' => $creativeId, 'zid' => $zoneId, 'dt' => $lastAction, 'window' => $aLinkedInfo[$action . '_window'], 'extra' => $extra);
                }
            }
        }
    }
    // If no actions matched, return false
    if (empty($aMatchingActions)) {
        return false;
    }
    // Sort by ascending #seconds since action
    ksort($aMatchingActions);
    // Return the first matching action
    return array_shift($aMatchingActions);
}
Example #3
0
function MAX_trackerCheckForValidAction($trackerid)
{
    $aTrackerLinkedAds = MAX_cacheGetTrackerLinkedCreatives($trackerid);
    if (empty($aTrackerLinkedAds)) {
        return false;
    }
    $aPossibleActions = _getActionTypes();
    $now = MAX_commonGetTimeNow();
    $aConf = $GLOBALS['_MAX']['CONF'];
    $aMatchingActions = array();
    foreach ($aTrackerLinkedAds as $creativeId => $aLinkedInfo) {
        foreach ($aPossibleActions as $actionId => $action) {
            if (!empty($aLinkedInfo[$action . '_window']) && !empty($_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId])) {
                if (stristr($_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId], ' ')) {
                    list($value, $extra) = explode(' ', $_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId], 2);
                    $_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId] = $value;
                } else {
                    $extra = '';
                }
                list($lastAction, $zoneId) = explode('-', $_COOKIE[$aConf['var']['last' . ucfirst($action)]][$creativeId]);
                $lastAction = MAX_commonUnCompressInt($lastAction);
                $lastSeenSecondsAgo = $now - $lastAction;
                if ($lastSeenSecondsAgo <= $aLinkedInfo[$action . '_window'] && $lastSeenSecondsAgo > 0) {
                    $aMatchingActions[$lastSeenSecondsAgo] = array('action_type' => $actionId, 'tracker_type' => $aLinkedInfo['tracker_type'], 'status' => $aLinkedInfo['status'], 'cid' => $creativeId, 'zid' => $zoneId, 'dt' => $lastAction, 'window' => $aLinkedInfo[$action . '_window'], 'extra' => $extra);
                }
            }
        }
    }
    if (empty($aMatchingActions)) {
        return false;
    }
    ksort($aMatchingActions);
    return array_shift($aMatchingActions);
}
Example #4
0
/**
 * The function to get and return the ads linked to a zone
 *
 * @param  int   $zoneid The id of the zone to get linked ads for
 *
 * @todo   Refactor this query (and others) to use OA_Dal_Delivery_buildQuery()
 * @return array|false
 *               The array containg zone information with nested arrays of linked ads
 *               or false on failure. Note that:
 *                  - Exclusive ads are in "xAds"
 *                  - Normal (paid) ads are in "ads"
 *                  - Low-priority ads are in "lAds"
 *                  - Companion ads, in addition to being in one of the above, are
 *                    also in "cAds" and "clAds"
 *                  - Exclusive and low-priority ads have had their priorities
 *                    calculated on the basis of the placement and advertisement
 *                    weight
 */
function OA_Dal_Delivery_getZoneLinkedAds($zoneid)
{
    $conf = $GLOBALS['_MAX']['CONF'];
    // Sanitise parameteres
    $zoneid = (int) $zoneid;
    $aRows = OA_Dal_Delivery_getZoneInfo($zoneid);
    $aRows['xAds'] = array();
    $aRows['cAds'] = array();
    $aRows['clAds'] = array();
    $aRows['ads'] = array();
    $aRows['lAds'] = array();
    $aRows['eAds'] = array();
    $aRows['count_active'] = 0;
    $aRows['zone_companion'] = false;
    $aRows['count_active'] = 0;
    $totals = array('xAds' => 0, 'cAds' => 0, 'clAds' => 0, 'ads' => 0, 'lAds' => 0);
    $query = "\n        SELECT\n            d.bannerid AS ad_id,\n            d.campaignid AS placement_id,\n            d.status AS status,\n            d.description AS name,\n            d.storagetype AS type,\n            d.contenttype AS contenttype,\n            d.pluginversion AS pluginversion,\n            d.filename AS filename,\n            d.imageurl AS imageurl,\n            d.htmltemplate AS htmltemplate,\n            d.htmlcache AS htmlcache,\n            d.width AS width,\n            d.height AS height,\n            d.weight AS weight,\n            d.seq AS seq,\n            d.target AS target,\n            d.url AS url,\n            d.alt AS alt,\n            d.statustext AS statustext,\n            d.bannertext AS bannertext,\n            d.adserver AS adserver,\n            d.block AS block_ad,\n            d.capping AS cap_ad,\n            d.session_capping AS session_cap_ad,\n            d.compiledlimitation AS compiledlimitation,\n            d.acl_plugins AS acl_plugins,\n            d.append AS append,\n            d.appendtype AS appendtype,\n            d.bannertype AS bannertype,\n            d.alt_filename AS alt_filename,\n            d.alt_imageurl AS alt_imageurl,\n            d.alt_contenttype AS alt_contenttype,\n            d.parameters AS parameters,\n            d.transparent AS transparent,\n            d.ext_bannertype AS ext_bannertype,\n            az.priority AS priority,\n            az.priority_factor AS priority_factor,\n            az.to_be_delivered AS to_be_delivered,\n            c.campaignid AS campaign_id,\n            c.priority AS campaign_priority,\n            c.weight AS campaign_weight,\n            c.companion AS campaign_companion,\n            c.block AS block_campaign,\n            c.capping AS cap_campaign,\n            c.session_capping AS session_cap_campaign,\n            c.clientid AS client_id,\n            c.clickwindow AS clickwindow,\n            c.viewwindow AS viewwindow,\n            m.advertiser_limitation AS advertiser_limitation,\n            a.account_id AS account_id,\n            z.affiliateid AS affiliate_id,\n            a.agencyid as agency_id\n        FROM\n            " . OX_escapeIdentifier($conf['table']['prefix'] . $conf['table']['banners']) . " AS d JOIN\n            " . OX_escapeIdentifier($conf['table']['prefix'] . $conf['table']['ad_zone_assoc']) . " AS az ON (d.bannerid = az.ad_id) JOIN\n            " . OX_escapeIdentifier($conf['table']['prefix'] . $conf['table']['zones']) . " AS z ON (az.zone_id = z.zoneid) JOIN\n            " . OX_escapeIdentifier($conf['table']['prefix'] . $conf['table']['campaigns']) . " AS c ON (c.campaignid = d.campaignid) LEFT JOIN\n            " . OX_escapeIdentifier($conf['table']['prefix'] . $conf['table']['clients']) . " AS m ON (m.clientid = c.clientid) LEFT JOIN\n            " . OX_escapeIdentifier($conf['table']['prefix'] . $conf['table']['agency']) . " AS a ON (a.agencyid = m.agencyid)\n        WHERE\n            az.zone_id = {$zoneid}\n          AND\n            d.status <= 0\n          AND\n            c.status <= 0\n    ";
    //    $query = OA_Dal_Delivery_buildQuery('', '', '');
    $rAds = OA_Dal_Delivery_query($query);
    if (!is_resource($rAds)) {
        if (defined('OA_DELIVERY_CACHE_FUNCTION_ERROR')) {
            return OA_DELIVERY_CACHE_FUNCTION_ERROR;
        } else {
            return null;
        }
    }
    // Get timezone data
    $aTimezones = MAX_cacheGetAccountTZs();
    $aConversionLinkedCreatives = MAX_cacheGetTrackerLinkedCreatives();
    while ($aAd = OA_Dal_Delivery_fetchAssoc($rAds)) {
        // Add timezone
        if (isset($aAd['account_id']) && isset($aTimezones['aAccounts'][$aAd['account_id']])) {
            $aAd['timezone'] = $aTimezones['aAccounts'][$aAd['account_id']];
        } else {
            $aAd['timezone'] = $aTimezones['default'];
        }
        $aAd['tracker_status'] = !empty($aConversionLinkedCreatives[$aAd['ad_id']]['status']) ? $aConversionLinkedCreatives[$aAd['ad_id']]['status'] : null;
        // Is the ad Exclusive, Low, or Normal Priority?
        if ($aAd['campaign_priority'] == -1) {
            // Ad is in an exclusive placement
            $aRows['xAds'][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
        } elseif ($aAd['campaign_priority'] == 0) {
            // Ad is in a low priority placement
            $aRows['lAds'][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
        } elseif ($aAd['campaign_priority'] == -2) {
            // Ad is in a low priority eCPM placement
            $aRows['eAds'][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
        } else {
            // Ad is in a paid placement
            $aRows['ads'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
            $aRows['count_active']++;
        }
        // Also store Companion ads in additional array
        if ($aAd['campaign_companion'] == 1) {
            if ($aAd['campaign_priority'] == 0) {
                // Store a low priority companion ad
                $aRows['zone_companion'][] = $aAd['placement_id'];
                $aRows['clAds'][$aAd['ad_id']] = $aAd;
            } else {
                // Store a paid priority companion ad
                $aRows['zone_companion'][] = $aAd['placement_id'];
                $aRows['cAds'][$aAd['campaign_priority']][$aAd['ad_id']] = $aAd;
            }
        }
    }
    // If there are exclusive ads, sort by priority
    if (is_array($aRows['xAds'])) {
        $totals['xAds'] = _setPriorityFromWeights($aRows['xAds']);
    }
    // If there are paid ads (or eCPM ads), prepare array of priority totals
    // to allow delivery to do the scaling work later
    if (is_array($aRows['ads'])) {
        $totals['ads'] = _getTotalPrioritiesByCP($aRows['ads']);
    }
    if (is_array($aRows['eAds'])) {
        $totals['eAds'] = _getTotalPrioritiesByCP($aRows['eAds']);
    }
    // If there are low priority ads, sort by priority
    if (is_array($aRows['lAds'])) {
        $totals['lAds'] = _setPriorityFromWeights($aRows['lAds']);
    }
    // If there are paid companion ads, prepare array of priority totals
    // to allow delivery to do the scaling work later
    if (is_array($aRows['cAds'])) {
        $totals['cAds'] = _getTotalPrioritiesByCP($aRows['cAds']);
    }
    // If there are low priority companion ads, sort by priority
    if (is_array($aRows['clAds'])) {
        $totals['clAds'] = _setPriorityFromWeights($aRows['clAds']);
    }
    $aRows['priority'] = $totals;
    return $aRows;
}