forked from FoolCode/FoolFuuka
/
Chan.php
1335 lines (1108 loc) · 49.9 KB
/
Chan.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
namespace Foolz\Foolfuuka\Controller;
use Foolz\Foolframe\Controller\Common;
use Foolz\Foolframe\Model\Config;
use Foolz\Foolframe\Model\Preferences;
use Foolz\Foolframe\Model\Uri;
use Foolz\Foolframe\Model\Util;
use Foolz\Foolframe\Model\Validation\ActiveConstraint\Trim;
use Foolz\Foolframe\Model\Validation\Validator;
use Foolz\Foolframe\Model\Cookie;
use Foolz\Foolfuuka\Model\Ban;
use Foolz\Foolfuuka\Model\BanFactory;
use Foolz\Foolfuuka\Model\Board;
use Foolz\Foolfuuka\Model\Comment;
use Foolz\Foolfuuka\Model\CommentBulk;
use Foolz\Foolfuuka\Model\CommentFactory;
use Foolz\Foolfuuka\Model\CommentInsert;
use Foolz\Foolfuuka\Model\Media;
use Foolz\Foolfuuka\Model\MediaFactory;
use Foolz\Foolfuuka\Model\Radix;
use Foolz\Foolfuuka\Model\RadixCollection;
use Foolz\Foolfuuka\Model\Report;
use Foolz\Foolfuuka\Model\Search;
use Foolz\Inet\Inet;
use Foolz\Profiler\Profiler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Validator\Constraints as Assert;
class Chan extends Common
{
/**
* The Theme object
*
* @var \Foolz\Theme\Theme
*/
protected $theme = null;
/**
* A builder object with some defaults appended
*
* @var \Foolz\Theme\Builder
*/
protected $builder = null;
/**
* The ParamManager object of the builder
*
* @var \Foolz\Theme\ParamManager
*/
protected $param_manager = null;
/**
* The Request object
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request = null;
/**
* The Response object
*
* @var Response|StreamedResponse
*/
protected $response = null;
/**
* The currently selected radix
*
* @var Radix|null
*/
protected $radix = null;
/**
* @var Config
*/
protected $config;
/**
* @var Preferences
*/
protected $preferences;
/**
* @var Uri
*/
protected $uri;
/**
* @var Profiler
*/
protected $profiler;
/**
* @var RadixCollection
*/
protected $radix_coll;
/**
* @var MediaFactory
*/
protected $media_factory;
/**
* @var CommentFactory
*/
protected $comment_factory;
public function before()
{
$this->config = $this->getContext()->getService('config');
$this->preferences = $this->getContext()->getService('preferences');
$this->uri = $this->getContext()->getService('uri');
$this->profiler = $this->getContext()->getService('profiler');
$this->radix_coll = $this->getContext()->getService('foolfuuka.radix_collection');
$this->media_factory = $this->getContext()->getService('foolfuuka.media_factory');
$this->comment_factory = $this->getContext()->getService('foolfuuka.comment_factory');
// this has already been forged in the foolfuuka bootstrap
$theme_instance = \Foolz\Theme\Loader::forge('foolfuuka');
try {
$theme_name = $this->getQuery('theme', $this->getCookie('theme')) ? : $this->preferences->get('foolfuuka.theme.default');
$theme_name_exploded = explode('/', $theme_name);
if (count($theme_name_exploded) >=2) {
$theme_name = $theme_name_exploded[0].'/'.$theme_name_exploded[1];
}
$theme = $theme_instance->get($theme_name);
if (!isset($theme->enabled) || !$theme->enabled) {
throw new \OutOfBoundsException;
}
$this->theme = $theme;
} catch (\OutOfBoundsException $e) {
$theme_name = 'foolz/foolfuuka-theme-foolfuuka';
$this->theme = $theme_instance->get('foolz/foolfuuka-theme-foolfuuka');
}
// TODO this is currently bootstrapped in the foolfuuka bootstrap because we need it running before the router.
//$this->theme->bootstrap();
$this->builder = $this->theme->createBuilder();
$this->param_manager = $this->builder->getParamManager();
$this->builder->createLayout('chan');
if (count($theme_name_exploded) == 3) {
try {
$this->builder->setStyle($theme_name_exploded[2]);
} catch (\OutOfBoundsException $e) {
// just let it go with default on getStyle()
}
}
$pass = $this->getCookie('reply_password', '');
$name = $this->getCookie('reply_name');
$email = $this->getCookie('reply_email');
// KEEP THIS IN SYNC WITH THE ONE IN THE POSTS ADMIN PANEL
$to_bind = [
'context' => $this->getContext(),
'request' => $this->getRequest(),
'user_name' => $name,
'user_email' => $email,
'user_pass' => $pass,
'disable_headers' => false,
'is_page' => false,
'is_thread' => false,
'is_last50' => false,
'order' => false,
'modifiers' => [],
'backend_vars' => [
'user_name' => $name,
'user_email' => $email,
'user_pass' => $pass,
'site_url' => $this->uri->base(),
'default_url' => $this->uri->base(),
'archive_url' => $this->uri->base(),
'system_url' => $this->uri->base(),
'api_url' => $this->uri->base(),
'cookie_domain' => $this->config->get('foolz/foolframe', 'config', 'config.cookie_domain'),
'cookie_prefix' => $this->config->get('foolz/foolframe', 'config', 'config.cookie_prefix'),
'selected_theme' => $theme_name,
'csrf_token_key' => 'csrf_token',
'images' => [
'banned_image' => $this->theme->getAssetManager()->getAssetLink('images/banned-image.png'),
'banned_image_width' => 150,
'banned_image_height' => 150,
'missing_image' => $this->theme->getAssetManager()->getAssetLink('images/missing-image.jpg'),
'missing_image_width' => 150,
'missing_image_height' => 150,
],
'gettext' => [
'submit_state' => _i('Submitting'),
'thread_is_real_time' => _i('This thread is being displayed in real time.'),
'update_now' => _i('Update now'),
'ghost_mode' => _i('This thread has entered ghost mode. Your reply will be marked as a ghost post and will only affect the ghost index.')
]
]
];
$this->param_manager->setParams($to_bind);
$this->builder->createPartial('tools_modal', 'tools_modal');
$this->builder->createPartial('tools_search', 'tools_search');
$this->builder->createPartial('tools_advanced_search', 'advanced_search');
}
public function router($method, $parameters)
{
$request = $this->getRequest();
// create response object, store request object
if ($request->isXmlHttpRequest()) {
$this->response = new JsonResponse();
} else {
$this->response = new Response();
}
$this->request = $request;
// let's see if we hit a radix route
if ($request->attributes->get('radix_shortname') !== null) {
// the radix for sure exists, we came here from the defined routes after all
$this->radix = $this->radix_coll->getByShortname($request->attributes->get('radix_shortname'));
$this->param_manager->setParam('radix', $this->radix);
$backend_vars = $this->param_manager->getParam('backend_vars');
$backend_vars['board_shortname'] = $this->radix->shortname;
$this->param_manager->setParam('backend_vars', $backend_vars);
$this->builder->getProps()->addTitle($this->radix->getValue('formatted_title'));
// methods callable with a radix are prefixed with radix_
if (method_exists($this, 'radix_'.$method)) {
return [$this, 'radix_'.$method, $parameters];
}
// a board and no function means we're out of the street
return [$this, 'action_404', []];
}
$this->radix = null;
$this->param_manager->setParam('radix', null);
$this->builder->getProps()->addTitle($this->preferences->get('foolframe.gen.website_title', $this->preferences->get('foolfuuka.gen.website_title')));
if (method_exists($this, 'action_'.$method)) {
return [$this, 'action_'.$method, $parameters];
}
return [$this, 'action_404', []];
}
public function setLastModified($timestamp = 0, $max_age = 0)
{
$time = new \DateTime('@'.$timestamp);
$etag = md5($this->builder->getTheme()->getDir().$this->builder->getStyle().'|'.$timestamp.'|'.$max_age);
$this->response->headers->addCacheControlDirective('must-revalidate', true);
$this->response->setLastModified($time);
$this->response->setEtag($etag);
$this->response->setMaxAge($max_age);
}
public function action_index()
{
$this->param_manager->setParam('disable_headers', true);
$this->builder->createPartial('body', 'index');
return $this->response->setContent($this->builder->build());
}
public function action_404($error = null)
{
return $this->error($error === null ? _i('Page not found. You can use the search if you were looking for something!') : $error, 404);
}
protected function error($error = null, $code = 200)
{
$this->builder->createPartial('body', 'error')
->getParamManager()
->setParams(['error' => $error === null ? _i('We encountered an unexpected error.') : $error]);
$this->response->setStatusCode($code);
if ($this->response instanceof StreamedResponse) {
$this->response->setCallback(function() {
$this->builder->stream();
});
} else {
$this->response->setContent($this->builder->build());
}
return $this->response;
}
protected function message($level = 'success', $message = null, $code = 200)
{
$this->builder->createPartial('body', 'message')
->getParamManager()
->setParams([
'level' => $level,
'message' => $message
]);
return $this->response->setContent($this->builder->build())->setStatusCode($code);
}
public function action_theme($vendor = 'foolz', $theme = 'foolfuuka-theme-default', $style = '')
{
$this->builder->getProps()->addTitle(_i('Changing Theme Settings'));
$theme = $vendor.'/'.$theme.'/'.$style;
$this->response->headers->setCookie(new Cookie($this->getContext(), 'theme', $theme, 31536000));
if ($this->getRequest()->headers->get('referer')) {
$url = $this->getRequest()->headers->get('referer');
} else {
$url = $this->uri->base();
}
$this->builder->createLayout('redirect')
->getParamManager()
->setParam('url', $url);
$this->builder->getProps()->addTitle(_i('Redirecting'));
return $this->response->setContent($this->builder->build());
}
public function action_language($language = 'en_EN')
{
$this->response->headers->setCookie(new Cookie($this->getContext(), 'language', $language, 31536000));
if ($this->getRequest()->headers->get('referer')) {
$url = $this->getRequest()->headers->get('referer');
} else {
$url = $this->uri->base();
}
$this->builder->createLayout('redirect')
->getParamManager()
->setParam('url', $url);
$this->builder->getProps()->addTitle(_i('Changing Language'));
return $this->response->setContent($this->builder->build());
}
public function action_opensearch()
{
$this->builder->createLayout('open_search');
return $this->response->setContent($this->builder->build());
}
public function radix_page_mode($_mode = 'by_post')
{
$mode = $_mode === 'by_thread' ? 'by_thread' : 'by_post';
$type = $this->radix->archive ? 'archive' : 'board';
$this->response = new RedirectResponse($this->uri->create($this->radix->shortname));
$this->response->headers->setCookie(new Cookie($this->getContext(), 'default_theme_page_mode_'.$type, $mode, 31536000));
return $this->response;
}
public function radix_page($page = 1)
{
$order = $this->getCookie('default_theme_page_mode_'. ($this->radix->archive ? 'archive' : 'board')) === 'by_thread'
? 'by_thread' : 'by_post';
$options = [
'per_page' => $this->radix->getValue('threads_per_page'),
'per_thread' => 5,
'order' => $order
];
return $this->latest($page, $options);
}
public function radix_ghost($page = 1)
{
$options = [
'per_page' => $this->radix->getValue('threads_per_page'),
'per_thread' => 5,
'order' => 'ghost'
];
return $this->latest($page, $options);
}
protected function latest($page = 1, $options = [])
{
$this->response = new StreamedResponse();
$this->profiler->log('Controller Chan::latest Start');
try {
$board = Board::forge($this->getContext())
->getLatest()
->setRadix($this->radix)
->setPage($page)
->setOptions($options);
// execute in case there's more exceptions to handle
$board->getComments();
$board->getCount();
} catch (\Foolz\Foolfuuka\Model\BoardException $e) {
$this->profiler->log('Controller Chan::latest End Prematurely');
return $this->error($e->getMessage());
}
if ($page > 1) {
switch($options['order']) {
case 'by_post':
$order_string = _i('Threads by latest replies');
break;
case 'by_thread':
$order_string = _i('Threads by creation');
break;
case 'ghost':
$order_string = _i('Threads by latest ghost replies');
break;
}
$this->builder->getProps()->addTitle(_i('Page').' '.$page);
$this->param_manager->setParam('section_title', $order_string.' - '._i('Page').' '.$page);
}
$this->builder->createPartial('body', 'board')
->getParamManager()->setParams([
'board' => $board->getComments(),
'posts_per_thread' => $options['per_thread'] - 1
]);
$this->param_manager->setParams([
'is_page' => true,
'order' => $options['order'],
'pagination' => [
'base_url' => $this->uri->create([$this->radix->shortname, $options['order'] === 'ghost' ? 'ghost' :
'page']),
'current_page' => $page,
'total' => $board->getPages()
],
'disable_image_upload' => $this->radix->getValue('op_image_upload_necessity') === 'never'
]);
if (!$this->radix->archive) {
$this->builder->createPartial('tools_new_thread_box', 'tools_reply_box');
}
$this->profiler->logMem('Controller Chan $this', $this);
$this->profiler->log('Controller Chan::latest End');
$this->response->setCallback(function() {
$this->builder->stream();
});
return $this->response;
}
public function radix_thread($num = 0)
{
return $this->thread($num);
}
public function radix_last50($num = 0)
{
return new RedirectResponse($this->uri->create($this->radix->shortname.'/last/50/'.$num));
}
public function radix_last($limit = 0, $num = 0)
{
if (!ctype_digit((string) $limit) || $limit < 1) {
return $this->action_404();
}
return $this->thread($num, ['type' => 'last_x', 'last_limit' => $limit]);
}
protected function thread($num = 0, $options = [])
{
$this->profiler->log('Controller Chan::thread Start');
$num = str_replace('S', '', $num);
try {
$board = Board::forge($this->getContext())
->getThread($num)
->setRadix($this->radix)
->setOptions($options);
// get the current status of the thread
$thread_status = $board->getThreadStatus();
$last_modified = $thread_status['last_modified'];
$this->setLastModified($last_modified);
if (!$this->response->isNotModified($this->request)) {
$this->response = new StreamedResponse();
$this->setLastModified($last_modified);
// execute in case there's more exceptions to handle
$thread = $board->getComments();
// get the latest doc_id and latest timestamp for realtime stuff
$latest_doc_id = $board->getHighest('doc_id')->comment->doc_id;
$latest_timestamp = $board->getHighest('timestamp')->comment->timestamp;
$this->builder->getProps()->addTitle(_i('Thread').' #'.$num);
$this->param_manager->setParams([
'thread_id' => $num,
'is_thread' => true,
'disable_image_upload' => $thread_status['disable_image_upload'],
'thread_dead' => $thread_status['dead'],
'latest_doc_id' => $latest_doc_id,
'latest_timestamp' => $latest_timestamp,
'thread_op_data' => $thread[$num]['op']
]);
$this->builder->createPartial('body', 'board')
->getParamManager()
->setParams([
'board' => $board->getComments(),
]);
$backend_vars = $this->param_manager->getParam('backend_vars');
$backend_vars['thread_id'] = $num;
$backend_vars['latest_timestamp'] = $latest_timestamp;
$backend_vars['latest_doc_id'] = $latest_doc_id;
$backend_vars['board_shortname'] = $this->radix->shortname;
if (isset($options['last_limit']) && $options['last_limit']) {
$this->param_manager->setParam('controller_method', 'last/'.$options['last_limit']);
$backend_vars['last_limit'] = $options['last_limit'];
}
$this->param_manager->setParam('backend_vars', $backend_vars);
if (!$thread_status['closed']) {
$this->builder->createPartial('tools_reply_box', 'tools_reply_box');
}
$this->profiler->logMem('Controller Chan $this', $this);
$this->profiler->log('Controller Chan::thread End');
$this->response->setCallback(function() {
$this->builder->stream();
});
}
} catch (\Foolz\Foolfuuka\Model\BoardThreadNotFoundException $e) {
$this->profiler->log('Controller Chan::thread End Prematurely');
return $this->error($e->getMessage(), 404);
} catch (\Foolz\Foolfuuka\Model\BoardException $e) {
$this->profiler->log('Controller Chan::thread End Prematurely');
return $this->error($e->getMessage());
}
return $this->response;
}
public function radix_gallery($page = 1)
{
$this->response = new StreamedResponse();
try {
$board = Board::forge($this->getContext())
->getThreads()
->setRadix($this->radix)
->setPage($page)
->setOptions('per_page', 100);
$comments = $board->getComments();
$this->builder->createPartial('body', 'gallery')
->getParamManager()
->setParams([
'board' => $board->getComments()
]);
$this->param_manager->setParams([
'pagination' => [
'base_url' => $this->uri->create([$this->radix->shortname, 'gallery']),
'current_page' => $page,
'total' => $board->getPages()
]
]);
} catch (\Foolz\Foolfuuka\Model\BoardException $e) {
return $this->error($e->getMessage());
}
$this->response->setCallback(function() {
$this->builder->stream();
});
return $this->response;
}
public function radix_post($num = 0)
{
try {
if ($this->getPost('post') || !Board::isValidPostNumber($num)) {
// obtain post number and unset search string
preg_match('/(?:^|\/)(\d+)(?:[_,]([0-9]*))?/', $this->getPost('post') ? : $num, $post);
unset($post[0]);
return new RedirectResponse($this->uri->create([$this->radix->shortname, 'post', implode('_', $post)]));
}
$board = Board::forge($this->getContext())
->getPost()
->setRadix($this->radix)
->setOptions('num', $num);
$comments = $board->getComments();
// it always returns an array
$comment = current($comments);
$redirect = $this->uri->create($this->radix->shortname.'/thread/'.$comment->comment->thread_num.'/');
if (!$comment->comment->op) {
$redirect .= '#'.$comment->comment->num.($comment->comment->subnum ? '_'.$comment->comment->subnum :'');
}
$this->builder->createLayout('redirect')
->getParamManager()
->setParam('url', $redirect);
$this->builder->getProps()->addTitle(_i('Redirecting'));
} catch (\Foolz\Foolfuuka\Model\BoardMalformedInputException $e) {
return $this->error(_i('The post number you submitted is invalid.'));
} catch (\Foolz\Foolfuuka\Model\BoardPostNotFoundException $e) {
return $this->error(_i('The post you are looking for does not exist.'));
}
return $this->response->setContent($this->builder->build());
}
public function action_reports()
{
if (!$this->getAuth()->hasAccess('comment.reports')) {
return $this->action_404();
}
$this->response = new StreamedResponse();
$this->param_manager->setParam('section_title', _i('Reports'));
/** @var Report[] $reports */
$reports = $this->getContext()->getService('foolfuuka.report_collection')->getAll();
$results = [];
foreach ($reports as $report) {
if (($result = $report->getComment()) !== null) {
$results[0]['posts'][] = $result;
}
}
$this->builder->createPartial('body', 'board')
->getParamManager()
->setParams([
'board' => $results,
'modifiers' => [
'post_show_board_name' => true,
'post_show_view_button' => true
]
]);
$this->response->setCallback(function() {
$this->builder->stream();
});
return $this->response;
}
/**
* Display all of the posts that contain the MEDIA HASH provided.
* As of 2012-05-17, fetching of posts with same media hash is done via search system.
* Due to backwards compatibility, this function will still be used for non-urlsafe and urlsafe hashes.
*/
public function radix_image()
{
// support non-urlsafe hash
$uri = array_filter(array_slice($this->uri->segments(), 5));
$imploded_uri = rawurldecode(implode('/', $uri));
if (mb_strlen($imploded_uri, 'utf-8') < 22) {
return $this->error(_i('Your image hash is malformed.'));
}
// obtain actual media hash (non-urlsafe)
$hash = mb_substr($imploded_uri, 0, 22, 'utf-8');
if (strpos($hash, '/') !== false || strpos($hash, '+') !== false) {
$hash = Media::urlsafe_b64encode(Media::urlsafe_b64decode($hash));
}
// Obtain the PAGE from URI.
$page = 1;
if (mb_strlen($imploded_uri, 'utf-8') > 28) {
$page = mb_substr($imploded_uri, 28, null, 'utf-8');
}
// Fetch the POSTS with same media hash and generate the IMAGEPOSTS.
$page = intval($page);
return new RedirectResponse($this->uri->create([
$this->radix->shortname, 'search', 'image', $hash, 'order', 'desc', 'page', $page]), 301);
}
public function radix_full_image($filename = null)
{
if ($filename === null) {
return $this->action_404();
}
try {
$bulk = $this->media_factory->getByFilename($this->radix, $filename);
} catch (\Foolz\Foolfuuka\Model\MediaException $e) {
return $this->action_404(_i('The image was never in our databases.'));
}
$media = new Media($this->getContext(), $bulk);
if ($media->getMediaLink($this->getRequest())) {
return new RedirectResponse($media->getMediaLink($this->getRequest()), 303);
}
return new RedirectResponse(
$this->uri->create([$this->radix->shortname, 'search', 'image', rawurlencode(substr($media->media_hash, 0,
-2))]));
}
public function radix_redirect($filename = null)
{
$redirect = $this->uri->create([$this->radix->shortname]).$filename;
if ($this->radix->archive) {
$redirect = ($this->radix->getValue('images_url') ? : '//images.4chan.org/'.$this->radix->shortname.'/src/').$filename;
}
$this->builder->createLayout('redirect')
->getParamManager()
->setParam('url', $redirect);
$this->builder->getProps()->addTitle(_i('Redirecting'));
return $this->response->setContent($this->builder->build());
}
public function radix_advanced_search()
{
return $this->action_advanced_search();
}
public function action_advanced_search()
{
$this->builder->createPartial('body', 'advanced_search')
->getParamManager()
->setParam('search_structure', Search::structure());
$this->builder->getParamManager()->setParam('section_title', _i('Advanced search'));
if ($this->radix !== null) {
$this->builder->getPartial('body')
->getParamManager()
->setParam('search', ['board' => [$this->radix->shortname]]);
}
return $this->response->setContent($this->builder->build());
}
public function action_search()
{
return call_user_func_array([$this, 'radix_search'], func_get_args());
}
public function radix_search()
{
if ($this->getPost('submit_search_global')) {
$this->radix = null;
}
$text = $this->getPost('text');
if ($this->radix !== null && $this->getPost('submit_post')) {
return $this->radix_post(str_replace(',', '_', $text));
}
$this->response = new StreamedResponse();
// Check all allowed search modifiers and apply only these
$modifiers = [
'boards', 'subject', 'text', 'username', 'tripcode', 'email', 'filename', 'capcode', 'uid',
'image', 'deleted', 'ghost', 'type', 'filter', 'start', 'end', 'order', 'page'
];
if ($this->getAuth()->hasAccess('comment.see_ip')) {;
$modifiers[] = 'poster_ip';
$modifiers[] = 'deletion_mode';
}
// GET -> URL Redirection to provide URL presentable for sharing links.
if ($this->getPost()) {
if ($this->radix !== null) {
$redirect_url = [$this->radix->shortname, 'search'];
} else {
$redirect_url = ['_', 'search'];
}
foreach ($modifiers as $modifier) {
if ($this->getPost($modifier)) {
if ($modifier === 'image') {
array_push($redirect_url, $modifier);
array_push($redirect_url,
rawurlencode(Media::urlsafe_b64encode(Media::urlsafe_b64decode($this->getPost($modifier))))
);
} elseif ($modifier === 'boards') {
if ($this->getPost('submit_search_global')) {
} elseif (count($this->getPost($modifier)) == 1) {
$boards = $this->getPost($modifier);
$redirect_url[0] = $boards[0];
} elseif (count($this->getPost($modifier)) > 1) {
$redirect_url[0] = '_';
// avoid setting this if we're just searching on all the boards
$sphinx_boards = [];
foreach ($this->radix_coll->getAll() as $k => $b) {
if ($b->sphinx) {
$sphinx_boards[] = $b;
}
}
if (count($sphinx_boards) !== count($this->getPost($modifier))) {
array_push($redirect_url, $modifier);
array_push($redirect_url, rawurlencode(implode('.', $this->getPost($modifier))));
}
}
} else {
array_push($redirect_url, $modifier);
array_push($redirect_url, rawurlencode($this->getPost($modifier)));
}
}
}
return new RedirectResponse($this->uri->create($redirect_url), 303);
}
$search = $this->uri->uri_to_assoc($this->request->getPathInfo(), 1, $modifiers);
$this->param_manager->setParam('search', $search);
// latest searches system
if (!is_array($cookie_array = @json_decode($this->getCookie('search_latest_5'), true))) {
$cookie_array = [];
}
// sanitize
foreach($cookie_array as $item) {
// all subitems must be array, all must have 'board'
if (!is_array($item) || !isset($item['board'])) {
$cookie_array = [];
break;
}
}
$search_opts = array_filter($search);
$search_opts['board'] = $this->radix !== null ? $this->radix->shortname : false;
unset($search_opts['page']);
// if it's already in the latest searches, remove the previous entry
foreach($cookie_array as $key => $item) {
if ($item === $search_opts) {
unset($cookie_array[$key]);
break;
}
}
// we don't want more than 5 entries for latest searches
if (count($cookie_array) > 4) {
array_pop($cookie_array);
}
array_unshift($cookie_array, $search_opts);
$this->builder->getPartial('tools_search')
->getParamManager()
->setParam('latest_searches', $cookie_array);
$this->response->headers->setCookie(
new Cookie($this->getContext(), 'search_latest_5', json_encode($cookie_array), 60 * 60 * 24 * 30)
);
foreach ($search as $key => $value) {
if ($value !== null) {
$search[$key] = trim(rawurldecode($value));
}
}
if ($search['boards'] !== null) {
$search['boards'] = explode('.', $search['boards']);
}
if ($search['image'] !== null) {
$search['image'] = base64_encode(Media::urlsafe_b64decode($search['image']));
}
if ($this->getAuth()->hasAccess('comment.see_ip') && $search['poster_ip'] !== null) {
if (!filter_var($search['poster_ip'], FILTER_VALIDATE_IP)) {
return $this->error(_i('The poster IP you inserted is not a valid IP address.'));
}
$search['poster_ip'] = Inet::ptod($search['poster_ip']);
}
try {
$board = Search::forge($this->getContext())
->getSearch($search)
->setRadix($this->radix)
->setPage($search['page'] ? $search['page'] : 1);
$board->getComments();
} catch (\Foolz\Foolfuuka\Model\SearchException $e) {
return $this->error($e->getMessage());
} catch (\Foolz\Foolfuuka\Model\BoardException $e) {
return $this->error($e->getMessage());
} catch (\Foolz\SphinxQL\ConnectionException $e) {
return $this->error($this->preferences->get('foolfuuka.sphinx.custom_message', 'It appears that the search engine is offline at the moment. Please try again later.'));
}
// Generate the $title with all search modifiers enabled.
$title = [];
if ($search['text'])
array_push($title,
sprintf(_i('that contain ‘%s’'),
e($search['text'])));
if ($search['subject'])
array_push($title,
sprintf(_i('with the subject ‘%s’'),
e($search['subject'])));
if ($search['username'])
array_push($title,
sprintf(_i('with the username ‘%s’'),
e($search['username'])));
if ($search['tripcode'])
array_push($title,
sprintf(_i('with the tripcode ‘%s’'),
e($search['tripcode'])));
if ($search['filename'])
array_push($title,
sprintf(_i('with the filename ‘%s’'),
e($search['filename'])));
if ($search['image']) {
array_push($title,
sprintf(_i('with the image hash ‘%s’'),
e($search['image'])));
}
if ($search['deleted'] == 'deleted')
array_push($title, _i('that have been deleted'));
if ($search['deleted'] == 'not-deleted')
array_push($title, _i('that has not been deleted'));
if ($search['ghost'] == 'only')
array_push($title, _i('that are by ghosts'));
if ($search['ghost'] == 'none')
array_push($title, _i('that are not by ghosts'));
if ($search['type'] == 'sticky')
array_push($title, _i('that were stickied'));
if ($search['type'] == 'op')
array_push($title, _i('that are only OP posts'));
if ($search['type'] == 'posts')
array_push($title, _i('that are only non-OP posts'));
if ($search['filter'] == 'image')
array_push($title, _i('that do not contain images'));
if ($search['filter'] == 'text')
array_push($title, _i('that only contain images'));
if ($search['capcode'] == 'user')
array_push($title, _i('that were made by users'));
if ($search['capcode'] == 'mod')
array_push($title, _i('that were made by mods'));
if ($search['capcode'] == 'admin')
array_push($title, _i('that were made by admins'));
if ($search['start'])
array_push($title, sprintf(_i('posts after %s'), e($search['start'])));
if ($search['end'])
array_push($title, sprintf(_i('posts before %s'), e($search['end'])));
if ($search['order'] == 'asc')
array_push($title, _i('in ascending order'));
if (!empty($title)) {
$title = sprintf(_i('Searching for posts %s.'),
implode(' ' . _i('and') . ' ', $title));
} else {
$title = _i('Displaying all posts with no filters applied.');
}
if ($this->radix) {
$this->builder->getProps()->addTitle($title);
} else {
$this->builder->getProps()->addTitle('Global Search » '.$title);
}
if ($board->getSearchCount() > 5000) {
$search_title = sprintf(_i('%s <small>Returning only first %d of %d results found.</small>',
$title, $this->preferences->get('foolfuuka.sphinx.max_matches', 5000), $board->getSearchCount()));
} else {