]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/PageApiController.php
API Docs: Allowed multi-paragraph descriptions
[bookstack] / app / Entities / Controllers / PageApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Exceptions\PermissionsException;
10 use BookStack\Http\ApiController;
11 use Exception;
12 use Illuminate\Http\Request;
13
14 class PageApiController extends ApiController
15 {
16     protected $rules = [
17         'create' => [
18             'book_id'    => ['required_without:chapter_id', 'integer'],
19             'chapter_id' => ['required_without:book_id', 'integer'],
20             'name'       => ['required', 'string', 'max:255'],
21             'html'       => ['required_without:markdown', 'string'],
22             'markdown'   => ['required_without:html', 'string'],
23             'tags'       => ['array'],
24         ],
25         'update' => [
26             'book_id'    => ['integer'],
27             'chapter_id' => ['integer'],
28             'name'       => ['string', 'min:1', 'max:255'],
29             'html'       => ['string'],
30             'markdown'   => ['string'],
31             'tags'       => ['array'],
32         ],
33     ];
34
35     public function __construct(
36         protected PageRepo $pageRepo
37     ) {
38     }
39
40     /**
41      * Get a listing of pages visible to the user.
42      */
43     public function list()
44     {
45         $pages = Page::visible();
46
47         return $this->apiListingResponse($pages, [
48             'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
49             'draft', 'template',
50             'created_at', 'updated_at',
51             'created_by', 'updated_by', 'owned_by',
52         ]);
53     }
54
55     /**
56      * Create a new page in the system.
57      *
58      * The ID of a parent book or chapter is required to indicate
59      * where this page should be located.
60      *
61      * Any HTML content provided should be kept to a single-block depth of plain HTML
62      * elements to remain compatible with the BookStack front-end and editors.
63      * Any images included via base64 data URIs will be extracted and saved as gallery
64      * images against the page during upload.
65      */
66     public function create(Request $request)
67     {
68         $this->validate($request, $this->rules['create']);
69
70         if ($request->has('chapter_id')) {
71             $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
72         } else {
73             $parent = Book::visible()->findOrFail($request->get('book_id'));
74         }
75         $this->checkOwnablePermission('page-create', $parent);
76
77         $draft = $this->pageRepo->getNewDraftPage($parent);
78         $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
79
80         return response()->json($draft->forJsonDisplay());
81     }
82
83     /**
84      * View the details of a single page.
85      * Pages will always have HTML content. They may have markdown content
86      * if the markdown editor was used to last update the page.
87      *
88      * The 'html' property is the fully rendered & escaped HTML content that BookStack
89      * would show on page view, with page includes handled.
90      * The 'raw_html' property is the direct database stored HTML content, which would be
91      * what BookStack shows on page edit.
92      *
93      * See the "Content Security" section of these docs for security considerations when using
94      * the page content returned from this endpoint.
95      */
96     public function read(string $id)
97     {
98         $page = $this->pageRepo->getById($id, []);
99
100         return response()->json($page->forJsonDisplay());
101     }
102
103     /**
104      * Update the details of a single page.
105      *
106      * See the 'create' action for details on the provided HTML/Markdown.
107      * Providing a 'book_id' or 'chapter_id' property will essentially move
108      * the page into that parent element if you have permissions to do so.
109      */
110     public function update(Request $request, string $id)
111     {
112         $requestData = $this->validate($request, $this->rules['update']);
113
114         $page = $this->pageRepo->getById($id, []);
115         $this->checkOwnablePermission('page-update', $page);
116
117         $parent = null;
118         if ($request->has('chapter_id')) {
119             $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
120         } elseif ($request->has('book_id')) {
121             $parent = Book::visible()->findOrFail($request->get('book_id'));
122         }
123
124         if ($parent && !$parent->matches($page->getParent())) {
125             $this->checkOwnablePermission('page-delete', $page);
126
127             try {
128                 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
129             } catch (Exception $exception) {
130                 if ($exception instanceof  PermissionsException) {
131                     $this->showPermissionError();
132                 }
133
134                 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
135             }
136         }
137
138         $updatedPage = $this->pageRepo->update($page, $requestData);
139
140         return response()->json($updatedPage->forJsonDisplay());
141     }
142
143     /**
144      * Delete a page.
145      * This will typically send the page to the recycle bin.
146      */
147     public function delete(string $id)
148     {
149         $page = $this->pageRepo->getById($id, []);
150         $this->checkOwnablePermission('page-delete', $page);
151
152         $this->pageRepo->destroy($page);
153
154         return response('', 204);
155     }
156 }