corrade-http-templates – Blame information for rev 51
?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 reads a visitors log and generates a json ## |
||
7 | ## object from the number of males and females that Corrade detected. ## |
||
8 | ########################################################################### |
||
9 | |||
10 | ########################################################################### |
||
11 | ## CONFIGURATION ## |
||
12 | ########################################################################### |
||
13 | |||
14 | # Visitors file. |
||
15 | $VISITOR_FILE = "visitors.log"; |
||
16 | |||
17 | ########################################################################### |
||
18 | ## INTERNALS ## |
||
19 | ########################################################################### |
||
20 | |||
21 | ########################################################################### |
||
22 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
23 | ########################################################################### |
||
24 | function atomized_get_contents($file) { |
||
25 | $fp = fopen($file, "r+"); |
||
26 | $ct = ''; |
||
27 | if (flock($fp, LOCK_SH)) { |
||
28 | if (filesize($file)) { |
||
29 | $ct = fread($fp, filesize($file)); |
||
30 | } |
||
31 | flock($fp, LOCK_UN); |
||
32 | } |
||
33 | fclose($fp); |
||
34 | return $ct; |
||
35 | } |
||
36 | |||
37 | /////////////////////////////////////////////////////////////////////////// |
||
38 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
||
39 | /////////////////////////////////////////////////////////////////////////// |
||
40 | function wasCSVToArray($csv) { |
||
41 | $l = array(); |
||
42 | $s = array(); |
||
43 | $m = ""; |
||
44 | for ($i = 0; $i < strlen($csv); ++$i) { |
||
45 | switch ($csv{$i}) { |
||
46 | case ',': |
||
47 | if (sizeof($s) == 0 || !current($s) == '"') { |
||
48 | array_push($l, $m); |
||
49 | $m = ""; |
||
50 | break; |
||
51 | } |
||
52 | $m .= $csv{$i}; |
||
53 | continue; |
||
54 | case '"': |
||
55 | if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) { |
||
56 | $m .= $csv{$i}; |
||
57 | ++$i; |
||
58 | break; |
||
59 | } |
||
60 | if (sizeof($s) == 0|| !current($s) == $csv[$i]) { |
||
61 | array_push($s, $csv{$i}); |
||
62 | continue; |
||
63 | } |
||
64 | array_pop($s); |
||
65 | break; |
||
66 | default: |
||
67 | $m .= $csv{$i}; |
||
68 | break; |
||
69 | } |
||
70 | } |
||
71 | array_push($l, $m); |
||
72 | return $l; |
||
73 | } |
||
74 | |||
75 | $f = 0; |
||
76 | $m = 0; |
||
77 | |||
78 | array_walk( |
||
79 | explode( |
||
80 | PHP_EOL, |
||
81 | atomized_get_contents( |
||
82 | $VISITOR_FILE |
||
83 | ) |
||
84 | ), |
||
85 | function($e, $k) use(&$f, &$m) { |
||
86 | switch(wasCSVToArray($e)[1]) { |
||
87 | case 'female': |
||
88 | ++$f; |
||
89 | break; |
||
90 | default: |
||
91 | ++$m; |
||
92 | break; |
||
93 | } |
||
94 | } |
||
95 | ); |
||
96 | |||
97 | header('content-type: application/json; charset=utf-8'); |
||
98 | echo json_encode( |
||
99 | array( |
||
100 | "Male" => $m, |
||
101 | "Female" => $f |
||
102 | ) |
||
103 | ); |
||
104 | |||
105 | ?> |