| 1 |
<?php |
<?php |
| 2 |
|
|
| 3 |
|
/* |
| 4 |
|
* Author : Tom MacWright, Young Hahn |
| 5 |
|
* Revision : $Id$ |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
/** |
/** |
| 9 |
* This module utilizes XSLT transforms to go from "export to web" |
* This module utilizes XSLT transforms to go from "export to web" |
| 10 |
* Word documents to clean, valid XHTML. |
* Word documents to clean, valid XHTML. |
| 27 |
'access' => user_access('administer site configuration'), |
'access' => user_access('administer site configuration'), |
| 28 |
); |
); |
| 29 |
} |
} |
| 30 |
if(is_numeric(arg(1)) && arg(0) == 'node') { |
if (is_numeric(arg(1)) && arg(0) == 'node') { |
| 31 |
if(variable_get('word2web_images', false)) { |
if (variable_get('word2web_images', false)) { |
| 32 |
$node = node_load(arg(1)); |
$node = node_load(arg(1)); |
| 33 |
$nodetypes = variable_get('word2web_nodetypes', array()); |
$nodetypes = variable_get('word2web_nodetypes', array()); |
| 34 |
if($nodetypes[$node->type]) { |
if ($nodetypes[$node->type]) { |
| 35 |
$items[] = array('path' => 'node/'. arg(1) .'/manage_images', |
$items[] = array('path' => 'node/'. arg(1) .'/manage_images', |
| 36 |
'title' => t('Manage Images'), |
'title' => t('Manage Images'), |
| 37 |
'callback' => 'drupal_get_form', |
'callback' => 'drupal_get_form', |
| 79 |
set_error_handler("_word2web_suppress_errors"); |
set_error_handler("_word2web_suppress_errors"); |
| 80 |
$node = node_load($nid); |
$node = node_load($nid); |
| 81 |
$images = _word2web_get_orphan_images($node->body); |
$images = _word2web_get_orphan_images($node->body); |
| 82 |
if($images) { |
if ($images) { |
| 83 |
$form['images_help'] = array( |
$form['images_help'] = array( |
| 84 |
'#value' => t("These images were included in the !type but have not been uploaded yet.<br /><br />", |
'#value' => t("These images were included in the !type but have not been uploaded yet.<br /><br />", |
| 85 |
array('!type' => $node->type))); |
array('!type' => $node->type))); |
| 86 |
$i = 0; |
$i = 0; |
| 87 |
foreach($images as $img) { |
foreach ($images as $img) { |
| 88 |
$form['images']['upload_'.$i] = array( |
$form['images']['upload_'. $i] = array( |
| 89 |
'#type' => 'file', |
'#type' => 'file', |
| 90 |
'#title' => t('!filename', array('!filename' => $img)), |
'#title' => t('!filename', array('!filename' => $img)), |
| 91 |
'#size' => 40); |
'#size' => 40); |
| 92 |
$form['file_names']['upload_'.$i] = array( |
$form['file_names']['upload_'. $i] = array( |
| 93 |
'#type' => 'hidden', |
'#type' => 'hidden', |
| 94 |
'#value' => $img); |
'#value' => $img); |
| 95 |
$i++; |
$i++; |
| 102 |
'#type' => 'submit', |
'#type' => 'submit', |
| 103 |
'#value' => 'Upload Images'); |
'#value' => 'Upload Images'); |
| 104 |
restore_error_handler(); |
restore_error_handler(); |
| 105 |
} else { |
} |
| 106 |
|
else { |
| 107 |
$form['images_complete'] = array( |
$form['images_complete'] = array( |
| 108 |
'#value' => t("All of the images in this report have been uploaded")); |
'#value' => t("All of the images in this report have been uploaded")); |
| 109 |
} |
} |
| 118 |
|
|
| 119 |
function word2web_manage_images_submit($form_id, $form) { |
function word2web_manage_images_submit($form_id, $form) { |
| 120 |
$success = true; |
$success = true; |
| 121 |
foreach($form as $f=>$v) { |
foreach ($form as $f => $v) { |
| 122 |
// !== because PHP's type system is weird / sucks |
// !== because PHP's type system is weird / sucks |
| 123 |
if(strpos($f, 'upload') !== false) { |
if (strpos($f, 'upload') !== false) { |
| 124 |
if($fi = file_check_upload($f)) { |
if ($fi = file_check_upload($f)) { |
| 125 |
$new_id = _word2web_node_image($fi->filepath, $fi->filename); |
$new_id = _word2web_node_image($fi->filepath, $fi->filename); |
| 126 |
if($new_id == -1) { |
if ($new_id == -1) { |
| 127 |
drupal_set_message("Image files could not be uploaded because the image directory is not configured correctly."); |
drupal_set_message("Image files could not be uploaded because the image directory is not configured correctly."); |
| 128 |
$success = false; |
$success = false; |
| 129 |
break; |
break; |
| 132 |
} |
} |
| 133 |
} |
} |
| 134 |
} |
} |
| 135 |
if($success) { |
if ($success) { |
| 136 |
_word2web_insert_images($form['book_nid'], $map); |
_word2web_insert_images($form['book_nid'], $map); |
| 137 |
} |
} |
| 138 |
} |
} |
| 150 |
$html->loadHTML($node->body); |
$html->loadHTML($node->body); |
| 151 |
$images = $html->getElementsByTagName("image"); |
$images = $html->getElementsByTagName("image"); |
| 152 |
$i = 0; |
$i = 0; |
| 153 |
foreach($images as $im) { |
foreach ($images as $im) { |
| 154 |
// Only do the replacement if |
// Only do the replacement if |
| 155 |
// 1. This image hasn't already been replaced |
// 1. This image hasn't already been replaced |
| 156 |
// 2. A file has been uploaded to replace this image |
// 2. A file has been uploaded to replace this image |
| 157 |
if(!$im->getAttribute("nid") && $map[$im->getAttribute("src")]) { |
if (!$im->getAttribute("nid") && $map[$im->getAttribute("src")]) { |
| 158 |
$im->removeChild($im->firstChild); |
$im->removeChild($im->firstChild); |
| 159 |
$im->setAttribute("nid", $map[$im->getAttribute("src")]); |
$im->setAttribute("nid", $map[$im->getAttribute("src")]); |
| 160 |
} |
} |
| 167 |
|
|
| 168 |
function _word2web_node_image($url, $title='') { |
function _word2web_node_image($url, $title='') { |
| 169 |
restore_error_handler(); |
restore_error_handler(); |
| 170 |
if(!$title) { |
if (!$title) { |
| 171 |
$title = basename($url); |
$title = basename($url); |
| 172 |
} |
} |
| 173 |
$node = array('type' => 'image'); |
$node = array('type' => 'image'); |
| 174 |
$values['title'] = basename($url); |
$values['title'] = basename($url); |
| 175 |
/* http://drupal.org/node/201594 */ |
/* http://drupal.org/node/201594 */ |
| 176 |
$file_data = file_get_contents($url); |
$file_data = file_get_contents($url); |
| 177 |
$file_temp = file_save_data($file_data, file_directory_path().'/'.basename($url), FILE_EXISTS_RENAME); |
$file_temp = file_save_data($file_data, file_directory_path() .'/'. basename($url), FILE_EXISTS_RENAME); |
| 178 |
if($file_temp) { |
if ($file_temp) { |
| 179 |
$node['field_imagefile'] = array( |
$node['field_imagefile'] = array( |
| 180 |
array( |
array( |
| 181 |
'fid' => 'upload', |
'fid' => 'upload', |
| 199 |
$html->loadHTML($node->body); |
$html->loadHTML($node->body); |
| 200 |
$images = $html->getElementsByTagName("image"); |
$images = $html->getElementsByTagName("image"); |
| 201 |
$i = 0; |
$i = 0; |
| 202 |
foreach($images as $im) { |
foreach ($images as $im) { |
| 203 |
if(!$im->getAttribute("nid")) { |
if (!$im->getAttribute("nid")) { |
| 204 |
$i++; |
$i++; |
| 205 |
} |
} |
| 206 |
} |
} |
| 207 |
restore_error_handler(); |
restore_error_handler(); |
| 208 |
if($i > 0) { |
if ($i > 0) { |
| 209 |
drupal_set_message(l(t('!num images in this !type need to be uploaded.', array('!type' => $node->type, '!num' => $i)), 'node/'.$node->nid.'/manage_images'), 'error'); |
drupal_set_message(l(t('!num images in this !type need to be uploaded.', array('!type' => $node->type, '!num' => $i)), 'node/'. $node->nid .'/manage_images'), 'error'); |
| 210 |
} |
} |
| 211 |
} |
} |
| 212 |
|
|
| 216 |
function word2web_nodeapi(&$node, $op) { |
function word2web_nodeapi(&$node, $op) { |
| 217 |
switch ($op) { |
switch ($op) { |
| 218 |
case 'view': |
case 'view': |
| 219 |
if(in_array($node->type, variable_get('word2web_nodetypes', array()))) { |
if (in_array($node->type, variable_get('word2web_nodetypes', array()))) { |
| 220 |
if(user_access("administer nodes")) { |
if (user_access("administer nodes")) { |
| 221 |
word2web_image_upload_message($node); |
word2web_image_upload_message($node); |
| 222 |
} |
} |
| 223 |
} |
} |
| 224 |
case 'submit': |
case 'submit': |
| 225 |
case 'update': |
case 'update': |
| 226 |
if($html_file = file_check_upload('word_document')) { |
if ($html_file = file_check_upload('word_document')) { |
| 227 |
$path = drupal_get_path('module', 'word2web'); |
$path = drupal_get_path('module', 'word2web'); |
| 228 |
$html_raw = file_get_contents($html_file->filepath); |
$html_raw = file_get_contents($html_file->filepath); |
| 229 |
$html_raw = _word2web_convert_chr($html_raw); |
$html_raw = _word2web_convert_chr($html_raw); |
| 239 |
* XSL transformations. It also collects a list of URLs of images |
* XSL transformations. It also collects a list of URLs of images |
| 240 |
* so that we'll know what to fetch from the user |
* so that we'll know what to fetch from the user |
| 241 |
*/ |
*/ |
| 242 |
foreach($images as $im) { |
foreach ($images as $im) { |
| 243 |
$image_node = $html->createElement("image"); |
$image_node = $html->createElement("image"); |
| 244 |
$image_node->appendChild($html->createTextNode(" ")); |
$image_node->appendChild($html->createTextNode(" ")); |
| 245 |
$image_node->setAttribute("src", $im->getAttribute("src")); |
$image_node->setAttribute("src", $im->getAttribute("src")); |
| 277 |
$html = new DOMDocument(); |
$html = new DOMDocument(); |
| 278 |
$html->loadHTML($c); |
$html->loadHTML($c); |
| 279 |
$images = $html->getElementsByTagName('image'); |
$images = $html->getElementsByTagName('image'); |
| 280 |
foreach($images as $im) { |
foreach ($images as $im) { |
| 281 |
if(!$im->getAttribute("nid")) { // If it doesn't have a node id, it isn't up |
if (!$im->getAttribute("nid")) { // If it doesn't have a node id, it isn't up |
| 282 |
$urls[] = $im->getAttribute("src"); |
$urls[] = $im->getAttribute("src"); |
| 283 |
} |
} |
| 284 |
} |
} |
| 299 |
$html = new DOMDocument(); |
$html = new DOMDocument(); |
| 300 |
$html->loadHTML($c); |
$html->loadHTML($c); |
| 301 |
$images = $html->getElementsByTagName('image'); |
$images = $html->getElementsByTagName('image'); |
| 302 |
foreach($images as $im) { |
foreach ($images as $im) { |
| 303 |
$urls[] = $im->getAttribute("src"); |
$urls[] = $im->getAttribute("src"); |
| 304 |
} |
} |
| 305 |
restore_error_handler(); |
restore_error_handler(); |
| 315 |
|
|
| 316 |
function word2web_manual($html_raw) { |
function word2web_manual($html_raw) { |
| 317 |
$html_raw = _word2web_convert_chr($html_raw); |
$html_raw = _word2web_convert_chr($html_raw); |
| 318 |
$path = drupal_get_path('module', 'word2web'); |
$path = drupal_get_path('module', 'word2web'); |
| 319 |
set_error_handler('_word2web_suppress_errors'); |
set_error_handler('_word2web_suppress_errors'); |
| 320 |
$html = $html_raw; |
$html = $html_raw; |
| 321 |
$html_raw = preg_replace("/<(img)([^>]*)>/mi", |
$html_raw = preg_replace("/<(img)([^>]*)>/mi", "<addr class='image' $2>-</addr>", $html_raw); |
|
"<addr class='image' $2>-</addr>", $html_raw); |
|
|
// Get the charset from the invalid Word HTML/XML |
|
| 322 |
//preg_match('/charset=([\w-]+)/', $html_raw, $matches); |
//preg_match('/charset=([\w-]+)/', $html_raw, $matches); |
|
//print_r($matches); |
|
| 323 |
// This step apparently cleans up the XML a little. |
// This step apparently cleans up the XML a little. |
| 324 |
$html = new DOMDocument(); |
$html = new DOMDocument(); |
| 325 |
$html->loadHTML($html_raw); |
$html->loadHTML($html_raw); |
| 331 |
//$html = iconv($matches[1], "UTF-8//IGNORE", $html); |
//$html = iconv($matches[1], "UTF-8//IGNORE", $html); |
| 332 |
$html = iconv("UTF-8", "UTF-8//IGNORE", $html); |
$html = iconv("UTF-8", "UTF-8//IGNORE", $html); |
| 333 |
// Actually run the xsl transformations |
// Actually run the xsl transformations |
| 334 |
$html = _word2web_xslt_transform($html, $path .'/empty.xsl'); |
$html = _word2web_xslt_transform($html, $path .'/empty.xsl'); |
| 335 |
|
|
| 336 |
$html = _word2web_xslt_transform($html, $path .'/w2html.xslt'); |
$html = _word2web_xslt_transform($html, $path .'/w2html.xslt'); |
| 337 |
restore_error_handler(); |
restore_error_handler(); |
| 338 |
$html = str_replace('/>', '', $html); |
$html = str_replace('/>', '', $html); |
| 339 |
return $html; |
return $html; |
| 340 |
} |
} |
| 341 |
|
|
| 342 |
/** |
/** |
| 377 |
|
|
| 378 |
|
|
| 379 |
|
|
| 380 |
if(module_exists('imagefield') && true) { |
if (module_exists('imagefield') && true) { |
| 381 |
if(isset($nodetypes['image'])) { |
if (isset($nodetypes['image'])) { |
| 382 |
$imagetype = content_types("image"); |
$imagetype = content_types("image"); |
| 383 |
if(isset($imagetype['fields']['field_imagefile'])) { |
if (isset($imagetype['fields']['field_imagefile'])) { |
| 384 |
$form['word2web_images'] = array( |
$form['word2web_images'] = array( |
| 385 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 386 |
'#default_value' => variable_get('word2web_images', 0), |
'#default_value' => variable_get('word2web_images', 0), |