Update activitypub.extend.php

This commit is contained in:
Namhyeon Go 2022-07-01 18:07:44 +09:00 committed by GitHub
parent 84f02933e5
commit 013c489b6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,7 @@ define("ACTIVITYPUB_DATA_URL", ACTIVITYPUB_URL . '/' . G5_DATA_DIR);
define("ACTIVITYPUB_G5_BOARDNAME", "apstreams"); define("ACTIVITYPUB_G5_BOARDNAME", "apstreams");
define("ACTIVITYPUB_G5_TABLENAME", G5_TABLE_PREFIX . ACTIVITYPUB_G5_BOARDNAME); define("ACTIVITYPUB_G5_TABLENAME", G5_TABLE_PREFIX . ACTIVITYPUB_G5_BOARDNAME);
define("ACTIVITYPUB_G5_USERNAME", "apstreams"); define("ACTIVITYPUB_G5_USERNAME", "apstreams");
define("ACTIVITYPUB_NEW_DAYS", (empty($config['cf_new_del']) ? 30 : $config['cf_new_del']));
define("NAMESPACE_ACTIVITYSTREAMS", "https://www.w3.org/ns/activitystreams"); define("NAMESPACE_ACTIVITYSTREAMS", "https://www.w3.org/ns/activitystreams");
define("NAMESPACE_ACTIVITYSTREAMS_PUBLIC", "https://www.w3.org/ns/activitystreams#Public"); define("NAMESPACE_ACTIVITYSTREAMS_PUBLIC", "https://www.w3.org/ns/activitystreams#Public");
@ -298,7 +299,7 @@ function activitypub_parse_content($content) {
return $entities; return $entities;
} }
function activitypub_add_post($data) { function activitypub_add_post($inbox = "inbox", $data, $mb) {
$wr_id = 0; $wr_id = 0;
// 기본 파라미터 // 기본 파라미터
@ -315,7 +316,7 @@ function activitypub_add_post($data) {
$write_table = ACTIVITYPUB_G5_TABLENAME; $write_table = ACTIVITYPUB_G5_TABLENAME;
$wr_num = get_next_num($write_table); $wr_num = get_next_num($write_table);
$wr_reply = ''; $wr_reply = '';
$ca_name = NAMESPACE_ACTIVITYSTREAMS; $ca_name = $inbox; // Inbox/Outbox
$wr_subject = mb_substr($content, 0, 50); $wr_subject = mb_substr($content, 0, 50);
$wr_seo_title = $content; $wr_seo_title = $content;
$wr_content = activitypub_json_encode($data); // Activity (Full Context) $wr_content = activitypub_json_encode($data); // Activity (Full Context)
@ -324,6 +325,21 @@ function activitypub_add_post($data) {
$wr_homepage = $data['actor']; $wr_homepage = $data['actor'];
$wr_6 = $data['type']; // Type of Activity $wr_6 = $data['type']; // Type of Activity
// 수신자 확인
$receivers = array();
foreach($to as $_to) {
// 수신자 주소(URL) 처리
$url_ctx = activitypub_parse_url($_to);
$host = $url_ctx['host'];
$query = $url_ctx['query'];
// 특정 회원이 지목되어 있다면 수신자 추가
if ($host == ACTIVITYPUB_HOST && !empty($query['mb_id'])) {
array_push($receivers, $query['mb_id']);
}
}
$wr_7 = implode($receivers);
$sql = " $sql = "
insert into $write_table insert into $write_table
set wr_num = '$wr_num', set wr_num = '$wr_num',
@ -355,7 +371,7 @@ function activitypub_add_post($data) {
wr_4 = '', wr_4 = '',
wr_5 = '', wr_5 = '',
wr_6 = '$wr_6', wr_6 = '$wr_6',
wr_7 = '', wr_7 = '$wr_7',
wr_8 = '', wr_8 = '',
wr_9 = '', wr_9 = '',
wr_10 = '' wr_10 = ''
@ -367,6 +383,34 @@ function activitypub_add_post($data) {
return $wr_id; return $wr_id;
} }
function activitypub_get_posts($mb_id = '', $inbox = "inbox") {
$posts = array();
// 정보 불러오기
$sql = "";
if(empty($mb_id)) {
$sql = "select wr_content from " . ACTIVITYPUB_G5_TABLENAME . "
where ca_name = '$inbox'
and wr_datetime BETWEEN CURDATE() - INTERVAL " . ACTIVITYPUB_G5_NEW_DAYS . " DAY AND CURDATE()
";
} else {
$sql = "select wr_content from " . ACTIVITYPUB_G5_TABLENAME . "
where ca_name = '$inbox'
and FIND_IN_SET('$mb_id', wr_7) > 0
and wr_datetime BETWEEN CURDATE() - INTERVAL " . ACTIVITYPUB_G5_NEW_DAYS . " DAY AND CURDATE()
";
$result = sql_query($sql);
}
$result = sql_query($sql);
// 정보 조회 후 처리
while ($row = sql_fetch_array($result)) {
array_push($posts, json_decode($row['wr_content'], true));
}
return $$posts;
}
class _GNUBOARD_ActivityPub { class _GNUBOARD_ActivityPub {
public static function open() { public static function open() {
header("Content-Type: application/ld+json; profile=\"" . NAMESPACE_ACTIVITYSTREAMS . "\""); header("Content-Type: application/ld+json; profile=\"" . NAMESPACE_ACTIVITYSTREAMS . "\"");
@ -501,11 +545,11 @@ class _GNUBOARD_ActivityPub {
} }
public static function inbox() { public static function inbox() {
global $g5; // HTTP 요청 유형에 따라 작업
switch ($_SERVER['REQUEST_METHOD']) {
case "POST":
// 개인에게 보낸 메시지는 쪽지에 저장 // 개인에게 보낸 메시지는 쪽지에 저장
// 공개(Public) 설정한 메시지는 ACTIVITYPUB_G5_TABLENAME에 저장 // 공개(Public) 설정한 메시지는 ACTIVITYPUB_G5_TABLENAME에 저장
$data = json_decode(file_get_contents("php://input"), true); $data = json_decode(file_get_contents("php://input"), true);
if (empty($data['@context'])) { if (empty($data['@context'])) {
@ -542,7 +586,7 @@ class _GNUBOARD_ActivityPub {
$object['content'] = "[NO CONTENT]"; $object['content'] = "[NO CONTENT]";
// 수신된 내용 등록 // 수신된 내용 등록
$activity_wr_id = activitypub_add_post($data); $activity_wr_id = activitypub_add_post("inbox", $data, $mb);
// 답글인지 확인 // 답글인지 확인
if (!empty($object['inReplyTo'])) { if (!empty($object['inReplyTo'])) {
@ -730,27 +774,39 @@ class _GNUBOARD_ActivityPub {
} }
} }
} }
else {
return activitypub_json_encode(array("message" => "Type could not be an empty"));
}
return activitypub_json_encode(array("message" => "Success"));
case "GET":
return activitypub_json_encode(activitypub_get_posts("inbox", $_GET['mb_id']));
default:
return activitypub_json_encode(array("message" => "Not supported method"));
}
} }
public static function outbox() { public static function outbox() {
// TODO // HTTP 요청 유형에 따라 작업
switch ($_SERVER['REQUEST_METHOD']) {
case "POST":
return activitypub_json_encode(array("message" => "Not implemented"));
case "GET":
return activitypub_json_encode(activitypub_get_posts("outbox", $_GET['mb_id']));
}
} }
public static function followers() { public static function followers() {
$params = array( $mb = get_member($_GET['mb_id']);
"mb_id" => $_GET['mb_id']
);
$mb = get_member($params['mb_id']);
return activitypub_json_encode(array("followers" => activitypub_get_followers($mb))); return activitypub_json_encode(array("followers" => activitypub_get_followers($mb)));
} }
public static function following() { public static function following() {
$params = array( $mb = get_member($_GET['mb_id']);
"mb_id" => $_GET['mb_id']
);
$mb = get_member($params['mb_id']);
return activitypub_json_encode(array("following" => activitypub_get_following($mb))); return activitypub_json_encode(array("following" => activitypub_get_following($mb)));
} }