toolbox.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. class PluginSinglesignonToolbox {
  3. /**
  4. * Generate a URL to callback
  5. * Some providers don't accept query string, it convert to PATH
  6. * @global array $CFG_GLPI
  7. * @param integer $id
  8. * @param array $query
  9. * @return string
  10. */
  11. public static function getCallbackUrl($row, $query = []) {
  12. global $CFG_GLPI;
  13. $url = $CFG_GLPI['root_doc'] . '/plugins/singlesignon/front/callback.php';
  14. $url .= "/provider/".$row['id'];
  15. if (!empty($query) && $row['type'] != 'google') {
  16. $url .= "/q/" . base64_encode(http_build_query($query));
  17. } else if (!empty($query) && $row['type'] == 'google') {
  18. $_SESSION['redirect'] = $query['redirect'];
  19. }
  20. return $url;
  21. }
  22. public static function getCallbackParameters($name = null) {
  23. $data = [];
  24. if (isset($_SERVER['PATH_INFO'])) {
  25. $path_info = trim($_SERVER['PATH_INFO'], '/');
  26. $parts = explode('/', $path_info);
  27. $key = null;
  28. foreach ($parts as $part) {
  29. if ($key === null) {
  30. $key = $part;
  31. } else {
  32. if ($key === "provider" || $key === "test") {
  33. $part = intval($part);
  34. } else {
  35. $tmp = base64_decode($part);
  36. parse_str($tmp, $part);
  37. }
  38. if ($key === $name) {
  39. return $part;
  40. }
  41. $data[$key] = $part;
  42. $key = null;
  43. }
  44. }
  45. }
  46. if (!isset($data[$name])) {
  47. return null;
  48. }
  49. return $data;
  50. }
  51. static public function startsWith($haystack, $needle) {
  52. $length = strlen($needle);
  53. return (substr($haystack, 0, $length) === $needle);
  54. }
  55. static function getPictureUrl($path) {
  56. global $CFG_GLPI;
  57. $path = Html::cleanInputText($path); // prevent xss
  58. if (empty($path)) {
  59. return null;
  60. }
  61. return $CFG_GLPI['root_doc'] . '/plugins/singlesignon/front/picture.send.php?path=' . $path;
  62. }
  63. static public function savePicture($src, $uniq_prefix = null) {
  64. if (function_exists('Document::isImage') && !Document::isImage($src)) {
  65. return false;
  66. }
  67. $filename = uniqid($uniq_prefix);
  68. $ext = pathinfo($src, PATHINFO_EXTENSION);
  69. $subdirectory = substr($filename, -2); // subdirectory based on last 2 hex digit
  70. $basePath = GLPI_PLUGIN_DOC_DIR . "/singlesignon";
  71. $i = 0;
  72. do {
  73. // Iterate on possible suffix while dest exists.
  74. // This case will almost never exists as dest is based on an unique id.
  75. $dest = $basePath
  76. . '/' . $subdirectory
  77. . '/' . $filename . ($i > 0 ? '_' . $i : '') . '.' . $ext;
  78. $i++;
  79. } while (file_exists($dest));
  80. if (!is_dir($basePath . '/' . $subdirectory) && !mkdir($basePath . '/' . $subdirectory)) {
  81. return false;
  82. }
  83. if (!rename($src, $dest)) {
  84. return false;
  85. }
  86. return substr($dest, strlen($basePath . '/')); // Return dest relative to GLPI_PICTURE_DIR
  87. }
  88. public static function deletePicture($path) {
  89. $basePath = GLPI_PLUGIN_DOC_DIR . "/singlesignon";
  90. $fullpath = $basePath . '/' . $path;
  91. if (!file_exists($fullpath)) {
  92. return false;
  93. }
  94. $fullpath = realpath($fullpath);
  95. if (!static::startsWith($fullpath, realpath($basePath))) {
  96. return false;
  97. }
  98. return @unlink($fullpath);
  99. }
  100. public static function renderButton($url, $data, $class = 'oauth-login') {
  101. $btn = '<span><a href="' . $url . '" class="singlesignon vsubmit ' . $class . '"';
  102. $style = '';
  103. if ((isset($data['bgcolor']) && $data['bgcolor'])) {
  104. $style .= 'background-color: ' . $data['bgcolor'] . ';';
  105. }
  106. if ((isset($data['color']) && $data['color'])) {
  107. $style .= 'color: ' . $data['color'] . ';';
  108. }
  109. if ($style) {
  110. $btn .= ' style="' . $style . '"';
  111. }
  112. $btn .= '>';
  113. if (isset($data['picture']) && $data['picture']) {
  114. $btn .= Html::image(
  115. static::getPictureUrl($data['picture']),
  116. [
  117. 'style' => 'max-height: 20px;',
  118. ]
  119. );
  120. $btn .= ' ';
  121. }
  122. $btn .= sprintf(__sso('Login with %s'), $data['name']);
  123. $btn .= '</a></span>';
  124. return $btn;
  125. }
  126. }