/**
  * the singleton pattern
  *
  * @return Tinebase_FileSystem_RecordAttachments
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Tinebase_FileSystem_RecordAttachments();
     }
     return self::$_instance;
 }
 public function testCreate()
 {
     $event = $this->getTestEvent();
     $persistentEvent = $this->_uit->create($event);
     Tinebase_FileSystem_RecordAttachments::getInstance()->addRecordAttachment($persistentEvent, 'agenda.txt', fopen('php://temp', 'rw'));
     Tinebase_FileSystem_RecordAttachments::getInstance()->addRecordAttachment($persistentEvent->exdate[0], 'exception.txt', fopen('php://temp', 'rw'));
     Tinebase_FileSystem_RecordAttachments::getInstance()->getMultipleAttachmentsOfRecords($persistentEvent);
     Tinebase_FileSystem_RecordAttachments::getInstance()->getMultipleAttachmentsOfRecords($persistentEvent->exdate[0]);
     $this->_assertTestEvent($persistentEvent);
     return $persistentEvent;
 }
 /**
  * import one purchase imvoice file
  * 
  * a new invoice with default data will be created and the invoice file will be attached
  * 
  * @param string $name  name of the invoice
  * @param resource $data  binary data of the invoice (aka a pdf)
  * @throws Sabre\DAV\Exception\Forbidden
  * @return Sales_Model_PurchaseInvoice
  */
 public function importPurchaseInvoice($name, $data)
 {
     // create invoice
     $purchaseInvoice = new Sales_Model_PurchaseInvoice(array('number' => '', 'description' => '', 'date' => Tinebase_DateTime::now(), 'discount' => 0, 'due_in' => 0, 'due_at' => Tinebase_DateTime::now(), 'price_gross' => 0, 'price_net' => 0, 'price_tax' => 0, 'sales_tax' => 0));
     $invoice = $this->create($purchaseInvoice);
     // attach invoice file (aka a pdf)
     $attachmentPath = Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachmentPath($purchaseInvoice, TRUE);
     $handle = Tinebase_FileSystem::getInstance()->fopen($attachmentPath . '/' . $name, 'w');
     if (!is_resource($handle)) {
         throw new Sabre\DAV\Exception\Forbidden('Permission denied to create file:' . $attachmentPath . '/' . $name);
     }
     if (is_resource($data)) {
         stream_copy_to_stream($data, $handle);
     }
     Tinebase_FileSystem::getInstance()->fclose($handle);
     return $this->get($purchaseInvoice);
 }
コード例 #4
0
 /**
  * testCreateLeadWithAttachment
  * 
  * @see 0005024: allow to attach external files to records
  */
 public function testCreateLeadWithAttachment()
 {
     $tempFileBackend = new Tinebase_TempFile();
     $tempFile = $tempFileBackend->createTempFile(dirname(dirname(__FILE__)) . '/Filemanager/files/test.txt');
     $lead = $this->_getLead()->toArray();
     $lead['attachments'] = array(array('tempFile' => $tempFile->toArray()));
     $savedLead = $this->_instance->saveLead($lead);
     // add path to files to remove
     $this->_objects['paths'][] = Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachmentPath(new Crm_Model_Lead($savedLead, TRUE)) . '/' . $tempFile->name;
     $this->assertTrue(isset($savedLead['attachments']), 'no attachments found');
     $this->assertEquals(1, count($savedLead['attachments']));
     $attachment = $savedLead['attachments'][0];
     $this->assertEquals('text/plain', $attachment['contenttype'], print_r($attachment, TRUE));
     $this->assertEquals(17, $attachment['size']);
     $this->assertTrue(is_array($attachment['created_by']), 'user not resolved: ' . print_r($attachment['created_by'], TRUE));
     $this->assertEquals(Tinebase_Core::getUser()->accountFullName, $attachment['created_by']['accountFullName'], 'user not resolved: ' . print_r($attachment['created_by'], TRUE));
     return $savedLead;
 }
