fork(5) download
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3.  
  4. class ZTVApi
  5. {
  6. ///////////////////////////////////////////////////////////////////////////
  7.  
  8. const DATETIME_FORMAT = 'Y:m:d H:i:sO';
  9.  
  10. ///////////////////////////////////////////////////////////////////////////
  11.  
  12. public static function set_terminal_params(stdClass $profile)
  13. {
  14. return HD::make_json_rpc_request('set_terminal_params',
  15. 'macaddr' => ZTVApi::get_mac_addr(),
  16. 'profile_id' => $profile->id
  17. )
  18. );
  19. }
  20.  
  21. ///////////////////////////////////////////////////////////////////////////
  22.  
  23. public static function get_terminal_params()
  24. {
  25. return HD::make_json_rpc_request('get_terminal_params',
  26. 'macaddr' => ZTVApi::get_mac_addr()
  27. )
  28. );
  29. }
  30.  
  31. ///////////////////////////////////////////////////////////////////////////
  32.  
  33. public static function get_profiles()
  34. {
  35. return HD::make_json_rpc_request('get_profiles',
  36. array( 'macaddr' => ZTVApi::get_mac_addr() )
  37. );
  38. }
  39.  
  40. ///////////////////////////////////////////////////////////////////////////
  41.  
  42. public static function get_playlists()
  43. {
  44. return HD::make_json_rpc_request('get_playlists',
  45. 'macaddr' => ZTVApi::get_mac_addr(),
  46. 'profile' => true
  47. )
  48. );
  49. }
  50.  
  51. ///////////////////////////////////////////////////////////////////////////
  52.  
  53. public static function get_epg($media_id, $limit, $start, $stop)
  54. {
  55. return HD::make_json_rpc_request('get_epg',
  56. 'media_id' => $media_id,
  57. 'limit' => $limit,
  58. 'start' => ZTVApi::format_timestamp($start, 'Y-m-d H:i:sO'),
  59. 'stop' => ZTVApi::format_timestamp($stop, 'Y-m-d H:i:sO')
  60. )
  61. );
  62. }
  63.  
  64. ///////////////////////////////////////////////////////////////////////////
  65.  
  66. public static function set_profile(stdClass $profile, $profile_password = null)
  67. {
  68. $query = array(
  69. ZTVApi::set_terminal_params($profile),
  70. ZTVApi::get_terminal_params()
  71. );
  72.  
  73. if( $profile->require_password && !is_null($profile_password) ) {
  74. $query[] = ZTVApi::update_profile($profile, $profile_password);
  75. }
  76.  
  77. return $query;
  78. }
  79.  
  80. ///////////////////////////////////////////////////////////////////////////
  81.  
  82. public static function update_profile(stdClass $profile, $profile_password)
  83. {
  84. return HD::make_json_rpc_request('update_profile',
  85. 'macaddr' => ZTVApi::get_mac_addr(),
  86. 'profile_id' => $profile->id,
  87. 'profile_password' => $profile_password
  88. )
  89. );
  90. }
  91.  
  92. ///////////////////////////////////////////////////////////////////////////
  93.  
  94. public static function call($json_request)
  95. {
  96. $json_reply = null;
  97.  
  98. for($i = 0; $i < 3; ++$i) {
  99. try {
  100. $doc = HD::http_post_document( ZTVConfig::API_URL, json_encode($json_request) );
  101. $json_reply = json_decode($doc);
  102. } catch(Exception $e) {
  103. hd_print("Error: failed to do HTTP-request.");
  104. if($i == 2) {
  105. throw new DuneException('API is unavailable', 0,
  106. ActionFactory::show_error(true, 'Системная ошибка',
  107. 'Сервер недоступен(' . $e->getMessage() . ').',
  108. 'Пожалуйста обратитесь в тех. поддержку.'
  109. )
  110. )
  111. );
  112. }
  113. usleep(100000 << $i);
  114. continue;
  115. }
  116. break;
  117. }
  118.  
  119. if( is_null($json_reply) ) {
  120. hd_print("Error: failed to decode API reply: '$doc'");
  121. throw new DuneException('API returned empty result', 0,
  122. ActionFactory::show_error(true, 'Системная ошибка',
  123. 'Сервер вернул пустой ответ.',
  124. 'Пожалуйста обратитесь в тех. поддержку.'
  125. )
  126. )
  127. );
  128. }
  129.  
  130. if( isset($json_reply->error) && $json_reply->error ) {
  131. hd_print("Error: API returned error status($doc)");
  132. throw new DuneException('API returned error', $json_reply->code,
  133. ActionFactory::show_error(true, 'Системная ошибка',
  134. 'Сервер вернул ошибку(' . $json_reply->message . ').',
  135. 'Пожалуйста обратитесь в тех. поддержку.'
  136. )
  137. )
  138. );
  139. }
  140.  
  141. // TODO: Think of a better check.
  142. if( !isset($json_request['method']) ) {
  143. $request_count = count($json_request);
  144. $reply_count = count($json_reply);
  145. if($request_count != $reply_count) {
  146. return false;
  147. }
  148.  
  149. for($i = 0, $n = 0; $i < $reply_count; $n = $n + ($json_reply[$i]->result ? 1 : 0), $i++);
  150. if($i != $n) {
  151. return false;
  152. }
  153. }
  154.  
  155. return $json_reply;
  156. }
  157.  
  158. public static function format_timestamp($ts, $fmt = ZTVApi::DATETIME_FORMAT)
  159. {
  160. return HD::format_timestamp($ts, $fmt);
  161. }
  162.  
  163. public static function parse_timestamp($ts_str, $fmt = ZTVApi::DATETIME_FORMAT)
  164. {
  165. if ( is_null ($ts_str) ) {
  166. return null;
  167. }
  168.  
  169. $ts = DateTime::createFromFormat($fmt, $ts_str);
  170. if ($ts === false) {
  171. hd_print ("Warning: invalid timestamp string '$ts_str'");
  172. $ts = null;
  173. }
  174. return $ts;
  175. }
  176.  
  177. public static function get_mac_addr()
  178. {
  179. static $mac_addr = null;
  180.  
  181. if ( is_null($mac_addr) ) {
  182. $mac_addr = str_replace( ':', '-', HD::get_mac_addr() );
  183. }
  184.  
  185. return $mac_addr;
  186. }
  187. }
  188.  
  189. ///////////////////////////////////////////////////////////////////////////
  190. ?>
Success #stdin #stdout 0.01s 24448KB
stdin
Standard input is empty
stdout
Standard output is empty