Exemplo n.º 1
0
/**
 * Creates attachment from uploaded file
 *
 * @param array $file
 * @param ApplicationObject $parent
 * @return Attachment
 */
function &make_attachment($file, $parent = null)
{
    if (!isset($file) || !isset($file['tmp_name'])) {
        return new Error(lang('File is not uploaded'));
    }
    // if
    $destination_file = get_available_uploads_filename();
    if (!move_uploaded_file($file['tmp_name'], $destination_file)) {
        return new Error(lang('Could not move uploaded file to uploads directory'));
    }
    // if
    $attachment = new Attachment();
    $attachment->setName($file['name']);
    $attachment->setLocation(basename($destination_file));
    $attachment->setMimeType(array_var($file, 'type', 'application/octet-stream'));
    $attachment->setSize(array_var($file, 'size', 0));
    if (instance_of($parent, 'ApplicationObject')) {
        $attachment->setParent($parent);
    }
    // if
    $save = $attachment->save();
    if (!$save || is_error($save)) {
        @unlink($destination_file);
        return $save;
    }
    // if
    return $attachment;
}
 /**
  * Clone attachments
  *
  * @param ProjectObject $from
  * @param ProjectObject $to
  * @return boolean
  */
 function cloneAttachments($original, $copy)
 {
     $attachments = Attachments::findByObject($original);
     if (is_foreachable($attachments)) {
         $new_files = array();
         $to_insert = array();
         foreach ($attachments as $attachment) {
             $source_file = $attachment->getFilePath();
             $target_file = get_available_uploads_filename();
             if (copy($source_file, $target_file)) {
                 $new_files[] = $target_file;
                 $to_insert[] = db_prepare_string("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($copy->getId(), get_class($copy), $attachment->getName(), $attachment->getMimeType(), $attachment->getSize(), basename($target_file), $attachment->getAttachmentType(), $attachment->getCreatedOn(), $attachment->getCreatedById(), $attachment->getCreatedByName(), $attachment->getCreatedByEmail()));
             }
             // if
         }
         // foreach
         if (is_foreachable($to_insert)) {
             $insert = db_execute("INSERT INTO " . TABLE_PREFIX . 'attachments (parent_id, parent_type, name, mime_type, size, location, attachment_type, created_on, created_by_id, created_by_name, created_by_email) VALUES ' . implode(', ', $to_insert));
             if ($insert && !is_error($insert)) {
                 return true;
             } else {
                 foreach ($new_files as $new_file) {
                     @unlink($new_file);
                 }
                 // if
                 return $insert;
             }
             // if
         }
         // if
     }
     // if
     return true;
 }
 /**
  * Attach uploaded file
  * 
  * $file is a single element of $_FILES auto global array
  *
  * @param array $file
  * @param User $user
  * @return boolean
  */
 function attachUploadedFile($file, $user)
 {
     if (is_array($file)) {
         if (isset($file['error']) && $file['error'] > 0) {
             use_error('UploadError');
             return new UploadError($file['error']);
         }
         // if
         $destination_file = get_available_uploads_filename();
         if (move_uploaded_file($file['tmp_name'], $destination_file)) {
             if (FIX_UPLOAD_PERMISSION !== false) {
                 @chmod($destination_file, FIX_UPLOAD_PERMISSION);
             }
             // if
             return $this->addPendingFile($destination_file, array_var($file, 'name'), array_var($file, 'type'), array_var($file, 'size'), $user);
         }
         // if
     }
     // if
     return false;
 }
 /**
  * Provides logic for image picker dialog
  * 
  * @param void
  * @return null
  */
 function image_picker()
 {
     $project_id = $this->request->get('project_id');
     if ($project_id) {
         $this->active_project = Projects::findById($project_id);
     }
     // if
     if (!instance_of($this->active_project, 'Project')) {
         $this->active_project = new Project();
     }
     // if
     $image_picker_url = assemble_url('image_picker', array('project_id' => $this->active_project->getId()));
     $this->smarty->assign(array('image_picker_url' => $image_picker_url, 'disable_upload' => (bool) $this->request->get('disable_upload')));
     if ($this->request->isSubmitted()) {
         $action = $this->request->post('widget_action');
         switch ($action) {
             case 'upload':
                 // check if any file is uploaded
                 $uploaded_file = array_var($_FILES, 'image', null);
                 if (!is_array($uploaded_file)) {
                     $this->httpError(HTTP_ERR_OPERATION_FAILED, lang('You did not uploaded any file'), true, true);
                 }
                 // if
                 // we are setting base attributes
                 $attachment = new Attachment();
                 $attachment->setName($uploaded_file['name']);
                 $attachment->setMimeType($uploaded_file['type']);
                 $attachment->setSize($uploaded_file['size']);
                 $attachment->setAttachmentType(ATTACHMENT_TYPE_ATTACHMENT);
                 $attachment->setCreatedBy($this->logged_user);
                 $attachment->setCreatedOn(new DateTimeValue());
                 // check if uploaded file is image
                 if (!$attachment->isImage()) {
                     $this->httpError(HTTP_ERR_OPERATION_FAILED, lang('Uploaded file is not image'), true, true);
                 }
                 // if
                 $destination_file = get_available_uploads_filename();
                 if (!move_uploaded_file($uploaded_file['tmp_name'], $destination_file)) {
                     $this->httpError(HTTP_ERR_OPERATION_FAILED, lang('Could not copy uploaded image to work folder'), true, true);
                 }
                 // if
                 if (FIX_UPLOAD_PERMISSION !== false) {
                     @chmod($destination_file, FIX_UPLOAD_PERMISSION);
                 }
                 // if
                 $attachment->setLocation(basename($destination_file));
                 $save = $attachment->save();
                 if (!$save || is_error($save)) {
                     @unlink($destination_file);
                     $this->httpError(HTTP_ERR_OPERATION_FAILED, $save->getMessage(), true, true);
                 }
                 // if
                 echo "<img attachment_id='" . $attachment->getId() . "' src='" . $attachment->getViewUrl($this->active_project->getId()) . "' />";
                 die;
                 break;
             default:
                 break;
         }
         // switch
     }
     // if
 }
/**
 * Hnalde on_project_object_copied event
 *
 * @param ProjectObject $original
 * @param ProjectObject $copy
 * @param Project $destination
 * @param mixed $cascade
 * @return null
 */
function resources_handle_on_project_object_copied(&$original, &$copy, &$destination, $cascade)
{
    if ($original->can_have_subscribers) {
        $subscribers = $original->getSubscribers();
        if (is_foreachable($subscribers)) {
            foreach ($subscribers as $subscriber) {
                if ($subscriber->isProjectMember($destination)) {
                    Subscriptions::subscribe($subscriber, $copy);
                }
                // if
            }
            // foreach
        }
        // if
    }
    // if
    if ($original->can_have_assignees) {
        Assignments::cloneAssignments($original, $copy);
    }
    // if
    if ($original->can_have_attachments) {
        Attachments::cloneAttachments($original, $copy);
    }
    // if
    // Copy child objects
    if ($cascade === true || is_foreachable($cascade)) {
        if ($cascade === true) {
            $rows = db_execute_all('SELECT * FROM ' . TABLE_PREFIX . 'project_objects WHERE parent_id = ?', $original->getId());
        } else {
            $rows = db_execute_all('SELECT * FROM ' . TABLE_PREFIX . 'project_objects WHERE parent_id = ? AND type IN (?)', $original->getId(), $cascade);
        }
        // if
        if (is_foreachable($rows)) {
            // We'll remember original and copy tasks ID-s here so we can move
            // assignments later on, when we have both instances
            $tasks = array();
            foreach ($rows as $row) {
                $subobject_original_id = $row['id'];
                $subobject_original_type = strtolower($row['type']);
                unset($row['id']);
                $row['project_id'] = $destination->getId();
                $row['parent_id'] = $copy->getId();
                $row['milestone_id'] = 0;
                // Copy file
                if ($subobject_original_type == 'attachment') {
                    $path = UPLOAD_PATH . '/' . $row['varchar_field_1'];
                    if (is_file($path)) {
                        $destination_file = get_available_uploads_filename();
                        if (copy($path, $destination_file)) {
                            $row['varchar_field_1'] = basename($destination_file);
                        }
                        // if
                    }
                    // if
                }
                // if
                // Escape values
                foreach ($row as $k => $v) {
                    $row[$k] = db_escape($v);
                }
                // foreach
                db_execute('INSERT INTO ' . TABLE_PREFIX . 'project_objects (' . implode(', ', array_keys($row)) . ') VALUES (' . implode(', ', $row) . ')');
                if ($subobject_original_type == 'task') {
                    $tasks[$subobject_original_id] = db_last_insert_id();
                }
                // if
            }
            // foraech
            if (instance_of($copy, 'Discussion')) {
                $last_comment = $copy->getLastComment();
                $last_comment_datetime = instance_of($last_comment, 'Comment') ? $last_comment->getCreatedOn() : null;
                $copy->setLastCommentOn($last_comment_datetime);
                $copy->save();
            }
            // if
            // Lets move task assinments if we have any tasks
            if (is_foreachable($tasks)) {
                foreach ($tasks as $task_original_id => $task_copy_id) {
                    $task_original = Tasks::findById($task_original_id);
                    $task_copy = Tasks::findById($task_copy_id);
                    if (instance_of($task_original, 'Task') && instance_of($task_copy, 'Task')) {
                        Assignments::cloneAssignments($task_original, $task_copy);
                        Subscriptions::cloneSubscriptions($task_original, $task_copy);
                    }
                    // if
                }
                // foreach
            }
            // if
        }
        // if
    }
    // if
}
 /**
  * Upload file document page action
  * 
  * @param void
  * @return void
  */
 function upload_file()
 {
     $this->wireframe->print_button = false;
     if (!Document::canAdd($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $file = $_FILES['file'];
     $file_data = $this->request->post('file');
     if (!is_array($file_data)) {
         $file_data = array('category_id' => $this->active_document_category->getId());
     }
     // if
     require_once SMARTY_PATH . '/plugins/modifier.filesize.php';
     $this->smarty->assign(array('file_data' => $file_data, 'max_upload_size' => smarty_modifier_filesize(get_max_upload_size())));
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $this->active_document->setAttributes($file_data);
         if (is_array($file)) {
             $destination_file = get_available_uploads_filename();
             if (move_uploaded_file($file['tmp_name'], $destination_file)) {
                 if (FIX_UPLOAD_PERMISSION !== false) {
                     @chmod($destination_file, FIX_UPLOAD_PERMISSION);
                 }
                 // if
                 $this->active_document->setName($file['name']);
                 $this->active_document->setBody(basename($destination_file));
                 $this->active_document->setMimeType($file['type']);
             }
             // if
         }
         // if
         $this->active_document->setCreatedBy($this->logged_user);
         $this->active_document->setType('file');
         $save = $this->active_document->save();
         if ($save && !is_error($save)) {
             $notify_user_ids = $this->request->post('notify_users');
             if (is_foreachable($notify_user_ids)) {
                 $notify_users = Users::findByIds($notify_user_ids);
                 $owner_company = get_owner_company();
                 if (is_foreachable($notify_users)) {
                     ApplicationMailer::send($notify_users, 'documents/new_upload_file_document', array('document_name' => $this->active_document->getName(), 'created_by_name' => $this->active_document->getCreatedByName(), 'created_by_url' => $this->logged_user->getViewUrl(), 'document_url' => $this->active_document->getViewUrl(), 'owner_company_name' => $owner_company->getName()), $this->active_document);
                 }
                 // if
             }
             // if
             db_commit();
             flash_success('Document ":document_name" has been uploaded', array('document_name' => $this->active_document->getName()));
             $this->redirectTo('documents');
         } else {
             db_rollback();
             $this->smarty->assign('errors', $save);
         }
         // if
     }
     // if
 }