1 <?php namespace BookStack\Repos;
7 use BookStack\Exceptions\NotFoundException;
8 use BookStack\Services\RestrictionService;
9 use Illuminate\Http\Request;
10 use Illuminate\Support\Facades\Auth;
11 use Illuminate\Support\Facades\Log;
12 use Illuminate\Support\Str;
14 use BookStack\PageRevision;
15 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20 protected $pageRevision;
21 protected $restrictionService;
24 * PageRepo constructor.
26 * @param PageRevision $pageRevision
27 * @param RestrictionService $restrictionService
29 public function __construct(Page $page, PageRevision $pageRevision, RestrictionService $restrictionService)
32 $this->pageRevision = $pageRevision;
33 $this->restrictionService = $restrictionService;
37 * Base query for getting pages, Takes restrictions into account.
40 private function pageQuery()
42 return $this->restrictionService->enforcePageRestrictions($this->page, 'view');
46 * Get a page via a specific ID.
50 public function getById($id)
52 return $this->pageQuery()->findOrFail($id);
56 * Get a page identified by the given slug.
60 * @throws NotFoundException
62 public function getBySlug($slug, $bookId)
64 $page = $this->pageQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
65 if ($page === null) throw new NotFoundException('Page not found');
70 * Search through page revisions and retrieve
71 * the last page in the current book that
72 * has a slug equal to the one given.
77 public function findPageUsingOldSlug($pageSlug, $bookSlug)
79 $revision = $this->pageRevision->where('slug', '=', $pageSlug)
80 ->whereHas('page', function($query) {
81 $this->restrictionService->enforcePageRestrictions($query);
83 ->where('book_slug', '=', $bookSlug)->orderBy('created_at', 'desc')
84 ->with('page')->first();
85 return $revision !== null ? $revision->page : null;
89 * Get a new Page instance from the given input.
93 public function newFromInput($input)
95 $page = $this->page->fill($input);
100 * Count the pages with a particular slug within a book.
105 public function countBySlug($slug, $bookId)
107 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
111 * Save a new page into the system.
112 * Input validation must be done beforehand.
113 * @param array $input
115 * @param int $chapterId
118 public function saveNew(array $input, Book $book, $chapterId = null)
120 $page = $this->newFromInput($input);
121 $page->slug = $this->findSuitableSlug($page->name, $book->id);
123 if ($chapterId) $page->chapter_id = $chapterId;
125 $page->html = $this->formatHtml($input['html']);
126 $page->text = strip_tags($page->html);
127 $page->created_by = auth()->user()->id;
128 $page->updated_by = auth()->user()->id;
130 $book->pages()->save($page);
135 * Formats a page's html to be tagged correctly
137 * @param string $htmlText
140 protected function formatHtml($htmlText)
142 if($htmlText == '') return $htmlText;
143 libxml_use_internal_errors(true);
144 $doc = new \DOMDocument();
145 $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8'));
147 $container = $doc->documentElement;
148 $body = $container->childNodes->item(0);
149 $childNodes = $body->childNodes;
151 // Ensure no duplicate ids are used
154 foreach ($childNodes as $index => $childNode) {
155 /** @var \DOMElement $childNode */
156 if (get_class($childNode) !== 'DOMElement') continue;
158 // Overwrite id if not a BookStack custom id
159 if ($childNode->hasAttribute('id')) {
160 $id = $childNode->getAttribute('id');
161 if (strpos($id, 'bkmrk') === 0 && array_search($id, $idArray) === false) {
167 // Create an unique id for the element
168 // Uses the content as a basis to ensure output is the same every time
169 // the same content is passed through.
170 $contentId = 'bkmrk-' . substr(strtolower(preg_replace('/\s+/', '-', trim($childNode->nodeValue))), 0, 20);
171 $newId = urlencode($contentId);
173 while (in_array($newId, $idArray)) {
174 $newId = urlencode($contentId . '-' . $loopIndex);
178 $childNode->setAttribute('id', $newId);
182 // Generate inner html as a string
184 foreach ($childNodes as $childNode) {
185 $html .= $doc->saveHTML($childNode);
193 * Gets pages by a search term.
194 * Highlights page content for showing in results.
195 * @param string $term
196 * @param array $whereTerms
198 * @param array $paginationAppends
201 public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
203 preg_match_all('/"(.*?)"/', $term, $matches);
204 if (count($matches[1]) > 0) {
205 $terms = $matches[1];
206 $term = trim(preg_replace('/"(.*?)"/', '', $term));
211 $terms = array_merge($terms, explode(' ', $term));
213 $pages = $this->restrictionService->enforcePageRestrictions($this->page->fullTextSearchQuery(['name', 'text'], $terms, $whereTerms))
214 ->paginate($count)->appends($paginationAppends);
216 // Add highlights to page text.
217 $words = join('|', explode(' ', preg_quote(trim($term), '/')));
218 //lookahead/behind assertions ensures cut between words
219 $s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
221 foreach ($pages as $page) {
222 preg_match_all('#(?<=[' . $s . ']).{1,30}((' . $words . ').{1,30})+(?=[' . $s . '])#uis', $page->text, $matches, PREG_SET_ORDER);
223 //delimiter between occurrences
225 foreach ($matches as $line) {
226 $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
229 if (count($results) > $matchLimit) {
230 $results = array_slice($results, 0, $matchLimit);
232 $result = join('... ', $results);
235 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
236 if (strlen($result) < 5) {
237 $result = $page->getExcerpt(80);
239 $page->searchSnippet = $result;
245 * Search for image usage.
246 * @param $imageString
249 public function searchForImage($imageString)
251 $pages = $this->pageQuery()->where('html', 'like', '%' . $imageString . '%')->get();
252 foreach ($pages as $page) {
253 $page->url = $page->getUrl();
257 return count($pages) > 0 ? $pages : false;
261 * Updates a page with any fillable data and saves it into the database.
263 * @param int $book_id
264 * @param string $input
267 public function updatePage(Page $page, $book_id, $input)
269 // Save a revision before updating
270 if ($page->html !== $input['html'] || $page->name !== $input['name']) {
271 $this->saveRevision($page);
274 // Prevent slug being updated if no name change
275 if ($page->name !== $input['name']) {
276 $page->slug = $this->findSuitableSlug($input['name'], $book_id, $page->id);
279 // Update with new details
281 $page->html = $this->formatHtml($input['html']);
282 $page->text = strip_tags($page->html);
283 $page->updated_by = auth()->user()->id;
289 * Restores a revision's content back into a page.
292 * @param int $revisionId
295 public function restoreRevision(Page $page, Book $book, $revisionId)
297 $this->saveRevision($page);
298 $revision = $this->getRevisionById($revisionId);
299 $page->fill($revision->toArray());
300 $page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id);
301 $page->text = strip_tags($page->html);
302 $page->updated_by = auth()->user()->id;
308 * Saves a page revision into the system.
312 public function saveRevision(Page $page)
314 $revision = $this->pageRevision->fill($page->toArray());
315 $revision->page_id = $page->id;
316 $revision->slug = $page->slug;
317 $revision->book_slug = $page->book->slug;
318 $revision->created_by = auth()->user()->id;
319 $revision->created_at = $page->updated_at;
321 // Clear old revisions
322 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
323 $this->pageRevision->where('page_id', '=', $page->id)
324 ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
330 * Gets a single revision via it's id.
334 public function getRevisionById($id)
336 return $this->pageRevision->findOrFail($id);
340 * Checks if a slug exists within a book already.
343 * @param bool|false $currentId
346 public function doesSlugExist($slug, $bookId, $currentId = false)
348 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
349 if ($currentId) $query = $query->where('id', '!=', $currentId);
350 return $query->count() > 0;
354 * Changes the related book for the specified page.
355 * Changes the book id of any relations to the page that store the book id.
360 public function changeBook($bookId, Page $page)
362 $page->book_id = $bookId;
363 foreach ($page->activity as $activity) {
364 $activity->book_id = $bookId;
367 $page->slug = $this->findSuitableSlug($page->name, $bookId, $page->id);
373 * Gets a suitable slug for the resource
376 * @param bool|false $currentId
379 public function findSuitableSlug($name, $bookId, $currentId = false)
381 $slug = Str::slug($name);
382 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
383 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
389 * Destroy a given page along with its dependencies.
392 public function destroy($page)
394 Activity::removeEntity($page);
395 $page->views()->delete();
396 $page->revisions()->delete();
397 $page->restrictions()->delete();
402 * Get the latest pages added to the system.
405 public function getRecentlyCreatedPaginated($count = 20)
407 return $this->pageQuery()->orderBy('created_at', 'desc')->paginate($count);
411 * Get the latest pages added to the system.
414 public function getRecentlyUpdatedPaginated($count = 20)
416 return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count);
420 * Updates pages restrictions from a request
424 public function updateRestrictionsFromRequest($request, $page)
426 // TODO - extract into shared repo
427 $page->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
428 $page->restrictions()->delete();
429 if ($request->has('restrictions')) {
430 foreach($request->get('restrictions') as $roleId => $restrictions) {
431 foreach ($restrictions as $action => $value) {
432 $page->restrictions()->create([
433 'role_id' => $roleId,
434 'action' => strtolower($action)