-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnostr-media.php
719 lines (570 loc) · 28.7 KB
/
nostr-media.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
/**
* Plugin Name: Nostr Media Uploads
* Description: Host the images you post on nostr on your own WordPress installation
* Version: 0.11
* Author: Fabian Lachman
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// Include Composer autoload file
require __DIR__ . '/vendor/autoload.php';
use swentel\nostr\Event\Event;
use swentel\nostr\Key\Key;
// Add the custom field to the profile page
add_action('show_user_profile', 'nmu_add_custom_user_profile_fields');
add_action('edit_user_profile', 'nmu_add_custom_user_profile_fields');
function nmu_add_custom_user_profile_fields($user) {
?>
<h3>Nostr Media</h3>
<table class="form-table">
<tr>
<th>
<label for="nostrNpub">Public key (npub)</label>
</th>
<td>
<input type="text" id="nostrNpub" name="nostrNpub" value="<?php echo esc_attr(get_the_author_meta('nostrNpub', $user->ID)); ?>" class="regular-text" />
<br /><span class="description">Only this npub will be allowed to upload media</span>
</td>
</tr>
</table>
<?php
}
// Save the value of the custom field
add_action('personal_options_update', 'nmu_save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'nmu_save_custom_user_profile_fields');
function nmu_save_custom_user_profile_fields($user_id) {
if (!current_user_can('edit_user', $user_id)) {
return ["valid" => false];
}
$nostrNpub = $_POST['nostrNpub'];
// Add validation for the nostrNpub field
if ((substr($nostrNpub, 0, 5) !== 'npub1') && ($nostrNpub !== "")) {
wp_die('Error: Key should start with "npub1". <a href="javascript:history.back()">Go back</a>.');
return ["valid" => false];
}
update_user_meta($user_id, 'nostrNpub', $nostrNpub);
}
// Check if the Authorization header matches valid NIP98 HTTP Auth
function nmu_validate_authorization_header() {
$headers = getallheaders();
if (isset($headers['Authorization'])) {
if (substr($headers['Authorization'], 0, 6) !== 'Nostr ') {
if (WP_DEBUG) {
error_log("Invalid Authorization header: {$headers['Authorization']}");
}
return ["valid" => false, "message" => "Invalid Authorization header."];
}
// Remove "Nostr " prefix from $headers['Authorization']
$base64 = substr($headers['Authorization'], 6);
// Decode the base64 encoded string
$jsonString = base64_decode($base64);
// Verify event signature
if (!Event::verify($jsonString)) {
if (WP_DEBUG) {
error_log("Invalid signature: {$jsonString}");
}
return ["valid" => false, "message" => "Invalid signature."];
}
// Decode the JSON string
$json = json_decode($jsonString, true);
$pubkey = $json["pubkey"];
// Check that pubkey is 64 characters long
if (strlen($pubkey) !== 64) {
if (WP_DEBUG) {
error_log("Invalid pubkey: {$pubkey}");
}
return ["valid" => false, "message" => "Invalid pubkey."];
}
$kind = $json["kind"];
// Check that kind is 27235
if ($kind !== 27235) {
if (WP_DEBUG) {
error_log("Wrong kind: {$kind}");
}
return ["valid" => false, "message" => "Invalid kind."];
}
$created_at = $json["created_at"];
// Check if created_at is less then 5 minutes ago
if (time() - $created_at > 300) {
if (WP_DEBUG) {
error_log("Kind 27235 Event is too old, created_at: {$created_at}");
}
return ["valid" => false, "message" => "Kind 27235 Event is too old."];
}
// TODO: Doesn't work:
// There is no way to get a body hash in PHP because php://input is not available with enctype="multipart/form-data" 🤷♂️🤷♂️🤷♂️
// Get the body hash
// $bodyHash = hash('sha256', file_get_contents('php://input'));
// check if hash matches payload tag
$didHaveValidU = false;
$didHaveValidMethod = false;
// $didHaveValidPayload = false; 🤷♂️🤷♂️🤷♂️
foreach (array_values($json["tags"]) as $tag => $value) {
switch ($value[0]) {
case "method":
if ($value[1] == "POST") {
$didHaveValidMethod = true;
}
else {
return ["valid" => false, "message" => "Invalid \"method\" tag"];
}
break;
case "u":
$base_url = get_site_url();
$api_url = $base_url . '/wp-json/nostrmedia/v1/upload/';
if (WP_DEBUG) {
error_log("Checking u {$value[1]} against api url {$api_url}");
}
if ($value[1] != $api_url) {
return ["valid" => false, "message" => "Invalid \"u\" tag"];
}
$didHaveValidU = true;
break;
// case "payload": 🤷♂️🤷♂️🤷♂️
// error_log("Checking payload {$value[1]} against body hash {$bodyHash}");
// if ($value[1] != $bodyHash) {
// return ["valid" => false, "message" => "Invalid \"payload\" tag"];
// }
// $didHaveValidPayload = true;
// break;
}
}
if (!$didHaveValidU || !$didHaveValidMethod) {
// if (!$didHaveValidU || !$didHaveValidMethod || !$didHaveValidPayload) { 🤷♂️🤷♂️🤷♂️
if (WP_DEBUG) {
error_log("Missing \"u\" or \"payload\" tag");
}
return ["valid" => false, "message" => "Missing \"u\" or \"payload\" tag"];
}
// convert pubkey to npub
$keys = new Key();
$npub = $keys->convertPublicKeyToBech32($pubkey);
// Check if we have a WordPress user with that npub
$users = get_users(array(
'meta_key' => 'nostrNpub',
'meta_value' => $npub,
'number' => 1,
));
if (!empty($users)) {
return [
"valid" => true,
"json" => $json,
"userId" => $users[0]->ID
];
}
if (WP_DEBUG) {
error_log("No user found with that npub: {$npub}");
}
return ["valid" => false, "message" => "No user found with that npub."];
}
return ["valid" => false, "message" => "Missing Authorization header."];
}
// Handle the image upload
add_action('rest_api_init', function() {
register_rest_route('nostrmedia/v1', '/upload/', array(
'methods' => 'POST',
'callback' => 'nmu_handle_image_upload',
));
});
// Disable default image sizes
function nmu_disable_default_image_sizes($sizes) {
return [];
}
function nmu_handle_image_upload() {
$base_directory = WP_CONTENT_DIR . '/uploads';
$base_url = content_url('/uploads');
$isValid = nmu_validate_authorization_header();
if ($isValid["valid"]) {
if (!function_exists('wp_handle_upload')) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
$mediafile_paramname = isset($_FILES['mediafile']) ? "mediafile" : "file";
$uploadedfile = [];
global $original_hash;
// var_dump($_FILES[$mediafile_paramname]['tmp_name']);
if (is_array($_FILES[$mediafile_paramname]['tmp_name'])) {
$uploadedfile = array(
'name' => $_FILES[$mediafile_paramname]['name'][0],
'type' => $_FILES[$mediafile_paramname]['type'][0],
'tmp_name' => $_FILES[$mediafile_paramname]['tmp_name'][0],
'error' => $_FILES[$mediafile_paramname]['error'][0],
'size' => $_FILES[$mediafile_paramname]['size'][0]
);
$original_hash = hash_file('sha256', $_FILES[$mediafile_paramname]['tmp_name'][0]);
}
else {
$uploadedfile = $_FILES[$mediafile_paramname];
$original_hash = hash_file('sha256', $_FILES[$mediafile_paramname]['tmp_name']);
}
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
// WP saves an image and then creates scaled version of it with suffix.
// We need <scaled hash>.ext to return to nostr client.
// But WP generates <whatever>-scaled.ext or <whatever>-150x150.ext etc.
// Or with a WebP plug-in it generates <whatever>-jpg.webp
// So we need to save the original as <original hash>.ext, then WP can
// generate <original hash>-scaled.ext etc, then we can rename <original hash>-scaled.ext to <scaled hash>.ext
// This way the nostr client gets the scaled version as <scaled hash>.ext and WP media backend
// will list: <scaled hash>.ext and in the detail it have a link to <original hash>.ext
if ($movefile && !isset($movefile['error'])) {
add_filter('intermediate_image_sizes_advanced', 'nmu_disable_default_image_sizes');
// Insert the file into the media library
require_once(ABSPATH . 'wp-admin/includes/image.php');
$filetype = wp_check_filetype(basename($movefile['file']), null);
// Save uploaded file to <hash>.ext
$pathinfo = pathinfo($movefile['file']);
$new_original_path = $pathinfo['dirname'] . '/' . $original_hash . '.' . $pathinfo['extension'];
rename($movefile['file'], $new_original_path);
$attachment = array(
'guid' => $new_original_path,
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($movefile['file'])),
'post_content' => '',
'post_status' => 'inherit',
'post_author' => $isValid["userId"]
);
$attach_id = wp_insert_attachment($attachment, $new_original_path);
if (strpos($filetype['type'], "video/") !== 0) { // should be image types here
$attach_data = wp_generate_attachment_metadata($attach_id, $new_original_path);
// Assign default tag (if one is selected on Settings -> Media)
$default_tag_id = get_option('nmu_default_tag');
if (!empty($default_tag_id)) {
wp_set_object_terms($attach_id, array((int) $default_tag_id), 'post_tag', true);
}
unset($attach_data["image_meta"]);
// If there is no scaled version, the scaled path and hash will default back to the original path and hash:
$scaled_image_path = $new_original_path;
$scaled_image_hash = hash_file('sha256', $base_directory . '/' . $attach_data["file"]);
$isScaled = $scaled_image_hash !== $original_hash;
$scaled_image_filepath = $base_directory . '/' . $attach_data["file"];
if ($isScaled) {
// Move file to new path nostr/s/c/<scaled hash>.ext
$extension = pathinfo($attach_data["file"], PATHINFO_EXTENSION);
$scaled_path_prefix = substr($scaled_image_hash, 0, 1) . '/' . substr($scaled_image_hash, 1, 1);
$new_path = 'nostr/' . $scaled_path_prefix . '/' . $scaled_image_hash . '.' .$extension;
// Save the scaled hash to the attachment metadata
$attach_data['scaled_file_hash'] = $scaled_image_hash;
// New path for the scaled image
// From: d/0/d0c0db5b65104add337d851725c451ccd8b618bdfc017946b78cca82599a3be6-jpg.webp (original hash)
// To: 5/6/56ef04e3a9d61edbc8bfe0314d945bb3dc7a054d53d46b09cd7bbe188809cd36.webp (scaled hash and -scaled or other suffixes removed)
$new_scaled_image_filepath = $base_directory . '/' . $new_path;
// If the file doesn't exist check, check if directory exists, and if not, create it
if (!file_exists($new_scaled_image_filepath)) {
$new_scaled_image_dir = dirname($new_scaled_image_filepath);
if (!file_exists($new_scaled_image_dir)) { // create prefix paths if they don't exist
wp_mkdir_p($new_scaled_image_dir);
}
}
// Rename the file on the disk
if (rename($scaled_image_filepath, $new_scaled_image_filepath)) {
// Update the 'file' key in the $attach_data array
$attach_data['file'] = $new_path;
// Update the 'sources' key for all types
if (isset($attach_data['sources'])) {
foreach ($attach_data['sources'] as $type => $source) {
$attach_data['sources'][$type]['file'] = $attach_data['file'];
}
}
$scaled_image_path = $new_path;
$scaled_image_filepath = $base_directory . '/' . $attach_data['file'];
// Update the '_wp_attached_file' meta key
update_post_meta($attach_id, '_wp_attached_file', $scaled_image_path);
}
}
// Save the original to the attachment metadata
$attach_data['original_file_hash'] = $original_hash;
// Save size and dimensions of the scaled image to the attachment metadata
$attach_data['dim'] = wp_getimagesize($scaled_image_filepath);
$attach_data['size'] = filesize($scaled_image_filepath);
wp_update_attachment_metadata($attach_id, $attach_data);
// Get the URL of the newly named scaled image (https://your-domain.com/wp-content/uploads/nostr/s/c/<scaled hash>.ext)
$scaled_image_url = $base_url . '/' . $attach_data['file'];
$response = array(
"status" => "success",
"message" => "File uploaded.",
"nip94_event" => array(
"pubkey" => $isValid["json"]["pubkey"],
"content" => "",
"id" => "",
"created_at" => $isValid["json"]["created_at"],
"kind" => 1063,
"sig" => "",
"tags" => array(
array("url", $scaled_image_url),
array("m", $movefile['type']),
array("ox", $original_hash),
array("x", $scaled_image_hash),
array("size", "" . $attach_data['size']), // Added file size of the scaled image
array("dim", $attach_data['dim'][0] . 'x' . $attach_data['dim'][1]) // Added dimensions of the scaled image
)
)
);
return new WP_REST_Response($response, 200);
}
else { // probably video/* types
// Same as before but all resizing removed
$attach_data = [];
// Assign default tag (if one is selected on Settings -> Media)
$default_tag_id = get_option('nmu_default_tag');
if (!empty($default_tag_id)) {
wp_set_object_terms($attach_id, array((int) $default_tag_id), 'post_tag', true);
}
// Save the original to the attachment metadata
$attach_data['original_file_hash'] = $original_hash;
$attach_data['size'] = filesize($new_original_path);
wp_update_attachment_metadata($attach_id, $attach_data);
$video_url = $base_url . '/nostr/' . substr($original_hash, 0, 1) . '/' . substr($original_hash, 1, 1) . '/' . $original_hash . '.' . $pathinfo['extension'];
$response = array(
"status" => "success",
"message" => "File uploaded.",
"nip94_event" => array(
"pubkey" => $isValid["json"]["pubkey"],
"content" => "",
"id" => "",
"created_at" => $isValid["json"]["created_at"],
"kind" => 1063,
"sig" => "",
"tags" => array(
array("url", $video_url),
array("m", $movefile['type']),
array("ox", $original_hash),
array("x", $original_hash),
array("size", "" . $attach_data['size'])
)
)
);
return new WP_REST_Response($response, 200);
}
} else {
return new WP_Error('upload_error', $movefile['error'], array('status' => 500));
}
} else {
$message = $isValid["message"] ?? "Invalid Authorization header.";
return new WP_Error('authorization_error', $message, array('status' => 401));
}
}
// show file hashes (ox and x) in the Media tab
function nmu_add_file_hash_to_media_library($form_fields, $post) {
$meta = get_post_meta($post->ID, '_wp_attachment_metadata', true);
if (isset($meta['original_file_hash'])) {
$original_file_hash = $meta['original_file_hash'];
if ($original_file_hash) {
$form_fields['original_file_hash'] = array(
'label' => 'Original hash (ox)',
'input' => 'html',
'html' => '<input type="text" name="attachments[' . $post->ID . '][original_file_hash]" id="attachments-' . $post->ID . '-original_file_hash" value="' . esc_attr($original_file_hash) . '" readonly>',
'value' => esc_attr($original_file_hash),
'helps' => 'SHA-256 hash of the original file'
);
}
}
$scaled_image_hash = isset($meta['scaled_file_hash']) ? $meta['scaled_file_hash'] : null;
if ($scaled_image_hash) {
$form_fields['scaled_image_hash'] = array(
'label' => 'Scaled hash (x)',
'input' => 'html',
'html' => '<input type="text" name="attachments[' . $post->ID . '][scaled_image_hash]" id="attachments-' . $post->ID . '-scaled_image_hash" value="' . esc_attr($scaled_image_hash) . '" readonly>',
'value' => esc_attr($scaled_image_hash),
'helps' => 'SHA-256 hash of scaled image'
);
}
return $form_fields;
}
add_filter('attachment_fields_to_edit', 'nmu_add_file_hash_to_media_library', 10, 2);
// /.well-known/nostr/nip96.json response
function nostr_custom_rewrite_rule() {
add_rewrite_rule('^\.well-known\/nostr\/nip96\.json$', 'index.php?nostr_nip96=true', 'top');
}
add_action('init', 'nostr_custom_rewrite_rule');
function nostr_custom_query_vars($vars) {
$vars[] = 'nostr_nip96';
return $vars;
}
add_filter('query_vars', 'nostr_custom_query_vars');
function nostr_custom_parse_request($wp) {
if (array_key_exists('nostr_nip96', $wp->query_vars)) {
header('Content-Type: application/json');
$base_url = get_site_url();
$api_url = $base_url . '/wp-json/nostrmedia/v1/upload/';
$response = array(
"api_url" => $api_url ,
"download_url" => $base_url,
"supported_nips" => array(
96,
98
),
"tos_url" => "",
"content_types" => array(
"image/png",
"image/jpg",
"image/jpeg",
"image/gif",
"image/webp",
"video/mp4",
"video/quicktime",
"video/mpeg",
"video/webm",
"audio/mpeg",
"audio/mpg",
"audio/mpeg3",
"audio/mp3"
)
);
echo json_encode($response);
exit;
}
}
add_action('parse_request', 'nostr_custom_parse_request');
// Upon plugin activation, check if ext-gmp is enabled.
function nmu_plugin_activation_check() {
if (!extension_loaded('gmp')) {
deactivate_plugins(plugin_basename(__FILE__)); // Deactivate our plugin.
wp_die('Error! Your server needs the GMP PHP extension enabled to use this plugin. Please contact your hosting provider or server administrator to enable the GMP extension.');
}
if (!extension_loaded('xml')) {
deactivate_plugins(plugin_basename(__FILE__)); // Deactivate our plugin.
wp_die('Error! Your server needs the XML PHP extension enabled to use this plugin. Please contact your hosting provider or server administrator to enable the XML extension.');
}
}
register_activation_hook(__FILE__, 'nmu_plugin_activation_check');
// Admin notice for showing any errors.
function nmu_plugin_admin_notices() {
// if post_max_size or upload_max_filesize is too low
// handle 128M or 128000000
$post_max_size = ini_get('post_max_size');
$upload_max_filesize = ini_get('upload_max_filesize');
$post_max_size_bytes = wp_convert_hr_to_bytes($post_max_size);
$upload_max_filesize_bytes = wp_convert_hr_to_bytes($upload_max_filesize);
if ($post_max_size_bytes < 8000000 || $upload_max_filesize_bytes < 8000000) {
printf('<div class="notice notice-error"><p>%1$s</p></div>', esc_html__('Nostr Media Uploads: post_max_size or upload_max_filesize is too low and may cause problems. Please contact your hosting provider or server administrator to increase these values. Recommended: 64M or 128M.'));
}
if (!extension_loaded('gmp')) {
printf('<div class="notice notice-error"><p>%1$s</p></div>', esc_html__('Error! Your server needs the GMP PHP extension enabled to use this plugin.'));
}
if (!extension_loaded('xml')) {
printf('<div class="notice notice-error"><p>%1$s</p></div>', esc_html__('Error! Your server needs the XML PHP extension enabled to use this plugin.'));
}
}
add_action('admin_notices', 'nmu_plugin_admin_notices');
// We need to store the files by hash instead of WP default YYYY/MM/... so we can check if we
// already have a file and return it instead of processing it again.
// We store in /a/b/hash.ext where /a/b/ is first 2 letters of the hash, so folder browsing does not become slow with too many files in one folder.
function nmu_custom_upload_dir($uploads) {
// Check if we are in the specific REST route
$current_route = $_SERVER['REQUEST_URI'] ?? '';
if (strpos($current_route, 'nostrmedia/v1/upload') !== false) {
// Assuming $original_hash is accessible here (otherwise, you'll need to calculate it again)
global $original_hash; // We'll set this in the file handling code later
$base_directory = WP_CONTENT_DIR . '/uploads';
$base_url = content_url('/uploads');
$custom_directory = '/nostr/' . substr($original_hash, 0, 1) . '/' . substr($original_hash, 1, 1);
$custom_url = '/nostr/' . substr($original_hash, 0, 1) . '/' . substr($original_hash, 1, 1);
$uploads['path'] = $base_directory . $custom_directory;
if (!file_exists($uploads['path'])) {
wp_mkdir_p($uploads['path']);
}
$uploads['url'] = $base_url . $custom_url;
$uploads['subdir'] = $custom_directory;
$uploads['basedir'] = $base_directory;
$uploads['baseurl'] = $base_url;
return $uploads;
}
return $uploads;
}
add_filter('upload_dir', 'nmu_custom_upload_dir');
register_deactivation_hook( __FILE__, 'nmu_plugin_deactivate' );
function nmu_plugin_deactivate() {
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'nmu_plugin_activate' );
function nmu_plugin_activate() {
add_action('wp_loaded', 'nmu_flush_rules');
}
function nmu_flush_rules() {
flush_rewrite_rules();
}
// On on the media page when opening an image it has the wrong folder for the original image.
// Because we're storing the scaled images as uploads/nostr/s/c/scaled-hash.ext and the original as uploads/nostr/o/r/original-hash.ext,
// but the link in the media page goes to uploads/nostr/s/c/original-hash.ext
// it seems to be using the scaled prefix, probably expecting the original and all resized versions to be in the same folder.
// So we need to change the link to the original image to the correct folder.
add_filter('wp_get_original_image_url', 'nmu_get_original_image_url', 10, 2);
function nmu_get_original_image_url($original_image_url, $post_id) {
// $original_image_url is http://.../wp-content/uploads/nostr/0/5/9e5aefbc4384aef20d9c2675ccc64c263e3eb8dcacecd56308a4371a515221d6.jpg
// if original_file_hash is part of the path, we need to replace the prefix with the original prefix
// so ../0/5/9e.. become ../9/e/9e..
// if the original url does not contain "/nostr/" then it's not a NIP-96 image, so return the original url
if (strpos($original_image_url, '/nostr/') === false) {
return $original_image_url;
}
// Fetch the metadata for this attachment.
$metadata = wp_get_attachment_metadata($post_id);
// If there is no metadata, return the original image URL.
if (!$metadata) {
return $original_image_url;
}
// If there is no original_file_hash in the metadata, return the original image URL.
if (!isset($metadata['original_file_hash'])) {
return $original_image_url;
}
// Get the original_file_hash from the metadata.
$original_file_hash = $metadata['original_file_hash'];
// Get the original prefix from the original_file_hash.
$original_prefix = substr($original_file_hash, 0, 1) . '/' . substr($original_file_hash, 1, 1);
$extension = pathinfo($original_image_url, PATHINFO_EXTENSION);
// Replace the prefix in the original_image_url with the original prefix.
$original_image_url = content_url('/uploads') . '/nostr/' . $original_prefix . '/' . $original_file_hash . '.' . $extension;
// Return the original_image_url.
return $original_image_url;
}
// add tags for media (attachments)
function nmu_add_tags_for_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'nmu_add_tags_for_attachments' );
// Register a new setting for storing the default tag for media uploads. In Settings -> Media.
function nmu_add_default_tag_setting() {
register_setting('media', 'nmu_default_tag');
// Add a new settings field for the default tag
add_settings_field(
'nmu_default_tag', // ID
'Tag for Nostr Media Uploads', // Label
'nmu_default_tag_field_callback', // Callback
'media', // Page
'default' // Section
);
}
add_action('admin_init', 'nmu_add_default_tag_setting');
// Callback function for the settings field
function nmu_default_tag_field_callback() {
$default_tag = get_option('nmu_default_tag');
echo '<select name="nmu_default_tag" id="nmu_default_tag">';
echo '<option value="">Select tag</option>';
// Fetch all tags and display them as options
$tags = get_terms(array('taxonomy' => 'post_tag', 'hide_empty' => false));
foreach ($tags as $tag) {
echo sprintf(
'<option value="%s" %s>%s</option>',
esc_attr($tag->term_id),
selected($default_tag, $tag->term_id, false),
esc_html($tag->name)
);
}
echo '</select>';
}
// Allow other origins on NIP-96 paths so other browser clients can make requests
function add_cors_http_header() {
// Define the paths where CORS headers should be applied
$allowed_paths = ['/wp-json/nostrmedia/v1/upload/', '/.well-known/nostr/nip96.json'];
// Get the requested URI
$request_uri = $_SERVER['REQUEST_URI'];
// Check if the origin is allowed and the path matches the allowed paths
if (in_array(parse_url($request_uri, PHP_URL_PATH), $allowed_paths)) {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization");
}
}
add_action( 'init', 'add_cors_http_header' );