1+ <?php
2+
3+ use DBA \Chunk ;
4+ use DBA \ContainFilter ;
5+ use JetBrains \PhpStorm \NoReturn ;
6+ use Psr \Http \Message \ResponseInterface as Response ;
7+ use Psr \Http \Message \ServerRequestInterface as Request ;
8+ use DBA \Factory ;
9+ use DBA \Hash ;
10+ use DBA \Hashlist ;
11+ use DBA \QueryFilter ;
12+ use DBA \Task ;
13+ use Middlewares \Utils \HttpErrorException ;
14+
15+ require_once (dirname (__FILE__ ) . "/../common/AbstractHelperAPI.class.php " );
16+
17+ class GetCracksOfTaskHelper extends AbstractHelperAPI {
18+ public static function getBaseUri (): string {
19+ return "/api/v2/helper/getCracksOfTask " ;
20+ }
21+
22+ public static function getAvailableMethods (): array {
23+ return ['GET ' ];
24+ }
25+
26+ public function getRequiredPermissions (string $ method ): array {
27+ return [Hashlist::PERM_READ , Hash::PERM_READ , Task::PERM_READ ];
28+ }
29+
30+ public static function getResponse (): null {
31+ return null ;
32+ }
33+
34+
35+ #[NoReturn] public function actionPost (array $ data ): object |array |null {
36+ assert (False , "getCracksOfTask has no POST " );
37+ }
38+
39+ /**
40+ * Description of get params for swagger.
41+ */
42+ public function getParamsSwagger (): array {
43+ return [
44+ [
45+ "in " => "query " ,
46+ "name " => "task " ,
47+ "schema " => [
48+ "type " => "integer " ,
49+ "format " => "int32 "
50+ ],
51+ "required " => true ,
52+ "example " => 1 ,
53+ "description " => "The ID of the task. "
54+ ]
55+ ];
56+ }
57+
58+ /**
59+ * Endpoint to get the cracked hashes of a certain task
60+ * @param Request $request
61+ * @param Response $response
62+ * @return Response
63+ * @throws HttpErrorException
64+ */
65+ public function handleGet (Request $ request , Response $ response ): Response {
66+ $ this ->preCommon ($ request );
67+ $ task = Factory::getTaskFactory ()->get ($ request ->getQueryParams ()['task ' ]);
68+ if ($ task == null ) {
69+ throw new HttpError ("No task has been found with provided task id " );
70+ }
71+ $ hashlists = Util::checkSuperHashlist (Factory::getHashlistFactory ()->get (Factory::getTaskWrapperFactory ()->get ($ task ->getTaskWrapperId ())->getHashlistId ()));
72+ if ($ hashlists [0 ]->getFormat () == DHashlistFormat::PLAIN ) {
73+ $ hashFactory = Factory::getHashFactory ();
74+ }
75+ else {
76+ $ hashFactory = Factory::getHashBinaryFactory ();
77+ }
78+ $ qF = new QueryFilter (Chunk::TASK_ID , $ task ->getId (), "= " );
79+ $ chunks = Factory::getChunkFactory ()->filter ([Factory::FILTER => $ qF ]);
80+ $ chunkIds = array ();
81+ foreach ($ chunks as $ chunk ) {
82+ $ chunkIds [] = $ chunk ->getId ();
83+ }
84+ $ queryFilters [] = new ContainFilter (Hash::CHUNK_ID , $ chunkIds );
85+ $ queryFilters [] = new QueryFilter (Hash::IS_CRACKED , 1 , "= " );
86+ $ hashes = $ hashFactory ->filter ([Factory::FILTER => $ queryFilters ]);
87+ $ converted = [];
88+
89+ foreach ($ hashes as $ hash ) {
90+ $ converted [] = self ::obj2Resource ($ hash );
91+ }
92+ $ ret = self ::createJsonResponse (data: $ converted );
93+
94+ $ body = $ response ->getBody ();
95+ $ body ->write ($ this ->ret2json ($ ret ));
96+
97+ return $ response ->withStatus (200 )
98+ ->withHeader ("Content-Type " , 'application/vnd.api+json; ' );
99+ }
100+
101+ static public function register ($ app ): void {
102+ $ baseUri = GetCracksOfTaskHelper::getBaseUri ();
103+
104+ /* Allow CORS preflight requests */
105+ $ app ->options ($ baseUri , function (Request $ request , Response $ response ): Response {
106+ return $ response ;
107+ });
108+ $ app ->get ($ baseUri , "getCracksOfTaskHelper:handleGet " );
109+ }
110+ }
111+
112+ GetCracksOfTaskHelper::register ($ app );
0 commit comments