Source of file SendThis_Log.php
Size: 16,633 Bytes - Last Modified: 2021-12-23T10:05:12+00:00
/var/www/docs.ssmods.com/process/src/code/logs/SendThis_Log.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 | <?php /** * Milkyway Multimedia * SendThis_Log.php * * @package milkyway-multimedia/ss-send-this * @author Mellisa Hankins <mellisa.hankins@me.com> */ use Milkyway\SS\SendThis\Mailer; class SendThis_Log extends DataObject { private static $web_based_clients = [ 'Gmail' => 'google.co', 'Hotmail' => 'live.co', 'Yahoo! Mail' => 'yahoo.co', 'Mail.com' => 'mail.co', 'AOL Mail' => 'aol.co', 'Inbox' => 'inbox.co', 'FastMail' => 'fastmail.fm', 'Lycos Mail' => 'lycos.co', ]; private static $db = [ 'Notes' => 'Text', 'To' => 'Text', 'From' => 'Text', 'Cc' => 'Text', 'Bcc' => 'Text', 'ReplyTo' => 'Varchar(256)', 'Subject' => 'Text', 'Attachments' => 'Text', 'Sent' => 'Datetime', 'Delivered' => 'Datetime', 'MessageID' => 'Text', 'Success' => 'Boolean', 'Notify_Sender' => 'Varchar(255)', 'Type' => "Enum('html,plain','html')", 'Opened' => 'Datetime', 'Opens' => 'Int', 'Track_Client' => 'Varchar(256)', 'Track_Data' => 'Text', 'Track_Links' => 'Boolean', 'Link_Data' => 'Text', 'Transport' => 'Varchar(256)', ]; private static $extensions = [ 'Milkyway\\SS\\Behaviours\\Extensions\\Sluggable', ]; private static $has_one = [ 'SentBy' => 'Member', ]; private static $has_many = [ 'Links' => 'SendThis_Link', 'Bounces' => 'SendThis_Bounce', ]; private static $singular_name = 'Email Log'; private static $summary_fields = [ 'To', 'Subject', 'Sent', 'Success', 'Opened' => 'Opened', 'Tracker_ForTemplate' => 'Details', ]; private static $casting = [ 'Tracker_ForTemplate' => 'HTMLText', ]; private static $viewable_has_one = [ 'SentBy', ]; private static $default_sort = 'Created DESC'; protected $excludeFromTrackerReports = [ 'Icon', 'ClientBrand', 'ClientIcon', 'ClientLink', 'ClientCompany', 'ClientCompanyLink', 'CountryCode', 'OperatingSystemIcon', 'OperatingSystemBrand', 'OperatingSystemLink', 'OperatingSystemCompany', 'OperatingSystemCompanyLink', ]; /** * Find the recent emails sent by a member and/or email * * @param int $limit * @param null $member * @param string $email * * @return DataList */ public static function recent($limit = 0, $member = null, $email = '') { $filters = []; $member = $member && $member !== false ? $member : Member::currentUser(); if (!$email && $member) { $email = [$member->Email, $member->ContactEmail, $member->ForEmail]; } if (!$member && !$email) { return ArrayList::create(); } if ($member) { $filters['SentByID'] = $member->ID; } if ($email) { $filters['From'] = $email; } if (!empty($filters)) { $emails = self::get() ->filter('Success', 1) ->filterAny($filters) ->sort('Created', 'DESC'); if ($limit) { return $emails->limit($limit); } } return ArrayList::create(); } /** * Map recent emails, ready for @DropdownField * * @param bool $emailsOnly * @param bool $limit * @param null $member * @param string $email * * @return array */ public static function recent_to_map($emailsOnly = false, $limit = false, $member = null, $email = '') { $emails = []; if (($recent = self::recent($limit, $member, $email)) && $recent->exists()) { foreach ($recent as $r) { if ($emailsOnly) { list($to, $name) = Mailer::split_email($r->To); $emails[$to] = htmlspecialchars($r->To); } else { $emails[htmlspecialchars($r->To)] = htmlspecialchars($r->To); } if ($r->Cc && $cc = explode(',', $r->Cc)) { foreach ($cc as $c) { $emails[trim($c)] = trim($c); } } if ($r->Bcc && $bcc = explode(',', $r->Bcc)) { foreach ($bcc as $bc) { $emails[trim($bc)] = trim($bc); } } } } return $emails; } public function getTitle() { return $this->Subject . ' (' . $this->obj('Created')->Nice() . ')'; } public function getCMSFields() { $this->beforeUpdateCMSFields( function (FieldList $fields) { $fields->removeByName('Track_IP'); $fields->removeByName('Links'); $fields->removeByName('Link_Data'); $fields->removeByName('Track_Links'); $linksExists = false; $fields->insertBefore( HeaderField::create('HEADER-ClientDetails', _t('SendThis_Log.ClientDetails', 'Email Client')), 'Type' ); if (!$this->Notes) { $fields->removeByName('Notes'); } if ($this->Success && !empty($this->Tracker)) { $fields->replaceField( 'Track_Data', $tf = ReadonlyField::create( 'Readonly_Track_Data', _t('SendThis_Log.Tracker', 'Tracker'), $this->Tracker_ForTemplate(false, true) ) ); $tf->dontEscape = true; } else { $fields->removeByName('Track_Data'); } if (!$this->Notify_Sender || !Email::is_valid_address($this->Notify_Sender)) { $fields->replaceField( 'Notify_Sender', CheckboxField::create( 'Notify_Sender', _t('SendThis_Log.NotifySender', 'Notify sender of this email when it fails to deliver') ) ); } if ($this->Links()->exists()) { $linksExists = true; $fields->addFieldsToTab( 'Root.Links', [ ReadonlyField::create( 'TrackedLinks_Count', _t('SendThis_Log.TrackedLinks_Count', 'Number of tracked links'), $this->Links()->count() ), GridField::create( 'TrackedLinks', _t('SendThis_Log.TrackedLinks', 'Tracked Links'), $this->Links(), GridFieldConfig_RecordViewer::create() ), ] ); } if (!empty($this->LinkData)) { $linkData = ''; foreach ($this->LinkData as $name => $value) { $linkData .= $name . ': ' . $value . "\n"; } $before = $linksExists ? 'Links' : null; $fields->addFieldToTab( 'Root.Links', $ldField = ReadonlyField::create( 'Readonly_Link_Data', _t('SendThis_Log.LinkData', 'Link data'), DBField::create_field('HTMLText', $linkData)->nl2list() ), $before ); $ldField->dontEscape = true; } if (($hasOnes = $this->hasOne()) && !empty($hasOnes)) { $viewable = (array)$this->config()->viewable_has_one; foreach ($hasOnes as $field => $type) { if (in_array($field, $viewable) && $this->$field()->exists()) { if ($old = $fields->dataFieldByName($field)) { $fields->removeByName($field); $fields->removeByName($field . 'ID'); $fields->addFieldToTab('Root.Related', $old->castedCopy($old->class)); } elseif ($old = $fields->dataFieldByName($field . 'ID')) { $fields->removeByName($field); $fields->removeByName($field . 'ID'); $fields->addFieldToTab('Root.Related', $old->castedCopy($old->class)); } } else { $fields->removeByName($field); $fields->removeByName($field . 'ID'); } } } } ); $fields = parent::getCMSFields(); return $fields; } public function setTracker($data = []) { $this->Track_Data = json_encode($data); } public function getTracker() { return json_decode($this->Track_Data, true); } public function setLinkData($data = []) { $this->Link_Data = json_encode($data); } public function getLinkData() { return json_decode($this->Link_Data, true); } public function Tracker_ForTemplate($includeContent = true, $full = false) { $output = ArrayList::create(); $vars = ['Left' => 1]; if ($this->Success) { if ($includeContent) { $output->push( ArrayData::create( [ 'Title' => _t('SendThis_Log.Content', 'Content'), 'FormattedValue' => $this->Type, ] ) ); } if (($tracker = $this->Tracker) && !empty($tracker)) { $exclude = $this->excludeFromTrackerReports; if (!$full) { $exclude[] = 'UserAgentString'; } foreach ($tracker as $title => $value) { if ($title == 'Client') { $formattedValue = isset($tracker['Icon']) ? '<img src="' . $tracker['Icon'] . '" alt="" class="icon-tiny" /> ' . $this->getClientFromTracker($value, $tracker) : $this->getClientFromTracker($value, $tracker); if (isset($tracker['ClientLink'])) { $formattedValue = '<a href="' . $tracker['ClientLink'] . '" target="_blank">' . $formattedValue . '</a>'; } $output->push( ArrayData::create( [ 'Title' => _t('SendThis_Log.LABEL-Client', 'Client'), 'FormattedValue' => $formattedValue, ] ) ); } elseif ($title == 'OperatingSystemFull') { $output->push( ArrayData::create( [ 'Title' => _t('SendThis_Log.LABEL-OS', 'Operating System'), 'FormattedValue' => isset($tracker['OperatingSystemIcon']) ? '<img src="' . $tracker['OperatingSystemIcon'] . '" alt="" class="icon-tiny" /> ' . $value : $value, ] ) ); } elseif ($value && !in_array($title, $exclude)) { $output->push( ArrayData::create( [ 'Title' => _t( 'SendThis_Log.LABEL-' . str_replace(' ', '_', $title), ucfirst(FormField::name_to_label($title)) ), 'FormattedValue' => $value, ] ) ); } } } if ($this->Track_Links && $this->Links()->exists()) { $output->push( ArrayData::create( [ 'Title' => _t('SendThis_Log.LABEL-LinksClicked', 'Links clicked'), 'FormattedValue' => $this->Links()->sum('Clicks'), ] ) ); $output->push( ArrayData::create( [ 'Title' => _t('SendThis_Log.LABEL-LinksUnique', 'Unique clicks'), 'FormattedValue' => $this->Links()->sum('Visits'), ] ) ); } } else { $vars['Message'] = $this->Notes; } $vars['Values'] = $output; return $this->renderWith('SendThis_Log_Tracker', $vars); } public function getTrackerField($field) { $tracker = $this->Tracker; if (isset($tracker) && !empty($tracker) && isset($tracker[$field])) { return $tracker[$field]; } return null; } public function init($type = 'html', &$headers = null) { $this->Type = $type; if ($headers && is_array($headers)) { if (isset($headers['X-SilverStripeMessageID'])) { $this->MessageID = $headers['X-SilverStripeMessageID']; } elseif (isset($headers['X-MilkywayMessageID'])) { $this->MessageID = $headers['X-MilkywayMessageID']; } elseif (isset($headers['Message-ID'])) { $this->MessageID = $headers['Message-ID']; } } return $this; } public function canView($member = null) { $method = __FUNCTION__; $this->beforeExtending(__FUNCTION__, function ($member) use ($method) { if (Permission::check('CAN_VIEW_SEND_LOGS', 'any', $member)) { return true; } }); return parent::canView($member); } protected function getClientFromTracker($client, $tracked = null) { if (!$tracked) { $tracked = $this->Tracker; } if (strtolower($client) == 'gmail image proxy') { return _t('SendThis_Log.WEB_CLIENT-GMAIL', 'Gmail'); } if (isset($tracked['Type']) && strtolower($tracked['Type']) == 'email client') { $client = $tracked['Client']; } elseif (isset($tracked['Type']) && strtolower($tracked['Type']) == 'browser' || strtolower($tracked['Type']) == 'mobile browser') { if (isset($tracked['ClientFull']) && !preg_match('/.*[0-9]$/', $tracked['ClientFull'])) { $client = _t( 'SendThis_Log.EMAIL_CLIENT-MAC', 'Mac Client (Apple Mail or Microsoft Entourage)' ); } elseif (isset($tracked['Referrer'])) { foreach ($this->config()->web_based_clients as $name => $url) { if (preg_match("/$url/", $tracked['Referrer'])) { $client = _t( 'SendThis_Log.WEB_CLIENT-' . strtoupper(str_replace(' ', '_', $name)), $name ); break; } } } if (!$client) { $client = _t('SendThis_Log.BROWSER_BASED', 'Web Browser'); } } return $client; } } |