api.hlstv.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * OmegaTV low-level API implementation
  4. */
  5. class HlsTV {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains public key
  14. *
  15. * @var string
  16. */
  17. protected $publicKey = '';
  18. /**
  19. * Contains private key
  20. *
  21. * @var string
  22. */
  23. protected $privateKey = '';
  24. /**
  25. * Current timestamp for all API requests
  26. *
  27. * @var int
  28. */
  29. protected $currentTimeStamp = 0;
  30. /**
  31. * Debug flag
  32. *
  33. * @var bool
  34. */
  35. protected $debug = false;
  36. /**
  37. * Default HLS API URL
  38. */
  39. const URL_API = 'https://api.hls.tv/';
  40. /**
  41. * Default debug log path
  42. */
  43. const LOG_PATH = 'exports/omegatv.log';
  44. /**
  45. * Configs options naming
  46. */
  47. const OPTION_PUBLIC = 'OMEGATV_PUBLIC_KEY';
  48. const OPTION_PRIVATE = 'OMEGATV_PRIVATE_KEY';
  49. /**
  50. * Creates new low-level API object instance
  51. *
  52. * @return void
  53. */
  54. public function __construct() {
  55. $this->loadConfigs();
  56. $this->setOptions();
  57. }
  58. /**
  59. * Loads required configs into protected properties for further usage
  60. *
  61. * @global object $ubillingConfig
  62. *
  63. * @return void
  64. */
  65. protected function loadConfigs() {
  66. global $ubillingConfig;
  67. $this->altCfg = $ubillingConfig->getAlter();
  68. }
  69. /**
  70. * Sets default options to object instance properties
  71. *
  72. * @return void
  73. */
  74. protected function setOptions() {
  75. if ((isset($this->altCfg[self::OPTION_PUBLIC])) AND ( (isset($this->altCfg[self::OPTION_PRIVATE])))) {
  76. $this->publicKey = $this->altCfg[self::OPTION_PUBLIC];
  77. $this->privateKey = $this->altCfg[self::OPTION_PRIVATE];
  78. }
  79. if (isset($this->altCfg['OMEGATV_DEBUG'])) {
  80. if ($this->altCfg['OMEGATV_DEBUG']) {
  81. $this->debug = true;
  82. }
  83. }
  84. $this->currentTimeStamp = time();
  85. }
  86. /**
  87. * Returns new API_HASH for some message
  88. *
  89. * @param array $message
  90. *
  91. * @return string
  92. */
  93. protected function generateApiHash($message = array()) {
  94. $message = $this->currentTimeStamp . $this->publicKey . http_build_query($message, '', '&');
  95. $result = hash_hmac('sha256', $message, $this->privateKey);
  96. return ($result);
  97. }
  98. /**
  99. * Pushes some request to remote API and returns decoded array or raw JSON reply.
  100. *
  101. * @param string $request
  102. * @param array $data
  103. * @param bool $raw
  104. *
  105. * @return array/json
  106. */
  107. public function pushApiRequest($request, $data = array(), $raw = false) {
  108. if ($this->debug) {
  109. file_put_contents(self::LOG_PATH, curdatetime() . "\n", FILE_APPEND);
  110. file_put_contents(self::LOG_PATH, '>>>>>QUERY>>>>>' . "\n", FILE_APPEND);
  111. file_put_contents(self::LOG_PATH, print_r($request, true) . "\n", FILE_APPEND);
  112. file_put_contents(self::LOG_PATH, print_r($data, true) . "\n", FILE_APPEND);
  113. }
  114. $curl = curl_init(self::URL_API . $request);
  115. curl_setopt($curl, CURLOPT_HEADER, false);
  116. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  117. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  118. 'API_ID: ' . $this->publicKey,
  119. 'API_TIME: ' . $this->currentTimeStamp,
  120. 'API_HASH:' . $this->generateApiHash($data)
  121. ));
  122. curl_setopt($curl, CURLOPT_POST, true);
  123. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  124. $jsonResponse = curl_exec($curl);
  125. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  126. if ($status != 200) {
  127. show_error('Error: call to URL ' . self::URL_API . ' failed with status ' . $status . ', response ' . $jsonResponse . ', curl_error ' . curl_error($curl) . ', curl_errno ' . curl_errno($curl));
  128. }
  129. curl_close($curl);
  130. if (!$raw) {
  131. $result = json_decode($jsonResponse, true);
  132. } else {
  133. $result = $jsonResponse;
  134. }
  135. if ($this->debug) {
  136. file_put_contents(self::LOG_PATH, '<<<<<RESPONSE<<<<<' . "\n", FILE_APPEND);
  137. file_put_contents(self::LOG_PATH, print_r($result, true) . "\n", FILE_APPEND);
  138. file_put_contents(self::LOG_PATH, '==================' . "\n", FILE_APPEND);
  139. }
  140. return ($result);
  141. }
  142. /**
  143. * Returns list of promo tariffs
  144. *
  145. * @return array
  146. */
  147. public function getTariffsPromo() {
  148. $result = $this->pushApiRequest('tariff/promo/list');
  149. return ($result);
  150. }
  151. /**
  152. * Returns list of main tariffs
  153. *
  154. * @return array
  155. */
  156. public function getTariffsBase() {
  157. $result = $this->pushApiRequest('tariff/base/list');
  158. return ($result);
  159. }
  160. /**
  161. * Returns list of bundle tariffs
  162. *
  163. * @return array
  164. */
  165. public function getTariffsBundle() {
  166. $result = $this->pushApiRequest('tariff/bundle/list');
  167. return ($result);
  168. }
  169. /**
  170. * Get all user info.
  171. *
  172. * @param int $customerId Unique user ID
  173. *
  174. * @return array
  175. */
  176. public function getUserInfo($customerId) {
  177. $result = $this->pushApiRequest('customer/get', array('customer_id' => $customerId));
  178. return ($result);
  179. }
  180. /**
  181. * Sets base tariff or some additional tariffs
  182. *
  183. * @param int $customerId unique user ID
  184. * @param array $tariffs example: array('base' =>1036, 'bundle' => 1046)
  185. *
  186. * @return array
  187. */
  188. public function setUserTariff($customerId, $tariffs) {
  189. $data = array('customer_id' => $customerId);
  190. if (!empty($tariffs)) {
  191. foreach ($tariffs as $io => $each) {
  192. $data[$io] = $each;
  193. }
  194. }
  195. $result = $this->pushApiRequest('customer/tariff/set', $data);
  196. return ($result);
  197. }
  198. /**
  199. * Sets user as blocked
  200. *
  201. * @param int $customerId
  202. *
  203. * @return array
  204. */
  205. public function setUserBlock($customerId) {
  206. $result = $this->pushApiRequest('customer/block', array('customer_id' => $customerId));
  207. return ($result);
  208. }
  209. /**
  210. * Sets user as unblocked
  211. *
  212. * @param int $customerId
  213. *
  214. * @return array
  215. */
  216. public function setUserActivate($customerId) {
  217. $result = $this->pushApiRequest('customer/activate', array('customer_id' => $customerId));
  218. return ($result);
  219. }
  220. /**
  221. * Returns user device activation code
  222. *
  223. * @param int $customerId
  224. *
  225. * @return array
  226. */
  227. public function getDeviceCode($customerId) {
  228. $result = $this->pushApiRequest('customer/device/get_code', array('customer_id' => $customerId));
  229. return ($result);
  230. }
  231. /**
  232. * Removes user device
  233. *
  234. * @param int $customerId
  235. * @param string $deviceId
  236. *
  237. * @return array
  238. */
  239. public function deleteDevice($customerId, $deviceId) {
  240. $result = $this->pushApiRequest('customer/device/remove', array('customer_id' => $customerId, 'uniq' => $deviceId));
  241. return ($result);
  242. }
  243. /**
  244. * Adds user device
  245. *
  246. * @param int $customerId
  247. * @param string $deviceId
  248. *
  249. * @return array
  250. */
  251. public function addDevice($customerId, $deviceId) {
  252. $result = $this->pushApiRequest('customer/device/add', array('uniq' => $deviceId, 'customer_id' => $customerId));
  253. return ($result);
  254. }
  255. /**
  256. * Returns list of all devices of company
  257. *
  258. * @return array
  259. */
  260. public function getDeviceList() {
  261. $result = array();
  262. $tmp = $this->pushApiRequest('device/list');
  263. //devices is now in items key
  264. if (isset($tmp['result'])) {
  265. if (isset($tmp['result']['items'])) {
  266. if (!empty($tmp['result']['items'])) {
  267. foreach ($tmp['result']['items'] as $io => $each) {
  268. $result['result'][] = $each;
  269. }
  270. }
  271. }
  272. }
  273. //shitty pagination processing here
  274. if (isset($tmp['result']['pages_count'])) {
  275. if ($tmp['result']['pages_count'] > 1) {
  276. $pagesCount = $tmp['result']['pages_count'];
  277. for ($i = 1; $i <= $pagesCount; $i++) {
  278. $tmp = $this->pushApiRequest('device/list', array('page' => $i));
  279. if (isset($tmp['result']['items'])) {
  280. if (!empty($tmp['result']['items'])) {
  281. foreach ($tmp['result']['items'] as $io => $each) {
  282. $result['result'][] = $each;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. return ($result);
  290. }
  291. /**
  292. * Assigns new playlist to some customer
  293. *
  294. * @param int $customerId
  295. *
  296. * @return array
  297. */
  298. public function addPlayList($customerId) {
  299. $result = $this->pushApiRequest('customer/url/add', array('customer_id' => $customerId));
  300. return ($result);
  301. }
  302. /**
  303. * Deletes playlist by its uniq
  304. *
  305. * @param int $customerId
  306. * @param string $playlistId
  307. *
  308. * @return array
  309. */
  310. public function deletePlayList($customerId, $playlistId) {
  311. $result = $this->pushApiRequest('customer/url/remove', array('customer_id' => $customerId, 'uniq' => $playlistId));
  312. return ($result);
  313. }
  314. }