public function add($name) { $this->view = null; try { $user = User::find(Session::uid()); if (!$user->getId() || !$user->getIs_admin()) { throw new Exception('Action not allowed.'); } if (!preg_match('/^\\d*[-a-zA-Z][-a-zA-Z0-9]*$/', $name)) { throw new Exception('The name of the project can only contain alphanumeric characters plus dashes and must have 1 alpha character at least'); } try { $project = Project::find($name); } catch (Exception $e) { } if (is_object($project) && $project->getProjectId($name)) { throw new Exception('Project with the same name already exists!'); } $file = new File(); $logo = ''; if (!empty($_POST['logo'])) { $file->findFileById($_POST['logo']); $logo = basename($file->getUrl()); } $project = new Project(); $project->setName($name); $project->setDescription($_POST['description']); $project->setWebsite($_POST['website']); $project->setContactInfo($user->getUsername()); $project->setOwnerId($user->getId()); $project->setActive(true); $project->setInternal(true); $project->setRequireSandbox(true); $project->setLogo($logo); $project->setRepo_type('git'); $project->setRepository($_POST['github_repo_url']); $project->setGithubId($_POST['github_client_id']); $project->setGithubSecret($_POST['github_client_secret']); $project->save(); if ($file->getId()) { $file->setProjectId($project->getProjectId()); $file->save(); } $journal_message = '@' . $user->getNickname() . ' added project *' . $name . '*'; Utils::systemNotification($journal_message); echo json_encode(array('success' => true, 'message' => $journal_message)); } catch (Exception $e) { $error = $e->getMessage(); echo json_encode(array('success' => false, 'message' => $error)); } }
public function add($reference = '', $isW9 = false) { try { $user = User::find(Session::uid()); if (!$user->getId()) { return $this->setOutput(array('success' => false, 'message' => 'Not enough rights!')); } // Upload data can be POST'ed as raw form data or uploaded via <iframe> and <form> // using regular multipart/form-data enctype (which is handled by PHP $_FILES). if (!empty($_FILES['fd-file']) and is_uploaded_file($_FILES['fd-file']['tmp_name'])) { // Regular multipart/form-data upload. $name = $_FILES['fd-file']['name']; $source = fopen($_FILES['fd-file']['tmp_name'], 'r'); $ext = end(explode(".", $name)); $fileName = File::uniqueFilename($ext); } else { // Raw POST data. $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); $source = fopen('php://input', 'r'); $ext = end(explode(".", $name)); $fileName = File::uniqueFilename($ext); } $path = UPLOAD_PATH . '/' . $fileName; $dest = fopen($path, 'w'); while (!feof($source)) { $chunk = fread($source, 1024); fwrite($dest, $chunk); } fclose($source); fclose($dest); $finfo = new finfo(FILEINFO_MIME_TYPE); $mime = $finfo->file($path); $title = basename($name); $url = SERVER_URL . 'uploads/' . $fileName; $workitem = is_numeric($reference) ? (int) $reference : null; $projectid = null; if (is_null($workitem) && strlen(trim($reference))) { $project = new Project(); if ($project->loadByName(trim($reference))) { $projectid = $project->getProjectId(); } } $file = new File(); $file->setMime($mime)->setUserid($_SESSION['userid'])->setWorkitem($workitem)->setProjectId($projectid)->setTitle($title)->setUrl($url); $success = $file->save(); $icon = File::getIconFromMime($file->getMime()); if ($icon === false) { $filetype = 'image'; $icon = 'images/icons/default.png'; } if ($workitem) { $workitem_attached = new WorkItem(); $workitem_attached->loadById($workitem); $journal_message = '@' . $user->getNickname() . ' uploaded an [attachment](' . $file->getUrl() . ') to #' . $workitem; Utils::systemNotification($journal_message); } $isW9 = (bool) $isW9; if ($isW9) { Notification::sendW9Request($user, $file->getUrl()); $user->setW9_status('pending-approval'); $user->save(); } return $this->setOutput(array('success' => true, 'fileid' => $file->getId(), 'url' => $file->getUrl(), 'icon' => $icon, 'title' => $file->getTitle(), 'description' => '', 'filetype' => isset($filetype) ? $filetype : '', 'can_delete' => $isW9 ? false : true)); } catch (Exception $e) { error_log($e->getMessage()); return $this->setOutput(array('success' => false, 'message' => 'An error occured while uploading to ' . $path . ' please try again!')); } }