LctvBadge.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * LiveEdu.tv Badges
  4. *
  5. * HTML badges and link buttons for liveedu.tv online status and stream stats
  6. *
  7. * @package LctvBadges\LctvBadges
  8. * @version 0.0.9
  9. * @since 0.0.8
  10. */
  11. require(__DIR__ . '/LctvBadgeConstants.inc') ;
  12. require(__DIR__ . '/../api/LctvApi.inc') ;
  13. require(__DIR__ . '/../img/v1//lctv-badges-svg-v1.inc') ;
  14. abstract class LctvBadge
  15. {
  16. /**
  17. * LctvApi library instance.
  18. *
  19. * @since 0.0.8
  20. * @access protected
  21. * @var LctvApi
  22. */
  23. protected $lctv_api = NULL ;
  24. /**
  25. * API api request object returned from LctvApi library request.
  26. *
  27. * @since 0.0.8
  28. * @access protected
  29. * @var stdClass
  30. */
  31. protected $api_request = NULL ;
  32. /**
  33. * Badge left-side text. (subclasas responsibility)
  34. *
  35. * @since 0.0.8
  36. * @access protected
  37. * @var string
  38. */
  39. protected $badge_l_text = '' ;
  40. /**
  41. * API api request endpoint. (subclasas responsibility)
  42. *
  43. * @since 0.0.8
  44. * @access protected
  45. * @var string
  46. */
  47. protected $api_endpoint = '' ;
  48. /**
  49. * liveedu.tv user with which to authenticate for API request. (user-supplied)
  50. *
  51. * @since 0.0.8
  52. * @access protected
  53. * @var string
  54. */
  55. protected $auth_user = '' ;
  56. /**
  57. * Badge style to return. (user-supplied)
  58. *
  59. * @since 0.0.8
  60. * @access protected
  61. * @var string
  62. */
  63. protected $badge_style = '' ;
  64. /**
  65. * Name of channel for which to fetch data. (user-supplied)
  66. *
  67. * @since 0.0.8
  68. * @access protected
  69. * @var string
  70. */
  71. protected $channel_name = '' ;
  72. /**
  73. * Hyperlink url to wrap image in. (user-supplied)
  74. *
  75. * @since 0.0.8
  76. * @access protected
  77. * @var string
  78. */
  79. protected $link_url = '' ;
  80. /**
  81. * true/false to show streaming title when live. (Overrides online message) (user-supplied)
  82. *
  83. * @since 0.0.8
  84. * @access protected
  85. * @var boolean
  86. */
  87. protected $use_title = false ;
  88. /**
  89. * Badge message if status is streaming. (user-supplied)
  90. *
  91. * @since 0.0.8
  92. * @access protected
  93. * @var string
  94. */
  95. protected $online_text = '' ;
  96. /**
  97. * Badge message if status is not streaming. (user-supplied)
  98. *
  99. * @since 0.0.8
  100. * @access protected
  101. * @var string
  102. */
  103. protected $offline_text = '' ;
  104. /**
  105. * Constructor.
  106. *
  107. * Supplied $args override class property defaults.
  108. * Handle auth requests and token checks on instantiation.
  109. *
  110. * @since 0.0.8
  111. *
  112. * @param array $params - user supplied badge styling parameters set to instance vars.
  113. * ->auth_user: (Optional) liveedu.tv user with which to authenticate for API request.
  114. * ->badge_style: (Optional) Badge style to return.
  115. * ->channel_name: (Required) Name of channel for which to fetch data.
  116. * ->link_url: (Optional) true/false to wrap image in hyperlink to channel.
  117. * ->use_title: (Optional) true/false to show streaming title when live. (Overrides online message.)
  118. * ->online_text: (Optional) Badge message if status is streaming.
  119. * ->offline_text: (Optional) Badge message if status is not streaming.
  120. */
  121. function __construct($params)
  122. {
  123. /* Set instance vars. */
  124. $keys = array_keys(get_object_vars($this)) ;
  125. foreach($keys as $key) if (isset($params[$key])) $this->$key = $params[$key] ;
  126. if (empty($this->channel_name)) throw new Exception(INVALID_CHANNEL_MSG , 1) ;
  127. /* Instantiate the API wrapper. */
  128. try { $this->lctv_api = new LctvApi($this->auth_user) ; }
  129. catch( Exception $ex ) { die($ex->getMessage()) ; }
  130. // assert that we have authorized with the LCTV API
  131. if (!$this->lctv_api->is_authorized()) self::returnUnauthorizedBadge() ;
  132. }
  133. /**
  134. * Send per-subclass request to the liveedu.tv API.
  135. *
  136. * @since 0.0.8
  137. * @access protected
  138. */
  139. protected function fetchData()
  140. {
  141. // make RESTful request
  142. $this->api_request = $this->lctv_api->api_request($this->api_endpoint) ;
  143. // if (isset($this->api_request->result->detail)) echo "fetchData() error=" . $this->api_request->result->detail ;
  144. // return API error status
  145. return $this->api_request !== false && !isset($this->api_request->result->detail) ;
  146. }
  147. /**
  148. * Return a badge indicating unauthorized API request.
  149. *
  150. * @since 0.0.8
  151. * @access private
  152. */
  153. private function returnUnauthorizedBadge()
  154. {
  155. header( "Content-type:image/svg+xml" );
  156. echo make_badge_svg_v1('' , UNAUTHORIZED_R_TEXT , RED_COLOR) ;
  157. die() ;
  158. }
  159. /**
  160. * Return a badge indicating API request error.
  161. *
  162. * @since 0.0.8
  163. * @access protected
  164. */
  165. protected function returnErrorBadge()
  166. {
  167. $request = $this->api_request ;
  168. $result = ($request !== false ) ? $request->result : NULL ;
  169. $detail = ($result !== NULL && isset($result->detail)) ? $result ->detail : NULL ;
  170. $error_msg = ($detail !== NULL ) ? $detail : "unknown error" ;
  171. header( "Content-type:image/svg+xml" );
  172. echo make_badge_svg_v1($this->badge_l_text , $error_msg , RED_COLOR) ;
  173. die() ;
  174. }
  175. }
  176. class LctvBadgeStatus extends LctvBadge
  177. {
  178. function __construct($badge_params)
  179. {
  180. parent::__construct($badge_params) ;
  181. $this->badge_l_text = STATUS_LTEXT ;
  182. $this->api_endpoint = STATUS_ENDPOINT . urlencode($this->channel_name) . '/' ;
  183. // API returned an error - this happens if user is not streaming
  184. if (!parent::fetchData()) $this->api_request->result->is_live = false ;
  185. $is_online = $this->api_request->result->is_live ;
  186. if ($this->badge_style == STATUS_V2_STYLE )
  187. header("Location: " . (($is_online) ? V2_ONLINE_PNG : V2_OFFLINE_PNG )) ;
  188. else if ($this->badge_style == STATUS_V3_STYLE )
  189. header("Location: " . (($is_online) ? V3_ONLINE_PNG : V3_OFFLINE_PNG )) ;
  190. else if ($this->badge_style == STATUS_V3_HOVER_STYLE )
  191. header("Location: " . (($is_online) ? V3_ONLINE_HOVER_PNG : V3_OFFLINE_HOVER_PNG )) ;
  192. else if ($this->badge_style == STATUS_V3_PUSHED_STYLE)
  193. header("Location: " . (($is_online) ? V3_ONLINE_PUSHED_PNG : V3_OFFLINE_PUSHED_PNG)) ;
  194. else /* if ($this->badge_style == STATUS_V1_STYLE) */
  195. {
  196. // display live stream title as $online_text
  197. $should_use_title = $this->use_title && !empty($this->api_request->result->title) ;
  198. $online_text = ($should_use_title) ? $this->api_request->result->title : ONLINE_TEXT ;
  199. $badge_r_text = ($is_online) ? $online_text : OFFLINE_TEXT ;
  200. $badge_r_color = ($is_online) ? GREEN_COLOR : RED_COLOR ;
  201. header( "Content-type:image/svg+xml" );
  202. echo make_badge_svg_v1($this->badge_l_text , $badge_r_text , $badge_r_color , $this->link_url) ;
  203. }
  204. }
  205. }
  206. class LctvBadgeViewers extends LctvBadge
  207. {
  208. function __construct($badge_params)
  209. {
  210. parent::__construct($badge_params) ;
  211. $this->badge_l_text = VIEWERS_LTEXT ;
  212. $this->api_endpoint = VIEWERS_ENDPOINT . urlencode($this->channel_name) . '/' ;
  213. // API returned an error - this happens if user is not streaming
  214. if (!parent::fetchData())
  215. {
  216. $this->api_request->result->viewers_live = 0 ;
  217. $this->api_request->result->is_live = false ;
  218. }
  219. $badge_r_text = $this->api_request->result->viewers_live ;
  220. $badge_r_color = ($this->api_request->result->is_live) ? GREEN_COLOR : RED_COLOR ;
  221. header( "Content-type:image/svg+xml" );
  222. echo make_badge_svg_v1($this->badge_l_text , $badge_r_text , $badge_r_color , $this->link_url) ;
  223. }
  224. }
  225. class LctvBadgeNextStream extends LctvBadge
  226. {
  227. function __construct($badge_params)
  228. {
  229. parent::__construct($badge_params) ;
  230. $this->badge_l_text = NEXTSTREAM_LTEXT ;
  231. $this->api_endpoint = NEXTSTREAM_ENDPOINT ;
  232. parent::fetchData() ;
  233. // search for specified channel name among all scheduled streams
  234. $streams = array_reverse($this->api_request->result->results) ;
  235. $stream_n = count($streams) ;
  236. while ($stream_n-- && $stream = $streams[$stream_n] &&
  237. strpos($stream->livestream , CHANNEL_NAME) === false) ;
  238. $next_stream = strtotime($stream->start_time_original_timezone) ;
  239. if (~$stream_n)
  240. {
  241. $badge_r_text = date('M j @ g:i a' , $next_stream) ;
  242. $badge_r_color = GREEN_COLOR ;
  243. }
  244. else
  245. {
  246. $badge_r_text = NO_NEXT_STREAM_TEXT ;
  247. $badge_r_color = RED_COLOR ;
  248. }
  249. header("Content-type:image/svg+xml") ;
  250. echo make_badge_svg_v1($this->badge_l_text , $badge_r_text , $badge_r_color , $this->link_url) ;
  251. }
  252. }
  253. class LctvBadgeFollowers extends LctvBadge
  254. {
  255. function __construct($badge_params)
  256. {
  257. parent::__construct($badge_params) ;
  258. $this->badge_l_text = FOLLOWERS_LTEXT ;
  259. $this->api_endpoint = FOLLOWERS_ENDPOINT ;
  260. if (!parent::fetchData()) { parent::returnErrorBadge() ; return ; }
  261. $badge_r_text = count($this->api_request->result) ;
  262. header("Content-type:image/svg+xml") ;
  263. echo make_badge_svg_v1($this->badge_l_text , $badge_r_text , GREEN_COLOR , $this->link_url) ;
  264. }
  265. }
  266. class LctvBadgeLastStream extends LctvBadge
  267. {
  268. function __construct($badge_params)
  269. {
  270. parent::__construct($badge_params) ;
  271. $this->badge_l_text = LASTSTREAM_LTEXT ;
  272. $this->api_endpoint = LASTSTREAM_ENDPOINT ;
  273. if (!parent::fetchData()) { parent::returnErrorBadge() ; return ; }
  274. $streams = $this->api_request->result ;
  275. $last_stream = $streams[0] ;
  276. $last_stream_dt = $last_stream->creation_time ;
  277. if (is_array($streams) && count($streams) > 0 && !empty($last_stream_dt))
  278. {
  279. $badge_r_text = date('M j, Y' , strtotime($last_stream_dt)) ;
  280. $badge_r_color = GREEN_COLOR ;
  281. }
  282. else
  283. {
  284. $badge_r_text = NO_LAST_STREAM_TEXT ;
  285. $badge_r_color = RED_COLOR ;
  286. }
  287. header("Content-type:image/svg+xml") ;
  288. echo make_badge_svg_v1($this->badge_l_text , $badge_r_text , $badge_r_color , $this->link_url) ;
  289. }
  290. }
  291. ?>