public static function search_songs($params)
 {
     if (!self::checkAuth($params)) {
         echo "<root>\n\t<error code='400'>Invalid login</error>\n</root>";
         return;
     }
     $filter = $params['filter'];
     $artists = OC_MEDIA_COLLECTION::getArtists($filter);
     $albums = OC_MEDIA_COLLECTION::getAlbums(0, $filter);
     $songs = OC_MEDIA_COLLECTION::getSongs(0, 0, $filter);
     foreach ($artists as $artist) {
         $songs = array_merge($songs, OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
     }
     foreach ($albums as $album) {
         $songs = array_merge($songs, OC_MEDIA_COLLECTION::getSongs($album['album_artist'], $album['album_id']));
     }
     echo '<root>';
     foreach ($songs as $song) {
         self::printSong($song);
     }
     echo '</root>';
 }
 function search($query)
 {
     require_once 'lib_collection.php';
     $artists = OC_MEDIA_COLLECTION::getArtists($query);
     $albums = OC_MEDIA_COLLECTION::getAlbums(0, $query);
     $songs = OC_MEDIA_COLLECTION::getSongs(0, 0, $query);
     $results = array();
     foreach ($artists as $artist) {
         $results[] = new OC_Search_Result($artist['artist_name'], '', OC_Helper::linkTo('apps/media', 'index.php#artist=' . urlencode($artist['artist_name'])), 'Music');
     }
     foreach ($albums as $album) {
         $artist = OC_MEDIA_COLLECTION::getArtistName($album['album_artist']);
         $results[] = new OC_Search_Result($album['album_name'], 'by ' . $artist, OC_Helper::linkTo('apps/media', 'index.php#artist=' . urlencode($artist) . '&album=' . urlencode($album['album_name'])), 'Music');
     }
     foreach ($songs as $song) {
         $minutes = floor($song['song_length'] / 60);
         $secconds = $song['song_length'] % 60;
         $artist = OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
         $album = OC_MEDIA_COLLECTION::getalbumName($song['song_album']);
         $results[] = new OC_Search_Result($song['song_name'], "by {$artist}, in {$album} {$minutes}:{$secconds}", OC_Helper::linkTo('apps/media', 'index.php#artist=' . urlencode($artist) . '&album=' . urlencode($album) . '&song=' . urlencode($song['song_name'])), 'Music');
     }
     return $results;
 }
Ejemplo n.º 3
0
 /**
  * scan a file for music
  * @param string $path
  * @return boolean
  */
 public static function scanFile($path)
 {
     if (!self::isMusic($path)) {
         return;
     }
     if (!self::$getID3) {
         self::$getID3 = @new getID3();
         self::$getID3->encoding = 'UTF-8';
     }
     $file = OC_Filesystem::getLocalFile($path);
     $data = @self::$getID3->analyze($file);
     getid3_lib::CopyTagsToComments($data);
     if (!isset($data['comments'])) {
         OCP\Util::writeLog('media', "error reading id3 tags in '{$file}'", OCP\Util::WARN);
         return;
     }
     if (!isset($data['comments']['artist'])) {
         OCP\Util::writeLog('media', "error reading artist tag in '{$file}'", OCP\Util::WARN);
         $artist = 'unknown';
     } else {
         $artist = OCP\Util::sanitizeHTML(stripslashes($data['comments']['artist'][0]));
     }
     if (!isset($data['comments']['album'])) {
         OCP\Util::writeLog('media', "error reading album tag in '{$file}'", OCP\Util::WARN);
         $album = 'unknown';
     } else {
         $album = OCP\Util::sanitizeHTML(stripslashes($data['comments']['album'][0]));
     }
     if (!isset($data['comments']['title'])) {
         OCP\Util::writeLog('media', "error reading title tag in '{$file}'", OCP\Util::WARN);
         $title = 'unknown';
     } else {
         $title = OCP\Util::sanitizeHTML(stripslashes($data['comments']['title'][0]));
     }
     $size = $data['filesize'];
     if (isset($data['comments']['track'])) {
         $track = $data['comments']['track'][0];
     } else {
         if (isset($data['comments']['track_number'])) {
             $track = $data['comments']['track_number'][0];
             $track = explode('/', $track);
             $track = $track[0];
         } else {
             $track = 0;
         }
     }
     $length = isset($data['playtime_seconds']) ? round($data['playtime_seconds']) : 0;
     if (!isset(self::$artists[$artist])) {
         $artistId = OC_MEDIA_COLLECTION::addArtist($artist);
         self::$artists[$artist] = $artistId;
     } else {
         $artistId = self::$artists[$artist];
     }
     if (!isset(self::$albums[$artist . '/' . $album])) {
         $albumId = OC_MEDIA_COLLECTION::addAlbum($album, $artistId);
         self::$albums[$artist . '/' . $album] = $albumId;
     } else {
         $albumId = self::$albums[$artist . '/' . $album];
     }
     $songId = OC_MEDIA_COLLECTION::addSong($title, $path, $artistId, $albumId, $length, $track, $size);
     return !($title == 'unknown' && $artist == 'unknown' && $album == 'unknown') ? $songId : 0;
 }
             unset($_SESSION['collection']);
             $songId = OC_MEDIA_SCANNER::scanFile($arguments['path']);
         }
         if ($songId > 0) {
             $song = OC_MEDIA_COLLECTION::getSong($songId);
             $song['artist'] = OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
             $song['album'] = OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
             OC_JSON::encodedPrint($song);
         }
     }
     break;
 case 'play':
     ob_end_clean();
     $ftype = OC_Filesystem::getMimeType($arguments['path']);
     $songId = OC_MEDIA_COLLECTION::getSongByPath($arguments['path']);
     OC_MEDIA_COLLECTION::registerPlay($songId);
     header('Content-Type:' . $ftype);
     // calc an offset of 24 hours
     $offset = 3600 * 24;
     // calc the string in GMT not localtime and add the offset
     $expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
     //output the HTTP header
     header($expire);
     header('Cache-Control: max-age=3600, must-revalidate');
     header('Pragma: public');
     header('Accept-Ranges: bytes');
     header('Content-Length: ' . OC_Filesystem::filesize($arguments['path']));
     $gmt_mtime = gmdate('D, d M Y H:i:s', OC_Filesystem::filemtime($arguments['path'])) . ' GMT';
     header("Last-Modified: " . $gmt_mtime);
     OC_Filesystem::readfile($arguments['path']);
     exit;
 /**
  * Get the list of songs that (optionally) match an artist and/or album and/or search string
  * @param integer artist optional
  * @param integer album optional
  * @param string search optional
  * @return array the list of songs found
  */
 public static function getSongs($artist = 0, $album = 0, $search = '', $exact = false)
 {
     $uid = self::$uid;
     if (empty($uid)) {
         $uid = self::$uid = $_SESSION['user_id'];
     }
     $params = array($uid);
     if ($artist != 0) {
         $artistString = "AND song_artist = ?";
         array_push($params, $artist);
     } else {
         $artistString = '';
     }
     if ($album != 0) {
         $albumString = "AND song_album = ?";
         array_push($params, $album);
     } else {
         $albumString = '';
     }
     if ($search) {
         if (!$exact) {
             $search = "%{$search}%";
         }
         $searchString = "AND song_name LIKE ?";
         array_push($params, $search);
     } else {
         $searchString = '';
     }
     $query = OC_DB::prepare("SELECT * FROM *PREFIX*media_songs WHERE song_user=? {$artistString} {$albumString} {$searchString}");
     return $query->execute($params)->fetchAll();
 }
