ads.txt Guru account and WordPress installation to enable automatic updates to your ads.txt and app-ads.txt files when changes are made via your ads.txt Guru account. To get started: 1) Click the 'Activate' link to the left of this description, 2) Go to the 'ads.txt Guru' link shown in the menu on the left of the page. Author: ads.txt Guru Author URI: https://adstxt.guru/ License: GPLv2 Version: 1.3.0 */ /* Copyright 2018 ionix Limited (email: contact@adstxt.guru) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // Defined here rather than inside atg_connect_init(), which only runs for an administrator: the // front-end request that writes the file reports the version back to ads.txt Guru, and that is // how ads.txt Guru knows whether this installation understands app-ads.txt. if (!defined('WP_ATG_VERSION')) { define('WP_ATG_VERSION', '1.3.0'); } if ((function_exists('add_action')) && (function_exists('add_filter'))) { if ((function_exists('is_admin')) && (is_admin())) { // Initiate admin functions add_action('init', 'atg_connect_init'); } elseif ((isset($_POST['atg-connect-key'])) && (isset($_POST['atg-connect-secret']))) { // Initiate frontend function, only when request contains the key, secret and token parameters add_action('init', 'atg_connect_wp_loaded'); } } function atg_connect_init() { // Initiate admin functions if ((function_exists('wp_get_current_user')) && (current_user_can('manage_options'))) { add_action('admin_menu', 'atg_connect_admin_menu'); } } function atg_connect_files() { // The files this plugin can write, and the configuration keys holding each one's path and its // custom records. ads.txt Guru names the file a synchronize is for; a name that is not in this // list is refused rather than guessed at, because the only thing a wrong answer can do is // overwrite one file with the other's records. return array( 'ads.txt' => array( 'path' => 'adstxt_path', 'custom' => 'adstxt_custom', 'label' => 'ads.txt' ), 'app-ads.txt' => array( 'path' => 'appads_path', 'custom' => 'appads_custom', 'label' => 'app-ads.txt' ) ); } function atg_connect_file_supported($file = '') { // Is this one of the files above? if ((!is_string($file)) || ($file=='')) { return false; } $atg_connect_files = atg_connect_files(); return isset($atg_connect_files[$file]); } function atg_connect_file_path($atg_connect, $file = 'ads.txt') { // Configured path for one file, '' when there is none to use. if (!atg_connect_file_supported($file)) { return ''; } $atg_connect_files = atg_connect_files(); $key = $atg_connect_files[$file]['path']; if ((isset($atg_connect[$key])) && (is_string($atg_connect[$key])) && ($atg_connect[$key]!='')) { return $atg_connect[$key]; } // An installation that predates 1.2.0 has no app-ads.txt path stored. Derive it from the // ads.txt path rather than recomputing it: that directory is already the one the customer has // told us serves this site (get_home_path() by default, or their own correction for a // WordPress installed in a sub-directory), and get_home_path() is a wp-admin function that // does not exist on the front-end request this has to work in. if ((isset($atg_connect['adstxt_path'])) && (is_string($atg_connect['adstxt_path'])) && ($atg_connect['adstxt_path']!='')) { return rtrim(dirname($atg_connect['adstxt_path']), '/\\').DIRECTORY_SEPARATOR.$file; } return ''; } function atg_connect_file_custom($atg_connect, $file = 'ads.txt') { // Custom records for one file, '' when none are configured. if (!atg_connect_file_supported($file)) { return ''; } $atg_connect_files = atg_connect_files(); $key = $atg_connect_files[$file]['custom']; if ((isset($atg_connect[$key])) && (is_string($atg_connect[$key]))) { return $atg_connect[$key]; } return ''; } function atg_connect_path_check($requested_path, $file) { // Shared by both path forms: one traversal guard, applied to whichever file the form was for. // Returns true, or the reason it refused so the caller can name the file in the notice. $real_home_path = realpath(get_home_path()); $real_requested_path = realpath(dirname($requested_path)); if (($real_home_path===false) || ($real_requested_path===false)) { // Neither path resolves to something on disk. This arm also closes a hole in the 1.1.2 // guard: realpath() returning false made the prefix test below strpos($path, '') on PHP 8, // which is 0, so a home path that could not be resolved allowed any directory at all. return 'directory'; } // Prefix test with the separator attached, so a sibling directory whose name merely starts // with the home path (/var/www/htmlEVIL against /var/www/html) is not inside it. if (($real_requested_path!==$real_home_path) && (strpos($real_requested_path, rtrim($real_home_path, '/\\').DIRECTORY_SEPARATOR)!==0)) { return 'directory'; } if (basename($requested_path)!==$file) { return 'file'; } if ((!file_exists($requested_path)) && (!is_writable($real_requested_path))) { // The file does not have to exist yet -- app-ads.txt usually will not -- but if it does not // then we have to be able to create it, otherwise the first synchronize fails instead. return 'missing'; } return true; } function atg_connect_admin_menu() { // Initiate admin menu add_menu_page('ads.txt Guru Connect', 'ads.txt Guru', 'manage_options', 'adstxt-guru-connect', 'atg_connect_home', '', 82.14); } function atg_connect_home() { // Admin plugin output $atg_connect_plugin_url = admin_url('admin.php?page=adstxt-guru-connect'); $atg_connect_notices = array(); $atg_connect_notice = ''; $atg_connect = false; if (isset($_POST['atg-connect-reset']) && isset($_POST['atg_connect_reset_nonce']) && wp_verify_nonce($_POST['atg_connect_reset_nonce'], 'atg_connect_reset')) { delete_option('atg-connect'); } if (($atg_connect = atg_connect_get_option())===false) { // No configuration data, generate key and secret. $atg_connect = array( 'key' => atg_connect_random_string(), 'secret' => atg_connect_random_string(), 'token_param' => atg_connect_random_string('8'), 'last_update' => '0', 'last_test' => '0', 'adstxt_path' => get_home_path().'ads.txt', 'adstxt_custom' => '', 'appads_path' => get_home_path().'app-ads.txt', 'appads_custom' => '', 'url' => site_url('/') ); if (atg_connect_add_option($atg_connect)!==true) { $atg_connect = false; } } if (!isset($atg_connect['adstxt_custom'])) { $atg_connect['adstxt_custom'] = ''; } // Upgraded in place from a version before 1.2.0: fill in the app-ads.txt keys so the forms // below have something to show. Not saved here -- an admin page load should not write the // option -- so atg_connect_file_path() derives the same default for the front-end request. if (!isset($atg_connect['appads_path'])) { $atg_connect['appads_path'] = atg_connect_file_path($atg_connect, 'app-ads.txt'); } if (!isset($atg_connect['appads_custom'])) { $atg_connect['appads_custom'] = ''; } if ($atg_connect===false) { print<<

