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