]> BookStack Code Mirror - website/blob - app/Http/Controllers/HomeController.php
Merge branch 'master' of github.com:ssddanbrown/BookStack-Site
[website] / app / Http / Controllers / HomeController.php
1 <?php namespace App\Http\Controllers;
2
3 use stdClass;
4
5 class HomeController extends Controller {
6
7
8     /**
9      * Show the homepage.
10      * Gets content from the blog to show.
11      * @return View
12      */
13         public function home()
14     {
15                 // Get Blog articles
16         if ($this->cache->has('blog-feed')) {
17             $blogItems = $this->cache->get('blog-feed');
18         } else {
19             $blogItems = $this->getBlogPosts();
20             $this->cache->put('blog-feed', $blogItems, 60*12);
21         }
22
23                 return view('home', [
24             'blogItems' => $blogItems
25         ]);
26         }
27
28     /**
29      * Get blog posts from the BookStack blog.
30      * @return Array[stdClass]
31      */
32     public function getBlogPosts()
33     {
34         $contents = file_get_contents('https://www.bookstackapp.com/blog/rss/');
35         $blogItems = [];
36         $rss = simplexml_load_string($contents);
37
38         if ($rss !== false) {
39             foreach ($rss->channel->{'item'} as $item) {
40                 $blogItem = new stdClass;
41                 $blogItem->link = (string) $item->link;
42                 $blogItem->title = (string) $item->title;
43                 $blogItem->description = (string) strip_tags($item->description);
44                 $blogItems[] = $blogItem;
45             }
46         } else {
47             return [];
48         }
49
50         $blogItems = array_slice($blogItems, 0, 3);
51         return $blogItems;
52     }
53
54 }