Ampache PHP Cross Reference Groupware Applications

Source: /lib/ui.lib.php - 569 lines - 17990 bytes - Summary - Text - Print

Description: This contains functions that are generic, and display information things like a confirmation box, etc and so forth LICENSE: GNU General Public License, version 2 (GPLv2) Copyright 2001 - 2014 Ampache.org

   1  <?php
   2  /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
   3  /**
   4   *
   5   * This contains functions that are generic, and display information
   6   * things like a confirmation box, etc and so forth
   7   *
   8   *
   9   * LICENSE: GNU General Public License, version 2 (GPLv2)
  10   * Copyright 2001 - 2014 Ampache.org
  11   *
  12   * This program is free software; you can redistribute it and/or
  13   * modify it under the terms of the GNU General Public License v2
  14   * as published by the Free Software Foundation.
  15   *
  16   * This program is distributed in the hope that it will be useful,
  17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19   * GNU General Public License for more details.
  20   *
  21   * You should have received a copy of the GNU General Public License
  22   * along with this program; if not, write to the Free Software
  23   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24   *
  25   */
  26  
  27  /**
  28   * show_confirmation
  29   *
  30   * shows a confirmation of an action
  31   *
  32   * @param    string    $title    The Title of the message
  33   * @param    string    $text    The details of the message
  34   * @param    string    $next_url    Where to go next
  35   * @param    integer    $cancel    T/F show a cancel button that uses return_referrer()
  36   * @return    void
  37   */
  38  function show_confirmation($title,$text,$next_url,$cancel=0,$form_name='confirmation')
  39  {
  40      if (substr_count($next_url,AmpConfig::get('web_path'))) {
  41          $path = $next_url;
  42      } else {
  43          $path = AmpConfig::get('web_path') . "/$next_url";
  44      }
  45  
  46      require AmpConfig::get('prefix') . '/templates/show_confirmation.inc.php';
  47  } // show_confirmation
  48  
  49  /**
  50   * return_referer
  51   * returns the script part of the referer address passed by the web browser
  52   * this is not %100 accurate. Also because this is not passed by us we need
  53   * to clean it up, take the filename then check for a /admin/ and dump the rest
  54   */
  55  function return_referer()
  56  {
  57      $referer = $_SERVER['HTTP_REFERER'];
  58      if (substr($referer, -1)=='/') {
  59          $file = 'index.php';
  60      } else {
  61          $file = basename($referer);
  62          /* Strip off the filename */
  63          $referer = substr($referer,0,strlen($referer)-strlen($file));
  64      }
  65  
  66      if (substr($referer,strlen($referer)-6,6) == 'admin/') {
  67          $file = 'admin/' . $file;
  68      }
  69  
  70      return $file;
  71  
  72  } // return_referer
  73  
  74  /**
  75   * get_location
  76   * This function gets the information about a person's current location.
  77   * This is used for A) sidebar highlighting & submenu showing and B) titlebar
  78   * information. It returns an array of information about what they are currently
  79   * doing.
  80   * Possible array elements
  81   * ['title']    Text name for the page
  82   * ['page']    actual page name
  83   * ['section']    name of the section we are in, admin, browse etc (submenu)
  84   */
  85  function get_location()
  86  {
  87      $location = array();
  88  
  89      if (strlen($_SERVER['PHP_SELF'])) {
  90          $source = $_SERVER['PHP_SELF'];
  91      } else {
  92          $source = $_SERVER['REQUEST_URI'];
  93      }
  94  
  95      /* Sanatize the $_SERVER['PHP_SELF'] variable */
  96      $source = str_replace(AmpConfig::get('raw_web_path'), "", $source);
  97      $location['page'] = preg_replace("/^\/(.+\.php)\/?.*/","$1",$source);
  98  
  99      switch ($location['page']) {
 100          case 'index.php':
 101              $location['title']     = T_('Home');
 102              break;
 103          case 'upload.php':
 104              $location['title']     = T_('Upload');
 105              break;
 106          case 'localplay.php':
 107              $location['title']     = T_('Local Play');
 108              break;
 109          case 'randomplay.php':
 110              $location['title']     = T_('Random Play');
 111              break;
 112          case 'playlist.php':
 113              $location['title']     = T_('Playlist');
 114              break;
 115          case 'search.php':
 116              $location['title']     = T_('Search');
 117              break;
 118          case 'preferences.php':
 119              $location['title']     = T_('Preferences');
 120              break;
 121          case 'admin/index.php':
 122              $location['title']     = T_('Admin-Catalog');
 123              $location['section']    = 'admin';
 124              break;
 125          case 'admin/catalog.php':
 126              $location['title']     = T_('Admin-Catalog');
 127              $location['section']    = 'admin';
 128              break;
 129          case 'admin/users.php':
 130              $location['title']    = T_('Admin-User Management');
 131              $location['section']    = 'admin';
 132              break;
 133          case 'admin/mail.php':
 134              $location['title']    = T_('Admin-Mail Users');
 135              $location['section']    = 'admin';
 136              break;
 137          case 'admin/access.php':
 138              $location['title']    = T_('Admin-Manage Access Lists');
 139              $location['section']    = 'admin';
 140              break;
 141          case 'admin/preferences.php':
 142              $location['title']    = T_('Admin-Site Preferences');
 143              $location['section']    = 'admin';
 144              break;
 145          case 'admin/modules.php':
 146              $location['title']    = T_('Admin-Manage Modules');
 147              $location['section']    = 'admin';
 148              break;
 149          case 'browse.php':
 150              $location['title']    = T_('Browse Music');
 151              $location['section']    = 'browse';
 152              break;
 153          case 'albums.php':
 154              $location['title']    = T_('Albums');
 155              $location['section']    = 'browse';
 156              break;
 157          case 'artists.php':
 158              $location['title']    = T_('Artists');
 159              $location['section']    = 'browse';
 160              break;
 161          case 'stats.php':
 162              $location['title']    = T_('Statistics');
 163              break;
 164          default:
 165              $location['title'] = '';
 166              break;
 167      } // switch on raw page location
 168  
 169      return $location;
 170  
 171  } // get_location
 172  
 173  /**
 174   * show_preference_box
 175   * This shows the preference box for the preferences pages.
 176   */
 177  function show_preference_box($preferences)
 178  {
 179      require AmpConfig::get('prefix') . '/templates/show_preference_box.inc.php';
 180  
 181  } // show_preference_box
 182  
 183  /**
 184   * show_album_select
 185   * This displays a select of every album that we've got in Ampache (which can be
 186   * hella long). It's used by the Edit page and takes a $name and a $album_id
 187   */
 188  function show_album_select($name='album',$album_id=0,$allow_add=0,$song_id=0)
 189  {
 190      static $album_id_cnt = 0;
 191  
 192      // Generate key to use for HTML element ID
 193      if ($song_id) {
 194          $key = "album_select_" . $song_id;
 195      } else {
 196          $key = "album_select_c" . ++$album_id_cnt;
 197      }
 198  
 199      // Added ID field so we can easily observe this element
 200      echo "<select name=\"$name\" id=\"$key\">\n";
 201  
 202      $sql = "SELECT `id`, `name`, `prefix`, `disk` FROM `album` ORDER BY `name`";
 203      $db_results = Dba::read($sql);
 204  
 205      while ($r = Dba::fetch_assoc($db_results)) {
 206          $selected = '';
 207          $album_name = trim($r['prefix'] . " " . $r['name']);
 208          if ($r['disk'] >= 1) {
 209              $album_name .= ' [Disk ' . $r['disk'] . ']';
 210          }
 211          if ($r['id'] == $album_id) {
 212              $selected = "selected=\"selected\"";
 213          }
 214  
 215          echo "\t<option value=\"" . $r['id'] . "\" $selected>" . scrub_out($album_name) . "</option>\n";
 216  
 217      } // end while
 218  
 219      if ($allow_add) {
 220          // Append additional option to the end with value=-1
 221          echo "\t<option value=\"-1\">" . T_('Add New') . "...</option>\n";
 222      }
 223  
 224      echo "</select>\n";
 225  
 226  } // show_album_select
 227  
 228  /**
 229   * show_artist_select
 230   * This is the same as show_album_select except it's *gasp* for artists! How
 231   * inventive!
 232   */
 233  function show_artist_select($name='artist', $artist_id=0, $allow_add=0, $song_id=0)
 234  {
 235      static $artist_id_cnt = 0;
 236      // Generate key to use for HTML element ID
 237      if ($song_id) {
 238          $key = "artist_select_" . $song_id;
 239      } else {
 240          $key = "artist_select_c" . ++$artist_id_cnt;
 241      }
 242  
 243      echo "<select name=\"$name\" id=\"$key\">\n";
 244  
 245      $sql = "SELECT `id`, `name`, `prefix` FROM `artist` ORDER BY `name`";
 246      $db_results = Dba::read($sql);
 247  
 248      while ($r = Dba::fetch_assoc($db_results)) {
 249          $selected = '';
 250          $artist_name = trim($r['prefix'] . " " . $r['name']);
 251          if ($r['id'] == $artist_id) {
 252              $selected = "selected=\"selected\"";
 253          }
 254  
 255          echo "\t<option value=\"" . $r['id'] . "\" $selected>" . scrub_out($artist_name) . "</option>\n";
 256  
 257      } // end while
 258  
 259      if ($allow_add) {
 260          // Append additional option to the end with value=-1
 261          echo "\t<option value=\"-1\">Add New...</option>\n";
 262      }
 263  
 264      echo "</select>\n";
 265  
 266  } // show_artist_select
 267  
 268  /**
 269   * show_catalog_select
 270   * Yet another one of these buggers. this shows a drop down of all of your
 271   * catalogs.
 272   */
 273  function show_catalog_select($name='catalog',$catalog_id=0,$style='')
 274  {
 275      echo "<select name=\"$name\" style=\"$style\">\n";
 276  
 277      $sql = "SELECT `id`, `name` FROM `catalog` ORDER BY `name`";
 278      $db_results = Dba::read($sql);
 279  
 280      while ($r = Dba::fetch_assoc($db_results)) {
 281          $selected = '';
 282          if ($r['id'] == $catalog_id) {
 283              $selected = "selected=\"selected\"";
 284          }
 285  
 286          echo "\t<option value=\"" . $r['id'] . "\" $selected>" . scrub_out($r['name']) . "</option>\n";
 287  
 288      } // end while
 289  
 290      echo "</select>\n";
 291  
 292  } // show_catalog_select
 293  
 294  /**
 295   * show_user_select
 296   * This one is for users! shows a select/option statement so you can pick a user
 297   * to blame
 298   */
 299  function show_user_select($name,$selected='',$style='')
 300  {
 301      echo "<select name=\"$name\" style=\"$style\">\n";
 302      echo "\t<option value=\"\">" . T_('All') . "</option>\n";
 303  
 304      $sql = "SELECT `id`,`username`,`fullname` FROM `user` ORDER BY `fullname`";
 305      $db_results = Dba::read($sql);
 306  
 307      while ($row = Dba::fetch_assoc($db_results)) {
 308          $select_txt = '';
 309          if ($row['id'] == $selected) {
 310              $select_txt = 'selected="selected"';
 311          }
 312          // If they don't have a full name, revert to the username
 313          $row['fullname'] = $row['fullname'] ? $row['fullname'] : $row['username'];
 314  
 315          echo "\t<option value=\"" . $row['id'] . "\" $select_txt>" . scrub_out($row['fullname']) . "</option>\n";
 316      } // end while users
 317  
 318      echo "</select>\n";
 319  
 320  } // show_user_select
 321  
 322  /**
 323   * show_playlist_select
 324   * This one is for playlists!
 325   */
 326  function show_playlist_select($name,$selected='',$style='')
 327  {
 328      echo "<select name=\"$name\" style=\"$style\">\n";
 329      echo "\t<option value=\"\">" . T_('None') . "</option>\n";
 330  
 331      $sql = "SELECT `id`,`name` FROM `playlist` ORDER BY `name`";
 332      $db_results = Dba::read($sql);
 333      $nb_items = Dba::num_rows($db_results);
 334      $index = 1;
 335      $already_selected = false;
 336  
 337      while ($row = Dba::fetch_assoc($db_results)) {
 338          $select_txt = '';
 339          if (!$already_selected && ($row['id'] == $selected || $index == $nb_items)) {
 340              $select_txt = 'selected="selected"';
 341              $already_selected = true;
 342          }
 343  
 344          echo "\t<option value=\"" . $row['id'] . "\" $select_txt>" . scrub_out($row['name']) . "</option>\n";
 345          ++$index;
 346      } // end while users
 347  
 348      echo "</select>\n";
 349  
 350  } // show_playlist_select
 351  
 352  function xoutput_headers()
 353  {
 354      $output = isset($_REQUEST['xoutput']) ? $_REQUEST['xoutput'] : 'xml';
 355      if ($output == 'xml') {
 356          header("Content-type: text/xml; charset=" . AmpConfig::get('site_charset'));
 357          header("Content-Disposition: attachment; filename=ajax.xml");
 358      } else {
 359          header("Content-type: application/json; charset=" . AmpConfig::get('site_charset'));
 360      }
 361  
 362      header("Expires: Tuesday, 27 Mar 1984 05:00:00 GMT");
 363      header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 364      header("Cache-Control: no-store, no-cache, must-revalidate");
 365      header("Pragma: no-cache");
 366  }
 367  
 368  function xoutput_from_array($array, $callback = false, $type = '')
 369  {
 370      $output = isset($_REQUEST['xoutput']) ? $_REQUEST['xoutput'] : 'xml';
 371      if ($output == 'xml') {
 372          return xml_from_array($array, $callback, $type);
 373      } elseif ($output == 'raw') {
 374          $outputnode = $_REQUEST['xoutputnode'];
 375          return $array[$outputnode];
 376      } else {
 377          return json_from_array($array, $callback, $type);
 378      }
 379  }
 380  
 381  // FIXME: This should probably go in XML_Data
 382  /**
 383   * xml_from_array
 384   * This takes a one dimensional array and creates a XML document from it. For
 385   * use primarily by the ajax mojo.
 386   */
 387  function xml_from_array($array, $callback = false, $type = '')
 388  {
 389      $string = '';
 390  
 391      // If we weren't passed an array then return
 392      if (!is_array($array)) { return $string; }
 393  
 394      // The type is used for the different XML docs we pass
 395      switch ($type) {
 396      case 'itunes':
 397          foreach ($array as $key=>$value) {
 398              if (is_array($value)) {
 399                  $value = xoutput_from_array($value,1,$type);
 400                  $string .= "\t\t<$key>\n$value\t\t</$key>\n";
 401              } else {
 402                  if ($key == "key") {
 403                  $string .= "\t\t<$key>$value</$key>\n";
 404                  } elseif (is_int($value)) {
 405                  $string .= "\t\t\t<key>$key</key><integer>$value</integer>\n";
 406                  } elseif ($key == "Date Added") {
 407                  $string .= "\t\t\t<key>$key</key><date>$value</date>\n";
 408                  } elseif (is_string($value)) {
 409                  /* We need to escape the value */
 410                  $string .= "\t\t\t<key>$key</key><string><![CDATA[$value]]></string>\n";
 411                  }
 412              }
 413  
 414          } // end foreach
 415  
 416          return $string;
 417      case 'xspf':
 418          foreach ($array as $key=>$value) {
 419              if (is_array($value)) {
 420                  $value = xoutput_from_array($value,1,$type);
 421                  $string .= "\t\t<$key>\n$value\t\t</$key>\n";
 422              } else {
 423                  if ($key == "key") {
 424                  $string .= "\t\t<$key>$value</$key>\n";
 425                  } elseif (is_numeric($value)) {
 426                  $string .= "\t\t\t<$key>$value</$key>\n";
 427                  } elseif (is_string($value)) {
 428                  /* We need to escape the value */
 429                  $string .= "\t\t\t<$key><![CDATA[$value]]></$key>\n";
 430                  }
 431              }
 432  
 433          } // end foreach
 434  
 435          return $string;
 436      default:
 437          foreach ($array as $key => $value) {
 438              // No numeric keys
 439              if (is_numeric($key)) {
 440                  $key = 'item';
 441              }
 442  
 443              if (is_array($value)) {
 444                  // Call ourself
 445                  $value = xoutput_from_array($value, true);
 446                  $string .= "\t<content div=\"$key\">$value</content>\n";
 447              } else {
 448                  /* We need to escape the value */
 449                  $string .= "\t<content div=\"$key\"><![CDATA[$value]]></content>\n";
 450              }
 451          // end foreach elements
 452          }
 453          if (!$callback) {
 454              $string = '<?xml version="1.0" encoding="utf-8" ?>' .
 455                  "\n<root>\n" . $string . "</root>\n";
 456          }
 457  
 458          return UI::clean_utf8($string);
 459      }
 460  } // xml_from_array
 461  
 462  function json_from_array($array, $callback = false, $type = '')
 463  {
 464      return json_encode($array);
 465  }
 466  
 467  /**
 468   * xml_get_header
 469   * This takes the type and returns the correct xml header
 470   */
 471  function xml_get_header($type)
 472  {
 473      switch ($type) {
 474      case 'itunes':
 475          $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
 476          "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"\n" .
 477          "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" .
 478          "<plist version=\"1.0\">\n" .
 479          "<dict>\n" .
 480          "       <key>Major Version</key><integer>1</integer>\n" .
 481          "       <key>Minor Version</key><integer>1</integer>\n" .
 482          "       <key>Application Version</key><string>7.0.2</string>\n" .
 483          "       <key>Features</key><integer>1</integer>\n" .
 484          "       <key>Show Content Ratings</key><true/>\n" .
 485          "       <key>Tracks</key>\n" .
 486          "       <dict>\n";
 487          return $header;
 488      case 'xspf':
 489          $header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" .
 490              "<!-- XML Generated by Ampache v." .  AmpConfig::get('version') . " -->";
 491              "<playlist version = \"1\" xmlns=\"http://xspf.org/ns/0/\">\n ".
 492              "<title>Ampache XSPF Playlist</title>\n" .
 493              "<creator>" . AmpConfig::get('site_title') . "</creator>\n" .
 494              "<annotation>" . AmpConfig::get('site_title') . "</annotation>\n" .
 495              "<info>". AmpConfig::get('web_path') ."</info>\n" .
 496              "<trackList>\n\n\n\n";
 497          return $header;
 498      default:
 499          $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 500          return $header;
 501      }
 502  } //xml_get_header
 503  
 504  /**
 505   * xml_get_footer
 506   * This takes the type and returns the correct xml footer
 507   */
 508  function xml_get_footer($type)
 509  {
 510      switch ($type) {
 511      case 'itunes':
 512          $footer = "      </dict>\n" .
 513          "</dict>\n" .
 514          "</plist>\n";
 515          return $footer;
 516      case 'xspf':
 517          $footer = "      </trackList>\n" .
 518                "</playlist>\n";
 519          return $footer;
 520      default:
 521  
 522      break;
 523      }
 524  } // xml_get_footer
 525  
 526  /**
 527   * toggle_visible
 528   * This is identical to the javascript command that it actually calls
 529   */
 530  function toggle_visible($element)
 531  {
 532      echo '<script type="text/javascript">';
 533      echo "toggleVisible('$element');";
 534      echo "</script>\n";
 535  
 536  } // toggle_visible
 537  
 538  /**
 539   * print_bool
 540   * This function takes a boolean value and then prints out a friendly text
 541   * message.
 542   */
 543  function print_bool($value)
 544  {
 545      if ($value) {
 546          $string = '<span class="item_on">' . T_('On') . '</span>';
 547      } else {
 548          $string = '<span class="item_off">' . T_('Off') . '</span>';
 549      }
 550  
 551      return $string;
 552  
 553  } // print_bool
 554  
 555  /**
 556   * show_now_playing
 557   * This shows the now playing templates and does some garbage collecion
 558   * this should really be somewhere else
 559   */
 560  function show_now_playing()
 561  {
 562      Session::gc();
 563      Stream::gc_now_playing();
 564  
 565      $web_path = AmpConfig::get('web_path');
 566      $results = Stream::get_now_playing();
 567      require_once AmpConfig::get('prefix') . '/templates/show_now_playing.inc.php';
 568  
 569  } // show_now_playing

title

Description

title

Description

title

Description

title

title

Body