Blame view

sources/apps/media/lib/media.php 3.48 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  <?php
  
  /**
   * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OCA\Media;
  
  class Media {
  	/**
  	 * get the sha256 hash of the password needed for ampache
  	 *
  	 * @param array $params, parameters passed from OC_Hook
  	 */
  	public static function loginListener($params) {
  		if (isset($params['uid']) and $params['password']) {
  			$name = $params['uid'];
  			$query = \OCP\DB::prepare("SELECT `user_id` from `*PREFIX*media_users` WHERE `user_id` LIKE ?");
  			$uid = $query->execute(array($name))->fetchAll();
  			if (count($uid) == 0) {
  				$password = hash('sha256', $_POST['password']);
  				$query = \OCP\DB::prepare("INSERT INTO `*PREFIX*media_users` (`user_id`, `user_password_sha256`) VALUES (?, ?);");
  				$query->execute(array($name, $password));
  			}
  		}
  	}
  
  	/**
  	 * get the sha256 hash of the password needed for ampache
  	 *
  	 * @param array $params, parameters passed from OC_Hook
  	 */
  	public static function passwordChangeListener($params) {
  		if (isset($params['uid']) and $params['password']) {
  			$name = $params['uid'];
  			$password = hash('sha256', $params['password']);
  			$query = \OCP\DB::prepare("UPDATE `*PREFIX*media_users` SET `user_password_sha256` = ? WHERE `user_id` = ?");
  			$query->execute(array($password, $name));
  		}
  	}
  
  	/**
  	 *
  	 */
  	public static function updateFile($params) {
  		$path = $params['path'];
  		if (!$path) return;
  		//fix a bug where there were multiply '/' in front of the path, it should only be one
  		while ($path[0] == '/') {
  			$path = substr($path, 1);
  		}
  		$path = '/' . $path;
  		$collection = new Collection(\OCP\User::getUser());
  		$scanner = new Scanner($collection);
  		$scanner->scanFile($path);
  	}
  
  	/**
  	 *
  	 */
  	public static function deleteFile($params) {
  		$path = $params['path'];
  		$collection = new Collection(\OCP\User::getUser());
  		$collection->deleteSongByPath($path);
  	}
  
  	public static function moveFile($params) {
  		$collection = new Collection(\OCP\User::getUser());
  		$collection->moveSong($params['oldpath'], $params['newpath']);
  	}
  }
  
  class SearchProvider extends \OC_Search_Provider {
  	function search($query) {
  		$collection = new Collection(\OCP\User::getUser());
  		$l = \OC_L10N::get('media');
  		$app_name = (string)$l->t('Music');
  		$artists = $collection->getArtists($query);
  		$albums = $collection->getAlbums(0, $query);
  		$songs = $collection->getSongs(0, 0, $query);
  		$results = array();
  		foreach ($artists as $artist) {
  			$results[] = new \OC_Search_Result($artist['artist_name'], '', \OCP\Util::linkTo('media', 'index.php') . '#artist=' . urlencode($artist['artist_name']), $app_name);
  		}
  		foreach ($albums as $album) {
  			$artist = $collection->getArtistName($album['album_artist']);
  			$results[] = new \OC_Search_Result($album['album_name'], 'by ' . $artist, \OCP\Util::linkTo('media', 'index.php') . '#artist=' . urlencode($artist) . '&album=' . urlencode($album['album_name']), $app_name);
  		}
  		foreach ($songs as $song) {
  			$minutes = floor($song['song_length'] / 60);
  			$seconds = $song['song_length'] % 60;
  			$artist = $collection->getArtistName($song['song_artist']);
  			$album = $collection->getalbumName($song['song_album']);
  			$results[] = new \OC_Search_Result($song['song_name'], "by $artist, in $album $minutes:$seconds", \OCP\Util::linkTo('media', 'index.php') . '#artist=' . urlencode($artist) . '&album=' . urlencode($album) . '&song=' . urlencode($song['song_name']), $app_name);
  		}
  		return $results;
  	}
  }