api-functions.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. <?php /** @noinspection SqlResolve */
  2. /** @noinspection SqlResolve */
  3. /** @noinspection SqlResolve */
  4. /** @noinspection SqlResolve */
  5. /** @noinspection SyntaxError */
  6. function login($array)
  7. {
  8. // Grab username and Password from login form
  9. $username = $password = '';
  10. foreach ($array['data'] as $items) {
  11. foreach ($items as $key => $value) {
  12. if ($key == 'name') {
  13. $newKey = $value;
  14. }
  15. if ($key == 'value') {
  16. $newValue = $value;
  17. }
  18. if (isset($newKey) && isset($newValue)) {
  19. $$newKey = $newValue;
  20. }
  21. }
  22. }
  23. $username = strtolower($username);
  24. $days = (isset($remember)) ? 7 : 1;
  25. try {
  26. $database = new Dibi\Connection([
  27. 'driver' => 'sqlite3',
  28. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  29. ]);
  30. $authSuccess = false;
  31. $function = 'plugin_auth_' . $GLOBALS['authBackend'];
  32. $result = $database->fetch('SELECT * FROM users WHERE username = ? COLLATE NOCASE OR email = ? COLLATE NOCASE', $username, $username);
  33. switch ($GLOBALS['authType']) {
  34. case 'external':
  35. if (function_exists($function)) {
  36. $authSuccess = $function($username, $password);
  37. }
  38. break;
  39. /** @noinspection PhpMissingBreakStatementInspection */
  40. case 'both':
  41. if (function_exists($function)) {
  42. $authSuccess = $function($username, $password);
  43. }
  44. // no break
  45. default: // Internal
  46. if (!$authSuccess) {
  47. // perform the internal authentication step
  48. if (password_verify($password, $result['password'])) {
  49. $authSuccess = true;
  50. }
  51. }
  52. }
  53. if ($authSuccess) {
  54. // Make sure user exists in database
  55. $userExists = false;
  56. $passwordMatches = false;
  57. $token = (is_array($authSuccess) && isset($authSuccess['token']) ? $authSuccess['token'] : '');
  58. if ($result['username']) {
  59. $userExists = true;
  60. $username = $result['username'];
  61. $passwordMatches = (password_verify($password, $result['password'])) ? true : false;
  62. }
  63. if ($userExists) {
  64. //does org password need to be updated
  65. if (!$passwordMatches) {
  66. $database->query('
  67. UPDATE users SET', [
  68. 'password' => password_hash($password, PASSWORD_BCRYPT)
  69. ], '
  70. WHERE id=?', $result['id']);
  71. writeLog('success', 'Login Function - User Password updated from backend', $username);
  72. }
  73. // 2FA might go here
  74. if ($result['auth_service'] !== 'internal' && strpos($result['auth_service'], '::') !== false) {
  75. $TFA = explode('::', $result['auth_service']);
  76. // Is code with login info?
  77. if ($tfaCode == '') {
  78. return '2FA';
  79. } else {
  80. if (!verify2FA($TFA[1], $tfaCode, $TFA[0])) {
  81. return '2FA-incorrect';
  82. }
  83. }
  84. }
  85. // End 2FA
  86. // authentication passed - 1) mark active and update token
  87. if (createToken($result['username'], $result['email'], $result['image'], $result['group'], $result['group_id'], $GLOBALS['organizrHash'], $days)) {
  88. writeLoginLog($username, 'success');
  89. writeLog('success', 'Login Function - A User has logged in', $username);
  90. ssoCheck($username, $password, $token); //need to work on this
  91. return true;
  92. } else {
  93. return 'error';
  94. }
  95. } else {
  96. // Create User
  97. ssoCheck($username, $password, $token);
  98. return authRegister((is_array($authSuccess) && isset($authSuccess['username']) ? $authSuccess['username'] : $username), $password, defaultUserGroup(), (is_array($authSuccess) && isset($authSuccess['email']) ? $authSuccess['email'] : ''));
  99. }
  100. } else {
  101. // authentication failed
  102. writeLoginLog($username, 'error');
  103. writeLog('error', 'Login Function - Wrong Password', $username);
  104. return 'mismatch';
  105. }
  106. } catch (Dibi\Exception $e) {
  107. return $e;
  108. }
  109. }
  110. function createDB($path, $filename)
  111. {
  112. try {
  113. if (!file_exists($path)) {
  114. mkdir($path, 0777, true);
  115. }
  116. $createDB = new Dibi\Connection([
  117. 'driver' => 'sqlite3',
  118. 'database' => $path . $filename,
  119. ]);
  120. // Create Users
  121. $createDB->query('CREATE TABLE `users` (
  122. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  123. `username` TEXT UNIQUE,
  124. `password` TEXT,
  125. `email` TEXT,
  126. `plex_token` TEXT,
  127. `group` TEXT,
  128. `group_id` INTEGER,
  129. `locked` INTEGER,
  130. `image` TEXT,
  131. `register_date` DATE,
  132. `auth_service` TEXT DEFAULT \'internal\'
  133. );');
  134. // Create Tokens
  135. $createDB->query('CREATE TABLE `chatroom` (
  136. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  137. `username` TEXT,
  138. `gravatar` TEXT,
  139. `uid` TEXT,
  140. `date` DATE,
  141. `ip` TEXT,
  142. `message` TEXT
  143. );');
  144. $createDB->query('CREATE TABLE `tokens` (
  145. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  146. `token` TEXT UNIQUE,
  147. `user_id` INTEGER,
  148. `created` DATE,
  149. `expires` DATE
  150. );');
  151. $createDB->query('CREATE TABLE `groups` (
  152. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  153. `group` TEXT UNIQUE,
  154. `group_id` INTEGER,
  155. `image` TEXT,
  156. `default` INTEGER
  157. );');
  158. $createDB->query('CREATE TABLE `categories` (
  159. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  160. `order` INTEGER,
  161. `category` TEXT UNIQUE,
  162. `category_id` INTEGER,
  163. `image` TEXT,
  164. `default` INTEGER
  165. );');
  166. // Create Tabs
  167. $createDB->query('CREATE TABLE `tabs` (
  168. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  169. `order` INTEGER,
  170. `category_id` INTEGER,
  171. `name` TEXT,
  172. `url` TEXT,
  173. `url_local` TEXT,
  174. `default` INTEGER,
  175. `enabled` INTEGER,
  176. `group_id` INTEGER,
  177. `image` TEXT,
  178. `type` INTEGER,
  179. `splash` INTEGER,
  180. `ping` INTEGER,
  181. `ping_url` TEXT
  182. );');
  183. // Create Options
  184. $createDB->query('CREATE TABLE `options` (
  185. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  186. `name` TEXT UNIQUE,
  187. `value` TEXT
  188. );');
  189. // Create Invites
  190. $createDB->query('CREATE TABLE `invites` (
  191. `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
  192. `code` TEXT UNIQUE,
  193. `date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  194. `email` TEXT,
  195. `username` TEXT,
  196. `dateused` TIMESTAMP,
  197. `usedby` TEXT,
  198. `ip` TEXT,
  199. `valid` TEXT,
  200. `type` TEXT
  201. );');
  202. return true;
  203. } catch (Dibi\Exception $e) {
  204. return false;
  205. }
  206. }
  207. // Upgrade Database
  208. function updateDB($oldVerNum = false)
  209. {
  210. $tempLock = $GLOBALS['dbLocation'] . 'DBLOCK.txt';
  211. if (!file_exists($tempLock)) {
  212. touch($tempLock);
  213. // Create Temp DB First
  214. $migrationDB = 'tempMigration.db';
  215. $pathDigest = pathinfo($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
  216. if (file_exists($GLOBALS['dbLocation'] . $migrationDB)) {
  217. unlink($GLOBALS['dbLocation'] . $migrationDB);
  218. }
  219. copy($GLOBALS['dbLocation'] . $GLOBALS['dbName'], $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db');
  220. @unlink($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
  221. $success = createDB($GLOBALS['dbLocation'], $migrationDB);
  222. try {
  223. $connectOldDB = new Dibi\Connection([
  224. 'driver' => 'sqlite3',
  225. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  226. ]);
  227. $connectNewDB = new Dibi\Connection([
  228. 'driver' => 'sqlite3',
  229. 'database' => $GLOBALS['dbLocation'] . $migrationDB,
  230. ]);
  231. $tables = $connectOldDB->fetchAll('SELECT name FROM sqlite_master WHERE type="table"');
  232. foreach ($tables as $table) {
  233. $data = $connectOldDB->fetchAll('SELECT * FROM ' . $table['name']);
  234. foreach ($data as $row) {
  235. $connectNewDB->query('INSERT into ' . $table['name'], $row);
  236. }
  237. }
  238. $connectOldDB->disconnect();
  239. $connectNewDB->disconnect();
  240. // Remove Current Database
  241. if (file_exists($GLOBALS['dbLocation'] . $migrationDB)) {
  242. copy($GLOBALS['dbLocation'] . $migrationDB, $GLOBALS['dbLocation'] . $GLOBALS['dbName']);
  243. @unlink($GLOBALS['dbLocation'] . $migrationDB);
  244. }
  245. writeLog('success', 'Update Function - Migrated Old Info to new Database', 'Database');
  246. unlink($tempLock);
  247. return true;
  248. } catch (Dibi\Exception $e) {
  249. writeLog('error', 'Update Function - Error [' . $e . ']', 'Database');
  250. unlink($tempLock);
  251. return false;
  252. }
  253. }
  254. }
  255. function createFirstAdmin($path, $filename, $username, $password, $email)
  256. {
  257. try {
  258. $createDB = new Dibi\Connection([
  259. 'driver' => 'sqlite3',
  260. 'database' => $path . $filename,
  261. ]);
  262. $userInfo = [
  263. 'username' => $username,
  264. 'password' => password_hash($password, PASSWORD_BCRYPT),
  265. 'email' => $email,
  266. 'group' => 'Admin',
  267. 'group_id' => 0,
  268. 'image' => gravatar($email),
  269. 'register_date' => $GLOBALS['currentTime'],
  270. ];
  271. $groupInfo0 = [
  272. 'group' => 'Admin',
  273. 'group_id' => 0,
  274. 'default' => false,
  275. 'image' => 'plugins/images/groups/admin.png',
  276. ];
  277. $groupInfo1 = [
  278. 'group' => 'Co-Admin',
  279. 'group_id' => 1,
  280. 'default' => false,
  281. 'image' => 'plugins/images/groups/coadmin.png',
  282. ];
  283. $groupInfo2 = [
  284. 'group' => 'Super User',
  285. 'group_id' => 2,
  286. 'default' => false,
  287. 'image' => 'plugins/images/groups/superuser.png',
  288. ];
  289. $groupInfo3 = [
  290. 'group' => 'Power User',
  291. 'group_id' => 3,
  292. 'default' => false,
  293. 'image' => 'plugins/images/groups/poweruser.png',
  294. ];
  295. $groupInfo4 = [
  296. 'group' => 'User',
  297. 'group_id' => 4,
  298. 'default' => true,
  299. 'image' => 'plugins/images/groups/user.png',
  300. ];
  301. $groupInfoGuest = [
  302. 'group' => 'Guest',
  303. 'group_id' => 999,
  304. 'default' => false,
  305. 'image' => 'plugins/images/groups/guest.png',
  306. ];
  307. $settingsInfo = [
  308. 'order' => 1,
  309. 'category_id' => 0,
  310. 'name' => 'Settings',
  311. 'url' => 'api/?v1/settings/page',
  312. 'default' => false,
  313. 'enabled' => true,
  314. 'group_id' => 1,
  315. 'image' => 'fontawesome::cog',
  316. 'type' => 0
  317. ];
  318. $homepageInfo = [
  319. 'order' => 2,
  320. 'category_id' => 0,
  321. 'name' => 'Homepage',
  322. 'url' => 'api/?v1/homepage/page',
  323. 'default' => false,
  324. 'enabled' => false,
  325. 'group_id' => 4,
  326. 'image' => 'fontawesome::home',
  327. 'type' => 0
  328. ];
  329. $unsortedInfo = [
  330. 'order' => 1,
  331. 'category' => 'Unsorted',
  332. 'category_id' => 0,
  333. 'image' => 'plugins/images/categories/unsorted.png',
  334. 'default' => true
  335. ];
  336. $createDB->query('INSERT INTO [users]', $userInfo);
  337. $createDB->query('INSERT INTO [groups]', $groupInfo0);
  338. $createDB->query('INSERT INTO [groups]', $groupInfo1);
  339. $createDB->query('INSERT INTO [groups]', $groupInfo2);
  340. $createDB->query('INSERT INTO [groups]', $groupInfo3);
  341. $createDB->query('INSERT INTO [groups]', $groupInfo4);
  342. $createDB->query('INSERT INTO [groups]', $groupInfoGuest);
  343. $createDB->query('INSERT INTO [tabs]', $settingsInfo);
  344. $createDB->query('INSERT INTO [tabs]', $homepageInfo);
  345. $createDB->query('INSERT INTO [categories]', $unsortedInfo);
  346. return true;
  347. } catch (Dibi\Exception $e) {
  348. writeLog('error', 'Wizard Function - Error [' . $e . ']', 'Wizard');
  349. return false;
  350. }
  351. }
  352. function defaultUserGroup()
  353. {
  354. try {
  355. $connect = new Dibi\Connection([
  356. 'driver' => 'sqlite3',
  357. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  358. ]);
  359. $all = $connect->fetch('SELECT * FROM groups WHERE `default` = 1');
  360. return $all;
  361. } catch (Dibi\Exception $e) {
  362. return false;
  363. }
  364. }
  365. function defaultTabCategory()
  366. {
  367. try {
  368. $connect = new Dibi\Connection([
  369. 'driver' => 'sqlite3',
  370. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  371. ]);
  372. $all = $connect->fetch('SELECT * FROM categories WHERE `default` = 1');
  373. return $all;
  374. } catch (Dibi\Exception $e) {
  375. return false;
  376. }
  377. }
  378. function getGuest()
  379. {
  380. if (isset($GLOBALS['dbLocation'])) {
  381. try {
  382. $connect = new Dibi\Connection([
  383. 'driver' => 'sqlite3',
  384. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  385. ]);
  386. $all = $connect->fetch('SELECT * FROM groups WHERE `group` = "Guest"');
  387. return $all;
  388. } catch (Dibi\Exception $e) {
  389. return false;
  390. }
  391. } else {
  392. return array(
  393. 'group' => 'Guest',
  394. 'group_id' => 999,
  395. 'image' => 'plugins/images/groups/guest.png'
  396. );
  397. }
  398. }
  399. function adminEditGroup($array)
  400. {
  401. switch ($array['data']['action']) {
  402. case 'changeDefaultGroup':
  403. try {
  404. $connect = new Dibi\Connection([
  405. 'driver' => 'sqlite3',
  406. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  407. ]);
  408. $connect->query('UPDATE groups SET `default` = 0');
  409. $connect->query('
  410. UPDATE groups SET', [
  411. 'default' => 1
  412. ], '
  413. WHERE id=?', $array['data']['id']);
  414. writeLog('success', 'Group Management Function - Changed Default Group from [' . $array['data']['oldGroupName'] . '] to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  415. return true;
  416. } catch (Dibi\Exception $e) {
  417. return false;
  418. }
  419. break;
  420. case 'deleteUserGroup':
  421. try {
  422. $connect = new Dibi\Connection([
  423. 'driver' => 'sqlite3',
  424. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  425. ]);
  426. $connect->query('DELETE FROM groups WHERE id = ?', $array['data']['id']);
  427. writeLog('success', 'Group Management Function - Deleted Group [' . $array['data']['groupName'] . ']', $GLOBALS['organizrUser']['username']);
  428. return true;
  429. } catch (Dibi\Exception $e) {
  430. return false;
  431. }
  432. break;
  433. case 'addUserGroup':
  434. try {
  435. $connect = new Dibi\Connection([
  436. 'driver' => 'sqlite3',
  437. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  438. ]);
  439. $newGroup = [
  440. 'group' => $array['data']['newGroupName'],
  441. 'group_id' => $array['data']['newGroupID'],
  442. 'default' => false,
  443. 'image' => $array['data']['newGroupImage'],
  444. ];
  445. $connect->query('INSERT INTO [groups]', $newGroup);
  446. writeLog('success', 'Group Management Function - Added Group [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  447. return true;
  448. } catch (Dibi\Exception $e) {
  449. return false;
  450. }
  451. break;
  452. case 'editUserGroup':
  453. try {
  454. $connect = new Dibi\Connection([
  455. 'driver' => 'sqlite3',
  456. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  457. ]);
  458. $connect->query('
  459. UPDATE groups SET', [
  460. 'group' => $array['data']['groupName'],
  461. 'image' => $array['data']['groupImage'],
  462. ], '
  463. WHERE id=?', $array['data']['id']);
  464. writeLog('success', 'Group Management Function - Edited Group Info for [' . $array['data']['oldGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  465. return true;
  466. } catch (Dibi\Exception $e) {
  467. return false;
  468. }
  469. break;
  470. default:
  471. return false;
  472. break;
  473. }
  474. }
  475. function adminEditUser($array)
  476. {
  477. switch ($array['data']['action']) {
  478. case 'changeGroup':
  479. try {
  480. $connect = new Dibi\Connection([
  481. 'driver' => 'sqlite3',
  482. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  483. ]);
  484. $connect->query('
  485. UPDATE users SET', [
  486. 'group' => $array['data']['newGroupName'],
  487. 'group_id' => $array['data']['newGroupID'],
  488. ], '
  489. WHERE id=?', $array['data']['id']);
  490. writeLog('success', 'User Management Function - User: ' . $array['data']['username'] . '\'s group was changed from [' . $array['data']['oldGroup'] . '] to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  491. return true;
  492. } catch (Dibi\Exception $e) {
  493. writeLog('error', 'User Management Function - Error - User: ' . $array['data']['username'] . '\'s group was changed from [' . $array['data']['oldGroup'] . '] to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  494. return false;
  495. }
  496. break;
  497. case 'editUser':
  498. try {
  499. $connect = new Dibi\Connection([
  500. 'driver' => 'sqlite3',
  501. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  502. ]);
  503. if (!usernameTakenExcept($array['data']['username'], $array['data']['email'], $array['data']['id'])) {
  504. $connect->query('
  505. UPDATE users SET', [
  506. 'username' => $array['data']['username'],
  507. 'email' => $array['data']['email'],
  508. ], '
  509. WHERE id=?', $array['data']['id']);
  510. if (!empty($array['data']['password'])) {
  511. $connect->query('
  512. UPDATE users SET', [
  513. 'password' => password_hash($array['data']['password'], PASSWORD_BCRYPT)
  514. ], '
  515. WHERE id=?', $array['data']['id']);
  516. }
  517. writeLog('success', 'User Management Function - User: ' . $array['data']['username'] . '\'s info was changed', $GLOBALS['organizrUser']['username']);
  518. return true;
  519. } else {
  520. return false;
  521. }
  522. } catch (Dibi\Exception $e) {
  523. writeLog('error', 'User Management Function - Error - User: ' . $array['data']['username'] . '\'s group was changed from [' . $array['data']['oldGroup'] . '] to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  524. return false;
  525. }
  526. break;
  527. case 'addNewUser':
  528. $defaults = defaultUserGroup();
  529. if (createUser($array['data']['username'], $array['data']['password'], $defaults, $array['data']['email'])) {
  530. writeLog('success', 'Create User Function - Account created for [' . $array['data']['username'] . ']', $GLOBALS['organizrUser']['username']);
  531. return true;
  532. } else {
  533. writeLog('error', 'Registration Function - An error occurred', $GLOBALS['organizrUser']['username']);
  534. return 'username taken';
  535. }
  536. break;
  537. case 'deleteUser':
  538. try {
  539. $connect = new Dibi\Connection([
  540. 'driver' => 'sqlite3',
  541. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  542. ]);
  543. $connect->query('DELETE FROM users WHERE id = ?', $array['data']['id']);
  544. writeLog('success', 'User Management Function - Deleted User [' . $array['data']['username'] . ']', $GLOBALS['organizrUser']['username']);
  545. return true;
  546. } catch (Dibi\Exception $e) {
  547. return false;
  548. }
  549. break;
  550. default:
  551. return false;
  552. break;
  553. }
  554. }
  555. function editTabs($array)
  556. {
  557. switch ($array['data']['action']) {
  558. case 'changeGroup':
  559. try {
  560. $connect = new Dibi\Connection([
  561. 'driver' => 'sqlite3',
  562. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  563. ]);
  564. $connect->query('
  565. UPDATE tabs SET', [
  566. 'group_id' => $array['data']['newGroupID'],
  567. ], '
  568. WHERE id=?', $array['data']['id']);
  569. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s group was changed to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  570. return true;
  571. } catch (Dibi\Exception $e) {
  572. return false;
  573. }
  574. break;
  575. case 'changeCategory':
  576. try {
  577. $connect = new Dibi\Connection([
  578. 'driver' => 'sqlite3',
  579. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  580. ]);
  581. $connect->query('
  582. UPDATE tabs SET', [
  583. 'category_id' => $array['data']['newCategoryID'],
  584. ], '
  585. WHERE id=?', $array['data']['id']);
  586. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s category was changed to [' . $array['data']['newCategoryName'] . ']', $GLOBALS['organizrUser']['username']);
  587. return true;
  588. } catch (Dibi\Exception $e) {
  589. return false;
  590. }
  591. break;
  592. case 'changeType':
  593. try {
  594. $connect = new Dibi\Connection([
  595. 'driver' => 'sqlite3',
  596. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  597. ]);
  598. $connect->query('
  599. UPDATE tabs SET', [
  600. 'type' => $array['data']['newTypeID'],
  601. ], '
  602. WHERE id=?', $array['data']['id']);
  603. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s type was changed to [' . $array['data']['newTypeName'] . ']', $GLOBALS['organizrUser']['username']);
  604. return true;
  605. } catch (Dibi\Exception $e) {
  606. return false;
  607. }
  608. break;
  609. case 'changeEnabled':
  610. try {
  611. $connect = new Dibi\Connection([
  612. 'driver' => 'sqlite3',
  613. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  614. ]);
  615. $connect->query('
  616. UPDATE tabs SET', [
  617. 'enabled' => $array['data']['tabEnabled'],
  618. ], '
  619. WHERE id=?', $array['data']['id']);
  620. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s enable status was changed to [' . $array['data']['tabEnabledWord'] . ']', $GLOBALS['organizrUser']['username']);
  621. return true;
  622. } catch (Dibi\Exception $e) {
  623. return false;
  624. }
  625. break;
  626. case 'changeSplash':
  627. try {
  628. $connect = new Dibi\Connection([
  629. 'driver' => 'sqlite3',
  630. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  631. ]);
  632. $connect->query('
  633. UPDATE tabs SET', [
  634. 'splash' => $array['data']['tabSplash'],
  635. ], '
  636. WHERE id=?', $array['data']['id']);
  637. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s splash status was changed to [' . $array['data']['tabSplashWord'] . ']', $GLOBALS['organizrUser']['username']);
  638. return true;
  639. } catch (Dibi\Exception $e) {
  640. return false;
  641. }
  642. break;
  643. case 'changePing':
  644. try {
  645. $connect = new Dibi\Connection([
  646. 'driver' => 'sqlite3',
  647. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  648. ]);
  649. $connect->query('
  650. UPDATE tabs SET', [
  651. 'ping' => $array['data']['tabPing'],
  652. ], '
  653. WHERE id=?', $array['data']['id']);
  654. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s ping status was changed to [' . $array['data']['tabPingWord'] . ']', $GLOBALS['organizrUser']['username']);
  655. return true;
  656. } catch (Dibi\Exception $e) {
  657. return false;
  658. }
  659. break;
  660. case 'changeDefault':
  661. try {
  662. $connect = new Dibi\Connection([
  663. 'driver' => 'sqlite3',
  664. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  665. ]);
  666. $connect->query('UPDATE tabs SET `default` = 0');
  667. $connect->query('
  668. UPDATE tabs SET', [
  669. 'default' => 1
  670. ], '
  671. WHERE id=?', $array['data']['id']);
  672. writeLog('success', 'Tab Editor Function - Changed Default Tab to [' . $array['data']['tab'] . ']', $GLOBALS['organizrUser']['username']);
  673. return true;
  674. } catch (Dibi\Exception $e) {
  675. return false;
  676. }
  677. break;
  678. case 'deleteTab':
  679. try {
  680. $connect = new Dibi\Connection([
  681. 'driver' => 'sqlite3',
  682. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  683. ]);
  684. $connect->query('DELETE FROM tabs WHERE id = ?', $array['data']['id']);
  685. writeLog('success', 'Tab Editor Function - Deleted Tab [' . $array['data']['tab'] . ']', $GLOBALS['organizrUser']['username']);
  686. return true;
  687. } catch (Dibi\Exception $e) {
  688. return false;
  689. }
  690. break;
  691. case 'editTab':
  692. try {
  693. $connect = new Dibi\Connection([
  694. 'driver' => 'sqlite3',
  695. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  696. ]);
  697. $connect->query('
  698. UPDATE tabs SET', [
  699. 'name' => $array['data']['tabName'],
  700. 'url' => $array['data']['tabURL'],
  701. 'ping_url' => $array['data']['pingURL'],
  702. 'image' => $array['data']['tabImage'],
  703. ], '
  704. WHERE id=?', $array['data']['id']);
  705. writeLog('success', 'Tab Editor Function - Edited Tab Info for [' . $array['data']['tabName'] . ']', $GLOBALS['organizrUser']['username']);
  706. return true;
  707. } catch (Dibi\Exception $e) {
  708. return false;
  709. }
  710. case 'changeOrder':
  711. try {
  712. $connect = new Dibi\Connection([
  713. 'driver' => 'sqlite3',
  714. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  715. ]);
  716. foreach ($array['data']['tabs']['tab'] as $key => $value) {
  717. if ($value['order'] != $value['originalOrder']) {
  718. $connect->query('
  719. UPDATE tabs SET', [
  720. 'order' => $value['order'],
  721. ], '
  722. WHERE id=?', $value['id']);
  723. writeLog('success', 'Tab Editor Function - ' . $value['name'] . ' Order Changed From ' . $value['order'] . ' to ' . $value['originalOrder'], $GLOBALS['organizrUser']['username']);
  724. }
  725. }
  726. writeLog('success', 'Tab Editor Function - Tab Order Changed', $GLOBALS['organizrUser']['username']);
  727. return true;
  728. } catch (Dibi\Exception $e) {
  729. return false;
  730. }
  731. break;
  732. case 'addNewTab':
  733. try {
  734. $default = defaultTabCategory()['category_id'];
  735. $connect = new Dibi\Connection([
  736. 'driver' => 'sqlite3',
  737. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  738. ]);
  739. $newTab = [
  740. 'order' => $array['data']['tabOrder'],
  741. 'category_id' => $default,
  742. 'name' => $array['data']['tabName'],
  743. 'url' => $array['data']['tabURL'],
  744. 'ping_url' => $array['data']['pingURL'],
  745. 'default' => $array['data']['tabDefault'],
  746. 'enabled' => 1,
  747. 'group_id' => $array['data']['tabGroupID'],
  748. 'image' => $array['data']['tabImage'],
  749. 'type' => $array['data']['tabType']
  750. ];
  751. $connect->query('INSERT INTO [tabs]', $newTab);
  752. writeLog('success', 'Tab Editor Function - Created Tab for: ' . $array['data']['tabName'], $GLOBALS['organizrUser']['username']);
  753. return true;
  754. } catch (Dibi\Exception $e) {
  755. return false;
  756. }
  757. break;
  758. default:
  759. return false;
  760. break;
  761. }
  762. }
  763. function editCategories($array)
  764. {
  765. switch ($array['data']['action']) {
  766. case 'changeDefault':
  767. try {
  768. $connect = new Dibi\Connection([
  769. 'driver' => 'sqlite3',
  770. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  771. ]);
  772. $connect->query('UPDATE categories SET `default` = 0');
  773. $connect->query('
  774. UPDATE categories SET', [
  775. 'default' => 1
  776. ], '
  777. WHERE id=?', $array['data']['id']);
  778. writeLog('success', 'Category Editor Function - Changed Default Category from [' . $array['data']['oldCategoryName'] . '] to [' . $array['data']['newCategoryName'] . ']', $GLOBALS['organizrUser']['username']);
  779. return true;
  780. } catch (Dibi\Exception $e) {
  781. return false;
  782. }
  783. break;
  784. case 'deleteCategory':
  785. try {
  786. $connect = new Dibi\Connection([
  787. 'driver' => 'sqlite3',
  788. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  789. ]);
  790. $connect->query('DELETE FROM categories WHERE id = ?', $array['data']['id']);
  791. writeLog('success', 'Category Editor Function - Deleted Category [' . $array['data']['category'] . ']', $GLOBALS['organizrUser']['username']);
  792. return true;
  793. } catch (Dibi\Exception $e) {
  794. return false;
  795. }
  796. break;
  797. case 'addNewCategory':
  798. try {
  799. $connect = new Dibi\Connection([
  800. 'driver' => 'sqlite3',
  801. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  802. ]);
  803. $newCategory = [
  804. 'category' => $array['data']['categoryName'],
  805. 'order' => $array['data']['categoryOrder'],
  806. 'category_id' => $array['data']['categoryID'],
  807. 'default' => false,
  808. 'image' => $array['data']['categoryImage'],
  809. ];
  810. $connect->query('INSERT INTO [categories]', $newCategory);
  811. writeLog('success', 'Category Editor Function - Added Category [' . $array['data']['categoryName'] . ']', $GLOBALS['organizrUser']['username']);
  812. return true;
  813. } catch (Dibi\Exception $e) {
  814. return $e;
  815. }
  816. break;
  817. case 'editCategory':
  818. try {
  819. $connect = new Dibi\Connection([
  820. 'driver' => 'sqlite3',
  821. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  822. ]);
  823. $connect->query('
  824. UPDATE categories SET', [
  825. 'category' => $array['data']['name'],
  826. 'image' => $array['data']['image'],
  827. ], '
  828. WHERE id=?', $array['data']['id']);
  829. writeLog('success', 'Category Editor Function - Edited Category Info for [' . $array['data']['name'] . ']', $GLOBALS['organizrUser']['username']);
  830. return true;
  831. } catch (Dibi\Exception $e) {
  832. return false;
  833. }
  834. break;
  835. case 'changeOrder':
  836. try {
  837. $connect = new Dibi\Connection([
  838. 'driver' => 'sqlite3',
  839. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  840. ]);
  841. foreach ($array['data']['categories']['category'] as $key => $value) {
  842. if ($value['order'] != $value['originalOrder']) {
  843. $connect->query('
  844. UPDATE categories SET', [
  845. 'order' => $value['order'],
  846. ], '
  847. WHERE id=?', $value['id']);
  848. writeLog('success', 'Category Editor Function - ' . $value['name'] . ' Order Changed From ' . $value['order'] . ' to ' . $value['originalOrder'], $GLOBALS['organizrUser']['username']);
  849. }
  850. }
  851. writeLog('success', 'Category Editor Function - Category Order Changed', $GLOBALS['organizrUser']['username']);
  852. return true;
  853. } catch (Dibi\Exception $e) {
  854. return false;
  855. }
  856. break;
  857. default:
  858. return false;
  859. break;
  860. }
  861. }
  862. function allUsers()
  863. {
  864. try {
  865. $connect = new Dibi\Connection([
  866. 'driver' => 'sqlite3',
  867. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  868. ]);
  869. $users = $connect->fetchAll('SELECT * FROM users');
  870. $groups = $connect->fetchAll('SELECT * FROM groups ORDER BY group_id ASC');
  871. foreach ($users as $k => $v) {
  872. // clear password from array
  873. unset($users[$k]['password']);
  874. }
  875. $all['users'] = $users;
  876. $all['groups'] = $groups;
  877. return $all;
  878. } catch (Dibi\Exception $e) {
  879. return false;
  880. }
  881. }
  882. function usernameTaken($username, $email)
  883. {
  884. try {
  885. $connect = new Dibi\Connection([
  886. 'driver' => 'sqlite3',
  887. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  888. ]);
  889. $all = $connect->fetch('SELECT * FROM users WHERE username = ? COLLATE NOCASE OR email = ? COLLATE NOCASE', $username, $email);
  890. return ($all) ? true : false;
  891. } catch (Dibi\Exception $e) {
  892. return false;
  893. }
  894. }
  895. function usernameTakenExcept($username, $email, $id)
  896. {
  897. try {
  898. $connect = new Dibi\Connection([
  899. 'driver' => 'sqlite3',
  900. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  901. ]);
  902. $all = $connect->fetch('SELECT * FROM users WHERE id IS NOT ? AND username = ? COLLATE NOCASE OR id IS NOT ? AND email = ? COLLATE NOCASE', $id, $username, $id, $email);
  903. return ($all) ? true : false;
  904. } catch (Dibi\Exception $e) {
  905. return false;
  906. }
  907. }
  908. function createUser($username, $password, $defaults, $email = null)
  909. {
  910. $email = ($email) ? $email : random_ascii_string(10) . '@placeholder.eml';
  911. try {
  912. if (!usernameTaken($username, $email)) {
  913. $createDB = new Dibi\Connection([
  914. 'driver' => 'sqlite3',
  915. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  916. ]);
  917. $userInfo = [
  918. 'username' => $username,
  919. 'password' => password_hash($password, PASSWORD_BCRYPT),
  920. 'email' => $email,
  921. 'group' => $defaults['group'],
  922. 'group_id' => $defaults['group_id'],
  923. 'image' => gravatar($email),
  924. 'register_date' => $GLOBALS['currentTime'],
  925. ];
  926. $createDB->query('INSERT INTO [users]', $userInfo);
  927. return true;
  928. } else {
  929. return false;
  930. }
  931. } catch (Dibi\Exception $e) {
  932. return false;
  933. }
  934. }
  935. function allTabs()
  936. {
  937. if (file_exists('config' . DIRECTORY_SEPARATOR . 'config.php')) {
  938. try {
  939. $connect = new Dibi\Connection([
  940. 'driver' => 'sqlite3',
  941. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  942. ]);
  943. $all['tabs'] = $connect->fetchAll('SELECT * FROM tabs ORDER BY `order` ASC');
  944. $all['categories'] = $connect->fetchAll('SELECT * FROM categories ORDER BY `order` ASC');
  945. $all['groups'] = $connect->fetchAll('SELECT * FROM groups ORDER BY `group_id` ASC');
  946. return $all;
  947. } catch (Dibi\Exception $e) {
  948. return false;
  949. }
  950. }
  951. return false;
  952. }
  953. function allGroups()
  954. {
  955. if (file_exists('config' . DIRECTORY_SEPARATOR . 'config.php')) {
  956. try {
  957. $connect = new Dibi\Connection([
  958. 'driver' => 'sqlite3',
  959. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  960. ]);
  961. $all = $connect->fetchAll('SELECT * FROM groups ORDER BY `group_id` ASC');
  962. return $all;
  963. } catch (Dibi\Exception $e) {
  964. return false;
  965. }
  966. }
  967. return false;
  968. }
  969. function loadTabs()
  970. {
  971. if (file_exists('config' . DIRECTORY_SEPARATOR . 'config.php')) {
  972. try {
  973. $connect = new Dibi\Connection([
  974. 'driver' => 'sqlite3',
  975. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  976. ]);
  977. $sort = ($GLOBALS['unsortedTabs'] == 'top') ? 'DESC' : 'ASC';
  978. $tabs = $connect->fetchAll('SELECT * FROM tabs WHERE `group_id` >= ? AND `enabled` = 1 ORDER BY `order` ' . $sort, $GLOBALS['organizrUser']['groupID']);
  979. $categories = $connect->fetchAll('SELECT * FROM categories ORDER BY `order` ASC');
  980. $all['tabs'] = $tabs;
  981. foreach ($tabs as $k => $v) {
  982. $v['access_url'] = isset($v['url_local']) && getenv('SERVER_ADDR') == userIP() ? $v['url_local'] : $v['url'];
  983. }
  984. $count = array_map(function ($element) {
  985. return $element['category_id'];
  986. }, $tabs);
  987. $count = (array_count_values($count));
  988. foreach ($categories as $k => $v) {
  989. $v['count'] = isset($count[$v['category_id']]) ? $count[$v['category_id']] : 0;
  990. }
  991. $all['categories'] = $categories;
  992. return $all;
  993. } catch (Dibi\Exception $e) {
  994. return false;
  995. }
  996. }
  997. return false;
  998. }