コード例 #5
0
 /**
  * download file attachment
  * 
  * @param string $nodeId
  * @param string $recordId
  * @param string $modelName
  */
 public function downloadRecordAttachment($nodeId, $recordId, $modelName)
 {
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Downloading attachment of ' . $modelName . ' record with id ' . $recordId);
     }
     $recordController = Tinebase_Core::getApplicationInstance($modelName);
     $record = $recordController->get($recordId);
     $node = Tinebase_FileSystem::getInstance()->get($nodeId);
     $path = Tinebase_Model_Tree_Node_Path::STREAMWRAPPERPREFIX . Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachmentPath($record) . '/' . $node->name;
     $this->_downloadFileNode($node, $path);
     exit;
 }
 protected function _resolveData($events)
 {
     $eventSet = $events instanceof Tinebase_Record_RecordSet ? $events->getClone(true) : new Tinebase_Record_RecordSet('Calendar_Model_Event', array($events));
     // get recur exceptions
     foreach ($eventSet as $event) {
         if ($event->rrule && !$event->exdate instanceof Tinebase_Record_RecordSet) {
             $exdates = $this->_eventController->getRecurExceptions($event, TRUE, $this->getEventFilter());
             $event->exdate = $exdates;
             $eventSet->merge($exdates);
         }
     }
     $this->_eventController->getAlarms($eventSet);
     Tinebase_FileSystem_RecordAttachments::getInstance()->getMultipleAttachmentsOfRecords($eventSet);
 }
コード例 #7
0
 /**
  * converts Tinebase_Record_RecordSet to external format
  * 
  * @param Tinebase_Record_RecordSet         $_records
  * @param Tinebase_Model_Filter_FilterGroup $_filter
  * @param Tinebase_Model_Pagination         $_pagination
  *
  * @return mixed
  */
 public function fromTine20RecordSet(Tinebase_Record_RecordSet $_records = NULL, $_filter = NULL, $_pagination = NULL)
 {
     if (count($_records) == 0) {
         return array();
     }
     Tinebase_Notes::getInstance()->getMultipleNotesOfRecords($_records);
     Tinebase_Tags::getInstance()->getMultipleTagsOfRecords($_records);
     if (Tinebase_Core::isFilesystemAvailable()) {
         Tinebase_FileSystem_RecordAttachments::getInstance()->getMultipleAttachmentsOfRecords($_records);
     }
     Calendar_Model_Attender::resolveAttendee($_records->attendee, TRUE, $_records);
     Calendar_Convert_Event_Json::resolveRrule($_records);
     Calendar_Controller_Event::getInstance()->getAlarms($_records);
     Calendar_Convert_Event_Json::resolveGrantsOfExternalOrganizers($_records);
     Calendar_Model_Rrule::mergeAndRemoveNonMatchingRecurrences($_records, $_filter);
     $_records->sortByPagination($_pagination);
     Tinebase_Frontend_Json_Abstract::resolveContainersAndTags($_records, array('container_id'));
     $_records->setTimezone(Tinebase_Core::getUserTimezone());
     $_records->convertDates = true;
     $eventsData = $_records->toArray();
     foreach ($eventsData as $idx => $eventData) {
         if (!(isset($eventData[Tinebase_Model_Grants::GRANT_READ]) || array_key_exists(Tinebase_Model_Grants::GRANT_READ, $eventData)) || !$eventData[Tinebase_Model_Grants::GRANT_READ]) {
             $eventsData[$idx] = array_intersect_key($eventsData[$idx], array_flip(array('id', 'dtstart', 'dtend', 'transp', 'is_all_day_event')));
         }
     }
     return $eventsData;
 }
 /**
  * create event with attachment
  *
  * @return multitype:Ambigous <Calendar_Frontend_WebDAV_Event, Calendar_Frontend_WebDAV_Event> Ambigous <Tinebase_Model_Tree_Node, Tinebase_Record_Interface, Tinebase_Record_Abstract, NULL, unknown>
  */
 public function createEventWithAttachment($count = 1)
 {
     $event = $this->testCreateRepeatingEvent();
     for ($i = 1; $i <= $count; $i++) {
         $suffix = $i > 1 ? $i : '';
         $agenda = fopen("php://temp", 'r+');
         fputs($agenda, "HELLO WORLD{$suffix}");
         rewind($agenda);
         $attachmentController = Tinebase_FileSystem_RecordAttachments::getInstance();
         $attachmentNode = $attachmentController->addRecordAttachment($event->getRecord(), "agenda{$suffix}.html", $agenda);
     }
     $event = new Calendar_Frontend_WebDAV_Event($event->getContainer(), $event->getRecord()->getId());
     return $event;
 }
