Skip to content

Commit

Permalink
ADD: API Яндекс ID и др. (шаблоны)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgip committed Jun 4, 2022
1 parent ea08c2e commit 8270ff2
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 83 deletions.
4 changes: 2 additions & 2 deletions app/Controllers/Auth/RecoverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Hleb\Constructor\Handlers\Request;
use App\Controllers\Controller;
use App\Models\User\{SettingModel, UserModel};
use Integration, Validation, SendEmail, Meta, Html, Msg, UserData;
use Google, Validation, SendEmail, Meta, Html, Msg, UserData;

class RecoverController extends Controller
{
Expand Down Expand Up @@ -35,7 +35,7 @@ public function index()
$redirect = url('recover');

if (config('general.captcha')) {
if (!Integration::checkCaptchaCode()) {
if (!Google::checkCaptchaCode()) {
Validation::comingBack(__('msg.code_error'), 'error', $redirect);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Controllers\Controller;
use App\Models\User\{InvitationModel, UserModel};
use App\Models\AuthModel;
use Integration, Validation, SendEmail, Meta, Html, UserData;
use Google, Validation, SendEmail, Meta, Html, UserData;

class RegisterController extends Controller
{
Expand Down Expand Up @@ -103,7 +103,7 @@ public function index()
// Проверим код проверки
if (!$inv_code) {
if (config('general.captcha')) {
if (!Integration::checkCaptchaCode()) {
if (!Google::checkCaptchaCode()) {
Validation::comingBack(__('msg.code_error'), 'error', $redirect);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/Post/AddPostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Controllers\Controller;
use App\Models\Item\WebModel;
use App\Models\{SubscriptionModel, ActionModel, PostModel, FacetModel, NotificationModel};
use Content, UploadImage, Integration, Validation, URLScraper, Meta, UserData;
use Content, UploadImage, Discord, Validation, URLScraper, Meta, UserData;

use Cocur\Slugify\Slugify;
use Utopia\Domains\Domain;
Expand Down Expand Up @@ -148,7 +148,7 @@ public function create($type)

if (config('general.discord')) {
if ($post_tl == 0 && $post_draft == 0) {
Integration::AddWebhook($content, $post_title, $redirect);
Discord::AddWebhook($content, $post_title, $redirect);
}
}

Expand Down
27 changes: 27 additions & 0 deletions app/Libraries/Curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

class Curl
{
public static function index($url, $params = [], $header = [])
{
$curl = curl_init();

if (strpos($url, 'https') !== false) {
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

$data = curl_exec($curl);

curl_close($curl);

return $data;
}
}
Original file line number Diff line number Diff line change
@@ -1,64 +1,8 @@
<?php

use Hleb\Constructor\Handlers\Request;

class Integration
class Discord
{
// Captcha v2
private static function callApi($params)
{
$api_url = 'https://www.google.com/recaptcha/api/siteverify';

// https://github.com/php-mod/curl
if (!function_exists('curl_init')) {

$data = @file_get_contents($api_url . '?' . http_build_query($params));
} else {

$curl = curl_init();

if (strpos($api_url, 'https') !== false) {
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

$data = curl_exec($curl);

curl_close($curl);
}

if (!$data) {
return false;
}
$data = json_decode($data, true);

return !empty($data['success']);
}

// Проверка в AuthControllerе
public static function checkCaptchaCode()
{
$response = Request::getPost('g-recaptcha-response');

if (!$response) {
return false;
}

return self::callApi(array(
'secret' => config('general.private_key'),
'response' => $response,
'remoteip' => $_SERVER['REMOTE_ADDR']
));
}

// Discord
public static function AddWebhook($text, $title, $url)
public static function addWebhook($text, $title, $url)
{
$text = strip_tags($text, '<p>');
$text = preg_replace(array('/(<p>)/', '(<\/p>)'), array('', '\n'), $text);
Expand All @@ -71,7 +15,7 @@ public static function AddWebhook($text, $title, $url)
return false;
}

$content = __('app.content_added', ['name' => __('app.post')]);
$content = __('app.post');
$color = hexdec("3366ff");

// Формируем даты
Expand Down Expand Up @@ -126,16 +70,6 @@ public static function AddWebhook($text, $title, $url)

], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

$ch = curl_init($webhookurl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
// echo $response;
curl_close($ch);
Curl::index($webhookurl, $json_data, ['Content-type: application/json']);
}
}
11 changes: 11 additions & 0 deletions app/Libraries/Integration/GitHub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Hleb\Constructor\Handlers\Request;

class GitHub
{
public static function callApi($params)
{
return false;
}
}
38 changes: 38 additions & 0 deletions app/Libraries/Integration/Google.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Hleb\Constructor\Handlers\Request;

class Google
{
// Captcha v2
private static function callApi($params)
{
$api_url = 'https://www.google.com/recaptcha/api/siteverify';

if (!function_exists('curl_init')) {
$data = @file_get_contents($api_url . '?' . http_build_query($params));
} else {
$data = Curl::index($api_url, $params, ['Accept: application/json']);
}

if (!$data) return false;

$data = json_decode($data, true);

return $data['success'] ?? false;
}

// Проверка в AuthControllerе
public static function checkCaptchaCode()
{
$response = Request::getPost('g-recaptcha-response');

if (!$response) return false;

return self::callApi(array(
'secret' => config('general.private_key'),
'response' => $response,
'remoteip' => $_SERVER['REMOTE_ADDR']
));
}
}
35 changes: 35 additions & 0 deletions app/Libraries/Integration/Yandex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Hleb\Constructor\Handlers\Request;

class Yandex
{
public static function callApi($params)
{
$code = Request::getPost('code');

if (!empty($code)) {

$sending_url = 'https://oauth.yandex.ru/token'; // отправка
$receiving_url = 'https://login.yandex.ru/info'; // получение

$params = [
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => 'ID_приложения',
'client_secret' => 'ПАРОЛЬ_приложения',
];

$info = Curl::index($sending_url, $params, ['Accept: application/json']);
$data = json_decode($info, true);

if (!empty($data['access_token'])) {

$info = Curl::index($receiving_url, ['format' => 'json'], ['Authorization: OAuth ' . $data['access_token']]);
$data = json_decode($info, true);
}
}

return $data;
}
}
7 changes: 6 additions & 1 deletion app/Optional/MainConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ public function add()
"Access" => "app/Libraries/Access.php",
"Msg" => "app/Libraries/Msg.php",

"Curl" => "app/Libraries/Curl.php",
"Google" => "app/Libraries/Integration/Google.php",
"Discord" => "app/Libraries/Integration/Discord.php",
"Yandex" => "app/Libraries/Integration/Yandex.php",
"GitHub" => "app/Libraries/Integration/GitHub.php",

"Content" => "app/Libraries/Content.php",
"UploadImage" => "app/Libraries/UploadImage.php",
"Integration" => "app/Libraries/Integration.php",
"Validation" => "app/Libraries/Validation.php",
"Html" => "app/Libraries/Html.php",
"Meta" => "app/Libraries/Meta.php",
Expand Down
2 changes: 1 addition & 1 deletion public/assets/css/style.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/views/default/global/base.header.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class="wrap">
<div class="d-header_contents">

<div class="flex">
<div class="flex items-center">
<div id="togglemenu" class="mr10"><i class="bi-list gray-600 text-xl"></i></div>
<div class="menu__button none mr10"><i class="bi-list gray-600 text-xl"></i></div>
<a title="<?= __('app.home'); ?>" class="logo" href="/"><?= config('meta.name'); ?></a>
Expand Down
6 changes: 2 additions & 4 deletions resources/views/default/scss/components/media.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ main { flex: 1; }

.menu__left, #togglemenu { display: none; }
aside { min-width: 240px; }
main { width: 100%; }
.menuno main { width: 100%; }

.menu__button {
transition: 0.3s ease;
Expand Down Expand Up @@ -57,7 +55,7 @@ main { flex: 1; }
.menu__left.menu__active .menu li {
background-color: #fff;
padding-left: 10px;
}
}

}

Expand All @@ -82,7 +80,7 @@ main { flex: 1; }
}

.mb-w-100,
.home-img { width: 100%; }
.home-img { width: 100%; }

aside,
.mb-none,
Expand Down

0 comments on commit 8270ff2

Please sign in to comment.