toolbox.class.php 4.5 KB

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