master
ブランチ master
LittleBeast / src / Std / Lib / Db / Convert / Stores.php
6.4 KB、102行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Lib\Db\Convert;
39
40/**
41 * LittleBeastのJSONのデータ(data/)とテーブルの対応
42 *
43 * lb_users data/user/{ID}.{ユーザー名}.json(dataにファイルの中身)
44 * lb_comment_threads data/comments/{スレッド}.json の「comments」以外(title等)
45 * lb_comments 同じファイルの「comments」(seqは順番)
46 * lb_comment_ap data/comments/ap.json(Fediverseの返信のID => スレッドとコメント)
47 * lb_notifications data/notifications/{ユーザー名}.json(seqは順番)
48 * lb_cron data/cron/cron.json(seqは順番)
49 * lb_followers data/followers.txt(ブログのアクターのフォロワー)
50 * lb_following data/following.txt
51 * lb_ap_notes data/ap/notes/{ID}.json(dataにファイルの中身)
52 * lb_files その他のファイル(パスと中身、キャッシュ・ロック・一時ファイルを除く)
53 * 他のテーブルはdata/db/{テーブル名}.jsonになる(定義と行)。
54 */
55final class Stores {
56 public const JSON_FLAGS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT;
57 /** 変換しないディレクトリ・ファイル(data/からの相対パス) */
58 public const SKIP = ['#^cache/#', '#^ratelimit/#', '#^tmp/#', '#^db/#', '#\.tmp$#', '#\.lock$#', '#^cron/run\.lock$#', '#(^|/)\.#'];
59
60 /** テーブルの定義 */
61 public static function tables(): array {
62 $text = fn(string $n, bool $notNull = false, ?int $size = null) => ['name' => $n, 'type' => $size ? 'varchar' : 'text', 'size' => $size, 'notNull' => $notNull];
63 $int = fn(string $n, bool $notNull = true) => ['name' => $n, 'type' => 'int', 'size' => 64, 'notNull' => $notNull];
64 $json = fn(string $n = 'data') => ['name' => $n, 'type' => 'json', 'notNull' => true];
65 return [
66 'lb_users' => ['name' => 'lb_users', 'columns' => [$int('id'), $text('username', true, 255), $text('email'), $int('role', false), $json()],
67 'pk' => ['id'], 'indexes' => [['name' => 'lb_users_username', 'columns' => ['username'], 'unique' => true]]],
68 'lb_comment_threads' => ['name' => 'lb_comment_threads', 'columns' => [$text('thread', true, 255), $json()], 'pk' => ['thread'], 'indexes' => []],
69 'lb_comments' => ['name' => 'lb_comments', 'columns' => [$text('thread', true, 255), $text('id', true, 64), $int('seq'), $json()],
70 'pk' => ['thread', 'id'], 'indexes' => []],
71 'lb_comment_ap' => ['name' => 'lb_comment_ap', 'columns' => [$text('ap_id', true, 700), $json()], 'pk' => ['ap_id'], 'indexes' => []],
72 'lb_notifications' => ['name' => 'lb_notifications', 'columns' => [$text('username', true, 255), $int('seq'), $json()], 'pk' => ['username', 'seq'], 'indexes' => []],
73 'lb_cron' => ['name' => 'lb_cron', 'columns' => [$int('seq'), $json()], 'pk' => ['seq'], 'indexes' => []],
74 'lb_followers' => ['name' => 'lb_followers', 'columns' => [$int('seq'), $text('actor', true)], 'pk' => ['seq'], 'indexes' => []],
75 'lb_following' => ['name' => 'lb_following', 'columns' => [$int('seq'), $text('actor', true)], 'pk' => ['seq'], 'indexes' => []],
76 'lb_ap_notes' => ['name' => 'lb_ap_notes', 'columns' => [$text('id', true, 255), $json()], 'pk' => ['id'], 'indexes' => []],
77 'lb_files' => ['name' => 'lb_files', 'columns' => [$text('path', true, 700), ['name' => 'content', 'type' => 'blob', 'notNull' => true]], 'pk' => ['path'], 'indexes' => []],
78 ];
79 }
80
81 /** データのディレクトリからの相対パスが安全か(「..」・絶対パス・制御文字が無い) */
82 public static function safePath(string $rel): bool {
83 if ($rel === '' || $rel[0] === '/' || str_contains($rel, '\\') || preg_match('/[\x00-\x1F]/', $rel) || preg_match('/^[A-Za-z]:/', $rel)) return false;
84 foreach (explode('/', $rel) as $part) if ($part === '' || $part === '.' || $part === '..') return false;
85 return true;
86 }
87
88 /** 名前(ユーザー名等)が安全か */
89 public static function safeName(string $n): bool {
90 return (bool)preg_match('/^[A-Za-z0-9_][A-Za-z0-9_.-]*$/', $n) && !str_contains($n, '..');
91 }
92
93 /** スレッドの名前(Comments::validThreadと同じ) */
94 public static function safeThread(string $t): bool {
95 return (bool)preg_match('#^[A-Za-z0-9_-]+(?:/[A-Za-z0-9_-][A-Za-z0-9._-]*){1,2}$#', $t);
96 }
97
98 /** 普通のテーブルのファイル名 */
99 public static function tableFile(string $name): string {
100 return 'db/'.rawurlencode($name).'.json';
101 }
102}