auth.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. // rename file to: `config.php`
  3. // @used by User class to login
  4. // - [ ] use to fetch user groups for acl
  5. /**
  6. * User object:
  7. * - ID // ADM_ID
  8. * - ADM_ACCOUNT // AUTHORIZE_USER, ADM_ACCOUNT
  9. * - ADM_NAME // ADM_NAME
  10. * - ADM_TECH_WORKER // ADM_TECH_WORKER
  11. * - ADM_COMPANY // ADM_COMPANY
  12. * - ADM_ADMIN_LEVEL // ADM_ADMIN_LEVEL
  13. * - ADM_PHONE // ADM_PHONE
  14. * - ADM_ADMIN_EXPIRE // ADM_ADMIN_EXPIRE
  15. * - ADM_ADMIN_DESC // ADM_ADMIN_DESC
  16. * - EMPLOYEE_TYPE // EMPLOYEE_TYPE
  17. * - EMAIL_IMAP_IMPORT_HOST // EMAIL_IMAP_IMPORT_HOST
  18. * - EMAIL_IMAP_IMPORT_USERNAME // EMAIL_IMAP_IMPORT_USERNAME
  19. */
  20. class Theme_Auth_panel_biall_net { // TODO: implements AuthBaseInterface {
  21. static function login($login, $pass) { // @return User object or null
  22. if (empty($login) || empty($pass)) throw new Exception("Proszę podać login i hasło!");
  23. // $item = self::fetchUser($login);
  24. // if (empty($item['PASSWD']) || 32 != strlen($item['PASSWD'])) {
  25. // throw new Exception("Proszę użyć funkcji przypomnienia hasła");
  26. // }
  27. // if (md5($pass) !== $item['PASSWD']) {
  28. // throw new Exception("Proszę podać poprawny login i hasło!");
  29. // }
  30. $userInfo = null;
  31. $activeUsers = self::makeActiveUsers($login);
  32. foreach ($activeUsers as $item) {
  33. if (empty($item['PASSWD']) || 32 != strlen($item['PASSWD'])) {
  34. continue; // throw new Exception("Proszę użyć funkcji przypomnienia hasła");
  35. }
  36. if (md5($pass) === $item['PASSWD']) {
  37. $userInfo = $item;
  38. break;
  39. }
  40. }
  41. if (!$userInfo) throw new Exception("Proszę podać poprawny login i hasło!");
  42. return (object)[
  43. 'ID' => $userInfo['ID'],
  44. 'ADM_ACCOUNT' => $userInfo['LOGIN'], // AUTHORIZE_USER, ADM_ACCOUNT
  45. 'ADM_NAME' => implode(" ", [ $userInfo['P_NAME'], $userInfo['P_NAME_SECOND'] ]), // ADM_NAME
  46. 'ADM_TECH_WORKER' => "", // ADM_TECH_WORKER
  47. 'ADM_COMPANY' => $userInfo['BILLING_OWNER'], // ADM_COMPANY
  48. 'ADM_ADMIN_LEVEL' => 5, // ADM_ADMIN_LEVEL // > 5 will show msg for Kandydat
  49. 'ADM_PHONE' => "", // ADM_PHONE
  50. 'ADM_ADMIN_EXPIRE' => "", // ADM_ADMIN_EXPIRE
  51. 'ADM_ADMIN_DESC' => "", // ADM_ADMIN_DESC
  52. 'EMPLOYEE_TYPE' => "Klient", // EMPLOYEE_TYPE // [ 'Pracownik','Kandydat','Partner','Anonymous','Kontakt','Skrypt' ]
  53. ];
  54. }
  55. static function testAuth($login, $pass) {
  56. $userInfo = null;
  57. $activeUsers = self::makeActiveUsers($login);
  58. foreach ($activeUsers as $item) {
  59. if (empty($item['PASSWD']) || 32 != strlen($item['PASSWD'])) {
  60. continue; // throw new Exception("Proszę użyć funkcji przypomnienia hasła");
  61. }
  62. if (md5($pass) === $item['PASSWD']) {
  63. $userInfo = $item;
  64. break;
  65. }
  66. }
  67. if (!$userInfo) throw new Exception("Proszę podać poprawny login i hasło!");
  68. return $userInfo;
  69. }
  70. // after auth set additional session variables
  71. static function authorizedTrigger($login, $pass = '') {
  72. $fetchAllUsers = self::fetchActiveUsers($login);
  73. $_SESSION['PANEL_BN_USERS'] = (count($fetchAllUsers) > 1) ? array_map(function ($item) {
  74. return [
  75. 'ID' => $item['ID'],
  76. 'LOGIN' => $item['LOGIN'],
  77. 'P_NAME' => $item['P_NAME'],
  78. 'P_NAME_SECOND' => $item['P_NAME_SECOND'],
  79. 'BILLING_OWNER' => $item['BILLING_OWNER'],
  80. ];
  81. }, $fetchAllUsers) : [];
  82. }
  83. static function test_fetchAllUsers($login) {
  84. $sqlTest = "
  85. select c.ID
  86. , c.user_mail_contact
  87. , c.P_NAME, c.P_NAME_SECOND
  88. , c.P_PESEL, c.P_NIP
  89. , c.A_STATUS, c.STATUS
  90. , c.A_CLASSIFIED, c.A_ADM_COMPANY
  91. , c.BILLING_OWNER -- 1 BN, 3 NETDAY
  92. from COMPANIES c
  93. where c.user_mail_contact = :email
  94. ";
  95. return DB::getPDO()->tryHandleException([ __CLASS__, 'preparePanelBNAuthTables' ], 'fetchAll', [
  96. $sqlTest, [ ':email' => $login ]
  97. ]);
  98. }
  99. static function makeActiveUsers($login) {
  100. $activeUsers = self::fetchActiveUsers($login);
  101. foreach ($activeUsers as $idx => $item) {
  102. if (!$item['ID_AUTH']) {
  103. $activeUsers[$idx]['P_PESEL'] = trim(str_replace(' ', '', $item['P_PESEL']));
  104. $activeUsers[$idx]['P_NIP'] = trim(str_replace([' ', '-'], '', $item['P_NIP']));
  105. $passwd = (!empty($activeUsers[$idx]['P_PESEL'])) ? $activeUsers[$idx]['P_PESEL'] : $activeUsers[$idx]['P_NIP'];
  106. $hashPass = ($passwd) ? md5($passwd) : null;
  107. DB::getPDO()->insert('PANEL_KLIENTA_BN_AUTH', [
  108. 'ID_BILLING_USERS' => $activeUsers[$idx]['ID'],
  109. 'LOGIN' => $activeUsers[$idx]['user_mail_contact'],
  110. 'PASSWD' => $hashPass,
  111. 'A_RECORD_CREATE_DATE' => "NOW()",
  112. ]);
  113. $activeUsers[$idx]['ID_BILLING_USERS'] = $activeUsers[$idx]['ID'];
  114. $activeUsers[$idx]['LOGIN'] = $activeUsers[$idx]['user_mail_contact'];
  115. $activeUsers[$idx]['PASSWD'] = $hashPass;
  116. }
  117. }
  118. return $activeUsers;
  119. }
  120. static function fetchActiveUsers($login) {
  121. // TODO: filtr BN / NETDAY ?
  122. // TODO: filtr ZGODA_NA mail/fv ?
  123. $sql = "
  124. select c.ID
  125. , c.user_mail_contact
  126. , c.P_NAME, c.P_NAME_SECOND
  127. , c.P_PESEL, c.P_NIP
  128. , c.A_STATUS, c.STATUS
  129. , c.A_CLASSIFIED, c.A_ADM_COMPANY
  130. , c.BILLING_OWNER -- 1 BN, 3 NETDAY
  131. , p.ID as ID_AUTH
  132. , p.LOGIN
  133. , p.PASSWD
  134. , p.REMIND_PASS_KEY
  135. , p.REMIND_PASS_VALID_TILL
  136. from COMPANIES c
  137. left join PANEL_KLIENTA_BN_AUTH p on ( p.ID_BILLING_USERS = c.ID and p.LOGIN = c.user_mail_contact )
  138. where c.user_mail_contact = :email
  139. and c.A_CLASSIFIED = :acl
  140. and c.A_ADM_COMPANY = :acl
  141. ";
  142. return DB::getPDO()->tryHandleException([ __CLASS__, 'preparePanelBNAuthTables' ], 'fetchAll', [
  143. $sql, [ ':email' => $login, ':acl' => '27_BIALL-NET' ]
  144. ]);
  145. }
  146. static function fetchUser($login) {
  147. $item = DB::getPDO()->tryHandleException([ __CLASS__, 'preparePanelBNAuthTables' ], 'fetchFirst', [
  148. "
  149. select c.ID, c.user_mail_contact, c.P_PESEL
  150. -- , c.PASSWD
  151. , c.P_NAME, c.P_NAME_SECOND
  152. , c.BILLING_OWNER -- 1 BN, 3 NETDAY
  153. , c.is_firma
  154. , c.P_NIP
  155. , p.ID as ID_AUTH
  156. , p.LOGIN
  157. , p.PASSWD
  158. , p.REMIND_PASS_KEY
  159. , p.REMIND_PASS_VALID_TILL
  160. from COMPANIES c
  161. left join PANEL_KLIENTA_BN_AUTH p on ( p.ID_BILLING_USERS = c.ID and p.LOGIN = c.user_mail_contact )
  162. where c.user_mail_contact like :email
  163. ",
  164. [
  165. ':email' => $login,
  166. ]
  167. ]);
  168. if (!$item) throw new Exception("BŁĄD: Brak zarejestrowanego użytkownika o wprowadzonym adresie email.");
  169. if (!$item['ID_AUTH']) {
  170. $item['P_PESEL'] = trim(str_replace(' ', '', $item['P_PESEL']));
  171. $item['P_NIP'] = trim(str_replace([' ', '-'], '', $item['P_NIP']));
  172. $passwd = (!empty($item['P_PESEL'])) ? $item['P_PESEL'] : $item['P_NIP'];
  173. $hashPass = ($passwd) ? md5($passwd) : null;
  174. DB::getPDO()->insert('PANEL_KLIENTA_BN_AUTH', [
  175. 'ID_BILLING_USERS' => $item['ID'],
  176. 'LOGIN' => $item['user_mail_contact'],
  177. 'PASSWD' => $hashPass,
  178. 'A_RECORD_CREATE_DATE' => "NOW()",
  179. ]);
  180. $item['ID_BILLING_USERS'] = $item['ID'];
  181. $item['LOGIN'] = $item['user_mail_contact'];
  182. $item['PASSWD'] = $hashPass;
  183. }
  184. return $item;
  185. }
  186. static function sendRemindPasswd($email, $resetLink, $recipient) {
  187. $recipient = "piotrl86+bn-test-remind@gmail.com"; // TODO: ($recipient) ? $recipient : $email
  188. $headers = "MIME-Version: 1.0\n";
  189. $headers .= "Content-Type: text/plain; charset=\"utf-8\"\n";
  190. $headers .= 'From: Panel klienta BIALL-NET <noreply@biall-net.pl>' . "\r\n";
  191. // $headers .= 'Bcc: piotrl86@gmail.com' . "\r\n";
  192. $subject = "Panel BIALL-NET: Ustawianie nowego hasła";
  193. $body = implode("\r\n\r\n", [
  194. "Ktoś poprosił o wygenerowanie nowego hasła dla następującego konta:",
  195. "Nazwa witryny: Panel klienta BIALL-NET",
  196. "Nazwa użytkownika: {$email}",
  197. "Jeśli to pomyłka po prostu zignoruj tego maila i nic się nie stanie.",
  198. "Aby zresetować hasło, przejdź tutaj:",
  199. "{$resetLink}",
  200. ]);
  201. mail($recipient, $subject, $body, $headers);
  202. }
  203. static function generateRemindKey($email) {
  204. $remindKey = substr(md5($email . "" . date("Y-m-d H:i:s")), 0, 16);
  205. $remindTill = date("Y-m-d", mktime(0,0,0, date("m"), date("d") + 2, date("Y")));
  206. // $userInfo = self::fetchUser($email);
  207. self::makeActiveUsers($email); // creates PANEL_KLIENTA_BN_AUTH if missing
  208. DB::getPDO()->execSql("
  209. update PANEL_KLIENTA_BN_AUTH
  210. set REMIND_PASS_KEY = :remind_key
  211. , REMIND_PASS_VALID_TILL = :remind_till
  212. , A_RECORD_UPDATE_DATE = NOW()
  213. where LOGIN = :login
  214. -- and ID_BILLING_USERS = :id_user
  215. ", [
  216. // ':id_user' => $userInfo['ID'],
  217. // ':login' => $userInfo['LOGIN'],
  218. ':login' => $email,
  219. ':remind_key' => $remindKey,
  220. ':remind_till' => $remindTill,
  221. ]);
  222. return $remindKey;
  223. }
  224. static function setPasswd($email, $newPasswd, $remindKey) {
  225. if (empty($email)) throw new Exception("Missing login!");
  226. if (empty($newPasswd)) throw new Exception("Missing password!");
  227. if (empty($remindKey)) throw new Exception("Missing remindKey!");
  228. // TODO: validate password!
  229. if (strlen($newPasswd) < 8) throw new Exception("Hasło musi się składać z co najmniej 8 znaków");
  230. $userInfo = self::fetchUser($email);
  231. // DBG::nicePrint([
  232. // 'c1' => empty($userInfo['REMIND_PASS_KEY']),
  233. // 'c2' => $userInfo['REMIND_PASS_KEY'] !== $remindKey,
  234. // 'c2.L' => $userInfo['REMIND_PASS_KEY'],
  235. // 'c2.R' => $remindKey,
  236. // 'c3' => date("Y-m-d") > $userInfo['REMIND_PASS_VALID_TILL'],
  237. // 'c3.L' => date("Y-m-d"),
  238. // 'c3.R' => $userInfo['REMIND_PASS_VALID_TILL'],
  239. // 'user' => $userInfo,
  240. // ], 'DBG');
  241. if (empty($userInfo['REMIND_PASS_KEY'])
  242. || $userInfo['REMIND_PASS_KEY'] !== $remindKey
  243. || date("Y-m-d") > $userInfo['REMIND_PASS_VALID_TILL']
  244. ) throw new Exception("Odnośnik do resetowania hasła wydaje się być niesprawny. Proszę użyć funkcji przypomnienia hasła.");
  245. DB::getPDO()->execSql("
  246. update PANEL_KLIENTA_BN_AUTH
  247. set REMIND_PASS_KEY = ''
  248. , REMIND_PASS_VALID_TILL = '0000-00-00'
  249. , PASSWD = :hash_passwd
  250. , A_RECORD_UPDATE_DATE = NOW()
  251. where LOGIN = :login
  252. -- and ID_BILLING_USERS = :id_user
  253. ", [
  254. // ':id_user' => $userInfo['ID'],
  255. // ':login' => $userInfo['LOGIN'],
  256. ':login' => $email,
  257. ':hash_passwd' => md5($newPasswd),
  258. ]);
  259. }
  260. static function preparePanelBNAuthTables() {
  261. DB::getPDO()->execSql("
  262. CREATE TABLE IF NOT EXISTS `PANEL_KLIENTA_BN_AUTH` (
  263. `ID` int(11) NOT NULL AUTO_INCREMENT,
  264. `ID_BILLING_USERS` int(11) NOT NULL,
  265. `LOGIN` varchar(255) NOT NULL DEFAULT '',
  266. `PASSWD` varchar(32) NOT NULL DEFAULT '',
  267. `REMIND_PASS_KEY` varchar(16) NOT NULL DEFAULT '',
  268. `REMIND_PASS_VALID_TILL` date NOT NULL DEFAULT '0000-00-00',
  269. `A_ADM_COMPANY` varchar(64) NOT NULL DEFAULT '',
  270. `A_CLASSIFIED` varchar(64) NOT NULL DEFAULT '',
  271. `A_RECORD_CREATE_DATE` datetime NOT NULL,
  272. `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
  273. `A_RECORD_UPDATE_DATE` datetime NOT NULL,
  274. `A_RECORD_UPDATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
  275. PRIMARY KEY (`ID`),
  276. UNIQUE KEY `COMPANY_LOGIN` (`LOGIN`, `ID_BILLING_USERS`)
  277. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 ;
  278. ");
  279. DB::getPDO()->execSql("
  280. CREATE TABLE IF NOT EXISTS `PANEL_KLIENTA_BN_AUTH_HIST` (
  281. `ID` int(11) NOT NULL AUTO_INCREMENT,
  282. `ID_USERS2` int(11) NOT NULL,
  283. `ID_BILLING_USERS` varchar(11) NOT NULL DEFAULT 'N/S;',
  284. `LOGIN` varchar(255) NOT NULL DEFAULT 'N/S;',
  285. `PASSWD` varchar(32) NOT NULL DEFAULT 'N/S;',
  286. `REMIND_PASS_KEY` varchar(16) NOT NULL DEFAULT 'N/S;',
  287. `REMIND_PASS_VALID_TILL` varchar(10) NOT NULL DEFAULT 'N/S;',
  288. `A_ADM_COMPANY` varchar(64) NOT NULL DEFAULT 'N/S;',
  289. `A_CLASSIFIED` varchar(64) NOT NULL DEFAULT 'N/S;',
  290. `A_RECORD_CREATE_DATE` varchar(10) NOT NULL DEFAULT 'N/S;',
  291. `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT 'N/S;',
  292. `A_RECORD_UPDATE_DATE` varchar(10) NOT NULL DEFAULT 'N/S;',
  293. `A_RECORD_UPDATE_AUTHOR` varchar(20) NOT NULL DEFAULT 'N/S;',
  294. PRIMARY KEY (`ID`),
  295. KEY `ID_USERS2` (`ID_USERS2`)
  296. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 ;
  297. ");
  298. }
  299. }