stateTransfer.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. include_once("../auth.php");
  3. $username = $_SESSION['login'];
  4. if (file_exists("tmp/transfer_sessions/$username") == false){
  5. if (!mkdir("tmp/transfer_sessions/$username", 0777, true)) {
  6. die('ERROR. Unable to create transfer_sessions directories. Please make sure you have correct permission settings.');
  7. }
  8. }
  9. if (isset($_GET['liststate'])){
  10. //List all the stored states
  11. $result = [];
  12. $states = glob("tmp/transfer_sessions/$username/*.json");
  13. foreach ($states as $state){
  14. array_push($result,basename($state));
  15. }
  16. $result = array_reverse($result);
  17. header('Content-Type: application/json');
  18. echo json_encode($result);
  19. exit(0);
  20. }
  21. if (isset($_GET['clearStates'])){
  22. $states = glob("tmp/transfer_sessions/$username/*.json");
  23. foreach ($states as $state){
  24. unlink($state);
  25. }
  26. echo "DONE";
  27. exit(0);
  28. }
  29. if (isset($_GET['restoreFromState'])){
  30. //State will be automatically removed after restore.
  31. $filename = $_GET['restoreFromState'];
  32. if (file_exists("tmp/transfer_sessions/$username/" . $filename)){
  33. //File exists. Read it content and send to the users
  34. $content = file_get_contents("tmp/transfer_sessions/$username/" . $filename);
  35. unlink("tmp/transfer_sessions/$username/" . $filename);
  36. echo $content;
  37. exit(0);
  38. }else{
  39. die("ERROR. State not found.");
  40. }
  41. }
  42. if(isset($_POST['createState']) && $_POST['createState'] != ""){
  43. //Create a new state from the given json data, and return the filename to client
  44. $filename = time() . ".json";
  45. $filepath = "tmp/transfer_sessions/$username/" . $filename;
  46. $content = $_POST['createState'];
  47. file_put_contents($filepath,$content);
  48. echo $filename;
  49. exit(0);
  50. }
  51. ?>
  52. <html>
  53. <head>
  54. <title>Desktop State Transfer Utilities</title>
  55. <link rel="stylesheet" href="../script/tocas/tocas.css">
  56. <script src="../script/tocas/tocas.js"></script>
  57. <script src="../script/jquery.min.js"></script>
  58. <script src="../script/ao_module.js"></script>
  59. <style>
  60. .item{
  61. cursor:pointer;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <br>
  67. <div class="ts container">
  68. <div class="ts segments">
  69. <div class="ts segment">
  70. <div class="ts header">
  71. <i class="desktop icon"></i><i class="exchange icon"></i><i class="desktop icon"></i>Desktop State Transfer
  72. <div class="sub header">Transfer your current Desktop to your other devices</div>
  73. </div>
  74. </div>
  75. <div class="ts segment">
  76. <p>Create a new state from the current Desktop</p>
  77. <div class="ts mini fluid action input">
  78. <input id="generatedDesktopState" type="text" value="" readonly="true">
  79. <button class="ts primary labeled icon button" onClick="generateDesktopState();">
  80. <i class="plus icon"></i>
  81. Create
  82. </button>
  83. </div>
  84. </div>
  85. <div class="ts segment">
  86. <p>Or Double Click list item to restore states</p>
  87. <div id="stateList" class="ts tiny segmented list" style="max-height:230px;overflow-y:auto;">
  88. <div class="item"><i class="sticky note icon"></i>No Desktop State Available</div>
  89. </div>
  90. </div>
  91. <div class="ts negative segment" align="right">
  92. <button class="ts small labeled negative icon button" onClick="clearAllStates();">
  93. <i class="trash outline icon"></i>
  94. Remove All States
  95. </button>
  96. </div>
  97. </div>
  98. </div>
  99. <script>
  100. initStateList();
  101. ao_module_setWindowSize(390,590);
  102. ao_module_setFixedWindowSize();
  103. function selectState(object){
  104. $(".item").removeClass("selected");
  105. $(object).addClass("selected");
  106. }
  107. function initStateList(){
  108. $.get("stateTransfer.php?liststate",function(data){
  109. if (data.length > 0){
  110. //Only update the list if there are more than 1 files
  111. $("#stateList").html("");
  112. for (var i =0; i < data.length; i++){
  113. var d = new Date(parseInt(data[i]) * 1000);
  114. $("#stateList").append('<div class="item" onClick="selectState(this);" ondblclick="restoreState(this);" filename="' + data[i] + '">' + d.toString() + '</div>');
  115. }
  116. }else{
  117. $("#stateList").html('<div class="item"><i class="sticky note icon"></i>No Desktop State Available</div>');
  118. }
  119. });
  120. }
  121. function restoreState(object){
  122. if (confirm("State can only be restore once. Are you sure that you want to restore the previous state?")){
  123. var filename = $(object).attr("filename");
  124. $.get("stateTransfer.php?restoreFromState=" + filename,function(data){
  125. if (data.substring(0,5) == "ERROR"){
  126. ao_module_msgbox("Something went wrong during the Window State Restore process.<br>" + data,"<i class='caution icon'></i>Desktop State Restore");
  127. return;
  128. }
  129. console.log(data);
  130. restoreFloatWindowsFromJSON(data);
  131. $("#generatedDesktopState").val("");
  132. initStateList();
  133. });
  134. }
  135. }
  136. function restoreFloatWindowsFromJSON(jsonString){
  137. var restoreList = JSON.parse(jsonString);
  138. if (restoreList.length > 0){
  139. //Restore the floatWindows from the list by force append the windows into the parent document body
  140. var targetDocument = window.parent.document;
  141. targetDocument = $(targetDocument).find("body");
  142. for (var i=0; i < restoreList.length; i++){
  143. targetDocument.append(restoreList[i]);
  144. var iconTag = $(restoreList[i]).find(".floatWindow").find("i").attr("class").replace("icon","");
  145. var uid = $(restoreList[i]).attr("id");
  146. var src = $(restoreList[i]).find("iframe").attr("src");
  147. injectButtonToMenu(iconTag,uid,src);
  148. }
  149. }
  150. ao_module_close();
  151. }
  152. function injectButtonToMenu(iconTag,uid,src){
  153. window.parent.AppendNewIcon(iconTag,uid,src);
  154. }
  155. function clearAllStates(){
  156. if (confirm("Clear All States - Confirm?")){
  157. $.get("stateTransfer.php?clearStates",function(){
  158. initStateList();
  159. });
  160. }
  161. }
  162. function generateDesktopState(){
  163. //This function get all the floatWindow in the parent windows and its properties so it can be saved as JSON on SERVER side
  164. if (!ao_module_virtualDesktop){
  165. alert("This function can only be usd under Virtual Desktop Environment with FunctionBar Enabled.")
  166. return;
  167. }
  168. var globalwindow = window.parent.document;
  169. var floatWindows = $(globalwindow).find(".floatWindow");
  170. var result = [];
  171. //For each floatWindow, save its HTML as JSON string
  172. for (var i = 0; i < floatWindows.length; i++){
  173. //Record all floatWindows except itself (The state restore window) and the new window cloning code
  174. if ($(floatWindows[i]).parent().attr("id") != "newWindow" && $(floatWindows[i]).parent().attr("id") != ao_module_windowID){
  175. var html = $(floatWindows[i]).parent().prop('outerHTML');
  176. result.push(html);
  177. }
  178. }
  179. result = JSON.stringify(result);
  180. //Push the data to server side and save as JSON
  181. $.post( "stateTransfer.php", { createState: result})
  182. .done(function( data ) {
  183. $("#generatedDesktopState").val(data);
  184. initStateList();
  185. });
  186. }
  187. </script>
  188. </body>
  189. </html>