StreamSetting.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. class Application_Model_StreamSetting
  3. {
  4. public static function setValue($key, $value, $type)
  5. {
  6. $con = Propel::getConnection();
  7. // Check if key already exists
  8. $sql = "SELECT COUNT(*) FROM cc_stream_setting"
  9. ." WHERE keyname = :key";
  10. $stmt = $con->prepare($sql);
  11. $stmt->bindParam(':key', $key);
  12. if ($stmt->execute()) {
  13. $result = $stmt->fetchColumn(0);
  14. } else {
  15. $msg = implode(',', $stmt->errorInfo());
  16. throw new Exception("Error: $msg");
  17. }
  18. if ($result == 1) {
  19. $sql = "UPDATE cc_stream_setting"
  20. ." SET value = :value, type = :type"
  21. ." WHERE keyname = :key";
  22. } else {
  23. $sql = "INSERT INTO cc_stream_setting (keyname, value, type)"
  24. ." VALUES (:key, :value, :type)";
  25. }
  26. $stmt = $con->prepare($sql);
  27. $stmt->bindParam(':key', $key);
  28. $stmt->bindParam(':value', $value);
  29. $stmt->bindParam(':type', $type);
  30. if ($stmt->execute()) {
  31. //do nothing
  32. } else {
  33. $msg = implode(',', $stmt->errorInfo());
  34. throw new Exception("Error: $msg");
  35. }
  36. }
  37. public static function getValue($key)
  38. {
  39. $con = Propel::getConnection();
  40. //Check if key already exists
  41. $sql = "SELECT value FROM cc_stream_setting"
  42. ." WHERE keyname = :key";
  43. $stmt = $con->prepare($sql);
  44. $stmt->bindParam(':key', $key);
  45. if ($stmt->execute()) {
  46. $result = $stmt->fetchColumn(0);
  47. } else {
  48. $msg = implode(',', $stmt->errorInfo());
  49. throw new Exception("Error: $msg");
  50. }
  51. return $result ? $result : "";
  52. }
  53. /* Returns the id's of all streams that are enabled in an array. An
  54. * example of the array returned in JSON notation is ["s1", "s2", "s3"] */
  55. public static function getEnabledStreamIds()
  56. {
  57. $con = Propel::getConnection();
  58. $sql = "SELECT * "
  59. ."FROM cc_stream_setting "
  60. ."WHERE keyname LIKE '%_enable' "
  61. ."AND value='true'";
  62. $ids = array();
  63. $rows = Application_Common_Database::prepareAndExecute($sql, array(), 'all');
  64. foreach ($rows as $row) {
  65. $ids[] = substr($row["keyname"], 0, strpos($row["keyname"], "_"));
  66. }
  67. return $ids;
  68. }
  69. /* Returns only global data as array*/
  70. public static function getGlobalData()
  71. {
  72. $con = Propel::getConnection();
  73. $sql = "SELECT * "
  74. ."FROM cc_stream_setting "
  75. ."WHERE keyname IN ('output_sound_device', 'icecast_vorbis_metadata')";
  76. $rows = Application_Common_Database::prepareAndExecute($sql, array(), 'all');
  77. $data = array();
  78. foreach ($rows as $row) {
  79. $data[$row["keyname"]] = $row["value"];
  80. }
  81. return $data;
  82. }
  83. /* Returns all information related to a specific stream. An example
  84. * of a stream id is 's1' or 's2'. */
  85. public static function getStreamData($p_streamId)
  86. {
  87. $con = Propel::getConnection();
  88. $streamId = pg_escape_string($p_streamId);
  89. $sql = "SELECT * "
  90. ."FROM cc_stream_setting "
  91. ."WHERE keyname LIKE '{$streamId}_%'";
  92. $stmt = $con->prepare($sql);
  93. if ($stmt->execute()) {
  94. $rows = $stmt->fetchAll();
  95. } else {
  96. $msg = implode(',', $stmt->errorInfo());
  97. throw new Exception("Error: $msg");
  98. }
  99. $data = array();
  100. foreach ($rows as $row) {
  101. $data[$row["keyname"]] = $row["value"];
  102. }
  103. return $data;
  104. }
  105. /* Similar to getStreamData, but removes all sX prefixes to
  106. * make data easier to iterate over */
  107. public static function getStreamDataNormalized($p_streamId)
  108. {
  109. $con = Propel::getConnection();
  110. $streamId = pg_escape_string($p_streamId);
  111. $sql = "SELECT * "
  112. ."FROM cc_stream_setting "
  113. ."WHERE keyname LIKE '{$streamId}_%'";
  114. $stmt = $con->prepare($sql);
  115. if ($stmt->execute()) {
  116. $rows = $stmt->fetchAll();
  117. } else {
  118. $msg = implode(',', $stmt->errorInfo());
  119. throw new Exception("Error: $msg");
  120. }
  121. $data = array();
  122. foreach ($rows as $row) {
  123. list($id, $key) = explode("_", $row["keyname"], 2);
  124. $data[$key] = $row["value"];
  125. }
  126. return $data;
  127. }
  128. public static function getStreamSetting()
  129. {
  130. $con = Propel::getConnection();
  131. $sql = "SELECT *"
  132. ." FROM cc_stream_setting"
  133. ." WHERE keyname not like '%_error' AND keyname not like '%_admin_%'";
  134. $rows = Application_Common_Database::prepareAndExecute($sql, array(), 'all');
  135. $exists = array();
  136. foreach ($rows as $r) {
  137. if ($r['keyname'] == 'master_live_stream_port') {
  138. $exists['master_live_stream_port'] = true;
  139. } elseif ($r['keyname'] == 'master_live_stream_mp') {
  140. $exists['master_live_stream_mp'] = true;
  141. } elseif ($r['keyname'] == 'dj_live_stream_port') {
  142. $exists['dj_live_stream_port'] = true;
  143. } elseif ($r['keyname'] == 'dj_live_stream_mp') {
  144. $exists['dj_live_stream_mp'] = true;
  145. }
  146. }
  147. if (!isset($exists["master_live_stream_port"])) {
  148. $rows[] = array("keyname" =>"master_live_stream_port",
  149. "value"=>self::getMasterLiveStreamPort(),
  150. "type"=>"integer");
  151. }
  152. if (!isset($exists["master_live_stream_mp"])) {
  153. $rows[] = array("keyname" =>"master_live_stream_mp",
  154. "value"=>self::getMasterLiveStreamMountPoint(),
  155. "type"=>"string");
  156. }
  157. if (!isset($exists["dj_live_stream_port"])) {
  158. $rows[] = array("keyname" =>"dj_live_stream_port",
  159. "value"=>self::getDjLiveStreamPort(),
  160. "type"=>"integer");
  161. }
  162. if (!isset($exists["dj_live_stream_mp"])) {
  163. $rows[] = array("keyname" =>"dj_live_stream_mp",
  164. "value"=>self::getDjLiveStreamMountPoint(),
  165. "type"=>"string");
  166. }
  167. return $rows;
  168. }
  169. private static function saveStreamSetting($key, $value)
  170. {
  171. $stream_setting = CcStreamSettingQuery::create()->filterByDbKeyName($key)->findOne();
  172. if (is_null($stream_setting)) {
  173. throw new Exception("Keyname $key does not exist!");
  174. }
  175. $stream_setting->setDbValue($value);
  176. $stream_setting->save();
  177. }
  178. /*
  179. * function that take all the information of stream and sets them.
  180. * This is used by stream setting via UI.
  181. *
  182. * @param $data - array that contains all the data. $data is [][] which
  183. * contains multiple stream information
  184. */
  185. public static function setStreamSetting($data)
  186. {
  187. foreach ($data as $key => $d) {
  188. if ($key == "output_sound_device" || $key == "icecast_vorbis_metadata") {
  189. $v = ($d == 1) ? "true" : "false";
  190. self::saveStreamSetting($key, $v);
  191. } elseif ($key == "output_sound_device_type") {
  192. self::saveStreamSetting($key, $d);
  193. } elseif (is_array($d)) {
  194. $temp = explode('_', $key);
  195. $prefix = $temp[0];
  196. foreach ($d as $k => $v) {
  197. $keyname = $prefix . "_" . $k;
  198. if ($k == 'enable') {
  199. $v = $d['enable'] == 1 ? 'true' : 'false';
  200. }
  201. $v = trim($v);
  202. if ($k != 'admin_pass') {
  203. self::saveStreamSetting($keyname, $v);
  204. /* We use 'xxxxxx' as the admin password placeholder so we
  205. * only want to save it when it is a different string
  206. */
  207. } elseif ($v != 'xxxxxx') {
  208. self::saveStreamSetting($keyname, $v);
  209. }
  210. }
  211. }
  212. }
  213. }
  214. /*
  215. * Sets indivisual stream setting.
  216. *
  217. * $data - data array. $data is [].
  218. * TODO: Make this SQL a prepared statement!
  219. *
  220. * Do not remove this function. It is called by airtime-system.php
  221. */
  222. public static function setIndividualStreamSetting($data)
  223. {
  224. foreach ($data as $keyname => $v) {
  225. $sql = "UPDATE cc_stream_setting SET value=:v WHERE keyname=:keyname";
  226. $map = array(":v" => $v, ":keyname"=>$keyname);
  227. $res = Application_Common_Database::prepareAndExecute($sql, $map,
  228. Application_Common_Database::EXECUTE);
  229. }
  230. }
  231. /*
  232. * Stores liquidsoap status if $boot_time > save time.
  233. * save time is the time that user clicked save on stream setting page
  234. */
  235. public static function setLiquidsoapError($stream_id, $msg, $boot_time=null)
  236. {
  237. $con = Propel::getConnection();
  238. $update_time = Application_Model_Preference::GetStreamUpdateTimestemp();
  239. if ($boot_time == null || $boot_time > $update_time) {
  240. $keyname = "s".$stream_id."_liquidsoap_error";
  241. $sql = "SELECT COUNT(*) FROM cc_stream_setting"
  242. ." WHERE keyname = :keyname";
  243. $stmt = $con->prepare($sql);
  244. $stmt->bindParam(':keyname', $keyname);
  245. if ($stmt->execute()) {
  246. $result= $stmt->fetchColumn(0);
  247. } else {
  248. $msg = implode(',', $stmt->errorInfo());
  249. throw new Exception("Error: $msg");
  250. }
  251. if ($result == 1) {
  252. $sql = "UPDATE cc_stream_setting"
  253. ." SET value = :msg"
  254. ." WHERE keyname = :keyname";
  255. } else {
  256. $sql = "INSERT INTO cc_stream_setting (keyname, value, type)"
  257. ." VALUES (:keyname, :msg, 'string')";
  258. }
  259. $stmt = $con->prepare($sql);
  260. $stmt->bindParam(':keyname', $keyname);
  261. $stmt->bindParam(':msg', $msg);
  262. if ($stmt->execute()) {
  263. //do nothing
  264. } else {
  265. $msg = implode(',', $stmt->errorInfo());
  266. throw new Exception("Error: $msg");
  267. }
  268. }
  269. }
  270. public static function getLiquidsoapError($stream_id)
  271. {
  272. $con = Propel::getConnection();
  273. $keyname = "s".$stream_id."_liquidsoap_error";
  274. $sql = "SELECT value FROM cc_stream_setting"
  275. ." WHERE keyname = :keyname";
  276. $stmt = $con->prepare($sql);
  277. $stmt->bindParam(':keyname', $keyname);
  278. if ($stmt->execute()) {
  279. $result= $stmt->fetchColumn(0);
  280. } else {
  281. $msg = implode(',', $stmt->errorInfo());
  282. throw new Exception("Error: $msg");
  283. }
  284. return ($result !== false) ? $result : null;
  285. }
  286. public static function getStreamEnabled($stream_id)
  287. {
  288. $con = Propel::getConnection();
  289. $keyname = "s" . $stream_id . "_enable";
  290. $sql = "SELECT value FROM cc_stream_setting"
  291. ." WHERE keyname = :keyname";
  292. $stmt = $con->prepare($sql);
  293. $stmt->bindParam(':keyname', $keyname);
  294. if ($stmt->execute()) {
  295. $result= $stmt->fetchColumn(0);
  296. } else {
  297. $msg = implode(',', $stmt->errorInfo());
  298. throw new Exception("Error: $msg");
  299. }
  300. return ($result != 'false');
  301. }
  302. /*
  303. * Only returns info that is needed for data collection
  304. * returns array('s1'=>array(keyname=>value))
  305. */
  306. public static function getStreamInfoForDataCollection()
  307. {
  308. $con = Propel::getConnection();
  309. $out = array();
  310. $enabled_stream = self::getEnabledStreamIds();
  311. foreach ($enabled_stream as $stream) {
  312. $keys = array("{$stream}_output", "{$stream}_type", "{$stream}_bitrate", "{$stream}_host");
  313. $key_csv = implode(',', $keys);
  314. $sql = "SELECT keyname, value FROM cc_stream_setting"
  315. ." WHERE keyname IN (:key_csv)";
  316. $stmt = $con->prepare($sql);
  317. $stmt->bindParam(':key_csv', $key_csv);
  318. if ($stmt->execute()) {
  319. $rows = $stmt->fetchAll();
  320. } else {
  321. $msg = implode(',', $stmt->errorInfo());
  322. throw new Exception("Error: $msg");
  323. }
  324. $info = array();
  325. foreach ($rows as $r) {
  326. $temp = explode("_", $r['keyname']);
  327. $info[$temp[1]] = $r['value'];
  328. $out[$stream] = $info;
  329. }
  330. }
  331. return $out;
  332. }
  333. public static function setMasterLiveStreamPort($value)
  334. {
  335. self::setValue("master_live_stream_port", $value, "integer");
  336. }
  337. public static function getMasterLiveStreamPort()
  338. {
  339. return self::getValue("master_live_stream_port");
  340. }
  341. public static function setMasterLiveStreamMountPoint($value)
  342. {
  343. self::setValue("master_live_stream_mp", $value, "string");
  344. }
  345. public static function getMasterLiveStreamMountPoint()
  346. {
  347. return self::getValue("master_live_stream_mp");
  348. }
  349. public static function setDjLiveStreamPort($value)
  350. {
  351. self::setValue("dj_live_stream_port", $value, "integer");
  352. }
  353. public static function getDjLiveStreamPort()
  354. {
  355. return self::getValue("dj_live_stream_port");
  356. }
  357. public static function setDjLiveStreamMountPoint($value)
  358. {
  359. self::setValue("dj_live_stream_mp", $value, "string");
  360. }
  361. public static function getDjLiveStreamMountPoint()
  362. {
  363. return self::getValue("dj_live_stream_mp");
  364. }
  365. public static function getAdminUser($stream){
  366. return self::getValue($stream."_admin_user");
  367. }
  368. public static function setAdminUser($stream, $v){
  369. self::setValue($stream."_admin_user", $v, "string");
  370. }
  371. public static function getAdminPass($stream){
  372. return self::getValue($stream."_admin_pass");
  373. }
  374. public static function setAdminPass($stream, $v){
  375. self::setValue($stream."_admin_pass", $v, "string");
  376. }
  377. public static function getOffAirMeta(){
  378. return self::getValue("off_air_meta");
  379. }
  380. public static function setOffAirMeta($offAirMeta){
  381. self::setValue("off_air_meta", $offAirMeta, "string");
  382. }
  383. public static function GetAllListenerStatErrors(){
  384. $sql = "SELECT * FROM cc_stream_setting WHERE keyname like :p1";
  385. $mounts = Application_Common_Database::prepareAndExecute($sql, array(':p1'=>'%_mount'));
  386. $mps = array();
  387. foreach($mounts as $mount) {
  388. $mps[] = "'" .$mount["value"] . "_listener_stat_error'";
  389. }
  390. $in = implode(",", $mps);
  391. $sql = "SELECT * FROM cc_stream_setting WHERE keyname IN ( $in )";
  392. return Application_Common_Database::prepareAndExecute($sql, array());
  393. }
  394. public static function SetListenerStatError($key, $v) {
  395. self::setValue($key, $v, 'string');
  396. }
  397. }