diff --git a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php index 2220cb62d..d7232d2f9 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php @@ -1383,7 +1383,12 @@ class REST_Items_Controller extends REST_Controller { ], 400); } $secret_key = get_option("tnc_option_recaptch_secret_key"); - $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret_key&response=".$captcha_data."&remoteip=".$_SERVER['REMOTE_ADDR'])); + $api_url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret_key&response=".$captcha_data."&remoteip=".$_SERVER['REMOTE_ADDR']; + + $response = wp_remote_get( $api_url ); + $body = wp_remote_retrieve_body( $response ); + $response = json_decode($body); + if ($response->success) { return true; } else { diff --git a/src/classes/class-tainacan-media.php b/src/classes/class-tainacan-media.php index 87412b9e7..0b008e627 100644 --- a/src/classes/class-tainacan-media.php +++ b/src/classes/class-tainacan-media.php @@ -103,54 +103,11 @@ class Media { * @return string the file path */ public function save_remote_file($url) { - set_time_limit(0); - - $filename = tempnam(sys_get_temp_dir(), basename($url)); - - # Open the file for writing... - self::$file_handle = fopen($filename, 'w+'); - self::$file_name = $filename; - - $callback = function ($ch, $str) { - $len = fwrite(self::$file_handle, $str); - return $len; - }; - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_FILE, self::$file_handle); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); # optional - curl_setopt($ch, CURLOPT_TIMEOUT, -1); # optional: -1 = unlimited, 3600 = 1 hour - curl_setopt($ch, CURLOPT_VERBOSE, false); # Set to true to see all the innards - - # Only if you need to bypass SSL certificate validation - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - - # Assign a callback function to the CURL Write-Function - curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); - - # Execute the download - note we DO NOT put the result into a variable! - curl_exec($ch); - if (curl_errno($ch)) { - $error_msg = curl_error($ch); - # Close CURL - curl_close($ch); - # Close the file pointer - fclose(self::$file_handle); - throw new \Exception( "[save_remote_file]:" . $error_msg); - } - - # Close CURL - curl_close($ch); - - # Close the file pointer - fclose(self::$file_handle); - - return $filename; + $filename = download_url($url, 900); + if( is_wp_error($filename) ) { + throw new \Exception( "[save_remote_file]:" . implode("\n", $filename->get_error_messages())); + } + return $filename; } diff --git a/src/classes/class-tainacan-private-files.php b/src/classes/class-tainacan-private-files.php index 9e489b2b6..160525220 100644 --- a/src/classes/class-tainacan-private-files.php +++ b/src/classes/class-tainacan-private-files.php @@ -117,12 +117,12 @@ class Private_Files { // regular ajax uploads via Admin Panel will send post_id if ( isset($_REQUEST['post_id']) && $_REQUEST['post_id'] ) { - $post_id = $_REQUEST['post_id']; + $post_id = sanitize_text_field($_REQUEST['post_id']); } // API requests to media endpoint will send post if ( false === $post_id && isset($_REQUEST['post']) && is_numeric($_REQUEST['post']) ) { - $post_id = $_REQUEST['post']; + $post_id = sanitize_text_field($_REQUEST['post']); } // tainacan internals, scripts and tests, will set this global @@ -191,7 +191,7 @@ class Private_Files { $upload_dir = wp_get_upload_dir(); $base_upload_url = preg_replace('/^https?:\/\//', '', $upload_dir['baseurl']); - $requested_uri = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; + $requested_uri = sanitize_text_field($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); if ( strpos($requested_uri, $base_upload_url) === false ) { // Not uploads diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 4a035b84e..48ff44919 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -564,7 +564,6 @@ class Item extends Entity { */ public function get_metadata_as_html($args = array()) { - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $return = ''; @@ -633,7 +632,7 @@ class Item extends Entity { } - return $return; + return wp_kses_tainacan($return); } @@ -702,7 +701,7 @@ class Item extends Entity { } } - return $return; + return wp_kses_tainacan($return); } @@ -772,8 +771,7 @@ class Item extends Entity { } - return apply_filters("tainacan-item-get-document-as-html", $output, $img_size, $this); - + return apply_filters("tainacan-item-get-document-as-html", wp_kses_tainacan($output), $img_size, $this); } /** @@ -806,8 +804,7 @@ class Item extends Entity { $output .= $embed; } } - - return $output; + return wp_kses_tainacan($output); } @@ -841,7 +838,7 @@ class Item extends Entity { } } - return $link; + return esc_url($link); } /** diff --git a/src/classes/exporter/class-tainacan-csv.php b/src/classes/exporter/class-tainacan-csv.php index a9bab17cf..ca174e622 100644 --- a/src/classes/exporter/class-tainacan-csv.php +++ b/src/classes/exporter/class-tainacan-csv.php @@ -312,7 +312,7 @@ class CSV extends Exporter {
: on this link.', 'tainacan')); ?> diff --git a/src/classes/importer/class-tainacan-flickr-importer.php b/src/classes/importer/class-tainacan-flickr-importer.php index 9865c2891..920c212ff 100644 --- a/src/classes/importer/class-tainacan-flickr-importer.php +++ b/src/classes/importer/class-tainacan-flickr-importer.php @@ -188,7 +188,9 @@ class Flickr_Importer extends Importer { $this->add_log('url ' . $api_url); - $json = json_decode(file_get_contents($api_url)); + $response = wp_remote_get( $api_url ); + $body = wp_remote_retrieve_body( $response ); + $json = json_decode($body); if( $json && isset($json->photoset) ){ return $json; } @@ -203,7 +205,10 @@ class Flickr_Importer extends Importer { $this->add_log('url ' . $api_url); - $json = json_decode(file_get_contents($api_url)); + $response = wp_remote_get( $api_url ); + $body = wp_remote_retrieve_body( $response ); + $json = json_decode($body); + if( $json && isset($json->photos) ){ return $json; @@ -218,7 +223,9 @@ class Flickr_Importer extends Importer { $this->add_log('url ' . $api_url); - $json = json_decode(file_get_contents($api_url)); + $response = wp_remote_get( $api_url ); + $body = wp_remote_retrieve_body( $response ); + $json = json_decode($body); if( $json && isset($json->photo) ){ return $json; @@ -428,8 +435,9 @@ class Flickr_Importer extends Importer { . $id . $this->format; $this->add_log('url ' . $api_url); - - $json = json_decode(file_get_contents($api_url)); + $response = wp_remote_get( $api_url ); + $body = wp_remote_retrieve_body( $response ); + $json = json_decode($body); if( $json && isset($json->photo) ){ return $json; diff --git a/src/classes/importer/class-tainacan-test-importer.php b/src/classes/importer/class-tainacan-test-importer.php index 8494244c1..0e4d6b948 100644 --- a/src/classes/importer/class-tainacan-test-importer.php +++ b/src/classes/importer/class-tainacan-test-importer.php @@ -125,7 +125,7 @@ class Test_Importer extends Importer {