corrade-http-templates – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | eva | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
6 | ## This is a script that stores group messages to a local file that can ## |
||
7 | ## then be read-in by other scripts and displayed. The script will only ## |
||
8 | ## write to a file "chat.log" in the current directory and the name of ## |
||
9 | ## the chat log ("chat.log") is hard-coded because it depends on this ## |
||
10 | ## script's HTML counter-part that has the name hard-coded too. ## |
||
11 | ########################################################################### |
||
12 | |||
13 | ########################################################################### |
||
14 | ## CONFIGURATION ## |
||
15 | ########################################################################### |
||
16 | |||
17 | # Set this to the name of the group. |
||
18 | $GROUP = 'My Group'; |
||
19 | # How many chat lines to store? |
||
20 | $CHAT_LINES = 10; |
||
21 | # Set this to the group password. |
||
22 | $PASSWORD = 'mypassword'; |
||
23 | # Set this to Corrade's HTTP Server URL. |
||
24 | $URL = 'http://corrade.hostname:8080'; |
||
25 | |||
26 | ########################################################################### |
||
27 | ## INTERNALS ## |
||
28 | ########################################################################### |
||
29 | |||
30 | # Check if this is the group chat notification. |
||
31 | if(!isset($_POST['type']) || $_POST['type'] != "group") return; |
||
32 | # Check that the notification is for the configured group. |
||
33 | if(!isset($_POST['group']) || $_POST['group'] != $GROUP) return; |
||
34 | # Bail if "firstname", "lastname" or the "message" variables are blank. |
||
35 | if($_POST['firstname'] == '' |
||
36 | || $_POST['lastname'] == '' |
||
37 | || $_POST['message'] == '') return; |
||
38 | |||
39 | ########################################################################### |
||
40 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
41 | ########################################################################### |
||
42 | function atomized_put_contents($file, $data) { |
||
43 | $fp = fopen($file, "w+"); |
||
44 | if (flock($fp, LOCK_EX)) { |
||
45 | fwrite($fp, $data); |
||
46 | fflush($fp); |
||
47 | flock($fp, LOCK_UN); |
||
48 | } |
||
49 | fclose($fp); |
||
50 | } |
||
51 | ########################################################################### |
||
52 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
53 | ########################################################################### |
||
54 | function atomized_get_contents($file) { |
||
55 | $fp = fopen($file, "r+"); |
||
56 | $ct = ''; |
||
57 | if (flock($fp, LOCK_SH)) { |
||
58 | if (filesize($file)) { |
||
59 | $ct = fread($fp, filesize($file)); |
||
60 | } |
||
61 | flock($fp, LOCK_UN); |
||
62 | } |
||
63 | fclose($fp); |
||
64 | return $ct; |
||
65 | } |
||
66 | |||
67 | #### |
||
68 | # I. Initialize a new blank array. |
||
69 | $data = array(); |
||
70 | |||
71 | #### |
||
72 | # II. If the file exists, trim it to the number of chat lines. |
||
73 | switch(file_exists("chat.log")) { |
||
74 | case TRUE: |
||
75 | # Open the chat log and trim the lines to the configured line size. |
||
76 | $data = explode( |
||
77 | PHP_EOL, |
||
78 | atomized_get_contents( |
||
79 | "chat.log" |
||
80 | ) |
||
81 | ); |
||
82 | while(sizeof($data) > $CHAT_LINES) |
||
83 | array_shift($data); |
||
84 | break; |
||
85 | } |
||
86 | |||
87 | #### |
||
88 | # III. Add the line at the end including the date. |
||
89 | array_push( |
||
90 | $data, |
||
91 | sprintf( |
||
92 | "[%s:%s] %s %s : %s", |
||
93 | date("H"), |
||
94 | date("i"), |
||
95 | $_POST['firstname'], |
||
96 | $_POST['lastname'], |
||
97 | $_POST['message'] |
||
98 | ) |
||
99 | ); |
||
100 | |||
101 | #### |
||
102 | # IV. Now dump the array to the file. |
||
103 | atomized_put_contents( |
||
104 | "chat.log", |
||
105 | implode( |
||
106 | PHP_EOL, |
||
107 | $data |
||
108 | ) |
||
109 | ); |
||
110 | |||
111 | ?> |