Project

General

Profile

Feature #16974 » alt_doc_ajax.php

Administrator Admin, 2007-02-12 01:54

 
<?php
/***************************************************************
* Copyright notice
*
* (c) 2006 Oliver Hader <oh@inpublica.de>
* (c) 2007 Máximo Cuadros <maximo.cuadros@grupobbva.com>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Main form rendering script for AJAX calls only.
*
* $Id: alt_doc_ajax.php 1931 2007-01-24 12:47:35Z ohader $
*
* @author Oliver Hader <oh@inpublica.de>
* @author Máximo Cuadros <maximo.cuadros@grupobbva.com>
*/
/**
* [CLASS/FUNCTION INDEX of SCRIPT]
*
*
*
* 102: class SC_alt_doc_ajax
* 110: function init()
* 147: function main()
* 173: function loadMID($package,$action)
* 208: function loadRequired($a)
* 230: function hadlerParser($string)
* 308: function callMID($args)
* 326: function printContent()
* 337: function printError($errno,$error)
*
* TOTAL FUNCTIONS: 8
* (This index is automatically created/updated by the extension "extdeveval")
*
*/



require('init.php');
require('template.php');
require_once (PATH_typo3.'contrib/json.php');


//This is needed??¿
$LANG->includeLLFile('EXT:lang/locallang_alt_doc.xml');
require_once (PATH_t3lib.'class.t3lib_tceforms.php');
require_once (PATH_t3lib.'class.t3lib_tcemain.php');
require_once (PATH_t3lib.'class.t3lib_loaddbgroup.php');
require_once (PATH_t3lib.'class.t3lib_transferdata.php');
t3lib_BEfunc::lockRecords();



//Example for:classForBeInstanced->Method
$TYPO3_CONF_VARS['BE']['mID_include']['IRRE']['createNewRecord']=array(
'handler' => 't3lib_TCEforms->inline->createNewRecord',
'required' => array(PATH_t3lib.'class.t3lib_tceforms.php'),
'encodeUTF8' => false
);

//Example for:$objectThatAlreadyExists->Method
$TYPO3_CONF_VARS['BE']['mID_include']['test']['instancied']=array(
'handler' => '$GLOBALS[\'JSON\']->encode',
'required' => array('/home/mcuadros/domains/t3.mcuadros.es/public_html/test.php'),
'encodeUTF8' => false
);

//Example for:function
$TYPO3_CONF_VARS['BE']['mID_include']['test']['function']=array(
'handler' => 'testFunction',
'required' => array('/home/mcuadros/domains/t3.mcuadros.es/public_html/test.php'),
'encodeUTF8' => false
);

//Example for:class::staticMethod
$TYPO3_CONF_VARS['BE']['mID_include']['test']['static']=array(
'handler' => 'subClass::test',
'required' => array('/home/mcuadros/domains/t3.mcuadros.es/public_html/test.php'),
'encodeUTF8' => false
);


