/**
  * Upload single file
  *
  * @param void
  * @return null
  */
 function upload_single()
 {
     if ($this->request->isSubmitted()) {
         if (!File::canAdd($this->logged_user, $this->active_project)) {
             if ($this->request->isApiCall()) {
                 $this->httpError(HTTP_ERR_FORBIDDEN, null, true, true);
             } else {
                 die('error - upload not permitted');
             }
             // if
         }
         // if
         $file_data = $this->request->post('file');
         if (!is_array($file_data)) {
             $file_data = array('milestone_id' => $this->request->get('milestone_id'), 'visibility' => $this->active_project->getDefaultVisibility());
             if (instance_of($this->active_category, 'Category')) {
                 $file_data['parent_id'] = $this->active_category->getId();
             }
             // if
         }
         // if
         $this->smarty->assign('file_data', $file_data);
         if ($this->request->isSubmitted()) {
             db_begin_work();
             $this->active_file = new File();
             $attached = attach_from_files($this->active_file, $this->logged_user);
             // Do we have an upload error?
             if (is_error($attached) || $attached != 1) {
                 if ($this->request->isApiCall()) {
                     $this->serveData(is_error($attached) ? $attached : new Error('0 files uploaded'));
                 } else {
                     die('error - nothing uploaded');
                 }
                 // if
             }
             // if
             $this->active_file->setAttributes($file_data);
             if ($this->active_file->getName() == '') {
                 $this->active_file->setName($this->active_file->pending_files[0]['name']);
             }
             // if
             $this->active_file->setRevision(1);
             $this->active_file->setProjectId($this->active_project->getId());
             if (trim($this->active_file->getCreatedByName()) == '' || trim($this->active_file->getCreatedByEmail()) == '') {
                 $this->active_file->setCreatedBy($this->logged_user);
             }
             // if
             $this->active_file->setState(STATE_VISIBLE);
             $save = $this->active_file->save();
             if ($save && !is_error($save)) {
                 if ($this->active_file->countRevisions() > 0) {
                     $subscribers = array($this->logged_user->getId());
                     if (is_foreachable($this->request->post('notify_users'))) {
                         $subscribers = array_merge($subscribers, $this->request->post('notify_users'));
                     } else {
                         $subscribers[] = $this->active_project->getLeaderId();
                     }
                     // if
                     if (!in_array($this->active_project->getLeaderId(), $subscribers)) {
                         $subscribers[] = $this->active_project->getLeaderId();
                     }
                     // if
                     Subscriptions::subscribeUsers($subscribers, $this->active_file);
                     db_commit();
                     $this->active_file->ready();
                     if ($this->request->isApiCall()) {
                         $this->serveData($this->active_file, 'file');
                     } else {
                         die('success');
                         // async
                     }
                     // if
                 } else {
                     if ($this->request->isApiCall()) {
                         $this->httpError(HTTP_ERR_OPERATION_FAILED, null, true, true);
                     } else {
                         die('error - unable to attach file');
                     }
                     // if
                 }
                 // if
             } else {
                 if ($this->request->isApiCall()) {
                     $this->serveData($save);
                 } else {
                     die('error - could not save file object');
                     // async
                 }
                 // if
             }
             // if
         }
         // if
     } else {
         if ($this->request->isApiCall()) {
             $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true);
         } else {
             die('error - request is not POST request');
             // async
         }
         // if
     }
     // if
 }
Example #2
0
 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));
     }
 }