コード例 #9
0
 /**
  * delete linked objects (notes, relations, ...) of record
  *
  * @param Tinebase_Record_Interface $_record
  */
 protected function _deleteLinkedObjects(Tinebase_Record_Interface $_record)
 {
     // delete notes & relations
     if ($_record->has('notes')) {
         Tinebase_Notes::getInstance()->deleteNotesOfRecord($this->_modelName, $this->_getBackendType(), $_record->getId());
     }
     if ($_record->has('relations')) {
         $this->_deleteLinkedRelations($_record);
     }
     if ($_record->has('attachments') && Tinebase_Core::isFilesystemAvailable()) {
         Tinebase_FileSystem_RecordAttachments::getInstance()->deleteRecordAttachments($_record);
     }
 }
 /**
  * get event attachments
  * 
  * @param Calendar_Model_Event $_event
  * @return array of Zend_Mime_Part
  */
 protected function _getEventAttachments($_event)
 {
     $attachments = array();
     foreach ($_event->attachments as $attachment) {
         if ($attachment->size < self::INVITATION_ATTACHMENT_MAX_FILESIZE) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Adding attachment " . $attachment->name . ' to invitation mail');
             }
             $path = Tinebase_Model_Tree_Node_Path::STREAMWRAPPERPREFIX . Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachmentPath($_event) . '/' . $attachment->name;
             $handle = fopen($path, 'r');
             $stream = fopen("php://temp", 'r+');
             stream_copy_to_stream($handle, $stream);
             rewind($stream);
             $part = new Zend_Mime_Part($stream);
             $part->encoding = Zend_Mime::ENCODING_BASE64;
             // ?
             $part->filename = $attachment->name;
             $part->setTypeAndDispositionForAttachment($attachment->contenttype, $attachment->name);
             fclose($handle);
             $attachments[] = $part;
         } else {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Not adding attachment " . $attachment->name . ' to invitation mail (size: ' . Tinebase_Helper::convertToMegabytes($attachment - size) . ')');
             }
         }
     }
     return $attachments;
 }
コード例 #11
0
 /**
  * Creates a new file in the directory
  *
  * @param string $name Name of the file
  * @param resource|string $data Initial payload
  * @return null|string
  */
 function createFile($name, $data = null)
 {
     if (is_resource($data)) {
         // convert to rewindable stream
         $rewindableStream = fopen('php://temp', 'r+');
         stream_copy_to_stream($data, $rewindableStream);
         rewind($rewindableStream);
         $data = $rewindableStream;
     }
     Tinebase_FileSystem_RecordAttachments::getInstance()->addRecordAttachment($this->_record, $name, $data);
 }
