Odyssey
create_emailreport.prg
1 #!/usr/local/bin/php
2 <?php
3  // ** REQUIRED FILES
4  require("XRC_class.prg");
5 
6  // **
7  $xrc_logfile = "/tmp/xrc_everyone.log";
8  $xrc_directory = "/home/httpd/hcupriv/";
9  $xrc_prefix = "emailrpt";
10 
11  $xrc_filename = $xrc_prefix . date("Ym");
12 
13  // ** Put the argument list in the cl_args Array
14  $cl_args = arguments($argv);
15 
16  // ** this will limit the number of clients to retrieve -- used for debugging
17  // ** ie --limit=3
18  $limit = 0;
19  if (isset($cl_args['limit'])) {
20  $limit = intval($cl_args['limit']);
21  }
22 
23  // ** Always include zero values -- the report will choose to print them or not
24  $ALL = true;
25 /*
26  if (isset($cl_args['ALL'])) {
27  $ALL = true;
28  }
29 */
30 
31  // ** When this is set it will write the send request information to the log
32  // ** ie -debug
33  $debug = false;
34  if (isset($cl_args['debug'])) {
35  $debug = true;
36  }
37 
38  /* ***
39  Originally this form was to be run from the web browser..
40  However, do to the length of time it can take to run, I am changing it
41  to be a command line program run from a crontab entry at the last day of the
42  month
43 
44  crontab entry -- executed on mork for reporting needs
45  // * This program will need 10 minutes to run...
46  // ** Run early on the first day of the month
47  /usr/local/bin???/create_emailreport.prg
48  0 0 1 * * /usr/local/bin???/create_emailreport.prg
49  */
50  set_time_limit(0);
51 
52 
53  $start_time = date("m/d/Y h:i:s");
54 
55  $xrc_fh = @fopen($xrc_logfile, "w");
56  $rpt_fh = @fopen($xrc_directory . $xrc_filename, "w");
57  if (!$xrc_fh) {
58  exit;
59  }
60  // ** Write the Starting time to the logfile
61  fwrite ($xrc_fh, "** START LOG FILE -- $start_time **\n");
62 
63  if (!$rpt_fh) {
64  fwrite ($xrc_fh, "Unable to open output file for reporting domain email");
65  end_xrc();
66  exit;
67  }
68 
69  // ** Remove any files older than 15 months
70  // --- Create a date that is 15months old
71  $RemoveLogDate = date("Ym", mktime(0, 0, 0, date("m") - 15, date("d"), date("Y")));
72  if (is_dir($xrc_directory)) {
73  if ($dir_hndl = opendir($xrc_directory)) {
74  while (($file = readdir($dir_hndl)) !== false) {
75  if (filetype($xrc_directory . $file) == "file" && (substr($file, 0, strlen($xrc_prefix)) == $xrc_prefix)) {
76  if (substr($file, strlen($xrc_prefix)) <= $RemoveLogDate) {
77  // ** Delete File
78  unlink($xrc_directory . $file);
79  }
80  }
81  }
82  closedir($dir_hndl);
83  }
84  }
85  // $>setReturnObject('{value, 'AliasInfo', 'EmailClientSummary', 'WebmailPresentation', 'Letter', 'MailboxSummary', 'FolderSummary', 'ForwardingInfo', 'AutoResponse', 'CreateUserOptions', 'UserAuthenticationInfo', 'ClientPreferences', 'WebMailPreferences', 'MatchingAccount'}, '{single, list}');
86 
87 
88  $report_XRC = new XRC_API();
89  $client_XRC = new XRC_API();
90  $acct_XRC = new XRC_API();
91 
92  // ** Get the client information --
93  if ($debug) fwrite ($xrc_fh, "Command - summarizeEmailClientsOfDistributor");
94  $report_XRC->setCmd('summarizeEmailClientsOfDistributor');
95  $report_XRC->SendRequest(true);
96  if ($debug) fwrite ($xrc_fh, " -- Completed\n");
97 
98  $DefaultDomainOfferID = 0;
99  if (is_array($report_XRC->values[0])) {
100  foreach ($report_XRC->values[0] as $key=>$value) {
101 
102  // ** RETRIEVE THE default offerID for this domain.. Pickup what's in the first position
103  $DefaultDomainOfferID = $value['offerIDs'][0];
104 
105  $client_count = 0;
106  $mailsize_100 = 0;
107  $mailsize_250 = 0;
108  $mailsize_1000 = 0;
109  $mailsize_2000 = 0;
110  $mailsize_100_3yrArchive = 0;
111  $mailsize_100_5yrArchive = 0;
112  $mailsize_100_7yrArchive = 0;
113 
114  // ** Creates an array of objects
115  // ** The following can be uncommented for debugging -- this will allow you to limit the number
116 
117  if (($loop_cntr >= $limit) && $limit > 0) {
118  break;
119  } else {
120  $loop_cntr++;
121  }
122 
123  $client_XRC->setCmd('listUserNamesOfClient');
124  $client_XRC->addArgs("I", trim($value['clientID']));
125  if ($debug) fwrite ($xrc_fh, "Command - listUserNamesOfClient:" . trim($value['clientID']));
126  $client_XRC->SendRequest(true);
127  if ($debug) fwrite ($xrc_fh, " -- Completed\n");
128 
129  if ($client_XRC->is_success) {
130 
131  // ** Lookup the client mailbox size for each account...
132  foreach ($client_XRC->values[0] as $client_key=>$client_value) {
133 
134  $acct_XRC->setCmd('getUserOffers');
135  $acct_XRC->addArgs("I", trim($value['clientID']));
136  $acct_XRC->addArgs("S", trim($client_value));
137 
138  // *** SEND REQUEST
139  $acct_XRC->SendRequest(true);
140 
141  $CurrentUserOfferID = $acct_XRC->values[0][0];
142 
143 
144  $OfferIDtoUse = ($CurrentUserOfferID > 0 ? $CurrentUserOfferID : $DefaultDomainOfferID);
145 // print "\n " . $value['clientID'] . " :: " . $DefaultDomainOfferID . " :: " . $CurrentUserOfferID . " :: " . $OfferIDtoUse . "\n";
146  switch ($OfferIDtoUse) {
147  case "6032":
148  case "6033":
149  $mailsize_100++;
150  break;
151  case "6034":
152  case "6035":
153  $mailsize_250++;
154  break;
155  case "6036":
156  case "6037":
157  $mailsize_1000++;
158  break;
159  case "6038":
160  case "6039":
161  $mailsize_2000++;
162  break;
163  case "12152":
164  case "12153":
165  $mailsize_100_3yrArchive++;
166  break;
167  case "12154":
168  case "12155":
169  $mailsize_100_5yrArchive++;
170  break;
171  case "12156":
172  case "12157":
173  $mailsize_100_7yrArchive++;
174  break;
175  }
176  }
177 
178  $client_count = count($client_XRC->values[0]);
179  }
180 // print "\nToTALS: 100::$mailsize_100 250::$mailsize_250 1000::$mailsize_1000 2000::$mailsize_2000 1003yr::$mailsize_100_3yrArchive 1005yr::$mailsize_100_5yrArchive 1007yr::$mailsize_100_7yrArchive ";
181  $domain_results[] = array($value['primaryEmailDomain'], $client_count, $mailsize_100, $mailsize_250, $mailsize_1000, $mailsize_2000, $mailsize_100_3yrArchive, $mailsize_100_5yrArchive, $mailsize_100_7yrArchive);
182  }
183  sort($domain_results);
184  }
185  foreach ($domain_results as $dom_key=>$dom_value) {
186  $dom_accts = 0;
187  $dom_accts_prem = 0;
188  $dom_credit = 10;
189  $dom_total_bill = 0;
190  $tr_class = ($tr_class == "odd_small" ? "even_small" : "odd_small");
191  // ** Add up the items in each bucket
192  $dom_accts = intval($dom_value[2]) + intval($dom_value[3]) + intval($dom_value[4]) + intval($dom_value[5]) + intval($dom_value[6]) + intval($dom_value[7]) + intval($dom_value[8]);
193  // ** Add up the premium account types
194  $dom_accts_prem = (intval($dom_value[3]) * 1) + (intval($dom_value[4]) * 2) + (intval($dom_value[5]) * 3) + (intval($dom_value[6]) * 3) + (intval($dom_value[7]) * 4) + (intval($dom_value[8]) * 5);
195  $dom_total_bill = ($dom_accts - $dom_credit);
196  // ** Premium is ALWAYS ADDED TO THE PRICE -- Credit does not affect the premium
197  $dom_total_bill = ($dom_total_bill <= 0 ? 0 : $dom_total_bill) + $dom_accts_prem;
198 
199 // ** ONLY PRINT FOR NON-ZERO BILLS
200  if ($dom_total_bill > 0 || $ALL) {
201  fwrite ($rpt_fh, "{$dom_value[0]}\t{$dom_value[1]}\t{$dom_value[2]}\t{$dom_value[3]}\t{$dom_value[4]}\t{$dom_value[5]}\t{$dom_value[6]}\t{$dom_value[7]}\t{$dom_value[8]}\t{$dom_accts}\t{$dom_credit}\t{$dom_accts_prem}\t{$dom_total_bill}\t\n");
202  }
203  }
204 
205  end_xrc();
206 
207 function end_xrc () {
208  global $xrc_fh, $rpt_fh;
209 
210  $end_time = date("m/d/Y h:i:s");
211 
212  if ($xrc_fh) {
213  fwrite ($xrc_fh, "\n** END LOG FILE -- $end_time **\n");
214  fclose($xrc_fh);
215  }
216  if ($rpt_fh) {
217  fclose($rpt_fh);
218  }
219 }
220 function arguments($argv) {
221  $_ARG = array();
222  foreach ($argv as $arg) {
223  if (preg_match('/--[a-zA-Z0-9]*=.*/',$arg)) {
224  $str = explode("=",$arg); $arg = '';
225  $key = str_replace("--",'',$str[0]);
226  for ( $i = 1; $i < count($str); $i++ ) {
227  $arg .= $str[$i];
228  }
229  $_ARG[$key] = $arg;
230  } elseif(preg_match('/-[a-zA-Z0-9]/',$arg)) {
231  $arg = str_replace("-",'',$arg);
232  $_ARG[$arg] = 'true';
233  }
234  }
235  return $_ARG;
236 }