1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Validate different things. |
4
|
|
|
* |
5
|
|
|
* @package ThreemaGateway |
6
|
|
|
* @author rugk |
7
|
|
|
* @copyright Copyright (c) 2016 rugk |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class ThreemaGateway_Handler_Validation |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Checks whether a Threema ID is valid and exists. |
15
|
|
|
* |
16
|
|
|
* @param string $threemaid The Threema ID to check. |
17
|
|
|
* @param string $type The type of the Threema ID (personal, gateway, any) |
18
|
|
|
* @param string $error |
19
|
|
|
* @param bool $checkExistence Whether not only formal aspects should |
20
|
|
|
* be checked, but also the existence of the ID. |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
public static function checkThreemaId(&$threemaid, $type, &$error, $checkExistence = true) |
24
|
|
|
{ |
25
|
|
|
$threemaid = strtoupper($threemaid); |
26
|
|
|
|
27
|
|
|
// check whether an id is formally correct |
28
|
|
|
if (!preg_match('/' . ThreemaGateway_Constants::REGEX_THREEMA_ID[$type] . '/', $threemaid)) { |
29
|
|
|
$error = (new XenForo_Phrase('threemagw_invalid_threema_id'))->render(); |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!$checkExistence) { |
34
|
|
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @var ThreemaGateway_Handler_Action_GatewayServer $gwServer */ |
38
|
|
|
$gwServer = new ThreemaGateway_Handler_Action_GatewayServer; |
39
|
|
|
|
40
|
|
|
// fetches public key of an id to check whether it exists |
41
|
|
|
try { |
42
|
|
|
$gwServer->fetchPublicKey($threemaid); |
43
|
|
|
} catch (Exception $e) { |
44
|
|
|
// to show detailed error messages: $error = new XenForo_Phrase('threemagw_threema_id_does_not_exist') . ' ' . $e->getMessage(); |
45
|
|
|
$error = (new XenForo_Phrase('threemagw_threema_id_does_not_exist'))->render(); |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Checks whether the directory is read- and writable. |
54
|
|
|
* It also automatically creates it if necessary. |
55
|
|
|
* |
56
|
|
|
* @param string $dir directory to check |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public static function checkDir($dir) |
60
|
|
|
{ |
61
|
|
|
if (!file_exists($dir)) { |
62
|
|
|
try { |
63
|
|
|
mkdir($dir, 0770, true); |
64
|
|
|
} catch (Exception $e) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
return (is_readable($dir) && is_writable($dir)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|