Exemple #1
0
 /**
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public static function get_allowed_bitrate($song)
 {
     $max_bitrate = AmpConfig::get('max_bit_rate');
     $min_bitrate = AmpConfig::get('min_bit_rate');
     // FIXME: This should be configurable for each output type
     $user_sample_rate = AmpConfig::get('sample_rate');
     // If the user's crazy, that's no skin off our back
     if ($user_sample_rate < $min_bitrate) {
         $min_bitrate = $user_sample_rate;
     }
     // Are there site-wide constraints? (Dynamic downsampling.)
     if ($max_bitrate > 1) {
         $sql = 'SELECT COUNT(*) FROM `now_playing` ' . 'WHERE `user` IN ' . '(SELECT DISTINCT `user_preference`.`user` ' . 'FROM `preference` JOIN `user_preference` ' . 'ON `preference`.`id` = ' . '`user_preference`.`preference` ' . "WHERE `preference`.`name` = 'play_type' " . "AND `user_preference`.`value` = 'downsample')";
         $db_results = Dba::read($sql);
         $results = Dba::fetch_row($db_results);
         $active_streams = intval($results[0]) ?: 0;
         debug_event('stream', 'Active transcoding streams: ' . $active_streams, 5);
         // We count as one for the algorithm
         // FIXME: Should this reflect the actual bit rates?
         $active_streams++;
         $sample_rate = floor($max_bitrate / $active_streams);
         // Exit if this would be insane
         if ($sample_rate < ($min_bitrate ?: 8)) {
             debug_event('stream', 'Max transcode bandwidth already allocated. Active streams: ' . $active_streams, 2);
             header('HTTP/1.1 503 Service Temporarily Unavailable');
             exit;
         }
         // Never go over the user's sample rate
         if ($sample_rate > $user_sample_rate) {
             $sample_rate = $user_sample_rate;
         }
     } else {
         $sample_rate = $user_sample_rate;
     }
     return $sample_rate;
 }
Exemple #2
0
 /**
  * gc
  * This cleans up art that no longer has a corresponding object
  */
 public static function gc($object_type = null, $object_id = null)
 {
     $types = array('album', 'artist', 'tvshow', 'tvshow_season', 'video', 'user');
     if ($object_type != null) {
         if (in_array($object_type, $types)) {
             if (AmpConfig::get('album_art_store_disk')) {
                 self::delete_from_dir($object_type, $object_id);
             }
             $sql = "DELETE FROM `image` WHERE `object_type` = ? AND `object_id` = ?";
             Dba::write($sql, array($object_type, $object_id));
         } else {
             debug_event('art', 'Garbage collect on type `' . $object_type . '` is not supported.', 1);
         }
     } else {
         // iterate over our types and delete the images
         foreach ($types as $type) {
             if (AmpConfig::get('album_art_store_disk')) {
                 $sql = "SELECT `image`.`object_id`, `image`.`object_type` FROM `image` LEFT JOIN `" . $type . "` ON `" . $type . "`.`id`=" . "`image`.`object_id` WHERE `object_type`='" . $type . "' AND `" . $type . "`.`id` IS NULL";
                 $db_results = Dba::read($sql);
                 while ($row = Dba::fetch_row($db_results)) {
                     self::delete_from_dir($row[1], $row[0]);
                 }
             }
             $sql = "DELETE FROM `image` USING `image` LEFT JOIN `" . $type . "` ON `" . $type . "`.`id`=" . "`image`.`object_id` WHERE `object_type`='" . $type . "' AND `" . $type . "`.`id` IS NULL";
             Dba::write($sql);
         }
         // foreach
     }
 }
Exemple #3
0
 /**
  * optimize_tables
  *
  * This runs an optimize on the tables and updates the stats to improve
  * join speed.
  * This can be slow, but is a good idea to do from time to time. We do
  * it in case the dba isn't doing it... which we're going to assume they
  * aren't.
  */
 public static function optimize_tables()
 {
     $sql = "SHOW TABLES";
     $db_results = Dba::read($sql);
     while ($row = Dba::fetch_row($db_results)) {
         $sql = "OPTIMIZE TABLE `" . $row[0] . "`";
         Dba::write($sql);
         $sql = "ANALYZE TABLE `" . $row[0] . "`";
         Dba::write($sql);
     }
 }
 /**
  * count_items
  * This returns a count of the total number of tracks that are in this
  * tmp playlist
  */
 public function count_items()
 {
     $id = Dba::escape($this->id);
     $sql = "SELECT COUNT(`id`) FROM `tmp_playlist_data` WHERE " . "`tmp_playlist`='{$id}'";
     $db_results = Dba::read($sql);
     $results = Dba::fetch_row($db_results);
     return $results['0'];
 }
