/** * Save the attachment to the file */ function SaveAttachmentFile($attachid, $filename, $filecontent) { global $adb; $dirname = decideFilePath(); if (!is_dir($dirname)) { mkdir($dirname); } $description = $filename; $filename = str_replace(' ', '_', $filename); $saveasfile = "{$dirname}{$attachid}" . "_{$filename}"; if (!file_exists($saveasfile)) { $fh = fopen($saveasfile, 'wb'); fwrite($fh, base64_decode($filecontent)); fclose($fh); } $mimetype = MailAttachmentMIME::detect($saveasfile); $adb->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?", array($attachid, $filename, $description, $mimetype, $dirname)); }
function _SaveAttachmentFile($attachid, $filename, $filecontent) { require_once 'modules/OSSMail/MailAttachmentMIME.php'; $adb = PearDatabase::getInstance(); $dirname = Vtiger_Functions::initStorageFileDirectory('OSSMailView'); if (!is_dir($dirname)) { mkdir($dirname); } $filename = str_replace(' ', '-', $filename); $filename = str_replace(':', '-', $filename); $filename = str_replace('/', '-', $filename); $saveasfile = "{$dirname}{$attachid}" . "_{$filename}"; if (!file_exists($saveasfile)) { $fh = fopen($saveasfile, 'wb'); fwrite($fh, $filecontent); fclose($fh); } $mimetype = MailAttachmentMIME::detect($saveasfile); $adb->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?", array($attachid, $filename, $description, $mimetype, $dirname)); return true; }
/** * Save the Mail Attachments to DB * @global PearDataBase Instance $adb * @global Users Instance $current_user * @global Array $upload_badext * @param String $filename - name of the file * @param Text $filecontent * @return Array with attachment information */ function __SaveAttachmentFile($filename, $filecontent) { require_once 'modules/Settings/MailScanner/core/MailAttachmentMIME.php'; global $adb, $current_user, $upload_badext; $dirname = decideFilePath(); $usetime = $adb->formatDate(date('ymdHis'), true); $binFile = sanitizeUploadFileName($filename, $upload_badext); $attachid = $adb->getUniqueId('vtiger_crmentity'); $saveasfile = "{$dirname}/{$attachid}" . "_" . $binFile; $fh = fopen($saveasfile, 'wb'); fwrite($fh, $filecontent); fclose($fh); $mimetype = MailAttachmentMIME::detect($saveasfile); $adb->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,\n\t\t\t\tmodifiedby, setype, description, createdtime, modifiedtime, presence, deleted)\n\t\t\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($attachid, $current_user->id, $current_user->id, $current_user->id, "MailManager Attachment", $binFile, $usetime, $usetime, 1, 0)); $adb->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?", array($attachid, $binFile, $binFile, $mimetype, $dirname)); $attachInfo = array('attachid' => $attachid, 'path' => $dirname, 'name' => $binFile, 'type' => $mimetype, 'size' => filesize($saveasfile)); return $attachInfo; }
/** * Save attachments from the email and add it to the module record. * @global PearDataBase $db * @global String $root_directory * @param MailManager_Message_Model $mailrecord * @param String $basemodule * @param Vtiger_Record_Model $recordModel */ public function __SaveAttachements($mailrecord, $basemodule, $recordModel) { $db = PearDatabase::getInstance(); // If there is no attachments return if (!$mailrecord->_attachments) { return; } $userid = $recordModel->get('assigned_user_id'); $recordId = $recordModel->getId(); $setype = "{$basemodule} Attachment"; $date_var = $db->formatDate(date('YmdHis'), true); foreach ($mailrecord->_attachments as $filename => $filecontent) { if (empty($filecontent)) { continue; } $attachid = $db->getUniqueId('vtiger_crmentity'); $description = $filename; $usetime = $db->formatDate($date_var, true); $db->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,\n\t\t\t\tmodifiedby, setype, description, createdtime, modifiedtime, presence, deleted)\n\t\t\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($attachid, $userid, $userid, $userid, $setype, $description, $usetime, $usetime, 1, 0)); $issaved = $this->__SaveAttachmentFile($attachid, $filename, $filecontent); if ($issaved) { // To compute file size & type $attachRes = $db->pquery("SELECT * FROM vtiger_attachments WHERE attachmentsid = ?", array($attachid)); if ($db->num_rows($attachRes)) { $filePath = $db->query_result($attachRes, 0, 'path'); $completeFilePath = vglobal('root_directory') . $filePath . $attachid . '_' . $filename; if (file_exists($completeFilePath)) { $fileSize = filesize($completeFilePath); $mimetype = MailAttachmentMIME::detect($completeFilePath); } } // Link file attached to emails also, for it to appear on email's page if (!empty($recordId) && !empty($attachid)) { $this->relateAttachment($recordId, $attachid); } } } }
/** * Creates an Attachments * @global PearDataBase $adb * @global Array $upload_badext * @global Users $current_user */ function saveAttachment() { global $adb, $upload_badext, $current_user; $uploadPath = decideFilePath(); $fileName = $this->getName(); if (!empty($fileName)) { $attachid = $adb->getUniqueId('vtiger_crmentity'); //sanitize the filename $binFile = sanitizeUploadFileName($fileName, $upload_badext); $fileName = ltrim(basename(" " . $binFile)); $saveAttachment = $this->save($uploadPath . $attachid . "_" . $fileName); if ($saveAttachment) { $description = $fileName; $date_var = $adb->formatDate(date('YmdHis'), true); $usetime = $adb->formatDate($date_var, true); $adb->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,\n\t\t\t\tmodifiedby, setype, description, createdtime, modifiedtime, presence, deleted)\n\t\t\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($attachid, $current_user->id, $current_user->id, $current_user->id, "Documents Attachment", $description, $usetime, $usetime, 1, 0)); $mimetype = MailAttachmentMIME::detect($uploadPath . $attachid . "_" . $fileName); $adb->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?", array($attachid, $fileName, $description, $mimetype, $uploadPath)); return $attachid; } } return false; }
function add_attachment_to_contact($cid, $email, $emailid) { // add vtiger_attachments to contact global $adb, $current_user, $default_charset; for ($j = 0; $j < 2; $j++) { if ($j == 0) { $attachments = $email->downloadAttachments(); } else { $attachments = $email->downloadInlineAttachments(); } $upload_filepath = decideFilePath(); for ($i = 0, $num_files = count($attachments); $i < $num_files; $i++) { $current_id = $adb->getUniqueID("vtiger_crmentity"); $date_var = $adb->formatDate(date('Y-m-d H:i:s'), true); $filename = preg_replace("/[ ()-]+/", "_", $attachments[$i]["filename"]); preg_match_all('/=\\?([^\\?]+)\\?([^\\?]+)\\?([^\\?]+)\\?=/', $filename, $matches); $totalmatches = count($matches[0]); for ($index = 0; $index < $totalmatches; ++$index) { $charset = $matches[1][$index]; $encoding = strtoupper($matches[2][$index]); $data = $matches[3][$index]; if ($encoding == 'B') { $filename = base64_decode($data); } else { if ($encoding == 'Q') { $filename = quoted_printable_decode($data); } } $filename = iconv(str_replace('_', '-', $charset), $default_charset, $filename); } $saveasfile = $upload_filepath . '/' . $current_id . '_' . $filename; $filetype = MailAttachmentMIME::detect($saveasfile); $filesize = $attachments[$i]["filesize"]; $query = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values(?,?,?,?,?,?,?)"; $qparams = array($current_id, $current_user->id, $current_user->id, 'Contacts Attachment', 'Uploaded from webmail during qualification', $date_var, $date_var); $result = $adb->pquery($query, $qparams); $sql = "insert into vtiger_attachments (attachmentsid,name,description,type,path) values(?,?,?,?,?)"; $params = array($current_id, $filename, 'Uploaded ' . $filename . ' from webmail', $filetype, $upload_filepath); $result = $adb->pquery($sql, $params); if (!empty($result)) { // Create document record $document = new Documents(); $document->column_fields['notes_title'] = $filename; $document->column_fields['filename'] = $filename; $document->column_fields['filesize'] = $filesize; $document->column_fields['filetype'] = $filetype; $document->column_fields['filestatus'] = 1; $document->column_fields['filelocationtype'] = 'I'; $document->column_fields['folderid'] = 1; // Default Folder $document->column_fields['assigned_user_id'] = $current_user->id; $document->save('Documents'); $sql1 = "insert into vtiger_senotesrel values(?,?)"; $params1 = array($cid, $document->id); $result = $adb->pquery($sql1, $params1); $sql1 = "insert into vtiger_seattachmentsrel values(?,?)"; $params1 = array($document->id, $current_id); $result = $adb->pquery($sql1, $params1); $sql1 = "insert into vtiger_seattachmentsrel values(?,?)"; $params1 = array($emailid, $current_id); $result = $adb->pquery($sql1, $params1); } //we have to add attachmentsid_ as prefix for the filename $move_filename = $upload_filepath . '/' . $current_id . '_' . $filename; $fp = fopen($move_filename, "w") or die("Can't open file"); fputs($fp, base64_decode($attachments[$i]["filedata"])); fclose($fp); } } }
/** * Save the attachment to the file */ function __SaveAttachmentFile($attachid, $filename, $filecontent) { global $adb; $dirname = $this->STORAGE_FOLDER; if (!is_dir($dirname)) { mkdir($dirname); } $description = $filename; $filename = str_replace(' ', '-', $filename); $saveasfile = "{$dirname}{$attachid}" . "_{$filename}"; if (!file_exists($saveasfile)) { $this->log("Saved attachement as {$saveasfile}\n"); $fh = fopen($saveasfile, 'wb'); fwrite($fh, $filecontent); fclose($fh); } $mimetype = MailAttachmentMIME::detect($saveasfile); $adb->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?", array($attachid, $filename, $description, $mimetype, $dirname)); return true; }
/** * Save attachments from the email and add it to the module record. * @global PearDataBase $adb * @global String $root_directory * @param MailManager_Message_Model $mailrecord * @param String $basemodule * @param CRMEntity $basefocus */ function __SaveAttachements($mailrecord, $basemodule, $basefocus) { global $adb, $root_directory; // If there is no attachments return if (!$mailrecord->_attachments) { return; } $userid = $basefocus->column_fields['assigned_user_id']; $setype = "{$basemodule} Attachment"; $date_var = $adb->formatDate(date('YmdHis'), true); foreach ($mailrecord->_attachments as $filename => $filecontent) { if (empty($filecontent)) { continue; } $attachid = $adb->getUniqueId('vtiger_crmentity'); $description = $filename; $usetime = $adb->formatDate($date_var, true); $adb->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,\n\t\t\t\tmodifiedby, setype, description, createdtime, modifiedtime, presence, deleted)\n\t\t\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($attachid, $userid, $userid, $userid, $setype, $description, $usetime, $usetime, 1, 0)); $issaved = $this->__SaveAttachmentFile($attachid, $filename, $filecontent); if ($issaved) { // To compute file size & type $attachRes = $adb->pquery("SELECT * FROM vtiger_attachments WHERE attachmentsid = ?", array($attachid)); if ($adb->num_rows($attachRes)) { $filePath = $adb->query_result($attachRes, 0, 'path'); $completeFilePath = $root_directory . $filePath . $attachid . '_' . $filename; if (file_exists($completeFilePath)) { $fileSize = filesize($completeFilePath); $mimetype = MailAttachmentMIME::detect($completeFilePath); } } // Create document record $docInfo = array('title' => $filename, 'filename' => $filename, 'assigneduser' => $userid, 'size' => $fileSize, 'filetype' => $mimetype); $documentId = $this->createDocument($docInfo); // Link file attached to document if (!empty($documentId) && !empty($attachid)) { $this->relateAttachment($documentId, $attachid); } // Link document to base record if (!empty($basefocus->id) && !empty($documentId)) { $this->relatedDocument($basefocus->id, $documentId); } // Link file attached to emails also, for it to appear on email's page if (!empty($basefocus->id) && !empty($attachid)) { $this->relateAttachment($basefocus->id, $attachid); } } } }
/** * Save the Mail Attachments to DB * @global PearDataBase Instance $db * @global Users Instance $currentUserModel * @global Array $upload_badext * @param String $filename - name of the file * @param Text $filecontent * @return Array with attachment information */ public function __SaveAttachmentFile($filename, $filecontent) { require_once 'modules/Settings/MailConverter/handlers/MailAttachmentMIME.php'; $db = PearDatabase::getInstance(); $currentUserModel = Users_Record_Model::getCurrentUserModel(); $filename = imap_utf8($filename); $dirname = decideFilePath(); $usetime = $db->formatDate(date('ymdHis'), true); $binFile = sanitizeUploadFileName($filename, vglobal('upload_badext')); $attachid = $db->getUniqueId('vtiger_crmentity'); $saveasfile = "{$dirname}/{$attachid}" . "_" . $binFile; $fh = fopen($saveasfile, 'wb'); fwrite($fh, $filecontent); fclose($fh); $mimetype = MailAttachmentMIME::detect($saveasfile); $db->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,\n\t\t\t\tmodifiedby, setype, description, createdtime, modifiedtime, presence, deleted)\n\t\t\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($attachid, $currentUserModel->getId(), $currentUserModel->getId(), $currentUserModel->getId(), "MailManager Attachment", $binFile, $usetime, $usetime, 1, 0)); $db->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?", array($attachid, $binFile, $binFile, $mimetype, $dirname)); $attachInfo = array('attachid' => $attachid, 'path' => $dirname, 'name' => $binFile, 'type' => $mimetype, 'size' => filesize($saveasfile)); return $attachInfo; }