Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.05% |
27 / 38 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SgfController | |
67.65% |
23 / 34 |
|
0.00% |
0 / 2 |
15.10 | |
0.00% |
0 / 1 |
| fetch | |
47.06% |
8 / 17 |
|
0.00% |
0 / 1 |
11.34 | |||
| upload | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
5.04 | |||
| 1 | <?php |
| 2 | |
| 3 | App::uses('AdminActivityLogger', 'Utility'); |
| 4 | App::uses('AdminActivityType', 'Model'); |
| 5 | App::uses('NotFoundException', 'Routing/Error'); |
| 6 | App::uses('BadRequestException', 'Routing/Error'); |
| 7 | |
| 8 | class SgfController extends AppController |
| 9 | { |
| 10 | public function fetch(int $sgfID) |
| 11 | { |
| 12 | if (!Auth::isLoggedIn()) |
| 13 | { |
| 14 | $this->response->statusCode(403); |
| 15 | $this->response->body('User not logged in.'); |
| 16 | return $this->response; |
| 17 | } |
| 18 | |
| 19 | $sgf = ClassRegistry::init('Sgf')->find('first', ['conditions' => ['id' => $sgfID], 'order' => 'id DESC']); |
| 20 | if (!$sgf) |
| 21 | { |
| 22 | $this->response->statusCode(404); |
| 23 | $this->response->body('Sgf not found.'); |
| 24 | return $this->response; |
| 25 | } |
| 26 | |
| 27 | $status = ClassRegistry::init('TsumegoStatus')->find('first', ['conditions' => ['tsumego_id' => $sgf['Sgf']['tsumego_id'], 'user_id' => Auth::getUserID()]]); |
| 28 | if (!Auth::isAdmin() && (!$status || !TsumegoUtil::isRecentlySolved($status['TsumegoStatus']['status']))) |
| 29 | { |
| 30 | $this->response->statusCode(403); |
| 31 | $this->response->body('Related tsumego is not in a solved state for the user ' . Auth::getUser()['name']); |
| 32 | return $this->response; |
| 33 | } |
| 34 | |
| 35 | $this->response->statusCode(200); |
| 36 | $this->response->body($sgf['Sgf']['sgf']); |
| 37 | return $this->response; |
| 38 | } |
| 39 | |
| 40 | public function upload($setConnectionID) |
| 41 | { |
| 42 | $setConnection = ClassRegistry::init('SetConnection')->findById($setConnectionID); |
| 43 | if (!$setConnection) |
| 44 | throw new NotFoundException("Specified set connection does not exist."); |
| 45 | |
| 46 | // Use besogo textarea if provided, otherwise use file upload |
| 47 | $fileUpload = isset($_FILES['adminUpload']) && $_FILES['adminUpload']['error'] === UPLOAD_ERR_OK ? $_FILES['adminUpload'] : null; |
| 48 | $sgfDataOrFile = $this->data['sgfForBesogo'] ?? file_get_contents($fileUpload['tmp_name']); |
| 49 | $sgfDataOrFile = str_replace("\r", '', $sgfDataOrFile); |
| 50 | $sgfDataOrFile = str_replace("\n", '"+"\n"+"', $sgfDataOrFile); |
| 51 | |
| 52 | if (!$sgfDataOrFile) |
| 53 | throw new BadRequestException('No SGF data provided.'); |
| 54 | |
| 55 | AdminActivityLogger::log( |
| 56 | AdminActivityType::SGF_EDIT, |
| 57 | $setConnection['SetConnection']['tsumego_id'], |
| 58 | $setConnection['SetConnection']['set_id'], |
| 59 | ); |
| 60 | |
| 61 | $this->set('sgf', $sgfDataOrFile); |
| 62 | $this->set('setConnectionID', $setConnectionID); |
| 63 | $this->render('/Tsumegos/setup_new_sgf'); |
| 64 | } |
| 65 | } |