Exemple #5
0
 /**
  * check_local_mp3
  * Checks the song to see if it's there already returns true if found, false if not
  */
 public function check_local_mp3($full_file, $gather_type = '')
 {
     $file_date = filemtime($full_file);
     if ($file_date < $this->last_add) {
         debug_event('Check', 'Skipping ' . $full_file . ' File modify time before last add run', '3');
         return true;
     }
     $sql = "SELECT `id` FROM `song` WHERE `file` = ?";
     $db_results = Dba::read($sql, array($full_file));
     //If it's found then return true
     if (Dba::fetch_row($db_results)) {
         return true;
     }
     return false;
 }
Exemple #6
0
 /**
  * check_network
  *
  * This takes a type, ip, user, level and key and then returns whether they
  * are allowed. The IP is passed as a dotted quad.
  */
 public static function check_network($type, $user, $level, $ip = null)
 {
     if (!AmpConfig::get('access_control')) {
         switch ($type) {
             case 'interface':
             case 'stream':
                 return true;
             default:
                 return false;
         }
     }
     // Clean incoming variables
     $ip = $ip ?: $_SERVER['REMOTE_ADDR'];
     $ip = inet_pton($ip);
     switch ($type) {
         case 'init-api':
             if ($user) {
                 $user = User::get_from_username($user);
                 $user = $user->id;
             }
         case 'api':
             $type = 'rpc';
         case 'network':
         case 'interface':
         case 'stream':
             break;
         default:
             return false;
     }
     // end switch on type
     $sql = 'SELECT `id` FROM `access_list` ' . 'WHERE `start` <= ? AND `end` >= ? ' . 'AND `level` >= ? AND `type` = ?';
     $params = array($ip, $ip, $level, $type);
     if (strlen($user) && $user != '-1') {
         $sql .= " AND `user` IN(?, '-1')";
         $params[] = $user;
     } else {
         $sql .= " AND `user` = '-1'";
     }
     $db_results = Dba::read($sql, $params);
     if (Dba::fetch_row($db_results)) {
         // Yah they have access they can use the mojo
         return true;
     }
     return false;
 }
Exemple #7
0
 /**
  * count
  *
  * This returns the number of user accounts that exist.
  */
 public static function count()
 {
     $sql = 'SELECT COUNT(`id`) FROM `user`';
     $db_results = Dba::read($sql);
     $data = Dba::fetch_row($db_results);
     $results = array();
     $results['users'] = $data[0];
     $time = time();
     $last_seen = $time - 1200;
     $sql = 'SELECT COUNT(DISTINCT `session`.`username`) FROM `session` ' . 'INNER JOIN `user` ON `session`.`username` = `user`.`username` ' . 'WHERE `session`.`expire` > ? and `user`.`last_seen` > ?';
     $db_results = Dba::read($sql, array($time, $last_seen));
     $data = Dba::fetch_row($db_results);
     $results['connected'] = $data[0];
     return $results;
 }
Exemple #8
0
 /**
  * get_total_duration
  * Get the total duration of all songs.
  */
 public function get_total_duration()
 {
     $songs = self::get_songs();
     $idlist = '(' . implode(',', $songs) . ')';
     $sql = "SELECT SUM(`time`) FROM `song` WHERE `id` IN {$idlist}";
     $db_results = Dba::read($sql);
     $results = Dba::fetch_row($db_results);
     return $results['0'];
 }
Exemple #9
0
 /**
  * get_newest
  * This returns an array of the newest artists/albums/whatever
  * in this ampache instance
  */
 public static function get_newest($type, $count = '', $offset = '', $catalog = 0)
 {
     if (!$count) {
         $count = AmpConfig::get('popular_threshold');
     }
     if (!$offset) {
         $limit = $count;
     } else {
         $limit = $offset . ',' . $count;
     }
     $sql = self::get_newest_sql($type, $catalog);
     $sql .= "LIMIT {$limit}";
     $db_results = Dba::read($sql);
     $items = array();
     while ($row = Dba::fetch_row($db_results)) {
         $items[] = $row[0];
     }
     // end while results
     return $items;
 }
