diff --git a/core/polyfills.php b/core/polyfills.php index 4d87b519..680879f4 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -752,3 +752,53 @@ function validate_input(array $inputs): array return $outputs; } + +/** + * Translates all possible directory separators to the appropriate one for the current system, + * and removes any duplicate separators. + */ +function sanitize_path(string $path): string +{ + return preg_replace('|[\\\\/]+|S',DIRECTORY_SEPARATOR,$path); +} + +/** + * Combines all path segments specified, ensuring no duplicate separators occur, + * as well as converting all possible separators to the one appropriate for the current system. + */ +function join_path(string ...$paths): string +{ + $output = ""; + foreach ($paths as $path) { + if(empty($path)) { + continue; + } + $path = sanitize_path($path); + if(empty($output)) { + $output = $path; + } else { + $output = rtrim($output, DIRECTORY_SEPARATOR); + $path = ltrim($path, DIRECTORY_SEPARATOR); + $output .= DIRECTORY_SEPARATOR . $path; + } + } + return $output; +} + +/** + * Perform callback on each item returned by an iterator. + */ +function iterator_map(callable $callback, iterator $iter): Generator +{ + foreach($iter as $i) { + yield call_user_func($callback,$i); + } +} + +/** + * Perform callback on each item returned by an iterator and combine the result into an array. + */ +function iterator_map_to_array(callable $callback, iterator $iter): array +{ + return iterator_to_array(iterator_map($callback, $iter)); +} \ No newline at end of file diff --git a/core/tests/polyfills.test.php b/core/tests/polyfills.test.php index 7aadec3d..68e0d10a 100644 --- a/core/tests/polyfills.test.php +++ b/core/tests/polyfills.test.php @@ -45,4 +45,77 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase $this->assertEquals(parse_shorthand_int("43.4KB"), 44441); $this->assertEquals(parse_shorthand_int("1231231231"), 1231231231); } + + public function test_sanitize_path() + { + + $this->assertEquals( + "one", + sanitize_path("one") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two", + sanitize_path("one\\two") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two", + sanitize_path("one/two") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two", + sanitize_path("one\\\\two") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two", + sanitize_path("one//two") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two", + sanitize_path("one\\\\\\two") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two", + sanitize_path("one///two") + ); + + $this->assertEquals( + DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR, + sanitize_path("\\/one/\\/\\/two\\/") + ); + + } + + public function test_join_path() + { + $this->assertEquals( + "one", + join_path("one") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two", + join_path("one","two") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three", + join_path("one","two","three") + ); + + $this->assertEquals( + "one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three", + join_path("one/two","three") + ); + + $this->assertEquals( + DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three".DIRECTORY_SEPARATOR, + join_path("\\/////\\\\one/\///"."\\//two\/\\//\\//","//\/\\\/three/\\/\/") + ); + } } diff --git a/core/tests/util.test.php b/core/tests/util.test.php index 9087dde5..3b3381f7 100644 --- a/core/tests/util.test.php +++ b/core/tests/util.test.php @@ -3,78 +3,6 @@ require_once "core/util.php"; class UtilTest extends \PHPUnit\Framework\TestCase { - public function test_sanitize_path() - { - - $this->assertEquals( - "one", - sanitize_path("one") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two", - sanitize_path("one\\two") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two", - sanitize_path("one/two") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two", - sanitize_path("one\\\\two") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two", - sanitize_path("one//two") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two", - sanitize_path("one\\\\\\two") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two", - sanitize_path("one///two") - ); - - $this->assertEquals( - DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR, - sanitize_path("\\/one/\\/\\/two\\/") - ); - - } - - public function test_join_path() - { - $this->assertEquals( - "one", - join_path("one") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two", - join_path("one","two") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three", - join_path("one","two","three") - ); - - $this->assertEquals( - "one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three", - join_path("one/two","three") - ); - - $this->assertEquals( - DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three".DIRECTORY_SEPARATOR, - join_path("\\/////\\\\one/\///"."\\//two\/\\//\\//","//\/\\\/three/\\/\/") - ); - } public function test_warehouse_path() { diff --git a/core/util.php b/core/util.php index ee7b39fc..43f62dd1 100644 --- a/core/util.php +++ b/core/util.php @@ -338,37 +338,6 @@ function path_to_tags(string $path): string return implode(" ", $tags); } -/** - * Translates all possible directory separators to the appropriate one for the current system, - * and removes any duplicate separators. - */ -function sanitize_path(string $path): string -{ - return preg_replace('|[\\\\/]+|S',DIRECTORY_SEPARATOR,$path); -} - -/** - * Combines all path segments specified, ensuring no duplicate separators occur, - * as well as converting all possible separators to the one appropriate for the current system. - */ -function join_path(string ...$paths): string -{ - $output = ""; - foreach ($paths as $path) { - if(empty($path)) { - continue; - } - $path = sanitize_path($path); - if(empty($output)) { - $output = $path; - } else { - $output = rtrim($output, DIRECTORY_SEPARATOR); - $path = ltrim($path, DIRECTORY_SEPARATOR); - $output .= DIRECTORY_SEPARATOR . $path; - } - } - return $output; -} function join_url(string $base, string ...$paths) { diff --git a/ext/pools/main.php b/ext/pools/main.php index 3a8b5a2b..37a1cc01 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -432,26 +432,18 @@ class Pools extends Extension $pool = $this->get_pool($pool_id); if ($this->have_permission($user, $pool)) { - $ids = []; - foreach($event->items as $item) { - $ids[] = $item->id; - } - send_event(new PoolAddPostsEvent($pool_id, $ids)); + send_event( + new PoolAddPostsEvent($pool_id,iterator_map_to_array("image_to_id", $event->items))); } break; case "bulk_pool_add_new": if (!isset($_POST['bulk_pool_new'])) { return; } - $ids = []; - foreach($event->items as $item) { - $ids[] = $item->id; - } - $new_pool_title = $_POST['bulk_pool_new']; $pce = new PoolCreationEvent($new_pool_title); send_event($pce); - send_event(new PoolAddPostsEvent($pce->new_id, $ids)); + send_event(new PoolAddPostsEvent($pce->new_id, iterator_map_to_array("image_to_id", $event->items))); break; } }