ch34s02.html 39.5 KB
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Step 1: Parsing the log file</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch34.html" title="Chapter 34. Showing SPAM statistics"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Step 1: Parsing the log file</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 34. Showing SPAM statistics</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Step 1: Parsing the log file"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2620470"></a>Step 1: Parsing the log file</h2></div></div></div>
            
            <p>The log file we will be parsing is the standard <span class="command"><strong>procmail</strong></span> log.
                An authentic example of the log file from our main mail server (that receives around
                ~10,000 spam mails per month) is shown below (slightly anonymized to remove the real
                name of the mailbox)</p>
            <p>
                </p><pre class="screen">...
From sterne@gvt.net.br  Thu Jul 16 00:03:32 2009
 Subject: *** SPAM (5.3) *** =?koi8-r?B?79DUyc3J2sHDydEg08HK1ME=?=
  Folder: /dev/null                                                       14278
From yhrtjgommsg@boomboomroom.com  Thu Jul 16 00:13:33 2009
 Subject: *** SPAM (6.7) *** Women will be begging you to sleep with you.
  Folder: /srv/mail/john/.Spam/new/1247696013.18366_0.lambda                2070
From ErikaFrazier12@aol.com  Thu Jul 16 00:13:33 2009
 Subject: *** SPAM (41.5) *** Obama Allows Meds Sold Online
  Folder: /dev/null 
...</pre><p>
            </p>
            <p>Each entry for a received mail in the log are three lines, sender, subject and the
                folder the mail will be stored in. For mails that are identified with (according to
                this setup) 100% confidence as spams the destination is set to
                    <code class="filename">/dev/null</code> , i.e they are immediately deleted. For mails
                that are determined to be spams but where there might be a chance that they are
                legitimate they are store in the users Spam folder. From the above log we can see
                that two mails are deleted immediately and one mail is stored in the users Spam
                folder.</p>
            <p>To analyze this we will create the class <code class="code">ParseProcmailLogFile</code> the
                constructor will take a file name of the log file as the only argument and to get
                hold of the statistics we use the method </p>
            <p>
                </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                        <p><code class="code">ParseProcmailLogFile::GetStat($aWindowSize)</code></p>
                        <p><code class="code">$aWindowSize</code> is the number of days back the stats should
                            be based on. The returned statistics will be an array with three array
                            elements with the following layout:</p>
                        <p><code class="code">($dateArray, nbrDevNull, nbrSpamFolder)</code></p>
                        <p>These returned values are both arrays indexed by date and value the
                            number of spams for that date (key)</p>
                    </li></ul></div><p>
            </p>
            <p>
                </p><div class="caution" title="Caution" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Caution</h3>
                    <p>Remember that the PHP process or user running the script must have read
                        privileges for the log file.</p>
                </div><p>
            </p>
            <p>We will not walk through the parsing class in any more details than what is given
                in the phpdoc comments in the source below</p>
            <p>
                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
