Index: branches/2.4/prototype/api/datalayer.js
===================================================================
--- branches/2.4/prototype/api/datalayer.js	(revision 6453)
+++ branches/2.4/prototype/api/datalayer.js	(revision 6754)
@@ -1,2 +1,6 @@
+
+(function($){$.parseQuery=function(options){var config={query:window.location.search||""},params={};if(typeof options==='string'){options={query:options};}
+$.extend(config,$.parseQuery,options);config.query=config.query.replace(/^\?/,'');$.each(config.query.split(config.separator),function(i,param){var pair=param.split('='),key=config.decode(pair.shift(),null).toString(),value=config.decode(pair.length?pair.join('='):null,key);if(config.array_keys(key)){params[key]=params[key]||[];params[key].push(value);}else{params[key]=value;}});return params;};$.parseQuery.decode=$.parseQuery.default_decode=function(string){return decodeURIComponent((string||"").replace('+',' '));};$.parseQuery.array_keys=function(){return false;};$.parseQuery.separator="&";}(jQuery));
+
 internalUrl = /^([A-z0-9-_]+)(:[A-z0-9-_]+)?$/;
 internalUri = /^([a-zA-Z0-9-_]+)\(([a-zA-Z0-9-_]+)\):\/\/(.*)|([a-zA-Z0-9-_]+):\/\/(.*)$/;
@@ -57,4 +61,7 @@
 
 	  jqXHR.abort();
+	  
+	  if( typeof options.data === "string" )
+	      options.data = $.parseQuery( options.data );
 
 	  switch( options.type.toUpperCase() )
@@ -624,7 +631,6 @@
     
     rollback: function( concept, ids ){
-      
-    if(!DataLayer.storage.cache[':diff']) 
-                return false; 
+    if(!DataLayer.storage.cache[':diff'])
+		return false;
 	if(concept){
 		if(ids){
@@ -1394,5 +1400,5 @@
 	  properties: criteria.properties || ''
 
-      } );
+      }, false, true );
     },
 
Index: branches/2.4/prototype/api/newcontroller.php
===================================================================
--- branches/2.4/prototype/api/newcontroller.php	(revision 5715)
+++ branches/2.4/prototype/api/newcontroller.php	(revision 6754)
@@ -5,4 +5,5 @@
 
 require_once(ROOTPATH.'/api/config.php');
