| 1 |
<?php |
<?php |
| 2 |
// $Id: drutex_remote.inc,v 1.3 2007/09/09 18:57:38 darthsteven Exp $ |
// $Id: drutex_remote.inc,v 1.4 2008/01/09 12:33:53 darthsteven Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 13 |
return (object) array( |
return (object) array( |
| 14 |
'title' => t('Remote LaTeX Renderer'), |
'title' => t('Remote LaTeX Renderer'), |
| 15 |
'description' => t('Provides different environments to create rendered (remotely) images (especially maths).'), |
'description' => t('Provides different environments to create rendered (remotely) images (especially maths).'), |
| 16 |
'toggle' => true, |
'toggle' => TRUE, |
| 17 |
'weight' => 2 |
'weight' => 2 |
| 18 |
); |
); |
| 19 |
} |
} |
| 22 |
* Implementation of subhook_defaults(). |
* Implementation of subhook_defaults(). |
| 23 |
*/ |
*/ |
| 24 |
function drutex_remote_defaults() { |
function drutex_remote_defaults() { |
| 25 |
$D['drutex_remote_active'] = false; |
$D['drutex_remote_active'] = FALSE; |
| 26 |
return $D; |
return $D; |
| 27 |
} |
} |
| 28 |
|
|
| 124 |
$text = _drutex_unescape($text); |
$text = _drutex_unescape($text); |
| 125 |
|
|
| 126 |
if (drutex_submodule_is_active('security', $format)) { |
if (drutex_submodule_is_active('security', $format)) { |
| 127 |
if (drutex_security($text, $format) == false) { |
if (drutex_security($text, $format) == FALSE) { |
| 128 |
return '<em class="error">Unallowed command detected!</em>'; |
return '<em class="error">Unallowed command detected!</em>'; |
| 129 |
} |
} |
| 130 |
} |
} |
| 162 |
drutex_var_get("drutex_server_remote_$format"), |
drutex_var_get("drutex_server_remote_$format"), |
| 163 |
array('Content-Type' => 'application/x-www-form-urlencoded'), |
array('Content-Type' => 'application/x-www-form-urlencoded'), |
| 164 |
'POST', |
'POST', |
| 165 |
http_build_query($data,'','&')); |
http_build_query($data, '', '&')); |
| 166 |
if ($response->code == 200) { |
if ($response->code == 200) { |
| 167 |
// We get the image back from the server. |
// We get the image back from the server. |
| 168 |
$errorcode = file_save_data($response->data, $image_file, FILE_EXISTS_REPLACE); |
$errorcode = file_save_data($response->data, $image_file, FILE_EXISTS_REPLACE); |
| 169 |
} |
} |
| 170 |
elseif ($response->code == 403) { |
elseif ($response->code == 403) { |
| 171 |
watchdog('DruTeX', 'The remote server denied the request to render LaTeX', WATCHDOG_WARNING); |
watchdog('DruTeX', 'The remote server denied the request to render LaTeX.', WATCHDOG_WARNING); |
| 172 |
} |
} |
| 173 |
elseif ($response->code == 404) { |
elseif ($response->code == 404) { |
| 174 |
watchdog('DruTeX', 'The remote server doesn\'t appear to have the renderer installed.', WATCHDOG_WARNING); |
watchdog('DruTeX', 'The remote server doesn\'t appear to have the renderer installed.', WATCHDOG_WARNING); |
| 175 |
} |
} |
| 176 |
else { |
else { |
| 177 |
watchdog('DruTeX', 'Unknown response ('. $response->code .') from the remote server when rendering LaTeX', WATCHDOG_WARNING); |
watchdog('DruTeX', 'Unknown response (%code) from the remote server when rendering LaTeX.', array('%code' => $response->code), WATCHDOG_WARNING); |
| 178 |
} |
} |
| 179 |
} |
} |
| 180 |
$success = is_file($image_file); |
$success = is_file($image_file); |
| 181 |
if (drutex_var_get("drutex_debug")) { |
if (drutex_var_get("drutex_debug")) { |
| 182 |
if ($success) { |
if ($success) { |
| 183 |
watchdog('DruTeX', "$image_file was created remotely and saved locally.", WATCHDOG_NOTICE); |
watchdog('DruTeX', '%imgfile was created remotely and saved locally.', array('%imgfile' => $image_file), WATCHDOG_NOTICE); |
| 184 |
} |
} |
| 185 |
else { |
else { |
| 186 |
watchdog('DruTeX', "$image_file couldn\'t be created remotely.", WATCHDOG_WARNING); |
watchdog('DruTeX', "%imgfile couldn\'t be created remotely.", array('%imgfile' => $image_file), WATCHDOG_WARNING); |
| 187 |
} |
} |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
$img_url = drutex_get_image_url($format) . "/$hash.$image_type"; |
$img_url = drutex_get_image_url($format) ."/$hash.$image_type"; |
| 191 |
|
|
| 192 |
if ($success) { |
if ($success) { |
| 193 |
$img_alt = _drutex_hide('set', check_plain($text)); |
$img_alt = _drutex_hide('set', check_plain($text)); |
| 224 |
} |
} |
| 225 |
|
|
| 226 |
if(!function_exists('http_build_query')) { |
if(!function_exists('http_build_query')) { |
| 227 |
function http_build_query($data,$prefix=null,$sep='',$key='') { |
function http_build_query($data, $prefix=NULL, $sep='', $key='') { |
| 228 |
$ret = array(); |
$ret = array(); |
| 229 |
foreach($data as $k => $v) { |
foreach ($data as $k => $v) { |
| 230 |
$k = urlencode($k); |
$k = urlencode($k); |
| 231 |
if(is_int($k) && $prefix != null) { |
if (is_int($k) && $prefix != NULL) { |
| 232 |
$k = $prefix.$k; |
$k = $prefix . $k; |
| 233 |
}; |
}; |
| 234 |
if(!empty($key)) { |
if (!empty($key)) { |
| 235 |
$k = $key."[".$k."]"; |
$k = $key ."[". $k ."]"; |
| 236 |
}; |
}; |
| 237 |
if(is_array($v) || is_object($v)) { |
if (is_array($v) || is_object($v)) { |
| 238 |
array_push($ret,http_build_query($v,"",$sep,$k)); |
array_push($ret, http_build_query($v, "", $sep, $k)); |
| 239 |
} |
} |
| 240 |
else { |
else { |
| 241 |
array_push($ret,$k."=".urlencode($v)); |
array_push($ret, $k ."=". urlencode($v)); |
| 242 |
}; |
}; |
| 243 |
}; |
}; |
| 244 |
if(empty($sep)) { |
if (empty($sep)) { |
| 245 |
$sep = ini_get("arg_separator.output"); |
$sep = ini_get("arg_separator.output"); |
| 246 |
}; |
}; |
| 247 |
return implode($sep, $ret); |
return implode($sep, $ret); |
| 248 |
}; |
}; |
| 249 |
}; |
}; |
| 250 |
|
|
| 251 |
function drutex_remote_do_render() { |
function drutex_remote_do_render() { |
| 252 |
|
|
| 253 |
if (variable_get('drutex_remote_rendering_enabled',0) == 1) { |
if (variable_get('drutex_remote_rendering_enabled', 0) == 1) { |
| 254 |
// Check the incoming ip: |
// Check the incoming ip: |
| 255 |
//Wilcard character is an asterix, * |
//Wilcard character is an asterix, * |
| 256 |
$ip_allow = explode("\n",variable_get('drutex_remote_allowed_ips','')); |
$ip_allow = explode("\n", variable_get('drutex_remote_allowed_ips', '')); |
| 257 |
|
|
| 258 |
function ip_first($ips) { |
function ip_first($ips) { |
| 259 |
if (($pos = strpos($ips, ',')) != false) { |
if (($pos = strpos($ips, ',')) != FALSE) { |
| 260 |
return substr($ips, 0, $pos); |
return substr($ips, 0, $pos); |
| 261 |
} |
} |
| 262 |
else { |
else { |
| 263 |
return $ips; |
return $ips; |
| 264 |
} |
} |
| 265 |
} |
} |
| 266 |
|
|
| 267 |
function ip_valid($ips) { |
function ip_valid($ips) { |
| 268 |
if (isset($ips)) { |
if (isset($ips)) { |
| 269 |
$ip = ip_first($ips); |
$ip = ip_first($ips); |
| 270 |
$ipnum = ip2long($ip); |
$ipnum = ip2long($ip); |
| 271 |
if ($ipnum !== -1 && $ipnum !== false && (long2ip($ipnum) === $ip)) { // PHP 4 and PHP 5 |
if ($ipnum !== -1 && $ipnum !== FALSE && (long2ip($ipnum) === $ip)) { // PHP 4 and PHP 5 |
| 272 |
if (($ipnum < 167772160 || $ipnum > 184549375) && // Not in 10.0.0.0/8 |
if (($ipnum < 167772160 || $ipnum > 184549375) && // Not in 10.0.0.0/8 |
| 273 |
($ipnum < -1408237568 || $ipnum > -1407188993) && // Not in 172.16.0.0/12 |
($ipnum < -1408237568 || $ipnum > -1407188993) && // Not in 172.16.0.0/12 |
| 274 |
($ipnum < -1062731776 || $ipnum > -1062666241)) { // Not in 192.168.0.0/16 |
($ipnum < -1062731776 || $ipnum > -1062666241)) { // Not in 192.168.0.0/16 |
| 275 |
return true; |
return TRUE; |
| 276 |
} |
} |
| 277 |
} |
} |
| 278 |
} |
} |
| 279 |
return false; |
return FALSE; |
| 280 |
} |
} |
| 281 |
|
|
| 282 |
function ip() { |
function ip() { |
| 283 |
$check = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', |
$check = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'HTTP_VIA', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM'); |
|
'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', |
|
|
'HTTP_VIA', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM' |
|
|
); |
|
| 284 |
foreach ($check as $c) { |
foreach ($check as $c) { |
| 285 |
if (ip_valid($_SERVER[$c])) { |
if (ip_valid($_SERVER[$c])) { |
| 286 |
return ip_first($_SERVER[$c]); |
return ip_first($_SERVER[$c]); |
| 287 |
} |
} |
| 288 |
} |
} |
| 289 |
return $_SERVER['REMOTE_ADDR']; |
return ip_server(); |
| 290 |
} |
} |
| 291 |
|
|
| 292 |
$ip = ip(); |
$ip = ip(); |
| 293 |
|
|
| 294 |
$allowed_to_run = FALSE; |
$allowed_to_run = FALSE; |
| 295 |
|
|
| 296 |
if(in_array($ip, $ip_allow)) { |
if (in_array($ip, $ip_allow)) { |
| 297 |
$allowed_to_run = TRUE; |
$allowed_to_run = TRUE; |
| 298 |
} |
} |
| 299 |
else { |
else { |
| 300 |
foreach($ip_allow as $allow) { |
foreach ($ip_allow as $allow) { |
| 301 |
if (trim($allow) != '') { |
if (trim($allow) != '') { |
| 302 |
if(eregi($allow, $ip)) { |
if (eregi($allow, $ip)) { |
| 303 |
$allowed_to_run = TRUE; |
$allowed_to_run = TRUE; |
| 304 |
} |
} |
| 305 |
} |
} |
| 306 |
} |
} |
| 307 |
} |
} |
| 311 |
// Get the variable and do the render: |
// Get the variable and do the render: |
| 312 |
$text = $_REQUEST['text']; |
$text = $_REQUEST['text']; |
| 313 |
$dpi = $_REQUEST['dpi']; |
$dpi = $_REQUEST['dpi']; |
| 314 |
$template_dir = drutex_var_get('drutex_dir') . '/templates/render'; |
$template_dir = drutex_var_get('drutex_dir') .'/templates/render'; |
| 315 |
$template_file = variable_get('drutex_remote_render_template', 'default.tex'); |
$template_file = variable_get('drutex_remote_render_template', 'default.tex'); |
| 316 |
$template = $template_dir . '/' . $template_file; |
$template = $template_dir .'/'. $template_file; |
| 317 |
$hash = sha1('latex_image'. $text . $dpi); |
$hash = sha1('latex_image'. $text . $dpi); |
| 318 |
$temporary_dir = _drutex_create_temporary_dir(); |
$temporary_dir = _drutex_create_temporary_dir(); |
| 319 |
$image_file = $temporary_dir .'/'. $hash .'.png'; |
$image_file = $temporary_dir .'/'. $hash .'.png'; |
| 374 |
} |
} |
| 375 |
else { |
else { |
| 376 |
if ($success) { |
if ($success) { |
| 377 |
watchdog('DruTeX', "$image_file was created. Temporary directory was $temporary_dir.", WATCHDOG_NOTICE); |
watchdog('DruTeX', '%imgfile was created. Temporary directory was %tmp.', array('%imgfile' => $image_file, '%tmp' => $temporary_dir), WATCHDOG_NOTICE); |
| 378 |
} |
} |
| 379 |
else { |
else { |
| 380 |
watchdog('DruTeX', "$image_file couldn't be created. Temporary directory was $temporary_dir.", WATCHDOG_WARNING); |
watchdog('DruTeX', '%imgfile couldn\'t be created. Temporary directory was %tmp.', array('%imgfile' => $image_file, '%tmp' => $temporary_dir), WATCHDOG_WARNING); |
| 381 |
} |
} |
| 382 |
} |
} |
| 383 |
|
|
| 397 |
|
|
| 398 |
} |
} |
| 399 |
|
|
| 400 |
function drutex_remote_settings() { |
/** |
| 401 |
|
* DruTex Admin Remote Settings page. |
| 402 |
|
*/ |
| 403 |
|
|
| 404 |
|
function drutex_remote_admin_settings() { |
| 405 |
/* conversion methods */ |
/* conversion methods */ |
| 406 |
$conversions = array('dvipng' => 'dvipng', |
$conversions = array('dvipng' => 'dvipng', |
| 407 |
'imagemagick' => 'ImageMagick (convert)', |
'imagemagick' => 'ImageMagick (convert)', |
| 408 |
'custom' => 'custom'); |
'custom' => 'custom'); |
|
$form = array(); |
|
| 409 |
|
|
| 410 |
$form['remote_fieldset'] = array( |
$form['remote_fieldset'] = array( |
| 411 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 412 |
'#title' => t('Remote rendering settings'), |
'#title' => t('Remote rendering settings'), |
| 413 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 414 |
); |
); |
| 415 |
|
|
| 416 |
$form['remote_fieldset']['drutex_remote_rendering_enabled'] = array( |
$form['remote_fieldset']['drutex_remote_rendering_enabled'] = array( |
| 417 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 418 |
'#title' => t('Allow remote LaTeX rendering on THIS server'), |
'#title' => t('Allow remote LaTeX rendering on THIS server'), |
| 419 |
'#default_value' => variable_get('drutex_remote_rendering_enabled',0), |
'#default_value' => variable_get('drutex_remote_rendering_enabled', 0), |
| 420 |
); |
); |
| 421 |
|
|
| 422 |
$form['remote_fieldset']['drutex_remote_allowed_ips'] = array( |
$form['remote_fieldset']['drutex_remote_allowed_ips'] = array( |
| 423 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 424 |
'#title' => t('Allowed remote IPs'), |
'#title' => t('Allowed remote IPs'), |
| 425 |
'#description' => t('Put the address of each remote IP that is allowed to render LaTeX on this server.'."<br />".'Put each address on a new line.'."<br />".'You may use * as a wildcard character.'."<br />".'e.g. 10.0.0.5, or 192.168.*.*'), |
'#description' => t('Put the address of each remote IP that is allowed to render LaTeX on this server.'."<br />".'Put each address on a new line.'."<br />".'You may use * as a wildcard character.'."<br />".'e.g. 10.0.0.5, or 192.168.*.*'), |
| 426 |
'#default_value' => variable_get('drutex_remote_allowed_ips',''), |
'#default_value' => variable_get('drutex_remote_allowed_ips', ''), |
| 427 |
); |
); |
| 428 |
|
|
| 429 |
|
|
| 463 |
'dvipng -o [IMG_FILE] -D [DPI] -T tight [TMP_DIR]/[HASH].dvi'); |
'dvipng -o [IMG_FILE] -D [DPI] -T tight [TMP_DIR]/[HASH].dvi'); |
| 464 |
|
|
| 465 |
/* description for <custom conversion method> */ |
/* description for <custom conversion method> */ |
| 466 |
$description = t('* Leave blank and select dvipng or ImageMagick, to display a template for one of these commands in this box.') . '<br />' |
$description = t('* Leave blank and select dvipng or ImageMagick, to display a template for one of these commands in this box.') .'<br />'. t('* Every line is executed in sequence, but in its own shell (so path changes aren\'t inherited).') .'<br />'. t('Placeholders available:<br /> |
|
. t('* Every line is executed in sequence, but in its own shell (so path changes aren\'t inherited).') . '<br />' |
|
|
. t('Placeholders available:<br /> |
|
| 467 |
[HASH] - hash (name) for the image<br /> |
[HASH] - hash (name) for the image<br /> |
| 468 |
[TMP_DIR] - temporary dir (automatically cleaned)<br /> |
[TMP_DIR] - temporary dir (automatically cleaned)<br /> |
| 469 |
[DPI] - dots per inch (see above)<br /> |
[DPI] - dots per inch (see above)<br /> |
| 477 |
'#rows' => 3, |
'#rows' => 3, |
| 478 |
'#cols' => 50, |
'#cols' => 50, |
| 479 |
'#default_value' => $default_value, |
'#default_value' => $default_value, |
| 480 |
'#description' => $description |
'#description' => $description, |
| 481 |
); |
); |
| 482 |
|
|
| 483 |
|
|
| 484 |
return system_settings_form($form); |
return system_settings_form($form); |
| 485 |
|
|
| 486 |
} |
} |
| 487 |
|
|
| 488 |
|
|