/** * inform users if a file was shared with them * * @param array $recipientList list of recipients * @param string $itemSource shared item source * @param string $itemType shared item type * @return array list of user to whom the mail send operation failed */ public function sendInternalShareMail($recipientList, $itemSource, $itemType) { $noMail = []; foreach ($recipientList as $recipient) { $recipientDisplayName = \OCP\User::getDisplayName($recipient); $to = $this->config->getUserValue($recipient, 'settings', 'email', ''); if ($to === '') { $noMail[] = $recipientDisplayName; continue; } $items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient); $filename = trim($items[0]['file_target'], '/'); $subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename)); $expiration = null; if (isset($items[0]['expiration'])) { try { $date = new DateTime($items[0]['expiration']); $expiration = $date->getTimestamp(); } catch (\Exception $e) { $this->logger->error("Couldn't read date: " . $e->getMessage(), ['app' => 'sharing']); } } // Link to folder, or root folder if a file if ($itemType === 'folder') { $args = array('dir' => $filename); } else { if (strpos($filename, '/')) { $args = array('dir' => '/' . dirname($filename), 'scrollto' => basename($filename)); } else { $args = array('dir' => '/', 'scrollto' => $filename); } } $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args); list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); // send it out now try { $message = $this->mailer->createMessage(); $message->setSubject($subject); $message->setTo([$to => $recipientDisplayName]); $message->setHtmlBody($htmlBody); $message->setPlainBody($textBody); $message->setFrom([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => (string) $this->l->t('%s via %s', [$this->senderDisplayName, $this->defaults->getName()])]); if (!is_null($this->replyTo)) { $message->setReplyTo([$this->replyTo]); } $this->mailer->send($message); } catch (\Exception $e) { $this->logger->error("Can't send mail to inform the user about an internal share: " . $e->getMessage(), ['app' => 'sharing']); $noMail[] = $recipientDisplayName; } } return $noMail; }
/** * inform users if a file was shared with them * * @param array $recipientList list of recipients * @param string $itemSource shared item source * @param string $itemType shared item type * @return array list of user to whom the mail send operation failed */ public function sendInternalShareMail($recipientList, $itemSource, $itemType) { $noMail = array(); foreach ($recipientList as $recipient) { $recipientDisplayName = \OCP\User::getDisplayName($recipient); $to = \OC::$server->getConfig()->getUserValue($recipient, 'settings', 'email', ''); if ($to === '') { $noMail[] = $recipientDisplayName; continue; } $items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient); $filename = trim($items[0]['file_target'], '/'); $subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename)); $expiration = null; if (isset($items[0]['expiration'])) { try { $date = new DateTime($items[0]['expiration']); $expiration = $date->getTimestamp(); } catch (\Exception $e) { \OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR); } } // Link to folder, or root folder if a file if ($itemType === 'folder') { $args = array('dir' => $filename); } else { if (strpos($filename, '/')) { $args = array('dir' => '/' . dirname($filename), 'scrollto' => basename($filename)); } else { $args = array('dir' => '/', 'scrollto' => $filename); } } $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args); list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration); // send it out now try { \OC_MAIL::send($to, $recipientDisplayName, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail, '', '', '', $this->replyTo); } catch (\Exception $e) { \OCP\Util::writeLog('sharing', "Can't send mail to inform the user about an internal share: " . $e->getMessage(), \OCP\Util::ERROR); $noMail[] = $recipientDisplayName; } } return $noMail; }
/** * inform users if a file was shared with them * * @param array $recipientList list of recipients * @param string $itemSource shared item source * @param string $itemType shared item type * @return array list of user to whom the mail send operation failed */ public function sendInternalShareMail($recipientList, $itemSource, $itemType) { $noMail = array(); foreach ($recipientList as $recipient) { $recipientDisplayName = \OCP\User::getDisplayName($recipient); $to = \OC_Preferences::getValue($recipient, 'settings', 'email', ''); if ($to === '') { $noMail[] = $recipientDisplayName; continue; } $items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient); $filename = trim($items[0]['file_target'], '/'); $subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename)); $expiration = null; if (isset($items[0]['expiration'])) { try { $date = new DateTime($items[0]['expiration']); $expiration = $date->getTimestamp(); } catch (\Exception $e) { \OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR); } } if ($itemType === 'folder') { $foldername = "/Shared/" . $filename; } else { // if it is a file we can just link to the Shared folder, // that's the place where the user will find the file $foldername = "/Shared"; } $link = \OCP\Util::linkToAbsolute('files', 'index.php', array("dir" => $foldername)); list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration); // send it out now try { \OCP\Util::sendMail($to, $recipientDisplayName, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail); } catch (\Exception $e) { \OCP\Util::writeLog('sharing', "Can't send mail to inform the user about an internal share: " . $e->getMessage(), \OCP\Util::ERROR); $noMail[] = $recipientDisplayName; } } return $noMail; }
/** * get shared parents * * @param int $itemSource item source ID * @param string $shareWith with whom should the item be shared * @param string $owner owner of the item * @return array with shares */ public function getParents($itemSource, $shareWith = null, $owner = null) { $result = array(); $parent = $this->getParentId($itemSource); while ($parent) { $shares = \OCP\Share::getItemSharedWithUser('folder', $parent, $shareWith, $owner); if ($shares) { foreach ($shares as $share) { $name = substr($share['path'], strrpos($share['path'], '/') + 1); $share['collection']['path'] = $name; $share['collection']['item_type'] = 'folder'; $share['file_path'] = $name; $displayNameOwner = \OCP\User::getDisplayName($share['uid_owner']); $displayNameShareWith = \OCP\User::getDisplayName($share['share_with']); $share['displayname_owner'] = $displayNameOwner ? $displayNameOwner : $share['uid_owner']; $share['share_with_displayname'] = $displayNameShareWith ? $displayNameShareWith : $share['uid_owner']; $result[] = $share; } } $parent = $this->getParentId($parent); } return $result; }
public function testGetItemSharedWithUserFromGroupShare() { \OC_User::setUserId($this->user1); //add dummy values to the share table $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' . ' `item_type`, `item_source`, `item_target`, `share_type`,' . ' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); $args = array('test', 99, 'target1', \OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user1); $query->execute($args); $args = array('test', 99, 'target2', \OCP\Share::SHARE_TYPE_GROUP, $this->group2, $this->user1); $query->execute($args); $args = array('test', 99, 'target3', \OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user2); $query->execute($args); $args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user4); $query->execute($args); // user2 is in group1 and group2 $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2, $this->user1); $this->assertSame(2, count($result1)); $this->verifyResult($result1, array('target1', 'target2')); $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1); $this->assertSame(2, count($result2)); $this->verifyResult($result2, array('target1', 'target2')); // user3 is in group1 and group2 $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3); $this->assertSame(3, count($result3)); $this->verifyResult($result3, array('target1', 'target3', 'target4')); $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null); $this->assertSame(4, count($result4)); $this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4')); $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6, null); $this->assertSame(0, count($result6)); }
$noMail = array(); $recipientList = array(); if ($shareType === \OCP\Share::SHARE_TYPE_USER) { $recipientList[] = $recipient; } elseif ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { $recipientList = \OC_Group::usersInGroup($recipient); } // don't send a mail to the user who shared the file $recipientList = array_diff($recipientList, array(\OCP\User::getUser())); // send mail to all recipients with an email address foreach ($recipientList as $recipient) { //get correct target folder name $email = OC_Preferences::getValue($recipient, 'settings', 'email', ''); if ($email !== '') { $displayName = \OCP\User::getDisplayName($recipient); $items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient); $filename = trim($items[0]['file_target'], '/'); $subject = (string) $l->t('%s shared »%s« with you', array($ownerDisplayName, $filename)); $expiration = null; if (isset($items[0]['expiration'])) { $date = new DateTime($items[0]['expiration']); $expiration = $date->format('Y-m-d'); } if ($itemType === 'folder') { $foldername = "/Shared/" . $filename; } else { // if it is a file we can just link to the Shared folder, // that's the place where the user will find the file $foldername = "/Shared"; } $link = \OCP\Util::linkToAbsolute('files', 'index.php', array("dir" => $foldername));