/**
  * @param RESTApiRequest $request
  * @return RESTApiResource
  * @throws \Exception
  * @throws RESTNotFound
  */
 public function getTarget(RESTApiRequest $request)
 {
     $resources_chain = $request->getResource();
     $target = null;
     for ($i = 0; $i < count($resources_chain); $i++) {
         try {
             $class_name = $this->getClassForResource($resources_chain[$i]);
         } catch (\Exception $e) {
             continue;
         }
         $reflection = new \ReflectionClass($class_name);
         $parent = $reflection->getParentClass();
         $parent_name = explode('\\', $parent->getName());
         $parent_name = $parent_name[count($parent_name) - 1];
         if (in_array($parent_name, array("RESTApiCollection", "RESTApiStore", "RESTApiController"))) {
             $this->nested_params[$resources_chain[$i]] = isset($resources_chain[$i + 1]) ? $resources_chain[$i + 1] : "";
         } else {
             if ($parent_name == "RESTApiDocument") {
                 continue;
             } else {
                 throw new \Exception("Undefined resource type");
             }
         }
         if ($parent_name == "RESTApiCollection") {
             $target = $resources_chain[$i];
         }
     }
     if (empty($target)) {
         throw new RESTNotFound("Resource not found");
     }
     $idx = array_search($target, $resources_chain);
     $external_params = array_slice($resources_chain, $idx + 1);
     array_pop($this->nested_params);
     return new $class_name($this->nested_params, $external_params);
 }
 public function get(RESTApiRequest $request)
 {
     //throw new RESTNotAllowedMethod("Please use /tv-channel/[ch_id]/epg instead");
     if (empty($this->nested_params['ch_id'])) {
         throw new RESTBadRequest("ch_id required");
     }
     $ch_ids = explode(',', $this->nested_params['ch_id']);
     $epg_data = array();
     foreach ($ch_ids as $ch_id) {
         $channel = \Itv::getChannelById((int) $ch_id);
         if (empty($channel)) {
             throw new RESTNotFound("Channel " . intval($ch_id) . " not found");
         }
         $from = (int) $request->getParam('from');
         $to = (int) $request->getParam('to');
         $next = (int) $request->getParam('next');
         if (!empty($next)) {
             $epg_data[(int) $ch_id] = $this->filter($this->manager->getCurProgramAndFewNext($channel['id'], $next));
         } else {
             $from = empty($from) ? "" : date("Y-m-d H:i:s", $from);
             $to = empty($to) ? "" : date("Y-m-d H:i:s", $to);
             $epg = $this->manager->getEpgForChannelsOnPeriod(array($channel['id']), $from, $to);
             $epg_data[(int) $ch_id] = $this->filter($epg[$channel['id']]);
         }
     }
     if (count($epg_data) == 1) {
         $keys = array_keys($epg_data);
         return $epg_data[$keys[0]];
     } else {
         return $epg_data;
     }
 }
 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 get(RESTApiRequest $request)
 {
     $channels = $this->manager->getRawAllUserChannels($this->user_id);
     if ($request->getLimit() !== null) {
         $channels->limit($request->getLimit(), $request->getOffset());
     }
     return $this->filter($channels->get()->all());
 }
 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 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 get(RESTApiRequest $request)
 {
     $genres = new \VideoGenre();
     $genres->setLocale($request->getLanguage());
     if (!empty($this->categories)) {
         $response = array();
         foreach ($this->categories as $category) {
             $response[$category['id']] = $this->filter($genres->getByCategoryId($category['id'], true));
         }
         return $response;
     } else {
         return $this->filter($genres->getAll(true));
     }
 }
 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);
 }
 private function prepareQuery(RESTApiRequest $request)
 {
     $raw_karaoke = $this->manager->getRawAll();
     $search = $request->getSearch();
     if ($search !== null) {
         $raw_karaoke->like(array('karaoke.name' => '%' . $search . '%', 'karaoke.singer' => '%' . $search . '%', 'karaoke_genre.title' => '%' . $search . '%'), 'OR');
     }
     if (!empty($this->genres_ids)) {
         $raw_karaoke->in('genre_id', $this->genres_ids);
     }
     if (!empty($this->karaoke_id)) {
         $raw_karaoke->where(array('karaoke.id' => $this->karaoke_id));
     }
     return $raw_karaoke;
 }
 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, $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 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 execute(RESTApiRequest $request)
 {
     $action = $request->getAction();
     if (empty($this->external_params)) {
         if (!$this->supportsAction($action)) {
             throw new RESTNotAllowedMethod("Resource does not support method '" . $request->getMethod() . "'");
         }
         return call_user_func(array($this, $action), $request);
     }
     if ($this->controllers->exist($this->external_params[0])) {
         $controller = $this->controllers->getByName($this->external_params[0]);
         if (!$controller->supportsAction($request->getAction())) {
             throw new RESTNotAllowedMethod("Controller does not support method '" . $request->getMethod() . "'");
         }
         return call_user_func(array($controller, $action), $request);
     }
     if (empty($this->document)) {
         throw new RESTNotFound("Resource not found");
     }
     if (!empty($this->external_params[1])) {
         if ($this->document->controllers->exist($this->external_params[1])) {
             $controller = $this->document->controllers->getByName($this->external_params[1]);
             if (!$controller->supportsAction($request->getAction())) {
                 throw new RESTNotAllowedMethod("Controller does not support method '" . $request->getMethod() . "'");
             }
             return call_user_func(array($controller, $action), $request, $this->external_params[0]);
         } else {
             if ($this->document->controllers->exist($this->external_params[count($this->external_params) - 1])) {
                 $controller_name = $this->external_params[count($this->external_params) - 1];
                 $controller = $this->document->controllers->getByName($controller_name);
                 if (!$controller->supportsAction($request->getAction())) {
                     throw new RESTNotAllowedMethod("Controller does not support method '" . $request->getMethod() . "'");
                 }
                 return call_user_func(array($controller, $action), $request, $this->external_params);
             } else {
                 throw new RESTNotFound("Resource not found");
             }
         }
     }
     if (!$this->document->supportsAction($request->getAction())) {
         throw new RESTNotAllowedMethod("Document does not support method '" . $request->getMethod() . "'");
     }
     return call_user_func(array($this->document, $action), $request, $this->external_params[0]);
 }
 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);
 }
 public function get(RESTApiRequest $request)
 {
     if ($request->getParam('mark') == 'favorite') {
         $this->favorite_filter_enabled = true;
     }
     $channels = $this->manager->getRawAllUserChannels($this->user_id);
     if ($this->favorite_filter_enabled) {
         $channels->in('id', $this->fav_channels);
     }
     if ($request->getLimit() !== null) {
         $channels->limit($request->getLimit(), $request->getOffset());
     }
     if ($this->genre_id) {
         $channels->where(array('tv_genre_id' => $this->genre_id));
     }
     if (!empty($this->channel_id)) {
         $channels->where(array('id' => $this->channel_id));
     }
     return $this->filter($channels->get()->all());
 }
 public function get(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();
     $this->user_channels = $itv->getAllUserChannelsIdsByUid($user['id']);
     if (!in_array($parent_id, $this->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");
     }
     $start = $request->getParam('start');
     if ($start) {
         // todo: time shift!
         throw new RESTNotFound("Time shift in progress...");
     }
     $urls = \Itv::getUrlsForChannel($channel['id']);
     if (!empty($urls)) {
         $link = $urls[0]['id'];
     } else {
         $link = null;
     }
     try {
         $url = $itv->getUrlByChannelId($parent_id, $link);
     } catch (\ItvChannelTemporaryUnavailable $e) {
         throw new RESTNotFound($e->getMessage());
     }
     if (preg_match("/(\\S+:\\/\\/\\S+)/", $url, $match)) {
         $url = $match[1];
     }
     return $url;
 }
 public function get(RESTApiRequest $request)
 {
     $categories = new \VideoCategory();
     $categories->setLocale($request->getLanguage());
     return $this->filter($categories->getAll(true));
 }
 public function get(RESTApiRequest $request)
 {
     $genres = new \TvGenre();
     $genres->setLocale($request->getLanguage());
     return $genres->getAll(true);
 }
 public function get(RESTApiRequest $request)
 {
     $this->manager->setLocale($request->getLanguage());
     $videos = $this->prepareQuery($request);
     if ($request->getLimit() !== null) {
         $videos->limit($request->getLimit(), $request->getOffset());
     }
     if ($request->getParam('sortby') == "name") {
         if (!$request->getLanguage() || $request->getLanguage() == 'ru') {
             $videos->orderby("name");
         } else {
             $videos->orderby("o_name");
         }
     } else {
         $videos->orderby("added", 'DESC');
     }
     return $this->filter($videos->get()->all());
 }