owl.lib.php 46.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
<?php

/*
 * owl.lib.php
 *
 * Copyright (c) 1999-2002 The Owl Project Team
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 *
 * $Id$
 */

// Support for reg.globals off WES

if (substr(phpversion(),0,5) >= "4.1.0")
        import_request_variables('pgc');
 else {
        if (!EMPTY($_POST)) {
                extract($_POST);
        } else {
                extract($HTTP_POST_VARS);
        }
        if (!EMPTY($_GET)) {
                extract($_GET);
        } else {
                extract($HTTP_GET_VARS);
        }
        if (!EMPTY($_FILE)) {
                extract($_FILE);
        } else {
                extract($HTTP_POST_FILES);
        }
}


if(!isset($sess)) $sess = 0;
if(!isset($loginname)) $loginname = 0;
if(!isset($login)) $login = 0;

if(isset($default->owl_lang)) {
	$langdir = "$default->owl_fs_root/locale/$default->owl_lang";
	if(is_dir("$langdir") != 1) {
		die("$lang_err_lang_1 $langdir $lang_err_lang_2");
	} else {
		$sql = new Owl_DB;
                $sql->query("select * from $default->owl_sessions_table where sessid = '$sess'");
		$sql->next_record();
                $numrows = $sql->num_rows($sql);
                $getuid = $sql->f("uid");
		if($numrows == 1) {
                	$sql->query("select * from $default->owl_users_table where id = $getuid");
			$sql->next_record();
                	$language = $sql->f("language");
			// BEGIN wes fix
			if(!$language) {
			  $language = $default->owl_lang;
			}
			// END wes fix
			require("$default->owl_fs_root/locale/$language/language.inc");
			$default->owl_lang = $language;
                }
                else
			require("$default->owl_fs_root/locale/$default->owl_lang/language.inc");
	}
} else {
	die("$lang_err_lang_notfound");
}


class Owl_DB extends DB_Sql {
        var $classname = "Owl_DB";

        // BEGIN wes changes -- moved these settings to config/owl.php
        // Server where the database resides
        var $Host = ""; 

        // Database name
        var $Database = "";

        // User to access database
        var $User = "";

        // Password for database
        var $Password = "";

	function Owl_DB() {
	  global $default;
	  $this->Host = $default->owl_db_host;
	  $this->Database = $default->owl_db_name;
	  $this->User = $default->owl_db_user;
	  $this->Password = $default->owl_db_pass;
	}
	// END wes changes

        function haltmsg($msg) {
                printf("</td></table><b>Database error:</b> %s<br>\n", $msg);
                printf("<b>SQL Error</b>: %s (%s)<br>\n",
                        $this->Errno, $this->Error);
        }
}


class Owl_Session {
	var $sessid;
	var $sessuid;
	var $sessdata;


