licensing->license_is_valid() ) {
add_filter( "rsssl_run_test", array( $this, 'mixed_content_scan' ), 10, 3 );
add_action( 'admin_init', array( $this, 'start_quick_scan_from_url' ) );
}
add_filter( 'rsssl_notices', array($this,'get_notices_list'),20, 1 );
}
static function this() {
return self::$_this;
}
/**
* Catch rest api request
* @param $response
* @param $test
* @param $data
*
* @return mixed
*/
public function mixed_content_scan($response, $test, $data ){
if ( $test === 'mixed_content_scan' ){
$state = $data['state'];
switch ($state) {
case 'start':
update_option( "rsssl_iteration", 1, false );
delete_transient( 'rsssl_scan' );
update_option( "rsssl_progress", 0, false );
update_option( "rsssl_current_action", "", false );
update_option( "rsssl_scan_type", "home", false );
update_option( "rsssl_scan_active", 'running', false );
break;
case 'start_full':
update_option( "rsssl_scan_type", "all", false );
update_option( "rsssl_scan_active", 'running', false );
break;
case 'stop':
update_option( "rsssl_scan_active", 'stop', false );
break;
case 'running':
update_option( "rsssl_scan_active", 'running', false );
}
//can be false or 'stop', if not active
if ( get_option( 'rsssl_scan_active' ) === 'running' ) {
$this->run_scan();
}
$progress = get_option( 'rsssl_progress' );
if ( $progress>=100 ) {
update_option( "rsssl_scan_active", 'stop', false );
}
$response = $this->get();
}
return $response;
}
/**
* Check if the user has completed a scan before, but the data was cleared due to timeout of transient
* @return bool
*/
public function has_cleared_scan_data() {
$completed_scan_before = false;
if ( $this->scan_completed_no_errors === 'COMPLETED' || $this->scan_completed_no_errors === 'ERRORS' ) {
$completed_scan_before = true;
}
if ( ! get_transient( 'rsssl_scan' ) && $completed_scan_before && get_option( 'rsssl_progress' ) == 100 ) {
return true;
}
return false;
}
/**
* Get list of notices for the dashboard
* @param array $notices
*
* @return array
*/
public function get_notices_list($notices)
{
$notices['mixed_content_scan'] = array(
'callback' => 'RSSSL()->scan->pro_scan_notice',
'score' => 10,
'output' => array(
'has-ssl-scan-has-errors' => array(
'msg' => __("The last scan was completed with errors. Only migrate if you are sure the found errors are not a problem for your site.", "really-simple-ssl"),
'icon' => 'warning',
'dismissible' => true,
),
'no-ssl-scan-has-errors' => array(
'msg' => __("The last scan was completed with errors. Are you sure these issues don't impact your site?", "really-simple-ssl"),
'icon' => 'warning',
'dismissible' => true,
),
),
);
return $notices;
}
/**
* Conditions for a notice
*
* @return string
*/
public function pro_scan_notice() {
if ( !rsssl_get_option('site_has_ssl') ) {
if ( $this->scan_completed_no_errors() == "ERRORS" ) {
return 'has-ssl-scan-has-errors';
} elseif ( ! RSSSL()->licensing->license_is_valid() ) {
return 'no-ssl-no-scan-done-invalid-license';
}
} else {
if ( $this->scan_completed_no_errors() == "COMPLETED" ) {
return 'no-ssl-no-scan-errors';
} elseif ( $this->scan_completed_no_errors() == "ERRORS" ) {
return 'no-ssl-scan-has-errors';
} elseif ( ! RSSSL()->licensing->license_is_valid() ) {
return 'no-ssl-no-scan-done-invalid-license';
}
}
}
/**
* Start a quick scan, invoked via $_GET parameter
*/
public function start_quick_scan_from_url() {
if ( ! isset( $_GET['rsssl_start_scan'] ) ) {
return;
}
if ( ! rsssl_user_can_manage() ) {
return;
}
update_option( "rsssl_scan_active", 'running', false );
update_option( "rsssl_iteration", 1, false );
delete_transient( 'rsssl_scan' );
update_option( "rsssl_progress", 0, false );
update_option( "rsssl_current_action", "", false );
update_option( "rsssl_scan_type", "home", false );
wp_redirect(add_query_arg(array('page'=>'really-simple-security','#'=>'settings/mixed_content_scan'), admin_url( 'options-general.php' ) ));
exit();
}
/**
* Get last scan time
* @return bool|string
*/
private function get_last_scan_time() {
if ( $this->last_scan_time == __( "Never", "really-simple-ssl" ) ) {
return $this->last_scan_time;
}
if ( ! empty( $this->last_scan_time ) ) {
//$date = date(DateTime::RFC850);
return sprintf(
_x( "%s at %s","'date' at 'time'", "really-simple-ssl" ),
date( get_option( 'date_format' ), $this->last_scan_time ),
date( "H:i", $this->last_scan_time )
);
}
return false;
}
/**
* run the scan
*/
public function run_scan() {
if ( get_option( 'rsssl_scan_active' ) !== 'running' ) {
return;
}
//we don't want the ajax request trigger a cron request
if ( isset( $_GET['rsssl_scan_request'] ) ) {
return;
}
$total_iterations = 14;
$iteration = get_option( "rsssl_iteration", 1 );
$in_queue = false;
if ( $iteration == 1 ) {
$this->load_results( true ); //true to reset all values
//get all pages of this website
$this->webpages = $this->get_webpage_list();
$this->queue = 1;
$progress = $this->calculate_queue_progress( 1, 1, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( 0 );
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action', __( "Generating web page list", "really-simple-ssl" ), false );
$this->save_results();
}
if ( $iteration == 2 ) {
$this->load_results();
//find all css and js files
$this->parse_for_css_js_and_external_files( $this->webpages );
$progress = $this->calculate_queue_progress( count( $this->webpages ), $this->queue, $total_iterations, $iteration );
$current_queue = ( $this->queue == 0 ) ? count( $this->webpages ) : $this->queue;
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action',
sprintf( __( "Searching for js and css files and links to external resources in website, %d of %d",
"really-simple-ssl" ), $current_queue,
count( $this->webpages ) ), false );
$in_queue = $this->still_in_queue( count( $this->webpages ) );
$this->save_results();
}
if ( $iteration == 3 ) {
$this->load_results();
//parse these files for http links
$this->css_js_with_mixed_content = $this->parse_for_http( $this->css_js_files, $this->css_js_with_mixed_content );
$progress = $this->calculate_queue_progress( count( $this->css_js_files ), $this->queue, $total_iterations, $iteration );
$current_queue = ( $this->queue == 0 ) ? count( $this->css_js_files ) : $this->queue;
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action', sprintf( __( "Searching for mixed content in css and js files, %s of %s",
"really-simple-ssl" ), $current_queue,
count( $this->css_js_files ) + 2 ), false );
$in_queue = $this->still_in_queue( count( $this->css_js_files ) );
$this->save_results();
}
if ( $iteration == 4 ) {
$this->load_results();
$this->get_file_array();
$this->queue = $this->still_in_queue( 1 );
$progress = $this->calculate_queue_progress( 1, 1, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( 0 );
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action', __( "Generating file list", "really-simple-ssl" ), false );
$this->save_results();
}
if ( $iteration == 5 ) {
$this->load_results();
$this->search_posts_for_external_urls();
//get the number of rows total
$total_post_count = $this->get_total_post_count();
$progress = $this->calculate_queue_progress( $total_post_count, $this->queue, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( $total_post_count );
$current_queue = ( $this->queue == 0 ) ? $total_post_count : $this->queue;
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action',
sprintf( __( "Checking posts for external URLs, %s of %s",
"really-simple-ssl" ), $current_queue,
$total_post_count ), false );
$this->save_results();
}
if ( $iteration == 6 ) {
$this->load_results();
//Also search for widgets with external urls
$this->search_widgets_for_external_urls();
$progress = $this->calculate_queue_progress( 1, 1, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( 0 );
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action', __( "Checking widgets for external URLs", "really-simple-ssl" ), false );
$this->save_results();
}
if ( $iteration == 7 ) {
$this->load_results();
$this->search_postmeta_for_external_urls();
$progress = $this->calculate_queue_progress( 1, 1, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( 0 );
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action',
__( "Checking which postmeta contain external resources",
"really-simple-ssl" ), false );
$this->save_results();
}
if ( $iteration == 8 ) {
$this->load_results();
//check which of these files cannot load over ssl
$this->find_blocked_resources( $this->external_resources );
$progress = $this->calculate_queue_progress( count( $this->external_resources ), $this->queue, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( count( $this->external_resources ) );
$current_queue = ( $this->queue == 0 ) ? count( $this->external_resources ) : $this->queue;
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action', sprintf( __( "Checking which resources can't load over ssl, %s of %s", "really-simple-ssl" ), $current_queue, count( $this->external_resources ) ), false );
$this->save_results();
}
if ( $iteration == 9 ) {
$this->load_results();
$external_css_js_files = $this->get_external_css_js_files();
//check which of these files contain http links.
$this->external_css_js_with_mixed_content = $this->parse_external_files_for_http( $external_css_js_files, $this->external_css_js_with_mixed_content );
$progress = $this->calculate_queue_progress( count( $external_css_js_files ), $this->queue, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( count( $external_css_js_files ) );
$current_queue = ( $this->queue == 0 ) ? count( $external_css_js_files ) : $this->queue;
update_option( 'rsssl_progress', $progress , false);
update_option( 'rsssl_current_action',
sprintf( __( "Checking if external js or css files contain http links, %s of %s",
"really-simple-ssl" ), $current_queue,
count( $external_css_js_files ) ), false );
$this->save_results();
}
if ( $iteration == 10 ) {
$this->load_results();
//search in php files and db for references to ext res.
$this->search_files_for_urls();
$progress = $this->calculate_queue_progress( count( $this->file_array ), $this->queue, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( count( $this->file_array ) );
$current_queue = ( $this->queue == 0 ) ? count( $this->file_array ) : $this->queue;
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action',
sprintf( __( "Looking up blocked resources in files, %s of %s",
"really-simple-ssl" ), $current_queue,
count( $this->file_array ) ), false );
$this->save_results();
}
if ( $iteration == 11 ) {
$this->load_results();
$this->find_posts_with_blocked_urls();
$this->queue = 1;
$progress = $this->calculate_queue_progress( 1, 1, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( 0 );
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action',
__( "Looking up blocked resources in posts",
"really-simple-ssl" ), false );
$this->save_results();
}
if ( $iteration == 12 ) {
$this->load_results();
$this->find_postmeta_with_blocked_urls();
$this->queue = 1;
$progress = $this->calculate_queue_progress( 1, 1, $total_iterations, $iteration );
$in_queue = $this->still_in_queue( 0 );
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action',
__( "Looking up blocked resources in postmeta",
"really-simple-ssl" ), false );
$this->save_results();
}
if ( $iteration == 13 ) {
$this->load_results();
$this->find_widgets_with_blocked_urls();
$this->queue = 1;
$progress = $this->calculate_queue_progress( 1, 1,
$total_iterations, $iteration )
- 1;//prevent progress from being 100 before last step
$in_queue = $this->still_in_queue( 0 );
update_option( 'rsssl_progress', $progress, false );
update_option( 'rsssl_current_action',
__( "Looking up blocked resources in widgets",
"really-simple-ssl" ), false );
$this->save_results();
}
if ( $iteration == 14 ) {
$this->load_results();
//dropped brute force database search
update_option( 'rsssl_current_action', __( "Finished scan", "really-simple-ssl" ), false );
$in_queue = $this->still_in_queue( 1 );
update_option( 'rsssl_progress', 100, false );
update_option( "rsssl_scan_active", 'stop', false );
$this->save_results();
}
if ( ! $in_queue ) {
$iteration ++;
}
update_option( "rsssl_iteration", $iteration, false );
}
/**
* Get total post count
* @return int
*/
private function get_total_post_count() {
global $wpdb;
$args = array(
'public' => true,
);
$post_types = get_post_types( $args );
$post_types_query = array();
foreach ( $post_types as $post_type ) {
$post_types_query[] = " post_type = '" . $post_type . "'";
$post_types_query[] = " post_type = 'wp_block'";
}
$posttypes_query = implode( " OR ", $post_types_query );
$count = get_transient( "rsssl_scan_post_count" );
if ( ! $count ) {
$sql = "select count(*) from $wpdb->posts where post_status='publish' and (" . $posttypes_query . ")";
$count = $wpdb->get_var( $sql );
set_transient( "rsssl_scan_post_count", $count, DAY_IN_SECONDS );
}
return $count;
}
/**
*Lookup all posts that have a blocked external url
*/
private function find_posts_with_blocked_urls() {
$posts_array = array();
$blocked_urls = $this->blocked_resources;
$posts_with_external_resources = $this->posts_with_external_resources;
foreach ( $posts_with_external_resources as $post_id => $urls ) {
//check if one of the found urls is in the blocked resources array.
foreach ( $urls as $url ) {
if ( $this->in_array_r( $url, $blocked_urls ) ) {
//add post to post list with blocked resources
if ( ! in_array( $post_id, $posts_array ) ) {
$posts_array[] = $post_id;
}
$this->traced_urls[] = $url;
}
}
}
$this->posts_with_blocked_resources = $posts_array;
}
/**
* find post meta fields with blocked url's
*/
private function find_postmeta_with_blocked_urls() {
$postmeta_array = array();
$blocked_urls = $this->blocked_resources;
$postmeta_with_external_resources = $this->postmeta_with_external_resources;
foreach ( $postmeta_with_external_resources as $post_id => $urls ) {
//check if one of the found urls is in the blocked resources array.
foreach ( $urls as $url ) {
if ( $this->in_array_r( $url, $blocked_urls ) ) {
//add post to post list with blocked resources
if ( ! in_array( $post_id, $postmeta_array ) ) {
$postmeta_array[] = $post_id;
}
$this->traced_urls[] = $url;
}
}
}
$this->postmeta_with_blocked_resources = $postmeta_array;
}
/**
* Get widget data by title
* @param $title
*
* @return array|bool
*/
public function get_widget_data( $title ) {
//Get the widget type, before the -
$type = substr( $title, 0, strpos( $title, '-' ) );
//Get the widget id, after type -
$id = substr( $title, strpos( $title, '-' ) + 1 );
//Get the widget options, save to array to retrieve the HTML
$widget_array = get_option( "widget_" . $type );
$widget_html = "";
$widget_title = "";
$type_found = false;
if ( isset( $widget_array[ $id ]["content"] ) ) {
$type_found = true;
$widget_html = $widget_array[ $id ]["content"];
}
if ( isset( $widget_array[ $id ]["url"] ) ) {
$type_found = true;
$widget_html = $widget_array[ $id ]["url"];
}
if ( isset( $widget_array[ $id ]["text"] ) ) {
$type_found = true;
$widget_html = $widget_array[ $id ]["text"];
}
if ( isset( $widget_array[ $id ]["title"] ) ) {
$widget_title = $widget_array[ $id ]["title"];
}
if ( $type_found ) {
return array(
"type" => $type,
"id" => $id,
"html" => $widget_html,
"title" => $widget_title
);
} else {
return false;
}
}
/**
* Save the edited html of a widget to the widget in question.
* @param string $title
* @param string $content
*
* @return bool
*/
public function update_widget_data( $title, $content ) {
//Get the widget type, before the -
$type = substr( $title, 0, strpos( $title, '-' ) );
//Get the widget id, after type -
$id = substr( $title, strpos( $title, '-' ) + 1 );
//Get the widget options, save to array to retrieve the HTML
$widget_array = get_option( "widget_" . $type );
$type_found = false;
if ( isset( $widget_array[ $id ]["content"] ) ) {
$type_found = true;
$widget_array[ $id ]["content"] = $content;
}
if ( isset( $widget_array[ $id ]["url"] ) ) {
$type_found = true;
$widget_array[ $id ]["url"] = $content;
}
if ( isset( $widget_array[ $id ]["text"] ) ) {
$type_found = true;
$widget_array[ $id ]["text"] = $content;
}
if ( $type_found ) {
update_option( "widget_" . $type, $widget_array, false );
}
return true;
}
private function get_widget_area( $search_widget_title ) {
$widget_areas = wp_get_sidebars_widgets();
foreach ( $widget_areas as $widget_area_name => $widgets ) {
$found = false;
foreach ( $widgets as $widget_title ) {
if ( $search_widget_title == $widget_title ) {
$found = true;
}
}
if ( $found ) {
return $widget_area_name;
}
}
return false;
}
/**
* Get the friendly title for a widget area
*
* @param string widget index
*
* @return string
*/
private function get_widget_title( $area ) {
global $wp_registered_sidebars, $wp_registered_widgets;
if ( isset( $wp_registered_sidebars[ $area ] ) ) {
$title = $wp_registered_sidebars[ $area ]["name"];
}
if ( isset( $wp_registered_widgets[ $area ] ) ) {
$title = $wp_registered_widgets[ $area ]["name"];
}
return $title;
}
/**
*
* Search for widgets in external URLs
*
**/
private function search_widgets_for_external_urls() {
//$external_resources = $this->external_resources;
$patterns = $this->external_domain_patterns();
$url_only_patterns = $this->external_domain_patterns( true );
$widgets_array = array();
//Check $type and pattern
$widget_areas = wp_get_sidebars_widgets();
foreach ( $widget_areas as $widgets ) {
foreach ( $widgets as $widget_title ) {
$widget_data = $this->get_widget_data( $widget_title );
if ( ! $widget_data ) {
continue;
}
if ( ! isset( $widget_data["html"] )
|| ! isset( $widget_data["type"] )
) {
continue;
}
$html = $widget_data["html"];
$type = $widget_data["type"];
//media_image is different because the URL pattern is different. media_image ONLY contains the URL. Other widgets contain HTML
posts where post_status='publish' and ("
. $posttypes_query . ") LIMIT " . $this->queue . ", "
. $this->nr_of_rows_in_one_run;
$results = $wpdb->get_results( $query );
foreach ( $results as $result ) {
$str = $result->post_content;
foreach ( $patterns as $pattern ) {
if ( preg_match_all( $pattern, $str, $matches,
PREG_PATTERN_ORDER )
) {
foreach ( $matches[1] as $key => $match ) {
if ( empty( $matches[2][ $key ] ) ) {
continue;
}
//list to show all posts with external urls
$url = $matches[1][ $key ] . $matches[2][ $key ];
//check if already in array
if ( ! isset( $posts_array[ $result->ID ] )
|| ( isset( $posts_array[ $result->ID ] )
&& ! in_array( $url,
$posts_array[ $result->ID ] ) )
) {
$posts_array[ $result->ID ][] = $url;
}
//list to check all external urls.
if ( ! in_array( $url, $external_resources ) ) {
$external_resources[] = $url;
}
//list to track those resource back to where they came from.
$this->source_of_resource[ $url ] = $result->ID;
}
}
}
}
$this->queue += $this->nr_of_rows_in_one_run;
$this->posts_with_external_resources = $posts_array;
$this->external_resources = $external_resources;
}
/*
* Search the wp_postmeta table for external URLs
*/
public function search_postmeta_for_external_urls() {
global $wpdb;
$external_resources = $this->external_resources;
$postmeta_array = $this->postmeta_with_external_resources;
$patterns = $this->external_domain_patterns();
$query
= "select post_id, meta_key, meta_value from $wpdb->postmeta LIMIT "
. $this->queue . ", " . $this->nr_of_rows_in_one_run;
$results = $wpdb->get_results( $query );
foreach ( $results as $result ) {
$str = $result->meta_value;
foreach ( $patterns as $pattern ) {
if ( preg_match_all( $pattern, $str, $matches,
PREG_PATTERN_ORDER )
) {
foreach ( $matches[1] as $key => $match ) {
if ( empty( $matches[2][ $key ] ) ) {
continue;
}
//list to show all posts with external urls
$url = $matches[1][ $key ] . $matches[2][ $key ];
//check if already in array
//if (!isset($postmeta_array[$result->post_id]) || (isset($postmeta_array[$result->post_id]) && !in_array($url, $postmeta_array[$result->post_id])))
$postmeta_array[ $result->post_id ][ $result->meta_key ]
= $url;
//list to check all external urls.
if ( ! in_array( $url, $external_resources ) ) {
$external_resources[] = $url;
}
//list to track those resource back to where they came from.
$this->source_of_resource[ $url ] = $result->post_id;
}
}
}
}
$this->queue += $this->nr_of_rows_in_one_run;
$this->postmeta_with_external_resources = $postmeta_array;
$this->external_resources = $external_resources;
}
/**
* Check if this URL should be ignored
* Should be ignored if
* - show ignored URLs is false OR
* - the url is in the ignored list
*
*/
public function ignore( $url ) {
$show_ignore_urls = get_option( "rsssl_show_ignore_urls" );
$ssl_url = str_replace( "http://", "https://", $url );
$http_url = str_replace( "https://", "http://", $url );
//When ignored URLS is off, ignore will be true
if ( ! $show_ignore_urls && in_array( $ssl_url, $this->ignored_urls )
|| in_array( $http_url, $this->ignored_urls )
|| in_array( $http_url, $this->safe_domains )
) {
return true;
}
return false;
}
/**
* check each item in the array to see if it can load over https, if no, adds it to the output array.
*/
private function find_blocked_resources( $external_resources ) {
$blocked_urls = $this->blocked_resources;
$start = $this->queue;
$count = 0;
for ( $i = $start; $i < count( $external_resources ); ++ $i ) {
$this->queue = $i + 1;
//sometimes indexes are removed as doubles, skip to next.
if ( ! isset( $external_resources[ $i ] ) ) {
continue;
}
$count ++;
$url = $external_resources[ $i ];
$ssl_url = str_replace( "http://", "https://", $url );
if ( ! in_array( $url, $blocked_urls ) ) {
$html = $this->get_contents( $ssl_url );
//if the mixed content fixer is active, the url might be https.
if ( $this->error_number != 0 ) {
$blocked_urls[] = str_replace( "https://", "http://",
$url );
}
}
if ( $count > $this->nr_requests_in_one_run ) {
break;
}
}
$this->blocked_resources = $blocked_urls;
}
/**
*
* Links in these files can contain http links, if these domains can be loaded over https.
* Generates a list of files with urls that could not be loaded over https.
*/
private function search_files_for_urls() {
$file_array = $this->file_array;
// var_dump( $file_array);
// die;
$start = $this->queue;
$count = 0;
for ( $i = $start; $i < count( $file_array ); ++ $i ) {
$this->queue = $i + 1;
//sometimes indexes are removed as doubles, skip to next.
if ( ! isset( $file_array[ $i ] ) ) {
continue;
}
$count ++;
$file = $file_array[ $i ];
if ( file_exists( $file ) ) {
$html = file_get_contents( $file );
//search the files where blocked resources are used.
foreach ( $this->blocked_resources as $url ) {
if ( strpos( $html, $url ) !== false ) {
if ( ! isset( $this->files_with_blocked_resources[ $file ] )
|| ( isset( $this->files_with_blocked_resources[ $file ] )
&& ! in_array( $url,
$this->files_with_blocked_resources[ $file ] ) )
) {
$this->files_with_blocked_resources[ $file ][]
= $url;
}
//by adding this one to a tracing list, we keep track of the urls that are accounted for.
if ( ! in_array( $url, $this->traced_urls ) ) {
$this->traced_urls[] = $url;
}
}
}
//search the files where external css or js is used.
foreach (
$this->external_css_js_with_mixed_content as $url => $value
) {
if ( strpos( $html, $url ) !== false ) {
if ( ! isset( $this->files_with_external_css_js[ $file ] )
|| ( isset( $this->files_with_external_css_js[ $file ] )
&& ! in_array( $url,
$this->files_with_external_css_js[ $file ] ) )
) {
$this->files_with_external_css_js[ $file ][] = $url;
}
//by adding this one to a tracing list, we keep track of the urls that are accounted for.
if ( ! in_array( $url, $this->traced_urls ) ) {
$this->traced_urls[] = $url;
}
}
}
}
if ( $count > $this->nr_files_in_one_run ) {
break;
}
}
}
/**
* get list of webpages on this site, only on per posttype, as we only need to check each template
*/
private function get_webpage_list() {
$scan_type = get_option( "rsssl_scan_type" );
$url_list = array();
$url_list[] = home_url();
if ( $scan_type != "home" ) {
$menus = get_nav_menu_locations();
foreach ( $menus as $location => $menu_id ) {
$menu_items = wp_get_nav_menu_items( $menu_id );
foreach ( (array) $menu_items as $key => $menu_item ) {
//only insert url if on the same domain as homeurl
if ( isset( $menu_item->url )
&& strpos( $menu_item->url, home_url() ) !== false
) {
$url_list[] = $menu_item->url;
}
}
}
//also add an url from each post type that is used in this website.
$args = array(
'public' => true,
);
$post_types = get_post_types( $args );
$post_types_query = array();
foreach ( $post_types as $post_type ) {
$post_types_query[] = " post_type = '" . $post_type . "'";
}
$sql = implode( " OR ", $post_types_query );
global $wpdb;
$sql
= "SELECT ID FROM $wpdb->posts where post_status='publish' and ("
. $sql . ") group by post_type";
$results = $wpdb->get_results( $sql );
foreach ( $results as $result ) {
if ( ! in_array( get_permalink( $result->ID ),
$url_list )
) {
$url_list[] = get_permalink( $result->ID );
}
}
}
return $url_list;
}
/**
* Create an array of all files we have to check in the plugins and theme directory.
*/
private function get_file_array() {
$childtheme_dir = get_stylesheet_directory();
$parenttheme_dir = get_template_directory();
//$plugin_dir = dirname(dirname( __FILE__ ));
$file_array = $this->get_filelist_from_dir( $childtheme_dir );
//if parentthemedir and childtheme dir are different, check those as well
if ( strcasecmp( $childtheme_dir, $parenttheme_dir ) == 0 ) {
$file_array = array_merge( $file_array,
$this->get_filelist_from_dir( $parenttheme_dir ) );
}
//$file_array = array_merge($file_array, $this->get_filelist_from_dir($plugin_dir));
$this->file_array = array_unique( $file_array );
}
/**
* Get dirname of uploads folder
* @return string
*/
public function uploads_dirname() {
// defaults to uploads.
$upload_dir_name = "uploads";
if ( defined( 'UPLOADS' ) ) {
$upload_dir_name = str_replace( trailingslashit( WP_CONTENT_DIR ),
'', untrailingslashit( UPLOADS ) );
}
return $upload_dir_name;
}
public function get_path_to( $directory, $file ) {
if ( $directory != "plugins" && $directory != "mu-plugins" && $directory != $this->uploads_dirname()
&& $directory != "themes"
) {
return $file;
}
//find position within wp-content
$needle = "wp-content/" . $directory . "/";
$pos = strpos( $file, $needle );
if ( $pos !== false ) {
$file = substr( $file, $pos + strlen( $needle ) );
}
return "wp-content/" . $directory . "/" . $file;
}
/**
* Get a list of files from a directory, with the extensions as passed.
*
* @param string $path : path to directory to search in.
*
* @return array
*/
private function get_filelist_from_dir( $path ) {
$filelist = array();
$extensions = array( "php" );
if ( $handle = opendir( $path ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( $file != "." && $file != ".." ) {
$file = $path . '/' . $file;
$ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
//we also exclude backup files generated by really simple Security, rsssl-bkp-
if ( is_file( $file ) && in_array( $ext, $extensions )
&& ( strpos( $file, "rsssl-bkp-" ) === false )
) {
$filelist[] = $file;
} elseif ( is_dir( $file ) ) {
if ( strpos( $file, "really-simple-ssl" ) === false ) {
$filelist = array_merge( $filelist, $this->get_filelist_from_dir( $file) );
}
}
}
}
closedir( $handle );
}
return $filelist;
}
/**
* These files are loaded in the website, but cannot be dynamically changed, so have to contain only https links.
* Input: array of js and css files, by url
* Output: array of js and css files, that contain http references.
*/
private function parse_for_http( $urls, $files_with_http ) {
$url_pattern = '([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-]?)';
$patterns = array(
'/url\([\'"]?\K(http:\/\/)' . $url_pattern . '/i',
'/].*?href=[\'"]\K(http:\/\/)' . $url_pattern . '/i',
'/].*?src=[\'"]\K(http:\/\/)' . $url_pattern
. '/i',
'/