summaryrefslogtreecommitdiffstats
path: root/lib/class/dba.class.php
diff options
context:
space:
mode:
authorPaul Arthur <paul.arthur@flowerysong.com>2013-05-27 20:32:02 -0400
committerPaul Arthur <paul.arthur@flowerysong.com>2013-05-27 20:32:02 -0400
commitee85e4d08ddb7e2ec03904c0893cf3dd464125fb (patch)
tree8b0065dc408466724ed93c726bf10dabe1a1b5d0 /lib/class/dba.class.php
parent914c903250de4837d3bf6e77e496c2a3ece5f638 (diff)
downloadampache-ee85e4d08ddb7e2ec03904c0893cf3dd464125fb.tar.gz
ampache-ee85e4d08ddb7e2ec03904c0893cf3dd464125fb.tar.bz2
ampache-ee85e4d08ddb7e2ec03904c0893cf3dd464125fb.zip
Change Dba::error() and audit its callers
Will hopefully return more useful information during installation, which is the only place it's actually used and useful.
Diffstat (limited to 'lib/class/dba.class.php')
-rw-r--r--lib/class/dba.class.php12
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/class/dba.class.php b/lib/class/dba.class.php
index 55608f76..96b313bc 100644
--- a/lib/class/dba.class.php
+++ b/lib/class/dba.class.php
@@ -39,6 +39,7 @@ class Dba {
public static $stats = array('query'=>0);
private static $_sql;
+ private static $_error;
private static $config;
/**
@@ -89,10 +90,12 @@ class Dba {
self::$stats['query']++;
if (!$stmt) {
+ self::$_error = json_encode($dbh->errorInfo());
debug_event('Dba', 'Error: ' . json_encode($dbh->errorInfo()), 1);
self::disconnect();
}
else if ($stmt->errorCode() && $stmt->errorCode() != '00000') {
+ self::$_error = json_encode($stmt->errorInfo());
debug_event('Dba', 'Error: ' . json_encode($stmt->errorInfo()), 1);
self::disconnect();
return false;
@@ -248,6 +251,7 @@ class Dba {
$dbh = new PDO($dsn, $username, $password);
}
catch (PDOException $e) {
+ self::$_error = $e->getMessage();
debug_event('Dba', 'Connection failed: ' . $e->getMessage(), 1);
return null;
}
@@ -267,6 +271,7 @@ class Dba {
}
if ($dbh->exec('USE `' . $database . '`') === false) {
+ self::$_error = json_encode($dbh->errorInfo());
debug_event('Dba', 'Unable to select database ' . $database . ': ' . json_encode($dbh->errorInfo()), 1);
}
@@ -286,6 +291,7 @@ class Dba {
$dbh = self::_connect();
if (!$dbh || $dbh->errorCode()) {
+ self::$_error = json_encode($dbh->errorInfo());
return false;
}
@@ -390,11 +396,7 @@ class Dba {
* this returns the error of the db
*/
public static function error() {
- $dbh = self::dbh();
- if ($dbh) {
- return $dbh->errorCode();
- }
- return false;
+ return self::$_error;
}
/**
ht .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<?php
/*

 Copyright (c) 2001 - 2007 Ampache.org
 All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License v2
 as published by the Free Software Foundation.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/**
 * xmlRpcServer
 * This class contains all the methods that the /server/xmlrpc.server.php will respond to 
 * to add a new method, just define a new public static function in here and it will be automagicaly
 * populated to xmlrpcserver.<FUNCTION> in /server/xmlrpc.server.php 
 */

class xmlRpcServer {

	/**
 	 * get_catalogs
	 * This returns a list of the current non-remote catalogs hosted on this Ampache instance
	 * It requires a key be passed as the first element
	 * //FIXME: USE TOKEN!
	 */
	public static function get_catalogs($xmlrpc_object) { 

		// Pull out the key
		$variable = $xmlrpc_object->getParam(0); 
		$key = $variable->scalarval(); 

		// Check it and make sure we're super green
		if (!vauth::session_exists('xml-rpc',$key)) { 
			debug_event('XMLSERVER','Error ' . $_SERVER['REMOTE_ADDR'] . ' with key ' . $key . ' does not match any ACLs','1'); 
			return new xmlrpcresp(0,'503','Key/IP Mis-match Access Denied'); 
		} 

		// Go ahead and gather up the information they are legit
		$results = array(); 

		$sql = "SELECT `catalog`.`name`,COUNT(`song`.`id`) AS `count`,`catalog`.`id` AS `catalog_id` FROM `catalog` ". 
			"LEFT JOIN `song` ON `catalog`.`id`=`song`.`catalog` WHERE `catalog`.`catalog_type`='local' " . 
			"GROUP BY `catalog`.`id`"; 
		$db_results = Dba::query($sql); 

		while ($row = Dba::fetch_assoc($db_results)) { 
			$results[] = $row; 
		} 

		// We need to set time limit at this point as who know how long this data is going to take
		// to return to the client
		set_time_limit(0); 

		$encoded_array = php_xmlrpc_encode($results); 
		debug_event('XMLSERVER','Returning data about ' . count($results) . ' catalogs to ' . $_SERVER['REMOTE_ADDR'],'5'); 

		return new xmlrpcresp($encoded_array); 

	} // get_catalogs

	/**
	 * get_songs
	 * This is a basic function to return all of the song data in a serialized format. It takes a start and end point
	 * as well as the TOKEN for auth mojo
	 * //FIXME: USE TOKEN!
  	 */
	public static function get_songs($xmlrpc_object) { 

		// We're going to be here a while
		set_time_limit(0); 

                // Pull out the key
                $variable = $xmlrpc_object->getParam(0);
                $key = $variable->scalarval();

                // Check it and make sure we're super green
                if (!vauth::session_exists('xml-rpc',$key)) {
                        debug_event('XMLSERVER','Error ' . $_SERVER['REMOTE_ADDR'] . ' with key ' . $key . ' does not match any ACLs','1');
                        return new xmlrpcresp(0,'503','Key/IP Mis-match Access Denied');
                }
		
		// Now pull out the start and end
		$start	= intval($xmlrpc_object->params['1']->me['int']); 
		$end	= intval($xmlrpc_object->params['2']->me['int']);

		// Get Catalogs first
		$sql = "SELECT `catalog`.`id` FROM `catalog` WHERE `catalog`.`catalog_type`='local'"; 
		$db_results = Dba::query($sql); 

		while ($row = Dba::fetch_assoc($db_results)) { 
			$where_sql .= "`song`.`catalog`='" . $row['id'] . "' OR"; 
		} 

		$where_sql = rtrim($where_sql,'OR'); 

		$sql = "SELECT `song`.`id` FROM `song` WHERE `song`.`enabled`='1' AND ($where_sql) LIMIT $start,$end"; 
		$db_results = Dba::query($sql); 

		while ($row = Dba::fetch_assoc($db_results)) { 
			$song = new Song($row['id']); 
			$song->fill_ext_info(); 
			$song->album	= $song->get_album_name(); 
			$song->artist	= $song->get_artist_name(); 
			$song->genre	= $song->get_genre_name(); 

			$output = serialize($song); 
			$results[] = $output; 
		} // end while

		$encoded_array = php_xmlrpc_encode($results); 
		debug_event('XMLSERVER','Encoded ' . count($results) . ' songs (' . $start . ',' . $end . ')','5'); 

		return new xmlrpcresp($encoded_array);

	} // get_songs

	/**
	 * handshake
	 * This should be run before any other XMLRPC actions, it checks the KEY encoded with a timestamp then returns a valid TOKEN to be
	 * used in all further communication 
	 */
	public static function handshake($xmlrpc_object) { 

		// Pull out the params
		$encoded_key 	= $xmlrpc_object->params['0']->me['string']; 
		$timestamp	= $xmlrpc_object->params['1']->me['int'];

		// Check the timestamp make sure it's recent
		if ($timestamp < (time() - 14400)) { 
			debug_event('XMLSERVER','Handshake failure, timestamp too old','1'); 
			return new xmlrpcresp(0,'503','Handshaek failure, timestamp too old');
		} 

		// Log the attempt
		debug_event('XMLSERVER','Login Attempt, IP: ' . $_SERVER['REMOTE_ADDR'] . ' Time: ' . $timestamp . ' Hash:' . $encoded_key,'5'); 

		// Convert the IP Address to an int
		$ip = ip2int($_SERVER['REMOTE_ADDR']); 

		// Run the query and return the key's for ACLs of type RPC that would match this IP 
		$sql = "SELECT * FROM `access_list` WHERE `type`='rpc' AND `start` <= '$ip' AND `end` >= '$ip'"; 
		$db_results = Dba::query($sql);

		while ($row = Dba::fetch_assoc($db_results)) { 
			
			// Build our encoded passphrase
			$md5pass = md5($timestamp . $row['key']); 

			if ($md5pass == $encoded_key) { 
				$data['type'] = 'xml-rpc';
				$data['username'] = 'System'; 
				$data['value'] = 'Handshake'; 
				$token = vauth::session_create($data); 
				return new xmlrpcresp(php_xmlrpc_encode($token));
			} 

		} // end while rows

		return new xmlrpcresp(0,'503','Handshaek failure, Key/IP Incorrect');

	} // handshake

} // xmlRpcServer
?>