	function Open_Session($sessid=0, $sessuid=0) {
                global $default;
		$this->sessid = $sessid;
		$this->sessuid = $sessuid;

		if($sessid == "0") { 		// if there is no user loged in, then create a session for them
			$current = time();
			$random = $this->sessuid . $current;
			$this->sessid = md5($random);
			$sql = new Owl_DB;
 			if(getenv("HTTP_CLIENT_IP")) {
                                $ip = getenv("HTTP_CLIENT_IP");
                        } elseif(getenv("HTTP_X_FORWARDED_FOR")) {
                                $forwardedip = getenv("HTTP_X_FORWARDED_FOR");
                                list($ip,$ip2,$ip3,$ip4)= split (",", $forwardedip);
                        } else {
                                $ip = getenv("REMOTE_ADDR");
                        }
			//$result = $sql->query("insert into active_sessions  values ('$this->sessid', '$this->sessuid', '$current', '$ip')");
			$result = $sql->query("insert into $default->owl_sessions_table  values ('$this->sessid', '$this->sessuid', '$current', '$ip')");
			if(!'result') die("$lang_err_sess_write");
		}

		// else we have a session id, try to validate it...
		$sql = new Owl_DB;
		$sql->query("select * from $default->owl_sessions_table where sessid = '$this->sessid'");

		// any matching session ids?
		$numrows = $sql->num_rows($sql);
		if(!$numrows) die("$lang_err_sess_notvalid");

		// return if we are a.o.k.
		while($sql->next_record()) {
			$this->sessdata["sessid"] = $sql->f("sessid");
		}
		return $this;
	}
}
function notify_users($groupid, $flag, $parent, $filename, $title, $desc, $type) {
                global $default;
                global $lang_notif_subject_new, $lang_notif_subject_upd, $lang_notif_msg;
                global $lang_title, $lang_description;
                $sql = new Owl_DB; 
// BEGIN BUG 548994
                $path = find_path($parent);
		$sql->query("select id from $default->owl_files_table where filename='$filename' AND parent='$parent'");
		$sql->next_record();
		$fileid = $sql->f("id");
// END BUG 548994 More Below
                $sql->query("select distinct id, email,language,attachfile from $default->owl_users_table as u, $default->owl_users_grpmem_table as m where notify = 1 and (u.groupid = $groupid or m.groupid = $groupid)");
                
                while($sql->next_record()) 
                {
// BEGIN BUG 548994
			if ( check_auth($fileid, "file_download", $sql->f(id)) == 1 ) {
// END BUG 548994 More Below
                  		$newpath = ereg_replace(" ","%20",$path);
                  		$newfilename = ereg_replace(" ","%20",$filename);
				$DefUserLang = $sql->f("language");
 				require("$default->owl_fs_root/locale/$DefUserLang/language.inc");

				$r=preg_split("(\;|\,)",$sql->f("email"));
				reset ($r);
				while (list ($occ, $email) = each ($r)) {
           				$mail = new phpmailer();
                                	// Create a temporary session id, the user
					// will need to get to this file before
					// the default session timeout
					$session = new Owl_Session;
                			$uid = $session->Open_Session(0,$sql->f("id"));
                                        $tempsess = $uid->sessdata["sessid"];
					
                  			if ( $flag == 0 ) {
                				$mail->IsSMTP();                                      // set mailer to use SMTP
                				$mail->Host = "$default->owl_email_server";        // specify main and backup server
                				$mail->From = "$default->owl_email_from";
                				$mail->FromName = "$default->owl_email_fromname";
                        			$mail->AddAddress($email);
                        			$mail->AddReplyTo("$default->owl_email_replyto", "OWL Intranet");
                				$mail->WordWrap = 50;                                 // set word wrap to 50 characters
                				$mail->IsHTML(true);                                  // set email format to HTML
                				$mail->Subject = "$lang_notif_subject_new";
						if ($type != "url") {
                                                        if ($sql->f("attachfile") == 1) {
                                                                $mail->Body    = "$lang_notif_msg<BR><BR>" . "$lang_title: $title" . "<BR><BR>$lang_description: $desc";
                        					$mail->altBody = "$lang_notif_msg\n\n" . "$lang_title: $title" . "\n\n $lang_description: $desc";
								if (!$default->owl_use_fs) {
									if (file_exists("$default->owl_FileDir/$filename")) {
                                        					unlink("$default->owl_FileDir/$filename");
                                					}
                                					$file = fopen("$default->owl_FileDir$filename", 'wb');
									$getfile = new Owl_DB;	
                                					$getfile->query("select data,compressed from $default->owl_files_data_table where id='$fileid'");
                                					while ($getfile->next_record()) {
                                        					if ($getfile->f("compressed")) {
					
                                                					$tmpfile = $default->owl_FileDir . "owltmp.$fileid.gz";
                                                					$uncomptmpfile = $default->owl_FileDir . "owltmp.$fileid";
                                                					if (file_exists($tmpfile)) unlink($tmpfile);
					
                                                					$fp=fopen($tmpfile,"w");
                                                					fwrite($fp, $getfile->f("data"));
                                                					fclose($fp);

                                                					system($default->gzip_path . " -df $tmpfile");

                                                					$fsize = filesize($uncomptmpfile);
                                                					$fd = fopen($uncomptmpfile, 'rb');
                                                					$filedata = fread($fd, $fsize);
                                                					fclose($fd);

                                                					fwrite($file, $filedata);
                                                					unlink($uncomptmpfile);
                                        					} else {
                                                					fwrite($file, $getfile->f("data"));
                                        					}
                                					}
                                					fclose($file);
                        						$mail->AddAttachment("$default->owl_FileDir$newfilename");
								} else {
                        						$mail->AddAttachment("$default->owl_FileDir/$newpath/$newfilename");
								}
                                                        }
                                                        else {
                                                                $mail->Body    = "$lang_notif_msg<BR><BR>" . "$lang_title: $title" . "<BR><BR>URL:  $default->owl_notify_link" . "browse.php?sess=$tempsess&parent=$parent&expand=1&fileid=$fileid" . "<BR><BR>$lang_description: $desc";
                        					$mail->altBody = "$lang_notif_msg\n\n" . "$lang_title: $title" . "\n\n $lang_description: $desc";
                                                        }
                				}
                				else {
                        				$mail->Body    = "URL: $newfilename <BR><BR>$lang_notif_msg<BR><BR>" . "$lang_title: $title" . "<BR><BR>$lang_description: $desc";
                        				$mail->altBody = "URL: $newfilename \n\n$lang_notif_msg\n\n" . "$lang_title: $title" . "\n\n $lang_description: $desc";
                				}

                                 	}
                  			else {
 						$mail = new phpmailer();
                                                $mail->IsSMTP();                                      // set mailer to use SMTP
                                                $mail->Host = "$default->owl_email_server";        // specify main and backup server
                                                $mail->From = "$default->owl_email_from";
                                                $mail->FromName = "$default->owl_email_fromname";
                                                $mail->AddAddress($email);
                                                $mail->AddReplyTo("$default->owl_email_replyto", "OWL Intranet");
                                                $mail->WordWrap = 50;                                 // set word wrap to 50 characters
                                                $mail->IsHTML(true);                                  // set email format to HTML
						$mail->Subject = "$lang_notif_subject_upd";
                                                if ($type != "url") {
                                                        if ($sql->f("attachfile") == 1) {
                                                                $mail->Body    = "$lang_notif_msg<BR><BR>" . "$lang_title: $title" . "<BR><BR>$lang_description: $desc";
                                                                $mail->altBody = "$lang_notif_msg\n\n" . "$lang_title: $title" . "\n\n $lang_description: $desc";
								if (!$default->owl_use_fs) {
                                                                        if (file_exists("$default->owl_FileDir/$filename")) {
                                                                                unlink("$default->owl_FileDir/$filename");
                                                                        }
                                                                        $file = fopen("$default->owl_FileDir$filename", 'wb');
                                                                        $getfile = new Owl_DB;
                                                                        $getfile->query("select data,compressed from $default->owl_files_data_table where id='$fileid'");
                                                                        while ($getfile->next_record()) {
                                                                                if ($getfile->f("compressed")) {

                                                                                        $tmpfile = $default->owl_FileDir . "owltmp.$fileid.gz";
                                                                                        $uncomptmpfile = $default->owl_FileDir . "owltmp.$fileid";
                                                                                        if (file_exists($tmpfile)) unlink($tmpfile);

                                                                                        $fp=fopen($tmpfile,"w");
                                                                                        fwrite($fp, $getfile->f("data"));
                                                                                        fclose($fp);

                                                                                        system($default->gzip_path . " -df $tmpfile");

                                                                                        $fsize = filesize($uncomptmpfile);
                                                                                        $fd = fopen($uncomptmpfile, 'rb');
                                                                                        $filedata = fread($fd, $fsize);
                                                                                        fclose($fd);

                                                                                        fwrite($file, $filedata);
                                                                                        unlink($uncomptmpfile);
                                                                                } else {
                                                                                        fwrite($file, $getfile->f("data"));
                                                                                }
                                                                        }
                                                                        fclose($file);
                                                                        $mail->AddAttachment("$default->owl_FileDir$newfilename");
                                                                } else {
                                                                        $mail->AddAttachment("$default->owl_FileDir/$newpath/$newfilename");
                                                                }

                                                        }
                                                        else {
                                                                $mail->Body    = "$lang_notif_msg<BR><BR>" . "$lang_title: $title" . "<BR><BR>URL:  $default->owl_notify_link" . "browse.php?sess=$tempsess&parent=$parent&expand=1&fileid=$fileid" . "<BR><BR>$lang_description: $desc";
                                                                $mail->altBody = "$lang_notif_msg\n\n" . "$lang_title: $title" . "\n\n $lang_description: $desc";
                                                        } 
                                                }
                                                else {
                                                        $mail->Body    = "URL: $newfilename <BR><BR>$lang_notif_msg<BR><BR>" . "$lang_title: $title" . "<BR><BR>$lang_description: $desc";
                                                        $mail->altBody = "URL: $newfilename \n\n$lang_notif_msg\n\n" . "$lang_title: $title" . "\n\n $lang_description: $desc";
                                                }
                                     	}
                  			$mail->Send();
					if (!$default->owl_use_fs && $sql->f("attachfile") == 1) {
                        			unlink("$default->owl_FileDir$newfilename");
					}
					
                               }
                            }
// BEGIN BUG 548994
                	}
// END BUG 548994
}

