public static function save_google_docs($note_id) { $edit_url = DB::GetOne('SELECT doc_id FROM utils_attachment_googledocs WHERE note_id = %d', array($note_id)); if (!$edit_url) { Base_StatusBarCommon::message(__('Document not found'), 'warning'); return false; } if(!preg_match('/(spreadsheet|document)%3A(.+)$/i',$edit_url,$matches)) { Base_StatusBarCommon::message(__('Document not found'), 'warning'); return false; } $edit_url = $matches[2]; $doc = $matches[1]=='document'; if ($doc) $export_url = 'https://docs.google.com/feeds/download/documents/Export?id='.$edit_url.'&exportFormat=doc'; else $export_url = 'https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key='.$edit_url.'&exportFormat=xls'; DB::Execute('DELETE FROM utils_attachment_googledocs WHERE note_id = %d', array($note_id)); $g_auth = Utils_AttachmentCommon::get_google_auth(null, null, $doc?'writely':'wise'); $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $headers = array( "Authorization: GoogleLogin auth=" . $g_auth, "If-Match: *", "GData-Version: 3.0", ); curl_setopt($curl, CURLOPT_URL, $export_url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, false); $response = curl_exec_follow($curl); $row = DB::GetRow('SELECT f.*,l.f_crypted as crypted FROM utils_attachment_file f INNER JOIN utils_attachment_data_1 l ON l.id=f.attach_id WHERE f.id=%d',array($note_id)); $local = DATA_DIR.'/Utils_Attachment/temp/'.Acl::get_user().'/gdocs'; @mkdir($local,0777,true); $dest_file = $local.'/'.$row['id']; if($row['crypted']) { $password = $_SESSION['client']['cp'.$row['attach_id']]; $response = Utils_AttachmentCommon::encrypt($response,$password); } file_put_contents($dest_file, $response); if($doc) { $ext = 'docx'; } else $ext = 'xlsx'; $row['original'] = substr($row['original'],0,strrpos($row['original'],'.')).'.'.$ext; Utils_AttachmentCommon::add_file($row['attach_id'], Acl::get_user(), $row['original'], $dest_file); DB::Execute('UPDATE utils_attachment_file SET deleted=1 WHERE id=%d',array($row['id'])); $headers = array( "Authorization: GoogleLogin auth=" . $g_auth, "If-Match: *", "GData-Version: 3.0", ); curl_setopt($curl, CURLOPT_URL, $edit_url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, false); $response = curl_exec($curl); Base_StatusBarCommon::message(__('Changes saved')); }
$new_image = imagecreatetruecolor($w, $h); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $w, $h, $img_w, $img_h); switch ($image_type) { case IMAGETYPE_JPEG: imagejpeg($new_image, $f_filename, 95); break; case IMAGETYPE_GIF: imagegif($new_image, $f_filename); break; case IMAGETYPE_PNG: imagepng($new_image, $f_filename); break; } $buffer = file_get_contents($f_filename); if ($crypted) { $buffer = Utils_AttachmentCommon::encrypt($buffer, $password); file_put_contents($f_filename, $buffer); } } } else { $buffer = file_get_contents($f_filename); } } else { $f_filename = $meta['file']; if (!file_exists($f_filename)) { die('File doesn\'t exists'); } @ini_set('memory_limit', ceil(filesize($f_filename) * 2 / 1024 / 1024 + 64) . 'M'); $buffer = file_get_contents($f_filename); } if ($crypted) {
public static function submit_attachment($values, $mode) { static $new_values, $old_password; switch ($mode) { case 'browse': if (isset($values['id']) && isset($values['crypted']) && $values['crypted'] == false) { // store to mark as read. Do not mark it here, because // we won't get red eye in the table view self::$mark_as_read[] = $values['id']; } return $values; case 'index': if ($values['crypted']) { unset($values['note']); } return $values; case 'adding': $values['edited_on'] = time(); return $values; case 'add': case 'edit': if (isset($values['__date'])) { $values['edited_on'] = $values['__date']; } else { $values['edited_on'] = time(); } $crypted = 0; $old_pass = $mode == 'edit' && isset($_SESSION['client']['cp' . $values['id']]) ? $_SESSION['client']['cp' . $values['id']] : ($mode == 'add' && isset($values['clone_id']) && isset($_SESSION['client']['cp' . $values['clone_id']]) ? $_SESSION['client']['cp' . $values['clone_id']] : ''); if (is_array($values['crypted']) && isset($values['crypted']['crypted']) && $values['crypted']['crypted'] || !is_array($values['crypted']) && $values['crypted']) { if (is_array($values['crypted']) && isset($values['crypted']['note_password'])) { if ($values['crypted']['note_password'] == '*@#old@#*') { $values['crypted']['note_password'] = $old_pass; } } $crypted = 1; } if (is_array($values['crypted']) && isset($values['crypted']['note_password']) && $mode == 'edit' && $old_pass != $values['crypted']['note_password']) { //reencrypt old revisions $old_notes = DB::GetAssoc('SELECT hd.edit_id,hd.old_value FROM utils_attachment_edit_history h INNER JOIN utils_attachment_edit_history_data hd ON h.id=hd.edit_id WHERE h.utils_attachment_id=%d AND hd.field="note"', array($values['id'])); foreach ($old_notes as $old_id => $old_note) { if ($old_pass !== '') { $old_note = Utils_AttachmentCommon::decrypt($old_note, $old_pass); } if ($old_note === false) { continue; } if ($crypted && $values['crypted']['note_password']) { $old_note = Utils_AttachmentCommon::encrypt($old_note, $values['crypted']['note_password']); } if ($old_note === false) { continue; } DB::Execute('UPDATE utils_attachment_edit_history_data SET old_value=%s WHERE edit_id=%d AND field="note"', array($old_note, $old_id)); } //file reencryption $old_files = DB::GetAssoc('SELECT uaf.id as id, uaf.filestorage_id FROM utils_attachment_file uaf WHERE uaf.attach_id=%d', array($values['id'])); foreach ($old_files as $id => $fsid) { try { $meta = Utils_FileStorageCommon::meta($fsid); } catch (Exception $e) { continue; } $filename = $meta['file']; $content = @file_get_contents($filename); if ($content === false) { continue; } if ($old_pass !== '') { $content = Utils_AttachmentCommon::decrypt($content, $old_pass); } if ($content === false) { continue; } if ($crypted && $values['crypted']['note_password']) { $content = Utils_AttachmentCommon::encrypt($content, $values['crypted']['note_password']); } if ($content === false) { continue; } $fsid = Utils_FileStorageCommon::write_content($meta['filename'], $content); DB::Execute('UPDATE utils_attachment_file SET filestorage_id=%d WHERE id=%d', array($fsid, $id)); Utils_FileStorageCommon::update_link('attachment_file/' . $id, $fsid); } } if ($crypted) { if (is_array($values['crypted']) && isset($values['crypted']['note_password'])) { $values['note'] = Utils_AttachmentCommon::encrypt($values['note'], $values['crypted']['note_password']); $values['note_password'] = $values['crypted']['note_password']; } $values['crypted'] = 1; } else { $values['crypted'] = 0; } $new_values = $values; break; case 'cloning': $values['clone_id'] = $values['id']; break; case 'added': if (isset($values['local'])) { DB::Execute('INSERT INTO utils_attachment_local(attachment,local,func,args) VALUES(%d,%s,%s,%s)', array($values['id'], $values['local'], $values['func'], $values['args'])); } $new_values = $values; break; case 'edit_changes': if (isset($values['note']) && isset($values['crypted']) && $new_values['crypted'] != $values['crypted']) { if ($new_values['crypted'] && isset($new_values['note_password'])) { $values['note'] = Utils_AttachmentCommon::encrypt($values['note'], $new_values['note_password']); } elseif (!$new_values['crypted'] && isset($_SESSION['client']['cp' . $new_values['id']])) { $values['note'] = Utils_AttachmentCommon::decrypt($values['note'], $_SESSION['client']['cp' . $new_values['id']]); unset($_SESSION['client']['cp' . $new_values['id']]); } } elseif (isset($new_values['note_password']) && isset($old_password) && $new_values['note_password'] != $old_password) { $values['note'] = Utils_AttachmentCommon::decrypt($values['note'], $old_password); $values['note'] = Utils_AttachmentCommon::encrypt($values['note'], $new_values['note_password']); } unset($values['edited_on']); break; case 'view': $ret = self::get_access($values['id']); if (!$ret) { print __('Access denied'); } return $ret; case 'display': if (DB::GetOne('SELECT 1 FROM utils_attachment_file WHERE attach_id=%d', array($values['id']))) { $ret = array(); $ret['new'] = array(); $ret['new']['crm_filter'] = '<a ' . Utils_TooltipCommon::open_tag_attrs(__('File history')) . ' ' . Module::create_href(array('file_history' => 1)) . '>F</a>'; if (isset($_REQUEST['file_history']) && (!$values['crypted'] || isset($_SESSION['client']['cp' . $values['id']]))) { Base_BoxCommon::push_module('Utils_Attachment', 'file_history', array($values)); } // CRM_FiltersCommon::set_profile('c'.$values['id']); return $ret; } break; case 'delete': if ($values['crypted'] && !isset($_SESSION['client']['cp' . $values['id']])) { Epesi::alert(__('Cannot delete encrypted note')); return false; } $count_locals = DB::GetOne('SELECT count(DISTINCT local) FROM utils_attachment_local WHERE attachment=%d', array($values['id'])); if ($count_locals > 1) { $is_local = false; if (isset($_SESSION['client']['utils_attachment_group'])) { $is_local = DB::GetOne('SELECT 1 FROM utils_attachment_local WHERE attachment=%d AND local=%s', array($values['id'], $_SESSION['client']['utils_attachment_group'])); } if ($is_local) { DB::Execute('DELETE FROM utils_attachment_local WHERE attachment=%d AND local=%s', array($values['id'], $_SESSION['client']['utils_attachment_group'])); self::new_watchdog_event($_SESSION['client']['utils_attachment_group'], '-', $values['id']); } else { Epesi::alert(__('This note is attached to multiple records - please go to record and delete note there.')); } location(array()); return false; } location(array()); return true; } switch ($mode) { case 'edit': case 'added': if (isset($values['note_password'])) { $old_password = isset($_SESSION['client']['cp' . $values['id']]) ? $_SESSION['client']['cp' . $values['id']] : ''; $_SESSION['client']['cp' . $values['id']] = $values['note_password']; } $note_id = $values['id']; if (isset($values['delete_files'])) { $deleted_files = array_filter(explode(';', $values['delete_files'])); } else { $deleted_files = array(); } foreach ($deleted_files as $k => $v) { $deleted_files[$k] = intVal($v); } if ($deleted_files) { $deleted_files = array_combine($deleted_files, $deleted_files); } if ($mode == 'added' && isset($values['clone_id'])) { //on cloning $locals = DB::Execute('SELECT local,func,args FROM utils_attachment_local WHERE attachment=%d', array($values['clone_id'])); while ($local = $locals->FetchRow()) { DB::Execute('INSERT INTO utils_attachment_local(attachment,local,func,args) VALUES(%d,%s,%s,%s)', array($note_id, $local['local'], $local['func'], $local['args'])); } $clone_files = DB::GetAll('SELECT id,original,created_by,created_on,filestorage_id FROM utils_attachment_file uaf WHERE uaf.attach_id=%d AND uaf.deleted=0', array($values['clone_id'])); foreach ($clone_files as $file) { $fsid = $file['filestorage_id']; $content = Utils_FileStorageCommon::read_content($fsid); if (isset($_SESSION['client']['cp' . $values['clone_id']]) && $_SESSION['client']['cp' . $values['clone_id']]) { $content = Utils_AttachmentCommon::decrypt($content, $_SESSION['client']['cp' . $values['clone_id']]); } if ($values['crypted']) { $content = Utils_AttachmentCommon::encrypt($content, $values['note_password']); } $fsid = Utils_FileStorageCommon::write_content($fsid, $content); DB::Execute('INSERT INTO utils_attachment_file (attach_id,deleted,original,created_by,created_on,filestorage_id) VALUES(%d,0,%s,%d,%T,%d)', array($note_id, $file['original'], $file['created_by'], $file['created_on'], $fsid)); Utils_FileStorageCommon::add_link('attachment_file/' . DB::Insert_ID('utils_attachment_file', 'id'), $fsid); } } $current_files = DB::GetAssoc('SELECT id, id FROM utils_attachment_file uaf WHERE uaf.attach_id=%d AND uaf.deleted=0', array($note_id)); $remaining_files = $current_files; foreach ($deleted_files as $k => $v) { if (!isset($remaining_files[$v])) { unset($deleted_files[$k]); } else { unset($remaining_files[$v]); } } foreach ($deleted_files as $v) { DB::Execute('UPDATE utils_attachment_file SET deleted=1 WHERE id=%d', array($v)); } if (isset($values['clipboard_files'])) { $clipboard_files = array_filter(explode(';', $values['clipboard_files'])); foreach ($clipboard_files as $cf_id) { $cf = DB::GetOne('SELECT filename FROM utils_attachment_clipboard WHERE id=%d', array($cf_id)); if ($values['crypted']) { file_put_contents($cf, Utils_AttachmentCommon::encrypt(file_get_contents($cf), $values['note_password'])); } Utils_AttachmentCommon::add_file($note_id, Acl::get_user(), __('clipboard') . '.png', $cf); } } $files = isset($_SESSION['client']['utils_attachment'][CID]['files']) ? $_SESSION['client']['utils_attachment'][CID]['files'] : array(); $_SESSION['client']['utils_attachment'][CID]['files'] = array(); foreach ($files as $f) { $file_path = $f['path']; $file_name = $f['name']; if ($values['crypted']) { file_put_contents($file_path, Utils_AttachmentCommon::encrypt(file_get_contents($file_path), $values['note_password'])); } Utils_AttachmentCommon::add_file($note_id, Acl::get_user(), $file_name, $file_path); } $locals = DB::GetCol('SELECT local FROM utils_attachment_local WHERE attachment=%d', array($note_id)); foreach ($locals as $local) { $param = explode('/', $local); if (count($param) == 2 && preg_match('/^[1-9][0-9]*$/', $param[1])) { $subscribers = Utils_WatchdogCommon::get_subscribers($param[0], $param[1]); foreach ($subscribers as $user_id) { Utils_WatchdogCommon::user_subscribe($user_id, 'utils_attachment', $note_id); } } } break; } return $values; }