user.php

来自「一个基于web的rpg游戏源代码」· PHP 代码 · 共 272 行

PHP
272
字号
<?php/*** Netlands World Server is the coordinator of the VR in the Netlands Project* Copyright (C) 2002 Ricard Pillosu* * 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.* * You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*//* vim: set expandtab tabstop=4 shiftwidth=4 *//*** log activities** Create an entry in the XXX.log file** @param message    string  message to send* @param type       string  where to send* @param connection object  References to connection*/function log_msg($message, $type = "error", &$connection = NULL){    global $config;    $now = date("[r]");    if($connection == NULL) {        $final_message = $now." ".$message."\n";    } else {        $final_message = $now." [".$connection->remote_ip."] ".$message."\n";    }    error_log($final_message, 3, $config['dirs']['log'].$type.".log");}/*** Unset a position of $connections array** @param connection object*/function unset_connection_data(&$connection){    global $connections;    // Filter arguments    if(is_a($connection, "connection") !== TRUE) {        $m = "bad argument connection";        throw new exception($m, debug_backtrace(), $connection);        return(FALSE);    }    $msg = "Closing socket";    log_msg ($msg, "game", $connect);    foreach(array_keys($connections) as $key) {        if($connections[$key]->socket == $connection->socket) {            unset($connections[$key]);            break;        }    }    return(TRUE);}/*** Process certain command** Search for proper function** @param connection object* @param command    array       command + args to process*/function process_command(&$connection, $xml_text){    global $ACCESS_LEVELS;    // Filter arguments    if(is_a($connection, "connection") !== TRUE) {        $m = "bad argument connection";        throw new exception($m, debug_backtrace(), $connection);        return(FALSE);    }    // Parse xml to array    $data = get_array_from_xml($xml_text);    // Check for validity     if(!is_array($data) || !function_exists("command_".$data["xml_name"])) {        if(empty($data["xml_name"])) $data["xml_name"] = "unknown";    	$answer = array(            "xml_name"  => $data["xml_name"],            "ok"        => "0",             "msg"       => "this command does not exists");    } else {        // Check por permissions // levels of access        $function = $data["xml_name"];        if(!check_command_level($connection, $function, $ACCESS_LEVELS)) {            $answer = array(                "xml_name"  => $data["xml_name"],                "ok"        => "0",                 "msg"       => "access denied");        } elseif(!check_command_args($function, $data, $ACCESS_LEVELS)) {            $answer = array(                "xml_name"  => $data["xml_name"],                "ok"        => "0",                 "msg"       => "bad argument list");        } elseif(!($character = get_character($connection, $function, $data))) {            $answer = array(                "xml_name"  => $data["xml_name"],                "ok"        => "0",                 "msg"       => "you don't control this character");        } else {            // Ok, execute             $user   = & $connection->user;            $func   = "command_$function";            $answer = $func($connection, $user, $character, $data);            $answer = add_xml_name($answer, $function);        }    }    // send response to client    if(!empty($answer) && is_array($answer)) {        $xml = create_proper_answer($answer);        write_data_to_socket($connection, $xml);    }    return(TRUE);}/** * Check if specified character ("me" in the protocol) belongs to user*/function &get_character(&$connection, $function, &$data) {    global $entities, $ACCESS_LEVELS;        $level = $ACCESS_LEVELS["$function"]["level"];    if($level != user_list::L_PLAYING) {        // no character is needed            return(TRUE);    }    if(empty($data["me"])) {        // We get the first char in the array, OK for most clients        foreach(array_keys($connection->user->character_list) as $key) {            return($connection->user->character_list[$key]);        }        return(FALSE);    } else {        $character = & $entities->get_entity($data["me"], FALSE);        if($character == FALSE) {            return(FALSE);        }        if($character->type != entity_list::T_CHAR) {            return(FALSE);        }        $char_user = $character->user->get_login();        $comm_user = $connection->user->get_login();        if($char_user != $comm_user) {            return(FALSE);        }        return($character);    }    return(TRUE);}/*** Concatenate arrays to create proper answers to user*/function create_proper_answer($answer) {    if(!empty($answer[0]) && is_array($answer[0])) {        // 2 dimensional array        if(empty($answer[0]["_xml_name"])) {            $name = strtolower($answer[0]["xml_name"]);        } else {            $name = strtolower($answer[0]["_xml_name"]);            unset($answer[0]["_xml_name"]);         }                /*        var_export($answer[0]);        var_export($answer[1]);        var_export($answer[2]);        var_dump(array_intersect($answer[0], $answer[1], $answer[2]));        */        $xml = "<$name>";        for($i=0;$i<count($answer);$i++) {            $xml.= get_xml_from_array($answer[$i]);        }        $xml.= "</$name>";    } else {        // 1 dimensional array        $xml = get_xml_from_array($answer);    }    return($xml);}/*** Check if user has send proper args to command*/function check_command_args($function, $data, $ACCESS_LEVELS) {    // If it is not defined, is OK    if(empty($ACCESS_LEVELS["$function"]["args"])) return(TRUE);    $requiered_args = $ACCESS_LEVELS["$function"]["args"];    foreach($requiered_args as $arg => $len) {        if(!isset($data["$arg"])) {            return(FALSE);        }        if(strlen($data["$arg"]) < $len) {            return(FALSE);        }    }    return(TRUE);}/*** Check if user can execute command*/function check_command_level(&$connection, $function, $ACCESS_LEVELS) {    // No allow not-defined commands    if(empty($ACCESS_LEVELS["$function"])) {        return(FALSE);    }    // level needed    $level = $ACCESS_LEVELS["$function"]["level"];    // out level    $user_level = get_connection_access($connection);    // check    if($user_level >= $level) {        return(TRUE);    } else {        return(FALSE);    }}/*** Just add name of the function to xml returned to user*/function add_xml_name($answer, $function) {    // Check if $answer is a 2 dimension array    if(!empty($answer[0]) && is_array($answer[0])) {        for($i=0;$i<count($answer);$i++) {            if(empty($answer[$i]["xml_name"])) {                $answer[$i]["xml_name"] = $function;            }        }    } else {        // Only 1 dimension, go        if(empty($answer["xml_name"]) && is_array($answer)) {            $answer["xml_name"] = $function;        }    }    return($answer);}/*** Just get the level from the connection*/function get_connection_access(&$connection) {    if(is_object($connection->user) == TRUE) {        return($connection->user->get_access());    }    return(user_list::L_CONNECTED);}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?