proxy.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script needed in order to avoid mixed content in links inside a learning path
  5. * In order to use this file you have to:
  6. *
  7. * 1. Modify configuration.php and add this setting: $_configuration['lp_fix_embed_content'] = true;
  8. * 2. Copy this file in app/courses/proxy.php
  9. * 3. Change your .htaccess in order to let the proxy.php to be read inside app/courses
  10. *
  11. */
  12. require_once '../config/configuration.php';
  13. /**
  14. * Returns "%" or "px"
  15. *
  16. * 800px => function returns "px"
  17. * 800% => function returns %
  18. *
  19. * @param string $value
  20. * @return string
  21. */
  22. function addPixelOrPercentage($value)
  23. {
  24. $addPixel = strpos($value, 'px');
  25. $addPixel = !($addPixel === false);
  26. $addCharacter = '';
  27. if ($addPixel == false) {
  28. $addPercentage = strpos($value, '%');
  29. $addPercentage = !($addPercentage === false);
  30. if ($addPercentage) {
  31. $addCharacter = '%';
  32. }
  33. } else {
  34. $addCharacter = 'px';
  35. }
  36. return $addCharacter;
  37. }
  38. function get_http_response_code($theURL)
  39. {
  40. $headers = get_headers($theURL);
  41. return substr($headers[0], 9, 3);
  42. }
  43. $height = isset($_GET['height']) ? (int) $_GET['height'].addPixelOrPercentage($_GET['height']) : '';
  44. $width = isset($_GET['width']) ? (int) $_GET['width'].addPixelOrPercentage($_GET['width']) : '';
  45. $vars = isset($_GET['flashvars']) ? htmlentities($_GET['flashvars']) : '';
  46. $src = isset($_GET['src']) ? htmlentities($_GET['src']) : '';
  47. $id = isset($_GET['id']) ? htmlentities($_GET['id']) : '';
  48. $type = isset($_GET['type']) ? $_GET['type'] : 'flash';
  49. // Fixes URL like: https://www.vopspsy.ugent.be/pdfs/download.php?own=mvsteenk&file=caleidoscoop.pdf
  50. if (strpos($src, 'download.php') !== false) {
  51. $src = str_replace('download.php', 'download.php?', $src);
  52. $src .= isset($_GET['own']) ? '&own='.htmlentities($_GET['own']) : '';
  53. $src .= isset($_GET['file']) ? '&file='.htmlentities($_GET['file']) : '';
  54. }
  55. $result = get_http_response_code($src);
  56. $urlToTest = parse_url($src, PHP_URL_HOST);
  57. $g = stream_context_create (array('ssl' => array('capture_peer_cert' => true)));
  58. $r = @stream_socket_client("ssl://$urlToTest:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $g);
  59. $cont = stream_context_get_params($r);
  60. $convertToSecure = false;
  61. $certinfo = openssl_x509_parse($cont['options']['ssl']['peer_certificate']);
  62. if (isset($certinfo) && isset($certinfo['subject']) && isset($certinfo['subject']['CN'])) {
  63. $certUrl = $certinfo['subject']['CN'];
  64. $parsed = parse_url($certUrl);
  65. // Remove www from URL
  66. $parsedUrl = preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $certUrl);
  67. if ($urlToTest == $certUrl || $parsedUrl == $urlToTest) {
  68. $convertToSecure = true;
  69. }
  70. if ($urlToTest != $certUrl) {
  71. // url and cert url are different this will show a warning in browsers
  72. // use normal "http" version
  73. $result = false;
  74. }
  75. }
  76. if ($result == false) {
  77. $src = str_replace('https', 'http', $src);
  78. }
  79. if ($convertToSecure) {
  80. $src = str_replace('http', 'https', $src);
  81. }
  82. $result = '';
  83. switch ($type) {
  84. case 'link':
  85. // Check if links comes from a course
  86. $srcParts = explode('/', $src);
  87. $srcParts = array_filter($srcParts);
  88. $srcParts = array_values($srcParts);
  89. if (isset($srcParts[0], $srcParts[2]) && $srcParts[0] === 'courses' && $srcParts[2] === 'document') {
  90. $src = $_configuration['root_web'].$src;
  91. }
  92. if (strpos($src, 'http') === false) {
  93. $src = "http://$src";
  94. }
  95. header('Location: '.$src);
  96. exit;
  97. break;
  98. case 'iframe':
  99. $result = '<iframe src="'.$src.'" width="'.$width.'" height="'.$height.'" ></iframe>';
  100. break;
  101. case 'flash':
  102. $result = '
  103. <object
  104. id="'.$id.'" width="'.$width.'" height="'.$height.'" align="center"
  105. codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
  106. <param name="id" value="'.$id.'">
  107. <param name="width" value="'.$width.'">
  108. <param name="height" value="'.$height.'">
  109. <param name="bgcolor" value="#ffffff">
  110. <param name="align" value="center">
  111. <param name="allowfullscreen" value="true">
  112. <param name="allowscriptaccess" value="always">
  113. <param name="quality" value="high">
  114. <param name="wmode" value="transparent">
  115. <param name="flashvars" value="'.$vars.'">
  116. <param name="src" value="'.$src.'">
  117. <embed
  118. id="'.$id.'" width="'.$width.'" height="'.$height.'" bgcolor="#ffffff" align="center"
  119. allowfullscreen="true" allowscriptaccess="always" quality="high" wmode="transparent"
  120. flashvars="'.$vars.'" src="'.$src.'"
  121. type="application/x-shockwave-flash"
  122. >
  123. </object>';
  124. }
  125. echo $result;