if (OC_User::checkPassword($user, $pass)) {
    OC_Util::setupFS($user);
    OC_MEDIA_COLLECTION::$uid = $user;
} else {
    exit;
}
if (isset($_POST['play']) and $_POST['play'] == 'true') {
    if (!isset($_POST['song'])) {
        exit;
    }
    $song = OC_MEDIA_COLLECTION::getSong($_POST['song']);
    $ftype = OC_Filesystem::getMimeType($song['song_path']);
    header('Content-Type:' . $ftype);
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . OC_Filesystem::filesize($song['song_path']));
    OC_Filesystem::readfile($song['song_path']);
}
$artist = isset($_POST['artist']) ? '%' . $_POST['artist'] . '%' : '';
$album = isset($_POST['album']) ? '%' . $_POST['album'] . '%' : '';
$song = isset($_POST['song']) ? $_POST['song'] : '';
$artist = OC_MEDIA_COLLECTION::getArtistId($artist);
$album = OC_MEDIA_COLLECTION::getAlbumId($album, $artist);
$songs = OC_MEDIA_COLLECTION::getSongs($artist, $album, $song);
$baseUrl = OC_Util::getServerURL() . OC_Helper::linkTo('media', 'tomahawk.php');
$results = array();
foreach ($songs as $song) {
    $results[] = (object) array('artist' => OC_MEDIA_COLLECTION::getArtistName($song['song_artist']), 'album' => OC_MEDIA_COLLECTION::getAlbumName($song['song_album']), 'track' => $song['song_name'], 'source' => 'ownCloud', 'mimetype' => OC_Filesystem::getMimeType($song['song_path']), 'extension' => substr($song['song_path'], strrpos($song['song_path'], '.')), 'url' => $baseUrl . '?play=true&song=' . $song['song_id'], 'bitrate' => round($song['song_id'] / $song['song_length'], 0), 'duration' => round($song['song_length'], 0), 'size' => $song['song_size'], 'score' => (double) 1.0);
}
OC_JSON::encodedPrint($results);
 /**
  * scan a file for music
  * @param string $path
  * @return boolean
  */
 public static function scanFile($path)
 {
     if (is_null(self::$useMp3Info)) {
         self::$useMp3Info = OC_Helper::canExecute("mp3info");
     }
     $file = OC_Filesystem::getLocalFile($path);
     if (substr($path, -3) == 'mp3' and self::$useMp3Info) {
         //use the command line tool id3info if possible
         $output = array();
         $size = filesize($file);
         exec('mp3info -p "%a\\n%l\\n%t\\n%n\\n%S" "' . $file . '"', $output);
         if (count($output) > 4) {
             $artist = $output[0];
             $album = $output[1];
             $title = $output[2];
             $track = $output[3];
             $length = $output[4];
         } else {
             return;
             //invalid mp3 file
         }
     } else {
         if (!self::isMusic($path)) {
             return;
         }
         if (!self::$getID3) {
             self::$getID3 = @new getID3();
         }
         $data = @self::$getID3->analyze($file);
         getid3_lib::CopyTagsToComments($data);
         if (!isset($data['comments'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading id3 tags in '{$file}'");
             }
             return;
         }
         if (!isset($data['comments']['artist'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading artist tag in '{$file}'");
             }
             $artist = 'unknown';
         } else {
             $artist = stripslashes($data['comments']['artist'][0]);
             $artist = utf8_encode($artist);
         }
         if (!isset($data['comments']['album'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading album tag in '{$file}'");
             }
             $album = 'unknown';
         } else {
             $album = stripslashes($data['comments']['album'][0]);
             $album = utf8_encode($album);
         }
         if (!isset($data['comments']['title'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading title tag in '{$file}'");
             }
             $title = 'unknown';
         } else {
             $title = stripslashes($data['comments']['title'][0]);
             $title = utf8_encode($title);
         }
         $size = $data['filesize'];
         $track = isset($data['comments']['track']) ? $data['comments']['track'][0] : 0;
         $length = isset($data['playtime_seconds']) ? round($data['playtime_seconds']) : 0;
     }
     if (!isset(self::$artists[$artist])) {
         $artistId = OC_MEDIA_COLLECTION::addArtist($artist);
         self::$artists[$artist] = $artistId;
     } else {
         $artistId = self::$artists[$artist];
     }
     if (!isset(self::$albums[$artist . '/' . $album])) {
         $albumId = OC_MEDIA_COLLECTION::addAlbum($album, $artistId);
         self::$albums[$artist . '/' . $album] = $albumId;
     } else {
         $albumId = self::$albums[$artist . '/' . $album];
     }
     $songId = OC_MEDIA_COLLECTION::addSong($title, $path, $artistId, $albumId, $length, $track, $size);
     return !($title == 'unkown' && $artist == 'unkown' && $album == 'unkown') ? $songId : 0;
 }