/**
 * Check to see if this impression contains the valid referrer.
 *
 * @param string $limitation The referrer limitation
 * @param string $op The operator (either '==' or '!=')
 * @param array $aParams An array of additional parameters to be checked
 * @return boolean Whether this impression's referrer passes this limitation's test.
 */
function MAX_checkSite_Referingpage($limitation, $op, $aParams = array())
{
    if ($limitation == '') {
        return true;
    }
    $url = empty($aParams) ? $GLOBALS['referer'] : $aParams['referer'];
    return MAX_limitationsMatchStringValue($url, $limitation, $op);
}
/**
 * The utility function which checks if the value in the parameters
 * in the request for an ad fulfill the requirements of delivery limitation.
 * This function checks string values.
 * The parameters are looked in the $aParam array. If the $aParam array
 * is empty or unspecified, then $GLOBALS['_MAX']['$namespace'] array
 * is used instead.
 *
 * @param string $paramName Name of the parameter to look for in an array.
 * @param string $limitation Value to be matched with.
 * @param string $op The operator used to compare strings.
 * @param string $aParams The array in which the value is looked for.
 * @param string $namespace The namespace in the $GLOBALS['_MAX'] array used
 *               if when $aParams is empty.
 * @return boolean True if the parameters fulfill the limitations, false
 *                 otherwise.
 * @see MAX_limitationsMatchStringClientGeo
 * @see MAX_limitationsMatchStringValue
 */
function MAX_limitationsMatchString($paramName, $limitation, $op, $aParams = array(), $namespace = 'CLIENT')
{
    if ($limitation == '') {
        return true;
    }
    if (empty($aParams)) {
        $aParams = $GLOBALS['_MAX'][$namespace];
    }
    $value = $aParams[$paramName];
    return MAX_limitationsMatchStringValue($value, $limitation, $op);
}
/**
 * Check to see if this impression contains the valid domain.
 *
 * @param string $limitation The domain limitation.
 * @param string $op The string operator.
 * @param array $aParams An array of additional parameters to be checked.
 * @return boolean Whether this impression's domain passes this limitation's test.
 */
function MAX_checkClient_Domain($limitation, $op, $aParams = array())
{
    if (empty($aParams)) {
        $aParams = $GLOBALS['_MAX']['CLIENT'];
    }
    if ($limitation == '') {
        return true;
    }
    $host = $_SERVER['REMOTE_HOST'];
    if (MAX_limitationsIsOperatorRegexp($op)) {
        $domain = $host;
    } else {
        $domain = substr($host, -strlen($limitation));
    }
    return MAX_limitationsMatchStringValue($domain, $limitation, $op);
}
/**
 * Check to see if this impression contains the valid city.
 *
 * @param string $limitation The city (or comma list of cities) limitation
 * @param string $op The operator (either '==' or '!=')
 * @param array $aParams An array of additional parameters to be checked
 * @return boolean Whether this impression's city passes this limitation's test.
 */
function MAX_checkGeo_City($limitation, $op, $aParams = array())
{
    if (empty($aParams)) {
        $aParams = $GLOBALS['_MAX']['CLIENT_GEO'];
    }
    if ($aParams && $aParams['city'] && $aParams['country_code']) {
        $aLimitation = array(substr($limitation, 0, strpos($limitation, '|')), substr($limitation, strpos($limitation, '|') + 1));
        $sCities = $aLimitation[1];
        if (!empty($aLimitation[0])) {
            return MAX_limitationsMatchStringValue($aParams['country_code'], $aLimitation[0], '==') && MAX_limitationsMatchArrayValue($aParams['city'], $sCities, $op);
        } else {
            return MAX_limitationsMatchArrayValue($aParams['city'], $sCities, $op);
        }
    } else {
        return false;
        // If client has no data about city, do not show the ad
    }
}
/**
 * Check to see if this impression contains the valid region.
 *
 * @param string $limitation The region (or comma list of regions) limitation
 * @param string $op The operator (either '==' or '!=')
 * @param array $aParams An array of additional parameters to be checked
 * @return boolean Whether this impression's region passes this limitation's test.
 */
function MAX_checkGeo_Region($limitation, $op, $aParams = array())
{
    if (empty($aParams)) {
        $aParams = $GLOBALS['_MAX']['CLIENT_GEO'];
    }
    if ($op != '=~' && $op != '!~') {
        // Provide backwards compatibility
        $op = '=~';
    }
    $aLimitation = explode('|', $limitation);
    $sCountry = $aLimitation[0];
    $sRegions = $aLimitation[1];
    if ($aParams && $aParams['region'] && $aParams['country_code']) {
        return MAX_limitationsMatchStringValue($aParams['country_code'], $sCountry, '==') && MAX_limitationsMatchArrayValue($aParams['region'], $sRegions, $op);
    } else {
        return false;
        // Do not show the ad if user has no data about region and country.
    }
}