toolbox.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * ---------------------------------------------------------------------
  4. * SingleSignOn is a plugin which allows to use SSO for auth
  5. * ---------------------------------------------------------------------
  6. * Copyright (C) 2022 Edgard
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. * ---------------------------------------------------------------------
  21. * @copyright Copyright © 2021 - 2022 Edgard
  22. * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
  23. * @link https://github.com/edgardmessias/glpi-singlesignon/
  24. * ---------------------------------------------------------------------
  25. */
  26. class PluginSinglesignonToolbox {
  27. /**
  28. * Generate a URL to callback
  29. * Some providers don't accept query string, it convert to PATH
  30. * @global array $CFG_GLPI
  31. * @param integer $id
  32. * @param array $query
  33. * @return string
  34. */
  35. public static function getCallbackUrl($id, $query = []) {
  36. global $CFG_GLPI;
  37. $url = $CFG_GLPI['root_doc'] . '/plugins/singlesignon/front/callback.php';
  38. $url .= "/provider/".$id;
  39. if (!empty($query)) {
  40. $_SESSION['redirect'] = $query['redirect'];
  41. }
  42. return $url;
  43. }
  44. public static function isDefault($row, $query = []) {
  45. if ($row['is_default'] == 1) {
  46. return true;
  47. }
  48. return false;
  49. }
  50. public static function getCallbackParameters($name = null) {
  51. $data = [];
  52. if (isset($_SERVER['PATH_INFO'])) {
  53. $path_info = trim($_SERVER['PATH_INFO'], '/');
  54. $parts = explode('/', $path_info);
  55. $key = null;
  56. foreach ($parts as $part) {
  57. if ($key === null) {
  58. $key = $part;
  59. } else {
  60. if ($key === "provider" || $key === "test") {
  61. $part = intval($part);
  62. } else {
  63. $tmp = base64_decode($part);
  64. parse_str($tmp, $part);
  65. }
  66. if ($key === $name) {
  67. return $part;
  68. }
  69. $data[$key] = $part;
  70. $key = null;
  71. }
  72. }
  73. }
  74. if (!isset($data[$name])) {
  75. return null;
  76. }
  77. return $data;
  78. }
  79. static public function startsWith($haystack, $needle) {
  80. $length = strlen($needle);
  81. return (substr($haystack, 0, $length) === $needle);
  82. }
  83. static function getPictureUrl($path) {
  84. global $CFG_GLPI;
  85. $path = Html::cleanInputText($path); // prevent xss
  86. if (empty($path)) {
  87. return null;
  88. }
  89. return $CFG_GLPI['root_doc'] . '/plugins/singlesignon/front/picture.send.php?path=' . $path;
  90. }
  91. static public function savePicture($src, $uniq_prefix = null) {
  92. if (function_exists('Document::isImage') && !Document::isImage($src)) {
  93. return false;
  94. }
  95. $filename = uniqid($uniq_prefix);
  96. $ext = pathinfo($src, PATHINFO_EXTENSION);
  97. $subdirectory = substr($filename, -2); // subdirectory based on last 2 hex digit
  98. $basePath = GLPI_PLUGIN_DOC_DIR . "/singlesignon";
  99. $i = 0;
  100. do {
  101. // Iterate on possible suffix while dest exists.
  102. // This case will almost never exists as dest is based on an unique id.
  103. $dest = $basePath
  104. . '/' . $subdirectory
  105. . '/' . $filename . ($i > 0 ? '_' . $i : '') . '.' . $ext;
  106. $i++;
  107. } while (file_exists($dest));
  108. if (!is_dir($basePath . '/' . $subdirectory) && !mkdir($basePath . '/' . $subdirectory)) {
  109. return false;
  110. }
  111. if (!rename($src, $dest)) {
  112. return false;
  113. }
  114. return substr($dest, strlen($basePath . '/')); // Return dest relative to GLPI_PICTURE_DIR
  115. }
  116. public static function deletePicture($path) {
  117. $basePath = GLPI_PLUGIN_DOC_DIR . "/singlesignon";
  118. $fullpath = $basePath . '/' . $path;
  119. if (!file_exists($fullpath)) {
  120. return false;
  121. }
  122. $fullpath = realpath($fullpath);
  123. if (!static::startsWith($fullpath, realpath($basePath))) {
  124. return false;
  125. }
  126. return @unlink($fullpath);
  127. }
  128. public static function renderButton($url, $data, $class = 'oauth-login') {
  129. $popupClass = "";
  130. if (isset($data['popup']) && $data['popup'] == 1) {
  131. $popupClass = "popup";
  132. }
  133. $btn = '<span><a href="' . $url . '" class="singlesignon vsubmit ' . $class . ' ' . $popupClass . '"';
  134. $style = '';
  135. if ((isset($data['bgcolor']) && $data['bgcolor'])) {
  136. $style .= 'background-color: ' . $data['bgcolor'] . ';';
  137. }
  138. if ((isset($data['color']) && $data['color'])) {
  139. $style .= 'color: ' . $data['color'] . ';';
  140. }
  141. if ($style) {
  142. $btn .= ' style="' . $style . '"';
  143. }
  144. $btn .= '>';
  145. if (isset($data['picture']) && $data['picture']) {
  146. $btn .= Html::image(
  147. static::getPictureUrl($data['picture']),
  148. [
  149. 'style' => 'max-height: 20px;margin-right: 4px',
  150. ]
  151. );
  152. $btn .= ' ';
  153. }
  154. $btn .= sprintf(__sso('Login with %s'), $data['name']);
  155. $btn .= '</a></span>';
  156. return $btn;
  157. }
  158. }