PluginProbe
codoc / 0.9.61
codoc v0.9.61
0.9.61 0.9.8.8 0.9.8.9 0.9.9 0.9.9.1 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.8.1 0.8.2 0.8.3 0.8.4 0.8.6 0.8.7 0.8.8 0.8.9 0.9 0.9.1 0.9.10 All 115 releases
codoc / class-codoc-util.php

class-codoc-util.php in codoc 0.9.61, at class-codoc-util.php

567 lines 24.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WPINC')) {
4 define( 'WPINC', 'wp-includes' );
5 }
6
7 if (!defined('CODOC_URL')) {
8 define( 'CODOC_URL', 'https://codoc.jp' );
9 }
10 global $wp_version;
11 if (version_compare( $wp_version,'5.9') >= 0 ) {
12 require_once( ABSPATH . WPINC . '/class-wp-http.php');
13 } else {
14 require_once( ABSPATH . WPINC . '/class-http.php');
15 }
16
17 require_once( ABSPATH . WPINC . '/class-wp-error.php');
18
19 final class CodocUtil {
20 public $codoc_url = CODOC_URL;
21 public function __construct($params = ['usercode' => null, 'token' => null,'codoc_url' => null]) {
22 $this->setAuthInfo(['usercode' => $params['usercode'], 'token' => $params['token']]);
23 if (isset($params['codoc_url'])) {
24 $this->codoc_url = $params['codoc_url'];
25 }
26 return $this;
27 }
28
29 public function callAPI($method,$path,$body = [],$headers = []) {
30 $usercode = $this->usercode;
31 $token = $this->token;
32
33 $headers['X-CodocToken'] = $token;
34 $host = $this->codoc_url;
35 $sslverify = true;
36
37 if (preg_match('/local/',$this->codoc_url)) {
38 $sslverify = false;
39 $host = 'https://host.docker.internal';
40 }
41 $url = sprintf("%s/api/v1/cms/%s%s",$host,$usercode,$path);
42 $http = new WP_Http();
43 try {
44 $response = $http->request(
45 $url,
46 [
47 'sslverify' => $sslverify,
48 'method' => $method,
49 'timeout' => 10,
50 'headers' => $headers,
51 'body' => $body,
52 ]
53 );
54 // notification を処理
55 $body = json_decode($response['body'],true);
56 if (isset($body['notification']) && !empty($body['notification'])) {
57 // notification をトランジェントに保存(24時間保持)
58 set_transient('codoc_api_notification', $body['notification'], DAY_IN_SECONDS);
59 }
60 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
61 //何もしない
62 //var_dump( $response );
63 }
64 } catch(Exception $e) {
65
66 }
67 if (!is_wp_error($response) and
68 isset($response['body']) and
69 is_string($response['body']) and
70 is_array(json_decode($response['body'], true)) and
71 (json_last_error() == JSON_ERROR_NONE)) {
72 return json_decode($response['body']);
73 } else {
74 return null;
75 }
76 }
77 public function callPaywallAPI($method,$path,$body = [],$headers = []) {
78 $site_usercode = get_option(CODOC_USERCODE_OPTION_NAME);
79 $paywall_token = $this->get_paywall_token_code();
80
81 $host = $this->codoc_url;
82 $sslverify = true;
83
84 if (preg_match('/local/',$this->codoc_url)) {
85 $sslverify = false;
86 $host = 'https://host.docker.internal';
87 }
88 $url = sprintf("%s/api/v1/paywall/%s%s",$host,$site_usercode,$path);
89 $http = new WP_Http();
90 try {
91 $response = $http->request(
92 $url,
93 [
94 'sslverify' => $sslverify,
95 'method' => $method,
96 'timeout' => 10,
97 'headers' => $headers,
98 'body' => $body,
99 ]
100 );
101 if ( is_wp_error($response) || $response['response']['code'] != 200 ) {
102 //何もしない
103 //var_dump( $response );
104 }
105 } catch(Exception $e) {
106
107 }
108 if (!is_wp_error($response) and
109 isset($response['body']) and
110 is_string($response['body']) and
111 is_array(json_decode($response['body'], true)) and
112 (json_last_error() == JSON_ERROR_NONE)) {
113 return json_decode($response['body']);
114 } else {
115 return null;
116 }
117 }
118 public function setAuthInfo($params = [ "usercode" => null, "token" => null ]) {
119 if ($params["usercode"]) {
120 $this->usercode = $params["usercode"];
121 }
122 if ($params["token"]) {
123 $this->token = $params["token"];
124 }
125 return true;
126 }
127 public function get_token($params = ["fetch_token_key" => null],$auth_info = ["usercode" => null, "token" => null]) {
128 $this->setAuthInfo($auth_info);
129 return $this->callAPI('GET','/token',[ "fetch_token_key" => $params["fetch_token_key"] ]);
130 }
131 public function get_user_info($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
132 $this->setAuthInfo($auth_info);
133 return $this->callAPI('GET','');
134 }
135 public function get_support_entry($params = [] ,$auth_info = ["usercode" => null, "token" => null]) {
136 $this->setAuthInfo($auth_info);
137 return $this->callAPI('GET','/support_entry');
138 }
139 // API側と統一した文字数上限のバリデーション。エラーメッセージの�
140 �列を返す (エラー無しなら空�
141 �列)
142 public function validate_entry_lengths($api_params) {
143 $errors = [];
144 $title = isset($api_params['title']) ? $api_params['title'] : '';
145 $body_free = isset($api_params['body_free']) ? $api_params['body_free'] : '';
146 $body_paywalled = isset($api_params['body_paywalled']) ? $api_params['body_paywalled'] : '';
147 $binded_url = isset($api_params['binded_url']) ? $api_params['binded_url'] : '';
148
149 if (mb_strlen($title) > CODOC_MAX_TITLE_LENGTH) {
150 // Translators: %1$d is the max length, %2$d is the current length.
151 $errors[] = sprintf(__('Title exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_TITLE_LENGTH, mb_strlen($title));
152 }
153 $len_body_free = mb_strlen($body_free);
154 $len_body_paywalled = mb_strlen($body_paywalled);
155 if ($len_body_free > CODOC_MAX_BODY_FREE_LENGTH) {
156 $errors[] = sprintf(__('Free area exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_FREE_LENGTH, $len_body_free);
157 }
158 if ($len_body_paywalled > CODOC_MAX_BODY_PAYWALLED_LENGTH) {
159 $errors[] = sprintf(__('Paid area exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_PAYWALLED_LENGTH, $len_body_paywalled);
160 }
161 $len_body_total = $len_body_free + $len_body_paywalled;
162 if ($len_body_total > CODOC_MAX_BODY_LENGTH) {
163 $errors[] = sprintf(__('Total body exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BODY_LENGTH, $len_body_total);
164 }
165 if (mb_strlen($binded_url) > CODOC_MAX_BINDED_URL_LENGTH) {
166 $errors[] = sprintf(__('URL exceeds the maximum length of %1$d characters (current: %2$d).', 'codoc'), CODOC_MAX_BINDED_URL_LENGTH, mb_strlen($binded_url));
167 }
168 return $errors;
169 }
170
171 public function sync_entry($params = [
172 "post_title" => null,
173 "post_content" => null,
174 "post_status" => null, # 0, 1
175 "post_permalink" => null,
176 "codoc_entry_code" => null,
177 ],$auth_info = ["usercode" => null, "token" => null]) {
178
179 $this->setAuthInfo($auth_info);
180
181 $post_content = $params['post_content'];
182 preg_match('/wp:codoc\/codoc-block +?({.*})/',$post_content,$matches);
183 // GUTENBERG で指定されたjson(記事�
184 報)がない場合
185 if (!$matches) {
186 return;
187 }
188 // GUTENBERG で保存している�
189 容を取得
190 $codoc_info = json_decode($matches[1],true);
191 // codoc タグがない場合はなにもしない
192 //preg_match('/(<span +data-id="codoc-tag"[^>]+>(?:.+|)<\/span>)/',$post_content,$matches);
193 preg_match('/(<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>)/',$post_content,$matches);
194 if (!$matches) {
195 return;
196 }
197 // TODO: split or html parse? HTMLがつながってる場合はおかしくなる
198 // classic(tinymce)版の場合p、もしくはdivにかこまれている
199 $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
200 $before_splited = preg_split($end_tag_regex,$post_content);
201 $post_content = $before_splited[0];
202 $splited = preg_split('/(?:<(?:div|p)(?:[^>]+|)>|)<\!-- +wp:codoc\/codoc-block .*<\!-- +\/wp:codoc\/codoc-block +-->(?:<\/(?:div|p)>|)/s',$post_content);
203 $status = $params['post_status']; # 0: 非�
204 �開 1: �
205 �開 2:限定�
206 �開(パスワードの場合は限定�
207 �開で同期する)
208 // codoc設定で限定�
209 �開を有効で、�
210 �開の場合は 限定�
211 �開にする
212 if ($status == 1 and isset($codoc_info['statusLimited']) and $codoc_info['statusLimited']) {
213 $status = 2;
214 }
215 # $body_free = '';
216 # $body_paywalled = '';
217 # if (count($splited) >= 2) {
218 #
219 # }
220 $binded_url = $params['post_permalink'];
221 $CODOC_SETTINGS = get_option(CODOC_SETTINGS_OPTION_NAME);
222 if (isset($CODOC_SETTINGS['str_replace_binded_url_from']) and
223 isset($CODOC_SETTINGS['str_replace_binded_url_to']) and
224 $CODOC_SETTINGS['str_replace_binded_url_from']) {
225 $binded_url = str_replace(sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_from']),
226 sanitize_text_field($CODOC_SETTINGS['str_replace_binded_url_to']),
227 $binded_url);
228 }
229 $api_params = [
230 'title' => $params['post_title'],
231 'body_free' => $splited[0],
232 'body_paywalled' => $splited[1],
233 'status' => $status,
234 'binded_url' => $binded_url,
235 'show_price' => isset($codoc_info['showPrice']) ? ($codoc_info['showPrice'] ? 1 : 0) : 0,
236 'price' => isset($codoc_info['price']) ? $codoc_info['price'] : 100,
237 'limited' => isset($codoc_info['limited']) ? ($codoc_info['limited'] ? 1 : 0) : 0,
238 'limited_count' => isset($codoc_info['limitedCount']) ? $codoc_info['limitedCount'] : 1,
239 'affiliate_mode' => isset($codoc_info['affiliateMode']) ? ($codoc_info['affiliateMode'] ? 1 : 0) : 0,
240 'affiliate_rate' => isset($codoc_info['affiliateRate']) ? $codoc_info['affiliateRate'] : '0.0500',
241 'show_support' => isset($codoc_info['showSupport']) ? ($codoc_info['showSupport'] ? 1 : 0) : 0,
242 'show_paywalled_support' => isset($codoc_info['showPaywalledSupport']) ? ($codoc_info['showPaywalledSupport'] ? 1 : 0) : 0,
243 'subscriptions' => isset($codoc_info['subscriptions']) ? array_keys($codoc_info['subscriptions']) : [],
244 ];
245 // ゲスト購�
246 �の可否 (未設定 = 旧バージョンのブロックの場合は送信せず本体側の既存値を維持)
247 if (isset($codoc_info['showPayWithoutAccountButton'])) {
248 $api_params['show_pay_without_account_button'] = $codoc_info['showPayWithoutAccountButton'] ? 1 : 0;
249 }
250 // API側と統一した文字数バリデーション
251 $errors = $this->validate_entry_lengths($api_params);
252 if ($errors) {
253 set_transient('codoc_entry_validation_errors', $errors, MINUTE_IN_SECONDS * 5);
254 return null;
255 }
256 $entryCode = $params['codoc_entry_code'];
257 $res = null;
258 if ($entryCode) {
259 $res = $this->callAPI('PUT','/entries/' . $entryCode ,$api_params);
260 } else {
261 $res = $this->callAPI('POST', '/entries', $api_params);
262 }
263 return $res;
264 }
265
266 function post_thumbnail( $params = [
267 "file_path" => null,
268 "boundary" => null,
269 "codoc_entry_code" => null,
270 ], $auth_info = ["usercode" => null, "token" => null] ) {
271
272 $this->setAuthInfo($auth_info);
273
274 $file_path = $params['file_path'];
275 $boundary = $params['boundary']; # ランダム変数24桁 wp_generate_password(24);
276 $res = null;
277 if (is_readable($file_path)) {
278 $name = 'file';
279
280 $payload = '';
281 $payload .= '--' . $boundary;
282 $payload .= "\r\n";
283 $payload .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . basename( $file_path ) . '"' . "\r\n";
284 $payload .= "Content-Type: application/octet-stream\r\n";
285 $payload .= "Content-Transfer-Encoding: binary\r\n";
286 $payload .= "\r\n";
287 $payload .= file_get_contents( $file_path );
288 $payload .= "\r\n";
289
290 $payload .= '--' . $boundary . '--';
291
292 $entryCode = $params['codoc_entry_code'];
293 $res = $this->callAPI(
294 'POST',
295 '/entries/' . $entryCode . '/thumbnail',
296 $payload,
297 ['content-type' => 'multipart/form-data; boundary=' . $boundary]
298 );
299 }
300 return $res;
301 }
302
303 function reset_thumbnail( $params = [
304 "codoc_entry_code" => null,
305 ],$auth_info = ["usercode" => null, "token" => null] ) {
306
307 $this->setAuthInfo($auth_info);
308
309 $entryCode = $params['codoc_entry_code'];
310 $res = $this->callAPI(
311 'POST',
312 '/entries/' . $entryCode . '/thumbnail',
313 [ "reset" => 1 ]
314 );
315 return $res;
316 }
317
318 function filter_content( $params = [
319 "post_content" => null,
320 "preview" => null,
321 "codoc_entry_code" => null,
322 "codoc_settings" => null,
323 "is_amp_endpoint" => null,
324 "post_permalink" => null,
325 "codoc_support_entry_code" => null,
326 ]) {
327 $CODOC_SETTINGS = $params['codoc_settings'];
328
329 $has_codoc_tag = '/(<(span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:div|span)>)/';
330 $post_content = $params['post_content'];
331 // codoc タグがあるかどうか (202001:span -> div に変更)
332 preg_match($has_codoc_tag,$post_content,$matches);
333
334 $show_support_message = '';
335 if (isset($CODOC_SETTINGS['show_support_message'])) {
336 $show_support_message = $CODOC_SETTINGS['show_support_message'];
337 }
338 $show_support_categories = '';
339 if (isset($CODOC_SETTINGS['show_support_categories'])) {
340 $show_support_categories = $CODOC_SETTINGS['show_support_categories'];
341 }
342 $show_support_location = 'bottom';
343 if (isset($CODOC_SETTINGS['show_support_location'])) {
344 $show_support_location = $CODOC_SETTINGS['show_support_location'];
345 }
346 // デフォルト値をうめておく
347 if (!isset($CODOC_SETTINGS['support_button_text'])) {
348 $CODOC_SETTINGS['support_button_text'] = 'サポートする';
349 }
350 if (!isset($CODOC_SETTINGS['show_like'])) {
351 $CODOC_SETTINGS['show_like'] = 1;
352 }
353 if (!isset($CODOC_SETTINGS['show_about_codoc'])) {
354 $CODOC_SETTINGS['show_about_codoc'] = 1;
355 }
356 if (!isset($CODOC_SETTINGS['show_powered_by'])) {
357 $CODOC_SETTINGS['show_powered_by'] = 1;
358 }
359 if (!isset($CODOC_SETTINGS['show_created_by'])) {
360 $CODOC_SETTINGS['show_created_by'] = 1;
361 }
362 if (!isset($CODOC_SETTINGS['show_copyright'])) {
363 $CODOC_SETTINGS['show_copyright'] = 1;
364 }
365 $show_support_entry = (is_single() && preg_grep("/(?:$show_support_categories)/",array_map( function($c) { return $c->name; },get_the_category(get_post()->ID))));
366 if (!$matches && ($support_entry_code = $params['codoc_support_entry_code']) && $show_support_entry) {
367 $support_tag = sprintf(
368 '<div class="codoc-entries" data-without-body="1" data-support-message="%s" id="codoc-entry-%s" data-support-button-text="%s" data-show-like="%s" data-show-about-codoc="%s" data-show-powered-by="%s" data-show-created-by="%s" data-show-copyright="%s"></div>',
369 htmlspecialchars($show_support_message,ENT_QUOTES),
370 $support_entry_code,
371 $CODOC_SETTINGS['support_button_text'],
372 $CODOC_SETTINGS['show_like'],
373 $CODOC_SETTINGS['show_about_codoc'],
374 $CODOC_SETTINGS['show_powered_by'],
375 $CODOC_SETTINGS['show_created_by'],
376 $CODOC_SETTINGS['show_copyright']
377 );
378 $post_content = $show_support_location == 'bottom' ?
379 $post_content . $support_tag :
380 $support_tag . $post_content ;
381 }
382
383 // data-wp-plugin-ver が無いタグは分割しない
384 if (!$matches) {
385 return $post_content;
386 }
387
388 // codocタグで分割 (202001: gutenbergで直前のdivタグを削除)
389 $tag_regex = '/(?:<div(?:[^>]+|)>|)<(?:span|div)[^>]+data-id="codoc-tag"(?:[^>]+|)>(?:.+|)<\/(?:span|div)>(?:<\/div>|)/';
390 $end_tag_regex = '/<(?:div|p)>::CODOC_WP_END_PAYWALL::<\/(?:div|p)>/';
391 if ( $params['preview'] ) {
392 $post_content = preg_replace($tag_regex,'<div class="codoc-continue">ここから上は無料で表示されます</div>',$post_content);
393 $post_content = preg_replace($end_tag_regex,'<div class="codoc-continue">ここから下は無料で表示されます</div>',$post_content);
394 return $post_content;
395 }
396 // codoc タグの前後で分ける
397 $before_splited = preg_split($end_tag_regex,$post_content);
398 $splited = preg_split($tag_regex,$before_splited[0]);
399 // codoc タグにID属性のentrycodeとdata-without-body(無料分を非表示)をつける
400 $entryCodeFormated = sprintf('"codoc-entry-%s" ',$params['codoc_entry_code']);
401
402 // 文言系の設定をつける
403 $tagAttributes = '';
404 if (isset($CODOC_SETTINGS['show_like']) and $CODOC_SETTINGS['show_like'] != 1) {
405 $tagAttributes = $tagAttributes . sprintf(' data-show-like="%s"',$CODOC_SETTINGS['show_like']);
406 }
407 if (isset($CODOC_SETTINGS['show_about_codoc']) and $CODOC_SETTINGS['show_about_codoc'] != 1) {
408 $tagAttributes = $tagAttributes . sprintf(' data-show-about-codoc="%s"',$CODOC_SETTINGS['show_about_codoc']);
409 }
410 if (isset($CODOC_SETTINGS['show_powered_by']) and $CODOC_SETTINGS['show_powered_by'] != 1) {
411 $tagAttributes = $tagAttributes . sprintf(' data-show-powered-by="%s"',$CODOC_SETTINGS['show_powered_by']);
412 }
413 if (isset($CODOC_SETTINGS['show_created_by']) and $CODOC_SETTINGS['show_created_by'] != 1) {
414 $tagAttributes = $tagAttributes . sprintf(' data-show-created-by="%s"',$CODOC_SETTINGS['show_created_by']);
415 }
416 if (isset($CODOC_SETTINGS['show_copyright']) and $CODOC_SETTINGS['show_copyright'] != 1) {
417 $tagAttributes = $tagAttributes . sprintf(' data-show-copyright="%s"',$CODOC_SETTINGS['show_copyright']);
418 }
419 if (isset($CODOC_SETTINGS['entry_button_text']) and $CODOC_SETTINGS['entry_button_text'] != '') {
420 $tagAttributes = $tagAttributes . sprintf(' data-entry-button-text="%s"',$CODOC_SETTINGS['entry_button_text']);
421 }
422 if (isset($CODOC_SETTINGS['subscription_button_text']) and $CODOC_SETTINGS['subscription_button_text'] != '') {
423 $tagAttributes = $tagAttributes . sprintf(' data-subscription-button-text="%s"',$CODOC_SETTINGS['subscription_button_text']);
424 }
425 if (isset($CODOC_SETTINGS['support_button_text']) and $CODOC_SETTINGS['support_button_text'] != '') {
426 $tagAttributes = $tagAttributes . sprintf(' data-support-button-text="%s"',$CODOC_SETTINGS['support_button_text']);
427 }
428 if (isset($CODOC_SETTINGS['subscription_message']) and $CODOC_SETTINGS['subscription_message'] != '') {
429 $tagAttributes = $tagAttributes . sprintf(' data-subscription-message="%s"',$CODOC_SETTINGS['subscription_message']);
430 }
431 if (isset($CODOC_SETTINGS['support_message']) and $CODOC_SETTINGS['support_message'] != '') {
432 $tagAttributes = $tagAttributes . sprintf(' data-support-message="%s"',$CODOC_SETTINGS['support_message']);
433 }
434 if (isset($CODOC_SETTINGS['codoc_tag_attributes']) and $CODOC_SETTINGS['codoc_tag_attributes'] != '') {
435 $tagAttributes = $tagAttributes . sprintf(' %s',$CODOC_SETTINGS['codoc_tag_attributes']);
436 }
437
438 $codoc_tag = preg_replace('/<((?:span|div)[^>]+)data-id="[^"]+"((?:[^>]+|))>/','<${1} ${2} data-without-body="1" id=' . $entryCodeFormated . $tagAttributes . ">", $matches[1]);
439 # THE THOR の page-lp.php が " " をけずるための対策
440 # "div class" -> "div class"
441 $codoc_tag = preg_replace('/div +class/','div class',$codoc_tag);
442
443 if ($params['is_amp_endpoint']) {
444 $format = preg_replace_callback('/(<(?:span|div)[^>]+>)((?:[^<>]+|))(<\/(?:span|div)>)/',function($matches) {
445 $format = $matches[1] . '<a href="%%s">%s</a>' . $matches[3];
446 return sprintf($format,($matches[2] ? $matches[2] : '続きを読む'));
447 },$codoc_tag);
448 $codoc_tag = sprintf($format,$params["post_permalink"]);
449 }
450
451 # タグの前後にHTMLを挿�
452 �
453 if (!isset($CODOC_SETTINGS['str_before_codoc_tag'])) {
454 $CODOC_SETTINGS['str_before_codoc_tag'] = '';
455 }
456 if (!isset($CODOC_SETTINGS['str_after_codoc_tag'])) {
457 $CODOC_SETTINGS['str_after_codoc_tag'] = '';
458 }
459 // 無料部分のみ表示
460 $post_content = $splited[0] . sprintf(
461 '%s<div class="wp-block-codoc-codoc-block">%s</div>%s',
462 $CODOC_SETTINGS['str_before_codoc_tag'],
463 $codoc_tag,
464 $CODOC_SETTINGS['str_after_codoc_tag']
465 );
466 // ::CODOC_WP_END_PAYWALL:: の後を足す
467 if (isset($before_splited[1]) and $before_splited[1]) {
468 $post_content .= $before_splited[1];
469 }
470 return $post_content;
471 }
472 /*
473
474 // Cookieの中のトークンを取得
475 $_CODOC->util->get_paywall_token_code();
476
477 */
478 function get_paywall_token_code() {
479 if (isset($_COOKIE['codocTokenCode']) and $codocTokenCode = $_COOKIE['codocTokenCode']) {
480 return $codocTokenCode;
481 }
482 return null;
483 }
484 /*
485
486 $_CODOC->util->check_login($mode); // cookie / strict
487
488 $response = $_CODOC->util->check_login('strict_with_data');
489 if ($response->status and $response->user) {
490 echo $reponse->user->code;
491 }
492
493 */
494 function check_login($mode = 'cookie') {
495 if ($codocTokenCode = $this->get_paywall_token_code()) {
496 if ($mode == 'cookie') {
497 // cookieの中にtokenがあればOK
498 return true;
499 }
500 if ($mode == 'strict' or $mode == 'strict_with_data') {
501 // ログイン状�
502 �を問い合わせ
503 $res = $this->callPaywallAPI('GET','/users',["paywall_token_code" => $codocTokenCode ]);
504 if ($res->status) {
505 if ($mode == 'strict_with_data') {
506 return $res;
507 } else {
508 return true;
509 }
510 } else {
511 return false;
512 }
513 }
514 }
515 return false;
516 }
517 /*
518
519 $_CODOC->util->check_owned($entry_code);
520
521 */
522 function check_owned($entry_code = null) {
523 if (!$entry_code) {
524 return false;
525 }
526 $res = $this->callPaywallAPI('GET','/entries/' . $entry_code, ["paywall_token_code" => $this->get_paywall_token_code(), "check_owned" => 1 ]);
527 if ($res->status) {
528 return true;
529 }
530 return false;
531 }
532
533 /*
534 $_CODOC->util->health_check();
535 */
536 function health_check() {
537 $sslverify = true;
538 $host = $this->codoc_url;
539 if (preg_match('/local/',$this->codoc_url)) {
540 $sslverify = false;
541 $host = 'https://host.docker.internal';
542 }
543 $url = sprintf("%s/codoc_health_check",$host);
544 $http = new WP_Http();
545 try {
546 $response = $http->request(
547 $url,
548 [
549 'sslverify' => $sslverify,
550 'method' => 'GET',
551 'timeout' => 10,
552 'headers' => [],
553 'body' => [],
554 ]
555 );
556 } catch(Exception $e) {
557
558 }
559 if ($response and $response['response']['code'] == '200') {
560 return true;
561 } else {
562 return null;
563 }
564 }
565 }
566
567