</span><span class="hl-comment">/*</span><span class="hl-comment">*
* Class ParseProcmailLogFile
</span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ParseProcmailLogFile</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-var">$iFileName</span><span class="hl-code">=</span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">;
    </span><span class="hl-comment">/*</span><span class="hl-comment">*
    * Constructor for the parse class
    *
    *</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">mixed $aFileName  Log file to read
    *</span><span class="hl-inlinedoc"> @return </span><span class="hl-comment">ParseProcmailLogFile
    </span><span class="hl-comment">*/</span><span class="hl-code">
    </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">__construct</span><span class="hl-brackets">(</span><span class="hl-var">$aFileName</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-var">$this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">iFileName</span><span class="hl-code"> = </span><span class="hl-var">$aFileName</span><span class="hl-code"> ;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-comment">/*</span><span class="hl-comment">*
    * Get line without trailing &quot;\n&quot;
    *
    *</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">mixed $fp Filepointer
    *</span><span class="hl-inlinedoc"> @return </span><span class="hl-comment">string Read line without trailing &quot;\n&quot;
    </span><span class="hl-comment">*/</span><span class="hl-code">
    </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">GetLine</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-var">$s</span><span class="hl-code"> = </span><span class="hl-identifier">fgets</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">substr</span><span class="hl-brackets">(</span><span class="hl-var">$s</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-identifier">strlen</span><span class="hl-brackets">(</span><span class="hl-var">$s</span><span class="hl-brackets">)</span><span class="hl-code">-</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-comment">/*</span><span class="hl-comment">*
    * Get statistics from the parsed log file
    *
    *</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">$aWindow Window size. How many days to include in the returned stats
    *</span><span class="hl-inlinedoc"> @return </span><span class="hl-comment">array with (date, number of killed spam, number of non killed spams)
    </span><span class="hl-comment">*/</span><span class="hl-code">
   </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">GetStat</span><span class="hl-brackets">(</span><span class="hl-var">$aWindow</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
 
        </span><span class="hl-var">$fp</span><span class="hl-code"> = </span><span class="hl-identifier">fopen</span><span class="hl-brackets">(</span><span class="hl-var">$this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">iFileName</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">r</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$fp</span><span class="hl-code"> === </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-identifier">JpGraphError</span><span class="hl-code">::</span><span class="hl-identifier">Raise</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Cannot read log file</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-comment">//</span><span class="hl-comment"> Now loop through the file. We always keep the last 3 lines in</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> the buffer to be able to get the context of a line since the</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> folder is stored on one line and the date of the main on the</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> previous line</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetLine</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetLine</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">;
 
        </span><span class="hl-var">$idx</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
        </span><span class="hl-var">$idx2</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
        </span><span class="hl-var">$found</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> All /dev/null spam headers</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-var">$found2</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> All Spam folder headers</span><span class="hl-comment"></span><span class="hl-code">
 
        </span><span class="hl-comment">//</span><span class="hl-comment"> Loop through all lines in the file and store the found emails</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> in the two $found arrays</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-reserved">while</span><span class="hl-brackets">(</span><span class="hl-code"> ! </span><span class="hl-identifier">feof</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
 
            </span><span class="hl-comment">//</span><span class="hl-comment">Shift buffer one step</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">;
            </span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-code">;
            </span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetLine</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">;
 
            </span><span class="hl-comment">//</span><span class="hl-comment"> Find /dev/null entries</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">strpos</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">Folder: /dev/null</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> !== </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">strpos</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">From </span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> !== </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    </span><span class="hl-var">$datepos</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code"> ;
                </span><span class="hl-brackets">}</span><span class="hl-code">
                </span><span class="hl-reserved">elseif</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">strpos</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">From </span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> !== </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    </span><span class="hl-var">$datepos</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code"> ;
                </span><span class="hl-brackets">}</span><span class="hl-code">
                </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    </span><span class="hl-reserved">continue</span><span class="hl-code">;
                </span><span class="hl-brackets">}</span><span class="hl-code">
                </span><span class="hl-comment">//</span><span class="hl-comment"> Aggregate all the data per day</span><span class="hl-comment"></span><span class="hl-code">
                </span><span class="hl-var">$date</span><span class="hl-code"> = </span><span class="hl-identifier">strtotime</span><span class="hl-brackets">(</span><span class="hl-identifier">date</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">D j M Y</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-identifier">strtotime</span><span class="hl-brackets">(</span><span class="hl-identifier">substr</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-var">$datepos</span><span class="hl-brackets">]</span><span class="hl-code">,-</span><span class="hl-number">24</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
                </span><span class="hl-var">$found</span><span class="hl-brackets">[</span><span class="hl-var">$idx</span><span class="hl-code">++</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-identifier">str_replace</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string"> Subject: </span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">,</span><span class="hl-var">$date</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-brackets">}</span><span class="hl-code">
 
            </span><span class="hl-comment">//</span><span class="hl-comment"> Find spam folder entries</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">strpos</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">.Spam</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> !== </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">strpos</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">From </span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> !== </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    </span><span class="hl-var">$datepos</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code"> ;
                </span><span class="hl-brackets">}</span><span class="hl-code">
                </span><span class="hl-reserved">elseif</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">strpos</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">From </span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> !== </span><span class="hl-reserved">false</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    </span><span class="hl-var">$datepos</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code"> ;
                </span><span class="hl-brackets">}</span><span class="hl-code">
                </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    </span><span class="hl-reserved">continue</span><span class="hl-code">;
                </span><span class="hl-brackets">}</span><span class="hl-code">
                </span><span class="hl-comment">//</span><span class="hl-comment"> Aggregate all the data per day</span><span class="hl-comment"></span><span class="hl-code">
                </span><span class="hl-var">$date</span><span class="hl-code"> = </span><span class="hl-identifier">strtotime</span><span class="hl-brackets">(</span><span class="hl-identifier">date</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">D j M Y</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-identifier">strtotime</span><span class="hl-brackets">(</span><span class="hl-identifier">substr</span><span class="hl-brackets">(</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-var">$datepos</span><span class="hl-brackets">]</span><span class="hl-code">,-</span><span class="hl-number">24</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
                </span><span class="hl-var">$found2</span><span class="hl-brackets">[</span><span class="hl-var">$idx2</span><span class="hl-code">++</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-identifier">str_replace</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string"> Subject: </span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-var">$buf</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">,</span><span class="hl-var">$date</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-identifier">fclose</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">;
 
        </span><span class="hl-comment">//</span><span class="hl-comment"> Find out how many at each day of dev null</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-var">$date</span><span class="hl-code"> = </span><span class="hl-var">$found</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">;
        </span><span class="hl-var">$daystat</span><span class="hl-brackets">[</span><span class="hl-var">$date</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
 
        </span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-var">$i</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-var">$i</span><span class="hl-code"> &lt; </span><span class="hl-var">$idx</span><span class="hl-code">; ++</span><span class="hl-var">$i</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$date</span><span class="hl-code"> == </span><span class="hl-var">$found</span><span class="hl-brackets">[</span><span class="hl-var">$i</span><span class="hl-brackets">]</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                ++</span><span class="hl-var">$daystat</span><span class="hl-brackets">[</span><span class="hl-var">$date</span><span class="hl-brackets">]</span><span class="hl-code">;
            </span><span class="hl-brackets">}</span><span class="hl-code">
            </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                </span><span class="hl-var">$date</span><span class="hl-code"> = </span><span class="hl-var">$found</span><span class="hl-brackets">[</span><span class="hl-var">$i</span><span class="hl-brackets">]</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">;
                </span><span class="hl-var">$daystat</span><span class="hl-brackets">[</span><span class="hl-var">$date</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
            </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-comment">//</span><span class="hl-comment"> Find out how many at each day of spam</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-var">$daystat2</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-var">$found2</span><span class="hl-brackets">)</span><span class="hl-code"> &gt; </span><span class="hl-number">0</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-var">$date</span><span class="hl-code"> = </span><span class="hl-var">$found2</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">;
            </span><span class="hl-var">$daystat2</span><span class="hl-brackets">[</span><span class="hl-var">$date</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
 
            </span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-var">$i</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-var">$i</span><span class="hl-code"> &lt; </span><span class="hl-var">$idx2</span><span class="hl-code">; ++</span><span class="hl-var">$i</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$date</span><span class="hl-code"> == </span><span class="hl-var">$found2</span><span class="hl-brackets">[</span><span class="hl-var">$i</span><span class="hl-brackets">]</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    ++</span><span class="hl-var">$daystat2</span><span class="hl-brackets">[</span><span class="hl-var">$date</span><span class="hl-brackets">]</span><span class="hl-code">;
                </span><span class="hl-brackets">}</span><span class="hl-code">
                </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                    </span><span class="hl-var">$date</span><span class="hl-code"> = </span><span class="hl-var">$found2</span><span class="hl-brackets">[</span><span class="hl-var">$i</span><span class="hl-brackets">]</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">;
                    </span><span class="hl-var">$daystat2</span><span class="hl-brackets">[</span><span class="hl-var">$date</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
                </span><span class="hl-brackets">}</span><span class="hl-code">
            </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-comment">//</span><span class="hl-comment"> Now make sure that both arrays have the same</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> number of entries.</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-reserved">foreach</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$daystat</span><span class="hl-code"> </span><span class="hl-reserved">as</span><span class="hl-code"> </span><span class="hl-var">$key</span><span class="hl-code"> =&gt; </span><span class="hl-var">$val</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> !</span><span class="hl-reserved">isset</span><span class="hl-brackets">(</span><span class="hl-var">$daystat2</span><span class="hl-brackets">[</span><span class="hl-var">$key</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                </span><span class="hl-var">$daystat2</span><span class="hl-brackets">[</span><span class="hl-var">$key</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
            </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-reserved">foreach</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$daystat2</span><span class="hl-code"> </span><span class="hl-reserved">as</span><span class="hl-code"> </span><span class="hl-var">$key</span><span class="hl-code"> =&gt; </span><span class="hl-var">$val</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> !</span><span class="hl-reserved">isset</span><span class="hl-brackets">(</span><span class="hl-var">$daystat</span><span class="hl-brackets">[</span><span class="hl-var">$key</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                </span><span class="hl-var">$daystat</span><span class="hl-brackets">[</span><span class="hl-var">$key</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
            </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-comment">//</span><span class="hl-comment"> Window and return the data</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-var">$n</span><span class="hl-code"> = </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-var">$daystat</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-var">$start</span><span class="hl-code"> = </span><span class="hl-var">$n</span><span class="hl-code"> &gt; </span><span class="hl-var">$aWindow</span><span class="hl-code"> ? </span><span class="hl-var">$n</span><span class="hl-code"> - </span><span class="hl-var">$aWindow</span><span class="hl-code"> : </span><span class="hl-number">0</span><span class="hl-code">;
        </span><span class="hl-var">$date_keys</span><span class="hl-code"> = </span><span class="hl-identifier">array_keys</span><span class="hl-brackets">(</span><span class="hl-var">$daystat</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-var">$idx</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;
 
        </span><span class="hl-var">$datax</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-var">$datay1</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-var">$datay2</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$i</span><span class="hl-code">=</span><span class="hl-var">$start</span><span class="hl-code">; </span><span class="hl-var">$i</span><span class="hl-code"> &lt; </span><span class="hl-var">$n</span><span class="hl-code">; ++</span><span class="hl-var">$i</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-var">$datax</span><span class="hl-brackets">[</span><span class="hl-var">$idx</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-identifier">date</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">D j M</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-var">$date_keys</span><span class="hl-brackets">[</span><span class="hl-var">$i</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-var">$datay1</span><span class="hl-brackets">[</span><span class="hl-var">$idx</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$daystat</span><span class="hl-brackets">[</span><span class="hl-var">$date_keys</span><span class="hl-brackets">[</span><span class="hl-var">$i</span><span class="hl-brackets">]</span><span class="hl-brackets">]</span><span class="hl-code">;
            </span><span class="hl-var">$datay2</span><span class="hl-brackets">[</span><span class="hl-var">$idx</span><span class="hl-code">++</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$daystat2</span><span class="hl-brackets">[</span><span class="hl-var">$date_keys</span><span class="hl-brackets">[</span><span class="hl-var">$i</span><span class="hl-brackets">]</span><span class="hl-brackets">]</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
 
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-var">$datax</span><span class="hl-code">,</span><span class="hl-var">$datay1</span><span class="hl-code">,</span><span class="hl-var">$datay2</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
            </p>
        </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="ch34.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>