strikt types

This commit is contained in:
Troy Grunt
2026-02-15 14:45:02 +01:00
parent a093603c3c
commit cd031464e6
13 changed files with 77 additions and 69 deletions

View File

@@ -2,15 +2,6 @@
## Issue Drafts (Ready for Gitea/GitHub) ## Issue Drafts (Ready for Gitea/GitHub)
- #TODO Enable strict types + typed signatures
- Aufwand: `M`
- Labels: `quality`, `refactor`
- Ziel: Konsistente Typen und weniger implizite Typfehler.
- Akzeptanzkriterien:
- Alle PHP-Dateien starten mit `declare(strict_types=1);` (wo kompatibel).
- Oeffentliche Funktionen erhalten Rueckgabe-/Parameter-Typen.
- Inkompatible Stellen sind dokumentiert oder angepasst.
- #TODO Define unified error strategy - #TODO Define unified error strategy
- Aufwand: `M` - Aufwand: `M`
- Labels: `quality`, `api` - Labels: `quality`, `api`

View File

@@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
// include ('config.php'); // include ('config.php');
include_once ('sql.php'); include_once ('sql.php');
$sql = new SQL (); $sql = new SQL ();
@@ -8,4 +9,4 @@ include_once ('mail.php');
include_once ('debug.php'); include_once ('debug.php');
include_once ('troy-api.php'); include_once ('troy-api.php');
// include_once ('markdown.php'); // include_once ('markdown.php');
?> ?>

View File

@@ -1,14 +1,16 @@
<?php <?php
function debug($s) { declare(strict_types=1);
function debug(mixed $s): void {
if(isset($_COOKIE['debug'])) if(isset($_COOKIE['debug']))
print_r($s); print_r($s);
} }
function debugCookie($on=true) { function debugCookie(bool $on = true): void {
if($on) { if($on) {
setcookie('debug','1',time()+(60*60*24*365),"/"); setcookie('debug','1',time()+(60*60*24*365),"/");
}else{ }else{
setcookie('debug',null,0,"/"); setcookie('debug',null,0,"/");
} }
} }
?> ?>

View File

