organizr-functions.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. $appearance['headerColor'] = $GLOBALS['headerColor'];
  144. return $appearance;
  145. }
  146. function getCustomizeAppearance(){
  147. if(file_exists('config'.DIRECTORY_SEPARATOR.'config.php')){
  148. return array(
  149. 'config' => array(/*
  150. array(
  151. 'type' => 'select',
  152. 'name' => 'branch',
  153. 'label' => 'Organizr Branch',
  154. 'value' => $GLOBALS['branch'],
  155. 'options' => array(
  156. 'Master' => 'v2-master',
  157. 'Develop' => 'v2-develop'
  158. )
  159. ),*/
  160. array(
  161. 'type' => 'input',
  162. 'name' => 'logo',
  163. 'label' => 'Logo',
  164. 'value' => $GLOBALS['logo']
  165. ),
  166. array(
  167. 'type' => 'input',
  168. 'name' => 'title',
  169. 'label' => 'Title',
  170. 'value' => $GLOBALS['title']
  171. ),
  172. array(
  173. 'type' => 'switch',
  174. 'name' => 'useLogo',
  175. 'label' => 'Use Logo instead of Title',
  176. 'value' => $GLOBALS['useLogo']
  177. ),
  178. array(
  179. 'type' => 'input',
  180. 'name' => 'headerColor',
  181. 'label' => 'Nav Bar Color',
  182. 'value' => $GLOBALS['headerColor'],
  183. 'class' => 'colorpicker',
  184. 'disabled' => true
  185. )
  186. ),
  187. 'database' => array(
  188. )
  189. );
  190. }
  191. }
  192. function auth(){
  193. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  194. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  195. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  196. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  197. $group = isset($_GET['group']) ? $_GET['group'] : 0;
  198. $currentIP = userIP();
  199. $currentUser = $GLOBALS['organizrUser']['username'];
  200. if ($whitelist) {
  201. if(in_array($currentIP, arrayIP($whitelist))) {
  202. !$debug ? exit(http_response_code(200)) : die("$currentIP Whitelist Authorized");
  203. }
  204. }
  205. if ($blacklist) {
  206. if(in_array($currentIP, arrayIP($blacklist))) {
  207. !$debug ? exit(http_response_code(401)) : die("$currentIP Blacklisted");
  208. }
  209. }
  210. if($group !== null){
  211. if(qualifyRequest($group)){
  212. !$debug ? exit(http_response_code(200)) : die("$currentUser on $currentIP Authorized");
  213. }else{
  214. !$debug ? exit(http_response_code(401)) : die("$currentUser on $currentIP Not Authorized");
  215. }
  216. }else{
  217. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  218. }
  219. }
  220. function logoOrText(){
  221. if($GLOBALS['useLogo'] == false){
  222. return '<h1>'.$GLOBALS['title'].'</h1>';
  223. }else{
  224. return '<img style="max-width: 350px;" src="'.$GLOBALS['logo'].'" alt="Home" />';
  225. }
  226. }
  227. function getImages(){
  228. $dirname = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  229. $path = 'plugins/images/tabs/';
  230. $images = scandir($dirname);
  231. $ignore = Array(".", "..", "._.DS_Store", ".DS_Store");
  232. $allIcons = array();
  233. foreach($images as $image){
  234. if(!in_array($image, $ignore)) {
  235. $allIcons[] = $path.$image;
  236. }
  237. }
  238. return $allIcons;
  239. }
  240. function editImages(){
  241. $array = array();
  242. $postCheck = array_filter($_POST);
  243. $filesCheck = array_filter($_FILES);
  244. if(!empty($postCheck)){
  245. if($_POST['data']['action'] == 'deleteImage'){
  246. if(file_exists(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])){
  247. writeLog('success', 'Image Manager Function - Deleted Image ['.$_POST['data']['imageName'].']', $GLOBALS['organizrUser']['username']);
  248. return (unlink(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])) ? true : false;
  249. }
  250. }
  251. }
  252. if(!empty($filesCheck)){
  253. $tempFile = $_FILES['file']['tmp_name'];
  254. $targetPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  255. $targetFile = $targetPath. $_FILES['file']['name'];
  256. return (move_uploaded_file($tempFile,$targetFile)) ? true : false;
  257. }
  258. return false;
  259. }