auth.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. <?php
  2. use dokuwiki\Utf8\Sort;
  3. /**
  4. * DokuWiki Plugin authpdo (Auth Component)
  5. *
  6. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  7. * @author Andreas Gohr <andi@splitbrain.org>
  8. */
  9. /**
  10. * Class auth_plugin_authpdo
  11. */
  12. class auth_plugin_authpdo extends DokuWiki_Auth_Plugin
  13. {
  14. /** @var PDO */
  15. protected $pdo;
  16. /** @var null|array The list of all groups */
  17. protected $groupcache = null;
  18. /**
  19. * Constructor.
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct(); // for compatibility
  24. if (!class_exists('PDO')) {
  25. $this->debugMsg('PDO extension for PHP not found.', -1, __LINE__);
  26. $this->success = false;
  27. return;
  28. }
  29. if (!$this->getConf('dsn')) {
  30. $this->debugMsg('No DSN specified', -1, __LINE__);
  31. $this->success = false;
  32. return;
  33. }
  34. try {
  35. $this->pdo = new PDO(
  36. $this->getConf('dsn'),
  37. $this->getConf('user'),
  38. conf_decodeString($this->getConf('pass')),
  39. array(
  40. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // always fetch as array
  41. PDO::ATTR_EMULATE_PREPARES => true, // emulating prepares allows us to reuse param names
  42. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
  43. )
  44. );
  45. } catch (PDOException $e) {
  46. $this->debugMsg($e);
  47. msg($this->getLang('connectfail'), -1);
  48. $this->success = false;
  49. return;
  50. }
  51. // can Users be created?
  52. $this->cando['addUser'] = $this->checkConfig(
  53. array(
  54. 'select-user',
  55. 'select-user-groups',
  56. 'select-groups',
  57. 'insert-user',
  58. 'insert-group',
  59. 'join-group'
  60. )
  61. );
  62. // can Users be deleted?
  63. $this->cando['delUser'] = $this->checkConfig(
  64. array(
  65. 'select-user',
  66. 'select-user-groups',
  67. 'select-groups',
  68. 'leave-group',
  69. 'delete-user'
  70. )
  71. );
  72. // can login names be changed?
  73. $this->cando['modLogin'] = $this->checkConfig(
  74. array(
  75. 'select-user',
  76. 'select-user-groups',
  77. 'update-user-login'
  78. )
  79. );
  80. // can passwords be changed?
  81. $this->cando['modPass'] = $this->checkConfig(
  82. array(
  83. 'select-user',
  84. 'select-user-groups',
  85. 'update-user-pass'
  86. )
  87. );
  88. // can real names be changed?
  89. $this->cando['modName'] = $this->checkConfig(
  90. array(
  91. 'select-user',
  92. 'select-user-groups',
  93. 'update-user-info:name'
  94. )
  95. );
  96. // can real email be changed?
  97. $this->cando['modMail'] = $this->checkConfig(
  98. array(
  99. 'select-user',
  100. 'select-user-groups',
  101. 'update-user-info:mail'
  102. )
  103. );
  104. // can groups be changed?
  105. $this->cando['modGroups'] = $this->checkConfig(
  106. array(
  107. 'select-user',
  108. 'select-user-groups',
  109. 'select-groups',
  110. 'leave-group',
  111. 'join-group',
  112. 'insert-group'
  113. )
  114. );
  115. // can a filtered list of users be retrieved?
  116. $this->cando['getUsers'] = $this->checkConfig(
  117. array(
  118. 'list-users'
  119. )
  120. );
  121. // can the number of users be retrieved?
  122. $this->cando['getUserCount'] = $this->checkConfig(
  123. array(
  124. 'count-users'
  125. )
  126. );
  127. // can a list of available groups be retrieved?
  128. $this->cando['getGroups'] = $this->checkConfig(
  129. array(
  130. 'select-groups'
  131. )
  132. );
  133. $this->success = true;
  134. }
  135. /**
  136. * Check user+password
  137. *
  138. * @param string $user the user name
  139. * @param string $pass the clear text password
  140. * @return bool
  141. */
  142. public function checkPass($user, $pass)
  143. {
  144. $userdata = $this->selectUser($user);
  145. if ($userdata == false) return false;
  146. // password checking done in SQL?
  147. if ($this->checkConfig(array('check-pass'))) {
  148. $userdata['clear'] = $pass;
  149. $userdata['hash'] = auth_cryptPassword($pass);
  150. $result = $this->query($this->getConf('check-pass'), $userdata);
  151. if ($result === false) return false;
  152. return (count($result) == 1);
  153. }
  154. // we do password checking on our own
  155. if (isset($userdata['hash'])) {
  156. // hashed password
  157. $passhash = new \dokuwiki\PassHash();
  158. return $passhash->verify_hash($pass, $userdata['hash']);
  159. } else {
  160. // clear text password in the database O_o
  161. return ($pass === $userdata['clear']);
  162. }
  163. }
  164. /**
  165. * Return user info
  166. *
  167. * Returns info about the given user needs to contain
  168. * at least these fields:
  169. *
  170. * name string full name of the user
  171. * mail string email addres of the user
  172. * grps array list of groups the user is in
  173. *
  174. * @param string $user the user name
  175. * @param bool $requireGroups whether or not the returned data must include groups
  176. * @return array|bool containing user data or false
  177. */
  178. public function getUserData($user, $requireGroups = true)
  179. {
  180. $data = $this->selectUser($user);
  181. if ($data == false) return false;
  182. if (isset($data['hash'])) unset($data['hash']);
  183. if (isset($data['clean'])) unset($data['clean']);
  184. if ($requireGroups) {
  185. $data['grps'] = $this->selectUserGroups($data);
  186. if ($data['grps'] === false) return false;
  187. }
  188. return $data;
  189. }
  190. /**
  191. * Create a new User [implement only where required/possible]
  192. *
  193. * Returns false if the user already exists, null when an error
  194. * occurred and true if everything went well.
  195. *
  196. * The new user HAS TO be added to the default group by this
  197. * function!
  198. *
  199. * Set addUser capability when implemented
  200. *
  201. * @param string $user
  202. * @param string $clear
  203. * @param string $name
  204. * @param string $mail
  205. * @param null|array $grps
  206. * @return bool|null
  207. */
  208. public function createUser($user, $clear, $name, $mail, $grps = null)
  209. {
  210. global $conf;
  211. if (($info = $this->getUserData($user, false)) !== false) {
  212. msg($this->getLang('userexists'), -1);
  213. return false; // user already exists
  214. }
  215. // prepare data
  216. if ($grps == null) $grps = array();
  217. array_unshift($grps, $conf['defaultgroup']);
  218. $grps = array_unique($grps);
  219. $hash = auth_cryptPassword($clear);
  220. $userdata = compact('user', 'clear', 'hash', 'name', 'mail');
  221. // action protected by transaction
  222. $this->pdo->beginTransaction();
  223. {
  224. // insert the user
  225. $ok = $this->query($this->getConf('insert-user'), $userdata);
  226. if ($ok === false) goto FAIL;
  227. $userdata = $this->getUserData($user, false);
  228. if ($userdata === false) goto FAIL;
  229. // create all groups that do not exist, the refetch the groups
  230. $allgroups = $this->selectGroups();
  231. foreach ($grps as $group) {
  232. if (!isset($allgroups[$group])) {
  233. $ok = $this->addGroup($group);
  234. if ($ok === false) goto FAIL;
  235. }
  236. }
  237. $allgroups = $this->selectGroups();
  238. // add user to the groups
  239. foreach ($grps as $group) {
  240. $ok = $this->joinGroup($userdata, $allgroups[$group]);
  241. if ($ok === false) goto FAIL;
  242. }
  243. }
  244. $this->pdo->commit();
  245. return true;
  246. // something went wrong, rollback
  247. FAIL:
  248. $this->pdo->rollBack();
  249. $this->debugMsg('Transaction rolled back', 0, __LINE__);
  250. msg($this->getLang('writefail'), -1);
  251. return null; // return error
  252. }
  253. /**
  254. * Modify user data
  255. *
  256. * @param string $user nick of the user to be changed
  257. * @param array $changes array of field/value pairs to be changed (password will be clear text)
  258. * @return bool
  259. */
  260. public function modifyUser($user, $changes)
  261. {
  262. // secure everything in transaction
  263. $this->pdo->beginTransaction();
  264. {
  265. $olddata = $this->getUserData($user);
  266. $oldgroups = $olddata['grps'];
  267. unset($olddata['grps']);
  268. // changing the user name?
  269. if (isset($changes['user'])) {
  270. if ($this->getUserData($changes['user'], false)) goto FAIL;
  271. $params = $olddata;
  272. $params['newlogin'] = $changes['user'];
  273. $ok = $this->query($this->getConf('update-user-login'), $params);
  274. if ($ok === false) goto FAIL;
  275. }
  276. // changing the password?
  277. if (isset($changes['pass'])) {
  278. $params = $olddata;
  279. $params['clear'] = $changes['pass'];
  280. $params['hash'] = auth_cryptPassword($changes['pass']);
  281. $ok = $this->query($this->getConf('update-user-pass'), $params);
  282. if ($ok === false) goto FAIL;
  283. }
  284. // changing info?
  285. if (isset($changes['mail']) || isset($changes['name'])) {
  286. $params = $olddata;
  287. if (isset($changes['mail'])) $params['mail'] = $changes['mail'];
  288. if (isset($changes['name'])) $params['name'] = $changes['name'];
  289. $ok = $this->query($this->getConf('update-user-info'), $params);
  290. if ($ok === false) goto FAIL;
  291. }
  292. // changing groups?
  293. if (isset($changes['grps'])) {
  294. $allgroups = $this->selectGroups();
  295. // remove membership for previous groups
  296. foreach ($oldgroups as $group) {
  297. if (!in_array($group, $changes['grps']) && isset($allgroups[$group])) {
  298. $ok = $this->leaveGroup($olddata, $allgroups[$group]);
  299. if ($ok === false) goto FAIL;
  300. }
  301. }
  302. // create all new groups that are missing
  303. $added = 0;
  304. foreach ($changes['grps'] as $group) {
  305. if (!isset($allgroups[$group])) {
  306. $ok = $this->addGroup($group);
  307. if ($ok === false) goto FAIL;
  308. $added++;
  309. }
  310. }
  311. // reload group info
  312. if ($added > 0) $allgroups = $this->selectGroups();
  313. // add membership for new groups
  314. foreach ($changes['grps'] as $group) {
  315. if (!in_array($group, $oldgroups)) {
  316. $ok = $this->joinGroup($olddata, $allgroups[$group]);
  317. if ($ok === false) goto FAIL;
  318. }
  319. }
  320. }
  321. }
  322. $this->pdo->commit();
  323. return true;
  324. // something went wrong, rollback
  325. FAIL:
  326. $this->pdo->rollBack();
  327. $this->debugMsg('Transaction rolled back', 0, __LINE__);
  328. msg($this->getLang('writefail'), -1);
  329. return false; // return error
  330. }
  331. /**
  332. * Delete one or more users
  333. *
  334. * Set delUser capability when implemented
  335. *
  336. * @param array $users
  337. * @return int number of users deleted
  338. */
  339. public function deleteUsers($users)
  340. {
  341. $count = 0;
  342. foreach ($users as $user) {
  343. if ($this->deleteUser($user)) $count++;
  344. }
  345. return $count;
  346. }
  347. /**
  348. * Bulk retrieval of user data [implement only where required/possible]
  349. *
  350. * Set getUsers capability when implemented
  351. *
  352. * @param int $start index of first user to be returned
  353. * @param int $limit max number of users to be returned
  354. * @param array $filter array of field/pattern pairs, null for no filter
  355. * @return array list of userinfo (refer getUserData for internal userinfo details)
  356. */
  357. public function retrieveUsers($start = 0, $limit = -1, $filter = null)
  358. {
  359. if ($limit < 0) $limit = 10000; // we don't support no limit
  360. if (is_null($filter)) $filter = array();
  361. if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
  362. foreach (array('user', 'name', 'mail', 'group') as $key) {
  363. if (!isset($filter[$key])) {
  364. $filter[$key] = '%';
  365. } else {
  366. $filter[$key] = '%' . $filter[$key] . '%';
  367. }
  368. }
  369. $filter['start'] = (int)$start;
  370. $filter['end'] = (int)$start + $limit;
  371. $filter['limit'] = (int)$limit;
  372. $result = $this->query($this->getConf('list-users'), $filter);
  373. if (!$result) return array();
  374. $users = array();
  375. if (is_array($result)) {
  376. foreach ($result as $row) {
  377. if (!isset($row['user'])) {
  378. $this->debugMsg("list-users statement did not return 'user' attribute", -1, __LINE__);
  379. return array();
  380. }
  381. $users[] = $this->getUserData($row['user']);
  382. }
  383. } else {
  384. $this->debugMsg("list-users statement did not return a list of result", -1, __LINE__);
  385. }
  386. return $users;
  387. }
  388. /**
  389. * Return a count of the number of user which meet $filter criteria
  390. *
  391. * @param array $filter array of field/pattern pairs, empty array for no filter
  392. * @return int
  393. */
  394. public function getUserCount($filter = array())
  395. {
  396. if (is_null($filter)) $filter = array();
  397. if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
  398. foreach (array('user', 'name', 'mail', 'group') as $key) {
  399. if (!isset($filter[$key])) {
  400. $filter[$key] = '%';
  401. } else {
  402. $filter[$key] = '%' . $filter[$key] . '%';
  403. }
  404. }
  405. $result = $this->query($this->getConf('count-users'), $filter);
  406. if (!$result || !isset($result[0]['count'])) {
  407. $this->debugMsg("Statement did not return 'count' attribute", -1, __LINE__);
  408. }
  409. return (int)$result[0]['count'];
  410. }
  411. /**
  412. * Create a new group with the given name
  413. *
  414. * @param string $group
  415. * @return bool
  416. */
  417. public function addGroup($group)
  418. {
  419. $sql = $this->getConf('insert-group');
  420. $result = $this->query($sql, array(':group' => $group));
  421. $this->clearGroupCache();
  422. if ($result === false) return false;
  423. return true;
  424. }
  425. /**
  426. * Retrieve groups
  427. *
  428. * Set getGroups capability when implemented
  429. *
  430. * @param int $start
  431. * @param int $limit
  432. * @return array
  433. */
  434. public function retrieveGroups($start = 0, $limit = 0)
  435. {
  436. $groups = array_keys($this->selectGroups());
  437. if ($groups === false) return array();
  438. if (!$limit) {
  439. return array_splice($groups, $start);
  440. } else {
  441. return array_splice($groups, $start, $limit);
  442. }
  443. }
  444. /**
  445. * Select data of a specified user
  446. *
  447. * @param string $user the user name
  448. * @return bool|array user data, false on error
  449. */
  450. protected function selectUser($user)
  451. {
  452. $sql = $this->getConf('select-user');
  453. $result = $this->query($sql, array(':user' => $user));
  454. if (!$result) return false;
  455. if (count($result) > 1) {
  456. $this->debugMsg('Found more than one matching user', -1, __LINE__);
  457. return false;
  458. }
  459. $data = array_shift($result);
  460. $dataok = true;
  461. if (!isset($data['user'])) {
  462. $this->debugMsg("Statement did not return 'user' attribute", -1, __LINE__);
  463. $dataok = false;
  464. }
  465. if (!isset($data['hash']) && !isset($data['clear']) && !$this->checkConfig(array('check-pass'))) {
  466. $this->debugMsg("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__);
  467. $dataok = false;
  468. }
  469. if (!isset($data['name'])) {
  470. $this->debugMsg("Statement did not return 'name' attribute", -1, __LINE__);
  471. $dataok = false;
  472. }
  473. if (!isset($data['mail'])) {
  474. $this->debugMsg("Statement did not return 'mail' attribute", -1, __LINE__);
  475. $dataok = false;
  476. }
  477. if (!$dataok) return false;
  478. return $data;
  479. }
  480. /**
  481. * Delete a user after removing all their group memberships
  482. *
  483. * @param string $user
  484. * @return bool true when the user was deleted
  485. */
  486. protected function deleteUser($user)
  487. {
  488. $this->pdo->beginTransaction();
  489. {
  490. $userdata = $this->getUserData($user);
  491. if ($userdata === false) goto FAIL;
  492. $allgroups = $this->selectGroups();
  493. // remove group memberships (ignore errors)
  494. foreach ($userdata['grps'] as $group) {
  495. if (isset($allgroups[$group])) {
  496. $this->leaveGroup($userdata, $allgroups[$group]);
  497. }
  498. }
  499. $ok = $this->query($this->getConf('delete-user'), $userdata);
  500. if ($ok === false) goto FAIL;
  501. }
  502. $this->pdo->commit();
  503. return true;
  504. FAIL:
  505. $this->pdo->rollBack();
  506. return false;
  507. }
  508. /**
  509. * Select all groups of a user
  510. *
  511. * @param array $userdata The userdata as returned by _selectUser()
  512. * @return array|bool list of group names, false on error
  513. */
  514. protected function selectUserGroups($userdata)
  515. {
  516. global $conf;
  517. $sql = $this->getConf('select-user-groups');
  518. $result = $this->query($sql, $userdata);
  519. if ($result === false) return false;
  520. $groups = array($conf['defaultgroup']); // always add default config
  521. if (is_array($result)) {
  522. foreach ($result as $row) {
  523. if (!isset($row['group'])) {
  524. $this->debugMsg("No 'group' field returned in select-user-groups statement", -1, __LINE__);
  525. return false;
  526. }
  527. $groups[] = $row['group'];
  528. }
  529. } else {
  530. $this->debugMsg("select-user-groups statement did not return a list of result", -1, __LINE__);
  531. }
  532. $groups = array_unique($groups);
  533. Sort::sort($groups);
  534. return $groups;
  535. }
  536. /**
  537. * Select all available groups
  538. *
  539. * @return array|bool list of all available groups and their properties
  540. */
  541. protected function selectGroups()
  542. {
  543. if ($this->groupcache) return $this->groupcache;
  544. $sql = $this->getConf('select-groups');
  545. $result = $this->query($sql);
  546. if ($result === false) return false;
  547. $groups = array();
  548. if (is_array($result)) {
  549. foreach ($result as $row) {
  550. if (!isset($row['group'])) {
  551. $this->debugMsg("No 'group' field returned from select-groups statement", -1, __LINE__);
  552. return false;
  553. }
  554. // relayout result with group name as key
  555. $group = $row['group'];
  556. $groups[$group] = $row;
  557. }
  558. } else {
  559. $this->debugMsg("select-groups statement did not return a list of result", -1, __LINE__);
  560. }
  561. Sort::ksort($groups);
  562. return $groups;
  563. }
  564. /**
  565. * Remove all entries from the group cache
  566. */
  567. protected function clearGroupCache()
  568. {
  569. $this->groupcache = null;
  570. }
  571. /**
  572. * Adds the user to the group
  573. *
  574. * @param array $userdata all the user data
  575. * @param array $groupdata all the group data
  576. * @return bool
  577. */
  578. protected function joinGroup($userdata, $groupdata)
  579. {
  580. $data = array_merge($userdata, $groupdata);
  581. $sql = $this->getConf('join-group');
  582. $result = $this->query($sql, $data);
  583. if ($result === false) return false;
  584. return true;
  585. }
  586. /**
  587. * Removes the user from the group
  588. *
  589. * @param array $userdata all the user data
  590. * @param array $groupdata all the group data
  591. * @return bool
  592. */
  593. protected function leaveGroup($userdata, $groupdata)
  594. {
  595. $data = array_merge($userdata, $groupdata);
  596. $sql = $this->getConf('leave-group');
  597. $result = $this->query($sql, $data);
  598. if ($result === false) return false;
  599. return true;
  600. }
  601. /**
  602. * Executes a query
  603. *
  604. * @param string $sql The SQL statement to execute
  605. * @param array $arguments Named parameters to be used in the statement
  606. * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error
  607. */
  608. protected function query($sql, $arguments = array())
  609. {
  610. $sql = trim($sql);
  611. if (empty($sql)) {
  612. $this->debugMsg('No SQL query given', -1, __LINE__);
  613. return false;
  614. }
  615. // execute
  616. $params = array();
  617. $sth = $this->pdo->prepare($sql);
  618. $result = false;
  619. try {
  620. // prepare parameters - we only use those that exist in the SQL
  621. foreach ($arguments as $key => $value) {
  622. if (is_array($value)) continue;
  623. if (is_object($value)) continue;
  624. if ($key[0] != ':') $key = ":$key"; // prefix with colon if needed
  625. if (strpos($sql, $key) === false) continue; // skip if parameter is missing
  626. if (is_int($value)) {
  627. $sth->bindValue($key, $value, PDO::PARAM_INT);
  628. } else {
  629. $sth->bindValue($key, $value);
  630. }
  631. $params[$key] = $value; //remember for debugging
  632. }
  633. $sth->execute();
  634. // only report last line's result
  635. $hasnextrowset = true;
  636. $currentsql = $sql;
  637. while ($hasnextrowset) {
  638. if (strtolower(substr($currentsql, 0, 6)) == 'select') {
  639. $result = $sth->fetchAll();
  640. } else {
  641. $result = $sth->rowCount();
  642. }
  643. $semi_pos = strpos($currentsql, ';');
  644. if ($semi_pos) {
  645. $currentsql = trim(substr($currentsql, $semi_pos + 1));
  646. }
  647. try {
  648. $hasnextrowset = $sth->nextRowset(); // run next rowset
  649. } catch (PDOException $rowset_e) {
  650. $hasnextrowset = false; // driver does not support multi-rowset, should be executed in one time
  651. }
  652. }
  653. } catch (Exception $e) {
  654. // report the caller's line
  655. $trace = debug_backtrace();
  656. $line = $trace[0]['line'];
  657. $dsql = $this->debugSQL($sql, $params, !defined('DOKU_UNITTEST'));
  658. $this->debugMsg($e, -1, $line);
  659. $this->debugMsg("SQL: <pre>$dsql</pre>", -1, $line);
  660. }
  661. $sth->closeCursor();
  662. $sth = null;
  663. return $result;
  664. }
  665. /**
  666. * Wrapper around msg() but outputs only when debug is enabled
  667. *
  668. * @param string|Exception $message
  669. * @param int $err
  670. * @param int $line
  671. */
  672. protected function debugMsg($message, $err = 0, $line = 0)
  673. {
  674. if (!$this->getConf('debug')) return;
  675. if (is_a($message, 'Exception')) {
  676. $err = -1;
  677. $msg = $message->getMessage();
  678. if (!$line) $line = $message->getLine();
  679. } else {
  680. $msg = $message;
  681. }
  682. if (defined('DOKU_UNITTEST')) {
  683. printf("\n%s, %s:%d\n", $msg, __FILE__, $line);
  684. } else {
  685. msg('authpdo: ' . $msg, $err, $line, __FILE__);
  686. }
  687. }
  688. /**
  689. * Check if the given config strings are set
  690. *
  691. * @param string[] $keys
  692. * @return bool
  693. * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
  694. *
  695. */
  696. protected function checkConfig($keys)
  697. {
  698. foreach ($keys as $key) {
  699. $params = explode(':', $key);
  700. $key = array_shift($params);
  701. $sql = trim($this->getConf($key));
  702. // check if sql is set
  703. if (!$sql) return false;
  704. // check if needed params are there
  705. foreach ($params as $param) {
  706. if (strpos($sql, ":$param") === false) return false;
  707. }
  708. }
  709. return true;
  710. }
  711. /**
  712. * create an approximation of the SQL string with parameters replaced
  713. *
  714. * @param string $sql
  715. * @param array $params
  716. * @param bool $htmlescape Should the result be escaped for output in HTML?
  717. * @return string
  718. */
  719. protected function debugSQL($sql, $params, $htmlescape = true)
  720. {
  721. foreach ($params as $key => $val) {
  722. if (is_int($val)) {
  723. $val = $this->pdo->quote($val, PDO::PARAM_INT);
  724. } elseif (is_bool($val)) {
  725. $val = $this->pdo->quote($val, PDO::PARAM_BOOL);
  726. } elseif (is_null($val)) {
  727. $val = 'NULL';
  728. } else {
  729. $val = $this->pdo->quote($val);
  730. }
  731. $sql = str_replace($key, $val, $sql);
  732. }
  733. if ($htmlescape) $sql = hsc($sql);
  734. return $sql;
  735. }
  736. }
  737. // vim:ts=4:sw=4:et: