예제 #1
0
 public static function send_event_to_events_gateway($url, $data_string)
 {
     try {
         $stream_function = function ($curl, $str) {
             return strlen($str);
         };
         $curl = curl_init();
         $curl_options = array(CURLOPT_URL => $url, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_USERAGENT => UBConfig::UB_USER_AGENT, CURLOPT_FOLLOWLOCATION => false, CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)), CURLOPT_HEADERFUNCTION => $stream_function, CURLOPT_WRITEFUNCTION => $stream_function, CURLOPT_POSTFIELDS => $data_string, CURLOPT_TIMEOUT => 2);
         curl_setopt_array($curl, $curl_options);
         $success = curl_exec($curl);
         $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
         if (!$success) {
             $message = 'Unable to send log messages to ' . $url . ': "' . curl_error($curl) . '" - HTTP status: ' . curl_errno($curl);
             UBLogger::warning($message);
         } elseif ($http_code >= 200 && $http_code < 300) {
             $message = 'Successfully sent log messsages to ' . $url . ' - HTTP status: ' . $http_code;
             UBLogger::debug($message);
         } else {
             $message = 'Unable to send log messages to ' . $url . ' - HTTP status: ' . $http_code;
             UBLogger::warning($message);
         }
         curl_close($curl);
     } catch (Exception $e) {
         $message = 'Unable to send log messages to ' . $url . ' - Error: ' . $e;
         UBLogger::warning($message);
     }
 }
    });
    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());
});
예제 #3
0
 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'));
 }
예제 #4
0
 public static function config($msg)
 {
     UBLogger::log('CONFIG', $msg);
 }