/
bp-rbe-functions.php
1694 lines (1384 loc) · 49.2 KB
/
bp-rbe-functions.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* BP Reply By Email Functions
*
* @package BP_Reply_By_Email
* @subpackage Functions
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/** RBE All-purpose *****************************************************/
/**
* Checks to see if minimum requirements are completed (admin settings, webhost requirements).
*
* @param mixed $settings If you already have a settings array available, pass it. Otherwise, default is false.
* @return bool
* @since 1.0-beta
*/
function bp_rbe_is_required_completed( $settings = false ) {
global $bp_rbe;
$settings = ! $settings ? $bp_rbe->settings : $settings;
if ( ! is_array( $settings ) ) {
return false;
}
// inbound mode requirements
if( isset( $settings['mode'] ) && $settings['mode'] == 'inbound' ) {
$required_key = array( 'inbound-domain', 'inbound-provider' );
// imap mode requirements
} else {
// check if the IMAP extension is enabled
if ( ! function_exists( 'imap_open' ) ) {
return false;
}
$required_key = array( 'servername', 'port', 'tag', 'username', 'password', 'key', 'keepalive', 'connect' );
}
foreach ( $required_key as $required ) {
if ( empty( $settings[$required] ) ) {
return false;
}
}
return true;
}
/**
* Get an individual setting from RBE's settings array.
*
* @since 1.0-RC3
*
* @param string $setting The setting parameter.
* @param array $args {
* Misc settings.
* @type bool $refetch Whether to refetch RBE's settings. Handy when you
* need to ensure the settings are updated. Defaults to false.
* }
* @return string|bool
*/
function bp_rbe_get_setting( $setting = '', $args = array() ) {
if ( empty( $setting ) || ! is_string( $setting ) ) {
return false;
}
$r = wp_parse_args( $args, array(
'refetch' => false,
) );
global $bp_rbe;
// refetches RBE options
if ( true === $r['refetch'] ) {
// flush cache if necessary
if ( ! wp_using_ext_object_cache() ) {
wp_cache_flush();
}
// refetch option
$bp_rbe->settings = bp_get_option( 'bp-rbe' );
}
return isset( $bp_rbe->settings[$setting] ) ? $bp_rbe->settings[$setting] : false;
}
/**
* Whether RBE is in inbound mode.
*
* Previous to 1.0-RC3, BP RBE only supported IMAP to check an inbox and post
* items to BuddyPress. Now, RBE will also support inbound email.
*
* Inbound email mode means BP emails are sent to an external service that
* parses the email content and will post back the parsed content to the site.
* RBE will then used the parsed content to post the content to BuddyPress.
*
* Inbound email will be the default mode going forward.
*
* @since 1.0-RC3
*
* @return bool
*/
function bp_rbe_is_inbound() {
return (bool) ( bp_rbe_get_setting( 'mode' ) == 'inbound' );
}
if ( ! function_exists( 'bp_rbe_is_connected' ) ) :
/**
* Check if we're connected to the IMAP inbox.
*
* This is updated in {@link BP_Reply_By_Email_IMAP::connect()} and in
* {@link BP_Reply_By_Email_IMAP::close()}.
*
* @since 1.0-RC4
*
* @return bool
*/
function bp_rbe_is_connected( $args = array() ) {
$r = wp_parse_args( $args, array(
'clearcache' => true,
) );
if ( true == $r['clearcache'] ) {
clearstatcache();
}
$file = bp_core_avatar_upload_path() . '/bp-rbe-connected.txt';
if ( file_exists( $file ) ) {
// check if we're connected
if ( time() <= ( filemtime( $file ) + bp_rbe_get_execution_time() + 15 ) ) {
return true;
}
}
return false;
}
endif;
if ( ! function_exists( 'bp_rbe_is_connecting' ) ) :
/**
* Check if we're in the process of connecting to the IMAP inbox.
*
* If we're already attempting a connection to the inbox, bail.
*
* Uses the filesystem. Function is pluggable, so redeclare this function if
* you want to use another method (eg. memcached).
*
* @since 1.0-RC3
*
* @see bp_rbe_add_imap_lock()
* @see bp_rbe_remove_imap_lock()
* @see bp_rbe_should_stop()
* @see bp_rbe_stop_imap()
* @param array
* @return bool
*/
function bp_rbe_is_connecting( $args = array() ) {
$r = wp_parse_args( $args, array(
'clearcache' => false,
) );
if ( true == $r['clearcache'] ) {
clearstatcache();
}
$lockfile = bp_core_avatar_upload_path() . '/bp-rbe-lock.txt';
if ( file_exists( $lockfile ) ) {
// check if we're already attempting to connect
if ( time() <= filemtime( $lockfile ) + WP_CRON_LOCK_TIMEOUT ) {
return true;
}
}
return false;
}
endif;
/**
* Cleanup RBE.
*
* Clears RBE's scheduled hook from WP, as well as any DB entries and
* files.
*
* @since 1.0-RC1
*/
function bp_rbe_cleanup() {
// remove remnants from any previous failed attempts to stop the inbox
bp_rbe_should_stop();
bp_rbe_remove_imap_lock();
bp_rbe_remove_imap_connection_marker();
// we don't use these options anymore
bp_delete_option( 'bp_rbe_is_connected' );
bp_delete_option( 'bp_rbe_spawn_cron' );
bp_delete_option( 'bp_rbe_lock' );
delete_site_transient( 'bp_rbe_is_connected' );
delete_site_transient( 'bp_rbe_lock' );
// Clear RBE's cron.
wp_clear_scheduled_hook( 'bp_rbe_schedule' );
}
/**
* Get execution time for IMAP loop.
* This is the amount of time that RBE stays connected to the IMAP inbox.
*
* If safe mode is enabled, we use the max_execution_time as set in PHP.
* Otherwise, this value is configurable from the admin page.
*
* @see BP_Reply_By_Email_IMAP:::run()
* @param string $value Either 'seconds' or 'minutes'. Default is 'seconds'.
* @return int The execution time in either seconds or minutes
* @since 1.0-beta
* @todo Remove safe mode support as safe mode is being deprecated in PHP 5.3+.
*/
function bp_rbe_get_execution_time( $value = 'seconds' ) {
// if webhost has enabled safe mode, we cannot set the time limit, so
// we have to accommodate their max execution time
if ( ini_get( 'safe_mode' ) ) :
// value is in seconds
$time = ini_get( 'max_execution_time' );
if ( $value == 'minutes' ) {
$time = floor( ini_get( 'max_execution_time' ) / 60 );
}
// apply a filter just in case someone wants to override this!
$time = apply_filters( 'bp_rbe_safe_mode_execution_time', $time );
else :
// if keepalive setting exists, use it; otherwise, set default keepalive to 15 minutes
$time = bp_rbe_get_setting( 'keepalive' ) ? bp_rbe_get_setting( 'keepalive' ) : 15;
if ( $value == 'seconds' ) {
$time = $time * 60;
}
endif;
return $time;
}
/**
* Injects querystring into an email address.
*
* For IMAP mode, the constructed email address will look like this:
* test@gmail.com -> test+THEQUERYSTRING@gmail.com
*
* For inbound mode, the constructed email address will look like this:
* THEQUERYSTRING@reply.yourdomain.com
*
* @since 1.0-beta
*
* @param string $qs The querystring we want to add to an email address.
* @returns string
*/
function bp_rbe_inject_qs_in_email( $qs ) {
// inbound mode uses subdomain addressing
// eg. whatever@reply.yourdomain.com
if ( bp_rbe_is_inbound() ) {
$retval = $qs . '@' . bp_rbe_get_setting( 'inbound-domain' );
// imap mode uses address tags
// eg. test+whatever@gmail.com
} else {
$email = bp_rbe_get_setting( 'email' );
$at_pos = strpos( $email, '@' );
// Address tag + $qs
$qs = bp_rbe_get_setting( 'tag' ) . $qs;
$retval = substr_replace( $email, $qs, $at_pos, 0 );
}
return apply_filters( 'bp_rbe_inject_qs_in_email', $retval, $qs );
}
/**
* Returns the RBE marker used to parse the email reply content.
*
* @since 1.0-RC4
*
* @return string
*/
function bp_rbe_get_marker() {
/**
* Filters the marker used in reply-to emails.
*
* @since 1.0-RC5
*
* @param string $marker RBE marker.
*/
$marker = apply_filters( 'bp_rbe_get_marker', __( '--- Reply ABOVE THIS LINE to add a comment ---', 'bp-rbe' ) );
return $marker;
}
/**
* Returns the notice used to denote if an email cannot be replied by email.
*
* @since 1.0-RC5
*
* @return string
*/
function bp_rbe_get_nonrbe_notice() {
/**
* Filters the non-RBE notice.
*
* @since 1.0-RC5
*
* @param string $notice Non-RBE notice.
*/
$notice = apply_filters( 'bp_rbe_get_nonrbe_notice', __( '--- Replying to this email will not send a message directly to the recipient or group ---', 'bp-rbe' ) );
return $notice;
}
/**
* Encodes a string.
*
* By default, uses AES encryption from {@link http://phpseclib.sourceforge.net/ phpseclib}.
* Licensed under the {@link http://www.opensource.org/licenses/mit-license.html MIT License}.
*
* Thanks phpseclib! :)
*
* @param array $args Array of arguments. See inline doc of function for full details.
* @return string The encoded string
* @since 1.0-beta
*/
function bp_rbe_encode( $args = array() ) {
$r = wp_parse_args( $args, array (
'string' => false, // the content we want to encode
'key' => bp_rbe_get_setting( 'key' ), // the key used to aid in encryption; defaults to the key set in the admin area
'param' => false, // the string we want to prepend to the key; handy to set different keys
'mode' => 'aes', // mode of encryption; defaults to 'aes'
) );
if ( empty( $r['string'] ) || empty( $r['key'] ) ) {
return false;
}
if ( $r['param'] ) {
$r['key'] = $r['param'] . $r['key'];
}
$encoded = false;
/**
* Set the encode mode for the string.
*
* Default mode is 'aes'. If you override this in the filter, you still have
* to modify the return value by using the 'bp_rbe_encode' and 'bp_rbe_decode'
* filters.
*
* @since 1.0-beta1
*
* @param string $mode
*/
$r['mode'] = apply_filters( 'bp_rbe_encode_mode', $r['mode'] );
if ( 'aes' == $r['mode'] ) {
if ( ! class_exists( 'Crypt_AES' ) ) {
require( BP_RBE_DIR . '/includes/phpseclib/AES.php' );
}
$cipher = new Crypt_AES();
$cipher->setKey( $r['key'] );
// converts AES binary string to hexadecimal
$encoded = bin2hex( $cipher->encrypt( $r['string'] ) );
}
/**
* Override the encoded value for a string.
*
* @since 1.0-beta1
*
* @param string|bool $encoded The encoded value. Boolean false if no value.
* @param string $string Unencoded string we want to encode.
* @param string $mode Encrypt mode.
* @param string $key Encryption key.
* @param string $param String to prepend to the key. Handy to set different keys.
*/
return apply_filters( 'bp_rbe_encode', $encoded, $r['string'], $r['mode'], $r['key'], $r['param'] );
}
/**
* Decodes an encrypted string.
*
* By default, uses AES decryption from {@link http://phpseclib.sourceforge.net/ phpseclib}.
* Licensed under the {@link http://www.opensource.org/licenses/mit-license.html MIT License}.
*
* Thanks phpseclib! :)
*
* @param array $args Array of arguments. See inline doc of function for full details.
* @return string The decoded string
* @since 1.0-beta
*/
function bp_rbe_decode( $args = array() ) {
$r = wp_parse_args( $args, array (
'string' => false, // the encoded string we want to dencode
'key' => bp_rbe_get_setting( 'key' ), // the key used to aid in encryption; defaults to the key set in the admin area
'param' => false, // the string we want to prepend to the key; handy to set different keys
'mode' => 'aes', // mode of decryption; defaults to 'aes'
) );
if ( empty( $r['string'] ) || empty( $r['key'] ) ) {
return false;
}
if ( $r['param'] ) {
$r['key'] = $r['param'] . $r['key'];
}
$decoded = false;
/** This filter is documented in bp_rbe_encode() - /includes/bp-rbe-functions.php */
$r['mode'] = apply_filters( 'bp_rbe_encode_mode', $r['mode'] );
if ( 'aes' == $r['mode'] ) {
if ( ! class_exists( 'Crypt_AES' ) ) {
require( BP_RBE_DIR . '/includes/phpseclib/AES.php' );
}
$cipher = new Crypt_AES();
$cipher->setKey( $r['key'] );
// converts hexadecimal AES string back to binary and then decrypts string back to plain-text
$decoded = $cipher->decrypt( hex2bin( $r['string'] ) );
}
/**
* Override the decoded value for a string.
*
* @since 1.0-beta1
*
* @param string|bool $decoded The decoded value. Boolean false if no value.
* @param string $string Encoded string we want to decode.
* @param string $mode Encrypt mode.
* @param string $key Encryption key.
* @param string $param String to prepend to the key. Handy to set different keys.
*/
return apply_filters( 'bp_rbe_decode', $decoded, $r['string'], $r['mode'], $r['key'], $r['param'] );
}
if ( ! function_exists( 'hex2bin' ) ) :
/**
* hex2bin() isn't available in PHP < 5.4.
*
* So let's add our compatible version here.
*
* @uses pack()
* @param string $text Hexadecimal representation of data.
* @return mixed Returns the binary representation of the given data or FALSE on failure.
*/
function hex2bin( $text ) {
return pack( 'H*', $text );
}
endif;
/**
* Should we enable SSL for the IMAP connection??
*
* Check to see if both the OpenSSL and IMAP modules are loaded. Next, see if
* the port is explictly 993.
*
* @since 1.0-beta
*
* @param int $port The port number for the IMAP server
* @return bool
*/
function bp_rbe_is_imap_ssl( $port = 0 ) {
if ( empty( $port ) ) {
$port = bp_rbe_get_setting( 'port' );
}
return BP_Reply_By_Email_Connect::is_ssl( (int) $port );
}
/**
* Logs BP Reply To Email actions to a debug log.
*
* @uses error_log()
* @since 1.0-beta
*/
function bp_rbe_log( $message ) {
// if debugging is off, stop now.
if ( ! constant( 'BP_RBE_DEBUG' ) )
return;
if ( empty( $message ) )
return;
error_log( '[' . gmdate( 'd-M-Y H:i:s' ) . '] ' . $message . "\n", 3, BP_RBE_DEBUG_LOG_PATH );
}
/**
* Writes inline data to a local temporary file.
*
* Note: The calling function must unlink() the file.
*
* @since 1.0-RC6
*
* @param string $filename Name of your file.
* @param mixed $data Data for your file.
* @return string|WP_Error String of path to temporary file on success, WP_Error on failure.
*/
function bp_rbe_inline_data_to_tmpfile( $filename = '', $data ) {
if ( ! function_exists( 'wp_tempnam' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$filename = wp_tempnam( $filename );
if ( ! $filename ) {
return new WP_Error( 'http_no_file', __( 'Could not create temporary file.', 'bp-rbe' ) );
}
if ( false === @file_put_contents( $filename, $data ) ) {
return new WP_Error( 'http_no_write', __( 'Could not write to temporary file.', 'bp-rbe' ) );
}
return $filename;
}
/**
* Check if we should pass the full RFC822 email during parsing.
*
* By RFC822 email, we are referring to the raw, MIME content for an email.
*
* For example, SparkPost requires the raw email for attachment parsing.
* By default, we pass the full email for bbPress posts.
*
* @since 1.0-RC6
*
* @param array $params Email parameters. {@see BP_Reply_By_Email_Parser::get_parameters()}.
* @return bool
*/
function bp_rbe_should_use_rfc822_email( $params = array() ) {
// We currently only allow bbPress posts for attachments.
$allowed = array_flip( array( 'bbpf', 'bbpr', 'bbpt' ) );
$check = (bool) array_intersect_key( (array) $params, $allowed );
/**
* Filter to override the full RFC822 email parse check.
*
* @since 1.0-RC6
*
* @param bool $check Current retval.
* @param array $params Email parameters. {@see BP_Reply_By_Email_Parser::get_parameters()}.
* @return bool
*/
return apply_filters( 'bp_rbe_should_use_rfc822_email', $check, $params );
}
/** Hook-related ********************************************************/
/**
* Inbound POST callback catcher.
*
* @since 1.0-RC3
*/
function bp_rbe_inbound_catch_callback() {
global $bp_rbe;
// make sure WP-cron / AJAX / XMLRPC isn't running.
if ( defined( 'DOING_CRON' ) || defined( 'DOING_AJAX' ) || isset( $_GET['doing_wp_cron'] ) || defined( 'XMLRPC_REQUEST' ) ) {
return;
}
// setup the webhook parser
if ( is_callable( array( $bp_rbe->inbound_provider, 'webhook_parser' ) ) ) {
call_user_func( array( $bp_rbe->inbound_provider, 'webhook_parser' ) );
}
}
/**
* Overrides an activity comment's action formatting.
*
* If an activity comment was posted via email, this function reformats the
* activity action to denote this.
*
* Only applicable in BuddyPress 2.0+. Lower versions uses {@link bp_rbe_activity_comment_action()}
* instead.
*
* @since 1.0-RC3
*
* @param string $retval The current activity comment action string
* @param BP_Activity_Activity $activity
*/
function bp_rbe_activity_comment_action_formatting( $retval, $activity ) {
if ( bp_activity_get_meta( $activity->id, 'bp_rbe' ) ) {
$retval = sprintf( __( '%s posted a new activity comment via email:', 'bp-rbe' ), bp_core_get_userlink( $activity->user_id ) );
}
return $retval;
}
/**
* Overrides an activity comment's action string.
*
* BP doesn't pass the $user_id in the "bp_activity_comment_action" filter.
* So, a little bit of hackery is done just to add the words "via email" to the comment action! :)
*
* @since 1.0-beta
*/
function bp_rbe_activity_comment_action_filter( $user_id ) {
global $bp_rbe;
// hack to pass user ID!
$bp_rbe->filter = new stdClass;
$bp_rbe->filter->user_id = $user_id;
add_filter( 'bp_activity_comment_action', 'bp_rbe_activity_comment_action' );
}
/**
* Callback for "bp_activity_comment_action" filter.
* Uses the passed user ID from {@link bp_rbe_activity_comment_action_filter()}.
*
* @since 1.0-beta
*/
function bp_rbe_activity_comment_action( $action ) {
global $bp_rbe;
// use our passed user ID from hack above
return sprintf( __( '%s posted a new activity comment via email:', 'bp-rbe' ), bp_core_get_userlink( $bp_rbe->filter->user_id ) );
}
/**
* Adds anchor to an activity comment's "View" link.
*
* Who likes scrolling all the way down the page to find their comment!
*
* @since 1.0-beta
*/
function bp_rbe_activity_comment_view_link( $link, $activity ) {
if ( $activity->type == 'activity_comment' ) {
$action = apply_filters_ref_array( 'bp_get_activity_action_pre_meta', array(
$activity->action,
&$activity,
array(
'no_timestamp' => false,
)
) );
$time_since = apply_filters_ref_array( 'bp_activity_time_since', array( '<span class="time-since">' . bp_core_time_since( $activity->date_recorded ) . '</span>', &$activity ) );
return $action . ' <a href="' . bp_activity_get_permalink( $activity->id ) . '#acomment-' . $activity->id . '" class="view activity-time-since" title="' . __( 'View Discussion', 'buddypress' ) . '">' . $time_since . '</a>';
}
return $link;
}
/**
* When posting via email, we also update the last activity entries in BuddyPress.
*
* This is so your BuddyPress site doesn't look dormant when your members
* are emailing each other back and forth! :)
*
* @param array $args Depending on the filter that this function is hooked into, contents will vary
* @since 1.0-RC1
*/
function bp_rbe_log_last_activity( $args ) {
// get user id from activity entry
if ( ! empty( $args['user_id'] ) )
$user_id = $args['user_id'];
// get user id from PM
elseif ( ! empty( $args['sender_id'] ) )
$user_id = $args['sender_id'];
else
$user_id = false;
// if no user ID, return now
if ( empty( $user_id ) )
return;
// update user's last activity
bp_update_user_last_activity( $user_id );
// now update 'last_activity' group meta entry if applicable
if ( ! empty( $args['type'] ) ) {
switch ( $args['type'] ) {
case 'new_forum_topic' :
case 'new_forum_post' :
case 'bbp_topic_create' :
case 'bbp_reply_create' :
// sanity check!
if ( ! bp_is_active( 'groups' ) )
return;
groups_update_last_activity( $args['item_id'] );
break;
// for group activity comments, we have to look up the parent activity to see
// if the activity comment came from a group
case 'activity_comment' :
// we don't need to proceed if the groups component was disabled
if ( ! bp_is_active( 'groups' ) )
return;
// sanity check!
if ( ! bp_is_active( 'activity' ) )
return;
// grab the parent activity
$activity = bp_activity_get_specific( 'activity_ids=' . $args['item_id'] );
if ( ! empty( $activity['activities'][0] ) ) {
$parent_activity = $activity['activities'][0];
// if parent activity update is from the groups component,
// that means the activity comment was in a group!
// so update group 'last_activity' meta entry
if ( $parent_activity->component == 'groups' )
groups_update_last_activity( $parent_activity->item_id );
}
break;
}
}
}
/**
* Removes end of line (EOL) and a given character from content.
* Used to remove the trailing ">" character from email replies. Wish Basecamp did this!
*
* @param string $content The content we want to modify
* @return string Either content without EOL + $char or the unmodified content.
* @since 1.0-beta
*/
function bp_rbe_remove_eol_char( $content ) {
$char = apply_filters( 'bp_rbe_eol_char', '>' );
if ( substr( $content, -strlen( $char ) ) == $char )
return substr( $content, 0, strrpos( $content, chr( 10 ) . $char ) );
return $content;
}
/**
* Converts HTML to plain-text.
*
* Uses the html2text() function by Jevon Wright. Thanks Jevon! :)
*
* @uses html2text() by Jevon Wright. Licensed under the EPL v1.0 and LGPL v3.0.
* We use a fork of 0.1.1 to maintain PHP 5.2 compatibility.
* @link https://github.com/r-a-y/html2text/tree/0.1.x
* @link https://github.com/soundasleep/html2text/
*
* @param string $content The HTML content we want to convert to plain-text.
* @return string Converted plain-text.
*/
function bp_rbe_html_to_plaintext( $content ) {
if ( empty( $content ) ) {
return $content;
}
if ( false === function_exists( 'convert_html_to_text' ) ) {
require BP_RBE_DIR . '/includes/functions.html2text.php';
}
// Suppress warnings when using DOMDocument.
// This addresses issues when failing to parse certain HTML.
if ( function_exists( 'libxml_use_internal_errors' ) ) {
libxml_use_internal_errors( true );
}
return convert_html_to_text( $content );
}
/**
* Removes line wrap from plain-text emails.
*
* Plain-text emails usually wrap after a certain amount of characters
* (GMail wraps after ~78 characters) and this will also be reflected on
* the frontend of BuddyPress.
*
* This function attempts to remove the line wrap from plain-text emails
* during email parsing so things will look pretty on the frontend.
*
* But, this isn't used at the moment due to bugginess!
* If you want to try it, hook this function to the 'bp_rbe_parse_email_body' filter.
*
* Note: Github's RBE doesn't strip line wraps.
*
* @param string $body The body we want to remove line-wraps for
* @param obj $structure The structure of the email from imap_fetchstructure()
* @return string Converted plain-text.
* @todo Need to check line endings on other OSs... might use PHP_EOL instead
*/
function bp_rbe_remove_line_wrap_from_plaintext( $body, $structure ) {
// just in case, we only do this to emails that are not HTML-only
if ( $structure->subtype != 'html' ) {
// replace double CRLF with double LF
$body = str_replace( "\r\n\r\n", "\n\n", $body );
// keep line breaks for certain instances
// hacky at best... :(
// doesn't handle numbered list items
// @todo craft a nice regex to do this instead and cover all instances?
// any line ending with a colon
$body = str_replace( ":\r\n", ':<RAY>', $body );
// any line beginning with '-', '*', ' '
$body = str_replace( "\r\n-", '<RAY>-', $body );
$body = str_replace( "\r\n*", '<RAY>*', $body );
$body = str_replace( "\r\n ", '<RAY> ', $body );
// now remove single CRLF so line wrap is gone!
$body = str_replace( "\r\n", ' ', $body );
// add back the line breaks
$body = str_replace( '<RAY>', "\n", $body );
}
return $body;
}
/**
* Tries to remove the email signature of *most* common email clients from email replies.
*
* Keyword here is *most*! :) A work-in-progress!
*
* @param string $content The content we want to modify
* @return string
* @since 1.0-beta
*/
function bp_rbe_remove_email_client_signature( $content ) {
// Good reference article:
// http://stackoverflow.com/questions/1372694/strip-signatures-and-replies-from-emails#answer-2193937
//
// I've implemented basically everything except #2 and #6
// helpful ascii whitespace debugger
//var_dump( str_replace( array( "\r\n", "\r", "\n", "\t"), array( '\\r\\n', '\\r', '\\n', '\\t' ), $content ) );
// (1) Standard email sig delimiter
//
// eg. "--\r\n
// John Doe"
//
if ( strpos( $content, chr( 10 ) . '--' . chr( 13 ) . chr( 10 ) ) !== false ) {
$content = substr( $content, 0, strpos( $content, chr( 10 ) . '--' . chr( 13 ) . chr( 10 ) ) );
}
// (2) Common mobile email client sigs:
// check to see if any line begins with "Sent from my "
elseif ( strrpos( $content, chr( 10 ) . 'Sent from my ' ) !== false ) {
$content = substr( $content, 0, strrpos( $content, chr( 10 ) . 'Sent from my ' ) );
}
// (3)(i) Miscellaneous email sigs: Outlook Desktop, Novell Groupwise Web Access
//
// These clients (and probably others) use an indeterminate amount of dashes to
// separate the body and the signature; let's check for at least 20 occurences in a row
// @todo Perhaps use a longer length to be extra safe?
elseif ( strrpos( $content, chr( 10 ) . '--------------------' ) !== false ) {
$content = substr( $content, 0, strrpos( $content, chr( 10 ) . '--------------------' ) );
}
// (3)(ii) Miscellaneous email sigs: Outlook Web Access
//
// Outlook Web Access sigs look like this:
//
// ________________________________________
// From: ...
//
// Since the multiple underscores are sometimes of an indeterminant length,
// we check for at least 20 occurences
// @todo Perhaps use a longer length to be extra safe?
elseif ( strrpos( $content, chr( 10 ) . '____________________' ) !== false ) {
$content = substr( $content, 0, strrpos( $content, chr( 10 ) . '____________________' ) );
}
// (3)(iii) Miscellaneous email sigs: Outlook Desktop
//
// Some Outlook Desktop sigs look like this:
//
// -----Original Message-----
// From: ...
//
elseif ( strrpos( $content, chr( 10 ) . '-----Original Message-----' ) !== false ) {
$content = substr( $content, 0, strrpos( $content, chr( 10 ) . '-----Original Message-----' ) );
}
// (3)(iv) Miscellaneous email sigs: Lotus Notes
//
// eg. "-----Blah <blah.com> wrote: -----"
//
// The reason we do two checks here is people might use five dashes to emulate a <hr> tag.
elseif ( strrpos( $content, chr( 10 ) . '-----' ) !== false && strrpos( $content, ': -----' ) !== false ) {
$content = substr( $content, 0, strrpos( $content, chr( 10 ) . '-----' ) );
}
// (4) Common email client sigs:
// check if last character of last line ends with a colon.
//
// eg. 'On DATE, USER wrote:'
// 'USER wrote:'
//
// This is the last check because it's slightly more intensive than the others
else {
// split email into an array of lines; reverse the order
$lines = array_reverse( preg_split( '/$\R?^/m', trim( $content ) ) );
//print_r($lines);
// trailing '>' character
//
// this might occur when you're replying to a HTML email generated by WP Better Emails
// if so, remove this line
if ( '>' === $lines[0] ) {
array_shift( $lines );
}
// last character of last line ends with a colon!
if ( substr( rtrim( $lines[0] ), -1 ) === ':' ) {
$i = 0;
// now we check to see if the sig was wrapped after a certain character limit.
//
// eg. 'On DATE, USER
// wrote:'
//
// chances are this sig takes up a maximum of two lines, but just to be safe, I'm using this method!
//
// this is done by checking if each line from the last line is less than the last line
while ( ! empty( $lines[$i + 1] ) ) {
// if the nth-to-last line is less than the current line, stop now!
if ( strlen( $lines[$i + 1] ) < strlen( $lines[$i] ) )
break;
// iterate!
++$i;
// find position of marker
$marker = strrpos( $content, $lines[$i] );
// if marker matches integer 0, remove the line preceding this one
if ( $marker === 0 ) {
$marker = strrpos( $content, $lines[$i-1] );
}
// get body until beginning of the sig
$content = substr( $content, 0, $marker );
}
// if $i didn't iterate, this means the sig is on the last line only
if ( $i == 0 ) {
$content = substr( $content, 0, strrpos( $content, $lines[0] ) );
}
}
}
return trim( $content );
}
/**
* Logs no match errors during RBE parsing.
*
* Also sends a failure message back to the original sender for feedback
* purposes if enabled.
*
* @since 1.0-RC3
*
* @uses bp_rbe_log() Logs error messages in a custom log
* @param object $parser The WP_Error object.
* @param array $data {
* An array of arguments.
*
* @type array $headers Email headers.
* @type string $to_email The 'To' email address.
* @type string $from_email The 'From' email address.
* @type string $content The email body content.
* @type string $subject The email subject line.
* @type bool $is_html Whether the email content is HTML or not.
* }
* @param int $i The current message number
* @param resource|bool $imap The IMAP connection if passed. Boolean false if not.
*/
function bp_rbe_log_no_matches( $parser, $data, $i, $imap ) {
$log = $message = false;
$type = is_wp_error( $parser ) ? $parser->get_error_code() : false;