1
0

IRequest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. // use OCP namespace for all classes that are considered public.
  9. // This means that they should be used by apps instead of the internal Nextcloud classes
  10. namespace OCP;
  11. /**
  12. * This interface provides an immutable object with with accessors to
  13. * request variables and headers.
  14. *
  15. * Access request variables by method and name.
  16. *
  17. * Examples:
  18. *
  19. * $request->post['myvar']; // Only look for POST variables
  20. * $request->myvar; or $request->{'myvar'}; or $request->{$myvar}
  21. * Looks in the combined GET, POST and urlParams array.
  22. *
  23. * If you access e.g. ->post but the current HTTP request method
  24. * is GET a \LogicException will be thrown.
  25. *
  26. * NOTE:
  27. * - When accessing ->put a stream resource is returned and the accessor
  28. * will return false on subsequent access to ->put or ->patch.
  29. * - When accessing ->patch and the Content-Type is either application/json
  30. * or application/x-www-form-urlencoded (most cases) it will act like ->get
  31. * and ->post and return an array. Otherwise the raw data will be returned.
  32. *
  33. * @property-read string[] $server
  34. * @property-read string[] $urlParams
  35. * @since 6.0.0
  36. */
  37. interface IRequest {
  38. /**
  39. * @since 9.1.0
  40. * @since 28.0.0 The regex has a group matching the version number
  41. */
  42. public const USER_AGENT_CLIENT_ANDROID = '/^Mozilla\/5\.0 \(Android\) (?:ownCloud|Nextcloud)\-android\/([^ ]*).*$/';
  43. /**
  44. * @since 13.0.0
  45. * @since 28.0.0 The regex has a group matching the version number
  46. */
  47. public const USER_AGENT_TALK_ANDROID = '/^Mozilla\/5\.0 \(Android\) Nextcloud\-Talk v([^ ]*).*$/';
  48. /**
  49. * @since 9.1.0
  50. * @since 28.0.0 The regex has a group matching the version number
  51. */
  52. public const USER_AGENT_CLIENT_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (?:mirall|csyncoC)\/([^ ]*).*$/';
  53. /**
  54. * @since 26.0.0
  55. * @since 28.0.0 The regex has a group matching the version number
  56. */
  57. public const USER_AGENT_TALK_DESKTOP = '/^Mozilla\/5\.0 \((?!Android|iOS)[A-Za-z ]+\) Nextcloud\-Talk v([^ ]*).*$/';
  58. /**
  59. * @since 9.1.0
  60. * @since 28.0.0 The regex has a group matching the version number
  61. */
  62. public const USER_AGENT_CLIENT_IOS = '/^Mozilla\/5\.0 \(iOS\) (?:ownCloud|Nextcloud)\-iOS\/([^ ]*).*$/';
  63. /**
  64. * @since 13.0.0
  65. * @since 28.0.0 The regex has a group matching the version number
  66. */
  67. public const USER_AGENT_TALK_IOS = '/^Mozilla\/5\.0 \(iOS\) Nextcloud\-Talk v([^ ]*).*$/';
  68. /**
  69. * @since 13.0.1
  70. * @since 28.0.0 The regex has a group matching the version number
  71. */
  72. public const USER_AGENT_OUTLOOK_ADDON = '/^Mozilla\/5\.0 \([A-Za-z ]+\) Nextcloud\-Outlook v([^ ]*).*$/';
  73. /**
  74. * @since 13.0.1
  75. * @since 28.0.0 The regex has a group matching the version number
  76. */
  77. public const USER_AGENT_THUNDERBIRD_ADDON = '/^Mozilla\/5\.0 \([A-Za-z ]+\) Nextcloud\-Thunderbird v([^ ]*).*$/';
  78. /**
  79. * @since 26.0.0
  80. */
  81. public const JSON_CONTENT_TYPE_REGEX = '/^application\/(?:[a-z0-9.-]+\+)?json\b/';
  82. /**
  83. * @param string $name
  84. *
  85. * @psalm-taint-source input
  86. *
  87. * @return string
  88. * @since 6.0.0
  89. */
  90. public function getHeader(string $name): string;
  91. /**
  92. * Lets you access post and get parameters by the index
  93. * In case of json requests the encoded json body is accessed
  94. *
  95. * @psalm-taint-source input
  96. *
  97. * @param string $key the key which you want to access in the URL Parameter
  98. * placeholder, $_POST or $_GET array.
  99. * The priority how they're returned is the following:
  100. * 1. URL parameters
  101. * 2. POST parameters
  102. * 3. GET parameters
  103. * @param mixed $default If the key is not found, this value will be returned
  104. * @return mixed the content of the array
  105. * @since 6.0.0
  106. */
  107. public function getParam(string $key, $default = null);
  108. /**
  109. * Returns all params that were received, be it from the request
  110. *
  111. * (as GET or POST) or through the URL by the route
  112. *
  113. * @psalm-taint-source input
  114. *
  115. * @return array the array with all parameters
  116. * @since 6.0.0
  117. */
  118. public function getParams(): array;
  119. /**
  120. * Returns the method of the request
  121. *
  122. * @return string the method of the request (POST, GET, etc)
  123. * @since 6.0.0
  124. */
  125. public function getMethod(): string;
  126. /**
  127. * Shortcut for accessing an uploaded file through the $_FILES array
  128. *
  129. * @param string $key the key that will be taken from the $_FILES array
  130. * @return array the file in the $_FILES element
  131. * @since 6.0.0
  132. */
  133. public function getUploadedFile(string $key);
  134. /**
  135. * Shortcut for getting env variables
  136. *
  137. * @param string $key the key that will be taken from the $_ENV array
  138. * @return array the value in the $_ENV element
  139. * @since 6.0.0
  140. */
  141. public function getEnv(string $key);
  142. /**
  143. * Shortcut for getting cookie variables
  144. *
  145. * @psalm-taint-source input
  146. *
  147. * @param string $key the key that will be taken from the $_COOKIE array
  148. * @return string|null the value in the $_COOKIE element
  149. * @since 6.0.0
  150. */
  151. public function getCookie(string $key);
  152. /**
  153. * Checks if the CSRF check was correct
  154. *
  155. * @return bool true if CSRF check passed
  156. * @since 6.0.0
  157. */
  158. public function passesCSRFCheck(): bool;
  159. /**
  160. * Checks if the strict cookie has been sent with the request if the request
  161. * is including any cookies.
  162. *
  163. * @return bool
  164. * @since 9.0.0
  165. */
  166. public function passesStrictCookieCheck(): bool;
  167. /**
  168. * Checks if the lax cookie has been sent with the request if the request
  169. * is including any cookies.
  170. *
  171. * @return bool
  172. * @since 9.0.0
  173. */
  174. public function passesLaxCookieCheck(): bool;
  175. /**
  176. * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging
  177. * If `mod_unique_id` is installed this value will be taken.
  178. *
  179. * @return string
  180. * @since 8.1.0
  181. */
  182. public function getId(): string;
  183. /**
  184. * Returns the remote address, if the connection came from a trusted proxy
  185. * and `forwarded_for_headers` has been configured then the IP address
  186. * specified in this header will be returned instead.
  187. * Do always use this instead of $_SERVER['REMOTE_ADDR']
  188. *
  189. * @return string IP address
  190. * @since 8.1.0
  191. */
  192. public function getRemoteAddress(): string;
  193. /**
  194. * Returns the server protocol. It respects reverse proxy servers and load
  195. * balancers.
  196. *
  197. * @return string Server protocol (http or https)
  198. * @since 8.1.0
  199. */
  200. public function getServerProtocol(): string;
  201. /**
  202. * Returns the used HTTP protocol.
  203. *
  204. * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0.
  205. * @since 8.2.0
  206. */
  207. public function getHttpProtocol(): string;
  208. /**
  209. * Returns the request uri, even if the website uses one or more
  210. * reverse proxies
  211. *
  212. * @psalm-taint-source input
  213. *
  214. * @return string
  215. * @since 8.1.0
  216. */
  217. public function getRequestUri(): string;
  218. /**
  219. * Get raw PathInfo from request (not urldecoded)
  220. *
  221. * @psalm-taint-source input
  222. *
  223. * @throws \Exception
  224. * @return string Path info
  225. * @since 8.1.0
  226. */
  227. public function getRawPathInfo(): string;
  228. /**
  229. * Get PathInfo from request
  230. *
  231. * @psalm-taint-source input
  232. *
  233. * @throws \Exception
  234. * @return string|false Path info or false when not found
  235. * @since 8.1.0
  236. */
  237. public function getPathInfo();
  238. /**
  239. * Returns the script name, even if the website uses one or more
  240. * reverse proxies
  241. *
  242. * @return string the script name
  243. * @since 8.1.0
  244. */
  245. public function getScriptName(): string;
  246. /**
  247. * Checks whether the user agent matches a given regex
  248. *
  249. * @param array $agent array of agent names
  250. * @return bool true if at least one of the given agent matches, false otherwise
  251. * @since 8.1.0
  252. */
  253. public function isUserAgent(array $agent): bool;
  254. /**
  255. * Returns the unverified server host from the headers without checking
  256. * whether it is a trusted domain
  257. *
  258. * @psalm-taint-source input
  259. *
  260. * @return string Server host
  261. * @since 8.1.0
  262. */
  263. public function getInsecureServerHost(): string;
  264. /**
  265. * Returns the server host from the headers, or the first configured
  266. * trusted domain if the host isn't in the trusted list
  267. *
  268. * @return string Server host
  269. * @since 8.1.0
  270. */
  271. public function getServerHost(): string;
  272. }