toolbox.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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($row, $query = []) {
  36. global $CFG_GLPI;
  37. $url = Plugin::getPhpDir("singlesignon", false) . '/front/callback.php';
  38. $url .= "/provider/".$row;
  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. public static 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 PluginSinglesignonToolbox::getBaseURL() . Plugin::getPhpDir("singlesignon", false) . '/front/picture.send.php?path=' . $path;
  90. }
  91. public static function savePicture($src, $uniq_prefix = "") {
  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 the base directory does not exists, create it
  109. if (!is_dir($basePath) && !mkdir($basePath)) {
  110. return false;
  111. }
  112. // If the sub directory does not exists, create the sub directory
  113. if (!is_dir($basePath . '/' . $subdirectory) && !mkdir($basePath . '/' . $subdirectory)) {
  114. return false;
  115. }
  116. if (!rename($src, $dest)) {
  117. return false;
  118. }
  119. return substr($dest, strlen($basePath . '/')); // Return dest relative to GLPI_PICTURE_DIR
  120. }
  121. public static function deletePicture($path) {
  122. $basePath = GLPI_PLUGIN_DOC_DIR . "/singlesignon";
  123. $fullpath = $basePath . '/' . $path;
  124. if (!file_exists($fullpath)) {
  125. return false;
  126. }
  127. $fullpath = realpath($fullpath);
  128. if (!static::startsWith($fullpath, realpath($basePath))) {
  129. return false;
  130. }
  131. return @unlink($fullpath);
  132. }
  133. public static function renderButton($url, $data, $class = 'oauth-login') {
  134. $popupClass = "";
  135. if (isset($data['popup']) && $data['popup'] == 1) {
  136. $popupClass = "popup";
  137. }
  138. $btn = '<span><a href="' . $url . '" class="singlesignon vsubmit ' . $class . ' ' . $popupClass . '"';
  139. $style = '';
  140. if ((isset($data['bgcolor']) && $data['bgcolor'])) {
  141. $style .= 'background-color: ' . $data['bgcolor'] . ';';
  142. }
  143. if ((isset($data['color']) && $data['color'])) {
  144. $style .= 'color: ' . $data['color'] . ';';
  145. }
  146. if ($style) {
  147. $btn .= ' style="' . $style . '"';
  148. }
  149. $btn .= '>';
  150. if (isset($data['picture']) && $data['picture']) {
  151. $btn .= Html::image(
  152. static::getPictureUrl($data['picture']),
  153. [
  154. 'style' => 'max-height: 20px;margin-right: 4px',
  155. ]
  156. );
  157. $btn .= ' ';
  158. }
  159. $btn .= sprintf(__sso('Login with %s'), $data['name']);
  160. $btn .= '</a></span>';
  161. return $btn;
  162. }
  163. /**
  164. * Get base URL without query string
  165. * @return string
  166. */
  167. public static function getBaseURL() {
  168. global $CFG_GLPI;
  169. if (!empty($CFG_GLPI['url_base'])) {
  170. return $CFG_GLPI['url_base'];
  171. }
  172. $baseURL = "";
  173. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  174. $baseURL = ($_SERVER["HTTP_X_FORWARDED_PROTO"] == "https") ? "https://" : "http://";
  175. } else if (isset($_SERVER["HTTPS"])) {
  176. $baseURL = ($_SERVER["HTTPS"] == "on") ? "https://" : "http://";
  177. } else {
  178. $baseURL = "http://";
  179. }
  180. if (isset($_SERVER["HTTP_X_FORWARDED_HOST"])) {
  181. $baseURL .= $_SERVER["HTTP_X_FORWARDED_HOST"];
  182. } else if (isset($_SERVER["HTTP_X_FORWARDED_HOST"])) {
  183. $baseURL .= $_SERVER["HTTP_X_FORWARDED_HOST"];
  184. } else {
  185. $baseURL .= $_SERVER["SERVER_NAME"];
  186. }
  187. $port = $_SERVER["SERVER_PORT"];
  188. if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
  189. $port = $_SERVER["HTTP_X_FORWARDED_PORT"];
  190. }
  191. if ($port != "80" && $port != "443") {
  192. $baseURL .= ":" . $_SERVER["SERVER_PORT"];
  193. }
  194. return $baseURL;
  195. }
  196. /**
  197. * Get current URL without query string
  198. * @return string
  199. */
  200. public static function getCurrentURL() {
  201. $currentURL = PluginSinglesignonToolbox::getBaseURL();
  202. // $currentURL .= $_SERVER["REQUEST_URI"];
  203. // Ignore Query String
  204. if (isset($_SERVER["SCRIPT_NAME"])) {
  205. $currentURL .= $_SERVER["SCRIPT_NAME"];
  206. }
  207. if (isset($_SERVER["PATH_INFO"])) {
  208. $currentURL .= $_SERVER["PATH_INFO"];
  209. }
  210. return $currentURL;
  211. }
  212. }