Guest User

Untitled

a guest
Sep 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. <?php
  2.  
  3. class MoodleValetDriver extends BasicValetDriver
  4. {
  5. protected $isStyleUri = false;
  6. protected $baseUri = '';
  7. protected $moodleStaticScripts = [
  8. 'styles.php',
  9. 'javascript.php',
  10. 'jquery.php',
  11. 'requirejs.php',
  12. 'font.php',
  13. 'image.php',
  14. 'yui_combo.php',
  15. 'pluginfile.php',
  16. 'draftfile.php'
  17. ];
  18. protected $sitePath = '';
  19. protected $siteName = '';
  20. protected $uri = '';
  21. /**
  22. * Determine if the driver serves the request.
  23. *
  24. * @param string $sitePath
  25. * @param string $siteName
  26. * @param string $uri
  27. * @return bool
  28. */
  29. public function serves($sitePath, $siteName, $uri)
  30. {
  31. $this->sitePath = $sitePath;
  32. $this->siteName = $siteName;
  33. $this->uri = $uri;
  34.  
  35. if (
  36. file_exists($sitePath.'/config-dist.php')
  37. && file_exists($sitePath.'/course')
  38. && file_exists($sitePath.'/grade')
  39. ) {
  40. return true;
  41. }
  42. }
  43.  
  44. /**
  45. * Determine if the incoming request is for a static file.
  46. *
  47. * @param string $sitePath
  48. * @param string $siteName
  49. * @param string $uri
  50. * @return string|false
  51. */
  52. public function isStaticFile($sitePath, $siteName, $uri)
  53. {
  54. if (file_exists($staticFilePath = $sitePath.$uri))
  55. {
  56. return $staticFilePath;
  57. }
  58.  
  59.  
  60. return false;
  61. }
  62.  
  63.  
  64. public function mutateUri($uri) {
  65. foreach($this->moodleStaticScripts as $script) {
  66. if(preg_match('/'.$script.'/i', $uri) && !preg_match('/'.$script.'$/i', $uri)) {
  67. $this->isStyleUri = true;
  68. $pos = strpos($uri, $script);
  69. $length = strlen($script);
  70. $this->baseUri = substr($uri, 0, $length + $pos);
  71.  
  72. return substr($uri, $length + $pos);
  73. }
  74. }
  75.  
  76. if(
  77. empty($uri)
  78. || (
  79. !preg_match('/.php/i', $uri)
  80. && !preg_match('/.html/i', $uri)
  81. && !preg_match('/.js$/i', $uri)
  82. && !preg_match('/.css$/i', $uri)
  83. && !preg_match('/.jpe?g/i', $uri)
  84. && !preg_match('/.png/i', $uri)
  85. && !preg_match('/.gif/i', $uri)
  86. && !preg_match('/.svg/i', $uri)
  87. )
  88. ) {
  89. return "{$uri}/index.php";
  90. }
  91.  
  92. return $uri;
  93. }
  94.  
  95. /**
  96. * Get the fully resolved path to the application's front controller.
  97. *
  98. * @param string $sitePath
  99. * @param string $siteName
  100. * @param string $uri
  101. * @return string
  102. */
  103. public function frontControllerPath($sitePath, $siteName, $uri)
  104. {
  105. $_SERVER['SERVER_SOFTWARE'] = 'PHP';
  106. $_SERVER['PHP_SELF'] = $uri;
  107. $_SERVER['SERVER_ADDR'] = '127.0.0.1';
  108.  
  109. if(
  110. (
  111. empty($uri)
  112. || (
  113. !preg_match('/.php/i', $uri)
  114. && !preg_match('/.html/i', $uri)
  115. && !preg_match('/.js$/i', $uri)
  116. )
  117. )
  118. && !$this->isStyleUri
  119. && !$this->isStaticFile($sitePath, $siteName, $uri)
  120. ) {
  121. return $this->asPhpIndexFileInDirectory($sitePath, $uri);
  122. }
  123.  
  124. if($this->isStyleUri) {
  125. $_SERVER['PATH_INFO'] = $uri;
  126. return $sitePath.$this->baseUri;
  127. }
  128.  
  129. return $sitePath.$uri;
  130. }
  131. }
Add Comment
Please, Sign In to add comment