public function create(RESTApiRequest $request, $parent_id)
 {
     $type = $request->getData('type');
     $media_id = (int) $request->getData('media_id');
     if (empty($type)) {
         throw new RESTBadRequest("Type is empty");
     }
     if (empty($media_id)) {
         throw new RESTBadRequest("Media ID is empty");
     }
     if (empty($this->types_map[$type])) {
         throw new RESTBadRequest("Type is not supported");
     }
     $media = \Mysql::getInstance()->from($this->types_map[$type]['target'])->where(array('id' => $media_id))->get()->first();
     if (empty($media)) {
         throw new RESTNotFound("Media not found");
     }
     //todo: save storage name for video and karaoke?
     //todo: load balancing
     if ($type == 'tv-archive') {
         $channel = \Itv::getChannelById($media['ch_id']);
         $now_playing_content = $channel ? $channel['name'] : '--';
     } else {
         $now_playing_content = $media[$this->types_map[$type]['title_field']];
     }
     return \Mysql::getInstance()->update('users', array('now_playing_type' => $this->types_map[$type]['code'], 'now_playing_link_id' => $media_id, 'now_playing_content' => $now_playing_content, 'now_playing_start' => 'NOW()', 'last_active' => 'NOW()'), array('id' => $parent_id))->result();
 }
 public function create(RESTApiRequest $request, $parent_id)
 {
     if (empty($this->params['users.id'])) {
         throw new RESTBadRequest("User required");
     }
     $user_id = $this->params['users.id'];
     $user = \Stb::getById($user_id);
     if (empty($user)) {
         throw new RESTNotFound("User not found");
     }
     $itv = \Itv::getInstance();
     $user_channels = $itv->getAllUserChannelsIdsByUid($user['id']);
     if (!in_array($parent_id, $user_channels)) {
         throw new RESTForbidden("User don't have access to this channel");
     }
     $channel = \Itv::getById($parent_id);
     if (empty($channel)) {
         throw new RESTNotFound("Channel not found");
     }
     if (!$channel['allow_pvr']) {
         throw new RESTForbidden("Channel does not support PVR");
     }
     $now_time = time();
     $start_time = (int) $request->getData('start_time');
     $end_time = (int) $request->getData('end_time');
     if ($start_time && $start_time < $now_time) {
         $start_time = $now_time;
     }
     if ($end_time) {
         if ($start_time && $end_time < $start_time || $end_time < $now_time) {
             throw new RESTNotAcceptable("Not acceptable time range");
         }
     }
     $pvr = new \RemotePvr();
     try {
         $rec_id = $pvr->startRecNowByChannelId($channel['id']);
     } catch (\nPVRException $e) {
         throw new RESTServerError($e->getMessage());
     }
     if (!$rec_id) {
         return false;
     }
     if ($end_time) {
         sleep(1);
         // give some time to dumpstream to startup
         $recorder = new \StreamRecorder();
         $recorder->stopDeferred($rec_id, ceil(($end_time - $now_time) / 60));
     }
     $recording = $pvr->getById($rec_id);
     return array('id' => $recording['id'], 'name' => $recording['program'], 'start_time' => strtotime($recording['t_start']), 'end_time' => strtotime($recording['t_stop']), 'ch_id' => (int) $recording['ch_id'], 'ch_name' => $channel['name'], 'status' => $recording['started'] ? $recording['ended'] ? 2 : 1 : 0);
 }
 public function update(RESTApiRequest $request, $video_id)
 {
     $end_time = (int) $request->getData('end_time');
     if (empty($end_time)) {
         throw new RESTBadRequest("Update data is empty");
     }
     $episode = (int) $request->getData('episode');
     if (empty($this->params['users.id'])) {
         throw new RESTBadRequest("User required");
     }
     $user_id = $this->params['users.id'];
     $user = \User::getInstance($user_id);
     return $user->setNotEndedVideo($video_id, $end_time, $episode);
 }
 public function create(RESTApiRequest $request, $parent_id)
 {
     $type = $request->getData('type');
     $media_id = (int) $request->getData('media_id');
     if (empty($type)) {
         throw new RESTBadRequest("Type is empty");
     }
     if (empty($media_id)) {
         throw new RESTBadRequest("Media ID is empty");
     }
     if (empty($this->types_map[$type])) {
         throw new RESTBadRequest("Type is not supported");
     }
     $media = \Mysql::getInstance()->from($this->types_map[$type]['target'])->where(array('id' => $media_id))->get()->first();
     if (empty($media)) {
         throw new RESTNotFound("Media not found");
     }
     $cache = \Cache::getInstance();
     $playback_session = $cache->get($parent_id . '_playback');
     if (!empty($playback_session) && is_array($playback_session)) {
         if ($playback_session['type'] == 'tv-channel' && isset($playback_session['id']) && $playback_session['id'] == $media_id && !empty($playback_session['streamer_id'])) {
             $now_playing_streamer_id = $playback_session['streamer_id'];
         } else {
             if ($playback_session['type'] == 'video' && isset($playback_session['id']) && $playback_session['id'] == $media_id && !empty($playback_session['storage'])) {
                 $storage_name = $playback_session['storage'];
             } else {
                 if ($playback_session['type'] == 'karaoke' && isset($playback_session['id']) && $playback_session['id'] == $media_id && !empty($playback_session['storage'])) {
                     $storage_name = $playback_session['storage'];
                 } else {
                     if ($playback_session['type'] == 'tv-archive' && isset($playback_session['id']) && $playback_session['id'] == $media_id && !empty($playback_session['storage'])) {
                         $storage_name = $playback_session['storage'];
                     }
                 }
             }
         }
     }
     if ($type == 'tv-archive') {
         $channel = \Itv::getChannelById($media['ch_id']);
         $now_playing_content = $channel ? $channel['name'] : '--';
     } else {
         $now_playing_content = $media[$this->types_map[$type]['title_field']];
     }
     \Mysql::getInstance()->insert('user_log', array('uid' => $parent_id, 'action' => 'play', 'param' => $now_playing_content, 'time' => 'NOW()', 'type' => $this->types_map[$type]['code']));
     return \Mysql::getInstance()->update('users', array('now_playing_type' => $this->types_map[$type]['code'], 'now_playing_link_id' => $media_id, 'now_playing_content' => $now_playing_content, 'now_playing_streamer_id' => isset($now_playing_streamer_id) ? $now_playing_streamer_id : 0, 'storage_name' => isset($storage_name) ? $storage_name : '', 'now_playing_start' => 'NOW()', 'last_active' => 'NOW()'), array('id' => $parent_id))->result();
 }
 public function update(RESTApiRequest $request, $parent_id)
 {
     $allowed_for_update = array_fill_keys(array("parent_password"), true);
     $data = $request->getData();
     if (empty($data)) {
         throw new RESTBadRequest("Update data is empty");
     }
     $data = array_intersect_key($data, $allowed_for_update);
     if (empty($data)) {
         throw new RESTBadRequest("Update data is empty");
     }
     return \Stb::updateById($parent_id, $data);
 }
 public function update(RESTApiRequest $request)
 {
     $ch_id = (int) $request->getData('ch_id');
     if (empty($ch_id)) {
         throw new RESTBadRequest("Update data is empty");
     }
     if (empty($this->params['users.id'])) {
         throw new RESTBadRequest("User required");
     }
     $user_id = $this->params['users.id'];
     $user = \User::getInstance($user_id);
     if (empty($user)) {
         throw new RESTNotFound("User not found");
     }
     return $user->setLastChannelId($ch_id);
 }
 public function create(RESTApiRequest $request)
 {
     $new_favorite = (int) $request->getData('ch_id');
     if (empty($new_favorite) || $new_favorite <= 0) {
         throw new RESTBadRequest("Favorite channel required");
     }
     $favorites = $this->manager->getFav($this->user_id);
     $idx = array_search($new_favorite, $favorites);
     if ($idx !== false) {
         array_splice($favorites, $idx, 1);
     }
     $favorites[] = (string) $new_favorite;
     $result = $this->manager->saveFav($favorites, $this->user_id);
     if (!$result) {
         throw new RESTServerError("Error while saving favorites");
     }
     return (bool) $result;
 }
 public function update(RESTApiRequest $request, $parent_id)
 {
     $allowed_for_update = array_fill_keys(array("parent_password", "theme"), true);
     $data = $request->getData();
     if (empty($data)) {
         throw new RESTBadRequest("Update data is empty");
     }
     $data = array_intersect_key($data, $allowed_for_update);
     if (empty($data)) {
         throw new RESTBadRequest("Update data is empty");
     }
     if (!empty($data['theme'])) {
         $themes = \Middleware::getThemes();
         if (!isset($themes[$data['theme']])) {
             throw new RESTBadRequest("Theme '" . $data['theme'] . "' is not supported");
         }
     }
     return \Stb::updateById($parent_id, $data);
 }