/** * do the initial handshake * * @param array $params */ public function handshake($params) { $auth = isset($params['auth']) ? $params['auth'] : false; $user = isset($params['user']) ? $params['user'] : false; $time = isset($params['timestamp']) ? $params['timestamp'] : false; $now = time(); if ($now - $time > 10 * 60) { $this->error(400, 'timestamp is more then 10 minutes old'); } if ($auth and $user and $time) { $query = \OCP\DB::prepare("SELECT `user_id`, `user_password_sha256` FROM `*PREFIX*media_users` WHERE `user_id`=?"); $result = $query->execute(array($user)); if ($row = $result->fetchRow()) { $pass = $row['user_password_sha256']; $key = hash('sha256', $time . $pass); if ($key == $auth) { $token = hash('sha256', 'oc_media_' . $key); $this->collection = new Collection($row['user_id']); $date = date('c'); //todo proper update/add/clean dates $songs = $this->collection->getSongCount(); $artists = $this->collection->getArtistCount(); $albums = $this->collection->getAlbumCount(); $query = \OCP\DB::prepare("INSERT INTO `*PREFIX*media_sessions` (`token`, `user_id`, `start`) VALUES (?, ?, now());"); $query->execute(array($token, $user)); $expire = date('c', time() + 600); $tmpl = new \OC_Template('media', 'ampache/handshake'); $tmpl->assign('token', $token); $tmpl->assign('date', $date); $tmpl->assign('songs', $songs); $tmpl->assign('artists', $artists); $tmpl->assign('albums', $albums); $tmpl->assign('expire', $expire); $tmpl->printPage(); return; } } $this->error(400, 'Invalid login'); } else { $this->error(400, 'Missing arguments'); } }