PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/lib/security2.lib.php

https://bitbucket.org/speedealing/speedealing
PHP | 260 lines | 156 code | 35 blank | 69 comment | 32 complexity | ce82d864ea4991bff440b814eee5392a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2011-2012 Herve Prot <herve.prot@symeos.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. /**
  21. * Return user/group account of web server
  22. *
  23. * @param string $mode 'user' or 'group'
  24. * @return string Return user or group of web server
  25. */
  26. function dol_getwebuser($mode) {
  27. $t = '?';
  28. if ($mode == 'user')
  29. $t = getenv('APACHE_RUN_USER'); // $_ENV['APACHE_RUN_USER'] is empty
  30. if ($mode == 'group')
  31. $t = getenv('APACHE_RUN_GROUP');
  32. return $t;
  33. }
  34. /**
  35. * Show Dolibarr default login page.
  36. * Part of this code is also duplicated into main.inc.php::top_htmlhead
  37. *
  38. * @param Translate $langs Lang object (must be initialized by a new).
  39. * @param Conf $conf Conf object
  40. * @param Societe $mysoc Company object
  41. * @return void
  42. */
  43. function dol_loginfunction($langs, $conf, $mysoc) {
  44. global $dolibarr_main_demo;
  45. global $hookmanager;
  46. // Instantiate hooks of thirdparty module only if not already define
  47. $hookmanager->initHooks(array('mainloginpage'));
  48. $langcode = (GETPOST('lang') ? ((is_object($langs) && $langs->defaultlang) ? $langs->defaultlang : 'auto') : GETPOST('lang'));
  49. $langs->setDefaultLang($langcode);
  50. $langs->load("main");
  51. $langs->load("other");
  52. $langs->load("help");
  53. $langs->load("admin");
  54. $main_authentication = $conf->file->main_authentication;
  55. $session_name = session_name();
  56. $dol_url_root = DOL_URL_ROOT;
  57. $php_self = $_SERVER['PHP_SELF'];
  58. $php_self.= $_SERVER["QUERY_STRING"] ? '?' . $_SERVER["QUERY_STRING"] : '';
  59. // Title
  60. $title = 'Speedealing ' . DOL_VERSION;
  61. // Select templates
  62. $template_dir = DOL_DOCUMENT_ROOT . "/core/tpl/";
  63. $conf->css = "/theme/" . (GETPOST('theme') ? GETPOST('theme', 'alpha') : $conf->theme) . "/style.css.php?lang=" . $langs->defaultlang;
  64. $conf_css = DOL_URL_ROOT . $conf->css;
  65. // Set cookie for timeout management
  66. $prefix = dol_getprefix();
  67. $sessiontimeout = 'DOLSESSTIMEOUT_' . $prefix;
  68. if (!empty($conf->global->MAIN_SESSION_TIMEOUT))
  69. setcookie($sessiontimeout, $conf->global->MAIN_SESSION_TIMEOUT, 0, "/", '', 0);
  70. if (GETPOST('urlfrom', 'alpha'))
  71. $_SESSION["urlfrom"] = GETPOST('urlfrom', 'alpha');
  72. else
  73. unset($_SESSION["urlfrom"]);
  74. // Show logo (search in order: small company logo, large company logo, theme logo, common logo)
  75. $width = 0;
  76. // Home message
  77. if (!empty($conf->global->MAIN_HOME)) {
  78. $i = 0;
  79. while (preg_match('/__\(([a-zA-Z]+)\)__/i', $conf->global->MAIN_HOME, $reg) && $i < 100) {
  80. $conf->global->MAIN_HOME = preg_replace('/__\(' . $reg[1] . '\)__/i', $langs->trans($reg[1]), $conf->global->MAIN_HOME);
  81. $i++;
  82. }
  83. }
  84. $main_home = (!empty($conf->global->MAIN_HOME) ? dol_htmlcleanlastbr($conf->global->MAIN_HOME) : '');
  85. // Google AD
  86. $main_google_ad_client = ((!empty($conf->global->MAIN_GOOGLE_AD_CLIENT) && !empty($conf->global->MAIN_GOOGLE_AD_SLOT)) ? 1 : 0);
  87. $dol_loginmesg = (!empty($_SESSION["dol_loginmesg"]) ? $_SESSION["dol_loginmesg"] : '');
  88. $favicon = DOL_URL_ROOT . '/theme/' . $conf->theme . '/img/favicon.ico';
  89. $jquerytheme = 'smoothness';
  90. include $template_dir . 'login.tpl.php'; // To use native PHP
  91. $_SESSION["dol_loginmesg"] = '';
  92. }
  93. /**
  94. * Fonction pour initialiser un salt pour la fonction crypt.
  95. *
  96. * @param int $type 2=>renvoi un salt pour cryptage DES
  97. * 12=>renvoi un salt pour cryptage MD5
  98. * non defini=>renvoi un salt pour cryptage par defaut
  99. * @return string Salt string
  100. */
  101. function makesalt($type = CRYPT_SALT_LENGTH) {
  102. dol_syslog("makesalt type=" . $type);
  103. switch ($type) {
  104. case 12: // 8 + 4
  105. $saltlen = 8;
  106. $saltprefix = '$1$';
  107. $saltsuffix = '$';
  108. break;
  109. case 8: // 8 (Pour compatibilite, ne devrait pas etre utilise)
  110. $saltlen = 8;
  111. $saltprefix = '$1$';
  112. $saltsuffix = '$';
  113. break;
  114. case 2: // 2
  115. default: // by default, fall back on Standard DES (should work everywhere)
  116. $saltlen = 2;
  117. $saltprefix = '';
  118. $saltsuffix = '';
  119. break;
  120. }
  121. $salt = '';
  122. while (dol_strlen($salt) < $saltlen)
  123. $salt.=chr(mt_rand(64, 126));
  124. $result = $saltprefix . $salt . $saltsuffix;
  125. dol_syslog("makesalt return=" . $result);
  126. return $result;
  127. }
  128. /**
  129. * Encode or decode database password in config file
  130. *
  131. * @param int $level Encode level: 0 no encoding, 1 encoding
  132. * @return int <0 if KO, >0 if OK
  133. */
  134. function encodedecode_dbpassconf($level = 0) {
  135. dol_syslog("encodedecode_dbpassconf level=" . $level, LOG_DEBUG);
  136. $config = '';
  137. $passwd = '';
  138. $passwd_crypted = '';
  139. if ($fp = fopen(DOL_DOCUMENT_ROOT . '/conf/conf.php', 'r')) {
  140. while (!feof($fp)) {
  141. $buffer = fgets($fp, 4096);
  142. $lineofpass = 0;
  143. if (preg_match('/^[^#]*dolibarr_main_db_encrypted_pass[\s]*=[\s]*(.*)/i', $buffer, $reg)) { // Old way to save crypted value
  144. $val = trim($reg[1]); // This also remove CR/LF
  145. $val = preg_replace('/^["\']/', '', $val);
  146. $val = preg_replace('/["\'][\s;]*$/', '', $val);
  147. if (!empty($val)) {
  148. $passwd_crypted = $val;
  149. $val = dol_decode($val);
  150. $passwd = $val;
  151. $lineofpass = 1;
  152. }
  153. } elseif (preg_match('/^[^#]*dolibarr_main_db_pass[\s]*=[\s]*(.*)/i', $buffer, $reg)) {
  154. $val = trim($reg[1]); // This also remove CR/LF
  155. $val = preg_replace('/^["\']/', '', $val);
  156. $val = preg_replace('/["\'][\s;]*$/', '', $val);
  157. if (preg_match('/crypted:/i', $buffer)) {
  158. $val = preg_replace('/crypted:/i', '', $val);
  159. $passwd_crypted = $val;
  160. $val = dol_decode($val);
  161. $passwd = $val;
  162. } else {
  163. $passwd = $val;
  164. $val = dol_encode($val);
  165. $passwd_crypted = $val;
  166. }
  167. $lineofpass = 1;
  168. }
  169. // Output line
  170. if ($lineofpass) {
  171. // Add value at end of file
  172. if ($level == 0) {
  173. $config .= '$dolibarr_main_db_pass=\'' . $passwd . '\';' . "\n";
  174. }
  175. if ($level == 1) {
  176. $config .= '$dolibarr_main_db_pass=\'crypted:' . $passwd_crypted . '\';' . "\n";
  177. }
  178. //print 'passwd = '.$passwd.' - passwd_crypted = '.$passwd_crypted;
  179. //exit;
  180. } else {
  181. $config .= $buffer;
  182. }
  183. }
  184. fclose($fp);
  185. // Write new conf file
  186. $file = DOL_DOCUMENT_ROOT . '/conf/conf.php';
  187. if ($fp = @fopen($file, 'w')) {
  188. fputs($fp, $config);
  189. fclose($fp);
  190. // It's config file, so we set read permission for creator only.
  191. // Should set permission to web user and groups for users used by batch
  192. //@chmod($file, octdec('0600'));
  193. return 1;
  194. } else {
  195. dol_syslog("encodedecode_dbpassconf Failed to open conf.php file for writing", LOG_WARNING);
  196. return -1;
  197. }
  198. } else {
  199. dol_syslog("encodedecode_dbpassconf Failed to read conf.php", LOG_ERR);
  200. return -2;
  201. }
  202. }
  203. /**
  204. * Return a generated password using default module
  205. *
  206. * @param boolean $generic true=Create generic password (a MD5 string), false=Use the configured password generation module
  207. * @return string New value for password
  208. */
  209. function getRandomPassword($generic = false) {
  210. global $db, $conf, $langs, $user;
  211. $generated_password = '';
  212. if ($generic)
  213. $generated_password = dol_hash(mt_rand());
  214. else if ($conf->global->USER_PASSWORD_GENERATED) {
  215. $nomclass = "modGeneratePass" . ucfirst($conf->global->USER_PASSWORD_GENERATED);
  216. $nomfichier = $nomclass . ".class.php";
  217. //print DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomclass;
  218. require_once(DOL_DOCUMENT_ROOT . "/core/modules/security/generate/" . $nomfichier);
  219. $genhandler = new $nomclass($db, $conf, $langs, $user);
  220. $generated_password = $genhandler->getNewGeneratedPassword();
  221. unset($genhandler);
  222. }
  223. return $generated_password;
  224. }
  225. ?>