Exemple #10
0
 /**
  * count_songs
  *
  * This returns the current number of songs, albums, and artists
  * in this catalog.
  */
 public static function count_songs($id = null)
 {
     $where_sql = $id ? 'WHERE `catalog` = ?' : '';
     $params = $id ? array($id) : null;
     $sql = 'SELECT COUNT(`id`), SUM(`time`), SUM(`size`) FROM `song` ' . $where_sql;
     $db_results = Dba::read($sql, $params);
     $data = Dba::fetch_row($db_results);
     $songs = $data[0];
     $time = $data[1];
     $size = $data[2];
     $sql = 'SELECT COUNT(DISTINCT(`album`)) FROM `song` ' . $where_sql;
     $db_results = Dba::read($sql, $params);
     $data = Dba::fetch_row($db_results);
     $albums = $data[0];
     $sql = 'SELECT COUNT(DISTINCT(`artist`)) FROM `song` ' . $where_sql;
     $db_results = Dba::read($sql, $params);
     $data = Dba::fetch_row($db_results);
     $artists = $data[0];
     $results = array();
     $results['songs'] = $songs;
     $results['albums'] = $albums;
     $results['artists'] = $artists;
     $results['size'] = $size;
     $results['time'] = $time;
     return $results;
 }
Exemple #11
0
 /**
  *
  * @param int|null $catalog_id
  * @param string $type
  * @return int
  */
 public static function get_videos_count($catalog_id = null, $type = '')
 {
     $sql = "SELECT COUNT(`video`.`id`) AS `video_cnt` FROM `video` ";
     if (!empty($type)) {
         $sql .= "JOIN `" . $type . "` ON `" . $type . "`.`id` = `video`.`id` ";
     }
     if ($catalog_id) {
         $sql .= "WHERE `video`.`catalog` = `" . intval($catalog_id) . "`";
     }
     $db_results = Dba::read($sql);
     $video_cnt = 0;
     if ($row = Dba::fetch_row($db_results)) {
         $video_cnt = $row[0];
     }
     return $video_cnt;
 }
Exemple #12
0
 /**
  * get_random_songs
  * gets a random number, and a random assortment of songs from this album
  */
 public function get_random_songs()
 {
     $sql = "SELECT `song`.`id` FROM `song` ";
     if (AmpConfig::get('catalog_disable')) {
         $sql .= "LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` ";
     }
     $sql .= "WHERE `song`.`album` = ? ";
     if (AmpConfig::get('catalog_disable')) {
         $sql .= "AND `catalog`.`enabled` = '1' ";
     }
     $sql .= "ORDER BY RAND()";
     $db_results = Dba::read($sql, array($this->id));
     $results = array();
     while ($r = Dba::fetch_row($db_results)) {
         $results[] = $r['0'];
     }
     return $results;
 }
Exemple #13
0
 public function add_episodes($episodes, $afterdate = 0, $gather = false)
 {
     foreach ($episodes as $episode) {
         $this->add_episode($episode, $afterdate);
     }
     // Select episodes to download
     $dlnb = AmpConfig::get('podcast_new_download');
     if ($dlnb != 0) {
         $sql = "SELECT `podcast_episode`.`id` FROM `podcast_episode` INNER JOIN `podcast` ON `podcast`.`id` = `podcast_episode`.`podcast` " . "WHERE `podcast`.`id` = ? AND `podcast_episode`.`addition_time` > `podcast`.`lastsync` " . "ORDER BY `podcast_episode`.`pubdate` DESC";
         if ($dlnb != -1) {
             $sql .= " LIMIT " . $dlnb;
         }
         $db_results = Dba::read($sql, array($this->id));
         while ($row = Dba::fetch_row($db_results)) {
             $episode = new Podcast_Episode($row[0]);
             $episode->change_state('pending');
             if ($gather) {
                 $episode->gather();
             }
         }
     }
     // Remove items outside limit
     $keepnb = AmpConfig::get('podcast_keep');
     if ($keepnb > 0) {
         $sql = "SELECT `podcast_episode`.`id` FROM `podcast_episode` WHERE `podcast_episode`.`podcast` = ? " . "ORDER BY `podcast_episode`.`pubdate` DESC LIMIT " . $keepnb . ",18446744073709551615";
         $db_results = Dba::read($sql, array($this->id));
         while ($row = Dba::fetch_row($db_results)) {
             $episode = new Podcast_Episode($row[0]);
             $episode->remove();
         }
     }
     $this->update_lastsync(time());
 }