Location: PHPKode > projects > phpMySOAP > phpMySOAP/loader.php
<?php

/**
 * @author Korbinian Ober
 * @copyright 2010 tuxphr34k media software solutions
 * @version 0.1a
 */

/**
 * fCreateClass()
 * 
 * @return string code of the proxy class for all available functions
 */
function fCreateClass() {
    $cl = "class ".$GLOBALS['config']['servicename']." {\n";
    $cl.= fWriteFunctionsForDirectory($GLOBALS['config']['functions.d']);
    $cl.= "}";
    return $cl;
}
/**
 * fWriteFunctionsForDirectory()
 * 
 * @param mixed $directory writes functions for all functions in this directory and calls this function recursively for subfolders
 * @return string function's code
 */
function fWriteFunctionsForDirectory($directory) {
    $cl = '';
    $dir = opendir($directory);
    while ($dirname = readdir($dir)) {
        if (substr($dirname, 0, 1) == '.')
            continue;
        if (is_dir($directory.'/'.$dirname)) {
            $cl.=fWriteFunctionsForDirectory($directory.'/'.$dirname);
            continue;
        }
        include($directory.'/'.$dirname);
        // no name defined
        if (!isset($function['name']))
            continue;
        $cl.= ' function '.$function['name'].'() {
        $args = func_get_args();
        $args[] = (defined("AUTH_USER") ? AUTH_USER : "");
        $args[] = (defined("AUTH_PASS") ? AUTH_PASS : ""); 
        if (file_exists("'.$directory.'/'.$dirname.'")) {
            include("'.$directory.'/'.$dirname.'");
            // replace args in sql queries
            if (isset($function["sql"])) 
                for ($i = 0; $i < count($function["sql"]); $i++)
                    for ($argc=0; $argc<count($args); $argc++) 
                        $function["sql"][$i] = str_replace("@@".$argc."@@", "\'".mysql_real_escape_string($args[$argc],$GLOBALS["db"])."\'", $function["sql"][$i]);
            $retval = array();
            if (isset($function["sql"])) {
                for ($i = 0; $i < count($function["sql"]); $i++) {
                    if (is_array($function["sql_type"][$i])) {
                        // SELECT
                        $res = mysql_unbuffered_query($function["sql"][$i], $GLOBALS["db"]);
                        $rows = array();
                        while ($row = mysql_fetch_assoc($res)) {
                            $rows[] = $row;
                        }
                        $retval[] = $rows;mysql_free_result($res);
                    } else {
                        // UPDATE, DELETE
                        mysql_query($function["sql"][$i], $GLOBALS["db"]);
                        $retval[] = mysql_affected_rows($GLOBALS["db"]);
                    }
                }
            }
            if (isset($function["eval"])) { 
                unset($r);
                $retval[] = eval($function["eval"]);
                if ((($retval[count($retval) - 1] === false) || ($retval[count($retval) - 1] === null)) && (isset($r))) {
                    $retval[count($retval) - 1] = $r;
                }
            }
            if (count($retval) == 1) 
                $retval = $retval[0];
            return $retval;
        }
    }';
    }
    closedir($dir);  
    return $cl;
}
/**
 * fRegisterFunctionsForDirectory()
 * 
 * @param mixed $server server variable (NuSOAP server)
 * @param mixed $directory directory to parse
 * @return
 */
function fRegisterFunctionsForDirectory(&$server, $directory) {
    $dir = opendir($directory);
    while ($dirname = readdir($dir)) {
        // break on hidden files / folder dirs
        if (substr($dirname, 0, 1) == '.')
            continue;
        // handle sub directories
        if (is_dir($directory.'/'.$dirname)) {
            fRegisterFunctionsForDirectory($server, $directory.'/'.$dirname);
            continue;
        }
        $function = null;
        // load function details
        include($directory.'/'.$dirname);
        include('function.default.php');
        $out = array();
        // register SQL output types
        if (isset($function['sql_type'])) {
            for ($i=0;$i<count($function['sql_type']);$i++){
                if (is_array($function['sql_type'][$i])) {
                $server->wsdl->addComplexType(
                  $function['name'].'SqlResultType'.$i.'Row',
                  'complexType',
                  'struct',
                  'all',
                  '',
                    $function['sql_type'][$i]);
                $server->wsdl->addComplexType(
                  $function['name'].'SqlResultType'.$i,
                  'complexType', 
                  'array', 
                  '', 
                  'SOAP-ENC:Array', 
                  array(),
                  array(
                    array('ref' => 'SOAP-ENC:arrayType', 
                         'wsdl:arrayType' => 'tns:'.$function['name'].'SqlResultType'.$i.'Row'.'[]')
                  ),
                  'tns:'.$function['name'].'SqlResultType'.$i.'Row'
                );
                $out['SqlResult'.$i] = 'tns:'.$function['name'].'SqlResultType'.$i;
                }
                else {
                $out['SqlResult'.$i] = $function['sql_type'][$i];                
                }
            }
        }
        // register eval return type 
        if (isset($function['eval_return'])) {
            $out['return'] = $function['eval_return'];
        }
        // register function
        $server->register(
            $GLOBALS['config']['servicename'].'.'.$function['name'],
            $function['in'],
            $out,
            $GLOBALS['config']['namespace'],
            $GLOBALS['config']['namespace'].'#'.$function['name'],
            'rpc',
            'encoded',
            $function['documentation']
        );
    }
    closedir($dir);

}
Return current item: phpMySOAP