PageRenderTime 567ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/web/editor/common/controller.php

https://bitbucket.org/scriptoid/diagramo
PHP | 1421 lines | 809 code | 310 blank | 302 comment | 142 complexity | b82a0bf642fca145d91669ccd924982f MD5 | raw file
  1. <?php
  2. /*
  3. Copyright [2014] [Diagramo]
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. require_once dirname(__FILE__) . '/delegate.php';
  15. session_start();
  16. ################################################################################
  17. ### REQUEST ################################################################
  18. // Collect the data (from POST or GET)
  19. $action = $_REQUEST['action'];
  20. switch ($action) {
  21. case 'info':
  22. info();
  23. break;
  24. case 'logoutExe':
  25. logoutExe();
  26. break;
  27. case 'loginExe':
  28. loginExe();
  29. break;
  30. case 'forgotPasswordExe':
  31. forgotPasswordExe();
  32. break;
  33. case 'resetPassword':
  34. resetPassword();
  35. break;
  36. case 'resetPasswordExe':
  37. resetPasswordExe();
  38. break;
  39. case 'saveSettingsExe':
  40. saveSettingsExe();
  41. break;
  42. case 'save':
  43. save();
  44. break;
  45. case 'saveAs':
  46. saveAs();
  47. break;
  48. case 'saveSvg':
  49. saveSvg();
  50. break;
  51. case 'newDiagramExe':
  52. newDiagramExe();
  53. break;
  54. case 'editDiagramExe':
  55. editDiagramExe();
  56. break;
  57. case 'firstSaveExe':
  58. firstSaveExe();
  59. break;
  60. case 'load':
  61. load();
  62. break;
  63. case 'loadTemp':
  64. loadTemp();
  65. break;
  66. case 'loadQuickStart':
  67. loadQuickStart();
  68. break;
  69. case 'getUploadedImageFileNames':
  70. getUploadedImageFileNames();
  71. break;
  72. case 'insertImage':
  73. insertImage();
  74. break;
  75. case 'importDiagramExe':
  76. importDiagramExe();
  77. break;
  78. case 'deleteDiagramExe':
  79. deleteDiagramExe();
  80. break;
  81. /*************************** */
  82. /*********QUICK start****** */
  83. /*************************** */
  84. case 'closeQuickStart':
  85. closeQuickStart();
  86. break;
  87. /*************************** */
  88. /*********COLABORATORS****** */
  89. /*************************** */
  90. case 'addUserExe':
  91. addUserExe();
  92. break;
  93. case 'cancelInvitationExe':
  94. cancelInvitationExe();
  95. break;
  96. case 'acceptInvitationExe':
  97. acceptInvitationExe();
  98. break;
  99. case 'declineInvitationExe':
  100. declineInvitationExe();
  101. break;
  102. case 'removeUser':
  103. removeUser();
  104. break;
  105. case 'removeMeFromDiagram':
  106. removeMeFromDiagram();
  107. break;
  108. /*************************** */
  109. /*********USERS****** */
  110. /*************************** */
  111. // case 'addUserExe':
  112. // addUserExe();
  113. // break;
  114. case 'registerExe':
  115. registerExe();
  116. break;
  117. case 'editUserExe':
  118. editUserExe();
  119. break;
  120. // /****************LICENSE--------------*/
  121. // case 'saveLicense':
  122. // saveLicense();
  123. // break;
  124. }
  125. function loginExe() {
  126. $email = trim($_REQUEST['email']);
  127. $password = trim($_REQUEST['password']);
  128. // Validate data
  129. if (validateString($email, 'Empty email or bad email syntax')) {
  130. #print "Wrong email";
  131. }
  132. if (validateString($password, 'Empty password')) {
  133. #print "Wrong password";
  134. }
  135. if (errors ()) {
  136. #print "Errors"; exit(0);
  137. //outer site
  138. redirect("../../index.php");
  139. exit(0);
  140. }
  141. $delegate = new Delegate();
  142. $user = $delegate->userGetByEmailAndPassword($email, $password);
  143. if (is_object($user)) {
  144. $_SESSION['userId'] = $user->id;
  145. //remember me option
  146. if ($_REQUEST['rememberMe'] === 'true') {
  147. $userCookie = packer(array('email' => $email, 'password' => md5($password)), PACKER_PACK);
  148. setcookie('biscuit', $userCookie, time() + ((60 * 60 * 24) * 5), '/');
  149. }
  150. $user->lastLoginDate = now();
  151. $user->lastLoginIp = $_SERVER['REMOTE_ADDR'];
  152. $user->lastBrowserType = $_SERVER['HTTP_USER_AGENT'];
  153. //$delegate->userUpdate($user);
  154. if($user->tutorial){
  155. redirect("../editor.php?diagramId=quickstart");
  156. }
  157. else{
  158. redirect("../editor.php");
  159. }
  160. exit(0);
  161. } else {
  162. addError("Authetication failed");
  163. //outer site
  164. redirect("../login.php");
  165. exit(0);
  166. }
  167. }
  168. /**
  169. * Logout
  170. */
  171. function logoutExe() {
  172. if (is_numeric($_SESSION['userId'])) {
  173. unset($_SESSION['userId']);
  174. // Clear the user cookie
  175. setcookie('biscuit', null, time() - ((60 * 60 * 24) * 5), '/');
  176. session_destroy();
  177. }
  178. addMessage("You were logged out!");
  179. //up to the outer site
  180. redirect("../login.php");
  181. }
  182. /**
  183. */
  184. function forgotPasswordExe() {
  185. $email = trim($_POST['email']);
  186. // Validate data
  187. if (!validateString($email, 'Empty email or bad email syntax')) {
  188. print "Wrong email: " . $email;
  189. exit();
  190. }
  191. if($_REQUEST['captcha'] != $_SESSION['captcha']){
  192. addError("Text was wrong. try again!");
  193. }
  194. if (errors ()) {
  195. // print "Errors"; exit(0);
  196. redirect("../forgot-password.php");
  197. exit(0);
  198. }
  199. $delegate = new Delegate();
  200. $WEBADDRESS = $delegate->settingsGetByKeyNative('WEBADDRESS');
  201. $user = $delegate->userGetByEmail($email);
  202. if (is_object($user)) {
  203. $url = $WEBADDRESS . '/editor/common/controller.php?action=resetPassword&k=' . $user->password . '&i=' . $user->id;
  204. $body =
  205. "<html>
  206. <head>
  207. <title>Reset your password</title>
  208. </head>
  209. <body>
  210. Hello, <p/>
  211. Here is your request to reset your password. Please click the link to reset your password.
  212. <a href=\"${url}\">${url}</a>
  213. </body>
  214. </html>";
  215. if (sendEmail($user->email, 'no-reply@diagramo.com', "Password reset", $body)) {
  216. addMessage("Reset email sent!");
  217. } else {
  218. addError("Reset email NOT sent!");
  219. }
  220. #outer site
  221. redirect("../forgot-password.php");
  222. exit(0);
  223. } else {
  224. addError("Email not present in DB");
  225. redirect("../forgot-password.php");
  226. exit(0);
  227. }
  228. }
  229. /* * Resets a password */
  230. function resetPassword() {
  231. $id = trim($_GET['i']); //get user Id
  232. $key = trim($_GET['k']); //get user's encrypted password :D
  233. // Validate data
  234. if (validateString($id, 'Wrong i param')) {
  235. #print "Wrong email";
  236. }
  237. if (validateString($key, 'Wrong k param')) {
  238. #print "Wrong email";
  239. }
  240. if (errors ()) {
  241. #print "Errors"; exit(0);
  242. redirect("../forgot-password.php");
  243. exit(0);
  244. }
  245. $delegate = new Delegate();
  246. $user = $delegate->userGetByIdAndEncryptedPassword($id, $key);
  247. #print_r($user);
  248. #exit();
  249. if (is_object($user)) {
  250. $_SESSION['userId'] = $user->id;
  251. redirect("../resetPassword.php");
  252. exit(0);
  253. } else {
  254. addError("User/Email not present in DB");
  255. redirect("../forgot-password.php");
  256. exit(0);
  257. }
  258. }
  259. /* * Resets a password */
  260. function resetPasswordExe() {
  261. if (!is_numeric($_SESSION['userId'])) {
  262. addError("Not permited");
  263. redirect("../editor.php");
  264. exit(0);
  265. }
  266. $password = trim($_POST['password']);
  267. // Validate data
  268. if (validateString($password, 'Password should have at least 4 characters', 4)) {
  269. #print "Wrong email";
  270. }
  271. if (errors ()) {
  272. #print "Errors"; exit(0);
  273. redirect("../resetPassword.php");
  274. exit(0);
  275. }
  276. $delegate = new Delegate();
  277. $user = $delegate->userGetById($_SESSION['userId']);
  278. $user->password = md5($password);
  279. #print_r($user);
  280. #exit();
  281. if ($delegate->userUpdate($user)) {
  282. //we will skip this message
  283. //addMessage("Password changed!");
  284. redirect("../editor.php");
  285. exit(0);
  286. } else {
  287. addError("Password not changed");
  288. redirect("../resetPassword.php");
  289. exit(0);
  290. }
  291. }
  292. /* * Resets a password */
  293. function saveSettingsExe() {
  294. if (!isset($_SESSION['userId']) || !is_numeric($_SESSION['userId'])) {
  295. addError("Not permited");
  296. redirect("../editor.php");
  297. exit(0);
  298. }
  299. $delegate = new Delegate();
  300. $user = $delegate->userGetById($_SESSION['userId']);
  301. // print_r($user);
  302. // exit();
  303. // $name = trim($_POST['name']);
  304. $currentPassword = trim($_POST['currentPassword']);
  305. $newPassword = trim($_POST['newPassword']);
  306. if (!strlen($newPassword) >= 4) {
  307. addError("New password too short or empty");
  308. }
  309. if (md5($currentPassword) != $user->password) {
  310. addError("Current password is wrong");
  311. } else {
  312. $user->password = md5($newPassword);
  313. }
  314. if (errors ()) {
  315. #print "Errors"; exit(0);
  316. redirect("../settings.php");
  317. exit(0);
  318. }
  319. if ($delegate->userUpdate($user)) {
  320. addMessage("Settings saved!");
  321. redirect("../settings.php");
  322. exit(0);
  323. } else {
  324. addError("Settings not saved (or nothing to save)");
  325. redirect("../settings.php");
  326. exit(0);
  327. }
  328. }
  329. /* * Save currently edited diagram
  330. * We have 3 cases:
  331. * 1. there is no account present (once time)
  332. * 2. account is present but this is the first save (seldomly)
  333. * 3. account is pressent and this is not the first save (the most common)
  334. */
  335. function save() {
  336. if (isset($_REQUEST['diagramId']) && is_numeric($_REQUEST['diagramId'])) { //we have a current working diagram
  337. //print($_POST['svg']);
  338. $delegate = new Delegate();
  339. $currentDiagramId = $_REQUEST['diagramId'];
  340. $nowIsNow = now();
  341. // 1 - update the Dia file
  342. $diaData = $delegate->diagramdataGetByDiagramIdAndType($currentDiagramId, Diagramdata::TYPE_DMO);
  343. $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.dmo', 'w');
  344. //$fh = fopen(dirname(__FILE__) . '/../diagrams/' . $currentDiagramId . '.dmo', 'w');
  345. // $diaFile = dirname(__FILE__) . '/../diagrams/' . $_REQUEST['diagramId'] . '.dmo';
  346. $diaSize = fwrite($fh, $_POST['diagram']);
  347. fclose($fh);
  348. $diaData->fileSize = $diaSize;
  349. $diaData->lastUpdate = $nowIsNow;
  350. $delegate->diagramdataUpdate($diaData);
  351. //end update Dia file
  352. /*SVG support discontinued
  353. //2 - update the SVG file
  354. $svgData = $delegate->diagramdataGetByDiagramIdAndType($currentDiagramId, Diagramdata::TYPE_SVG);
  355. $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.svg', 'w');
  356. $svgSize = fwrite($fh, $_POST['svg']);
  357. fclose($fh);
  358. $svgData->fileSize = $svgSize;
  359. $svgData->lastUpdate = $nowIsNow;
  360. $delegate->diagramdataUpdate($svgData);
  361. //end update the SVG file
  362. //update the Diagram
  363. $diagram = $delegate->diagramGetById($currentDiagramId);
  364. $diagram->size = $diaSize;
  365. $diagram->lastUpdate = $nowIsNow;
  366. */
  367. //3 - update the PNG file
  368. $pngData = $delegate->diagramdataGetByDiagramIdAndType($currentDiagramId, Diagramdata::TYPE_PNG);
  369. $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.png', 'wb');
  370. $data = substr($_POST['png'], strpos($_POST['png'], ','));
  371. $imgData = base64_decode($data);
  372. $pngSize = fwrite($fh, $imgData);
  373. fclose($fh);
  374. $pngData->fileSize = $pngSize;
  375. $pngData->lastUpdate = $nowIsNow;
  376. $delegate->diagramdataUpdate($pngData);
  377. //end update the SVG file
  378. //update the link map
  379. $csvData = $delegate->diagramdataGetByDiagramIdAndType($currentDiagramId, Diagramdata::TYPE_CSV);
  380. $fh = fopen(getStorageFolder() . '/' . $currentDiagramId . '.csv', 'w');
  381. $data = $_POST['linkMap'];
  382. $csvSize = fwrite($fh, $data);
  383. fclose($fh);
  384. $csvData->fileSize = $csvSize;
  385. $csvData->lastUpdate = $nowIsNow;
  386. $delegate->diagramdataUpdate($csvData);
  387. //end update the link map
  388. //update the Diagram
  389. $diagram = $delegate->diagramGetById($currentDiagramId);
  390. $diagram->size = $diaSize;
  391. $diagram->lastUpdate = $nowIsNow;
  392. if ($delegate->diagramUpdate($diagram)) {
  393. print "saved";
  394. } else {
  395. print 'diagramdata not saved';
  396. }
  397. exit();
  398. } else { //no current working diagram
  399. $_SESSION['tempDiagram'] = $_POST['diagram'];
  400. $_SESSION['tempSVG'] = $_POST['svg'];
  401. $_SESSION['tempPNG'] = $_POST['png'];
  402. $_SESSION['tempLinkMap'] = $_POST['linkMap'];
  403. print "firstSave";
  404. exit();
  405. }
  406. }
  407. /* * Save currently edited diagram. We always have an user logged
  408. * We have 2 cases:
  409. * 1. there is no account present (do nothing)
  410. * 2. account is present so store diagram in session and redirect (from JavaScript) to save Diabram form
  411. */
  412. function saveAs() {
  413. if (!is_numeric($_SESSION['userId'])) { //no user logged
  414. $_SESSION['tempDiagram'] = $_POST['diagram'];
  415. $_SESSION['tempSVG'] = $_POST['svg'];
  416. $_SESSION['tempPNG'] = $_POST['png'];
  417. $_SESSION['tempLinkMap'] = $_POST['linkMap'];
  418. print "noaccount";
  419. exit();
  420. } else { //user is logged
  421. $_SESSION['tempDiagram'] = $_POST['diagram'];
  422. $_SESSION['tempSVG'] = $_POST['svg'];
  423. $_SESSION['tempPNG'] = $_POST['png'];
  424. $_SESSION['tempLinkMap'] = $_POST['linkMap'];
  425. print "step1Ok";
  426. exit();
  427. }
  428. }
  429. /* * Save currently SVG-ed diagram
  430. */
  431. function saveSvg() {
  432. if (!empty($_POST['svg'])) { //no user logged
  433. $_SESSION['svg'] = $_POST['svg'];
  434. print "svg_ok";
  435. exit();
  436. } else { //user is not logged
  437. print "svg_failed";
  438. exit();
  439. }
  440. }
  441. function newDiagramExe() {
  442. // if(!is_numeric($_SESSION['userId'])) { //no user logged
  443. // print "wrong turn";
  444. // exit();
  445. // }
  446. //reset ay temporary diagram
  447. $_SESSION['tempDiagram'] = null;
  448. unset($_SESSION['tempDiagram']);
  449. redirect('../editor.php');
  450. }
  451. function editDiagramExe() {
  452. if (!is_numeric($_SESSION['userId'])) { //no user logged
  453. print "Not allowed";
  454. exit();
  455. }
  456. if (!is_numeric($_REQUEST['diagramId'])) { //no diagram specified
  457. print "No diagram";
  458. exit();
  459. }
  460. $d = new Delegate();
  461. $diagram = $d->diagramGetById($_REQUEST['diagramId']);
  462. $diagram->title = trim($_REQUEST['title']);
  463. $diagram->description = trim($_REQUEST['description']);
  464. $diagram->public = ($_REQUEST['public'] == true) ? 1 : 0;
  465. $diagram->lastUpdate = now();
  466. if ($d->diagramUpdate($diagram)) {
  467. addMessage("Diagram updated");
  468. } else {
  469. addError("Diagram not updated");
  470. }
  471. redirect('../myDiagrams.php');
  472. }
  473. /* * We already have the temporary diagram saved in session */
  474. function firstSaveExe() {
  475. // print_r($_SESSION);
  476. // exit();
  477. if (!is_numeric($_SESSION['userId'])) {
  478. print "Wrong way";
  479. exit();
  480. }
  481. //store current time
  482. $nowIsNow = now();
  483. //save Diagram
  484. $diagram = new Diagram();
  485. $diagram->title = trim($_REQUEST['title']);
  486. $diagram->description = trim($_REQUEST['description']);
  487. $diagram->public = (isset($_REQUEST['public']) && $_REQUEST['public'] == 'true') ? 1 : 0;
  488. $diagram->createdDate = $nowIsNow;
  489. $diagram->lastUpdate = $nowIsNow;
  490. $diagram->size = strlen($_SESSION['tempDiagram']); //TODO: it might be not very accurate
  491. $delegate = new Delegate();
  492. // $token = '';
  493. // do {
  494. // $token = generateRandom(6);
  495. // } while ($delegate->diagramCountByHash($token) > 0);
  496. //
  497. // $diagram->hash = $token;
  498. $diagramId = $delegate->diagramCreate($diagram);
  499. //end save Diagram
  500. //create Dia file
  501. $diagramdata = new Diagramdata();
  502. $diagramdata->diagramId = $diagramId;
  503. $diagramdata->type = Diagramdata::TYPE_DMO;
  504. $diagramdata->fileName = $diagramId . '.dmo';
  505. $fh = fopen(getStorageFolder() . '/' . $diagramId . '.dmo', 'w');
  506. $size = fwrite($fh, $_SESSION['tempDiagram']);
  507. fclose($fh);
  508. $diagramdata->fileSize = $size;
  509. $diagramdata->lastUpdate = $nowIsNow;
  510. $delegate->diagramdataCreate($diagramdata);
  511. //end Dia file
  512. /*
  513. //create SVG file
  514. $diagramdata = new Diagramdata();
  515. $diagramdata->diagramId = $diagramId;
  516. $diagramdata->type = Diagramdata::TYPE_SVG;
  517. $diagramdata->fileName = $diagramId . '.svg';
  518. $fh = fopen(getStorageFolder() . '/' . $diagramId . '.svg', 'w');
  519. $size = fwrite($fh, $_SESSION['tempSVG']);
  520. fclose($fh);
  521. $diagramdata->fileSize = $size;
  522. $diagramdata->lastUpdate = $nowIsNow;
  523. $delegate->diagramdataCreate($diagramdata);
  524. //end SVG file
  525. */
  526. //create PNG file
  527. $diagramdata = new Diagramdata();
  528. $diagramdata->diagramId = $diagramId;
  529. $diagramdata->type = Diagramdata::TYPE_PNG;
  530. $diagramdata->fileName = $diagramId . '.png';
  531. $fh = fopen(getStorageFolder() . '/' . $diagramId . '.png', 'wb');
  532. $data = substr($_SESSION['tempPNG'], strpos($_SESSION['tempPNG'], ','));
  533. $imgData = base64_decode($data);
  534. $size = fwrite($fh, $imgData);
  535. fclose($fh);
  536. $diagramdata->fileSize = $size;
  537. $diagramdata->lastUpdate = $nowIsNow;
  538. $delegate->diagramdataCreate($diagramdata);
  539. //end PNG file
  540. //create CSV file
  541. $diagramdata = new Diagramdata();
  542. $diagramdata->diagramId = $diagramId;
  543. $diagramdata->type = Diagramdata::TYPE_CSV;
  544. $diagramdata->fileName = $diagramId . '.csv';
  545. $fh = fopen(getStorageFolder() . '/' . $diagramId . '.csv', 'w');
  546. $size = fwrite($fh, $_SESSION['tempLinkMap']);
  547. fclose($fh);
  548. $diagramdata->fileSize = $size;
  549. $diagramdata->lastUpdate = $nowIsNow;
  550. $delegate->diagramdataCreate($diagramdata);
  551. //end CSV file
  552. //clean temporary diagram
  553. unset($_SESSION['tempDiagram']);
  554. unset($_SESSION['tempSVG']);
  555. unset($_SESSION['tempPNG']);
  556. unset($_SESSION['tempLinkMap']);
  557. redirect("../editor.php?diagramId=" . $diagramId);
  558. }
  559. /**Loads a diagram*/
  560. function load() {
  561. if (!is_numeric($_REQUEST['diagramId'])) {
  562. print "Wrong diagram id : " . $_REQUEST['diagramId'];
  563. exit();
  564. }
  565. $d = new Delegate();
  566. $diagram = $d->diagramGetById($_REQUEST['diagramId']);
  567. $allow = false;
  568. if($diagram->public){
  569. $allow = true;
  570. }
  571. else{ //no public so only logged users can see it
  572. if (!is_numeric($_SESSION['userId'])) {
  573. print "Wrong user id";
  574. exit();
  575. }
  576. $allow = true;
  577. }
  578. if($allow){
  579. $diagramdata = $d->diagramdataGetByDiagramIdAndType($_REQUEST['diagramId'], Diagramdata::TYPE_DMO);
  580. $diaFile = getStorageFolder() . '/' . $_REQUEST['diagramId'] . '.dmo';
  581. /**When switching from Linux to Windows some files might get corrupt so we will use file_get_contents*/
  582. // $fh = fopen($diaFile, 'r');
  583. // $data = fread($fh, $diagramdata->fileSize);
  584. // fclose($fh);
  585. $data = file_get_contents($diaFile);
  586. print $data;
  587. }
  588. }
  589. /**Loads a temporary diagram*/
  590. function loadTemp() {
  591. if (!is_numeric($_SESSION['userId'])) {
  592. print "Wrong way";
  593. exit();
  594. }
  595. $tempName = $_REQUEST['tempName'];
  596. $diaFile = getStorageFolder() . '/' . $tempName;
  597. $data = file_get_contents($diaFile);
  598. print $data;
  599. }
  600. /**Loads quick start diagram*/
  601. function loadQuickStart() {
  602. if (!is_numeric($_SESSION['userId'])) {
  603. print "Wrong way";
  604. exit();
  605. }
  606. $diaFile = getDataFolder() . '/quickstart.dmo';
  607. $data = file_get_contents($diaFile);
  608. print $data;
  609. }
  610. /** Returns filenames array for uploaded images.
  611. * @author Artyom Pokatilov <artyom.pokatilov@gmail.com>
  612. */
  613. function getUploadedImageFileNames() {
  614. // get folder of uploaded images
  615. $dir = getUploadedImageFolder() . "/";
  616. // index of uploaded image
  617. $i = 0;
  618. // print filenames that matched to images pattern
  619. echo "[";
  620. // find images in upload directory
  621. foreach(glob($dir . "*.{jpg,jpeg,png,gif,bmp}", GLOB_BRACE) as $filename){
  622. if (!is_dir($filename)) {
  623. $filename = str_replace($dir, "", $filename);
  624. // added comma before every filename after first
  625. if ($i > 0) {
  626. echo ",";
  627. }
  628. echo "\"" . $filename . "\"";
  629. $i++;
  630. }
  631. }
  632. echo "]";
  633. }
  634. /**Insert image inside diagram:
  635. 1) Check: get image from URL or get uploaded one.
  636. 2) Save image on server.
  637. 3) Return path to it.
  638. * @author Artyom Pokatilov <artyom.pokatilov@gmail.com>
  639. */
  640. function insertImage() {
  641. if (!is_numeric($_SESSION['userId'])) { //no user logged
  642. print "Not allowed";
  643. exit();
  644. }
  645. $imgSource = $_REQUEST['image-group'];
  646. switch ($imgSource) {
  647. case 'URL':
  648. $imageURL = $_REQUEST['imageURL'];
  649. $fileName = basename($imageURL);
  650. $ext = pathinfo($fileName, PATHINFO_EXTENSION);
  651. $imageFile = get($imageURL);
  652. $imagePath = getUploadedImageFolder() . '/' . $fileName;
  653. if ($imageFile !== false && strlen($imageFile) > 0
  654. && ($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "gif" || $ext == "bmp")) { //file is image
  655. $fh = fopen($imagePath, 'w');
  656. $size = fwrite($fh, $imageFile);
  657. fclose($fh);
  658. } else {
  659. // can't upload image from URL
  660. // call insert image function and send error message
  661. print '<script type="text/javascript">'
  662. . 'window.top.window.insertImage("", "Error uploading image from URL. Check typed URL. Max allowed size is 5MB." )'
  663. . '</script>';
  664. exit();
  665. }
  666. break;
  667. case 'Upload':
  668. $imageFile = $_FILES['imageFile']['tmp_name'];
  669. $fileName = $_FILES["imageFile"]["name"];
  670. $ext = pathinfo($fileName, PATHINFO_EXTENSION);
  671. if (is_uploaded_file($imageFile) && filesize($imageFile) > 0
  672. && ($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "gif" || $ext == "bmp")) { //file is image
  673. $imagePath = getUploadedImageFolder() . '/' . $fileName;
  674. if (!move_uploaded_file($imageFile, $imagePath)) {
  675. // can't move uploaded file
  676. // call insert image function and send error message
  677. print '<script type="text/javascript">'
  678. . 'window.top.window.insertImage("", "Error uploading image. Check chosen file." )'
  679. . '</script>';
  680. exit();
  681. }
  682. } else {
  683. // file isn't uploaded
  684. // call insert image function and send error message
  685. print '<script type="text/javascript">'
  686. . 'window.top.window.insertImage("", "Error uploading image. Check chosen file. " )'
  687. . '</script>';
  688. exit();
  689. }
  690. break;
  691. case 'Reuse':
  692. $fileName = $_REQUEST['reuseImageFile'];
  693. break;
  694. }
  695. // call insert image function and send saved image path to it
  696. print '<script type="text/javascript">'
  697. . 'window.top.window.insertImage("' . $fileName . '")'
  698. . '</script>';
  699. }
  700. /**Imports a Diagram from a file*/
  701. function importDiagramExe() {
  702. if (!is_numeric($_SESSION['userId'])) { //no user logged
  703. print "Not allowed";
  704. exit();
  705. }
  706. $d = new Delegate();
  707. if (is_uploaded_file($_FILES['diagramFile']['tmp_name']) && filesize($_FILES['diagramFile']['tmp_name']) > 0) { //file is fine
  708. // $size = filesize($_FILES['diagramFile']['tmp_name']);
  709. //
  710. // //create entry in 'diagram' table
  711. // $nd = new Diagram();
  712. // $nd->title = "Imported on " + now();
  713. // $nd->public = 0;
  714. // $nd->createdDate = now();
  715. // $nd->lastUpdate = now();
  716. // $nd->size = $size;
  717. //
  718. // $diagramId = $d->diagramCreate($nd);
  719. //
  720. // //create entry in 'diagramdata' table
  721. // $ndd = new Diagramdata();
  722. // $ndd->diagramId = $diagramId;
  723. // $ndd->type = Diagramdata::TYPE_DIA;
  724. // $ndd->fileName = $diagramId . '.dmo';
  725. // $ndd->fileSize = $size;
  726. // $ndd->lastUpdate = now();
  727. //
  728. // $d->diagramdataCreate($ndd);
  729. //compute destination file
  730. $newFileName = 'tmp' . time() . '.dmo';
  731. $destFile = dirname(__FILE__) . '/../data/diagrams/' . $newFileName;
  732. if (move_uploaded_file($_FILES['diagramFile']['tmp_name'], $destFile)) {
  733. redirect('../editor.php?diagramId=' . $newFileName);
  734. }
  735. else{
  736. redirect('../myDiagrams.php?error=Could not import diagram');
  737. }
  738. }
  739. }
  740. function deleteDiagramExe() {
  741. if (!is_numeric($_SESSION['userId'])) {
  742. print "Wrong way";
  743. exit();
  744. }
  745. if (!is_numeric($_REQUEST['diagramId'])) {
  746. print "Wrong diagram id : " . $_REQUEST['diagramId'];
  747. exit();
  748. }
  749. //TODO: usually ONLY the author can delete the diagram
  750. $d = new Delegate();
  751. // print_r($_REQUEST);
  752. // exit();
  753. //delete diagramdata
  754. $diagramDatas = $d->diagramdataGetByDiagramId($_REQUEST['diagramId']);
  755. $storageFolder = getStorageFolder();
  756. foreach($diagramDatas as $diagramData){
  757. //TODO: we can make more tests here
  758. unlink($storageFolder . '/' . $diagramData->fileName);
  759. $d->diagramdataDeleteByDiagramIdAndType($diagramData->diagramId, $diagramData->type);
  760. }
  761. //delete diagram
  762. if ($d->diagramDelete($_REQUEST['diagramId'])) {
  763. addMessage("Diagram deleted");
  764. } else {
  765. addError("Diagram could not be deleted from database");
  766. }
  767. redirect('../myDiagrams.php');
  768. }
  769. function closeQuickStart(){
  770. if (!is_numeric($_SESSION['userId'])) {
  771. print "Wrong way";
  772. exit();
  773. }
  774. $delegate = new Delegate();
  775. $user = $delegate->userGetById($_SESSION['userId']);
  776. $user->tutorial = 0;
  777. $delegate->userUpdate($user);
  778. newDiagramExe();
  779. }
  780. /**Invite a collaborator to a diagram.
  781. * There are 2 kind of invitations:
  782. * 1. outside people - when you send an email with invitation and they will
  783. * first create an account and then accept the invitation
  784. * 2. known people - You already know those people and you invite them.
  785. * They will get an email + an "accept invitation" link on main page.
  786. */
  787. function addUserExe() {
  788. if (!is_numeric($_SESSION['userId'])) {
  789. print "Wrong way";
  790. exit();
  791. }
  792. if (empty($_REQUEST['email'])) {
  793. print "Email is empty";
  794. exit();
  795. }
  796. if (empty($_REQUEST['password'])) {
  797. print "Password is empty";
  798. exit();
  799. }
  800. $d = new Delegate();
  801. $loggedUser = $d->userGetById($_SESSION['userId']);
  802. $user = new User();
  803. $user->email = trim($_REQUEST['email']);
  804. $user->password = md5(trim($_REQUEST['password']));
  805. $user->createdDate = now();
  806. if($d->userCreate($user)){
  807. addMessage("User added");
  808. }
  809. else{
  810. addError("User not added");;
  811. }
  812. //refirect back to collaborators
  813. redirect('../users.php');
  814. }
  815. /**Delete an invitation*/
  816. function cancelInvitationExe() {
  817. if (!is_numeric($_SESSION['userId'])) {
  818. print "Wrong way";
  819. exit();
  820. }
  821. if (empty($_REQUEST['invitationId'])) {
  822. print "Invitation id is wrong";
  823. exit();
  824. }
  825. $d = new Delegate();
  826. $loggedUser = $d->userGetById($_SESSION['userId']);
  827. $invitation = $d->invitationGetById($_REQUEST['invitationId']);
  828. $diagram = $d->diagramGetById($invitation->diagramId);
  829. //are u allocated?
  830. $userdiagram = $d->userdiagramGetByIds($loggedUser->id, $diagram->id);
  831. if (!is_object($userdiagram)) {
  832. addError("Not working on that diagram.");
  833. redirect('../myDiagrams.php');
  834. exit();
  835. }
  836. if($userdiagram->level != Userdiagram::LEVEL_AUTHOR){
  837. addError("You have no rights.");
  838. redirect('../myDiagrams.php');
  839. exit();
  840. }
  841. if($d->invitationDelete($invitation->id)){
  842. addMessage("Invitation deleted");
  843. }
  844. else{
  845. addError("Invitation NOT deleted");
  846. }
  847. redirect('../colaborators.php?diagramId=' . $diagram->id);
  848. }
  849. /**
  850. * Remove a colaborator
  851. */
  852. function removeUser(){
  853. // print_r($_REQUEST);
  854. // exit();
  855. if (!is_numeric($_SESSION['userId'])) {
  856. print("Wrong way");
  857. exit();
  858. }
  859. if(!is_numeric($_REQUEST['userId'])){
  860. print("Wrong user");
  861. exit();
  862. }
  863. $delegate = new Delegate();
  864. if($delegate->userDeleteById($_REQUEST['userId'])){
  865. addMessage("User deleted");
  866. }
  867. else{
  868. addError("User not deleted");
  869. }
  870. redirect('../users.php');
  871. }
  872. /**
  873. * The collaborator remove itself from diagram
  874. */
  875. function removeMeFromDiagram(){
  876. if (!is_numeric($_SESSION['userId'])) {
  877. print("Wrong way");
  878. exit();
  879. }
  880. if(!is_numeric($_REQUEST['diagramId'])){
  881. print("No diagram");
  882. exit();
  883. }
  884. $delegate = new Delegate();
  885. $loggedUser = $delegate->userGetById($_SESSION['userId']);
  886. $userdiagram = $delegate->userdiagramGetByIds($loggedUser->id, $_REQUEST['diagramId']);
  887. if ($userdiagram) {
  888. /**author can not remove itself. he has to delete the diagram*/
  889. if($userdiagram->level == Userdiagram::LEVEL_AUTHOR){
  890. addError("Author can not remove itself from a diagram");
  891. redirect('../myDiagrams.php');
  892. exit();
  893. }
  894. if ($delegate->userdiagramDelete($loggedUser->id, $_REQUEST['diagramId'])) {
  895. addMessage("Removed from diagram");
  896. //TODO: notify author ?
  897. } else {
  898. addError("You were not removed from diagram");
  899. }
  900. redirect('../myDiagrams.php');
  901. }
  902. else{
  903. print('No rights');
  904. exit();
  905. }
  906. }
  907. function info() {
  908. phpinfo();
  909. }
  910. function registerExe(){
  911. if(!validateEmail($_REQUEST['email'])){
  912. addError("Email is wrong");
  913. }
  914. $d = new Delegate();
  915. $existingUser = $d->userGetByEmail(trim($_REQUEST['email']));
  916. if(is_object($existingUser)){
  917. addError("An user with same email already present.");
  918. }
  919. if(!validateString($_REQUEST['password'])){
  920. addError("Password is not ok");
  921. }
  922. if($_REQUEST['password'] != $_REQUEST['password2']){
  923. addError("Passwords do not match");
  924. }
  925. if( !isset ($_REQUEST['invitationToken']) ){
  926. if($_REQUEST['captcha'] != $_SESSION['captcha']){
  927. addError("Code was incorrect");
  928. }
  929. }
  930. if(errors()){
  931. redirect('../../register.php');
  932. exit(0);
  933. }
  934. $user = new User();
  935. $user->email = trim($_REQUEST['email']);
  936. $user->password = md5($_REQUEST['password']);
  937. $user->createdDate = now();
  938. $user->lastLoginDate = now();
  939. $user->lastLoginIp = $_SERVER['REMOTE_ADDR'];
  940. $user->lastBrowserType = $_SERVER['HTTP_USER_AGENT'];
  941. $userId = $d->userCreate($user);
  942. if(is_numeric($userId)){
  943. addMessage("You were registered");
  944. $_SESSION['userId'] = $userId;
  945. $_SESSION['captcha'] = null;
  946. unset($_SESSION['captcha']);
  947. //TODO: if we have a temp diagram we will redirect to save page
  948. if( isset ($_SESSION['tempDiagram']) ){
  949. redirect('../saveDiagram.php');
  950. }
  951. else if( isset($_REQUEST['invitationToken']) ){
  952. $invitation = $d->invitationGetByToken($_REQUEST['invitationToken']);
  953. if(is_object($invitation)){
  954. //find the diagram
  955. $diagram = $d->diagramGetById($invitation->diagramId);
  956. //create userdiagram
  957. $userdiagram = new Userdiagram();
  958. $userdiagram->diagramId = $diagram->id;
  959. $userdiagram->invitedDate = $invitation->createdDate;
  960. $userdiagram->level = Userdiagram::LEVEL_EDITOR;
  961. $userdiagram->status = Userdiagram::STATUS_ACCEPTED;
  962. $userdiagram->userId = $userId;
  963. if(!$d->userdiagramCreate($userdiagram)){
  964. addError("Could not add you to the diagram");
  965. redirect('../editor.php');
  966. exit();
  967. }
  968. //delete invitation
  969. $d->invitationDelete($invitation->id);
  970. //all is fine, redirect to the diagram
  971. redirect('../editor.php?diagramId=' . $diagram->id);
  972. }
  973. else{
  974. }
  975. redirect('../editor.php');
  976. }
  977. else{
  978. redirect('../editor.php');
  979. }
  980. exit(0);
  981. }
  982. else{
  983. addError("User not added ");
  984. redirect('../../register.php');
  985. exit(0);
  986. }
  987. }
  988. //function saveLicense(){
  989. // print_r($_REQUEST);
  990. //
  991. // $serial = $_REQUEST['serial'];
  992. // $host = $_REQUEST['host'];
  993. //
  994. // $d = new Delegate();
  995. //
  996. // $DIAGRAMO = $d->settingsGetByKeyNative('DIAGRAMO');
  997. //
  998. // $url = $DIAGRAMO . "/dcentral/activator.php?serial=$serial&host=$host";
  999. // #print 'URL: ' . $url;
  1000. //
  1001. //
  1002. // $license = get($url);
  1003. //
  1004. // #print 'License: ' . $license;
  1005. // #exit();
  1006. //
  1007. // $d->settingsSaveNative('LICENSE', $license);
  1008. //
  1009. // addMessage("App activated");
  1010. // redirect('../license.php');
  1011. // exit(0);
  1012. //}
  1013. //function editUserExe(){
  1014. //
  1015. // if (!is_numeric($_SESSION['userId'])) {
  1016. // print("Wrong way");
  1017. // exit();
  1018. // }
  1019. //
  1020. // if(!is_numeric($_REQUEST['userId'])){
  1021. // print("Wrong user");
  1022. // exit();
  1023. // }
  1024. //
  1025. // #exit('here');
  1026. // $d = new Delegate();
  1027. //
  1028. // $loggedUser = $d->userGetById($_SESSION['userId']);
  1029. //
  1030. //
  1031. //
  1032. // if(errors()){
  1033. // redirect('../users.php');
  1034. // exit(0);
  1035. // }
  1036. //
  1037. // $user = $d->userGetById($_REQUEST['userId']);
  1038. //
  1039. // if(strlen($_REQUEST['password']) > 0 ){
  1040. // $user->password = md5($_REQUEST['password']) ;
  1041. // if($d->userUpdate($user)){
  1042. // addMessage("User updated");
  1043. // }
  1044. // else{
  1045. // addError("User NOT updated");
  1046. // }
  1047. // }
  1048. //
  1049. //
  1050. // redirect('../users.php');
  1051. //}
  1052. function acceptInvitationExe(){
  1053. if (!is_numeric($_SESSION['userId'])) {
  1054. print("Wrong way");
  1055. exit();
  1056. }
  1057. if( !isset ($_REQUEST['invitationId']) ){
  1058. print("Wrong Invitation");
  1059. exit();
  1060. }
  1061. $d = new Delegate();
  1062. $loggedUser = $d->userGetById($_SESSION['userId']);
  1063. $invitation = $d->invitationGetById($_REQUEST['invitationId']);
  1064. if($invitation->email == $loggedUser->email){ //a match made in stars...how lovely :)
  1065. $diagram = $d->diagramGetById($invitation->diagramId);
  1066. //create userdiagram
  1067. $userdiagram = new Userdiagram();
  1068. $userdiagram->diagramId = $diagram->id;
  1069. $userdiagram->invitedDate = $invitation->createdDate;
  1070. $userdiagram->level = Userdiagram::LEVEL_EDITOR;
  1071. $userdiagram->status = Userdiagram::STATUS_ACCEPTED;
  1072. $userdiagram->userId = $loggedUser->id;
  1073. //store it in DB
  1074. $d->userdiagramCreate($userdiagram);
  1075. //delete invitation
  1076. $d->invitationDelete($invitation->id);
  1077. addMessage("Invitation accepted");
  1078. redirect('../editor.php?diagramId=' . $diagram->id);
  1079. }
  1080. else{
  1081. addError("Nope");
  1082. redirect('../myDiagrams.php');
  1083. }
  1084. }
  1085. function declineInvitationExe(){
  1086. if (!is_numeric($_SESSION['userId'])) {
  1087. print("Wrong way");
  1088. exit();
  1089. }
  1090. if( !isset ($_REQUEST['invitationId']) ){
  1091. print("Wrong Invitation");
  1092. exit();
  1093. }
  1094. $d = new Delegate();
  1095. $loggedUser = $d->userGetById($_SESSION['userId']);
  1096. $invitation = $d->invitationGetById($_REQUEST['invitationId']);
  1097. if($invitation->email == $loggedUser->email){ //a match made in stars...how lovely :)
  1098. $d->invitationDelete($invitation->id);
  1099. addMessage("Invitation declined.");
  1100. }
  1101. redirect('../myDiagrams.php');
  1102. }
  1103. ?>