Added rantest and cqtool to repo
[clearscm.git] / rantest / web / php / Utils.php
1 <?php
2 ////////////////////////////////////////////////////////////////////////////////
3 //
4 // File:        Utils.php
5 // Description: Utility funcitons
6 // Author:      Andrew@ClearSCm.com
7 // Created:     Mon Apr 28 15:20:06 MST 2008
8 // Modified:    
9 // Language:    PHP
10 //
11 // (c) Copyright 2008, General Dynamics, all rights reserved.
12 //
13 // All rights reserved except as subject to DFARS 252.227-7014 of contract
14 // number CP02H8901N issued under prime contract N00039-04-C-2009.
15 //
16 // Warning: This document contains technical data whose export is restricted
17 // by the Arms Export Control Act (Title 22, U.S.C., Sec 2751, et seq.) or the
18 // Export Administration Act of 1979, as amended, Title, 50, U.S.C., App. 2401
19 // et seq. Violations of these export laws are subject to severe criminal
20 // penalties. Disseminate in accordance with provisions of DoD Directive
21 // 5230.25.
22 //
23 ////////////////////////////////////////////////////////////////////////////////
24 // Constants
25 $VERSION = "1.2.4";
26
27 $webdir = dirname  ($_SERVER["PHP_SELF"]);
28
29 function debug ($msg) {
30   global $debug;
31
32   if ($debug == 1) {
33     print "<font color=red>DEBUG:</font> $msg<br>";
34   } // if
35 } // debug
36
37 function dumpObject ($object) {
38   print "<pre>";
39   print_r ($object);
40   print "</pre>";
41 } // dumpObject
42
43 function Error ($msg) {
44   print "<font color=red><b>ERROR:</b></font> $msg<br>";
45 } // Error
46
47 function banner () {
48   $banner = <<<END
49 <table class="banner" width="100%" border="0" cellpadding="0" cellspacing="0">
50   <tr>
51     <td class="bannerLeft"><a href="/Rantest">
52     <img src="/images/companyLogo.gif" alt="General Dynamics C4 Systems" border="0"></a></td>
53     <td class="bannerRight">&nbsp;</td>
54   </tr>
55 </table>
56 END;
57
58   return $banner;
59 } // banner
60
61 function YMD2MDY ($date) {
62   return substr ($date, 5, 2) . "/" .
63          substr ($date, 8, 2) . "/" .
64          substr ($date, 0, 4);
65 } // YMD2MDY
66
67 function MDY2YMD ($date) {
68   return substr ($date, 6, 4) . "-" .
69          substr ($date, 0, 2) . "-" .
70          substr ($date, 3, 2);
71 } // MDY2YMD
72
73 function cmpStr ($a, $b, $field, $direction) {
74   if ($direction == "ascending") {
75     return strcmp ($a[$field], $b[$field]);
76   } else {
77     return strcmp ($b[$field], $a[$field]);
78   } // if
79 } // cmpStr
80
81 function cmpNbr ($a, $b, $field, $direction) {
82   if ($a[$field] == $b[$field]) {
83     return 0;
84   } // if
85
86   if ($direction == "ascending") {
87     return ($a[$field] < $b[$field]) ? -1 : 1;
88   } else {
89     return ($a[$field] < $b[$field]) ? 1 : -1;
90   } // if
91 } // cmpNbr
92
93 function getUsername ($userid) {
94   exec ("ypmatch $userid passwd | cut -f5 -d:", &$username);
95
96   if (empty ($username)) {
97     return "Unknown";
98   } else {
99     if ($username[0] == "SWIT account for SWIT testing") {
100       return "pswit";
101     } else {
102       return $username[0];
103     } // if
104   } // if
105 } // getUsername
106
107 function emailUsersDropdown () {
108   define (DIM, "#888");
109
110   exec ("ypcat passwd | grep ^p|cut -f1,5 -d: | grep -v 'SWIT account'", &$userids);
111
112   $users ["unknown"] = "&lt;Select User&gt;";
113
114   foreach ($userids as $user) {
115     list ($pnbr, $name) = explode (":", $user);
116
117     $users [$pnbr] = $name;
118   } # foreach
119
120   asort ($users);
121
122   $dropdown = "Email to <select name=user class=inputfield style=\"color: " . DIM . "\">";
123   
124   foreach ($users as $pnbr => $name) {
125     $dropdown .= "<option value=\"$pnbr:$name\" style=\"color: ";
126
127     if ($pnbr != "unknown") {
128       $dropdown .= "black";
129     } else {
130       $dropdown .= DIM;
131     } // if
132
133     $dropdown .= "\">$name</option>";
134   } // foreach
135
136   $dropdown .= "</select>";
137
138   return $dropdown;
139 } // emailUsersDropdown
140
141 function copyright () {
142   global $VERSION;
143
144   $year = date ("Y");
145
146   $thisFile      = "$_SERVER[DOCUMENT_ROOT]/$_SERVER[PHP_SELF]";
147   $lastModified = date ("F d Y @ g:i a", filemtime ($thisFile));
148
149   print <<<END
150 <div class=copyright>
151 Rantest Web Version: <a href="ChangeLog.php#$VERSION">$VERSION</a> - EAST Automation Team<br>
152 Last Modified: $lastModified<br>
153 Copyright $year &copy; General Dynamics, all rights reserved<br>
154 <a href="/"><img border=0 src="/images/HomeSmall.gif">Home</a>
155 &nbsp;|&nbsp;
156 <a href="http://ranweb/dokuwiki/doku.php">Wiki</a>
157 &nbsp;|&nbsp;
158 <a href="http://ranweb/dokuwiki/doku.php?id=ran:ran_test_automation_users_guide">Users Guide</a>
159 &nbsp;|&nbsp;
160 <a href="http://ranweb/dokuwiki/doku.php?id=ran:rantest">Usage</a>
161 &nbsp;|&nbsp;
162 <a href="/docs">Other Docs</a><br>
163 </div>
164 END;
165 } // copyright
166
167 function Today2SQLDatetime () {
168   return date ("Y-m-d H:i:s");
169 } // Today2SQLDatetime
170
171 function FormatDuration ($difference) {
172   $seconds_per_min  = 60;
173   $seconds_per_hour = 60 * $seconds_per_min;
174   $seconds_per_day  = $seconds_per_hour * 24;
175
176   $days    = 0;
177   $hours   = 0;
178   $minutes = 0;
179   $seconds = 0;
180
181   if ($difference > $seconds_per_day) {
182     $days       = (int) ($difference / $seconds_per_day);
183     $difference = $difference % $seconds_per_day;
184   } // if
185
186   if ($difference > $seconds_per_hour) {
187     $hours      = (int) ($difference / $seconds_per_hour);
188     $difference = $difference % $seconds_per_hour;
189   } // if
190
191   if ($difference > $seconds_per_min) {
192     $minutes    = (int) ($difference / $seconds_per_min);
193     $difference = $difference % $seconds_per_min;
194   } // if
195
196   $seconds = $difference;
197
198   $day_str  = "";
199   $hour_str = "";
200   $min_str  = "";
201   $sec_str  = "";
202   $duration = "";
203
204   if ($days > 0) {
205     $day_str  = $days == 1 ? "1 day" : "$hours days";
206     $duration = $day_str;
207   } // if
208
209   if ($hours > 0) {
210     $hour_str = $hours == 1 ? "1 hr" : "$hours hrs";
211
212     if ($duration != "") {
213       $duration .= " ". $hour_str;
214     } else {
215       $duration = $hour_str;
216     } // if
217   } // if
218
219   if ($minutes > 0) {
220     $min_str = $minutes == 1 ? "1 min" : "$minutes mins";
221
222     if ($duration != "") {
223       $duration .= " " . $min_str;
224     } else {
225       $duration = $min_str;
226     } // if
227   } // if
228
229   if ($seconds > 0) {
230     $sec_str = $seconds == 1 ? "1 sec" : "$seconds secs";
231
232     if ($duration != "") {
233       $duration .= " " . $sec_str;
234     } else {
235       $duration = $sec_str;
236     } // if
237   } // if
238
239   if ($duration == "" and $seconds == 0) {
240     $duration = "under 1 second";
241   } // if
242
243   return $duration;
244 } // FormatDuration
245
246 function Duration ($start, $end) {
247   $start_timestamp = strtotime ($start);
248   $end_timestamp   = strtotime ($end);
249
250   $difference = $end_timestamp - $start_timestamp;
251
252   return FormatDuration ($difference);
253 } // Duration
254
255 // Returns a string representation of a CSV file given a hash
256 // representing the data. If $title is supplied it is made the first
257 // line. Next a header row is generated based on the keys of the $data
258 // hash. Finally data rows are generated. Any key in the hash
259 // beginning with "_" is skipped.
260 function exportCSV ($data, $title = "") {
261   if (isset ($title)) {
262     $csv .= "$title\n";
263   } // if
264
265   // Create header line
266   $firstTime = true;
267
268   foreach ($data[0] as $key => $value) {
269     // Skip "hidden" fields - fields beginning with "_"
270     if (preg_match ("/^_/", $key) == 1) {
271       continue;
272     } // if
273
274     if (!$firstTime) {
275       $csv .= ",";
276     } else {
277       $firstTime = false;
278     } // if
279
280     $csv .= "\"$key\"";
281   } // foreach
282
283   $csv .= "\n";
284
285   // Data lines
286   foreach ($data as $entry) {
287     $firstTime = true;
288
289     foreach ($entry as $key => $value) {
290       // Skip "hidden" fields - fields beginning with "_"
291       if (preg_match ("/^_/", $key) == 1) {
292         continue;
293       } // if
294
295       if (!$firstTime) {
296         $csv .= ",";
297       } else {
298         $firstTime = false;
299       } // if
300
301       $csv .= "\"$value\"";
302     } // foreach
303
304     $csv .= "\n";
305   } // foreach
306
307   return $csv;
308 } // exportCSV
309
310 function getStyleSheets () {
311   $styleSheet = "\n<style type=\"text/css\">\n";
312
313   foreach (file ("$_SERVER[DOCUMENT_ROOT]/css/Testing.css") as $line) {
314     $styleSheet .= $line;
315   } // foreach
316
317   $styleSheet .= "\n";
318
319   foreach (file ("$_SERVER[DOCUMENT_ROOT]/css/Tables.css") as $line) {
320     $styleSheet .= $line;
321   } // foreach
322
323   $styleSheet .= "\n</style>\n";
324
325   return $styleSheet;
326 } // getStyleSheets
327
328 function mailReport ($pnbr, $username, $subject, $body, $filename, $attachment) {
329   if ($pnbr == "unknown") {
330     return "<p><span class=error>ERROR:</span> You need to select a user to email to first!</p>";
331   } // if    
332
333   $mimeSeparator = md5 (time ());
334
335   $to       = "$username <$pnbr@gdc4s.com>";
336   $toDisplay= "$username &lt;<a href=\"mailto:$pnbr@gdc4s.com\">$pnbr@gdc4s.com</a>&gt;";
337
338   $headers  = "From: RanTestWeb@gdc4s.com\n";
339   $headers .= "MIME-Version: 1.0\n";
340   $headers .= "X-Mailer: PHP\n";
341   $headers .= "Content-Type: multipart/mixed;\n";
342   $headers .=" boundary=\"$mimeSeparator\"";
343
344   $message  = "This is a multi-part message in MIME format.\n";
345   $message .= "--$mimeSeparator\n";
346   $message .= "Content-Type: text/html; charset=ISO-8859-1\n";
347   $message .= "Content-Transfer-Encoding: 7bit\n\n";
348
349   $message .= <<<END
350 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
351 <html>
352 <head>
353 END;
354
355   $message .= getStyleSheets ();
356   $message .= <<<END
357 </head>
358 <body>
359 <h1 align="center">$subject</h1>
360 END;
361  
362   $message .= $body;
363
364   $message .= "\n--$mimeSeparator\n";
365   $message .= "Content-Type: text/vnd.ms-excel; name=\"$filename\"\n";
366   $message .= "Content-Disposition: inline; filename=\"$filename\"\n\n";
367   $message .= $attachment;
368   $message .= "\n\n--$mimeSeparator\n";
369
370   if (mail ($to, $subject, $message, $headers) == false) {
371     return "<p><span class=error>ERROR:</span> Unable to send email to $to</p>";
372   } else {
373     return "<p>Email sent to $toDisplay</p>";
374   } // if
375 } // mailReport
376 ?>