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