organizr-functions.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. function upgradeCheck() {
  3. $compare = new Composer\Semver\Comparator;
  4. // Upgrade check start for vserion below
  5. $versionCheck = '2.25.0-alpha.101';
  6. $config = loadConfig();
  7. if (isset($config['dbLocation']) && (!isset($config['configVersion']) || $compare->lessThan($config['configVersion'], $versionCheck))) {
  8. $oldVer = $config['configVersion'];
  9. return 'Upgraded Needed - Current Version '.$oldVer.' - New Version: '.$versionCheck;
  10. // Upgrade database to latest version
  11. //updateDB($GLOBALS['dbLocation'],$GLOBALS['dbName'],$oldVer);
  12. // Update Version and Commit
  13. //$config['configVersion'] = $versionCheck;
  14. //copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][1.40].bak.php');
  15. //$createConfigSuccess = createConfig($config);
  16. //unset($config);
  17. }else{
  18. //unset($config);
  19. return 'No Upgraded Needed - Current Version Above: '.$versionCheck;
  20. }
  21. return true;
  22. }
  23. function wizardConfig($array){
  24. foreach ($array['data'] as $items) {
  25. foreach ($items as $key => $value) {
  26. if($key == 'name'){
  27. $newKey = $value;
  28. }
  29. if($key == 'value'){
  30. $newValue = $value;
  31. }
  32. if(isset($newKey) && isset($newValue)){
  33. $$newKey = $newValue;
  34. }
  35. }
  36. }
  37. $location = cleanDirectory($location);
  38. $dbName = $dbName.'.db';
  39. $configVersion = $GLOBALS['installedVersion'];
  40. $configArray = array(
  41. 'dbName' => $dbName,
  42. 'dbLocation' => $location,
  43. 'license' => $license,
  44. 'organizrHash' => $hashKey,
  45. 'organizrAPI' => $api,
  46. 'registrationPassword' => $registrationPassword,
  47. );
  48. // Create Config
  49. if(createConfig($configArray)){
  50. // Call DB Create
  51. if(createDB($location,$dbName)){
  52. // Add in first user
  53. if(createFirstAdmin($location,$dbName,$username,$password,$email)){
  54. if(createToken($username,$email,gravatar($email),'Admin',0,$hashKey,1)){
  55. return true;
  56. }
  57. }
  58. }
  59. }
  60. return false;
  61. }
  62. function register($array){
  63. // Grab username and password from login form
  64. foreach ($array['data'] as $items) {
  65. foreach ($items as $key => $value) {
  66. if($key == 'name'){
  67. $newKey = $value;
  68. }
  69. if($key == 'value'){
  70. $newValue = $value;
  71. }
  72. if(isset($newKey) && isset($newValue)){
  73. $$newKey = $newValue;
  74. }
  75. }
  76. }
  77. if($registrationPassword == $GLOBALS['registrationPassword']){
  78. $defaults = defaultUserGroup();
  79. writeLog('success', 'Registration Function - Registration Password Verified', $username);
  80. if(createUser($username,$password,$defaults,$email)){
  81. writeLog('success', 'Registration Function - A User has registered', $username);
  82. if(createToken($username,$email,gravatar($email),$defaults['group'],$defaults['group_id'],$GLOBALS['organizrHash'],1)){
  83. writeLoginLog($username, 'success');
  84. writeLog('success', 'Login Function - A User has logged in', $username);
  85. return true;
  86. }
  87. }else{
  88. writeLog('error', 'Registration Function - An error occured', $username);
  89. return 'username taken';
  90. }
  91. }else{
  92. writeLog('warning', 'Registration Function - Wrong Password', $username);
  93. return 'mismatch';
  94. }
  95. }
  96. function editUser($array){
  97. return $array;
  98. }
  99. function logout(){
  100. coookie('delete','organizrToken');
  101. $GLOBALS['organizrUser'] = false;
  102. return true;
  103. }
  104. function qualifyRequest($accessLevelNeeded){
  105. if(getUserLevel() <= $accessLevelNeeded){
  106. return true;
  107. }else{
  108. return false;
  109. }
  110. }
  111. function getUserLevel(){
  112. $requesterToken = isset(getallheaders()['Token']) ? getallheaders()['Token'] : false;
  113. // Check token or API key
  114. // If API key, return 0 for admin
  115. if(strlen($requesterToken) == 20 && $requesterToken == $GLOBALS['organizrAPI']){
  116. //DO API CHECK
  117. return 0;
  118. }elseif(isset($GLOBALS['organizrUser'])){
  119. return $GLOBALS['organizrUser']['groupID'];
  120. }
  121. // All else fails? return guest id
  122. return 999;
  123. }
  124. function organizrStatus(){
  125. $status = array();
  126. $dependenciesActive = array();
  127. $dependenciesInactive = array();
  128. $extensions = array("PDO_SQLITE", "PDO", "SQLITE3", "zip", "cURL", "openssl", "simplexml", "json", "session");
  129. $functions = array("hash", "fopen", "fsockopen", "fwrite", "fclose", "readfile");
  130. foreach($extensions as $check){
  131. if(extension_loaded($check)){
  132. array_push($dependenciesActive,$check);
  133. }else{
  134. array_push($dependenciesInactive,$check);
  135. }
  136. }
  137. foreach($functions as $check){
  138. if(function_exists($check)){
  139. array_push($dependenciesActive,$check);
  140. }else{
  141. array_push($dependenciesInactive,$check);
  142. }
  143. }
  144. if(!file_exists('config'.DIRECTORY_SEPARATOR.'config.php')){
  145. $status['status'] = "wizard";//wizard - ok for test
  146. }
  147. if(count($dependenciesInactive)>0 || !is_writable(dirname(__DIR__,2))){
  148. $status['status'] = "dependencies";
  149. }
  150. $status['status'] = (!empty($status['status'])) ? $status['status'] : $status['status'] = "ok";
  151. $status['writable'] = is_writable(dirname(__DIR__,2)) ? 'yes' : 'no';
  152. $status['dependenciesActive'] = $dependenciesActive;
  153. $status['dependenciesInactive'] = $dependenciesInactive;
  154. $status['version'] = $GLOBALS['installedVersion'];
  155. $status['os'] = getOS();
  156. $status['php'] = phpversion();
  157. return $status;
  158. }
  159. function loadAppearance(){
  160. $appearance = array();
  161. $appearance['logo'] = $GLOBALS['logo'];
  162. $appearance['title'] = $GLOBALS['title'];
  163. $appearance['useLogo'] = $GLOBALS['useLogo'];
  164. $appearance['headerColor'] = $GLOBALS['headerColor'];
  165. $appearance['loginWallpaper'] = $GLOBALS['loginWallpaper'];
  166. return $appearance;
  167. }
  168. function getCustomizeAppearance(){
  169. if(file_exists(dirname(__DIR__,1).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php')){
  170. return array(
  171. 'config' => array(/*
  172. array(
  173. 'type' => 'select',
  174. 'name' => 'branch',
  175. 'label' => 'Organizr Branch',
  176. 'value' => $GLOBALS['branch'],
  177. 'options' => array(
  178. 'Master' => 'v2-master',
  179. 'Develop' => 'v2-develop'
  180. )
  181. ),*/
  182. array(
  183. 'type' => 'input',
  184. 'name' => 'logo',
  185. 'label' => 'Logo',
  186. 'value' => $GLOBALS['logo']
  187. ),
  188. array(
  189. 'type' => 'input',
  190. 'name' => 'loginWallpaper',
  191. 'label' => 'Login Wallpaper',
  192. 'value' => $GLOBALS['loginWallpaper']
  193. ),
  194. array(
  195. 'type' => 'input',
  196. 'name' => 'title',
  197. 'label' => 'Title',
  198. 'value' => $GLOBALS['title']
  199. ),
  200. array(
  201. 'type' => 'switch',
  202. 'name' => 'useLogo',
  203. 'label' => 'Use Logo instead of Title',
  204. 'value' => $GLOBALS['useLogo']
  205. ),
  206. array(
  207. 'type' => 'input',
  208. 'name' => 'headerColor',
  209. 'label' => 'Nav Bar Color',
  210. 'value' => $GLOBALS['headerColor'],
  211. 'class' => 'colorpicker',
  212. 'disabled' => true
  213. )
  214. ),
  215. 'database' => array(
  216. )
  217. );
  218. }
  219. }
  220. function editAppearance($array){
  221. switch ($array['data']['value']) {
  222. case 'true':
  223. $array['data']['value'] = (bool) true;
  224. break;
  225. case 'false':
  226. $array['data']['value'] = (bool) false;
  227. break;
  228. default:
  229. $array['data']['value'] = $array['data']['value'];
  230. }
  231. //return gettype($array['data']['value']).' - '.$array['data']['value'];
  232. switch ($array['data']['action']) {
  233. case 'editCustomizeAppearance':
  234. $newItem = array(
  235. $array['data']['name'] => $array['data']['value']
  236. );
  237. return (updateConfig($newItem)) ? true : false;
  238. break;
  239. default:
  240. # code...
  241. break;
  242. }
  243. }
  244. function updateConfigItem($array){
  245. switch ($array['data']['value']) {
  246. case 'true':
  247. $array['data']['value'] = (bool) true;
  248. break;
  249. case 'false':
  250. $array['data']['value'] = (bool) false;
  251. break;
  252. default:
  253. $array['data']['value'] = $array['data']['value'];
  254. }
  255. // Hash
  256. if($array['data']['type'] == 'password'){
  257. $array['data']['value'] = encrypt($array['data']['value']);
  258. }
  259. //return gettype($array['data']['value']).' - '.$array['data']['value'];
  260. $newItem = array(
  261. $array['data']['name'] => $array['data']['value']
  262. );
  263. return (updateConfig($newItem)) ? true : false;
  264. }
  265. function getPlugins(){
  266. if(file_exists(dirname(__DIR__,1).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php')){
  267. $pluginList = array();
  268. foreach($GLOBALS['plugins'] as $plugin){
  269. foreach ($plugin as $key => $value) {
  270. $plugin[$key]['enabled'] = $GLOBALS[$value['configPrefix'].'-enabled'];
  271. }
  272. $pluginList = array_merge($pluginList, $plugin);
  273. }
  274. return $pluginList;
  275. }
  276. return false;
  277. }
  278. function editPlugins($array){
  279. switch ($array['data']['action']) {
  280. case 'enable':
  281. $newItem = array(
  282. $array['data']['configName'] => true
  283. );
  284. writeLog('success', 'Plugin Function - Enabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  285. return (updateConfig($newItem)) ? true : false;
  286. break;
  287. case 'disable':
  288. $newItem = array(
  289. $array['data']['configName'] => false
  290. );
  291. writeLog('success', 'Plugin Function - Disabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  292. return (updateConfig($newItem)) ? true : false;
  293. break;
  294. default:
  295. # code...
  296. break;
  297. }
  298. }
  299. function auth(){
  300. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  301. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  302. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  303. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  304. $group = isset($_GET['group']) ? $_GET['group'] : 0;
  305. $currentIP = userIP();
  306. $currentUser = $GLOBALS['organizrUser']['username'];
  307. if ($whitelist) {
  308. if(in_array($currentIP, arrayIP($whitelist))) {
  309. !$debug ? exit(http_response_code(200)) : die("$currentIP Whitelist Authorized");
  310. }
  311. }
  312. if ($blacklist) {
  313. if(in_array($currentIP, arrayIP($blacklist))) {
  314. !$debug ? exit(http_response_code(401)) : die("$currentIP Blacklisted");
  315. }
  316. }
  317. if($group !== null){
  318. if(qualifyRequest($group)){
  319. !$debug ? exit(http_response_code(200)) : die("$currentUser on $currentIP Authorized");
  320. }else{
  321. !$debug ? exit(http_response_code(401)) : die("$currentUser on $currentIP Not Authorized");
  322. }
  323. }else{
  324. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  325. }
  326. }
  327. function logoOrText(){
  328. if($GLOBALS['useLogo'] == false){
  329. return '<h1>'.$GLOBALS['title'].'</h1>';
  330. }else{
  331. return '<img style="max-width: 350px;" src="'.$GLOBALS['logo'].'" alt="Home" />';
  332. }
  333. }
  334. function getImages(){
  335. $dirname = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  336. $path = 'plugins/images/tabs/';
  337. $images = scandir($dirname);
  338. $ignore = Array(".", "..", "._.DS_Store", ".DS_Store");
  339. $allIcons = array();
  340. foreach($images as $image){
  341. if(!in_array($image, $ignore)) {
  342. $allIcons[] = $path.$image;
  343. }
  344. }
  345. return $allIcons;
  346. }
  347. function editImages(){
  348. $array = array();
  349. $postCheck = array_filter($_POST);
  350. $filesCheck = array_filter($_FILES);
  351. if(!empty($postCheck)){
  352. if($_POST['data']['action'] == 'deleteImage'){
  353. if(file_exists(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])){
  354. writeLog('success', 'Image Manager Function - Deleted Image ['.$_POST['data']['imageName'].']', $GLOBALS['organizrUser']['username']);
  355. return (unlink(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])) ? true : false;
  356. }
  357. }
  358. }
  359. if(!empty($filesCheck)){
  360. ini_set('upload_max_filesize', '10M');
  361. ini_set('post_max_size', '10M');
  362. $tempFile = $_FILES['file']['tmp_name'];
  363. $targetPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  364. $targetFile = $targetPath. $_FILES['file']['name'];
  365. return (move_uploaded_file($tempFile,$targetFile)) ? true : false;
  366. }
  367. return false;
  368. }
  369. /*
  370. function sendEmail($email = null, $username = "Organizr User", $subject, $body, $cc = null, $bcc = null){
  371. try {
  372. $mail = new PHPMailer(true);
  373. $mail->isSMTP();
  374. $mail->Host = $GLOBALS['smtpHost'];
  375. $mail->SMTPAuth = $GLOBALS['smtpHostAuth'];
  376. $mail->Username = $GLOBALS['smtpHostUsername'];
  377. $mail->Password = $GLOBALS['smtpHostPassword'];
  378. $mail->SMTPSecure = $GLOBALS['smtpHostType'];
  379. $mail->Port = $GLOBALS['smtpHostPort'];
  380. $mail->setFrom($GLOBALS['smtpHostSenderEmail'], $GLOBALS['smtpHostSenderName']);
  381. $mail->addReplyTo($GLOBALS['smtpHostSenderEmail'], $GLOBALS['smtpHostSenderName']);
  382. $mail->isHTML(true);
  383. if($email){
  384. $mail->addAddress($email, $username);
  385. }
  386. if($cc){
  387. $mail->addCC($cc);
  388. }
  389. if($bcc){
  390. if(strpos($bcc , ',') === false){
  391. $mail->addBCC($bcc);
  392. }else{
  393. $allEmails = explode(",",$bcc);
  394. foreach($allEmails as $gotEmail){
  395. $mail->addBCC($gotEmail);
  396. }
  397. }
  398. }
  399. $mail->Subject = $subject;
  400. $mail->Body = $body;
  401. $mail->send();
  402. writeLog('success', 'Mail Function - E-Mail Sent', $GLOBALS['organizrUser']['username']);
  403. return true;
  404. } catch (Exception $e) {
  405. writeLog('error', 'Mail Function - E-Mail Failed['.$mail->ErrorInfo.']', $GLOBALS['organizrUser']['username']);
  406. return false;
  407. }
  408. return false;
  409. }
  410. //EMAIL SHIT
  411. function sendTestEmail($to, $from, $host, $auth, $username, $password, $type, $port, $sendername){
  412. try {
  413. $mail = new PHPMailer(true);
  414. $mail->isSMTP();
  415. $mail->Host = $host;
  416. $mail->SMTPAuth = $auth;
  417. $mail->Username = $username;
  418. $mail->Password = $password;
  419. $mail->SMTPSecure = $type;
  420. $mail->Port = $port;
  421. $mail->setFrom($from, $sendername);
  422. $mail->addReplyTo($from, $sendername);
  423. $mail->isHTML(true);
  424. $mail->addAddress($to, "Organizr Admin");
  425. $mail->Subject = "Organizr Test E-Mail";
  426. $mail->Body = "This was just a test!";
  427. $mail->send();
  428. writeLog('success', 'Mail Function - E-Mail Test Sent', $GLOBALS['organizrUser']['username']);
  429. return true;
  430. } catch (Exception $e) {
  431. writeLog('error', 'Mail Function - E-Mail Test Failed['.$mail->ErrorInfo.']', $GLOBALS['organizrUser']['username']);
  432. return false;
  433. }
  434. return false;
  435. }
  436. */