PhpLabWare version 0.3 
/includes/ -> functions_inc.php

1  <?php
2 
3  // functions_inc.php - Functions for all scripts
4  // functions_inc.php - author: Nico Stuurman <nicost@sourceforge.net>
5    /***************************************************************************
6    * Copyright (c) 2001 by Nico Stuurman *
7    * ------------------------------------------------------------------------ *
8    * Part of phplabware, a web-driven groupware suite for research labs *
9    * This file contains classes and functions needed in all script. *
10    * *
11    * This program is free software; you can redistribute it and/or modify it *
12    * under the terms of the GNU General Public License as published by the *
13    * Free Software Foundation; either version 2 of the License, or (at your *
14    * option) any later version. *
15    \**************************************************************************/
16 
17  ////
18  // !class to get and store browser/OS info
19  class cl_client {
20     var $browser;
21     var $OS;
22 
23     function cl_client () {
24        $HTTP_USER_AGENT = getenv (HTTP_USER_AGENT);
25        $temp = strtolower ($HTTP_USER_AGENT);
26        if (strstr($temp, "opera"))
27           $this->browser = "Opera";
28        elseif (strstr($temp, "msie"))
29           $this->browser = "Internet Explorer";
30        elseif (strstr($temp, "mozilla/4"))
31           $this->browser = "Netscape 4";
32        else
33           $this->browser = "Netscape or the like";
34        if (strstr($temp, "windows"))
35           $this->OS = "Windows";
36        elseif (strstr($temp, "linux"))
37           $this->OS = "Linux";
38        elseif (strstr($temp, "sunos"))
39           $this->OS = "Sun";
40        elseif (strstr($temp, "mac"))
41           $this->OS = "Mac OS";
42        elseif (strstr($temp, "irix"))
43           $this->OS = "IRIX";
44     }
45  }
46 
47  ////
48  // !returns a string with a 'nice' representation of the input number of bytes
49  function nice_bytes ($bytes) {
50     $last = $bytes[strlen($bytes)-1];
51     $bytes = (float) $bytes;
52     if (!is_float($bytes) ) return false;
53     if ($last == "M") $bytes = $bytes*1048576;
54     if ($bytes==0) return "0 bytes";
55     elseif ($bytes==1) return "1 byte";
56     elseif ($bytes < 1024) return "$bytes byte";
57     elseif ($bytes < 16384) {
58        $bytes = $bytes/1024;
59        $bytes = number_format($bytes,1);
60        return "$bytes kb";
61     }
62     elseif ($bytes < 1048576) {
63        $bytes = $bytes/1024;
64        $bytes = number_format($bytes,0);
65        return "$bytes kb";
66     }
67     elseif ($bytes < 16777216) {
68        $bytes = $bytes/1048576;
69        $bytes = number_format($bytes,1);
70        return "$bytes Mb";
71     }
72     elseif ($bytes < 1073741824){
73        $bytes = $bytes/1048576;
74        $bytes = number_format($bytes,0);
75        return "$bytes Mb";
76     }
77     elseif ($bytes >= 1073741824) {
78        $bytes = $bytes/1073741824;
79        $bytes = number_format($bytes,1);
80        return "$bytes Gb";
81     }
82     else return "$bytes byte";
83  }
84 
85 
86  ////
87  // !function that checks if user is allowed to view page
88  // This function should be called before a printheader() call
89  function allowonly($required, $current) {
90     if (! ($required & $current)) {
91        printheader("Not Allowed");
92        navbar(1);
93        echo "&nbsp;<br><h3 align='center'>Sorry, but this page is not for you!";
94        echo "</hr><br>&nbsp;\n";
95        printfooter();
96        exit;
97     }
98  }
99 
100 
101  ////
102  // returns a link to a randomly selected image from the designated directory
103  // imagedir should be relative to the running script and be web-accessible
104  function randomImage($imagedir) {
105     // determine contents of imagedir and store imagefilenames in imagearray
106     $dirhandle = opendir ($imagedir);
107     if (!$dirhandle)
108        return false;
109     $j = 0;
110     while ($file = readdir ($dirhandle)) {
111        if (strstr ($file, ".png") || strstr($file, ".jpg") ||
112                                      strstr($file,".gif") ) {
113           $imagearray[$j]=$file;
114           $j++;
115        }
116     }
117     $filecount=sizeof ($imagearray);
118     // no files
119     if (!$filecount)
120        return false;
121     // 'select' a random file
122     srand((double)microtime()*1000000);
123     if ($filecount>1)
124        $filenr=rand (0,$filecount-1);
125     else
126        $filenr=0;
127     // construct link to randomly selected file
128     $filename=$imagearray[$filenr];
129     return "<img src='$imagedir/$filename' alt='Pretty BioPicture here'>\n";
130  }
131 
132  ////
133  // !returns get vars plus SID when needed
134  function url_get_string ($url) {
135     $get_string=getenv("QUERY_STRING");
136     $sid=SID;
137     if ($get_string) {
138        $url=$url."?$get_string";
139        if ($sid)
140           $url=$url."&amp;$sid";
141        return $url;
142     }
143     if ($sid)
144        $url=$url."?$sid";
145     return $url;
146  }
147 
148  ////
149  // !presents the login screen when authenticating witth sessions
150  function loginscreen ($message="<h3>Login to PhpLabWare</h3>") {
151     global $HTTP_SERVER_VARS, $system_settings;
152 
153     $PHP_SELF=$HTTP_SERVER_VARS["PHP_SELF"];
154     if ($system_settings["secure_server"]) {
155        $server= getenv ("HTTP_HOST");
156        if (!$server)
157           $server=$HTTP_SERVER_VARS["HTTP_HOST"];
158        $addres="https://$server$PHP_SELF";
159     }
160     else
161        $addres=$PHP_SELF;
162     $addres=url_get_string($addres);
163     printheader ("Login to PhpLabWare");
164     echo "<form name='loginform' method='post' action='$addres' onSubmit='this.javascript_enabled.value=\"true\"'>\n";
165     echo "<input type='hidden' name='logon' value='true'>\n";
166     echo "<input type='hidden' name='javascript_enabled' value='0'>\n";
167     echo "<table align=center>\n";
168     echo "<tr><td colspan=2 align='center'>$message</td>\n";
169     $imstring = randomimage("frontims");
170     if ($imstring);
171        echo "<td rowspan=6>&nbsp;&nbsp&nbsp;$imstring</td>";
172     echo "</tr>\n";
173     echo "<tr><td>Your login name:</td>\n";
174     echo "<td><input name='user' size=10 value=''></td></tr>\n";
175     echo "<tr><td>Password:</td>\n";
176     echo "<td><input type='password' name='pwd' size=10 value=''></td></tr>\n";
177     echo "<tr><td colspan=2 align='center'>";
178     if ($system_settings["secure_server"]) {
179        echo "<input type='checkbox' name='ssl' checked>Keep a secure connection";
180     }
181     echo "</td></tr>\n";
182     echo "<tr><td colspan=2 align='center'>";
183     echo "<input type='submit' name='submit' value='Login'></td></tr>\n";
184     echo "<tr><td colspan=2 align='center'>";
185     //echo "Note: Cookies must be enabled beyond this point</td></tr>\n";
186     echo "</table>\n</form>\n";
187     printfooter();
188  }
189 
190 
191  ////
192  // !checks wether variables are present in ${type} and makes them available
193  // variables are only set when they are not null in ${type}
194  // $var_string is a comma delimited list
195  function globalize_vars ($var_string, $type) {
196 
197     if ($var_string && $type) {
198        $var_name = strtok ($var_string, ",");
199        global ${$var_name};
200        if (!${$var_name})
201           ${$var_name} = $type["$var_name"];
202        while ($var_name) {
203           $var_name = strtok (",");
204           global ${$var_name};
205           if (!${$var_name})
206              ${$var_name} = $type["$var_name"];
207        }
208     }
209  }
210 
211 
212  ////
213  // !Return the value of specified cell in the database
214  // Returns false if no or multiple rows have requested value
215  function get_cell ($db, $table, $column, $column2, $value) {
216     $query="SELECT $column FROM $table WHERE $column2='$value'";
217     $result=$db->Execute($query);
218     if ($result) {
219        $out=$result->fields[0];
220     }
221     else {
222        return false;
223     }
224     $result->MoveNext();
225     if ($result->EOF) {
226        return $out;
227     }
228     else {
229        return false;
230     }
231  }
232 
233  ////
234  // ! Returns a formatted link with name of the person identified by id
235  function get_person_link ($db,$id) {
236     $query="SELECT firstname,lastname,email FROM users WHERE id=$id";
237     $r=$db->Execute($query);
238     if ($r->fields["email"]) {
239        $submitter="<a href='mailto:".$r->fields["email"]."'>";
240        $submitter.= $r->fields["firstname"]." ".$r->fields["lastname"]."</a> ";
241     }
242     else {
243        $submitter=$r->fields["firstname"]." ";
244        $submitter.=$r->fields["lastname"] ." ";
245     }
246     return $submitter;
247  }
248 
249  ////
250  // !Prints a table with usefull links
251  function navbar($permissions) {
252     include ('includes/defines_inc.php');
253     global $db, $USER, $HTTP_SESSION_VARS;
254 
255     if ($HTTP_SESSION_VARS["javascript_enabled"] && $USER["settings"]["menustyle"])
256        $mode="menu";
257     if ($mode=="menu") {
258        // construct link menu
259        if ($permissions) {
260           $r=$db->Execute("select display from tableoftables where tablename ='linkbar'");
261           if ($r->fields[0]=="1") {
262              $linkr=$db->Execute("select label,linkurl,target from linkbar where display ='Y' ORDER by sortkey");
263              if ($linkr) {
264                 $linkmenu="<select name='themenu' onchange='linkmenu(this)'>\n";
265                 $linkmenu.="<option value=''>--Links--</option>\n";
266                 while (!$linkr->EOF) {
267                    $Tlinkname=$linkr->fields[0];
268                    $urlname=$linkr->fields[1];
269                    // we use 'target ' as a code for js linkmenu to open a new win
270                    if ($linkr->fields[2]=="N")
271                       $urlname="target ".$urlname;
272                    $linkmenu.="<option value='$urlname'>$Tlinkname</option>\n";
273                    $linkr->MoveNext();
274                 }
275                 $linkmenu.="</select>\n";
276              }
277           }
278           else
279              $linkmenu="&nbsp;";
280        }
281 
282        // construct Table menu
283        $records=$db->Execute("select tablename,custom,id,label from tableoftables where display='Y' and permission='Users' ORDER by sortkey");
284        $count=0;
285        if ($records && $USER) {
286           $query="SELECT tableid FROM groupxtable_display WHERE (groupid='".$USER["group_array"][0]."' ";
287           for ($i=1;$i<sizeof($USER['group_array']);$i++)
288              $query.="OR groupid='".$USER['group_array'][$i]."' ";
289           $query.=')';
290           $rb=$db->Execute($query);
291           while ($rb && !$rb->EOF) {
292              $showtables[]=$rb->fields['tableid'];
293              $rb->MoveNext();
294           }
295           $tablemenu="<select name='tablemenu' onchange='linkmenu(this)'>\n";
296           $tablemenu.="<option value=''>--Databases--</option>\n";
297           while (!$records->EOF) {
298              if (in_array($records->fields['id'],$showtables)) {
299                 $tabname=$records->fields[0];
300                 $scriptname=$records->fields[1];
301                 $label=$records->fields['label'];
302                 $linkname='';
303                 if ($scriptname=='')
304                    $linkname="general.php?tablename=$tabname&amp;".SID;
305                 else
306                    $linkname=$scriptname.'?'.SID;
307                 // Allow use of separators, everything that starts with three dashes will not result in a link
308                 if (substr($label,0,3)=='---')
309                    $tablemenu.=" <option value=''>$label</option>\n";
310                 else
311                    $tablemenu.=" <option value='$linkname'>$label</option>\n";
312              }
313              $records->MoveNext();
314           }
315           $tablemenu .="</select>\n";
316        }
317 
318        // construct system menu
319        $systemmenu="<select name='systemmenu' onchange='linkmenu(this)'>\n";
320        $systemmenu.="<option value=''>--System--</option>\n";
321        if (SID)
322           $SID="?".SID;
323        if ($permissions) {
324           $systemmenu.=" <option value='users.php?type=me&dummy=true&".SID."'>my settings</a>\n";
325        }
326        if ($permissions & $ADMIN)
327           $systemmenu.=" <option value='users.php$SID'>users</a>\n";
328        if ($permissions & $SUPER) {
329           $systemmenu.=" <option value='groups.php$SID'>groups</a>\n";
330           $systemmenu.=" <option value='tablemanage.php$SID'>table design</a>\n";
331           $systemmenu.=" <option value='dumptable.php$SID'>export tables</a>\n";
332           $systemmenu.=" <option value='import.php$SID'>import data</a>\n";
333           $systemmenu.=" <option value='dumpcontent.php$SID'>export data</a>\n";
334           $systemmenu.=" <option value='linkbar.php$SID'>linkbar</a>\n";
335           $systemmenu.=" <option value='setup.php$SID'>setup</a>\n";
336        }
337        if ($permissions) {
338           $systemmenu.=" <option value='logout.php$SID'>logout</a>\n";
339           $systemmenu .="</select>\n";
340        }
341        else
342           $systemmenu="<a href='login.php'>login</a>\n";
343 
344        // display the menus
345        echo "<table border=0 width=100% cellspacing='0' cellpadding='0' bgcolor='eeeeff'>\n";
346        echo "<tr>\n<form name='menubar'>\n";
347        echo "<td width='7%'>&nbsp;</td>\n";
348        echo "<td width='1%'>$linkmenu</td>\n";
349        echo "<td width='1%'>$tablemenu</td>\n";
350        echo "<td width='1%'>$systemmenu</td>\n";
351        echo "<td width='90%'>&nbsp;</td>\n";
352        echo "</tr>\n</form>\n</table>\n";
353     }
354     
355     // No javascript or the user preferes the old style menus:
356     else {
357        $links_per_row=6;
358     
359        if ($permissions & $ACTIVE) {
360                                  
361           echo "<table border=0 width=100% cellspacing='0' cellpadding='0' bgcolor='eeeeff'>\n";
362           $records=$db->Execute("select tablename,custom,id,label from tableoftables where display='Y' and permission='Users' ORDER by sortkey");
363           $count=0;
364           if ($records) {
365              $query="SELECT tableid FROM groupxtable_display WHERE (groupid='".$USER["group_array"][0]."' ";
366              for ($i=1;$i<sizeof($USER["group_array"]);$i++)
367                 $query.="OR groupid='".$USER["group_array"][$i]."' ";
368           $query.=")";
369              $rb=$db->Execute($query);
370              while ($rb && !$rb->EOF) {
371                 $showtables[]=$rb->fields["tableid"];
372                 $rb->MoveNext();
373              }
374              while (!$records->EOF) {
375                 if (in_array($records->fields["id"],$showtables)) {
376                    if (($count % $links_per_row)==0)
377                       echo "</tr>\n<tr bgcolor='eeeeff' align='center'>\n";
378                    $tabname=$records->fields[0];
379                    $scriptname=$records->fields[1];
380                    $label=$records->fields["label"];
381                    $linkname="";
382                    if ($scriptname=="")
383                       $linkname="general.php?tablename=$tabname&".SID;
384                    else
385                       $linkname=$scriptname."?".SID;
386                    echo " <td style='width: 20%' align='center'><a href=\"$linkname\">$label</a></td>\n";
387                    $count++;
388           }
389                 $records->MoveNext();
390              }
391              // the following is needed to make table look decent in Netscape 4
392              $range=$count % $links_per_row;
393              if ($range) {
394                 $range=$links_per_row-$range;
395                 for ($i=0;$i<$range;$i++)
396                    echo "<td style='width: 20% align='center'>&nbsp;</td>\n";
397              }
398           }
399        }
400        echo "</tr>\n</table>\n\n";
401 
402        echo "<table border=0 width=100% cellspacing='0' cellpadding='0'>\n";
403        echo "<tr bgcolor='eeeeff' align='center'>";
404        if ($permissions) {
405           ?>
406        <td align='center'><a href="users.php?type=me&<?=SID?>">settings</a></td>
407  <?php
408        }
409        if ($permissions & $ADMIN) {
410           ?>
411        <td align='center'><a href="users.php?<?=SID?>">users</a></td>
412  <?php
413        }
414        if ($permissions & $SUPER) {
415           ?>
416        <td align='center'><a href="groups.php?<?=SID?>">groups</a></td>
417        <td align='center'><a href="tablemanage.php?<?=SID?>">tables</a></td>
418        <td align='center'><a href="linkbar.php?<?=SID?>">linkbar</a></td>
419        <td align='center'><a href="setup.php?<?=SID?>">system</a></td>
420  <?php
421        }
422        if ($permissions) {
423           ?>
424        <td align='right'><a href="logout.php?<?=SID?>">logout</a>&nbsp;</td>
425  <?php
426        }
427        else
428           echo "<td align='right'><a href='login.php'>login</a></td>";
429        echo "</tr>\n</table>\n<hr>\n";
430     }
431     echo "<!--************************END OF NAVBAR**********************-->\n";
432  }
433 
434  ////
435  // !adds javascript headers to argument
436  function add_js ($script) {
437     $out="\n<script language='Javascript'>\n<!--\n";
438     $out.=$script;
439     $out.="\n//End Javascript -->\n</script>\n\n";
440     return $out;
441  }
442 
443  ////
444  // !Prints initial part of webpage
445  function printheader($title,$head=false, $jsfile=false) {
446     global $client,$db,$version,$active,$USER,$HTTP_SESSION_VARS;
447 
448     // let Netscape 4 users use their back button
449     // all others should not cache
450     if ($client->browser != 'Netscape 4') {
451        header('Cache-Control: private, no-cache, must-revalidate');
452        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
453        header('Pragma: no-cache');
454     }
455     header ('Content-Type: text/html; charset=ISO-8859-1');
456 
457  ?>
458  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN"
459          "http://www.w3.org/TR/html4/loose.dtd">
460  <HTML>
461  <HEAD>
462  <?php
463  echo $head;
464  if ($HTTP_SESSION_VARS['javascript_enabled'] && $USER['settings']['menustyle']) {
465     echo "\n<script language='Javascript'>\n<!--\n";
466     if ($jsfile && is_readable($jsfile))
467        readfile($jsfile);
468     readfile("includes/js/linkmenu.js");
469     echo "\n// End Javascript -->\n</script>\n\n";
470     $mode='menu';
471  }
472  if ($mode<>'menu') {
473        $links_per_row=5;
474        $r=$db->Execute("select display from tableoftables where tablename ='linkbar'");
475        if ($r->fields[0]=='1') {
476           $linkr=$db->Execute("select label,linkurl,target from linkbar where display ='Y' ORDER by sortkey");
477           if ($linkr) {
478              while (!$linkr->EOF) {
479                 if ($count && (($count%$links_per_row)==0) )
480                   $linkbar.="</tr><tr bgcolor='333388' align='center'>";
481                 $Tlinkname=$linkr->fields[0];
482                 $urlname=$linkr->fields[1];
483                 if ($linkr->fields[2]=='S')
484                    $targetstr="target='_TOP'";
485                 else
486                    $targetstr="target='_BLANK'";
487                 $linkbar.="<td style='width: 20%' align='center'><a href=\"$urlname\" $targetstr><font color='#ffffff'>$Tlinkname</font></a></td>\n";
488                 $count++;
489                 $linkr->MoveNext();
490              }
491           }
492        }
493     }
494    //TOPMARGIN="0" LEFTMARGIN="0"
495    //MARGINWIDTH="0" MARGINHEIGHT="0">
496 
497  ?>
498  <TITLE><?php echo "$title" ?></TITLE>
499  <LINK rel="STYLESHEET" type="text/css" href="phplabware.css">
500  </HEAD>
501  <BODY BGCOLOR="#ffffff">
502  <table width="100%" rules="none" border="0" cellspacing="0" cellpadding="0" bgcolor="333388">
503  <tr class='header' bgcolor="333388">
504  <?php
505     // get the time
506     $today = date('F j, Y, g:i a');
507     // first display the linkbar if activated
508     // only show linkbar when we have been authenticated
509     if ($active) {
510        echo '<td>';
511        if ($mode<>'menu')
512           echo "$linkbar";
513        echo '</td>';
514     }
515 
516     ?>
517        <td align=left>
518           <font color="#ffffff"><b><i>
519           <?php echo "$today"; ?>
520           </i></b></font>
521        </td>
522        <td align=right>
523           <a href="http://phplabware.sourceforge.net">
524           <font color="#ffffff"><i>PhpLabWare
525               <?php if ($version) echo "version $version"; ?>&nbsp;</i></font>
526           </a>
527        </td>
528     </tr>
529  </table>
530  <a name="top"></a>
531  <!--************************END OF PRINTHEADER**************************-->
532 
533  <?php
534  }
535 
536  ////
537  // !Prints footer
538  function printfooter($db=false,$USER=false) {
539  ?>
540 
541  <!--********************START OF PRINTFOOTER****************************-->
542  &nbsp;<br>
543  <hr>
544  </BODY>
545  </HTML>
546 
547  <?php
548     if ($db && $USER["settings"])
549        $db->Execute("UPDATE users SET settings='".serialize($USER["settings"])."'
550         WHERE id=".$USER["id"]);
551  }
552  ?>


Generated: Sun Oct 5 21:17:35 2003 SourceForge Logo Generated by PHPXref 0.2