/** * crops a image identified by an imgageURL and returns a new tempFileImage * * @param string $imageurl imageURL of the image to be croped * @param int $left left position of crop window * @param int $top top position of crop window * @param int $widht widht of crop window * @param int $height heidht of crop window * @return string imageURL of new temp image * */ public function cropImage($imageurl, $left, $top, $widht, $height) { $this->checkAuth(); $image = Tinebase_Model_Image::getImageFromImageURL($imageurl); Tinebase_ImageHelper::crop($image, $left, $top, $widht, $height); }
/** * create contact pdf * * @param Addressbook_Model_Contact $_contact contact data * * @return string the contact pdf */ public function generate(Addressbook_Model_Contact $_contact) { $locale = Tinebase_Core::get('locale'); $translate = Tinebase_Translation::getTranslation('Addressbook'); // set user timezone $_contact->setTimezone(Tinebase_Core::getUserTimezone()); $contactFields = array(array('label' => $translate->_('Business Contact Data'), 'type' => 'separator'), array('label' => $translate->_('Organisation / Unit'), 'type' => 'singleRow', 'value' => array(array('org_name', 'org_unit')), 'glue' => ' / '), array('label' => $translate->_('Business Address'), 'type' => 'multiRow', 'value' => array('adr_one_street', 'adr_one_street2', array('adr_one_postalcode', 'adr_one_locality'), array('adr_one_region', 'adr_one_countryname'))), array('label' => $translate->_('Email'), 'value' => array('email')), array('label' => $translate->_('Telephone Work'), 'value' => array('tel_work')), array('label' => $translate->_('Telephone Cellphone'), 'value' => array('tel_cell')), array('label' => $translate->_('Telephone Car'), 'value' => array('tel_car')), array('label' => $translate->_('Telephone Fax'), 'value' => array('tel_fax')), array('label' => $translate->_('Telephone Page'), 'value' => array('tel_pager')), array('label' => $translate->_('URL'), 'value' => array('url')), array('label' => $translate->_('Role'), 'value' => array('role')), array('label' => $translate->_('Room'), 'value' => array('room')), array('label' => $translate->_('Assistant'), 'value' => array('assistent')), array('label' => $translate->_('Assistant Telephone'), 'value' => array('tel_assistent')), array('label' => $translate->_('Private Contact Data'), 'type' => 'separator'), array('label' => $translate->_('Private Address'), 'type' => 'multiRow', 'value' => array('adr_two_street', 'adr_two_street2', array('adr_two_postalcode', 'adr_two_locality'), array('adr_two_region', 'adr_two_countryname'))), array('label' => $translate->_('Email Home'), 'value' => array('email_home')), array('label' => $translate->_('Telephone Home'), 'value' => array('tel_home')), array('label' => $translate->_('Telephone Cellphone Private'), 'value' => array('tel_cell_private')), array('label' => $translate->_('Telephone Fax Home'), 'value' => array('tel_fax_home')), array('label' => $translate->_('URL Home'), 'value' => array('url_home')), array('label' => $translate->_('Other Data'), 'type' => 'separator'), array('label' => $translate->_('Birthday'), 'value' => array('bday')), array('label' => $translate->_('Job Title'), 'value' => array('title'))); try { $tineImage = Addressbook_Controller::getInstance()->getImage($_contact->getId()); Tinebase_ImageHelper::resize($tineImage, 160, 240, Tinebase_ImageHelper::RATIOMODE_PRESERVANDCROP); $tmpPath = tempnam(Tinebase_Core::getTempDir(), 'tine20_tmp_gd'); $tmpPath .= $tineImage->getImageExtension(); file_put_contents($tmpPath, $tineImage->blob); $contactPhoto = Zend_Pdf_Image::imageWithPath($tmpPath); unlink($tmpPath); } catch (Exception $e) { // gif images are not supported yet by zf (or some other error) if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $e->__toString()); } $contactPhoto = NULL; } // build title (name) + subtitle + icon $nameFields = array('n_prefix', 'n_given', 'n_middle', 'n_family', 'n_suffix'); $titleArray = array(); foreach ($nameFields as $nameField) { if (!empty($_contact[$nameField])) { $titleArray[] = $_contact[$nameField]; } } $title = implode(' ', $titleArray); $subtitle = $_contact->org_name; $titleIcon = "/images/oxygen/32x32/apps/system-users.png"; // add data to array $record = array(); foreach ($contactFields as $fieldArray) { if (!isset($fieldArray['type']) || $fieldArray['type'] !== 'separator') { $values = array(); foreach ($fieldArray['value'] as $valueFields) { $content = array(); if (is_array($valueFields)) { $keys = $valueFields; } else { $keys = array($valueFields); } foreach ($keys as $key) { if ($_contact->{$key} instanceof Tinebase_DateTime) { $content[] = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_contact->{$key}, NULL, NULL, 'date'); } elseif (!empty($_contact->{$key})) { if (preg_match("/countryname/", $key)) { $content[] = Zend_Locale::getTranslation($_contact->{$key}, 'country', $locale); } else { $content[] = $_contact->{$key}; } } } if (!empty($content)) { $glue = isset($fieldArray['glue']) ? $fieldArray['glue'] : " "; $values[] = implode($glue, $content); } } if (!empty($values)) { $record[] = array('label' => $fieldArray['label'], 'type' => isset($fieldArray['type']) ? $fieldArray['type'] : 'singleRow', 'value' => sizeof($values) === 1 ? $values[0] : $values); } } elseif (isset($fieldArray['type']) && $fieldArray['type'] === 'separator') { $record[] = $fieldArray; } } // add notes $record = $this->_addActivities($record, $_contact->notes); //print_r($record); // tags $tags = isset($_contact['tags']) ? $_contact['tags'] : array(); // generate pdf $this->generatePdf($record, $title, $subtitle, $tags, $_contact->note, $titleIcon, $contactPhoto, array(), FALSE); }
/** * saves image to db * * @param string $contactId * @param blob $imageData * @return blob|string */ public function _saveImage($contactId, $imageData) { if (!empty($imageData)) { // make sure that we got a valid image blob try { Tinebase_ImageHelper::getImageInfoFromBlob($imageData); } catch (Exception $e) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Invalid image blob data, preserving old image'); Tinebase_Exception::log($e); return $this->getImage($contactId); } } if (!empty($imageData)) { $currentImg = $this->getImage($contactId); if (empty($currentImg)) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Creating contact image.'); } $this->_db->insert($this->_tablePrefix . 'addressbook_image', array('contact_id' => $contactId, 'image' => base64_encode($imageData))); } else { if ($currentImg !== $imageData) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating contact image.'); } $where = array($this->_db->quoteInto($this->_db->quoteIdentifier('contact_id') . ' = ?', $contactId)); $this->_db->update($this->_tablePrefix . 'addressbook_image', array('image' => base64_encode($imageData)), $where); } } } else { $this->_db->delete($this->_tablePrefix . 'addressbook_image', $this->_db->quoteInto($this->_db->quoteIdentifier('contact_id') . ' = ?', $contactId)); } return $imageData; }
/** * gets image info and data * * @param string $application application which manages the image * @param string $identifier identifier of image/record * @param string $location optional additional identifier * @return Tinebase_Model_Image * @throws Tinebase_Exception_NotFound * @throws Tinebase_Exception_UnexpectedValue */ public function getImage($application, $identifier, $location = '') { if ($location === 'vfs') { $node = Tinebase_FileSystem::getInstance()->get($identifier); $path = Tinebase_Model_Tree_Node_Path::STREAMWRAPPERPREFIX . Tinebase_FileSystem::getInstance()->getPathOfNode($node, true); $image = Tinebase_ImageHelper::getImageInfoFromBlob(file_get_contents($path)); } else { if ($application == 'Tinebase' && $location == 'tempFile') { $tempFile = Tinebase_TempFile::getInstance()->getTempFile($identifier); $image = Tinebase_ImageHelper::getImageInfoFromBlob(file_get_contents($tempFile->path)); } else { $appController = Tinebase_Core::getApplicationInstance($application); if (!method_exists($appController, 'getImage')) { throw new Tinebase_Exception_NotFound("{$application} has no getImage function."); } $image = $appController->getImage($identifier, $location); } } if (!$image instanceof Tinebase_Model_Image) { if (is_array($image)) { $image = new Tinebase_Model_Image($image + array('application' => $application, 'id' => $identifier, 'location' => $location)); } else { throw new Tinebase_Exception_UnexpectedValue('broken image'); } } return $image; }
/** * returns contact image * * @param string $_identifier record identifier * @param string $_location not used, required by interface * @return Tinebase_Model_Image * @throws Addressbook_Exception_NotFound if no image found */ public function getImage($_identifier, $_location = '') { // get contact to ensure user has read rights $image = Addressbook_Controller_Contact::getInstance()->getImage($_identifier); if (empty($image)) { throw new Addressbook_Exception_NotFound('Contact has no image.'); } $imageInfo = Tinebase_ImageHelper::getImageInfoFromBlob($image); return new Tinebase_Model_Image($imageInfo + array('id' => $_identifier, 'application' => $this->_applicationName, 'data' => $image)); }
/** * returns image from given blob * * @param string $_blob * @return Tinebase_Model_Image */ public static function getImageFromBlob($_blob) { return new Tinebase_Model_Image(Tinebase_ImageHelper::getImageInfoFromBlob($_blob), true); }
/** * returns binary image data from a image identified by a imagelink * * @param array $imageParams * @return string binary data * @throws Tinebase_Exception_UnexpectedValue */ public static function getImageData($imageParams) { $tempFile = Tinebase_TempFile::getInstance()->getTempFile($imageParams['id']); if (!Tinebase_ImageHelper::isImageFile($tempFile->path)) { throw new Tinebase_Exception_UnexpectedValue('Given file is not an image.'); } return file_get_contents($tempFile->path); }
/** * test preserve no fill fit height */ public function testResizeRatioModePreservNoFillHeight() { $testImageRatio = $this->_testImageData['width'] / $this->_testImageData['height']; $dstHeight = 100; Tinebase_ImageHelper::resize($this->_testImage, $dstHeight * 100, $dstHeight, Tinebase_ImageHelper::RATIOMODE_PRESERVNOFILL); $this->assertEquals($dstHeight, $this->_testImage->height); $this->assertEquals(floor($dstHeight * $testImageRatio), $this->_testImage->width); }
/** * download message part * * @param string $_messageId * @param string $_partId * @param string $disposition * @param boolean $validateImage */ protected function _outputMessagePart($_messageId, $_partId = NULL, $disposition = 'attachment', $validateImage = FALSE) { $oldMaxExcecutionTime = Tinebase_Core::setExecutionLifeTime(0); try { // fetch extracted winmail dat contents if (strstr($_partId, 'winmail-')) { $partIndex = explode('winmail-', $_partId); $partIndex = intval($partIndex[1]); $files = Felamimail_Controller_Message::getInstance()->extractWinMailDat($_messageId); $file = $files[$partIndex]; $part = NULL; $path = Tinebase_Core::getTempDir() . '/winmail/'; $path = $path . $_messageId . '/'; $contentType = mime_content_type($path . $file); $this->_prepareHeader($file, $contentType); $stream = fopen($path . $file, 'r'); } else { // fetch normal attachment $part = Felamimail_Controller_Message::getInstance()->getMessagePart($_messageId, $_partId); $contentType = $_partId === NULL ? Felamimail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822 : $part->type; $filename = $this->_getDownloadFilename($part, $_messageId, $contentType); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . ' filename: ' . $filename . ' content type ' . $contentType); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($part, TRUE)); } $this->_prepareHeader($filename, $contentType); $stream = $_partId === NULL ? $part->getRawStream() : $part->getDecodedStream(); } if ($validateImage) { $tmpPath = tempnam(Tinebase_Core::getTempDir(), 'tine20_tmp_imgdata'); $tmpFile = fopen($tmpPath, 'w'); stream_copy_to_stream($stream, $tmpFile); fclose($tmpFile); // @todo check given mimetype or all images types? if (!Tinebase_ImageHelper::isImageFile($tmpPath)) { if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Resource is no image file: ' . $filename); } } else { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Verified ' . $contentType . ' image.'); } readfile($tmpPath); } } else { fpassthru($stream); } fclose($stream); } catch (Exception $e) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Failed to get message part: ' . $e->getMessage()); } Tinebase_Core::setExecutionLifeTime($oldMaxExcecutionTime); exit; }
/** * adds image property with image url like this: * 'index.php?method=Tinebase.getImage&application=Tinebase&location=vfs&id=e4b7de34e229672c0d5e22be0912779441e6e051' * * @param $records */ public static function resolveAttachmentImage($records) { // get all images from attachments and set 'image' properties // TODO find an additional condition to better detect the attachments that should be the record image(s) /** @var Tinebase_Record_Interface $record */ foreach ($records as $record) { if ($record->has('image') && $record->attachments instanceof Tinebase_Record_RecordSet) { foreach ($record->attachments as $attachment) { if (in_array($attachment->contenttype, Tinebase_ImageHelper::getSupportedImageMimeTypes())) { $record->image = Tinebase_Model_Image::getImageUrl('Tinebase', $attachment->getId(), 'vfs'); break; } } } } }
/** * set contact image * * @param array $_data */ protected function _setContactImage(&$_data) { if (!isset($_data['jpegphoto']) || $_data['jpegphoto'] === '') { return; } $imageParams = Tinebase_ImageHelper::parseImageLink($_data['jpegphoto']); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' image params:' . print_r($imageParams, TRUE)); } if ($imageParams['isNewImage']) { try { $_data['jpegphoto'] = Tinebase_ImageHelper::getImageData($imageParams); } catch (Tinebase_Exception_UnexpectedValue $teuv) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not add contact image: ' . $teuv->getMessage()); unset($_data['jpegphoto']); } } else { unset($_data['jpegphoto']); } }
/** * check if data is valid, check and allow * * @param HTMLPurifier_URI $uri * @param HTMLPurifier_Token $token * @return boolean */ protected function _checkData($uri, $token) { $result = FALSE; if ($token->name === 'img' && isset($token->attr['src'])) { $imgSrc = $token->attr['src']; $imgSrc = str_replace(array("\r", "\n"), '', $imgSrc); if (preg_match('/([a-z\\/]*);base64,(.*)/', $imgSrc, $matches)) { $mimetype = $matches[1]; $base64 = $matches[2]; if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Found base64 image: ' . $base64); } $tmpPath = tempnam(Tinebase_Core::getTempDir(), 'tine20_tmp_imgdata'); file_put_contents($tmpPath, @base64_decode($base64)); // @todo check given mimetype or all images types? if (!Tinebase_ImageHelper::isImageFile($tmpPath)) { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' URI data is no image file: ' . $uri->toString()); } } else { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Verified ' . $mimetype . ' image.'); } $result = TRUE; } } } else { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Only allow images data uris, discarding: ' . $token->name); } } return $result; }