Example #1
0
 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!'));
     }
 }