-catch all exceptions in Preference model and make header 503

This commit is contained in:
Martin Konecny 2012-05-05 22:29:16 -04:00
parent 5204da4502
commit 1f214747b2

View file

@ -4,101 +4,108 @@ class Application_Model_Preference
{ {
public static function SetValue($key, $value, $isUserValue = false){ public static function SetValue($key, $value, $isUserValue = false){
global $CC_CONFIG;
$con = Propel::getConnection();
//called from a daemon process
if(!class_exists("Zend_Auth", false) || !Zend_Auth::getInstance()->hasIdentity()) {
$id = NULL;
}
else {
$auth = Zend_Auth::getInstance();
$id = $auth->getIdentity()->id;
}
$key = pg_escape_string($key);
$value = pg_escape_string($value);
//Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref"
." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if($isUserValue) {
$sql .= " AND subjid = '$id'";
}
$result = $con->query($sql)->fetchColumn(0);
if($value == "") {
$value = "NULL";
}else {
$value = "'$value'";
}
if($result == 1) {
// result found
if(is_null($id) || !$isUserValue) {
// system pref
$sql = "UPDATE cc_pref"
." SET subjid = NULL, valstr = $value"
." WHERE keystr = '$key'";
} else {
// user pref
$sql = "UPDATE cc_pref"
. " SET valstr = $value"
. " WHERE keystr = '$key' AND subjid = $id";
}
} else {
// result not found
if(is_null($id) || !$isUserValue) {
// system pref
$sql = "INSERT INTO cc_pref (keystr, valstr)"
." VALUES ('$key', $value)";
} else {
// user pref
$sql = "INSERT INTO cc_pref (subjid, keystr, valstr)"
." VALUES ($id, '$key', $value)";
}
}
try { try {
$con = Propel::getConnection();
//called from a daemon process
if(!class_exists("Zend_Auth", false) || !Zend_Auth::getInstance()->hasIdentity()) {
$id = NULL;
}
else {
$auth = Zend_Auth::getInstance();
$id = $auth->getIdentity()->id;
}
$key = pg_escape_string($key);
$value = pg_escape_string($value);
//Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref"
." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if($isUserValue) {
$sql .= " AND subjid = '$id'";
}
$result = $con->query($sql)->fetchColumn(0);
if($value == "") {
$value = "NULL";
}else {
$value = "'$value'";
}
if($result == 1) {
// result found
if(is_null($id) || !$isUserValue) {
// system pref
$sql = "UPDATE cc_pref"
." SET subjid = NULL, valstr = $value"
." WHERE keystr = '$key'";
} else {
// user pref
$sql = "UPDATE cc_pref"
. " SET valstr = $value"
. " WHERE keystr = '$key' AND subjid = $id";
}
} else {
// result not found
if(is_null($id) || !$isUserValue) {
// system pref
$sql = "INSERT INTO cc_pref (keystr, valstr)"
." VALUES ('$key', $value)";
} else {
// user pref
$sql = "INSERT INTO cc_pref (subjid, keystr, valstr)"
." VALUES ($id, '$key', $value)";
}
}
$con->exec($sql); $con->exec($sql);
} catch (Exception $e){ } catch (Exception $e){
Logging::log("Could not connect to database.");
header('HTTP/1.0 503 Service Unavailable'); header('HTTP/1.0 503 Service Unavailable');
Logging::log("Could not connect to database.");
exit; exit;
} }
} }
public static function GetValue($key, $isUserValue = false){ public static function GetValue($key, $isUserValue = false){
global $CC_CONFIG; try {
$con = Propel::getConnection(); $con = Propel::getConnection();
//Check if key already exists //Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref" $sql = "SELECT COUNT(*) FROM cc_pref"
." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if ($isUserValue) {
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()) {
$id = $auth->getIdentity()->id;
$sql .= " AND subjid = '$id'";
}
}
$result = $con->query($sql)->fetchColumn(0);
if ($result == 0)
return "";
else {
$sql = "SELECT valstr FROM cc_pref"
." WHERE keystr = '$key'"; ." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
//For user specific preference, check if id matches as well if ($isUserValue) {
if($isUserValue && $auth->hasIdentity()) { $auth = Zend_Auth::getInstance();
$sql .= " AND subjid = '$id'"; if($auth->hasIdentity()) {
} $id = $auth->getIdentity()->id;
$sql .= " AND subjid = '$id'";
}
}
$result = $con->query($sql)->fetchColumn(0); $result = $con->query($sql)->fetchColumn(0);
return ($result !== false) ? $result : ""; if ($result == 0)
return "";
else {
$sql = "SELECT valstr FROM cc_pref"
." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if($isUserValue && $auth->hasIdentity()) {
$sql .= " AND subjid = '$id'";
}
$result = $con->query($sql)->fetchColumn(0);
return ($result !== false) ? $result : "";
}
} catch (Exception $e) {
header('HTTP/1.0 503 Service Unavailable');
Logging::log("Could not connect to database.");
exit;
} }
} }
@ -567,10 +574,12 @@ class Application_Model_Preference
public static function GetAirtimeVersion(){ public static function GetAirtimeVersion(){
if (defined('APPLICATION_ENV') && APPLICATION_ENV == "development" && function_exists('exec')){ if (defined('APPLICATION_ENV') && APPLICATION_ENV == "development" && function_exists('exec')){
return self::GetValue("system_version")."+".exec("git rev-parse --short HEAD"); $version = exec("git rev-parse --short HEAD 2>/dev/null", $out, $return_code);
} else { if ($return_code == 0){
return self::GetValue("system_version"); return self::GetValue("system_version")."+".$version;
}
} }
return self::GetValue("system_version");
} }
public static function GetLatestVersion(){ public static function GetLatestVersion(){