/** * build_cache * Build a cache based on the array of ids passed, saves lots of little queries */ public static function build_cache($ids = array()) { if (!is_array($ids) or !count($ids)) { return false; } $idlist = '(' . implode(',', $ids) . ')'; $sql = "SELECT * FROM `video` WHERE `video`.`id` IN {$idlist}"; $db_results = Dba::read($sql); while ($row = Dba::fetch_assoc($db_results)) { parent::add_to_cache('video', $row['id'], $row); } }
/** * get_info * retrieves the info from the database and puts it in the cache */ public function get_info($id, $table_name = '') { $table_name = $table_name ? Dba::escape($table_name) : Dba::escape(strtolower(get_class($this))); // Make sure we've got a real id if (!is_numeric($id)) { return array(); } if (self::is_cached($table_name, $id)) { return self::get_from_cache($table_name, $id); } $sql = "SELECT * FROM `{$table_name}` WHERE `id`='{$id}'"; $db_results = Dba::read($sql); if (!$db_results) { return array(); } $row = Dba::fetch_assoc($db_results); self::add_to_cache($table_name, $id, $row); return $row; }
/** * rebuild_all_preferences * This rebuilds the user preferences for all installed users, called by the plugin functions */ public static function rebuild_all_preferences() { // Clean out any preferences garbage left over $sql = "DELETE `user_preference`.* FROM `user_preference` " . "LEFT JOIN `user` ON `user_preference`.`user` = `user`.`id` " . "WHERE `user_preference`.`user` != -1 AND `user`.`id` IS NULL"; Dba::write($sql); // Get only users who has less preferences than excepted // otherwise it would have significant performance issue with large user database $sql = "SELECT `user` FROM `user_preference` " . "GROUP BY `user` HAVING COUNT(*) < (" . "SELECT COUNT(`id`) FROM `preference` WHERE `catagory` != 'system')"; $db_results = Dba::read($sql); while ($row = Dba::fetch_assoc($db_results)) { User::fix_preferences($row['user']); } return true; }
/** * get * This functions returns an array containing information about * The songs that vlc currently has in it's playlist. This must be * done in a standardized fashion * Warning ! if you got files in vlc medialibary those files will be sent to the php xml parser * to, not to your browser but still this can take a lot of work for your server. * The xml files of vlc need work, not much documentation on them.... */ public function get() { /* Get the Current Playlist */ $list = $this->_vlc->get_tracks(); if (!$list) { return array(); } $counterforarray = 0; // here we look if there are song in the playlist when media libary is used if ($list['node']['node'][0]['leaf'][$counterforarray]['attr']['uri']) { while ($list['node']['node'][0]['leaf'][$counterforarray]) { $songs[] = htmlspecialchars_decode($list['node']['node'][0]['leaf'][$counterforarray]['attr']['uri'], ENT_NOQUOTES); $songid[] = $list['node']['node'][0]['leaf'][$counterforarray]['attr']['id']; $counterforarray++; } // if there is only one song look here,and media libary is used } elseif ($list['node']['node'][0]['leaf']['attr']['uri']) { $songs[] = htmlspecialchars_decode($list['node']['node'][0]['leaf']['attr']['uri'], ENT_NOQUOTES); $songid[] = $list['node']['node'][0]['leaf']['attr']['id']; } elseif ($list['node']['node']['leaf'][$counterforarray]['attr']['uri']) { while ($list['node']['node']['leaf'][$counterforarray]) { $songs[] = htmlspecialchars_decode($list['node']['node']['leaf'][$counterforarray]['attr']['uri'], ENT_NOQUOTES); $songid[] = $list['node']['node']['leaf'][$counterforarray]['attr']['id']; $counterforarray++; } } elseif ($list['node']['node']['leaf']['attr']['uri']) { $songs[] = htmlspecialchars_decode($list['node']['node']['leaf']['attr']['uri'], ENT_NOQUOTES); $songid[] = $list['node']['node']['leaf']['attr']['id']; } else { return array(); } $counterforarray = 0; foreach ($songs as $key => $entry) { $data = array(); /* Required Elements */ $data['id'] = $songid[$counterforarray]; // id number of the files in the vlc playlist, needed for other operations $data['raw'] = $entry; $url_data = $this->parse_url($entry); switch ($url_data['primary_key']) { case 'oid': $data['oid'] = $url_data['oid']; $song = new Song($data['oid']); $song->format(); $data['name'] = $song->f_title . ' - ' . $song->f_album . ' - ' . $song->f_artist; $data['link'] = $song->f_link; break; case 'demo_id': $democratic = new Democratic($url_data['demo_id']); $data['name'] = T_('Democratic') . ' - ' . $democratic->name; $data['link'] = ''; break; case 'random': $data['name'] = T_('Random') . ' - ' . scrub_out(ucfirst($url_data['type'])); $data['link'] = ''; break; default: /* If we don't know it, look up by filename */ $filename = Dba::escape($entry); $sql = "SELECT `name` FROM `live_stream` WHERE `url`='{$filename}' "; $db_results = Dba::read($sql); if ($row = Dba::fetch_assoc($db_results)) { //if stream is known just send name $data['name'] = htmlspecialchars(substr($row['name'], 0, 50)); } elseif (strncmp($entry, 'http', 4) == 0) { $data['name'] = htmlspecialchars("(VLC stream) " . substr($entry, 0, 50)); } else { $getlast = explode("/", $entry); $lastis = count($getlast) - 1; $data['name'] = htmlspecialchars("(VLC local) " . substr($getlast[$lastis], 0, 50)); } // end if loop break; } // end switch on primary key type $data['track'] = $key + 1; $counterforarray++; $results[] = $data; } // foreach playlist items return $results; }
/** * update_380005 * * Add manual update flag on artist */ public static function update_380005() { $retval = true; $sql = "ALTER TABLE `artist` ADD COLUMN `manual_update` SMALLINT( 1 ) DEFAULT '0'"; $retval &= Dba::write($sql); return $retval; }
/** * get_songs * This functions returns an array containing information about * the songs that MPD currently has in its playlist. This must be * done in a standardized fashion */ public function get() { // If we don't have the playlist yet, pull it if (!isset($this->_mpd->playlist)) { $this->_mpd->RefreshInfo(); } /* Get the Current Playlist */ $playlist = $this->_mpd->playlist; foreach ($playlist as $entry) { $data = array(); /* Required Elements */ $data['id'] = $entry['Pos']; $data['raw'] = $entry['file']; $url_data = $this->parse_url($entry['file']); switch ($url_data['primary_key']) { case 'oid': $data['oid'] = $url_data['oid']; $song = new Song($data['oid']); $song->format(); $data['name'] = $song->f_title . ' - ' . $song->f_album . ' - ' . $song->f_artist; $data['link'] = $song->f_link; break; case 'demo_id': $democratic = new Democratic($url_data['demo_id']); $data['name'] = T_('Democratic') . ' - ' . $democratic->name; $data['link'] = ''; break; case 'random': $data['name'] = T_('Random') . ' - ' . scrub_out(ucfirst($url_data['type'])); $data['link'] = ''; break; default: /* If we don't know it, look up by filename */ $filename = Dba::escape($entry['file']); $sql = "SELECT `id`,'song' AS `type` FROM `song` WHERE `file` LIKE '%{$filename}' " . "UNION ALL " . "SELECT `id`,'live_stream' AS `type` FROM `live_stream` WHERE `url`='{$filename}' "; $db_results = Dba::read($sql); if ($row = Dba::fetch_assoc($db_results)) { $media = new $row['type']($row['id']); $media->format(); switch ($row['type']) { case 'song': $data['name'] = $media->f_title . ' - ' . $media->f_album . ' - ' . $media->f_artist; $data['link'] = $media->f_link; break; case 'live_stream': $frequency = $media->frequency ? '[' . $media->frequency . ']' : ''; $site_url = $media->site_url ? '(' . $media->site_url . ')' : ''; $data['name'] = "{$media->name} {$frequency} {$site_url}"; $data['link'] = $media->site_url; break; } // end switch on type } else { $data['name'] = T_('Unknown'); $data['link'] = ''; } break; } // end switch on primary key type /* Optional Elements */ $data['track'] = $entry['Pos'] + 1; $results[] = $data; } // foreach playlist items return $results; }
/** * remove_plugin_version * This removes the version row from the db done on uninstall */ public function remove_plugin_version() { $name = Dba::escape('Plugin_' . $this->_plugin->name); $sql = "DELETE FROM `update_info` WHERE `key`='{$name}'"; Dba::write($sql); return true; }
/** * save_objects * This takes the full array of object ids, often passed into show and * if necessary it saves them */ public function save_objects($object_ids) { // Saving these objects has two operations, one holds it in // a local variable and then second holds it in a row in the // tmp_browse table // Only do this if it's not a simple browse if (!$this->is_simple()) { $this->_cache = $object_ids; $this->set_total(count($object_ids)); $id = $this->id; if ($id != 'nocache') { $data = self::_serialize($this->_cache); $sql = 'UPDATE `tmp_browse` SET `object_data` = ? ' . 'WHERE `sid` = ? AND `id` = ?'; Dba::write($sql, array($data, session_id(), $id)); } } return true; }
/** * get_access_lists * returns a full listing of all access rules on this server */ public static function get_access_lists() { $sql = 'SELECT `id` FROM `access_list`'; $db_results = Dba::read($sql); $results = array(); while ($row = Dba::fetch_assoc($db_results)) { $results[] = $row['id']; } return $results; }
/** * 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; }
public static function get_all_radios($catalog = null) { $sql = "SELECT `live_stream`.`id` FROM `live_stream` JOIN `catalog` ON `catalog`.`id` = `live_stream`.`catalog` "; if (AmpConfig::get('catalog_disable')) { $sql .= "WHERE `catalog`.`enabled` = '1' "; } $params = array(); if ($catalog) { if (AmpConfig::get('catalog_disable')) { $sql .= "AND "; } $sql .= "`catalog`.`id` = ?"; $params[] = $catalog; } $db_results = Dba::read($sql, $params); $radios = array(); while ($results = Dba::fetch_assoc($db_results)) { $radios[] = $results['id']; } return $radios; }
/** * Sort the tracks and save the new position */ public function sort_tracks() { /* First get all of the songs in order of their tracks */ $sql = "SELECT A.`id`\n FROM `playlist_data` AS A\n LEFT JOIN `song` AS B ON A.object_id = B.id\n LEFT JOIN `artist` AS C ON B.artist = C.id\n LEFT JOIN `album` AS D ON B.album = D.id\n WHERE A.`playlist` = ?\n ORDER BY C.`name` ASC,\n B.`title` ASC,\n D.`year` ASC,\n D.`name` ASC,\n B.`track` ASC"; $db_results = Dba::query($sql, array($this->id)); $i = 1; $results = array(); while ($r = Dba::fetch_assoc($db_results)) { $new_data = array(); $new_data['id'] = $r['id']; $new_data['track'] = $i; $results[] = $new_data; $i++; } // end while results foreach ($results as $data) { $sql = "UPDATE `playlist_data` SET `track` = ? WHERE `id` = ?"; Dba::write($sql, array($data['track'], $data['id'])); } // foreach re-ordered results return true; }
public static function gc() { $sql = 'DELETE FROM `song_preview` USING `song_preview` ' . 'LEFT JOIN `session` ON `session`.`id`=`song_preview`.`session` ' . 'WHERE `session`.`id` IS NULL'; return Dba::write($sql); }
/** * mysql_auth * * This is the core function of our built-in authentication. */ private static function mysql_auth($username, $password) { if (strlen($password) && strlen($username)) { $sql = 'SELECT `password` FROM `user` WHERE `username` = ?'; $db_results = Dba::read($sql, array($username)); if ($row = Dba::fetch_assoc($db_results)) { // Use SHA2 now... cooking with fire. // For backwards compatibility we hash a couple of different // variations of the password. Increases collision chances, but // doesn't break things. // FIXME: Break things in the future. $hashed_password = array(); $hashed_password[] = hash('sha256', $password); $hashed_password[] = hash('sha256', Dba::escape(stripslashes(htmlspecialchars(strip_tags($password))))); // Automagically update the password if it's old and busted. if ($row['password'] == $hashed_password[1] && $hashed_password[0] != $hashed_password[1]) { $user = User::get_from_username($username); $user->update_password($password); } if (in_array($row['password'], $hashed_password)) { return array('success' => true, 'type' => 'mysql', 'username' => $username); } } } return array('success' => false, 'error' => 'MySQL login attempt failed'); }
/** * set_rating * This function sets the rating for the current object. * If no userid is passed in, we use the currently logged in user. */ public function set_rating($rating, $user_id = null) { if (is_null($user_id)) { $user_id = $GLOBALS['user']->id; } $user_id = intval($user_id); debug_event('Rating', "Setting rating for {$this->type} {$this->id} to {$rating}", 5); // If score is -1, then remove rating if ($rating == '-1') { $sql = "DELETE FROM `rating` WHERE " . "`object_id` = ? AND " . "`object_type` = ? AND " . "`user` = ?"; $params = array($this->id, $this->type, $user_id); } else { $sql = "REPLACE INTO `rating` " . "(`object_id`, `object_type`, `rating`, `user`) " . "VALUES (?, ?, ?, ?)"; $params = array($this->id, $this->type, $rating, $user_id); } Dba::write($sql, $params); parent::add_to_cache('rating_' . $this->type . '_user' . $user_id, $this->id, $rating); foreach (Plugin::get_plugins('save_rating') as $plugin_name) { $plugin = new Plugin($plugin_name); if ($plugin->load($GLOBALS['user'])) { $plugin->_plugin->save_rating($this, $rating); } } return true; }
/** * check_lock_media * * This checks to see if the media is already being played. */ public static function check_lock_media($media_id, $type) { $sql = 'SELECT `object_id` FROM `now_playing` WHERE ' . '`object_id` = ? AND `object_type` = ?'; $db_results = Dba::read($sql, array($media_id, $type)); if (Dba::num_rows($db_results)) { debug_event('Stream', 'Unable to play media currently locked by another user', 3); return false; } return true; }
/** * show_playlist_select * This one is for playlists! */ function show_playlist_select($name, $selected = '', $style = '') { echo "<select name=\"{$name}\" style=\"{$style}\">\n"; echo "\t<option value=\"\">" . T_('None') . "</option>\n"; $sql = "SELECT `id`,`name` FROM `playlist` ORDER BY `name`"; $db_results = Dba::read($sql); $nb_items = Dba::num_rows($db_results); $index = 1; $already_selected = false; while ($row = Dba::fetch_assoc($db_results)) { $select_txt = ''; if (!$already_selected && ($row['id'] == $selected || $index == $nb_items)) { $select_txt = 'selected="selected"'; $already_selected = true; } echo "\t<option value=\"" . $row['id'] . "\" {$select_txt}>" . scrub_out($row['name']) . "</option>\n"; ++$index; } // end while users echo "</select>\n"; }
public static function get_shouts($object_type, $object_id) { $sql = "SELECT `id` FROM `user_shout` WHERE `object_type` = ? AND `object_id` = ? ORDER BY `sticky`, `date` DESC"; $db_results = Dba::read($sql, array($object_type, $object_id)); $results = array(); while ($row = Dba::fetch_assoc($db_results)) { $results[] = $row['id']; } return $results; }
public function remove_from_disk() { $deleted = true; $season_ids = $this->get_seasons(); foreach ($season_ids as $id) { $season = new TVShow_Season($id); $deleted = $season->remove_from_disk(); if (!$deleted) { debug_event('tvshow', 'Error when deleting the season `' . $id . '`.', 1); break; } } if ($deleted) { $sql = "DELETE FROM `tvshow` WHERE `id` = ?"; $deleted = Dba::write($sql, array($this->id)); if ($deleted) { Art::gc('tvshow', $this->id); Userflag::gc('tvshow', $this->id); Rating::gc('tvshow', $this->id); Shoutbox::gc('tvshow', $this->id); Useractivity::gc('tvshow', $this->id); } } return $deleted; }
/** * show_footer * * Shows the footer template and possibly profiling info. */ public static function show_footer() { require_once AmpConfig::get('prefix') . '/templates/footer.inc.php'; if (isset($_REQUEST['profiling'])) { Dba::show_profile(); } }
/** * Remove the video from disk. */ public function remove_from_disk() { $deleted = parent::remove_from_disk(); if ($deleted) { $sql = "DELETE FROM `tvshow_episode` WHERE `id` = ?"; $deleted = Dba::write($sql, array($this->id)); } return $deleted; }
protected function deleteRecord($id) { $sql = 'DELETE FROM ' . $this->getTableName() . ' WHERE id = ?'; \Dba::write($sql, array($id)); }
/** * 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); } }
/** * delete_track * This deletes a track from the tmpplaylist */ public function delete_track($id) { /* delete the track its self */ $sql = "DELETE FROM `tmp_playlist_data` WHERE `id` = ?"; Dba::write($sql, array($id)); return true; }
/** * Duplicate an object associate images to a new object * @param string $object_type * @param int $old_object_id * @param int $new_object_id * @return boolean */ public static function duplicate($object_type, $old_object_id, $new_object_id) { if (AmpConfig::get('album_art_store_disk')) { $sql = "SELECT `size`, `kind` FROM `image` WHERE `object_type` = ? AND `object_id` = ?"; $db_results = Dba::read($sql, array($object_type, $old_object_id)); while ($row = Dba::fetch_assoc($db_results)) { $image = self::read_from_dir($row['size'], $object_type, $old_object_id, $row['kind']); if ($image != null) { self::write_to_dir($image, $row['size'], $object_type, $new_object_id, $row['kind']); } } } $sql = "INSERT INTO `image` (`image`, `mime`, `size`, `object_type`, `object_id`, `kind`) SELECT `image`, `mime`, `size`, `object_type`, ? as `object_id`, `kind` FROM `image` WHERE `object_type` = ? AND `object_id` = ?"; return Dba::write($sql, array($new_object_id, $object_type, $old_object_id)); }
/** * Get broadcasts from an user. * @param int $user_id * @return int[] */ public static function get_broadcasts($user_id) { $sql = "SELECT `id` FROM `broadcast` WHERE `user` = ?"; $db_results = Dba::read($sql, array($user_id)); $broadcasts = array(); while ($results = Dba::fetch_assoc($db_results)) { $broadcasts[] = $results['id']; } return $broadcasts; }
/** * update * * This function updates the saved version with the current settings. */ public function update(array $data = null) { if ($data && is_array($data)) { $this->name = $data['name']; $this->type = $data['pl_type']; $this->random = $data['random']; $this->limit = $data['limit']; } if (!$this->id) { return false; } $sql = "UPDATE `search` SET `name` = ?, `type` = ?, `rules` = ?, `logic_operator` = ?, `random` = ?, `limit` = ? WHERE `id` = ?"; Dba::write($sql, array($this->name, $this->type, serialize($this->rules), $this->logic_operator, $this->random, $this->limit, $this->id)); return $this->id; }
/** * get_instance * This returns a single instance and all it's variables */ public function get_instance($instance = '') { $instance = $instance ? $instance : AmpConfig::get('xbmc_active'); $sql = "SELECT * FROM `localplay_xbmc` WHERE `id` = ?"; $db_results = Dba::query($sql, array($instance)); $row = Dba::fetch_assoc($db_results); return $row; }
/** * remove_map * This will only remove tag maps for the current user */ public function remove_map($type, $object_id) { if (!Core::is_library_item($type)) { return false; } // TODO: Review the tag edition per user. $sql = "DELETE FROM `tag_map` WHERE `tag_id` = ? AND `object_type` = ? AND `object_id` = ? "; //AND `user` = ?"; Dba::write($sql, array($this->id, $type, $object_id)); //, $GLOBALS['user']->id)); return true; }
UI::show_header(); /* Switch on action boys */ switch ($_REQUEST['action']) { /* This re-generates the config file comparing * /config/ampache.cfg to .cfg.dist */ case 'generate_config': ob_end_clean(); $current = parse_ini_file(AmpConfig::get('prefix') . '/config/ampache.cfg.php'); $final = generate_config($current); $browser = new Horde_Browser(); $browser->downloadHeaders('ampache.cfg.php', 'text/plain', false, filesize(AmpConfig::get('prefix') . '/config/ampache.cfg.php.dist')); echo $final; exit; case 'reset_db_charset': Dba::reset_db_charset(); show_confirmation(T_('Database Charset Updated'), T_('Your Database and associated tables have been updated to match your currently configured charset'), AmpConfig::get('web_path') . '/admin/system.php?action=show_debug'); break; case 'show_debug': $configuration = AmpConfig::get_all(); if ($_REQUEST['autoupdate'] == 'force') { $version = AutoUpdate::get_latest_version(true); } require_once AmpConfig::get('prefix') . '/templates/show_debug.inc.php'; break; default: // Rien a faire break; } // end switch UI::show_footer();