ads.txt Guru Connect

Unable to set-up initial configuration, please contact support.
END; } else { if ($atg_connect['url']!=site_url('/')) { $reset_nonce = wp_create_nonce('atg_connect_reset'); $atg_connect_notices[] = <<
END; } // One pass per file, so ads.txt and app-ads.txt get the same guard and the same messages. // The field and nonce names for ads.txt are the historical ones: a customer with the // previous version's page open in a tab still has a valid nonce for it. $atg_connect_forms = array( 'ads.txt' => array( 'path_field' => 'atg-connect-path', 'path_nonce' => 'atg_connect_nonce', 'path_action' => 'atg_connect_update_path', 'custom_field' => 'atg-connect-custom', 'custom_nonce' => 'atg_connect_custom_nonce', 'custom_action' => 'atg_connect_update_custom', 'repair_field' => 'atg-connect-repair-path', 'repair_nonce' => 'atg_connect_repair_nonce', 'repair_action' => 'atg_connect_repair_path' ), 'app-ads.txt' => array( 'path_field' => 'atg-connect-appads-path', 'path_nonce' => 'atg_connect_appads_nonce', 'path_action' => 'atg_connect_update_appads_path', 'custom_field' => 'atg-connect-appads-custom', 'custom_nonce' => 'atg_connect_appads_custom_nonce', 'custom_action' => 'atg_connect_update_appads_custom', 'repair_field' => 'atg-connect-repair-appads-path', 'repair_nonce' => 'atg_connect_repair_appads_nonce', 'repair_action' => 'atg_connect_repair_appads_path' ) ); $atg_connect_files = atg_connect_files(); foreach ($atg_connect_forms as $atg_connect_form_file => $atg_connect_form) { $path_key = $atg_connect_files[$atg_connect_form_file]['path']; $custom_key = $atg_connect_files[$atg_connect_form_file]['custom']; if (isset($_POST[$atg_connect_form['path_field']]) && isset($_POST[$atg_connect_form['path_nonce']]) && wp_verify_nonce($_POST[$atg_connect_form['path_nonce']], $atg_connect_form['path_action'])) { // Change path to the file $requested_path = sanitize_text_field($_POST[$atg_connect_form['path_field']]); // Security: prevent directory traversal, and only ever this one filename $path_check = atg_connect_path_check($requested_path, $atg_connect_form_file); if ($path_check===true) { $atg_connect[$path_key] = $requested_path; atg_connect_update_option($atg_connect); $atg_connect_notice = '
Path to '.$atg_connect_form_file.' file updated successfully!
'; } elseif ($path_check==='file') { $atg_connect_notices[] = 'Security error: Only '.$atg_connect_form_file.' files are allowed.'; } elseif ($path_check==='missing') { $atg_connect_notices[] = 'Invalid path to '.$atg_connect_form_file.' file, please ensure the '.$atg_connect_form_file.' file exists (this can be an empty text file) or that its directory is writable.'; } else { $atg_connect_notices[] = 'Security error: Path must be within the WordPress installation directory.'; } } elseif (isset($_POST[$atg_connect_form['path_field']])) { // CSRF token verification failed $atg_connect_notices[] = 'Security error: Invalid security token. Please refresh the page and try again.'; } // Repair: put the path back to what this WordPress installation says its own root is. // // The stored path is written ONCE, when the plugin first installs, from get_home_path(). // Nothing re-derives it afterwards, so a site that moves hosting (or moves its WordPress // root) keeps a path describing the OLD server for ever, and every synchronize fails // against a directory that is not there. Offered as a button rather than done silently: // changing where a customer's ads.txt gets written is their decision, and a // sub-directory installation may legitimately want a path we would not guess. if (isset($_POST[$atg_connect_form['repair_field']]) && isset($_POST[$atg_connect_form['repair_nonce']]) && wp_verify_nonce($_POST[$atg_connect_form['repair_nonce']], $atg_connect_form['repair_action'])) { $repair_path = get_home_path().$atg_connect_form_file; if (atg_connect_path_check($repair_path, $atg_connect_form_file)===true) { $atg_connect[$path_key] = $repair_path; atg_connect_update_option($atg_connect); $atg_connect_notice = '
Path to '.$atg_connect_form_file.' file repaired: '.htmlentities($repair_path).'
'; } else { $atg_connect_notices[] = 'Could not repair the path to '.$atg_connect_form_file.' automatically. The WordPress installation directory ('.htmlentities(get_home_path()).') is not writable, so please create an empty '.$atg_connect_form_file.' file there, or make the directory writable, and try again.'; } } elseif (isset($_POST[$atg_connect_form['repair_field']])) { $atg_connect_notices[] = 'Security error: Invalid security token. Please refresh the page and try again.'; } if (isset($_POST[$atg_connect_form['custom_field']]) && isset($_POST[$atg_connect_form['custom_nonce']]) && wp_verify_nonce($_POST[$atg_connect_form['custom_nonce']], $atg_connect_form['custom_action'])) { $atg_connect[$custom_key] = $_POST[$atg_connect_form['custom_field']]; atg_connect_update_option($atg_connect); $custom_path = atg_connect_file_path($atg_connect, $atg_connect_form_file); if (file_exists($custom_path)) { $adstxt_data = explode("\n# ATG-CUSTOM\n", file_get_contents($custom_path)); $adstxt_data = $adstxt_data[0]."\n# ATG-CUSTOM\n".$atg_connect[$custom_key]; } else { $adstxt_data = "\n# ATG-CUSTOM\n".$atg_connect[$custom_key]; } if (@file_put_contents($custom_path, $adstxt_data)!==false) { $atg_connect_notice = '
Custom '.$atg_connect_form_file.' records updated successfully!
'; } else { // Saved in the configuration and appended at the next synchronize, but the file // itself could not be written now. This used to report success either way. $atg_connect_notices[] = 'Custom '.$atg_connect_form_file.' records saved, but the '.$atg_connect_form_file.' file could not be written. Please check the path and file permissions.
Path to '.$atg_connect_form_file.' File: '.htmlentities($custom_path).''; } } } if ($atg_connect['last_test'] false, 'request' => false, 'response' => '', 'code' => '0' ); $atg_connect_test['request'] = wp_remote_post('https://adstxt.guru/connect/1.0/', array( 'body' => array( 'test' => '1', 'version' => WP_ATG_VERSION ) )); $atg_connect_test['code'] = wp_remote_retrieve_response_code($atg_connect_test['request']); if ($atg_connect_test['code']=='200') { $atg_connect_test['response'] = json_decode(wp_remote_retrieve_body($atg_connect_test['request']), true); if ($atg_connect_test['response']['success']===true) { $atg_connect['last_test'] = time(); if (atg_connect_update_option($atg_connect)) { $atg_connect_test['success'] = true; } } } if ($atg_connect_test['success']!==true) { $atg_connect_notices[] = 'Unable to connect to ads.txt Guru system, please contact support.'; } } // Health-check the STORED path on every admin page load, not only when the form is saved. // // Until 1.3.0 the path was validated at save time and never again, so the one path that is // never saved by anybody -- the one the plugin writes for itself at install, from // get_home_path() -- was also the one never checked. Move the site to different hosting and // that path describes a server that no longer exists: every synchronize fails, and the // plugin page says nothing at all. Reported by a publisher on 2026-09-01 whose ads.txt had // been 404ing while the plugin page looked perfectly healthy. // // ($atg_connect_adstxt_path_exists and _writable were set here and read nowhere, in every // version back to 1.0.0. The notices below are the whole point of the check.) foreach ($atg_connect_files as $atg_connect_check_file => $atg_connect_check) { $check_path = atg_connect_file_path($atg_connect, $atg_connect_check_file); if ($check_path=='') { continue; } $check_result = atg_connect_path_check($check_path, $atg_connect_check_file); if ($check_result===true) { // The path is usable. A file that EXISTS and cannot be written is a separate // problem the path check deliberately does not fail on, so it is reported here. if ((file_exists($check_path)) && (!is_writable($check_path))) { $atg_connect_notices[] = $atg_connect_check_file.' file is write protected, please adjust file permissions so file is writable, e.g. 666 or 777.
Path to '.$atg_connect_check_file.' File: '.htmlentities($check_path).''; } continue; } // Something is wrong with the stored path. Say which, in terms of what the customer // can act on, and offer the repair when this installation can name a path that works. if ($check_result==='file') { $atg_connect_notices[] = 'The configured path does not end in '.$atg_connect_check_file.', so it cannot be used.
Path to '.$atg_connect_check_file.' File: '.htmlentities($check_path).''; continue; } if ($check_result==='missing') { $atg_connect_notices[] = $atg_connect_check_file.' cannot be written: the directory it is configured in does not allow this site to create files there. Please create an empty '.$atg_connect_check_file.' file at that location, or make its directory writable.
Path to '.$atg_connect_check_file.' File: '.htmlentities($check_path).''; continue; } // 'directory': the path is not inside this WordPress installation. Almost always a // path left behind by a move to different hosting, so lead with that. $repair_notice = 'Your '.$atg_connect_check_file.' file cannot be updated: the configured path is not inside this WordPress installation, so nothing can be written to it. This usually means the site has moved to different hosting since the plugin was installed, and the path still describes the old server.
Path to '.$atg_connect_check_file.' File: '.htmlentities($check_path).''; $repair_default = get_home_path().$atg_connect_check_file; if ((isset($atg_connect_forms[$atg_connect_check_file])) && (atg_connect_path_check($repair_default, $atg_connect_check_file)===true)) { $repair_form = $atg_connect_forms[$atg_connect_check_file]; $repair_nonce_field = wp_nonce_field($repair_form['repair_action'], $repair_form['repair_nonce'], true, false); $repair_default_html = htmlentities($repair_default); $repair_notice .= <<This installation reports its own root as $repair_default_html, which is writable.
$repair_nonce_field
END; } $atg_connect_notices[] = $repair_notice; } // Generate copy and paste string to establish connection $atg_connect_copy_string = array( 'key' => $atg_connect['key'], 'secret' => $atg_connect['secret'], 'param' => $atg_connect['token_param'], 'url' => site_url('/') ); $atg_connect_copy_string = base64_encode(json_encode($atg_connect_copy_string)); // Output admin plugin $atg_connect_notices_count = count($atg_connect_notices); for ($i=0; $i<$atg_connect_notices_count; $i++) { $atg_connect_notices[$i] = '
Error
'.$atg_connect_notices[$i].'
'; } $atg_connect_notices = implode('
', $atg_connect_notices); if ($atg_connect_notices=='') { if ($atg_connect['last_update']>time() - 31536000) { // Which file that was. One timestamp covers both, so it names the file it wrote // rather than claiming ads.txt for an app-ads.txt synchronize. $atg_connect_last_file = 'ads.txt'; if ((isset($atg_connect['last_file'])) && (atg_connect_file_supported($atg_connect['last_file']))) { $atg_connect_last_file = $atg_connect['last_file']; } $atg_connect_notices = '
'.$atg_connect_last_file.' file last updated '.human_time_diff($atg_connect['last_update']).' ago.
'; } } // Retrieve contents of current ads.txt file $atg_connect_adstxt = ''; $atg_connect_adstxt_path = htmlentities($atg_connect['adstxt_path']); $atg_connect_adstxt_custom = htmlentities($atg_connect['adstxt_custom']); $atg_connect_adstxt_time = ''; if (file_exists($atg_connect['adstxt_path'])) { $atg_connect_adstxt = htmlentities(file_get_contents($atg_connect['adstxt_path'])); $atg_connect_adstxt_time = human_time_diff(filemtime($atg_connect['adstxt_path'])); } $atg_connect_appads_path = htmlentities(atg_connect_file_path($atg_connect, 'app-ads.txt')); $atg_connect_appads_custom = htmlentities(atg_connect_file_custom($atg_connect, 'app-ads.txt')); print<<

