/** * check_username * This checks to make sure the username passed doesn't already * exist in this instance of ampache */ public static function check_username($username) { $username = Dba::escape($username); $sql = "SELECT `id` FROM `user` WHERE `username`='{$username}'"; $db_results = Dba::read($sql); if (Dba::num_rows($db_results)) { return false; } return true; }
/** * is_installed * This returns true or false if vlc controller is installed */ public function is_installed() { $sql = "DESCRIBE `localplay_vlc`"; $db_results = Dba::query($sql); return Dba::num_rows($db_results); }
/** * playlist_import * Attempts to create a Public Playlist based on the playlist file */ public static function import_playlist($playlist) { $data = file_get_contents($playlist); if (substr($playlist, -3, 3) == 'm3u') { $files = self::parse_m3u($data); } elseif (substr($playlist, -3, 3) == 'pls') { $files = self::parse_pls($data); } elseif (substr($playlist, -3, 3) == 'asx') { $files = self::parse_asx($data); } elseif (substr($playlist, -4, 4) == 'xspf') { $files = self::parse_xspf($data); } $songs = array(); $pinfo = pathinfo($playlist); if (isset($files)) { foreach ($files as $file) { $file = trim($file); // Check to see if it's a url from this ampache instance if (substr($file, 0, strlen(AmpConfig::get('web_path'))) == AmpConfig::get('web_path')) { $data = Stream_URL::parse($file); $sql = 'SELECT COUNT(*) FROM `song` WHERE `id` = ?'; $db_results = Dba::read($sql, array($data['id'])); if (Dba::num_rows($db_results)) { $songs[] = $data['id']; } } else { // Remove file:// prefix if any if (strpos($file, "file://") !== false) { $file = urldecode(substr($file, 7)); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // Removing starting / on Windows OS. if (substr($file, 0, 1) == '/') { $file = substr($file, 1); } // Restore real directory separator $file = str_replace("/", DIRECTORY_SEPARATOR, $file); } } debug_event('catalog', 'Add file ' . $file . ' to playlist.', '5'); // First, try to found the file as absolute path $sql = "SELECT `id` FROM `song` WHERE `file` = ?"; $db_results = Dba::read($sql, array($file)); $results = Dba::fetch_assoc($db_results); if (isset($results['id'])) { $songs[] = $results['id']; } else { // Not found in absolute path, create it from relative path $file = $pinfo['dirname'] . DIRECTORY_SEPARATOR . $file; // Normalize the file path. realpath requires the files to exists. $file = realpath($file); if ($file) { $sql = "SELECT `id` FROM `song` WHERE `file` = ?"; $db_results = Dba::read($sql, array($file)); $results = Dba::fetch_assoc($db_results); if (isset($results['id'])) { $songs[] = $results['id']; } } } } // if it's a file } } debug_event('import_playlist', "Parsed " . $playlist . ", found " . count($songs) . " songs", 5); if (count($songs)) { $name = $pinfo['extension'] . " - " . $pinfo['filename']; $playlist_id = Playlist::create($name, 'public'); if (!$playlist_id) { return array('success' => false, 'error' => T_('Failed to create playlist.')); } /* Recreate the Playlist */ $playlist = new Playlist($playlist_id); $playlist->add_songs($songs, true); return array('success' => true, 'id' => $playlist_id, 'count' => count($songs)); } return array('success' => false, 'error' => T_('No valid songs found in playlist file.')); }
/** * is_installed * This returns true or false if vlc controller is installed */ public function is_installed() { $sql = "SHOW TABLES LIKE 'localplay_vlc'"; $db_results = Dba::query($sql); return Dba::num_rows($db_results) > 0; }
/** * is_installed * This returns true or false if this controller is installed */ public function is_installed() { $sql = "DESCRIBE `localplay_httpq`"; $db_results = Dba::read($sql); return Dba::num_rows($db_results); }
/** * 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; }
/** * check_database_inserted * * Checks to make sure that you have inserted the database * and that the user you are using has access to it. */ public static function check_database_inserted() { $sql = "DESCRIBE session"; $db_results = Dba::read($sql); if (!$db_results) { return false; } // Make sure the table is there if (Dba::num_rows($db_results) < 1) { return false; } return true; }
/** * create_type * * This creates a new catalog type entry for a catalog * It checks to make sure its parameters is not already used before creating * the catalog. */ public static function create_type($catalog_id, $data) { // TODO: This Method should be required / provided by parent $beetsdb = $data['beetsdb']; if (preg_match('/^[\\s]+$/', $beetsdb)) { Error::add('general', T_('Error: Beets selected, but no Beets DB File provided')); return false; } // Make sure this uri isn't already in use by an existing catalog $selectSql = 'SELECT `id` FROM `catalog_beets` WHERE `beetsdb` = ?'; $db_results = Dba::read($selectSql, array($beetsdb)); if (Dba::num_rows($db_results)) { debug_event('catalog', 'Cannot add catalog with duplicate uri ' . $beetsdb, 1); Error::add('general', sprintf(T_('Error: Catalog with %s already exists'), $beetsdb)); return false; } $insertSql = 'INSERT INTO `catalog_beets` (`beetsdb`, `catalog_id`) VALUES (?, ?)'; Dba::write($insertSql, array($beetsdb, $catalog_id)); return true; }
/** * create_type * * This creates a new catalog type entry for a catalog * It checks to make sure its parameters is not already used before creating * the catalog. */ public static function create_type($catalog_id, $data) { $uri = $data['uri']; $username = $data['username']; $password = $data['password']; if (substr($uri, 0, 7) != 'http://' && substr($uri, 0, 8) != 'https://') { Error::add('general', T_('Error: Remote selected, but path is not a URL')); return false; } if (!strlen($username) or !strlen($password)) { Error::add('general', T_('Error: Username and Password Required for Remote Catalogs')); return false; } $password = hash('sha256', $password); // Make sure this uri isn't already in use by an existing catalog $sql = 'SELECT `id` FROM `catalog_remote` WHERE `uri` = ?'; $db_results = Dba::read($sql, array($uri)); if (Dba::num_rows($db_results)) { debug_event('catalog', 'Cannot add catalog with duplicate uri ' . $uri, 1); Error::add('general', sprintf(T_('Error: Catalog with %s already exists'), $uri)); return false; } $sql = 'INSERT INTO `catalog_remote` (`uri`, `username`, `password`, `catalog_id`) VALUES (?, ?, ?, ?)'; Dba::write($sql, array($uri, $username, $password, $catalog_id)); return true; }
private static function check_session($code) { // Purge expired sessions $sql = "DELETE FROM `daap_session` WHERE `creationdate` < ?"; Dba::write($sql, array(time() - 1800)); self::check_auth($code); if (!isset($_GET['session-id'])) { debug_event('daap', 'Missing session id.', ''); } else { $sql = "SELECT * FROM `daap_session` WHERE `id` = ?"; $db_results = Dba::read($sql, array($_GET['session-id'])); if (Dba::num_rows($db_results) == 0) { debug_event('daap', 'Unknown session id `' . $_GET['session-id'] . '`.', '4'); } } }
/** * create_type * * This creates a new catalog type entry for a catalog * It checks to make sure its parameters is not already used before creating * the catalog. */ public static function create_type($catalog_id, $data) { // TODO: This Method should be required / provided by parent $uri = $data['uri']; if (substr($uri, 0, 7) != 'http://' && substr($uri, 0, 8) != 'https://') { AmpError::add('general', T_('Error: Beets selected, but path is not a URL')); return false; } // Make sure this uri isn't already in use by an existing catalog $selectSql = 'SELECT `id` FROM `catalog_beets` WHERE `uri` = ?'; $db_results = Dba::read($selectSql, array($uri)); if (Dba::num_rows($db_results)) { debug_event('catalog', 'Cannot add catalog with duplicate uri ' . $uri, 1); AmpError::add('general', sprintf(T_('Error: Catalog with %s already exists'), $uri)); return false; } $insertSql = 'INSERT INTO `catalog_beetsremote` (`uri`, `catalog_id`) VALUES (?, ?)'; Dba::write($insertSql, array($uri, $catalog_id)); return true; }
/** * create_type * * This creates a new catalog type entry for a catalog * It checks to make sure its parameters is not already used before creating * the catalog. */ public static function create_type($catalog_id, $data) { $apikey = $data['apikey']; $secret = $data['secret']; $path = $data['path']; $getchunk = $data['getchunk']; if (!strlen($apikey) or !strlen($secret)) { Error::add('general', T_('Error: API Key and Secret Required for Dropbox Catalogs')); return false; } $pathError = Dropbox\Path::findError($path); if ($pathError !== null) { Error::add('general', T_('Invalid <dropbox-path>: ' . $pathError)); return false; } // Make sure this app isn't already in use by an existing catalog $sql = 'SELECT `id` FROM `catalog_dropbox` WHERE `apikey` = ?'; $db_results = Dba::read($sql, array($apikey)); if (Dba::num_rows($db_results)) { debug_event('catalog', 'Cannot add catalog with duplicate key ' . $apikey, 1); Error::add('general', sprintf(T_('Error: Catalog with %s already exists'), $apikey)); return false; } $sql = 'INSERT INTO `catalog_dropbox` (`apikey`, `secret`, `path`, `getchunk`, `catalog_id`) VALUES (?, ?, ?, ?, ?)'; Dba::write($sql, array($apikey, $secret, $path, $getchunk ? 1 : 0, $catalog_id)); return true; }
/** * has_vote * This checks to see if the current user has already voted on this object */ public function has_vote($object_id, $type = 'song') { $tmp_id = Dba::escape($this->tmp_playlist); $object_id = Dba::escape($object_id); $type = Dba::escape($type); $user_id = Dba::escape($GLOBALS['user']->id); /* Query vote table */ $sql = 'SELECT `tmp_playlist_data`.`object_id` ' . 'FROM `user_vote` INNER JOIN `tmp_playlist_data` ' . 'ON `tmp_playlist_data`.`id`=`user_vote`.`object_id` ' . "WHERE `user_vote`.`user`='{$user_id}' " . "AND `tmp_playlist_data`.`object_type`='{$type}' " . "AND `tmp_playlist_data`.`object_id`='{$object_id}' " . "AND `tmp_playlist_data`.`tmp_playlist`='{$tmp_id}'"; $db_results = Dba::read($sql); /* If we find row, they've voted!! */ if (Dba::num_rows($db_results)) { return true; } return false; }
/** * exists * * This checks to see if the specified session of the specified type * exists * based on the type. */ public static function exists($type, $key) { // Switch on the type they pass switch ($type) { case 'api': case 'stream': $sql = 'SELECT * FROM `session` WHERE `id` = ? AND `expire` > ? ' . "AND `type` IN ('api', 'stream')"; $db_results = Dba::read($sql, array($key, time())); if (Dba::num_rows($db_results)) { return true; } break; case 'interface': $sql = 'SELECT * FROM `session` WHERE `id` = ? AND `expire` > ?'; if (AmpConfig::get('use_auth')) { // Build a list of enabled authentication types $types = AmpConfig::get('auth_methods'); $enabled_types = implode("','", $types); $sql .= " AND `type` IN('{$enabled_types}')"; } $db_results = Dba::read($sql, array($key, time())); if (Dba::num_rows($db_results)) { return true; } break; default: return false; } // Default to false return false; }
/** * create_type * * This creates a new catalog type entry for a catalog * It checks to make sure its parameters is not already used before creating * the catalog. */ public static function create_type($catalog_id, $data) { // Clean up the path just in case $path = rtrim(rtrim(trim($data['path']), '/'), '\\'); if (!strlen($path)) { AmpError::add('general', T_('Error: Path not specified')); return false; } // Make sure that there isn't a catalog with a directory above this one if (self::get_from_path($path)) { AmpError::add('general', T_('Error: Defined Path is inside an existing catalog')); return false; } // Make sure the path is readable/exists if (!Core::is_readable($path)) { debug_event('catalog', 'Cannot add catalog at unopenable path ' . $path, 1); AmpError::add('general', sprintf(T_('Error: %s is not readable or does not exist'), scrub_out($data['path']))); return false; } // Make sure this path isn't already in use by an existing catalog $sql = 'SELECT `id` FROM `catalog_local` WHERE `path` = ?'; $db_results = Dba::read($sql, array($path)); if (Dba::num_rows($db_results)) { debug_event('catalog', 'Cannot add catalog with duplicate path ' . $path, 1); AmpError::add('general', sprintf(T_('Error: Catalog with %s already exists'), $path)); return false; } $sql = 'INSERT INTO `catalog_local` (`path`, `catalog_id`) VALUES (?, ?)'; Dba::write($sql, array($path, $catalog_id)); return true; }
/** * install_check_status * this function checks to see if we actually * still need to install ampache. This function is * very important, we don't want to reinstall over top of an existing install */ function install_check_status($configfile) { /* Check and see if the config file exists if it does they can't use the web interface to install ampache. */ if (!file_exists($configfile)) { return true; } else { //Error::add('general', T_('Config file already exists, install is probably completed')); } /* Check and see if they've got _any_ account if they don't then they're cool */ $results = parse_ini_file($configfile); AmpConfig::set_by_array($results, true); if (!Dba::check_database()) { Error::add('general', T_('Unable to connect to database, check your ampache config')); return false; } $sql = 'SELECT * FROM `user`'; $db_results = Dba::read($sql); if (!$db_results) { Error::add('general', T_('Unable to query database, check your ampache config')); return false; } if (!Dba::num_rows($db_results)) { return true; } else { Error::add('general', T_('Existing Database detected, unable to continue installation')); return false; } }
/** * get_total * This returns the total number of objects for this current sort type. * If it's already cached used it. if they pass us an array then use * that. */ public function get_total($objects = null) { // If they pass something then just return that if (is_array($objects) and !$this->is_simple()) { return count($objects); } // See if we can find it in the cache if (isset($this->_state['total'])) { return $this->_state['total']; } $db_results = Dba::read($this->get_sql(false)); $num_rows = Dba::num_rows($db_results); $this->_state['total'] = $num_rows; return $num_rows; }
/** * exists * This just checks to see if a preference currently exists */ public static function exists($preference) { // We assume it's the name $name = Dba::escape($preference); $sql = "SELECT * FROM `preference` WHERE `name`='{$name}'"; $db_results = Dba::read($sql); return Dba::num_rows($db_results); }
/** * 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"; }
/** * has_vote * This checks to see if the current user has already voted on this object */ public function has_vote($object_id, $type = 'song') { $params = array($type, $object_id, $this->tmp_playlist); /* Query vote table */ $sql = 'SELECT `tmp_playlist_data`.`object_id` ' . 'FROM `user_vote` INNER JOIN `tmp_playlist_data` ' . 'ON `tmp_playlist_data`.`id`=`user_vote`.`object_id` ' . "WHERE `tmp_playlist_data`.`object_type` = ? " . "AND `tmp_playlist_data`.`object_id` = ? " . "AND `tmp_playlist_data`.`tmp_playlist` = ? "; if ($GLOBALS['user']->id > 0) { $sql .= "AND `user_vote`.`user` = ? "; $params[] = $GLOBALS['user']->id; } else { $sql .= "AND `user_vote`.`sid` = ? "; $params[] = session_id(); } $db_results = Dba::read($sql, $params); /* If we find row, they've voted!! */ if (Dba::num_rows($db_results)) { return true; } return false; }
/** * update_340005 * This update fixes the preferences types */ public static function update_340005() { $retval = true; $sql = "UPDATE `preference` SET `catagory`='playlist' WHERE `name`='random_method'"; $retval &= Dba::write($sql); $sql = "INSERT INTO `preference` (`name`,`value`,`description`,`level`,`type`,`catagory`) " . "VALUES ('transcode','default','Transcoding','25','string','streaming')"; $retval &= Dba::write($sql); /* We need to check for playlist_method here because I fubar'd an earlier update */ $sql = "SELECT * FROM `preference` WHERE `name`='playlist_method'"; $db_results = Dba::read($sql); if (!Dba::num_rows($db_results)) { /* Add the playlist_method preference and remove it from the user table */ $sql = "INSERT INTO `preference` (`name`,`value`,`description`,`level`,`type`,`catagory`) " . "VALUES ('playlist_method','default','Playlist Method','5','string','playlist')"; $retval &= Dba::write($sql); } // Add in the object_type to the tmpplaylist data table so that we can have non-songs in there $sql = "ALTER TABLE `tmp_playlist_data` ADD `object_type` VARCHAR( 32 ) NULL AFTER `tmp_playlist`"; $retval &= Dba::write($sql); return $retval; }
/** * create_type * * This creates a new catalog type entry for a catalog * It checks to make sure its parameters is not already used before creating * the catalog. */ public static function create_type($catalog_id, $data) { $userid = $data['userid']; $secret = $data['secret']; if (!strlen($userid) or !strlen($secret)) { AmpError::add('general', T_('Error: UserID and Secret Required for SoundCloud Catalogs')); return false; } // Make sure this email isn't already in use by an existing catalog $sql = 'SELECT `id` FROM `catalog_soundcloud` WHERE `userid` = ?'; $db_results = Dba::read($sql, array($userid)); if (Dba::num_rows($db_results)) { debug_event('catalog', 'Cannot add catalog with duplicate user id ' . $userid, 1); AmpError::add('general', sprintf(T_('Error: Catalog with %s already exists'), $userid)); return false; } $sql = 'INSERT INTO `catalog_soundcloud` (`userid`, `secret`, `catalog_id`) VALUES (?, ?, ?)'; Dba::write($sql, array($userid, $secret, $catalog_id)); return true; }
/** * save * * Save this search to the database for use as a smart playlist */ public function save() { // Make sure we have a unique name if (!$this->name) { $this->name = $GLOBALS['user']->username . ' - ' . date('Y-m-d H:i:s', time()); } $sql = "SELECT `id` FROM `search` WHERE `name` = ?"; $db_results = Dba::read($sql, array($this->name)); if (Dba::num_rows($db_results)) { $this->name .= uniqid('', true); } $sql = "INSERT INTO `search` (`name`, `type`, `user`, `rules`, `logic_operator`, `random`, `limit`) VALUES (?, ?, ?, ?, ?, ?, ?)"; Dba::write($sql, array($this->name, $this->type, $GLOBALS['user']->id, serialize($this->rules), $this->logic_operator, $this->random, $this->limit)); $insert_id = Dba::insert_id(); $this->id = $insert_id; return $insert_id; }
public static function auth_remember() { $auth = false; $cname = AmpConfig::get('session_name') . '_remember'; if (isset($_COOKIE[$cname])) { list($username, $token, $mac) = explode(':', $_COOKIE[$cname]); if ($mac === hash_hmac('sha256', $username . ':' . $token, AmpConfig::get('secret_key'))) { $sql = "SELECT * FROM `session_remember` WHERE `username` = ? AND `token` = ? AND `expire` >= ?"; $db_results = Dba::read($sql, array($username, $token, time())); if (Dba::num_rows($db_results) > 0) { Session::create_cookie(); self::create(array('type' => 'mysql', 'username' => $username)); $_SESSION['userdata']['username'] = $username; $auth = true; } } } return $auth; }