function verify_login($username, $password) {
	global $default;
	$sql = new Owl_DB; 
	$query = "select * from $default->owl_users_table where username = '$username' and password = '" . md5($password) . "'";
	$sql->query("select * from $default->owl_users_table where username = '$username' and password = '" . md5($password) . "'");
	$numrows = $sql->num_rows($sql);
        // Bozz Begin added Password Encryption above, but for now 
        // I will allow admin to use non crypted password untile he 
        // upgrades all users 
	if ($numrows == "1") {
               	while($sql->next_record()) {
                       if ( $sql->f("disabled") == 1 )  
                        	$verified["bit"]        = 2;
                       else
                        	$verified["bit"]        = 1;
                        $verified["user"]       = $sql->f("username");
                        $verified["uid"]        = $sql->f("id");
                        $verified["group"]      = $sql->f("groupid");
                        $maxsessions            = $sql->f("maxsessions") + 1;
                }
        }
        // Remove this else in a future version
        else {
           if ($username == "admin") {
	        $sql->query("select * from $default->owl_users_table where username = '$username' and password = '$password'");
                $numrows = $sql->num_rows($sql);
                if ($numrows == "1") {
                   while($sql->next_record()) {
                        $verified["bit"]        = 1;
                        $verified["user"]       = $sql->f("username");
                        $verified["uid"]        = $sql->f("id");
                        $verified["group"]      = $sql->f("groupid");
                        $maxsessions            = $sql->f("maxsessions") + 1;
                   }
                }
           }
        }

        // remove stale sessions from the database for the user
        // that is signing on.
        //
        $time = time() -  $default->owl_timeout;
	$sql = new Owl_DB; $sql->query("delete from $default->owl_sessions_table where uid = '".$verified["uid"]."' and lastused <= $time ");
        // Check if Maxsessions has been reached
        //

	$sql = new Owl_DB; 
        $sql->query("select * from $default->owl_sessions_table where uid = '".$verified["uid"]."'");

	if ($sql->num_rows($sql) >= $maxsessions && $verified["bit"] != 0) {
          if ( $verified["group"] == 0)
	  	$verified["bit"] = 1;
          else
	  	$verified["bit"] = 3;
	}
	return $verified;
}

function verify_session($sess) {
        getprefs();
	global $default, $lang_sesstimeout, $lang_sessinuse, $lang_clicklogin;
        $sess = ltrim($sess);
	$verified["bit"] = 0;
	$sql = new Owl_DB; 
        $sql->query("select * from $default->owl_sessions_table where sessid = '$sess'");
	$numrows = $sql->num_rows($sql);
	$time = time();
	if ($numrows == "1") {
		while($sql->next_record()) {
			if(getenv("HTTP_CLIENT_IP")) {
				$ip = getenv("HTTP_CLIENT_IP");
			} elseif(getenv("HTTP_X_FORWARDED_FOR")) {
				$forwardedip = getenv("HTTP_X_FORWARDED_FOR");
				list($ip,$ip2,$ip3,$ip4)= split (",", $forwardedip);
			} else {
				$ip = getenv("REMOTE_ADDR");
			}
			if ($ip == $sql->f("ip")) {
				if(($time - $sql->f("lastused")) <= $default->owl_timeout) {
					$verified["bit"] = 1;
					$verified["userid"] = $sql->f("uid");
					$sql->query("select * from $default->owl_users_table where id = '".$verified["userid"]."'");
					while($sql->next_record()) $verified["groupid"] = $sql->f("groupid");
				} else {
                                        // Bozz Bug Fix begin
                                        if (file_exists("./lib/header.inc")) {
					    include("./lib/header.inc");
                                        } else {
                                           include("../lib/header.inc");
                                        }
                                        // Bozz Buf Fix End
					print("<BR><BR><CENTER>".$lang_sesstimeout);
                                        if ($parent == "" || $fileid == "")
						print("<A HREF='$default->owl_root_url/index.php'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/btn_login.gif' BORDER=0 ></A>");
                                        else
						print("<A HREF='$default->owl_root_url/index.php?parent=$parent&fileid=$fileid'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/btn_login.gif' BORDER=0 ></A>");
					exit();
				}
			} else {
                                // Bozz Bug Fix begin
                                if (file_exists("./lib/header.inc")) {
				    include("./lib/header.inc");
                                } else {
                                    include("../lib/header.inc");
                                }
                                // Bozz Bug Fix End
				print("<BR><BR><CENTER>".$lang_sessinuse);
				exit;
			}
		}
	}
	return $verified;
}