ads.txt Guru Connect

$atg_connect_notice $atg_connect_notices

ads.txt Guru Connect enables you to connect your ads.txt Guru account to your WordPress installation to automate your ads.txt file upload whenever you or a collaborator makes changes to your ads.txt file using the ads.txt Guru management system, this eliminates the need to manually upload your ads.txt file.

What is ads.txt Guru?

ads.txt Guru is a revolutionary tool to eliminate the burden of maintaining website ads.txt files! The ads.txt Guru system allows you to manage your ads.txt files online, automatically validate your ads.txt files, and collaborate between publisher and ad network to automate ads.txt updates. By allowing ad networks to manage their ads.txt records on your website without your intervention, the time-consuming process of manually updating your ads.txt files when ad networks need to make changes to their records can now be totally eliminated.

Connect WordPress

To connect WordPress simply log-in to your ads.txt Guru account and proceed to the 'Settings' section for your website/domain, then select the 'Connect WordPress' tab and copy and paste the following code into the 'Connect Data' field.

 

Connect Data


Copy Connect Data to Clipboard
 

What is the 'Connect Data'?

The 'Connect Data' code contains a random key, secret and token, along with the URL of your website, this has been encoded to make it easier for you to copy and paste. This data enables us to connect to your website to provoke this plugin to download and update your ads.txt file using a secure process.

Path to ads.txt file

Your ads.txt file must be placed at the root of your domain (e.g. http://yourdomain.com/ads.txt). If your WordPress installation is installed in a sub-directory you may need to adjust the path to your ads.txt file using the form below.

 

Path to ads.txt file


END; wp_nonce_field('atg_connect_update_path', 'atg_connect_nonce'); print<<
 

Custom ads.txt Records

To append custom ads.txt records to the end of the ads.txt file generated by ads.txt Guru simply enter those records below. Please ensure these records are validated using the ads.txt Guru validator.

 

Custom ads.txt Records


END; wp_nonce_field('atg_connect_update_custom', 'atg_connect_custom_nonce'); print<<
 

app-ads.txt

app-ads.txt is the mobile app equivalent of ads.txt, and it is a separate file served from the same website: the developer URL listed on your app store pages. If you manage an app-ads.txt file in your ads.txt Guru account for this website, this plugin keeps it up to date exactly as it does your ads.txt file. Nothing is written to app-ads.txt unless ads.txt Guru asks for it, so there is nothing to switch on here.

Your app-ads.txt file must be placed at the root of your domain (e.g. http://yourdomain.com/app-ads.txt). If your WordPress installation is installed in a sub-directory you may need to adjust the path to your app-ads.txt file using the form below. The file does not have to exist yet.

 

Path to app-ads.txt file


END; wp_nonce_field('atg_connect_update_appads_path', 'atg_connect_appads_nonce'); print<<
 

Custom app-ads.txt Records

To append custom app-ads.txt records to the end of the app-ads.txt file generated by ads.txt Guru simply enter those records below. These are kept separately from your custom ads.txt records, so each file only receives the records intended for it.

 

Custom app-ads.txt Records


END; wp_nonce_field('atg_connect_update_appads_custom', 'atg_connect_appads_custom_nonce'); print<<
 

Updating your ads.txt file

To update your ads.txt or app-ads.txt file simply log-in to your ads.txt Guru account and select the relevant domain/website, then select the 'Synchronize' button.

END; /* if ($atg_connect_adstxt!='') { print<<Your ads.txt file

The contents of your current ads.txt file are shown below:

 

$atg_connect_adstxt_path (updated $atg_connect_adstxt_time ago)


 
END; } */ print<< END; } } function atg_connect_wp_loaded() { // Authenticate and process ads.txt / app-ads.txt update if (($atg_connect = atg_connect_get_option())!==false) { if (isset($atg_connect['token_param'])) { $atg_connect_token_param = $atg_connect['token_param']; if ((isset($_POST['atg-connect-token-'.$atg_connect_token_param])) && (isset($atg_connect['key'])) && (isset($atg_connect['secret'])) && ($_POST['atg-connect-key']==$atg_connect['key']) && ($_POST['atg-connect-secret']==$atg_connect['secret']) && (strlen($_POST['atg-connect-token-'.$atg_connect_token_param])=='32') && (!preg_match('/[^a-zA-Z0-9]/', $_POST['atg-connect-token-'.$atg_connect_token_param]))) { // Request authenticated. Which file is this synchronize for? ads.txt Guru names it; // its absence means ads.txt, which is the only file this plugin could write before // 1.2.0 and therefore the only thing an older ads.txt Guru can be asking for. A name // we do not recognise is refused rather than guessed at. $atg_connect_file = 'ads.txt'; if (isset($_POST['atg-connect-file'])) { if (atg_connect_file_supported($_POST['atg-connect-file'])) { $atg_connect_file = $_POST['atg-connect-file']; } else { atg_connect_json(array( 'success' => false, 'reason' => 'file-unsupported' )); } } $atg_connect_file_path = atg_connect_file_path($atg_connect, $atg_connect_file); if ($atg_connect_file_path=='') { // No path configured and none derivable. Visiting the plugin page once fills // this in, so say so rather than writing to a guessed location. atg_connect_json(array( 'success' => false, 'reason' => 'adstxt-path' )); } // Process the file retrieval from ads.txt Guru and update the website's file if ((file_exists($atg_connect_file_path)) && (!is_writable($atg_connect_file_path))) { // File write protected. Checked before calling ads.txt Guru because the token is // single use: consuming it here would lose the update as well as fail it. atg_connect_json(array( 'success' => false, 'reason' => 'adstxt-protected' )); } elseif ((!file_exists($atg_connect_file_path)) && (!is_writable(dirname($atg_connect_file_path)))) { // The file is not there and we could not create it: the directory is missing, or // it is read-only. is_writable() answers false for both, which is what we want. // // This arm is why the CONNECTION TEST was worth fixing rather than only the // update. Both come through here, and until 1.3.0 the only pre-check was the one // above, which needs the file to EXIST to fail. A path whose directory had been // deleted therefore passed it -- file_exists() is false, so the arm never fired -- // and the test reported a healthy connection for a path that could not be // written to under any circumstances. A publisher on 2026-09-01 spent that // difference: a green connection test, and every synchronize silently failing. // // Deliberately NOT atg_connect_path_check(): that calls get_home_path(), which is // a wp-admin function and is not loaded on this front-end request. atg_connect_json(array( 'success' => false, 'reason' => 'adstxt-unwritable' )); } else { $atg_connect_adstxt = array( 'request' => false, 'response' => '', 'code' => '0' ); // 'file' is echoed back to ads.txt Guru, which is how it knows this installation // understood the request. A plugin older than 1.2.0 cannot echo it, so ads.txt // Guru withholds an app-ads.txt payload from it rather than have it written to // the website's ads.txt file. if (isset($_POST['atg-connect-test'])) { $atg_connect_adstxt['request'] = wp_remote_post('https://adstxt.guru/connect/1.0/', array( 'body' => array( 'test' => '1', 'file' => $atg_connect_file, 'version' => WP_ATG_VERSION ) )); } else { $atg_connect_adstxt['request'] = wp_remote_post('https://adstxt.guru/connect/1.0/', array( 'body' => array( 'token' => $_POST['atg-connect-token-'.$atg_connect_token_param], 'file' => $atg_connect_file, 'version' => WP_ATG_VERSION ) )); } $atg_connect_adstxt['code'] = wp_remote_retrieve_response_code($atg_connect_adstxt['request']); if ($atg_connect_adstxt['code']=='200') { $atg_connect_adstxt['response'] = wp_remote_retrieve_body($atg_connect_adstxt['request']); if ($atg_connect_adstxt['response']!='') { $atg_connect_adstxt['response'] = @json_decode($atg_connect_adstxt['response'], true); if ((isset($_POST['atg-connect-test'])) && (isset($atg_connect_adstxt['response']['success'])) && ($atg_connect_adstxt['response']['success']===true)){ // Connection test. The file is reported back so ads.txt Guru can // tell an installation that supports app-ads.txt from one that does // not, at the point the customer connects rather than at the first // synchronize. atg_connect_json(array( 'success' => true, 'file' => $atg_connect_file, 'version' => WP_ATG_VERSION )); } elseif ((isset($atg_connect_adstxt['response']['success'])) && (isset($atg_connect_adstxt['response']['data'])) && ($atg_connect_adstxt['response']['success']===true)) { if ((isset($atg_connect_adstxt['response']['file'])) && ($atg_connect_adstxt['response']['file']!==$atg_connect_file)) { // ads.txt Guru answered about a different file than the one it // asked us for. Belt and braces: the payload arrives over HTTPS // keyed to a single-use token, so this cannot happen by accident, // but writing one file's records into the other is the one // outcome worth refusing outright. atg_connect_json(array( 'success' => false, 'reason' => 'file-mismatch' )); } if (substr(trim($atg_connect_adstxt['response']['data']), 0, 6)=='# ATG-') { $atg_connect_custom = atg_connect_file_custom($atg_connect, $atg_connect_file); if ($atg_connect_custom!='') { $atg_connect_adstxt['response']['data'] .= "\n# ATG-CUSTOM\n".$atg_connect_custom; } // One write, one read-back. These two arms were duplicated // verbatim either side of the custom-records append. if (file_put_contents($atg_connect_file_path, $atg_connect_adstxt['response']['data'])!==false) { if (file_get_contents($atg_connect_file_path)==$atg_connect_adstxt['response']['data']) { // Success, file updated $atg_connect['last_update'] = time(); $atg_connect['last_test'] = time(); $atg_connect['last_file'] = $atg_connect_file; atg_connect_update_option($atg_connect); atg_connect_json(array( 'success' => true, 'file' => $atg_connect_file, 'version' => WP_ATG_VERSION )); } else { // File not overwritten correctly atg_connect_json(array( 'success' => false, 'reason' => 'adstxt-check' )); } } else { // File could not be written atg_connect_json(array( 'success' => false, 'reason' => 'adstxt-write' )); } } else { // File invalid atg_connect_json(array( 'success' => false, 'reason' => 'adstxt-invalid' )); } } else { if ((isset($atg_connect_adstxt['response']['reason'])) && ($atg_connect_adstxt['response']['reason']=='token')) { // Token expired atg_connect_json(array( 'success' => false, 'reason' => 'connect-token' )); } elseif ((isset($atg_connect_adstxt['response']['reason'])) && ($atg_connect_adstxt['response']['reason']=='maintenance')) { // ads.txt Guru Connect API down for maintenance atg_connect_json(array( 'success' => false, 'reason' => 'connect-maintenance' )); } elseif ((isset($atg_connect_adstxt['response']['reason'])) && ($atg_connect_adstxt['response']['reason']=='invalid')) { // ads.txt Guru Connect API received invalid request atg_connect_json(array( 'success' => false, 'reason' => 'connect-invalid' )); } else { // Unknown process error, return body for debugging atg_connect_json(array( 'success' => false, 'reason' => 'connect-process', 'body' => $atg_connect_adstxt['response'] )); } } } else { // Invalid response atg_connect_json(array( 'success' => false, 'reason' => 'connect-retrieve' )); } } else { // Connection failed atg_connect_json(array( 'success' => false, 'reason' => 'connect-error' )); } } } } } // Authentication failed, ignore } function atg_connect_json($output = false) { // Output JSON and exit if ($output===false) { $output = array( 'success' => false, 'reason' => 'error' ); } print json_encode($output, JSON_PRETTY_PRINT); exit; } function atg_connect_add_option($option = array()) { // Save plugin configuration $option = base64_encode(serialize($option)); return add_option('atg-connect', $option, '', 'no'); } function atg_connect_update_option($option = array()) { // Update plugin configuration $option = base64_encode(serialize($option)); return update_option('atg-connect', $option); } function atg_connect_get_option() { // Retrieve plugin configuration if ($option = get_option('atg-connect')) { $option = unserialize(base64_decode($option)); if (is_array($option)) { return $option; } } return false; } function atg_connect_random_string($length = '32', $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') { // Generate random string // wp_rand() rather than rand(): this generates the key, secret and token parameter that are the // only authentication between ads.txt Guru and this installation, and wp_rand() draws on // random_int()/the CSPRNG where the platform has one. It has been in WordPress since 2.6.2 and, // unlike wp_generate_password(), nothing can filter its output into a value the ads.txt Guru end // would reject. $string = ''; for ($i=0; $i<$length; $i++) { $string .= $characters[wp_rand(0,strlen($characters)-1)]; } return $string; } ?>