rocketchat.class.php
<?php
class RCManager {
// 기본접속 정보
public $server;
public $rc_id;
public $rc_pw;
// 통신에 따라 생성되는 변수
protected $status;
protected $userId;
protected $authToken;
// 클래스 생성
public function __construct($server, $rc_id, $rc_pw) {
$this->server = $server;
$this->rc_id = $rc_id;
$this->rc_pw = $rc_pw;
$this->connect();
}
public function __destruct() {}
// 서버 접속
public function connect() {
$Result = $this->Curl($this->server.'/api/v1/info', '', '', 'GET');
$Result = json_decode($Result, true);
//echo $this->print_r($Result);
if(empty($Result['success'])) $this->status = '[server] 서버에 접속할 수 없습니다.';
else if($Result['success'] == 1) $this->status = 'success';
else $this->status = '[server] 서버에 접속할 수 없습니다.';
if($this->status == 'success') {
$this->login();
}
}
// 사용자 계정 로그인
protected function login() {
if($this->status != 'success') return;
$Result = $this->Curl($this->server.'/api/v1/login', array('user'=>$this->rc_id, 'password'=>$this->rc_pw), array('Content-Type: application/x-www-form-urlencoded'));
//echo $this->print_r($Result);
$Result = json_decode($Result, true);
if($Result['status'] == 'success') {
$this->authToken = $Result['data']['authToken'];
$this->userId = $Result['data']['userId'];
return;
}
else {
$this->status = '[server] 사용자 계정이 잘못되었습니다.';
return;
}
}
//전체 사용자 정보 조회
public function UserList() {
if($this->status != 'success') return;
$Result = $this->Curl($this->server.'/api/v1/users.list', '', array('X-Auth-Token:'.$this->authToken, 'X-User-Id:'.$this->userId), 'GET');
$Result = json_decode($Result, true);
//echo $this->print_r($Result);
return $Result;
}
//사용자 정보 조회
public function GetUserId($user) {
if($this->status != 'success') return;
$Data = array();
$Data['user'] = strtolower($user);
$Result = $this->Curl($this->server.'/api/v1/users.info', $Data, array('X-Auth-Token:'.$this->authToken, 'X-User-Id:'.$this->userId), 'GET');
$Result = json_decode($Result, true);
return $Result['user']['_id'];
}
//사용자 Disabled
public function UserStatusModify($user, $new_status) {
if($this->status != 'success') return;
if($new_status){
$Data = '{"userId":"'.$user.'", "activeStatus":true}';
}else{
$Data = '{"userId":"'.$user.'", "activeStatus":false}';
}
$Result = $this->Curl($this->server.'/api/v1/users.setActiveStatus', $Data, array('X-Auth-Token:'.$this->authToken, 'X-User-Id:'.$this->userId, 'Content-type:application/json'), 'POST', 10, 'json');
$Result = json_decode($Result, true);
if(empty($Result['success'])) return false;
else if($Result['success']) return true;
else return false;
}
// CURL
public function Curl($url, $data=array(), $header=array(), $method='POST', $timeout=10, $type='') {
global $PROXY_USE;
$parts = parse_url($url);
$method = strtoupper($method);
if(in_array($method, array('POST', 'GET')) === false) $method = 'POST'; // method 보정
if(empty($parts['host'])) {
$this->status = '[Curl] 호스트를 찾을 수 없습니다.';
return;
}
if($method == 'GET' && is_array($data) === true && count($data) > 0) {
$build = http_build_query($data);
$url .= (empty($parts['query'])?'?'.$build:'&'.$build);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($parts['scheme'] == "https") {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
if(is_array($header) === true && count($header) > 0) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, ($method == 'POST'?1:0));
if($method == 'POST' && count($data) > 0){
if($type=='json'){
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
}
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if($PROXY_USE){
curl_setopt($ch, CURLOPT_PROXY, PROXY_URL);
curl_setopt($ch, CURLOPT_PROXYPORT, PROXY_PORT);
}
$rt = curl_exec($ch); // 실행
curl_close($ch);
$this->status = 'success';
return $rt;
}
// xmp print_r
public function print_r($arr = array()) {
echo '<xmp>';
print_r($arr);
echo '</xmp>';
}
}
rocketchat.php
/*===========================================================
로켓챗 사용자 Disabled START
===========================================================*/
function rocketchat_Disabled($uid){
include_once '../lib/rocketchat.class.php';
//유효성 검사
if($uid == "") return 'Parameter null';
$rcm = new RCManager('서버주소', 'admin ID', 'admin PWD');
//사용자 _id 정보 조회
$user_id = $rcm->GetUserId(strtolower($uid));
log_write2($user_id);
//사용자 Disabled
$edit_status = $rcm->UserStatusModify($user_id, false);
log_write2($edit_status);
if($edit_status){
return 'Disabled 처리';
}else{
return 'Disabled 처리 실패';
}
}
/*===========================================================
로켓챗 사용자 Disabled END
===========================================================*/
rocketchat api document
docs.rocket.chat/developer-guides/rest-api
'[ Web 관련 ] > php' 카테고리의 다른 글
변수 타입 확인하기 (0) | 2020.07.03 |
---|---|
excel upload 샘플 (0) | 2020.05.21 |
php + python 코드 실행 (0) | 2020.03.20 |
php curl에서 proxy 사용 + 이미지를 Base64 변환 (0) | 2020.03.19 |
php 날짜 더하기 (날짜 계산) (0) | 2020.02.14 |