function classifyFile($classification_data, $email, $parsedEmail, $validWS, $mantainWs = true, $csv = '')
 {
     if (!is_array($classification_data)) {
         $classification_data = array();
     }
     if (!isset($parsedEmail["Attachments"])) {
         throw new Exception(lang('no attachments found for email'));
     }
     for ($c = 0; $c < count($classification_data); $c++) {
         if (isset($classification_data["att_" . $c]) && $classification_data["att_" . $c]) {
             $att = $parsedEmail["Attachments"][$c];
             $fName = str_starts_with($att["FileName"], "=?") ? iconv_mime_decode($att["FileName"], 0, "UTF-8") : utf8_safe($att["FileName"]);
             if (trim($fName) == "" && strlen($att["FileName"]) > 0) {
                 $fName = utf8_encode($att["FileName"]);
             }
             try {
                 $file = ProjectFiles::findOne(array('conditions' => "`filename` = " . DB::escape($fName) . " AND `mail_id` = " . $email->getId()));
                 DB::beginWork();
                 if ($file == null) {
                     $fileIsNew = true;
                     $file = new ProjectFile();
                     $file->setFilename($fName);
                     $file->setIsVisible(true);
                     $file->setIsPrivate(false);
                     $file->setIsImportant(false);
                     $file->setCommentsEnabled(true);
                     $file->setAnonymousCommentsEnabled(false);
                     $file->setMailId($email->getId());
                     $file->save();
                 } else {
                     $fileIsNew = false;
                 }
                 if (!$mantainWs && !$fileIsNew) {
                     $file->removeFromWorkspaces(logged_user()->getWorkspacesQuery());
                 }
                 foreach ($validWS as $w) {
                     if (!$file->hasWorkspace($w)) {
                         $file->addToWorkspace($w);
                     }
                 }
                 $file->setTagsFromCSV($csv);
                 $enc = array_var($parsedMail, 'Encoding', 'UTF-8');
                 $ext = utf8_substr($fName, strrpos($fName, '.') + 1, utf8_strlen($fName, $enc), $enc);
                 $mime_type = '';
                 if (Mime_Types::instance()->has_type($att["content-type"])) {
                     $mime_type = $att["content-type"];
                     //mime type is listed & valid
                 } else {
                     $mime_type = Mime_Types::instance()->get_type($ext);
                     //Attempt to infer mime type
                 }
                 if ($fileIsNew) {
                     $tempFileName = ROOT . "/tmp/" . logged_user()->getId() . "x" . gen_id();
                     $fh = fopen($tempFileName, 'w') or die("Can't open file");
                     fwrite($fh, $att["Data"]);
                     fclose($fh);
                     $fileToSave = array("name" => $fName, "type" => $mime_type, "tmp_name" => $tempFileName, "error" => 0, "size" => filesize($tempFileName));
                     $revision = $file->handleUploadedFile($fileToSave, true, lang('attachment from email', $email->getSubject()));
                     // handle uploaded file
                     $email->linkObject($file);
                     ApplicationLogs::createLog($file, $email->getWorkspaces(), ApplicationLogs::ACTION_ADD);
                 }
                 DB::commit();
                 // Error...
             } catch (Exception $e) {
                 DB::rollback();
                 flash_error($e->getMessage());
                 ajx_current("empty");
             }
             if (isset($tempFileName) && is_file($tempFileName)) {
                 unlink($tempFileName);
             }
         }
     }
 }
 function classifyFile($classification_data, $email, $parsedEmail, $members, $remove_prev, $use_transaction)
 {
     if (!is_array($classification_data)) {
         $classification_data = array();
     }
     if (!isset($parsedEmail["Attachments"])) {
         return;
         //throw new Exception(lang('no attachments found for email'));
     }
     $account_owner = logged_user() instanceof contact ? logged_user() : Contacts::findById($email->getAccount()->getContactId());
     for ($c = 0; $c < count($classification_data); $c++) {
         if (isset($classification_data["att_" . $c]) && $classification_data["att_" . $c] && isset($parsedEmail["Attachments"][$c])) {
             // dont classify inline images
             if (array_var($parsedEmail["Attachments"][$c], 'FileDisposition') == 'attachment') {
                 $att = $parsedEmail["Attachments"][$c];
                 $fName = str_starts_with($att["FileName"], "=?") ? iconv_mime_decode($att["FileName"], 0, "UTF-8") : utf8_safe($att["FileName"]);
                 if (trim($fName) == "" && strlen($att["FileName"]) > 0) {
                     $fName = utf8_encode($att["FileName"]);
                 }
                 $extension = get_file_extension(basename($fName));
                 $type_file_allow = FileTypes::getByExtension($extension);
                 if (!$type_file_allow instanceof FileType || $type_file_allow->getIsAllow() == 1) {
                     try {
                         $remove_previous_members = $remove_prev;
                         // check for file name and size, if there are some then compare the contents, if content is equal do not classify the attachment.
                         $file_exists = 0;
                         $possible_equal_file_rows = DB::executeAll("SELECT * FROM " . TABLE_PREFIX . "project_file_revisions r \r\n\t\t\t\t\t\t\t\tINNER JOIN " . TABLE_PREFIX . "objects o ON o.id=r.file_id  \r\n\t\t\t\t\t\t\t\tINNER JOIN " . TABLE_PREFIX . "project_files f ON f.object_id=r.file_id\r\n\t\t\t\t\t\t\t\tWHERE o.name=" . DB::escape($fName) . " AND r.filesize='" . strlen($att["Data"]) . "' \r\n\t\t\t\t\t\t\t\tAND r.revision_number=(SELECT max(r2.revision_number) FROM " . TABLE_PREFIX . "project_file_revisions r2 WHERE r2.file_id=r.file_id)");
                         if (is_array($possible_equal_file_rows)) {
                             foreach ($possible_equal_file_rows as $row) {
                                 $content = FileRepository::getFileContent($row['repository_id']);
                                 if ($content == $att['Data']) {
                                     // file already exists
                                     $file_exists = $row['file_id'];
                                     //Logger::log($email->getId()." - ".$row['mail_id']." - $fName");
                                     if ($remove_previous_members && $row['mail_id'] != $email->getId()) {
                                         $remove_previous_members = false;
                                     }
                                     break;
                                 }
                             }
                         }
                         if ($file_exists > 0) {
                             $file = ProjectFiles::findById($file_exists);
                         } else {
                             $file = ProjectFiles::findOne(array('conditions' => "mail_id = " . $email->getId() . " AND o.name = " . DB::escape($fName) . ""));
                         }
                         if ($use_transaction) {
                             DB::beginWork();
                         }
                         if ($file == null) {
                             $fileIsNew = true;
                             $file = new ProjectFile();
                             $file->setFilename($fName);
                             $file->setIsVisible(true);
                             $file->setMailId($email->getId());
                             $file->setCreatedById($account_owner->getId());
                             $file->save();
                         } else {
                             $fileIsNew = false;
                         }
                         if ($remove_previous_members) {
                             $dim_ids = array(0);
                             foreach ($members as $m) {
                                 $dim_ids[$m->getDimensionId()] = $m->getDimensionId();
                             }
                             ObjectMembers::delete('`object_id` = ' . $file->getId() . ' AND `member_id` IN (SELECT `m`.`id` FROM `' . TABLE_PREFIX . 'members` `m` WHERE `m`.`dimension_id` IN (' . implode(',', $dim_ids) . '))');
                         }
                         $file->addToMembers($members);
                         // fill sharing table in background
                         add_object_to_sharing_table($file, $account_owner);
                         //$file->addToSharingTable();
                         $enc = array_var($parsedMail, 'Encoding', 'UTF-8');
                         $ext = utf8_substr($fName, strrpos($fName, '.') + 1, utf8_strlen($fName, $enc), $enc);
                         $mime_type = '';
                         if (Mime_Types::instance()->has_type($att["content-type"])) {
                             $mime_type = $att["content-type"];
                             //mime type is listed & valid
                         } else {
                             $mime_type = Mime_Types::instance()->get_type($ext);
                             //Attempt to infer mime type
                         }
                         $userid = logged_user() ? logged_user()->getId() : "0";
                         $tempFileName = ROOT . "/tmp/" . $userid . "x" . gen_id();
                         $fh = fopen($tempFileName, 'w') or die("Can't open file");
                         fwrite($fh, $att["Data"]);
                         fclose($fh);
                         $fileToSave = array("name" => $fName, "type" => $mime_type, "tmp_name" => $tempFileName, "error" => 0, "size" => filesize($tempFileName));
                         if ($fileIsNew || !$file->getLastRevision() instanceof ProjectFileRevision) {
                             $revision = $file->handleUploadedFile($fileToSave, true, lang('attachment from email', $email->getSubject()));
                             // handle uploaded file
                             $revision->setCreatedById($account_owner->getId());
                             $revision->save();
                             ApplicationLogs::createLog($file, ApplicationLogs::ACTION_ADD);
                             /*	}else{
                             			$revision = $file->getLastRevision();
                             			$new_hash = hash_file("sha256", $tempFileName);
                             			if ($revision->getHash() != $new_hash) {
                             				$revision = $file->handleUploadedFile($fileToSave, true, lang('attachment from email', $email->getSubject())); // handle uploaded file
                             				ApplicationLogs::createLog($file, ApplicationLogs::ACTION_ADD);
                             			}*/
                         }
                         if ($use_transaction) {
                             DB::commit();
                         }
                         // Error...
                     } catch (Exception $e) {
                         if ($use_transaction) {
                             DB::rollback();
                         }
                         flash_error($e->getMessage());
                         ajx_current("empty");
                     }
                 } else {
                     flash_error(lang('file extension no allow classify', $fName));
                 }
                 if (isset($tempFileName) && is_file($tempFileName)) {
                     unlink($tempFileName);
                 }
             }
         }
     }
 }
