4
0

organizr-functions.php 16 KB

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