function plugins_releasemgt_file_generate_unique_name($p_seed, $p_filepath)
{
    do {
        $t_string = file_generate_name($p_seed);
    } while (!plugins_releasemgt_diskfile_is_name_unique($t_string, $p_filepath));
    return $t_string;
}
 function generate_pdf()
 {
     log_debug("template_engine_htmltopdf", "Executing generate_pdf()");
     //unset($_SESSION);
     /*
     	Generate temporary file for template
     */
     // generate unique tmp filename
     $tmp_filename = file_generate_name("/tmp/amberdms_billing_system");
     // write out template data
     if (!($handle = fopen("{$tmp_filename}.html", "w"))) {
         log_write("error", "template_engine_htmltopdf", "Failed to create temporary file ({$tmp_filename}.html) with template data");
         return 0;
     }
     $directory_prefix = $tmp_filename . "_";
     //$directory_prefix = "https://devel-web-tom.local.amberdms.com/development/amberdms/oss-amberdms-bs/trunk/templates/ar_invoice/ar_invoice_htmltopdf_telcostyle/";
     foreach ((array) $this->processed as $key => $processed_row) {
         $this->processed[$key] = str_replace("(tmp_filename)", $directory_prefix, $processed_row);
     }
     //print(implode("",$this->processed));
     //log_debug_render();
     //exit();
     foreach ($this->processed as $line) {
         if (fwrite($handle, $line) === FALSE) {
             log_write("error", "template_engine_htmltopdf", "Error occured whilst writing file ({$tmp_filename}.tex)");
             return 0;
         }
     }
     fclose($handle);
     /*
     	Create Directory for HTML data files & copy them over
     */
     $tmp_data_directory = $tmp_filename . "_html_data";
     mkdir($tmp_filename . "_html_data", 0700);
     $data_directory_items = glob($this->template_directory . "/html_data/*");
     foreach ((array) $data_directory_items as $data_dir_file) {
         $filename = basename($data_dir_file);
         $new_file_path = $tmp_data_directory . "/" . $filename;
         copy($data_dir_file, $new_file_path);
     }
     /*
     	Process with wkhtmltopdf
     
     	Note: In future, this may be extended to support alternative HTML to PDF rendering engines
     */
     // get the current directory so that we can change back after switching to the tmp directory
     $run_directory = getcwd();
     $app_wkhtmltopdf = sql_get_singlevalue("SELECT value FROM config WHERE name='APP_WKHTMLTOPDF' LIMIT 1");
     if (!is_executable($app_wkhtmltopdf)) {
         log_write("error", "process", "You have selected a template that requires the wkhtmltopdf engine, however {$app_wkhtmltopdf} does not exist or is not executable by your webserver process");
         return 0;
     }
     chdir("/tmp");
     // TODO: fix this to be a configurable value
     exec("{$app_wkhtmltopdf} -B 5mm -L 5mm -R 5mm -T 5mm {$tmp_filename}.html {$tmp_filename}.pdf 2>&1", $output);
     foreach ($output as $line) {
         log_debug("template_engine_htmltopdf", "wkhtmltopdf: {$line}");
     }
     if (in_array("wkhtmltopdf: cannot connect to X server", $output)) {
         log_debug("template_engine_htmltopdf", "Known fault on older systems, unable to run wkhtmltopdf without X server instance");
         /*
         	On older distribution versions of wkhtmltopdf, the shipped version of wkhtmltopdf/QT doesn't
         	work without a running X server.
         
         	This was fixed by patching both QT and wkhtmltopdf in later releases, however most distributions
         	don't appear to go to the extend of shipping this patched QT version. On RHEL platforms (as of RHEL 4/5/6), there is
         	no wkhtmltopdf, so we just ship a good package, on Ubuntu (as of 10.04 lts) where is the more limited package
         	we execute a wrapper script which runs a short lived Xorg session in a virtual framebuffer.
         
         	It's not perfect, in testing the PDFs are rendering nicely, apart from the page size being incorrect, regardless what paramaters
         	are passed to it - hence, we give the user a notification warning, so they know why the invoices are weird and how to fix it.
         
         	If we have numerious problems with a popular platform, then it will be worth Amberdms building packages for that platform, but
         	it's not a small effort.
         
         	TODO: Maybe we should have an external/natives/ static binary directory for some key essentials for common architectures?
         */
         $install_directory = dirname(__FILE__);
         $legacy_wrapper_cmd = "{$install_directory}/../../external/legacy/wkhtmltopdf_x11wrap.sh {$tmp_filename}.html {$tmp_filename}.pdf 2>&1";
         log_debug("template_engine_htmltopdf", "Executing {$legacy_wrapper_cmd}");
         $output = array();
         exec($legacy_wrapper_cmd, $output);
         foreach ($output as $line) {
             log_debug("template_engine_htmltopdf", "wkhtmltopdf (legacy wrapper): {$line}");
         }
         log_write("notification", "template_engine_htmltopdf", "Warning: Your server has a older/limited version of wkhtmltopdf installed, this can cause some performance and page-size rendering issues. If these cause you major issues, consider obtaining the static binary version and adjusting the configured executable path. A static version can be found on the wkhtmltopdf developer's website at http://code.google.com/p/wkhtmltopdf/downloads/list ");
     }
     /*
     	check that a PDF was generated
     */
     if (file_exists("{$tmp_filename}.pdf")) {
         log_debug("template_engine_htmltopdf", "Temporary PDF {$tmp_filename}.pdf generated");
         // import file data into memory
         $this->output = file_get_contents("{$tmp_filename}.pdf");
         // remove temporary files from disk
         @unlink("{$tmp_filename}");
         @unlink("{$tmp_filename}.aux");
         @unlink("{$tmp_filename}.log");
         @unlink("{$tmp_filename}.html");
         @unlink("{$tmp_filename}.pdf");
         // cleanup texlive home directory
         system("rm -rf " . $tmp_filename . "_html_data");
         // return back to original directory
         chdir($run_directory);
         return 1;
     } else {
         log_write("error", "template_engine_htmltopdf", "Unable to use wkhtmltopdf ({$app_wkhtmltopdf}) to generate PDF file");
         // return back to original directory
         chdir($run_directory);
         return 0;
     }
 }
Example #3
0
 function email_invoice($email_sender, $email_to, $email_cc, $email_bcc, $email_subject, $email_message)
 {
     log_debug("invoice", "Executing email_invoice([options])");
     // external dependency of Mail_Mime
     if (!@(include_once 'Mail.php')) {
         log_write("error", "invoice", "Unable to find Mail module required for sending email");
         return 0;
     }
     if (!@(include_once 'Mail/mime.php')) {
         log_write("error", "invoice", "Unable to find Mail::Mime module required for sending email");
         return 0;
     }
     // track attachment files to tidy up
     $file_attachments = array();
     /*
     	Prepare Email Mime Data & Headers
     */
     // fetch sender address
     //
     // users have the choice of sending as the company or as their own staff email address & name.
     //
     if ($email_sender == "user") {
         // send as the user
         $email_sender = "\"" . user_information("realname") . "\" <" . user_information("contact_email") . ">";
     } else {
         // send as the system
         $email_sender = "\"" . sql_get_singlevalue("SELECT value FROM config WHERE name='COMPANY_NAME'") . "\" <" . sql_get_singlevalue("SELECT value FROM config WHERE name='COMPANY_CONTACT_EMAIL'") . ">";
     }
     // prepare headers
     $mail_headers = array('From' => $email_sender, 'Subject' => $email_subject, 'Cc' => $email_cc, 'Bcc' => $email_bcc);
     $mail_mime = new Mail_mime("\n");
     $mail_mime->setTXTBody($email_message);
     /*
     	Generate a PDF of the invoice and save to tmp file
     */
     log_debug("invoice", "Generating invoice PDF for emailing");
     // generate PDF
     $this->generate_pdf();
     if (error_check()) {
         return 0;
     }
     // save to a temporary file
     if ($this->type == "ar") {
         $tmp_file_invoice = file_generate_name($GLOBALS["config"]["PATH_TMPDIR"] . "/invoice_" . $this->data["code_invoice"] . "", "pdf");
     } else {
         $tmp_file_invoice = file_generate_name($GLOBALS["config"]["PATH_TMPDIR"] . "/quote_" . $this->data["code_quote"] . "", "pdf");
         //$email_template	= sql_get_singlevalue("SELECT value FROM config WHERE name IN('TEMPLATE_QUOTE_EMAIL') LIMIT 1");
     }
     if (!($fhandle = fopen($tmp_file_invoice, "w"))) {
         log_write("error", "invoice", "A fatal error occured whilst writing invoice PDF to file {$tmp_file_invoice}, unable to send email");
         return 0;
     }
     fwrite($fhandle, $this->obj_pdf->output);
     fclose($fhandle);
     // attach
     $mail_mime->addAttachment($tmp_file_invoice, 'application/pdf');
     $file_attachments[] = $tmp_file_invoice;
     /*
     	Fetch Extra Attachments
     
     	Certain billing processes may add file attachments to the journal that should be sent along with the invoice
     	when an email is generated.
     
     	Here we grab those file attachments and send each one.
     */
     $obj_sql_journal = new sql_query();
     $obj_sql_journal->string = "SELECT id FROM journal WHERE journalname='account_ar' AND customid='" . $this->id . "' AND title LIKE 'SERVICE:%'";
     $obj_sql_journal->execute();
     if ($obj_sql_journal->num_rows()) {
         $obj_sql_journal->fetch_array();
         foreach ($obj_sql_journal->data as $data_journal) {
             // there are journaled attachments to send
             //
             // we don't care about any of the journal data, we just need to pull the file attachment from
             // storage, write to disk and then attach to the email
             //
             // fetch file object
             $file_obj = new file_storage();
             $file_obj->data["type"] = "journal";
             $file_obj->data["customid"] = $data_journal["id"];
             if (!$file_obj->load_data_bytype()) {
                 log_write("error", "inc_invoices", "Unable to load file from journal to attach to invoice email - possible file storage issue?");
                 return 0;
             }
             $file_extension = format_file_extension($file_obj->data["file_name"]);
             $file_name = format_file_noextension($file_obj->data["file_name"]);
             $file_ctype = format_file_contenttype($file_extension);
             // we have to write the file to disk before attaching it
             $tmp_file_attach = file_generate_name($GLOBALS["config"]["PATH_TMPDIR"] . "/" . $file_name, $file_extension);
             if (!$file_obj->filedata_write($tmp_file_attach)) {
                 log_write("error", "inc_invoices", "Unable to write file attachments from journal to tmp space");
                 return 0;
             }
             // add to the invoice
             $mail_mime->addAttachment($tmp_file_attach, $file_ctype);
             $file_attachments[] = $tmp_file_attach;
             // cleanup - tmp file will be removed ;ater
             unset($file_obj);
         }
         // end of for each journal item
     }
     // end if sendable journal items
     unset($obj_sql_journal);
     /*
     	Email the invoice
     */
     log_write("debug", "invoice", "Sending generated email....");
     $mail_body = $mail_mime->get();
     $mail_headers = $mail_mime->headers($mail_headers);
     $mail =& Mail::factory('mail', "-f " . $GLOBALS["config"]["COMPANY_CONTACT_EMAIL"]);
     $status = $mail->send($email_to, $mail_headers, $mail_body);
     if (PEAR::isError($status)) {
         log_write("error", "inc_invoice", "An error occured whilst attempting to send the email: " . $status->getMessage() . "");
     } else {
         log_write("debug", "inc_invoice", "Successfully sent email invoice");
         /*
         	Start SQL Transaction to post email to journal
         */
         $sql_obj = new sql_query();
         $sql_obj->trans_begin();
         /*
         	Mark the invoice as having been sent
         */
         $sql_obj = new sql_query();
         $sql_obj->string = "UPDATE account_" . $this->type . " SET date_sent='" . date("Y-m-d") . "', sentmethod='email' WHERE id='" . $this->id . "'";
         $sql_obj->execute();
         /*
         	Add the email information to the journal, including attaching a copy
         	of the generated PDF
         */
         log_write("debug", "inc_invoice", "Uploading PDF and email details to journal...");
         // create journal entry
         $journal = new journal_process();
         $journal->prepare_set_journalname("account_" . $this->type);
         $journal->prepare_set_customid($this->id);
         $journal->prepare_set_type("file");
         $journal->prepare_set_title("EMAIL: {$email_subject}");
         $data["content"] = NULL;
         $data["content"] .= "To:\t" . $email_to . "\n";
         $data["content"] .= "Cc:\t" . $email_cc . "\n";
         $data["content"] .= "Bcc:\t" . $email_bcc . "\n";
         $data["content"] .= "From:\t" . $email_sender . "\n";
         $data["content"] .= "\n";
         $data["content"] .= $email_message;
         $data["content"] .= "\n";
         $journal->prepare_set_content($data["content"]);
         $journal->action_update();
         // create journal entry
         $journal->action_lock();
         // lock it to prevent any changes to historical record of delivered email
         // upload PDF file as an attachement
         $file_obj = new file_storage();
         $file_obj->data["type"] = "journal";
         $file_obj->data["customid"] = $journal->structure["id"];
         if (!$file_obj->action_update_file($tmp_file_invoice)) {
             log_write("error", "inc_invoice", "Unable to upload emailed PDF to journal entry");
         }
         /*
         	Commit
         */
         if (error_check()) {
             $sql_obj->trans_rollback();
         } else {
             $sql_obj->trans_commit();
         }
     }
     // end if successful send
     // cleanup - remove the temporary files
     log_debug("inc_invoice", "Performing cleanup, removing temporary files used for emails");
     foreach ($file_attachments as $filename) {
         log_debug("inc_invoice", "Removing tmp file {$filename}");
         unlink($filename);
     }
     // return
     if (error_check()) {
         return 0;
     } else {
         return 1;
     }
 }
