5 function timeDiff ($timestamp,$detailed=false,$n = 0) {
8 #If the difference is positive "ago" - negative "away"
9 ($timestamp >= $now) ? $action = 'away' : $action = 'ago';
10 //echo "Away: $action<br>\n";
11 //if ( $timestamp >= $now //)
13 // echo "Val: greater $timestamp : $now<br>\n";
15 // echo "Val: less than $timestamp : $now<br>\n";
19 $diff = ($action == 'away' ? $timestamp - $now : $now - $timestamp);
21 # Set the periods of time
22 $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
23 $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
25 # Go from decades backwards to seconds
26 $i = sizeof($lengths) - 1; # Size of the lengths / periods in case you change them
27 $time = ""; # The string we will hold our times in
29 # if the difference is greater than the length we are checking... continue
30 if ($diff > $lengths[$i-1]) {
31 # 65 / 60 = 1. That means one minute. 130 / 60 = 2. Two minutes.. etc
32 $val = floor($diff / $lengths[$i-1]);
33 # The value, then the name associated, then add 's' if plural
34 $time .= $val ." ". $periods[$i-1].($val > 1 ? 's ' : ' ');
35 # subtract the values we just used from the overall diff so we can
36 # find the rest of the information
37 $diff -= ($val * $lengths[$i-1]);
38 # if detailed is turn off (default) only show the first set found,
39 # else show all information
40 if(!$detailed) { $i = 0; }
45 # Basic error checking.
47 return "error: bad time";
54 public static function constructList($cname, $list) {
56 foreach ($list as $item) {
57 $ret_list[] = new $cname ($item);
72 function Person($person) {
73 $this->roles = $person['role_ids'];
74 $this->person_id = $person['person_id'];
75 $this->first_name = $person['first_name'];
76 $this->last_name = $person['last_name'];
77 $this->email = $person['email'];
78 $this->enabled = $person['enabled'];
79 $this->data = $person;
82 public static function getPIs($persons) {
84 foreach( $persons as $person ) {
85 $role_ids= $person->roles;
87 if ( in_array( '20', $role_ids ) && $person->enabled )
88 $pis[$person->person_id]= $person->email;
93 public static function getTechs($persons) {
95 foreach( $persons as $person ) {
96 $role_ids= $person->roles;
97 if( in_array( '40', $role_ids ) && $person->enabled )
98 $techs[$person->person_id]= $person->email;
103 function getSites() {
104 return $this->data['site_ids'];
106 function isMember($site_id) {
107 return in_array($site_id, $this->data['site_ids']);
111 return in_array( '10', $this->roles);
114 return in_array( '20', $this->roles);
117 return in_array( '30', $this->roles);
120 return in_array( '40', $this->roles);
123 function link($str) {
124 return "<a href='/db/persons/index.php?id=" . $this->person_id . "'>" . $str . "</a>";
129 $person[] = $this->first_name . " " . $this->last_name;
130 $person[] = $this->link($this->email);
143 function deletePCUlink($node) {
144 return "<a href='/db/sites/index.php?id=" . $node->site_id .
145 "&delete_node_from_pcu=" . $node->node_id .
146 "&pcu_id=" . $this->data['pcu_id'] . "'> Remove from PCU</a>";
148 function pcu_name() {
149 if ( $this->data['hostname'] != NULL and $this->data['hostname'] != "" ):
150 return $this->data['hostname'];
152 if ( $this->data['ip'] != NULL and $this->data['ip'] != "" ):
153 return $this->data['ip'];
155 return "NO-HOSTNAME-OR-IP";
160 function link($str) {
161 return "<a href='/db/sites/pcu.php?id=" . $this->data['pcu_id'] . "'>" . $str . "</a>";
165 return substr($this->data['hostname'], 0, strpos($this->data['hostname'], '.'));
172 function Address($address) {
173 $this->data = $address;
176 function link($str) {
177 return "<a href='/db/addresses/index.php?id=" . $this->data['address_id'] . "'>" . $str . "</a>";
182 $comma= sizeof( $this->data['address_types'] );
184 foreach( $this->data['address_types'] as $add_type ) {
187 if ( $comma > 0 && $count != $comma )
196 class Node extends PlcObject {
207 function Node($node) {
208 global $plc, $api, $adm;
210 $this->model = $node['model'];
211 $this->node_id = $node['node_id'];
212 $this->hostname = $node['hostname'];
213 $this->boot_state = $node['boot_state'];
214 $this->date_created = $node['date_created'];
215 $this->last_updated = $node['last_updated'];
216 $this->last_contact = $node['last_contact'];
217 $this->site_id = $node['site_id'];
218 $this->pcu_ids = $node['pcu_ids'];
219 $this->nn = $api->GetInterfaces($node['interface_ids']);
220 foreach ($this->nn as $nnet)
222 if ( $nnet['is_primary'] == true )
224 $this->ip = $nnet['ip'];
225 $this->netmask = $nnet['netmask'];
226 $this->network = $nnet['network'];
227 $this->gateway= $nnet['gateway'];
228 $this->broadcast = $nnet['broadcast'];
229 $this->dns1 = $nnet['dns1'];
230 $this->dns2 = $nnet['dns2'];
231 $this->method = $nnet['method'];
236 public static function filter($nodes, $nodes_listed) {
238 foreach ($nodes as $node) {
239 if ( ! in_array($node, $nodes_listed) ) {
246 function link($str) {
247 return "<a href='/db/nodes/index.php?id=" . $this->node_id . "'>" . $str . "</a>";
249 function pcuport($pcu) {
251 foreach ( $pcu->data['node_ids'] as $node_id ) {
252 if ( $node_id == $this->node_id ) {
253 return $pcu->data['ports'][$count];
260 function hasPCU($pcu) {
261 $pcu_id = $pcu->data['pcu_id'];
262 return in_array( $pcu_id, $this->pcu_ids );
264 function dateCreated() {
265 $date_created = date("M j, Y", $this->date_created);
266 return $date_created;
268 function lastUpdated() {
269 return $this->timeaway($this->last_updated);
271 function lastContact() {
272 return $this->timeaway($this->last_contact);
276 $STALE_LENGTH = 60*60; /* TODO: set by some policy */
277 if ( $this->last_contact + $STALE_LENGTH > $now ) {
283 function timeaway($val) {
284 if ( $val != NULL ) {
285 $ret = timeDiff(intval($val));
297 function Slice($val) {
301 // <!--sort_slices( $slices ); -->
302 function dateCreated() {
303 $date_created = date("M j, Y", $this->data['created']);
304 return $date_created;
308 if ( $this->data['expires'] != 0 ) {
309 $expires = timeDiff(intval($this->data['expires']));
317 class Site extends PlcObject {
328 function Site($site_id) {
329 global $plc, $api, $adm;
330 $site_info= $adm->GetSites( array( intval($site_id) ) );
331 $this->data = $site_info[0];
333 $this->site_id = intval($site_id);
334 $this->site_name = $site_info[0]['name'];
335 $this->address_ids = $site_info[0]['address_ids'];
336 $this->pcu_ids = $site_info[0]['pcu_ids'];
337 $this->node_ids = $site_info[0]['node_ids'];
338 $this->person_ids = $site_info[0]['person_ids'];
339 $this->slice_ids = $site_info[0]['slice_ids'];
340 $this->enabled = $site_info[0]['enabled'];
341 $this->peer_id = $site_info[0]['peer_id'];
344 function getSiteObjects() {
345 global $plc, $api, $adm;
347 $adm->GetAddresses( $this->address_ids );
348 $adm->GetPCUs( $this->pcu_ids );
349 $adm->GetNodes( $this->node_ids, array( "node_id", "hostname", "boot_state",
350 "date_created", "last_updated", "last_contact", "site_id", "pcu_ids" ) );
351 $adm->GetPersons( $this->person_ids, array( "role_ids", "person_id", "first_name",
352 "last_name", "email", "enabled" ) );
353 $adm->GetSlices( $this->slice_ids, array( "name", "slice_id", "instantiation", "created", "expires" ) );
354 return $adm->commit();
358 /* class Blue extends PlcObject
367 $cl = PlcObject::constructList('Blue', array('this', 'is', 'a', 'test'));
368 echo sizeof($cl) . "\n";
369 foreach ($cl as $obj)
371 echo get_class( $obj) . "\n";
372 echo $obj->val . "\n";