Example #1
0
 public static function get_flash($cookie_name, $default = null)
 {
     return UBUtil::array_fetch($_COOKIE, "ub-flash-{$cookie_name}", $default);
 }
 private static function last_status_success($domain_info)
 {
     $last_status = UBUtil::array_fetch($domain_info, 'last_status');
     return $last_status !== null && $last_status !== 'FAILURE';
 }
Example #3
0
<?php

echo '<div class="ub-plugin-wrapper>';
$is_authorized = UBConfig::is_authorized_domain($domain);
$diagnostics_failed = in_array(false, UBDiagnostics::checks($domain, $domain_info));
echo UBTemplate::render('main_header', array('img_url' => plugins_url('img/unbounce-logo-blue.png', __FILE__), 'is_authorized' => $is_authorized, 'authorization' => UBUtil::get_flash('authorization'), 'diagnostics_failed' => $diagnostics_failed));
if ($is_authorized) {
    $proxyable_url_set = UBUtil::array_fetch($domain_info, 'proxyable_url_set', array());
    $proxyable_url_set_fetched_at = UBUtil::array_fetch($domain_info, 'proxyable_url_set_fetched_at');
    $last_refreshed = UBUtil::time_ago($proxyable_url_set_fetched_at);
    echo UBTemplate::render('main_authorized', array('domain' => $domain, 'proxyable_url_set' => $proxyable_url_set, 'last_refreshed' => $last_refreshed));
    add_action('in_admin_footer', function () {
        echo UBTemplate::render('main_authorized_footer');
    });
} else {
    if (UBConfig::has_authorized()) {
        // They've attempted to authorize, but this domain isn't in the list
        echo UBTemplate::render('main_failed_authorization', array('domain' => $domain));
    } else {
        echo UBTemplate::render('main_unauthorized', array('domain' => $domain));
    }
    add_action('in_admin_footer', function () {
        echo UBTemplate::render('main_unauthorized_footer');
    });
}
echo '</div>';
Example #4
0
 public static function stream_request($method, $target_url, $cookie_string, $headers0, $user_agent)
 {
     $existing_headers = headers_list();
     $forwarded_for = UBUtil::array_fetch($_SERVER, 'HTTP_X_FORWARDED_FOR');
     $remote_ip = UBUtil::array_fetch($_SERVER, 'REMOTE_ADDR');
     $headers = UBHTTP::get_proxied_for_header($headers0, $forwarded_for, $remote_ip);
     UBLogger::debug_var('target_url', $target_url);
     $stream_headers = UBHTTP::stream_headers_function($existing_headers);
     $stream_body = UBHTTP::stream_response_function();
     $curl = curl_init();
     // http://php.net/manual/en/function.curl-setopt.php
     $curl_options = array(CURLOPT_URL => $target_url, CURLOPT_POST => $method == "POST", CURLOPT_CUSTOMREQUEST => $method, CURLOPT_USERAGENT => $user_agent, CURLOPT_COOKIE => $cookie_string, CURLOPT_HTTPHEADER => $headers, CURLOPT_HEADERFUNCTION => $stream_headers, CURLOPT_WRITEFUNCTION => $stream_body, CURLOPT_FOLLOWLOCATION => false, CURLOPT_TIMEOUT => 30);
     if ($method == "POST") {
         // Use raw post body to allow the same post key to occur more than once
         $curl_options[CURLOPT_POSTFIELDS] = file_get_contents('php://input');
     }
     curl_setopt_array($curl, $curl_options);
     $resp = curl_exec($curl);
     if (!$resp) {
         $message = 'Error proxying to "' . $target_url . ", " . '": "' . curl_error($curl) . '" - Code: ' . curl_errno($curl);
         UBLogger::warning($message);
         http_response_code(500);
         $result = array(false, $message);
     } else {
         $result = array(true, null);
     }
     curl_close($curl);
     return $result;
 }
 public static function _read_unbounce_domain_info($options_getter, $options_setter, $fetch_proxyable_url_set, $domain, $expire_now = false)
 {
     $proxyable_url_set = null;
     $cache_max_time_default = 10;
     $ps_domain = $options_getter(UBConfig::UB_PAGE_SERVER_DOMAIN_KEY);
     $domains_info = $options_getter(UBConfig::UB_ROUTES_CACHE_KEY);
     $domain_info = UBUtil::array_fetch($domains_info, $domain, array());
     $proxyable_url_set = UBUtil::array_fetch($domain_info, 'proxyable_url_set');
     $proxyable_url_set_fetched_at = UBUtil::array_fetch($domain_info, 'proxyable_url_set_fetched_at');
     $proxyable_url_set_cache_timeout = UBUtil::array_fetch($domain_info, 'proxyable_url_set_cache_timeout');
     $proxyable_url_set_etag = UBUtil::array_fetch($domain_info, 'proxyable_url_set_etag');
     $cache_max_time = is_null($proxyable_url_set_cache_timeout) ? $cache_max_time_default : $proxyable_url_set_cache_timeout;
     $current_time = time();
     if ($expire_now || is_null($proxyable_url_set) || $current_time - $proxyable_url_set_fetched_at > $cache_max_time) {
         try {
             $can_fetch = UBUtil::get_lock();
             UBLogger::debug('Locking: ' . $can_fetch);
             if ($can_fetch) {
                 $result_array = call_user_func($fetch_proxyable_url_set, $domain, $proxyable_url_set_etag, $ps_domain);
                 list($routes_status, $etag, $max_age, $proxyable_url_set_new) = $result_array;
                 if ($routes_status['status'] == 'NEW') {
                     $domain_info['proxyable_url_set'] = $proxyable_url_set_new;
                     $domain_info['proxyable_url_set_etag'] = $etag;
                     $domain_info['proxyable_url_set_cache_timeout'] = $max_age;
                 } elseif ($routes_status['status'] == 'SAME') {
                     // Just extend the cache
                     $domain_info['proxyable_url_set_cache_timeout'] = $max_age;
                 } elseif ($routes_status['status'] == 'NONE') {
                     $domain_info['proxyable_url_set'] = array();
                     $domain_info['proxyable_url_set_etag'] = null;
                 } elseif ($routes_status['status'] == 'FAILURE') {
                     UBLogger::warning('Route fetching failed');
                 } else {
                     UBLogger::warning("Unknown response from route fetcher: '{$routes_status}'");
                 }
                 // Creation of domain_info entry
                 $domain_info['proxyable_url_set_fetched_at'] = $current_time;
                 $domain_info['last_status'] = $routes_status['status'];
                 if ($routes_status['status'] == 'FAILURE') {
                     $domain_info['failure_message'] = $routes_status['failure_message'];
                 }
                 $domains_info[$domain] = $domain_info;
                 // set autoload to false so that options are always loaded from DB
                 $options_setter(UBConfig::UB_ROUTES_CACHE_KEY, $domains_info, false);
             }
         } catch (Exception $e) {
             UBLogger::warning('Could not update sitemap: ' . $e);
         }
         $release_result = UBUtil::release_lock();
         UBLogger::debug('Unlocking: ' . $release_result);
     }
     return UBUtil::array_select_by_key($domain_info, array('proxyable_url_set', 'proxyable_url_set_fetched_at', 'failure_message', 'last_status'));
 }
    });
    if ($domains && is_array($domains)) {
        $authorization = 'success';
        $has_authorized = get_option(UBConfig::UB_HAS_AUTHORIZED_KEY, false);
        $data = array('domain_name' => UBConfig::domain(), 'first_authorization' => !$has_authorized, 'user_id' => UBUtil::array_fetch($_POST, 'user_id', ''), 'client_id' => UBUtil::array_fetch($_POST, 'client_id', ''), 'domain_id' => UBUtil::array_fetch($_POST, 'domain_id', ''));
        UBConfig::update_authorization_options($domains, $data);
        if (UBConfig::is_authorized_domain(UBConfig::domain())) {
            $event = UBEvents::successful_authorization_event($data);
        } else {
            $event = UBEvents::failed_authorization_event($data);
        }
        UBHTTP::send_event_to_events_gateway(UBConfig::remote_events_url(), $event);
    } else {
        $authorization = 'failure';
    }
    UBUtil::set_flash('authorization', $authorization);
    status_header(301);
    $location = admin_url('admin.php?page=unbounce-pages');
    header("Location: {$location}");
});
add_action('admin_post_flush_unbounce_pages', function () {
    $domain = UBConfig::domain();
    // Expire cache and redirect
    $_domain_info = UBConfig::read_unbounce_domain_info($domain, true);
    status_header(301);
    $location = admin_url('admin.php?page=unbounce-pages');
    header("Location: {$location}");
});
add_action('shutdown', function () {
    UBLogger::upload_logs_to_unbounce(UBConfig::remote_log_url());
});