Example #4
0
function file_generate_unique_name($p_seed, $p_filepath)
{
    do {
        $t_string = file_generate_name($p_seed);
    } while (!diskfile_is_name_unique($t_string, $p_filepath));
    return $t_string;
}
Example #5
0
 function email_credit($email_sender, $email_to, $email_cc, $email_bcc, $email_subject, $email_message)
 {
     log_debug("inc_credits", "Executing email_credit([options])");
     // external dependency of Mail_Mime
     if (!@(include_once 'Mail.php')) {
         log_write("error", "inc_credits", "Unable to find Mail module required for sending email");
         return 0;
     }
     if (!@(include_once 'Mail/mime.php')) {
         log_write("error", "inc_credits", "Unable to find Mail::Mime module required for sending email");
         return 0;
     }
     /*
     	Generate a PDF of the credit note and save to tmp file
     */
     log_debug("inc_credits", "Generating credit note PDF for emailing");
     // generate PDF
     $this->generate_pdf();
     if (error_check()) {
         return 0;
     }
     // save to a temporary file
     $tmp_filename = file_generate_name("/tmp/credit_" . $this->data["code_credit"] . "", "pdf");
     if (!($fhandle = fopen($tmp_filename, "w"))) {
         die("fatal error occured whilst writing to file {$tmp_filename}");
     }
     fwrite($fhandle, $this->obj_pdf->output);
     fclose($fhandle);
     /*
     	Email the credit note
     */
     log_debug("inc_credits", "Sending email");
     // fetch sender address
     //
     // users have the choice of sending as the company or as their own staff email address & name.
     //
     if ($email_sender == "user") {
         // send as the user
         $email_sender = "\"" . user_information("realname") . "\" <" . user_information("contact_email") . ">";
     } else {
         // send as the system
         $email_sender = "\"" . sql_get_singlevalue("SELECT value FROM config WHERE name='COMPANY_NAME'") . "\" <" . sql_get_singlevalue("SELECT value FROM config WHERE name='COMPANY_CONTACT_EMAIL'") . ">";
     }
     // prepare headers
     $mail_headers = array('From' => $email_sender, 'Subject' => $email_subject, 'Cc' => $email_cc, 'Bcc' => $email_bcc);
     $mail_mime = new Mail_mime("\n");
     $mail_mime->setTXTBody($email_message);
     $mail_mime->addAttachment($tmp_filename, 'application/pdf');
     $mail_body = $mail_mime->get();
     $mail_headers = $mail_mime->headers($mail_headers);
     $mail =& Mail::factory('mail', "-f " . $GLOBALS["config"]["COMPANY_CONTACT_EMAIL"]);
     $status = $mail->send($email_to, $mail_headers, $mail_body);
     if (PEAR::isError($status)) {
         log_write("error", "inc_credits", "An error occured whilst attempting to send the email: " . $status->getMessage() . "");
     } else {
         log_write("debug", "inc_credits", "Successfully sent email invoice");
         /*
         	Start SQL Transaction to post email to journal
         */
         $sql_obj = new sql_query();
         $sql_obj->trans_begin();
         /*
         	Mark the invoice as having been sent
         */
         $sql_obj = new sql_query();
         $sql_obj->string = "UPDATE account_" . $this->type . " SET date_sent='" . date("Y-m-d") . "', sentmethod='email' WHERE id='" . $this->id . "'";
         $sql_obj->execute();
         /*
         	Add the email information to the journal, including attaching a copy
         	of the generated PDF
         */
         log_write("debug", "inc_credits", "Uploading PDF and email details to journal...");
         // create journal entry
         $journal = new journal_process();
         $journal->prepare_set_journalname("account_" . $this->type);
         $journal->prepare_set_customid($this->id);
         $journal->prepare_set_type("file");
         $journal->prepare_set_title("EMAIL: {$email_subject}");
         $data["content"] = NULL;
         $data["content"] .= "To:\t" . $email_to . "\n";
         $data["content"] .= "Cc:\t" . $email_cc . "\n";
         $data["content"] .= "Bcc:\t" . $email_bcc . "\n";
         $data["content"] .= "From:\t" . $email_sender . "\n";
         $data["content"] .= "\n";
         $data["content"] .= $email_message;
         $data["content"] .= "\n";
         $journal->prepare_set_content($data["content"]);
         $journal->action_update();
         // create journal entry
         $journal->action_lock();
         // lock it to prevent any changes to historical record of delivered email
         // upload PDF file as an attachement
         $file_obj = new file_storage();
         $file_obj->data["type"] = "journal";
         $file_obj->data["customid"] = $journal->structure["id"];
         if (!$file_obj->action_update_file($tmp_filename)) {
             log_write("error", "inc_credits", "Unable to upload emailed PDF to journal entry");
         }
         /*
         	Commit
         */
         if (error_check()) {
             $sql_obj->trans_rollback();
         } else {
             $sql_obj->trans_commit();
         }
     }
     // end if successful send
     // cleanup - remove the temporary files
     log_debug("inc_credits", "Performing cleanup - removing temporary file {$tmp_filename}");
     unlink($tmp_filename);
     // return
     if (error_check()) {
         return 0;
     } else {
         return 1;
     }
 }
Example #6
0
function file_generate_tmpfile()
{
    log_debug("inc_misc", "Executing file_generate_tmpfile()");
    $path_tmpdir = sql_get_singlevalue("SELECT value FROM config WHERE name='PATH_TMPDIR'");
    return file_generate_name("{$path_tmpdir}/temporary_file");
}