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 filter($channels)
 {
     $genres = new \TvGenre();
     $all_genres = $genres->getAll(true, true);
     $all_genres_map = array();
     foreach ($all_genres as $genre) {
         $all_genres_map[$genre['_id']] = $genre;
     }
     $fav_channels = $this->fav_channels;
     $fields_map = $this->fields_map;
     $user_channels = $this->user_channels;
     $channels = array_map(function ($channel) use($fav_channels, $fields_map, $user_channels, $all_genres_map) {
         $new_channel = array_intersect_key($channel, $fields_map);
         $new_channel['id'] = (int) $channel['id'];
         $new_channel['number'] = (int) $channel['number'];
         $new_channel['genre_id'] = isset($all_genres_map[$channel['tv_genre_id']]) ? $all_genres_map[$channel['tv_genre_id']]['id'] : '';
         $new_channel['favorite'] = in_array($channel['id'], $fav_channels) ? 1 : 0;
         $new_channel['archive'] = (int) $channel['enable_tv_archive'];
         $new_channel['censored'] = (int) $channel['censored'];
         $new_channel['archive_range'] = (int) $channel['tv_archive_duration'];
         $new_channel['pvr'] = (int) $channel['allow_pvr'];
         if ($channel['enable_monitoring']) {
             $new_channel['monitoring_status'] = (int) $channel['monitoring_status'];
         } else {
             $new_channel['monitoring_status'] = 1;
         }
         if (!empty($_SERVER['HTTP_UA_RESOLUTION']) && in_array($_SERVER['HTTP_UA_RESOLUTION'], array(120, 160, 240, 320))) {
             $resolution = (int) $_SERVER['HTTP_UA_RESOLUTION'];
         } else {
             $resolution = 320;
         }
         $new_channel['logo'] = \Itv::getLogoUriById($channel['id'], $resolution);
         $urls = \Itv::getUrlsForChannel($channel['id']);
         if (!empty($urls) && $urls[0]['use_http_tmp_link'] == 0 && $urls[0]['use_load_balancing'] == 0) {
             $new_channel['url'] = $urls[0]['url'];
         } else {
             $new_channel['url'] = "";
         }
         if (preg_match("/(\\S+:\\/\\/\\S+)/", $new_channel['url'], $match)) {
             $new_channel['url'] = $match[1];
         }
         return $new_channel;
     }, $channels);
     return $channels;
 }