function chron_scheduler_send_message($to, $from, $subject, $message) { $em = new \CI_Email(); $em->from($from); $em->to($to); $em->subject($subject); $em->message($message); return $em->send(); }
protected function send_email($to, $subject, $text) { $this->load->library('email'); $this->email->from(MAIL_FROM_ADDRESS, MAIL_FROM_NAME); $this->email->bcc($to); $this->email->bcc_batch_mode = true; $this->email->subject($subject); $this->email->message($text); $this->email->batch_bcc_send(); return $this->email->send(); }
function backup_email_log($to, $from, $subject) { $tmp = function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : '/tmp'; $email_options = array('useragent' => 'freepbx', 'protocol' => 'mail'); $email = new CI_Email(); $msg[] = _('BACKUP LOG ATTACHED'); $email->from($from); $email->to($to); $email->subject(_('Backup Log:') . $subject); $email->message(implode("\n", $msg)); $email->attach($tmp . '/backup.log'); $email->send(); unset($msg); }
/** FINALIZE PRINT TASK * * @param $tid - TASK ID * @param $status - TASK STATUS (STOPPED - PERFORMED) * **/ function finalize_print($tid, $status) { //global $log; //$log->info('Task #'.$tid.' print '.$status); //$log->info('Task #'.$tid.' start finalizing'); //LOAD DB $db = new Database(); //GET TASK $task = $db->query('select * from sys_tasks where id=' . $tid); $reset = false; //CHECK IF TASK WAS ALREARDY FINALIZED if ($task['status'] == 'stopped' || $task['status'] == 'performed') { //$log->info('Task #'.$tid.' already finalized. Exit'); return; } //GET TASK ATTRIBUTES $attributes = json_decode($task['attributes'], TRUE); $print_type = $attributes['print_type']; if ($status == 'stopped' && $print_type == 'additive') { //IF % PROGRESS IS < 0.5 FOR SECURITY REASON I RESET THE BOARD CONTROLLER $monitor = json_decode(file_get_contents($attributes['monitor']), TRUE); $percent = $monitor['print']['stats']['percent']; if ($percent < 0.2) { /** FORCE RESET CONTROLLER */ $_command = 'sudo python ' . PYTHON_PATH . 'force_reset.py'; shell_exec($_command); $reset = true; //$log->info('Task #'.$tid.' reset controller'); } } //GET TYPE OF FILE (ADDITIVE OR SUBTRACTIVE) FOR ENDING MACRO $file = $db->query('select * from sys_files where id=' . $attributes['id_file']); //UPDATE TASK update_task($tid, $status); $_macro_end_print_response = TEMP_PATH . 'macro_response'; $_macro_end_print_trace = TEMP_PATH . 'macro_trace'; /* if(($file['print_type'] == 'additive') && !$reset){ echo 'sudo python '.PYTHON_PATH.'gmacro.py end_print_additive_safe_zone '.$_macro_end_print_trace.' '.$_macro_end_print_response.' > /dev/null &'; shell_exec('sudo python '.PYTHON_PATH.'gmacro.py end_print_additive_safe_zone '.$_macro_end_print_trace.' '.$_macro_end_print_response.' > /dev/null &'); } */ $end_macro = $file['print_type'] == 'subtractive' ? 'end_print_subtractive' : 'end_print_additive'; write_file($_macro_end_print_trace, '', 'w'); chmod($_macro_end_print_trace, 0777); write_file($_macro_end_print_response, '', 'w'); chmod($_macro_end_print_response, 0777); //EXEC END MACRO shell_exec('sudo python ' . PYTHON_PATH . 'gmacro.py ' . $end_macro . ' ' . $_macro_end_print_trace . ' ' . $_macro_end_print_response . ' > /dev/null &'); //$log->info('Task #'.$tid.' end macro: '.$end_macro); sleep(2); shell_exec('sudo kill ' . $attributes['pid']); // SEND MAIL if (isset($attributes['mail']) && $attributes['mail'] == 1 && $status == 'peformed') { $user = $db->query('select * from sys_user where id=' . $task['user']); // CREATE IMAGE TO SEND write_file($attributes['folder'] . 'print.jpg', '', 'w'); chmod($attributes['folder'] . 'print.jpg', 0777); // TAKE PICTURE shell_exec('sudo raspistill -hf -vf -rot 90 --exposure off -awb sun -ISO 400 -w 768 -h 1024 -o ' . $attributes['folder'] . 'print.jpg' . ' -t 0'); $email = new CI_Email(); $config['mailtype'] = 'html'; $email->initialize($config); $email->from('*****@*****.**', 'Your Personal Fabricator - Fabtotum'); $email->to($user['email']); // ATTACH $email->attach($attributes['folder'] . 'print.jpg'); $email->subject('Your print is finished'); $message = 'Dear <strong>' . ucfirst($user['first_name']) . '</strong>,<br>i want to inform you that the print is finished right now'; $email->message($message); if (!$email->send()) { //$log->error('Task #'.$tid.' mail sent to '.$user['email']); } else { //$log->info('Task #'.$tid.' mail sent to '.$user['email']); } } $db->close(); //WAIT FOR THE UI TO FINALIZE THE PROCESS //sleep(7); //REMOVE ALL TEMPORARY FILES shell_exec('sudo rm -rf ' . $attributes['folder']); //$log->info('Task #'.$tid.' end finalizing'); }
public function from($from, $name = '', $return_path = NULL) { $from = (string) $from; $name = (string) $name; $return_path = (string) $return_path; if ($this->mailer_engine == 'phpmailer') { if (preg_match('/\\<(.*)\\>/', $from, $match)) { $from = $match['1']; } if ($this->validate) { $this->validate_email($this->_str_to_array($from)); if ($return_path) { $this->validate_email($this->_str_to_array($return_path)); } } $this->phpmailer->setFrom($from, $name, 0); if (!$return_path) { $return_path = $from; } $this->phpmailer->Sender = $return_path; } else { if ($this->_is_ci_3) { parent::from($from, $name, $return_path); } else { parent::from($from, $name); } } return $this; }
/** * Send an email to a user * @param int $id The user ID * @param string $subject The email subject * @param string $body The email body */ public function sendEmail($id, $subject, $body) { $user = $this->getUserByID($id); if (empty($user) || empty($user['email'])) { return false; } $email_options = array('useragent' => $this->brand, 'protocol' => 'mail'); $email = new \CI_Email(); //TODO: Stop gap until sysadmin becomes a full class if (!function_exists('sysadmin_get_storage_email') && $this->FreePBX->Modules->checkStatus('sysadmin') && file_exists($this->FreePBX->Config()->get('AMPWEBROOT') . '/admin/modules/sysadmin/functions.inc.php')) { include $this->FreePBX->Config()->get('AMPWEBROOT') . '/admin/modules/sysadmin/functions.inc.php'; } $femail = $this->FreePBX->Config()->get('AMPUSERMANEMAILFROM'); if (function_exists('sysadmin_get_storage_email')) { $emails = sysadmin_get_storage_email(); if (!empty($emails['fromemail']) && filter_var($emails['fromemail'], FILTER_VALIDATE_EMAIL)) { $femail = $emails['fromemail']; } } $from = !empty($femail) ? $femail : get_current_user() . '@' . gethostname(); $email->from($from); $email->to($user['email']); $email->subject($subject); $email->message($body); $email->send(); }
function from($from_email, $from_name) { $this->_plaintext_from_name = $from_name; $this->_plaintext_from_email = $from_email; parent::from($from_email, $from_name); }
function store_backup() { foreach ($this->b['storage_servers'] as $s) { $s = $this->s[$s]; switch ($s['type']) { case 'local': $path = backup__($s['path']) . '/' . $this->b['_dirname']; //ensure directory structure if (!is_dir($path)) { mkdir($path, 0755, true); } //would rather use the native copy() here, but by defualt //php doesnt support files > 2GB //see here for a posible solution: //http://ca3.php.net/manual/en/function.fopen.php#37791 $cmd[] = fpbx_which('cp'); $cmd[] = $this->b['_tmpfile']; $cmd[] = $path . '/' . $this->b['_file'] . '.tgz'; exec(implode(' ', $cmd), $error, $status); unset($cmd, $error); if ($status !== 0) { $this->b['error'] = 'Error copying ' . $this->b['_tmpfile'] . ' to ' . $path . '/' . $this->b['_file'] . '.tgz: ' . $error; backup_log($this->b['error']); } //run maintenance on the directory $this->maintenance($s['type'], $s); break; case 'email': //TODO: set agent to something informative, including fpbx & backup versions $email_options = array('useragent' => 'freepbx', 'protocol' => 'mail'); $email = new \CI_Email(); //Generic email $from = '*****@*****.**'; //If we have sysadmin and "from is set" if (function_exists('sysadmin_get_storage_email')) { $emails = sysadmin_get_storage_email(); //Check that what we got back above is a email address if (!empty($emails['fromemail']) && filter_var($emails['fromemail'], FILTER_VALIDATE_EMAIL)) { $from = $emails['fromemail']; } } //If the user set an email in advanced settings it wins, otherwise take whatever won above. $from = filter_var($this->amp_conf['AMPBACKUPEMAILFROM'], FILTER_VALIDATE_EMAIL) ? $this->amp_conf['AMPBACKUPEMAILFROM'] : $from; $msg[] = _('Name') . ': ' . $this->b['name']; $msg[] = _('Created') . ': ' . date('r', $this->b['_ctime']); $msg[] = _('Files') . ': ' . $this->manifest['file_count']; $msg[] = _('Mysql Db\'s') . ': ' . $this->manifest['mysql_count']; $msg[] = _('astDb\'s') . ': ' . $this->manifest['astdb_count']; $email->from($from); $email->to(backup__($s['addr'])); $email->subject($this->amp_conf['FREEPBX_SYSTEM_IDENT'] . ' ' . _('Backup') . ' ' . $this->b['name']); $body = implode("\n", $msg); // If the backup file is more than 25MB, yell $encodedsize = ceil(filesize($this->b['_tmpfile']) / 3) * 4; if ($encodedsize > 26214400) { $email->subject($this->amp_conf['FREEPBX_SYSTEM_IDENT'] . ' ' . _('Backup ERROR (exceeded SMTP limits)') . ' ' . $this->b['name']); $email->message(_('BACKUP NOT ATTACHED') . "\n" . _('The backup file exceeded the maximum SMTP limits of 25MB. It was not attempted to be sent. Please shrink your backup, or use a different method of transferring your backup.') . "\n{$body}\n"); } elseif ($encodedsize > $s['maxsize']) { $email->subject($this->amp_conf['FREEPBX_SYSTEM_IDENT'] . ' ' . _('Backup ERROR (exceeded soft limit)') . ' ' . $this->b['name']); $email->message(_('BACKUP NOT ATTACHED') . "\n" . _('The backup file exceeded the soft limit set in SMTP configuration (%s bytes). It was not attempted to be sent. Please shrink your backup, or use a different method of transferring your backup.') . "\n{$body}\n"); } else { $email->message($body); $email->attach($this->b['_tmpfile']); } $email->send(); unset($msg); break; case 'ftp': //subsitute variables if nesesary $s['host'] = backup__($s['host']); $s['port'] = backup__($s['port']); $s['user'] = backup__($s['user']); $s['password'] = backup__($s['password']); $s['path'] = trim(backup__($s['path']), '/'); $fstype = isset($s['fstype']) ? $s['fstype'] : 'auto'; $path = $s['path'] . '/' . $this->b['_dirname']; $connection = new Connection($s['host'], $s['user'], $s['password'], $s['port'], 90, $s['transfer'] == 'passive'); try { $connection->open(); } catch (\Exception $e) { $this->b['error'] = $e->getMessage(); backup_log($this->b['error']); return; } $wrapper = new FTPWrapper($connection); $permFactory = new PermissionsFactory(); switch ($fstype) { case 'auto': $ftptype = $wrapper->systype(); if (strtolower($ftptype) == "unix") { $fsFactory = new FilesystemFactory($permFactory); } else { $fsFactory = new WindowsFilesystemFactory(); } break; case 'unix': $fsFactory = new FilesystemFactory($permFactory); break; case 'windows': $fsFactory = new WindowsFilesystemFactory(); break; } $manager = new FTPFilesystemManager($wrapper, $fsFactory); $dlVoter = new DownloaderVoter(); $ulVoter = new UploaderVoter(); $ulVoter->addDefaultFTPUploaders($wrapper); $crVoter = new CreatorVoter(); $crVoter->addDefaultFTPCreators($wrapper, $manager); $deVoter = new DeleterVoter(); $deVoter->addDefaultFTPDeleters($wrapper, $manager); $ftp = new FTP($manager, $dlVoter, $ulVoter, $crVoter, $deVoter); if (!$ftp) { $this->b['error'] = _("Error creating the FTP object"); backup_log($this->b['error']); return; } if (!$ftp->directoryExists(new Directory($path))) { backup_log(sprintf(_("Creating directory '%s'"), $path)); try { $ftp->create(new Directory($path), array(FTP::RECURSIVE => true)); } catch (\Exception $e) { $this->b['error'] = sprintf(_("Directory '%s' did not exist and we could not create it"), $path); backup_log($this->b['error']); backup_log($e->getMessage()); return; } } try { backup_log(_("Saving file to remote ftp")); $ftp->upload(new File($path . '/' . $this->b['_file'] . '.tgz'), $this->b['_tmpfile']); } catch (\Exception $e) { $this->b['error'] = _("Unable to upload file to the remote server"); backup_log($this->b['error']); backup_log($e->getMessage()); return; } //run maintenance on the directory $this->maintenance($s['type'], $path, $ftp); break; case 'awss3': //subsitute variables if nesesary $s['bucket'] = backup__($s['bucket']); $s['awsaccesskey'] = backup__($s['awsaccesskey']); $s['awssecret'] = backup__($s['awssecret']); $awss3 = new \S3($s['awsaccesskey'], $s['awssecret']); // Does this bucket already exist? $buckets = $awss3->listBuckets(); if (!in_array($s['bucket'], $buckets)) { // Create the bucket $awss3->putBucket($s['bucket'], \S3::ACL_PUBLIC_READ); } //copy file if ($awss3->putObjectFile($this->b['_tmpfile'], $s['bucket'], $this->b['name'] . "/" . $this->b['_file'] . '.tgz', \S3::ACL_PUBLIC_READ)) { dbug('S3 successfully uploaded your backup file.'); } else { dbug('S3 failed to accept your backup file'); } //run maintenance on the directory $this->maintenance($s['type'], $s, $awss3); break; case 'ssh': //subsitute variables if nesesary $s['path'] = backup__($s['path']); $s['user'] = backup__($s['user']); $s['host'] = backup__($s['host']); $destdir = $s['path'] . '/' . $this->b['_dirname']; //ensure directory structure $cmd = fpbx_which('ssh') . ' -o StrictHostKeyChecking=no -i '; $cmd .= $s['key'] . " -l " . $s['user'] . ' ' . $s['host'] . ' -p ' . $s['port']; $cmd .= " 'mkdir -p {$destdir}'"; exec($cmd, $output, $ret); if ($ret !== 0) { backup_log("SSH Error ({$ret}) - Received " . json_encode($output) . " from {$cmd}"); } $output = null; //put file // Note that SCP (*unlike SSH*) needs IPv6 addresses in ['s. Consistancy is awesome. if (filter_var($s['host'], \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { $scphost = "[" . $s['host'] . "]"; } else { $scphost = $s['host']; } $cmd = fpbx_which('scp') . ' -o StrictHostKeyChecking=no -i ' . $s['key'] . ' -P ' . $s['port']; $cmd .= " " . $this->b['_tmpfile'] . " " . $s['user'] . "@{$scphost}:{$destdir}"; exec($cmd, $output, $ret); if ($ret !== 0) { backup_log("SCP Error ({$ret}) - Received " . json_encode($output) . " from {$cmd}"); } //run maintenance on the directory $this->maintenance($s['type'], $s); break; } } }
if ($var['exten']) { $user = \FreePBX::Userman()->getUserByID($var['exten']); if (!empty($user)) { $name = !empty($user['displayname']) ? $user['displayname'] : trim($user['fname'] . " " . $user['lname']); $name = !empty($name) ? $name : $user['username']; } else { $name = $var['exten']; } $msg .= _('For User') . ': ' . $name . "\n"; } } $tif = $var['file']; if (!empty($var['to'])) { //build email $email = new CI_Email(); $email->from($var['from']); $email->to($var['to']); $email->subject($var['subject']); $email->message($msg); switch ($var['attachformat']) { case 'both': $pdf = fax_file_convert('tif2pdf', $var['file'], '', true); $email->attach($pdf); $email->attach($tif); break; case 'tif': $email->attach($tif); break; case 'pdf': $pdf = fax_file_convert('tif2pdf', $var['file'], '', true); $email->attach($pdf);
/** * Replacement of CI_Email send function * Added: App email data added to CI_Email (from, to, etc) before actual send. * * @return boolean Whether or not the email is sent */ public function send() { $valid = $this->_prepare_send(); if ($valid) { // set CI_Email parent::from($this->_data['from_email'], $this->_data['from_name']); parent::to($this->_data['to']); parent::subject($this->_data['subject']); parent::message($this->_data['message']); if (!empty($this->_data['alt_message'])) { parent::set_alt_message($this->_data['alt_message']); } $send = parent::send(); if ($send) { // log email $this->ci->load->library('app_log'); $this->ci->app_log->log('app_email', $this->_data['subject'] . ' (to: ' . $this->_data['to'][0] . ')'); } return $send; } else { // mail not send, see error for details return FALSE; } }
/** * Forward a voicemail message to a new folder * @param string $msg The message ID * @param int $ext The voicemail extension message is coming from * @param int $rcpt The recipient, voicemail will wind up in the INBOX */ public function forwardMessageByExtension($msg, $ext, $to) { $fromVM = $this->getVoicemailBoxByExtension($ext); $messages = $this->getMessagesByExtension($ext); if (isset($messages['messages'][$msg])) { $info = $messages['messages'][$msg]; $txt = $info['path'] . "/" . $info['fid'] . ".txt"; if (file_exists($txt) && is_readable($txt)) { $toVM = $this->getVoicemailBoxByExtension($to); $context = $toVM['vmcontext']; $toFolder = $this->vmPath . '/' . $context . '/' . $to . '/INBOX'; if (file_exists($toFolder) && is_writable($toFolder)) { $files = array(); $files[] = $txt; $movedFiles = array(); foreach ($info['format'] as $format) { if (file_exists($format['path'] . "/" . $format['filename'])) { $files[] = $format['path'] . "/" . $format['filename']; } } //if the folder is empty (meaning we dont have a 0000 file) then set this to 0000 $tname = preg_replace('/([0-9]+)/', '0000', basename($txt)); $vminfotxt = ''; if (!file_exists($toFolder . "/" . $tname)) { foreach ($files as $file) { $fname = preg_replace('/msg([0-9]+)/', 'msg0000', basename($file)); copy($file, $toFolder . "/" . $fname); $movedFiles[] = $toFolder . "/" . $fname; } } else { //Else we have other voicemail data in here so do something else //figure out the last file in the directory $oldFiles = glob($toFolder . "/*.txt"); $numbers = array(); foreach ($oldFiles as $file) { $file = basename($file); preg_match('/([0-9]+)/', $file, $matches); $numbers[] = $matches[1]; } rsort($numbers); $next = sprintf('%04d', $numbers[0] + 1); foreach ($files as $file) { $fname = preg_replace('/msg([0-9]+)/', "msg" . $next, basename($file)); copy($file, $toFolder . "/" . $fname); $movedFiles[] = $toFolder . "/" . $fname; } } //send email from/to new mailbox $vm = $this->FreePBX->LoadConfig->getConfig("voicemail.conf"); $emailInfo = array("normal" => array("body" => !empty($vm['general']['emailbody']) ? $vm['general']['emailbody'] : 'Dear ${VM_NAME}:\\n\\n\\tjust wanted to let you know you were just left a ${VM_DUR} long message (number ${VM_MSGNUM})\\nin mailbox ${VM_MAILBOX} from ${VM_CALLERID}, on ${VM_DATE}, so you might\\nwant to check it when you get a chance. Thanks!\\n\\n\\t\\t\\t\\t--Asterisk\\n', "subject" => !empty($vm['general']['emailsubject']) ? $vm['general']['emailsubject'] : (isset($vm['general']['pbxskip']) && $vm['general']['pbxskip'] == "no" ? "[PBX]: " : "") . 'New message ${VM_MSGNUM} in mailbox ${VM_MAILBOX}', "fromstring" => !empty($vm['general']['fromstring']) ? $vm['general']['fromstring'] : 'The Asterisk PBX'), "pager" => array("body" => !empty($vm['general']['pagerbody']) ? $vm['general']['pagerbody'] : 'New ${VM_DUR} long msg in box ${VM_MAILBOX}\\nfrom ${VM_CALLERID}, on ${VM_DATE}', "subject" => !empty($vm['general']['pagersubject']) ? $vm['general']['pagersubject'] : 'New VM', "fromstring" => !empty($vm['general']['pagerfromstring']) ? $vm['general']['pagerfromstring'] : 'The Asterisk PBX')); $processUser = posix_getpwuid(posix_geteuid()); $from = !empty($vm['general']['serveremail']) ? $vm['general']['serveremail'] : $processUser['name'] . '@' . gethostname(); foreach ($emailInfo as &$einfo) { $einfo['body'] = str_replace(array('\\n', '\\t'), array("\n", "\t"), $einfo['body']); $einfo['body'] = str_replace(array('${VM_NAME}', '${VM_MAILBOX}', '${VM_CALLERID}', '${VM_DUR}', '${VM_DATE}', '${VM_MSGNUM}'), array($toVM['name'], $to, $info['callerid'], $info['duration'], $info['origdate'], $info['msg_id']), $einfo['body']); $einfo['subject'] = str_replace(array('${VM_NAME}', '${VM_MAILBOX}', '${VM_CALLERID}', '${VM_DUR}', '${VM_DATE}', '${VM_MSGNUM}'), array($toVM['name'], $to, $info['callerid'], $info['duration'], $info['origdate'], $info['msg_id']), $einfo['subject']); } if (!empty($toVM['email'])) { $em = new \CI_Email(); if ($toVM['options']['attach'] == "yes") { $em->attach($info['path'] . "/" . $info['file']); } $em->from($from, $emailInfo['normal']['fromstring']); $em->to($toVM['email']); $em->subject($emailInfo['normal']['subject']); $em->message($emailInfo['normal']['body']); $em->send(); } if (!empty($toVM['pager'])) { $em = new \CI_Email(); $em->from($from, $emailInfo['pager']['fromstring']); $em->to($toVM['email']); $em->subject($emailInfo['pager']['subject']); $em->message($emailInfo['pager']['body']); $em->send(); } if (isset($toVM['delete']) && $toVM['delete'] == "yes") { //now delete the voicemail wtf. foreach ($movedFiles as $file) { unlink($file); } } //Just for sanity sakes recheck the directories hopefully this doesnt take hours though. $this->renumberAllMessages($toFolder); } } } if (!empty($fromVM['context'])) { $this->astman->VoicemailRefresh($fromVM['context'], $ext); } if (!empty($toVM['context'])) { $this->astman->VoicemailRefresh($toVM['context'], $to); } }
$config['crlf'] = "\r\n"; $config['newline'] = "\r\n"; $to_address = '*****@*****.**'; $from_address = '*****@*****.**'; // input field $form_name = 'email name'; $subject = 'subject to test email'; // Pass some data into the email templates $email_data = array(); $email_data['firstname'] = 'Look'; $email_data['lastname'] = 'look'; $email_data['email'] = '*****@*****.**'; define('MB_ENABLED', TRUE); define('ICONV_ENABLED', TRUE); $email_sender = new CI_Email($config); $email_sender->from($from_address, $form_name); $email_sender->to($to_address); $email_sender->subject($subject); $email_sender->message(view('email_template', $email_data)); //$email_sender->set_alt_message(view('email_template', $email_data)); if (!$email_sender->send()) { exit('error' . 'User::create - Unable to send new user email. Response is as follows:' . $email_sender->print_debugger() . "\n\n"); } else { echo 'email sent'; } // Load view function function view($path, $data) { ob_start(); extract($data); include $path . '.php';
function store_backup() { foreach ($this->b['storage_servers'] as $s) { $s = $this->s[$s]; switch ($s['type']) { case 'local': $path = backup__($s['path']) . '/' . $this->b['_dirname']; //ensure directory structure if (!is_dir($path)) { mkdir($path, 0755, true); } //would rather use the native copy() here, but by defualt //php doesnt support files > 2GB //see here for a posible solution: //http://ca3.php.net/manual/en/function.fopen.php#37791 $cmd[] = fpbx_which('cp'); $cmd[] = $this->b['_tmpfile']; $cmd[] = $path . '/' . $this->b['_file'] . '.tgz'; exec(implode(' ', $cmd), $error, $status); unset($cmd, $error); if ($status !== 0) { $this->b['error'] = 'Error copying ' . $this->b['_tmpfile'] . ' to ' . $path . '/' . $this->b['_file'] . '.tgz: ' . $error; backup_log($this->b['error']); } //run maintenance on the directory $this->maintenance($s['type'], $s); break; case 'email': //TODO: set agent to something informative, including fpbx & backup versions $email_options = array('useragent' => 'freepbx', 'protocol' => 'mail'); $email = new \CI_Email(); $from = $this->amp_conf['AMPBACKUPEMAILFROM'] ? $this->amp_conf['AMPBACKUPEMAILFROM'] : '*****@*****.**'; $msg[] = _('Name') . ': ' . $this->b['name']; $msg[] = _('Created') . ': ' . date('r', $this->b['_ctime']); $msg[] = _('Files') . ': ' . $this->manifest['file_count']; $msg[] = _('Mysql Db\'s') . ': ' . $this->manifest['mysql_count']; $msg[] = _('astDb\'s') . ': ' . $this->manifest['astdb_count']; $email->from($from); $email->to(backup__($s['addr'])); $email->subject(_('Backup') . ' ' . $this->b['name']); $body = implode("\n", $msg); // If the backup file is more than 25MB, yell $encodedsize = ceil(filesize($this->b['_tmpfile']) / 3) * 4; if ($encodedsize > 26214400) { $email->subject(_('Backup ERROR (exceeded SMTP limits)') . ' ' . $this->b['name']); $email->message(_('BACKUP NOT ATTACHED') . "\n" . _('The backup file exceeded the maximum SMTP limits of 25MB. It was not attempted to be sent. Please shrink your backup, or use a different method of transferring your backup.') . "\n{$body}\n"); } elseif ($encodedsize > $s['maxsize']) { $email->subject(_('Backup ERROR (exceeded soft limit)') . ' ' . $this->b['name']); $email->message(_('BACKUP NOT ATTACHED') . "\n" . _('The backup file exceeded the soft limit set in SMTP configuration (%s bytes). It was not attempted to be sent. Please shrink your backup, or use a different method of transferring your backup.') . "\n{$body}\n"); } else { $email->message($body); $email->attach($this->b['_tmpfile']); } $email->send(); unset($msg); break; case 'ftp': //subsitute variables if nesesary $s['host'] = backup__($s['host']); $s['port'] = backup__($s['port']); $s['user'] = backup__($s['user']); $s['password'] = backup__($s['password']); $s['path'] = backup__($s['path']); $ftp = @ftp_connect($s['host'], $s['port']); if ($ftp === false) { $this->b['error'] = _("Error connecting to the FTP Server... Check your host name or DNS"); backup_log($this->b['error']); return $ftp; } if (ftp_login($ftp, $s['user'], $s['password'])) { //chose pasive/active transfer mode ftp_pasv($ftp, $s['transfer'] == 'passive'); //switch to directory. If we fail, build directory structure and try again if (!@ftp_chdir($ftp, $s['path'] . '/' . $this->b['_dirname'])) { //ensure directory structure @ftp_mkdir($ftp, $s['path']); @ftp_mkdir($ftp, $s['path'] . '/' . $this->b['_dirname']); ftp_chdir($ftp, $s['path'] . '/' . $this->b['_dirname']); } //copy file ftp_put($ftp, $this->b['_file'] . '.tgz', $this->b['_tmpfile'], FTP_BINARY); //run maintenance on the directory $this->maintenance($s['type'], $s, $ftp); //release handel ftp_close($ftp); } else { $this->b['error'] = _("Error connecting to the FTP Server..."); backup_log($this->b['error']); } break; case 'awss3': //subsitute variables if nesesary $s['bucket'] = backup__($s['bucket']); $s['awsaccesskey'] = backup__($s['awsaccesskey']); $s['awssecret'] = backup__($s['awssecret']); $awss3 = new \S3($s['awsaccesskey'], $s['awssecret']); // Does this bucket already exist? $buckets = $awss3->listBuckets(); if (!in_array($s['bucket'], $buckets)) { // Create the bucket $awss3->putBucket($s['bucket'], \S3::ACL_PUBLIC_READ); } //copy file if ($awss3->putObjectFile($this->b['_tmpfile'], $s['bucket'], $this->b['name'] . "/" . $this->b['_file'] . '.tgz', \S3::ACL_PUBLIC_READ)) { dbug('S3 successfully uploaded your backup file.'); } else { dbug('S3 failed to accept your backup file'); } //run maintenance on the directory $this->maintenance($s['type'], $s, $awss3); break; case 'ssh': //subsitute variables if nesesary $s['path'] = backup__($s['path']); $s['user'] = backup__($s['user']); $s['host'] = backup__($s['host']); $destdir = $s['path'] . '/' . $this->b['_dirname']; //ensure directory structure $cmd = fpbx_which('ssh') . ' -o StrictHostKeyChecking=no -i '; $cmd .= $s['key'] . " -l " . $s['user'] . ' ' . $s['host'] . ' -p ' . $s['port']; $cmd .= " 'mkdir -p {$destdir}'"; exec($cmd, $output, $ret); if ($ret !== 0) { backup_log("SSH Error ({$ret}) - Received " . json_encode($output) . " from {$cmd}"); } $output = null; //put file // Note that SCP (*unlike SSH*) needs IPv6 addresses in ['s. Consistancy is awesome. if (filter_var($s['host'], \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { $scphost = "[" . $s['host'] . "]"; } else { $scphost = $s['host']; } $cmd = fpbx_which('scp') . ' -o StrictHostKeyChecking=no -i ' . $s['key'] . ' -P ' . $s['port']; $cmd .= " " . $this->b['_tmpfile'] . " " . $s['user'] . "@{$scphost}:{$destdir}"; exec($cmd, $output, $ret); if ($ret !== 0) { backup_log("SCP Error ({$ret}) - Received " . json_encode($output) . " from {$cmd}"); } //run maintenance on the directory $this->maintenance($s['type'], $s); break; } } }