handles urls with settings
[plewww.git] / planetlab / includes / plc_objects.php
1
2 <?php
3
4 function timeDiff ($timestamp,$detailed=false,$n = 0) {
5   $now = time();
6
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 //)
11   //{
12   //    echo "Val: greater $timestamp : $now<br>\n";
13   //} else{
14   //    echo "Val: less than $timestamp : $now<br>\n";
15   //}
16
17
18   $diff = ($action == 'away' ? $timestamp - $now : $now - $timestamp);
19
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);
23
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
27   while($i >= $n) {
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; }                
40     }
41     $i--;
42   }
43         
44 # Basic error checking.
45   if ($time == "") {
46     return "error: bad time";
47   } else {
48     return $time.$action;
49   }
50 }
51
52 class PlcObject {
53   public static function constructList($cname, $list) {
54     $ret_list = array();
55     foreach ($list as $item) {
56       $ret_list[] = new $cname ($item);
57     }
58     return $ret_list;
59   }
60 }
61
62
63 class Person {
64   var $roles;
65   var $person_id;
66   var $first_name; 
67   var $last_name;
68   var $email;
69   var $enabled;
70
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;
79   }
80
81   public static function getPIs($persons) {
82     $pis = array();
83     foreach( $persons as $person ) {
84       $role_ids= $person->roles;
85
86       if ( in_array( '20', $role_ids ) && $person->enabled )
87         $pis[$person->person_id]= $person->email;
88     }
89     return $pis;
90   }
91
92   public static function getTechs($persons) {
93     $techs = array();
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;
98     }
99     return $techs;
100   }
101
102   function getSites() {
103     return $this->data['site_ids'];
104   }
105   function isMember($site_id) {
106     return in_array($site_id, $this->data['site_ids']);
107   }
108
109   function isAdmin() {
110     return in_array( '10', $this->roles);
111   }
112   function isPI() {
113     return in_array( '20', $this->roles);
114   }
115   function isUser() {
116     return in_array( '30', $this->roles);
117   }
118   function isTech() {
119     return in_array( '40', $this->roles);
120   }
121
122   function link($str) {
123     return "<a href='/db/persons/index.php?id=" . $this->person_id . "'>" . $str . "</a>";
124   }
125
126   function display() {
127     $person = array();
128     $person[] = $this->first_name . " " . $this->last_name;
129     $person[] = $this->link($this->email);
130     return $person;
131   }
132 }
133
134
135 class PCU {
136   var $data;
137
138   function PCU($pcu) {
139     $this->data = $pcu;
140   }
141
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'] . "'>&nbsp;Remove from PCU</a>";
146   }
147   function pcu_name() {
148     if ( $this->data['hostname'] != NULL and $this->data['hostname'] != "" ):
149       return $this->data['hostname'];
150     else: 
151       if ( $this->data['ip'] != NULL and $this->data['ip'] != "" ):
152         return $this->data['ip'];
153       else:
154         return "NO-HOSTNAME-OR-IP";
155     endif;
156     endif;
157   }
158
159   function link($str) {
160     return "<a href='/db/sites/pcu.php?id=" . $this->data['pcu_id'] . "'>" . $str . "</a>";
161   }
162
163   function host() {
164     return substr($this->data['hostname'], 0, strpos($this->data['hostname'], '.'));
165   }
166 }
167
168 class Address {
169   var $data;
170
171   function Address($address) {
172     $this->data = $address;
173   }
174
175   function link($str) {
176     return "<a href='/db/addresses/index.php?id=" . $this->data['address_id'] . "'>" . $str . "</a>";
177   }
178         
179   function label() {
180     $label = "";
181     $comma= sizeof( $this->data['address_types'] );
182     $count= 0;
183     foreach( $this->data['address_types'] as $add_type ) {
184       $label .= $add_type;
185       $count++;
186       if ( $comma > 0 && $count != $comma )
187         $label .= ", ";
188     }
189     return $label;
190   }
191
192 }
193
194
195 class Node extends PlcObject {
196   var $node_id;
197   var $hostname;
198   var $boot_state;
199   var $date_created;
200   var $last_updated;
201   var $last_contact;
202   var $site_id;
203   var $pcu_ids;
204   var $data;
205
206   function Node($node) {
207     global $plc, $api, $adm;
208     $this->data = $node;
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)
220       {
221         if ( $nnet['is_primary'] == true )
222           {
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'];
231           }
232       }
233   }
234
235   public static function filter($nodes, $nodes_listed) {
236     $ret = array();
237     foreach ($nodes as $node) {
238       if ( ! in_array($node, $nodes_listed) ) {
239         $ret[] = $node;
240       }
241     }
242     return $ret;
243   }
244
245   function link($str) {
246     return "<a href='/db/nodes/index.php?id=" . $this->node_id . "'>" . $str . "</a>";
247   }
248   function pcuport($pcu) {
249     $count = 0;
250     foreach ( $pcu->data['node_ids'] as $node_id ) {
251       if ( $node_id == $this->node_id ) {
252         return $pcu->data['ports'][$count];
253       }
254       $count += 1;
255     }
256     return 0;
257   }
258
259   function hasPCU($pcu) {
260     $pcu_id = $pcu->data['pcu_id'];
261     return in_array( $pcu_id, $this->pcu_ids );
262   }
263   function dateCreated() {
264     $date_created = date("M j, Y", $this->date_created);
265     return $date_created;
266   }
267   function lastUpdated() {
268     return $this->timeaway($this->last_updated);
269   }
270   function lastContact() {
271     return $this->timeaway($this->last_contact);
272   }
273   function timeaway($val) {
274     if ( $val != NULL ) {
275       $ret = timeDiff(intval($val));
276     } else {
277       $ret = "Never";
278     }
279     return $ret;
280     
281   }
282 }
283
284 class Slice {
285   var $data;
286
287   function Slice($val) {
288     $this->data = $val;
289   }
290
291   //            <!--sort_slices( $slices ); -->
292   function dateCreated() {
293     $date_created = date("M j, Y", $this->data['created']);
294     return $date_created;
295   }
296
297   function expires() {
298     if ( $this->data['expires'] != 0 ) {
299       $expires = timeDiff(intval($this->data['expires']));
300     } else {
301       $expires = "Never";
302     }
303     return $expires;
304   }
305 }
306
307 class Site extends PlcObject {
308   var $address_ids;
309   var $pcu_ids;
310   var $node_ids;
311   var $person_ids;
312   var $slice_ids;
313   var $enabled;
314   var $peer_id;
315   var $site_id;
316   var $data;
317
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];
322
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'];
332   }
333
334   function getSiteObjects() {
335     global $plc, $api, $adm;
336     $adm->begin();
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();
345   }
346 }
347
348 /* class Blue extends PlcObject
349  {
350  var $val;
351  function Blue($arg)
352  {
353  $this->val = $arg;
354  }
355  }
356
357  $cl = PlcObject::constructList('Blue', array('this', 'is', 'a', 'test'));
358  echo sizeof($cl) . "\n";
359  foreach ($cl as $obj)
360  {
361  echo get_class( $obj) . "\n";
362  echo $obj->val . "\n";
363  }*/
364
365
366 ?>