2 ////////////////////////////////////////////////////////////////////////////////
5 // Description: Utility funcitons
6 // Author: Andrew@ClearSCm.com
7 // Created: Mon Apr 28 15:20:06 MST 2008
11 // (c) Copyright 2008, General Dynamics, all rights reserved.
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.
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
23 ////////////////////////////////////////////////////////////////////////////////
27 $webdir = dirname ($_SERVER["PHP_SELF"]);
29 function debug ($msg) {
33 print "<font color=red>DEBUG:</font> $msg<br>";
37 function dumpObject ($object) {
43 function Error ($msg) {
44 print "<font color=red><b>ERROR:</b></font> $msg<br>";
49 <table class="banner" width="100%" border="0" cellpadding="0" cellspacing="0">
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"> </td>
61 function YMD2MDY ($date) {
62 return substr ($date, 5, 2) . "/" .
63 substr ($date, 8, 2) . "/" .
67 function MDY2YMD ($date) {
68 return substr ($date, 6, 4) . "-" .
69 substr ($date, 0, 2) . "-" .
73 function cmpStr ($a, $b, $field, $direction) {
74 if ($direction == "ascending") {
75 return strcmp ($a[$field], $b[$field]);
77 return strcmp ($b[$field], $a[$field]);
81 function cmpNbr ($a, $b, $field, $direction) {
82 if ($a[$field] == $b[$field]) {
86 if ($direction == "ascending") {
87 return ($a[$field] < $b[$field]) ? -1 : 1;
89 return ($a[$field] < $b[$field]) ? 1 : -1;
93 function getUsername ($userid) {
94 exec ("ypmatch $userid passwd | cut -f5 -d:", &$username);
96 if (empty ($username)) {
99 if ($username[0] == "SWIT account for SWIT testing") {
107 function emailUsersDropdown () {
108 define (DIM, "#888");
110 exec ("ypcat passwd | grep ^p|cut -f1,5 -d: | grep -v 'SWIT account'", &$userids);
112 $users ["unknown"] = "<Select User>";
114 foreach ($userids as $user) {
115 list ($pnbr, $name) = explode (":", $user);
117 $users [$pnbr] = $name;
122 $dropdown = "Email to <select name=user class=inputfield style=\"color: " . DIM . "\">";
124 foreach ($users as $pnbr => $name) {
125 $dropdown .= "<option value=\"$pnbr:$name\" style=\"color: ";
127 if ($pnbr != "unknown") {
128 $dropdown .= "black";
133 $dropdown .= "\">$name</option>";
136 $dropdown .= "</select>";
139 } // emailUsersDropdown
141 function copyright () {
146 $thisFile = "$_SERVER[DOCUMENT_ROOT]/$_SERVER[PHP_SELF]";
147 $lastModified = date ("F d Y @ g:i a", filemtime ($thisFile));
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 © General Dynamics, all rights reserved<br>
154 <a href="/"><img border=0 src="/images/HomeSmall.gif">Home</a>
156 <a href="http://ranweb/dokuwiki/doku.php">Wiki</a>
158 <a href="http://ranweb/dokuwiki/doku.php?id=ran:ran_test_automation_users_guide">Users Guide</a>
160 <a href="http://ranweb/dokuwiki/doku.php?id=ran:rantest">Usage</a>
162 <a href="/docs">Other Docs</a><br>
167 function Today2SQLDatetime () {
168 return date ("Y-m-d H:i:s");
169 } // Today2SQLDatetime
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;
181 if ($difference > $seconds_per_day) {
182 $days = (int) ($difference / $seconds_per_day);
183 $difference = $difference % $seconds_per_day;
186 if ($difference > $seconds_per_hour) {
187 $hours = (int) ($difference / $seconds_per_hour);
188 $difference = $difference % $seconds_per_hour;
191 if ($difference > $seconds_per_min) {
192 $minutes = (int) ($difference / $seconds_per_min);
193 $difference = $difference % $seconds_per_min;
196 $seconds = $difference;
205 $day_str = $days == 1 ? "1 day" : "$hours days";
206 $duration = $day_str;
210 $hour_str = $hours == 1 ? "1 hr" : "$hours hrs";
212 if ($duration != "") {
213 $duration .= " ". $hour_str;
215 $duration = $hour_str;
220 $min_str = $minutes == 1 ? "1 min" : "$minutes mins";
222 if ($duration != "") {
223 $duration .= " " . $min_str;
225 $duration = $min_str;
230 $sec_str = $seconds == 1 ? "1 sec" : "$seconds secs";
232 if ($duration != "") {
233 $duration .= " " . $sec_str;
235 $duration = $sec_str;
239 if ($duration == "" and $seconds == 0) {
240 $duration = "under 1 second";
246 function Duration ($start, $end) {
247 $start_timestamp = strtotime ($start);
248 $end_timestamp = strtotime ($end);
250 $difference = $end_timestamp - $start_timestamp;
252 return FormatDuration ($difference);
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)) {
265 // Create header line
268 foreach ($data[0] as $key => $value) {
269 // Skip "hidden" fields - fields beginning with "_"
270 if (preg_match ("/^_/", $key) == 1) {
286 foreach ($data as $entry) {
289 foreach ($entry as $key => $value) {
290 // Skip "hidden" fields - fields beginning with "_"
291 if (preg_match ("/^_/", $key) == 1) {
301 $csv .= "\"$value\"";
310 function getStyleSheets () {
311 $styleSheet = "\n<style type=\"text/css\">\n";
313 foreach (file ("$_SERVER[DOCUMENT_ROOT]/css/Testing.css") as $line) {
314 $styleSheet .= $line;
319 foreach (file ("$_SERVER[DOCUMENT_ROOT]/css/Tables.css") as $line) {
320 $styleSheet .= $line;
323 $styleSheet .= "\n</style>\n";
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>";
333 $mimeSeparator = md5 (time ());
335 $to = "$username <$pnbr@gdc4s.com>";
336 $toDisplay= "$username <<a href=\"mailto:$pnbr@gdc4s.com\">$pnbr@gdc4s.com</a>>";
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\"";
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";
350 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
355 $message .= getStyleSheets ();
359 <h1 align="center">$subject</h1>
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";
370 if (mail ($to, $subject, $message, $headers) == false) {
371 return "<p><span class=error>ERROR:</span> Unable to send email to $to</p>";
373 return "<p>Email sent to $toDisplay</p>";