httpPassword/src/Utils.php

149 lines
3.6 KiB
PHP
Raw Normal View History

2022-12-31 00:48:40 +00:00
<?php
/**
* @brief httpPassword, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Frederic PLE and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-03-25 23:03:44 +00:00
declare(strict_types=1);
2022-12-31 00:48:40 +00:00
2023-03-25 23:03:44 +00:00
namespace Dotclear\Plugin\httpPassword;
use dcCore;
2022-12-31 00:48:40 +00:00
2023-03-25 23:03:44 +00:00
class Utils
{
/**
* Crypt password
2023-04-22 10:26:06 +00:00
*
* @param string $secret The secret
*
* @return string The crypt password (empty on error)
2023-03-25 23:03:44 +00:00
*/
2022-12-31 00:48:40 +00:00
public static function crypt(?string $secret): string
{
2023-04-22 10:26:06 +00:00
$secret = (string) $secret;
2023-03-25 23:03:44 +00:00
switch (self::cryptMethod()) {
2022-12-31 00:48:40 +00:00
case 'plaintext':
$saltlen = -1;
$salt = '';
break;
case 'crypt_std_des':
$saltlen = 2;
$salt = '';
break;
case 'crypt_ext_des':
$saltlen = 9;
$salt = '';
break;
case 'crypt_md5':
$saltlen = 12;
$salt = '$1$';
break;
case 'crypt_blowfish':
$saltlen = 16;
$salt = '$2$';
break;
case 'crypt_sha256':
$saltlen = 16;
$salt = '$5$';
break;
case 'crypt_sha512':
$saltlen = 16;
$salt = '$6$';
break;
default:
return '';
}
if ($saltlen > 0) {
$salt .= substr(
sha1(dcCore::app()->getNonce() . date('U')),
2,
$saltlen - strlen($salt)
);
$secret = crypt($secret, $salt);
}
2023-04-22 10:26:06 +00:00
return $secret;
2022-12-31 00:48:40 +00:00
}
2023-03-25 23:03:44 +00:00
/**
* Setting: active
2023-04-22 10:26:06 +00:00
*
* @return bool True if module is active
2023-03-25 23:03:44 +00:00
*/
public static function isActive(): bool
{
2023-04-22 10:26:06 +00:00
return !is_null(dcCore::app()->blog) && (bool) dcCore::app()->blog->settings->get(My::id())->get('active');
2023-03-25 23:03:44 +00:00
}
/**
* Setting: crypt
2023-04-22 10:26:06 +00:00
*
* @return string The crypt method
2023-03-25 23:03:44 +00:00
*/
public static function cryptMethod(): string
{
2023-05-13 09:38:18 +00:00
return !is_null(dcCore::app()->blog) && is_string(dcCore::app()->blog->settings->get(My::id())->get('crypt')) ? dcCore::app()->blog->settings->get(My::id())->get('crypt') : '';
2023-03-25 23:03:44 +00:00
}
/**
* Setting: message
2023-04-22 10:26:06 +00:00
*
* @return string The frontend message
2023-03-25 23:03:44 +00:00
*/
public static function httpMessage(): string
{
2023-05-13 09:38:18 +00:00
return !is_null(dcCore::app()->blog) && is_string(dcCore::app()->blog->settings->get(My::id())->get('message')) ? dcCore::app()->blog->settings->get(My::id())->get('message') : '';
2023-03-25 23:03:44 +00:00
}
/**
* Get passwords file path
2023-04-22 10:26:06 +00:00
*
* @return string The passwords file path (empty on error)
2023-03-25 23:03:44 +00:00
*/
public static function passwordFile(): string
{
2023-04-22 10:26:06 +00:00
return is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->public_path . DIRECTORY_SEPARATOR . My::FILE_PASSWORD;
2023-03-25 23:03:44 +00:00
}
/**
* Check passwords file
2023-04-22 10:26:06 +00:00
*
* @return bool True if passwords file is writable
2023-03-25 23:03:44 +00:00
*/
2022-12-31 00:48:40 +00:00
public static function isWritable(): bool
{
2023-03-25 23:03:44 +00:00
if (false === ($fp = fopen(self::passwordFile(), 'a+'))) {
2022-12-31 00:48:40 +00:00
return false;
}
fclose($fp);
return true;
}
2023-03-25 23:03:44 +00:00
/**
* Send HTTP message
*/
2022-12-31 00:48:40 +00:00
public static function sendHttp401(): void
{
header('HTTP/1.1 401 Unauthorized');
2023-03-25 23:03:44 +00:00
header('WWW-Authenticate: Basic realm="' . utf8_decode(htmlspecialchars_decode(self::httpMessage())) . '"');
2022-12-31 00:48:40 +00:00
exit(0);
}
}