DbHandler.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 $query->getLastInsertId();
  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. $qResult = $query->execute();
  111. $result = $qResult->fetchAll();
  112. $qResult->closeCursor();
  113. if (empty($result)) {
  114. throw new \Exception('No Server found with ID: ' . $id);
  115. }
  116. return $result[0];
  117. }
  118. /**
  119. * get all trusted servers
  120. *
  121. * @return array
  122. */
  123. public function getAllServer() {
  124. $query = $this->connection->getQueryBuilder();
  125. $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
  126. ->from($this->dbTable);
  127. $statement = $query->execute();
  128. $result = $statement->fetchAll();
  129. $statement->closeCursor();
  130. return $result;
  131. }
  132. /**
  133. * check if server already exists in the database table
  134. *
  135. * @param string $url
  136. * @return bool
  137. */
  138. public function serverExists($url) {
  139. $hash = $this->hash($url);
  140. $query = $this->connection->getQueryBuilder();
  141. $query->select('url')
  142. ->from($this->dbTable)
  143. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  144. ->setParameter('url_hash', $hash);
  145. $statement = $query->execute();
  146. $result = $statement->fetchAll();
  147. $statement->closeCursor();
  148. return !empty($result);
  149. }
  150. /**
  151. * write token to database. Token is used to exchange the secret
  152. *
  153. * @param string $url
  154. * @param string $token
  155. */
  156. public function addToken($url, $token) {
  157. $hash = $this->hash($url);
  158. $query = $this->connection->getQueryBuilder();
  159. $query->update($this->dbTable)
  160. ->set('token', $query->createParameter('token'))
  161. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  162. ->setParameter('url_hash', $hash)
  163. ->setParameter('token', $token);
  164. $query->execute();
  165. }
  166. /**
  167. * get token stored in database
  168. *
  169. * @param string $url
  170. * @return string
  171. * @throws \Exception
  172. */
  173. public function getToken($url) {
  174. $hash = $this->hash($url);
  175. $query = $this->connection->getQueryBuilder();
  176. $query->select('token')->from($this->dbTable)
  177. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  178. ->setParameter('url_hash', $hash);
  179. $statement = $query->execute();
  180. $result = $statement->fetch();
  181. $statement->closeCursor();
  182. if (!isset($result['token'])) {
  183. throw new \Exception('No token found for: ' . $url);
  184. }
  185. return $result['token'];
  186. }
  187. /**
  188. * add shared Secret to database
  189. *
  190. * @param string $url
  191. * @param string $sharedSecret
  192. */
  193. public function addSharedSecret($url, $sharedSecret) {
  194. $hash = $this->hash($url);
  195. $query = $this->connection->getQueryBuilder();
  196. $query->update($this->dbTable)
  197. ->set('shared_secret', $query->createParameter('sharedSecret'))
  198. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  199. ->setParameter('url_hash', $hash)
  200. ->setParameter('sharedSecret', $sharedSecret);
  201. $query->execute();
  202. }
  203. /**
  204. * get shared secret from database
  205. *
  206. * @param string $url
  207. * @return string
  208. */
  209. public function getSharedSecret($url) {
  210. $hash = $this->hash($url);
  211. $query = $this->connection->getQueryBuilder();
  212. $query->select('shared_secret')->from($this->dbTable)
  213. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  214. ->setParameter('url_hash', $hash);
  215. $statement = $query->execute();
  216. $result = $statement->fetch();
  217. $statement->closeCursor();
  218. return $result['shared_secret'];
  219. }
  220. /**
  221. * set server status
  222. *
  223. * @param string $url
  224. * @param int $status
  225. * @param string|null $token
  226. */
  227. public function setServerStatus($url, $status, $token = null) {
  228. $hash = $this->hash($url);
  229. $query = $this->connection->getQueryBuilder();
  230. $query->update($this->dbTable)
  231. ->set('status', $query->createNamedParameter($status))
  232. ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
  233. if (!is_null($token)) {
  234. $query->set('sync_token', $query->createNamedParameter($token));
  235. }
  236. $query->execute();
  237. }
  238. /**
  239. * get server status
  240. *
  241. * @param string $url
  242. * @return int
  243. */
  244. public function getServerStatus($url) {
  245. $hash = $this->hash($url);
  246. $query = $this->connection->getQueryBuilder();
  247. $query->select('status')->from($this->dbTable)
  248. ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
  249. ->setParameter('url_hash', $hash);
  250. $statement = $query->execute();
  251. $result = $statement->fetch();
  252. $statement->closeCursor();
  253. return (int)$result['status'];
  254. }
  255. /**
  256. * create hash from URL
  257. *
  258. * @param string $url
  259. * @return string
  260. */
  261. protected function hash($url) {
  262. $normalized = $this->normalizeUrl($url);
  263. return sha1($normalized);
  264. }
  265. /**
  266. * normalize URL, used to create the sha1 hash
  267. *
  268. * @param string $url
  269. * @return string
  270. */
  271. protected function normalizeUrl($url) {
  272. $normalized = $url;
  273. if (strpos($url, 'https://') === 0) {
  274. $normalized = substr($url, strlen('https://'));
  275. } elseif (strpos($url, 'http://') === 0) {
  276. $normalized = substr($url, strlen('http://'));
  277. }
  278. $normalized = Filesystem::normalizePath($normalized);
  279. $normalized = trim($normalized, '/');
  280. return $normalized;
  281. }
  282. /**
  283. * @param $username
  284. * @param $password
  285. * @return bool
  286. */
  287. public function auth($username, $password) {
  288. if ($username !== 'system') {
  289. return false;
  290. }
  291. $query = $this->connection->getQueryBuilder();
  292. $query->select('url')->from($this->dbTable)
  293. ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
  294. $statement = $query->execute();
  295. $result = $statement->fetch();
  296. $statement->closeCursor();
  297. return !empty($result);
  298. }
  299. }