+use prototype\api\Config as Config;
 
 /**
Index: branches/2.4/prototype/api/rest.js
===================================================================
--- branches/2.4/prototype/api/rest.js	(revision 6754)
+++ branches/2.4/prototype/api/rest.js	(revision 6754)
@@ -0,0 +1,172 @@
+REST = {
+    
+    resquests: {},
+    
+    taksResquests: {},
+    
+    isProcessRefreshToken: false,
+
+    /**
+    * This method is used to read resources from the server.
+    * 
+    * @param uri Uri of the resource that gonna be readed.
+    * @param callback A function that is called when the resource is loaded successfully. When the parameter is ignored the request is made synchrounsly.
+    * @param accept The attribute accept that is used to ask the target format to the server
+    * @return The target resource when the callback is ommitted ( made it synchronous )
+    */
+
+    get: function( uri, data, callback, accept ){
+	return this.send( (this.dispatchPath + '../rest' + uri), [ 'get', accept || 'json' ], data, callback, !!!callback);
+      
+    },
+
+    /**
+    * This method is used to create resources from the server.
+    * 
+    * @param uri Uri of the resource that gonna be readed.
+    * @param callback A function that is called when the resource is created on the server successfully. When the parameter is ignored the request is made synchrounsly.
+    * @param accept The attribute accept that is used to ask the target format to the server.
+    * @return The result response of the create from the server when the callback is ommitted ( made it synchronous )
+    */
+    
+    post: function( uri, data, callback, accept ){
+	return this.send( (this.dispatchPath + '../rest' + uri), [ 'post', accept || 'json' ], data, callback, !!!callback);      
+    },
+
+    /**
+    * This method is used to update resources from the server.
+    * 
+    * @param uri Uri of the resource that gonna be readed.
+    * @param callback A function that is called when the resource is update on the server successfully. When the parameter is ignored the request is made synchrounsly.
+    * @param accept The attribute accept that is used to ask the target format to the server
+    * @return The result response of the update from the server when the callback is ommitted ( made it synchronous )
+    */
+
+    put: function( uri, data, callback, accept ){
+	return this.send( (this.dispatchPath + '../rest' + uri), [ 'put', accept || 'json' ], data, callback, !!!callback);
+      
+    },
+
+    /**
+    * This method is used to delete resources from the server.
+    * 
+    * @param uri Uri of the resource that gonna be readed.
+    * @param callback A function that is called when the resource is deleted successfully in the server. When the parameter is ignored the request is made synchrounsly.
+    * @param accept The attribute accept that is used to ask the target format to the server
+    * @return The result response of the delete from the server when the callback is ommitted ( made it synchronous )
+    */
+
+    "delete": function( uri, callback, accept ){
+	return this.send( (this.dispatchPath + '../rest' + uri), [ 'delete', accept || 'json' ], false, callback, !!!callback);
+    },
+    
+    send: function( url, type, data, callback, sync, extraOptions ){
+	this.id = url;
+
+	if(REST.isProcessRefreshToken && !!callback){
+
+	    REST.taksResquests[REST.taksResquests.length] = {
+		url: url, 
+		type: type, 
+		data: data, 
+		callback: callback, 
+		sync: sync, 
+		extraOptions: extraOptions
+	    };
+
+	}else if(REST.isProcessRefreshToken)
+	    setTimeout(REST.send(url, type, data, callback, sync, extraOptions), 100);
+
+	REST.resquests[url] = {
+	    url: url, 
+	    type: type, 
+	    data: data, 
+	    callback: callback, 
+	    sync: sync, 
+	    extraOptions: extraOptions
+	};    
+      
+	var result = false;      
+	var envelope = {
+
+	    async: ( typeof sync !== "undefined" ? !sync : !!callback ),
+	    url: url,
+	    success: function( dt, textStatus, jqXHR ){
+
+		if(REST.isInvalidToken(dt)){
+		    REST.load('', true);
+		    
+		    if(!REST.me)
+			return {error: 'Empty session', title: 'Error in refresh token', description: 'Error in refresh token.'};
+		    
+		    var ref = REST.resquests[this.url];
+		    result =  REST.send(ref.url, ref.type, ref.data, ref.callback, ref.sync, ref.extraOptions)
+		}
+		delete REST.resquests[this.url];
+		
+		if(result == false){
+
+		    if( callback )
+		    {
+			result = callback( dt, textStatus, jqXHR );
+		    }
+		    else
+			result = dt;
+		    }
+
+	    },
+	    error: function( dt, textStatus, jqXHR ){
+		
+		var response = {error: 'error', status: dt.status, description: dt.responseText, statusText: dt.responseText};
+
+		if( callback )
+		{
+		    result = callback( response, textStatus, jqXHR );
+		}
+		else
+		    result = response;
+
+	    },
+
+	    type: $.isArray( type ) ? type[0] : type,
+	    data: data
+
+	};
+
+	if( $.isArray( type ) && type[1] )
+	    envelope['dataType'] = type[1];
+
+	if(REST.me)
+	    envelope = $.extend( envelope, {
+		beforeSend: function (xhr){
+		    xhr.setRequestHeader('Authorization', "OAUTH Bearer " + REST.me.token)
+		}
+	    });
+
+	if( extraOptions )
+	    envelope = $.extend( envelope, extraOptions );
+
+	$.ajax( envelope );
+      
+	return( result );
+    },
+    
+    isInvalidToken: function(data){
+	return ((data) && (data.error && data.error == 'invalid_grant') && (data.error_description == 'The access token provided has expired.')) ? true : false
+    },
+    
+    dispatch: function( dispatcher, data, callback, isPost, dataType ){
+	return this.send( this.dispatchPath + dispatcher + ".php", 
+	    [ ( isPost ? 'post' : 'get' ), dataType || 'json' ],
+	    data, callback );
+    },
+
+    dispatchPath: '../prototype',
+
+    load: function(url, isRefresh){
+	this.me = this.dispatch( (url || '') + "me", (isRefresh ? {
+	    refreshToken: true
+	} : {}), false, true)	
+    }
+    
+}
Index: branches/2.4/prototype/api/controller.php
===================================================================
--- branches/2.4/prototype/api/controller.php	(revision 6420)
+++ branches/2.4/prototype/api/controller.php	(revision 6754)
@@ -44,5 +44,5 @@
 
 require_once(ROOTPATH.'/api/config.php');
