1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-05 02:39:47 +02:00

fix 1201 and rewrite LDAP class

and ran php-cs-fixer as told in the wiki
This commit is contained in:
Niols 2016-04-15 17:11:41 +02:00
parent b6a8e86f05
commit 6cbf700151
77 changed files with 105 additions and 143 deletions

View file

@ -208,4 +208,3 @@ switch ($_REQUEST['action']) {
require_once 'templates/show_install_lang.inc.php'; require_once 'templates/show_install_lang.inc.php';
break; break;
} // end action switch } // end action switch

View file

@ -249,4 +249,3 @@ class Ajax
self::$include_override = false; self::$include_override = false;
} // end_container } // end_container
} // end Ajax class } // end Ajax class

View file

@ -1085,4 +1085,3 @@ class Album extends database_object implements library_item
return $results; return $results;
} }
} //end of album class } //end of album class

View file

@ -378,4 +378,3 @@ class Ampache_RSS
return $element['date']; return $element['date'];
} // pubdate_recently_played } // pubdate_recently_played
} // end Ampache_RSS class } // end Ampache_RSS class

View file

@ -143,4 +143,3 @@ class AmpError
} }
} // auto_init } // auto_init
} // Error } // Error

View file

@ -1120,4 +1120,3 @@ class Api
} }
} // friends_timeline } // friends_timeline
} // API class } // API class

View file

@ -1775,4 +1775,3 @@ class Art extends database_object
return true; return true;
} }
} // Art } // Art

View file

@ -917,4 +917,3 @@ class Artist extends database_object implements library_item
return $deleted; return $deleted;
} }
} // end of artist class } // end of artist class

View file

@ -86,4 +86,3 @@ class Artist_Event
return false; return false;
} }
} // end of recommendation class } // end of recommendation class

View file

@ -153,4 +153,3 @@ class Bookmark extends database_object
$f_user = $user->username; $f_user = $user->username;
} }
} //end bookmark class } //end bookmark class

View file

@ -423,4 +423,3 @@ class Broadcast extends database_object implements library_item
return $oid; return $oid;
} }
} // end of broadcast class } // end of broadcast class

View file

@ -439,4 +439,3 @@ class Broadcast_Server implements MessageComponentInterface
return $websocket_address . '/broadcast'; return $websocket_address . '/broadcast';
} }
} // end of broadcast_server class } // end of broadcast_server class

View file

@ -587,4 +587,3 @@ class Browse extends Query
return $css; return $css;
} }
} // browse } // browse

View file

@ -2409,4 +2409,3 @@ abstract class Catalog extends database_object
} }
// end of catalog class // end of catalog class

View file

@ -547,4 +547,3 @@ class Channel extends database_object implements media, library_item
return($s); return($s);
} }
} // end of channel class } // end of channel class

View file

@ -134,4 +134,3 @@ class Clip extends Video
return null; return null;
} }
} // Clip class } // Clip class

View file

@ -392,4 +392,3 @@ class Core
return $tmp_dir; return $tmp_dir;
} }
} // Core } // Core

View file

@ -139,4 +139,3 @@ abstract class database_object
self::$_enabled = AmpConfig::get('memory_cache'); self::$_enabled = AmpConfig::get('memory_cache');
} // _auto_init } // _auto_init
} // end database_object } // end database_object

View file

@ -649,4 +649,3 @@ class Democratic extends Tmp_Playlist
return $voters; return $voters;
} // get_voters } // get_voters
} // Democratic class } // Democratic class

View file

