organizr-functions.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. function wizardConfig($array){
  3. foreach ($array['data'] as $items) {
  4. foreach ($items as $key => $value) {
  5. if($key == 'name'){
  6. $newKey = $value;
  7. }
  8. if($key == 'value'){
  9. $newValue = $value;
  10. }
  11. if(isset($newKey) && isset($newValue)){
  12. $$newKey = $newValue;
  13. }
  14. }
  15. }
  16. $location = cleanDirectory($location);
  17. $dbName = $dbName.'.db';
  18. $configVersion = $GLOBALS['installedVersion'];
  19. $configArray = array(
  20. 'dbName' => $dbName,
  21. 'dbLocation' => $location,
  22. 'license' => $license,
  23. 'organizrHash' => $hashKey,
  24. 'organizrAPI' => $api,
  25. 'registrationPassword' => $registrationPassword,
  26. );
  27. // Create Config
  28. if(createConfig($configArray)){
  29. // Call DB Create
  30. if(createDB($location,$dbName)){
  31. // Add in first user
  32. if(createFirstAdmin($location,$dbName,$username,$password,$email)){
  33. if(createToken($username,$email,gravatar($email),'Admin',0,$hashKey,1)){
  34. return true;
  35. }
  36. }
  37. }
  38. }
  39. return false;
  40. }
  41. function register($array){
  42. // Grab username and password from login form
  43. foreach ($array['data'] as $items) {
  44. foreach ($items as $key => $value) {
  45. if($key == 'name'){
  46. $newKey = $value;
  47. }
  48. if($key == 'value'){
  49. $newValue = $value;
  50. }
  51. if(isset($newKey) && isset($newValue)){
  52. $$newKey = $newValue;
  53. }
  54. }
  55. }
  56. if($registrationPassword == $GLOBALS['registrationPassword']){
  57. $defaults = defaultUserGroup();
  58. writeLog('success', 'Registration Function - Registration Password Verified', $username);
  59. if(createUser($username,$password,$defaults,$email)){
  60. writeLog('success', 'Registration Function - A User has registered', $username);
  61. if(createToken($username,$email,gravatar($email),$defaults['group'],$defaults['group_id'],$GLOBALS['organizrHash'],1)){
  62. writeLoginLog($username, 'success');
  63. writeLog('success', 'Login Function - A User has logged in', $username);
  64. return true;
  65. }
  66. }else{
  67. writeLog('error', 'Registration Function - An error occured', $username);
  68. return 'username taken';
  69. }
  70. }else{
  71. writeLog('warning', 'Registration Function - Wrong Password', $username);
  72. return 'mismatch';
  73. }
  74. }
  75. function editUser($array){
  76. return $array;
  77. }
  78. function logout(){
  79. coookie('delete','organizrToken');
  80. $GLOBALS['organizrUser'] = false;
  81. return true;
  82. }
  83. function qualifyRequest($accessLevelNeeded){
  84. if(getUserLevel() <= $accessLevelNeeded){
  85. return true;
  86. }else{
  87. return false;
  88. }
  89. }
  90. function getUserLevel(){
  91. $requesterToken = isset(getallheaders()['Token']) ? getallheaders()['Token'] : false;
  92. // Check token or API key
  93. // If API key, return 0 for admin
  94. if(strlen($requesterToken) == 20 && $requesterToken == $GLOBALS['organizrAPI']){
  95. //DO API CHECK
  96. return 0;
  97. }elseif(isset($GLOBALS['organizrUser'])){
  98. return $GLOBALS['organizrUser']['groupID'];
  99. }
  100. // All else fails? return guest id
  101. return 999;
  102. }
  103. function organizrStatus(){
  104. $status = array();
  105. $dependenciesActive = array();
  106. $dependenciesInactive = array();
  107. $extensions = array("PDO_SQLITE", "PDO", "SQLITE3", "zip", "cURL", "openssl", "simplexml", "json", "session");
  108. $functions = array("hash", "fopen", "fsockopen", "fwrite", "fclose", "readfile");
  109. foreach($extensions as $check){
  110. if(extension_loaded($check)){
  111. array_push($dependenciesActive,$check);
  112. }else{
  113. array_push($dependenciesInactive,$check);
  114. }
  115. }
  116. foreach($functions as $check){
  117. if(function_exists($check)){
  118. array_push($dependenciesActive,$check);
  119. }else{
  120. array_push($dependenciesInactive,$check);
  121. }
  122. }
  123. if(!file_exists('config'.DIRECTORY_SEPARATOR.'config.php')){
  124. $status['status'] = "wizard";//wizard - ok for test
  125. }
  126. if(count($dependenciesInactive)>0 || !is_writable(dirname(__DIR__,2))){
  127. $status['status'] = "dependencies";
  128. }
  129. $status['status'] = (!empty($status['status'])) ? $status['status'] : $status['status'] = "ok";
  130. $status['writable'] = is_writable(dirname(__DIR__,2)) ? 'yes' : 'no';
  131. $status['dependenciesActive'] = $dependenciesActive;
  132. $status['dependenciesInactive'] = $dependenciesInactive;
  133. $status['version'] = $GLOBALS['installedVersion'];
  134. $status['os'] = getOS();
  135. $status['php'] = phpversion();
  136. return $status;
  137. }
  138. function loadAppearance(){
  139. $appearance = array();
  140. $appearance['logo'] = $GLOBALS['logo'];
  141. $appearance['title'] = $GLOBALS['title'];
  142. $appearance['useLogo'] = $GLOBALS['useLogo'];
  143. return $appearance;
  144. }
  145. function auth(){
  146. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  147. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  148. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  149. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  150. $group = isset($_GET['group']) ? $_GET['group'] : 0;
  151. $currentIP = userIP();
  152. $currentUser = $GLOBALS['organizrUser']['username'];
  153. if ($whitelist) {
  154. if(in_array($currentIP, arrayIP($whitelist))) {
  155. !$debug ? exit(http_response_code(200)) : die("$currentIP Whitelist Authorized");
  156. }
  157. }
  158. if ($blacklist) {
  159. if(in_array($currentIP, arrayIP($blacklist))) {
  160. !$debug ? exit(http_response_code(401)) : die("$currentIP Blacklisted");
  161. }
  162. }
  163. if($group !== null){
  164. if(qualifyRequest($group)){
  165. !$debug ? exit(http_response_code(200)) : die("$currentUser on $currentIP Authorized");
  166. }else{
  167. !$debug ? exit(http_response_code(401)) : die("$currentUser on $currentIP Not Authorized");
  168. }
  169. }else{
  170. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  171. }
  172. }
  173. function logoOrText(){
  174. if($GLOBALS['useLogo'] == false){
  175. return '<h1>'.$GLOBALS['title'].'</h1>';
  176. }else{
  177. return '<img style="max-width: 350px;" src="'.$GLOBALS['logo'].'" alt="Home" />';
  178. }
  179. }