コード例 #12
0
 /**
  * resolve multiple record fields (Tinebase_ModelConfiguration._recordsFields)
  * 
  * @param Tinebase_Record_RecordSet $_records
  * @param Tinebase_ModelConfiguration $modelConfiguration
  * @param boolean $multiple
  * @throws Tinebase_Exception_UnexpectedValue
  */
 protected function _resolveMultipleRecordFields(Tinebase_Record_RecordSet $_records, $modelConfiguration = NULL, $multiple = false)
 {
     if (!$modelConfiguration || !$_records->count()) {
         return;
     }
     if (!($resolveFields = $modelConfiguration->recordsFields)) {
         return;
     }
     $ownIds = $_records->{$modelConfiguration->idProperty};
     // iterate fields to resolve
     foreach ($resolveFields as $fieldKey => $c) {
         $config = $c['config'];
         // resolve records, if omitOnSearch is definitively set to FALSE (by default they won't be resolved on search)
         if ($multiple && !(isset($config['omitOnSearch']) && $config['omitOnSearch'] === FALSE)) {
             continue;
         }
         if (!isset($config['controllerClassName'])) {
             throw new Tinebase_Exception_UnexpectedValue('Controller class name needed');
         }
         // fetch the fields by the refIfField
         /** @var Tinebase_Controller_Record_Interface|Tinebase_Controller_SearchInterface $controller */
         /** @noinspection PhpUndefinedMethodInspection */
         $controller = $config['controllerClassName']::getInstance();
         $filterName = $config['filterClassName'];
         $filterArray = array();
         // addFilters can be added and must be added if the same model resides in more than one records fields
         if (isset($config['addFilters']) && is_array($config['addFilters'])) {
             $filterArray = $config['addFilters'];
         }
         /** @var Tinebase_Model_Filter_FilterGroup $filter */
         $filter = new $filterName($filterArray);
         $filter->addFilter(new Tinebase_Model_Filter_Id(array('field' => $config['refIdField'], 'operator' => 'in', 'value' => $ownIds)));
         $paging = NULL;
         if (isset($config['paging']) && is_array($config['paging'])) {
             $paging = new Tinebase_Model_Pagination($config['paging']);
         }
         $foreignRecords = $controller->search($filter, $paging);
         /** @var Tinebase_Record_Interface $foreignRecordClass */
         $foreignRecordClass = $foreignRecords->getRecordClassName();
         $foreignRecordModelConfiguration = $foreignRecordClass::getConfiguration();
         $foreignRecords->setTimezone(Tinebase_Core::getUserTimezone());
         $foreignRecords->convertDates = true;
         $fr = $foreignRecords->getFirstRecord();
         // @todo: resolve alarms?
         // @todo: use parts parameter?
         if ($foreignRecordModelConfiguration->resolveRelated && $fr) {
             if ($fr->has('notes')) {
                 Tinebase_Notes::getInstance()->getMultipleNotesOfRecords($foreignRecords);
             }
             if ($fr->has('tags')) {
                 Tinebase_Tags::getInstance()->getMultipleTagsOfRecords($foreignRecords);
             }
             if ($fr->has('relations')) {
                 $relations = Tinebase_Relations::getInstance()->getMultipleRelations($foreignRecordClass, 'Sql', $foreignRecords->{$fr->getIdProperty()});
                 $foreignRecords->setByIndices('relations', $relations);
             }
             if ($fr->has('customfields')) {
                 Tinebase_CustomField::getInstance()->resolveMultipleCustomfields($foreignRecords);
             }
             if ($fr->has('attachments') && Tinebase_Core::isFilesystemAvailable()) {
                 Tinebase_FileSystem_RecordAttachments::getInstance()->getMultipleAttachmentsOfRecords($foreignRecords);
             }
         }
         if ($foreignRecords->count() > 0) {
             /** @var Tinebase_Record_Interface $record */
             foreach ($_records as $record) {
                 $filtered = $foreignRecords->filter($config['refIdField'], $record->getId())->toArray();
                 $filtered = $this->_resolveAfterToArray($filtered, $foreignRecordModelConfiguration, TRUE);
                 $record->{$fieldKey} = $filtered;
             }
         } else {
             $_records->{$fieldKey} = NULL;
         }
     }
 }
 /**
  * test getting multiple attachments at once
  */
 public function testGetMultipleAttachmentsOfRecords()
 {
     $recordAttachments = Tinebase_FileSystem_RecordAttachments::getInstance();
     $records = new Tinebase_Record_RecordSet('Addressbook_Model_Contact');
     for ($i = 0; $i < 10; $i++) {
         $record = new Addressbook_Model_Contact(array('n_family' => Tinebase_Record_Abstract::generateUID()));
         $record->setId(Tinebase_Record_Abstract::generateUID());
         $recordAttachments->addRecordAttachment($record, $i . 'Test.txt', fopen(__FILE__, 'r'));
         $records->addRecord($record);
     }
     $recordAttachments->getMultipleAttachmentsOfRecords($records);
     foreach ($records as $records) {
         $this->assertEquals(1, $record->attachments->count(), 'Attachments missing');
     }
 }
 public function testDropBoxPut()
 {
     $event = $this->calDAVTests->testCreateEventWithInternalOrganizer();
     $request = new Sabre\HTTP\Request(array('REQUEST_METHOD' => 'PUT', 'REQUEST_URI' => '/calendars/' . Tinebase_Core::getUser()->contact_id . '/dropbox/' . $event->getRecord()->getId() . '.dropbox/agenda.txt'));
     $agenda = 'HELLO WORLD';
     $request->setBody($agenda);
     $this->server->httpRequest = $request;
     $this->server->exec();
     //         echo $this->response->body;
     $attachments = Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachments($event->getRecord());
     $this->assertEquals(1, $attachments->count());
     $this->assertEquals('agenda.txt', $attachments[0]->name);
 }
コード例 #15
0
 /**
  * delete linked objects (notes, relations, ...) of record
  *
  * @param Tinebase_Record_Interface $_record
  */
 protected function _deleteLinkedObjects(Tinebase_Record_Interface $_record)
 {
     // delete notes & relations
     if ($_record->has('notes')) {
         Tinebase_Notes::getInstance()->deleteNotesOfRecord($this->_modelName, $this->_getBackendType(), $_record->getId());
     }
     if ($_record->has('relations')) {
         // TODO check if this needs to be done, as we might already deleting this "from the other side"
         $this->deleteLinkedRelations($_record);
     }
     if ($_record->has('attachments') && Tinebase_Core::isFilesystemAvailable()) {
         Tinebase_FileSystem_RecordAttachments::getInstance()->deleteRecordAttachments($_record);
     }
 }