File: C4Network.php

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (79 lines) | stat: -rw-r--r-- 2,079 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
 * Provides functionality to communicate with a
 * Clonk 4 engine with ini-style strings.
 *
 * @package C4Masterserver
 * @version 1.2.0-en
 * @author  Benedict Etzel <b.etzel@live.de>
 * @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
 */
abstract class C4Network {

    /**
     * Creates a Clonk 4 conform answer string.
     *
     * @param array $data
     * @return string
     */
    public static function createAnswer($data) {
        $message = '[Response]'."\n";
        foreach ($data as $key => $value) {
            $message .= $key.'='.$value."\n";
        }
        return $message;
    }

    /**
     * Returns a Clonk 4 conform error string.
     *
     * @param string message
     * @return string
     */
    public static function createError($message) {
        return C4Network::createAnswer(array("Status" => "Failure", "Message" => $message));
    }

    /**
     * Sends a Clonk 4 conform answer string.
     *
     * @param string $message
     * @return void
     */
    public static function sendAnswer($message) {
        header('Content-Length: '.strlen($message));
        echo $message;
    }

    /**
     * Cleans a Clonk 4 conform text sting human readable.
     *
     * @param string $message
     * @return string
     */
    public static function cleanString($message) {
        $coded = $decoded = array();
        preg_match_all('|\\\[0-9]{3}|', $message, $coded);
        foreach($coded[0] as $numstr) {
            $num = ereg_replace("[^0-9]", "", $numstr);
            $decoded[$num] = C4Network::decodeEntitiyString($num);
        }
        foreach($decoded as $num => $entity) {
            $message = str_replace('\\'.$num, $entity, $message);
        }
        return $message;
    }

    /**
     * Decodes Clonk 4 conform entitiy string to its character.
     *
     * @param string $string
     * @return string
     */
    public static function decodeEntitiyString($string) {
        $num = ereg_replace("[^0-9]", "", $string);
        $num = octdec($num);
        return chr($num);
    }
}
?>