function fid_to_name($parent) {
	global $default;
	$sql = new Owl_DB; $sql->query("select name from $default->owl_folders_table where id = $parent");
	while($sql->next_record()) return $sql->f("name");
}

function flid_to_name($id) {
	global $default;
	$sql = new Owl_DB; $sql->query("select name from $default->owl_files_table where id = $id");
	while($sql->next_record()) return $sql->f("name");
}

function flid_to_filename($id) {
	global $default;
	$sql = new Owl_DB; $sql->query("select filename from $default->owl_files_table where id = $id");
	while($sql->next_record()) return $sql->f("filename");
}

function owlusergroup($userid) {
	global $default;
	$sql = new Owl_DB; $sql->query("select groupid from $default->owl_users_table where id = '$userid'");
	while($sql->next_record()) $groupid = $sql->f("groupid");
	return $groupid;
}

function owlfilecreator($fileid) {
	global $default;
	$sql = new Owl_DB; $sql->query("select creatorid from ".$default->owl_files_table." where id = '$fileid'");
	while($sql->next_record()) $filecreator = $sql->f("creatorid");
	return $filecreator;
}

function owlfoldercreator($folderid) {
	global $default;
	$sql = new Owl_DB; $sql->query("select creatorid from ".$default->owl_folders_table." where id = '$folderid'");
	while($sql->next_record()) $foldercreator = $sql->f("creatorid");
	return $foldercreator;
}

function owlfilegroup($fileid) {
	global $default;
	$sql = new Owl_DB; $sql->query("select groupid from $default->owl_files_table where id = '$fileid'");
	while($sql->next_record()) $filegroup = $sql->f("groupid");
	return $filegroup;
}

function owlfoldergroup($folderid) {
	global $default;
	$sql = new Owl_DB; $sql->query("select groupid from $default->owl_folders_table where id = '$folderid'");
	while($sql->next_record()) $foldergroup = $sql->f("groupid");
	return $foldergroup;
}

function owlfolderparent($folderid) {
	global $default;
	$sql = new Owl_DB; $sql->query("select parent from $default->owl_folders_table where id = '$folderid'");
	while($sql->next_record()) $folderparent = $sql->f("parent");
	return $folderparent;
}

function owlfileparent($fileid) {
	global $default;
	$sql = new Owl_DB; $sql->query("select parent from $default->owl_files_table where id = '$fileid'");
	while($sql->next_record()) $fileparent = $sql->f("parent");
	return $fileparent;
}

function fid_to_creator($id) {

	global $default;
	$sql = new Owl_DB; 
	$sql->query("select creatorid from ".$default->owl_files_table." where id = '$id'");
	$sql2 = new Owl_DB; 
	while($sql->next_record()) {
		$creatorid = $sql->f("creatorid");
		$sql2->query("select name from $default->owl_users_table where id = '".$creatorid."'");
		$sql2->next_record();
		$name = $sql2->f("name");
	}
	return $name;
}

function group_to_name($id) {
	global $default;
	$sql = new Owl_DB; $sql->query("select name from $default->owl_groups_table where id = '$id'");
	while($sql->next_record()) return $sql->f("name");
}

function uid_to_name($id) {
	global $default;
	$sql = new Owl_DB; $sql->query("select name from $default->owl_users_table where id = '$id'");
	while($sql->next_record()) $name = $sql->f("name");
	if ($name == "") $name = "Owl";
	return $name;
}

function prefaccess($id) {
	global $default;
	$prefaccess = 1;
	$sql = new Owl_DB; $sql->query("select noprefaccess from $default->owl_users_table where id = '$id'");
	while($sql->next_record()) $prefaccess = !($sql->f("noprefaccess"));
	return $prefaccess;
}

function gen_navbar($parent) {
	global $default;
	global $sess, $expand, $sort, $sortorder, $order;
	$name = fid_to_name($parent);
	$navbar = "<A HREF='browse.php?sess=$sess&parent=$parent&expand=$expand&order=$order&$sortorder=$sort'>$name</A>";
	$new = $parent;
	while ($new != "1") {
		$sql = new Owl_DB; $sql->query("select parent from $default->owl_folders_table where id = '$new'");
		while($sql->next_record()) $newparentid = $sql->f("parent");
		$name = fid_to_name($newparentid);
		$navbar = "<A HREF='browse.php?sess=$sess&parent=$newparentid&expand=$expand&order=$order&$sortorder=$sort'>$name</A>/" . $navbar;
		$new = $newparentid;
	}
	return $navbar;
}

