4
0

organizr-functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. function upgradeCheck() {
  3. $compare = new Composer\Semver\Comparator;
  4. // Upgrade to 1.50
  5. $config = loadConfig();
  6. if (isset($config['dbLocation']) && (!isset($config['configVersion']) || $compare->lessThan($config['configVersion'], '1.25.0-alpha.101'))) {
  7. return 'yup';
  8. // Upgrade database to latest version
  9. //updateSQLiteDB($config['database_Location'],'1.40');
  10. // Update Version and Commit
  11. //$config['CONFIG_VERSION'] = '1.50';
  12. //copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][1.40].bak.php');
  13. //$createConfigSuccess = createConfig($config);
  14. //unset($config);
  15. }else{
  16. return 'no';
  17. }
  18. //return true;
  19. }
  20. function wizardConfig($array){
  21. foreach ($array['data'] as $items) {
  22. foreach ($items as $key => $value) {
  23. if($key == 'name'){
  24. $newKey = $value;
  25. }
  26. if($key == 'value'){
  27. $newValue = $value;
  28. }
  29. if(isset($newKey) && isset($newValue)){
  30. $$newKey = $newValue;
  31. }
  32. }
  33. }
  34. $location = cleanDirectory($location);
  35. $dbName = $dbName.'.db';
  36. $configVersion = $GLOBALS['installedVersion'];
  37. $configArray = array(
  38. 'dbName' => $dbName,
  39. 'dbLocation' => $location,
  40. 'license' => $license,
  41. 'organizrHash' => $hashKey,
  42. 'organizrAPI' => $api,
  43. 'registrationPassword' => $registrationPassword,
  44. );
  45. // Create Config
  46. if(createConfig($configArray)){
  47. // Call DB Create
  48. if(createDB($location,$dbName)){
  49. // Add in first user
  50. if(createFirstAdmin($location,$dbName,$username,$password,$email)){
  51. if(createToken($username,$email,gravatar($email),'Admin',0,$hashKey,1)){
  52. return true;
  53. }
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. function register($array){
  60. // Grab username and password from login form
  61. foreach ($array['data'] as $items) {
  62. foreach ($items as $key => $value) {
  63. if($key == 'name'){
  64. $newKey = $value;
  65. }
  66. if($key == 'value'){
  67. $newValue = $value;
  68. }
  69. if(isset($newKey) && isset($newValue)){
  70. $$newKey = $newValue;
  71. }
  72. }
  73. }
  74. if($registrationPassword == $GLOBALS['registrationPassword']){
  75. $defaults = defaultUserGroup();
  76. writeLog('success', 'Registration Function - Registration Password Verified', $username);
  77. if(createUser($username,$password,$defaults,$email)){
  78. writeLog('success', 'Registration Function - A User has registered', $username);
  79. if(createToken($username,$email,gravatar($email),$defaults['group'],$defaults['group_id'],$GLOBALS['organizrHash'],1)){
  80. writeLoginLog($username, 'success');
  81. writeLog('success', 'Login Function - A User has logged in', $username);
  82. return true;
  83. }
  84. }else{
  85. writeLog('error', 'Registration Function - An error occured', $username);
  86. return 'username taken';
  87. }
  88. }else{
  89. writeLog('warning', 'Registration Function - Wrong Password', $username);
  90. return 'mismatch';
  91. }
  92. }
  93. function editUser($array){
  94. return $array;
  95. }
  96. function logout(){
  97. coookie('delete','organizrToken');
  98. $GLOBALS['organizrUser'] = false;
  99. return true;
  100. }
  101. function qualifyRequest($accessLevelNeeded){
  102. if(getUserLevel() <= $accessLevelNeeded){
  103. return true;
  104. }else{
  105. return false;
  106. }
  107. }
  108. function getUserLevel(){
  109. $requesterToken = isset(getallheaders()['Token']) ? getallheaders()['Token'] : false;
  110. // Check token or API key
  111. // If API key, return 0 for admin
  112. if(strlen($requesterToken) == 20 && $requesterToken == $GLOBALS['organizrAPI']){
  113. //DO API CHECK
  114. return 0;
  115. }elseif(isset($GLOBALS['organizrUser'])){
  116. return $GLOBALS['organizrUser']['groupID'];
  117. }
  118. // All else fails? return guest id
  119. return 999;
  120. }
  121. function organizrStatus(){
  122. $status = array();
  123. $dependenciesActive = array();
  124. $dependenciesInactive = array();
  125. $extensions = array("PDO_SQLITE", "PDO", "SQLITE3", "zip", "cURL", "openssl", "simplexml", "json", "session");
  126. $functions = array("hash", "fopen", "fsockopen", "fwrite", "fclose", "readfile");
  127. foreach($extensions as $check){
  128. if(extension_loaded($check)){
  129. array_push($dependenciesActive,$check);
  130. }else{
  131. array_push($dependenciesInactive,$check);
  132. }
  133. }
  134. foreach($functions as $check){
  135. if(function_exists($check)){
  136. array_push($dependenciesActive,$check);
  137. }else{
  138. array_push($dependenciesInactive,$check);
  139. }
  140. }
  141. if(!file_exists('config'.DIRECTORY_SEPARATOR.'config.php')){
  142. $status['status'] = "wizard";//wizard - ok for test
  143. }
  144. if(count($dependenciesInactive)>0 || !is_writable(dirname(__DIR__,2))){
  145. $status['status'] = "dependencies";
  146. }
  147. $status['status'] = (!empty($status['status'])) ? $status['status'] : $status['status'] = "ok";
  148. $status['writable'] = is_writable(dirname(__DIR__,2)) ? 'yes' : 'no';
  149. $status['dependenciesActive'] = $dependenciesActive;
  150. $status['dependenciesInactive'] = $dependenciesInactive;
  151. $status['version'] = $GLOBALS['installedVersion'];
  152. $status['os'] = getOS();
  153. $status['php'] = phpversion();
  154. return $status;
  155. }
  156. function loadAppearance(){
  157. $appearance = array();
  158. $appearance['logo'] = $GLOBALS['logo'];
  159. $appearance['title'] = $GLOBALS['title'];
  160. $appearance['useLogo'] = $GLOBALS['useLogo'];
  161. $appearance['headerColor'] = $GLOBALS['headerColor'];
  162. $appearance['loginWallpaper'] = $GLOBALS['loginWallpaper'];
  163. return $appearance;
  164. }
  165. function getCustomizeAppearance(){
  166. if(file_exists('config'.DIRECTORY_SEPARATOR.'config.php')){
  167. return array(
  168. 'config' => array(/*
  169. array(
  170. 'type' => 'select',
  171. 'name' => 'branch',
  172. 'label' => 'Organizr Branch',
  173. 'value' => $GLOBALS['branch'],
  174. 'options' => array(
  175. 'Master' => 'v2-master',
  176. 'Develop' => 'v2-develop'
  177. )
  178. ),*/
  179. array(
  180. 'type' => 'input',
  181. 'name' => 'logo',
  182. 'label' => 'Logo',
  183. 'value' => $GLOBALS['logo']
  184. ),
  185. array(
  186. 'type' => 'input',
  187. 'name' => 'loginWallpaper',
  188. 'label' => 'Login Wallpaper',
  189. 'value' => $GLOBALS['loginWallpaper']
  190. ),
  191. array(
  192. 'type' => 'input',
  193. 'name' => 'title',
  194. 'label' => 'Title',
  195. 'value' => $GLOBALS['title']
  196. ),
  197. array(
  198. 'type' => 'switch',
  199. 'name' => 'useLogo',
  200. 'label' => 'Use Logo instead of Title',
  201. 'value' => $GLOBALS['useLogo']
  202. ),
  203. array(
  204. 'type' => 'input',
  205. 'name' => 'headerColor',
  206. 'label' => 'Nav Bar Color',
  207. 'value' => $GLOBALS['headerColor'],
  208. 'class' => 'colorpicker',
  209. 'disabled' => true
  210. )
  211. ),
  212. 'database' => array(
  213. )
  214. );
  215. }
  216. }
  217. function auth(){
  218. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  219. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  220. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  221. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  222. $group = isset($_GET['group']) ? $_GET['group'] : 0;
  223. $currentIP = userIP();
  224. $currentUser = $GLOBALS['organizrUser']['username'];
  225. if ($whitelist) {
  226. if(in_array($currentIP, arrayIP($whitelist))) {
  227. !$debug ? exit(http_response_code(200)) : die("$currentIP Whitelist Authorized");
  228. }
  229. }
  230. if ($blacklist) {
  231. if(in_array($currentIP, arrayIP($blacklist))) {
  232. !$debug ? exit(http_response_code(401)) : die("$currentIP Blacklisted");
  233. }
  234. }
  235. if($group !== null){
  236. if(qualifyRequest($group)){
  237. !$debug ? exit(http_response_code(200)) : die("$currentUser on $currentIP Authorized");
  238. }else{
  239. !$debug ? exit(http_response_code(401)) : die("$currentUser on $currentIP Not Authorized");
  240. }
  241. }else{
  242. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  243. }
  244. }
  245. function logoOrText(){
  246. if($GLOBALS['useLogo'] == false){
  247. return '<h1>'.$GLOBALS['title'].'</h1>';
  248. }else{
  249. return '<img style="max-width: 350px;" src="'.$GLOBALS['logo'].'" alt="Home" />';
  250. }
  251. }
  252. function getImages(){
  253. $dirname = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  254. $path = 'plugins/images/tabs/';
  255. $images = scandir($dirname);
  256. $ignore = Array(".", "..", "._.DS_Store", ".DS_Store");
  257. $allIcons = array();
  258. foreach($images as $image){
  259. if(!in_array($image, $ignore)) {
  260. $allIcons[] = $path.$image;
  261. }
  262. }
  263. return $allIcons;
  264. }
  265. function editImages(){
  266. $array = array();
  267. $postCheck = array_filter($_POST);
  268. $filesCheck = array_filter($_FILES);
  269. if(!empty($postCheck)){
  270. if($_POST['data']['action'] == 'deleteImage'){
  271. if(file_exists(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])){
  272. writeLog('success', 'Image Manager Function - Deleted Image ['.$_POST['data']['imageName'].']', $GLOBALS['organizrUser']['username']);
  273. return (unlink(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])) ? true : false;
  274. }
  275. }
  276. }
  277. if(!empty($filesCheck)){
  278. ini_set('upload_max_filesize', '10M');
  279. ini_set('post_max_size', '10M');
  280. $tempFile = $_FILES['file']['tmp_name'];
  281. $targetPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  282. $targetFile = $targetPath. $_FILES['file']['name'];
  283. return (move_uploaded_file($tempFile,$targetFile)) ? true : false;
  284. }
  285. return false;
  286. }