@ -40,7 +40,7 @@ class LDAPException extends Exception
*/ */
public function __construct ($message) public function __construct ($message)
{ {
if (is_int (message)) { if (is_int ($message)) {
$message = 'LDAP error: [' . $message . '] ' . ldap_err2str($message); $message = 'LDAP error: [' . $message . '] ' . ldap_err2str($message);
} }
@ -67,6 +67,98 @@ class LDAP
} }
/**
* Connect to the LDAP
* Note: This does not open a connection. It checks whether
* the given parameters are plausibe and can be used to open a
* connection as soon as one is needed.
*/
private static function connect ()
{
if (! $url = AmpConfig::get('ldap_url')) {
throw new LDAPException('Required configuration value missing: ldap_url');
}
if (! $link = ldap_connect ($url)) {
throw new LDAPException('Could not connect to ' . $url);
}
$protocol_version = AmpConfig::get('ldap_protocol_version', 3);
if (! ldap_set_option ($link, LDAP_OPT_PROTOCOL_VERSION, $protocol_version)) {
throw new LDAPException('Could not set option PROTOCOL_VERSION to ' . $protocol_version);
}
if (AmpConfig::get('ldap_start_tls', "false") != "false") {
if (! ldap_start_tls ($link)) {
throw new LDAPException('Could not use StartTLS');
}
}
return $link;
}
/**
* Binds to the LDAP
*/
private static function bind ($link, $username = null, $password = null)
{
if ($username === null && $password === null) {
$username = AmpConfig::get('ldap_username', '');
$password = AmpConfig::get('ldap_password', '');
}
if (! ldap_bind ($link, $ampache_username, $ampache_password)) {
throw new LDAPException("Could not bind to server using username `$username`");
}
}
/**
* Unbinds from the LDAP
*/
private static function unbind ($link)
{
ldap_unbind ($link);
}
/**
* Read attributes for a DN from the LDAP
*/
private static function read ($link, $dn, $attrs = [], $filter='objectClass=*')
{
if (! $result = ldap_read ($link, $dn, $filter, $attrs)) {
$attrs = json_encode ($attrs);
throw new LDAPException("Could not read attributes `$attrs` for dn `$dn`");
}
if (! $infos = ldap_get_entries ($link, $result)) {
throw new LDAPException("Empty search result for dn `$dn`");
}
return $infos[0];
}
/**
* Search for a DN in the LDAP
*/
private static function search ($link, $base_dn, $filter)
{
if (! $result = ldap_search ($link, $base_dn, $filter)) {
throw new LDAPException(ldap_errno($link));
}
if (! $entries = ldap_get_entries ($link, $result)) {
throw new LDAPException("Empty search results for filter `$filter`");
}
return $entries[0];
}
/** /**
* ldap_auth * ldap_auth
* *
@ -79,46 +171,8 @@ class LDAP
public static function auth ($username, $password) public static function auth ($username, $password)
{ {
try { try {
/* Connect to the LDAP $link = self::connect ();
Note: This does not open a connection. It checks whether self::bind($link);
the given parameters are plausibe and can be used to open a
connection as soon as one is needed. */
if (! $url = AmpConfig::get('ldap_url')) {
throw new LDAPException('Required configuration value missing: ldap_url');
}
if (! $link = ldap_connect ($url)) {
throw new LDAPException('Could not connect to ' . $url);
}
/* Set the LDAP protocol version (default: 3) */
$protocol_version = AmpConfig::get('ldap_protocol_version', 3);
if (! ldap_set_option ($link, LDAP_OPT_PROTOCOL_VERSION, $protocol_version)) {
throw new LDAPException('Could not set option PROTOCOL_VERSION to ' . $protocol_version);
}
/* Use StartTLS if asked */
if (AmpConfig::get('ldap_start_tls', "false") != "false") {
if (! ldap_start_tls ($link)) {
throw new LDAPException('Could not use StartTLS');
}
}
/* Connect to the LDAP using the given username and password.
If these parameters do not exist, an anonymous connection
will be used */
$ampache_username = AmpConfig::get('ldap_username');
$ampache_password = AmpConfig::get('ldap_password');
if (! ldap_bind ($link, $ampache_username, $ampache_password)) {
throw new LDAPException('Could not bind to server using username `'
. $ampache_username . '` and password `'
. $ampache_password . '`');
}
/* Search for the user with given base_dn, filter, objectclass and username */ /* Search for the user with given base_dn, filter, objectclass and username */
@ -143,43 +197,25 @@ class LDAP
throw new LDAPException('Required configuration value missing: ldap_search_dn'); throw new LDAPException('Required configuration value missing: ldap_search_dn');
} }
if (! $result = ldap_search ($link, $base_dn, $search)) { $user_entry = self::search ($link, $base_dn, $search);
throw new LDAPException(ldap_errno($link));
}
/* Bind with the user's DN and the password */
if (! $user_entry = ldap_first_entry ($link, $result)) {
throw new LDAPException('Empty search result');
}
if (! $user_dn = ldap_get_dn ($link, $user_entry)) { if (! $user_dn = ldap_get_dn ($link, $user_entry)) {
throw new LDAPException(ldap_errno($link)); throw new LDAPException(ldap_errno($link));
} }
$user_entry = ldap_get_entries ($link, $result) [0]; self::bind ($link, $user_dn, $password);
if (! ldap_bind ($link, $user_dn, $password)) {
throw new LDAPException('Wrong password');
}
/* Test if the user is in the required group (optional) */ /* Test if the user is in the required group (optional) */
if ($group_dn = AmpConfig::get('ldap_require_group')) { if ($group_dn = AmpConfig::get('ldap_require_group')) {
$member_attribute = AmpConfig::get('ldap_member_attribute', 'member'); $member_attribute = AmpConfig::get('ldap_member_attribute', 'member');
if (! $group_result = ldap_read ($link, $group_dn, 'objectClass=*', [$member_attribute])) { $group_infos = self::read ($link, $group_dn, [$member_attribute]);
throw new LDAPException("Could not read member attribute `$member_attribute`"
. " for group `$group_dn`");
}
if (! $group_infos = ldap_get_entries ($link, $group_result) [0]) { /* if (! in_array ($username, $group_infos[$member_attribute])) { */
throw new LDAPException('Empty group search result'); if (! preg_grep("/^$user_dn\$/i", $group_infos[$member_attribute])) {
} throw new LDAPException("`$username` is not member of the group `$group_dn`");
}
if (! in_array ($username, $group_infos[$member_attribute])) {
throw new LDAPException("`$username` is not member of the group `$group_dn`");
}
} }
/* Obtain name and email field. Reconstruct name field to allow /* Obtain name and email field. Reconstruct name field to allow
@ -212,7 +248,9 @@ class LDAP
]; ];
} }
ldap_unbind ($link); if (isset ($link)) {
self::unbind ($link);
}
debug_event('LDAP', 'Return value of authentication: ' . json_encode($return_value), 6); debug_event('LDAP', 'Return value of authentication: ' . json_encode($return_value), 6);

View file

@ -43,4 +43,3 @@ interface library_item extends playable_item
public static function gc(); public static function gc();
} // end interface } // end interface

View file

@ -146,4 +146,3 @@ class License
return $results; return $results;
} // get_licenses } // get_licenses
} // License class } // License class

View file

@ -344,4 +344,3 @@ class Live_Stream extends database_object implements media, library_item
// Do nothing // Do nothing
} }
} //end of radio class } //end of radio class

View file

@ -636,4 +636,3 @@ class Localplay
return $track_name; return $track_name;
} // get_user_playing } // get_user_playing
} // end localplay class } // end localplay class

View file

@ -107,4 +107,3 @@ abstract class localplay_controller
return $data; return $data;
} // parse_url } // parse_url
} // end localplay_controller interface } // end localplay_controller interface

View file

@ -208,4 +208,3 @@ class Mailer
return $this->send($mail); return $this->send($mail);
} }
} // Mailer class } // Mailer class

View file

@ -63,4 +63,3 @@ interface media
public function set_played($user, $agent, $location); public function set_played($user, $agent, $location);
} // end interface } // end interface

View file

@ -157,4 +157,3 @@ class Movie extends Video
return $deleted; return $deleted;
} }
} // Movie class } // Movie class

View file

@ -76,4 +76,3 @@ class Openid
return $policies; return $policies;
} }
} // end of Openid class } // end of Openid class

View file

@ -112,4 +112,3 @@ class Personal_Video extends Video
return $deleted; return $deleted;
} }
} // Personal_Video class } // Personal_Video class

View file

@ -79,4 +79,3 @@ interface playable_item
*/ */
public function get_catalogs(); public function get_catalogs();
} // end interface } // end interface

View file

@ -535,4 +535,3 @@ class Playlist extends playlist_object
return true; return true;
} // sort_tracks } // sort_tracks
} // class Playlist } // class Playlist

View file

@ -202,4 +202,3 @@ abstract class playlist_object extends database_object implements library_item
return array(); return array();
} }
} // end playlist_object } // end playlist_object

View file

@ -322,4 +322,3 @@ class Plugin
return true; return true;
} // remove_plugin_version } // remove_plugin_version
} //end plugin class } //end plugin class

View file

@ -503,4 +503,3 @@ class Preference extends database_object
$_SESSION['userdata']['uid'] = $user_id; $_SESSION['userdata']['uid'] = $user_id;
} // init } // init
} // end Preference class } // end Preference class

View file

@ -2438,4 +2438,3 @@ class Query
$this->_state['ak'] = $ak; $this->_state['ak'] = $ak;
} }
} // query } // query

View file

@ -356,4 +356,3 @@ class Random
} }
} // advanced } // advanced
} //end of random class } //end of random class

View file

@ -281,4 +281,3 @@ class Rating extends database_object
} }
} // show } // show
} //end rating class } //end rating class

View file

@ -361,4 +361,3 @@ class Recommendation
return $results; return $results;
} // get_artist_info } // get_artist_info
} // end of recommendation class } // end of recommendation class

