[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/swftools/shared/1pixelout/ -> audio-player.php (source)

   1  <?php
   2  /*

   3  Plugin Name: Audio player

   4  Plugin URI: http://www.1pixelout.net/code/audio-player-wordpress-plugin/

   5  Description: Highly configurable single track mp3 player

   6  Version: 1.2.3

   7  Author: Martin Laine

   8  Author URI: http://www.1pixelout.net

   9  

  10  Change log:

  11  

  12      1.2.3 (01 March 2006)

  13      

  14          * Added page background and disable transparency option

  15  

  16      1.2.2 (14 February 2006)

  17      

  18          * Fixed a bug for the "replace all mp3 links" option (now case-insensitive)

  19  

  20      1.2.1 (07 February 2006)

  21      

  22          * Fixed a bug for the "replace all mp3 links" option (now supports extra attributes in a tags)

  23  

  24      1.2 (07 February 2006)

  25      

  26          * Implemented post/pre append clip feature

  27          * Amended player to allow for clip sequence playback

  28          * Improved plugn php code syntax

  29          * Minor improvements to slider bar appearance

  30          * Added configurable behaviour options: [audio] syntax, enclosure integration and mp3 link replace

  31          * Added configurable RSS alternate content option: insert download link, nothing or custom content

  32          * Player now closes automatically if you open another one on the same page

  33          * Fixed a problem with colour options in Flash 6

  34          * Added player preview to colour scheme configurator

  35          * Check for updates and automatic upgrade feature

  36  

  37      1.0.1 (31 December 2005)

  38  

  39          * All text fields now use device fonts (much crisper text rendering, support for many more characters and even smaller player file size)

  40          * General clean up and commenting of source code

  41  

  42      1.0 (26 December 2005)

  43          

  44          * Player now based on the emff player (http://www.marcreichelt.de/)

  45          * New thinner design (suggested by Don Bledsoe - http://screenwriterradio.com/)

  46          * More colour options

  47          * New slider function to move around the track

  48          * Simple scrolling ID3 tag support for title and artist (thanks to Ari - http://www.adrstudios.com/)

  49          * Time display now includes hours for very long tracks

  50          * Support for autostart and loop (suggested by gotjosh - http://gotblogua.gotjosh.net/)

  51          * Support for custom colours per player instance

  52          * Fixed an issue with rss feeds. Post content in rss feeds now only shows a link to the file rather than the player (thanks to Blair Kitchen - http://96rpm.the-blair.com/)

  53          * Better handling of buffering and file not found errors 

  54  

  55      0.7.1 beta (29 October 2005)

  56  

  57          * MP3 files are no longer pre-loaded (saves on bandwidth if you have multiple players on one page)

  58  

  59      0.7 beta (24 October 2005)

  60  

  61          * Added colour customisation.

  62  

  63      0.6 beta (23 October 2005)

  64  

  65          * Fixed bug in flash player: progress bar was not updating properly.

  66  

  67      0.5 beta (19 October 2005)

  68  

  69          * Moved player.swf to plugins folder

  70          * Default location of audio files is now top-level /audio folder

  71          * Better handling of paths and URIs

  72          * Added support for linking to external files

  73  

  74      0.2 beta (19 October 2005)

  75  

  76          * Bug fix: the paths to the flash player and the mp3 files didn?t respect the web path option. This caused problems for blogs that don?t live in the root of the domain (eg www.mydomain.com/blog/)

  77  

  78  License:

  79  

  80      Copyright 2005-2006  Martin Laine  (email : martin@1pixelout.net)

  81  

  82      This program is free software; you can redistribute it and/or modify

  83      it under the terms of the GNU General Public License as published by

  84      the Free Software Foundation; either version 2 of the License, or

  85      (at your option) any later version.

  86  

  87      This program is distributed in the hope that it will be useful,

  88      but WITHOUT ANY WARRANTY; without even the implied warranty of

  89      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

  90      GNU General Public License for more details.

  91  

  92      You should have received a copy of the GNU General Public License

  93      along with this program; if not, write to the Free Software

  94      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  95  */
  96  
  97  // Option defaults

  98  add_option('audio_player_web_path', '/audio', "Web path to audio files", true);
  99  add_option('audio_player_behaviour', 'default', "Plugin behaviour", true);
 100  add_option('audio_player_rssalternate', 'nothing', "RSS alternate content", true);
 101  add_option('audio_player_rsscustomalternate', '[See post to listen to audio]', "Custom RSS alternate content", true);
 102  add_option('audio_player_prefixaudio', '', "Pre-Stream Audio", true);
 103  add_option('audio_player_postfixaudio', '', "Post-Stream Audio", true);
 104  
 105  if(get_option('audio_player_iconcolor') != '' && get_option('audio_player_lefticoncolor') == '') {
 106      // Upgrade options from version 0.x

 107      $ap_color = '';
 108      $ap_color = str_replace("#", "0x", get_option('audio_player_iconcolor'));
 109      add_option('audio_player_lefticoncolor', $ap_color, "Left icon color", true);
 110      add_option('audio_player_righticoncolor', $ap_color, "Right icon color", true);
 111      add_option('audio_player_righticonhovercolor', $ap_color, "Right icon hover color", true);
 112      delete_option('audio_player_iconcolor');
 113  
 114      update_option('audio_player_textcolor', str_replace("#", "0x", get_option('audio_player_textcolor')));
 115  
 116      $ap_color = str_replace("#", "0x", get_option('audio_player_bgcolor'));
 117      update_option('audio_player_bgcolor', $ap_color);
 118      add_option('audio_player_leftbgcolor', $ap_color, "Left background color", true);
 119  
 120      $ap_color = str_replace("#", "0x", get_option('audio_player_buttoncolor'));
 121      add_option('audio_player_rightbgcolor', $ap_color, "Right background color", true);
 122      delete_option('audio_player_buttoncolor');
 123  
 124      $ap_color = str_replace("#", "0x", get_option('audio_player_buttonhovercolor'));
 125      add_option('audio_player_rightbghovercolor', $ap_color, "Right background hover color", true);
 126      delete_option('audio_player_buttonhovercolor');
 127  
 128      $ap_color = str_replace("#", "0x", get_option('audio_player_pathcolor'));
 129      add_option('audio_player_trackcolor', $ap_color, "Progress track color", true);
 130      delete_option('audio_player_pathcolor');
 131  
 132      $ap_color = str_replace("#", "0x", get_option('audio_player_barcolor'));
 133      add_option('audio_player_loadercolor', $ap_color, "Loader bar color", true);
 134      add_option('audio_player_bordercolor', $ap_color, "Border color", true);
 135      delete_option('audio_player_barcolor');
 136  
 137      add_option('audio_player_slidercolor', '0x666666', "Progress slider color", true);
 138  } else {
 139      // Default color options

 140      add_option('audio_player_bgcolor', '0xf8f8f8', "Background color", true);
 141      add_option('audio_player_textcolor', '0x666666', "Text color", true);
 142      add_option('audio_player_leftbgcolor', '0xeeeeee', "Left background color", true);
 143      add_option('audio_player_lefticoncolor', '0x666666', "Left icon color", true);
 144      add_option('audio_player_rightbgcolor', '0xcccccc', "Right background color", true);
 145      add_option('audio_player_rightbghovercolor', '0x999999', "Right background hover color", true);
 146      add_option('audio_player_righticoncolor', '0x666666', "Right icon color", true);
 147      add_option('audio_player_righticonhovercolor', '0xffffff', "Right icon hover color", true);
 148      add_option('audio_player_slidercolor', '0x666666', "Progress slider color", true);
 149      add_option('audio_player_trackcolor', '0xFFFFFF', "Progress track color", true);
 150      add_option('audio_player_loadercolor', '0x9FFFB8', "Loader bar color", true);
 151      add_option('audio_player_bordercolor', '0x666666', "Border color", true);
 152  }
 153  
 154  add_option('audio_player_transparentpagebgcolor', 'true', "Transparent player background", true);
 155  add_option('audio_player_pagebgcolor', '#FFFFFF', "Page background color", true);
 156  
 157  // Global variables

 158  $ap_version = "1.2.2";
 159  $ap_updateURL = "http://www.1pixelout.net/download/audio-player-update.txt";
 160  $ap_docURL = "http://www.1pixelout.net/code/audio-player-wordpress-plugin/";
 161  $ap_colorkeys = array("bg","leftbg","lefticon","rightbg","rightbghover","righticon","righticonhover","text","slider","track","border","loader");
 162  $ap_playerURL = get_settings('siteurl') . '/wp-content/plugins/audio-player/player.swf';
 163  $ap_audioURL = get_settings('siteurl') . get_option("audio_player_web_path");
 164  // Initialise playerID (each instance gets unique ID)

 165  $ap_playerID = 0;
 166  // Convert behaviour options to array

 167  $ap_behaviour = explode( ",", get_option("audio_player_behaviour") );
 168  
 169  $ap_options = array();
 170  
 171  // Builds global array of color options (we need a function because the options update code further down needs it again)

 172  function ap_set_options() {
 173      global $ap_options, $ap_colorkeys;
 174      foreach( $ap_colorkeys as $value ) $ap_options[$value] = get_option("audio_player_" . $value . "color");
 175  }
 176  
 177  ap_set_options();
 178  
 179  // Declare instances global variable

 180  $ap_instances = array();
 181  
 182  // Filter function (inserts player instances according to behaviour option)

 183  function ap_insert_player_widgets($content = '') {
 184      global $ap_behaviour, $ap_instances;
 185      
 186      // Reset instance array

 187      $ap_instances = array();
 188  
 189      // Replace mp3 links

 190      if( in_array( "links", $ap_behaviour ) ) $content = preg_replace_callback( "/<a ([^=]+=\"[^\"]+\" )*href=\"([^\"]+\.mp3)\"( [^=]+=\"[^\"]+\")*>[^<]+<\/a>/i", "ap_replace", $content );
 191      
 192      // Replace [audio syntax]

 193      if( in_array( "default", $ap_behaviour ) ) $content = preg_replace_callback( "/\[audio:(([^]]+))]/i", "ap_replace", $content );
 194  
 195      // Enclosure integration

 196      if( in_array( "enclosure", $ap_behaviour ) ) {
 197          $enclosure = get_enclosed($post_id);
 198  
 199          // Insert prefix and postfix clips if set

 200          $prefixAudio = get_option( "audio_player_prefixaudio" );
 201          if( $prefixAudio != "" ) $prefixAudio .= ",";
 202          $postfixAudio = get_option( "audio_player_postfixaudio" );
 203          if( $postfixAudio != "" ) $postfixAudio = "," . $postfixAudio;
 204  
 205          if( count($enclosure) > 0 ) {
 206              for($i = 0;$i < count($enclosure);$i++) {
 207                  // Make sure the enclosure is an mp3 file and it hasn't been inserted into the post yet

 208                  if( preg_match( "/.*\.mp3$/", $enclosure[$i] ) == 1 && !in_array( $enclosure[$i], $ap_instances ) ) {
 209                      $content .= "\n\n" . ap_getplayer( $prefixAudio . $enclosure[$i] . $postfixAudio );
 210                  }
 211              }
 212          }
 213      }
 214      
 215      return $content;
 216  }
 217  
 218  // Callback function for preg_replace_callback

 219  function ap_replace($matches) {
 220      global $ap_audioURL, $ap_instances;
 221      // Split options

 222      $data = preg_split("/[\|]/", $matches[2]);
 223      $files = array();
 224      
 225      if(!is_feed()) {
 226          // Insert prefix clip if set

 227          $prefixAudio = get_option( "audio_player_prefixaudio" );
 228          if( $prefixAudio != "" ) array_push( $files, $prefixAudio );
 229      }
 230  
 231      // If file doesn't start with http://, assume it is in the default audio folder

 232      foreach( explode( ",", $data[0] ) as $afile ) {
 233          if(strpos($afile, "http://") !== 0) $afile = $ap_audioURL . "/" . $afile;
 234          array_push( $files, $afile );
 235  
 236          // Add source file to instances already added to the post

 237          array_push( $ap_instances, $afile );
 238      }
 239  
 240      if(!is_feed()) {
 241          // Insert postfix clip if set

 242          $postfixAudio = get_option( "audio_player_postfixaudio" );
 243          if( $postfixAudio != "" ) array_push( $files, $postfixAudio );
 244      }
 245  
 246      $file = implode( ",", $files );
 247      
 248      // Build runtime options array

 249      $options = array();
 250      for($i=1;$i<count($data);$i++) {
 251          $pair = explode("=", $data[$i]);
 252          $options[$pair[0]] = $pair[1]; 
 253      }
 254      
 255      // Return player instance code

 256      return ap_getplayer( $file, $options );
 257  }
 258  
 259  // Generic player instance function (returns object tag code)

 260  function ap_getplayer($source, $options = array()) {
 261      global $ap_playerURL, $ap_options, $ap_playerID;
 262      
 263      // Get next player ID

 264      $ap_playerID++;
 265      // Add source to options

 266      $options["soundFile"] = $source;
 267      // Merge runtime options to default colour options (runtime options overwrite default options)

 268      $options = array_merge( $ap_options, $options );
 269  
 270      // Build FlashVars string (url encode everything)

 271      $flashVars = "playerID=" . $ap_playerID;
 272      foreach($options as $key => $value) $flashVars .= '&amp;' . $key . '=' . rawurlencode($value);
 273      
 274      if(is_feed()) {
 275          // We are in a feed so use RSS alternate content option

 276          switch( get_option( "audio_player_rssalternate" ) ) {
 277              case "download":
 278                  // Get filenames from path and output a link for each file in the sequence

 279                  $files = explode(",", $source);
 280                  $links = "";
 281                  for($i=0;$i<count($files);$i++) {
 282                      $fileparts = explode("/", $files[$i]);
 283                      $fileName = $fileparts[count($fileparts)-1];
 284                      $links .= '<a href="' . $files[$i] . '">Download audio file (' . $fileName . ')</a><br />';
 285                  }
 286                  return $links;
 287                  break;
 288              case "nothing":
 289                  return "";
 290                  break;
 291              case "custom":
 292                  return get_option( "audio_player_rsscustomalternate" );
 293                  break;
 294          }
 295      }
 296      // Not in a feed so return formatted object tag

 297      else {
 298          if( get_option("audio_player_transparentpagebg") ) $bgparam = '<param name="wmode" value="transparent" />';
 299          else $bgparam = '<param name="bgcolor" value="' . get_option("audio_player_pagebgcolor") . '" />';
 300          return '<object type="application/x-shockwave-flash" data="' . $ap_playerURL . '" width="290" height="24" id="audioplayer' . $ap_playerID . '"><param name="movie" value="' . $ap_playerURL . '" /><param name="FlashVars" value="' . $flashVars . '" /><param name="quality" value="high" /><param name="menu" value="false" />' . $bgparam . '</object>';
 301      }
 302  }
 303  
 304  // Add filter hook

 305  add_filter('the_content', 'ap_insert_player_widgets');
 306  
 307  // Helper function for displaying a system message

 308  function ap_showMessage( $message ) {
 309      echo '<div id="message" class="updated fade"><p><strong>' . $message . '</strong></p></div>';
 310  }
 311  
 312  // Option panel functionality

 313  function ap_options_subpanel() {
 314      global $ap_colorkeys, $ap_options, $ap_version, $ap_updateURL, $ap_docURL;
 315      
 316      if( $_POST['ap_updateCheck'] ) {
 317          // Update check. Gets the file at the update URL , reads and compares it with the current version

 318          $ap_contents = "";
 319          if( function_exists( "curl_init" ) ) {
 320              $source = curl_init();
 321              curl_setopt( $source, CURLOPT_URL, $ap_updateURL );
 322              curl_setopt( $source, CURLOPT_CONNECTTIMEOUT, 10 );
 323              curl_setopt( $source, CURLOPT_FAILONERROR, 1 );
 324              curl_setopt( $source, CURLOPT_RETURNTRANSFER, 1 );
 325              $ap_contents = curl_exec( $source );
 326              if( curl_errno( $source ) > 0 ) {
 327                  $ap_contents = "error";
 328              } else curl_close( $source );
 329          } else if( ini_get( "allow_url_fopen" ) ) {
 330              if( $source = @fopen( $ap_updateURL, "r" ) ) {
 331                  while( !feof( $source ) ) {
 332                      $ap_contents .= fread( $source, 8192 );
 333                  }
 334                  fclose($source);
 335              } else $ap_contents = "error";
 336          }
 337  
 338          // Errors

 339          if( $ap_contents == "error" ) {
 340              ap_showMessage( 'Failed to contact update server. Please visit <a href="' . $ap_docURL . '">1 Pixel Out</a> to check for updates.' );
 341          } else if( $ap_contents == "" ) {
 342              ap_showMessage( 'Some PHP functionality used by the Upgrade wizard are disabled on your server. Please visit <a href="' . $ap_docURL . '">1 Pixel Out</a> to check for updates.' );
 343          } else if( $ap_contents != $ap_version ) {
 344              // A new version is available so announce this with a message

 345              // Check that the zip extension is loaded and the PHP engine is 4.2.0 at least (otherwise the upgrade wizard won't work)

 346              if( version_compare( phpversion(), "4.2.0" ) == 1 && in_array( "zip", get_loaded_extensions() ) ) {
 347                  ap_showMessage( 'Version ' . $ap_contents . ' of the Audio Player plugin is now available! <a href="javascript:ap_startUpgradeWizard()">Upgrade now</a>.' );
 348              } else {
 349                  ap_showMessage( 'Version ' . $ap_contents . ' of the Audio Player plugin is now available! Download it <a href="' . $ap_docURL . '">here</a>.' );
 350              }
 351          } else {
 352              // Plugin is up-to-date

 353              ap_showMessage( "Your copy of Audio Player plugin is up-to-date." );
 354          }
 355      } else if( $_POST['Submit'] ) {
 356          // Update plugin options

 357      
 358          // Set audio web path

 359          if( substr( $_POST['ap_audiowebpath'], -1 ) == "/" ) $_POST['ap_audiowebpath'] = substr( $_POST['ap_audiowebpath'], 0, strlen( $_POST['ap_audiowebpath'] ) - 1 );
 360          update_option('audio_player_web_path', $_POST['ap_audiowebpath']);
 361  
 362          // Update behaviour and rss alternate content options

 363          update_option('audio_player_behaviour', implode(",", $_POST['ap_behaviour']));
 364          update_option('audio_player_rssalternate', $_POST['ap_rssalternate']);
 365          update_option('audio_player_rsscustomalternate', $_POST['ap_rsscustomalternate']);
 366          update_option('audio_player_prefixaudio', $_POST['ap_audioprefixwebpath']);
 367          update_option('audio_player_postfixaudio', $_POST['ap_audiopostfixwebpath']);
 368  
 369          // Update colour options

 370          foreach( $ap_colorkeys as $colorkey ) {
 371              // Ignore missing or invalid color values

 372              if( isset( $_POST["ap_" . $colorkey . "color"] ) && preg_match( "/#[0-9A-Fa-f]{6}/", $_POST["ap_" . $colorkey . "color"] ) == 1 ) {
 373                  update_option( "audio_player_" . $colorkey . "color", str_replace( "#", "0x", $_POST["ap_" . $colorkey . "color"] ) );
 374              }
 375          }
 376  
 377          if(isset( $_POST["ap_pagebgcolor"] )) update_option('audio_player_pagebgcolor', $_POST['ap_pagebgcolor']);
 378          update_option('audio_player_transparentpagebg', isset( $_POST["ap_transparentpagebg"] ));
 379          
 380          // Need to do this again for the player preview on the options panel

 381          ap_set_options();
 382          
 383          // Print confirmation message

 384          ap_showMessage( "Options updated." );
 385      }
 386  
 387      $ap_behaviour = explode(",", get_option("audio_player_behaviour"));
 388      $ap_rssalternate = get_option('audio_player_rssalternate');
 389      
 390      // Preview player options

 391      $ap_demo_options = array();
 392      $ap_demo_options["autostart"] = "yes";
 393      $ap_demo_options["loop"] = "yes";
 394      
 395      // Include options panel

 396      include( "audio-player/options-panel.php" );
 397  }
 398  
 399  // Add options page to admin menu

 400  function ap_post_add_options() {
 401      add_options_page('Audio player options', 'Audio player', 8, basename(__FILE__), 'ap_options_subpanel');
 402  }
 403  add_action('admin_menu', 'ap_post_add_options');
 404  
 405  // Output script tag in WP front-end head

 406  function ap_wp_head() {
 407      global $ap_playerID;
 408      echo '<script language="javascript1.4" type="text/javascript" src="' . get_settings("siteurl") . '/wp-content/plugins/audio-player/audio-player.js"></script>';
 409      echo "\n";
 410  }
 411  add_action('wp_head', 'ap_wp_head');
 412  
 413  // Output script tag in WP admin head

 414  function ap_wp_admin_head() {
 415      global $ap_playerID;
 416      echo '<script language="javascript" type="text/javascript" src="' . get_settings("siteurl") . '/wp-content/plugins/audio-player/audio-player-admin.js"></script>';
 417      echo "\n";
 418      echo '<script language="javascript" type="text/javascript">';
 419      echo "\n";
 420      echo 'var ap_updateURL = "' . get_option("siteurl") . '/wp-content/plugins/audio-player/upgrade/";';
 421      echo "\n";
 422      echo '</script>';
 423      echo "\n";
 424  }
 425  add_action('admin_head', 'ap_wp_admin_head');
 426  
 427  ?>


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7