4 function timeDiff ($timestamp,$detailed=false,$n = 0) {
7 #If the difference is positive "ago" - negative "away"
8 ($timestamp >= $now) ? $action = 'away' : $action = 'ago';
9 //echo "Away: $action<br>\n";
10 //if ( $timestamp >= $now //)
12 // echo "Val: greater $timestamp : $now<br>\n";
14 // echo "Val: less than $timestamp : $now<br>\n";
18 $diff = ($action == 'away' ? $timestamp - $now : $now - $timestamp);
20 # Set the periods of time
21 $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
22 $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
24 # Go from decades backwards to seconds
25 $i = sizeof($lengths) - 1; # Size of the lengths / periods in case you change them
26 $time = ""; # The string we will hold our times in
28 # if the difference is greater than the length we are checking... continue
29 if ($diff > $lengths[$i-1]) {
30 # 65 / 60 = 1. That means one minute. 130 / 60 = 2. Two minutes.. etc
31 $val = floor($diff / $lengths[$i-1]);
32 # The value, then the name associated, then add 's' if plural
33 $time .= $val ." ". $periods[$i-1].($val > 1 ? 's ' : ' ');
34 # subtract the values we just used from the overall diff so we can
35 # find the rest of the information
36 $diff -= ($val * $lengths[$i-1]);
37 # if detailed is turn off (default) only show the first set found,
38 # else show all information
39 if(!$detailed) { $i = 0; }
44 # Basic error checking.
46 return "error: bad time";
53 public static function constructList($cname, $list) {
55 foreach ($list as $item) {
56 $ret_list[] = new $cname ($item);
71 function Person($person) {
72 $this->roles = $person['role_ids'];
73 $this->person_id = $person['person_id'];
74 $this->first_name = $person['first_name'];
75 $this->last_name = $person['last_name'];
76 $this->email = $person['email'];
77 $this->enabled = $person['enabled'];
78 $this->data = $person;
81 public static function getPIs($persons) {
83 foreach( $persons as $person ) {
84 $role_ids= $person->roles;
86 if ( in_array( '20', $role_ids ) && $person->enabled )
87 $pis[$person->person_id]= $person->email;
92 public static function getTechs($persons) {
94 foreach( $persons as $person ) {
95 $role_ids= $person->roles;
96 if( in_array( '40', $role_ids ) && $person->enabled )
97 $techs[$person->person_id]= $person->email;
102 function getSites() {
103 return $this->data['site_ids'];
105 function isMember($site_id) {
106 return in_array($site_id, $this->data['site_ids']);
110 return in_array( '10', $this->roles);
113 return in_array( '20', $this->roles);
116 return in_array( '30', $this->roles);
119 return in_array( '40', $this->roles);
122 function link($str) {
123 return "<a href='/db/persons/index.php?id=" . $this->person_id . "'>" . $str . "</a>";
128 $person[] = $this->first_name . " " . $this->last_name;
129 $person[] = $this->link($this->email);
142 function deletePCUlink($node) {
143 return "<a href='/db/sites/index.php?id=" . $node->site_id .
144 "&delete_node_from_pcu=" . $node->node_id .
145 "&pcu_id=" . $this->data['pcu_id'] . "'> Remove from PCU</a>";
147 function pcu_name() {
148 if ( $this->data['hostname'] != NULL and $this->data['hostname'] != "" ):
149 return $this->data['hostname'];
151 if ( $this->data['ip'] != NULL and $this->data['ip'] != "" ):
152 return $this->data['ip'];
154 return "NO-HOSTNAME-OR-IP";
159 function link($str) {
160 return "<a href='/db/sites/pcu.php?id=" . $this->data['pcu_id'] . "'>" . $str . "</a>";
164 return substr($this->data['hostname'], 0, strpos($this->data['hostname'], '.'));
171 function Address($address) {
172 $this->data = $address;
175 function link($str) {
176 return "<a href='/db/addresses/index.php?id=" . $this->data['address_id'] . "'>" . $str . "</a>";
181 $comma= sizeof( $this->data['address_types'] );
183 foreach( $this->data['address_types'] as $add_type ) {
186 if ( $comma > 0 && $count != $comma )
195 class Node extends PlcObject {
206 function Node($node) {
207 global $plc, $api, $adm;
209 $this->model = $node['model'];
210 $this->node_id = $node['node_id'];
211 $this->hostname = $node['hostname'];
212 $this->boot_state = $node['boot_state'];
213 $this->date_created = $node['date_created'];
214 $this->last_updated = $node['last_updated'];
215 $this->last_contact = $node['last_contact'];
216 $this->site_id = $node['site_id'];
217 $this->pcu_ids = $node['pcu_ids'];
218 $this->nn = $api->GetNodeNetworks($node['nodenetwork_ids']);
219 foreach ($this->nn as $nnet)
221 if ( $nnet['is_primary'] == true )
223 $this->ip = $nnet['ip'];
224 $this->netmask = $nnet['netmask'];
225 $this->network = $nnet['network'];
226 $this->gateway= $nnet['gateway'];
227 $this->broadcast = $nnet['broadcast'];
228 $this->dns1 = $nnet['dns1'];
229 $this->dns2 = $nnet['dns2'];
230 $this->method = $nnet['method'];
235 public static function filter($nodes, $nodes_listed) {
237 foreach ($nodes as $node) {
238 if ( ! in_array($node, $nodes_listed) ) {
245 function link($str) {
246 return "<a href='/db/nodes/index.php?id=" . $this->node_id . "'>" . $str . "</a>";
248 function pcuport($pcu) {
250 foreach ( $pcu->data['node_ids'] as $node_id ) {
251 if ( $node_id == $this->node_id ) {
252 return $pcu->data['ports'][$count];
259 function hasPCU($pcu) {
260 $pcu_id = $pcu->data['pcu_id'];
261 return in_array( $pcu_id, $this->pcu_ids );
263 function dateCreated() {
264 $date_created = date("M j, Y", $this->date_created);
265 return $date_created;
267 function lastUpdated() {
268 return $this->timeaway($this->last_updated);
270 function lastContact() {
271 return $this->timeaway($this->last_contact);
273 function timeaway($val) {
274 if ( $val != NULL ) {
275 $ret = timeDiff(intval($val));
287 function Slice($val) {
291 // <!--sort_slices( $slices ); -->
292 function dateCreated() {
293 $date_created = date("M j, Y", $this->data['created']);
294 return $date_created;
298 if ( $this->data['expires'] != 0 ) {
299 $expires = timeDiff(intval($this->data['expires']));
307 class Site extends PlcObject {
318 function Site($site_id) {
319 global $plc, $api, $adm;
320 $site_info= $adm->GetSites( array( intval($site_id) ) );
321 $this->data = $site_info[0];
323 $this->site_id = intval($site_id);
324 $this->site_name = $site_info[0]['name'];
325 $this->address_ids = $site_info[0]['address_ids'];
326 $this->pcu_ids = $site_info[0]['pcu_ids'];
327 $this->node_ids = $site_info[0]['node_ids'];
328 $this->person_ids = $site_info[0]['person_ids'];
329 $this->slice_ids = $site_info[0]['slice_ids'];
330 $this->enabled = $site_info[0]['enabled'];
331 $this->peer_id = $site_info[0]['peer_id'];
334 function getSiteObjects() {
335 global $plc, $api, $adm;
337 $adm->GetAddresses( $this->address_ids );
338 $adm->GetPCUs( $this->pcu_ids );
339 $adm->GetNodes( $this->node_ids, array( "node_id", "hostname", "boot_state",
340 "date_created", "last_updated", "last_contact", "site_id", "pcu_ids" ) );
341 $adm->GetPersons( $this->person_ids, array( "role_ids", "person_id", "first_name",
342 "last_name", "email", "enabled" ) );
343 $adm->GetSlices( $this->slice_ids, array( "name", "slice_id", "instantiation", "created", "expires" ) );
344 return $adm->commit();
348 /* class Blue extends PlcObject
357 $cl = PlcObject::constructList('Blue', array('this', 'is', 'a', 'test'));
358 echo sizeof($cl) . "\n";
359 foreach ($cl as $obj)
361 echo get_class( $obj) . "\n";
362 echo $obj->val . "\n";