diff --git a/src/Convert/Strings.php b/src/Convert/Strings.php index 2935e26..8163239 100644 --- a/src/Convert/Strings.php +++ b/src/Convert/Strings.php @@ -134,6 +134,18 @@ class Strings $path ) ?? $path; } + + /** + * Remove UTF8 BOM Byte string from line + * Note: this is often found in CSV files exported from Excel at the first row, first element + * + * @param string $text + * @return string + */ + public static function stripUTF8BomBytes(string $text): string + { + return trim($text, pack('H*', 'EFBBBF')); + } } // __END__ diff --git a/src/DB/IO.php b/src/DB/IO.php index a6d7ad6..c28471e 100644 --- a/src/DB/IO.php +++ b/src/DB/IO.php @@ -2402,7 +2402,7 @@ class IO // flag if we have cache data stored at the moment 'cached' => false, // when fetch array or cache read returns false - // in loop read that means dbReturn retuns false without erro + // in loop read that means dbReturn retuns false without error 'finished' => false, // read from cache/db (pos == rows) 'read_finished' => false, diff --git a/src/Get/System.php b/src/Get/System.php index 690b43b..3a2bd2b 100644 --- a/src/Get/System.php +++ b/src/Get/System.php @@ -116,6 +116,29 @@ class System 3 ) === 'cli' ? true : false; } + + /** + * Collect all IP addresses + * REMOTE_ADDR, HTTP_X_FORWARD_FOR, CLIENT_IP + * and retuns them in an array with index of io source + * if address source has addresses with "," will add "-array" with these as array block + * + * @return array> + */ + public static function getIpAddresses(): array + { + $ip_addr = []; + foreach (['REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR', 'CLIENT_IP'] as $_ip_source) { + if (!empty($_SERVER[$_ip_source])) { + $ip_addr[$_ip_source] = $_SERVER[$_ip_source]; + // same level as ARRAY IF there is a , inside + if (strstr($_SERVER[$_ip_source], ',') !== false) { + $ip_addr[$_ip_source . '-array'] = explode(',', $_SERVER[$_ip_source]); + } + } + } + return $ip_addr; + } } // __END__ diff --git a/src/Template/HtmlBuilder/Block.php b/src/Template/HtmlBuilder/Block.php index 2015280..ec8b9e3 100644 --- a/src/Template/HtmlBuilder/Block.php +++ b/src/Template/HtmlBuilder/Block.php @@ -19,7 +19,7 @@ use CoreLibs\Template\HtmlBuilder\General\HtmlBuilderExcpetion; class Block { /** - * Undocumented function + * Create Element * * @param string $tag * @param string $id @@ -86,7 +86,7 @@ class Block } /** - * Undocumented function + * Add multiple elements to the base element * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $base * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} ...$attach @@ -101,7 +101,7 @@ class Block } /** - * Undocumented function + * Add multiple sub elements to the base element * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $sub @@ -117,7 +117,7 @@ class Block } /** - * Undocumented function + * Remove all sub element entries * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @return array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} @@ -131,7 +131,7 @@ class Block // CSS Elements /** - * Undocumented function + * Add css entry to the css entries * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @param string ...$css @@ -144,7 +144,7 @@ class Block } /** - * Undocumented function + * Remove a css entry entry from the css array * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $element * @param string ...$css @@ -157,7 +157,7 @@ class Block } /** - * Undocumented function + * Switch CSS entries * scssel (switch) is not supported * use rcssel -> acssel * @@ -175,7 +175,7 @@ class Block } /** - * Undocumented function + * Build HTML from the content tree * alias phfo * * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $tree @@ -231,7 +231,19 @@ class Block } /** - * Undocumented function + * Alias for phfo + * + * @param array{tag:string,id:string,name:string,content:string,css:array,options:array,sub:array} $tree + * @param bool $add_nl [default=false] + * @return string + */ + public static function phfo(array $tree, bool $add_nl = false): string + { + return self::buildHtml($tree, $add_nl); + } + + /** + * Build HTML elements from an array of elements * alias phfa * * @param array,options:array,sub:array}> $list @@ -248,8 +260,7 @@ class Block } /** - * Undocumented function - * wrapper for buildHtmlFromList + * alias for buildHtmlFromList * * @param array,options:array,sub:array}> $list array of Elements to build string from * @param bool $add_nl [default=false] Optional output string line break diff --git a/src/Template/SmartyExtend.php b/src/Template/SmartyExtend.php index 9487066..e9b906d 100644 --- a/src/Template/SmartyExtend.php +++ b/src/Template/SmartyExtend.php @@ -203,7 +203,8 @@ class SmartyExtend extends \Smarty _bind_textdomain_codeset($this->domain, $this->encoding); // register smarty variable - $this->registerPlugin('modifier', 'getvar', [&$this, 'getTemplateVars']); + // $this->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'getvar', [&$this, 'getTemplateVars']); + $this->registerPlugin(self::PLUGIN_MODIFIER, 'getvar', [&$this, 'getTemplateVars']); $this->page_name = \CoreLibs\Get\System::getPageName(); diff --git a/test/phpunit/Convert/CoreLibsConvertStringsTest.php b/test/phpunit/Convert/CoreLibsConvertStringsTest.php index 0b2f79a..c6c9225 100644 --- a/test/phpunit/Convert/CoreLibsConvertStringsTest.php +++ b/test/phpunit/Convert/CoreLibsConvertStringsTest.php @@ -13,6 +13,8 @@ use PHPUnit\Framework\TestCase; */ final class CoreLibsConvertStringsTest extends TestCase { + private const DATA_FOLDER = __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR; + /** * Undocumented function * @@ -330,6 +332,52 @@ final class CoreLibsConvertStringsTest extends TestCase \CoreLibs\Convert\Strings::stripMultiplePathSlashes($input) ); } + + /** + * Undocumented function + * + * @return array + */ + public function providerStripUTF8BomBytes(): array + { + return [ + "utf8-bom" => [ + "file" => "UTF8BOM.csv", + "expect" => "Asset Type,Epic,File Name\n", + ], + "utf8" => [ + "file" => "UTF8.csv", + "expect" => "Asset Type,Epic,File Name\n", + ], + ]; + } + + /** + * test utf8 bom remove + * + * @covers ::stripUTF8BomBytes + * @dataProvider providerStripUTF8BomBytes + * @testdox stripUTF8BomBytes $file will be $expected [$_dataName] + * + * @param string $file + * @param string $expected + * @return void + */ + public function testStripUTF8BomBytes(string $file, string $expected): void + { + // load sample file + if (!is_file(self::DATA_FOLDER . $file)) { + $this->markTestSkipped('File: ' . $file . ' could not be opened'); + } + $file = file_get_contents(self::DATA_FOLDER . $file); + if ($file === false) { + $this->markTestSkipped('File: ' . $file . ' could not be read'); + } + $this->assertEquals( + $expected, + \CoreLibs\Convert\Strings::stripUTF8BomBytes($file) + ); + } } // __END__ diff --git a/test/phpunit/Convert/data/UTF8.csv b/test/phpunit/Convert/data/UTF8.csv new file mode 100644 index 0000000..e19b244 --- /dev/null +++ b/test/phpunit/Convert/data/UTF8.csv @@ -0,0 +1 @@ +Asset Type,Epic,File Name diff --git a/test/phpunit/Convert/data/UTF8BOM.csv b/test/phpunit/Convert/data/UTF8BOM.csv new file mode 100644 index 0000000..87ddd55 --- /dev/null +++ b/test/phpunit/Convert/data/UTF8BOM.csv @@ -0,0 +1 @@ +Asset Type,Epic,File Name diff --git a/test/phpunit/Get/CoreLibsGetSystemTest.php b/test/phpunit/Get/CoreLibsGetSystemTest.php index a73e91f..9017a6d 100644 --- a/test/phpunit/Get/CoreLibsGetSystemTest.php +++ b/test/phpunit/Get/CoreLibsGetSystemTest.php @@ -216,6 +216,29 @@ final class CoreLibsGetSystemTest extends TestCase ); } } + + /** + * Undocumented function + * + * @covers ::getIpAddresses + * @testdox getIpAddresses check + * + * @return void + */ + public function testGetIpAddresses() + { + // response must have "REMOTE_ADDR" entry, others are optional + // NOTE: we have no IP addresses on command line + $this->assertTrue( + true, + "We do not have REMOTE_ADDR on command line" + ); + // $this->assertContains( + // 'REMOTE_ADDR', + // array_keys(\CoreLibs\Get\System::getIpAddresses()), + // 'failed REMOTE_ADDR assert' + // ); + } } // __END__