//only get dir path from db
function get_dirpath($parent) {
        global $default;
        global $sess, $expand;
        $name = fid_to_name($parent);
        $navbar = "$name";
        $new = $parent;
        while ($new != "1") {
                $sql = new Owl_DB; $sql->query("select parent from $default->owl_folders_table where id = '$new'");
                while($sql->next_record()) $newparentid = $sql->f("parent");
                $name = fid_to_name($newparentid);
                $navbar = "$name/" . $navbar;
                $new = $newparentid;
        }
        return $navbar;
}


function gen_filesize($file_size) {
	if(ereg("[^0-9]", $file_size)) return $file_size;

	if ($file_size >= 1073741824) {
		$file_size = round($file_size / 1073741824 * 100) / 100 . "g";
        } elseif ($file_size >= 1048576) {
                $file_size = round($file_size / 1048576 * 100) / 100 . "m";
        } elseif ($file_size >= 1024) {
                $file_size = round($file_size / 1024 * 100) / 100 . "k";
        } else {
                $file_size = $file_size . "b"; 
        }
	return $file_size;
}

function uploadCompat($varname) {

   if ($_FILES[$varname]) return $_FILES[$varname];
   if ($HTTP_POST_FILES[$varname]) return $HTTP_POST_FILES[$varname];
   $tmp = "$varname_name"; global $$tmp; $retfile['name'] = $$tmp;
   $tmp = "$varname_type"; global $$tmp; $retfile['type'] = $$tmp;
   $tmp = "$varname_size"; global $$tmp; $retfile['size'] = $$tmp;
   $tmp = "$varname_error"; global $$tmp; $retfile['error'] = $$tmp;
   $tmp = "$varname_tmp_name"; global $$tmp; $retfile['tmp_name'] = $$tmp;
   return $retfile;
}


if ($sess) {
        gethtmlprefs();
        $ok = verify_session($sess);
        $temporary_ok =  $ok["bit"];
	$userid = $ok["userid"];
	$usergroupid = $ok["groupid"];
        if ($ok["bit"] != "1") {
                // Bozz Bug Fix begin
                if (file_exists("./lib/header.inc")) {
                     include("./lib/header.inc");
                } else {
                     include("../lib/header.inc");
                }
                // Bozz Bug Fix end
                print("<BR><BR><CENTER>".$lang_invalidsess);
                if ($parent == "" || $fileid == "")
			print("<A HREF='$default->owl_root_url/index.php'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/btn_login.gif' BORDER=0 ></A>");
                else
			print("<A HREF='$default->owl_root_url/index.php?parent=$parent&fileid=$fileid'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/btn_login.gif' BORDER=0 ></A>");
		exit;
        } else {
		$lastused = time();
		$sql = new Owl_DB;
		$sql->query("update $default->owl_sessions_table set lastused = '$lastused' where uid = '$userid'");
	}
}
function checkrequirements()
{
    global $default, $lang_err_bad_version_1, $lang_err_bad_version_2, $lang_err_bad_version_3;

    if (substr(phpversion(),0,5) < $default->phpversion) {
        print("<CENTER><H3>$lang_err_bad_version_1<BR>");
        print("$default->phpversion<BR>");
        print("$lang_err_bad_version_2<BR>");
        print phpversion();
        print("<BR>$lang_err_bad_version_3</H3></CENTER>");
        return 1; 
    }
    else {
        return 0;
    }
}

function myExec($_cmd, &$lines, &$errco) {
	$cmd = "$_cmd ; echo $?";
	exec($cmd, $lines);
	// Get rid of the last errco line...
	$errco = (integer) array_pop($lines);
	if (count($lines) == 0) {
		return "";
	} else {
		return $lines[count($lines) - 1];
	}
}

function myDelete($file) {
	if (file_exists($file)) {
		chmod($file,0777);
		if (is_dir($file)) {
			$handle = opendir($file);
			while($filename = readdir($handle)) {
				if ($filename != "." && $filename != "..") {
					myDelete($file."/".$filename);
				}
			}
			closedir($handle);
			rmdir($file);
		} else {
			unlink($file);
		}
	}
}

function printError($message, $submessage) {
	global $default;
        global $sess, $parent, $expand, $order, $sortorder ,$sortname, $userid;
        global $language;
 
	require("$default->owl_fs_root/locale/$default->owl_lang/language.inc");
	include("./lib/header.inc");

	if(check_auth($parent, "folder_view", $userid) != "1") {
 		$sql = new Owl_DB;
                $sql->query("select * from $default->owl_folders_table where id = '$parent'");
                $sql->next_record();
                $parent = $sql->f("parent");
	}

	echo("<TABLE WIDTH=$default->table_expand_width BGCOLOR=\"#d0d0d0\" CELLSPACING=0 CELLPADDING=0 BORDER=0 HEIGHT=30>");
	echo("<TR><TD ALIGN=LEFT>");
	print("$lang_user: ");
	print("<A HREF='prefs.php?owluser=$userid&sess=$sess&expand=$expand&order=$order&sortname=$sortname'>");
	print uid_to_name($userid);
	print ("</A><FONT SIZE=-1>");
	print("<A HREF='index.php?login=logout&sess=$sess'> $lang_logout</A>");
	print("</FONT></TD>");
	print("<TD ALIGN=RIGHT><A HREF='browse.php?sess=$sess&parent=$parent&expand=$expand&order=$order&$sortorder=$sortname'><IMG SRC='$default->owl_root_url/locale/$language/graphics/btn_browse.gif' BORDER=0></A>");
	print("</TD></TR></TABLE><BR><BR><CENTER>");
	print $message;
        print("<BR>");
	print $submessage;
	include("./lib/footer.inc");
        exit();
}

