<?php

	function fo_um_open_channel() {
		$s = fsockopen( "tcp://localhost" , 23000 );
		if (!$s)
			return false;
		$GLOBALS["user_management_channel"]=$s;
		return $s;
	}

	function fo_um_close_channel() {
		if (!isset( $GLOBALS["user_management_channel"] ))
			return;
		$s = $GLOBALS["user_management_channel"];
		unset( $GLOBALS["user_management_channel"] );
		fwrite( $s , "QUIT\n" );
		fclose( $s );
	}

	function fo_um_request( $req ) {
		if (isset( $GLOBALS["user_management_channel"] )) {
			$s = $GLOBALS["user_management_channel"];
		} else {
			$s = fsockopen( "tcp://localhost" , 23000 );
		}
		if (!$s) return false;
		$command = sprintf( "%s\n" , $req );
		fwrite( $s , $command );
		$retval = "";
		// read all
		while (!feof( $s )) {
			// read row
			$row = fgets( $s );
			$retval.=$row;
			if ($row=="DONE\n") break;
			if (substr( $row , 0 , 6 )=="ERROR ") break;
		}
		if (!isset( $GLOBALS["user_management_channel"] )) {
			fwrite( $s , "QUIT\n" );
			fclose( $s );
		}
		return $retval;
	}

	function fo_um_get_user_exists( $name , $onfailure = true ) {
		$req = sprintf( "QUERY %s" , $name );
		$response = fo_um_request( $req );
		if ($response===false) return $onfailure;
		$rows = explode( "\n" , $response );
		foreach ($rows as $row) {
			$m = explode( " " , $row );
			if ($m[0]!="LINUX-UID") continue;
			if ($m[1]=="NODATA") return false;
			return true;
		}
		return false;
	}


	function fo_um_get_user_mailbox_exists( $name , $onfailure = false ) {
		$req = sprintf( "QUERY %s" , $name );
		$response = fo_um_request( $req );
		if ($response===false) return $onfailure;
		$rows = explode( "\n" , $response );
		foreach ($rows as $row) {
			$m = explode( " " , $row );
			if ($m[0]!="MAILBOX") continue;
			if ($m[1]=="NODATA") return false;
			return true;
		}
		return false;
	}
	
	function fo_um_get_user_mailrecord_exists( $name , $onfailure = false ) {
		$req = sprintf( "QUERY %s" , $name );
		$response = fo_um_request( $req );
		if ($response===false) return $onfailure;
		$rows = explode( "\n" , $response );
		foreach ($rows as $row) {
			$m = explode( " " , $row );
			if ($m[0]!="MAILRECORD") continue;
			if ($m[1]=="NODATA") return false;
			return true;
		}
		return false;
	}

	function fo_um_add_user_unix( $name ) {
		/* tested */
		$req = sprintf( "ADD-LINUX %s" , $name );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="ERROR") return false;
		return true;
	}

	function fo_um_del_user_unix( $name ) {
		/* tested */
		$req = sprintf( "DEL-LINUX %s" , $name );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="DONE") return true;
		return false;
	}
	
	function fo_um_add_user_mailbox( $name ) {
		/* tested */
		$req = sprintf( "ADD-MAILBOX %s" , $name );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="DONE") return true;
		return false;
	}
	
	function fo_um_del_user_mailbox( $name ) {
		/* tested */
		$req = sprintf( "DEL-MAILBOX %s" , $name );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="DONE") return true;
		return false;
	}

	function fo_um_add_user_mailrecord( $name ) {
		/* tested */
		$req = sprintf( "ADD-MAILRECORD %s %s@fordit.hu" , $name , $name );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="DONE") return true;
		return false;
	}

	function fo_um_del_user_mailrecord( $name ) {
		/* tested */
		$req = sprintf( "DEL-MAILRECORD %s %s@fordit.hu" , $name , $name );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="DONE") return true;
		return false;
	}

	function fo_um_change_user_password( $name , $pwd ) {
		$req = sprintf( "PASSWORD %s %s" , $name , $pwd );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="DONE") return true;
		return false;
	}
	
	function fo_um_query_all_unix( ) {
		$retval = array();
		$req = sprintf( "QUERY-ALL LINUX" );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		foreach ($rows as $row) {
			$user = explode( " " , $row );
			if ($user[0]!="USER") continue;
			$retval[$user[1]]=$user[1];
		}
		return $retval;
	}

	function fo_um_query_all_mailbox() {
		$retval = array();
		$req = sprintf( "QUERY-ALL MAILBOX" );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		foreach ($rows as $row) {
			$user = explode( " " , $row );
			if ($user[0]!="USER") continue;
			$retval[$user[1]]=$user[1];
		}
		return $retval;
	}
	function fo_um_query_all_mailrecord() {
		$retval = array();
		$req = sprintf( "QUERY-ALL MAILRECORD" );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		foreach ($rows as $row) {
			$user = explode( " " , $row );
			if ($user[0]!="USER") continue;
			$retval[$user[1]]=$user[1];
		}
		return $retval;
	}

	function fo_um_query_all_homedirectories() {
		$retval = array();
		$req = sprintf( "QUERY-ALL HOMEDIR-RIGHTS" );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		foreach ($rows as $row) {
			$user = explode( " " , $row );
			if ($user[0]!="USER") continue;
			$val = (isset($user[2])) ? $user[2] : false;
			$retval[$user[1]]=$val;
		}
		return $retval;
	}

	function fo_um_del_homedir( $username ) {
		/* tested */
		$req = sprintf( "DEL-HOMEDIR %s" , $username );
		$response = fo_um_request( $req );
		$rows = explode( "\n" , $response );
		$len = count( $rows );
		$m = explode( " " , $rows[0] );
		if ($m[0]=="DONE") return true;
		return false;

	}

?>
