Skip to content
Snippets Groups Projects
Commit daa1f050 authored by ale's avatar ale
Browse files

Initial commit

POC
parents
Branches
Tags v0.0.1
No related merge requests found
Pipeline #54064 passed
include: "https://git.autistici.org/noblogs/composer-publish/raw/master/common.yml"
\ No newline at end of file
{
"name": "noblogs/wp-mat",
"description": "git@git.autistici.org:noblogs/wp-mat.git",
"type": "wordpress-plugin",
"require": {
"composer/installers": "^1.9"
},
"authors": [
{
"name": "lucha",
"email": "lucha@paranoici.org"
}
]
}
<?php
/**
* Plugin Name: wp-mat
* Plugin URI: https://git.autistici.org/noblogs/wp-mat
* Description: Process uploaded files through MAT
* Version: 0.0.1
* Author: Autistici/Inventati
* Author URI: https://www.autistici.org/
* License: MIT
* License URI: http://opensource.org/licenses/MIT
*/
// Check if the mime type is one of those we want to clean up.
function wp_mat_is_supported_mime_type($mimetype) {
if (substr($mimetype, 0, 6) === "image/") {
return true;
}
return false;
}
// Make a POST request to the MAT service with the uploaded file and
// store the results into '$output_path'.
function wp_mat_issue_request($path, $mimetype, $output_path) {
$upfile = new CURLFile($path, $mimetype, 'input');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, WP_MAT_ENDPOINT);
curl_setopt($curl, CURLOPT_USERAGENT, 'Wordpress');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
"file" => $upfile,
"mime_type" => $mimetype,
));
if (($fp = fopen($output_path, "wb")) === false) {
throw new Exception("fopen error for filename $output_path");
}
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
$ret = curl_exec($curl);
fclose($fp);
curl_close($curl);
return $ret;
}
function wp_mat_handle_upload_prefilter(&$file) {
if (isset($file['error'])) {
return $file;
}
if (!wp_mat_is_supported_mime_type($file['type'])) {
return $file;
}
$new_tmp_file = tempnam("", "wp_mat_");
if (wp_mat_issue_request($file['tmp_name'], $file['type'], $new_tmp_file)) {
unlink($file['tmp_name']);
return array(
"name" => $file['name'],
"type" => $file['type'],
"tmp_name" => $new_tmp_file,
"size" => filesize($new_tmp_file),
);
} else {
@unlink($new_tmp_file);
}
return $file;
}
add_filter('wp_handle_upload_prefilter', 'wp_mat_handle_upload_prefilter');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment