api-functions.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. $backupDB = $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db';
  220. copy($GLOBALS['dbLocation'] . $GLOBALS['dbName'], $backupDB);
  221. $success = createDB($GLOBALS['dbLocation'], $migrationDB);
  222. if ($success) {
  223. try {
  224. $connectOldDB = new Dibi\Connection([
  225. 'driver' => 'sqlite3',
  226. 'database' => $backupDB,
  227. ]);
  228. $connectNewDB = new Dibi\Connection([
  229. 'driver' => 'sqlite3',
  230. 'database' => $GLOBALS['dbLocation'] . $migrationDB,
  231. ]);
  232. $tables = $connectOldDB->fetchAll('SELECT name FROM sqlite_master WHERE type="table"');
  233. foreach ($tables as $table) {
  234. $data = $connectOldDB->fetchAll('SELECT * FROM ' . $table['name']);
  235. foreach ($data as $row) {
  236. $connectNewDB->query('INSERT into ' . $table['name'], $row);
  237. }
  238. }
  239. $connectOldDB->disconnect();
  240. $connectNewDB->disconnect();
  241. // Remove Current Database
  242. if (file_exists($GLOBALS['dbLocation'] . $migrationDB)) {
  243. $oldFileSize = filesize($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
  244. $newFileSize = filesize($GLOBALS['dbLocation'] . $migrationDB);
  245. if ($newFileSize >= $oldFileSize) {
  246. @unlink($GLOBALS['dbLocation'] . $GLOBALS['dbName']);
  247. copy($GLOBALS['dbLocation'] . $migrationDB, $GLOBALS['dbLocation'] . $GLOBALS['dbName']);
  248. @unlink($GLOBALS['dbLocation'] . $migrationDB);
  249. writeLog('success', 'Update Function - Migrated Old Info to new Database', 'Database');
  250. unlink($tempLock);
  251. return true;
  252. }
  253. }
  254. unlink($tempLock);
  255. return false;
  256. } catch (Dibi\Exception $e) {
  257. writeLog('error', 'Update Function - Error [' . $e . ']', 'Database');
  258. unlink($tempLock);
  259. return false;
  260. }
  261. }
  262. unlink($tempLock);
  263. return false;
  264. }
  265. return false;
  266. }
  267. function createFirstAdmin($path, $filename, $username, $password, $email)
  268. {
  269. try {
  270. $createDB = new Dibi\Connection([
  271. 'driver' => 'sqlite3',
  272. 'database' => $path . $filename,
  273. ]);
  274. $userInfo = [
  275. 'username' => $username,
  276. 'password' => password_hash($password, PASSWORD_BCRYPT),
  277. 'email' => $email,
  278. 'group' => 'Admin',
  279. 'group_id' => 0,
  280. 'image' => gravatar($email),
  281. 'register_date' => $GLOBALS['currentTime'],
  282. ];
  283. $groupInfo0 = [
  284. 'group' => 'Admin',
  285. 'group_id' => 0,
  286. 'default' => false,
  287. 'image' => 'plugins/images/groups/admin.png',
  288. ];
  289. $groupInfo1 = [
  290. 'group' => 'Co-Admin',
  291. 'group_id' => 1,
  292. 'default' => false,
  293. 'image' => 'plugins/images/groups/coadmin.png',
  294. ];
  295. $groupInfo2 = [
  296. 'group' => 'Super User',
  297. 'group_id' => 2,
  298. 'default' => false,
  299. 'image' => 'plugins/images/groups/superuser.png',
  300. ];
  301. $groupInfo3 = [
  302. 'group' => 'Power User',
  303. 'group_id' => 3,
  304. 'default' => false,
  305. 'image' => 'plugins/images/groups/poweruser.png',
  306. ];
  307. $groupInfo4 = [
  308. 'group' => 'User',
  309. 'group_id' => 4,
  310. 'default' => true,
  311. 'image' => 'plugins/images/groups/user.png',
  312. ];
  313. $groupInfoGuest = [
  314. 'group' => 'Guest',
  315. 'group_id' => 999,
  316. 'default' => false,
  317. 'image' => 'plugins/images/groups/guest.png',
  318. ];
  319. $settingsInfo = [
  320. 'order' => 1,
  321. 'category_id' => 0,
  322. 'name' => 'Settings',
  323. 'url' => 'api/?v1/settings/page',
  324. 'default' => false,
  325. 'enabled' => true,
  326. 'group_id' => 1,
  327. 'image' => 'fontawesome::cog',
  328. 'type' => 0
  329. ];
  330. $homepageInfo = [
  331. 'order' => 2,
  332. 'category_id' => 0,
  333. 'name' => 'Homepage',
  334. 'url' => 'api/?v1/homepage/page',
  335. 'default' => false,
  336. 'enabled' => false,
  337. 'group_id' => 4,
  338. 'image' => 'fontawesome::home',
  339. 'type' => 0
  340. ];
  341. $unsortedInfo = [
  342. 'order' => 1,
  343. 'category' => 'Unsorted',
  344. 'category_id' => 0,
  345. 'image' => 'plugins/images/categories/unsorted.png',
  346. 'default' => true
  347. ];
  348. $createDB->query('INSERT INTO [users]', $userInfo);
  349. $createDB->query('INSERT INTO [groups]', $groupInfo0);
  350. $createDB->query('INSERT INTO [groups]', $groupInfo1);
  351. $createDB->query('INSERT INTO [groups]', $groupInfo2);
  352. $createDB->query('INSERT INTO [groups]', $groupInfo3);
  353. $createDB->query('INSERT INTO [groups]', $groupInfo4);
  354. $createDB->query('INSERT INTO [groups]', $groupInfoGuest);
  355. $createDB->query('INSERT INTO [tabs]', $settingsInfo);
  356. $createDB->query('INSERT INTO [tabs]', $homepageInfo);
  357. $createDB->query('INSERT INTO [categories]', $unsortedInfo);
  358. return true;
  359. } catch (Dibi\Exception $e) {
  360. writeLog('error', 'Wizard Function - Error [' . $e . ']', 'Wizard');
  361. return false;
  362. }
  363. }
  364. function defaultUserGroup()
  365. {
  366. try {
  367. $connect = new Dibi\Connection([
  368. 'driver' => 'sqlite3',
  369. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  370. ]);
  371. $all = $connect->fetch('SELECT * FROM groups WHERE `default` = 1');
  372. return $all;
  373. } catch (Dibi\Exception $e) {
  374. return false;
  375. }
  376. }
  377. function defaultTabCategory()
  378. {
  379. try {
  380. $connect = new Dibi\Connection([
  381. 'driver' => 'sqlite3',
  382. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  383. ]);
  384. $all = $connect->fetch('SELECT * FROM categories WHERE `default` = 1');
  385. return $all;
  386. } catch (Dibi\Exception $e) {
  387. return false;
  388. }
  389. }
  390. function getGuest()
  391. {
  392. if (isset($GLOBALS['dbLocation'])) {
  393. try {
  394. $connect = new Dibi\Connection([
  395. 'driver' => 'sqlite3',
  396. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  397. ]);
  398. $all = $connect->fetch('SELECT * FROM groups WHERE `group` = "Guest"');
  399. return $all;
  400. } catch (Dibi\Exception $e) {
  401. return false;
  402. }
  403. } else {
  404. return array(
  405. 'group' => 'Guest',
  406. 'group_id' => 999,
  407. 'image' => 'plugins/images/groups/guest.png'
  408. );
  409. }
  410. }
  411. function adminEditGroup($array)
  412. {
  413. switch ($array['data']['action']) {
  414. case 'changeDefaultGroup':
  415. try {
  416. $connect = new Dibi\Connection([
  417. 'driver' => 'sqlite3',
  418. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  419. ]);
  420. $connect->query('UPDATE groups SET `default` = 0');
  421. $connect->query('
  422. UPDATE groups SET', [
  423. 'default' => 1
  424. ], '
  425. WHERE id=?', $array['data']['id']);
  426. writeLog('success', 'Group Management Function - Changed Default Group from [' . $array['data']['oldGroupName'] . '] to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  427. return true;
  428. } catch (Dibi\Exception $e) {
  429. return false;
  430. }
  431. break;
  432. case 'deleteUserGroup':
  433. try {
  434. $connect = new Dibi\Connection([
  435. 'driver' => 'sqlite3',
  436. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  437. ]);
  438. $connect->query('DELETE FROM groups WHERE id = ?', $array['data']['id']);
  439. writeLog('success', 'Group Management Function - Deleted Group [' . $array['data']['groupName'] . ']', $GLOBALS['organizrUser']['username']);
  440. return true;
  441. } catch (Dibi\Exception $e) {
  442. return false;
  443. }
  444. break;
  445. case 'addUserGroup':
  446. try {
  447. $connect = new Dibi\Connection([
  448. 'driver' => 'sqlite3',
  449. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  450. ]);
  451. $newGroup = [
  452. 'group' => $array['data']['newGroupName'],
  453. 'group_id' => $array['data']['newGroupID'],
  454. 'default' => false,
  455. 'image' => $array['data']['newGroupImage'],
  456. ];
  457. $connect->query('INSERT INTO [groups]', $newGroup);
  458. writeLog('success', 'Group Management Function - Added Group [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  459. return true;
  460. } catch (Dibi\Exception $e) {
  461. return false;
  462. }
  463. break;
  464. case 'editUserGroup':
  465. try {
  466. $connect = new Dibi\Connection([
  467. 'driver' => 'sqlite3',
  468. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  469. ]);
  470. $connect->query('
  471. UPDATE groups SET', [
  472. 'group' => $array['data']['groupName'],
  473. 'image' => $array['data']['groupImage'],
  474. ], '
  475. WHERE id=?', $array['data']['id']);
  476. writeLog('success', 'Group Management Function - Edited Group Info for [' . $array['data']['oldGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  477. return true;
  478. } catch (Dibi\Exception $e) {
  479. return false;
  480. }
  481. break;
  482. default:
  483. return false;
  484. break;
  485. }
  486. }
  487. function adminEditUser($array)
  488. {
  489. switch ($array['data']['action']) {
  490. case 'changeGroup':
  491. try {
  492. $connect = new Dibi\Connection([
  493. 'driver' => 'sqlite3',
  494. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  495. ]);
  496. $connect->query('
  497. UPDATE users SET', [
  498. 'group' => $array['data']['newGroupName'],
  499. 'group_id' => $array['data']['newGroupID'],
  500. ], '
  501. WHERE id=?', $array['data']['id']);
  502. writeLog('success', 'User Management Function - User: ' . $array['data']['username'] . '\'s group was changed from [' . $array['data']['oldGroup'] . '] to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  503. return true;
  504. } catch (Dibi\Exception $e) {
  505. 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']);
  506. return false;
  507. }
  508. break;
  509. case 'editUser':
  510. try {
  511. $connect = new Dibi\Connection([
  512. 'driver' => 'sqlite3',
  513. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  514. ]);
  515. if (!usernameTakenExcept($array['data']['username'], $array['data']['email'], $array['data']['id'])) {
  516. $connect->query('
  517. UPDATE users SET', [
  518. 'username' => $array['data']['username'],
  519. 'email' => $array['data']['email'],
  520. ], '
  521. WHERE id=?', $array['data']['id']);
  522. if (!empty($array['data']['password'])) {
  523. $connect->query('
  524. UPDATE users SET', [
  525. 'password' => password_hash($array['data']['password'], PASSWORD_BCRYPT)
  526. ], '
  527. WHERE id=?', $array['data']['id']);
  528. }
  529. writeLog('success', 'User Management Function - User: ' . $array['data']['username'] . '\'s info was changed', $GLOBALS['organizrUser']['username']);
  530. return true;
  531. } else {
  532. return false;
  533. }
  534. } catch (Dibi\Exception $e) {
  535. 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']);
  536. return false;
  537. }
  538. break;
  539. case 'addNewUser':
  540. $defaults = defaultUserGroup();
  541. if (createUser($array['data']['username'], $array['data']['password'], $defaults, $array['data']['email'])) {
  542. writeLog('success', 'Create User Function - Account created for [' . $array['data']['username'] . ']', $GLOBALS['organizrUser']['username']);
  543. return true;
  544. } else {
  545. writeLog('error', 'Registration Function - An error occurred', $GLOBALS['organizrUser']['username']);
  546. return 'username taken';
  547. }
  548. break;
  549. case 'deleteUser':
  550. try {
  551. $connect = new Dibi\Connection([
  552. 'driver' => 'sqlite3',
  553. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  554. ]);
  555. $connect->query('DELETE FROM users WHERE id = ?', $array['data']['id']);
  556. writeLog('success', 'User Management Function - Deleted User [' . $array['data']['username'] . ']', $GLOBALS['organizrUser']['username']);
  557. return true;
  558. } catch (Dibi\Exception $e) {
  559. return false;
  560. }
  561. break;
  562. default:
  563. return false;
  564. break;
  565. }
  566. }
  567. function editTabs($array)
  568. {
  569. switch ($array['data']['action']) {
  570. case 'changeGroup':
  571. try {
  572. $connect = new Dibi\Connection([
  573. 'driver' => 'sqlite3',
  574. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  575. ]);
  576. $connect->query('
  577. UPDATE tabs SET', [
  578. 'group_id' => $array['data']['newGroupID'],
  579. ], '
  580. WHERE id=?', $array['data']['id']);
  581. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s group was changed to [' . $array['data']['newGroupName'] . ']', $GLOBALS['organizrUser']['username']);
  582. return true;
  583. } catch (Dibi\Exception $e) {
  584. return false;
  585. }
  586. break;
  587. case 'changeCategory':
  588. try {
  589. $connect = new Dibi\Connection([
  590. 'driver' => 'sqlite3',
  591. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  592. ]);
  593. $connect->query('
  594. UPDATE tabs SET', [
  595. 'category_id' => $array['data']['newCategoryID'],
  596. ], '
  597. WHERE id=?', $array['data']['id']);
  598. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s category was changed to [' . $array['data']['newCategoryName'] . ']', $GLOBALS['organizrUser']['username']);
  599. return true;
  600. } catch (Dibi\Exception $e) {
  601. return false;
  602. }
  603. break;
  604. case 'changeType':
  605. try {
  606. $connect = new Dibi\Connection([
  607. 'driver' => 'sqlite3',
  608. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  609. ]);
  610. $connect->query('
  611. UPDATE tabs SET', [
  612. 'type' => $array['data']['newTypeID'],
  613. ], '
  614. WHERE id=?', $array['data']['id']);
  615. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s type was changed to [' . $array['data']['newTypeName'] . ']', $GLOBALS['organizrUser']['username']);
  616. return true;
  617. } catch (Dibi\Exception $e) {
  618. return false;
  619. }
  620. break;
  621. case 'changeEnabled':
  622. try {
  623. $connect = new Dibi\Connection([
  624. 'driver' => 'sqlite3',
  625. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  626. ]);
  627. $connect->query('
  628. UPDATE tabs SET', [
  629. 'enabled' => $array['data']['tabEnabled'],
  630. ], '
  631. WHERE id=?', $array['data']['id']);
  632. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s enable status was changed to [' . $array['data']['tabEnabledWord'] . ']', $GLOBALS['organizrUser']['username']);
  633. return true;
  634. } catch (Dibi\Exception $e) {
  635. return false;
  636. }
  637. break;
  638. case 'changeSplash':
  639. try {
  640. $connect = new Dibi\Connection([
  641. 'driver' => 'sqlite3',
  642. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  643. ]);
  644. $connect->query('
  645. UPDATE tabs SET', [
  646. 'splash' => $array['data']['tabSplash'],
  647. ], '
  648. WHERE id=?', $array['data']['id']);
  649. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s splash status was changed to [' . $array['data']['tabSplashWord'] . ']', $GLOBALS['organizrUser']['username']);
  650. return true;
  651. } catch (Dibi\Exception $e) {
  652. return false;
  653. }
  654. break;
  655. case 'changePing':
  656. try {
  657. $connect = new Dibi\Connection([
  658. 'driver' => 'sqlite3',
  659. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  660. ]);
  661. $connect->query('
  662. UPDATE tabs SET', [
  663. 'ping' => $array['data']['tabPing'],
  664. ], '
  665. WHERE id=?', $array['data']['id']);
  666. writeLog('success', 'Tab Editor Function - Tab: ' . $array['data']['tab'] . '\'s ping status was changed to [' . $array['data']['tabPingWord'] . ']', $GLOBALS['organizrUser']['username']);
  667. return true;
  668. } catch (Dibi\Exception $e) {
  669. return false;
  670. }
  671. break;
  672. case 'changeDefault':
  673. try {
  674. $connect = new Dibi\Connection([
  675. 'driver' => 'sqlite3',
  676. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  677. ]);
  678. $connect->query('UPDATE tabs SET `default` = 0');
  679. $connect->query('
  680. UPDATE tabs SET', [
  681. 'default' => 1
  682. ], '
  683. WHERE id=?', $array['data']['id']);
  684. writeLog('success', 'Tab Editor Function - Changed Default Tab to [' . $array['data']['tab'] . ']', $GLOBALS['organizrUser']['username']);
  685. return true;
  686. } catch (Dibi\Exception $e) {
  687. return false;
  688. }
  689. break;
  690. case 'deleteTab':
  691. try {
  692. $connect = new Dibi\Connection([
  693. 'driver' => 'sqlite3',
  694. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  695. ]);
  696. $connect->query('DELETE FROM tabs WHERE id = ?', $array['data']['id']);
  697. writeLog('success', 'Tab Editor Function - Deleted Tab [' . $array['data']['tab'] . ']', $GLOBALS['organizrUser']['username']);
  698. return true;
  699. } catch (Dibi\Exception $e) {
  700. return false;
  701. }
  702. break;
  703. case 'editTab':
  704. try {
  705. $connect = new Dibi\Connection([
  706. 'driver' => 'sqlite3',
  707. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  708. ]);
  709. $connect->query('
  710. UPDATE tabs SET', [
  711. 'name' => $array['data']['tabName'],
  712. 'url' => $array['data']['tabURL'],
  713. 'ping_url' => $array['data']['pingURL'],
  714. 'image' => $array['data']['tabImage'],
  715. ], '
  716. WHERE id=?', $array['data']['id']);
  717. writeLog('success', 'Tab Editor Function - Edited Tab Info for [' . $array['data']['tabName'] . ']', $GLOBALS['organizrUser']['username']);
  718. return true;
  719. } catch (Dibi\Exception $e) {
  720. return false;
  721. }
  722. case 'changeOrder':
  723. try {
  724. $connect = new Dibi\Connection([
  725. 'driver' => 'sqlite3',
  726. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  727. ]);
  728. foreach ($array['data']['tabs']['tab'] as $key => $value) {
  729. if ($value['order'] != $value['originalOrder']) {
  730. $connect->query('
  731. UPDATE tabs SET', [
  732. 'order' => $value['order'],
  733. ], '
  734. WHERE id=?', $value['id']);
  735. writeLog('success', 'Tab Editor Function - ' . $value['name'] . ' Order Changed From ' . $value['order'] . ' to ' . $value['originalOrder'], $GLOBALS['organizrUser']['username']);
  736. }
  737. }
  738. writeLog('success', 'Tab Editor Function - Tab Order Changed', $GLOBALS['organizrUser']['username']);
  739. return true;
  740. } catch (Dibi\Exception $e) {
  741. return false;
  742. }
  743. break;
  744. case 'addNewTab':
  745. try {
  746. $default = defaultTabCategory()['category_id'];
  747. $connect = new Dibi\Connection([
  748. 'driver' => 'sqlite3',
  749. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  750. ]);
  751. $newTab = [
  752. 'order' => $array['data']['tabOrder'],
  753. 'category_id' => $default,
  754. 'name' => $array['data']['tabName'],
  755. 'url' => $array['data']['tabURL'],
  756. 'ping_url' => $array['data']['pingURL'],
  757. 'default' => $array['data']['tabDefault'],
  758. 'enabled' => 1,
  759. 'group_id' => $array['data']['tabGroupID'],
  760. 'image' => $array['data']['tabImage'],
  761. 'type' => $array['data']['tabType']
  762. ];
  763. $connect->query('INSERT INTO [tabs]', $newTab);
  764. writeLog('success', 'Tab Editor Function - Created Tab for: ' . $array['data']['tabName'], $GLOBALS['organizrUser']['username']);
  765. return true;
  766. } catch (Dibi\Exception $e) {
  767. return false;
  768. }
  769. break;
  770. default:
  771. return false;
  772. break;
  773. }
  774. }
  775. function editCategories($array)
  776. {
  777. switch ($array['data']['action']) {
  778. case 'changeDefault':
  779. try {
  780. $connect = new Dibi\Connection([
  781. 'driver' => 'sqlite3',
  782. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  783. ]);
  784. $connect->query('UPDATE categories SET `default` = 0');
  785. $connect->query('
  786. UPDATE categories SET', [
  787. 'default' => 1
  788. ], '
  789. WHERE id=?', $array['data']['id']);
  790. writeLog('success', 'Category Editor Function - Changed Default Category from [' . $array['data']['oldCategoryName'] . '] to [' . $array['data']['newCategoryName'] . ']', $GLOBALS['organizrUser']['username']);
  791. return true;
  792. } catch (Dibi\Exception $e) {
  793. return false;
  794. }
  795. break;
  796. case 'deleteCategory':
  797. try {
  798. $connect = new Dibi\Connection([
  799. 'driver' => 'sqlite3',
  800. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  801. ]);
  802. $connect->query('DELETE FROM categories WHERE id = ?', $array['data']['id']);
  803. writeLog('success', 'Category Editor Function - Deleted Category [' . $array['data']['category'] . ']', $GLOBALS['organizrUser']['username']);
  804. return true;
  805. } catch (Dibi\Exception $e) {
  806. return false;
  807. }
  808. break;
  809. case 'addNewCategory':
  810. try {
  811. $connect = new Dibi\Connection([
  812. 'driver' => 'sqlite3',
  813. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  814. ]);
  815. $newCategory = [
  816. 'category' => $array['data']['categoryName'],
  817. 'order' => $array['data']['categoryOrder'],
  818. 'category_id' => $array['data']['categoryID'],
  819. 'default' => false,
  820. 'image' => $array['data']['categoryImage'],
  821. ];
  822. $connect->query('INSERT INTO [categories]', $newCategory);
  823. writeLog('success', 'Category Editor Function - Added Category [' . $array['data']['categoryName'] . ']', $GLOBALS['organizrUser']['username']);
  824. return true;
  825. } catch (Dibi\Exception $e) {
  826. return $e;
  827. }
  828. break;
  829. case 'editCategory':
  830. try {
  831. $connect = new Dibi\Connection([
  832. 'driver' => 'sqlite3',
  833. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  834. ]);
  835. $connect->query('
  836. UPDATE categories SET', [
  837. 'category' => $array['data']['name'],
  838. 'image' => $array['data']['image'],
  839. ], '
  840. WHERE id=?', $array['data']['id']);
  841. writeLog('success', 'Category Editor Function - Edited Category Info for [' . $array['data']['name'] . ']', $GLOBALS['organizrUser']['username']);
  842. return true;
  843. } catch (Dibi\Exception $e) {
  844. return false;
  845. }
  846. break;
  847. case 'changeOrder':
  848. try {
  849. $connect = new Dibi\Connection([
  850. 'driver' => 'sqlite3',
  851. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  852. ]);
  853. foreach ($array['data']['categories']['category'] as $key => $value) {
  854. if ($value['order'] != $value['originalOrder']) {
  855. $connect->query('
  856. UPDATE categories SET', [
  857. 'order' => $value['order'],
  858. ], '
  859. WHERE id=?', $value['id']);
  860. writeLog('success', 'Category Editor Function - ' . $value['name'] . ' Order Changed From ' . $value['order'] . ' to ' . $value['originalOrder'], $GLOBALS['organizrUser']['username']);
  861. }
  862. }
  863. writeLog('success', 'Category Editor Function - Category Order Changed', $GLOBALS['organizrUser']['username']);
  864. return true;
  865. } catch (Dibi\Exception $e) {
  866. return false;
  867. }
  868. break;
  869. default:
  870. return false;
  871. break;
  872. }
  873. }
  874. function allUsers()
  875. {
  876. try {
  877. $connect = new Dibi\Connection([
  878. 'driver' => 'sqlite3',
  879. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  880. ]);
  881. $users = $connect->fetchAll('SELECT * FROM users');
  882. $groups = $connect->fetchAll('SELECT * FROM groups ORDER BY group_id ASC');
  883. foreach ($users as $k => $v) {
  884. // clear password from array
  885. unset($users[$k]['password']);
  886. }
  887. $all['users'] = $users;
  888. $all['groups'] = $groups;
  889. return $all;
  890. } catch (Dibi\Exception $e) {
  891. return false;
  892. }
  893. }
  894. function usernameTaken($username, $email)
  895. {
  896. try {
  897. $connect = new Dibi\Connection([
  898. 'driver' => 'sqlite3',
  899. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  900. ]);
  901. $all = $connect->fetch('SELECT * FROM users WHERE username = ? COLLATE NOCASE OR email = ? COLLATE NOCASE', $username, $email);
  902. return ($all) ? true : false;
  903. } catch (Dibi\Exception $e) {
  904. return false;
  905. }
  906. }
  907. function usernameTakenExcept($username, $email, $id)
  908. {
  909. try {
  910. $connect = new Dibi\Connection([
  911. 'driver' => 'sqlite3',
  912. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  913. ]);
  914. $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);
  915. return ($all) ? true : false;
  916. } catch (Dibi\Exception $e) {
  917. return false;
  918. }
  919. }
  920. function createUser($username, $password, $defaults, $email = null)
  921. {
  922. $email = ($email) ? $email : random_ascii_string(10) . '@placeholder.eml';
  923. try {
  924. if (!usernameTaken($username, $email)) {
  925. $createDB = new Dibi\Connection([
  926. 'driver' => 'sqlite3',
  927. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  928. ]);
  929. $userInfo = [
  930. 'username' => $username,
  931. 'password' => password_hash($password, PASSWORD_BCRYPT),
  932. 'email' => $email,
  933. 'group' => $defaults['group'],
  934. 'group_id' => $defaults['group_id'],
  935. 'image' => gravatar($email),
  936. 'register_date' => $GLOBALS['currentTime'],
  937. ];
  938. $createDB->query('INSERT INTO [users]', $userInfo);
  939. return true;
  940. } else {
  941. return false;
  942. }
  943. } catch (Dibi\Exception $e) {
  944. return false;
  945. }
  946. }
  947. function allTabs()
  948. {
  949. if (file_exists('config' . DIRECTORY_SEPARATOR . 'config.php')) {
  950. try {
  951. $connect = new Dibi\Connection([
  952. 'driver' => 'sqlite3',
  953. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  954. ]);
  955. $all['tabs'] = $connect->fetchAll('SELECT * FROM tabs ORDER BY `order` ASC');
  956. $all['categories'] = $connect->fetchAll('SELECT * FROM categories ORDER BY `order` ASC');
  957. $all['groups'] = $connect->fetchAll('SELECT * FROM groups ORDER BY `group_id` ASC');
  958. return $all;
  959. } catch (Dibi\Exception $e) {
  960. return false;
  961. }
  962. }
  963. return false;
  964. }
  965. function allGroups()
  966. {
  967. if (file_exists('config' . DIRECTORY_SEPARATOR . 'config.php')) {
  968. try {
  969. $connect = new Dibi\Connection([
  970. 'driver' => 'sqlite3',
  971. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  972. ]);
  973. $all = $connect->fetchAll('SELECT * FROM groups ORDER BY `group_id` ASC');
  974. return $all;
  975. } catch (Dibi\Exception $e) {
  976. return false;
  977. }
  978. }
  979. return false;
  980. }
  981. function loadTabs()
  982. {
  983. if (file_exists('config' . DIRECTORY_SEPARATOR . 'config.php')) {
  984. try {
  985. $connect = new Dibi\Connection([
  986. 'driver' => 'sqlite3',
  987. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  988. ]);
  989. $sort = ($GLOBALS['unsortedTabs'] == 'top') ? 'DESC' : 'ASC';
  990. $tabs = $connect->fetchAll('SELECT * FROM tabs WHERE `group_id` >= ? AND `enabled` = 1 ORDER BY `order` ' . $sort, $GLOBALS['organizrUser']['groupID']);
  991. $categories = $connect->fetchAll('SELECT * FROM categories ORDER BY `order` ASC');
  992. $all['tabs'] = $tabs;
  993. foreach ($tabs as $k => $v) {
  994. $v['access_url'] = isset($v['url_local']) && getenv('SERVER_ADDR') == userIP() ? $v['url_local'] : $v['url'];
  995. }
  996. $count = array_map(function ($element) {
  997. return $element['category_id'];
  998. }, $tabs);
  999. $count = (array_count_values($count));
  1000. foreach ($categories as $k => $v) {
  1001. $v['count'] = isset($count[$v['category_id']]) ? $count[$v['category_id']] : 0;
  1002. }
  1003. $all['categories'] = $categories;
  1004. return $all;
  1005. } catch (Dibi\Exception $e) {
  1006. return false;
  1007. }
  1008. }
  1009. return false;
  1010. }