function getprefs ( )
{
	global $default;

	$sql = new Owl_DB;
	//$sql->query("select * from $default->owl_prefs_table");
	$sql->query("select * from prefs");
	$sql->next_record();

	$default->owl_email_from     	= $sql->f("email_from");
	$default->owl_email_fromname   	= $sql->f("email_fromname");
	$default->owl_email_replyto    	= $sql->f("email_replyto");
	$default->owl_email_server     	= $sql->f("email_server");
	$default->owl_LookAtHD     	= $sql->f("lookathd");
	$default->owl_def_file_security	= $sql->f("def_file_security");
	$default->owl_def_file_group_owner= $sql->f("def_file_group_owner");
	$default->owl_def_file_owner    = $sql->f("def_file_owner");
	$default->owl_def_file_title    = $sql->f("def_file_title");
	$default->owl_def_file_meta     = $sql->f("def_file_meta");
	$default->owl_def_fold_security	= $sql->f("def_fold_security");
	$default->owl_def_fold_group_owner= $sql->f("def_fold_group_owner");
	$default->owl_def_fold_owner   	= $sql->f("def_fold_owner");
	$default->max_filesize     	= $sql->f("max_filesize");
	$default->owl_timeout     	= $sql->f("timeout");
	$default->expand       		= $sql->f("expand");
	$default->owl_version_control  	= $sql->f("version_control");
	$default->restrict_view       	= $sql->f("restrict_view");
	$default->dbdump_path 		= $sql->f("dbdump_path");
	$default->gzip_path 		= $sql->f("gzip_path");
	$default->tar_path 		= $sql->f("tar_path");


};


function gethtmlprefs ( )
{
	global $default;

	$sql = new Owl_DB;
	$sql->query("select * from $default->owl_html_table");
	$sql->next_record();

	$default->table_border          = $sql->f("table_border");
	$default->table_header_bg       = $sql->f("table_header_bg");
	$default->table_cell_bg         = $sql->f("table_cell_bg");
	$default->table_cell_bg_alt     = $sql->f("table_cell_bg_alt");
	$default->table_expand_width    = $sql->f("table_expand_width");
	$default->table_collapse_width  = $sql->f("table_collapse_width");
	$default->main_header_bgcolor	= $sql->f("main_header_bgcolor");
	$default->body_bgcolor          = $sql->f("body_bgcolor");
	$default->body_textcolor        = $sql->f("body_textcolor");
	$default->body_link             = $sql->f("body_link");
	$default->body_vlink            = $sql->f("body_vlink");

};

function printfileperm($currentval, $namevariable, $printmessage, $type) {
	global $default;
	global $lang_everyoneread, $lang_everyonewrite, $lang_everyonewrite_nod, $lang_groupread, $lang_groupwrite, $lang_groupwrite_nod, $lang_groupwrite_worldread, $lang_groupwrite_worldread_nod, $lang_onlyyou;
	global $lang_everyoneread_ad, $lang_everyonewrite_ad, $lang_everyonewrite_ad_nod, $lang_groupread_ad, $lang_groupwrite_ad, $lang_groupwrite_ad_nod, $lang_groupwrite_worldread_ad, $lang_groupwrite_worldread_ad_nod, $lang_onlyyou_ad;


                   $file_perm[0][0] = 0;
                   $file_perm[1][0] = 1;
                   $file_perm[2][0] = 2;
                   $file_perm[3][0] = 3;
                   $file_perm[4][0] = 4;
                   $file_perm[5][0] = 5;
                   $file_perm[6][0] = 6;
                   $file_perm[7][0] = 7;
                   $file_perm[8][0] = 8;
	if ($type == "admin") {
        	$file_perm[0][1] = "$lang_everyoneread_ad";
       		$file_perm[1][1] = "$lang_everyonewrite_ad";
       		$file_perm[2][1] = "$lang_groupread_ad";
       		$file_perm[3][1] = "$lang_groupwrite_ad";
       		$file_perm[4][1] = "$lang_onlyyou_ad";
       		$file_perm[5][1] = "$lang_groupwrite_ad_nod";
       		$file_perm[6][1] = "$lang_everyonewrite_ad_nod";
       		$file_perm[7][1] = "$lang_groupwrite_worldread_ad";
       		$file_perm[8][1] = "$lang_groupwrite_worldread_ad_nod";
      	} 
	else {
        	$file_perm[0][1] = "$lang_everyoneread";
       		$file_perm[1][1] = "$lang_everyonewrite";
       		$file_perm[2][1] = "$lang_groupread";
       		$file_perm[3][1] = "$lang_groupwrite";
       		$file_perm[4][1] = "$lang_onlyyou";
       		$file_perm[5][1] = "$lang_groupwrite_nod";
       		$file_perm[6][1] = "$lang_everyonewrite_nod";
       		$file_perm[7][1] = "$lang_groupwrite_worldread";
       		$file_perm[8][1] = "$lang_groupwrite_worldread_nod";
	}
            
          print("<TR><TD ALIGN=RIGHT BGCOLOR=$default->table_header_bg>$printmessage</TD><TD align=left><SELECT NAME=$namevariable>");
                foreach($file_perm as $fp) {
                        print("<OPTION VALUE=$fp[0] ");
                        if($fp[0] == $currentval)
                                print("SELECTED");
                        print(">$fp[1]");
                }
                           print("</SELECT></TD></TR>");


};

function printFileIcons ($fid, $filename, $checked_out, $url, $allicons, $ext)
{
	global $default;
	global $sess, $parent, $expand, $order, $sortorder ,$sortname, $userid;
	global $lang_log_file, $lang_reallydelete, $lang_del_file_alt, $lang_mod_file_alt;
	global $lang_move_file_alt,$lang_upd_file_alt,$lang_get_file_alt,$lang_lock_file,$lang_email_alt,$lang_view_file_alt;

	if ($allicons == 1)
	{
		if ($url != "1")
       			print("<a href='log.php?sess=$sess&id=".$fid."&filename=".$filename."&parent=$parent&expand=$expand&order=$order&sortname=$sortname'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/log.gif' BORDER=0 ALT='$lang_log_file' TITLE='$lang_log_file'></a>");
		}

     	if (($checked_out == 0) || ($checked_out == $userid)) {
       		// *****************************************************************************
    		// Don't Show the delete icon if the user doesn't have delete access to the file
    		// *****************************************************************************

    		if (check_auth($fid, "file_delete", $userid) == 1)
        		if ($url == "1")
            			print("\t<A HREF='dbmodify.php?sess=$sess&action=file_delete&type=url&id=".$fid."&parent=$parent&expand=$expand&order=$order&sortname=$sortname'\tonClick='return confirm(\"$lang_reallydelete ".$filename."?\");'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/trash.gif' ALT='$lang_del_file_alt' TITLE='$lang_del_file_alt'\tBORDER=0></A>");
    			else
            			print("\t<A HREF='dbmodify.php?sess=$sess&action=file_delete&id=".$fid."&parent=$parent&expand=$expand&order=$order&sortname=$sortname'\tonClick='return confirm(\"$lang_reallydelete ".$filename."?\");'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/trash.gif' ALT='$lang_del_file_alt' TITLE='$lang_del_file_alt'\tBORDER=0></A>");

		// *****************************************************************************
    		// Don't Show the modify icon if the user doesn't have modify access to the file
    		// *****************************************************************************

    		if(check_auth($fid, "file_modify", $userid) == 1)
            		print("<A HREF='modify.php?sess=$sess&action=file_modify&id=".$fid."&parent=$parent&expand=$expand&order=$order&sortname=$sortname'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/edit.gif' BORDER=0 ALT='$lang_mod_file_alt' TITLE='$lang_mod_file_alt'></A>");

    		// *****************************************************************************
    		// Don't Show the move modify icon if the user doesn't have move access to the file
    		// *****************************************************************************

    		if(check_auth($fid, "file_modify", $userid) == 1)
            		if ($url == "1")
                    		print("<A HREF='move.php?sess=$sess&id=".$fid."&parent=$parent&expand=$expand&action=file&type=url&order=$order&sortname=$sortname'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/icons/move.gif' BORDER=0 ALT='$lang_move_file_alt' TITLE='$lang_move_file_alt'></A>");
            		else
                    		print("<A HREF='move.php?sess=$sess&id=".$fid."&parent=$parent&expand=$expand&action=file&order=$order&sortname=$sortname'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/icons/move.gif' BORDER=0 ALT='$lang_move_file_alt' TITLE='$lang_move_file_alt'></A>");
		// *****************************************************************************
    		// Don't Show the file update icon if the user doesn't have update access to the file
    		// *****************************************************************************

    		if(check_auth($fid, "file_modify", $userid) == 1)
            		if ($url != "1")
                    		print("<A HREF='$default->owl_root_url/modify.php?sess=$sess&expand=$expand&action=file_update&order=$order&sortname=$sortname&id=".$fid."&parent=".$parent."'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/update.gif' BORDER=0 ALT='$lang_upd_file_alt' TITLE='$lang_upd_file_alt'></A>");

    		// *****************************************************************************
    		// Don't Show the file dowload icon if the user doesn't have download access to the file
    		// *****************************************************************************

    		if(check_auth($fid, "file_download", $userid) == 1)
            		if ($url != "1")
                    		print("<A HREF='$default->owl_root_url/download.php?sess=$sess&id=".$fid."&parent=".$parent."&binary=1'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/icons/bin.gif' BORDER=0 ALT='$lang_get_file_alt' TITLE='$lang_get_file_alt'></A>");

		if ($allicons == 1)
		{
    			// *****************************************************************************
    			// Don't Show the lock icon if the user doesn't have access to the file
    			// *****************************************************************************
    			if(check_auth($fid, "file_modify", $userid) == 1)
            			if ($url != "1")
                    			print("<A HREF='dbmodify.php?sess=$sess&action=file_lock&id=".$fid."&parent=$parent&expand=$expand&order=$order&sortname=$sortname'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/lock.gif' BORDER=0 ALT='$lang_lock_file' TITLE='$lang_lock_file'></a>");
			}

    		// *****************************************************************************
    		// Don't Show the email icon if the user doesn't have access to email the file
    		// *****************************************************************************

    		if(check_auth($fid, "file_modify", $userid) == 1)
            		if ($url == "1")
                    		print("<A HREF='$default->owl_root_url/modify.php?sess=$sess&expand=$expand&action=file_email&type=url&order=$order&sortname=$sortname&id=".$fid."&parent=".$parent."'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/email.gif' BORDER=0 ALT='$lang_email_alt' TITLE='$lang_email_alt'></A>");
            		else
                    		print("<A HREF='$default->owl_root_url/modify.php?sess=$sess&expand=$expand&action=file_email&order=$order&sortname=$sortname&id=".$fid."&parent=".$parent."'><IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/email.gif' BORDER=0 ALT='$lang_email_alt' TITLE='$lang_email_alt'></A>");
	
		// *****************************************************************************
		// Don't Show the view icon if the user doesn't have download access to the file
		// *****************************************************************************
		
		 if(check_auth($fid, "file_download", $userid) == 1)
    			if ($url != "1") {
            			$imgfiles = array("jpg","gif");
            			if ($ext != "" && preg_grep("/$ext/", $imgfiles)) {
                    			print("<A HREF='view.php?sess=$sess&id=".$fid."&parent=$parent&action=image_preview&expand=$expand&order=$order&sortname=$sortname'>&nbsp;<IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/mag.jpg' BORDER=0 ALT='$lang_view_file_alt' TITLE='$lang_view_file_alt'></A>");
            			}
            			$htmlfiles = array("html","htm",xml);
            			if ($ext != "" && preg_grep("/$ext/", $htmlfiles)) {
					print("<A HREF='view.php?sess=$sess&id=".$fid."&parent=$parent&action=html_show&expand=$expand&order=$order&sortname=$sortname'>&nbsp;<IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/mag.jpg' BORDER=0 ALT='$lang_view_file_alt' TITLE='$lang_view_file_alt'></A>");
            			}
            			$txtfiles = array("txt","text","README", "readme", "sh", "c", "cpp", "php", "php3", "pl", "perl", "sql", "py");
            			if ($ext != "" && preg_grep("/$ext/", $txtfiles)) {
					print("<A HREF='view.php?sess=$sess&id=".$fid."&parent=$parent&action=text_show&expand=$expand&order=$order&sortname=$sortname'>&nbsp;<IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/mag.jpg' BORDER=0 ALT='$lang_view_file_alt' TITLE='$lang_view_file_alt'></A>");
            			}
            			if (substr(php_uname(), 0, 7) != "Windows") {
                    			$zipfiles = array("tar.gz", "tgz", "tar", "gz");
                    			if ($ext != "" && preg_grep("/$ext/", $zipfiles))
                            			print("<A HREF='view.php?sess=$sess&id=".$fid."&parent=$parent&action=zip_preview&expand=$expand&order=$order&sortname=$sortname'>&nbsp;<IMG SRC='$default->owl_root_url/locale/$default->owl_lang/graphics/mag.jpg' BORDER=0 ALT='$lang_view_file_alt' TITLE='$lang_view_file_alt'></A>");
            			}
    			} 
	}
};

function printgroupperm($currentval, $namevariable, $printmessage, $type) {
	global $default;
	global $lang_geveryoneread, $lang_geveryonewrite, $lang_geveryonewrite_nod, $lang_ggroupread, $lang_ggroupwrite, $lang_ggroupwrite_nod, $lang_ggroupwrite_worldread, $lang_ggroupwrite_worldread_nod, $lang_gonlyyou;
        global $lang_geveryoneread_ad, $lang_geveryonewrite_ad, $lang_geveryonewrite_ad_nod, $lang_ggroupread_ad, $lang_ggroupwrite_ad, $lang_ggroupwrite_ad_nod, $lang_ggroupwrite_worldread_ad, $lang_ggroupwrite_worldread_ad_nod, $lang_gonlyyou_ad;


                   $group_perm[0][0] = 50;
                   $group_perm[1][0] = 51;
                   $group_perm[2][0] = 52;
                   $group_perm[3][0] = 53;
                   $group_perm[4][0] = 54;
                   $group_perm[5][0] = 55;
                   $group_perm[6][0] = 56;
                   $group_perm[7][0] = 57;
                   $group_perm[8][0] = 58;
	if ($type == "admin") {
                   $group_perm[0][1] = "$lang_geveryoneread_ad";
                   $group_perm[1][1] = "$lang_geveryonewrite_ad";
                   $group_perm[2][1] = "$lang_ggroupread_ad";
                   $group_perm[3][1] = "$lang_ggroupwrite_ad";
                   $group_perm[4][1] = "$lang_gonlyyou_ad";
                   $group_perm[5][1] = "$lang_ggroupwrite_ad_nod";
                   $group_perm[6][1] = "$lang_geveryonewrite_ad_nod";
                   $group_perm[7][1] = "$lang_ggroupwrite_worldread_ad";
                   $group_perm[8][1] = "$lang_ggroupwrite_worldread_ad_nod";

	}
	else {
                   $group_perm[0][1] = "$lang_geveryoneread";
                   $group_perm[1][1] = "$lang_geveryonewrite";
                   $group_perm[2][1] = "$lang_ggroupread";
                   $group_perm[3][1] = "$lang_ggroupwrite";
                   $group_perm[4][1] = "$lang_gonlyyou";
                   $group_perm[5][1] = "$lang_ggroupwrite_nod";
                   $group_perm[6][1] = "$lang_geveryonewrite_nod";
                   $group_perm[7][1] = "$lang_ggroupwrite_worldread";
                   $group_perm[8][1] = "$lang_ggroupwrite_worldread_nod";
	}

          print("<TR><TD ALIGN=RIGHT BGCOLOR=$default->table_header_bg>$printmessage</TD><TD align=left><SELECT NAME=$namevariable>");
                foreach($group_perm as $fp) {
                        print("<OPTION VALUE=$fp[0] ");
                        if($fp[0] == $currentval)
                                print("SELECTED");
                        print(">$fp[1]");
                }
                           print("</SELECT></TD></TR>");

};


if (!$sess && !$loginname && !$login) 
	if(!isset($fileid))
		header("Location: " . $default->owl_root_url . "/index.php?login=1");
	else
		header("Location: " . $default->owl_root_url . "/index.php?login=1&fileid=$fileid&parent=$parent");

?>