corrade-http-templates – Diff between revs 2 and 4

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