]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Added configurable robots.txt file.
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Repos\EntityRepo;
5 use Illuminate\Http\Request;
6 use Illuminate\Http\Response;
7 use Views;
8
9 class HomeController extends Controller
10 {
11     protected $entityRepo;
12
13     /**
14      * HomeController constructor.
15      * @param EntityRepo $entityRepo
16      */
17     public function __construct(EntityRepo $entityRepo)
18     {
19         $this->entityRepo = $entityRepo;
20         parent::__construct();
21     }
22
23
24     /**
25      * Display the homepage.
26      * @return Response
27      */
28     public function index()
29     {
30         $activity = Activity::latest(10);
31         $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
32         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
33         $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
34         $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 12);
35
36         // Custom homepage
37         $customHomepage = false;
38         $homepageSetting = setting('app-homepage');
39         if ($homepageSetting) {
40             $id = intval(explode(':', $homepageSetting)[0]);
41             $customHomepage = $this->entityRepo->getById('page', $id, false, true);
42             $this->entityRepo->renderPage($customHomepage, true);
43         }
44
45         $view = $customHomepage ? 'home-custom' : 'home';
46         return view($view, [
47             'activity' => $activity,
48             'recents' => $recents,
49             'recentlyUpdatedPages' => $recentlyUpdatedPages,
50             'draftPages' => $draftPages,
51             'customHomepage' => $customHomepage
52         ]);
53     }
54
55     /**
56      * Get a js representation of the current translations
57      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
58      * @throws \Exception
59      */
60     public function getTranslations()
61     {
62         $locale = app()->getLocale();
63         $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
64         if (cache()->has($cacheKey) && config('app.env') !== 'development') {
65             $resp = cache($cacheKey);
66         } else {
67             $translations = [
68                 // Get only translations which might be used in JS
69                 'common' => trans('common'),
70                 'components' => trans('components'),
71                 'entities' => trans('entities'),
72                 'errors' => trans('errors')
73             ];
74             if ($locale !== 'en') {
75                 $enTrans = [
76                     'common' => trans('common', [], 'en'),
77                     'components' => trans('components', [], 'en'),
78                     'entities' => trans('entities', [], 'en'),
79                     'errors' => trans('errors', [], 'en')
80                 ];
81                 $translations = array_replace_recursive($enTrans, $translations);
82             }
83             $resp = 'window.translations = ' . json_encode($translations);
84             cache()->put($cacheKey, $resp, 120);
85         }
86
87         return response($resp, 200, [
88             'Content-Type' => 'application/javascript'
89         ]);
90     }
91
92     /**
93      * Get an icon via image request.
94      * Can provide a 'color' parameter with hex value to color the icon.
95      * @param $iconName
96      * @param Request $request
97      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
98      */
99     public function getIcon($iconName, Request $request)
100     {
101         $attrs = [];
102         if ($request->filled('color')) {
103             $attrs['fill'] = '#' . $request->get('color');
104         }
105
106         $icon = icon($iconName, $attrs);
107         return response($icon, 200, [
108             'Content-Type' => 'image/svg+xml',
109             'Cache-Control' => 'max-age=3600',
110         ]);
111     }
112
113     /**
114      * Get custom head HTML, Used in ajax calls to show in editor.
115      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
116      */
117     public function customHeadContent()
118     {
119         return view('partials/custom-head-content');
120     }
121
122     /**
123      * Show the view for /robots.txt
124      * @return $this
125      */
126     public function getRobots()
127     {
128         $sitePublic = setting('app-public', false);
129         $allowRobots = config('app.allow_robots');
130         if ($allowRobots === null) {
131             $allowRobots = $sitePublic;
132         }
133         return response()
134             ->view('robots', ['allowRobots' => $allowRobots])
135             ->header('Content-Type', 'text/plain');
136     }
137 }