public function newServerFileAction()
 {
     if ($this->request->isPost() && $this->request->isAjax()) {
         $data = array();
         $filename = $this->request->getPost("filename");
         if (!Phalcon\Text::endsWith($filename, ".java")) {
             $filename .= ".java";
         }
         $team_id = $this->request->getPost("team_id");
         $cond = "friendlyName = :friendlyName: AND team_id = :team_id:";
         $para = array("friendlyName" => $filename, "team_id" => $team_id);
         $serverFile = ServerFiles::findFirst(array($cond, "bind" => $para));
         if ($serverFile) {
             $data["success"] = false;
             $data["message"] = "You already have a file with that name";
             echo json_encode($data);
             return;
         }
         $name = Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
         $serverFile = new ServerFiles();
         $serverFile->setTeam(Teams::findFirst($team_id));
         $serverFile->setFriendlyName($filename);
         $serverFile->setPath("../app/data/{$name}");
         $serverFile->setLastSave($this->request->getPost("lastSave"));
         if ($serverFile->save() && fopen($serverFile->getPath(), "w")) {
             $data["success"] = true;
             $data["friendlyName"] = $serverFile->getFriendlyName();
         } else {
             $serverFile->delete();
             $data["success"] = false;
             $data["message"] = "There was an error saving your file. Please try again.";
             echo json_encode($data);
             return;
         }
         echo json_encode($data);
         return;
     }
 }
<?php

echo Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM);
//"aloiwkqz"
 public function problemsAction()
 {
     if (!$this->checkLogin()) {
         return;
     }
     if ($this->request->isPost() && $this->request->hasPost("type") && $this->security->checkToken()) {
         $this->session->set("changeOccurred", true);
         $this->session->set("changeSuccessful", true);
         $this->session->set("teamsGenerated", false);
         switch ($this->request->getPost("type")) {
             case 'update':
                 $problem = Problems::findFirst(intval($this->request->getPost("id")));
                 if ($problem) {
                     $problem->setName($this->request->getPost("name"));
                     $problem->setRuntime($this->request->getPost("runtime"));
                     if ($this->request->hasPost("hasDataFile")) {
                         if ($this->request->hasFiles()) {
                             // Adding data file
                             foreach ($this->request->getUploadedFiles() as $file) {
                                 if ($file->getKey() == "dataFile") {
                                     unlink($problem->getDataFile());
                                     $dataFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                                     $problem->setDataFile($dataFileLocation);
                                     $problem->setDataFileFriendly($file->getName());
                                     $file->moveTo($dataFileLocation);
                                 }
                             }
                         }
                     } else {
                         if ($problem->getDataFile() != null) {
                             // Removing data file
                             unlink($problem->getDataFile());
                             $problem->setDataFile(null);
                             $problem->setDataFileFriendly(null);
                         }
                     }
                     if ($this->request->hasPost("hasJudgeFile")) {
                         if ($this->request->hasFiles()) {
                             // Adding judge file
                             foreach ($this->request->getUploadedFiles() as $file) {
                                 if ($file->getKey() == "judgeFile") {
                                     unlink($problem->getJudgeFile());
                                     $judgeFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                                     $problem->setJudgeFile($judgeFileLocation);
                                     $problem->setJudgeFileFriendly($file->getName());
                                     $file->moveTo($judgeFileLocation);
                                 }
                             }
                         }
                     } else {
                         // Removing judge file
                         if ($problem->getJudgeFile() != null) {
                             unlink($problem->getJudgeFile());
                             $problem->setJudgeFile(null);
                             $problem->setJudgeFileFriendly(null);
                         }
                     }
                     if (!$problem->save()) {
                         $this->session->set("changeSuccessful", false);
                     }
                 } else {
                     $this->session->set("changeSuccessful", false);
                 }
                 break;
             case 'create':
                 $error = false;
                 $problem = new Problems();
                 $problem->setName($this->request->getPost("name"));
                 $problem->setRuntime($this->request->getPost("runtime"));
                 if ($this->request->hasFiles()) {
                     foreach ($this->request->getUploadedFiles() as $file) {
                         if ($file->getKey() == "dataFile" && $this->request->hasPost("hasDataFile")) {
                             $dataFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                             $problem->setDataFile($dataFileLocation);
                             $problem->setDataFileFriendly($file->getName());
                             $file->moveTo($dataFileLocation);
                         }
                         if ($file->getKey() == "judgeFile" && $this->request->hasPost("hasJudgeFile")) {
                             $judgeFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                             $problem->setJudgeFile($judgeFileLocation);
                             $problem->setJudgeFileFriendly($file->getName());
                             $file->moveTo($judgeFileLocation);
                         }
                     }
                 }
                 if ($error || !$problem->save()) {
                     $this->session->set("changeSuccessful", false);
                 }
                 break;
             case 'delete':
                 $problem = Problems::findFirst(intval($this->request->getPost("id")));
                 $dataLocation = $problem->getDataFile();
                 $judgeLocation = $problem->getJudgeFile();
                 if ($problem->delete()) {
                     unlink($dataLocation);
                     unlink($judgeLocation);
                 } else {
                     $this->session->set("changeSuccessful", false);
                 }
                 break;
             default:
                 $this->session->set("changeSuccessful", false);
                 break;
         }
         return $this->response->redirect("/admin/problems");
     } else {
         if ($this->session->has("changeOccurred")) {
             $this->view->changeOccurred = $this->session->get("changeOccurred");
             $this->session->remove("changeOccurred");
         }
         if ($this->session->has("changeSuccessful")) {
             $this->view->changeSuccessful = $this->session->get("changeSuccessful");
             $this->session->remove("changeSuccessful");
         }
     }
     $this->view->problems = Problems::find();
 }