-
+use prototype\api\Config as Config;
 /**
 TODO list:
Index: branches/2.4/prototype/api/config.php
===================================================================
--- branches/2.4/prototype/api/config.php	(revision 5764)
+++ branches/2.4/prototype/api/config.php	(revision 6754)
@@ -1,100 +1,113 @@
 <?php
+
+namespace prototype\api;
 
 class Config 
 {
-    static $register;
-    static $sessionStarted;
+	static $register;
+	static $sessionStarted;
   
-    static function module($config , $module = false)
-    {
-        //Todo: registrar na nova api o currentapp
-        if(!$module)
-            $module =  $_SESSION['flags']['currentapp'];
+	static function module($config , $module = false)
+	{
+		//Todo: registrar na nova api o currentapp
+		if(!$module)
+				$module =  $_SESSION['flags']['currentapp'];
   
-        if( !isset( $_SESSION['config'][$module] ) || !isset( $_SESSION['config'][$module][$config] ))
-            $_SESSION['config'][$module] = parse_ini_file( ROOTPATH."/config/$module.ini", true );
+		if( !isset( $_SESSION['config'][$module] ) || !isset( $_SESSION['config'][$module][$config] ))
+				$_SESSION['config'][$module] = parse_ini_file( ROOTPATH."/config/$module.ini", true );
         
-        return isset($_SESSION['config'][$module][$config]) ? $_SESSION['config'][$module][$config] : false;
+		return isset($_SESSION['config'][$module][$config]) ? $_SESSION['config'][$module][$config] : false;
      
-    }
+	}
     
-    static function me($config)
-    {
+	static function me($config)
+	{
         
-        return isset($_SESSION['wallet']['user'][$config]) ? $_SESSION['wallet']['user'][$config] : false;
-    }
+		return isset($_SESSION['wallet']['user'][$config]) ? $_SESSION['wallet']['user'][$config] : false;
+	}
     
-    static function service( $service , $config )
-    {
-        if( !isset( $_SESSION['wallet'][$service] ) || !isset( $_SESSION['wallet'][$service][$config] ))
-            $_SESSION['wallet'][$service] = parse_ini_file( ROOTPATH."/config/$service.srv", true );
+	static function service( $service , $config )
+	{
+		if( !isset( $_SESSION['wallet'][$service] ) || !isset( $_SESSION['wallet'][$service][$config] ))
+				$_SESSION['wallet'][$service] = parse_ini_file( ROOTPATH."/config/$service.srv", true );
        
-        return (isset($_SESSION['wallet'][$service][$config])) ? $_SESSION['wallet'][$service][$config] : false;
-    }
+		return (isset($_SESSION['wallet'][$service][$config])) ? $_SESSION['wallet'][$service][$config] : false;
+	}
     
-    static function get( $concept , $config = false , $module = false )
-    {
-        $load = parse_ini_file( ROOTPATH."/config/$concept.ini", true );
+	static function get( $concept , $config = false , $module = false )
+	{
+		$load = parse_ini_file( ROOTPATH."/config/$concept.ini", true );
         
-        if($config === false) return $load;
+		if($config === false) return $load;
         
-        return (isset($load[$config])) ? $load[$config] : false;
-    }
+		return (isset($load[$config])) ? $load[$config] : false;
+	}
     
-    static function regSet( $name , $value)
-    {
-        self::$register[$name] = $value;
-    }
-    static function regGet ($name )
-    {
-        return (isset(self::$register[$name]) ? self::$register[$name] : false );
-    }
+	static function regSet( $name , $value)
+	{
+		self::$register[$name] = $value;
+	}
+	static function regGet ($name )
+	{
+		return (isset(self::$register[$name]) ? self::$register[$name] : false );
+	}
 
-    static function init( )
-    {
+	static function init( )
+	{
+    
+		if( !defined( 'ROOTPATH' ) )
+				define( 'ROOTPATH', dirname(__FILE__).'/..' );
         
-        if( !defined( 'ROOTPATH' ) )
-            define( 'ROOTPATH', dirname(__FILE__).'/..' );
-        
-        if ( isset( $_COOKIE[ 'sessionid' ] ) ) 
-        {
-            session_id( $_COOKIE[ 'sessionid' ] ); 
-            $GLOBALS['phpgw']->session->sessionid = $_COOKIE[ 'sessionid' ];
-        }
+		if ( isset( $_COOKIE[ 'sessionid' ] ) ) 
+		{
+				session_id( $_COOKIE[ 'sessionid' ] ); 
+				$GLOBALS['phpgw']->session->sessionid = $_COOKIE[ 'sessionid' ];
+		}
 		
 	if( !self::$sessionStarted )
-            self::$sessionStarted = session_start();
-//		
-//	if( $header === true )
-//	{
-//            require_once (dirname(__FILE__).'/../../header.inc.php');
-//
-//            $_SESSION['wallet']['Sieve']['user']          =  $GLOBALS['phpgw_info']['user']['account_lid'];
-//            $_SESSION['wallet']['Sieve']['password']      =  $GLOBALS['phpgw_info']['user']['passwd'];
-//
-//            $_SESSION['wallet']['Cyrus']['user']          =  $GLOBALS['phpgw_info']['user']['account_lid'];
-//            $_SESSION['wallet']['Cyrus']['password']      =  $GLOBALS['phpgw_info']['user']['passwd'];
-//
-//            $_SESSION['wallet']['user']['uid']	      	  =  $GLOBALS['phpgw_info']['user']['userid'];
-//            $_SESSION['wallet']['user']['uidNumber']      =  $GLOBALS['phpgw_info']['user']['account_id'];
-//            $_SESSION['wallet']['user']['password']       =  $GLOBALS['phpgw_info']['user']['passwd'];
-//            $_SESSION['wallet']['user']['cn']             =  $GLOBALS['phpgw_info']['user']['cn'];
-//            $_SESSION['wallet']['user']['mail']           =  $GLOBALS['phpgw_info']['user']['email'];
-//
-//            $_SESSION['wallet']['PostgreSQL']['user']     = $GLOBALS['phpgw_info']['server']['db_user'];
-//            $_SESSION['wallet']['PostgreSQL']['password'] = $GLOBALS['phpgw_info']['server']['db_pass'];
-//            $_SESSION['wallet']['PostgreSQL']['dbname']   = $GLOBALS['phpgw_info']['server']['db_name'];
-//            $_SESSION['wallet']['PostgreSQL']['host']     = $GLOBALS['phpgw_info']['server']['db_host'];
-//            
-//            $_SESSION['wallet']['OpenLDAP']['context']   =  $GLOBALS['phpgw_info']['server']['ldap_context'];
-//            $_SESSION['wallet']['OpenLDAP']['host']      =  $GLOBALS['phpgw_info']['server']['ldap_host'];
-//           
-//	}
+				self::$sessionStarted = session_start();
 		
-    }
+	}
+    
+	public static function writeIniFile($assoc_arr, $path, $has_sections)
+	{
+		$content = '';	
+		self::_writeIniFile($content, $assoc_arr, $has_sections);
+		if( file_put_contents($path, $content) === false)
+		{
+			trigger_error("Permission failure when trying to write in the file: $path ", E_USER_WARNING);
+			return false;
+		}
+		return true;
+	}
+
+	private static function _writeIniFile(&$content, $assoc_arr, $has_sections)
+	{
+		foreach ($assoc_arr as $key => $val)
+		{
+			if (is_array($val))
+			{
+				if($has_sections)
+				{
+					$content .= "[$key]\n";
+					self::_writeIniFile(&$content, $val, false);
+				}
+				else				
+					foreach($val as $iKey => $iVal)
+					{
+						if (is_int($iKey))
+							$content .= $key ."[] = $iVal\n";
+						else
+							$content .= $key ."[$iKey] = $iVal\n";
+					}
+			}
+			else
+				$content .= "$key = $val\n";
+		}
+	}
 
 }
 
-    Config::init();
+	Config::init();
 	
 
