1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14: 
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: global $area, $action, $perm;
 19: 
 20: $page = new cGuiPage('mail_log_overview');
 21: $page->addScript('mail_log.js');
 22: 
 23: if (!$perm->have_perm_area_action($area)) {
 24:     $notification = new cGuiNotification();
 25:     $page->displayError(i18n('Permission denied'));
 26:     $page->render();
 27:     exit();
 28: }
 29: 
 30: 
 31: if (getSystemProperty('system', 'mail_log') === 'false') {
 32:     $page->displayWarning(i18n('Mail logging is disabled!'));
 33: }
 34: 
 35: $mailLogCollection = new cApiMailLogCollection();
 36: 
 37: 
 38: if ($action == 'mail_log_delete') {
 39:     
 40:     
 41:     if (!empty($_REQUEST['idmails'])) {
 42:         $mailLogSuccessCollection = new cApiMailLogSuccessCollection();
 43:         $idmails = json_decode($_REQUEST['idmails'], true);
 44:         foreach ($idmails as $idmail) {
 45:             $mailLogCollection->delete($idmail);
 46:             
 47:             $mailLogSuccessCollection->deleteBy('idmail', $idmail);
 48:         }
 49:     }
 50: } else if ($action == 'mail_log_resend') {
 51:     $mailer = new cMailer();
 52:     $mailer->resendMail($_REQUEST['idmailsuccess']);
 53: }
 54: 
 55: $mailLogCollection->query();
 56: 
 57: if ($mailLogCollection->count() === 0) {
 58:     $page->displayInfo(i18n('No mails have been logged yet.'));
 59:     $page->render();
 60:     exit();
 61: }
 62: 
 63: if ($area === 'mail_log' || $area === 'mail_log_overview') {
 64:     
 65:     $form = new cHTMLForm('bulk_editing', '', 'post', 'action-form');
 66:     $form->setVar('area', '');
 67:     $form->setVar('frame', '4');
 68:     $form->setVar('action', '');
 69:     $form->setVar('contenido', cRegistry::getSession()->id);
 70:     $form->setVar('idmail', '');
 71:     $form->setVar('idmails', '');
 72:     $page->appendContent($form);
 73: 
 74:     
 75:     $page->appendContent(mailLogBulkEditingFunctions());
 76: 
 77:     
 78:     $table = new cHTMLTable();
 79:     $table->setClass('generic');
 80:     $table->setWidth('100%');
 81: 
 82:     
 83:     $headers = array(
 84:         'checkbox' => i18n('Mark'), 'subject' => i18n('Subject'), 'to' => i18n('To'), 'created' => i18n('Date'), 'client' => i18n('Client'), 'action' => i18n('Action')
 85:     );
 86:     $thead = new cHTMLTableHeader();
 87:     $tr = new cHTMLTableRow();
 88:     foreach ($headers as $header) {
 89:         $th = new cHTMLTableHead();
 90:         $th->setContent($header);
 91:         $tr->appendContent($th);
 92:     }
 93:     $thead->setContent($tr);
 94:     $table->appendContent($thead);
 95: 
 96:     
 97:     $tbody = new cHTMLTableBody();
 98:     
 99:     while (($item = $mailLogCollection->next()) !== false) {
100:         $tr = new cHTMLTableRow();
101:         foreach ($headers as $key => $value) {
102:             $td = new cHTMLTableData();
103:             switch ($key) {
104:                 case 'checkbox':
105:                     $checkbox = new cHTMLCheckbox('', $item->get('idmail'), '', false, '', '', '', 'mark_emails');
106:                     $td->setContent($checkbox->toHtml(false));
107:                     break;
108:                 case 'client':
109:                     $idclient = $item->get('idclient');
110:                     $clientItem = new cApiClient($idclient);
111:                     $td->setContent($clientItem->get('name') . ' ');
112:                     break;
113:                 case 'from':
114:                 case 'to':
115:                     $addresses = $item->get($key);
116:                     $addresses = mailLogDecodeAddresses($addresses);
117:                     $td->setContent($addresses . ' ');
118:                     break;
119:                 case 'action':
120:                     
121:                     $img = new cHTMLImage('images/info.gif');
122:                     $link = new cHTMLLink('javascript:void(0)');
123:                     $link->setEvent('click', 'showInfo(' . $item->get('idmail') . ');');
124:                     $link->setContent($img);
125:                     $link->appendStyleDefinition('margin-right', '5px');
126:                     $td->appendContent($link);
127:                     
128:                     $img = new cHTMLImage('images/delete.gif');
129:                     $link = new cHTMLLink('javascript:void(0)');
130:                     $link->setEvent('click', 'showConfirmation("' . i18n('Do you really want to delete this email?') . '", function() { deleteEmails(' . $item->get('idmail') . '); });return false;');
131:                     $link->setContent($img);
132:                     $td->appendContent($link);
133:                     break;
134:                 default:
135:                     $td->setContent($item->get($key) . ' ');
136:             }
137:             $tr->appendContent($td);
138:         }
139:         $tbody->appendContent($tr);
140:     }
141:     $table->appendContent($tbody);
142:     $page->appendContent($table);
143: 
144:     
145:     $page->appendContent(mailLogBulkEditingFunctions());
146: } else if ($area === 'mail_log_detail') {
147:     if (isset($_REQUEST['idmail']) && is_numeric($_REQUEST['idmail'])) {
148:         
149:         $link = new cHTMLLink(cRegistry::getBackendUrl() . 'main.php?area=mail_log&frame=4&contenido=' . cRegistry::getSession()->id);
150:         $image = new cHTMLImage('images/but_back.gif');
151:         $image->appendStyleDefinition('cursor', 'pointer');
152:         $image->appendStyleDefinition('margin-bottom', '10px');
153:         $link->setContent($image);
154:         $page->appendContent($link);
155: 
156:         $idmail = $_REQUEST['idmail'];
157:         
158:         $form = new cHTMLForm('', '', 'post', 'action-form');
159:         $form->setVar('area', '');
160:         $form->setVar('frame', '4');
161:         $form->setVar('action', 'mail_log_overview');
162:         $form->setVar('contenido', cRegistry::getSession()->id);
163:         $form->setVar('idmail', $idmail);
164:         $form->setVar('idmails', '[' . $idmail . ']');
165:         $form->setVar('idmailsuccess', '');
166:         $page->appendContent($form);
167: 
168:         
169:         $tableHeaderDetail = array(
170:             'from' => i18n('From'), 'to' => i18n('To'), 'reply_to' => i18n('Reply to'), 'cc' => i18n('CC'), 'bcc' => i18n('BCC'), 'subject' => i18n('Subject'), 'body' => i18n('Body'), 'created' => i18n('Date')
171:         );
172:         $table = new cHTMLTable();
173:         $table->setClass('generic');
174:         $table->appendStyleDefinition('border-top', '1px solid #B3B3B3');
175:         $mailItem = new cApiMailLog($idmail);
176:         foreach ($tableHeaderDetail as $key => $value) {
177:             $tr = new cHTMLTableRow();
178:             $td = new cHTMLTableData();
179:             $td->setContent($value . ' ');
180:             $tr->appendContent($td);
181:             switch ($key) {
182:                 
183:                 case 'from':
184:                 case 'to':
185:                 case 'reply_to':
186:                 case 'cc':
187:                 case 'bcc':
188:                     $td = new cHTMLTableData();
189:                     $addresses = mailLogDecodeAddresses($mailItem->get($key));
190:                     $td->setContent($addresses . ' ');
191:                     $tr->appendContent($td);
192:                     break;
193:                 default:
194:                     $td = new cHTMLTableData();
195:                     $data = $mailItem->get($key);
196:                     if ($mailItem->get('content_type') === 'text/plain') {
197:                         $data = nl2br($data);
198:                     }
199:                     $td->setContent($data . ' ');
200:                     $tr->appendContent($td);
201:             }
202:             $table->appendContent($tr);
203:         }
204:         
205:         $tr = new cHTMLTableRow();
206:         $td = new cHTMLTableData();
207:         $td->setContent(i18n('Action'));
208:         $tr->appendContent($td);
209:         $td = new cHTMLTableData();
210:         $link = new cHTMLLink('javascript:void(0)');
211:         $link->setClass('con_deletemails');
212:         $link->setEvent('click', 'showConfirmation("' . i18n('Do you really want to delete this email?') . '", function() { deleteEmails(' . $idmail . '); });return false;');
213:         $image = new cHTMLImage('images/delete.gif');
214:         $image->setAlt(i18n('Delete emails'));
215:         $link->setContent($image);
216:         $td->setContent($link);
217:         $tr->appendContent($td);
218:         $table->appendContent($tr);
219:         $page->appendContent($table);
220: 
221:         
222:         $successTable = new cHTMLTable();
223:         $successTable->setClass('generic');
224:         $successTable->appendStyleDefinition('margin-top', '20px');
225:         
226:         $mailLogSuccessCollection = new cApiMailLogSuccessCollection();
227:         $mailLogSuccessCollection->select('`idmail`=' . $idmail);
228:         $tr = new cHTMLTableRow();
229:         $th = new cHTMLTableHead();
230:         $th->setContent(i18n('Recipient'));
231:         $tr->appendContent($th);
232:         $th = new cHTMLTableHead();
233:         $th->setContent(i18n('Status'));
234:         $tr->appendContent($th);
235:         $successTable->appendContent($tr);
236: 
237:         
238:         while (($mailSuccessItem = $mailLogSuccessCollection->next()) !== false) {
239:             $tr = new cHTMLTableRow();
240:             $td = new cHTMLTableData();
241:             $td->setContent(mailLogDecodeAddresses($mailSuccessItem->get('recipient')));
242:             $tr->appendContent($td);
243:             $td = new cHTMLTableData();
244:             if ($mailSuccessItem->get('success')) {
245:                 
246:                 $image = new cHTMLImage('images/but_ok.gif');
247:                 $image->appendStyleDefinition('display', 'block');
248:                 $image->appendStyleDefinition('margin', '0 auto');
249:                 $image->appendStyleDefinition('padding', '3px');
250:                 $image->setAlt(i18n('Mail sent successfully!'));
251:                 $td->setContent($image);
252:             } else {
253:                 
254:                 $link = new cHTMLLink('javascript:void(0)');
255:                 $link->setEvent('click', 'resendEmail(' . $mailSuccessItem->get('idmailsuccess') . ')');
256:                 $link->setAlt(i18n('Resend email'));
257:                 $image = new cHTMLImage('images/but_refresh.gif');
258:                 $image->appendStyleDefinition('display', 'block');
259:                 $image->appendStyleDefinition('margin', '0 auto');
260:                 $image->appendStyleDefinition('padding', '3px');
261:                 $link->setContent($image);
262:                 $td->setContent($link);
263:             }
264:             $tr->appendContent($td);
265:             $successTable->appendContent($tr);
266:         }
267:         $page->appendContent($successTable);
268:     } else {
269:         
270:         $contenidoNotification = new cGuiNotification();
271:         $contenidoNotification->displayNotification('error', i18n('No item selected!'));
272:     }
273: }
274: 
275: $page->render();
276: 
277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 
289: function mailLogDecodeAddresses($addresses) {
290:     $result = '';
291:     $addresses = json_decode($addresses, true);
292:     foreach ($addresses as $mail => $name) {
293:         $result .= $name . ' <' . $mail . '><br>';
294:     }
295:     $result = substr($result, 0, strlen($result) - 4);
296: 
297:     return $result;
298: }
299: 
300: function mailLogBulkEditingFunctions() {
301:     $table = new cHTMLTable();
302:     $table->setClass('generic');
303:     $table->setWidth('100%');
304:     $table->appendStyleDefinition('margin', '10px 0');
305:     $tr = new cHTMLTableRow();
306:     $th = new cHTMLTableHead();
307:     $th->appendStyleDefinition('border-bottom', '1px solid #B3B3B3');
308:     
309:     $link = new cHTMLLink();
310:     $link->setClass('flip_mark');
311:     $image = new cHTMLImage('images/but_invert_selection.gif');
312:     $image->setAlt(i18n('Flip Selection'));
313:     $link->appendContent($image);
314:     $link->appendContent(' ' . i18n('Flip Selection'));
315:     $th->appendContent($link);
316: 
317:     
318:     $link = new cHTMLLink('javascript:void(0)');
319:     $link->setClass('con_deletemails');
320:     $link->setEvent('click', 'showConfirmation("' . i18n('Do you really want to delete the selected emails?') . '", deleteEmails);return false;');
321:     $image = new cHTMLImage('images/delete.gif');
322:     $image->setAlt(i18n('Delete emails'));
323:     $link->setContent($image);
324:     $div = new cHTMLDiv(i18n('Apply to all selected emails:'), 'bulk_editing_functions');
325:     $div->appendContent($link);
326:     $th->appendContent($div);
327: 
328:     $tr->setContent($th);
329:     $table->setContent($tr);
330: 
331:     return $table;
332: }
333: