From c8bfff18ef4f70b0abf9f3d7e2eaf0079bab29f8 Mon Sep 17 00:00:00 2001 From: Karl 'vollmerk' Vollmer Date: Tue, 14 Aug 2007 07:21:35 +0000 Subject: step one of toasting /lib/preference.lib.php and also added a missing GPL note --- lib/class/error.class.php | 22 +++++ lib/class/preference.class.php | 155 +++++++++++++++++++++++++++++++++ login.php | 15 ++-- modules/plugins/Lastfm.plugin.php | 30 ++----- modules/plugins/OpenStrands.plugin.php | 10 +-- 5 files changed, 190 insertions(+), 42 deletions(-) create mode 100644 lib/class/preference.class.php diff --git a/lib/class/error.class.php b/lib/class/error.class.php index bc839bdb..3ff9c126 100644 --- a/lib/class/error.class.php +++ b/lib/class/error.class.php @@ -1,4 +1,26 @@ username . ' attempted to update ' . $name . ' but does not have sufficient permissions','3'); + } + + return false; + } // update + + /** + * has_access + * This checks to see if the current user has access to modify this preference + * as defined by the preference name + */ + public static function has_access($preference) { + + // Nothing for those demo thugs + if (Config::get('demo_mode')) { return false; } + + $preference = Dba::escape($preference); + + $sql = "SELECT `level` FROM `preference` WHERE `name`='$preference'"; + $db_results = Dba::query($sql); + + if ($GLOBALS['user']->has_access($data['level'])) { + return true; + } + + return false; + + } // has_access + + /** + * id_from_name + * This takes a name and returns the id + */ + public static function id_from_name($name) { + + $name = Dba::escape($name); + + $sql = "SELECT `id` FROM `preference` WHERE `name`='$name'"; + $db_results = Dba::query($sql); + + $row = Dba::fetch_assoc($db_results); + + return $row['id']; + + } // id_from_name + + /** + * name_from_id + * This returns the name from an id, it's the exact opposite + * of the function above it, amazing! + */ + public static function name_from_id($id) { + + $id = Dba::escape($id); + + $sql = "SELECT `name` FROM `preference` WHERE `id`='$id'"; + $db_results = Dba::query($sql); + + $row = Dba::fetch_assoc($db_results); + + return $row['name']; + + } // name_from_id + + /** + * insert + * This inserts a new preference into the preference table + * it does NOT sync up the users, that should be done independtly + */ + public static function insert($name,$description,$default,$level,$type,$catagory) { + + // Clean em up + $name = Dba::escape($name); + $description = Dba::escape($description); + $default = Dba::escape($default); + $level = Dba::escape($level); + $type = Dba::escape($type); + $catagory = Dba::escape($catagory); + + $sql = "INSERT INTO `preference` (`name`,`description`,`value`,`level`,`catagory`) " . + "VALUES ('$name','$description','$default','$level','$catagory')"; + $db_results = Dba::query($sql); + + if (!$db_results) { return false; } + + return true; + + } // insert + +} // end Preference class diff --git a/login.php b/login.php index 4b9fa991..94231a05 100644 --- a/login.php +++ b/login.php @@ -137,16 +137,11 @@ if ($auth['success']) { debug_event('LastFM','Handshake Failed: ' . $lastfm->error_msg,'3'); } - // Get the preference IDs - $port_id = get_preference_id('lastfm_port'); - $url_id = get_preference_id('lastfm_url'); - $host_id = get_preference_id('lastfm_host'); - $challenge_id = get_preference_id('lastfm_challenge'); - update_preference($user->id,'lastfm_port',$port_id,$handshake['submit_port']); - update_preference($user->id,'lastfm_host',$host_id,$handshake['submit_host']); - update_preference($user->id,'lastfm_url',$url_id,$handshake['submit_url']); - update_preference($user->id,'lastfm_challenge',$challenge_id,$handshake['challenge']); - + // Update the preferences + Preference::update('lastfm_port',$user->id,$handshake['submit_port']); + Preference::update('lastfm_host',$user->id,$handshake['submit_host']); + Preference::update('lastfm_url',$user->id,$handshake['submit_url']); + Preference::update('lastfm_challenge',$challenge_id,$handshake['challenge']); } // if LastFM diff --git a/modules/plugins/Lastfm.plugin.php b/modules/plugins/Lastfm.plugin.php index c0896b71..7407d7ba 100644 --- a/modules/plugins/Lastfm.plugin.php +++ b/modules/plugins/Lastfm.plugin.php @@ -45,30 +45,12 @@ class AmpacheLastfm { */ public function install() { - /* We need to insert the new preferences */ - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('lastfm_user',' ','Last.FM Username','25','string','options')"; - $db_results = Dba::query($sql); - - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('lastfm_pass',' ','Last.FM Password','25','string','options')"; - $db_results = Dba::query($sql); - - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('lastfm_port',' ','Last.FM Submission port','5','string','internal')"; - $db_results = Dba::query($sql); - - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('lastfm_host',' ','Last.FM Submission host','5','string','internal')"; - $db_results = Dba::query($sql); - - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('lastfm_url',' ','Last.FM Submission url','5','string','internal')"; - $db_results = Dba::query($sql); - - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('lastfm_challenge',' ','Last.FM Submission Challenge','5','string','internal')"; - $db_results = Dba::query($sql); + Preference::insert('lastfm_user','Last.FM Username',' ','25','string','options'); + Preference::insert('lastfm_pass','Last.FM Password',' ','25','string','options'); + Preference::insert('lastfm_port','Last.FM Submit Port',' ','25','string','internal'); + Preference::insert('lastfm_host','Last.FM Submit Host',' ','25','string','internal'); + Preference::insert('lastfm_url','Last.FM Submit URL',' ','25','string','internal'); + Preference::insert('lastfm_challenge','Last.FM Submit Challenge',' ','25','string','internal'); } // install diff --git a/modules/plugins/OpenStrands.plugin.php b/modules/plugins/OpenStrands.plugin.php index 5fe6821e..1a7b1b78 100644 --- a/modules/plugins/OpenStrands.plugin.php +++ b/modules/plugins/OpenStrands.plugin.php @@ -45,14 +45,8 @@ class AmpacheOpenStrands { */ public function install() { - /* We need to insert the new preferences */ - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('mystrands_user',' ','MyStrands Username','25','string','options')"; - $db_results = Dba::query($sql); - - $sql = "INSERT INTO preference (`name`,`value`,`description`,`level`,`type`,`catagory`) " . - "VALUES ('mystrands_pass',' ','MyStrands Password','25','string','options')"; - $db_results = Dba::query($sql); + Preference::insert('mystrands_user','MyStrands Login',' ','25','string','options'); + Preference::insert('mystrands_pass','MyStrands Password',' ','25','string','options'); } // install -- cgit