View source
<?php
define("DEFAULT_SERVLET_URL", variable_get('fillpdf_remote_protocol', 'http') . "://" . variable_get('fillpdf_remote_endpoint', "fillpdf-service.com/xmlrpc.php"));
module_load_include('inc', 'fillpdf', 'fillpdf.admin');
function fillpdf_help($path, $arg) {
switch ($path) {
case 'admin/help#fillpdf':
$content = t('See the <a href="!documentation">documentation on drupal.org</a> for a full description of and guide to this module.', array(
'!documentation' => url('http://drupal.org/documentation/modules/fillpdf'),
));
return $content;
case 'admin/structure/fillpdf':
if (module_exists('help')) {
return t('See the !link for an explanation on dowloading these forms to PDF', array(
'!link' => l(t('documentation'), 'http://drupal.org/documentation/modules/fillpdf'),
));
}
else {
return t('Activate the help module if you need an ' . 'explanation on downloading these forms to PDF.');
}
}
}
function fillpdf_menu() {
$access = array(
'administer pdfs',
);
$items = array();
$items['fillpdf'] = array(
'page callback' => 'fillpdf_parse_uri',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['admin/structure/fillpdf'] = array(
'title' => 'FillPDF',
'description' => 'Manage your PDFs',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_forms_admin',
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%'] = array(
'title' => 'Edit PDF form',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_form_edit',
3,
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%/delete'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_form_delete_confirm',
3,
),
'access arguments' => $access,
'type' => MENU_CALLBACK,
);
$items['admin/structure/fillpdf/%/export'] = array(
'title' => 'Export FillPDF field mappings',
'page callback' => 'fillpdf_form_export',
'page arguments' => array(
3,
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%/import'] = array(
'title' => 'Import FillPDF field mappings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_form_import_form',
3,
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%/add'] = array(
'title' => 'Add field',
'page callback' => 'fillpdf_field',
'page arguments' => array(
4,
3,
),
'access arguments' => $access,
'type' => MENU_LOCAL_TASK,
);
$items['admin/structure/fillpdf/%/edit/%'] = array(
'page callback' => 'fillpdf_field',
'page arguments' => array(
4,
3,
5,
),
'access arguments' => $access,
);
return $items;
}
function fillpdf_permission() {
$permissions = array(
'administer pdfs' => array(
'title' => t('Administer PDFs'),
'description' => t('Allows usage of the FillPDF administration screen.'),
),
'publish own pdfs' => array(
'title' => t('Publish Own PDFs'),
'description' => t("Allows filling in and downloading PDFs with one's own site content."),
),
'publish all pdfs' => array(
'title' => t('Publish All PDFs'),
'description' => t('Allows filling in and downloading PDFs with any site content.'),
),
);
if (module_exists('uc_order')) {
$order_statuses = uc_order_status_list();
foreach ($order_statuses as $order_status) {
$id = $order_status['id'];
$title = $order_status['title'];
$permissions["publish {$id} order data"] = array(
'title' => t("Publish data from %status Ubercart orders", array(
'%status' => $title,
)),
'description' => t("Publish data from Ubercart orders and ordered products with a status of %status.", array(
'%status' => $title,
)),
);
}
}
return $permissions;
}
function fillpdf_modules_enabled($modules) {
if (in_array('uc_order', $modules)) {
module_load_install('fillpdf');
_fillpdf_add_publish_completed_orders_permission();
}
}
function fillpdf_pdf_link($fid, $nids = NULL, $webform_arr = NULL, $sample = FALSE, $uc_order_ids = NULL, $uc_order_product_ids = NULL) {
$nids_uri = $webforms_uri = $uc_orders_uri = $uc_order_products_uri = "";
if (is_array($nids)) {
$nids_uri = '&nids[]=' . implode('&nids[]=', $nids);
}
elseif (isset($nids)) {
$nids_uri = "&nids[]={$nids}";
}
if (is_array($webform_arr)) {
if ($webform_arr['nid']) {
$webform_arr = array(
$webform_arr,
);
}
foreach ($webform_arr as $key => $webform) {
$webforms_uri .= "&webforms[{$key}][nid]={$webform['nid']}";
}
$webforms_uri .= $webform['sid'] ? "&webforms[{$key}][sid]={$webform['sid']}" : "";
}
if (is_array($uc_order_ids)) {
$uc_orders_uri = '&uc_order_ids[]=' . implode('&uc_order_ids[]=', $uc_order_ids);
}
elseif (isset($uc_order_ids)) {
$uc_orders_uri = "&uc_order_ids[]={$uc_order_ids}";
}
if (is_array($uc_order_product_ids)) {
$uc_order_products_uri = '&uc_order_product_ids[]=' . implode('&uc_order_product_ids[]=', $uc_order_product_ids);
}
elseif (isset($uc_order_product_ids)) {
$uc_order_products_uri = "&uc_order_product_ids[]={$uc_order_product_ids}";
}
$sample = $sample ? '&sample=true' : '';
return url('', array(
'absolute' => TRUE,
)) . "fillpdf?fid={$fid}{$nids_uri}{$webforms_uri}{$uc_orders_uri}{$uc_order_products_uri}{$sample}";
}
function fillpdf_parse_uri() {
$_GET += array(
'nid' => NULL,
'nids' => NULL,
'webform' => NULL,
'webforms' => NULL,
'uc_order_id' => NULL,
'uc_order_ids' => NULL,
'uc_order_product_id' => NULL,
'uc_order_product_ids' => NULL,
'fid' => NULL,
'sample' => NULL,
'download' => NULL,
'flatten' => NULL,
);
$force_download = FALSE;
$flatten = TRUE;
$sample = $_GET['sample'];
$fid = $_GET['fid'];
$nids = $webforms = $uc_order_ids = $uc_order_product_ids = array();
if ($_GET['nid'] || $_GET['nids']) {
$nids = $_GET['nid'] ? array(
$_GET['nid'],
) : $_GET['nids'];
}
if ($_GET['webform'] || $_GET['webforms']) {
$webforms = $_GET['webform'] ? array(
$_GET['webform'],
) : $_GET['webforms'];
}
if ($_GET['uc_order_id'] || $_GET['uc_order_ids']) {
$uc_order_ids = $_GET['uc_order_id'] ? array(
$_GET['uc_order_id'],
) : $_GET['uc_order_ids'];
}
if ($_GET['uc_order_product_id'] || $_GET['uc_order_product_ids']) {
$uc_order_product_ids = $_GET['uc_order_product_id'] ? array(
$_GET['uc_order_product_id'],
) : $_GET['uc_order_product_ids'];
}
if (isset($_GET['download']) && (int) $_GET['download'] == 1) {
$force_download = TRUE;
}
if (isset($_GET['flatten']) && (int) $_GET['flatten'] == 0) {
$flatten = FALSE;
}
fillpdf_merge_pdf($fid, $nids, $webforms, $sample, $force_download, FALSE, $flatten, TRUE, $uc_order_ids, $uc_order_product_ids);
}
function fillpdf_merge_pdf($fid, $nids = NULL, $webform_arr = NULL, $sample = NULL, $force_download = FALSE, $skip_access_check = FALSE, $flatten = TRUE, $handle = TRUE, $uc_order_ids = NULL, $uc_order_product_ids) {
if (is_null($fid)) {
drupal_set_message(t('FillPDF Form ID required to print a PDF.'), 'warning');
drupal_goto();
}
$fillpdf_info = fillpdf_load($fid);
if ($fillpdf_info === FALSE) {
drupal_set_message(t('Non-existent FillPDF Form ID.'), 'error');
drupal_not_found();
drupal_exit();
}
global $user;
$nodes = $webforms = $uc_orders = $uc_order_products = array();
if (!empty($fillpdf_info->default_nid) && is_array($webform_arr)) {
foreach (array_keys($webform_arr) as $key) {
if (empty($webform_arr[$key]['nid'])) {
$webform_arr[$key]['nid'] = $fillpdf_info->default_nid;
}
}
}
if (!empty($fillpdf_info->default_nid) && empty($nids) && empty($webform_arr)) {
$default_node = node_load($fillpdf_info->default_nid);
if ($default_node) {
if (empty($default_node->webform)) {
$nodes[] = $default_node;
}
else {
$webform_arr = array(
array(
'nid' => $fillpdf_info->default_nid,
'node' => $default_node,
),
);
}
}
}
if (is_array($nids)) {
foreach ($nids as $nid) {
$nodes[] = node_load($nid);
}
}
if (module_exists('webform') && is_array($webform_arr)) {
$included = module_load_include('inc', 'webform', 'includes/webform.submissions');
if ($included === FALSE) {
module_load_include('inc', 'webform', 'webform_submissions');
}
foreach ($webform_arr as $webform) {
if (!empty($webform['nid'])) {
if (empty($webform['sid'])) {
$webform['sid'] = db_query('SELECT sid FROM {webform_submissions}
WHERE nid = :nid AND uid = :uid ORDER BY submitted DESC', array(
':nid' => $webform['nid'],
':uid' => $user->uid,
))
->fetchField();
}
if ($webform['sid'] !== FALSE) {
$webforms[] = array(
'webform' => empty($webform['node']) ? node_load($webform['nid']) : $webform['node'],
'submission' => webform_get_submission($webform['nid'], $webform['sid']),
);
}
}
}
}
if (module_exists('uc_order') && is_array($uc_order_ids)) {
foreach ($uc_order_ids as $uc_order_id) {
$uc_orders[] = uc_order_load($uc_order_id);
}
}
if (module_exists('uc_order') && is_array($uc_order_product_ids)) {
foreach ($uc_order_product_ids as $uc_order_product_id) {
$uc_order_products[] = uc_order_product_load($uc_order_product_id);
}
}
if ($skip_access_check !== TRUE) {
if (!fillpdf_merge_pdf_access($nodes, $webforms, $uc_orders, $uc_order_products)) {
drupal_access_denied();
drupal_exit();
}
}
$fields = $token_objects = $image_data = array();
$query = db_query("SELECT * FROM {fillpdf_fields} WHERE fid = :fid", array(
':fid' => $fid,
));
foreach ($query as $obj) {
$obj->replacements = _fillpdf_replacements_to_array($obj->replacements);
$transform_string = FALSE;
if ($sample == 'true') {
$fields[$obj->pdf_key] = $obj->pdf_key;
$_GET['destination'] = "admin/structure/fillpdf/{$fid}";
}
else {
$nodes = array_reverse($nodes);
$webforms = array_reverse($webforms);
$uc_orders = array_reverse($uc_orders);
$uc_order_products = array_reverse($uc_order_products);
if (!empty($nodes)) {
foreach ($nodes as $node) {
$token_objects['node'] = $node;
$token = token_replace($obj->value, array(
'node' => $node,
), array(
'clear' => TRUE,
'sanitize' => FALSE,
));
if ($token && $token != $obj->value) {
break;
}
}
$transform_string = TRUE;
if (strstr($obj->value, '[stamp:')) {
$left_side = strpos($obj->value, '[stamp:') + 7;
$right_side = strpos($obj->value, ']');
$field_name = substr($obj->value, $left_side, $right_side - $left_side);
if (isset($node->{$field_name}[$node->language])) {
$image_path = $node->{$field_name}[$node->language][0]['uri'];
$transform_string = FALSE;
$fields[$obj->pdf_key] = '{image}' . drupal_realpath($image_path);
$image_path_info = pathinfo(drupal_realpath($image_path));
$file_data = file_get_contents(drupal_realpath($image_path));
if ($file_data) {
$image_data[$obj->pdf_key] = array(
'data' => base64_encode($file_data),
'filenamehash' => md5($image_path_info['filename']) . '.' . $image_path_info['extension'],
);
}
}
}
}
if (!empty($webforms)) {
foreach ($webforms as $webform) {
$token_objects += array(
'webform-submission' => $webform['submission'],
'submission' => $webform['submission'],
'node' => $webform['webform'],
);
$token = token_replace($obj->value, $token_objects, array(
'clear' => TRUE,
'sanitize' => FALSE,
));
if ($token) {
break;
}
}
$transform_string = TRUE;
}
if (!empty($uc_orders)) {
foreach ($uc_orders as $uc_order) {
$token_objects += array(
'uc_order' => $uc_order,
);
$token = token_replace($obj->value, $token_objects, array(
'clear' => TRUE,
'sanitize' => FALSE,
));
if ($token) {
break;
}
}
$transform_string = TRUE;
}
if (!empty($uc_order_products)) {
foreach ($uc_order_products as $uc_order_product) {
$token_objects += array(
'uc_order_product' => $uc_order_product,
'uc_order' => uc_order_load($uc_order_product->order_id),
'node' => node_load($uc_order_product->nid),
);
$token = token_replace($obj->value, $token_objects, array(
'clear' => TRUE,
'sanitize' => FALSE,
));
if ($token) {
break;
}
}
$transform_string = TRUE;
}
if ($transform_string) {
$str = preg_replace('|<br />|', '
', $token);
$str = _fillpdf_transform_field_value($str, $fillpdf_info->replacements, $obj->replacements);
$fields[$obj->pdf_key] = $str;
}
}
if (isset($fields[$obj->pdf_key]) && $fields[$obj->pdf_key]) {
if ($obj->prefix) {
$fields[$obj->pdf_key] = $obj->prefix . $fields[$obj->pdf_key];
}
if ($obj->suffix) {
$fields[$obj->pdf_key] .= $obj->suffix;
}
}
}
$context = array(
'nodes' => $nodes,
'webforms' => $webforms,
'uc_orders' => $uc_orders,
'uc_order_products' => $uc_order_products,
);
drupal_alter('fillpdf_merge_fields_alter', $fields, $context);
$fill_options = array(
'flatten' => $flatten,
'image data' => $image_data,
);
$data = pdf_forms_fill($fillpdf_info->url, $fields, $fill_options);
if ($webform_arr && isset($webform['webform'])) {
$node = $webform['webform'];
}
if (!empty($node)) {
watchdog('fillpdf', 'User generated form "%form" for node "%node".', array(
'%form' => $fillpdf_info->title,
'%node' => $node->title,
));
}
$fillpdf_object = _fillpdf_build_options_object($force_download, $flatten, $fillpdf_info, $data, $nodes, $webforms, $uc_orders, $uc_order_products, $token_objects);
if ($handle === TRUE) {
module_invoke_all('fillpdf_merge_pre_handle', $fillpdf_object);
fillpdf_merge_handle_pdf($fillpdf_object->info, $fillpdf_object->data, $fillpdf_object->token_objects, 'default', $force_download);
}
else {
return $fillpdf_object;
}
}
function _fillpdf_build_options_object($force_download, $flatten, $fillpdf_info, $data, $nodes, $webforms, $uc_orders, $uc_order_products, $token_objects) {
$fillpdf_object = new stdClass();
$fillpdf_object->info = $fillpdf_info;
$fillpdf_object->data = $data;
$fillpdf_object->context = array(
'nodes' => $nodes,
'webforms' => $webforms,
'uc_orders' => $uc_orders,
'uc_order_products' => $uc_order_products,
);
$fillpdf_object->token_objects = $token_objects;
$fillpdf_object->options = array(
'download' => $force_download,
'flatten' => $flatten,
);
return $fillpdf_object;
}
function fillpdf_merge_handle_pdf($pdf_info, $pdf_data, $token_objects, $action = 'download', $force_download = FALSE) {
if (in_array($action, array(
'default',
'download',
'save',
'redirect',
)) === FALSE) {
return;
}
$output_name = _fillpdf_process_filename($pdf_info->title, $token_objects);
if ($action == 'default') {
if (empty($pdf_info->destination_path) === FALSE) {
$action = 'save';
}
else {
$action = 'download';
}
}
$redirect_to_file = FALSE;
switch ($action) {
case 'redirect':
$redirect_to_file = $pdf_info->destination_redirect;
case 'save':
fillpdf_save_to_file($pdf_info, $pdf_data, $token_objects, $output_name, !$force_download, $redirect_to_file);
case 'download':
drupal_add_http_header("Pragma", "public");
drupal_add_http_header('Expires', 0);
drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
drupal_add_http_header('Content-type', 'application-download');
drupal_add_http_header('Content-Length', strlen($pdf_data));
drupal_add_http_header('Content-disposition', 'attachment; filename="' . $output_name . '"');
drupal_add_http_header('Content-Transfer-Encoding', 'binary');
echo $pdf_data;
drupal_exit();
break;
}
}
function fillpdf_save_to_file($pdf_info, $pdf_data, $token_objects, $output_name, $redirect = TRUE, $redirect_to_file = FALSE, $destination_path_override = NULL) {
if (isset($destination_path_override) && empty($destination_path_override) !== FALSE) {
$destination_path = $destination_path_override;
}
if (empty($pdf_info->destination_path) && empty($destination_path_override)) {
$destination_path = 'fillpdf';
}
else {
$destination_path = $pdf_info->destination_path;
}
$destination_path = _fillpdf_process_destination_path($pdf_info->destination_path, $token_objects);
$path_exists = file_prepare_directory($destination_path, FILE_CREATE_DIRECTORY + FILE_MODIFY_PERMISSIONS);
if ($path_exists === FALSE) {
watchdog('fillpdf', "The path %destination_path does not exist and could not be\n automatically created. Therefore, the previous submission was not saved. If\n the URL contained download=1, then the PDF was still sent to the user's browser.\n If you were redirecting them to the PDF, they were sent to the homepage instead.\n If the destination path looks wrong and you have used tokens, check that you have\n used the correct token and that it is available to FillPDF at the time of PDF\n generation.", array(
'%destination_path' => $destination_path,
));
}
else {
$saved_file_path = file_unmanaged_save_data($pdf_data, $destination_path . "/{$output_name}", FILE_EXISTS_RENAME);
if ($redirect === TRUE) {
if (isset($_GET['destination']) === FALSE) {
if ($redirect_to_file) {
drupal_goto(file_create_url($saved_file_path));
}
}
}
}
if ($redirect === TRUE) {
drupal_goto();
}
return $saved_file_path;
}
function fillpdf_fillpdf_merge_pre_handle($fillpdf) {
if (module_exists('rules')) {
rules_invoke_event('fillpdf_merge_pre_handle', $fillpdf);
}
}
function fillpdf_merge_pdf_access($nodes = array(), $webforms = array(), $uc_orders = array(), $uc_order_products = array()) {
if (user_access('administer pdfs') || user_access('publish all pdfs')) {
return TRUE;
}
if (!user_access('publish own pdfs')) {
return FALSE;
}
global $user;
$account = user_load($user->uid);
if (empty($webforms)) {
foreach ($nodes as $node) {
if (!node_access('view', $node) || $node->uid != $user->uid) {
return FALSE;
}
}
}
else {
foreach ($webforms as $webform) {
if (!node_access('view', node_load($webform['webform']->nid))) {
return FALSE;
}
}
}
if (!empty($webforms)) {
foreach ($webforms as $webform) {
if (!webform_submission_access($webform['webform'], $webform['submission'], 'view')) {
return FALSE;
}
}
}
if (!empty($uc_orders)) {
foreach ($uc_orders as $uc_order) {
$order_status = $uc_order->order_status;
if ((!uc_order_order_entity_access('view', $uc_order, $account) || !user_access("publish {$order_status} order data")) && !user_access('view all orders')) {
return FALSE;
}
}
}
if (!empty($uc_order_products)) {
foreach ($uc_order_products as $uc_order_product) {
$order = uc_order_load($uc_order_product->order_id);
$order_status = $order->order_status;
if ((!uc_order_order_product_access('view', $uc_order_product, $account) || !user_access("publish {$order_status} order data")) && !user_access('view all orders')) {
return FALSE;
}
}
}
return TRUE;
}
function _fillpdf_process_filename($original, $token_objects) {
if (!empty($token_objects)) {
$original = token_replace($original, $token_objects);
}
$output_name = str_replace(' ', '_', $original);
$output_name = preg_replace('/\\.pdf$/i', '', $output_name);
$output_name = preg_replace('/[^a-zA-Z0-9_.-]+/', '', $output_name) . '.pdf';
return $output_name;
}
function fillpdf_build_filename($original, $token_objects) {
return _fillpdf_process_filename($original, $token_objects);
}
function fillpdf_execute_merge($method, $fields, $fillpdf, $mode = 'url', $flatten = TRUE) {
$data = NULL;
switch ($mode) {
case 'url':
$filename = $fillpdf->url;
break;
case 'stream':
$filename = file_unmanaged_save_data($fillpdf, file_directory_temp() . '/pdf_data.pdf', FILE_EXISTS_RENAME);
break;
}
switch ($method) {
case 'pdftk':
module_load_include('inc', 'pdf_forms', 'pdf_forms.xfdf');
$xfdfname = $filename . '.xfdf';
$xfdf = pdf_forms_xfdf_create(basename($xfdfname), $fields);
$xfdffile = file_save_data($xfdf, $xfdfname, FILE_EXISTS_RENAME);
$path_to_pdftk = variable_get('fillpdf_pdftk_path', 'pdftk');
ob_start();
passthru($path_to_pdftk . ' ' . escapeshellarg(drupal_realpath($filename)) . ' fill_form ' . escapeshellarg(drupal_realpath($xfdffile->uri)) . ' output - ' . ($flatten ? 'flatten ' : '') . 'drop_xfa');
$data = ob_get_clean();
if ($data === FALSE) {
drupal_set_message(t('pdftk not properly installed. No PDF generated.'), 'error');
}
file_delete($xfdffile);
if ($mode == 'stream') {
file_unmanaged_delete($filename);
}
break;
}
if ($data) {
return $data;
}
else {
return FALSE;
}
}
function fillpdf_parse_pdf($fid) {
$filename = db_query("SELECT url FROM {fillpdf_forms} WHERE fid = :fid", array(
':fid' => $fid,
))
->fetchField();
db_delete('fillpdf_fields')
->condition('fid', $fid)
->execute();
$fields = pdf_forms_parse($filename);
foreach ((array) $fields as $key => $arr) {
if ($arr['type']) {
$arr['name'] = str_replace('�', '', $arr['name']);
$field = new stdClass();
$field->fid = $fid;
$field->pdf_key = $arr['name'];
$field->label = NULL;
$field->value = '';
drupal_write_record('fillpdf_fields', $field);
}
}
}
function fillpdf_execute_parse($method, $fillpdf, $mode = 'url') {
switch ($mode) {
case 'url':
$filename = $fillpdf;
break;
case 'stream':
$filename = file_unmanaged_save_data($fillpdf, file_directory_temp() . '/pdf_data.pdf', FILE_EXISTS_RENAME);
break;
}
$path_to_pdftk = variable_get('fillpdf_pdftk_path', 'pdftk');
$status = fillpdf_pdftk_check($path_to_pdftk);
if ($status === FALSE) {
drupal_set_message(t('pdftk not properly installed.'), 'error');
return array();
}
$output = array();
exec($path_to_pdftk . ' ' . escapeshellarg(drupal_realpath($filename)) . ' dump_data_fields', $output, $status);
if (count($output) === 0) {
drupal_set_message(t('PDF does not contain fillable fields.'), 'warning');
return array();
}
$data_fields_map = array(
'FieldType' => 'type',
'FieldName' => 'name',
'FieldFlags' => 'flags',
'FieldJustification' => 'justification',
);
$fields = array();
$fieldindex = -1;
foreach ($output as $line => $lineitem) {
if ($lineitem == '---') {
$fieldindex++;
continue;
}
$linedata = explode(':', $lineitem);
if (in_array($linedata[0], array_keys($data_fields_map))) {
$fields[$fieldindex][$data_fields_map[$linedata[0]]] = trim($linedata[1]);
}
}
if ($mode == 'stream') {
file_unmanaged_delete($filename);
}
return $fields;
}
function _fillpdf_get_file_contents($filepath, $error_goto = NULL) {
$filepath = drupal_realpath($filepath);
if ($error_goto && !file_exists($filepath)) {
drupal_set_message(t('@filepath does not exist. Check your
filesystem settings, as well as http://drupal.org/node/764936', array(
'@filepath' => $filepath,
)), 'error');
drupal_goto($error_goto);
}
$handle = fopen($filepath, "r");
$content = fread($handle, filesize($filepath));
fclose($handle);
return $content;
}
function _fillpdf_xmlrpc_request($url, $method) {
$args = func_get_args();
array_shift($args);
$args = array(
$args[0] => array_slice($args, 1),
);
$result = xmlrpc($url, $args);
$ret = new stdClass();
if (isset($result['error'])) {
drupal_set_message($result['error'], 'error');
$ret->error = TRUE;
}
elseif ($result == FALSE || xmlrpc_error()) {
$error = xmlrpc_error();
$ret->error = TRUE;
drupal_set_message(t('There was a problem contacting the FillPDF Service.
It may be down, or you may not have internet access. [ERROR @code: @message]', array(
'@code' => $error->code,
'@message' => $error->message,
)), 'error');
}
else {
$ret->data = $result['data'];
$ret->error = FALSE;
}
return $ret;
}
function fillpdf_get_fields($fid) {
$result = db_query('SELECT * FROM {fillpdf_fields} WHERE fid = :fid', array(
':fid' => $fid,
));
$return = array(
'pdf_key' => '',
'label' => '',
'value' => '',
);
foreach ($result as $result_array) {
$return[$result_array->pdf_key] = array(
'label' => $result_array->label,
'value' => $result_array->value,
);
}
return $return;
}
function _fillpdf_process_destination_path($destination_path, $token_objects) {
$orig_path = $destination_path;
$destination_path = trim($orig_path);
$types = array();
if (isset($token_objects['node'])) {
$types[] = 'node';
}
elseif (isset($token_objects['webform'])) {
$types[] = 'webform';
}
foreach ($types as $type) {
$destination_path = token_replace($destination_path, array(
$type => $token_objects[$type],
), array(
'clear' => TRUE,
));
}
if (drupal_substr($destination_path, 0, 1) === '/') {
}
else {
$destination_path = file_build_uri($destination_path);
}
return $destination_path;
}
function _fillpdf_replacements_to_array($replacements) {
if (empty($replacements) !== TRUE) {
$standardized_replacements = str_replace(array(
"\r\n",
"\r",
), "\n", $replacements);
$lines = explode("\n", $standardized_replacements);
$return = array();
foreach ($lines as $replacement) {
if (!empty($replacement)) {
$split = explode('|', $replacement);
if (count($split) == 2) {
$return[$split[0]] = preg_replace('|<br />|', '
', $split[1]);
}
}
}
return $return;
}
else {
return array();
}
}
function _fillpdf_transform_field_value($value, $pdf_replacements, $field_replacements) {
if (empty($pdf_replacements) && empty($field_replacements)) {
return $value;
}
elseif (!empty($field_replacements) && isset($field_replacements[$value])) {
return $field_replacements[$value];
}
elseif (!empty($pdf_replacements) && isset($pdf_replacements[$value])) {
return $pdf_replacements[$value];
}
else {
return $value;
}
}
function fillpdf_load($fid, $reset = FALSE) {
static $fillpdf = array();
if (isset($fillpdf[$fid]) && $reset === FALSE) {
}
else {
$fillpdf[$fid] = db_query("SELECT * FROM {fillpdf_forms} WHERE fid = :fid", array(
':fid' => $fid,
))
->fetch();
}
if ($fillpdf[$fid]) {
$fillpdf[$fid]->replacements = _fillpdf_replacements_to_array($fillpdf[$fid]->replacements);
}
if ($fillpdf[$fid]) {
return $fillpdf[$fid];
}
else {
return FALSE;
}
}
function fillpdf_pdftk_check($pdftk_path = 'pdftk') {
if (empty($pdftk_path)) {
$pdftk_path = 'pdftk';
}
$output = array();
$status = NULL;
exec($pdftk_path, $output, $status);
if (in_array($status, array(
126,
127,
))) {
return FALSE;
}
return TRUE;
}