예제 #1
0
function a360_request_handler()
{
    if (!empty($_GET['a360_action'])) {
        switch ($_GET['a360_action']) {
            case 'admin_js':
                a360_admin_js();
                break;
            case 'admin_css_ie':
                header('Content-type: text/css');
                require 'css/a360-ie.css';
                die;
                break;
            case 'admin_css':
                header('Content-type: text/css');
                require 'css/datePicker.css';
                require 'css/a360.css';
                die;
                break;
            case 'capture_ga_token':
                if (!current_user_can('manage_options')) {
                    wp_die(__('You are not allowed to do that.', 'analytics360'));
                }
                $args = array();
                parse_str($_SERVER['QUERY_STRING'], $args);
                $token = NULL;
                if (isset($args['token'])) {
                    $ch = curl_init('https://www.google.com/accounts/AuthSubSessionToken');
                    curl_setopt($ch, CURLOPT_HEADER, true);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $args['token'] . '"'));
                    $result = curl_exec($ch);
                    $matches = array();
                    $found = preg_match('/Token=(.*)/', $result, $matches);
                    if ($found) {
                        $token = $matches[1];
                        $result = update_option('a360_ga_token', $token);
                    }
                }
                if (!$token) {
                    $q = http_build_query(array('a360_error' => 'Authentication with Google did not succeed. Please try again.'));
                } else {
                    delete_option('a360_ga_profile_id');
                    $q = http_build_query(array('updated' => true));
                }
                wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . $q);
                break;
            case 'get_wp_posts':
                add_filter('posts_where', create_function('$where', 'return $where." AND post_date >= \'' . $_GET['start_date'] . '\' AND post_date < \'' . $_GET['end_date'] . '\'";'));
                $results = query_posts('post_status=publish&posts_per_page=999');
                header('Content-type: text/javascript');
                die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false)));
                break;
            case 'get_mc_data':
                global $a360_api_key;
                if (!class_exists('MCAPI')) {
                    include_once ABSPATH . PLUGINDIR . '/analytics360/php/MCAPI.class.php';
                }
                $api = new MCAPI($a360_api_key);
                switch ($_GET['data_type']) {
                    case 'campaigns':
                        $results = $api->campaigns(array('sendtime_start' => $_GET['start_date'], 'end_start' => $_GET['end_date']));
                        if ($results) {
                            die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false)));
                        } else {
                            die(cf_json_encode(array('success' => false, 'error' => $api->errorMessage)));
                        }
                        break;
                    case 'list_growth':
                        $results = $api->listGrowthHistory($_GET['list_id']);
                        if ($results) {
                            die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false)));
                        } else {
                            die(cf_json_encode(array('success' => false, 'error' => $api->errorMessage)));
                        }
                        break;
                }
                break;
            case 'get_ga_data':
                global $a360_ga_token, $a360_ga_profile_id;
                $parameters = array('start-date' => $_GET['start_date'], 'end-date' => $_GET['end_date'], 'sort' => 'ga:date', 'ids' => 'ga:' . $a360_ga_profile_id);
                // split up top referrals by filtering on each medium in turn
                if ($_GET['data_type'] == 'top_referrals') {
                    $handles = array('referral' => null, 'organic' => null, 'email' => null, 'cpc' => null, '*' => null);
                    $parameters['dimensions'] = 'ga:medium,ga:source';
                    $parameters['metrics'] = 'ga:visits,ga:timeOnSite,ga:pageviews';
                    $parameters['sort'] = '-ga:visits';
                    foreach ($handles as $filter => $handle) {
                        $p = $filter == '*' ? array('max-results' => 200) : array('filters' => 'ga:medium==' . $filter, 'max-results' => 200);
                        $handles[$filter] = $handle = curl_init('https://www.google.com/analytics/feeds/data?' . http_build_query(array_merge($parameters, $p)));
                        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($handle, CURLOPT_TIMEOUT, 10);
                        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, true);
                        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, true);
                        curl_setopt($handle, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $a360_ga_token . '"'));
                    }
                    $mh = curl_multi_init();
                    foreach ($handles as $handle) {
                        curl_multi_add_handle($mh, $handle);
                    }
                    $running = null;
                    do {
                        curl_multi_exec($mh, $running);
                    } while ($running > 0);
                    $all_results = array();
                    foreach ($handles as $filter => $handle) {
                        $http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
                        if (substr($http_code, 0, 1) == '2') {
                            $all_results[$filter] = a360_reportObjectMapper(curl_multi_getcontent($handle));
                        } else {
                            $all_results[$filter] = curl_multi_getcontent($handle);
                        }
                        curl_multi_remove_handle($mh, $handle);
                    }
                    curl_multi_close($mh);
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => true, 'data' => $all_results, 'cached' => false)));
                } else {
                    switch ($_GET['data_type']) {
                        case 'visits':
                            $parameters['dimensions'] = 'ga:date,ga:medium';
                            $parameters['metrics'] = 'ga:visits,ga:bounces,ga:entrances,ga:pageviews,ga:newVisits,ga:timeOnSite';
                            //$parameters['filters'] = 'ga:medium==referral,ga:medium==organic,ga:medium==email,ga:medium==cpc';
                            //$parameters['sort'] = '-ga:visits';
                            break;
                        case 'geo':
                            $parameters['dimensions'] = 'ga:country';
                            $parameters['metrics'] = 'ga:visits';
                            $parameters['sort'] = '-ga:visits';
                            break;
                        case 'top_referrals':
                            $parameters['dimensions'] = 'ga:medium,ga:source';
                            $parameters['metrics'] = 'ga:visits,ga:timeOnSite,ga:pageviews';
                            $parameters['sort'] = '-ga:visits';
                            $parameters['filters'] = 'ga:medium==referral,ga:medium==organic,ga:medium==email,ga:medium==cpc';
                            break;
                        case 'referral_media':
                            $parameters['dimensions'] = 'ga:medium';
                            $parameters['metrics'] = 'ga:visits';
                            $parameters['sort'] = '-ga:visits';
                            break;
                        case 'top_content':
                            $parameters['dimensions'] = 'ga:pagePath';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            break;
                        case 'keywords':
                            $parameters['dimensions'] = 'ga:keyword';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            $parameters['filters'] = 'ga:source==' . $_GET['source_name'];
                            break;
                        case 'referral_paths':
                            $parameters['dimensions'] = 'ga:source,ga:referralPath';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            $parameters['filters'] = 'ga:source==' . $_GET['source_name'];
                            break;
                        case 'email_referrals':
                            $parameters['dimensions'] = 'ga:campaign';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            $parameters['filters'] = 'ga:medium==email';
                            break;
                        default:
                            break;
                    }
                    $ch = curl_init('https://www.google.com/analytics/feeds/data?' . http_build_query($parameters));
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
                    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $a360_ga_token . '"'));
                    $result = curl_exec($ch);
                }
                if (!$result) {
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => false, 'error' => curl_error($ch))));
                }
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if (substr($http_code, 0, 1) == '2') {
                    $result = a360_reportObjectMapper($result);
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => true, 'data' => $result, 'cached' => false)));
                } else {
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => false, 'error' => $result)));
                }
                break;
        }
    }
    if (!empty($_POST['a360_action']) && current_user_can('manage_options')) {
        switch ($_POST['a360_action']) {
            case 'update_mc_api_key':
                if (isset($_POST['a360_username']) && isset($_POST['a360_password'])) {
                    $key_result = a360_fetch_API_key($_POST['a360_username'], $_POST['a360_password']);
                    if ($key_result['success']) {
                        delete_option('a360_chimp_chatter_url');
                        update_option('a360_api_key', $key_result['api_key']);
                        $q = http_build_query(array('updated' => 'true'));
                    } else {
                        $q = http_build_query(array('a360_error' => $key_result['error']));
                    }
                }
                wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . $q);
                die;
                break;
            case 'clear_mc_api_key':
                delete_option('a360_api_key');
                delete_option('a360_chimp_chatter_url');
                wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . http_build_query(array('updated' => 'true')));
                break;
            case 'revoke_ga_token':
                global $a360_ga_token;
                $ch = curl_init('https://www.google.com/accounts/AuthSubRevokeToken');
                curl_setopt($ch, CURLOPT_HEADER, true);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="' . $a360_ga_token . '"'));
                $result = curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if ($http_code == 200) {
                    delete_option('a360_ga_token');
                    delete_option('a360_ga_profile_id');
                    wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&update=true');
                } else {
                    wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . http_build_query(array('a360_error' => 'Could not revoke token!')));
                }
                break;
            case 'set_ga_profile_id':
                $result = update_option('a360_ga_profile_id', $_POST['profile_id']);
                wp_redirect(trailingslashit(get_bloginfo('wpurl')) . 'wp-admin/options-general.php?page=' . basename(__FILE__) . '&updated=true');
                break;
        }
    }
}
예제 #2
0
function a360_request_handler()
{
    if (!empty($_GET['a360_action']) && current_user_can('manage_options')) {
        switch ($_GET['a360_action']) {
            case 'admin_js':
                a360_admin_js();
                break;
            case 'admin_css_ie':
                header('Content-type: text/css');
                require 'css/a360-ie.css';
                die;
                break;
            case 'admin_css':
                header('Content-type: text/css');
                require 'css/datePicker.css';
                require 'css/a360.css';
                die;
                break;
            case 'capture_ga_token':
                $args = array();
                parse_str($_SERVER['QUERY_STRING'], $args);
                $token = NULL;
                if (isset($args['token'])) {
                    $wp_http = a360_get_wp_http();
                    $request_args = array('method' => 'GET', 'headers' => a360_get_authsub_headers($args['token']), 'sslverify' => false);
                    $response = $wp_http->request('https://www.google.com/accounts/AuthSubSessionToken', $request_args);
                    $error_messages = array();
                    if (is_wp_error($response)) {
                        // couldn't connect
                        $error_messages = $response->get_error_messages();
                    } else {
                        if (is_array($response)) {
                            $matches = array();
                            $found = preg_match('/Token=(.*)/', $response['body'], $matches);
                            if ($found) {
                                $token = $matches[1];
                                $result = update_option('a360_ga_token', $token);
                            } else {
                                // connected, but no token in response.
                                $error_messages = array($repsonse['body']);
                            }
                        }
                    }
                }
                if (!$token) {
                    if (count($error_messages)) {
                        $capture_errors .= implode("\n", $error_messages);
                    } else {
                        $capture_errors = 'unknown error';
                    }
                    $q = build_query(array('a360_ga_token_capture_errors' => $capture_errors), '', '&');
                } else {
                    delete_option('a360_ga_profile_id');
                    $q = build_query(array('updated' => true), '', '&');
                }
                wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . $q));
                break;
            case 'get_wp_posts':
                $start = preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $_GET['start_date']) ? $_GET['start_date'] : '0000-00-00';
                $end = preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $_GET['end_date']) ? $_GET['end_date'] : '0000-00-00';
                add_filter('posts_where', create_function('$where', 'return $where." AND post_date >= \'' . $start . '\' AND post_date < \'' . $end . '\'";'));
                $results = query_posts('post_status=publish&posts_per_page=999');
                header('Content-type: text/javascript');
                die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false)));
                break;
            case 'get_mc_data':
                global $a360_api_key;
                $api = a360_get_mcapi($a360_api_key);
                switch ($_GET['data_type']) {
                    case 'campaigns':
                        $results = $api->campaigns(array('sendtime_start' => $_GET['start_date'], 'end_start' => $_GET['end_date']));
                        if ($results) {
                            die(cf_json_encode(array('success' => true, 'data' => $results['data'], 'cached' => false)));
                        } else {
                            if (empty($api->errorCode)) {
                                die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false)));
                            } else {
                                die(cf_json_encode(array('success' => false, 'error' => $api->errorMessage)));
                            }
                        }
                        break;
                    case 'list_growth':
                        $results = $api->listGrowthHistory($_GET['list_id']);
                        if ($results) {
                            die(cf_json_encode(array('success' => true, 'data' => $results, 'cached' => false)));
                        } else {
                            die(cf_json_encode(array('success' => false, 'error' => $api->errorMessage)));
                        }
                        break;
                }
                break;
            case 'get_ga_data':
                global $a360_ga_token, $a360_ga_profile_id;
                $parameters = array('start-date' => $_GET['start_date'], 'end-date' => $_GET['end_date'], 'sort' => 'ga:date', 'ids' => 'ga:' . $a360_ga_profile_id);
                // split up top referrals by filtering on each medium in turn
                if ($_GET['data_type'] == 'top_referrals') {
                    $requests = array('referral' => null, 'organic' => null, 'email' => null, 'cpc' => null, '*' => null);
                    $parameters['dimensions'] = 'ga:medium,ga:source';
                    $parameters['metrics'] = 'ga:visits,ga:timeOnSite,ga:pageviews';
                    $parameters['sort'] = '-ga:visits';
                    $all_results = array();
                    foreach ($requests as $filter => $request) {
                        $p = $filter == '*' ? array('max-results' => 200) : array('filters' => 'ga:medium==' . $filter, 'max-results' => 200);
                        $requests[$filter] = $request = a360_get_wp_http();
                        $all_results[$filter] = $request->request('https://www.googleapis.com/analytics/v2.4/data?' . build_query(array_merge($parameters, $p), '', '&'), array('headers' => a360_get_authsub_headers(), 'timeout' => 10, 'sslverify' => false));
                    }
                    foreach ($all_results as $filter => $results) {
                        if (is_wp_error($results)) {
                            header('Content-type: text/javascript');
                            die(cf_json_encode(array('success' => false, 'error' => implode('<br/>', $results->get_error_messages()))));
                        }
                        if (substr($results['response']['code'], 0, 1) == '2') {
                            $all_results[$filter] = a360_reportObjectMapper($results['body']);
                        } else {
                            header('Content-type: text/javascript');
                            die(cf_json_encode(array('success' => false, 'error' => $results['body'])));
                        }
                    }
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => true, 'data' => $all_results, 'cached' => false)));
                } else {
                    switch ($_GET['data_type']) {
                        case 'visits':
                            $parameters['dimensions'] = 'ga:date,ga:medium';
                            $parameters['metrics'] = 'ga:visits,ga:bounces,ga:entrances,ga:pageviews,ga:newVisits,ga:timeOnSite';
                            //$parameters['filters'] = 'ga:medium==referral,ga:medium==organic,ga:medium==email,ga:medium==cpc';
                            //$parameters['sort'] = '-ga:visits';
                            break;
                        case 'geo':
                            $parameters['dimensions'] = 'ga:country';
                            $parameters['metrics'] = 'ga:visits';
                            $parameters['sort'] = '-ga:visits';
                            break;
                        case 'top_referrals':
                            $parameters['dimensions'] = 'ga:medium,ga:source';
                            $parameters['metrics'] = 'ga:visits,ga:timeOnSite,ga:pageviews';
                            $parameters['sort'] = '-ga:visits';
                            $parameters['filters'] = 'ga:medium==referral,ga:medium==organic,ga:medium==email,ga:medium==cpc';
                            break;
                        case 'referral_media':
                            $parameters['dimensions'] = 'ga:medium';
                            $parameters['metrics'] = 'ga:visits';
                            $parameters['sort'] = '-ga:visits';
                            break;
                        case 'top_content':
                            $parameters['dimensions'] = 'ga:pagePath';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            break;
                        case 'keywords':
                            $parameters['dimensions'] = 'ga:keyword';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            $parameters['filters'] = 'ga:source==' . $_GET['source_name'];
                            break;
                        case 'referral_paths':
                            $parameters['dimensions'] = 'ga:source,ga:referralPath';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            $parameters['filters'] = 'ga:source==' . $_GET['source_name'];
                            break;
                        case 'email_referrals':
                            $parameters['dimensions'] = 'ga:campaign';
                            $parameters['metrics'] = 'ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:exits';
                            $parameters['sort'] = '-ga:pageviews';
                            $parameters['filters'] = 'ga:medium==email';
                            break;
                        default:
                            break;
                    }
                    $wp_http = a360_get_wp_http();
                    $url = 'https://www.google.com/analytics/feeds/data?' . build_query($parameters, '', '&');
                    $request_args = array('headers' => a360_get_authsub_headers(), 'timeout' => 10, 'sslverify' => false);
                    $result = $wp_http->request($url, $request_args);
                }
                if (is_wp_error($result)) {
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => false, 'error' => implode('<br/>', $result->get_error_messages()))));
                }
                if (substr($result['response']['code'], 0, 1) == '2') {
                    $result = a360_reportObjectMapper($result['body']);
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => true, 'data' => $result, 'cached' => false)));
                } else {
                    header('Content-type: text/javascript');
                    die(cf_json_encode(array('success' => false, 'error' => $result['body'])));
                }
                break;
        }
    }
    if (!empty($_POST['a360_action']) && current_user_can('manage_options')) {
        a360_check_nonce($_POST['a360_nonce'], $_POST['a360_action']);
        switch ($_POST['a360_action']) {
            case 'update_mc_api_key':
                if (!empty($_POST['a360_api_key']) && isset($_POST['a360_api_key'])) {
                    $key_result = a360_validate_API_key($_POST['a360_api_key']);
                }
                if (!empty($key_result)) {
                    if ($key_result['success']) {
                        delete_option('a360_chimp_chatter_url');
                        update_option('a360_api_key', $key_result['api_key']);
                        $q = build_query(array('updated' => 'true'), '', '&');
                    } else {
                        $q = build_query(array('a360_mc_auth_error' => $key_result['error']), '', '&');
                    }
                }
                wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . $q));
                die;
                break;
            case 'clear_mc_api_key':
                delete_option('a360_api_key');
                delete_option('a360_chimp_chatter_url');
                wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . build_query(array('updated' => 'true'), '', '&')));
                break;
            case 'revoke_ga_token':
                global $a360_ga_token;
                $wp_http = a360_get_wp_http();
                $request_args = array('headers' => a360_get_authsub_headers(), 'sslverify' => false);
                $response = $wp_http->request('https://www.google.com/accounts/AuthSubRevokeToken', $request_args);
                if ($response['response']['code'] == 200) {
                    delete_option('a360_ga_token');
                    delete_option('a360_ga_profile_id');
                    wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&update=true'));
                } else {
                    if ($response['response']['code'] == 403) {
                        wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . build_query(array('a360_revoke_token_chicken_and_egg' => $response['response']['code'] . ': ' . $response['response']['message']), '', '&')));
                    } else {
                        if (is_wp_error($response)) {
                            $errors = $response->get_error_messages();
                        } else {
                            $errors = array($response['response']['code'] . ': ' . $response['response']['message']);
                        }
                        wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&' . build_query(array('a360_error' => implode("\n", $errors)), '', '&')));
                    }
                }
                break;
            case 'forget_ga_token':
                delete_option('a360_ga_token');
                delete_option('a360_ga_profile_id');
                wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&update=true'));
                break;
            case 'set_ga_profile_id':
                if (update_option('a360_ga_profile_id', $_POST['profile_id'])) {
                    wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&updated=true'));
                } else {
                    wp_redirect(site_url('wp-admin/options-general.php?page=' . basename(__FILE__) . '&a360_error=' . urlencode(__('Could not save Analytics profile information', 'analytics360'))));
                }
                break;
        }
    }
}