organizr-functions.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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(dirname(__DIR__,1).DIRECTORY_SEPARATOR.'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 editAppearance($array){
  218. switch ($array['data']['value']) {
  219. case 'true':
  220. $array['data']['value'] = (bool) true;
  221. break;
  222. case 'false':
  223. $array['data']['value'] = (bool) false;
  224. break;
  225. default:
  226. $array['data']['value'] = $array['data']['value'];
  227. }
  228. //return gettype($array['data']['value']).' - '.$array['data']['value'];
  229. switch ($array['data']['action']) {
  230. case 'editCustomizeAppearance':
  231. $newItem = array(
  232. $array['data']['name'] => $array['data']['value']
  233. );
  234. return (updateConfig($newItem)) ? true : false;
  235. break;
  236. default:
  237. # code...
  238. break;
  239. }
  240. }
  241. function updateConfigItem($array){
  242. switch ($array['data']['value']) {
  243. case 'true':
  244. $array['data']['value'] = (bool) true;
  245. break;
  246. case 'false':
  247. $array['data']['value'] = (bool) false;
  248. break;
  249. default:
  250. $array['data']['value'] = $array['data']['value'];
  251. }
  252. // Hash
  253. if($array['data']['type'] == 'password'){
  254. $array['data']['value'] = encrypt($array['data']['value']);
  255. }
  256. //return gettype($array['data']['value']).' - '.$array['data']['value'];
  257. $newItem = array(
  258. $array['data']['name'] => $array['data']['value']
  259. );
  260. return (updateConfig($newItem)) ? true : false;
  261. }
  262. function getPlugins(){
  263. if(file_exists(dirname(__DIR__,1).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php')){
  264. $pluginList = array();
  265. foreach($GLOBALS['plugins'] as $plugin){
  266. foreach ($plugin as $key => $value) {
  267. $plugin[$key]['enabled'] = $GLOBALS[$value['configPrefix'].'-enabled'];
  268. }
  269. $pluginList = array_merge($pluginList, $plugin);
  270. }
  271. return $pluginList;
  272. }
  273. return false;
  274. }
  275. function editPlugins($array){
  276. switch ($array['data']['action']) {
  277. case 'enable':
  278. $newItem = array(
  279. $array['data']['configName'] => true
  280. );
  281. writeLog('success', 'Plugin Function - Enabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  282. return (updateConfig($newItem)) ? true : false;
  283. break;
  284. case 'disable':
  285. $newItem = array(
  286. $array['data']['configName'] => false
  287. );
  288. writeLog('success', 'Plugin Function - Disabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  289. return (updateConfig($newItem)) ? true : false;
  290. break;
  291. default:
  292. # code...
  293. break;
  294. }
  295. }
  296. function auth(){
  297. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  298. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  299. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  300. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  301. $group = isset($_GET['group']) ? $_GET['group'] : 0;
  302. $currentIP = userIP();
  303. $currentUser = $GLOBALS['organizrUser']['username'];
  304. if ($whitelist) {
  305. if(in_array($currentIP, arrayIP($whitelist))) {
  306. !$debug ? exit(http_response_code(200)) : die("$currentIP Whitelist Authorized");
  307. }
  308. }
  309. if ($blacklist) {
  310. if(in_array($currentIP, arrayIP($blacklist))) {
  311. !$debug ? exit(http_response_code(401)) : die("$currentIP Blacklisted");
  312. }
  313. }
  314. if($group !== null){
  315. if(qualifyRequest($group)){
  316. !$debug ? exit(http_response_code(200)) : die("$currentUser on $currentIP Authorized");
  317. }else{
  318. !$debug ? exit(http_response_code(401)) : die("$currentUser on $currentIP Not Authorized");
  319. }
  320. }else{
  321. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  322. }
  323. }
  324. function logoOrText(){
  325. if($GLOBALS['useLogo'] == false){
  326. return '<h1>'.$GLOBALS['title'].'</h1>';
  327. }else{
  328. return '<img style="max-width: 350px;" src="'.$GLOBALS['logo'].'" alt="Home" />';
  329. }
  330. }
  331. function getImages(){
  332. $dirname = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  333. $path = 'plugins/images/tabs/';
  334. $images = scandir($dirname);
  335. $ignore = Array(".", "..", "._.DS_Store", ".DS_Store");
  336. $allIcons = array();
  337. foreach($images as $image){
  338. if(!in_array($image, $ignore)) {
  339. $allIcons[] = $path.$image;
  340. }
  341. }
  342. return $allIcons;
  343. }
  344. function editImages(){
  345. $array = array();
  346. $postCheck = array_filter($_POST);
  347. $filesCheck = array_filter($_FILES);
  348. if(!empty($postCheck)){
  349. if($_POST['data']['action'] == 'deleteImage'){
  350. if(file_exists(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])){
  351. writeLog('success', 'Image Manager Function - Deleted Image ['.$_POST['data']['imageName'].']', $GLOBALS['organizrUser']['username']);
  352. return (unlink(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])) ? true : false;
  353. }
  354. }
  355. }
  356. if(!empty($filesCheck)){
  357. ini_set('upload_max_filesize', '10M');
  358. ini_set('post_max_size', '10M');
  359. $tempFile = $_FILES['file']['tmp_name'];
  360. $targetPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  361. $targetFile = $targetPath. $_FILES['file']['name'];
  362. return (move_uploaded_file($tempFile,$targetFile)) ? true : false;
  363. }
  364. return false;
  365. }
  366. /*
  367. function sendEmail($email = null, $username = "Organizr User", $subject, $body, $cc = null, $bcc = null){
  368. try {
  369. $mail = new PHPMailer(true);
  370. $mail->isSMTP();
  371. $mail->Host = $GLOBALS['smtpHost'];
  372. $mail->SMTPAuth = $GLOBALS['smtpHostAuth'];
  373. $mail->Username = $GLOBALS['smtpHostUsername'];
  374. $mail->Password = $GLOBALS['smtpHostPassword'];
  375. $mail->SMTPSecure = $GLOBALS['smtpHostType'];
  376. $mail->Port = $GLOBALS['smtpHostPort'];
  377. $mail->setFrom($GLOBALS['smtpHostSenderEmail'], $GLOBALS['smtpHostSenderName']);
  378. $mail->addReplyTo($GLOBALS['smtpHostSenderEmail'], $GLOBALS['smtpHostSenderName']);
  379. $mail->isHTML(true);
  380. if($email){
  381. $mail->addAddress($email, $username);
  382. }
  383. if($cc){
  384. $mail->addCC($cc);
  385. }
  386. if($bcc){
  387. if(strpos($bcc , ',') === false){
  388. $mail->addBCC($bcc);
  389. }else{
  390. $allEmails = explode(",",$bcc);
  391. foreach($allEmails as $gotEmail){
  392. $mail->addBCC($gotEmail);
  393. }
  394. }
  395. }
  396. $mail->Subject = $subject;
  397. $mail->Body = $body;
  398. $mail->send();
  399. writeLog('success', 'Mail Function - E-Mail Sent', $GLOBALS['organizrUser']['username']);
  400. return true;
  401. } catch (Exception $e) {
  402. writeLog('error', 'Mail Function - E-Mail Failed['.$mail->ErrorInfo.']', $GLOBALS['organizrUser']['username']);
  403. return false;
  404. }
  405. return false;
  406. }
  407. //EMAIL SHIT
  408. function sendTestEmail($to, $from, $host, $auth, $username, $password, $type, $port, $sendername){
  409. try {
  410. $mail = new PHPMailer(true);
  411. $mail->isSMTP();
  412. $mail->Host = $host;
  413. $mail->SMTPAuth = $auth;
  414. $mail->Username = $username;
  415. $mail->Password = $password;
  416. $mail->SMTPSecure = $type;
  417. $mail->Port = $port;
  418. $mail->setFrom($from, $sendername);
  419. $mail->addReplyTo($from, $sendername);
  420. $mail->isHTML(true);
  421. $mail->addAddress($to, "Organizr Admin");
  422. $mail->Subject = "Organizr Test E-Mail";
  423. $mail->Body = "This was just a test!";
  424. $mail->send();
  425. writeLog('success', 'Mail Function - E-Mail Test Sent', $GLOBALS['organizrUser']['username']);
  426. return true;
  427. } catch (Exception $e) {
  428. writeLog('error', 'Mail Function - E-Mail Test Failed['.$mail->ErrorInfo.']', $GLOBALS['organizrUser']['username']);
  429. return false;
  430. }
  431. return false;
  432. }
  433. */