View file

@ -136,4 +136,3 @@ Website: %s
echo $data; echo $data;
} // show_agreement } // show_agreement
} // end registration class } // end registration class

View file

@ -278,4 +278,3 @@ class scrobbler
} }
} // love } // love
} // end audioscrobbler class } // end audioscrobbler class

View file

@ -384,4 +384,3 @@ class Share extends database_object
echo "</ul>"; echo "</ul>";
} }
} // end of recommendation class } // end of recommendation class

View file

@ -347,4 +347,3 @@ class Shoutbox
return $results; return $results;
} }
} // Shoutbox class } // Shoutbox class

View file

@ -59,4 +59,3 @@ class Slideshow
return $images; return $images;
} }
} // end of Slideshow class } // end of Slideshow class

View file

@ -2110,4 +2110,3 @@ class Song extends database_object implements media, library_item
return $deleted; return $deleted;
} }
} // end of song class } // end of song class

View file

@ -351,4 +351,3 @@ class Song_Preview extends database_object implements media, playable_item
return Dba::write($sql); return Dba::write($sql);
} }
} // end of song_preview class } // end of song_preview class

View file

@ -462,4 +462,3 @@ class Stats
return $items; return $items;
} // get_newest } // get_newest
} // Stats class } // Stats class

View file

@ -498,4 +498,3 @@ class Stream
return $url; return $url;
} // get_base_url } // get_base_url
} //end of stream class } //end of stream class

View file

@ -834,4 +834,3 @@ class Tag extends database_object implements library_item
return false; return false;
} }
} // end of Tag class } // end of Tag class

View file

@ -347,4 +347,3 @@ class Tmp_Playlist extends database_object
return true; return true;
} // delete_track } // delete_track
} // class Tmp_Playlist } // class Tmp_Playlist

View file

@ -431,4 +431,3 @@ class TVShow extends database_object implements library_item
return $deleted; return $deleted;
} }
} // end of tvshow class } // end of tvshow class

View file

@ -347,4 +347,3 @@ class TVShow_Season extends database_object implements library_item
return Dba::write($sql, array($tvshow_id, $season_id)); return Dba::write($sql, array($tvshow_id, $season_id));
} }
} // end of tvshow_season class } // end of tvshow_season class

View file

@ -229,4 +229,3 @@ class Upload
return $rootdir; return $rootdir;
} }
} // Upload class } // Upload class

View file

@ -1597,4 +1597,3 @@ class User extends database_object
return true; return true;
} }
} //end user class } //end user class

View file

@ -235,4 +235,3 @@ class Useractivity extends database_object
echo '</div><br />'; echo '</div><br />';
} // show } // show
} //end useractivity class } //end useractivity class

View file