@@ -1,4 +1,6 @@
<?php <?php
declare(strict_types=1);
$_ips_crawler = array ( $_ips_crawler = array (
'34.79.234.76', // google '34.79.234.76', // google
'40.77.167.', // bing bot '40.77.167.', // bing bot
@@ -28,7 +30,7 @@ $_ips_crawler = array (
'2a01:4f8:190:4244::2', // mj12bot '2a01:4f8:190:4244::2', // mj12bot
'2a01:4f8:162:43c5::2', // mj12bot '2a01:4f8:162:43c5::2', // mj12bot
); );
function checkHuman() { function checkHuman(): bool {
global $_ips_crawler; global $_ips_crawler;
if (stripos ( $_SERVER ['HTTP_USER_AGENT'], 'bot' ) !== false || stripos ( $_SERVER ['HTTP_USER_AGENT'], 'crawler' ) !== false) { if (stripos ( $_SERVER ['HTTP_USER_AGENT'], 'bot' ) !== false || stripos ( $_SERVER ['HTTP_USER_AGENT'], 'crawler' ) !== false) {
return false; return false;
@@ -40,4 +42,4 @@ function checkHuman() {
} }
return true; return true;
} }
?> ?>

View File

@@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
function httpContext(int $timeout = 8) { function httpContext(int $timeout = 8) {
return stream_context_create([ return stream_context_create([
@@ -158,11 +159,7 @@ function getPageInfo(string $url): array {
return $ret; return $ret;
} }
function sanitizeTags($input): array { function sanitizeTags(array $input): array {
if (!is_array($input)) {
return [];
}
$ret = []; $ret = [];
foreach ($input as $tag) { foreach ($input as $tag) {
if (!is_string($tag)) { if (!is_string($tag)) {

View File

@@ -1,5 +1,7 @@
<?php <?php
function send_mail($an, $betreff, $text, $ok = '', $error = '') { declare(strict_types=1);
function send_mail(string $an, string $betreff, string $text, string $ok = '', string $error = ''): void {
global $absender; global $absender;
$sender = 'noreply@troy-grunt.de'; $sender = 'noreply@troy-grunt.de';
if(isset($absender) && $absender) { if(isset($absender) && $absender) {
@@ -23,7 +25,7 @@ function send_mail($an, $betreff, $text, $ok = '', $error = '') {
} }
} }
function send_html_mail($an, $betreff, $text, $ok = '', $error = '') { function send_html_mail(string $an, string $betreff, string $text, string $ok = '', string $error = ''): void {
global $absender; global $absender;
$sender = 'noreply@troy-grunt.de'; $sender = 'noreply@troy-grunt.de';
if(isset($absender) && $absender) { if(isset($absender) && $absender) {
@@ -57,7 +59,7 @@ function send_html_mail($an, $betreff, $text, $ok = '', $error = '') {
} }
} }
function send_php_mail($an, $betreff, $text, $ok = '', $error = '') { function send_php_mail(string $an, string $betreff, string $text, string $ok = '', string $error = ''): void {
global $_sendermail; global $_sendermail;
$sender = 'noreply@troy-grunt.de'; $sender = 'noreply@troy-grunt.de';
if (isset ( $_sendermail )) { if (isset ( $_sendermail )) {
@@ -78,4 +80,4 @@ function send_php_mail($an, $betreff, $text, $ok = '', $error = '') {
echo 'Message sent!'; echo 'Message sent!';
} }
} }
?> ?>

View File

@@ -1,6 +1,7 @@
<?php <?php
declare(strict_types=1);
// TODO markdown imple // TODO markdown imple
function md($str) { function md(string $str): string {
// return nl2br ( $str ); // TODO md problem // return nl2br ( $str ); // TODO md problem
$text = '<p>'; $text = '<p>';
$lv = 0; $lv = 0;
@@ -75,10 +76,10 @@ function md($str) {
$text .= '</p>'; $text .= '</p>';
return $text; return $text;
} }
function _md_link_replacer($in) { function _md_link_replacer(array $in): string {
// var_dump ( $in ); // var_dump ( $in );
$in = explode ( '|', $in [1], 2 ); $in = explode ( '|', $in [1], 2 );
return '<a href="' . $in [0] . '" target="_blank">' . (isset ( $in [1] ) ? $in [1] : $in [0]) . '</a>'; return '<a href="' . $in [0] . '" target="_blank">' . (isset ( $in [1] ) ? $in [1] : $in [0]) . '</a>';
} }
?> ?>

View File

@@ -1,5 +1,7 @@
<?php <?php
function decade($zahl) declare(strict_types=1);
function decade(int|float|string $zahl): int|float|string
{ {
if (! is_numeric($zahl) || $zahl == 0) if (! is_numeric($zahl) || $zahl == 0)
return $zahl; return $zahl;
@@ -31,7 +33,7 @@ function decade($zahl)
return $zahl . ' ' . $si[$e]; return $zahl . ' ' . $si[$e];
} }
function onlyNumeric($num) { function onlyNumeric(string $num): string {
return preg_replace("/[^0-9\.\-]+/", "", $num); return preg_replace("/[^0-9\.\-]+/", "", $num);
} }
?> ?>

6
og.php
View File

@@ -1,5 +1,7 @@
<?php <?php
function scanOG($url) { declare(strict_types=1);
function scanOG(string $url): array {
$og = array(); $og = array();
$html = file_get_contents($url); $html = file_get_contents($url);
@@ -12,4 +14,4 @@ function scanOG($url) {
//print_r($og); //print_r($og);
return $og; return $og;
} }
?> ?>

View File

@@ -1,15 +1,17 @@
<?php <?php
if (!defined('SQL_LOG')) define ( 'SQL_LOG', 1 ); // schreibt sql querys in eine log declare(strict_types=1);
$_m['host'] = 'localhost'; if (!defined('SQL_LOG')) define ( 'SQL_LOG', 1 ); // schreibt sql querys in eine log
$_m['user'] = '';
$_m['pass'] = ''; $_m['host'] = 'localhost';
$_m['data'] = ''; $_m['user'] = '';
$_m['pre'] = ''; $_m['pass'] = '';
$_m['salt'] = ''; $_m['data'] = '';
$_m['pre'] = '';
$_sendermail = 'noreply@.de'; $_m['salt'] = '';
$_smtp['srv'] = 'mail.seemsleg.it';
$_smtp['user'] = 'noreply@.de'; $_sendermail = 'noreply@.de';
$_smtp['pw'] = ''; $_smtp['srv'] = 'mail.seemsleg.it';
?> $_smtp['user'] = 'noreply@.de';
$_smtp['pw'] = '';
?>

14
sql.php
View File

@@ -1,4 +1,6 @@
<?php <?php
declare(strict_types=1);
class SQL { class SQL {
private $h; private $h;
private $res = false; private $res = false;
@@ -22,7 +24,7 @@ class SQL {
} }
return true; return true;
} }
public function get($que, $t = '', $p = array ()) { public function get(string $que, string $t = '', mixed $p = array ()): array|false {
// echo $que; // echo $que;
$this->cnt_get ++; $this->cnt_get ++;
if (SQL_LOG) if (SQL_LOG)
@@ -89,14 +91,14 @@ class SQL {
} }
return $ret; return $ret;
} }
public function single($que, $t = '', $p = array ()) { public function single(string $que, string $t = '', mixed $p = array ()): array|false {
$data = $this->get ( $que, $t, $p ); $data = $this->get ( $que, $t, $p );
if ($data) { if ($data) {
return $data [0]; return $data [0];
} }
return false; return false;
} }
public function list($que, $t = '', $p = array ()) { public function list(string $que, string $t = '', mixed $p = array ()): array|false {
$data = $this->get ( $que, $t, $p ); $data = $this->get ( $que, $t, $p );
if ($data) { if ($data) {
$ret = array (); $ret = array ();
@@ -109,7 +111,7 @@ class SQL {
} }
return false; return false;
} }
public function keyval($que, $k, $v, $t = '', $p = array ()) { public function keyval(string $que, string|int $k, string|int $v, string $t = '', mixed $p = array ()): array|false {
$data = $this->get ( $que, $t, $p ); $data = $this->get ( $que, $t, $p );
if ($data) { if ($data) {
$ret = array (); $ret = array ();
@@ -120,7 +122,7 @@ class SQL {
} }
return false; return false;
} }
public function set($que, $t = '', $p = array (), $id = false) { public function set(string $que, string $t = '', mixed $p = array (), bool $id = false): int|false {
// echo $que; // echo $que;
$this->cnt_set ++; $this->cnt_set ++;
$statement = $this->h->prepare ( $que ); $statement = $this->h->prepare ( $que );
@@ -189,7 +191,7 @@ class SQL {
return $statement->affected_rows; return $statement->affected_rows;
} }
} }
function __destruct() { function __destruct(): void {
if (SQL_LOG) if (SQL_LOG)
$this->h->close (); $this->h->close ();
// echo 'DESTROY'; // echo 'DESTROY';

View File

@@ -1,5 +1,7 @@
<?php <?php
function umlaute($str) { declare(strict_types=1);
function umlaute(string $str): string {
return str_replace ( array ( return str_replace ( array (
'Ä', 'Ä',
'Ö', 'Ö',
@@ -20,10 +22,10 @@ function umlaute($str) {
'&amp;' '&amp;'
), $str ); ), $str );
} }
function chk($str) { function chk(string $str): string {
return str_replace ( "'", '"', $str ); return str_replace ( "'", '"', $str );
} }
function noScript($str) { function noScript(string $str): string {
return str_replace ( array ( return str_replace ( array (
'<', '<',
'>' '>'
@@ -32,7 +34,7 @@ function noScript($str) {
'&gt;' '&gt;'
), $str ); ), $str );
} }
function random($name_laenge) { function random(int $name_laenge): string {
$zeichen = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ0123456789"; $zeichen = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ0123456789";
$name_neu = ""; $name_neu = "";
@@ -43,32 +45,32 @@ function random($name_laenge) {
} }
return $name_neu; return $name_neu;
} }
function startsWith($haystack, $needle) { function startsWith(string $haystack, string $needle): bool {
$length = strlen ( $needle ); $length = strlen ( $needle );
return (substr ( $haystack, 0, $length ) === $needle); return (substr ( $haystack, 0, $length ) === $needle);
} }
function endsWith($haystack, $needle) { function endsWith(string $haystack, string $needle): bool {
$length = strlen ( $needle ); $length = strlen ( $needle );
return $length === 0 || (substr ( $haystack, - $length ) === $needle); return $length === 0 || (substr ( $haystack, - $length ) === $needle);
} }
function onlyAlpha($str, $zus = '') { function onlyAlpha(string $str, string $zus = ''): string {
return preg_replace ( "/[^a-zA-Z0-9 \-\{$zus}_]+/", "", $str ); return preg_replace ( "/[^a-zA-Z0-9 \-\{$zus}_]+/", "", $str );
} }
function shortener($str, $len = 50, $fill = '...') { function shortener(string $str, int $len = 50, string $fill = '...'): string {
if (strlen ( $str ) > $len) { if (strlen ( $str ) > $len) {
$str = substr ( $str, 0, $len - strlen ( $fill ) ) . $fill; $str = substr ( $str, 0, $len - strlen ( $fill ) ) . $fill;
} }
return $str; return $str;
} }
function isEmail($str) { function isEmail(string $str): string|false {
$match = preg_match ( "/[a-zA-Z0-9\-\_\.]*\@[a-zA-Z0-9\-\_\.]*.[a-z]{2,10}/", $str ); $match = preg_match ( "/[a-zA-Z0-9\-\_\.]*\@[a-zA-Z0-9\-\_\.]*.[a-z]{2,10}/", $str );
if ($match) { if ($match) {
return $str; return $str;
} }
return false; return false;
} }
function markUp($text) { function markUp(string $text): string {
$r = ''; $r = '';
$lv = 0; $lv = 0;
foreach ( explode ( "\n", $text ) as $t ) { foreach ( explode ( "\n", $text ) as $t ) {
@@ -127,7 +129,7 @@ function markUp($text) {
} }
return $r; return $r;
} }
function onlySimpleHTML($s) { function onlySimpleHTML(string $s): string {
$s = str_replace ( array ( $s = str_replace ( array (
'<', '<',
'>' '>'
@@ -259,14 +261,14 @@ function onlySimpleHTML($s) {
return $s; return $s;
} }
function linkify($input) { function linkify(string $input): string {
$pattern = '@(http(s)?://[a-zA-Z0-9/\.\#\-\_]*)@'; $pattern = '@(http(s)?://[a-zA-Z0-9/\.\#\-\_]*)@';
return $output = preg_replace ( $pattern, '<a href="$1">$1</a>', $input ); return $output = preg_replace ( $pattern, '<a href="$1">$1</a>', $input );
} }
function inStr($needle, $haystack) { function inStr(string $needle, string $haystack): bool {
if (strpos ( $haystack, $needle ) !== false) { if (strpos ( $haystack, $needle ) !== false) {
return true; return true;
} }
return false; return false;
} }
?> ?>

View File

@@ -1,5 +1,7 @@
<?php <?php
function sendToTroy($data) { declare(strict_types=1);
function sendToTroy(array $data): string|false {
$url = 'https://troy-grunt.de/api.php'; $url = 'https://troy-grunt.de/api.php';
$options = array ( $options = array (
'http' => array ( 'http' => array (
@@ -14,7 +16,7 @@ function sendToTroy($data) {
return file_get_contents ( $url, false, $context ); return file_get_contents ( $url, false, $context );
} }
function sendToGitea($title, $message) { function sendToGitea(string $title, string $message): array|null {
// secret.php liegt in lib/ // secret.php liegt in lib/
require 'secret.php'; require 'secret.php';