DbHandler.php 8.3 KB

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