dbhandler.php 7.4 KB

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