/
functions.php
1687 lines (1486 loc) · 48.6 KB
/
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
/* User flags */
define('BUGS_NORMAL_USER', 1<<0);
define('BUGS_DEV_USER', 1<<1);
define('BUGS_TRUSTED_DEV', 1<<2);
define('BUGS_SECURITY_DEV', 1<<3);
/* Contains functions and variables used throughout the bug system */
// used in mail_bug_updates(), below, and class for search results
$tla = [
'Open' => 'Opn',
'Not a bug' => 'Nab',
'Feedback' => 'Fbk',
'No Feedback' => 'NoF',
'Wont fix' => 'Wfx',
'Duplicate' => 'Dup',
'Critical' => 'Ctl',
'Assigned' => 'Asn',
'Analyzed' => 'Ana',
'Verified' => 'Ver',
'Suspended' => 'Sus',
'Closed' => 'Csd',
'Spam' => 'Spm',
'Re-Opened' => 'ReO',
];
$bug_types = [
'Bug' => 'Bug',
'Feature/Change Request' => 'Req',
'Documentation Problem' => 'Doc',
'Security' => 'Sec Bug'
];
// Used in show_state_options()
$state_types = [
'Open' => 2,
'Closed' => 2,
'Re-Opened' => 1,
'Duplicate' => 1,
'Critical' => 1,
'Assigned' => 2,
'Not Assigned' => 0,
'Analyzed' => 1,
'Verified' => 1,
'Suspended' => 1,
'Wont fix' => 1,
'No Feedback' => 1,
'Feedback' => 1,
'Old Feedback' => 0,
'Stale' => 0,
'Fresh' => 0,
'Not a bug' => 1,
'Spam' => 1,
'All' => 0,
];
/**
* Authentication
*/
function verify_user_password($user, $pass)
{
global $errors;
$post = http_build_query(
[
'token' => getenv('AUTH_TOKEN'),
'username' => $user,
'password' => $pass,
]
);
$opts = [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post,
];
$ctx = stream_context_create(['http' => $opts]);
$s = file_get_contents('https://master.php.net/fetch/cvsauth.php', false, $ctx);
$a = @unserialize($s);
if (!is_array($a)) {
$errors[] = "Failed to get authentication information.\nMaybe master is down?\n";
return false;
}
if (isset($a['errno'])) {
$errors[] = "Authentication failed: {$a['errstr']}\n";
return false;
}
$_SESSION["user"] = $user;
return true;
}
function bugs_has_access ($bug_id, $bug, $pw, $user_flags)
{
global $auth_user;
if ($bug['private'] != 'Y') {
return true;
}
// When the bug is private, only the submitter, trusted devs, security devs and assigned dev
// should see the report info
if ($user_flags & (BUGS_SECURITY_DEV | BUGS_TRUSTED_DEV)) {
// trusted and security dev
return true;
} else if (($user_flags == BUGS_NORMAL_USER) && $pw != '' && verify_bug_passwd($bug_id, bugs_get_hash($pw))) {
// The submitter
return true;
} else if (($user_flags & BUGS_DEV_USER) && $bug['reporter_name'] != '' &&
strtolower($bug['reporter_name']) == strtolower($auth_user->handle)) {
// The submitter (php developer)
return true;
} else if (($user_flags & BUGS_DEV_USER) && $bug['assign'] != '' &&
strtolower($bug['assign']) == strtolower($auth_user->handle)) {
// The assigned dev
return true;
}
return false;
}
function bugs_authenticate (&$user, &$pw, &$logged_in, &$user_flags)
{
global $auth_user, $ROOT_DIR;
// Default values
$user = '';
$pw = '';
$logged_in = false;
$user_flags = BUGS_NORMAL_USER;
// Set username and password
if (!empty($_POST['pw'])) {
if (empty($_POST['user'])) {
$user = '';
} else {
$user = htmlspecialchars($_POST['user']);
}
$user = strtolower($user);
$pw = $_POST['pw'];
} elseif (isset($auth_user) && is_object($auth_user) && $auth_user->handle) {
$user = $auth_user->handle;
$pw = $auth_user->password;
}
// Authentication and user level check
// User levels are: reader (0), commenter/patcher/etc. (edit = 3), submitter (edit = 2), developer (edit = 1)
if (!empty($_SESSION["user"])) {
$user = $_SESSION["user"];
$user_flags = BUGS_DEV_USER;
$logged_in = 'developer';
$auth_user = new stdClass;
$auth_user->handle = $user;
$auth_user->email = "{$user}@php.net";
$auth_user->name = $user;
} elseif ($user != '' && $pw != '' && verify_user_password($user, $pw)) {
$user_flags = BUGS_DEV_USER;
$logged_in = 'developer';
$auth_user = new stdClass;
$auth_user->handle = $user;
$auth_user->email = "{$user}@php.net";
$auth_user->name = $user;
} else {
$auth_user = new stdClass;
$auth_user->email = isset($_POST['in']['email']) ? $_POST['in']['email'] : '';
$auth_user->handle = '';
$auth_user->name = '';
}
// Check if developer is trusted
if ($logged_in == 'developer') {
require_once "{$ROOT_DIR}/include/trusted-devs.php";
if (in_array(strtolower($user), $trusted_developers)) {
$user_flags |= BUGS_TRUSTED_DEV;
}
if (in_array(strtolower($user), $security_developers)) {
$user_flags |= BUGS_SECURITY_DEV;
}
}
}
/* Primitive check for SPAM. Add more later. */
function is_spam($string)
{
// @php.net users are given permission to spam... we gotta eat! See also bug #48126
if (!empty($GLOBALS['auth_user']->handle)) {
return false;
}
if (substr_count(strtolower($string), 'http://') > 5) {
return true;
}
$keywords = [
'spy',
'bdsm',
'massage',
'mortage',
'sex',
'11nong',
'oxycontin',
'distance-education',
'sismatech',
'justiceplan',
'prednisolone',
'baclofen',
'diflucan',
'unbra.se',
'objectis',
'angosso',
'colchicine',
'zovirax',
'korsbest',
'coachbags',
'chaneljpoutlet',
'\/Members\/',
'michaelkorsshop',
'mkmichaelkors',
'Burberrysale4u',
'gadboisphotos',
'oakleysunglasseslol',
'partydressuk',
'leslunettesdesoleil',
'PaulRGuthrie',
'[a-z]*?fuck[a-z]*?',
'jerseys',
'wholesale',
'fashionretailshop01',
'amoxicillin',
];
if (preg_match('/\b('. implode('|', $keywords) . ')\b/i', $string)) {
return true;
}
return false;
}
/* Primitive check for SPAMmy user. Add more later. */
function is_spam_user($email)
{
if (preg_match("/(rhsoft|reindl|phpbugreports)/i", $email)) {
return true;
}
return false;
}
/**
* Obfuscates email addresses to hinder spammer's spiders
*
* Turns "@" into character entities that get interpreted as "at" and
* turns "." into character entities that get interpreted as "dot".
*
* @param string $txt the email address to be obfuscated
* @param string $format how the output will be displayed ('html', 'text', 'reverse')
*
* @return string the altered email address
*/
function spam_protect($txt, $format = 'html')
{
/* php.net addresses are not protected! */
if (preg_match('/^(.+)@php\.net$/i', $txt)) {
return $txt;
}
if ($format == 'html') {
$translate = [
'@' => ' at ',
'.' => ' dot ',
];
} else {
$translate = [
'@' => ' at ',
'.' => ' dot ',
];
if ($format == 'reverse') {
$translate = array_flip($translate);
}
}
return strtr($txt, $translate);
}
/**
* Goes through each variable submitted and returns the value
* from the first variable which has a non-empty value
*
* Handy function for when you're dealing with user input or a default.
*
* @param mixed as many variables as you wish to check
*
* @return mixed the value, if any
*
* @see field(), txfield()
*/
function oneof()
{
foreach (func_get_args() as $arg) {
if ($arg) {
return $arg;
}
}
}
/**
* Returns the data from the field requested and sanitizes
* it for use as HTML
*
* If the data from a form submission exists, that is used.
* But if that's not there, the info is obtained from the database.
*
* @param string $n the name of the field to be looked for
*
* @return mixed the data requested
*
* @see oneof(), txfield()
*/
function field($n)
{
return oneof(isset($_POST['in']) ?
htmlspecialchars(isset($_POST['in'][$n]) ? $_POST['in'][$n] : '') : null,
htmlspecialchars($GLOBALS['bug'][$n]));
}
/**
* Escape string so it can be used as HTML
*
* @param string $in the string to be sanitized
*
* @return string the sanitized string
*
* @see txfield()
*/
function clean($in)
{
return mb_encode_numericentity($in,
[
0x0, 0x8, 0, 0xffffff,
0xb, 0xc, 0, 0xffffff,
0xe, 0x1f, 0, 0xffffff,
0x22, 0x22, 0, 0xffffff,
0x26, 0x27, 0, 0xffffff,
0x3c, 0x3c, 0, 0xffffff,
0x3e, 0x3e, 0, 0xffffff,
0x7f, 0x84, 0, 0xffffff,
0x86, 0x9f, 0, 0xffffff,
0xfdd0, 0xfdef, 0, 0xffffff,
0x1fffe, 0x1ffff, 0, 0xffffff,
0x2fffe, 0x2ffff, 0, 0xffffff,
0x3fffe, 0x3ffff, 0, 0xffffff,
0x4fffe, 0x4ffff, 0, 0xffffff,
0x5fffe, 0x5ffff, 0, 0xffffff,
0x6fffe, 0x6ffff, 0, 0xffffff,
0x7fffe, 0x7ffff, 0, 0xffffff,
0x8fffe, 0x8ffff, 0, 0xffffff,
0x9fffe, 0x9ffff, 0, 0xffffff,
0xafffe, 0xaffff, 0, 0xffffff,
0xbfffe, 0xbffff, 0, 0xffffff,
0xcfffe, 0xcffff, 0, 0xffffff,
0xdfffe, 0xdffff, 0, 0xffffff,
0xefffe, 0xeffff, 0, 0xffffff,
0xffffe, 0xfffff, 0, 0xffffff,
0x10fffe, 0x10ffff, 0, 0xffffff,
],
'UTF-8');
}
/**
* Returns the data from the field requested and sanitizes
* it for use as plain text
*
* If the data from a form submission exists, that is used.
* But if that's not there, the info is obtained from the database.
*
* @param string $n the name of the field to be looked for
*
* @return mixed the data requested
*
* @see clean()
*/
function txfield($n, $bug = null, $in = null)
{
$one = (isset($in) && isset($in[$n])) ? $in[$n] : false;
if ($one) {
return $one;
}
$two = (isset($bug) && isset($bug[$n])) ? $bug[$n] : false;
if ($two) {
return $two;
}
}
/**
* Prints age <option>'s for use in a <select>
*
* @param string $current the field's current value
*
* @return void
*/
function show_byage_options($current)
{
$opts = [
'0' => 'the beginning',
'1' => 'yesterday',
'7' => '7 days ago',
'15' => '15 days ago',
'30' => '30 days ago',
'90' => '90 days ago',
];
foreach ($opts as $k => $v) {
echo "<option value=\"$k\"", ($current==$k ? ' selected="selected"' : ''), ">$v</option>\n";
}
}
/**
* Prints a list of <option>'s for use in a <select> element
* asking how many bugs to display
*
* @param int $limit the presently selected limit to be used as the default
*
* @return void
*/
function show_limit_options($limit = 30)
{
for ($i = 10; $i < 100; $i += 10) {
echo '<option value="' . $i . '"';
if ($limit == $i) {
echo ' selected="selected"';
}
echo ">$i bugs</option>\n";
}
echo '<option value="All"';
if ($limit == 'All') {
echo ' selected="selected"';
}
echo ">All</option>\n";
}
/**
* Prints bug type <option>'s for use in a <select>
*
* Options include "Bug", "Documentation Problem" and "Feature/Change Request."
*
* @param string $current bug's current type
* @param bool $all whether or not 'All' should be an option
*
* @retun void
*/
function show_type_options($current = 'Bug', $all = false)
{
global $bug_types;
if ($all) {
if (!$current) {
$current = 'All';
}
echo '<option value="All"';
if ($current == 'All') {
echo ' selected="selected"';
}
echo ">All</option>\n";
} elseif (!$current) {
$current = 'bug';
}
foreach ($bug_types as $k => $v) {
$selected = strcasecmp($current, $k) ? '' : ' selected="selected"';
$k = htmlentities($k, ENT_QUOTES);
echo "<option value=\"$k\"$selected>$k</option>";
}
}
/**
* Prints bug state <option>'s for use in a <select> list
*
* @param string $state the bug's present state
* @param int $user_mode the 'edit' mode
* @param string $default the default value
*
* @return void
*/
function show_state_options($state, $user_mode = 0, $default = '', $assigned = 0)
{
global $state_types;
if (!$state && !$default) {
$state = $assigned ? 'Assigned' : 'Open';
} elseif (!$state) {
$state = $default;
}
/* regular users can only pick states with type 2 for unclosed bugs */
if ($state != 'All' && isset($state_types[$state]) && $state_types[$state] == 1 && $user_mode == 2) {
switch ($state)
{
/* If state was 'Feedback', set state automatically to 'Assigned' if the bug was
* assigned to someone before it to be set to 'Feedback', otherwise set it to 'Open'.
*/
case 'Feedback':
if ($assigned) {
echo "<option>Assigned</option>\n";
} else {
echo "<option>Open</option>\n";
}
break;
case 'No Feedback':
echo "<option>Re-Opened</option>\n";
break;
default:
echo "<option>$state</option>\n";
break;
}
/* Allow state 'Closed' always when current state is not 'Not a bug' */
if ($state != 'Not a bug') {
echo "<option>Closed</option>\n";
}
} else {
foreach($state_types as $type => $mode) {
if (($state == 'Closed' && $type == 'Open')
|| ($state == 'Open' && $type == 'Re-Opened')) {
continue;
}
if ($mode >= $user_mode) {
echo '<option';
if ($type == $state) {
echo ' selected="selected"';
}
echo ">$type</option>\n";
}
}
}
}
/**
* Prints bug resolution <option>'s for use in a <select> list
*
* @param string $current the bug's present state
* @param int $expande whether or not a longer explanation should be displayed
*
* @return void
*/
function show_reason_types($current = '', $expanded = 0)
{
global $RESOLVE_REASONS;
if ($expanded) {
echo '<option value=""></option>' . "\n";
}
foreach ($RESOLVE_REASONS as $val)
{
if (empty($val['package_name'])) {
$sel = ($current == $val['name']) ? " selected='selected'" : '';
echo "<option value='{$val['name']}' {$sel} >{$val['title']}";
if ($expanded) {
echo " ({$val['status']})";
}
echo "</option>\n";
}
}
}
/**
* Prints PHP version number <option>'s for use in a <select> list
*
* @param string $current the bug's current version number
*
* @return void
*/
function show_version_options($current)
{
global $ROOT_DIR, $versions;
$use = 0;
echo '<option value="">--Please Select--</option>' , "\n";
foreach($versions as $v) {
echo '<option';
if ($current == $v) {
echo ' selected="selected"';
}
echo '>' , htmlspecialchars($v) , "</option>\n";
if ($current == $v) {
$use++;
}
}
if (!$use && $current) {
echo '<option selected="selected">' , htmlspecialchars($current) , "</option>\n";
}
echo '<option value="earlier">Earlier? Upgrade first!</option>', "\n";
}
/**
* Prints package name <option>'s for use in a <select> list
*
* @param string $current the bug's present state
* @param int $show_any whether or not 'Any' should be an option. 'Any'
* will only be printed if no $current value exists.
* @param string $default the default value
*
* @return void
*/
function show_package_options($current, $show_any, $default = '')
{
global $pseudo_pkgs;
$disabled_style = ' style="background-color:#eee;"';
static $bug_groups;
if (!isset($bug_groups)) {
$bug_groups = array_filter(
$pseudo_pkgs,
function ($value) {
return is_array($value[2]);
}
);
}
if (!$current && (!$default || $default == 'none') && !$show_any) {
echo "<option value=\"none\">--Please Select--</option>\n";
} elseif (!$current && $show_any == 1) {
$current = 'Any';
} elseif (!$current) {
$current = $default;
}
if (!is_array($bug_groups)) {
return;
}
foreach ($bug_groups as $key => $bug_group) {
echo "<optgroup label=\"${bug_group[0]}\"" .
(($bug_group[1]) ? $disabled_style : ''), "\n>";
array_unshift($bug_group[2], $key);
foreach ($bug_group[2] as $name) {
$child = $pseudo_pkgs[$name];
if ($show_any == 1 || $key != 'Any') {
echo "<option value=\"$name\"";
if ((is_array($current) && in_array($name, $current)) || ($name == $current)) {
echo ' selected="selected"';
}
// Show disabled categories with different background color in listing
echo (($child[1]) ? $disabled_style : ''), ">{$child[0]}</option>\n";
}
}
echo "</optgroup>\n";
}
}
/**
* Prints a series of radio inputs to determine how the search
* term should be looked for
*
* @param string $current the users present selection
*
* @return void
*/
function show_boolean_options($current)
{
$options = ['any', 'all', 'raw'];
foreach ($options as $val => $type) {
echo '<input type="radio" name="boolean" value="', $val, '"';
if ($val === $current) {
echo ' checked="checked"';
}
echo ">$type \n";
}
}
/**
* Display errors or warnings as a <ul> inside a <div>
*
* Here's what happens depending on $in:
* + string: value is printed
* + array: looped through and each value is printed.
* If array is empty, nothing is displayed.
*
* @param string|array $in see long description
* @param string $class name of the HTML class for the <div> tag. ("errors", "warnings")
* @param string $head string to be put above the message
*
* @return bool true if errors were submitted, false if not
*/
function display_bug_error($in, $class = 'errors', $head = 'ERROR:')
{
if (!is_array($in)) {
$in = [$in];
} elseif (!count($in)) {
return false;
}
echo "<div class='{$class}'>{$head}<ul>";
foreach ($in as $msg) {
echo '<li>' , htmlspecialchars($msg) , "</li>\n";
}
echo "</ul></div>\n";
return true;
}
/**
* Returns array of changes going to be made
*/
function bug_diff($bug, $in)
{
$changed = [];
if (!empty($in['email']) && (trim($in['email']) != trim($bug['email']))) {
$changed['reported_by']['from'] = spam_protect($bug['email'], 'text');
$changed['reported_by']['to'] = spam_protect(txfield('email', $bug, $in), 'text');
}
$fields = [
'sdesc' => 'Summary',
'status' => 'Status',
'bug_type' => 'Type',
'package_name' => 'Package',
'php_os' => 'Operating System',
'php_version' => 'PHP Version',
'assign' => 'Assigned To',
'block_user_comment' => 'Block user comment',
'private' => 'Private report',
'cve_id' => 'CVE-ID'
];
foreach (array_keys($fields) as $name) {
if (array_key_exists($name, $in) && array_key_exists($name, $bug)) {
$to = trim($in[$name]);
$from = trim($bug[$name]);
if ($from != $to) {
if (in_array($name, ['private', 'block_user_comment'])) {
$from = $from == 'Y' ? 'Yes' : 'No';
$to = $to == 'Y' ? 'Yes' : 'No';
}
$changed[$name]['from'] = $from;
$changed[$name]['to'] = $to;
}
}
}
return $changed;
}
function bug_diff_render_html($diff)
{
$fields = [
'sdesc' => 'Summary',
'status' => 'Status',
'bug_type' => 'Type',
'package_name' => 'Package',
'php_os' => 'Operating System',
'php_version' => 'PHP Version',
'assign' => 'Assigned To',
'block_user_comment' => 'Block user comment',
'private' => 'Private report',
'cve_id' => 'CVE-ID'
];
// make diff output aligned
$actlength = $maxlength = 0;
foreach (array_keys($diff) as $v) {
$actlength = strlen($fields[$v]) + 2;
$maxlength = ($maxlength < $actlength) ? $actlength : $maxlength;
}
$changes = '<div class="changeset">' . "\n";
$spaces = str_repeat(' ', $maxlength + 1);
foreach ($diff as $name => $content) {
// align header content with headers (if a header contains
// more than one line, wrap it intelligently)
$field = str_pad($fields[$name] . ':', $maxlength);
$from = wordwrap('-'.$field.$content['from'], 72 - $maxlength, "\n$spaces"); // wrap and indent
$from = rtrim($from); // wordwrap may add spacer to last line
$to = wordwrap('+'.$field.$content['to'], 72 - $maxlength, "\n$spaces"); // wrap and indent
$to = rtrim($to); // wordwrap may add spacer to last line
$changes .= '<span class="removed">' . clean($from) . '</span>' . "\n";
$changes .= '<span class="added">' . clean($to) . '</span>' . "\n";
}
$changes .= '</div>';
return $changes;
}
/**
* Send an email notice about bug aditions and edits
*
* @param
*
* @return void
*/
function mail_bug_updates($bug, $in, $from, $ncomment, $edit = 1, $id = false)
{
global $tla, $bug_types, $siteBig, $site_method, $site_url, $basedir;
$text = [];
$headers = [];
$changed = bug_diff($bug, $in);
$from = str_replace(["\n", "\r"], '', $from);
/* Default addresses */
list($mailto, $mailfrom, $bcc, $params) = get_package_mail(oneof(@$in['package_name'], $bug['package_name']), $id, oneof(@$in['bug_type'], $bug['bug_type']));
$headers[] = [' ID', $bug['id']];
switch ($edit) {
case 4:
$headers[] = [' Patch added by', $from];
$from = "\"{$from}\" <{$mailfrom}>";
break;
case 3:
$headers[] = [' Comment by', $from];
$from = "\"{$from}\" <{$mailfrom}>";
break;
case 2:
$from = spam_protect(txfield('email', $bug, $in), 'text');
$headers[] = [' User updated by', $from];
$from = "\"{$from}\" <{$mailfrom}>";
break;
default:
$headers[] = [' Updated by', $from];
}
$fields = [
'email' => 'Reported by',
'sdesc' => 'Summary',
'status' => 'Status',
'bug_type' => 'Type',
'package_name' => 'Package',
'php_os' => 'Operating System',
'php_version' => 'PHP Version',
'assign' => 'Assigned To',
'block_user_comment' => 'Block user comment',
'private' => 'Private report',
'cve_id' => 'CVE-ID',
];
foreach ($fields as $name => $desc) {
$prefix = ' ';
if (isset($changed[$name])) {
$headers[] = ["-{$desc}", $changed[$name]['from']];
$prefix = '+';
}
/* only fields that are set get added. */
if ($f = txfield($name, $bug, $in)) {
if ($name == 'email') {
$f = spam_protect($f, 'text');
}
$foo = isset($changed[$name]['to']) ? $changed[$name]['to'] : $f;
$headers[] = [$prefix.$desc, $foo];
}
}
/* Make header output aligned */
$maxlength = 0;
$actlength = 0;
foreach ($headers as $v) {
$actlength = strlen($v[0]) + 1;
$maxlength = (($maxlength < $actlength) ? $actlength : $maxlength);
}
/* Align header content with headers (if a header contains more than one line, wrap it intelligently) */
$header_text = '';
$spaces = str_repeat(' ', $maxlength + 1);
foreach ($headers as $v) {
$hcontent = wordwrap($v[1], 72 - $maxlength, "\n{$spaces}"); // wrap and indent
$hcontent = rtrim($hcontent); // wordwrap may add spacer to last line
$header_text .= str_pad($v[0] . ':', $maxlength) . " {$hcontent}\n";
}
if ($ncomment) {
# $ncomment = preg_replace('#<div class="changeset">(.*)</div>#sUe', "ltrim(strip_tags('\\1'))", $ncomment);
$ncomment = preg_replace_callback('#<div class="changeset">(.*)</div>#sU', function ($m) { return ltrim(strip_tags($m[0])); }, $ncomment);
$text[] = " New Comment:\n\n{$ncomment}";
}
$old_comments = get_old_comments($bug['id'], empty($ncomment));
# $old_comments = preg_replace('#<div class="changeset">(.*)</div>#sUe', "ltrim(strip_tags('\\1'))", $old_comments);
$old_comments = preg_replace_callback('#<div class="changeset">(.*)</div>#sU', function ($m) { return ltrim(strip_tags($m[0])); }, $old_comments);
$text[] = $old_comments;
$wrapped_text = join("\n", $text);
/* user text with attention, headers and previous messages */
$user_text = <<< USER_TEXT
ATTENTION! Do NOT reply to this email!
To reply, use the web interface found at
{$site_method}://{$site_url}{$basedir}/bug.php?id={$bug['id']}&edit=2
{$header_text}
{$wrapped_text}
USER_TEXT;
/* developer text with headers, previous messages, and edit link */
$dev_text = <<< DEV_TEXT
Edit report at {$site_method}://{$site_url}{$basedir}/bug.php?id={$bug['id']}&edit=1
{$header_text}
{$wrapped_text}
--
Edit this bug report at {$site_method}://{$site_url}{$basedir}/bug.php?id={$bug['id']}&edit=1
DEV_TEXT;
if (preg_match('/.*@php\.net\z/', $bug['email'])) {
$user_text = $dev_text;
}
// Defaults
$subj = $bug_types[$bug['bug_type']];
$sdesc = txfield('sdesc', $bug, $in);
/* send mail if status was changed, there is a comment, private turned on/off or the bug type was changed to/from Security */
if (empty($in['status']) || $in['status'] != $bug['status'] || $ncomment != '' ||
(isset($in['private']) && $in['private'] != $bug['private']) ||
(isset($in['bug_type']) && $in['bug_type'] != $bug['bug_type'] &&
($in['bug_type'] == 'Security' || $bug['bug_type'] == 'Security'))) {
if (isset($in['bug_type']) && $in['bug_type'] != $bug['bug_type']) {
$subj = $bug_types[$bug['bug_type']] . '->' . $bug_types[$in['bug_type']];
}
$old_status = $bug['status'];
$new_status = $bug['status'];
if (isset($in['status']) && $in['status'] != $bug['status'] && $edit != 3) { /* status changed */
$new_status = $in['status'];
$subj .= " #{$bug['id']} [{$tla[$old_status]}->{$tla[$new_status]}]";
} elseif ($edit == 4) { /* patch */
$subj .= " #{$bug['id']} [PATCH]";
} elseif ($edit == 3) { /* comment */
$subj .= " #{$bug['id']} [Com]";
} else { /* status did not change and not comment */
$subj .= " #{$bug['id']} [{$tla[$bug['status']]}]";
}
// the user gets sent mail with an envelope sender that ignores bounces
bugs_mail(
$bug['email'],
"{$subj}: {$sdesc}",
$user_text,
"From: {$siteBig} Bug Database <{$mailfrom}>\n" .
"Bcc: {$bcc}\n" .
"X-PHP-Bug: {$bug['id']}\n" .
"X-PHP-Site: {$siteBig}\n" .
"In-Reply-To: <bug-{$bug['id']}@{$site_url}>"
);
// Spam protection
$tmp = $edit != 3 ? $in : $bug;
$tmp['new_status'] = $new_status;
$tmp['old_status'] = $old_status;
foreach (['bug_type', 'php_version', 'package_name', 'php_os'] as $field) {
$tmp[$field] = strtok($tmp[$field], "\r\n");
}
// but we go ahead and let the default sender get used for the list
bugs_mail(
$mailto,
"{$subj}: {$sdesc}",
$dev_text,
"From: {$from}\n".
"X-PHP-Bug: {$bug['id']}\n" .
"X-PHP-Site: {$siteBig}\n" .
"X-PHP-Type: {$tmp['bug_type']}\n" .
"X-PHP-Version: {$tmp['php_version']}\n" .
"X-PHP-Category: {$tmp['package_name']}\n" .
"X-PHP-OS: {$tmp['php_os']}\n" .
"X-PHP-Status: {$tmp['new_status']}\n" .
"X-PHP-Old-Status: {$tmp['old_status']}\n" .
"In-Reply-To: <bug-{$bug['id']}@{$site_url}>",
$params
);
}
/* if a developer assigns someone else, let that other person know about it */
if ($edit == 1 && $in['assign'] && $in['assign'] != $bug['assign']) {
$email = $in['assign'] . '@php.net';
// If the developer assigns him self then skip
if ($email == $from) {
return;
}
bugs_mail(
$email,
$bug_types[$bug['bug_type']] . ' #' . $bug['id'] . ' ' . txfield('sdesc', $bug, $in),
"{$in['assign']} you have just been assigned to this bug by {$from}\n\n{$dev_text}",
"From: {$from}\n" .
"X-PHP-Bug: {$bug['id']}\n" .