PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/web/editor/common/controller.php

https://bitbucket.org/dineshkummarc/diagramo
PHP | 1130 lines | 664 code | 258 blank | 208 comment | 103 complexity | 385067afbcc9bb00beed0cbad8410884 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require_once dirname(__FILE__) . '/delegate.php';
  3. session_start();
  4. ################################################################################
  5. ### REQUEST ################################################################
  6. // Collect the data (from POST or GET)
  7. $action = $_REQUEST['action'];
  8. switch ($action) {
  9. case 'info':
  10. info();
  11. break;
  12. case 'logoutExe':
  13. logoutExe();
  14. break;
  15. case 'loginExe':
  16. loginExe();
  17. break;
  18. case 'forgotPasswordExe':
  19. forgotPasswordExe();
  20. break;
  21. case 'resetPassword':
  22. resetPassword();
  23. break;
  24. case 'resetPasswordExe':
  25. resetPasswordExe();
  26. break;
  27. case 'saveSettingsExe':
  28. saveSettingsExe();
  29. break;
  30. case 'save':
  31. save();
  32. break;
  33. case 'saveAs':
  34. saveAs();
  35. break;
  36. case 'saveSvg':
  37. saveSvg();
  38. break;
  39. case 'newDiagramExe':
  40. newDiagramExe();
  41. break;
  42. case 'editDiagramExe':
  43. editDiagramExe();
  44. break;
  45. case 'firstSaveExe':
  46. firstSaveExe();
  47. break;
  48. case 'load':
  49. load();
  50. break;
  51. case 'deleteDiagramExe':
  52. deleteDiagramExe();
  53. break;
  54. /*************************** */
  55. /*********COLABORATORS****** */
  56. /*************************** */
  57. case 'addUserExe':
  58. addUserExe();
  59. break;
  60. case 'cancelInvitationExe':
  61. cancelInvitationExe();
  62. break;
  63. case 'acceptInvitationExe':
  64. acceptInvitationExe();
  65. break;
  66. case 'declineInvitationExe':
  67. declineInvitationExe();
  68. break;
  69. case 'removeUser':
  70. removeUser();
  71. break;
  72. case 'removeMeFromDiagram':
  73. removeMeFromDiagram();
  74. break;
  75. /*************************** */
  76. /*********USERS****** */
  77. /*************************** */
  78. // case 'addUserExe':
  79. // addUserExe();
  80. // break;
  81. case 'registerExe':
  82. registerExe();
  83. break;
  84. case 'editUserExe':
  85. editUserExe();
  86. break;
  87. /****************LICENSE--------------*/
  88. case 'saveLicense':
  89. saveLicense();
  90. break;
  91. }
  92. function loginExe() {
  93. $email = trim($_REQUEST['email']);
  94. $password = trim($_REQUEST['password']);
  95. // Validate data
  96. if (validateString($email, 'Empty email or bad email syntax')) {
  97. #print "Wrong email";
  98. }
  99. if (validateString($password, 'Empty password')) {
  100. #print "Wrong password";
  101. }
  102. if (errors ()) {
  103. #print "Errors"; exit(0);
  104. //outer site
  105. redirect("../../index.php");
  106. exit(0);
  107. }
  108. $delegate = new Delegate();
  109. $user = $delegate->userGetByEmailAndPassword($email, $password);
  110. if (is_object($user)) {
  111. $_SESSION['userId'] = $user->id;
  112. //remember me option
  113. if ($_REQUEST['rememberMe'] === 'true') {
  114. $userCookie = packer(array('email' => $email, 'password' => md5($password)), PACKER_PACK);
  115. setcookie('biscuit', $userCookie, time() + ((60 * 60 * 24) * 5), '/');
  116. }
  117. $user->lastLoginDate = now();
  118. $user->lastLoginIp = $_SERVER['REMOTE_ADDR'];
  119. $user->lastBrowserType = $_SERVER['HTTP_USER_AGENT'];
  120. //$delegate->userUpdate($user);
  121. redirect("../editor.php");
  122. exit(0);
  123. } else {
  124. addError("Authetication failed");
  125. //outer site
  126. redirect("../login.php");
  127. exit(0);
  128. }
  129. }
  130. /**
  131. * Logout
  132. */
  133. function logoutExe() {
  134. if (is_numeric($_SESSION['userId'])) {
  135. unset($_SESSION['userId']);
  136. // Clear the user cookie
  137. setcookie('biscuit', null, time() - ((60 * 60 * 24) * 5), '/');
  138. session_destroy();
  139. }
  140. addMessage("You were logged out!");
  141. //up to the outer site
  142. redirect("../login.php");
  143. }
  144. /**
  145. */
  146. function forgotPasswordExe() {
  147. $email = trim($_POST['email']);
  148. // Validate data
  149. if (!validateString($email, 'Empty email or bad email syntax')) {
  150. print "Wrong email: " . $email;
  151. exit();
  152. }
  153. if($_REQUEST['captcha'] != $_SESSION['captcha']){
  154. addError("Text was wrong. try again!");
  155. }
  156. if (errors ()) {
  157. // print "Errors"; exit(0);
  158. redirect("../forgot-password.php");
  159. exit(0);
  160. }
  161. $delegate = new Delegate();
  162. $WEBADDRESS = $delegate->settingsGetByKeyNative('WEBADDRESS');
  163. $user = $delegate->userGetByEmail($email);
  164. if (is_object($user)) {
  165. $url = $WEBADDRESS . '/editor/common/controller.php?action=resetPassword&k=' . $user->password . '&i=' . $user->id;
  166. $body =
  167. "<html>
  168. <head>
  169. <title>Reset your password</title>
  170. </head>
  171. <body>
  172. Hello, <p/>
  173. Here is your request to reset your password. Please click the link to reset your password.
  174. <a href=\"${url}\">${url}</a>
  175. </body>
  176. </html>";
  177. if (sendEmail($user->email, 'no-reply@diagramo.com', "Password reset", $body)) {
  178. addMessage("Reset email sent!");
  179. } else {
  180. addError("Reset email NOT sent!");
  181. }
  182. #outer site
  183. redirect("../forgot-password.php");
  184. exit(0);
  185. } else {
  186. addError("Email not present in DB");
  187. redirect("../forgot-password.php");
  188. exit(0);
  189. }
  190. }
  191. /* * Resets a password */
  192. function resetPassword() {
  193. $id = trim($_GET['i']); //get user Id
  194. $key = trim($_GET['k']); //get user's encrypted password :D
  195. // Validate data
  196. if (validateString($id, 'Wrong i param')) {
  197. #print "Wrong email";
  198. }
  199. if (validateString($key, 'Wrong k param')) {
  200. #print "Wrong email";
  201. }
  202. if (errors ()) {
  203. #print "Errors"; exit(0);
  204. redirect("../forgot-password.php");
  205. exit(0);
  206. }
  207. $delegate = new Delegate();
  208. $user = $delegate->userGetByIdAndEncryptedPassword($id, $key);
  209. #print_r($user);
  210. #exit();
  211. if (is_object($user)) {
  212. $_SESSION['userId'] = $user->id;
  213. redirect("../resetPassword.php");
  214. exit(0);
  215. } else {
  216. addError("User/Email not present in DB");
  217. redirect("../forgot-password.php");
  218. exit(0);
  219. }
  220. }
  221. /* * Resets a password */
  222. function resetPasswordExe() {
  223. if (!is_numeric($_SESSION['userId'])) {
  224. addError("Not permited");
  225. redirect("../editor.php");
  226. exit(0);
  227. }
  228. $password = trim($_POST['password']);
  229. // Validate data
  230. if (validateString($password, 'Password should have at least 4 characters', 4)) {
  231. #print "Wrong email";
  232. }
  233. if (errors ()) {
  234. #print "Errors"; exit(0);
  235. redirect("../resetPassword.php");
  236. exit(0);
  237. }
  238. $delegate = new Delegate();
  239. $user = $delegate->userGetById($_SESSION['userId']);
  240. $user->password = md5($password);
  241. #print_r($user);
  242. #exit();
  243. if ($delegate->userUpdate($user)) {
  244. //we will skip this message
  245. //addMessage("Password changed!");
  246. redirect("../editor.php");
  247. exit(0);
  248. } else {
  249. addError("Password not changed");
  250. redirect("../resetPassword.php");
  251. exit(0);
  252. }
  253. }
  254. /* * Resets a password */
  255. function saveSettingsExe() {
  256. if (!isset($_SESSION['userId']) || !is_numeric($_SESSION['userId'])) {
  257. addError("Not permited");
  258. redirect("../editor.php");
  259. exit(0);
  260. }
  261. $delegate = new Delegate();
  262. $user = $delegate->userGetById($_SESSION['userId']);
  263. // print_r($user);
  264. // exit();
  265. // $name = trim($_POST['name']);
  266. $currentPassword = trim($_POST['currentPassword']);
  267. $newPassword = trim($_POST['newPassword']);
  268. if (!strlen($newPassword) >= 4) {
  269. addError("New password too short or empty");
  270. }
  271. if (md5($currentPassword) != $user->password) {
  272. addError("Current password is wrong");
  273. } else {
  274. $user->password = md5($newPassword);
  275. }
  276. if (errors ()) {
  277. #print "Errors"; exit(0);
  278. redirect("../settings.php");
  279. exit(0);
  280. }
  281. if ($delegate->userUpdate($user)) {
  282. addMessage("Settings saved!");
  283. redirect("../settings.php");
  284. exit(0);
  285. } else {
  286. addError("Settings not saved (or nothing to save)");
  287. redirect("../settings.php");
  288. exit(0);
  289. }
  290. }
  291. /* * Save currently edited diagram
  292. * We have 3 cases:
  293. * 1. there is no account present (once time)
  294. * 2. account is present but this is the first save (seldomly)
  295. * 3. account is pressent and this is not the first save (the most common)
  296. */
  297. function save() {
  298. if (isset($_REQUEST['diagramId']) && is_numeric($_REQUEST['diagramId'])) { //we have a current working diagram
  299. //print($_POST['svg']);
  300. $delegate = new Delegate();
  301. $currentDiagramId = $_REQUEST['diagramId'];
  302. $nowIsNow = now();
  303. // 1 - update the Dia file
  304. $diaData = $delegate->diagramdataGetByDiagramIdAndType($currentDiagramId, Diagramdata::TYPE_DIA);
  305. $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.dia', 'w');
  306. //$fh = fopen(dirname(__FILE__) . '/../diagrams/' . $currentDiagramId . '.dia', 'w');
  307. // $diaFile = dirname(__FILE__) . '/../diagrams/' . $_REQUEST['diagramId'] . '.dia';
  308. $diaSize = fwrite($fh, $_POST['diagram']);
  309. fclose($fh);
  310. $diaData->fileSize = $diaSize;
  311. $diaData->lastUpdate = $nowIsNow;
  312. $delegate->diagramdataUpdate($diaData);
  313. //end update Dia file
  314. // //2 - update the SVG file
  315. // $svgData = $delegate->diagramdataGetByDiagramIdAndType($currentDiagramId, Diagramdata::TYPE_SVG);
  316. //
  317. // $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.svg', 'w');
  318. // $svgSize = fwrite($fh, $_POST['svg']);
  319. // fclose($fh);
  320. //
  321. // $svgData->fileSize = $svgSize;
  322. // $svgData->lastUpdate = $nowIsNow;
  323. // $delegate->diagramdataUpdate($svgData);
  324. // //end update the SVG file
  325. // //update the Diagram
  326. // $diagram = $delegate->diagramGetById($currentDiagramId);
  327. // $diagram->size = $diaSize;
  328. // $diagram->lastUpdate = $nowIsNow;
  329. //3 - update the PNG file
  330. $svgData = $delegate->diagramdataGetByDiagramIdAndType($currentDiagramId, Diagramdata::TYPE_PNG);
  331. $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.png', 'wb');
  332. // $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.svg', 'w');
  333. $data = substr($_POST['png'], strpos($_POST['png'], ','));
  334. $imgData = base64_decode($data);
  335. $svgSize = fwrite($fh, $imgData);
  336. fclose($fh);
  337. $svgData->fileSize = $svgSize;
  338. $svgData->lastUpdate = $nowIsNow;
  339. $delegate->diagramdataUpdate($svgData);
  340. //end update the SVG file
  341. //update the Diagram
  342. $diagram = $delegate->diagramGetById($currentDiagramId);
  343. $diagram->size = $diaSize;
  344. $diagram->lastUpdate = $nowIsNow;
  345. if ($delegate->diagramUpdate($diagram)) {
  346. print "saved";
  347. } else {
  348. print 'diagramdata not saved';
  349. }
  350. exit();
  351. } else { //no current working diagram
  352. $_SESSION['tempDiagram'] = $_POST['diagram'];
  353. $_SESSION['tempSVG'] = $_POST['svg'];
  354. $_SESSION['tempPNG'] = $_POST['png'];
  355. print "firstSave";
  356. exit();
  357. }
  358. }
  359. /* * Save currently edited diagram. We always have an user logged
  360. * We have 2 cases:
  361. * 1. there is no account present (do nothing)
  362. * 2. account is present so store diagram in session and redirect (from JavaScript) to save Diabram form
  363. */
  364. function saveAs() {
  365. if (!is_numeric($_SESSION['userId'])) { //no user logged
  366. $_SESSION['tempDiagram'] = $_POST['diagram'];
  367. $_SESSION['tempSVG'] = $_POST['svg'];
  368. $_SESSION['tempPNG'] = $_POST['png'];
  369. print "noaccount";
  370. exit();
  371. } else { //user is logged
  372. $_SESSION['tempDiagram'] = $_POST['diagram'];
  373. $_SESSION['tempSVG'] = $_POST['svg'];
  374. $_SESSION['tempPNG'] = $_POST['png'];
  375. print "step1Ok";
  376. exit();
  377. }
  378. }
  379. /* * Save currently SVG-ed diagram
  380. */
  381. function saveSvg() {
  382. if (!empty($_POST['svg'])) { //no user logged
  383. $_SESSION['svg'] = $_POST['svg'];
  384. print "svg_ok";
  385. exit();
  386. } else { //user is not logged
  387. print "svg_failed";
  388. exit();
  389. }
  390. }
  391. function newDiagramExe() {
  392. // if(!is_numeric($_SESSION['userId'])) { //no user logged
  393. // print "wrong turn";
  394. // exit();
  395. // }
  396. //reset ay temporary diagram
  397. $_SESSION['tempDiagram'] = null;
  398. unset($_SESSION['tempDiagram']);
  399. redirect('../editor.php');
  400. }
  401. function editDiagramExe() {
  402. if (!is_numeric($_SESSION['userId'])) { //no user logged
  403. print "Not allowed";
  404. exit();
  405. }
  406. if (!is_numeric($_REQUEST['diagramId'])) { //no diagram specified
  407. print "No diagram";
  408. exit();
  409. }
  410. $d = new Delegate();
  411. $diagram = $d->diagramGetById($_REQUEST['diagramId']);
  412. $diagram->title = trim($_REQUEST['title']);
  413. $diagram->description = trim($_REQUEST['description']);
  414. $diagram->public = ($_REQUEST['public'] == true) ? 1 : 0;
  415. $diagram->lastUpdate = now();
  416. if ($d->diagramUpdate($diagram)) {
  417. addMessage("Diagram updated");
  418. } else {
  419. addError("Diagram not updated");
  420. }
  421. redirect('../myDiagrams.php');
  422. }
  423. /* * We already have the temporary diagram saved in session */
  424. function firstSaveExe() {
  425. // print_r($_SESSION);
  426. // exit();
  427. if (!is_numeric($_SESSION['userId'])) {
  428. print "Wrong way";
  429. exit();
  430. }
  431. //store current time
  432. $nowIsNow = now();
  433. //save Diagram
  434. $diagram = new Diagram();
  435. $diagram->title = trim($_REQUEST['title']);
  436. $diagram->description = trim($_REQUEST['description']);
  437. $diagram->public = (isset($_REQUEST['public']) && $_REQUEST['public'] == 'true') ? 1 : 0;
  438. $diagram->createdDate = $nowIsNow;
  439. $diagram->lastUpdate = $nowIsNow;
  440. $diagram->size = strlen($_SESSION['tempDiagram']); //TODO: it might be not very accurate
  441. $delegate = new Delegate();
  442. // $token = '';
  443. // do {
  444. // $token = generateRandom(6);
  445. // } while ($delegate->diagramCountByHash($token) > 0);
  446. //
  447. // $diagram->hash = $token;
  448. $diagramId = $delegate->diagramCreate($diagram);
  449. //end save Diagram
  450. //create Dia file
  451. $diagramdata = new Diagramdata();
  452. $diagramdata->diagramId = $diagramId;
  453. $diagramdata->type = Diagramdata::TYPE_DIA;
  454. $diagramdata->fileName = $diagramId . '.dia';
  455. $fh = fopen(getStorageFolder() . '/' . $diagramId . '.dia', 'w');
  456. $size = fwrite($fh, $_SESSION['tempDiagram']);
  457. fclose($fh);
  458. $diagramdata->fileSize = $size;
  459. $diagramdata->lastUpdate = $nowIsNow;
  460. $delegate->diagramdataCreate($diagramdata);
  461. //end Dia file
  462. /*
  463. //create SVG file
  464. $diagramdata = new Diagramdata();
  465. $diagramdata->diagramId = $diagramId;
  466. $diagramdata->type = Diagramdata::TYPE_SVG;
  467. $diagramdata->fileName = $diagramId . '.svg';
  468. $fh = fopen(getStorageFolder() . '/' . $diagramId . '.svg', 'w');
  469. $size = fwrite($fh, $_SESSION['tempSVG']);
  470. fclose($fh);
  471. $diagramdata->fileSize = $size;
  472. $diagramdata->lastUpdate = $nowIsNow;
  473. $delegate->diagramdataCreate($diagramdata);
  474. //end SVG file
  475. */
  476. //create PNG file
  477. $diagramdata = new Diagramdata();
  478. $diagramdata->diagramId = $diagramId;
  479. $diagramdata->type = Diagramdata::TYPE_PNG;
  480. $diagramdata->fileName = $diagramId . '.png';
  481. $fh = fopen(getStorageFolder() . '/' . $diagramId . '.png', 'wb');
  482. $data = substr($_SESSION['tempPNG'], strpos($_SESSION['tempPNG'], ','));
  483. $imgData = base64_decode($data);
  484. $size = fwrite($fh, $imgData);
  485. fclose($fh);
  486. $diagramdata->fileSize = $size;
  487. $diagramdata->lastUpdate = $nowIsNow;
  488. $delegate->diagramdataCreate($diagramdata);
  489. //end Dia file
  490. //clean temporary diagram
  491. unset($_SESSION['tempDiagram']);
  492. unset($_SESSION['tempSVG']);
  493. unset($_SESSION['tempPNG']);
  494. //attach it to an user
  495. redirect("../editor.php?diagramId=" . $diagramId);
  496. }
  497. /**Loads a diagram*/
  498. function load() {
  499. if (!is_numeric($_REQUEST['diagramId'])) {
  500. print "Wrong diagram id : " . $_REQUEST['diagramId'];
  501. exit();
  502. }
  503. $d = new Delegate();
  504. $diagram = $d->diagramGetById($_REQUEST['diagramId']);
  505. $allow = false;
  506. if($diagram->public){
  507. $allow = true;
  508. }
  509. else{ //no public so only logged users can see it
  510. if (!is_numeric($_SESSION['userId'])) {
  511. print "Wrong user id";
  512. exit();
  513. }
  514. $allow = true;
  515. }
  516. if($allow){
  517. $diagramdata = $d->diagramdataGetByDiagramIdAndType($_REQUEST['diagramId'], Diagramdata::TYPE_DIA);
  518. $diaFile = getStorageFolder() . '/' . $_REQUEST['diagramId'] . '.dia';
  519. /**When switching from Linux to Windows some files might get corrupt so we will use file_get_contents*/
  520. // $fh = fopen($diaFile, 'r');
  521. // $data = fread($fh, $diagramdata->fileSize);
  522. // fclose($fh);
  523. $data = file_get_contents($diaFile);
  524. print $data;
  525. }
  526. }
  527. function deleteDiagramExe() {
  528. if (!is_numeric($_SESSION['userId'])) {
  529. print "Wrong way";
  530. exit();
  531. }
  532. if (!is_numeric($_REQUEST['diagramId'])) {
  533. print "Wrong diagram id : " . $_REQUEST['diagramId'];
  534. exit();
  535. }
  536. //TODO: usually ONLY the author can delete the diagram
  537. $d = new Delegate();
  538. // print_r($_REQUEST);
  539. // exit();
  540. //delete diagramdata
  541. $diagramDatas = $d->diagramdataGetByDiagramId($_REQUEST['diagramId']);
  542. $storageFolder = getStorageFolder();
  543. foreach($diagramDatas as $diagramData){
  544. //TODO: we can make more tests here
  545. unlink($storageFolder . '/' . $diagramData->fileName);
  546. $d->diagramdataDeleteByDiagramIdAndType($diagramData->diagramId, $diagramData->type);
  547. }
  548. //delete diagram
  549. if ($d->diagramDelete($_REQUEST['diagramId'])) {
  550. addMessage("Diagram deleted");
  551. } else {
  552. addError("Diagram could not be deleted from database");
  553. }
  554. redirect('../myDiagrams.php');
  555. }
  556. /**Invite a collaborator to a diagram.
  557. * There are 2 kind of invitations:
  558. * 1. outside people - when you send an email with invitation and they will
  559. * first create an account and then accept the invitation
  560. * 2. known people - You already know those people and you invite them.
  561. * They will get an email + an "accept invitation" link on main page.
  562. */
  563. function addUserExe() {
  564. if (!is_numeric($_SESSION['userId'])) {
  565. print "Wrong way";
  566. exit();
  567. }
  568. if (empty($_REQUEST['email'])) {
  569. print "Email is empty";
  570. exit();
  571. }
  572. if (empty($_REQUEST['password'])) {
  573. print "Password is empty";
  574. exit();
  575. }
  576. $d = new Delegate();
  577. $loggedUser = $d->userGetById($_SESSION['userId']);
  578. $user = new User();
  579. $user->email = trim($_REQUEST['email']);
  580. $user->password = md5(trim($_REQUEST['password']));
  581. $user->createdDate = now();
  582. if($d->userCreate($user)){
  583. addMessage("User added");
  584. }
  585. else{
  586. addError("User not added");;
  587. }
  588. //refirect back to collaborators
  589. redirect('../users.php');
  590. }
  591. /**Delete an invitation*/
  592. function cancelInvitationExe() {
  593. if (!is_numeric($_SESSION['userId'])) {
  594. print "Wrong way";
  595. exit();
  596. }
  597. if (empty($_REQUEST['invitationId'])) {
  598. print "Invitation id is wrong";
  599. exit();
  600. }
  601. $d = new Delegate();
  602. $loggedUser = $d->userGetById($_SESSION['userId']);
  603. $invitation = $d->invitationGetById($_REQUEST['invitationId']);
  604. $diagram = $d->diagramGetById($invitation->diagramId);
  605. //are u allocated?
  606. $userdiagram = $d->userdiagramGetByIds($loggedUser->id, $diagram->id);
  607. if (!is_object($userdiagram)) {
  608. addError("Not working on that diagram.");
  609. redirect('../myDiagrams.php');
  610. exit();
  611. }
  612. if($userdiagram->level != Userdiagram::LEVEL_AUTHOR){
  613. addError("You have no rights.");
  614. redirect('../myDiagrams.php');
  615. exit();
  616. }
  617. if($d->invitationDelete($invitation->id)){
  618. addMessage("Invitation deleted");
  619. }
  620. else{
  621. addError("Invitation NOT deleted");
  622. }
  623. redirect('../colaborators.php?diagramId=' . $diagram->id);
  624. }
  625. /**
  626. * Remove a colaborator
  627. */
  628. function removeUser(){
  629. // print_r($_REQUEST);
  630. // exit();
  631. if (!is_numeric($_SESSION['userId'])) {
  632. print("Wrong way");
  633. exit();
  634. }
  635. if(!is_numeric($_REQUEST['userId'])){
  636. print("Wrong user");
  637. exit();
  638. }
  639. $delegate = new Delegate();
  640. if($delegate->userDeleteById($_REQUEST['userId'])){
  641. addMessage("User deleted");
  642. }
  643. else{
  644. addError("User not deleted");
  645. }
  646. redirect('../users.php');
  647. }
  648. /**
  649. * The collaborator remove itself from diagram
  650. */
  651. function removeMeFromDiagram(){
  652. if (!is_numeric($_SESSION['userId'])) {
  653. print("Wrong way");
  654. exit();
  655. }
  656. if(!is_numeric($_REQUEST['diagramId'])){
  657. print("No diagram");
  658. exit();
  659. }
  660. $delegate = new Delegate();
  661. $loggedUser = $delegate->userGetById($_SESSION['userId']);
  662. $userdiagram = $delegate->userdiagramGetByIds($loggedUser->id, $_REQUEST['diagramId']);
  663. if ($userdiagram) {
  664. /**author can not remove itself. he has to delete the diagram*/
  665. if($userdiagram->level == Userdiagram::LEVEL_AUTHOR){
  666. addError("Author can not remove itself from a diagram");
  667. redirect('../myDiagrams.php');
  668. exit();
  669. }
  670. if ($delegate->userdiagramDelete($loggedUser->id, $_REQUEST['diagramId'])) {
  671. addMessage("Removed from diagram");
  672. //TODO: notify author ?
  673. } else {
  674. addError("You were not removed from diagram");
  675. }
  676. redirect('../myDiagrams.php');
  677. }
  678. else{
  679. print('No rights');
  680. exit();
  681. }
  682. }
  683. function info() {
  684. phpinfo();
  685. }
  686. function registerExe(){
  687. if(!validateEmail($_REQUEST['email'])){
  688. addError("Email is wrong");
  689. }
  690. $d = new Delegate();
  691. $existingUser = $d->userGetByEmail(trim($_REQUEST['email']));
  692. if(is_object($existingUser)){
  693. addError("An user with same email already present.");
  694. }
  695. if(!validateString($_REQUEST['password'])){
  696. addError("Password is not ok");
  697. }
  698. if($_REQUEST['password'] != $_REQUEST['password2']){
  699. addError("Passwords do not match");
  700. }
  701. if( !isset ($_REQUEST['invitationToken']) ){
  702. if($_REQUEST['captcha'] != $_SESSION['captcha']){
  703. addError("Code was incorrect");
  704. }
  705. }
  706. if(errors()){
  707. redirect('../../register.php');
  708. exit(0);
  709. }
  710. $user = new User();
  711. $user->email = trim($_REQUEST['email']);
  712. $user->password = md5($_REQUEST['password']);
  713. $user->createdDate = now();
  714. $user->lastLoginDate = now();
  715. $user->lastLoginIp = $_SERVER['REMOTE_ADDR'];
  716. $user->lastBrowserType = $_SERVER['HTTP_USER_AGENT'];
  717. $userId = $d->userCreate($user);
  718. if(is_numeric($userId)){
  719. addMessage("You were registered");
  720. $_SESSION['userId'] = $userId;
  721. $_SESSION['captcha'] = null;
  722. unset($_SESSION['captcha']);
  723. //TODO: if we have a temp diagram we will redirect to save page
  724. if( isset ($_SESSION['tempDiagram']) ){
  725. redirect('../saveDiagram.php');
  726. }
  727. else if( isset($_REQUEST['invitationToken']) ){
  728. $invitation = $d->invitationGetByToken($_REQUEST['invitationToken']);
  729. if(is_object($invitation)){
  730. //find the diagram
  731. $diagram = $d->diagramGetById($invitation->diagramId);
  732. //create userdiagram
  733. $userdiagram = new Userdiagram();
  734. $userdiagram->diagramId = $diagram->id;
  735. $userdiagram->invitedDate = $invitation->createdDate;
  736. $userdiagram->level = Userdiagram::LEVEL_EDITOR;
  737. $userdiagram->status = Userdiagram::STATUS_ACCEPTED;
  738. $userdiagram->userId = $userId;
  739. if(!$d->userdiagramCreate($userdiagram)){
  740. addError("Could not add you to the diagram");
  741. redirect('../editor.php');
  742. exit();
  743. }
  744. //delete invitation
  745. $d->invitationDelete($invitation->id);
  746. //all is fine, redirect to the diagram
  747. redirect('../editor.php?diagramId=' . $diagram->id);
  748. }
  749. else{
  750. }
  751. redirect('../editor.php');
  752. }
  753. else{
  754. redirect('../editor.php');
  755. }
  756. exit(0);
  757. }
  758. else{
  759. addError("User not added ");
  760. redirect('../../register.php');
  761. exit(0);
  762. }
  763. }
  764. function saveLicense(){
  765. print_r($_REQUEST);
  766. $serial = $_REQUEST['serial'];
  767. $host = $_REQUEST['host'];
  768. $d = new Delegate();
  769. $DIAGRAMO = $d->settingsGetByKeyNative('DIAGRAMO');
  770. $url = $DIAGRAMO . "/dcentral/activator.php?serial=$serial&host=$host";
  771. #print 'URL: ' . $url;
  772. $license = get($url);
  773. #print 'License: ' . $license;
  774. #exit();
  775. $d->settingsSaveNative('LICENSE', $license);
  776. addMessage("App activated");
  777. redirect('../license.php');
  778. exit(0);
  779. }
  780. //function editUserExe(){
  781. //
  782. // if (!is_numeric($_SESSION['userId'])) {
  783. // print("Wrong way");
  784. // exit();
  785. // }
  786. //
  787. // if(!is_numeric($_REQUEST['userId'])){
  788. // print("Wrong user");
  789. // exit();
  790. // }
  791. //
  792. // #exit('here');
  793. // $d = new Delegate();
  794. //
  795. // $loggedUser = $d->userGetById($_SESSION['userId']);
  796. //
  797. //
  798. //
  799. // if(errors()){
  800. // redirect('../users.php');
  801. // exit(0);
  802. // }
  803. //
  804. // $user = $d->userGetById($_REQUEST['userId']);
  805. //
  806. // if(strlen($_REQUEST['password']) > 0 ){
  807. // $user->password = md5($_REQUEST['password']) ;
  808. // if($d->userUpdate($user)){
  809. // addMessage("User updated");
  810. // }
  811. // else{
  812. // addError("User NOT updated");
  813. // }
  814. // }
  815. //
  816. //
  817. // redirect('../users.php');
  818. //}
  819. function acceptInvitationExe(){
  820. if (!is_numeric($_SESSION['userId'])) {
  821. print("Wrong way");
  822. exit();
  823. }
  824. if( !isset ($_REQUEST['invitationId']) ){
  825. print("Wrong Invitation");
  826. exit();
  827. }
  828. $d = new Delegate();
  829. $loggedUser = $d->userGetById($_SESSION['userId']);
  830. $invitation = $d->invitationGetById($_REQUEST['invitationId']);
  831. if($invitation->email == $loggedUser->email){ //a match made in stars...how lovely :)
  832. $diagram = $d->diagramGetById($invitation->diagramId);
  833. //create userdiagram
  834. $userdiagram = new Userdiagram();
  835. $userdiagram->diagramId = $diagram->id;
  836. $userdiagram->invitedDate = $invitation->createdDate;
  837. $userdiagram->level = Userdiagram::LEVEL_EDITOR;
  838. $userdiagram->status = Userdiagram::STATUS_ACCEPTED;
  839. $userdiagram->userId = $loggedUser->id;
  840. //store it in DB
  841. $d->userdiagramCreate($userdiagram);
  842. //delete invitation
  843. $d->invitationDelete($invitation->id);
  844. addMessage("Invitation accepted");
  845. redirect('../editor.php?diagramId=' . $diagram->id);
  846. }
  847. else{
  848. addError("Nope");
  849. redirect('../myDiagrams.php');
  850. }
  851. }
  852. function declineInvitationExe(){
  853. if (!is_numeric($_SESSION['userId'])) {
  854. print("Wrong way");
  855. exit();
  856. }
  857. if( !isset ($_REQUEST['invitationId']) ){
  858. print("Wrong Invitation");
  859. exit();
  860. }
  861. $d = new Delegate();
  862. $loggedUser = $d->userGetById($_SESSION['userId']);
  863. $invitation = $d->invitationGetById($_REQUEST['invitationId']);
  864. if($invitation->email == $loggedUser->email){ //a match made in stars...how lovely :)
  865. $d->invitationDelete($invitation->id);
  866. addMessage("Invitation declined.");
  867. }
  868. redirect('../myDiagrams.php');
  869. }
  870. ?>