diff options
author | VirtualTam <virtualtam@flibidi.org> | 2015-03-12 00:43:02 +0100 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-06-11 00:45:45 +0200 |
commit | ca74886f30da323f42aa4bd70461003f46ef299b (patch) | |
tree | 3714895c25d80bdb472211a564f83d3744d90f34 /application/Utils.php | |
parent | cbecab773526b0c39f3cffa1d4595b5caa781bda (diff) | |
download | Shaarli-ca74886f30da323f42aa4bd70461003f46ef299b.tar.gz Shaarli-ca74886f30da323f42aa4bd70461003f46ef299b.tar.zst Shaarli-ca74886f30da323f42aa4bd70461003f46ef299b.zip |
LinkDB: move to a proper file, add test coverage
Relates to #71
LinkDB
- move to application/LinkDB.php
- code cleanup
- indentation
- whitespaces
- formatting
- comment cleanup
- add missing documentation
- unify formatting
Test coverage for LinkDB
- constructor
- public / private access
- link-related methods
Shaarli utilities (LinkDB dependencies)
- move startsWith() and endsWith() functions to application/Utils.php
- add test coverage
Dev utilities
- Composer: add PHPUnit to dev dependencies
- Makefile:
- update lint targets
- add test targets
- generate coverage reports
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'application/Utils.php')
-rw-r--r-- | application/Utils.php | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/application/Utils.php b/application/Utils.php new file mode 100644 index 00000000..737f1502 --- /dev/null +++ b/application/Utils.php | |||
@@ -0,0 +1,45 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Shaarli utilities | ||
4 | */ | ||
5 | |||
6 | /** | ||
7 | * Returns the small hash of a string, using RFC 4648 base64url format | ||
8 | * | ||
9 | * Small hashes: | ||
10 | * - are unique (well, as unique as crc32, at last) | ||
11 | * - are always 6 characters long. | ||
12 | * - only use the following characters: a-z A-Z 0-9 - _ @ | ||
13 | * - are NOT cryptographically secure (they CAN be forged) | ||
14 | * | ||
15 | * In Shaarli, they are used as a tinyurl-like link to individual entries, | ||
16 | * e.g. smallHash('20111006_131924') --> yZH23w | ||
17 | */ | ||
18 | function smallHash($text) | ||
19 | { | ||
20 | $t = rtrim(base64_encode(hash('crc32', $text, true)), '='); | ||
21 | return strtr($t, '+/', '-_'); | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | * Tells if a string start with a substring | ||
26 | */ | ||
27 | function startsWith($haystack, $needle, $case=true) | ||
28 | { | ||
29 | if ($case) { | ||
30 | return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0); | ||
31 | } | ||
32 | return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0); | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Tells if a string ends with a substring | ||
37 | */ | ||
38 | function endsWith($haystack, $needle, $case=true) | ||
39 | { | ||
40 | if ($case) { | ||
41 | return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0); | ||
42 | } | ||
43 | return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0); | ||
44 | } | ||
45 | ?> | ||