Overview
Comment:Fix HTML entities in search of web pages
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA3-256: 548016fe5f0a1776ca337e64099c2c4739067b888773abaecb440e26199b8ff0
User & Date: bohwaz on 2022-07-12 17:54:54
Other Links: manifest | tags
Context
2022-07-12
20:49
Fix printing issue on trial balance check-in: c0c7c89780 user: bohwaz tags: trunk, stable
17:54
Fix HTML entities in search of web pages check-in: 548016fe5f user: bohwaz tags: trunk, stable
2022-07-05
21:00
Actually, config and skeletons should be public as well check-in: 9c93de8169 user: bohwaz tags: trunk, stable
Changes

Modified src/include/lib/Garradin/Entities/Files/File.php from [91390b68a8] to [0fd27e98eb].

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
		return self::createAndStore(Utils::dirname($target), Utils::basename($target), Files::callStorage('getFullPath', $this), null);
	}

	public function setContent(string $content): self
	{
		$this->set('modified', new \DateTime);
		$this->store(null, rtrim($content));
		$this->indexForSearch(null, $content);
		return $this;
	}

	/**
	 * Store contents in file, either from a local path or from a binary string
	 * If one parameter is supplied, the other must be NULL (you cannot omit one)
	 *







|







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
		return self::createAndStore(Utils::dirname($target), Utils::basename($target), Files::callStorage('getFullPath', $this), null);
	}

	public function setContent(string $content): self
	{
		$this->set('modified', new \DateTime);
		$this->store(null, rtrim($content));
		$this->indexForSearch($content);
		return $this;
	}

	/**
	 * Store contents in file, either from a local path or from a binary string
	 * If one parameter is supplied, the other must be NULL (you cannot omit one)
	 *
332
333
334
335
336
337
338
339
340



341
342
343
344
345
346
347
348
349
350
351
352
353


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378






379
380
381
382
383
384
385

		if (!$return) {
			throw new UserException('Le fichier n\'a pas pu être enregistré.');
		}

		Plugin::fireSignal('files.store', ['file' => $this]);

		if (!$index_search) {
			$this->indexForSearch($source_path, $source_content);



		}

		// clean up thumbnails
		foreach (self::ALLOWED_THUMB_SIZES as $key => $operations)
		{
			Static_Cache::remove(sprintf(self::THUMB_CACHE_ID, $this->pathHash(), $key));
		}

		return $this;
	}

	public function indexForSearch(?string $source_path, ?string $source_content, ?string $title = null): void
	{


		// Store content in search table
		if (substr($this->mime, 0, 5) == 'text/') {
			$content = $source_content !== null ? $source_content : Files::callStorage('fetch', $this);

			if ($this->mime === 'text/html' || $this->mime == 'text/xml') {
				$content = strip_tags($content);
			}
		}
		else {
			$content = null;
		}

		// Only index valid UTF-8
		if (isset($content) && preg_match('//u', $content)) {
			// Truncate content at 150KB
			$content = substr(trim($content), 0, 150*1024);
		}
		else {
			$content = null;
		}

		$db = DB::getInstance();
		$db->preparedQuery('DELETE FROM files_search WHERE path = ?;', $this->path);
		$db->preparedQuery('INSERT INTO files_search (path, title, content) VALUES (?, ?, ?);', $this->path, $title ?? $this->name, $content);
	}







	/**
	 * Create and store a file
	 * If one parameter is supplied, the other must be NULL (you cannot omit one)
	 * @param  string $path           Target path
	 * @param  string $name           Target name
	 * @param  string $source_path    Source file path







|
|
>
>
>











|

>
>

|
|

|
|



















>
>
>
>
>
>







332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

		if (!$return) {
			throw new UserException('Le fichier n\'a pas pu être enregistré.');
		}

		Plugin::fireSignal('files.store', ['file' => $this]);

		if ($index_search) {
			$this->indexForSearch($source_content);
		}
		else {
			$this->removeFromSearch();
		}

		// clean up thumbnails
		foreach (self::ALLOWED_THUMB_SIZES as $key => $operations)
		{
			Static_Cache::remove(sprintf(self::THUMB_CACHE_ID, $this->pathHash(), $key));
		}

		return $this;
	}

	public function indexForSearch(?string $source_content, ?string $title = null, ?string $forced_mime = null): void
	{
		$mime = $forced_mime ?? $this->mime;

		// Store content in search table
		if (substr($mime, 0, 5) == 'text/') {
			$content = $source_content !== null ? $source_content : ($source_path !== null ? Files::callStorage('fetch', $this) : null);

			if ($mime === 'text/html' || $mime == 'text/xml') {
				$content = htmlspecialchars_decode(strip_tags($content));
			}
		}
		else {
			$content = null;
		}

		// Only index valid UTF-8
		if (isset($content) && preg_match('//u', $content)) {
			// Truncate content at 150KB
			$content = substr(trim($content), 0, 150*1024);
		}
		else {
			$content = null;
		}

		$db = DB::getInstance();
		$db->preparedQuery('DELETE FROM files_search WHERE path = ?;', $this->path);
		$db->preparedQuery('INSERT INTO files_search (path, title, content) VALUES (?, ?, ?);', $this->path, $title ?? $this->name, $content);
	}

	public function removeFromSearch(): void
	{
		$db = DB::getInstance();
		$db->preparedQuery('DELETE FROM files_search WHERE path = ?;', $this->path);
	}

	/**
	 * Create and store a file
	 * If one parameter is supplied, the other must be NULL (you cannot omit one)
	 * @param  string $path           Target path
	 * @param  string $name           Target name
	 * @param  string $source_path    Source file path

Modified src/include/lib/Garradin/Entities/Web/Page.php from [cb4f32a782] to [b52cb5ff16].

190
191
192
193
194
195
196
197




198

199
200
201
202
203
204
205
		}

		$this->syncSearch();
	}

	public function syncSearch(): void
	{
		$content = $this->format == Render::FORMAT_ENCRYPTED ? null : strip_tags($this->render());




		$this->file()->indexForSearch(null, $content, $this->title);

	}

	public function save(): bool
	{
		$change_parent = null;

		if (isset($this->_modified['uri']) || isset($this->_modified['path'])) {







|
>
>
>
>
|
>







190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
		}

		$this->syncSearch();
	}

	public function syncSearch(): void
	{
		if ($this->format == Render::FORMAT_ENCRYPTED) {
			$this->file()->removeFromSearch();
		}
		else {
			$content = $this->render();
			$this->file()->indexForSearch($content, $this->title, 'text/html');
		}
	}

	public function save(): bool
	{
		$change_parent = null;

		if (isset($this->_modified['uri']) || isset($this->_modified['path'])) {