class SC_alt_doc_ajax {
var $content; // Content accumulation
var $ajax = array(); // the AJAX paramerts from get/post
var $obj = false; // The object loaded on the function loadMID
var $howCall = array(); // Array created at loadMid, that is used to execute the handler defined at the mID
var $package = ''; // mID package requested
var $action = ''; // mID action requested

function init() {

// get argues
$this->ajax = t3lib_div::_GP('ajax');
$this->package = t3lib_div::_GP('mID');
$this->action = t3lib_div::_GP('mACT');

//Load JSON object
$GLOBALS['JSON'] = t3lib_div::makeInstance('Services_JSON');


// MENU-ITEMS:
// If array, then it's a selector box menu
// If empty string it's just a variable, that'll be saved.
// Values NOT in this array will not be saved in the settings-array for the module.
// @TODO: showPalettes etc. should be stored on client side and submitted via each ajax call
$this->MOD_MENU = array(
'showPalettes' => '',
'showDescriptions' => '',
'disableRTE' => ''
);
// Setting virtual document name
$this->MCONF['name']='xMOD_alt_doc.php';
// CLEANSE SETTINGS
$this->MOD_SETTINGS = t3lib_BEfunc::getModuleData($this->MOD_MENU, t3lib_div::_GP('SET'), $this->MCONF['name']);



}

/**
* The main function for the AJAX call.
* The out is written to $this->content
*
* @return void
*/
function main() {
header('Expires: Fri, 27 Nov 1981 09:04:00 GMT');
header('Last-Modified: '.gmdate("D, d M Y H:i:s").' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type: text/javascript; charset=utf-8');

if ( $this->loadMID($this->package,$this->action) ) {
$this->content = $this->callMID($this->ajax);
}

}



/**
* Load all the data of the action of the packaged given, if something is wrong can output errors.
*
* @param string $package: key of the associative array $TYPO3_CONF_VARS['BE']['mID_include']
* @param string $action: key of the associative array $TYPO3_CONF_VARS['BE']['mID_include'][$package]
* @return boolean Return a true if all goes ok, or false if something happends
*/
function loadMID($package,$action) {
global $TYPO3_CONF_VARS;
if ( is_array($TYPO3_CONF_VARS['BE']['mID_include'][$package]) ) {
//We chechk if this mID is set
if ( is_array($TYPO3_CONF_VARS['BE']['mID_include'][$package][$action]) && count($TYPO3_CONF_VARS['BE']['mID_include'][$package][$action]) >= 2 ) {

// If this miD have any required class is loaded
if ( !$this->loadRequired($TYPO3_CONF_VARS['BE']['mID_include'][$package][$action]['required']) ) {
return false;
}

if ( !$this->hadlerParser($TYPO3_CONF_VARS['BE']['mID_include'][$package][$action]['handler']) ) {
return false;
}

return true;

} else {
$this->printError('00002','The requested action not exist or not is proper defined in this package');
}
} else {
$this->printError('00001','The requested package not exist');
}


return false;

}

/**
* Load all the required files definet at the action, key required
*
* @param array $a: Array with the files to be included
* @return boolean True if all goes ok, or false if something goes wrong
*/
function loadRequired($a) {
if ( is_array($a) ) {
$i=0; $max=count($a);
while ($i < $max) {
if ( strlen($a[$i]) != 0 && is_file($a[$i]) ) {
require_once($a[$i]);
} else {
$this->printError('00003','Error loading the required file #'.$i);
return false;
}
$i++;
}
}
return true;
}

/**
* Parse the string hadler, and set the vars $this->obj and $this->howCall, used to make the call and return the answer
*
* @param string $string: String of the key handler of the action
* @return boolean True if all goes ok, or false if something goes wrong
*/
function hadlerParser($string) {

//If the string is a function, its clear is a function :)
if ( function_exists($string) ) {
$this->obj = false;
$this->howCall = $string;

return true;
}

//If is_callable returns true is a static method
$tmp=explode('::',$string);
if ( is_callable($tmp) && count($tmp) == 2 ) {
$this->obj = false;
$this->howCall = $tmp;

return true;
}


$tmp=explode('->',$string);
$classOrObj = array_shift($tmp);
if ( $classOrObj[0] == '$' ) {
//Make a reference of the obj
eval('$this->obj = &'.$classOrObj.';');

//If is_callable returns true is a method of a object instanced
if ( is_object($this->obj) && count($tmp) >= 1 ) {

$i = 0; $max=count($tmp)-1;
while ($i < $max) {
if ( !is_object($this->obj->$tmp[$i]) ) {
$this->printError('00006','Error getting halder subobj');
return false;
}
$this->obj=$this->obj->$tmp[$i];
$i++;
}
$this->howCall = $tmp[count($tmp)-1];


return true;
}
} else {
//If class_exists returns true is a method of a class that sould be instance
if ( class_exists($classOrObj) && count($tmp) >= 1 ) {
$this->obj = t3lib_div::makeInstance($classOrObj);
$i = 0; $max=count($tmp)-1;
while ($i < $max) {
if ( !is_object($this->obj->$tmp[$i]) ) {
$this->printError('00006','Error getting halder subobj');
return false;
}
$this->obj=$this->obj->$tmp[$i];
$i++;
}
$this->howCall = $tmp[count($tmp)-1];


return true;
}
}

//If this method still run, then nothing usable was found on the string, we return a error
$this->printError('00004','Nothing usable was found on the hadler');
return false;
}



/**
* Call the function defined at the action element.
*
* @param array $args: args of the function called
* @return mixed Return the anser of the function called, of fallse if exist some error
*/
function callMID($args) {
if ( $this->obj && is_callable(Array($this->obj,$this->howCall))) {
return call_user_func_array(Array($this->obj,$this->howCall),$args);
} elseif ( !$this->obj && is_callable($this->howCall) ) {

return call_user_func_array($this->howCall,$args);
} else {
$this->printError('00005','Impossible to run this mID');
return false;
}
}


/**
* Performs the output of $this->content.
*
* @return void
*/
function printContent() {
echo $this->content;
}

/**
* Return a json content with the error text and id.
*
* @param string $errno: Error code id
* @param string $error: Error text
* @return [type] ...
*/
function printError($errno,$error) {
$error=Array($errno,$error);
$GLOBALS['JSON']->encode($error);
exit();
}

}

// Include extension?
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/alt_doc_ajax.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/alt_doc_ajax.php']);
}

/**
This must be deleted
Error codes:
00001:The requested package not exist
00002:The requested action not exist or not is proper defined in this package
00003:Error loading the required file #
00004:Nothing usable was found on the hadler
00005:Impossible to run this mID
00006:Error getting halder subobj
00007:Unknow instanced object
**/


// Make instance:
$SOBE = t3lib_div::makeInstance('SC_alt_doc_ajax');

// Main:
$SOBE->init();
$SOBE->main();
$SOBE->printContent();

?>
    (1-1/1)