📄 ajaxacapplication.class.php
字号:
/**
* setConfigValue
*
* Add a parameter to the config. You can optionally choose to set it only
* if it doesn't already exist using the force parameter
*
* @access protected
* @param string $key The name of the config value to set
* @param mixed $val The value to set
* @param bool $force True to set the value no matter, false to set if doesn't exist
* @return bool True if value set, false if not (can only return false if $force is false or $key is empty)
*/
function setConfigValue($key, $val, $force = true)
{
if (strlen($key) > 0 && ($force || !array_key_exists($key, $this->_config))) {
$this->_config[$key] = $val;
return true;
}
return false;
}
/**
* getConfigValue
*
* Retrieve a parameter from the application config. You can choose to return
* some default value if the parameter is null or not set
*
* @access protected
* @param string $key The name of the config value to return
* @param mixed $default A default value to return if the key doesn't exist or is null
* @return mixed The fetched or default value
*/
function getConfigValue($key, $default = null)
{
if (!isset($this->_config) || !array_key_exists($key, $this->_config) || is_null($this->_config[$key]))
return $default;
return $this->_config[$key];
}
/**
* sendResponseData
*
* Send some data back as a response to a HTTP subrequest. Can be in various
* data formats, each of which treat the data different and send headers
* accordingly. The script exits after this method is called.
*
* @todo Cache control functionality
* @todo Move actual data output / headers into separate section so can be used elsewhere
* @param string $type The type of data being sent
* @param mixed $data The data to return
*/
function sendResponseData($type, $data)
{
$type = strtolower($type);
// check if there's a handler for this data type. if not
// just assume it's plain text and send the data as is with a text/plain
// mime type.
$callback = 'response_' . $type;
if (method_exists($this, $callback)) {
$response = $this->$callback($data);
// the returned data should be an array with a 'mime' elements
// and a 'data' element. if not an array, then the returned data
// is output. if no mime type found then text/plain is used
if (is_array($response)) {
if (isset($response['mime']))
$mime = $response['mime'];
if (isset($response['data']))
$data = $response['data'];
else
$data = '';
}
else {
$mime = 'text/plain';
$data = $response;
}
}
else
$mime = 'text/plain';
// create array of all headers to be output
$headers = array('Content-type: ' . $this->getContentType($mime),
'Content-length: ' . strlen($data));
// output each http header
foreach ($headers as $header)
header($header);
echo $data;
exit;
}
/**
* response_xml
*
* Outputs XML data. Assumes it is receiving well-formed XML
*
* @param mixed $data The data to return
* @return array A reponse type array, with mime and data elements
*/
function response_xml($data)
{
return array('mime' => 'text/xml',
'data' => $data);
}
/**
* response_jsarray
*
* Handle the jsarray response type
*
* @param mixed $data The data to return
* @return array A reponse type array, with mime and data elements
*/
function response_jsarray($data)
{
return array('mime' => 'text/javascript',
'data' => $this->_phpArrayToJs($data));
}
/**
* _phpArrayToJs
*
* Helper function for jsarray response type
*/
function _phpArrayToJs($arr)
{
$items = array();
foreach ($arr as $k => $v) {
if (is_array($v))
$items[] = $this->_phpArrayToJs($v);
else if (is_int($v))
$items[] = $v;
else
$items[] = "'" . $this->escapeJs($v) . "'";
}
return '[' . join(',', $items) . ']';
}
/**
* escapeJs
*
* Make a string JavaScript-safe so errors are not generated. This code was
* shamelessly borrowed from Smarty
*
* @access protected
* @param string $str The string to escape
* @return string The escaped string
*/
function escapeJs($str)
{
// borrowed from smarty
return strtr($str, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
}
/**
* escapeXml
*
* Make a string XML safe so all entities are correctly transposed to
* produce valid XML. This should be used inside attributes and for CDATA
*
* @access protected
* @param string $str The string to escape
* @return string The escaped string
*/
function escapeXml($str)
{
static $trans;
if (!isset($trans)) {
$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $key => $value)
$trans[$key] = '&#'.ord($key).';';
// dont translate the '&' in case it is part of &xxx;
$trans[chr(38)] = '&';
}
return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/","&" , strtr($str, $trans));
}
/**
* getContentType
*
* Determine the full content type value for the given mime type. At this
* stage this involves appending the appropriate charset definition for
* text/* mime types
*
* @param string $type The mime type to rewrite
* @return string The rewritten mime type
*/
function getContentType($type)
{
$type = trim($type);
if (preg_match('|^text/|i', $type)) {
$charset = $this->getConfigValue('charset', $this->_defaultCharset);
$type .= '; charset=' . $charset;
}
return $type;
}
/**
* debug
*
* Write a debug message to the application log file, if the debug level
* matches the runtime debug level
*
* @param int $level The debug level this message is for
* @param string $str The string to write to the debug file
*/
function debug($level, $str)
{
if (($level & $this->getConfigValue('debug.level')) && !$this->getConfigValue('debug.abort')) {
if (!isset($this->_debug_fp)) {
$this->_debug_fp = @fopen($this->getConfigValue('debug.log_file'), 'a+');
if (!$this->_debug_fp) {
$this->setConfigValue('debug.abort', true);
return;
}
fwrite($this->_debug_fp, "\n\n\n\n\n[" . date('Y-m-d H:i:s') . "] -- MARK --\n");
}
fwrite($this->_debug_fp, '[' . date('Y-m-d H:i:s') . '] ' . $str . "\n");
}
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -