toolbox.class.php 4.2 KB

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