예제 #3
0
/**
 * Call back function for file link
 * 
 * @param mixed $matches
 * @return
 */
function replace_file_link_callback($matches)
{
    if (count($matches) < 2) {
        return null;
    }
    // if
    if (!logged_user()->isMemberOfOwnerCompany()) {
        $object = ProjectFiles::findOne(array('conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
    } else {
        $object = ProjectFiles::findOne(array('conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
    }
    // if
    if (!$object instanceof ProjectFile) {
        return '<del>' . lang('invalid reference') . '</del>';
    } else {
        return '<a href="' . $object->getViewUrl() . '">' . $object->getFilename() . '</a>';
    }
    // if
}
예제 #4
0
/**
 * Call back function for file link
 * 
 * @param mixed $matches
 * @return
 */
function replace_file_link_callback($matches)
{
    if (count($matches) < 2) {
        return null;
    }
    // if
    if (!logged_user()->isMemberOfOwnerCompany()) {
        $object = ProjectFiles::findOne(array('conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
    } else {
        $object = ProjectFiles::findOne(array('conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
    }
    // if
    if (!$object instanceof ProjectFile) {
        return '<del>' . lang('invalid reference', $matches[0]) . '</del>';
    } else {
        if ($object->getFileType()->getIsImage()) {
            return '<img src="' . externalUrl($object->getDownloadUrl()) . '" alt="' . lang('file') . '" style="max-width:100%; max-height:100%;" />';
        }
        return '<a href="' . externalUrl($object->getViewUrl()) . '" title="' . lang('file') . '">' . $object->getFilename() . '</a>';
    }
    // if
}