📄 server.php
字号:
}
// if were not in combined output compare etags, if method returns true were done
if (!$combinedOutput && isset($stubETag)) {
if ($this->_compareEtags($stubETag)) {
ob_end_clean();
return;
}
}
// output the stubs for all the classes in our list
foreach($classList as $class) {
echo $this->ajax->generateClassStub($class);
}
// if were cacheing and the rule expects content make a tag and check it, if the check is true were done
if ($this->cacheOptions['httpCacheStub'] &&
$this->cacheOptions['StubCacheExpects'] == 'content')
{
$stubETag = $this->_callCacheRule('Stub',ob_get_contents());
}
// if were not in combined output compare etags, if method returns true were done
if (!$combinedOutput && isset($stubETag)) {
if ($this->_compareEtags($stubETag)) {
ob_end_clean();
return;
}
}
}
if (count($fileList) > 0) {
// if were caching and need a file list build our jsETag
if ($this->cacheOptions['httpCacheClient'] &&
$this->cacheOptions['ClientCacheExpects'] === 'files')
{
$jsETag = $this->_callCacheRule('Client',$fileList);
}
// if were not in combined output compare etags, if method returns true were done
if (!$combinedOutput && isset($jsETag)) {
if ($this->_compareEtags($jsETag)) {
ob_end_clean();
return;
}
}
// output the needed client js files
foreach($fileList as $file) {
$this->_readFile($file);
}
// if were caching and need content build the etag
if ($this->cacheOptions['httpCacheClient'] &&
$this->cacheOptions['ClientCacheExpects'] === 'content')
{
$jsETag = $this->_callCacheRule('Client',ob_get_contents());
}
// if were not in combined output compare etags, if method returns true were done
if (!$combinedOutput && isset($jsETag)) {
if ($this->_compareEtags($jsETag)) {
ob_end_clean();
return;
}
}
// were in combined output, merge the 2 ETags and compare
else if (isset($jsETag) && isset($stubETag)) {
if ($this->_compareEtags(md5($stubETag.$jsETag))) {
ob_end_clean();
return;
}
}
}
// were outputting content, add our length header and send the output
$length = ob_get_length();
$output = ob_get_contents();
ob_end_clean();
if ($this->ajax->packJavaScript) {
$output = $this->ajax->packJavaScript($output);
$length = strlen($output);
}
if ($length > 0 && $this->ajax->_sendContentLength()) {
//$headers['Content-Length'] = $length;
}
$headers['Content-Type'] = 'text/javascript; charset=utf-8';
$this->ajax->_sendHeaders($headers);
echo($output);
}
/**
* Run readfile on input with basic error checking
*
* @param string $file file to read
* @access private
* @todo is addslashes enough encoding for js?
*/
function _readFile($file)
{
if (file_exists($file)) {
readfile($file);
} else {
$file = addslashes($file);
echo "alert('Unable to find javascript file: $file');";
}
}
/**
* Get the location of the client js
* To override the default pear datadir location set $this->clientJsLocation
*
* @return string
*/
function clientJsLocation()
{
if (!$this->clientJsLocation) {
$path = '@data-dir@'.DIRECTORY_SEPARATOR.'HTML_AJAX'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR;
if(strpos($path, '@'.'data-dir@') === 0)
{
$path = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'js').DIRECTORY_SEPARATOR;
}
return $path;
} else {
return $this->clientJsLocation;
}
}
/**
* Set the location of the client js
*
* @access public
* @param string $location Location
* @return void
*/
function setClientJsLocation($location)
{
$this->clientJsLocation = $location;
}
/**
* Set the path to a Javascript libraries
*
* @access public
* @param string $library Library name
* @param string $path Path
* @return void
*/
function setJavascriptLibraryPath($library, $path)
{
$this->javascriptLibraryPaths[$library] = $path;
}
/**
* Set the path to more than one Javascript libraries at once
*
* @access public
* @param array $paths Paths
* @return void
*/
function setJavascriptLibraryPaths($paths)
{
if (is_array($paths)) {
$this->javascriptLibraryPaths = array_merge($this->javascriptLibraryPaths, $paths);
}
}
/**
* Load options from _GET
*
* @access private
*/
function _loadOptions()
{
$this->options = array('client'=>array(),'stub'=>array());
if (isset($_GET['client'])) {
$clients = explode(',',$this->ajax->_getVar('client'));
$client = array();
foreach($clients as $val) {
$cleanVal = $this->_cleanIdentifier($val);
if (!empty($cleanVal)) {
$client[] = strtolower($cleanVal);
}
}
if (count($client) > 0) {
$this->options['client'] = $client;
}
}
if (isset($_GET['stub'])) {
$stubs = explode(',',$this->ajax->_getVar('stub'));
$stub = array();
foreach($stubs as $val) {
$cleanVal = $this->_cleanIdentifier($val);
if (!empty($cleanVal)) {
$stub[] = strtolower($cleanVal);
}
}
if (count($stub) > 0) {
$this->options['stub'] = $stub;
}
}
}
/**
* Clean an identifier like a class name making it safe to use
*
* @param string $input
* @return string
* @access private
*/
function _cleanIdentifier($input) {
return trim(preg_replace('/[^A-Za-z_0-9]/','',$input));
}
/**
* Run every init method on the class
*
* @access private
*/
function _initAll()
{
if ($this->initMethods) {
foreach($this->_initLookup as $class => $method) {
$this->_init($class);
}
}
}
/**
* Init one class
*
* @param string $className
* @access private
*/
function _init($className)
{
$className = strtolower($className);
if ($this->initMethods) {
if (isset($this->_initLookup[$className])) {
$method =& $this->_initLookup[$className];
if (is_array($method)) {
call_user_func($method);
}
else {
$this->$method();
}
} else {
trigger_error("Could find an init method for class: " . $className);
}
}
}
/**
* Generate a hash from a list of files
*
* @param array $files file list
* @return string a hash that can be used as an etag
* @access private
*/
function _cacheRuleFile($files) {
$signature = "";
foreach($files as $file) {
if (file_exists($file)) {
$signature .= $file.filemtime($file);
}
}
return md5($signature);
}
/**
* Generate a hash from the api of registered classes
*
* @param array $classes class list
* @return string a hash that can be used as an etag
* @access private
*/
function _cacheRuleApi($classes) {
$signature = "";
foreach($classes as $class) {
if (isset($this->ajax->_exportedInstances[$class])) {
$signature .= $class.implode(',',$this->ajax->_exportedInstances[$class]['exportedMethods']);
}
}
return md5($signature);
}
/**
* Generate a hash from the raw content
*
* @param array $content
* @return string a hash that can be used as an etag
* @access private
*/
function _cacheRuleContent($content) {
return md5($content);
}
/**
* Send cache control headers
* @access private
*/
function _sendCacheHeaders($etag,$notModified) {
header('Cache-Control: must-revalidate');
header('ETag: '.$etag);
if ($notModified) {
header('HTTP/1.0 304 Not Modified',false,304);
}
}
/**
* Compare eTags
*
* @param string $serverETag server eTag
* @return boolean
* @access private
*/
function _compareEtags($serverETag) {
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
if (strcmp($this->ajax->_getServer('HTTP_IF_NONE_MATCH'),$serverETag) == 0) {
$this->_sendCacheHeaders($serverETag,true);
return true;
}
}
$this->_sendCacheHeaders($serverETag,false);
return false;
}
/**
* Call a cache rule and return its retusn
*
* @param string $rule Stub|Client
* @param mixed $payload
* @return boolean
* @access private
* @todo decide if error checking is needed
*/
function _callCacheRule($rule,$payload) {
$method = '_cacheRule'.$this->cacheOptions[$rule.'CacheRule'];
return call_user_func(array(&$this,$method),$payload);
}
}
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -