public function ajaxSubmitAction() { // 获取参数 $globalId = (int) Request::getPOST('global-id'); $title = trim(Request::getPOST('title')); $source = trim(Request::getPOST('source')); $timeLimit = max(1, Request::getPOST('time-limit')); $memoryLimit = max(32, Request::getPOST('memory-limit')); $description = Request::getPOST('description', '', true); $input = Request::getPOST('input', '', true); $output = Request::getPOST('output', '', true); $sampleInput = Request::getPOST('sample-input', '', true); $sampleOutput = Request::getPOST('sample-output', '', true); $hint = Request::getPOST('hint', '', true); if (!in_array($timeLimit, StatusVars::$TIME_LIMIT) || !in_array($memoryLimit, StatusVars::$MEMORY_LIMIT)) { $this->renderError('参数错误!'); } // 校验 if (mb_strlen($title, 'utf8') > 50) { $this->renderError('标题限制50个字以内!'); } if (mb_strlen($source, 'utf8') > 50) { $this->renderError('来源限制50个字以内!'); } // 校验problem $problemInfo = OjProblemInterface::getById(array('id' => $globalId)); if (empty($problemInfo) || !$problemInfo['hidden'] || $problemInfo['user_id'] != $this->loginUserInfo['id']) { $this->renderError('你没有操作权限!'); } // 更新 $data = array('id' => $globalId, 'title' => $title, 'source' => str_replace(',', ',', $source), 'time_limit' => $timeLimit * 1000, 'memory_limit' => $memoryLimit * 1024, 'description' => $description, 'input' => $input, 'output' => $output, 'sample_input' => $sampleInput, 'sample_output' => $sampleOutput, 'hint' => $hint); OjProblemInterface::save($data); // 记录编辑历史 if ($problemInfo['remote'] == StatusVars::REMOTE_HQU) { $dateTime = date('Y-m-d H:i:s', time()); $history = "<p>{$dateTime} 用户{$this->loginUserInfo['username']}编辑了题目</p>"; OjProblemInterface::auditHistory(array('problem_id' => $problemInfo['problem_id'], 'append_history' => $history)); } $this->setNotice(FrameworkVars::NOTICE_SUCCESS, '编辑成功!'); $this->renderAjax(0); }
public function ajaxUploadAction() { $problemId = Request::getPOST('problem-id'); $fileList = Upload::getFileList('file'); if (empty($fileList) || empty($problemId)) { $this->echoJson(1, '参数错误!'); } // 校验权限 $problemInfo = OjProblemInterface::getDetail(array('remote' => StatusVars::REMOTE_HQU, 'problem_id' => $problemId)); if (empty($problemInfo)) { $this->echoJson(1, '题目不存在!'); } // 获取upload所有文件,并计算大小 $dataUploadDir = '/home/judge/data/' . $problemId . '/upload/'; if (false == ($handle = opendir($dataUploadDir))) { $this->echoJson(1, '打开upload文件夹失败!'); } $uploadFileSize = 0; while (false !== ($filename = readdir($handle))) { $file = $dataUploadDir . '/' . $filename; if ($filename[0] == '.' || is_dir($file)) { continue; } $uploadFileSize += filesize($file); } closedir($handle); foreach ($fileList as $key => $fileInfo) { if (!preg_match('/^[A-Za-z0-9_\\.]{1,14}$/', $fileInfo['name'])) { $this->echoJson(1, '文件名必须是字母数字下划线组成,并且不能超过14个字符!'); return; } $uploadFileSize += $fileInfo['size']; } if ($uploadFileSize > 5 * 1024 * 1024) { $this->echoJson(1, '上传目录空间限制5M,你已经超出范围啦!'); } // 移动 foreach ($fileList as $key => $fileInfo) { $path = '/home/judge/data/' . $problemId . '/upload/' . $fileInfo['name']; move_uploaded_file($fileInfo['tmp_name'], $path); } // 重新获取upload文件夹 if (false == ($handle = opendir($dataUploadDir))) { $this->echoJson(1, '打开upload文件夹失败!'); } $uploadFileList = array(); while (false !== ($filename = readdir($handle))) { $file = $dataUploadDir . '/' . $filename; if ($filename[0] == '.' || is_dir($file)) { continue; } $fileInfo = array(); $fileInfo['filename'] = $filename; //文件名,包含扩展名 $fileInfo['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间 $fileInfo['filesize'] = filesize($file); $uploadFileList[] = $fileInfo; } closedir($handle); // 排序 function cmpFunc($a, $b) { return strcmp($a['filename'], $b['filename']); } usort($uploadFileList, 'cmpFunc'); // 计算大小,并格式化 $uploadFileSize = 0; foreach ($uploadFileList as $key => $fileInfo) { $uploadFileList[$key]['format'] = sprintf('%-14s %10d %s', $fileInfo['filename'], $fileInfo['filesize'], $fileInfo['datetime']); $uploadFileList[$key]['format'] = str_replace(' ', ' ', $uploadFileList[$key]['format']); $uploadFileSize += $uploadFileList[$key]['filesize']; } // 记录操作 $dateTime = date('Y-m-d H:i:s', time()); $history = "<p>{$dateTime} 管理员{$this->loginUserInfo['username']}批量上传了文件</p>"; OjProblemInterface::auditHistory(array('problem_id' => $problemId, 'append_history' => $history)); $this->echoJson(0, 'success', array('uploadFileList' => $uploadFileList, 'uploadFileSize' => $uploadFileSize)); }