@ -270,4 +270,3 @@ class Userflag extends database_object
require AmpConfig::get('prefix') . UI::find_template('show_object_userflag.inc.php'); require AmpConfig::get('prefix') . UI::find_template('show_object_userflag.inc.php');
} // show } // show
} //end rating class } //end rating class

View file

@ -1250,4 +1250,3 @@ class vainfo
return $data; return $data;
} }
} // end class vainfo } // end class vainfo

View file

@ -1069,4 +1069,3 @@ class Video extends database_object implements media, library_item
return true; return true;
} // _update_item } // _update_item
} // end Video class } // end Video class

View file

@ -543,4 +543,3 @@ class Wanted extends database_object
return $results; return $results;
} }
} // end of recommendation class } // end of recommendation class

View file

@ -335,4 +335,3 @@ class Waveform
return Dba::write($sql, array($waveform, $song_id)); return Dba::write($sql, array($waveform, $song_id));
} }
} // Waveform class } // Waveform class

View file

@ -867,4 +867,3 @@ class XML_Data
} }
} }
} // XML_Data } // XML_Data

View file

@ -151,4 +151,3 @@ function debug_event($type, $message, $level, $file = '', $username = '')
log_event($username, $type, $line, $file); log_event($username, $type, $line, $file);
} }
} // debug_event } // debug_event

View file

@ -193,4 +193,3 @@ if (isset($auth) && $auth['success'] && isset($user)) {
header('Location: ' . AmpConfig::get('web_path') . '/index.php'); header('Location: ' . AmpConfig::get('web_path') . '/index.php');
exit(); exit();
} // auth success } // auth success

View file

@ -55,4 +55,3 @@ function get_rating_name($score)
return T_("Off the Charts!"); return T_("Off the Charts!");
} // end switch } // end switch
} // get_rating_name } // get_rating_name

View file

@ -115,4 +115,3 @@ function theme_exists($theme_name)
return true; return true;
} // theme_exists } // theme_exists

View file

@ -511,4 +511,3 @@ class Catalog_dropbox extends Catalog
return null; return null;
} }
} // end of catalog class } // end of catalog class

View file

@ -870,4 +870,3 @@ class Catalog_local extends Catalog
return $media; return $media;
} }
} // end of local catalog class } // end of local catalog class

View file

@ -381,4 +381,3 @@ class Catalog_remote extends Catalog
return null; return null;
} }
} // end of catalog class } // end of catalog class

View file

@ -452,4 +452,3 @@ class Catalog_soundcloud extends Catalog
return null; return null;
} }
} // end of catalog class } // end of catalog class

View file

@ -377,4 +377,3 @@ class Catalog_subsonic extends Catalog
return null; return null;
} }
} // end of catalog class } // end of catalog class

View file

@ -561,4 +561,3 @@ class AmpacheHttpq extends localplay_controller
return false; return false;
} // connect } // connect
} //end of AmpacheHttpq } //end of AmpacheHttpq

View file

@ -565,4 +565,3 @@ class AmpacheMpd extends localplay_controller
return false; return false;
} // connect } // connect
} //end of AmpacheMpd } //end of AmpacheMpd

View file

@ -578,4 +578,3 @@ class AmpacheVlc extends localplay_controller
return false; return false;
} // connect } // connect
} //end of AmpacheVlc } //end of AmpacheVlc

View file

@ -665,4 +665,3 @@ class AmpacheXbmc extends localplay_controller
} }
} // connect } // connect
} //end of AmpacheXbmc } //end of AmpacheXbmc

View file

@ -168,4 +168,3 @@ switch ($_REQUEST['action']) {
require_once AmpConfig::get('prefix') . UI::find_template('show_user_registration.inc.php'); require_once AmpConfig::get('prefix') . UI::find_template('show_user_registration.inc.php');
break; break;
} // end switch on action } // end switch on action

View file

@ -53,4 +53,3 @@ switch ($_REQUEST['action']) {
echo "</div>"; echo "</div>";
break; break;
} // switch on the action } // switch on the action

View file

@ -41,4 +41,3 @@ switch ($action) {
require_once $prefix . '/templates/show_test.inc.php'; require_once $prefix . '/templates/show_test.inc.php';
break; break;
} // end switch on action } // end switch on action