public static function save(\GO\Base\Fs\File $file, array $config) { $configData = "<?php\n/**\n" . " * Group-Office configuration file.\n" . " * Visit https://www.group-office.com/wiki/Configuration_file for available values\n" . " */\n\n"; $configReflection = new ReflectionClass(GO::config()); $defaults = $configReflection->getDefaultProperties(); foreach ($config as $key => $value) { if (!isset($defaults[$key]) || $defaults[$key] !== $value) { $configData .= '$config["' . $key . '"]=' . var_export($value, true) . ';' . "\n"; } } //make sure directory exists $file->parent()->create(); //clear opcache in PHP 5.5 if (function_exists('opcache_invalidate')) { opcache_invalidate($file->path(), true); } return file_put_contents($file->path(), $configData); }
private function _link($params, \GO\Base\Mail\Message $message, $model = false, $tags = array()) { $autoLinkContacts = false; if (!$model) { if (!empty($params['link'])) { $linkProps = explode(':', $params['link']); $model = GO::getModel($linkProps[0])->findByPk($linkProps[1]); } $autoLinkContacts = GO::modules()->addressbook && GO::modules()->savemailas && !empty(GO::config()->email_autolink_contacts); } else { //don't link the same model twice on sent. It parses the new autolink tag //and handles the link to field. $linkProps = explode(':', $params['link']); if ($linkProps[0] == $model->className() && $linkProps[1] == $model->id) { return false; } } if ($model || $autoLinkContacts || count($tags)) { $path = 'email/' . date('mY') . '/sent_' . time() . '.eml'; $file = new \GO\Base\Fs\File(GO::config()->file_storage_path . $path); $file->parent()->create(); $fbs = new \Swift_ByteStream_FileByteStream($file->path(), true); $message->toByteStream($fbs); if (!$file->exists()) { throw new \Exception("Failed to save email to file!"); } $attributes = array(); $alias = \GO\Email\Model\Alias::model()->findByPk($params['alias_id']); $attributes['from'] = (string) \GO\Base\Mail\EmailRecipients::createSingle($alias->email, $alias->name); if (isset($params['to'])) { $attributes['to'] = $params['to']; } if (isset($params['cc'])) { $attributes['cc'] = $params['cc']; } if (isset($params['bcc'])) { $attributes['bcc'] = $params['bcc']; } $attributes['subject'] = !empty($params['subject']) ? $params['subject'] : GO::t('no_subject', 'email'); // $attributes['path'] = $path; $attributes['time'] = $message->getDate(); $attributes['uid'] = $alias->email . '-' . $message->getDate(); $linkedModels = array(); if ($model) { $attributes['acl_id'] = $model->findAclId(); $linkedEmail = \GO\Savemailas\Model\LinkedEmail::model()->findSingleByAttributes(array('uid' => $attributes['uid'], 'acl_id' => $attributes['acl_id'])); if (!$linkedEmail) { $linkedEmail = new \GO\Savemailas\Model\LinkedEmail(); $linkedEmail->setAttributes($attributes); try { $linkedEmail->save(); } catch (\GO\Base\Exception\AccessDenied $e) { throw new \Exception(GO::t('linkMustHavePermissionToWrite', 'email')); } } $linkedEmail->link($model); $linkedModels[] = $model; GO::debug('1'); } //process tags in the message body while ($tag = array_shift($tags)) { $linkModel = GO::getModel($tag['model'])->findByPk($tag['model_id'], false, true); if ($linkModel && !$linkModel->equals($linkedModels) && $linkModel->checkPermissionLevel(\GO\Base\Model\Acl::WRITE_PERMISSION)) { $attributes['acl_id'] = $linkModel->findAclId(); $linkedEmail = \GO\Savemailas\Model\LinkedEmail::model()->findSingleByAttributes(array('uid' => $attributes['uid'], 'acl_id' => $attributes['acl_id'])); if (!$linkedEmail) { $linkedEmail = new \GO\Savemailas\Model\LinkedEmail(); $linkedEmail->setAttributes($attributes); $linkedEmail->save(); } $linkedEmail->link($linkModel); $linkedModels[] = $linkModel; } } if ($autoLinkContacts) { $to = new \GO\Base\Mail\EmailRecipients($params['to'] . "," . $params['bcc']); $to = $to->getAddresses(); // var_dump($to); foreach ($to as $email => $name) { //$contact = \GO\Addressbook\Model\Contact::model()->findByEmail($email, \GO\Base\Db\FindParams::newInstance()->permissionLevel(Acl::WRITE_PERMISSION)->single()); $stmt = \GO\Addressbook\Model\Contact::model()->findByEmail($email, \GO\Base\Db\FindParams::newInstance()->permissionLevel(Acl::WRITE_PERMISSION)->limit(1)); $contact = $stmt->fetch(); if ($contact && !$contact->equals($linkedModels)) { $attributes['acl_id'] = $contact->findAclId(); $linkedEmail = \GO\Savemailas\Model\LinkedEmail::model()->findSingleByAttributes(array('uid' => $attributes['uid'], 'acl_id' => $attributes['acl_id'])); if (!$linkedEmail) { $linkedEmail = new \GO\Savemailas\Model\LinkedEmail(); $linkedEmail->setAttributes($attributes); $linkedEmail->save(); } $linkedEmail->link($contact); // Also link the company to the email if the contact has a company attached to it. if (!empty(GO::config()->email_autolink_companies) && !empty($contact->company_id)) { $company = $contact->company; if ($company && !$company->equals($linkedModels)) { $linkedEmail->link($company); } } } } } } }
private function _processFileColumns($cols) { foreach ($cols as $column => $newValue) { $oldValue = $this->_attributes[$column]; if (empty($newValue)) { //unset of file column if (!empty($oldValue)) { $file = new \GO\Base\Fs\File(GO::config()->file_storage_path . $oldValue); $file->delete(); $this->{$column} = ""; } } elseif ($newValue instanceof \GO\Base\Fs\File) { if (!isset($this->columns[$column]['filePathTemplate'])) { throw new \Exception('For file columns you must set a filePathTemplate'); } $destination = $this->columns[$column]['filePathTemplate']; foreach ($this->_attributes as $key => $value) { $destination = str_replace('{' . $key . '}', $value, $destination); } $destination = str_replace('{extension}', $newValue->extension(), $destination); $destinationFile = new \GO\Base\Fs\File(GO::config()->file_storage_path . $destination); $destinationFolder = $destinationFile->parent(); $destinationFolder->create(); $newValue->move($destinationFolder, $destinationFile->name()); $this->{$column} = $destinationFile->stripFileStoragePath(); } else { throw new \Exception("Column {$column} must be an instance of GO\\Base\\Fs\\File. " . var_export($newValue, true)); } } return !empty($cols); }
protected function actionThumb($params) { GO::session()->closeWriting(); $dir = GO::config()->root_path . 'views/Extjs3/themes/Default/images/128x128/filetypes/'; $url = GO::config()->host . 'views/Extjs3/themes/Default/images/128x128/filetypes/'; $file = new \GO\Base\Fs\File(GO::config()->file_storage_path . $params['src']); if (is_dir(GO::config()->file_storage_path . $params['src'])) { $src = $dir . 'folder.png'; } else { switch (strtolower($file->extension())) { case 'svg': case 'ico': case 'jpg': case 'jpeg': case 'png': case 'gif': case 'xmind': $src = GO::config()->file_storage_path . $params['src']; break; case 'tar': case 'tgz': case 'gz': case 'bz2': case 'zip': $src = $dir . 'zip.png'; break; case 'odt': case 'docx': case 'doc': case 'htm': case 'html': $src = $dir . 'doc.png'; break; case 'odc': case 'ods': case 'xls': case 'xlsx': $src = $dir . 'spreadsheet.png'; break; case 'odp': case 'pps': case 'pptx': case 'ppt': $src = $dir . 'pps.png'; break; case 'eml': $src = $dir . 'message.png'; break; case 'log': $src = $dir . 'txt.png'; break; default: if (file_exists($dir . $file->extension() . '.png')) { $src = $dir . $file->extension() . '.png'; } else { $src = $dir . 'unknown.png'; } break; } } $file = new \GO\Base\Fs\File($src); if ($file->size() > \GO::config()->max_thumbnail_size * 1024 * 1024) { throw new \Exception("Image may not be larger than 5MB."); } $w = isset($params['w']) ? intval($params['w']) : 0; $h = isset($params['h']) ? intval($params['h']) : 0; $zc = !empty($params['zc']) && !empty($w) && !empty($h); $lw = isset($params['lw']) ? intval($params['lw']) : 0; $lh = isset($params['lh']) ? intval($params['lh']) : 0; $pw = isset($params['pw']) ? intval($params['pw']) : 0; $ph = isset($params['ph']) ? intval($params['ph']) : 0; if ($file->extension() == 'xmind') { // $filename = $file->nameWithoutExtension().'.jpeg'; // // if (!file_exists($GLOBALS['GO_CONFIG']->file_storage_path . 'thumbcache/' . $filename) || filectime($GLOBALS['GO_CONFIG']->file_storage_path . 'thumbcache/' . $filename) < filectime($GLOBALS['GO_CONFIG']->file_storage_path . $path)) { // $zipfile = zip_open($GLOBALS['GO_CONFIG']->file_storage_path . $path); // // while ($entry = zip_read($zipfile)) { // if (zip_entry_name($entry) == 'Thumbnails/thumbnail.jpg') { // require_once($GLOBALS['GO_CONFIG']->class_path . 'filesystem.class.inc'); // zip_entry_open($zipfile, $entry, 'r'); // file_put_contents($GLOBALS['GO_CONFIG']->file_storage_path . 'thumbcache/' . $filename, zip_entry_read($entry, zip_entry_filesize($entry))); // zip_entry_close($entry); // break; // } // } // zip_close($zipfile); // } // $path = 'thumbcache/' . $filename; } $cacheDir = new \GO\Base\Fs\Folder(GO::config()->orig_tmpdir . 'thumbcache'); $cacheDir->create(); $cacheFilename = str_replace(array('/', '\\'), '_', $file->parent()->path() . '_' . $w . '_' . $h . '_' . $lw . '_' . $ph . '_' . '_' . $pw . '_' . $lw); if ($zc) { $cacheFilename .= '_zc'; } //$cache_filename .= '_'.filesize($full_path); $cacheFilename .= $file->name(); $readfile = $cacheDir->path() . '/' . $cacheFilename; $thumbExists = file_exists($cacheDir->path() . '/' . $cacheFilename); $thumbMtime = $thumbExists ? filemtime($cacheDir->path() . '/' . $cacheFilename) : 0; GO::debug("Thumb mtime: " . $thumbMtime . " (" . $cacheFilename . ")"); if (!empty($params['nocache']) || !$thumbExists || $thumbMtime < $file->mtime() || $thumbMtime < $file->ctime()) { GO::debug("Resizing image"); $image = new \GO\Base\Util\Image($file->path()); if (!$image->load_success) { GO::debug("Failed to load image for thumbnailing"); //failed. Stream original image $readfile = $file->path(); } else { if ($zc) { $image->zoomcrop($w, $h); } else { if ($lw || $lh || $pw || $lw) { //treat landscape and portrait differently $landscape = $image->landscape(); if ($landscape) { $w = $lw; $h = $lh; } else { $w = $pw; $h = $ph; } } GO::debug($w . "x" . $h); if ($w && $h) { $image->resize($w, $h); } elseif ($w) { $image->resizeToWidth($w); } else { $image->resizeToHeight($h); } } $image->save($cacheDir->path() . '/' . $cacheFilename); } } header("Expires: " . date("D, j M Y G:i:s ", time() + 86400 * 365) . 'GMT'); //expires in 1 year header('Cache-Control: cache'); header('Pragma: cache'); header('Content-Type: ' . $file->mimeType()); header('Content-Disposition: inline; filename="' . $cacheFilename . '"'); header('Content-Transfer-Encoding: binary'); readfile($readfile); // case 'pdf': // $this->redirect($url . 'pdf.png'); // break; // // case 'tar': // case 'tgz': // case 'gz': // case 'bz2': // case 'zip': // $this->redirect( $url . 'zip.png'); // break; // case 'odt': // case 'docx': // case 'doc': // $this->redirect( $url . 'doc.png'); // break; // // case 'odc': // case 'ods': // case 'xls': // case 'xlsx': // $this->redirect( $url . 'spreadsheet.png'); // break; // // case 'odp': // case 'pps': // case 'pptx': // case 'ppt': // $this->redirect( $url . 'pps.png'); // break; // case 'eml': // $this->redirect( $url . 'message.png'); // break; // // case 'htm': // $this->redirect( $url . 'doc.png'); // break; // // case 'log': // $this->redirect( $url . 'txt.png'); // break; // // default: // if (file_exists($dir . $file->extension() . '.png')) { // $this->redirect( $url . $file->extension() . '.png'); // } else { // $this->redirect( $url . 'unknown.png'); // } // break; }
/** * Import a filesystem file into the database. * * @param \GO\Base\Fs\File $fsFile * @return File */ public static function importFromFilesystem(\GO\Base\Fs\File $fsFile) { $folderPath = str_replace(\GO::config()->file_storage_path, "", $fsFile->parent()->path()); $folder = Folder::model()->findByPath($folderPath, true); return $folder->hasFile($fsFile->name()) || $folder->addFile($fsFile->name()); }
private function _decryptFile(\GO\Base\Fs\File $srcFile, \GO\Email\Model\Account $account) { $data = $srcFile->getContents(); if (strpos($data, "enveloped-data") || strpos($data, 'Encrypted Message')) { $cert = \GO\Smime\Model\Certificate::model()->findByPk($account->id); $password = \GO::session()->values['smime']['passwords'][$_REQUEST['account_id']]; openssl_pkcs12_read($cert->cert, $certs, $password); $decryptedFile = \GO\Base\Fs\File::tempFile(); $ret = openssl_pkcs7_decrypt($srcFile->path(), $decryptedFile->path(), $certs['cert'], array($certs['pkey'], $password)); if (!$decryptedFile->exists()) { throw new \Exception("Could not decrypt message: " . openssl_error_string()); } $decryptedFile->move($srcFile->parent(), $srcFile->name()); } }