Location: PHPKode > projects > RepMan > repman/inc/functions.php
<?
  require ('inc/config.php');

  function auth_query() {
    @header('WWW-Authenticate: Basic realm="My Realm"');
    @header('HTTP/1.0 401 Unauthorized');
    return;
  } 

  function authorize(){
    if (!isset($_SERVER['PHP_AUTH_USER'])&&$_SERVER['PHP_AUTH_USER']!=$GLOBALS["access_user"]){
      auth_query();
    }else{
      echo "authorized.<br>\n";
      session_start();
      $_SESSION["log"]=1;
    }
  }  

  function ConnectIt() {
    @$pripojeni=MySQL_Connect($GLOBALS["db_kam"],$GLOBALS["db_kdo"],$GLOBALS["db_pas"]); // zkusi se pripojit k DB : kam, kdo, pass
    if(!$pripojeni){      // kdyz se nepripoji zobrazi hlasku...
      logerr("Nepodarilo se pripojit k databazi");
      break;    // ...a zastavi cinnost
    }else{
      logdebug("Pripojen na '".$GLOBALS["db_kam"]."'!");
      Query("SET NAMES 'utf8'");
      @$vyber=MySQL_Select_DB($GLOBALS["db_nazev"]);  // vybere DB
      if ($vyber) {
        logdebug("DB '".$GLOBALS["db_nazev"]."' byla vybrana...");;
      } else {
        logerr("Pri vyberu DB '".$GLOBALS["db_nazev"]."' nastal problem!");
      };
    };    
    return;
  }

  function ELevel($errnum) {
    switch ($errnum) {
      case '-1':
        return "<span class=\"e-1\">&nbsp;N/A&nbsp;</span>";
        break;
      case '0':
        return "<span class=\"e0\">&nbsp;OK&nbsp;</span>";
        break;
      default:    	
        return "<span class=\"e1\">&nbsp;Error(".$errnum.")&nbsp;</span>";
    }
    
  }
  
  function isLoggedIn() {
    if (isset($_SESSION["log"])&&($_SESSION["log"]==1)) {
      return 1;
    } else {
      return 0;
    }
  }
  
  function ifAsUser() {
    if (isset($_SESSION["username"])&&$_SESSION["username"]!="") {
      return $_SESSION["username"];
    } else {
      return 0;
    }
  }
  
  function logdebug($logthis) {
    if ($GLOBALS["mode_debug"]) {
      $f = $GLOBALS["log_debug"];
      if(!$file=fopen($f, "a")) {
        echo "Cann't open file '$f'";
      } else {
      if (filesize($f)>=10737418240) {  // kdyz bude mit soubor 10MB
        fclose($file);
        $apdx = date("Ymd_His");
        rename($f, $f."_".$apdx);       // bude prejmenovan na _yymmdd_hhmmss
        if ($file=fopen($f,"a")) {
          echo $f." was renamed to '".$f."_".$apdx."' and was created new.";
          if (chmod($f, 0777)) { echo "$f changed mod to 0777."; }    // opravneni vse pro vsechny 
        } else {
          echo "Failed to create new '$f'!";
        }  
       }
      }
      
      if(!fputs($file,date(DATE_RFC822)."\t".$logthis."\n")) { echo "Nepodarilo se mi to ulozit do souboru '$f'"; };
      fclose($file);
    }
  }

  function logerr($logthis) {
    myfile($GLOBALS["log_errfile"],date(DATE_RFC822)."\t".$logthis."\n");    
  }

  function loginput($logthis) {
    myfile($GLOBALS["log_inputfile"],date(DATE_RFC822)."\t".$logthis."\n");    
  }

  function Login($name,$pass,$logindb = 0) {   // 0 - autentifikace v config.php, 1 - autentifikace v db (nepouzito)
    if ($logindb==0) {
      if ($name == "config") {
        $name = $GLOBALS["access_user"];
      }
      if (($name == $GLOBALS["access_user"])&&(md5($pass) == $GLOBALS["access_pass"])) {  
        $_SESSION["logname"]=$name;
        $_SESSION["log"]=1;
        return 1;
      } else {
        return 0;
      } 
    }
  }

  function Logout() {
    $_SESSION["log"]=0;
    $_SESSION["logname"]="";
    return;
  } 
  
  function myfile($f,$wstring) {  // muj soubor pro zapsani $f nazev souboru, $wstring retezec k zapisu
    if(!$file=fopen($f, "a")) {
      echo "Cann't open file '$f'";
    } else {
     logdebug("uspesne otevreni souboru '$f'");
     if (filesize($f)>=10737418240) {   // kdyz bude mit soubor 10MB
      fclose($file);
      $apdx = date("Ymd_His");
      rename($f, $f."_".$apdx);       // bude prejmenovan na _yymmdd_hhmmss
      if ($file=fopen($f,"a")) {
        logdebug($f." was renamed to '".$f."_".$apdx."' and was created new.");
        if (chmod($f, 0777)) {logdebug("$f changed mod to 0777.");}     // opravneni vse pro vsechny
      } else {
        logdebug("Failed to create new '$f'!");
      }  
     }
    }
    
    if(!fputs($file,$wstring."\n")) { echo "Unsuccessfull save to file '$f'"; } else { logdebug("zapsano do souboru '$f'"); };
    fclose($file);
  } /* date(DATE_RFC822) */

  function Ordinal($number) {
    if ($number==0) {
      return $number;
    } else {
      $ord = substr($number, -1);
      $suffix = $GLOBALS["ordinal_suffix"];
      switch ($ord) {
        case 1:
          return $number.$suffix[$ord];
        	break;
        case 2:
          return $number.$suffix[$ord];
        	break;
        case 3:
          return $number.$suffix[$ord];
        	break;      	
        default:
          return $number.$suffix[0];
      	break;
      }
    }
  }

  function PHPname() {
    $cesta = split("/", $_SERVER["PHP_SELF"]);
    return $cesta[count($cesta)-1];    
  }

  function Query($query) {
   $sql=MySQL_Query($query);
   if(substr($query,0,6)=="SELECT"){
     while($sql2=MySQL_Fetch_Array($sql)) {$sql3[] = $sql2;}
     }else{$sql3=$sql;}
   return $sql3;
  };

  function say_boolean($boolval) {
    if ($boolval == 0) {
      return "<span class=\"boolfalse\">False&nbsp;</span>";
    } else {
      return "<span class=\"booltrue\">True&nbsp;&nbsp;</span>";
    }
  }

  function sayifadmin($say,$charcount) {
    if (isset($_SESSION["log"])&&$_SESSION["log"]==1) {
      return $say;
    } else {
      return str_repeat("&nbsp;", $charcount);
    }
  }

?>

Return current item: RepMan