레이블이 php인 게시물을 표시합니다. 모든 게시물 표시
레이블이 php인 게시물을 표시합니다. 모든 게시물 표시

2016년 11월 24일 목요일

php 로 Active Directory 의 lastLogon 구하기

php로 Active Directory 인증/조회/수정을 할 수 있도록  adLDAP class 를 사용하고 있다.



lastLogon 특성을 매 분마다 수집하여 사용하고자 한다.

그런데 문제는 AD 서버가 2대인데 사용자 인증에 사용된 서버에서만 lastLogon 특성 값이 업데이트가 된다.

그래서 양쪽 서버에서 조회한 값을 비교해서 합쳐주도록 했다.
$options1 = array("domain_controllers" => array("ad1st"));
$options2 = array("domain_controllers" => array("ad2nd"));

$adldap1 = new adLDAP($options1);
$adldap2 = new adLDAP($options2);

$users = $adldap1->user()->all(true,"*", true);
$lastLogons = array();
foreach($users as $id => $name ) {
    $info1 = $adldap1->user()->infoCollection($id, array("*"));
    $mktime1 = round(convertWindowsTimestamp($info1->lastLogon));

    $info2 = $adldap2->user()->infoCollection($id, array("*"));
    $mktime2 = round(convertWindowsTimestamp($info2->lastLogon));

    if( $mktime1 >  $mktime2 ) {
        $mktime = $mktime1;
        $auth = "ad1st";
    } else if( $mktime1 < $mktime2 ) {
        $mktime = $mktime2;
        $auth = "ad2nd";
    } else {
        $mktime = $mktime1;
        $auth = "ad1st";
    }

    $lastLogon = date("YmdHis", $mktime);
    $lastLogons["$id"] = array("auth" => $auth, "lastlogon" => $lastLogon);
}

전체 사용자 정보를 담은 인스턴스를 생성 후 각 사용자 마다 lastLogon 정보를 찾아서 비교 후에 다시 배열로 저장했다.

그런데 lastLogon 의 값은 linux 의 mktime() 값과 다르기에 convertWindowsTimestamp()를 사용하였다.

function convertWindowsTimestamp($wintime) {
   return $wintime / 10000000 - 11644473600;
}


2016년 4월 25일 월요일

MAC OS X + XAMPP

MAC 에 XAMPP 설치를 해보자.

XAMPP 란? Apache + MariaDB + php + perl 을 포함한 PHP 개발 환경을 제공하는 오픈 소스 패키지다.

설치 파일은 아래 링크에서

https://www.apachefriends.org/index.html

설치는 패키지 파일을 더블 클릭 > next 클릭으로 완료된다.

manager-osx 를 실행해보면 Apache Web Server 는 실행 중이다.

MySQL Database Server 를 시작 후 터미널을 열어 root 암호를 설정해야 한다.

초기 설치 시 default root 암호는 설정 되어 있지 않으니 필히 설정하도록 하자.

/Applications/XAMPP/bin/mysqladmin --user=root password "newpass"

이후 phpMyAdmin 을 접속해보자.

주소창에 http://localhost 입력 후 우측 상단의 phpMyAdmin 을 클릭하면....

어라? 접근이 거부된다!는 당연한 것이.. phpmyadmin 설정을 변경해야한다.

/Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php
..
$cfg['Servers'][$i]['password'] = 'newpass';

에 위에서 변경한 암호를 입력해야 한다.

이후 http://localhost/phpmyadmin 에 접속해보면 바로 접속이 되는 것을 확인 할 수 있다.

그런데... config.inc.php 파일에 계정 정보를 기록하는것이 좀 그렇다.

그럴경우에는 auth_type 을 http 로 변경해 주자. 물론 http 로 변경 할 경우 user 와 password 는 공백으로 수정해야 한다.

$cfg['Servers'][$i]['auth_type'] = 'http';

auth_type 과 관련한 내용은 아래 링크를 참고하자.

https://wiki.phpmyadmin.net/pma/Auth_types




2015년 12월 3일 목요일

andorid <-> php AES256 en/decode

출처 : http://www.imcore.net/encrypt-decrypt-aes256-c-objective-ios-iphone-ipad-php-java-android-perl-javascript-python/

android AES256Util class

public class AES256Util {
    public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    public static String AES_Encode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

        byte[] textBytes = str.getBytes("UTF-8");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);

        return Base64.encodeToString(cipher.doFinal(textBytes), 0);
    }

    public static String AES_Decode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

        byte[] textBytes =Base64.decode(str,0);
        //byte[] textBytes = str.getBytes("UTF-8");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
        return new String(cipher.doFinal(textBytes), "UTF-8");
    }
}

php function
$AES256KEY = "12345678123456781234567812345678";

function AES_Encode($plain_text) {
    global $AES256KEY;
    return base64_encode(openssl_encrypt($plain_text, "AES-256-CBC", $AES256KEY, true, str_repeat(chr(0), 16)));

}

function AES_Decode($base64_text) {
    global $AES256KEY;
    return openssl_decrypt(base64_decode($base64_text), "AES-256-CBC", $AES256KEY, true, str_repeat(chr(0), 16));
}

webview 로 데이터 전달
try {
    encoeText = Uri.encode(AES256Util.AES_Encode(palinText, aes256key));
} catch (Exception e) {}

String postdata = "enText="+encodeText;
webView.postUrl(URL, postdata.getBytes());

webview 로 전달된 데이터가 복호화가 안된다면 전달하는 값에 +가 포함되어 그럴수도 있더라... 그래서 Uri.encode() 로 전달했다.