KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
7.1KB

  1. <?php
  2. class Audio extends Feathers implements Feather {
  3. public function __init() {
  4. $this->setField(array("attr" => "audio",
  5. "type" => "file",
  6. "label" => __("MP3 File", "audio"),
  7. "note" => "<small>(Max. file size: ".ini_get('upload_max_filesize').")</small>"));
  8. if (isset($_GET['action']) and $_GET['action'] == "bookmarklet")
  9. $this->setField(array("attr" => "from_url",
  10. "type" => "text",
  11. "label" => __("From URL?", "audio"),
  12. "optional" => true,
  13. "no_value" => true));
  14. $this->setField(array("attr" => "description",
  15. "type" => "text_block",
  16. "label" => __("Description", "audio"),
  17. "optional" => true,
  18. "preview" => true,
  19. "bookmarklet" => "selection"));
  20. $this->setFilter("description", array("markup_text", "markup_post_text"));
  21. $this->respondTo("delete_post", "delete_file");
  22. $this->respondTo("javascript", "player_js");
  23. $this->respondTo("feed_item", "enclose_mp3");
  24. $this->respondTo("filter_post", "filter_post");
  25. $this->respondTo("admin_write_post", "swfupload");
  26. $this->respondTo("admin_edit_post", "swfupload");
  27. $this->respondTo("post_options", "add_option");
  28. }
  29. public function swfupload($admin, $post = null) {
  30. if (isset($post) and $post->feather != "audio" or
  31. isset($_GET['feather']) and $_GET['feather'] != "audio")
  32. return;
  33. Trigger::current()->call("prepare_swfupload", "audio", "*.mp3");
  34. }
  35. public function submit() {
  36. if (!isset($_POST['filename'])) {
  37. if (isset($_FILES['audio']) and $_FILES['audio']['error'] == 0)
  38. $filename = upload($_FILES['audio'], "mp3");
  39. elseif (!empty($_POST['from_url']))
  40. $filename = upload_from_url($_POST['from_url'], "mp3");
  41. else
  42. error(__("Error"), __("Couldn't upload audio file."));
  43. } else
  44. $filename = $_POST['filename'];
  45. return Post::add(array("filename" => $filename,
  46. "description" => $_POST['description']),
  47. $_POST['slug'],
  48. Post::check_url($_POST['slug']));
  49. }
  50. public function update($post) {
  51. if (!isset($_POST['filename']))
  52. if (isset($_FILES['audio']) and $_FILES['audio']['error'] == 0) {
  53. $this->delete_file($post);
  54. $filename = upload($_FILES['audio'], "mp3");
  55. } elseif (!empty($_POST['from_url'])) {
  56. $this->delete_file($post);
  57. $filename = upload_from_url($_POST['from_url'], "mp3");
  58. } else
  59. $filename = $post->filename;
  60. else {
  61. $this->delete_file($post);
  62. $filename = $_POST['filename'];
  63. }
  64. $post->update(array("filename" => $filename,
  65. "description" => $_POST['description']));
  66. }
  67. public function title($post) {
  68. return oneof($post->title, $post->title_from_excerpt());
  69. }
  70. public function excerpt($post) {
  71. return $post->description;
  72. }
  73. public function feed_content($post) {
  74. return $post->description;
  75. }
  76. public function delete_file($post) {
  77. if ($post->feather != "audio") return;
  78. unlink(MAIN_DIR.Config::current()->uploads_path.$post->filename);
  79. }
  80. public function filter_post($post) {
  81. if ($post->feather != "audio") return;
  82. $post->audio_player = $this->flash_player_for($post->filename, array(), $post);
  83. }
  84. public function player_js() {
  85. ?>//<script>
  86. var ap_instances = new Array();
  87. function ap_stopAll(playerID) {
  88. for(var i = 0;i<ap_instances.length;i++) {
  89. try {
  90. if(ap_instances[i] != playerID) document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 1);
  91. else document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 0);
  92. } catch( errorObject ) {
  93. // stop any errors
  94. }
  95. }
  96. }
  97. function ap_registerPlayers() {
  98. var objectID;
  99. var objectTags = document.getElementsByTagName("object");
  100. for(var i=0;i<objectTags.length;i++) {
  101. objectID = objectTags[i].id;
  102. if(objectID.indexOf("audioplayer") == 0) {
  103. ap_instances[i] = objectID.substring(11, objectID.length);
  104. }
  105. }
  106. }
  107. var ap_clearID = setInterval( ap_registerPlayers, 100 );
  108. <?php
  109. }
  110. public function enclose_mp3($post) {
  111. $config = Config::current();
  112. if ($post->feather != "audio" or !file_exists(uploaded($post->filename, false)))
  113. return;
  114. $length = filesize(uploaded($post->filename, false));
  115. echo ' <link rel="enclosure" href="'.uploaded($post->filename).'" type="audio/mpeg" title="MP3" length="'.$length.'" />'."\n";
  116. }
  117. public function flash_player_for($filename, $params = array(), $post) {
  118. $vars = "";
  119. foreach ($params as $name => $val)
  120. $vars.= "&amp;".$name."=".$val;
  121. $config = Config::current();
  122. $player = '<script src="'.$config->chyrp_url.'/feathers/audio/lib/audio-player.js" type="text/javascript" charset="utf-8"></script>'."\n";
  123. $player.= '<object type="application/x-shockwave-flash" data="'.$config->chyrp_url.'/feathers/audio/lib/player.swf" id="audioplayer'.$post->id.'" height="24" width="290">'."\n\t";
  124. $player.= '<param name="movie" value="'.$config->chyrp_url.'/feathers/audio/lib/player.swf" />'."\n\t";
  125. $player.= '<param name="FlashVars" value="playerID='.$post->id.'&amp;soundFile='.$config->chyrp_url.$config->uploads_path.$filename.$vars.'" />'."\n\t";
  126. $player.= '<param name="quality" value="high" />'."\n\t";
  127. $player.= '<param name="menu" value="false" />'."\n\t";
  128. $player.= '<param name="wmode" value="transparent" />'."\n";
  129. $player.= '</object>'."\n";
  130. return $player;
  131. }
  132. public function add_option($options, $post = null) {
  133. if (isset($post) and $post->feather != "audio") return;
  134. if (!isset($_GET['feather']) and Config::current()->enabled_feathers[0] != "audio" or
  135. isset($_GET['feather']) and $_GET['feather'] != "audio") return;
  136. $options[] = array("attr" => "from_url",
  137. "label" => __("From URL?", "audio"),
  138. "type" => "text");
  139. return $options;
  140. }
  141. }