<?php
/**
*
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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 at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class ajax {
const URI = '?ajax='; //uri that makes pagegen process the ajax request (@see isAjaxReq())
private static $AJX, $RPL, $PATH;
protected static $CB;
private static $FUNC = Array();
private static function initlib(){
if ( isset(self::$AJX) ){ return; }
self::$AJX = new xajax();
self::$AJX->setRequestURI(self::URI); //let know to xajax about the url that make pgen process the ajax request
self::$AJX->configure('exitAllowed', TRUE); //no exit after reply is sent.
//self::$AJX->statusMessagesOn(); //we informe browser of our activity
self::$AJX->configure('errorHandlerOff', TRUE); //we use our own errorhandler
self::$RPL = new xajaxResponse();
}
public static function RegisterFunction($func){
if ( self::isRegistrated($func) ){ return TRUE; }
if ( FALSE === CORE::getdeps(split(PATH_SEPARATOR, PATH_LIBS), $func, 'ajx_', 'class', 'php') ){
errors::raise('Ajax callback '.$func.'not found in '.PATH_LIBS, CORE_LOG_WARNING, 'NAJAX');
return FALSE;
}
self::initlib();
$xuf = new xajaxUserFunction(array($func, "ajx_$func", $func));
self::$AJX->register(XAJAX_FUNCTION, $xuf);
self::$FUNC[] = $func;
errors::raise('Ajax callback '.$func.' has been registrated', CORE_LOG_NOTICE, 'NAJAX');
return TRUE;
}
public static function isRegistrated($func){ return array_search($func, self::$FUNC); }
public static function getRegistred(){ return self::$FUNC; }
public static function isAjaxReq(){
self::initlib();
return (isset($_GET['ajax']) AND self::$AJX->canProcessRequest());
}
public static function EnforceReq($func){
if ( isset(self::$AJX) ){ unset(self::$AJX); }
$_GET['ajax'] = "";
$_GET['xjxfun'] = $func;
$_POST['xjxfun'] = $func;
self::initlib();
if ( FALSE === self::RegisterFunction($func) ){
errors::raise("Enforcing Ajax Request failed : could not register callback $func", CORE_LOG_ERROR, 'AJAX');
} else {
errors::raise("Enforcing Ajax Request $func", CORE_LOG_INFO, 'AJAX');
}
self::ProcessReq();
exit;
}
public static function ProcessReq(){
if ( self::isAjaxReq() ){ self::$AJX->processRequest(); }
}
protected static function addAssign($id, $atrib, $val){ self::$RPL->assign($id, $atrib, $val); }
protected static function addAppend($id, $atrib, $val){ self::$RPL->append($id, $atrib, $val); }
protected static function addScript($script){ self::$RPL->script($script); }
protected static function getReply($cb=NULL){
self::AppendEvent();
self::AppenDebug();
return self::$RPL;
}
private static function AppenDebug(){
$debug = ( tfeat_debug::get_client_conf('fetch') AND tfeat_debug::get_client_conf('ajax') );
if ( FALSE === $debug ){ return; }
$backtrace = tfeat_debug::get_client_conf('backtrace');
$backvis = tfeat_debug::get_client_conf('backvis');
$color = tfeat_debug::get_client_conf('color');
$hash = CORE::hash(NULL, 5);
$o = '<div id="debug_ajax_'.$hash.'" class="hidden_debug">
<div class="event_header">
<b>'.langs::dico_translate('ajax_time', NULL, 'debug').' :</b><br/>';
$reqtimes = chrono::stopall();
foreach($reqtimes as $k => $time){
$islast = ( $k == count($reqtimes)-1 );
$isfirst = ($k == 0);
if ( $islast ){ $o .= ' = '; } elseif (!$isfirst) { $o .= ' + '; }
$o .= '<span id="time_'.$time['ref'].'" title="'.$time['ref'].'">'.$time['ref'].'='.$time['time'].'</span> s';
}
$o .= '<br/>';
if ( function_exists('memory_get_peak_usage') ){
$o .= '<b>'.langs::dico_translate('mempeak', NULL, 'debug').' :</b> '.langs::number(floor(memory_get_peak_usage()/1024/1024), 1).'MB<br/>';
}
$o .= '<b>'.langs::dico_translate('memlimit', NULL, 'debug').' :</b> '.ini_get('memory_limit');
$o .= '</div>';
while( $event = errors::fetch_debugchan($backtrace, $color) ){
$id = CORE::hash(NULL, 5);
$o .= '<div class="event_container"><div class="event_header">';
if ( FALSE != $backtrace ){
$o .='<span class="blockvis" onclick="javascript:swap_blockvis(0, \'event_'.$id.'\', \'event_content_\');">[±]</span> ';
}
$o .= $event['event'].'</div>';
if ( FALSE != $backtrace ){
if ( FALSE != $backvis ){ $dft = 'open'; } else { $dft = 'close'; }
$o .= '<div id="event_'.$id.'" class="event_content_'.$dft.'">bcktrace :<br />'.$event['backtrace'].'</div>';
$o .= '</div>';
}
}
$o .= ' <div style="height:5px"></div>
<div onclick="javascript:swap_blockvis(0, \'block_debug\', \'debug_content_\');" title="Click to swap debug display status">[-]</div>
</div></div>';
self::$RPL->append('block_debug', 'innerHTML', $o);
self::$RPL->append('switch_debug', 'innerHTML', ' [ <a href="javascript:switch_debug(\'ajax_'.$hash.'\')" >Ajax ('.self::$CB.')</a> ] ');
}
private static function AppendEvent(){
$events = pagegen::getevents();
$o='';
if ( is_array($events) ){
self::addAssign('events', 'innerHTML', '');
foreach($events as &$event ){ $o .= '<div class="event">'.$event."</div>\n"; }
}
if ( $o != '' ){ self::$RPL->append('events', 'innerHTML', $o); }
}
}