DbHandler.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Federation;
  28. use OC\Files\Filesystem;
  29. use OC\HintException;
  30. use OCP\IDBConnection;
  31. use OCP\IL10N;
  32. /**
  33. * Class DbHandler
  34. *
  35. * handles all database calls for the federation app
  36. *
  37. * @group DB
  38. * @package OCA\Federation
  39. */
  40. class DbHandler {
  41. /** @var IDBConnection */
  42. private $connection;
  43. /** @var IL10N */
  44. private $IL10N;
  45. /** @var string */
  46. private $dbTable = 'trusted_servers';
  47. /**
  48. * @param IDBConnection $connection
  49. * @param IL10N $il10n
  50. */
  51. public function __construct(
  52. IDBConnection $connection,
  53. IL10N $il10n
  54. ) {
  55. $this->connection = $connection;
  56. $this->IL10N = $il10n;
  57. }
  58. /**
  59. * add server to the list of trusted servers
  60. *
  61. * @param string $url
  62. * @return int
  63. * @throws HintException
  64. */
  65. public function addServer($url) {
  66. $hash = $this->hash($url);
  67. $url = rtrim($url, '/');
  68. $query = $this->connection->getQueryBuilder();
  69. $query->insert($this->dbTable)
  70. ->values(
  71. [
  72. 'url' => $query->createParameter('url'),
  73. 'url_hash' => $query->createParameter('url_hash'),
  74. ]
  75. )
  76. ->setParameter('url', $url)
  77. ->setParameter('url_hash', $hash);
  78. $result = $query->execute();
  79. if ($result) {
  80. return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
  81. }
  82. $message = 'Internal failure, Could not add trusted server: ' . $url;
  83. $message_t = $this->IL10N->t('Could not add server');
  84. throw new HintException($message, $message_t);
  85. }
  86. /**
  87. * remove server from the list of trusted servers
  88. *
  89. * @param int $id
  90. */
  91. public function removeServer($id) {
  92. $query = $this->connection->getQueryBuilder();
  93. $query->delete($this->dbTable)
  94. ->where($query->expr()->eq('id', $query->createParameter('id')))
  95. ->setParameter('id', $id);
  96. $query->execute();
  97. }
  98. /**
  99. * get trusted server with given ID
  100. *
  101. * @param int $id
  102. * @return array
  103. * @throws \Exception
  104. */
  105. public function getServerById($id) {
  106. $query = $this->connection->getQueryBuilder();
  107. $query->select('*')->from($this->dbTable)
  108. ->where($query->expr()->eq('id', $query->createParameter('id')))
  109. ->setParameter('id', $id);
  110. $query->execute();
  111. $result = $query->execute()->fetchAll();
  112. if (empty($result)) {
  113. throw new \Exception('No Server found with ID: ' . $id);
  114. }
  115. return $result[0];
  116. }
  117. /**
  118. * get all trusted servers
  119. *
  120. * @return array
  121. */
  122. public function getAllServer() {
  123. $query = $this->connection->getQueryBuilder();
  124. $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
  125. ->from($this->dbTable);
  126. $statement = $query->execute();
  127. $result = $statement->fetchAll();
  128. $statement->closeCursor();
  129. return $result;
  130. }
  131. /**
  132. * check if server already exists in the database table
  133. *
  134. * @param string $url
  135. * @return bool
  136. */
  137. public function serverExists($url) {
  138. $hash = $this->hash($url);
  139. $query = $this->connection->getQueryBuilder();
  140. $query->select('url')
  141. ->from($this->dbTable)
  142. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  143. ->setParameter('url_hash', $hash);
  144. $statement = $query->execute();
  145. $result = $statement->fetchAll();
  146. $statement->closeCursor();
  147. return !empty($result);
  148. }
  149. /**
  150. * write token to database. Token is used to exchange the secret
  151. *
  152. * @param string $url
  153. * @param string $token
  154. */
  155. public function addToken($url, $token) {
  156. $hash = $this->hash($url);
  157. $query = $this->connection->getQueryBuilder();
  158. $query->update($this->dbTable)
  159. ->set('token', $query->createParameter('token'))
  160. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  161. ->setParameter('url_hash', $hash)
  162. ->setParameter('token', $token);
  163. $query->execute();
  164. }
  165. /**
  166. * get token stored in database
  167. *
  168. * @param string $url
  169. * @return string
  170. * @throws \Exception
  171. */
  172. public function getToken($url) {
  173. $hash = $this->hash($url);
  174. $query = $this->connection->getQueryBuilder();
  175. $query->select('token')->from($this->dbTable)
  176. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  177. ->setParameter('url_hash', $hash);
  178. $statement = $query->execute();
  179. $result = $statement->fetch();
  180. $statement->closeCursor();
  181. if (!isset($result['token'])) {
  182. throw new \Exception('No token found for: ' . $url);
  183. }
  184. return $result['token'];
  185. }
  186. /**
  187. * add shared Secret to database
  188. *
  189. * @param string $url
  190. * @param string $sharedSecret
  191. */
  192. public function addSharedSecret($url, $sharedSecret) {
  193. $hash = $this->hash($url);
  194. $query = $this->connection->getQueryBuilder();
  195. $query->update($this->dbTable)
  196. ->set('shared_secret', $query->createParameter('sharedSecret'))
  197. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  198. ->setParameter('url_hash', $hash)
  199. ->setParameter('sharedSecret', $sharedSecret);
  200. $query->execute();
  201. }
  202. /**
  203. * get shared secret from database
  204. *
  205. * @param string $url
  206. * @return string
  207. */
  208. public function getSharedSecret($url) {
  209. $hash = $this->hash($url);
  210. $query = $this->connection->getQueryBuilder();
  211. $query->select('shared_secret')->from($this->dbTable)
  212. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  213. ->setParameter('url_hash', $hash);
  214. $statement = $query->execute();
  215. $result = $statement->fetch();
  216. $statement->closeCursor();
  217. return $result['shared_secret'];
  218. }
  219. /**
  220. * set server status
  221. *
  222. * @param string $url
  223. * @param int $status
  224. * @param string|null $token
  225. */
  226. public function setServerStatus($url, $status, $token = null) {
  227. $hash = $this->hash($url);
  228. $query = $this->connection->getQueryBuilder();
  229. $query->update($this->dbTable)
  230. ->set('status', $query->createNamedParameter($status))
  231. ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
  232. if (!is_null($token)) {
  233. $query->set('sync_token', $query->createNamedParameter($token));
  234. }
  235. $query->execute();
  236. }
  237. /**
  238. * get server status
  239. *
  240. * @param string $url
  241. * @return int
  242. */
  243. public function getServerStatus($url) {
  244. $hash = $this->hash($url);
  245. $query = $this->connection->getQueryBuilder();
  246. $query->select('status')->from($this->dbTable)
  247. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  248. ->setParameter('url_hash', $hash);
  249. $statement = $query->execute();
  250. $result = $statement->fetch();
  251. $statement->closeCursor();
  252. return (int)$result['status'];
  253. }
  254. /**
  255. * create hash from URL
  256. *
  257. * @param string $url
  258. * @return string
  259. */
  260. protected function hash($url) {
  261. $normalized = $this->normalizeUrl($url);
  262. return sha1($normalized);
  263. }
  264. /**
  265. * normalize URL, used to create the sha1 hash
  266. *
  267. * @param string $url
  268. * @return string
  269. */
  270. protected function normalizeUrl($url) {
  271. $normalized = $url;
  272. if (strpos($url, 'https://') === 0) {
  273. $normalized = substr($url, strlen('https://'));
  274. } elseif (strpos($url, 'http://') === 0) {
  275. $normalized = substr($url, strlen('http://'));
  276. }
  277. $normalized = Filesystem::normalizePath($normalized);
  278. $normalized = trim($normalized, '/');
  279. return $normalized;
  280. }
  281. /**
  282. * @param $username
  283. * @param $password
  284. * @return bool
  285. */
  286. public function auth($username, $password) {
  287. if ($username !== 'system') {
  288. return false;
  289. }
  290. $query = $this->connection->getQueryBuilder();
  291. $query->select('url')->from($this->dbTable)
  292. ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
  293. $statement = $query->execute();
  294. $result = $statement->fetch();
  295. $statement->closeCursor();
  296. return !empty($result);
  297. }
  298. }