@@ -31,136 +31,136 @@ |
||
| 31 | 31 | * capabilities. |
| 32 | 32 | */ |
| 33 | 33 | abstract class Backend implements UserInterface { |
| 34 | - /** |
|
| 35 | - * error code for functions not provided by the user backend |
|
| 36 | - */ |
|
| 37 | - const NOT_IMPLEMENTED = -501; |
|
| 34 | + /** |
|
| 35 | + * error code for functions not provided by the user backend |
|
| 36 | + */ |
|
| 37 | + const NOT_IMPLEMENTED = -501; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * actions that user backends can define |
|
| 41 | - */ |
|
| 42 | - const CREATE_USER = 1; // 1 << 0 |
|
| 43 | - const SET_PASSWORD = 16; // 1 << 4 |
|
| 44 | - const CHECK_PASSWORD = 256; // 1 << 8 |
|
| 45 | - const GET_HOME = 4096; // 1 << 12 |
|
| 46 | - const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
| 47 | - const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
| 48 | - const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
| 49 | - const COUNT_USERS = 268435456; // 1 << 28 |
|
| 39 | + /** |
|
| 40 | + * actions that user backends can define |
|
| 41 | + */ |
|
| 42 | + const CREATE_USER = 1; // 1 << 0 |
|
| 43 | + const SET_PASSWORD = 16; // 1 << 4 |
|
| 44 | + const CHECK_PASSWORD = 256; // 1 << 8 |
|
| 45 | + const GET_HOME = 4096; // 1 << 12 |
|
| 46 | + const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
| 47 | + const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
| 48 | + const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
| 49 | + const COUNT_USERS = 268435456; // 1 << 28 |
|
| 50 | 50 | |
| 51 | - protected $possibleActions = [ |
|
| 52 | - self::CREATE_USER => 'createUser', |
|
| 53 | - self::SET_PASSWORD => 'setPassword', |
|
| 54 | - self::CHECK_PASSWORD => 'checkPassword', |
|
| 55 | - self::GET_HOME => 'getHome', |
|
| 56 | - self::GET_DISPLAYNAME => 'getDisplayName', |
|
| 57 | - self::SET_DISPLAYNAME => 'setDisplayName', |
|
| 58 | - self::PROVIDE_AVATAR => 'canChangeAvatar', |
|
| 59 | - self::COUNT_USERS => 'countUsers', |
|
| 60 | - ]; |
|
| 51 | + protected $possibleActions = [ |
|
| 52 | + self::CREATE_USER => 'createUser', |
|
| 53 | + self::SET_PASSWORD => 'setPassword', |
|
| 54 | + self::CHECK_PASSWORD => 'checkPassword', |
|
| 55 | + self::GET_HOME => 'getHome', |
|
| 56 | + self::GET_DISPLAYNAME => 'getDisplayName', |
|
| 57 | + self::SET_DISPLAYNAME => 'setDisplayName', |
|
| 58 | + self::PROVIDE_AVATAR => 'canChangeAvatar', |
|
| 59 | + self::COUNT_USERS => 'countUsers', |
|
| 60 | + ]; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get all supported actions |
|
| 64 | - * @return int bitwise-or'ed actions |
|
| 65 | - * |
|
| 66 | - * Returns the supported actions as int to be |
|
| 67 | - * compared with self::CREATE_USER etc. |
|
| 68 | - */ |
|
| 69 | - public function getSupportedActions() { |
|
| 70 | - $actions = 0; |
|
| 71 | - foreach($this->possibleActions AS $action => $methodName) { |
|
| 72 | - if(method_exists($this, $methodName)) { |
|
| 73 | - $actions |= $action; |
|
| 74 | - } |
|
| 75 | - } |
|
| 62 | + /** |
|
| 63 | + * Get all supported actions |
|
| 64 | + * @return int bitwise-or'ed actions |
|
| 65 | + * |
|
| 66 | + * Returns the supported actions as int to be |
|
| 67 | + * compared with self::CREATE_USER etc. |
|
| 68 | + */ |
|
| 69 | + public function getSupportedActions() { |
|
| 70 | + $actions = 0; |
|
| 71 | + foreach($this->possibleActions AS $action => $methodName) { |
|
| 72 | + if(method_exists($this, $methodName)) { |
|
| 73 | + $actions |= $action; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - return $actions; |
|
| 78 | - } |
|
| 77 | + return $actions; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Check if backend implements actions |
|
| 82 | - * @param int $actions bitwise-or'ed actions |
|
| 83 | - * @return boolean |
|
| 84 | - * |
|
| 85 | - * Returns the supported actions as int to be |
|
| 86 | - * compared with self::CREATE_USER etc. |
|
| 87 | - */ |
|
| 88 | - public function implementsActions($actions) { |
|
| 89 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 90 | - } |
|
| 80 | + /** |
|
| 81 | + * Check if backend implements actions |
|
| 82 | + * @param int $actions bitwise-or'ed actions |
|
| 83 | + * @return boolean |
|
| 84 | + * |
|
| 85 | + * Returns the supported actions as int to be |
|
| 86 | + * compared with self::CREATE_USER etc. |
|
| 87 | + */ |
|
| 88 | + public function implementsActions($actions) { |
|
| 89 | + return (bool)($this->getSupportedActions() & $actions); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * delete a user |
|
| 94 | - * @param string $uid The username of the user to delete |
|
| 95 | - * @return bool |
|
| 96 | - * |
|
| 97 | - * Deletes a user |
|
| 98 | - */ |
|
| 99 | - public function deleteUser( $uid ) { |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 92 | + /** |
|
| 93 | + * delete a user |
|
| 94 | + * @param string $uid The username of the user to delete |
|
| 95 | + * @return bool |
|
| 96 | + * |
|
| 97 | + * Deletes a user |
|
| 98 | + */ |
|
| 99 | + public function deleteUser( $uid ) { |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Get a list of all users |
|
| 105 | - * |
|
| 106 | - * @param string $search |
|
| 107 | - * @param null|int $limit |
|
| 108 | - * @param null|int $offset |
|
| 109 | - * @return string[] an array of all uids |
|
| 110 | - */ |
|
| 111 | - public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 112 | - return []; |
|
| 113 | - } |
|
| 103 | + /** |
|
| 104 | + * Get a list of all users |
|
| 105 | + * |
|
| 106 | + * @param string $search |
|
| 107 | + * @param null|int $limit |
|
| 108 | + * @param null|int $offset |
|
| 109 | + * @return string[] an array of all uids |
|
| 110 | + */ |
|
| 111 | + public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 112 | + return []; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * check if a user exists |
|
| 117 | - * @param string $uid the username |
|
| 118 | - * @return boolean |
|
| 119 | - */ |
|
| 120 | - public function userExists($uid) { |
|
| 121 | - return false; |
|
| 122 | - } |
|
| 115 | + /** |
|
| 116 | + * check if a user exists |
|
| 117 | + * @param string $uid the username |
|
| 118 | + * @return boolean |
|
| 119 | + */ |
|
| 120 | + public function userExists($uid) { |
|
| 121 | + return false; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * get the user's home directory |
|
| 126 | - * @param string $uid the username |
|
| 127 | - * @return boolean |
|
| 128 | - */ |
|
| 129 | - public function getHome($uid) { |
|
| 130 | - return false; |
|
| 131 | - } |
|
| 124 | + /** |
|
| 125 | + * get the user's home directory |
|
| 126 | + * @param string $uid the username |
|
| 127 | + * @return boolean |
|
| 128 | + */ |
|
| 129 | + public function getHome($uid) { |
|
| 130 | + return false; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * get display name of the user |
|
| 135 | - * @param string $uid user ID of the user |
|
| 136 | - * @return string display name |
|
| 137 | - */ |
|
| 138 | - public function getDisplayName($uid) { |
|
| 139 | - return $uid; |
|
| 140 | - } |
|
| 133 | + /** |
|
| 134 | + * get display name of the user |
|
| 135 | + * @param string $uid user ID of the user |
|
| 136 | + * @return string display name |
|
| 137 | + */ |
|
| 138 | + public function getDisplayName($uid) { |
|
| 139 | + return $uid; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Get a list of all display names and user ids. |
|
| 144 | - * |
|
| 145 | - * @param string $search |
|
| 146 | - * @param string|null $limit |
|
| 147 | - * @param string|null $offset |
|
| 148 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 149 | - */ |
|
| 150 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 151 | - $displayNames = []; |
|
| 152 | - $users = $this->getUsers($search, $limit, $offset); |
|
| 153 | - foreach ( $users as $user) { |
|
| 154 | - $displayNames[$user] = $user; |
|
| 155 | - } |
|
| 156 | - return $displayNames; |
|
| 157 | - } |
|
| 142 | + /** |
|
| 143 | + * Get a list of all display names and user ids. |
|
| 144 | + * |
|
| 145 | + * @param string $search |
|
| 146 | + * @param string|null $limit |
|
| 147 | + * @param string|null $offset |
|
| 148 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 149 | + */ |
|
| 150 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 151 | + $displayNames = []; |
|
| 152 | + $users = $this->getUsers($search, $limit, $offset); |
|
| 153 | + foreach ( $users as $user) { |
|
| 154 | + $displayNames[$user] = $user; |
|
| 155 | + } |
|
| 156 | + return $displayNames; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Check if a user list is available or not |
|
| 161 | - * @return boolean if users can be listed or not |
|
| 162 | - */ |
|
| 163 | - public function hasUserListings() { |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 159 | + /** |
|
| 160 | + * Check if a user list is available or not |
|
| 161 | + * @return boolean if users can be listed or not |
|
| 162 | + */ |
|
| 163 | + public function hasUserListings() { |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | 166 | } |
@@ -39,14 +39,14 @@ discard block |
||
| 39 | 39 | /** |
| 40 | 40 | * actions that user backends can define |
| 41 | 41 | */ |
| 42 | - const CREATE_USER = 1; // 1 << 0 |
|
| 43 | - const SET_PASSWORD = 16; // 1 << 4 |
|
| 44 | - const CHECK_PASSWORD = 256; // 1 << 8 |
|
| 45 | - const GET_HOME = 4096; // 1 << 12 |
|
| 46 | - const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
| 47 | - const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
| 48 | - const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
| 49 | - const COUNT_USERS = 268435456; // 1 << 28 |
|
| 42 | + const CREATE_USER = 1; // 1 << 0 |
|
| 43 | + const SET_PASSWORD = 16; // 1 << 4 |
|
| 44 | + const CHECK_PASSWORD = 256; // 1 << 8 |
|
| 45 | + const GET_HOME = 4096; // 1 << 12 |
|
| 46 | + const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
| 47 | + const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
| 48 | + const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
| 49 | + const COUNT_USERS = 268435456; // 1 << 28 |
|
| 50 | 50 | |
| 51 | 51 | protected $possibleActions = [ |
| 52 | 52 | self::CREATE_USER => 'createUser', |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function getSupportedActions() { |
| 70 | 70 | $actions = 0; |
| 71 | - foreach($this->possibleActions AS $action => $methodName) { |
|
| 72 | - if(method_exists($this, $methodName)) { |
|
| 71 | + foreach ($this->possibleActions AS $action => $methodName) { |
|
| 72 | + if (method_exists($this, $methodName)) { |
|
| 73 | 73 | $actions |= $action; |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | * compared with self::CREATE_USER etc. |
| 87 | 87 | */ |
| 88 | 88 | public function implementsActions($actions) { |
| 89 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 89 | + return (bool) ($this->getSupportedActions() & $actions); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * |
| 97 | 97 | * Deletes a user |
| 98 | 98 | */ |
| 99 | - public function deleteUser( $uid ) { |
|
| 99 | + public function deleteUser($uid) { |
|
| 100 | 100 | return false; |
| 101 | 101 | } |
| 102 | 102 | |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
| 151 | 151 | $displayNames = []; |
| 152 | 152 | $users = $this->getUsers($search, $limit, $offset); |
| 153 | - foreach ( $users as $user) { |
|
| 153 | + foreach ($users as $user) { |
|
| 154 | 154 | $displayNames[$user] = $user; |
| 155 | 155 | } |
| 156 | 156 | return $displayNames; |
@@ -41,53 +41,53 @@ |
||
| 41 | 41 | |
| 42 | 42 | class TagManager implements \OCP\ITagManager { |
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * User session |
|
| 46 | - * |
|
| 47 | - * @var \OCP\IUserSession |
|
| 48 | - */ |
|
| 49 | - private $userSession; |
|
| 44 | + /** |
|
| 45 | + * User session |
|
| 46 | + * |
|
| 47 | + * @var \OCP\IUserSession |
|
| 48 | + */ |
|
| 49 | + private $userSession; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * TagMapper |
|
| 53 | - * |
|
| 54 | - * @var TagMapper |
|
| 55 | - */ |
|
| 56 | - private $mapper; |
|
| 51 | + /** |
|
| 52 | + * TagMapper |
|
| 53 | + * |
|
| 54 | + * @var TagMapper |
|
| 55 | + */ |
|
| 56 | + private $mapper; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Constructor. |
|
| 60 | - * |
|
| 61 | - * @param TagMapper $mapper Instance of the TagMapper abstraction layer. |
|
| 62 | - * @param \OCP\IUserSession $userSession the user session |
|
| 63 | - */ |
|
| 64 | - public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) { |
|
| 65 | - $this->mapper = $mapper; |
|
| 66 | - $this->userSession = $userSession; |
|
| 58 | + /** |
|
| 59 | + * Constructor. |
|
| 60 | + * |
|
| 61 | + * @param TagMapper $mapper Instance of the TagMapper abstraction layer. |
|
| 62 | + * @param \OCP\IUserSession $userSession the user session |
|
| 63 | + */ |
|
| 64 | + public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) { |
|
| 65 | + $this->mapper = $mapper; |
|
| 66 | + $this->userSession = $userSession; |
|
| 67 | 67 | |
| 68 | - } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Create a new \OCP\ITags instance and load tags from db. |
|
| 72 | - * |
|
| 73 | - * @see \OCP\ITags |
|
| 74 | - * @param string $type The type identifier e.g. 'contact' or 'event'. |
|
| 75 | - * @param array $defaultTags An array of default tags to be used if none are stored. |
|
| 76 | - * @param boolean $includeShared Whether to include tags for items shared with this user by others. |
|
| 77 | - * @param string $userId user for which to retrieve the tags, defaults to the currently |
|
| 78 | - * logged in user |
|
| 79 | - * @return \OCP\ITags |
|
| 80 | - */ |
|
| 81 | - public function load($type, $defaultTags = [], $includeShared = false, $userId = null) { |
|
| 82 | - if (is_null($userId)) { |
|
| 83 | - $user = $this->userSession->getUser(); |
|
| 84 | - if ($user === null) { |
|
| 85 | - // nothing we can do without a user |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 88 | - $userId = $this->userSession->getUser()->getUId(); |
|
| 89 | - } |
|
| 90 | - return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared); |
|
| 91 | - } |
|
| 70 | + /** |
|
| 71 | + * Create a new \OCP\ITags instance and load tags from db. |
|
| 72 | + * |
|
| 73 | + * @see \OCP\ITags |
|
| 74 | + * @param string $type The type identifier e.g. 'contact' or 'event'. |
|
| 75 | + * @param array $defaultTags An array of default tags to be used if none are stored. |
|
| 76 | + * @param boolean $includeShared Whether to include tags for items shared with this user by others. |
|
| 77 | + * @param string $userId user for which to retrieve the tags, defaults to the currently |
|
| 78 | + * logged in user |
|
| 79 | + * @return \OCP\ITags |
|
| 80 | + */ |
|
| 81 | + public function load($type, $defaultTags = [], $includeShared = false, $userId = null) { |
|
| 82 | + if (is_null($userId)) { |
|
| 83 | + $user = $this->userSession->getUser(); |
|
| 84 | + if ($user === null) { |
|
| 85 | + // nothing we can do without a user |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | + $userId = $this->userSession->getUser()->getUId(); |
|
| 89 | + } |
|
| 90 | + return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | } |
@@ -33,180 +33,180 @@ |
||
| 33 | 33 | * Helper class for large files on 32-bit platforms. |
| 34 | 34 | */ |
| 35 | 35 | class LargeFileHelper { |
| 36 | - /** |
|
| 37 | - * pow(2, 53) as a base-10 string. |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - const POW_2_53 = '9007199254740992'; |
|
| 36 | + /** |
|
| 37 | + * pow(2, 53) as a base-10 string. |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + const POW_2_53 = '9007199254740992'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * pow(2, 53) - 1 as a base-10 string. |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - const POW_2_53_MINUS_1 = '9007199254740991'; |
|
| 42 | + /** |
|
| 43 | + * pow(2, 53) - 1 as a base-10 string. |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + const POW_2_53_MINUS_1 = '9007199254740991'; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @brief Checks whether our assumptions hold on the PHP platform we are on. |
|
| 50 | - * |
|
| 51 | - * @throws \RunTimeException if our assumptions do not hold on the current |
|
| 52 | - * PHP platform. |
|
| 53 | - */ |
|
| 54 | - public function __construct() { |
|
| 55 | - $pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0; |
|
| 56 | - if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) { |
|
| 57 | - throw new \RuntimeException( |
|
| 58 | - 'This class assumes floats to be double precision or "better".' |
|
| 59 | - ); |
|
| 60 | - } |
|
| 61 | - } |
|
| 48 | + /** |
|
| 49 | + * @brief Checks whether our assumptions hold on the PHP platform we are on. |
|
| 50 | + * |
|
| 51 | + * @throws \RunTimeException if our assumptions do not hold on the current |
|
| 52 | + * PHP platform. |
|
| 53 | + */ |
|
| 54 | + public function __construct() { |
|
| 55 | + $pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0; |
|
| 56 | + if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) { |
|
| 57 | + throw new \RuntimeException( |
|
| 58 | + 'This class assumes floats to be double precision or "better".' |
|
| 59 | + ); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @brief Formats a signed integer or float as an unsigned integer base-10 |
|
| 65 | - * string. Passed strings will be checked for being base-10. |
|
| 66 | - * |
|
| 67 | - * @param int|float|string $number Number containing unsigned integer data |
|
| 68 | - * |
|
| 69 | - * @throws \UnexpectedValueException if $number is not a float, not an int |
|
| 70 | - * and not a base-10 string. |
|
| 71 | - * |
|
| 72 | - * @return string Unsigned integer base-10 string |
|
| 73 | - */ |
|
| 74 | - public function formatUnsignedInteger($number) { |
|
| 75 | - if (is_float($number)) { |
|
| 76 | - // Undo the effect of the php.ini setting 'precision'. |
|
| 77 | - return number_format($number, 0, '', ''); |
|
| 78 | - } else if (is_string($number) && ctype_digit($number)) { |
|
| 79 | - return $number; |
|
| 80 | - } else if (is_int($number)) { |
|
| 81 | - // Interpret signed integer as unsigned integer. |
|
| 82 | - return sprintf('%u', $number); |
|
| 83 | - } else { |
|
| 84 | - throw new \UnexpectedValueException( |
|
| 85 | - 'Expected int, float or base-10 string' |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - } |
|
| 63 | + /** |
|
| 64 | + * @brief Formats a signed integer or float as an unsigned integer base-10 |
|
| 65 | + * string. Passed strings will be checked for being base-10. |
|
| 66 | + * |
|
| 67 | + * @param int|float|string $number Number containing unsigned integer data |
|
| 68 | + * |
|
| 69 | + * @throws \UnexpectedValueException if $number is not a float, not an int |
|
| 70 | + * and not a base-10 string. |
|
| 71 | + * |
|
| 72 | + * @return string Unsigned integer base-10 string |
|
| 73 | + */ |
|
| 74 | + public function formatUnsignedInteger($number) { |
|
| 75 | + if (is_float($number)) { |
|
| 76 | + // Undo the effect of the php.ini setting 'precision'. |
|
| 77 | + return number_format($number, 0, '', ''); |
|
| 78 | + } else if (is_string($number) && ctype_digit($number)) { |
|
| 79 | + return $number; |
|
| 80 | + } else if (is_int($number)) { |
|
| 81 | + // Interpret signed integer as unsigned integer. |
|
| 82 | + return sprintf('%u', $number); |
|
| 83 | + } else { |
|
| 84 | + throw new \UnexpectedValueException( |
|
| 85 | + 'Expected int, float or base-10 string' |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * @brief Tries to get the size of a file via various workarounds that |
|
| 92 | - * even work for large files on 32-bit platforms. |
|
| 93 | - * |
|
| 94 | - * @param string $filename Path to the file. |
|
| 95 | - * |
|
| 96 | - * @return null|int|float Number of bytes as number (float or int) or |
|
| 97 | - * null on failure. |
|
| 98 | - */ |
|
| 99 | - public function getFileSize($filename) { |
|
| 100 | - $fileSize = $this->getFileSizeViaCurl($filename); |
|
| 101 | - if (!is_null($fileSize)) { |
|
| 102 | - return $fileSize; |
|
| 103 | - } |
|
| 104 | - $fileSize = $this->getFileSizeViaExec($filename); |
|
| 105 | - if (!is_null($fileSize)) { |
|
| 106 | - return $fileSize; |
|
| 107 | - } |
|
| 108 | - return $this->getFileSizeNative($filename); |
|
| 109 | - } |
|
| 90 | + /** |
|
| 91 | + * @brief Tries to get the size of a file via various workarounds that |
|
| 92 | + * even work for large files on 32-bit platforms. |
|
| 93 | + * |
|
| 94 | + * @param string $filename Path to the file. |
|
| 95 | + * |
|
| 96 | + * @return null|int|float Number of bytes as number (float or int) or |
|
| 97 | + * null on failure. |
|
| 98 | + */ |
|
| 99 | + public function getFileSize($filename) { |
|
| 100 | + $fileSize = $this->getFileSizeViaCurl($filename); |
|
| 101 | + if (!is_null($fileSize)) { |
|
| 102 | + return $fileSize; |
|
| 103 | + } |
|
| 104 | + $fileSize = $this->getFileSizeViaExec($filename); |
|
| 105 | + if (!is_null($fileSize)) { |
|
| 106 | + return $fileSize; |
|
| 107 | + } |
|
| 108 | + return $this->getFileSizeNative($filename); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * @brief Tries to get the size of a file via a CURL HEAD request. |
|
| 113 | - * |
|
| 114 | - * @param string $fileName Path to the file. |
|
| 115 | - * |
|
| 116 | - * @return null|int|float Number of bytes as number (float or int) or |
|
| 117 | - * null on failure. |
|
| 118 | - */ |
|
| 119 | - public function getFileSizeViaCurl($fileName) { |
|
| 120 | - if (\OC::$server->getIniWrapper()->getString('open_basedir') === '') { |
|
| 121 | - $encodedFileName = rawurlencode($fileName); |
|
| 122 | - $ch = curl_init("file:///$encodedFileName"); |
|
| 123 | - curl_setopt($ch, CURLOPT_NOBODY, true); |
|
| 124 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 125 | - curl_setopt($ch, CURLOPT_HEADER, true); |
|
| 126 | - $data = curl_exec($ch); |
|
| 127 | - curl_close($ch); |
|
| 128 | - if ($data !== false) { |
|
| 129 | - $matches = []; |
|
| 130 | - preg_match('/Content-Length: (\d+)/', $data, $matches); |
|
| 131 | - if (isset($matches[1])) { |
|
| 132 | - return 0 + $matches[1]; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - return null; |
|
| 137 | - } |
|
| 111 | + /** |
|
| 112 | + * @brief Tries to get the size of a file via a CURL HEAD request. |
|
| 113 | + * |
|
| 114 | + * @param string $fileName Path to the file. |
|
| 115 | + * |
|
| 116 | + * @return null|int|float Number of bytes as number (float or int) or |
|
| 117 | + * null on failure. |
|
| 118 | + */ |
|
| 119 | + public function getFileSizeViaCurl($fileName) { |
|
| 120 | + if (\OC::$server->getIniWrapper()->getString('open_basedir') === '') { |
|
| 121 | + $encodedFileName = rawurlencode($fileName); |
|
| 122 | + $ch = curl_init("file:///$encodedFileName"); |
|
| 123 | + curl_setopt($ch, CURLOPT_NOBODY, true); |
|
| 124 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 125 | + curl_setopt($ch, CURLOPT_HEADER, true); |
|
| 126 | + $data = curl_exec($ch); |
|
| 127 | + curl_close($ch); |
|
| 128 | + if ($data !== false) { |
|
| 129 | + $matches = []; |
|
| 130 | + preg_match('/Content-Length: (\d+)/', $data, $matches); |
|
| 131 | + if (isset($matches[1])) { |
|
| 132 | + return 0 + $matches[1]; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + return null; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * @brief Tries to get the size of a file via an exec() call. |
|
| 141 | - * |
|
| 142 | - * @param string $filename Path to the file. |
|
| 143 | - * |
|
| 144 | - * @return null|int|float Number of bytes as number (float or int) or |
|
| 145 | - * null on failure. |
|
| 146 | - */ |
|
| 147 | - public function getFileSizeViaExec($filename) { |
|
| 148 | - if (\OC_Helper::is_function_enabled('exec')) { |
|
| 149 | - $os = strtolower(php_uname('s')); |
|
| 150 | - $arg = escapeshellarg($filename); |
|
| 151 | - $result = null; |
|
| 152 | - if (strpos($os, 'linux') !== false) { |
|
| 153 | - $result = $this->exec("stat -c %s $arg"); |
|
| 154 | - } else if (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) { |
|
| 155 | - $result = $this->exec("stat -f %z $arg"); |
|
| 156 | - } |
|
| 157 | - return $result; |
|
| 158 | - } |
|
| 159 | - return null; |
|
| 160 | - } |
|
| 139 | + /** |
|
| 140 | + * @brief Tries to get the size of a file via an exec() call. |
|
| 141 | + * |
|
| 142 | + * @param string $filename Path to the file. |
|
| 143 | + * |
|
| 144 | + * @return null|int|float Number of bytes as number (float or int) or |
|
| 145 | + * null on failure. |
|
| 146 | + */ |
|
| 147 | + public function getFileSizeViaExec($filename) { |
|
| 148 | + if (\OC_Helper::is_function_enabled('exec')) { |
|
| 149 | + $os = strtolower(php_uname('s')); |
|
| 150 | + $arg = escapeshellarg($filename); |
|
| 151 | + $result = null; |
|
| 152 | + if (strpos($os, 'linux') !== false) { |
|
| 153 | + $result = $this->exec("stat -c %s $arg"); |
|
| 154 | + } else if (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) { |
|
| 155 | + $result = $this->exec("stat -f %z $arg"); |
|
| 156 | + } |
|
| 157 | + return $result; |
|
| 158 | + } |
|
| 159 | + return null; |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * @brief Gets the size of a file via a filesize() call and converts |
|
| 164 | - * negative signed int to positive float. As the result of filesize() |
|
| 165 | - * will wrap around after a file size of 2^32 bytes = 4 GiB, this |
|
| 166 | - * should only be used as a last resort. |
|
| 167 | - * |
|
| 168 | - * @param string $filename Path to the file. |
|
| 169 | - * |
|
| 170 | - * @return int|float Number of bytes as number (float or int). |
|
| 171 | - */ |
|
| 172 | - public function getFileSizeNative($filename) { |
|
| 173 | - $result = filesize($filename); |
|
| 174 | - if ($result < 0) { |
|
| 175 | - // For file sizes between 2 GiB and 4 GiB, filesize() will return a |
|
| 176 | - // negative int, as the PHP data type int is signed. Interpret the |
|
| 177 | - // returned int as an unsigned integer and put it into a float. |
|
| 178 | - return (float) sprintf('%u', $result); |
|
| 179 | - } |
|
| 180 | - return $result; |
|
| 181 | - } |
|
| 162 | + /** |
|
| 163 | + * @brief Gets the size of a file via a filesize() call and converts |
|
| 164 | + * negative signed int to positive float. As the result of filesize() |
|
| 165 | + * will wrap around after a file size of 2^32 bytes = 4 GiB, this |
|
| 166 | + * should only be used as a last resort. |
|
| 167 | + * |
|
| 168 | + * @param string $filename Path to the file. |
|
| 169 | + * |
|
| 170 | + * @return int|float Number of bytes as number (float or int). |
|
| 171 | + */ |
|
| 172 | + public function getFileSizeNative($filename) { |
|
| 173 | + $result = filesize($filename); |
|
| 174 | + if ($result < 0) { |
|
| 175 | + // For file sizes between 2 GiB and 4 GiB, filesize() will return a |
|
| 176 | + // negative int, as the PHP data type int is signed. Interpret the |
|
| 177 | + // returned int as an unsigned integer and put it into a float. |
|
| 178 | + return (float) sprintf('%u', $result); |
|
| 179 | + } |
|
| 180 | + return $result; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Returns the current mtime for $fullPath |
|
| 185 | - * |
|
| 186 | - * @param string $fullPath |
|
| 187 | - * @return int |
|
| 188 | - */ |
|
| 189 | - public function getFileMtime($fullPath) { |
|
| 190 | - try { |
|
| 191 | - $result = filemtime($fullPath); |
|
| 192 | - } catch (\Exception $e) { |
|
| 193 | - $result =- 1; |
|
| 194 | - } |
|
| 195 | - if ($result < 0) { |
|
| 196 | - if (\OC_Helper::is_function_enabled('exec')) { |
|
| 197 | - $os = strtolower(php_uname('s')); |
|
| 198 | - if (strpos($os, 'linux') !== false) { |
|
| 199 | - return $this->exec('stat -c %Y ' . escapeshellarg($fullPath)); |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - return $result; |
|
| 183 | + /** |
|
| 184 | + * Returns the current mtime for $fullPath |
|
| 185 | + * |
|
| 186 | + * @param string $fullPath |
|
| 187 | + * @return int |
|
| 188 | + */ |
|
| 189 | + public function getFileMtime($fullPath) { |
|
| 190 | + try { |
|
| 191 | + $result = filemtime($fullPath); |
|
| 192 | + } catch (\Exception $e) { |
|
| 193 | + $result =- 1; |
|
| 194 | + } |
|
| 195 | + if ($result < 0) { |
|
| 196 | + if (\OC_Helper::is_function_enabled('exec')) { |
|
| 197 | + $os = strtolower(php_uname('s')); |
|
| 198 | + if (strpos($os, 'linux') !== false) { |
|
| 199 | + return $this->exec('stat -c %Y ' . escapeshellarg($fullPath)); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + return $result; |
|
| 204 | 204 | |
| 205 | 205 | |
| 206 | - } |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - protected function exec($cmd) { |
|
| 209 | - $result = trim(exec($cmd)); |
|
| 210 | - return ctype_digit($result) ? 0 + $result : null; |
|
| 211 | - } |
|
| 208 | + protected function exec($cmd) { |
|
| 209 | + $result = trim(exec($cmd)); |
|
| 210 | + return ctype_digit($result) ? 0 + $result : null; |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * PHP platform. |
| 53 | 53 | */ |
| 54 | 54 | public function __construct() { |
| 55 | - $pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0; |
|
| 55 | + $pow_2_53 = (float) self::POW_2_53_MINUS_1 + 1.0; |
|
| 56 | 56 | if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) { |
| 57 | 57 | throw new \RuntimeException( |
| 58 | 58 | 'This class assumes floats to be double precision or "better".' |
@@ -190,13 +190,13 @@ discard block |
||
| 190 | 190 | try { |
| 191 | 191 | $result = filemtime($fullPath); |
| 192 | 192 | } catch (\Exception $e) { |
| 193 | - $result =- 1; |
|
| 193 | + $result = - 1; |
|
| 194 | 194 | } |
| 195 | 195 | if ($result < 0) { |
| 196 | 196 | if (\OC_Helper::is_function_enabled('exec')) { |
| 197 | 197 | $os = strtolower(php_uname('s')); |
| 198 | 198 | if (strpos($os, 'linux') !== false) { |
| 199 | - return $this->exec('stat -c %Y ' . escapeshellarg($fullPath)); |
|
| 199 | + return $this->exec('stat -c %Y '.escapeshellarg($fullPath)); |
|
| 200 | 200 | } |
| 201 | 201 | } |
| 202 | 202 | } |
@@ -65,872 +65,872 @@ |
||
| 65 | 65 | */ |
| 66 | 66 | class Request implements \ArrayAccess, \Countable, IRequest { |
| 67 | 67 | |
| 68 | - const USER_AGENT_IE = '/(MSIE)|(Trident)/'; |
|
| 69 | - // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx |
|
| 70 | - const USER_AGENT_MS_EDGE = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+ Edge\/[0-9.]+$/'; |
|
| 71 | - // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
|
| 72 | - const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/'; |
|
| 73 | - // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent |
|
| 74 | - const USER_AGENT_CHROME = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\)( Ubuntu Chromium\/[0-9.]+|) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+( (Vivaldi|Brave|OPR)\/[0-9.]+|)$/'; |
|
| 75 | - // Safari User Agent from http://www.useragentstring.com/pages/Safari/ |
|
| 76 | - const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/'; |
|
| 77 | - // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
|
| 78 | - const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
|
| 79 | - const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
|
| 80 | - const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost|::1)$/'; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_IOS instead |
|
| 84 | - */ |
|
| 85 | - const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/'; |
|
| 86 | - /** |
|
| 87 | - * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_ANDROID instead |
|
| 88 | - */ |
|
| 89 | - const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
|
| 90 | - /** |
|
| 91 | - * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_DESKTOP instead |
|
| 92 | - */ |
|
| 93 | - const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
|
| 94 | - |
|
| 95 | - protected $inputStream; |
|
| 96 | - protected $content; |
|
| 97 | - protected $items = []; |
|
| 98 | - protected $allowedKeys = [ |
|
| 99 | - 'get', |
|
| 100 | - 'post', |
|
| 101 | - 'files', |
|
| 102 | - 'server', |
|
| 103 | - 'env', |
|
| 104 | - 'cookies', |
|
| 105 | - 'urlParams', |
|
| 106 | - 'parameters', |
|
| 107 | - 'method', |
|
| 108 | - 'requesttoken', |
|
| 109 | - ]; |
|
| 110 | - /** @var ISecureRandom */ |
|
| 111 | - protected $secureRandom; |
|
| 112 | - /** @var IConfig */ |
|
| 113 | - protected $config; |
|
| 114 | - /** @var string */ |
|
| 115 | - protected $requestId = ''; |
|
| 116 | - /** @var ICrypto */ |
|
| 117 | - protected $crypto; |
|
| 118 | - /** @var CsrfTokenManager|null */ |
|
| 119 | - protected $csrfTokenManager; |
|
| 120 | - |
|
| 121 | - /** @var bool */ |
|
| 122 | - protected $contentDecoded = false; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @param array $vars An associative array with the following optional values: |
|
| 126 | - * - array 'urlParams' the parameters which were matched from the URL |
|
| 127 | - * - array 'get' the $_GET array |
|
| 128 | - * - array|string 'post' the $_POST array or JSON string |
|
| 129 | - * - array 'files' the $_FILES array |
|
| 130 | - * - array 'server' the $_SERVER array |
|
| 131 | - * - array 'env' the $_ENV array |
|
| 132 | - * - array 'cookies' the $_COOKIE array |
|
| 133 | - * - string 'method' the request method (GET, POST etc) |
|
| 134 | - * - string|false 'requesttoken' the requesttoken or false when not available |
|
| 135 | - * @param ISecureRandom $secureRandom |
|
| 136 | - * @param IConfig $config |
|
| 137 | - * @param CsrfTokenManager|null $csrfTokenManager |
|
| 138 | - * @param string $stream |
|
| 139 | - * @see http://www.php.net/manual/en/reserved.variables.php |
|
| 140 | - */ |
|
| 141 | - public function __construct(array $vars= [], |
|
| 142 | - ISecureRandom $secureRandom = null, |
|
| 143 | - IConfig $config, |
|
| 144 | - CsrfTokenManager $csrfTokenManager = null, |
|
| 145 | - string $stream = 'php://input') { |
|
| 146 | - $this->inputStream = $stream; |
|
| 147 | - $this->items['params'] = []; |
|
| 148 | - $this->secureRandom = $secureRandom; |
|
| 149 | - $this->config = $config; |
|
| 150 | - $this->csrfTokenManager = $csrfTokenManager; |
|
| 151 | - |
|
| 152 | - if(!array_key_exists('method', $vars)) { |
|
| 153 | - $vars['method'] = 'GET'; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - foreach($this->allowedKeys as $name) { |
|
| 157 | - $this->items[$name] = isset($vars[$name]) |
|
| 158 | - ? $vars[$name] |
|
| 159 | - : []; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $this->items['parameters'] = array_merge( |
|
| 163 | - $this->items['get'], |
|
| 164 | - $this->items['post'], |
|
| 165 | - $this->items['urlParams'], |
|
| 166 | - $this->items['params'] |
|
| 167 | - ); |
|
| 168 | - |
|
| 169 | - } |
|
| 170 | - /** |
|
| 171 | - * @param array $parameters |
|
| 172 | - */ |
|
| 173 | - public function setUrlParameters(array $parameters) { |
|
| 174 | - $this->items['urlParams'] = $parameters; |
|
| 175 | - $this->items['parameters'] = array_merge( |
|
| 176 | - $this->items['parameters'], |
|
| 177 | - $this->items['urlParams'] |
|
| 178 | - ); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Countable method |
|
| 183 | - * @return int |
|
| 184 | - */ |
|
| 185 | - public function count(): int { |
|
| 186 | - return \count($this->items['parameters']); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * ArrayAccess methods |
|
| 191 | - * |
|
| 192 | - * Gives access to the combined GET, POST and urlParams arrays |
|
| 193 | - * |
|
| 194 | - * Examples: |
|
| 195 | - * |
|
| 196 | - * $var = $request['myvar']; |
|
| 197 | - * |
|
| 198 | - * or |
|
| 199 | - * |
|
| 200 | - * if(!isset($request['myvar']) { |
|
| 201 | - * // Do something |
|
| 202 | - * } |
|
| 203 | - * |
|
| 204 | - * $request['myvar'] = 'something'; // This throws an exception. |
|
| 205 | - * |
|
| 206 | - * @param string $offset The key to lookup |
|
| 207 | - * @return boolean |
|
| 208 | - */ |
|
| 209 | - public function offsetExists($offset): bool { |
|
| 210 | - return isset($this->items['parameters'][$offset]); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @see offsetExists |
|
| 215 | - * @param string $offset |
|
| 216 | - * @return mixed |
|
| 217 | - */ |
|
| 218 | - public function offsetGet($offset) { |
|
| 219 | - return isset($this->items['parameters'][$offset]) |
|
| 220 | - ? $this->items['parameters'][$offset] |
|
| 221 | - : null; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @see offsetExists |
|
| 226 | - * @param string $offset |
|
| 227 | - * @param mixed $value |
|
| 228 | - */ |
|
| 229 | - public function offsetSet($offset, $value) { |
|
| 230 | - throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @see offsetExists |
|
| 235 | - * @param string $offset |
|
| 236 | - */ |
|
| 237 | - public function offsetUnset($offset) { |
|
| 238 | - throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Magic property accessors |
|
| 243 | - * @param string $name |
|
| 244 | - * @param mixed $value |
|
| 245 | - */ |
|
| 246 | - public function __set($name, $value) { |
|
| 247 | - throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Access request variables by method and name. |
|
| 252 | - * Examples: |
|
| 253 | - * |
|
| 254 | - * $request->post['myvar']; // Only look for POST variables |
|
| 255 | - * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
|
| 256 | - * Looks in the combined GET, POST and urlParams array. |
|
| 257 | - * |
|
| 258 | - * If you access e.g. ->post but the current HTTP request method |
|
| 259 | - * is GET a \LogicException will be thrown. |
|
| 260 | - * |
|
| 261 | - * @param string $name The key to look for. |
|
| 262 | - * @throws \LogicException |
|
| 263 | - * @return mixed|null |
|
| 264 | - */ |
|
| 265 | - public function __get($name) { |
|
| 266 | - switch($name) { |
|
| 267 | - case 'put': |
|
| 268 | - case 'patch': |
|
| 269 | - case 'get': |
|
| 270 | - case 'post': |
|
| 271 | - if($this->method !== strtoupper($name)) { |
|
| 272 | - throw new \LogicException(sprintf('%s cannot be accessed in a %s request.', $name, $this->method)); |
|
| 273 | - } |
|
| 274 | - return $this->getContent(); |
|
| 275 | - case 'files': |
|
| 276 | - case 'server': |
|
| 277 | - case 'env': |
|
| 278 | - case 'cookies': |
|
| 279 | - case 'urlParams': |
|
| 280 | - case 'method': |
|
| 281 | - return isset($this->items[$name]) |
|
| 282 | - ? $this->items[$name] |
|
| 283 | - : null; |
|
| 284 | - case 'parameters': |
|
| 285 | - case 'params': |
|
| 286 | - return $this->getContent(); |
|
| 287 | - default; |
|
| 288 | - return isset($this[$name]) |
|
| 289 | - ? $this[$name] |
|
| 290 | - : null; |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * @param string $name |
|
| 296 | - * @return bool |
|
| 297 | - */ |
|
| 298 | - public function __isset($name) { |
|
| 299 | - if (\in_array($name, $this->allowedKeys, true)) { |
|
| 300 | - return true; |
|
| 301 | - } |
|
| 302 | - return isset($this->items['parameters'][$name]); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * @param string $id |
|
| 307 | - */ |
|
| 308 | - public function __unset($id) { |
|
| 309 | - throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Returns the value for a specific http header. |
|
| 314 | - * |
|
| 315 | - * This method returns null if the header did not exist. |
|
| 316 | - * |
|
| 317 | - * @param string $name |
|
| 318 | - * @return string |
|
| 319 | - */ |
|
| 320 | - public function getHeader(string $name): string { |
|
| 321 | - |
|
| 322 | - $name = strtoupper(str_replace('-', '_',$name)); |
|
| 323 | - if (isset($this->server['HTTP_' . $name])) { |
|
| 324 | - return $this->server['HTTP_' . $name]; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // There's a few headers that seem to end up in the top-level |
|
| 328 | - // server array. |
|
| 329 | - switch ($name) { |
|
| 330 | - case 'CONTENT_TYPE' : |
|
| 331 | - case 'CONTENT_LENGTH' : |
|
| 332 | - case 'REMOTE_ADDR': |
|
| 333 | - if (isset($this->server[$name])) { |
|
| 334 | - return $this->server[$name]; |
|
| 335 | - } |
|
| 336 | - break; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - return ''; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Lets you access post and get parameters by the index |
|
| 344 | - * In case of json requests the encoded json body is accessed |
|
| 345 | - * |
|
| 346 | - * @param string $key the key which you want to access in the URL Parameter |
|
| 347 | - * placeholder, $_POST or $_GET array. |
|
| 348 | - * The priority how they're returned is the following: |
|
| 349 | - * 1. URL parameters |
|
| 350 | - * 2. POST parameters |
|
| 351 | - * 3. GET parameters |
|
| 352 | - * @param mixed $default If the key is not found, this value will be returned |
|
| 353 | - * @return mixed the content of the array |
|
| 354 | - */ |
|
| 355 | - public function getParam(string $key, $default = null) { |
|
| 356 | - return isset($this->parameters[$key]) |
|
| 357 | - ? $this->parameters[$key] |
|
| 358 | - : $default; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Returns all params that were received, be it from the request |
|
| 363 | - * (as GET or POST) or throuh the URL by the route |
|
| 364 | - * @return array the array with all parameters |
|
| 365 | - */ |
|
| 366 | - public function getParams(): array { |
|
| 367 | - return is_array($this->parameters) ? $this->parameters : []; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Returns the method of the request |
|
| 372 | - * @return string the method of the request (POST, GET, etc) |
|
| 373 | - */ |
|
| 374 | - public function getMethod(): string { |
|
| 375 | - return $this->method; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Shortcut for accessing an uploaded file through the $_FILES array |
|
| 380 | - * @param string $key the key that will be taken from the $_FILES array |
|
| 381 | - * @return array the file in the $_FILES element |
|
| 382 | - */ |
|
| 383 | - public function getUploadedFile(string $key) { |
|
| 384 | - return isset($this->files[$key]) ? $this->files[$key] : null; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Shortcut for getting env variables |
|
| 389 | - * @param string $key the key that will be taken from the $_ENV array |
|
| 390 | - * @return array the value in the $_ENV element |
|
| 391 | - */ |
|
| 392 | - public function getEnv(string $key) { |
|
| 393 | - return isset($this->env[$key]) ? $this->env[$key] : null; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Shortcut for getting cookie variables |
|
| 398 | - * @param string $key the key that will be taken from the $_COOKIE array |
|
| 399 | - * @return string the value in the $_COOKIE element |
|
| 400 | - */ |
|
| 401 | - public function getCookie(string $key) { |
|
| 402 | - return isset($this->cookies[$key]) ? $this->cookies[$key] : null; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Returns the request body content. |
|
| 407 | - * |
|
| 408 | - * If the HTTP request method is PUT and the body |
|
| 409 | - * not application/x-www-form-urlencoded or application/json a stream |
|
| 410 | - * resource is returned, otherwise an array. |
|
| 411 | - * |
|
| 412 | - * @return array|string|resource The request body content or a resource to read the body stream. |
|
| 413 | - * |
|
| 414 | - * @throws \LogicException |
|
| 415 | - */ |
|
| 416 | - protected function getContent() { |
|
| 417 | - // If the content can't be parsed into an array then return a stream resource. |
|
| 418 | - if ($this->method === 'PUT' |
|
| 419 | - && $this->getHeader('Content-Length') !== '0' |
|
| 420 | - && $this->getHeader('Content-Length') !== '' |
|
| 421 | - && strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false |
|
| 422 | - && strpos($this->getHeader('Content-Type'), 'application/json') === false |
|
| 423 | - ) { |
|
| 424 | - if ($this->content === false) { |
|
| 425 | - throw new \LogicException( |
|
| 426 | - '"put" can only be accessed once if not ' |
|
| 427 | - . 'application/x-www-form-urlencoded or application/json.' |
|
| 428 | - ); |
|
| 429 | - } |
|
| 430 | - $this->content = false; |
|
| 431 | - return fopen($this->inputStream, 'rb'); |
|
| 432 | - } else { |
|
| 433 | - $this->decodeContent(); |
|
| 434 | - return $this->items['parameters']; |
|
| 435 | - } |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Attempt to decode the content and populate parameters |
|
| 440 | - */ |
|
| 441 | - protected function decodeContent() { |
|
| 442 | - if ($this->contentDecoded) { |
|
| 443 | - return; |
|
| 444 | - } |
|
| 445 | - $params = []; |
|
| 446 | - |
|
| 447 | - // 'application/json' must be decoded manually. |
|
| 448 | - if (strpos($this->getHeader('Content-Type'), 'application/json') !== false) { |
|
| 449 | - $params = json_decode(file_get_contents($this->inputStream), true); |
|
| 450 | - if($params !== null && \count($params) > 0) { |
|
| 451 | - $this->items['params'] = $params; |
|
| 452 | - if($this->method === 'POST') { |
|
| 453 | - $this->items['post'] = $params; |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - // Handle application/x-www-form-urlencoded for methods other than GET |
|
| 458 | - // or post correctly |
|
| 459 | - } elseif($this->method !== 'GET' |
|
| 460 | - && $this->method !== 'POST' |
|
| 461 | - && strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') !== false) { |
|
| 462 | - |
|
| 463 | - parse_str(file_get_contents($this->inputStream), $params); |
|
| 464 | - if(\is_array($params)) { |
|
| 465 | - $this->items['params'] = $params; |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - if (\is_array($params)) { |
|
| 470 | - $this->items['parameters'] = array_merge($this->items['parameters'], $params); |
|
| 471 | - } |
|
| 472 | - $this->contentDecoded = true; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Checks if the CSRF check was correct |
|
| 478 | - * @return bool true if CSRF check passed |
|
| 479 | - */ |
|
| 480 | - public function passesCSRFCheck(): bool { |
|
| 481 | - if($this->csrfTokenManager === null) { |
|
| 482 | - return false; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - if(!$this->passesStrictCookieCheck()) { |
|
| 486 | - return false; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - if (isset($this->items['get']['requesttoken'])) { |
|
| 490 | - $token = $this->items['get']['requesttoken']; |
|
| 491 | - } elseif (isset($this->items['post']['requesttoken'])) { |
|
| 492 | - $token = $this->items['post']['requesttoken']; |
|
| 493 | - } elseif (isset($this->items['server']['HTTP_REQUESTTOKEN'])) { |
|
| 494 | - $token = $this->items['server']['HTTP_REQUESTTOKEN']; |
|
| 495 | - } else { |
|
| 496 | - //no token found. |
|
| 497 | - return false; |
|
| 498 | - } |
|
| 499 | - $token = new CsrfToken($token); |
|
| 500 | - |
|
| 501 | - return $this->csrfTokenManager->isTokenValid($token); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Whether the cookie checks are required |
|
| 506 | - * |
|
| 507 | - * @return bool |
|
| 508 | - */ |
|
| 509 | - private function cookieCheckRequired(): bool { |
|
| 510 | - if ($this->getHeader('OCS-APIREQUEST')) { |
|
| 511 | - return false; |
|
| 512 | - } |
|
| 513 | - if($this->getCookie(session_name()) === null && $this->getCookie('nc_token') === null) { |
|
| 514 | - return false; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - return true; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Wrapper around session_get_cookie_params |
|
| 522 | - * |
|
| 523 | - * @return array |
|
| 524 | - */ |
|
| 525 | - public function getCookieParams(): array { |
|
| 526 | - return session_get_cookie_params(); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Appends the __Host- prefix to the cookie if applicable |
|
| 531 | - * |
|
| 532 | - * @param string $name |
|
| 533 | - * @return string |
|
| 534 | - */ |
|
| 535 | - protected function getProtectedCookieName(string $name): string { |
|
| 536 | - $cookieParams = $this->getCookieParams(); |
|
| 537 | - $prefix = ''; |
|
| 538 | - if($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
| 539 | - $prefix = '__Host-'; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - return $prefix.$name; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Checks if the strict cookie has been sent with the request if the request |
|
| 547 | - * is including any cookies. |
|
| 548 | - * |
|
| 549 | - * @return bool |
|
| 550 | - * @since 9.1.0 |
|
| 551 | - */ |
|
| 552 | - public function passesStrictCookieCheck(): bool { |
|
| 553 | - if(!$this->cookieCheckRequired()) { |
|
| 554 | - return true; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - $cookieName = $this->getProtectedCookieName('nc_sameSiteCookiestrict'); |
|
| 558 | - if($this->getCookie($cookieName) === 'true' |
|
| 559 | - && $this->passesLaxCookieCheck()) { |
|
| 560 | - return true; |
|
| 561 | - } |
|
| 562 | - return false; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * Checks if the lax cookie has been sent with the request if the request |
|
| 567 | - * is including any cookies. |
|
| 568 | - * |
|
| 569 | - * @return bool |
|
| 570 | - * @since 9.1.0 |
|
| 571 | - */ |
|
| 572 | - public function passesLaxCookieCheck(): bool { |
|
| 573 | - if(!$this->cookieCheckRequired()) { |
|
| 574 | - return true; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - $cookieName = $this->getProtectedCookieName('nc_sameSiteCookielax'); |
|
| 578 | - if($this->getCookie($cookieName) === 'true') { |
|
| 579 | - return true; |
|
| 580 | - } |
|
| 581 | - return false; |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
|
| 587 | - * If `mod_unique_id` is installed this value will be taken. |
|
| 588 | - * @return string |
|
| 589 | - */ |
|
| 590 | - public function getId(): string { |
|
| 591 | - if(isset($this->server['UNIQUE_ID'])) { |
|
| 592 | - return $this->server['UNIQUE_ID']; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - if(empty($this->requestId)) { |
|
| 596 | - $validChars = ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS; |
|
| 597 | - $this->requestId = $this->secureRandom->generate(20, $validChars); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - return $this->requestId; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Checks if given $remoteAddress matches given $trustedProxy. |
|
| 605 | - * If $trustedProxy is an IPv4 IP range given in CIDR notation, true will be returned if |
|
| 606 | - * $remoteAddress is an IPv4 address within that IP range. |
|
| 607 | - * Otherwise $remoteAddress will be compared to $trustedProxy literally and the result |
|
| 608 | - * will be returned. |
|
| 609 | - * @return boolean true if $remoteAddress matches $trustedProxy, false otherwise |
|
| 610 | - */ |
|
| 611 | - protected function matchesTrustedProxy($trustedProxy, $remoteAddress) { |
|
| 612 | - $cidrre = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})$/'; |
|
| 613 | - |
|
| 614 | - if (preg_match($cidrre, $trustedProxy, $match)) { |
|
| 615 | - $net = $match[1]; |
|
| 616 | - $shiftbits = min(32, max(0, 32 - intval($match[2]))); |
|
| 617 | - $netnum = ip2long($net) >> $shiftbits; |
|
| 618 | - $ipnum = ip2long($remoteAddress) >> $shiftbits; |
|
| 619 | - |
|
| 620 | - return $ipnum === $netnum; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - return $trustedProxy === $remoteAddress; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * Checks if given $remoteAddress matches any entry in the given array $trustedProxies. |
|
| 628 | - * For details regarding what "match" means, refer to `matchesTrustedProxy`. |
|
| 629 | - * @return boolean true if $remoteAddress matches any entry in $trustedProxies, false otherwise |
|
| 630 | - */ |
|
| 631 | - protected function isTrustedProxy($trustedProxies, $remoteAddress) { |
|
| 632 | - foreach ($trustedProxies as $tp) { |
|
| 633 | - if ($this->matchesTrustedProxy($tp, $remoteAddress)) { |
|
| 634 | - return true; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - return false; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * Returns the remote address, if the connection came from a trusted proxy |
|
| 643 | - * and `forwarded_for_headers` has been configured then the IP address |
|
| 644 | - * specified in this header will be returned instead. |
|
| 645 | - * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
|
| 646 | - * @return string IP address |
|
| 647 | - */ |
|
| 648 | - public function getRemoteAddress(): string { |
|
| 649 | - $remoteAddress = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
|
| 650 | - $trustedProxies = $this->config->getSystemValue('trusted_proxies', []); |
|
| 651 | - |
|
| 652 | - if(\is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress)) { |
|
| 653 | - $forwardedForHeaders = $this->config->getSystemValue('forwarded_for_headers', [ |
|
| 654 | - 'HTTP_X_FORWARDED_FOR' |
|
| 655 | - // only have one default, so we cannot ship an insecure product out of the box |
|
| 656 | - ]); |
|
| 657 | - |
|
| 658 | - foreach($forwardedForHeaders as $header) { |
|
| 659 | - if(isset($this->server[$header])) { |
|
| 660 | - foreach(explode(',', $this->server[$header]) as $IP) { |
|
| 661 | - $IP = trim($IP); |
|
| 662 | - if (filter_var($IP, FILTER_VALIDATE_IP) !== false) { |
|
| 663 | - return $IP; |
|
| 664 | - } |
|
| 665 | - } |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - return $remoteAddress; |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Check overwrite condition |
|
| 675 | - * @param string $type |
|
| 676 | - * @return bool |
|
| 677 | - */ |
|
| 678 | - private function isOverwriteCondition(string $type = ''): bool { |
|
| 679 | - $regex = '/' . $this->config->getSystemValue('overwritecondaddr', '') . '/'; |
|
| 680 | - $remoteAddr = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
|
| 681 | - return $regex === '//' || preg_match($regex, $remoteAddr) === 1 |
|
| 682 | - || $type !== 'protocol'; |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * Returns the server protocol. It respects one or more reverse proxies servers |
|
| 687 | - * and load balancers |
|
| 688 | - * @return string Server protocol (http or https) |
|
| 689 | - */ |
|
| 690 | - public function getServerProtocol(): string { |
|
| 691 | - if($this->config->getSystemValue('overwriteprotocol') !== '' |
|
| 692 | - && $this->isOverwriteCondition('protocol')) { |
|
| 693 | - return $this->config->getSystemValue('overwriteprotocol'); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - if ($this->fromTrustedProxy() && isset($this->server['HTTP_X_FORWARDED_PROTO'])) { |
|
| 697 | - if (strpos($this->server['HTTP_X_FORWARDED_PROTO'], ',') !== false) { |
|
| 698 | - $parts = explode(',', $this->server['HTTP_X_FORWARDED_PROTO']); |
|
| 699 | - $proto = strtolower(trim($parts[0])); |
|
| 700 | - } else { |
|
| 701 | - $proto = strtolower($this->server['HTTP_X_FORWARDED_PROTO']); |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - // Verify that the protocol is always HTTP or HTTPS |
|
| 705 | - // default to http if an invalid value is provided |
|
| 706 | - return $proto === 'https' ? 'https' : 'http'; |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - if (isset($this->server['HTTPS']) |
|
| 710 | - && $this->server['HTTPS'] !== null |
|
| 711 | - && $this->server['HTTPS'] !== 'off' |
|
| 712 | - && $this->server['HTTPS'] !== '') { |
|
| 713 | - return 'https'; |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - return 'http'; |
|
| 717 | - } |
|
| 718 | - |
|
| 719 | - /** |
|
| 720 | - * Returns the used HTTP protocol. |
|
| 721 | - * |
|
| 722 | - * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
|
| 723 | - */ |
|
| 724 | - public function getHttpProtocol(): string { |
|
| 725 | - $claimedProtocol = $this->server['SERVER_PROTOCOL']; |
|
| 726 | - |
|
| 727 | - if (\is_string($claimedProtocol)) { |
|
| 728 | - $claimedProtocol = strtoupper($claimedProtocol); |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - $validProtocols = [ |
|
| 732 | - 'HTTP/1.0', |
|
| 733 | - 'HTTP/1.1', |
|
| 734 | - 'HTTP/2', |
|
| 735 | - ]; |
|
| 736 | - |
|
| 737 | - if(\in_array($claimedProtocol, $validProtocols, true)) { |
|
| 738 | - return $claimedProtocol; |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - return 'HTTP/1.1'; |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - /** |
|
| 745 | - * Returns the request uri, even if the website uses one or more |
|
| 746 | - * reverse proxies |
|
| 747 | - * @return string |
|
| 748 | - */ |
|
| 749 | - public function getRequestUri(): string { |
|
| 750 | - $uri = isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : ''; |
|
| 751 | - if($this->config->getSystemValue('overwritewebroot') !== '' && $this->isOverwriteCondition()) { |
|
| 752 | - $uri = $this->getScriptName() . substr($uri, \strlen($this->server['SCRIPT_NAME'])); |
|
| 753 | - } |
|
| 754 | - return $uri; |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * Get raw PathInfo from request (not urldecoded) |
|
| 759 | - * @throws \Exception |
|
| 760 | - * @return string Path info |
|
| 761 | - */ |
|
| 762 | - public function getRawPathInfo(): string { |
|
| 763 | - $requestUri = isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : ''; |
|
| 764 | - // remove too many slashes - can be caused by reverse proxy configuration |
|
| 765 | - $requestUri = preg_replace('%/{2,}%', '/', $requestUri); |
|
| 766 | - |
|
| 767 | - // Remove the query string from REQUEST_URI |
|
| 768 | - if ($pos = strpos($requestUri, '?')) { |
|
| 769 | - $requestUri = substr($requestUri, 0, $pos); |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - $scriptName = $this->server['SCRIPT_NAME']; |
|
| 773 | - $pathInfo = $requestUri; |
|
| 774 | - |
|
| 775 | - // strip off the script name's dir and file name |
|
| 776 | - // FIXME: Sabre does not really belong here |
|
| 777 | - list($path, $name) = \Sabre\Uri\split($scriptName); |
|
| 778 | - if (!empty($path)) { |
|
| 779 | - if($path === $pathInfo || strpos($pathInfo, $path.'/') === 0) { |
|
| 780 | - $pathInfo = substr($pathInfo, \strlen($path)); |
|
| 781 | - } else { |
|
| 782 | - throw new \Exception("The requested uri($requestUri) cannot be processed by the script '$scriptName')"); |
|
| 783 | - } |
|
| 784 | - } |
|
| 785 | - if ($name === null) { |
|
| 786 | - $name = ''; |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - if (strpos($pathInfo, '/'.$name) === 0) { |
|
| 790 | - $pathInfo = substr($pathInfo, \strlen($name) + 1); |
|
| 791 | - } |
|
| 792 | - if ($name !== '' && strpos($pathInfo, $name) === 0) { |
|
| 793 | - $pathInfo = substr($pathInfo, \strlen($name)); |
|
| 794 | - } |
|
| 795 | - if($pathInfo === false || $pathInfo === '/'){ |
|
| 796 | - return ''; |
|
| 797 | - } else { |
|
| 798 | - return $pathInfo; |
|
| 799 | - } |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - /** |
|
| 803 | - * Get PathInfo from request |
|
| 804 | - * @throws \Exception |
|
| 805 | - * @return string|false Path info or false when not found |
|
| 806 | - */ |
|
| 807 | - public function getPathInfo() { |
|
| 808 | - $pathInfo = $this->getRawPathInfo(); |
|
| 809 | - // following is taken from \Sabre\HTTP\URLUtil::decodePathSegment |
|
| 810 | - $pathInfo = rawurldecode($pathInfo); |
|
| 811 | - $encoding = mb_detect_encoding($pathInfo, ['UTF-8', 'ISO-8859-1']); |
|
| 812 | - |
|
| 813 | - switch($encoding) { |
|
| 814 | - case 'ISO-8859-1' : |
|
| 815 | - $pathInfo = utf8_encode($pathInfo); |
|
| 816 | - } |
|
| 817 | - // end copy |
|
| 818 | - |
|
| 819 | - return $pathInfo; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - /** |
|
| 823 | - * Returns the script name, even if the website uses one or more |
|
| 824 | - * reverse proxies |
|
| 825 | - * @return string the script name |
|
| 826 | - */ |
|
| 827 | - public function getScriptName(): string { |
|
| 828 | - $name = $this->server['SCRIPT_NAME']; |
|
| 829 | - $overwriteWebRoot = $this->config->getSystemValue('overwritewebroot'); |
|
| 830 | - if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) { |
|
| 831 | - // FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous |
|
| 832 | - $serverRoot = str_replace('\\', '/', substr(__DIR__, 0, -\strlen('lib/private/appframework/http/'))); |
|
| 833 | - $suburi = str_replace('\\', '/', substr(realpath($this->server['SCRIPT_FILENAME']), \strlen($serverRoot))); |
|
| 834 | - $name = '/' . ltrim($overwriteWebRoot . $suburi, '/'); |
|
| 835 | - } |
|
| 836 | - return $name; |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - /** |
|
| 840 | - * Checks whether the user agent matches a given regex |
|
| 841 | - * @param array $agent array of agent names |
|
| 842 | - * @return bool true if at least one of the given agent matches, false otherwise |
|
| 843 | - */ |
|
| 844 | - public function isUserAgent(array $agent): bool { |
|
| 845 | - if (!isset($this->server['HTTP_USER_AGENT'])) { |
|
| 846 | - return false; |
|
| 847 | - } |
|
| 848 | - foreach ($agent as $regex) { |
|
| 849 | - if (preg_match($regex, $this->server['HTTP_USER_AGENT'])) { |
|
| 850 | - return true; |
|
| 851 | - } |
|
| 852 | - } |
|
| 853 | - return false; |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * Returns the unverified server host from the headers without checking |
|
| 858 | - * whether it is a trusted domain |
|
| 859 | - * @return string Server host |
|
| 860 | - */ |
|
| 861 | - public function getInsecureServerHost(): string { |
|
| 862 | - if ($this->fromTrustedProxy() && $this->getOverwriteHost() !== null) { |
|
| 863 | - return $this->getOverwriteHost(); |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - $host = 'localhost'; |
|
| 867 | - if ($this->fromTrustedProxy() && isset($this->server['HTTP_X_FORWARDED_HOST'])) { |
|
| 868 | - if (strpos($this->server['HTTP_X_FORWARDED_HOST'], ',') !== false) { |
|
| 869 | - $parts = explode(',', $this->server['HTTP_X_FORWARDED_HOST']); |
|
| 870 | - $host = trim(current($parts)); |
|
| 871 | - } else { |
|
| 872 | - $host = $this->server['HTTP_X_FORWARDED_HOST']; |
|
| 873 | - } |
|
| 874 | - } else { |
|
| 875 | - if (isset($this->server['HTTP_HOST'])) { |
|
| 876 | - $host = $this->server['HTTP_HOST']; |
|
| 877 | - } else if (isset($this->server['SERVER_NAME'])) { |
|
| 878 | - $host = $this->server['SERVER_NAME']; |
|
| 879 | - } |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - return $host; |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - |
|
| 886 | - /** |
|
| 887 | - * Returns the server host from the headers, or the first configured |
|
| 888 | - * trusted domain if the host isn't in the trusted list |
|
| 889 | - * @return string Server host |
|
| 890 | - */ |
|
| 891 | - public function getServerHost(): string { |
|
| 892 | - // overwritehost is always trusted |
|
| 893 | - $host = $this->getOverwriteHost(); |
|
| 894 | - if ($host !== null) { |
|
| 895 | - return $host; |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - // get the host from the headers |
|
| 899 | - $host = $this->getInsecureServerHost(); |
|
| 900 | - |
|
| 901 | - // Verify that the host is a trusted domain if the trusted domains |
|
| 902 | - // are defined |
|
| 903 | - // If no trusted domain is provided the first trusted domain is returned |
|
| 904 | - $trustedDomainHelper = new TrustedDomainHelper($this->config); |
|
| 905 | - if ($trustedDomainHelper->isTrustedDomain($host)) { |
|
| 906 | - return $host; |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - $trustedList = (array)$this->config->getSystemValue('trusted_domains', []); |
|
| 910 | - if (count($trustedList) > 0) { |
|
| 911 | - return reset($trustedList); |
|
| 912 | - } |
|
| 913 | - |
|
| 914 | - return ''; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - /** |
|
| 918 | - * Returns the overwritehost setting from the config if set and |
|
| 919 | - * if the overwrite condition is met |
|
| 920 | - * @return string|null overwritehost value or null if not defined or the defined condition |
|
| 921 | - * isn't met |
|
| 922 | - */ |
|
| 923 | - private function getOverwriteHost() { |
|
| 924 | - if($this->config->getSystemValue('overwritehost') !== '' && $this->isOverwriteCondition()) { |
|
| 925 | - return $this->config->getSystemValue('overwritehost'); |
|
| 926 | - } |
|
| 927 | - return null; |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - private function fromTrustedProxy(): bool { |
|
| 931 | - $remoteAddress = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
|
| 932 | - $trustedProxies = $this->config->getSystemValue('trusted_proxies', []); |
|
| 933 | - |
|
| 934 | - return \is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress); |
|
| 935 | - } |
|
| 68 | + const USER_AGENT_IE = '/(MSIE)|(Trident)/'; |
|
| 69 | + // Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx |
|
| 70 | + const USER_AGENT_MS_EDGE = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+ Edge\/[0-9.]+$/'; |
|
| 71 | + // Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference |
|
| 72 | + const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/'; |
|
| 73 | + // Chrome User Agent from https://developer.chrome.com/multidevice/user-agent |
|
| 74 | + const USER_AGENT_CHROME = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\)( Ubuntu Chromium\/[0-9.]+|) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+( (Vivaldi|Brave|OPR)\/[0-9.]+|)$/'; |
|
| 75 | + // Safari User Agent from http://www.useragentstring.com/pages/Safari/ |
|
| 76 | + const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/'; |
|
| 77 | + // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent |
|
| 78 | + const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; |
|
| 79 | + const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#'; |
|
| 80 | + const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost|::1)$/'; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_IOS instead |
|
| 84 | + */ |
|
| 85 | + const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/'; |
|
| 86 | + /** |
|
| 87 | + * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_ANDROID instead |
|
| 88 | + */ |
|
| 89 | + const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/'; |
|
| 90 | + /** |
|
| 91 | + * @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_DESKTOP instead |
|
| 92 | + */ |
|
| 93 | + const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
|
| 94 | + |
|
| 95 | + protected $inputStream; |
|
| 96 | + protected $content; |
|
| 97 | + protected $items = []; |
|
| 98 | + protected $allowedKeys = [ |
|
| 99 | + 'get', |
|
| 100 | + 'post', |
|
| 101 | + 'files', |
|
| 102 | + 'server', |
|
| 103 | + 'env', |
|
| 104 | + 'cookies', |
|
| 105 | + 'urlParams', |
|
| 106 | + 'parameters', |
|
| 107 | + 'method', |
|
| 108 | + 'requesttoken', |
|
| 109 | + ]; |
|
| 110 | + /** @var ISecureRandom */ |
|
| 111 | + protected $secureRandom; |
|
| 112 | + /** @var IConfig */ |
|
| 113 | + protected $config; |
|
| 114 | + /** @var string */ |
|
| 115 | + protected $requestId = ''; |
|
| 116 | + /** @var ICrypto */ |
|
| 117 | + protected $crypto; |
|
| 118 | + /** @var CsrfTokenManager|null */ |
|
| 119 | + protected $csrfTokenManager; |
|
| 120 | + |
|
| 121 | + /** @var bool */ |
|
| 122 | + protected $contentDecoded = false; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @param array $vars An associative array with the following optional values: |
|
| 126 | + * - array 'urlParams' the parameters which were matched from the URL |
|
| 127 | + * - array 'get' the $_GET array |
|
| 128 | + * - array|string 'post' the $_POST array or JSON string |
|
| 129 | + * - array 'files' the $_FILES array |
|
| 130 | + * - array 'server' the $_SERVER array |
|
| 131 | + * - array 'env' the $_ENV array |
|
| 132 | + * - array 'cookies' the $_COOKIE array |
|
| 133 | + * - string 'method' the request method (GET, POST etc) |
|
| 134 | + * - string|false 'requesttoken' the requesttoken or false when not available |
|
| 135 | + * @param ISecureRandom $secureRandom |
|
| 136 | + * @param IConfig $config |
|
| 137 | + * @param CsrfTokenManager|null $csrfTokenManager |
|
| 138 | + * @param string $stream |
|
| 139 | + * @see http://www.php.net/manual/en/reserved.variables.php |
|
| 140 | + */ |
|
| 141 | + public function __construct(array $vars= [], |
|
| 142 | + ISecureRandom $secureRandom = null, |
|
| 143 | + IConfig $config, |
|
| 144 | + CsrfTokenManager $csrfTokenManager = null, |
|
| 145 | + string $stream = 'php://input') { |
|
| 146 | + $this->inputStream = $stream; |
|
| 147 | + $this->items['params'] = []; |
|
| 148 | + $this->secureRandom = $secureRandom; |
|
| 149 | + $this->config = $config; |
|
| 150 | + $this->csrfTokenManager = $csrfTokenManager; |
|
| 151 | + |
|
| 152 | + if(!array_key_exists('method', $vars)) { |
|
| 153 | + $vars['method'] = 'GET'; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + foreach($this->allowedKeys as $name) { |
|
| 157 | + $this->items[$name] = isset($vars[$name]) |
|
| 158 | + ? $vars[$name] |
|
| 159 | + : []; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $this->items['parameters'] = array_merge( |
|
| 163 | + $this->items['get'], |
|
| 164 | + $this->items['post'], |
|
| 165 | + $this->items['urlParams'], |
|
| 166 | + $this->items['params'] |
|
| 167 | + ); |
|
| 168 | + |
|
| 169 | + } |
|
| 170 | + /** |
|
| 171 | + * @param array $parameters |
|
| 172 | + */ |
|
| 173 | + public function setUrlParameters(array $parameters) { |
|
| 174 | + $this->items['urlParams'] = $parameters; |
|
| 175 | + $this->items['parameters'] = array_merge( |
|
| 176 | + $this->items['parameters'], |
|
| 177 | + $this->items['urlParams'] |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Countable method |
|
| 183 | + * @return int |
|
| 184 | + */ |
|
| 185 | + public function count(): int { |
|
| 186 | + return \count($this->items['parameters']); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * ArrayAccess methods |
|
| 191 | + * |
|
| 192 | + * Gives access to the combined GET, POST and urlParams arrays |
|
| 193 | + * |
|
| 194 | + * Examples: |
|
| 195 | + * |
|
| 196 | + * $var = $request['myvar']; |
|
| 197 | + * |
|
| 198 | + * or |
|
| 199 | + * |
|
| 200 | + * if(!isset($request['myvar']) { |
|
| 201 | + * // Do something |
|
| 202 | + * } |
|
| 203 | + * |
|
| 204 | + * $request['myvar'] = 'something'; // This throws an exception. |
|
| 205 | + * |
|
| 206 | + * @param string $offset The key to lookup |
|
| 207 | + * @return boolean |
|
| 208 | + */ |
|
| 209 | + public function offsetExists($offset): bool { |
|
| 210 | + return isset($this->items['parameters'][$offset]); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @see offsetExists |
|
| 215 | + * @param string $offset |
|
| 216 | + * @return mixed |
|
| 217 | + */ |
|
| 218 | + public function offsetGet($offset) { |
|
| 219 | + return isset($this->items['parameters'][$offset]) |
|
| 220 | + ? $this->items['parameters'][$offset] |
|
| 221 | + : null; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @see offsetExists |
|
| 226 | + * @param string $offset |
|
| 227 | + * @param mixed $value |
|
| 228 | + */ |
|
| 229 | + public function offsetSet($offset, $value) { |
|
| 230 | + throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @see offsetExists |
|
| 235 | + * @param string $offset |
|
| 236 | + */ |
|
| 237 | + public function offsetUnset($offset) { |
|
| 238 | + throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Magic property accessors |
|
| 243 | + * @param string $name |
|
| 244 | + * @param mixed $value |
|
| 245 | + */ |
|
| 246 | + public function __set($name, $value) { |
|
| 247 | + throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Access request variables by method and name. |
|
| 252 | + * Examples: |
|
| 253 | + * |
|
| 254 | + * $request->post['myvar']; // Only look for POST variables |
|
| 255 | + * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} |
|
| 256 | + * Looks in the combined GET, POST and urlParams array. |
|
| 257 | + * |
|
| 258 | + * If you access e.g. ->post but the current HTTP request method |
|
| 259 | + * is GET a \LogicException will be thrown. |
|
| 260 | + * |
|
| 261 | + * @param string $name The key to look for. |
|
| 262 | + * @throws \LogicException |
|
| 263 | + * @return mixed|null |
|
| 264 | + */ |
|
| 265 | + public function __get($name) { |
|
| 266 | + switch($name) { |
|
| 267 | + case 'put': |
|
| 268 | + case 'patch': |
|
| 269 | + case 'get': |
|
| 270 | + case 'post': |
|
| 271 | + if($this->method !== strtoupper($name)) { |
|
| 272 | + throw new \LogicException(sprintf('%s cannot be accessed in a %s request.', $name, $this->method)); |
|
| 273 | + } |
|
| 274 | + return $this->getContent(); |
|
| 275 | + case 'files': |
|
| 276 | + case 'server': |
|
| 277 | + case 'env': |
|
| 278 | + case 'cookies': |
|
| 279 | + case 'urlParams': |
|
| 280 | + case 'method': |
|
| 281 | + return isset($this->items[$name]) |
|
| 282 | + ? $this->items[$name] |
|
| 283 | + : null; |
|
| 284 | + case 'parameters': |
|
| 285 | + case 'params': |
|
| 286 | + return $this->getContent(); |
|
| 287 | + default; |
|
| 288 | + return isset($this[$name]) |
|
| 289 | + ? $this[$name] |
|
| 290 | + : null; |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * @param string $name |
|
| 296 | + * @return bool |
|
| 297 | + */ |
|
| 298 | + public function __isset($name) { |
|
| 299 | + if (\in_array($name, $this->allowedKeys, true)) { |
|
| 300 | + return true; |
|
| 301 | + } |
|
| 302 | + return isset($this->items['parameters'][$name]); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * @param string $id |
|
| 307 | + */ |
|
| 308 | + public function __unset($id) { |
|
| 309 | + throw new \RuntimeException('You cannot change the contents of the request object'); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Returns the value for a specific http header. |
|
| 314 | + * |
|
| 315 | + * This method returns null if the header did not exist. |
|
| 316 | + * |
|
| 317 | + * @param string $name |
|
| 318 | + * @return string |
|
| 319 | + */ |
|
| 320 | + public function getHeader(string $name): string { |
|
| 321 | + |
|
| 322 | + $name = strtoupper(str_replace('-', '_',$name)); |
|
| 323 | + if (isset($this->server['HTTP_' . $name])) { |
|
| 324 | + return $this->server['HTTP_' . $name]; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // There's a few headers that seem to end up in the top-level |
|
| 328 | + // server array. |
|
| 329 | + switch ($name) { |
|
| 330 | + case 'CONTENT_TYPE' : |
|
| 331 | + case 'CONTENT_LENGTH' : |
|
| 332 | + case 'REMOTE_ADDR': |
|
| 333 | + if (isset($this->server[$name])) { |
|
| 334 | + return $this->server[$name]; |
|
| 335 | + } |
|
| 336 | + break; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + return ''; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Lets you access post and get parameters by the index |
|
| 344 | + * In case of json requests the encoded json body is accessed |
|
| 345 | + * |
|
| 346 | + * @param string $key the key which you want to access in the URL Parameter |
|
| 347 | + * placeholder, $_POST or $_GET array. |
|
| 348 | + * The priority how they're returned is the following: |
|
| 349 | + * 1. URL parameters |
|
| 350 | + * 2. POST parameters |
|
| 351 | + * 3. GET parameters |
|
| 352 | + * @param mixed $default If the key is not found, this value will be returned |
|
| 353 | + * @return mixed the content of the array |
|
| 354 | + */ |
|
| 355 | + public function getParam(string $key, $default = null) { |
|
| 356 | + return isset($this->parameters[$key]) |
|
| 357 | + ? $this->parameters[$key] |
|
| 358 | + : $default; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Returns all params that were received, be it from the request |
|
| 363 | + * (as GET or POST) or throuh the URL by the route |
|
| 364 | + * @return array the array with all parameters |
|
| 365 | + */ |
|
| 366 | + public function getParams(): array { |
|
| 367 | + return is_array($this->parameters) ? $this->parameters : []; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Returns the method of the request |
|
| 372 | + * @return string the method of the request (POST, GET, etc) |
|
| 373 | + */ |
|
| 374 | + public function getMethod(): string { |
|
| 375 | + return $this->method; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Shortcut for accessing an uploaded file through the $_FILES array |
|
| 380 | + * @param string $key the key that will be taken from the $_FILES array |
|
| 381 | + * @return array the file in the $_FILES element |
|
| 382 | + */ |
|
| 383 | + public function getUploadedFile(string $key) { |
|
| 384 | + return isset($this->files[$key]) ? $this->files[$key] : null; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Shortcut for getting env variables |
|
| 389 | + * @param string $key the key that will be taken from the $_ENV array |
|
| 390 | + * @return array the value in the $_ENV element |
|
| 391 | + */ |
|
| 392 | + public function getEnv(string $key) { |
|
| 393 | + return isset($this->env[$key]) ? $this->env[$key] : null; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Shortcut for getting cookie variables |
|
| 398 | + * @param string $key the key that will be taken from the $_COOKIE array |
|
| 399 | + * @return string the value in the $_COOKIE element |
|
| 400 | + */ |
|
| 401 | + public function getCookie(string $key) { |
|
| 402 | + return isset($this->cookies[$key]) ? $this->cookies[$key] : null; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Returns the request body content. |
|
| 407 | + * |
|
| 408 | + * If the HTTP request method is PUT and the body |
|
| 409 | + * not application/x-www-form-urlencoded or application/json a stream |
|
| 410 | + * resource is returned, otherwise an array. |
|
| 411 | + * |
|
| 412 | + * @return array|string|resource The request body content or a resource to read the body stream. |
|
| 413 | + * |
|
| 414 | + * @throws \LogicException |
|
| 415 | + */ |
|
| 416 | + protected function getContent() { |
|
| 417 | + // If the content can't be parsed into an array then return a stream resource. |
|
| 418 | + if ($this->method === 'PUT' |
|
| 419 | + && $this->getHeader('Content-Length') !== '0' |
|
| 420 | + && $this->getHeader('Content-Length') !== '' |
|
| 421 | + && strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false |
|
| 422 | + && strpos($this->getHeader('Content-Type'), 'application/json') === false |
|
| 423 | + ) { |
|
| 424 | + if ($this->content === false) { |
|
| 425 | + throw new \LogicException( |
|
| 426 | + '"put" can only be accessed once if not ' |
|
| 427 | + . 'application/x-www-form-urlencoded or application/json.' |
|
| 428 | + ); |
|
| 429 | + } |
|
| 430 | + $this->content = false; |
|
| 431 | + return fopen($this->inputStream, 'rb'); |
|
| 432 | + } else { |
|
| 433 | + $this->decodeContent(); |
|
| 434 | + return $this->items['parameters']; |
|
| 435 | + } |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Attempt to decode the content and populate parameters |
|
| 440 | + */ |
|
| 441 | + protected function decodeContent() { |
|
| 442 | + if ($this->contentDecoded) { |
|
| 443 | + return; |
|
| 444 | + } |
|
| 445 | + $params = []; |
|
| 446 | + |
|
| 447 | + // 'application/json' must be decoded manually. |
|
| 448 | + if (strpos($this->getHeader('Content-Type'), 'application/json') !== false) { |
|
| 449 | + $params = json_decode(file_get_contents($this->inputStream), true); |
|
| 450 | + if($params !== null && \count($params) > 0) { |
|
| 451 | + $this->items['params'] = $params; |
|
| 452 | + if($this->method === 'POST') { |
|
| 453 | + $this->items['post'] = $params; |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + // Handle application/x-www-form-urlencoded for methods other than GET |
|
| 458 | + // or post correctly |
|
| 459 | + } elseif($this->method !== 'GET' |
|
| 460 | + && $this->method !== 'POST' |
|
| 461 | + && strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') !== false) { |
|
| 462 | + |
|
| 463 | + parse_str(file_get_contents($this->inputStream), $params); |
|
| 464 | + if(\is_array($params)) { |
|
| 465 | + $this->items['params'] = $params; |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + if (\is_array($params)) { |
|
| 470 | + $this->items['parameters'] = array_merge($this->items['parameters'], $params); |
|
| 471 | + } |
|
| 472 | + $this->contentDecoded = true; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Checks if the CSRF check was correct |
|
| 478 | + * @return bool true if CSRF check passed |
|
| 479 | + */ |
|
| 480 | + public function passesCSRFCheck(): bool { |
|
| 481 | + if($this->csrfTokenManager === null) { |
|
| 482 | + return false; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + if(!$this->passesStrictCookieCheck()) { |
|
| 486 | + return false; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + if (isset($this->items['get']['requesttoken'])) { |
|
| 490 | + $token = $this->items['get']['requesttoken']; |
|
| 491 | + } elseif (isset($this->items['post']['requesttoken'])) { |
|
| 492 | + $token = $this->items['post']['requesttoken']; |
|
| 493 | + } elseif (isset($this->items['server']['HTTP_REQUESTTOKEN'])) { |
|
| 494 | + $token = $this->items['server']['HTTP_REQUESTTOKEN']; |
|
| 495 | + } else { |
|
| 496 | + //no token found. |
|
| 497 | + return false; |
|
| 498 | + } |
|
| 499 | + $token = new CsrfToken($token); |
|
| 500 | + |
|
| 501 | + return $this->csrfTokenManager->isTokenValid($token); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Whether the cookie checks are required |
|
| 506 | + * |
|
| 507 | + * @return bool |
|
| 508 | + */ |
|
| 509 | + private function cookieCheckRequired(): bool { |
|
| 510 | + if ($this->getHeader('OCS-APIREQUEST')) { |
|
| 511 | + return false; |
|
| 512 | + } |
|
| 513 | + if($this->getCookie(session_name()) === null && $this->getCookie('nc_token') === null) { |
|
| 514 | + return false; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + return true; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Wrapper around session_get_cookie_params |
|
| 522 | + * |
|
| 523 | + * @return array |
|
| 524 | + */ |
|
| 525 | + public function getCookieParams(): array { |
|
| 526 | + return session_get_cookie_params(); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Appends the __Host- prefix to the cookie if applicable |
|
| 531 | + * |
|
| 532 | + * @param string $name |
|
| 533 | + * @return string |
|
| 534 | + */ |
|
| 535 | + protected function getProtectedCookieName(string $name): string { |
|
| 536 | + $cookieParams = $this->getCookieParams(); |
|
| 537 | + $prefix = ''; |
|
| 538 | + if($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
| 539 | + $prefix = '__Host-'; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + return $prefix.$name; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Checks if the strict cookie has been sent with the request if the request |
|
| 547 | + * is including any cookies. |
|
| 548 | + * |
|
| 549 | + * @return bool |
|
| 550 | + * @since 9.1.0 |
|
| 551 | + */ |
|
| 552 | + public function passesStrictCookieCheck(): bool { |
|
| 553 | + if(!$this->cookieCheckRequired()) { |
|
| 554 | + return true; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + $cookieName = $this->getProtectedCookieName('nc_sameSiteCookiestrict'); |
|
| 558 | + if($this->getCookie($cookieName) === 'true' |
|
| 559 | + && $this->passesLaxCookieCheck()) { |
|
| 560 | + return true; |
|
| 561 | + } |
|
| 562 | + return false; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + /** |
|
| 566 | + * Checks if the lax cookie has been sent with the request if the request |
|
| 567 | + * is including any cookies. |
|
| 568 | + * |
|
| 569 | + * @return bool |
|
| 570 | + * @since 9.1.0 |
|
| 571 | + */ |
|
| 572 | + public function passesLaxCookieCheck(): bool { |
|
| 573 | + if(!$this->cookieCheckRequired()) { |
|
| 574 | + return true; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + $cookieName = $this->getProtectedCookieName('nc_sameSiteCookielax'); |
|
| 578 | + if($this->getCookie($cookieName) === 'true') { |
|
| 579 | + return true; |
|
| 580 | + } |
|
| 581 | + return false; |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
|
| 587 | + * If `mod_unique_id` is installed this value will be taken. |
|
| 588 | + * @return string |
|
| 589 | + */ |
|
| 590 | + public function getId(): string { |
|
| 591 | + if(isset($this->server['UNIQUE_ID'])) { |
|
| 592 | + return $this->server['UNIQUE_ID']; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + if(empty($this->requestId)) { |
|
| 596 | + $validChars = ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS; |
|
| 597 | + $this->requestId = $this->secureRandom->generate(20, $validChars); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + return $this->requestId; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Checks if given $remoteAddress matches given $trustedProxy. |
|
| 605 | + * If $trustedProxy is an IPv4 IP range given in CIDR notation, true will be returned if |
|
| 606 | + * $remoteAddress is an IPv4 address within that IP range. |
|
| 607 | + * Otherwise $remoteAddress will be compared to $trustedProxy literally and the result |
|
| 608 | + * will be returned. |
|
| 609 | + * @return boolean true if $remoteAddress matches $trustedProxy, false otherwise |
|
| 610 | + */ |
|
| 611 | + protected function matchesTrustedProxy($trustedProxy, $remoteAddress) { |
|
| 612 | + $cidrre = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})$/'; |
|
| 613 | + |
|
| 614 | + if (preg_match($cidrre, $trustedProxy, $match)) { |
|
| 615 | + $net = $match[1]; |
|
| 616 | + $shiftbits = min(32, max(0, 32 - intval($match[2]))); |
|
| 617 | + $netnum = ip2long($net) >> $shiftbits; |
|
| 618 | + $ipnum = ip2long($remoteAddress) >> $shiftbits; |
|
| 619 | + |
|
| 620 | + return $ipnum === $netnum; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + return $trustedProxy === $remoteAddress; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * Checks if given $remoteAddress matches any entry in the given array $trustedProxies. |
|
| 628 | + * For details regarding what "match" means, refer to `matchesTrustedProxy`. |
|
| 629 | + * @return boolean true if $remoteAddress matches any entry in $trustedProxies, false otherwise |
|
| 630 | + */ |
|
| 631 | + protected function isTrustedProxy($trustedProxies, $remoteAddress) { |
|
| 632 | + foreach ($trustedProxies as $tp) { |
|
| 633 | + if ($this->matchesTrustedProxy($tp, $remoteAddress)) { |
|
| 634 | + return true; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + return false; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * Returns the remote address, if the connection came from a trusted proxy |
|
| 643 | + * and `forwarded_for_headers` has been configured then the IP address |
|
| 644 | + * specified in this header will be returned instead. |
|
| 645 | + * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
|
| 646 | + * @return string IP address |
|
| 647 | + */ |
|
| 648 | + public function getRemoteAddress(): string { |
|
| 649 | + $remoteAddress = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
|
| 650 | + $trustedProxies = $this->config->getSystemValue('trusted_proxies', []); |
|
| 651 | + |
|
| 652 | + if(\is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress)) { |
|
| 653 | + $forwardedForHeaders = $this->config->getSystemValue('forwarded_for_headers', [ |
|
| 654 | + 'HTTP_X_FORWARDED_FOR' |
|
| 655 | + // only have one default, so we cannot ship an insecure product out of the box |
|
| 656 | + ]); |
|
| 657 | + |
|
| 658 | + foreach($forwardedForHeaders as $header) { |
|
| 659 | + if(isset($this->server[$header])) { |
|
| 660 | + foreach(explode(',', $this->server[$header]) as $IP) { |
|
| 661 | + $IP = trim($IP); |
|
| 662 | + if (filter_var($IP, FILTER_VALIDATE_IP) !== false) { |
|
| 663 | + return $IP; |
|
| 664 | + } |
|
| 665 | + } |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + return $remoteAddress; |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Check overwrite condition |
|
| 675 | + * @param string $type |
|
| 676 | + * @return bool |
|
| 677 | + */ |
|
| 678 | + private function isOverwriteCondition(string $type = ''): bool { |
|
| 679 | + $regex = '/' . $this->config->getSystemValue('overwritecondaddr', '') . '/'; |
|
| 680 | + $remoteAddr = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
|
| 681 | + return $regex === '//' || preg_match($regex, $remoteAddr) === 1 |
|
| 682 | + || $type !== 'protocol'; |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * Returns the server protocol. It respects one or more reverse proxies servers |
|
| 687 | + * and load balancers |
|
| 688 | + * @return string Server protocol (http or https) |
|
| 689 | + */ |
|
| 690 | + public function getServerProtocol(): string { |
|
| 691 | + if($this->config->getSystemValue('overwriteprotocol') !== '' |
|
| 692 | + && $this->isOverwriteCondition('protocol')) { |
|
| 693 | + return $this->config->getSystemValue('overwriteprotocol'); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + if ($this->fromTrustedProxy() && isset($this->server['HTTP_X_FORWARDED_PROTO'])) { |
|
| 697 | + if (strpos($this->server['HTTP_X_FORWARDED_PROTO'], ',') !== false) { |
|
| 698 | + $parts = explode(',', $this->server['HTTP_X_FORWARDED_PROTO']); |
|
| 699 | + $proto = strtolower(trim($parts[0])); |
|
| 700 | + } else { |
|
| 701 | + $proto = strtolower($this->server['HTTP_X_FORWARDED_PROTO']); |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + // Verify that the protocol is always HTTP or HTTPS |
|
| 705 | + // default to http if an invalid value is provided |
|
| 706 | + return $proto === 'https' ? 'https' : 'http'; |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + if (isset($this->server['HTTPS']) |
|
| 710 | + && $this->server['HTTPS'] !== null |
|
| 711 | + && $this->server['HTTPS'] !== 'off' |
|
| 712 | + && $this->server['HTTPS'] !== '') { |
|
| 713 | + return 'https'; |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + return 'http'; |
|
| 717 | + } |
|
| 718 | + |
|
| 719 | + /** |
|
| 720 | + * Returns the used HTTP protocol. |
|
| 721 | + * |
|
| 722 | + * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
|
| 723 | + */ |
|
| 724 | + public function getHttpProtocol(): string { |
|
| 725 | + $claimedProtocol = $this->server['SERVER_PROTOCOL']; |
|
| 726 | + |
|
| 727 | + if (\is_string($claimedProtocol)) { |
|
| 728 | + $claimedProtocol = strtoupper($claimedProtocol); |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + $validProtocols = [ |
|
| 732 | + 'HTTP/1.0', |
|
| 733 | + 'HTTP/1.1', |
|
| 734 | + 'HTTP/2', |
|
| 735 | + ]; |
|
| 736 | + |
|
| 737 | + if(\in_array($claimedProtocol, $validProtocols, true)) { |
|
| 738 | + return $claimedProtocol; |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + return 'HTTP/1.1'; |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + /** |
|
| 745 | + * Returns the request uri, even if the website uses one or more |
|
| 746 | + * reverse proxies |
|
| 747 | + * @return string |
|
| 748 | + */ |
|
| 749 | + public function getRequestUri(): string { |
|
| 750 | + $uri = isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : ''; |
|
| 751 | + if($this->config->getSystemValue('overwritewebroot') !== '' && $this->isOverwriteCondition()) { |
|
| 752 | + $uri = $this->getScriptName() . substr($uri, \strlen($this->server['SCRIPT_NAME'])); |
|
| 753 | + } |
|
| 754 | + return $uri; |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * Get raw PathInfo from request (not urldecoded) |
|
| 759 | + * @throws \Exception |
|
| 760 | + * @return string Path info |
|
| 761 | + */ |
|
| 762 | + public function getRawPathInfo(): string { |
|
| 763 | + $requestUri = isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : ''; |
|
| 764 | + // remove too many slashes - can be caused by reverse proxy configuration |
|
| 765 | + $requestUri = preg_replace('%/{2,}%', '/', $requestUri); |
|
| 766 | + |
|
| 767 | + // Remove the query string from REQUEST_URI |
|
| 768 | + if ($pos = strpos($requestUri, '?')) { |
|
| 769 | + $requestUri = substr($requestUri, 0, $pos); |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + $scriptName = $this->server['SCRIPT_NAME']; |
|
| 773 | + $pathInfo = $requestUri; |
|
| 774 | + |
|
| 775 | + // strip off the script name's dir and file name |
|
| 776 | + // FIXME: Sabre does not really belong here |
|
| 777 | + list($path, $name) = \Sabre\Uri\split($scriptName); |
|
| 778 | + if (!empty($path)) { |
|
| 779 | + if($path === $pathInfo || strpos($pathInfo, $path.'/') === 0) { |
|
| 780 | + $pathInfo = substr($pathInfo, \strlen($path)); |
|
| 781 | + } else { |
|
| 782 | + throw new \Exception("The requested uri($requestUri) cannot be processed by the script '$scriptName')"); |
|
| 783 | + } |
|
| 784 | + } |
|
| 785 | + if ($name === null) { |
|
| 786 | + $name = ''; |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + if (strpos($pathInfo, '/'.$name) === 0) { |
|
| 790 | + $pathInfo = substr($pathInfo, \strlen($name) + 1); |
|
| 791 | + } |
|
| 792 | + if ($name !== '' && strpos($pathInfo, $name) === 0) { |
|
| 793 | + $pathInfo = substr($pathInfo, \strlen($name)); |
|
| 794 | + } |
|
| 795 | + if($pathInfo === false || $pathInfo === '/'){ |
|
| 796 | + return ''; |
|
| 797 | + } else { |
|
| 798 | + return $pathInfo; |
|
| 799 | + } |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + /** |
|
| 803 | + * Get PathInfo from request |
|
| 804 | + * @throws \Exception |
|
| 805 | + * @return string|false Path info or false when not found |
|
| 806 | + */ |
|
| 807 | + public function getPathInfo() { |
|
| 808 | + $pathInfo = $this->getRawPathInfo(); |
|
| 809 | + // following is taken from \Sabre\HTTP\URLUtil::decodePathSegment |
|
| 810 | + $pathInfo = rawurldecode($pathInfo); |
|
| 811 | + $encoding = mb_detect_encoding($pathInfo, ['UTF-8', 'ISO-8859-1']); |
|
| 812 | + |
|
| 813 | + switch($encoding) { |
|
| 814 | + case 'ISO-8859-1' : |
|
| 815 | + $pathInfo = utf8_encode($pathInfo); |
|
| 816 | + } |
|
| 817 | + // end copy |
|
| 818 | + |
|
| 819 | + return $pathInfo; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + /** |
|
| 823 | + * Returns the script name, even if the website uses one or more |
|
| 824 | + * reverse proxies |
|
| 825 | + * @return string the script name |
|
| 826 | + */ |
|
| 827 | + public function getScriptName(): string { |
|
| 828 | + $name = $this->server['SCRIPT_NAME']; |
|
| 829 | + $overwriteWebRoot = $this->config->getSystemValue('overwritewebroot'); |
|
| 830 | + if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) { |
|
| 831 | + // FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous |
|
| 832 | + $serverRoot = str_replace('\\', '/', substr(__DIR__, 0, -\strlen('lib/private/appframework/http/'))); |
|
| 833 | + $suburi = str_replace('\\', '/', substr(realpath($this->server['SCRIPT_FILENAME']), \strlen($serverRoot))); |
|
| 834 | + $name = '/' . ltrim($overwriteWebRoot . $suburi, '/'); |
|
| 835 | + } |
|
| 836 | + return $name; |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + /** |
|
| 840 | + * Checks whether the user agent matches a given regex |
|
| 841 | + * @param array $agent array of agent names |
|
| 842 | + * @return bool true if at least one of the given agent matches, false otherwise |
|
| 843 | + */ |
|
| 844 | + public function isUserAgent(array $agent): bool { |
|
| 845 | + if (!isset($this->server['HTTP_USER_AGENT'])) { |
|
| 846 | + return false; |
|
| 847 | + } |
|
| 848 | + foreach ($agent as $regex) { |
|
| 849 | + if (preg_match($regex, $this->server['HTTP_USER_AGENT'])) { |
|
| 850 | + return true; |
|
| 851 | + } |
|
| 852 | + } |
|
| 853 | + return false; |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * Returns the unverified server host from the headers without checking |
|
| 858 | + * whether it is a trusted domain |
|
| 859 | + * @return string Server host |
|
| 860 | + */ |
|
| 861 | + public function getInsecureServerHost(): string { |
|
| 862 | + if ($this->fromTrustedProxy() && $this->getOverwriteHost() !== null) { |
|
| 863 | + return $this->getOverwriteHost(); |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + $host = 'localhost'; |
|
| 867 | + if ($this->fromTrustedProxy() && isset($this->server['HTTP_X_FORWARDED_HOST'])) { |
|
| 868 | + if (strpos($this->server['HTTP_X_FORWARDED_HOST'], ',') !== false) { |
|
| 869 | + $parts = explode(',', $this->server['HTTP_X_FORWARDED_HOST']); |
|
| 870 | + $host = trim(current($parts)); |
|
| 871 | + } else { |
|
| 872 | + $host = $this->server['HTTP_X_FORWARDED_HOST']; |
|
| 873 | + } |
|
| 874 | + } else { |
|
| 875 | + if (isset($this->server['HTTP_HOST'])) { |
|
| 876 | + $host = $this->server['HTTP_HOST']; |
|
| 877 | + } else if (isset($this->server['SERVER_NAME'])) { |
|
| 878 | + $host = $this->server['SERVER_NAME']; |
|
| 879 | + } |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + return $host; |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + |
|
| 886 | + /** |
|
| 887 | + * Returns the server host from the headers, or the first configured |
|
| 888 | + * trusted domain if the host isn't in the trusted list |
|
| 889 | + * @return string Server host |
|
| 890 | + */ |
|
| 891 | + public function getServerHost(): string { |
|
| 892 | + // overwritehost is always trusted |
|
| 893 | + $host = $this->getOverwriteHost(); |
|
| 894 | + if ($host !== null) { |
|
| 895 | + return $host; |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + // get the host from the headers |
|
| 899 | + $host = $this->getInsecureServerHost(); |
|
| 900 | + |
|
| 901 | + // Verify that the host is a trusted domain if the trusted domains |
|
| 902 | + // are defined |
|
| 903 | + // If no trusted domain is provided the first trusted domain is returned |
|
| 904 | + $trustedDomainHelper = new TrustedDomainHelper($this->config); |
|
| 905 | + if ($trustedDomainHelper->isTrustedDomain($host)) { |
|
| 906 | + return $host; |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + $trustedList = (array)$this->config->getSystemValue('trusted_domains', []); |
|
| 910 | + if (count($trustedList) > 0) { |
|
| 911 | + return reset($trustedList); |
|
| 912 | + } |
|
| 913 | + |
|
| 914 | + return ''; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + /** |
|
| 918 | + * Returns the overwritehost setting from the config if set and |
|
| 919 | + * if the overwrite condition is met |
|
| 920 | + * @return string|null overwritehost value or null if not defined or the defined condition |
|
| 921 | + * isn't met |
|
| 922 | + */ |
|
| 923 | + private function getOverwriteHost() { |
|
| 924 | + if($this->config->getSystemValue('overwritehost') !== '' && $this->isOverwriteCondition()) { |
|
| 925 | + return $this->config->getSystemValue('overwritehost'); |
|
| 926 | + } |
|
| 927 | + return null; |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + private function fromTrustedProxy(): bool { |
|
| 931 | + $remoteAddress = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
|
| 932 | + $trustedProxies = $this->config->getSystemValue('trusted_proxies', []); |
|
| 933 | + |
|
| 934 | + return \is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress); |
|
| 935 | + } |
|
| 936 | 936 | } |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @param string $stream |
| 139 | 139 | * @see http://www.php.net/manual/en/reserved.variables.php |
| 140 | 140 | */ |
| 141 | - public function __construct(array $vars= [], |
|
| 141 | + public function __construct(array $vars = [], |
|
| 142 | 142 | ISecureRandom $secureRandom = null, |
| 143 | 143 | IConfig $config, |
| 144 | 144 | CsrfTokenManager $csrfTokenManager = null, |
@@ -149,11 +149,11 @@ discard block |
||
| 149 | 149 | $this->config = $config; |
| 150 | 150 | $this->csrfTokenManager = $csrfTokenManager; |
| 151 | 151 | |
| 152 | - if(!array_key_exists('method', $vars)) { |
|
| 152 | + if (!array_key_exists('method', $vars)) { |
|
| 153 | 153 | $vars['method'] = 'GET'; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - foreach($this->allowedKeys as $name) { |
|
| 156 | + foreach ($this->allowedKeys as $name) { |
|
| 157 | 157 | $this->items[$name] = isset($vars[$name]) |
| 158 | 158 | ? $vars[$name] |
| 159 | 159 | : []; |
@@ -263,12 +263,12 @@ discard block |
||
| 263 | 263 | * @return mixed|null |
| 264 | 264 | */ |
| 265 | 265 | public function __get($name) { |
| 266 | - switch($name) { |
|
| 266 | + switch ($name) { |
|
| 267 | 267 | case 'put': |
| 268 | 268 | case 'patch': |
| 269 | 269 | case 'get': |
| 270 | 270 | case 'post': |
| 271 | - if($this->method !== strtoupper($name)) { |
|
| 271 | + if ($this->method !== strtoupper($name)) { |
|
| 272 | 272 | throw new \LogicException(sprintf('%s cannot be accessed in a %s request.', $name, $this->method)); |
| 273 | 273 | } |
| 274 | 274 | return $this->getContent(); |
@@ -319,9 +319,9 @@ discard block |
||
| 319 | 319 | */ |
| 320 | 320 | public function getHeader(string $name): string { |
| 321 | 321 | |
| 322 | - $name = strtoupper(str_replace('-', '_',$name)); |
|
| 323 | - if (isset($this->server['HTTP_' . $name])) { |
|
| 324 | - return $this->server['HTTP_' . $name]; |
|
| 322 | + $name = strtoupper(str_replace('-', '_', $name)); |
|
| 323 | + if (isset($this->server['HTTP_'.$name])) { |
|
| 324 | + return $this->server['HTTP_'.$name]; |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | // There's a few headers that seem to end up in the top-level |
@@ -447,21 +447,21 @@ discard block |
||
| 447 | 447 | // 'application/json' must be decoded manually. |
| 448 | 448 | if (strpos($this->getHeader('Content-Type'), 'application/json') !== false) { |
| 449 | 449 | $params = json_decode(file_get_contents($this->inputStream), true); |
| 450 | - if($params !== null && \count($params) > 0) { |
|
| 450 | + if ($params !== null && \count($params) > 0) { |
|
| 451 | 451 | $this->items['params'] = $params; |
| 452 | - if($this->method === 'POST') { |
|
| 452 | + if ($this->method === 'POST') { |
|
| 453 | 453 | $this->items['post'] = $params; |
| 454 | 454 | } |
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | // Handle application/x-www-form-urlencoded for methods other than GET |
| 458 | 458 | // or post correctly |
| 459 | - } elseif($this->method !== 'GET' |
|
| 459 | + } elseif ($this->method !== 'GET' |
|
| 460 | 460 | && $this->method !== 'POST' |
| 461 | 461 | && strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') !== false) { |
| 462 | 462 | |
| 463 | 463 | parse_str(file_get_contents($this->inputStream), $params); |
| 464 | - if(\is_array($params)) { |
|
| 464 | + if (\is_array($params)) { |
|
| 465 | 465 | $this->items['params'] = $params; |
| 466 | 466 | } |
| 467 | 467 | } |
@@ -478,11 +478,11 @@ discard block |
||
| 478 | 478 | * @return bool true if CSRF check passed |
| 479 | 479 | */ |
| 480 | 480 | public function passesCSRFCheck(): bool { |
| 481 | - if($this->csrfTokenManager === null) { |
|
| 481 | + if ($this->csrfTokenManager === null) { |
|
| 482 | 482 | return false; |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | - if(!$this->passesStrictCookieCheck()) { |
|
| 485 | + if (!$this->passesStrictCookieCheck()) { |
|
| 486 | 486 | return false; |
| 487 | 487 | } |
| 488 | 488 | |
@@ -510,7 +510,7 @@ discard block |
||
| 510 | 510 | if ($this->getHeader('OCS-APIREQUEST')) { |
| 511 | 511 | return false; |
| 512 | 512 | } |
| 513 | - if($this->getCookie(session_name()) === null && $this->getCookie('nc_token') === null) { |
|
| 513 | + if ($this->getCookie(session_name()) === null && $this->getCookie('nc_token') === null) { |
|
| 514 | 514 | return false; |
| 515 | 515 | } |
| 516 | 516 | |
@@ -535,7 +535,7 @@ discard block |
||
| 535 | 535 | protected function getProtectedCookieName(string $name): string { |
| 536 | 536 | $cookieParams = $this->getCookieParams(); |
| 537 | 537 | $prefix = ''; |
| 538 | - if($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
| 538 | + if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
| 539 | 539 | $prefix = '__Host-'; |
| 540 | 540 | } |
| 541 | 541 | |
@@ -550,12 +550,12 @@ discard block |
||
| 550 | 550 | * @since 9.1.0 |
| 551 | 551 | */ |
| 552 | 552 | public function passesStrictCookieCheck(): bool { |
| 553 | - if(!$this->cookieCheckRequired()) { |
|
| 553 | + if (!$this->cookieCheckRequired()) { |
|
| 554 | 554 | return true; |
| 555 | 555 | } |
| 556 | 556 | |
| 557 | 557 | $cookieName = $this->getProtectedCookieName('nc_sameSiteCookiestrict'); |
| 558 | - if($this->getCookie($cookieName) === 'true' |
|
| 558 | + if ($this->getCookie($cookieName) === 'true' |
|
| 559 | 559 | && $this->passesLaxCookieCheck()) { |
| 560 | 560 | return true; |
| 561 | 561 | } |
@@ -570,12 +570,12 @@ discard block |
||
| 570 | 570 | * @since 9.1.0 |
| 571 | 571 | */ |
| 572 | 572 | public function passesLaxCookieCheck(): bool { |
| 573 | - if(!$this->cookieCheckRequired()) { |
|
| 573 | + if (!$this->cookieCheckRequired()) { |
|
| 574 | 574 | return true; |
| 575 | 575 | } |
| 576 | 576 | |
| 577 | 577 | $cookieName = $this->getProtectedCookieName('nc_sameSiteCookielax'); |
| 578 | - if($this->getCookie($cookieName) === 'true') { |
|
| 578 | + if ($this->getCookie($cookieName) === 'true') { |
|
| 579 | 579 | return true; |
| 580 | 580 | } |
| 581 | 581 | return false; |
@@ -588,12 +588,12 @@ discard block |
||
| 588 | 588 | * @return string |
| 589 | 589 | */ |
| 590 | 590 | public function getId(): string { |
| 591 | - if(isset($this->server['UNIQUE_ID'])) { |
|
| 591 | + if (isset($this->server['UNIQUE_ID'])) { |
|
| 592 | 592 | return $this->server['UNIQUE_ID']; |
| 593 | 593 | } |
| 594 | 594 | |
| 595 | - if(empty($this->requestId)) { |
|
| 596 | - $validChars = ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS; |
|
| 595 | + if (empty($this->requestId)) { |
|
| 596 | + $validChars = ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS; |
|
| 597 | 597 | $this->requestId = $this->secureRandom->generate(20, $validChars); |
| 598 | 598 | } |
| 599 | 599 | |
@@ -649,15 +649,15 @@ discard block |
||
| 649 | 649 | $remoteAddress = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
| 650 | 650 | $trustedProxies = $this->config->getSystemValue('trusted_proxies', []); |
| 651 | 651 | |
| 652 | - if(\is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress)) { |
|
| 652 | + if (\is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress)) { |
|
| 653 | 653 | $forwardedForHeaders = $this->config->getSystemValue('forwarded_for_headers', [ |
| 654 | 654 | 'HTTP_X_FORWARDED_FOR' |
| 655 | 655 | // only have one default, so we cannot ship an insecure product out of the box |
| 656 | 656 | ]); |
| 657 | 657 | |
| 658 | - foreach($forwardedForHeaders as $header) { |
|
| 659 | - if(isset($this->server[$header])) { |
|
| 660 | - foreach(explode(',', $this->server[$header]) as $IP) { |
|
| 658 | + foreach ($forwardedForHeaders as $header) { |
|
| 659 | + if (isset($this->server[$header])) { |
|
| 660 | + foreach (explode(',', $this->server[$header]) as $IP) { |
|
| 661 | 661 | $IP = trim($IP); |
| 662 | 662 | if (filter_var($IP, FILTER_VALIDATE_IP) !== false) { |
| 663 | 663 | return $IP; |
@@ -676,7 +676,7 @@ discard block |
||
| 676 | 676 | * @return bool |
| 677 | 677 | */ |
| 678 | 678 | private function isOverwriteCondition(string $type = ''): bool { |
| 679 | - $regex = '/' . $this->config->getSystemValue('overwritecondaddr', '') . '/'; |
|
| 679 | + $regex = '/'.$this->config->getSystemValue('overwritecondaddr', '').'/'; |
|
| 680 | 680 | $remoteAddr = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : ''; |
| 681 | 681 | return $regex === '//' || preg_match($regex, $remoteAddr) === 1 |
| 682 | 682 | || $type !== 'protocol'; |
@@ -688,7 +688,7 @@ discard block |
||
| 688 | 688 | * @return string Server protocol (http or https) |
| 689 | 689 | */ |
| 690 | 690 | public function getServerProtocol(): string { |
| 691 | - if($this->config->getSystemValue('overwriteprotocol') !== '' |
|
| 691 | + if ($this->config->getSystemValue('overwriteprotocol') !== '' |
|
| 692 | 692 | && $this->isOverwriteCondition('protocol')) { |
| 693 | 693 | return $this->config->getSystemValue('overwriteprotocol'); |
| 694 | 694 | } |
@@ -734,7 +734,7 @@ discard block |
||
| 734 | 734 | 'HTTP/2', |
| 735 | 735 | ]; |
| 736 | 736 | |
| 737 | - if(\in_array($claimedProtocol, $validProtocols, true)) { |
|
| 737 | + if (\in_array($claimedProtocol, $validProtocols, true)) { |
|
| 738 | 738 | return $claimedProtocol; |
| 739 | 739 | } |
| 740 | 740 | |
@@ -748,8 +748,8 @@ discard block |
||
| 748 | 748 | */ |
| 749 | 749 | public function getRequestUri(): string { |
| 750 | 750 | $uri = isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : ''; |
| 751 | - if($this->config->getSystemValue('overwritewebroot') !== '' && $this->isOverwriteCondition()) { |
|
| 752 | - $uri = $this->getScriptName() . substr($uri, \strlen($this->server['SCRIPT_NAME'])); |
|
| 751 | + if ($this->config->getSystemValue('overwritewebroot') !== '' && $this->isOverwriteCondition()) { |
|
| 752 | + $uri = $this->getScriptName().substr($uri, \strlen($this->server['SCRIPT_NAME'])); |
|
| 753 | 753 | } |
| 754 | 754 | return $uri; |
| 755 | 755 | } |
@@ -776,7 +776,7 @@ discard block |
||
| 776 | 776 | // FIXME: Sabre does not really belong here |
| 777 | 777 | list($path, $name) = \Sabre\Uri\split($scriptName); |
| 778 | 778 | if (!empty($path)) { |
| 779 | - if($path === $pathInfo || strpos($pathInfo, $path.'/') === 0) { |
|
| 779 | + if ($path === $pathInfo || strpos($pathInfo, $path.'/') === 0) { |
|
| 780 | 780 | $pathInfo = substr($pathInfo, \strlen($path)); |
| 781 | 781 | } else { |
| 782 | 782 | throw new \Exception("The requested uri($requestUri) cannot be processed by the script '$scriptName')"); |
@@ -792,7 +792,7 @@ discard block |
||
| 792 | 792 | if ($name !== '' && strpos($pathInfo, $name) === 0) { |
| 793 | 793 | $pathInfo = substr($pathInfo, \strlen($name)); |
| 794 | 794 | } |
| 795 | - if($pathInfo === false || $pathInfo === '/'){ |
|
| 795 | + if ($pathInfo === false || $pathInfo === '/') { |
|
| 796 | 796 | return ''; |
| 797 | 797 | } else { |
| 798 | 798 | return $pathInfo; |
@@ -810,7 +810,7 @@ discard block |
||
| 810 | 810 | $pathInfo = rawurldecode($pathInfo); |
| 811 | 811 | $encoding = mb_detect_encoding($pathInfo, ['UTF-8', 'ISO-8859-1']); |
| 812 | 812 | |
| 813 | - switch($encoding) { |
|
| 813 | + switch ($encoding) { |
|
| 814 | 814 | case 'ISO-8859-1' : |
| 815 | 815 | $pathInfo = utf8_encode($pathInfo); |
| 816 | 816 | } |
@@ -826,12 +826,12 @@ discard block |
||
| 826 | 826 | */ |
| 827 | 827 | public function getScriptName(): string { |
| 828 | 828 | $name = $this->server['SCRIPT_NAME']; |
| 829 | - $overwriteWebRoot = $this->config->getSystemValue('overwritewebroot'); |
|
| 829 | + $overwriteWebRoot = $this->config->getSystemValue('overwritewebroot'); |
|
| 830 | 830 | if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) { |
| 831 | 831 | // FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous |
| 832 | 832 | $serverRoot = str_replace('\\', '/', substr(__DIR__, 0, -\strlen('lib/private/appframework/http/'))); |
| 833 | 833 | $suburi = str_replace('\\', '/', substr(realpath($this->server['SCRIPT_FILENAME']), \strlen($serverRoot))); |
| 834 | - $name = '/' . ltrim($overwriteWebRoot . $suburi, '/'); |
|
| 834 | + $name = '/'.ltrim($overwriteWebRoot.$suburi, '/'); |
|
| 835 | 835 | } |
| 836 | 836 | return $name; |
| 837 | 837 | } |
@@ -906,7 +906,7 @@ discard block |
||
| 906 | 906 | return $host; |
| 907 | 907 | } |
| 908 | 908 | |
| 909 | - $trustedList = (array)$this->config->getSystemValue('trusted_domains', []); |
|
| 909 | + $trustedList = (array) $this->config->getSystemValue('trusted_domains', []); |
|
| 910 | 910 | if (count($trustedList) > 0) { |
| 911 | 911 | return reset($trustedList); |
| 912 | 912 | } |
@@ -921,7 +921,7 @@ discard block |
||
| 921 | 921 | * isn't met |
| 922 | 922 | */ |
| 923 | 923 | private function getOverwriteHost() { |
| 924 | - if($this->config->getSystemValue('overwritehost') !== '' && $this->isOverwriteCondition()) { |
|
| 924 | + if ($this->config->getSystemValue('overwritehost') !== '' && $this->isOverwriteCondition()) { |
|
| 925 | 925 | return $this->config->getSystemValue('overwritehost'); |
| 926 | 926 | } |
| 927 | 927 | return null; |
@@ -36,58 +36,58 @@ |
||
| 36 | 36 | |
| 37 | 37 | class CapabilitiesManager { |
| 38 | 38 | |
| 39 | - /** @var \Closure[] */ |
|
| 40 | - private $capabilities = []; |
|
| 39 | + /** @var \Closure[] */ |
|
| 40 | + private $capabilities = []; |
|
| 41 | 41 | |
| 42 | - /** @var ILogger */ |
|
| 43 | - private $logger; |
|
| 42 | + /** @var ILogger */ |
|
| 43 | + private $logger; |
|
| 44 | 44 | |
| 45 | - public function __construct(ILogger $logger) { |
|
| 46 | - $this->logger = $logger; |
|
| 47 | - } |
|
| 45 | + public function __construct(ILogger $logger) { |
|
| 46 | + $this->logger = $logger; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Get an array of al the capabilities that are registered at this manager |
|
| 51 | - * |
|
| 52 | - * @param bool $public get public capabilities only |
|
| 53 | - * @throws \InvalidArgumentException |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function getCapabilities(bool $public = false) : array { |
|
| 57 | - $capabilities = []; |
|
| 58 | - foreach($this->capabilities as $capability) { |
|
| 59 | - try { |
|
| 60 | - $c = $capability(); |
|
| 61 | - } catch (QueryException $e) { |
|
| 62 | - $this->logger->logException($e, [ |
|
| 63 | - 'message' => 'CapabilitiesManager', |
|
| 64 | - 'level' => ILogger::ERROR, |
|
| 65 | - 'app' => 'core', |
|
| 66 | - ]); |
|
| 67 | - continue; |
|
| 68 | - } |
|
| 49 | + /** |
|
| 50 | + * Get an array of al the capabilities that are registered at this manager |
|
| 51 | + * |
|
| 52 | + * @param bool $public get public capabilities only |
|
| 53 | + * @throws \InvalidArgumentException |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function getCapabilities(bool $public = false) : array { |
|
| 57 | + $capabilities = []; |
|
| 58 | + foreach($this->capabilities as $capability) { |
|
| 59 | + try { |
|
| 60 | + $c = $capability(); |
|
| 61 | + } catch (QueryException $e) { |
|
| 62 | + $this->logger->logException($e, [ |
|
| 63 | + 'message' => 'CapabilitiesManager', |
|
| 64 | + 'level' => ILogger::ERROR, |
|
| 65 | + 'app' => 'core', |
|
| 66 | + ]); |
|
| 67 | + continue; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - if ($c instanceof ICapability) { |
|
| 71 | - if(!$public || $c instanceof IPublicCapability) { |
|
| 72 | - $capabilities = array_replace_recursive($capabilities, $c->getCapabilities()); |
|
| 73 | - } |
|
| 74 | - } else { |
|
| 75 | - throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface'); |
|
| 76 | - } |
|
| 77 | - } |
|
| 70 | + if ($c instanceof ICapability) { |
|
| 71 | + if(!$public || $c instanceof IPublicCapability) { |
|
| 72 | + $capabilities = array_replace_recursive($capabilities, $c->getCapabilities()); |
|
| 73 | + } |
|
| 74 | + } else { |
|
| 75 | + throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface'); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - return $capabilities; |
|
| 80 | - } |
|
| 79 | + return $capabilities; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 84 | - * capabilities are actually requested |
|
| 85 | - * |
|
| 86 | - * $callable has to return an instance of OCP\Capabilities\ICapability |
|
| 87 | - * |
|
| 88 | - * @param \Closure $callable |
|
| 89 | - */ |
|
| 90 | - public function registerCapability(\Closure $callable) { |
|
| 91 | - $this->capabilities[] = $callable; |
|
| 92 | - } |
|
| 82 | + /** |
|
| 83 | + * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 84 | + * capabilities are actually requested |
|
| 85 | + * |
|
| 86 | + * $callable has to return an instance of OCP\Capabilities\ICapability |
|
| 87 | + * |
|
| 88 | + * @param \Closure $callable |
|
| 89 | + */ |
|
| 90 | + public function registerCapability(\Closure $callable) { |
|
| 91 | + $this->capabilities[] = $callable; |
|
| 92 | + } |
|
| 93 | 93 | } |
@@ -53,313 +53,313 @@ |
||
| 53 | 53 | |
| 54 | 54 | class TemplateLayout extends \OC_Template { |
| 55 | 55 | |
| 56 | - private static $versionHash = ''; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var \OCP\IConfig |
|
| 60 | - */ |
|
| 61 | - private $config; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param string $renderAs |
|
| 65 | - * @param string $appId application id |
|
| 66 | - */ |
|
| 67 | - public function __construct( $renderAs, $appId = '' ) { |
|
| 68 | - |
|
| 69 | - // yes - should be injected .... |
|
| 70 | - $this->config = \OC::$server->getConfig(); |
|
| 71 | - |
|
| 72 | - if(\OCP\Util::isIE()) { |
|
| 73 | - \OC_Util::addStyle('ie'); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Decide which page we show |
|
| 77 | - if($renderAs === 'user') { |
|
| 78 | - parent::__construct( 'core', 'layout.user' ); |
|
| 79 | - if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
|
| 80 | - $this->assign('bodyid', 'body-settings'); |
|
| 81 | - }else{ |
|
| 82 | - $this->assign('bodyid', 'body-user'); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // Add navigation entry |
|
| 86 | - $this->assign( 'application', ''); |
|
| 87 | - $this->assign( 'appid', $appId ); |
|
| 88 | - $navigation = \OC::$server->getNavigationManager()->getAll(); |
|
| 89 | - $this->assign( 'navigation', $navigation); |
|
| 90 | - $settingsNavigation = \OC::$server->getNavigationManager()->getAll('settings'); |
|
| 91 | - $this->assign( 'settingsnavigation', $settingsNavigation); |
|
| 92 | - foreach($navigation as $entry) { |
|
| 93 | - if ($entry['active']) { |
|
| 94 | - $this->assign( 'application', $entry['name'] ); |
|
| 95 | - break; |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - foreach($settingsNavigation as $entry) { |
|
| 100 | - if ($entry['active']) { |
|
| 101 | - $this->assign( 'application', $entry['name'] ); |
|
| 102 | - break; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - $userDisplayName = \OC_User::getDisplayName(); |
|
| 106 | - $this->assign('user_displayname', $userDisplayName); |
|
| 107 | - $this->assign('user_uid', \OC_User::getUser()); |
|
| 108 | - |
|
| 109 | - if (\OC_User::getUser() === false) { |
|
| 110 | - $this->assign('userAvatarSet', false); |
|
| 111 | - } else { |
|
| 112 | - $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists()); |
|
| 113 | - $this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0)); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - // check if app menu icons should be inverted |
|
| 117 | - try { |
|
| 118 | - /** @var \OCA\Theming\Util $util */ |
|
| 119 | - $util = \OC::$server->query(\OCA\Theming\Util::class); |
|
| 120 | - $this->assign('themingInvertMenu', $util->invertTextColor(\OC::$server->getThemingDefaults()->getColorPrimary())); |
|
| 121 | - } catch (\OCP\AppFramework\QueryException $e) { |
|
| 122 | - $this->assign('themingInvertMenu', false); |
|
| 123 | - } catch (\OCP\AutoloadNotAllowedException $e) { |
|
| 124 | - $this->assign('themingInvertMenu', false); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - } else if ($renderAs === 'error') { |
|
| 128 | - parent::__construct('core', 'layout.guest', '', false); |
|
| 129 | - $this->assign('bodyid', 'body-login'); |
|
| 130 | - $this->assign('user_displayname', ''); |
|
| 131 | - $this->assign('user_uid', ''); |
|
| 132 | - } else if ($renderAs === 'guest') { |
|
| 133 | - parent::__construct('core', 'layout.guest'); |
|
| 134 | - \OC_Util::addStyle('guest'); |
|
| 135 | - $this->assign('bodyid', 'body-login'); |
|
| 136 | - |
|
| 137 | - $userDisplayName = \OC_User::getDisplayName(); |
|
| 138 | - $this->assign('user_displayname', $userDisplayName); |
|
| 139 | - $this->assign('user_uid', \OC_User::getUser()); |
|
| 140 | - } else if ($renderAs === 'public') { |
|
| 141 | - parent::__construct('core', 'layout.public'); |
|
| 142 | - $this->assign( 'appid', $appId ); |
|
| 143 | - $this->assign('bodyid', 'body-public'); |
|
| 144 | - |
|
| 145 | - /** @var IRegistry $subscription */ |
|
| 146 | - $subscription = \OC::$server->query(IRegistry::class); |
|
| 147 | - $showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true); |
|
| 148 | - if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) { |
|
| 149 | - $showSimpleSignup = false; |
|
| 150 | - } |
|
| 151 | - $this->assign('showSimpleSignUpLink', $showSimpleSignup); |
|
| 152 | - } else { |
|
| 153 | - parent::__construct('core', 'layout.base'); |
|
| 154 | - |
|
| 155 | - } |
|
| 156 | - // Send the language and the locale to our layouts |
|
| 157 | - $lang = \OC::$server->getL10NFactory()->findLanguage(); |
|
| 158 | - $locale = \OC::$server->getL10NFactory()->findLocale($lang); |
|
| 159 | - |
|
| 160 | - $lang = str_replace('_', '-', $lang); |
|
| 161 | - $this->assign('language', $lang); |
|
| 162 | - $this->assign('locale', $locale); |
|
| 163 | - |
|
| 164 | - if(\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 165 | - if (empty(self::$versionHash)) { |
|
| 166 | - $v = \OC_App::getAppVersions(); |
|
| 167 | - $v['core'] = implode('.', \OCP\Util::getVersion()); |
|
| 168 | - self::$versionHash = substr(md5(implode(',', $v)), 0, 8); |
|
| 169 | - } |
|
| 170 | - } else { |
|
| 171 | - self::$versionHash = md5('not installed'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // Add the js files |
|
| 175 | - $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
|
| 176 | - $this->assign('jsfiles', []); |
|
| 177 | - if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { |
|
| 178 | - if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) { |
|
| 179 | - $jsConfigHelper = new JSConfigHelper( |
|
| 180 | - \OC::$server->getL10N('lib'), |
|
| 181 | - \OC::$server->query(Defaults::class), |
|
| 182 | - \OC::$server->getAppManager(), |
|
| 183 | - \OC::$server->getSession(), |
|
| 184 | - \OC::$server->getUserSession()->getUser(), |
|
| 185 | - $this->config, |
|
| 186 | - \OC::$server->getGroupManager(), |
|
| 187 | - \OC::$server->getIniWrapper(), |
|
| 188 | - \OC::$server->getURLGenerator(), |
|
| 189 | - \OC::$server->getCapabilitiesManager() |
|
| 190 | - ); |
|
| 191 | - $this->assign('inline_ocjs', $jsConfigHelper->getConfig()); |
|
| 192 | - } else { |
|
| 193 | - $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash])); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - foreach($jsFiles as $info) { |
|
| 197 | - $web = $info[1]; |
|
| 198 | - $file = $info[2]; |
|
| 199 | - $this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() ); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - try { |
|
| 203 | - $pathInfo = \OC::$server->getRequest()->getPathInfo(); |
|
| 204 | - } catch (\Exception $e) { |
|
| 205 | - $pathInfo = ''; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - // Do not initialise scss appdata until we have a fully installed instance |
|
| 209 | - // Do not load scss for update, errors, installation or login page |
|
| 210 | - if(\OC::$server->getSystemConfig()->getValue('installed', false) |
|
| 211 | - && !\OCP\Util::needUpgrade() |
|
| 212 | - && $pathInfo !== '' |
|
| 213 | - && !preg_match('/^\/login/', $pathInfo) |
|
| 214 | - && $renderAs !== 'error' |
|
| 215 | - ) { |
|
| 216 | - $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
|
| 217 | - } else { |
|
| 218 | - // If we ignore the scss compiler, |
|
| 219 | - // we need to load the guest css fallback |
|
| 220 | - \OC_Util::addStyle('guest'); |
|
| 221 | - $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $this->assign('cssfiles', []); |
|
| 225 | - $this->assign('printcssfiles', []); |
|
| 226 | - $this->assign('versionHash', self::$versionHash); |
|
| 227 | - foreach($cssFiles as $info) { |
|
| 228 | - $web = $info[1]; |
|
| 229 | - $file = $info[2]; |
|
| 230 | - |
|
| 231 | - if (substr($file, -strlen('print.css')) === 'print.css') { |
|
| 232 | - $this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() ); |
|
| 233 | - } else { |
|
| 234 | - $suffix = $this->getVersionHashSuffix($web, $file); |
|
| 235 | - |
|
| 236 | - if (strpos($file, '?v=') == false) { |
|
| 237 | - $this->append( 'cssfiles', $web.'/'.$file . $suffix); |
|
| 238 | - } else { |
|
| 239 | - $this->append( 'cssfiles', $web.'/'.$file . '-' . substr($suffix, 3)); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** @var InitialStateService $initialState */ |
|
| 246 | - $initialState = \OC::$server->query(InitialStateService::class); |
|
| 247 | - $this->assign('initialStates', $initialState->getInitialStates()); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param string $path |
|
| 252 | - * @param string $file |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - protected function getVersionHashSuffix($path = false, $file = false) { |
|
| 256 | - if ($this->config->getSystemValue('debug', false)) { |
|
| 257 | - // allows chrome workspace mapping in debug mode |
|
| 258 | - return ""; |
|
| 259 | - } |
|
| 260 | - $themingSuffix = ''; |
|
| 261 | - $v = []; |
|
| 262 | - |
|
| 263 | - if ($this->config->getSystemValue('installed', false)) { |
|
| 264 | - if (\OC::$server->getAppManager()->isInstalled('theming')) { |
|
| 265 | - $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0'); |
|
| 266 | - } |
|
| 267 | - $v = \OC_App::getAppVersions(); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - // Try the webroot path for a match |
|
| 271 | - if ($path !== false && $path !== '') { |
|
| 272 | - $appName = $this->getAppNamefromPath($path); |
|
| 273 | - if(array_key_exists($appName, $v)) { |
|
| 274 | - $appVersion = $v[$appName]; |
|
| 275 | - return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix; |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - // fallback to the file path instead |
|
| 279 | - if ($file !== false && $file !== '') { |
|
| 280 | - $appName = $this->getAppNamefromPath($file); |
|
| 281 | - if(array_key_exists($appName, $v)) { |
|
| 282 | - $appVersion = $v[$appName]; |
|
| 283 | - return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix; |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return '?v=' . self::$versionHash . $themingSuffix; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @param array $styles |
|
| 292 | - * @return array |
|
| 293 | - */ |
|
| 294 | - static public function findStylesheetFiles($styles, $compileScss = true) { |
|
| 295 | - // Read the selected theme from the config file |
|
| 296 | - $theme = \OC_Util::getTheme(); |
|
| 297 | - |
|
| 298 | - if($compileScss) { |
|
| 299 | - $SCSSCacher = \OC::$server->query(SCSSCacher::class); |
|
| 300 | - } else { |
|
| 301 | - $SCSSCacher = null; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $locator = new \OC\Template\CSSResourceLocator( |
|
| 305 | - \OC::$server->getLogger(), |
|
| 306 | - $theme, |
|
| 307 | - [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 308 | - [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 309 | - $SCSSCacher |
|
| 310 | - ); |
|
| 311 | - $locator->find($styles); |
|
| 312 | - return $locator->getResources(); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * @param string $path |
|
| 317 | - * @return string|boolean |
|
| 318 | - */ |
|
| 319 | - public function getAppNamefromPath($path) { |
|
| 320 | - if ($path !== '' && is_string($path)) { |
|
| 321 | - $pathParts = explode('/', $path); |
|
| 322 | - if ($pathParts[0] === 'css') { |
|
| 323 | - // This is a scss request |
|
| 324 | - return $pathParts[1]; |
|
| 325 | - } |
|
| 326 | - return end($pathParts); |
|
| 327 | - } |
|
| 328 | - return false; |
|
| 329 | - |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @param array $scripts |
|
| 334 | - * @return array |
|
| 335 | - */ |
|
| 336 | - static public function findJavascriptFiles($scripts) { |
|
| 337 | - // Read the selected theme from the config file |
|
| 338 | - $theme = \OC_Util::getTheme(); |
|
| 339 | - |
|
| 340 | - $locator = new \OC\Template\JSResourceLocator( |
|
| 341 | - \OC::$server->getLogger(), |
|
| 342 | - $theme, |
|
| 343 | - [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 344 | - [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 345 | - \OC::$server->query(JSCombiner::class) |
|
| 346 | - ); |
|
| 347 | - $locator->find($scripts); |
|
| 348 | - return $locator->getResources(); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Converts the absolute file path to a relative path from \OC::$SERVERROOT |
|
| 353 | - * @param string $filePath Absolute path |
|
| 354 | - * @return string Relative path |
|
| 355 | - * @throws \Exception If $filePath is not under \OC::$SERVERROOT |
|
| 356 | - */ |
|
| 357 | - public static function convertToRelativePath($filePath) { |
|
| 358 | - $relativePath = explode(\OC::$SERVERROOT, $filePath); |
|
| 359 | - if(count($relativePath) !== 2) { |
|
| 360 | - throw new \Exception('$filePath is not under the \OC::$SERVERROOT'); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - return $relativePath[1]; |
|
| 364 | - } |
|
| 56 | + private static $versionHash = ''; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var \OCP\IConfig |
|
| 60 | + */ |
|
| 61 | + private $config; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param string $renderAs |
|
| 65 | + * @param string $appId application id |
|
| 66 | + */ |
|
| 67 | + public function __construct( $renderAs, $appId = '' ) { |
|
| 68 | + |
|
| 69 | + // yes - should be injected .... |
|
| 70 | + $this->config = \OC::$server->getConfig(); |
|
| 71 | + |
|
| 72 | + if(\OCP\Util::isIE()) { |
|
| 73 | + \OC_Util::addStyle('ie'); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Decide which page we show |
|
| 77 | + if($renderAs === 'user') { |
|
| 78 | + parent::__construct( 'core', 'layout.user' ); |
|
| 79 | + if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
|
| 80 | + $this->assign('bodyid', 'body-settings'); |
|
| 81 | + }else{ |
|
| 82 | + $this->assign('bodyid', 'body-user'); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // Add navigation entry |
|
| 86 | + $this->assign( 'application', ''); |
|
| 87 | + $this->assign( 'appid', $appId ); |
|
| 88 | + $navigation = \OC::$server->getNavigationManager()->getAll(); |
|
| 89 | + $this->assign( 'navigation', $navigation); |
|
| 90 | + $settingsNavigation = \OC::$server->getNavigationManager()->getAll('settings'); |
|
| 91 | + $this->assign( 'settingsnavigation', $settingsNavigation); |
|
| 92 | + foreach($navigation as $entry) { |
|
| 93 | + if ($entry['active']) { |
|
| 94 | + $this->assign( 'application', $entry['name'] ); |
|
| 95 | + break; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + foreach($settingsNavigation as $entry) { |
|
| 100 | + if ($entry['active']) { |
|
| 101 | + $this->assign( 'application', $entry['name'] ); |
|
| 102 | + break; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + $userDisplayName = \OC_User::getDisplayName(); |
|
| 106 | + $this->assign('user_displayname', $userDisplayName); |
|
| 107 | + $this->assign('user_uid', \OC_User::getUser()); |
|
| 108 | + |
|
| 109 | + if (\OC_User::getUser() === false) { |
|
| 110 | + $this->assign('userAvatarSet', false); |
|
| 111 | + } else { |
|
| 112 | + $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists()); |
|
| 113 | + $this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0)); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + // check if app menu icons should be inverted |
|
| 117 | + try { |
|
| 118 | + /** @var \OCA\Theming\Util $util */ |
|
| 119 | + $util = \OC::$server->query(\OCA\Theming\Util::class); |
|
| 120 | + $this->assign('themingInvertMenu', $util->invertTextColor(\OC::$server->getThemingDefaults()->getColorPrimary())); |
|
| 121 | + } catch (\OCP\AppFramework\QueryException $e) { |
|
| 122 | + $this->assign('themingInvertMenu', false); |
|
| 123 | + } catch (\OCP\AutoloadNotAllowedException $e) { |
|
| 124 | + $this->assign('themingInvertMenu', false); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + } else if ($renderAs === 'error') { |
|
| 128 | + parent::__construct('core', 'layout.guest', '', false); |
|
| 129 | + $this->assign('bodyid', 'body-login'); |
|
| 130 | + $this->assign('user_displayname', ''); |
|
| 131 | + $this->assign('user_uid', ''); |
|
| 132 | + } else if ($renderAs === 'guest') { |
|
| 133 | + parent::__construct('core', 'layout.guest'); |
|
| 134 | + \OC_Util::addStyle('guest'); |
|
| 135 | + $this->assign('bodyid', 'body-login'); |
|
| 136 | + |
|
| 137 | + $userDisplayName = \OC_User::getDisplayName(); |
|
| 138 | + $this->assign('user_displayname', $userDisplayName); |
|
| 139 | + $this->assign('user_uid', \OC_User::getUser()); |
|
| 140 | + } else if ($renderAs === 'public') { |
|
| 141 | + parent::__construct('core', 'layout.public'); |
|
| 142 | + $this->assign( 'appid', $appId ); |
|
| 143 | + $this->assign('bodyid', 'body-public'); |
|
| 144 | + |
|
| 145 | + /** @var IRegistry $subscription */ |
|
| 146 | + $subscription = \OC::$server->query(IRegistry::class); |
|
| 147 | + $showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true); |
|
| 148 | + if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) { |
|
| 149 | + $showSimpleSignup = false; |
|
| 150 | + } |
|
| 151 | + $this->assign('showSimpleSignUpLink', $showSimpleSignup); |
|
| 152 | + } else { |
|
| 153 | + parent::__construct('core', 'layout.base'); |
|
| 154 | + |
|
| 155 | + } |
|
| 156 | + // Send the language and the locale to our layouts |
|
| 157 | + $lang = \OC::$server->getL10NFactory()->findLanguage(); |
|
| 158 | + $locale = \OC::$server->getL10NFactory()->findLocale($lang); |
|
| 159 | + |
|
| 160 | + $lang = str_replace('_', '-', $lang); |
|
| 161 | + $this->assign('language', $lang); |
|
| 162 | + $this->assign('locale', $locale); |
|
| 163 | + |
|
| 164 | + if(\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 165 | + if (empty(self::$versionHash)) { |
|
| 166 | + $v = \OC_App::getAppVersions(); |
|
| 167 | + $v['core'] = implode('.', \OCP\Util::getVersion()); |
|
| 168 | + self::$versionHash = substr(md5(implode(',', $v)), 0, 8); |
|
| 169 | + } |
|
| 170 | + } else { |
|
| 171 | + self::$versionHash = md5('not installed'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // Add the js files |
|
| 175 | + $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
|
| 176 | + $this->assign('jsfiles', []); |
|
| 177 | + if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { |
|
| 178 | + if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) { |
|
| 179 | + $jsConfigHelper = new JSConfigHelper( |
|
| 180 | + \OC::$server->getL10N('lib'), |
|
| 181 | + \OC::$server->query(Defaults::class), |
|
| 182 | + \OC::$server->getAppManager(), |
|
| 183 | + \OC::$server->getSession(), |
|
| 184 | + \OC::$server->getUserSession()->getUser(), |
|
| 185 | + $this->config, |
|
| 186 | + \OC::$server->getGroupManager(), |
|
| 187 | + \OC::$server->getIniWrapper(), |
|
| 188 | + \OC::$server->getURLGenerator(), |
|
| 189 | + \OC::$server->getCapabilitiesManager() |
|
| 190 | + ); |
|
| 191 | + $this->assign('inline_ocjs', $jsConfigHelper->getConfig()); |
|
| 192 | + } else { |
|
| 193 | + $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash])); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + foreach($jsFiles as $info) { |
|
| 197 | + $web = $info[1]; |
|
| 198 | + $file = $info[2]; |
|
| 199 | + $this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() ); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + try { |
|
| 203 | + $pathInfo = \OC::$server->getRequest()->getPathInfo(); |
|
| 204 | + } catch (\Exception $e) { |
|
| 205 | + $pathInfo = ''; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + // Do not initialise scss appdata until we have a fully installed instance |
|
| 209 | + // Do not load scss for update, errors, installation or login page |
|
| 210 | + if(\OC::$server->getSystemConfig()->getValue('installed', false) |
|
| 211 | + && !\OCP\Util::needUpgrade() |
|
| 212 | + && $pathInfo !== '' |
|
| 213 | + && !preg_match('/^\/login/', $pathInfo) |
|
| 214 | + && $renderAs !== 'error' |
|
| 215 | + ) { |
|
| 216 | + $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
|
| 217 | + } else { |
|
| 218 | + // If we ignore the scss compiler, |
|
| 219 | + // we need to load the guest css fallback |
|
| 220 | + \OC_Util::addStyle('guest'); |
|
| 221 | + $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $this->assign('cssfiles', []); |
|
| 225 | + $this->assign('printcssfiles', []); |
|
| 226 | + $this->assign('versionHash', self::$versionHash); |
|
| 227 | + foreach($cssFiles as $info) { |
|
| 228 | + $web = $info[1]; |
|
| 229 | + $file = $info[2]; |
|
| 230 | + |
|
| 231 | + if (substr($file, -strlen('print.css')) === 'print.css') { |
|
| 232 | + $this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() ); |
|
| 233 | + } else { |
|
| 234 | + $suffix = $this->getVersionHashSuffix($web, $file); |
|
| 235 | + |
|
| 236 | + if (strpos($file, '?v=') == false) { |
|
| 237 | + $this->append( 'cssfiles', $web.'/'.$file . $suffix); |
|
| 238 | + } else { |
|
| 239 | + $this->append( 'cssfiles', $web.'/'.$file . '-' . substr($suffix, 3)); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** @var InitialStateService $initialState */ |
|
| 246 | + $initialState = \OC::$server->query(InitialStateService::class); |
|
| 247 | + $this->assign('initialStates', $initialState->getInitialStates()); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param string $path |
|
| 252 | + * @param string $file |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + protected function getVersionHashSuffix($path = false, $file = false) { |
|
| 256 | + if ($this->config->getSystemValue('debug', false)) { |
|
| 257 | + // allows chrome workspace mapping in debug mode |
|
| 258 | + return ""; |
|
| 259 | + } |
|
| 260 | + $themingSuffix = ''; |
|
| 261 | + $v = []; |
|
| 262 | + |
|
| 263 | + if ($this->config->getSystemValue('installed', false)) { |
|
| 264 | + if (\OC::$server->getAppManager()->isInstalled('theming')) { |
|
| 265 | + $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0'); |
|
| 266 | + } |
|
| 267 | + $v = \OC_App::getAppVersions(); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + // Try the webroot path for a match |
|
| 271 | + if ($path !== false && $path !== '') { |
|
| 272 | + $appName = $this->getAppNamefromPath($path); |
|
| 273 | + if(array_key_exists($appName, $v)) { |
|
| 274 | + $appVersion = $v[$appName]; |
|
| 275 | + return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix; |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + // fallback to the file path instead |
|
| 279 | + if ($file !== false && $file !== '') { |
|
| 280 | + $appName = $this->getAppNamefromPath($file); |
|
| 281 | + if(array_key_exists($appName, $v)) { |
|
| 282 | + $appVersion = $v[$appName]; |
|
| 283 | + return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return '?v=' . self::$versionHash . $themingSuffix; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @param array $styles |
|
| 292 | + * @return array |
|
| 293 | + */ |
|
| 294 | + static public function findStylesheetFiles($styles, $compileScss = true) { |
|
| 295 | + // Read the selected theme from the config file |
|
| 296 | + $theme = \OC_Util::getTheme(); |
|
| 297 | + |
|
| 298 | + if($compileScss) { |
|
| 299 | + $SCSSCacher = \OC::$server->query(SCSSCacher::class); |
|
| 300 | + } else { |
|
| 301 | + $SCSSCacher = null; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $locator = new \OC\Template\CSSResourceLocator( |
|
| 305 | + \OC::$server->getLogger(), |
|
| 306 | + $theme, |
|
| 307 | + [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 308 | + [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 309 | + $SCSSCacher |
|
| 310 | + ); |
|
| 311 | + $locator->find($styles); |
|
| 312 | + return $locator->getResources(); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * @param string $path |
|
| 317 | + * @return string|boolean |
|
| 318 | + */ |
|
| 319 | + public function getAppNamefromPath($path) { |
|
| 320 | + if ($path !== '' && is_string($path)) { |
|
| 321 | + $pathParts = explode('/', $path); |
|
| 322 | + if ($pathParts[0] === 'css') { |
|
| 323 | + // This is a scss request |
|
| 324 | + return $pathParts[1]; |
|
| 325 | + } |
|
| 326 | + return end($pathParts); |
|
| 327 | + } |
|
| 328 | + return false; |
|
| 329 | + |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @param array $scripts |
|
| 334 | + * @return array |
|
| 335 | + */ |
|
| 336 | + static public function findJavascriptFiles($scripts) { |
|
| 337 | + // Read the selected theme from the config file |
|
| 338 | + $theme = \OC_Util::getTheme(); |
|
| 339 | + |
|
| 340 | + $locator = new \OC\Template\JSResourceLocator( |
|
| 341 | + \OC::$server->getLogger(), |
|
| 342 | + $theme, |
|
| 343 | + [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 344 | + [ \OC::$SERVERROOT => \OC::$WEBROOT ], |
|
| 345 | + \OC::$server->query(JSCombiner::class) |
|
| 346 | + ); |
|
| 347 | + $locator->find($scripts); |
|
| 348 | + return $locator->getResources(); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Converts the absolute file path to a relative path from \OC::$SERVERROOT |
|
| 353 | + * @param string $filePath Absolute path |
|
| 354 | + * @return string Relative path |
|
| 355 | + * @throws \Exception If $filePath is not under \OC::$SERVERROOT |
|
| 356 | + */ |
|
| 357 | + public static function convertToRelativePath($filePath) { |
|
| 358 | + $relativePath = explode(\OC::$SERVERROOT, $filePath); |
|
| 359 | + if(count($relativePath) !== 2) { |
|
| 360 | + throw new \Exception('$filePath is not under the \OC::$SERVERROOT'); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + return $relativePath[1]; |
|
| 364 | + } |
|
| 365 | 365 | } |
@@ -29,107 +29,107 @@ |
||
| 29 | 29 | * Abstract base class for user management |
| 30 | 30 | */ |
| 31 | 31 | abstract class Backend implements \OCP\GroupInterface { |
| 32 | - /** |
|
| 33 | - * error code for functions not provided by the group backend |
|
| 34 | - */ |
|
| 35 | - const NOT_IMPLEMENTED = -501; |
|
| 32 | + /** |
|
| 33 | + * error code for functions not provided by the group backend |
|
| 34 | + */ |
|
| 35 | + const NOT_IMPLEMENTED = -501; |
|
| 36 | 36 | |
| 37 | - protected $possibleActions = [ |
|
| 38 | - self::CREATE_GROUP => 'createGroup', |
|
| 39 | - self::DELETE_GROUP => 'deleteGroup', |
|
| 40 | - self::ADD_TO_GROUP => 'addToGroup', |
|
| 41 | - self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
| 42 | - self::COUNT_USERS => 'countUsersInGroup', |
|
| 43 | - self::GROUP_DETAILS => 'getGroupDetails', |
|
| 44 | - self::IS_ADMIN => 'isAdmin', |
|
| 45 | - ]; |
|
| 37 | + protected $possibleActions = [ |
|
| 38 | + self::CREATE_GROUP => 'createGroup', |
|
| 39 | + self::DELETE_GROUP => 'deleteGroup', |
|
| 40 | + self::ADD_TO_GROUP => 'addToGroup', |
|
| 41 | + self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
| 42 | + self::COUNT_USERS => 'countUsersInGroup', |
|
| 43 | + self::GROUP_DETAILS => 'getGroupDetails', |
|
| 44 | + self::IS_ADMIN => 'isAdmin', |
|
| 45 | + ]; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Get all supported actions |
|
| 49 | - * @return int bitwise-or'ed actions |
|
| 50 | - * |
|
| 51 | - * Returns the supported actions as int to be |
|
| 52 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 53 | - */ |
|
| 54 | - public function getSupportedActions() { |
|
| 55 | - $actions = 0; |
|
| 56 | - foreach($this->possibleActions AS $action => $methodName) { |
|
| 57 | - if(method_exists($this, $methodName)) { |
|
| 58 | - $actions |= $action; |
|
| 59 | - } |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Get all supported actions |
|
| 49 | + * @return int bitwise-or'ed actions |
|
| 50 | + * |
|
| 51 | + * Returns the supported actions as int to be |
|
| 52 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 53 | + */ |
|
| 54 | + public function getSupportedActions() { |
|
| 55 | + $actions = 0; |
|
| 56 | + foreach($this->possibleActions AS $action => $methodName) { |
|
| 57 | + if(method_exists($this, $methodName)) { |
|
| 58 | + $actions |= $action; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - return $actions; |
|
| 63 | - } |
|
| 62 | + return $actions; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Check if backend implements actions |
|
| 67 | - * @param int $actions bitwise-or'ed actions |
|
| 68 | - * @return bool |
|
| 69 | - * |
|
| 70 | - * Returns the supported actions as int to be |
|
| 71 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 72 | - */ |
|
| 73 | - public function implementsActions($actions) { |
|
| 74 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 75 | - } |
|
| 65 | + /** |
|
| 66 | + * Check if backend implements actions |
|
| 67 | + * @param int $actions bitwise-or'ed actions |
|
| 68 | + * @return bool |
|
| 69 | + * |
|
| 70 | + * Returns the supported actions as int to be |
|
| 71 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 72 | + */ |
|
| 73 | + public function implementsActions($actions) { |
|
| 74 | + return (bool)($this->getSupportedActions() & $actions); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * is user in group? |
|
| 79 | - * @param string $uid uid of the user |
|
| 80 | - * @param string $gid gid of the group |
|
| 81 | - * @return bool |
|
| 82 | - * |
|
| 83 | - * Checks whether the user is member of a group or not. |
|
| 84 | - */ |
|
| 85 | - public function inGroup($uid, $gid) { |
|
| 86 | - return in_array($gid, $this->getUserGroups($uid)); |
|
| 87 | - } |
|
| 77 | + /** |
|
| 78 | + * is user in group? |
|
| 79 | + * @param string $uid uid of the user |
|
| 80 | + * @param string $gid gid of the group |
|
| 81 | + * @return bool |
|
| 82 | + * |
|
| 83 | + * Checks whether the user is member of a group or not. |
|
| 84 | + */ |
|
| 85 | + public function inGroup($uid, $gid) { |
|
| 86 | + return in_array($gid, $this->getUserGroups($uid)); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Get all groups a user belongs to |
|
| 91 | - * @param string $uid Name of the user |
|
| 92 | - * @return array an array of group names |
|
| 93 | - * |
|
| 94 | - * This function fetches all groups a user belongs to. It does not check |
|
| 95 | - * if the user exists at all. |
|
| 96 | - */ |
|
| 97 | - public function getUserGroups($uid) { |
|
| 98 | - return []; |
|
| 99 | - } |
|
| 89 | + /** |
|
| 90 | + * Get all groups a user belongs to |
|
| 91 | + * @param string $uid Name of the user |
|
| 92 | + * @return array an array of group names |
|
| 93 | + * |
|
| 94 | + * This function fetches all groups a user belongs to. It does not check |
|
| 95 | + * if the user exists at all. |
|
| 96 | + */ |
|
| 97 | + public function getUserGroups($uid) { |
|
| 98 | + return []; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * get a list of all groups |
|
| 103 | - * @param string $search |
|
| 104 | - * @param int $limit |
|
| 105 | - * @param int $offset |
|
| 106 | - * @return array an array of group names |
|
| 107 | - * |
|
| 108 | - * Returns a list with all groups |
|
| 109 | - */ |
|
| 101 | + /** |
|
| 102 | + * get a list of all groups |
|
| 103 | + * @param string $search |
|
| 104 | + * @param int $limit |
|
| 105 | + * @param int $offset |
|
| 106 | + * @return array an array of group names |
|
| 107 | + * |
|
| 108 | + * Returns a list with all groups |
|
| 109 | + */ |
|
| 110 | 110 | |
| 111 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 112 | - return []; |
|
| 113 | - } |
|
| 111 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 112 | + return []; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * check if a group exists |
|
| 117 | - * @param string $gid |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function groupExists($gid) { |
|
| 121 | - return in_array($gid, $this->getGroups($gid, 1)); |
|
| 122 | - } |
|
| 115 | + /** |
|
| 116 | + * check if a group exists |
|
| 117 | + * @param string $gid |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function groupExists($gid) { |
|
| 121 | + return in_array($gid, $this->getGroups($gid, 1)); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * get a list of all users in a group |
|
| 126 | - * @param string $gid |
|
| 127 | - * @param string $search |
|
| 128 | - * @param int $limit |
|
| 129 | - * @param int $offset |
|
| 130 | - * @return array an array of user ids |
|
| 131 | - */ |
|
| 132 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 133 | - return []; |
|
| 134 | - } |
|
| 124 | + /** |
|
| 125 | + * get a list of all users in a group |
|
| 126 | + * @param string $gid |
|
| 127 | + * @param string $search |
|
| 128 | + * @param int $limit |
|
| 129 | + * @param int $offset |
|
| 130 | + * @return array an array of user ids |
|
| 131 | + */ |
|
| 132 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 133 | + return []; |
|
| 134 | + } |
|
| 135 | 135 | } |
@@ -53,8 +53,8 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | public function getSupportedActions() { |
| 55 | 55 | $actions = 0; |
| 56 | - foreach($this->possibleActions AS $action => $methodName) { |
|
| 57 | - if(method_exists($this, $methodName)) { |
|
| 56 | + foreach ($this->possibleActions AS $action => $methodName) { |
|
| 57 | + if (method_exists($this, $methodName)) { |
|
| 58 | 58 | $actions |= $action; |
| 59 | 59 | } |
| 60 | 60 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * compared with \OC\Group\Backend::CREATE_GROUP etc. |
| 72 | 72 | */ |
| 73 | 73 | public function implementsActions($actions) { |
| 74 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 74 | + return (bool) ($this->getSupportedActions() & $actions); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
@@ -33,502 +33,502 @@ |
||
| 33 | 33 | |
| 34 | 34 | class Event implements IEvent { |
| 35 | 35 | |
| 36 | - /** @var string */ |
|
| 37 | - protected $app = ''; |
|
| 38 | - /** @var string */ |
|
| 39 | - protected $type = ''; |
|
| 40 | - /** @var string */ |
|
| 41 | - protected $affectedUser = ''; |
|
| 42 | - /** @var string */ |
|
| 43 | - protected $author = ''; |
|
| 44 | - /** @var int */ |
|
| 45 | - protected $timestamp = 0; |
|
| 46 | - /** @var string */ |
|
| 47 | - protected $subject = ''; |
|
| 48 | - /** @var array */ |
|
| 49 | - protected $subjectParameters = []; |
|
| 50 | - /** @var string */ |
|
| 51 | - protected $subjectParsed = ''; |
|
| 52 | - /** @var string */ |
|
| 53 | - protected $subjectRich = ''; |
|
| 54 | - /** @var array */ |
|
| 55 | - protected $subjectRichParameters = []; |
|
| 56 | - /** @var string */ |
|
| 57 | - protected $message = ''; |
|
| 58 | - /** @var array */ |
|
| 59 | - protected $messageParameters = []; |
|
| 60 | - /** @var string */ |
|
| 61 | - protected $messageParsed = ''; |
|
| 62 | - /** @var string */ |
|
| 63 | - protected $messageRich = ''; |
|
| 64 | - /** @var array */ |
|
| 65 | - protected $messageRichParameters = []; |
|
| 66 | - /** @var string */ |
|
| 67 | - protected $objectType = ''; |
|
| 68 | - /** @var int */ |
|
| 69 | - protected $objectId = 0; |
|
| 70 | - /** @var string */ |
|
| 71 | - protected $objectName = ''; |
|
| 72 | - /** @var string */ |
|
| 73 | - protected $link = ''; |
|
| 74 | - /** @var string */ |
|
| 75 | - protected $icon = ''; |
|
| 76 | - |
|
| 77 | - /** @var IEvent|null */ |
|
| 78 | - protected $child; |
|
| 79 | - /** @var IValidator */ |
|
| 80 | - protected $richValidator; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param IValidator $richValidator |
|
| 84 | - */ |
|
| 85 | - public function __construct(IValidator $richValidator) { |
|
| 86 | - $this->richValidator = $richValidator; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Set the app of the activity |
|
| 91 | - * |
|
| 92 | - * @param string $app |
|
| 93 | - * @return IEvent |
|
| 94 | - * @throws \InvalidArgumentException if the app id is invalid |
|
| 95 | - * @since 8.2.0 |
|
| 96 | - */ |
|
| 97 | - public function setApp(string $app): IEvent { |
|
| 98 | - if ($app === '' || isset($app[32])) { |
|
| 99 | - throw new \InvalidArgumentException('The given app is invalid'); |
|
| 100 | - } |
|
| 101 | - $this->app = $app; |
|
| 102 | - return $this; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function getApp(): string { |
|
| 109 | - return $this->app; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Set the type of the activity |
|
| 114 | - * |
|
| 115 | - * @param string $type |
|
| 116 | - * @return IEvent |
|
| 117 | - * @throws \InvalidArgumentException if the type is invalid |
|
| 118 | - * @since 8.2.0 |
|
| 119 | - */ |
|
| 120 | - public function setType(string $type): IEvent { |
|
| 121 | - if ($type === '' || isset($type[255])) { |
|
| 122 | - throw new \InvalidArgumentException('The given type is invalid'); |
|
| 123 | - } |
|
| 124 | - $this->type = $type; |
|
| 125 | - return $this; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - public function getType(): string { |
|
| 132 | - return $this->type; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Set the affected user of the activity |
|
| 137 | - * |
|
| 138 | - * @param string $affectedUser |
|
| 139 | - * @return IEvent |
|
| 140 | - * @throws \InvalidArgumentException if the affected user is invalid |
|
| 141 | - * @since 8.2.0 |
|
| 142 | - */ |
|
| 143 | - public function setAffectedUser(string $affectedUser): IEvent { |
|
| 144 | - if ($affectedUser === '' || isset($affectedUser[64])) { |
|
| 145 | - throw new \InvalidArgumentException('The given affected user is invalid'); |
|
| 146 | - } |
|
| 147 | - $this->affectedUser = $affectedUser; |
|
| 148 | - return $this; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @return string |
|
| 153 | - */ |
|
| 154 | - public function getAffectedUser(): string { |
|
| 155 | - return $this->affectedUser; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Set the author of the activity |
|
| 160 | - * |
|
| 161 | - * @param string $author |
|
| 162 | - * @return IEvent |
|
| 163 | - * @throws \InvalidArgumentException if the author is invalid |
|
| 164 | - * @since 8.2.0 |
|
| 165 | - */ |
|
| 166 | - public function setAuthor(string $author): IEvent { |
|
| 167 | - if (isset($author[64])) { |
|
| 168 | - throw new \InvalidArgumentException('The given author user is invalid'); |
|
| 169 | - } |
|
| 170 | - $this->author = $author; |
|
| 171 | - return $this; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @return string |
|
| 176 | - */ |
|
| 177 | - public function getAuthor(): string { |
|
| 178 | - return $this->author; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Set the timestamp of the activity |
|
| 183 | - * |
|
| 184 | - * @param int $timestamp |
|
| 185 | - * @return IEvent |
|
| 186 | - * @throws \InvalidArgumentException if the timestamp is invalid |
|
| 187 | - * @since 8.2.0 |
|
| 188 | - */ |
|
| 189 | - public function setTimestamp(int $timestamp): IEvent { |
|
| 190 | - $this->timestamp = $timestamp; |
|
| 191 | - return $this; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @return int |
|
| 196 | - */ |
|
| 197 | - public function getTimestamp(): int { |
|
| 198 | - return $this->timestamp; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Set the subject of the activity |
|
| 203 | - * |
|
| 204 | - * @param string $subject |
|
| 205 | - * @param array $parameters |
|
| 206 | - * @return IEvent |
|
| 207 | - * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 208 | - * @since 8.2.0 |
|
| 209 | - */ |
|
| 210 | - public function setSubject(string $subject, array $parameters = []): IEvent { |
|
| 211 | - if (isset($subject[255])) { |
|
| 212 | - throw new \InvalidArgumentException('The given subject is invalid'); |
|
| 213 | - } |
|
| 214 | - $this->subject = $subject; |
|
| 215 | - $this->subjectParameters = $parameters; |
|
| 216 | - return $this; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - public function getSubject(): string { |
|
| 223 | - return $this->subject; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @return array |
|
| 228 | - */ |
|
| 229 | - public function getSubjectParameters(): array { |
|
| 230 | - return $this->subjectParameters; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @param string $subject |
|
| 235 | - * @return $this |
|
| 236 | - * @throws \InvalidArgumentException if the subject is invalid |
|
| 237 | - * @since 11.0.0 |
|
| 238 | - */ |
|
| 239 | - public function setParsedSubject(string $subject): IEvent { |
|
| 240 | - if ($subject === '') { |
|
| 241 | - throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 242 | - } |
|
| 243 | - $this->subjectParsed = $subject; |
|
| 244 | - return $this; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @return string |
|
| 249 | - * @since 11.0.0 |
|
| 250 | - */ |
|
| 251 | - public function getParsedSubject(): string { |
|
| 252 | - return $this->subjectParsed; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @param string $subject |
|
| 257 | - * @param array $parameters |
|
| 258 | - * @return $this |
|
| 259 | - * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 260 | - * @since 11.0.0 |
|
| 261 | - */ |
|
| 262 | - public function setRichSubject(string $subject, array $parameters = []): IEvent { |
|
| 263 | - if ($subject === '') { |
|
| 264 | - throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 265 | - } |
|
| 266 | - $this->subjectRich = $subject; |
|
| 267 | - $this->subjectRichParameters = $parameters; |
|
| 268 | - |
|
| 269 | - return $this; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * @return string |
|
| 274 | - * @since 11.0.0 |
|
| 275 | - */ |
|
| 276 | - public function getRichSubject(): string { |
|
| 277 | - return $this->subjectRich; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * @return array[] |
|
| 282 | - * @since 11.0.0 |
|
| 283 | - */ |
|
| 284 | - public function getRichSubjectParameters(): array { |
|
| 285 | - return $this->subjectRichParameters; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Set the message of the activity |
|
| 290 | - * |
|
| 291 | - * @param string $message |
|
| 292 | - * @param array $parameters |
|
| 293 | - * @return IEvent |
|
| 294 | - * @throws \InvalidArgumentException if the message or parameters are invalid |
|
| 295 | - * @since 8.2.0 |
|
| 296 | - */ |
|
| 297 | - public function setMessage(string $message, array $parameters = []): IEvent { |
|
| 298 | - if (isset($message[255])) { |
|
| 299 | - throw new \InvalidArgumentException('The given message is invalid'); |
|
| 300 | - } |
|
| 301 | - $this->message = $message; |
|
| 302 | - $this->messageParameters = $parameters; |
|
| 303 | - return $this; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * @return string |
|
| 308 | - */ |
|
| 309 | - public function getMessage(): string { |
|
| 310 | - return $this->message; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @return array |
|
| 315 | - */ |
|
| 316 | - public function getMessageParameters(): array { |
|
| 317 | - return $this->messageParameters; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * @param string $message |
|
| 322 | - * @return $this |
|
| 323 | - * @throws \InvalidArgumentException if the message is invalid |
|
| 324 | - * @since 11.0.0 |
|
| 325 | - */ |
|
| 326 | - public function setParsedMessage(string $message): IEvent { |
|
| 327 | - $this->messageParsed = $message; |
|
| 328 | - return $this; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @return string |
|
| 333 | - * @since 11.0.0 |
|
| 334 | - */ |
|
| 335 | - public function getParsedMessage(): string { |
|
| 336 | - return $this->messageParsed; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * @param string $message |
|
| 341 | - * @param array $parameters |
|
| 342 | - * @return $this |
|
| 343 | - * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 344 | - * @since 11.0.0 |
|
| 345 | - */ |
|
| 346 | - public function setRichMessage(string $message, array $parameters = []): IEvent { |
|
| 347 | - $this->messageRich = $message; |
|
| 348 | - $this->messageRichParameters = $parameters; |
|
| 349 | - |
|
| 350 | - return $this; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @return string |
|
| 355 | - * @since 11.0.0 |
|
| 356 | - */ |
|
| 357 | - public function getRichMessage(): string { |
|
| 358 | - return $this->messageRich; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * @return array[] |
|
| 363 | - * @since 11.0.0 |
|
| 364 | - */ |
|
| 365 | - public function getRichMessageParameters(): array { |
|
| 366 | - return $this->messageRichParameters; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set the object of the activity |
|
| 371 | - * |
|
| 372 | - * @param string $objectType |
|
| 373 | - * @param int $objectId |
|
| 374 | - * @param string $objectName |
|
| 375 | - * @return IEvent |
|
| 376 | - * @throws \InvalidArgumentException if the object is invalid |
|
| 377 | - * @since 8.2.0 |
|
| 378 | - */ |
|
| 379 | - public function setObject(string $objectType, int $objectId, string $objectName = ''): IEvent { |
|
| 380 | - if (isset($objectType[255])) { |
|
| 381 | - throw new \InvalidArgumentException('The given object type is invalid'); |
|
| 382 | - } |
|
| 383 | - if (isset($objectName[4000])) { |
|
| 384 | - throw new \InvalidArgumentException('The given object name is invalid'); |
|
| 385 | - } |
|
| 386 | - $this->objectType = $objectType; |
|
| 387 | - $this->objectId = $objectId; |
|
| 388 | - $this->objectName = $objectName; |
|
| 389 | - return $this; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * @return string |
|
| 394 | - */ |
|
| 395 | - public function getObjectType(): string { |
|
| 396 | - return $this->objectType; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * @return int |
|
| 401 | - */ |
|
| 402 | - public function getObjectId(): int { |
|
| 403 | - return $this->objectId; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - public function getObjectName(): string { |
|
| 410 | - return $this->objectName; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Set the link of the activity |
|
| 415 | - * |
|
| 416 | - * @param string $link |
|
| 417 | - * @return IEvent |
|
| 418 | - * @throws \InvalidArgumentException if the link is invalid |
|
| 419 | - * @since 8.2.0 |
|
| 420 | - */ |
|
| 421 | - public function setLink(string $link): IEvent { |
|
| 422 | - if (isset($link[4000])) { |
|
| 423 | - throw new \InvalidArgumentException('The given link is invalid'); |
|
| 424 | - } |
|
| 425 | - $this->link = $link; |
|
| 426 | - return $this; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * @return string |
|
| 431 | - */ |
|
| 432 | - public function getLink(): string { |
|
| 433 | - return $this->link; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * @param string $icon |
|
| 438 | - * @return $this |
|
| 439 | - * @throws \InvalidArgumentException if the icon is invalid |
|
| 440 | - * @since 11.0.0 |
|
| 441 | - */ |
|
| 442 | - public function setIcon(string $icon): IEvent { |
|
| 443 | - if (isset($icon[4000])) { |
|
| 444 | - throw new \InvalidArgumentException('The given icon is invalid'); |
|
| 445 | - } |
|
| 446 | - $this->icon = $icon; |
|
| 447 | - return $this; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * @return string |
|
| 452 | - * @since 11.0.0 |
|
| 453 | - */ |
|
| 454 | - public function getIcon(): string { |
|
| 455 | - return $this->icon; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * @param IEvent $child |
|
| 460 | - * @return $this |
|
| 461 | - * @since 11.0.0 - Since 15.0.0 returns $this |
|
| 462 | - */ |
|
| 463 | - public function setChildEvent(IEvent $child): IEvent { |
|
| 464 | - $this->child = $child; |
|
| 465 | - return $this; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * @return IEvent|null |
|
| 470 | - * @since 11.0.0 |
|
| 471 | - */ |
|
| 472 | - public function getChildEvent() { |
|
| 473 | - return $this->child; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * @return bool |
|
| 478 | - * @since 8.2.0 |
|
| 479 | - */ |
|
| 480 | - public function isValid(): bool { |
|
| 481 | - return |
|
| 482 | - $this->isValidCommon() |
|
| 483 | - && |
|
| 484 | - $this->getSubject() !== '' |
|
| 485 | - ; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * @return bool |
|
| 490 | - * @since 8.2.0 |
|
| 491 | - */ |
|
| 492 | - public function isValidParsed(): bool { |
|
| 493 | - if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { |
|
| 494 | - try { |
|
| 495 | - $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); |
|
| 496 | - } catch (InvalidObjectExeption $e) { |
|
| 497 | - return false; |
|
| 498 | - } |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) { |
|
| 502 | - try { |
|
| 503 | - $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters()); |
|
| 504 | - } catch (InvalidObjectExeption $e) { |
|
| 505 | - return false; |
|
| 506 | - } |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - return |
|
| 510 | - $this->isValidCommon() |
|
| 511 | - && |
|
| 512 | - $this->getParsedSubject() !== '' |
|
| 513 | - ; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - protected function isValidCommon(): bool { |
|
| 517 | - return |
|
| 518 | - $this->getApp() !== '' |
|
| 519 | - && |
|
| 520 | - $this->getType() !== '' |
|
| 521 | - && |
|
| 522 | - $this->getAffectedUser() !== '' |
|
| 523 | - && |
|
| 524 | - $this->getTimestamp() !== 0 |
|
| 525 | - /** |
|
| 526 | - * Disabled for BC with old activities |
|
| 527 | - * && |
|
| 528 | - * $this->getObjectType() !== '' |
|
| 529 | - * && |
|
| 530 | - * $this->getObjectId() !== 0 |
|
| 531 | - */ |
|
| 532 | - ; |
|
| 533 | - } |
|
| 36 | + /** @var string */ |
|
| 37 | + protected $app = ''; |
|
| 38 | + /** @var string */ |
|
| 39 | + protected $type = ''; |
|
| 40 | + /** @var string */ |
|
| 41 | + protected $affectedUser = ''; |
|
| 42 | + /** @var string */ |
|
| 43 | + protected $author = ''; |
|
| 44 | + /** @var int */ |
|
| 45 | + protected $timestamp = 0; |
|
| 46 | + /** @var string */ |
|
| 47 | + protected $subject = ''; |
|
| 48 | + /** @var array */ |
|
| 49 | + protected $subjectParameters = []; |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $subjectParsed = ''; |
|
| 52 | + /** @var string */ |
|
| 53 | + protected $subjectRich = ''; |
|
| 54 | + /** @var array */ |
|
| 55 | + protected $subjectRichParameters = []; |
|
| 56 | + /** @var string */ |
|
| 57 | + protected $message = ''; |
|
| 58 | + /** @var array */ |
|
| 59 | + protected $messageParameters = []; |
|
| 60 | + /** @var string */ |
|
| 61 | + protected $messageParsed = ''; |
|
| 62 | + /** @var string */ |
|
| 63 | + protected $messageRich = ''; |
|
| 64 | + /** @var array */ |
|
| 65 | + protected $messageRichParameters = []; |
|
| 66 | + /** @var string */ |
|
| 67 | + protected $objectType = ''; |
|
| 68 | + /** @var int */ |
|
| 69 | + protected $objectId = 0; |
|
| 70 | + /** @var string */ |
|
| 71 | + protected $objectName = ''; |
|
| 72 | + /** @var string */ |
|
| 73 | + protected $link = ''; |
|
| 74 | + /** @var string */ |
|
| 75 | + protected $icon = ''; |
|
| 76 | + |
|
| 77 | + /** @var IEvent|null */ |
|
| 78 | + protected $child; |
|
| 79 | + /** @var IValidator */ |
|
| 80 | + protected $richValidator; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param IValidator $richValidator |
|
| 84 | + */ |
|
| 85 | + public function __construct(IValidator $richValidator) { |
|
| 86 | + $this->richValidator = $richValidator; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Set the app of the activity |
|
| 91 | + * |
|
| 92 | + * @param string $app |
|
| 93 | + * @return IEvent |
|
| 94 | + * @throws \InvalidArgumentException if the app id is invalid |
|
| 95 | + * @since 8.2.0 |
|
| 96 | + */ |
|
| 97 | + public function setApp(string $app): IEvent { |
|
| 98 | + if ($app === '' || isset($app[32])) { |
|
| 99 | + throw new \InvalidArgumentException('The given app is invalid'); |
|
| 100 | + } |
|
| 101 | + $this->app = $app; |
|
| 102 | + return $this; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function getApp(): string { |
|
| 109 | + return $this->app; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Set the type of the activity |
|
| 114 | + * |
|
| 115 | + * @param string $type |
|
| 116 | + * @return IEvent |
|
| 117 | + * @throws \InvalidArgumentException if the type is invalid |
|
| 118 | + * @since 8.2.0 |
|
| 119 | + */ |
|
| 120 | + public function setType(string $type): IEvent { |
|
| 121 | + if ($type === '' || isset($type[255])) { |
|
| 122 | + throw new \InvalidArgumentException('The given type is invalid'); |
|
| 123 | + } |
|
| 124 | + $this->type = $type; |
|
| 125 | + return $this; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + public function getType(): string { |
|
| 132 | + return $this->type; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Set the affected user of the activity |
|
| 137 | + * |
|
| 138 | + * @param string $affectedUser |
|
| 139 | + * @return IEvent |
|
| 140 | + * @throws \InvalidArgumentException if the affected user is invalid |
|
| 141 | + * @since 8.2.0 |
|
| 142 | + */ |
|
| 143 | + public function setAffectedUser(string $affectedUser): IEvent { |
|
| 144 | + if ($affectedUser === '' || isset($affectedUser[64])) { |
|
| 145 | + throw new \InvalidArgumentException('The given affected user is invalid'); |
|
| 146 | + } |
|
| 147 | + $this->affectedUser = $affectedUser; |
|
| 148 | + return $this; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @return string |
|
| 153 | + */ |
|
| 154 | + public function getAffectedUser(): string { |
|
| 155 | + return $this->affectedUser; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Set the author of the activity |
|
| 160 | + * |
|
| 161 | + * @param string $author |
|
| 162 | + * @return IEvent |
|
| 163 | + * @throws \InvalidArgumentException if the author is invalid |
|
| 164 | + * @since 8.2.0 |
|
| 165 | + */ |
|
| 166 | + public function setAuthor(string $author): IEvent { |
|
| 167 | + if (isset($author[64])) { |
|
| 168 | + throw new \InvalidArgumentException('The given author user is invalid'); |
|
| 169 | + } |
|
| 170 | + $this->author = $author; |
|
| 171 | + return $this; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @return string |
|
| 176 | + */ |
|
| 177 | + public function getAuthor(): string { |
|
| 178 | + return $this->author; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Set the timestamp of the activity |
|
| 183 | + * |
|
| 184 | + * @param int $timestamp |
|
| 185 | + * @return IEvent |
|
| 186 | + * @throws \InvalidArgumentException if the timestamp is invalid |
|
| 187 | + * @since 8.2.0 |
|
| 188 | + */ |
|
| 189 | + public function setTimestamp(int $timestamp): IEvent { |
|
| 190 | + $this->timestamp = $timestamp; |
|
| 191 | + return $this; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @return int |
|
| 196 | + */ |
|
| 197 | + public function getTimestamp(): int { |
|
| 198 | + return $this->timestamp; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Set the subject of the activity |
|
| 203 | + * |
|
| 204 | + * @param string $subject |
|
| 205 | + * @param array $parameters |
|
| 206 | + * @return IEvent |
|
| 207 | + * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 208 | + * @since 8.2.0 |
|
| 209 | + */ |
|
| 210 | + public function setSubject(string $subject, array $parameters = []): IEvent { |
|
| 211 | + if (isset($subject[255])) { |
|
| 212 | + throw new \InvalidArgumentException('The given subject is invalid'); |
|
| 213 | + } |
|
| 214 | + $this->subject = $subject; |
|
| 215 | + $this->subjectParameters = $parameters; |
|
| 216 | + return $this; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + public function getSubject(): string { |
|
| 223 | + return $this->subject; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @return array |
|
| 228 | + */ |
|
| 229 | + public function getSubjectParameters(): array { |
|
| 230 | + return $this->subjectParameters; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @param string $subject |
|
| 235 | + * @return $this |
|
| 236 | + * @throws \InvalidArgumentException if the subject is invalid |
|
| 237 | + * @since 11.0.0 |
|
| 238 | + */ |
|
| 239 | + public function setParsedSubject(string $subject): IEvent { |
|
| 240 | + if ($subject === '') { |
|
| 241 | + throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 242 | + } |
|
| 243 | + $this->subjectParsed = $subject; |
|
| 244 | + return $this; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @return string |
|
| 249 | + * @since 11.0.0 |
|
| 250 | + */ |
|
| 251 | + public function getParsedSubject(): string { |
|
| 252 | + return $this->subjectParsed; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @param string $subject |
|
| 257 | + * @param array $parameters |
|
| 258 | + * @return $this |
|
| 259 | + * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 260 | + * @since 11.0.0 |
|
| 261 | + */ |
|
| 262 | + public function setRichSubject(string $subject, array $parameters = []): IEvent { |
|
| 263 | + if ($subject === '') { |
|
| 264 | + throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 265 | + } |
|
| 266 | + $this->subjectRich = $subject; |
|
| 267 | + $this->subjectRichParameters = $parameters; |
|
| 268 | + |
|
| 269 | + return $this; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * @return string |
|
| 274 | + * @since 11.0.0 |
|
| 275 | + */ |
|
| 276 | + public function getRichSubject(): string { |
|
| 277 | + return $this->subjectRich; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * @return array[] |
|
| 282 | + * @since 11.0.0 |
|
| 283 | + */ |
|
| 284 | + public function getRichSubjectParameters(): array { |
|
| 285 | + return $this->subjectRichParameters; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Set the message of the activity |
|
| 290 | + * |
|
| 291 | + * @param string $message |
|
| 292 | + * @param array $parameters |
|
| 293 | + * @return IEvent |
|
| 294 | + * @throws \InvalidArgumentException if the message or parameters are invalid |
|
| 295 | + * @since 8.2.0 |
|
| 296 | + */ |
|
| 297 | + public function setMessage(string $message, array $parameters = []): IEvent { |
|
| 298 | + if (isset($message[255])) { |
|
| 299 | + throw new \InvalidArgumentException('The given message is invalid'); |
|
| 300 | + } |
|
| 301 | + $this->message = $message; |
|
| 302 | + $this->messageParameters = $parameters; |
|
| 303 | + return $this; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * @return string |
|
| 308 | + */ |
|
| 309 | + public function getMessage(): string { |
|
| 310 | + return $this->message; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @return array |
|
| 315 | + */ |
|
| 316 | + public function getMessageParameters(): array { |
|
| 317 | + return $this->messageParameters; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * @param string $message |
|
| 322 | + * @return $this |
|
| 323 | + * @throws \InvalidArgumentException if the message is invalid |
|
| 324 | + * @since 11.0.0 |
|
| 325 | + */ |
|
| 326 | + public function setParsedMessage(string $message): IEvent { |
|
| 327 | + $this->messageParsed = $message; |
|
| 328 | + return $this; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @return string |
|
| 333 | + * @since 11.0.0 |
|
| 334 | + */ |
|
| 335 | + public function getParsedMessage(): string { |
|
| 336 | + return $this->messageParsed; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * @param string $message |
|
| 341 | + * @param array $parameters |
|
| 342 | + * @return $this |
|
| 343 | + * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 344 | + * @since 11.0.0 |
|
| 345 | + */ |
|
| 346 | + public function setRichMessage(string $message, array $parameters = []): IEvent { |
|
| 347 | + $this->messageRich = $message; |
|
| 348 | + $this->messageRichParameters = $parameters; |
|
| 349 | + |
|
| 350 | + return $this; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @return string |
|
| 355 | + * @since 11.0.0 |
|
| 356 | + */ |
|
| 357 | + public function getRichMessage(): string { |
|
| 358 | + return $this->messageRich; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * @return array[] |
|
| 363 | + * @since 11.0.0 |
|
| 364 | + */ |
|
| 365 | + public function getRichMessageParameters(): array { |
|
| 366 | + return $this->messageRichParameters; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set the object of the activity |
|
| 371 | + * |
|
| 372 | + * @param string $objectType |
|
| 373 | + * @param int $objectId |
|
| 374 | + * @param string $objectName |
|
| 375 | + * @return IEvent |
|
| 376 | + * @throws \InvalidArgumentException if the object is invalid |
|
| 377 | + * @since 8.2.0 |
|
| 378 | + */ |
|
| 379 | + public function setObject(string $objectType, int $objectId, string $objectName = ''): IEvent { |
|
| 380 | + if (isset($objectType[255])) { |
|
| 381 | + throw new \InvalidArgumentException('The given object type is invalid'); |
|
| 382 | + } |
|
| 383 | + if (isset($objectName[4000])) { |
|
| 384 | + throw new \InvalidArgumentException('The given object name is invalid'); |
|
| 385 | + } |
|
| 386 | + $this->objectType = $objectType; |
|
| 387 | + $this->objectId = $objectId; |
|
| 388 | + $this->objectName = $objectName; |
|
| 389 | + return $this; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * @return string |
|
| 394 | + */ |
|
| 395 | + public function getObjectType(): string { |
|
| 396 | + return $this->objectType; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * @return int |
|
| 401 | + */ |
|
| 402 | + public function getObjectId(): int { |
|
| 403 | + return $this->objectId; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + public function getObjectName(): string { |
|
| 410 | + return $this->objectName; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Set the link of the activity |
|
| 415 | + * |
|
| 416 | + * @param string $link |
|
| 417 | + * @return IEvent |
|
| 418 | + * @throws \InvalidArgumentException if the link is invalid |
|
| 419 | + * @since 8.2.0 |
|
| 420 | + */ |
|
| 421 | + public function setLink(string $link): IEvent { |
|
| 422 | + if (isset($link[4000])) { |
|
| 423 | + throw new \InvalidArgumentException('The given link is invalid'); |
|
| 424 | + } |
|
| 425 | + $this->link = $link; |
|
| 426 | + return $this; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * @return string |
|
| 431 | + */ |
|
| 432 | + public function getLink(): string { |
|
| 433 | + return $this->link; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * @param string $icon |
|
| 438 | + * @return $this |
|
| 439 | + * @throws \InvalidArgumentException if the icon is invalid |
|
| 440 | + * @since 11.0.0 |
|
| 441 | + */ |
|
| 442 | + public function setIcon(string $icon): IEvent { |
|
| 443 | + if (isset($icon[4000])) { |
|
| 444 | + throw new \InvalidArgumentException('The given icon is invalid'); |
|
| 445 | + } |
|
| 446 | + $this->icon = $icon; |
|
| 447 | + return $this; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * @return string |
|
| 452 | + * @since 11.0.0 |
|
| 453 | + */ |
|
| 454 | + public function getIcon(): string { |
|
| 455 | + return $this->icon; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * @param IEvent $child |
|
| 460 | + * @return $this |
|
| 461 | + * @since 11.0.0 - Since 15.0.0 returns $this |
|
| 462 | + */ |
|
| 463 | + public function setChildEvent(IEvent $child): IEvent { |
|
| 464 | + $this->child = $child; |
|
| 465 | + return $this; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * @return IEvent|null |
|
| 470 | + * @since 11.0.0 |
|
| 471 | + */ |
|
| 472 | + public function getChildEvent() { |
|
| 473 | + return $this->child; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * @return bool |
|
| 478 | + * @since 8.2.0 |
|
| 479 | + */ |
|
| 480 | + public function isValid(): bool { |
|
| 481 | + return |
|
| 482 | + $this->isValidCommon() |
|
| 483 | + && |
|
| 484 | + $this->getSubject() !== '' |
|
| 485 | + ; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * @return bool |
|
| 490 | + * @since 8.2.0 |
|
| 491 | + */ |
|
| 492 | + public function isValidParsed(): bool { |
|
| 493 | + if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { |
|
| 494 | + try { |
|
| 495 | + $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); |
|
| 496 | + } catch (InvalidObjectExeption $e) { |
|
| 497 | + return false; |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) { |
|
| 502 | + try { |
|
| 503 | + $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters()); |
|
| 504 | + } catch (InvalidObjectExeption $e) { |
|
| 505 | + return false; |
|
| 506 | + } |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + return |
|
| 510 | + $this->isValidCommon() |
|
| 511 | + && |
|
| 512 | + $this->getParsedSubject() !== '' |
|
| 513 | + ; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + protected function isValidCommon(): bool { |
|
| 517 | + return |
|
| 518 | + $this->getApp() !== '' |
|
| 519 | + && |
|
| 520 | + $this->getType() !== '' |
|
| 521 | + && |
|
| 522 | + $this->getAffectedUser() !== '' |
|
| 523 | + && |
|
| 524 | + $this->getTimestamp() !== 0 |
|
| 525 | + /** |
|
| 526 | + * Disabled for BC with old activities |
|
| 527 | + * && |
|
| 528 | + * $this->getObjectType() !== '' |
|
| 529 | + * && |
|
| 530 | + * $this->getObjectId() !== 0 |
|
| 531 | + */ |
|
| 532 | + ; |
|
| 533 | + } |
|
| 534 | 534 | } |
@@ -39,242 +39,242 @@ |
||
| 39 | 39 | |
| 40 | 40 | class Manager implements IManager { |
| 41 | 41 | |
| 42 | - /** @var array */ |
|
| 43 | - protected $encryptionModules; |
|
| 44 | - |
|
| 45 | - /** @var IConfig */ |
|
| 46 | - protected $config; |
|
| 47 | - |
|
| 48 | - /** @var ILogger */ |
|
| 49 | - protected $logger; |
|
| 50 | - |
|
| 51 | - /** @var Il10n */ |
|
| 52 | - protected $l; |
|
| 53 | - |
|
| 54 | - /** @var View */ |
|
| 55 | - protected $rootView; |
|
| 56 | - |
|
| 57 | - /** @var Util */ |
|
| 58 | - protected $util; |
|
| 59 | - |
|
| 60 | - /** @var ArrayCache */ |
|
| 61 | - protected $arrayCache; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param IConfig $config |
|
| 65 | - * @param ILogger $logger |
|
| 66 | - * @param IL10N $l10n |
|
| 67 | - * @param View $rootView |
|
| 68 | - * @param Util $util |
|
| 69 | - * @param ArrayCache $arrayCache |
|
| 70 | - */ |
|
| 71 | - public function __construct(IConfig $config, ILogger $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) { |
|
| 72 | - $this->encryptionModules = []; |
|
| 73 | - $this->config = $config; |
|
| 74 | - $this->logger = $logger; |
|
| 75 | - $this->l = $l10n; |
|
| 76 | - $this->rootView = $rootView; |
|
| 77 | - $this->util = $util; |
|
| 78 | - $this->arrayCache = $arrayCache; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Check if encryption is enabled |
|
| 83 | - * |
|
| 84 | - * @return bool true if enabled, false if not |
|
| 85 | - */ |
|
| 86 | - public function isEnabled() { |
|
| 87 | - |
|
| 88 | - $installed = $this->config->getSystemValue('installed', false); |
|
| 89 | - if (!$installed) { |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
| 94 | - return $enabled === 'yes'; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * check if new encryption is ready |
|
| 99 | - * |
|
| 100 | - * @return bool |
|
| 101 | - * @throws ServiceUnavailableException |
|
| 102 | - */ |
|
| 103 | - public function isReady() { |
|
| 104 | - |
|
| 105 | - if ($this->isKeyStorageReady() === false) { |
|
| 106 | - throw new ServiceUnavailableException('Key Storage is not ready'); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return true; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @param string $user |
|
| 114 | - */ |
|
| 115 | - public function isReadyForUser($user) { |
|
| 116 | - if (!$this->isReady()) { |
|
| 117 | - return false; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - foreach ($this->getEncryptionModules() as $module) { |
|
| 121 | - /** @var IEncryptionModule $m */ |
|
| 122 | - $m = call_user_func($module['callback']); |
|
| 123 | - if (!$m->isReadyForUser($user)) { |
|
| 124 | - return false; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Registers an callback function which must return an encryption module instance |
|
| 133 | - * |
|
| 134 | - * @param string $id |
|
| 135 | - * @param string $displayName |
|
| 136 | - * @param callable $callback |
|
| 137 | - * @throws Exceptions\ModuleAlreadyExistsException |
|
| 138 | - */ |
|
| 139 | - public function registerEncryptionModule($id, $displayName, callable $callback) { |
|
| 140 | - |
|
| 141 | - if (isset($this->encryptionModules[$id])) { |
|
| 142 | - throw new Exceptions\ModuleAlreadyExistsException($id, $displayName); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - $this->encryptionModules[$id] = [ |
|
| 146 | - 'id' => $id, |
|
| 147 | - 'displayName' => $displayName, |
|
| 148 | - 'callback' => $callback, |
|
| 149 | - ]; |
|
| 150 | - |
|
| 151 | - $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 152 | - |
|
| 153 | - if (empty($defaultEncryptionModuleId)) { |
|
| 154 | - $this->setDefaultEncryptionModule($id); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Unregisters an encryption module |
|
| 160 | - * |
|
| 161 | - * @param string $moduleId |
|
| 162 | - */ |
|
| 163 | - public function unregisterEncryptionModule($moduleId) { |
|
| 164 | - unset($this->encryptionModules[$moduleId]); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * get a list of all encryption modules |
|
| 169 | - * |
|
| 170 | - * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
| 171 | - */ |
|
| 172 | - public function getEncryptionModules() { |
|
| 173 | - return $this->encryptionModules; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * get a specific encryption module |
|
| 178 | - * |
|
| 179 | - * @param string $moduleId |
|
| 180 | - * @return IEncryptionModule |
|
| 181 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
| 182 | - */ |
|
| 183 | - public function getEncryptionModule($moduleId = '') { |
|
| 184 | - if (!empty($moduleId)) { |
|
| 185 | - if (isset($this->encryptionModules[$moduleId])) { |
|
| 186 | - return call_user_func($this->encryptionModules[$moduleId]['callback']); |
|
| 187 | - } else { |
|
| 188 | - $message = "Module with ID: $moduleId does not exist."; |
|
| 189 | - $hint = $this->l->t('Module with ID: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]); |
|
| 190 | - throw new Exceptions\ModuleDoesNotExistsException($message, $hint); |
|
| 191 | - } |
|
| 192 | - } else { |
|
| 193 | - return $this->getDefaultEncryptionModule(); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * get default encryption module |
|
| 199 | - * |
|
| 200 | - * @return \OCP\Encryption\IEncryptionModule |
|
| 201 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
| 202 | - */ |
|
| 203 | - protected function getDefaultEncryptionModule() { |
|
| 204 | - $defaultModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 205 | - if (!empty($defaultModuleId)) { |
|
| 206 | - if (isset($this->encryptionModules[$defaultModuleId])) { |
|
| 207 | - return call_user_func($this->encryptionModules[$defaultModuleId]['callback']); |
|
| 208 | - } else { |
|
| 209 | - $message = 'Default encryption module not loaded'; |
|
| 210 | - throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 211 | - } |
|
| 212 | - } else { |
|
| 213 | - $message = 'No default encryption module defined'; |
|
| 214 | - throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * set default encryption module Id |
|
| 221 | - * |
|
| 222 | - * @param string $moduleId |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 225 | - public function setDefaultEncryptionModule($moduleId) { |
|
| 226 | - try { |
|
| 227 | - $this->getEncryptionModule($moduleId); |
|
| 228 | - } catch (\Exception $e) { |
|
| 229 | - return false; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - $this->config->setAppValue('core', 'default_encryption_module', $moduleId); |
|
| 233 | - return true; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * get default encryption module Id |
|
| 238 | - * |
|
| 239 | - * @return string |
|
| 240 | - */ |
|
| 241 | - public function getDefaultEncryptionModuleId() { |
|
| 242 | - return $this->config->getAppValue('core', 'default_encryption_module'); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Add storage wrapper |
|
| 247 | - */ |
|
| 248 | - public function setupStorage() { |
|
| 249 | - // If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper |
|
| 250 | - if (!empty($this->encryptionModules) || $this->isEnabled()) { |
|
| 251 | - $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger); |
|
| 252 | - Filesystem::addStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage'], 2); |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * check if key storage is ready |
|
| 259 | - * |
|
| 260 | - * @return bool |
|
| 261 | - */ |
|
| 262 | - protected function isKeyStorageReady() { |
|
| 263 | - |
|
| 264 | - $rootDir = $this->util->getKeyStorageRoot(); |
|
| 265 | - |
|
| 266 | - // the default root is always valid |
|
| 267 | - if ($rootDir === '') { |
|
| 268 | - return true; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // check if key storage is mounted correctly |
|
| 272 | - if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { |
|
| 273 | - return true; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return false; |
|
| 277 | - } |
|
| 42 | + /** @var array */ |
|
| 43 | + protected $encryptionModules; |
|
| 44 | + |
|
| 45 | + /** @var IConfig */ |
|
| 46 | + protected $config; |
|
| 47 | + |
|
| 48 | + /** @var ILogger */ |
|
| 49 | + protected $logger; |
|
| 50 | + |
|
| 51 | + /** @var Il10n */ |
|
| 52 | + protected $l; |
|
| 53 | + |
|
| 54 | + /** @var View */ |
|
| 55 | + protected $rootView; |
|
| 56 | + |
|
| 57 | + /** @var Util */ |
|
| 58 | + protected $util; |
|
| 59 | + |
|
| 60 | + /** @var ArrayCache */ |
|
| 61 | + protected $arrayCache; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param IConfig $config |
|
| 65 | + * @param ILogger $logger |
|
| 66 | + * @param IL10N $l10n |
|
| 67 | + * @param View $rootView |
|
| 68 | + * @param Util $util |
|
| 69 | + * @param ArrayCache $arrayCache |
|
| 70 | + */ |
|
| 71 | + public function __construct(IConfig $config, ILogger $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) { |
|
| 72 | + $this->encryptionModules = []; |
|
| 73 | + $this->config = $config; |
|
| 74 | + $this->logger = $logger; |
|
| 75 | + $this->l = $l10n; |
|
| 76 | + $this->rootView = $rootView; |
|
| 77 | + $this->util = $util; |
|
| 78 | + $this->arrayCache = $arrayCache; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Check if encryption is enabled |
|
| 83 | + * |
|
| 84 | + * @return bool true if enabled, false if not |
|
| 85 | + */ |
|
| 86 | + public function isEnabled() { |
|
| 87 | + |
|
| 88 | + $installed = $this->config->getSystemValue('installed', false); |
|
| 89 | + if (!$installed) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
| 94 | + return $enabled === 'yes'; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * check if new encryption is ready |
|
| 99 | + * |
|
| 100 | + * @return bool |
|
| 101 | + * @throws ServiceUnavailableException |
|
| 102 | + */ |
|
| 103 | + public function isReady() { |
|
| 104 | + |
|
| 105 | + if ($this->isKeyStorageReady() === false) { |
|
| 106 | + throw new ServiceUnavailableException('Key Storage is not ready'); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return true; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @param string $user |
|
| 114 | + */ |
|
| 115 | + public function isReadyForUser($user) { |
|
| 116 | + if (!$this->isReady()) { |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + foreach ($this->getEncryptionModules() as $module) { |
|
| 121 | + /** @var IEncryptionModule $m */ |
|
| 122 | + $m = call_user_func($module['callback']); |
|
| 123 | + if (!$m->isReadyForUser($user)) { |
|
| 124 | + return false; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Registers an callback function which must return an encryption module instance |
|
| 133 | + * |
|
| 134 | + * @param string $id |
|
| 135 | + * @param string $displayName |
|
| 136 | + * @param callable $callback |
|
| 137 | + * @throws Exceptions\ModuleAlreadyExistsException |
|
| 138 | + */ |
|
| 139 | + public function registerEncryptionModule($id, $displayName, callable $callback) { |
|
| 140 | + |
|
| 141 | + if (isset($this->encryptionModules[$id])) { |
|
| 142 | + throw new Exceptions\ModuleAlreadyExistsException($id, $displayName); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + $this->encryptionModules[$id] = [ |
|
| 146 | + 'id' => $id, |
|
| 147 | + 'displayName' => $displayName, |
|
| 148 | + 'callback' => $callback, |
|
| 149 | + ]; |
|
| 150 | + |
|
| 151 | + $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 152 | + |
|
| 153 | + if (empty($defaultEncryptionModuleId)) { |
|
| 154 | + $this->setDefaultEncryptionModule($id); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Unregisters an encryption module |
|
| 160 | + * |
|
| 161 | + * @param string $moduleId |
|
| 162 | + */ |
|
| 163 | + public function unregisterEncryptionModule($moduleId) { |
|
| 164 | + unset($this->encryptionModules[$moduleId]); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * get a list of all encryption modules |
|
| 169 | + * |
|
| 170 | + * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
| 171 | + */ |
|
| 172 | + public function getEncryptionModules() { |
|
| 173 | + return $this->encryptionModules; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * get a specific encryption module |
|
| 178 | + * |
|
| 179 | + * @param string $moduleId |
|
| 180 | + * @return IEncryptionModule |
|
| 181 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
| 182 | + */ |
|
| 183 | + public function getEncryptionModule($moduleId = '') { |
|
| 184 | + if (!empty($moduleId)) { |
|
| 185 | + if (isset($this->encryptionModules[$moduleId])) { |
|
| 186 | + return call_user_func($this->encryptionModules[$moduleId]['callback']); |
|
| 187 | + } else { |
|
| 188 | + $message = "Module with ID: $moduleId does not exist."; |
|
| 189 | + $hint = $this->l->t('Module with ID: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]); |
|
| 190 | + throw new Exceptions\ModuleDoesNotExistsException($message, $hint); |
|
| 191 | + } |
|
| 192 | + } else { |
|
| 193 | + return $this->getDefaultEncryptionModule(); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * get default encryption module |
|
| 199 | + * |
|
| 200 | + * @return \OCP\Encryption\IEncryptionModule |
|
| 201 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
| 202 | + */ |
|
| 203 | + protected function getDefaultEncryptionModule() { |
|
| 204 | + $defaultModuleId = $this->getDefaultEncryptionModuleId(); |
|
| 205 | + if (!empty($defaultModuleId)) { |
|
| 206 | + if (isset($this->encryptionModules[$defaultModuleId])) { |
|
| 207 | + return call_user_func($this->encryptionModules[$defaultModuleId]['callback']); |
|
| 208 | + } else { |
|
| 209 | + $message = 'Default encryption module not loaded'; |
|
| 210 | + throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 211 | + } |
|
| 212 | + } else { |
|
| 213 | + $message = 'No default encryption module defined'; |
|
| 214 | + throw new Exceptions\ModuleDoesNotExistsException($message); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * set default encryption module Id |
|
| 221 | + * |
|
| 222 | + * @param string $moduleId |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | + public function setDefaultEncryptionModule($moduleId) { |
|
| 226 | + try { |
|
| 227 | + $this->getEncryptionModule($moduleId); |
|
| 228 | + } catch (\Exception $e) { |
|
| 229 | + return false; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + $this->config->setAppValue('core', 'default_encryption_module', $moduleId); |
|
| 233 | + return true; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * get default encryption module Id |
|
| 238 | + * |
|
| 239 | + * @return string |
|
| 240 | + */ |
|
| 241 | + public function getDefaultEncryptionModuleId() { |
|
| 242 | + return $this->config->getAppValue('core', 'default_encryption_module'); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Add storage wrapper |
|
| 247 | + */ |
|
| 248 | + public function setupStorage() { |
|
| 249 | + // If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper |
|
| 250 | + if (!empty($this->encryptionModules) || $this->isEnabled()) { |
|
| 251 | + $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger); |
|
| 252 | + Filesystem::addStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage'], 2); |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * check if key storage is ready |
|
| 259 | + * |
|
| 260 | + * @return bool |
|
| 261 | + */ |
|
| 262 | + protected function isKeyStorageReady() { |
|
| 263 | + |
|
| 264 | + $rootDir = $this->util->getKeyStorageRoot(); |
|
| 265 | + |
|
| 266 | + // the default root is always valid |
|
| 267 | + if ($rootDir === '') { |
|
| 268 | + return true; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // check if key storage is mounted correctly |
|
| 272 | + if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { |
|
| 273 | + return true; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + return false; |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | 279 | |
| 280 | 280 | } |