corrade-http-templates – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
6 | function atomized_put_contents($file, $data) { |
||
7 | $fp = fopen($file, "w+"); |
||
8 | if (flock($fp, LOCK_EX)) { |
||
9 | fwrite($fp, $data); |
||
10 | fflush($fp); |
||
11 | flock($fp, LOCK_UN); |
||
12 | } |
||
13 | fclose($fp); |
||
14 | } |
||
15 | |||
16 | ########################################################################### |
||
17 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
18 | ########################################################################### |
||
19 | function atomized_get_contents($file) { |
||
20 | $fp = fopen($file, "r+"); |
||
21 | $ct = ''; |
||
22 | if (flock($fp, LOCK_SH)) { |
||
23 | if (filesize($file)) { |
||
24 | $ct = fread($fp, filesize($file)); |
||
25 | } |
||
26 | flock($fp, LOCK_UN); |
||
27 | } |
||
28 | fclose($fp); |
||
29 | return $ct; |
||
30 | } |
||
31 | |||
32 | ########################################################################### |
||
33 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
34 | ########################################################################### |
||
35 | function wasKeyValueGet($key, $data) { |
||
36 | return array_reduce( |
||
37 | explode( |
||
38 | "&", |
||
39 | $data |
||
40 | ), |
||
41 | function($o, $p) { |
||
42 | $x = explode("=", $p); |
||
43 | return array_shift($x) != $o ? $o : array_shift($x); |
||
44 | }, |
||
45 | $key |
||
46 | ); |
||
47 | } |
||
48 | |||
49 | ########################################################################### |
||
50 | ## Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 ## |
||
51 | ########################################################################### |
||
52 | function storeAvatarConversation($firstname, $lastname, $message, $chatFile, $lines) { |
||
53 | #### |
||
54 | # I. Initialize an array to store the chat lines. |
||
55 | $data = array(); |
||
56 | |||
57 | #### |
||
58 | # II. If the file exists, trim it to the number of chat lines. |
||
59 | switch(file_exists($chatFile)) { |
||
60 | case TRUE: |
||
61 | # Open the chat log and trim the lines to the configured line size. |
||
62 | $data = explode( |
||
63 | PHP_EOL, |
||
64 | atomized_get_contents( |
||
65 | $chatFile |
||
66 | ) |
||
67 | ); |
||
68 | while(sizeof($data) > $lines) |
||
69 | array_shift($data); |
||
70 | break; |
||
71 | } |
||
72 | |||
73 | #### |
||
74 | # III. Add the line at the end. |
||
75 | array_push( |
||
76 | $data, |
||
77 | empty($lastname) ? |
||
78 | sprintf( |
||
79 | "[%s:%s] %s : %s", |
||
80 | date("H"), |
||
81 | date("i"), |
||
82 | $firstname, # Don't normalize the name. |
||
83 | $message |
||
84 | ) : |
||
85 | sprintf( |
||
86 | "[%s:%s] %s %s : %s", |
||
87 | date("H"), |
||
88 | date("i"), |
||
89 | ucfirst( |
||
90 | strtolower( |
||
91 | $firstname |
||
92 | ) |
||
93 | ), |
||
94 | ucfirst( |
||
95 | strtolower( |
||
96 | $lastname |
||
97 | ) |
||
98 | ), |
||
99 | $message |
||
100 | ) |
||
101 | ); |
||
102 | |||
103 | #### |
||
104 | # IV. Now dump the array to the file. |
||
105 | atomized_put_contents( |
||
106 | $chatFile, |
||
107 | implode( |
||
108 | PHP_EOL, |
||
109 | $data |
||
110 | ) |
||
111 | ); |
||
112 | } |