4 function paginate( $fn_array, $table_id, $caption, $limit, $main_field, $other_func= NULL, $fid= NULL ) {
5 // get vars from call adjust them
6 $dir= strtolower( $caption );
9 $link_page= 'index.php';
11 // check for page number
12 if( empty( $_GET['page'] ) )
17 // reorder array_chunk
18 foreach( $fn_array as $arr1 ) {
21 foreach( $arr1 as $key => $val ) {
22 // if( substr( $key, -3 ) == "_id" )
23 if ( $key == $table_id ) {
30 foreach( $id as $key => $val )
33 foreach( $data as $key => $val )
39 $totalrows= count( $as_array );
41 // if array has no rows display msg
43 return "Nothing to display";
45 // set key and break up data struct
47 $newarray= array_chunk( $as_array, $limit );
50 $echo.= "<table class='list_set' border=0 cellpadding=2>\n";
52 // if there is a caption add it to table
54 $echo.= "<caption class='list_set'>$caption</caption>\n";
56 $echo.= "<thead><tr class='list_set'>";
58 // go through keys of one array row for table headers
59 foreach( $newarray[$newkey][0] as $key => $val ) {
60 // if( substr( $key, -3 ) != "_id" )
61 if ( $key != $table_id && $key != 'peer_id' )
62 $echo.= "<th class='list_set'>". ucfirst( $key ) ."</th>";
65 if( $other_func == 'slivers' )
66 $echo.= "<th>Slivers</th>";
68 $echo.= "</tr></thead><tbody>\n";
70 // go through array row by row to output table rows
71 foreach( $newarray[$newkey] as $assoc ) {
74 if ($assoc['peer_id']) {
75 $extraclass="plc-foreign";
79 $echo.= "<tr class='list_set'>";
81 foreach( $assoc as $key => $val ) {
82 // do not rely on keys order
83 $id = $assoc[$table_id];
84 // if( substr( $key, -3 ) == "_id" )
85 if ($key == $table_id) {
88 } elseif( $key == $main_field ) {
89 $echo.= "<td class='list_set $extraclass'><a href='/db/$dir/$link_page?id=$id'>$val</a></td>";
90 } elseif ($key != 'peer_id') {
91 $echo.= "<td class='list_set $extraclass'>";
92 if( is_array( $val ) ) {
95 foreach( $val as $k => $v ) {
109 if( $other_func == 'slivers' )
110 $echo.= "<td><a href='slivers.php?node=$fid&slice=$id'>view</a></td>";
116 $echo.= "</tbody></table>\n";
119 // find total number of pages
120 $numofpages = $totalrows / $limit;
122 // start navigation links
123 if( $numofpages > 1 ) {
124 // if page is not 1 display first and prev links
125 if( $page != 1 && $page ) {
126 $pageprev= $page - 1;
127 $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=1\">FIRST</a> ";
128 $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pageprev\">PREV ".$limit."</a> ";
131 $echo.= "PREV ". $limit ." ";
133 // if less than 30 pages display all
134 // otherwise show 30 pages but put ... inbetween
135 if( $numofpages < 30 ) {
136 $npages= $numofpages;
141 if( $npages > $numofpages )
142 $npages= $numofpages;
150 // display pages, no link if current page
151 for( $i= $start; $i <= $npages; $i++ ) {
155 $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
159 if( ( $totalrows % $limit ) != 0 ) {
160 $last= $numofpages + 1;
164 $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
169 if( $numofpages >= 30 ) {
170 if( $page != $numofpages )
174 if( ( $totalrows - ($limit * $page) ) > 0 ) {
175 $pagenext= $page + 1;
176 $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pagenext\">NEXT ".$limit."</a> ";
179 $echo.= "NEXT ". $limit;
181 $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=". intval( $last ) ."\">LAST</a>\n";
189 // function for getting the diff of multi dimention array
190 function arr_diff( $a1, $a2 ) {
192 foreach( $a1 as $k=>$v ) {
194 for( $x= 0; $x < count( $a2 ); $x++ ) {
196 if( array_search( $v, $a2 ) === false )
198 else if( is_array( $v ) )
199 $dv= arr_diff( $v, $a2[$x] );
200 if( $dv && !in_array( $dv, $diff ) )
207 else if(is_array($v))
208 $dv=arr_diff($v,$a2[$x]);
218 function is_valid_email_addr($email)
220 if( ereg("^.+@.+\\..+$", $email) )
226 function is_valid_url($url)
228 if( ereg("^(http|https)://.+\..+$", strtolower($url) ) )
234 function is_valid_ip($ip)
236 if( ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) )
238 // it's at least in the right format, now check to see if
239 // each part is equal to less than 255
240 $parts= explode( '.', $ip );
241 $count= count($parts);
243 for( $i= 0; $i < $count; $i++ )
245 if( intval($parts[$i]) > 255 )
256 function is_valid_network_addr($network_addr,$mask)
258 $lNetwork= ip2long($network_addr);
259 $lMask= ip2long($mask);
261 // are they the correct format?
262 if( $lNetwork == -1 || $lMask == -1 )
265 // is network address valid for the mask?
266 if( ($lNetwork & $lMask) != $lNetwork )
273 // returns whether or not a network address is in the reserved space
274 // in the case of a invalid network address, false will be returned.
275 function is_reserved_network_addr($network_addr)
277 $lNetwork= ip2long($network_addr);
279 if( $lNetwork == -1 )
282 // does the network address fall in a reserved block?
283 $reserved_ips = array (
284 array('10.0.0.0','10.255.255.255'),
285 array('172.16.0.0','172.31.0.0'),
286 array('192.168.0.0','192.168.255.0')
288 foreach ($reserved_ips as $r)
290 $min = ip2long($r[0]);
291 $max = ip2long($r[1]);
293 if (($lNetwork >= $min) && ($lNetwork <= $max))
300 // builds a table from an array of strings, with the given class
301 function plc_make_table ($class, $messages) {
302 // pretty print the cell
304 if (! empty ($messages)) {
305 $formatted="<table class='" . $class . "'>";
306 foreach ($messages as $message) {
307 $formatted .= "<tr><td>" . $message . "</td></tr>";
309 $formatted .= "</table>";
314 // shows a php variable verbatim with a heading message
315 function plc_debug($message,$object) {
316 print "<br>" . $message . "<pre>";
321 // attempt to normalize the delete buttons and confirmations
322 function plc_delete_button($width=15) {
323 return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/includes/delete.png"></span>';
326 function plc_js_confirm($message) {
327 return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
330 function plc_delete_link($url,$delete_message,$visible) {
331 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
334 function plc_delete_link_button($url,$delete_message,$width=15) {
335 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
338 function plc_event_button($type,$param,$id) {
339 return '<a href="/db/events/index.php?type=' . $type . '&' . $param . '=' . $id . '"> <span title="Related events"> <img src="/planetlab/includes/event.png" width=18></span></a>';
342 function plc_comon_button ($field, $value,$target="") {
344 if (!empty($target)) {
345 $result.='target="' . $target . '" ';
347 $result.='href="/db/nodes/comon.php?' . $field . "=" . $value . '">';
348 $result.='<span title="Link to Comon"> <img src="/planetlab/includes/comon.png" width="18"></span></a>';
352 function plc_peers_option_list ($api) {
355 $peers=$api->GetPeers(NULL,array('peer_id','peername'));
356 if (count($peers)==0) {
357 $predef=array(array("peer_id"=>"","peername"=>"All (no known peers)"));
359 $predef=array(array("peer_id"=>"","peername"=>"All peers"),
360 array("peer_id"=>"local","peername"=>"Local only"));
361 // show a 'foreign' button only if that makes sense
362 if (count($peers) >= 2) {
363 $predef [] = array("peer_id"=>"foreign","peername"=>"Foreign peers");
368 foreach ($predef as $a) {
369 $peer_line = "<option value='" . $a['peer_id'] . "'>" . $a['peername'] . "</option>\n";
370 $result .= $peer_line;
373 if (!empty($peers)) {
374 foreach ($peers as $a) {
375 $peer_line = "<option value='" . $a['peer_id'] . "'>" . $a['peername'] . "</option>\n";
376 $result .= $peer_line;