Created
December 16, 2009 14:16
-
-
Save tautologistics/257847 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('CONFIG_PATHS', 'path'); | |
define('CONFIG_URLS', 'url'); | |
define('CONFIG_DATABASE', 'db'); | |
define('CONFIG_LOGGING', 'log'); | |
class Config { | |
private static $instance = null; | |
private $storage; | |
function __construct () { | |
$this->storage = array(); | |
} | |
public static function GetInstance () { | |
if (self::$instance === null) { | |
$className = __CLASS__; | |
self::$instance = new $className(); | |
} | |
return(self::$instance); | |
} | |
function setVal ($sNameSpace, $sKey, $sVal) { | |
if (is_null($sKey)) | |
$this->storage[$sNameSpace][] = $sVal; | |
else | |
$this->storage[$sNameSpace][$sKey] = $sVal; | |
} | |
function getVal ($sNameSpace, $sKey) { | |
if (!array_key_exists($sNameSpace, $this->storage)) return ''; | |
if (!array_key_exists($sKey, $this->storage[$sNameSpace])) return ''; | |
return $this->storage[$sNameSpace][$sKey]; | |
} | |
function getVals ($sNameSpace) { | |
if (!array_key_exists($sNameSpace, $this->storage)) return array(); | |
return array_keys($this->storage[$sNameSpace]); | |
} | |
function hasVal ($sNameSpace, $sKey) { | |
return array_key_exists($sKey, $this->storage[$sNameSpace]); | |
} | |
function delVal ($sNameSpace, $sKey) { | |
unset($this->storage[$sNameSpace][$sKey]); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("Config.php"); | |
require_once("Logging.php"); | |
class Database { | |
private static $instances = array(); | |
private static $debug_global = false; | |
private $debug_local = false; | |
private $server; | |
private $schema; | |
private $login; | |
private $password; | |
private $connection; | |
public function __construct ($server, $schema, $login, $password) { | |
$this->server = $server; | |
$this->schema = $schema; | |
$this->login = $login; | |
$this->password = $password; | |
$this->Connect(); | |
} | |
public function __destruct () { | |
$this->Close(); | |
} | |
public function SetDebug ($value) { | |
if (isset($this)) { | |
$this->debug_local = $value; | |
} | |
else { | |
self::$debug_global = $value; | |
} | |
} | |
public static function GetInstance ($server = null, $schema = null, $login = null, $password = null) { | |
$config = Config::GetInstance(); | |
if ($server === null) | |
$server = $config->getVal(CONFIG_DATABASE, 'server'); | |
if ($schema === null) | |
$schema = $config->getVal(CONFIG_DATABASE, 'schema'); | |
if ($login === null) | |
$login = $config->getVal(CONFIG_DATABASE, 'login'); | |
if ($password === null) | |
$password = $config->getVal(CONFIG_DATABASE, 'password'); | |
$key = '|'.$server.'|'.$schema.'|'.$login.'|'.$password.'|'; | |
if (!isset(self::$instances[$key])) { | |
$className = __CLASS__; | |
self::$instances[$key] = new $className($server, $schema, $login, $password); | |
} | |
return(self::$instances[$key]); | |
} | |
public static function Instances () { | |
return(array_keys(self::$instances)); | |
} | |
public function Escape ($value) { | |
return(mysql_real_escape_string($value)); | |
} | |
public function Quote ($value) { | |
return("'".$this->Escape($value)."'"); | |
} | |
public function SmartQuote ($value) { | |
return(is_numeric($value) ? | |
$this->Escape($value) | |
: | |
"'".$this->Escape($value)."'" | |
); | |
} | |
public function QuoteMap ($value) { | |
$result = ""; | |
if (is_array($value)) { | |
$escaped = array(); | |
foreach ($value as $entry) { | |
$escaped[] = $this->SmartQuote($entry); | |
} | |
$result = implode(', ', $escaped); | |
} | |
else { | |
$result = $this->Escape($value); | |
} | |
return($result); | |
} | |
public function Connect() { | |
$this->connection = @mysql_connect($this->server, $this->login, $this->password); | |
if ($this->connection === false) { | |
Logging::GetInstance()->Log("Connection to DB server failed (" . mysql_error() . ")", PEAR_LOG_CRIT); | |
return(false); | |
} | |
if (!@mysql_select_db($this->schema, $this->connection)) { | |
Logging::GetInstance()->Log( | |
"Opening of DB schema failed (" . $this->schema . ", #" . @mysql_errno($this->connection) . ": " . @mysql_error($this->connection) . ")", | |
PEAR_LOG_CRIT | |
); | |
return(false); | |
} | |
return(true); | |
} | |
public function Close () { | |
mysql_close($this->connection); | |
} | |
public function Prepare ($query) { | |
return(new DatabaseQuery($query, $this)); | |
} | |
public function ParseQuery () { | |
$params = func_get_args(); | |
$query = array_shift($params); | |
$escapedParams = array(); | |
foreach ($params as $param) { | |
$escapedParams[] = $this->QuoteMap($param); | |
} | |
array_unshift($escapedParams, $query); | |
return($this->Query(call_user_func_array("sprintf", $escapedParams))); | |
} | |
public function Query ($query) { | |
if (self::$debug_global || $this->debug_local) { | |
Logging::GetInstance()->Log("Database::Query(): ".$query, PEAR_LOG_DEBUG); | |
} | |
$result = mysql_query($query, $this->connection); | |
if ($result === false) { | |
Logging::GetInstance()->Log("Problem encountered while executing the query. (" . $query . ")", PEAR_LOG_ERR); | |
return(false); | |
} | |
return(new DatabaseResult($result, $this->LastInsertID())); | |
} | |
public function ReturnedRows ($result) { | |
return(mysql_num_rows($result)); | |
} | |
public function AffectedRows ($result) { | |
return(mysql_affected_rows($result)); | |
} | |
public function GetRow (&$result) { | |
return(mysql_fetch_array($result)); | |
} | |
public function GetAllRows (&$result) { | |
$rows = array(); | |
if ($this->ReturnedRows($result) > 0) { | |
while ($row = $this->GetRow($result)) { | |
$rows[] = $row; | |
} | |
} | |
return($rows); | |
} | |
public function LastInsertID () { | |
return(mysql_insert_id($this->connection)); | |
} | |
} | |
class DatabaseQuery { | |
private $query; | |
private $connection; | |
public function __construct ($query, Database $connection) { | |
$this->query = $query; | |
$this->connection = $connection; | |
} | |
public function Exec () { | |
$params = func_get_args(); | |
array_unshift($params, $this->query); | |
return(call_user_func_array(array($this->connection, 'ParseQuery'), $params)); | |
} | |
} | |
class DatabaseResult { | |
private $result; | |
private $lastId; | |
private $numRows = null; | |
private $affectedRows = null; | |
public function __construct ($result, $lastId = -1) { | |
$this->result = $result; | |
$this->lastId = $lastId; | |
} | |
public function ReturnedRows () { | |
if ($this->numRows == null) { | |
$this->numRows = Database::ReturnedRows($this->result); | |
} | |
return($this->numRows); | |
} | |
public function AffectedRows () { | |
if ($this->affectedRows == null) { | |
$this->affectedRows = Database::AffectedRows($this->result); | |
} | |
return($this->affectedRows); | |
} | |
public function GetRow () { | |
return(Database::GetRow($this->result)); | |
} | |
public function GetAllRows () { | |
return(Database::GetAllRows($this->result)); | |
} | |
public function LastInsertID () { | |
return($this->lastId); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("HTTP/Request.php"); | |
class EveryBit { | |
const URL = "https://alpha.nokoscope.com/eb2/get?"; | |
const URL_VERSION = "ver=2.0"; | |
const URL_USER = "user"; | |
const URL_TOKEN = "token"; | |
const URL_TYPE = "type"; | |
const URL_PAGESIZE = "limit"; | |
const URL_PAGEOFFSET = "offset"; | |
const URL_NAMES = "names"; | |
const OPT_PAGESIZE_ALL = -1; | |
const OPT_PAGESIZE_DEFAULT = null; | |
const OPT_TYPE_ALL = null; | |
const OPT_TYPE_BLUETOOTH = "bt"; | |
const OPT_TYPE_CONTACT = "contacts"; | |
const OPT_TYPE_CALLS = "call_log"; | |
const OPT_TYPE_EVERYBIT = "eb"; | |
const OPT_TYPE_GPS = "gps"; | |
const OPT_TYPE_CELLTOWER = "gsm"; | |
const OPT_TYPE_MEDIA = "media_play"; | |
const OPT_TYPE_SMS = "sms"; | |
const OPT_TYPE_DEVICE = "sys"; | |
const OPT_TYPE_TAGS = "tags"; | |
const OPT_TYPE_WIFI = "wlan"; | |
const RESULT_OK = 200; | |
const RESULT_BAD_REQUEST = 400; | |
const RESULT_NOT_AUTHORIZED = 401; | |
const RESULT_SERVER_ERROR = 500; | |
const RESULT_CONNECT_FAILED = 503; | |
private $user = ""; | |
private $pass = ""; | |
// TODO: Use PEAR::http_request if available or log info and fallback to raw sockets | |
function __construct ($user, $pass) { | |
$this->user = $user; | |
$this->pass = $pass; | |
} | |
function Get ($type = self::OPT_TYPE_ALL, $names = null, $pageSize = null, $pageOffset = null) { | |
if (is_array($type)) { | |
$type = implode(' ' , $type); | |
} | |
if (is_array($names)) { | |
$names = implode(' ' , $names); | |
} | |
if (!is_numeric($pageSize)) { | |
$pagSize = null; | |
} elseif ($pageSize === self::OPT_PAGESIZE_ALL) { | |
$pageSize = "all"; | |
} | |
if (!is_numeric($pageOffset)) { $pagSize = null; } | |
$url = $this->Url(); | |
if (!is_null($type)) { $url .= "&".self::URL_TYPE."=".urlencode($type); } | |
if (!is_null($names)) { $url .= "&".self::URL_NAMES."=".urlencode($names); } | |
if (!is_null($pageSize)) { $url .= "&".self::URL_PAGESIZE."=".$pageSize; } | |
if (!is_null($pageOffset)) { $url .= "&".self::URL_PAGEOFFSET."=".urlencode($pageOffset); } | |
// echo($url); | |
$request = new HTTP_Request($url, | |
array( | |
"method" => "GET", | |
"http" => "1.1" | |
)); | |
$request->sendRequest(); | |
$response = array("resultCode" => $request->getResponseCode(), "resultMsg" => "", "records" => array()); | |
if ($response["resultCode"] === false) { | |
$response["resultCode"] = self::RESULT_CONNECT_FAILED; | |
$response["resultMsg"] = "Connection failed"; | |
} | |
elseif ($response["resultCode"] !== self::RESULT_OK) { | |
$response["resultMsg"] = $request->getResponseBody(); | |
} | |
else { | |
$response["records"] = is_null($names) ? | |
array($this->user => json_decode($request->getResponseBody())) | |
: | |
json_decode($request->getResponseBody()) | |
; | |
} | |
return($response); | |
} | |
function Url () { | |
$url = self::URL.self::URL_VERSION; | |
$url .= "&".self::URL_USER."=".urlencode($this->user); | |
$url .= "&".self::URL_TOKEN."=".urlencode($this->Password()); | |
return($url); | |
} | |
//private | |
function Password () { | |
return(sha1($this->pass)); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (strpos(strtolower($_SERVER['SCRIPT_FILENAME']), strtolower(__FILE__))) { | |
header('Location: ./'); | |
exit(); | |
} | |
/* [PRODUCTION] | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'server', 'localhost:3306'); | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'schema', 'prod_db'); | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'login', 'prod_user'); | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'password', 'prod_pass'); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'type', 'file'); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'target', '/logs/sampleapp.web.{{date}}.log'); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'level', "PEAR_LOG_ERR"); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'notify_level', (E_ERROR | E_WARNING)); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'halt_level', (E_ERROR)); | |
/*/// [DEVELOPMENT] | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'server', 'localhost:3306'); | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'schema', 'dev_db'); | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'login', 'dev_user'); | |
Config::GetInstance()->setVal(CONFIG_DATABASE, 'password', 'dev_pass'); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'type', 'file'); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'target', "/logs/sampleapp.web.{{date}}.log"); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'level', "PEAR_LOG_DEBUG"); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'notify_level', (E_ALL | E_STRICT)); | |
Config::GetInstance()->setVal(CONFIG_LOGGING, 'halt_level', (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_USER_WARNING)); | |
//*/ | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("Config.php"); | |
require_once("Log.php"); | |
require_once("FirePHP.php"); | |
class Logging { | |
private static $instance = null; | |
private static $instanceName = ""; | |
private static $priorityMap = array( | |
PEAR_LOG_EMERG => 'Emergency' | |
, PEAR_LOG_ALERT => 'Alert' | |
, PEAR_LOG_CRIT => 'Critical' | |
, PEAR_LOG_ERR => 'Error' | |
, PEAR_LOG_WARNING => 'Warning' | |
, PEAR_LOG_NOTICE => 'Notice' | |
, PEAR_LOG_INFO => 'Info' | |
, PEAR_LOG_DEBUG => 'Debug' | |
); | |
private $log = null; | |
function __construct () { | |
$this->log = &Log::singleton( | |
Config::GetInstance()->getVal(CONFIG_LOGGING, 'type'), | |
// self::ParseFilename( | |
// Config::GetInstance()->getVal(CONFIG_PATHS, 'root'). | |
// Config::GetInstance()->getVal(CONFIG_LOGGING, 'target') | |
// ), | |
self::GetFilename(), | |
null, | |
array()//, | |
//constant(Config::GetInstance()->getVal(CONFIG_LOGGING, 'level')) | |
); | |
//$this->log->setMask(constant(Config::GetInstance()->getVal(CONFIG_LOGGING, 'level'))); | |
} | |
// TODO: track changes to the filename | |
public static function GetInstance () { | |
if (self::$instance === null || self::$instanceName !== self::GetFilename()) { | |
$className = __CLASS__; | |
self::$instance = new $className(); | |
self::$instanceName = self::GetFilename(); | |
} | |
return(self::$instance); | |
} | |
public function Log ($msg, $priority = PEAR_LOG_DEBUG) { | |
@FirePHP::getInstance(true)->fb($msg, self::MapToFirePHPLogLevel($priority)); | |
return($this->log->log($msg, $priority)); | |
} | |
public static function MapFromPHPErrorLevel ($level) { | |
$logLevel; | |
switch ($level) { | |
case E_ERROR: | |
case E_USER_ERROR: | |
case E_COMPILE_ERROR: | |
$logLevel = PEAR_LOG_ERR; | |
break; | |
case E_WARNING: | |
case E_USER_WARNING: | |
case E_COMPILE_WARNING: | |
$logLevel = PEAR_LOG_WARNING; | |
break; | |
case E_NOTICE: | |
case E_USER_NOTICE: | |
default: | |
$logLevel = PEAR_LOG_INFO; | |
} | |
return($logLevel); | |
} | |
protected static function MapToFirePHPLogLevel ($priority) { | |
$logLevel; | |
switch ($priority) { | |
case PEAR_LOG_EMERG: | |
case PEAR_LOG_ALERT: | |
case PEAR_LOG_CRIT: | |
case PEAR_LOG_ERR: | |
$logLevel = FirePHP::ERROR; | |
break; | |
case PEAR_LOG_WARNING: | |
$logLevel = FirePHP::WARNING; | |
break; | |
case PEAR_LOG_NOTICE: | |
case PEAR_LOG_INFO: | |
case PEAR_LOG_DEBUG: | |
default: | |
$logLevel = FirePHP::INFO; | |
break; | |
} | |
return($logLevel); | |
} | |
protected static function ParseFilename ($filename) { | |
return(str_replace("{{date}}", date("Y.m.d"), $filename)); | |
} | |
protected static function GetFilename () { | |
return(self::ParseFilename( | |
Config::GetInstance()->getVal(CONFIG_PATHS, 'root'). | |
Config::GetInstance()->getVal(CONFIG_LOGGING, 'target') | |
)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment