ok == true) { // Save the key if its valid update_option('adpage_apikey', $_POST['api-key']); } else { // Create an error $errors[] = 'This API-key is not valid!'; } } if ($action == 'clear_cache') { // Count total cleared cache files $i = 0; // Get cache files from directory $cache_dir = glob(dirname(__FILE__) . '/cache/*'); // Loop through each cache file foreach ($cache_dir as $cache) { // Update the counter $i++; // Make sure it is a file if (is_file($cache)) { // Delete the cache file unlink($cache); } } // Show a message to the user if ($i > 0) { $notices[] = 'Cache for '.$i.' campaign(s) has been cleared.'; } else { $notices[] = 'There wasn\'t any cache to clear.'; } } $api_key = get_option('adpage_apikey'); $campaigns = get_option('adpage_campaigns'); // Connect a new campaign if ($action == 'connect') { // Get the posted content $hash = $_POST['hash']; $path = trim($_POST['path']); // Path needs to be alphanumerical if ((ctype_alnum($path)) AND (isset($path))) { // Path needs at least 5 characters if (strlen($path) >= 5) { // Validate received hash if (!preg_match('/[^a-z\-0-9]/i', $hash)) { // Retrieve all users campaigns $api_request = adpgc_api_request('key'); $campaigns = json_decode($campaigns, true); // Loop through all campaigns foreach ($api_request->campaigns as $api_campaign) { // Add the new campaign if ($hash == $api_campaign->hash) { $campaigns[$hash] = array( 'hash' => $api_campaign->hash, 'title' => $api_campaign->name, 'path' => $api_campaign->link, 'slug' => $path ); } } // Update database entities update_option('adpage_campaigns', json_encode($campaigns)); // Show a messaga to the user $notices[] = 'Campaign has been connected successfully!'; } else { $notices[] = 'Invalid parameters received!'; } } else { // Show an error $notices[] = 'Could not connect campaign, path must contain at least 5 characters.'; } } else { // Show an error $notices[] = 'Could not connect campaign, path is not alphanumerical.'; } } // Disconnect a campaign if ($action == 'disconnect') { // The hash of the campaign to delete $hash = $_GET['hash']; // Validate received hash if (!preg_match('/[^a-z\-0-9]/i', $hash)) { $campaigns = json_decode($campaigns, true); $campaigns_new = array(); // Loop through all results foreach ($campaigns as $storage_campaign) { // Remove the entity in case if ($hash != $storage_campaign['hash']) { $campaigns_new[$storage_campaign['hash']] = array( 'hash' => $storage_campaign['hash'], 'title' => $storage_campaign['title'], 'path' => $storage_campaign['path'], 'slug' => $storage_campaign['slug'] ); } } // Update the entities in the database update_option('adpage_campaigns', json_encode($campaigns_new)); // Show a messaga to the user $notices[] = 'Campaign has been disconnected successfully.'; } else { $notices[] = 'Invalid parameters received!'; } } // Get campaigns from the database $campaigns = get_option('adpage_campaigns'); if (strlen($api_key) == 0) { // Welcome page if there isnt a API-key include dirname(__FILE__) . '/pages/api_key.php'; } else { // Check if the key is valid $api_request = adpgc_api_request('key'); if ($api_request->ok == true) { // Show the settings page include dirname(__FILE__) . '/pages/settings.php'; } else { // Create and display an error $errors['title'] = 'The API key is no longer valid!'; $errors['description'] = 'It seems like the provided API-key is no longer valid or access has been revoked. Create a new API-key on the AdPage dashboard.'; include dirname(__FILE__) . '/pages/error.php'; } } } function adpgc_endpoint() { global $wp; // Get the campaigns from the database $get_campaigns = json_decode(get_option('adpage_campaigns'), true); $campaign = false; // Loop through all campaigns foreach($get_campaigns as $value) { // If a campaign-slug matched the request-URL if ($value['slug'] == $wp->request) { $campaign = $value; } } // We've got a campaign to display if ($campaign != false) { // Set the HTTP-header header('Content-Type: text/html'); // Campaign parameters from database $campaign_path = $campaign['path']; $campaign_title = $campaign['title']; // Get the campaign content $fetch_campaign = adpgc_get_campaign($campaign_path); if ($fetch_campaign != false) { // Replace the default title $campaign = str_replace('Live', ''.$campaign_title.'', $fetch_campaign); // Output the campaign echo $campaign; exit; } } } function adpgc_validate_key($key) { // Call the AdPage API $result = adpgc_api_request($key); // Check if we've got content if ($result) { return $result; } else { return false; } } function adpgc_api_request($request_key) { // Define the API-key if ($request_key != 'key') { $key = $request_key; } else { $key = get_option('adpage_apikey'); } // Get all campaigns from AdPage $request = wp_remote_get(ADPGC_API_URL . '/info', array( 'body' => $data, 'headers' => array( 'Token' => $key, ), )); // Decode returned JSON from API $result = json_decode($request['body']); return $result; } function adpgc_get_campaign($path) { // Create a hash from the path name $hash = md5($path); // Get the cache path $cache = dirname(__FILE__) . '/cache/'.$hash.'.html'; // Check if the cache exist if (file_exists($cache)) { // Return campaign from cache return file_get_contents($cache); } else { // Download campaign from AdPage $campaign = adpgc_download_campaign($path); // Create cache dir if it doesnt exist if (!file_exists(dirname(__FILE__) . '/cache')) { mkdir(dirname(__FILE__) . '/cache'); } // Cache the campaign locally file_put_contents($cache, $campaign); return $campaign; } } function adpgc_download_campaign($path) { // Retrieve campaign from AdPage $request = wp_remote_get($path); // Get the status code $status_code = wp_remote_retrieve_response_code($request); // Status code tells if the campaign exists if (in_array($status_code, array(200, 202))) { // Return the content body $body = $request['body']; return $body; } else { return false; } } ?>