/**
  * Return link for current channel and current time
  *
  * NGINX config:
  *
  * location /tslink/ {
  *
  *     rewrite ^/tslink/(.+)/archive/(\d+)/(.+) /stalker_portal/server/api/chk_tmp_timeshift_link.php?key=$1&file=$3 last;
  *
  *     proxy_set_header Host 192.168.1.71; # <- portal ip
  *     proxy_set_header X-Real-IP $remote_addr;
  *     proxy_pass http://192.168.1.71:88/; # <- portal ip
  * }
  *
  * location /archive/ {
  *     root /var/www/bb1;
  *     internal;
  * }
  *
  * @return string
  */
 public function getLinkForChannel()
 {
     $ch_id = intval($_REQUEST['ch_id']);
     $res = array('id' => 0, 'cmd' => '', 'storage_id' => '', 'load' => '0', 'error' => '');
     try {
         $task = $this->getLessLoadedTaskByChId($ch_id);
     } catch (StorageSessionLimitException $e) {
         $res['error'] = 'limit';
         $res['storage_name'] = $e->getStorageName();
         return $res;
     }
     if (empty($task)) {
         $res['error'] = 'server_error';
         return $res;
     }
     $storage = Master::getStorageByName($task['storage_name']);
     //$channel = Itv::getChannelById($ch_id);
     $tz = new DateTimeZone(Stb::$server_timezone);
     $date = new DateTime(date('r'));
     $date->setTimeZone($tz);
     $date_now = new DateTime('now', new DateTimeZone(Stb::$server_timezone));
     $dst_diff = $date->format('Z') - $date_now->format('Z');
     if (!$storage['flussonic_dvr'] && !$storage['wowza_dvr']) {
         if ($dst_diff > 0) {
             $date->add(new DateInterval('PT' . $dst_diff . 'S'));
         } elseif ($dst_diff < 0) {
             $dst_diff *= -1;
             $date->sub(new DateInterval('PT' . $dst_diff . 'S'));
         }
     }
     $position = intval($date->format("i")) * 60 + intval($date->format("s"));
     $channel = Itv::getChannelById($ch_id);
     $filename = $date->format("Ymd-H");
     $filename .= '.mpg';
     if ($channel['flussonic_dvr']) {
         if (preg_match("/:\\/\\/([^\\/]*)\\/([^\\/]*).*(mpegts|m3u8)\$/", $channel['mc_cmd'], $match)) {
             if ($match[3] == 'mpegts') {
                 $res['cmd'] = 'http://' . $storage['storage_ip'] . '/' . $match[2] . '/archive/' . strtotime(date("Y-m-d H:00:00")) . '/3600/mpegts';
             } else {
                 $res['cmd'] = preg_replace('/:\\/\\/([^\\/]*)/', '://' . $storage['storage_ip'], $channel['mc_cmd']);
                 $res['cmd'] = preg_replace('/\\.m3u8/', '-' . strtotime(date("Y-m-d H:00:00")) . '-3600' . '.m3u8', $res['cmd']);
                 // todo: current hour?
             }
             $res['cmd'] .= '' . '?token=' . $this->createTemporaryToken($this->stb->id) . ' position:' . $position . ' media_len:' . (intval(date("H")) * 3600 + intval(date("i")) * 60 + intval(date("s")));
         } else {
             $res['error'] = 'server_error';
         }
     } elseif ($channel['wowza_dvr']) {
         if (preg_match("/:\\/\\/([^\\/]*)\\/.*\\.m3u8/", $channel['mc_cmd'], $match)) {
             $url = preg_replace('/:\\/\\/([^\\/]*)/', '://' . $storage['storage_ip'], $channel['mc_cmd']);
             $res['cmd'] = preg_replace('/\\.m3u8.*/', '.m3u8?DVR&wowzadvrplayliststart=' . gmdate("YmdH0000") . '&wowzadvrplaylistduration=3600000', $url) . '&token=' . $this->createTemporaryToken("1") . ' position:' . $position . ' media_len:' . (intval(date("H")) * 3600 + intval(date("i")) * 60 + intval(date("s")));
         } else {
             $res['error'] = 'server_error';
         }
         return $res;
     } else {
         if (Config::getSafe('enable_timeshift_tmp_link', false)) {
             $redirect_url = '/archive/' . $ch_id . '/' . $filename;
             $link_result = $this->createTemporaryTimeShiftToken($redirect_url);
             $res['cmd'] = 'ffmpeg http://' . $storage['storage_ip'] . '/tslink/' . $link_result . '/archive/' . $ch_id . '/' . $filename . ' position:' . $position . ' media_len:' . (intval(date("H")) * 3600 + intval(date("i")) * 60 + intval(date("s")));
         } else {
             $res['cmd'] = 'ffmpeg http://' . $storage['storage_ip'] . '/archive/' . $ch_id . '/' . $filename . ' position:' . $position . ' media_len:' . (intval(date("H")) * 3600 + intval(date("i")) * 60 + intval(date("s")));
         }
     }
     return $res;
 }