public function create(RESTRequest $request)
 {
     $media_name = $request->getData('media_name');
     $media_id = $request->getData('media_id');
     if (empty($media_name)) {
         throw new ErrorException('Empty media_name');
     }
     if (empty($media_id)) {
         throw new ErrorException('Empty media_id');
     }
     return $this->manager->createLink($media_name, $media_id);
 }
 public function update(RESTRequest $request)
 {
     $data = $request->getData();
     if (empty($data)) {
         throw new RESTCommandException('HTTP PUT data is empty');
     }
     $account = array_intersect_key($data, array('subscribed' => true, 'subscribed_id' => true, 'unsubscribed' => true, 'unsubscribed_id' => true));
     if (empty($account)) {
         throw new RESTCommandException('Insert data is empty');
     }
     $identifiers = $request->getIdentifiers();
     if (!empty($identifiers[0]) && strlen($identifiers[0]) >= 12) {
         $users_ids = Stb::getUidByMacs($identifiers);
     } else {
         $users_ids = Stb::getUidByAccountNumber($identifiers);
     }
     if (count($identifiers) == 1 && count($users_ids) == 0) {
         throw new RESTCommandException('Account not found');
     }
     if (count($identifiers) > 1) {
         throw new RESTCommandException('Only one identifier allowed');
     }
     $subscribed = empty($data['subscribed']) ? array() : $data['subscribed'];
     $subscribed_id = empty($data['subscribed_id']) ? array() : $data['subscribed_id'];
     $unsubscribed = empty($data['unsubscribed']) ? array() : $data['unsubscribed'];
     $unsubscribed_id = empty($data['unsubscribed_id']) ? array() : $data['unsubscribed_id'];
     $result = true;
     foreach ($users_ids as $user_id) {
         $user = User::getInstance($user_id);
         $subscribe = $user->updateOptionalPackageSubscription(array('subscribe' => $subscribed, 'subscribe_ids' => $subscribed_id, 'unsubscribe' => $unsubscribed, 'unsubscribe_ids' => $unsubscribed_id));
         $result = $result && $subscribe;
         User::clear();
     }
     return $result;
 }
 /**
  * Create recording task
  *
  * @param RESTRequest $request
  * @return bool
  * @throws ErrorException
  */
 public function create(RESTRequest $request)
 {
     $task = $request->getData('task');
     if (empty($task)) {
         throw new ErrorException('Empty task');
     }
     return $this->manager->start($task);
 }
 public function create(RESTRequest $request)
 {
     $media_name = $request->getData('media_name');
     if (empty($media_name)) {
         throw new ErrorException('Empty media_name');
     }
     return $this->manager->startMD5Sum($media_name);
 }
 public function send()
 {
     if (!empty($this->request) && strpos($this->request->getAccept(), 'text/channel-monitoring-id-url') !== false) {
         if (is_array($this->body['results'])) {
             $channels = array_filter($this->body['results'], function ($channel) {
                 return $channel['enable_monitoring'];
             });
             //$channels = $this->body['results'];
             if (preg_match("/items=(\\d+)-(\\d*)/", $this->request->getAccept(), $match)) {
                 $start = $match[1];
                 $end = empty($match[2]) ? count($channels) : $match[2];
                 $channels = array_slice($channels, $start - 1, $end - $start + 1);
                 //var_dump($start, $end, $channels);
             } elseif (preg_match("/part=(\\d+)\\/(\\d+)/", $this->request->getAccept(), $match)) {
                 $length = count($channels);
                 $start = round((intval($match[1]) - 1) * ($length / intval($match[2])));
                 $end = round(intval($match[1]) * ($length / intval($match[2])));
                 $channels = array_slice($channels, $start, $end - $start);
                 //var_dump($length, $start, $end);
             }
             $body = array_reduce($channels, function ($prev, $curr) {
                 return $prev . $curr['id'] . ' ' . $curr['url'] . (isset($curr['type']) ? ' ' . $curr['type'] : '') . ' ' . str_replace(' ', '_', $curr["ch_name"]) . ' ' . ((int) $curr["status"] == 1 ? "up" : "down") . "\n";
             }, '');
             header("Content-Type: text/plain");
             echo $body;
         }
         return;
     }
     header("Content-Type: " . $this->content_type);
     $this->setOutput();
     $response = json_encode($this->body);
     echo $response;
     ob_end_flush();
     if (!Config::getSafe('enable_api_log', true)) {
         return;
     }
     $logger = new Logger();
     $logger->setPrefix("api_");
     // format: ip - login - [date] method "query" - "data" response_bytes;
     $logger->access(sprintf("%s - %s - [%s] %s \"%s\" - \"%s\" %d\n", empty($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_REAL_IP'], @$_SERVER['PHP_AUTH_USER'], date("r"), $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], empty($this->request) ? 'no resource' : http_build_query($this->request->getData()), strlen($response)));
     if (!empty($this->body['error'])) {
         // format: ip - login - [date] method "query" - "data": error message;
         $logger->error(sprintf("%s - %s - [%s] %s \"%s\" - \"%s\": %s\n", empty($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_REAL_IP'], @$_SERVER['PHP_AUTH_USER'], date("r"), $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], empty($this->request) ? 'no resource' : http_build_query($this->request->getData()), $this->body['error']));
     }
 }
 private function setManager(RESTRequest $request)
 {
     $type = $request->getData('type');
     if (empty($type)) {
         $type = empty($_GET['type']) ? 'itv' : $_GET['type'];
     }
     $base_class = ucfirst($type);
     if (class_exists($base_class)) {
         $this->manager = $base_class::getInstance();
     }
 }
 public function create(RESTRequest $request)
 {
     $stb_list = $request->getConvertedIdentifiers();
     if (empty($stb_list)) {
         throw new RESTCommandException('Empty stb list');
     }
     $msg = $request->getData("msg");
     if (empty($msg)) {
         throw new RESTCommandException('Empty msg');
     }
     $event = new SysEvent();
     $ttl = (int) $request->getData("ttl");
     if (!empty($ttl)) {
         $event->setTtl($ttl);
     }
     $auto_hide_timeout = (int) $request->getData('auto_hide_timeout');
     if ($auto_hide_timeout) {
         $event->setAutoHideTimeout($auto_hide_timeout);
     }
     $event->setUserListById($stb_list);
     $event->sendMsg($msg);
     return true;
 }
 public function update(RESTRequest $request)
 {
     $stb_list = $request->getConvertedIdentifiers();
     if (empty($stb_list)) {
         throw new RESTCommandException('Empty stb list');
     }
     foreach ($stb_list as $uid) {
         Mysql::getInstance()->update('users', array('access_token' => strtoupper(md5(microtime(1) . uniqid()))), array('id' => $uid));
         if ($request->getData('reset_device_id')) {
             Mysql::getInstance()->update('users', array('mac' => '', 'device_id' => '', 'device_id2' => ''), array('id' => $uid));
         }
         Mysql::getInstance()->update('access_tokens', array('token' => 'invalid_' . md5(microtime(1) . uniqid()), 'refresh_token' => 'invalid_' . md5(microtime(1) . uniqid())), array('uid' => $uid));
     }
     return true;
 }
 public function update(RESTRequest $request)
 {
     $ids = $request->getIdentifiers();
     if (empty($ids[0]) || intval($ids[0]) == 0) {
         throw new ErrorException('Empty channel id');
     }
     $data = $request->getData();
     if (key_exists('start_time', $data)) {
         $this->manager->updateStartTime(intval($ids[0]), $data['start_time']);
     }
     if (key_exists('end_time', $data)) {
         $this->manager->updateEndTime(intval($ids[0]), $data['end_time']);
     }
     return true;
 }
 public function create(RESTRequest $request)
 {
     $data = $request->getData();
     if (empty($data)) {
         throw new RESTCommandException('HTTP POST data is empty');
     }
     $allowed_to_update_fields = array_fill_keys(array('mac', 'login', 'password', 'status', 'additional_services_on', 'ls', 'end_date', 'account_balance'), true);
     $data = array_intersect_key($data, $allowed_to_update_fields);
     if (empty($data)) {
         throw new RESTCommandException('Insert data is empty');
     }
     if (isset($data['end_date'])) {
         $data['expire_billing_date'] = $data['end_date'];
         unset($data['end_date']);
     }
     if (!empty($data['mac'])) {
         $mac = Middleware::normalizeMac($data['mac']);
         if (!$mac) {
             throw new RESTCommandException('Not valid mac address');
         }
         $data['mac'] = $mac;
     }
     if (empty($data['mac']) && (empty($data['login']) || empty($data['password']))) {
         throw new RESTCommandException('Login and password required');
     }
     try {
         $uid = Stb::create($data);
     } catch (Exception $e) {
         throw new RESTCommandException($e->getMessage());
     }
     $stb_list = $this->manager->getByUids(array($uid));
     $stb_list = $this->formatList($stb_list);
     if (count($stb_list) == 1) {
         return $stb_list[0];
     }
     return $stb_list;
 }
 public function update(RESTRequest $request)
 {
     $data = $request->getData();
     if (empty($data)) {
         throw new RESTCommandException('HTTP POST data is empty');
     }
     $allowed_to_update_fields = array_fill_keys(array('login', 'password', 'full_name', 'phone', 'account_number', 'tariff_plan', 'status', 'stb_mac', 'comment', 'end_date', 'account_balance'), true);
     $account = array_intersect_key($data, $allowed_to_update_fields);
     if (empty($account)) {
         throw new RESTCommandException('Insert data is empty');
     }
     $identifiers = $request->getIdentifiers();
     if (count($identifiers) == 0) {
         throw new RESTCommandException('Identifier required');
     }
     $users_ids = $this->getUsersIdsFromIdentifiers($identifiers);
     if (count($identifiers) == 1 && count($users_ids) == 0) {
         throw new RESTCommandException('Account not found');
     }
     if (count($identifiers) > 1) {
         throw new RESTCommandException('Only one identifier allowed');
     }
     if (!empty($account['login'])) {
         $user = User::getByLogin($account['login']);
         if (!empty($user) && ($user->getId() != $users_ids[0] || count($users_ids) > 1)) {
             throw new RESTCommandException('Login already in use');
         }
     }
     $result = true;
     foreach ($users_ids as $user_id) {
         $user = User::getInstance($user_id);
         $result = $user->updateAccount($account) && $result;
         User::clear();
     }
     return $result;
 }
 public function create(RESTRequest $request)
 {
     $stb_list = $request->getConvertedIdentifiers();
     $identifiers = $request->getIdentifiers();
     if (empty($stb_list) && !empty($identifiers)) {
         throw new RESTCommandException('STB not found');
     }
     $event = new SysEvent();
     if (empty($identifiers)) {
         $event->setUserListByMac('all');
     } else {
         $event->setUserListById($stb_list);
     }
     if ($request->getData('ttl')) {
         $event->setTtl($request->getData('ttl'));
     }
     switch ($request->getData('event')) {
         case 'send_msg':
             if ($request->getData('need_reboot')) {
                 $event->sendMsgAndReboot($request->getData('msg'));
             } else {
                 $event->sendMsg($request->getData('msg'));
             }
             if ($request->getData('auto_hide_timeout')) {
                 $event->setAutoHideTimeout($request->getData('auto_hide_timeout'));
             }
             break;
         case 'send_msg_with_video':
             $event->sendMsgWithVideo($request->getData('msg'), $request->getData('video'));
             break;
         case 'reboot':
             $event->sendReboot();
             break;
         case 'reload_portal':
             $event->sendReloadPortal();
             break;
         case 'update_channels':
             $event->sendUpdateChannels();
             break;
         case 'play_channel':
             $event->sendPlayChannel($request->getData('channel'));
             break;
         case 'play_radio_channel':
             $event->sendPlayRadioChannel($request->getData('channel'));
             break;
         case 'update_image':
             $event->sendUpdateImage();
             break;
         case 'cut_off':
             $event->sendCutOff();
             break;
         case 'show_menu':
             $event->sendShowMenu();
             break;
         default:
             return false;
     }
     return true;
 }