« lib/auto/share/dist/App-SocialCalc-Multiplayer/simpleedit15.pl
↑ Summary
lib/auto/share/dist/App-SocialCalc-Multiplayer/simpleedit14.pl »
| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # | ||
| 4 | # SocialCalc Server | ||
| 5 | # | ||
| 6 | # This is a simple Perl CGI server that uses SocialCalc in the browser | ||
| 7 | # to edit files stored on the server or a regular personal computer. | ||
| 8 | # | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use CGI qw(:standard); | ||
| 12 | use URI; | ||
| 13 | use HTTP::Daemon; | ||
| 14 | use HTTP::Status; | ||
| 15 | use HTTP::Response; | ||
| 16 | use Socket; | ||
| 17 | use CGI::Cookie; | ||
| 18 | use LWP::UserAgent; | ||
| 19 | |||
| 20 | # use SocialCalcServersideUtilities; | ||
| 21 | |||
| 22 | # Defaults | ||
| 23 | |||
| 24 | my $settingsfile = "socialcalcserversettings.txt"; # file with values for the following | ||
| 25 | my $datadir = "se2-data/"; # The subdirectory of where the code is that holds the socialcalc data files | ||
| 26 | my $versionstr = "0.2.3"; | ||
| 27 | my $titlestr = "SocialCalc Server $versionstr"; | ||
| 28 | my $jsdir = "/sgi/scjs/"; # The subdirectory of the server home page (when run thru CGI) | ||
| 29 | # where the .js files are, and ./images/ subdirectory. | ||
| 30 | my $imagedir = "/images/sc-"; | ||
| 31 | |||
| 32 | # | ||
| 33 | # This whole first section lets this code run either as a CGI script on a server | ||
| 34 | # or standalone on the desktop run from the Perl command line. | ||
| 35 | # | ||
| 36 | # The main processing starts with process_request. | ||
| 37 | # | ||
| 38 | |||
| 39 | if ($ENV{REQUEST_METHOD}) { # being run as a CGI on a server | ||
| 40 | print "Content-type: text/html\n\n"; | ||
| 41 | my $q = new CGI; | ||
| 42 | print process_request($q); | ||
| 43 | exit; | ||
| 44 | } | ||
| 45 | |||
| 46 | # running locally - do mini-server | ||
| 47 | |||
| 48 | my $d = HTTP::Daemon->new ( | ||
| 49 | LocalPort => 6557, | ||
| 50 | Reuse => 1); | ||
| 51 | |||
| 52 | if (!$d) { | ||
| 53 | print "simpleedit could not start on 127.0.0.1:6557\n"; | ||
| 54 | exit; | ||
| 55 | } | ||
| 56 | |||
| 57 | print "socialcalcserver\nAccess at: http://127.0.0.1:6557/\n"; | ||
| 58 | |||
| 59 | while (my $c = $d->accept) { | ||
| 60 | |||
| 61 | # Make sure the request is from our machine | ||
| 62 | |||
| 63 | if ($c) { | ||
| 64 | my ($port, $host) = sockaddr_in(getpeername($c)); | ||
| 65 | if ($host ne inet_aton("127.0.0.1")) { | ||
| 66 | $c->close; # no - ignore request completely | ||
| 67 | undef($c); | ||
| 68 | next; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | # Process the request | ||
| 73 | |||
| 74 | while ((defined $c) && (my $r = $c->get_request)) { | ||
| 75 | if ($r->method eq 'POST' || $r->method eq 'GET') { | ||
| 76 | $c->force_last_request; | ||
| 77 | my $uri = $r->uri; | ||
| 78 | if ($uri =~ /favicon/) { # if this is a request for favicon.ico, ignore | ||
| 79 | $c->send_error(RC_NOT_FOUND); | ||
| 80 | next; | ||
| 81 | } | ||
| 82 | if ($uri =~ /\/quit$/) { | ||
| 83 | $c->send_file_response("quitmessage.html"); | ||
| 84 | $c->close; | ||
| 85 | undef($c); | ||
| 86 | exit; | ||
| 87 | } | ||
| 88 | if ($uri =~ /\/([a-z\-0-9]+)\.(gif|js|css|png)(\?.*)*$/) { # ok request | ||
| 89 | $uri = "$1.$2"; | ||
| 90 | $uri = "images/$uri" if ($2 eq "gif" || $2 eq "png"); | ||
| 91 | # if ($2 eq "js") { | ||
| 92 | # $res->content_type("text/html; charset=UTF-8"); | ||
| 93 | # } | ||
| 94 | $c->send_file_response($uri); | ||
| 95 | next; | ||
| 96 | } | ||
| 97 | |||
| 98 | my $resp=""; | ||
| 99 | if ($r->method eq 'POST') { | ||
| 100 | my $q = new CGI($r->content()); | ||
| 101 | $resp = process_request($q) | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | my $q = new CGI($r->uri->query()); | ||
| 105 | $resp = process_request($q) | ||
| 106 | } | ||
| 107 | my $res = new HTTP::Response(200); | ||
| 108 | $res->content_type("text/html; charset=UTF-8"); | ||
| 109 | $res->expires("-1d"); | ||
| 110 | $res->content($resp); | ||
| 111 | $c->send_response($res); | ||
| 112 | } | ||
| 113 | |||
| 114 | else { | ||
| 115 | $c->send_error(RC_FORBIDDEN); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | $c->close; | ||
| 120 | undef($c); | ||
| 121 | } | ||
| 122 | |||
| 123 | # | ||
| 124 | # Main routine starts here: | ||
| 125 | # | ||
| 126 | |||
| 127 | sub process_request { | ||
| 128 | |||
| 129 | my ($request) = @_; | ||
| 130 | my $q = new CGI($request); | ||
| 131 | |||
| 132 | my $response; | ||
| 133 | |||
| 134 | my ($statusmessage); | ||
| 135 | |||
| 136 | if (-e $settingsfile) { | ||
| 137 | open (SETTINGSFILE, $settingsfile); | ||
| 138 | while (my $line = <SETTINGSFILE>) { | ||
| 139 | chomp $line; $line =~ s/\r//g; | ||
| 140 | my @sline = split /\:/, $line; | ||
| 141 | $datadir = $sline[1] if ($sline[0] eq "datadir"); | ||
| 142 | $jsdir = $sline[1] if ($sline[0] eq "jsdir"); | ||
| 143 | } | ||
| 144 | close SETTINGSFILE; | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | if ($q->param('setup')) { # got settings - do this once | ||
| 148 | $datadir = $q->param('datadir'); | ||
| 149 | $jsdir = $q->param('jsdir'); | ||
| 150 | open (SETTINGSFILE, ">$settingsfile"); | ||
| 151 | print SETTINGSFILE <<"EOF"; | ||
| 152 | # SocialcalcServer settings | ||
| 153 | version:1.0 | ||
| 154 | datadir:$datadir | ||
| 155 | jsdir:$jsdir | ||
| 156 | EOF | ||
| 157 | close SETTINGSFILE; | ||
| 158 | mkdir $datadir; | ||
| 159 | } | ||
| 160 | else { | ||
| 161 | $response = <<"EOF"; | ||
| 162 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | ||
| 163 | "http://www.w3.org/TR/html4/strict.dtd"> | ||
| 164 | <html> | ||
| 165 | <head> | ||
| 166 | <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| 167 | <title>$titlestr</title> | ||
| 168 | <style> | ||
| 169 | body, td, input, texarea | ||
| 170 | {font-family:verdana,helvetica,sans-serif;font-size:small;} | ||
| 171 | .smaller {font-size:smaller;} | ||
| 172 | </style> | ||
| 173 | </head> | ||
| 174 | <body> | ||
| 175 | <form action="" method="POST"> | ||
| 176 | <div style="padding:6px;background-color:#80A9F3;"> | ||
| 177 | <div style="font-weight:bold;color:white;">SIMPLE SYSTEM FOR EDITING SOCIALCALC FILES</div> | ||
| 178 | <div style="color:#FDD;font-weight:bold;">Initial Setup</div> | ||
| 179 | <div style="padding:6px;background-color:#80A9F3;"> | ||
| 180 | Name of subdirectory of where the code is to hold spreadsheet data files: | ||
| 181 | <input name="datadir" type="text" value="scdata/"><br> | ||
| 182 | Name of subdirectory of server home page that has the JavaScript files and the images subdirectory | ||
| 183 | (only needed if running on Apache, etc., not when running standalone from the | ||
| 184 | command line in Perl): <input name="jsdir" type="text" value="/sgi/scjs/"><br> | ||
| 185 | <input type="submit" name="setup" value="Save"> | ||
| 186 | </div> | ||
| 187 | <br> | ||
| 188 | </form> | ||
| 189 | </div> | ||
| 190 | <br> | ||
| 191 | </body> | ||
| 192 | </html> | ||
| 193 | EOF | ||
| 194 | |||
| 195 | return $response; | ||
| 196 | } | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | my $pagename = $q->param('pagename'); | ||
| 201 | |||
| 202 | if ($q->param('newpage')) { | ||
| 203 | $pagename = $q->param('newpagename'); | ||
| 204 | } | ||
| 205 | |||
| 206 | $pagename =~ s/[^a-zA-Z0-9_\-\.]*//g; | ||
| 207 | if (!$pagename) { | ||
| 208 | $pagename = "[None]"; | ||
| 209 | return do_displaypage($q, $pagename, $statusmessage); | ||
| 210 | } | ||
| 211 | |||
| 212 | if ($q->param("editpage") || $q->param("editrawpage")) { # when one of the "editpage" buttons is pressed | ||
| 213 | return do_editpage($q, $pagename, $statusmessage); | ||
| 214 | } | ||
| 215 | |||
| 216 | if ($q->param("edit")) { # "edit" pressed | ||
| 217 | return start_editsheet($pagename, $q, $statusmessage); | ||
| 218 | } | ||
| 219 | if ($q->param("view")) { # "view" pressed | ||
| 220 | return start_viewsheet($pagename, $q, $statusmessage); | ||
| 221 | } | ||
| 222 | |||
| 223 | if ($q->param('savespreadsheet')) { # save the edited spreadsheet | ||
| 224 | my $pagestr = $q->param('newstr'); | ||
| 225 | |||
| 226 | open (PAGEFILEOUT, ">$datadir$pagename"); | ||
| 227 | print PAGEFILEOUT $pagestr; | ||
| 228 | close PAGEFILEOUT; | ||
| 229 | $statusmessage = | ||
| 230 | "Saved updated '$pagename'.<br>"; | ||
| 231 | } | ||
| 232 | |||
| 233 | if ($q->param('filecontents')) { # return contents of file | ||
| 234 | my $fileurl = $q->param('filecontents'); | ||
| 235 | |||
| 236 | open (PAGEFILEIN, "$fileurl"); | ||
| 237 | my $filestr; | ||
| 238 | while (my $line = <PAGEFILEIN>) { | ||
| 239 | $filestr .= $line; | ||
| 240 | } | ||
| 241 | close PAGEFILEIN; | ||
| 242 | print $filestr; | ||
| 243 | return $filestr; | ||
| 244 | |||
| 245 | } | ||
| 246 | |||
| 247 | $response = do_displaypage($q, $pagename, $statusmessage); # Otherwise, display page | ||
| 248 | |||
| 249 | return $response; | ||
| 250 | |||
| 251 | } | ||
| 252 | |||
| 253 | # | ||
| 254 | # $response = do_displaypage($q, $pagename, $statusmessage) - Display page | ||
| 255 | # | ||
| 256 | |||
| 257 | sub do_displaypage { | ||
| 258 | |||
| 259 | my ($q, $pagename, $statusmessage) = @_; | ||
| 260 | my $response; | ||
| 261 | |||
| 262 | $response = <<"EOF"; | ||
| 263 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | ||
| 264 | "http://www.w3.org/TR/html4/strict.dtd"> | ||
| 265 | <html> | ||
| 266 | <head> | ||
| 267 | <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| 268 | <title>$titlestr</title> | ||
| 269 | <style> | ||
| 270 | body, td, input, texarea | ||
| 271 | {font-family:verdana,helvetica,sans-serif;font-size:small;} | ||
| 272 | .smaller {font-size:smaller;} | ||
| 273 | </style> | ||
| 274 | <script> | ||
| 275 | function doedit(p) { | ||
| 276 | document.getElementById("pagename").value = p; | ||
| 277 | document.getElementById("edit").value = "yes"; | ||
| 278 | } | ||
| 279 | |||
| 280 | function doview(p) { | ||
| 281 | document.getElementById("pagename").value = p; | ||
| 282 | document.getElementById("view").value = "yes"; | ||
| 283 | } | ||
| 284 | |||
| 285 | </script> | ||
| 286 | </head> | ||
| 287 | <body> | ||
| 288 | <form action="" method="POST"> | ||
| 289 | <div style="padding:6px;background-color:#80A9F3;"> | ||
| 290 | <div style="font-weight:bold;color:white;">SIMPLE SYSTEM FOR EDITING SOCIALCALC FILES</div> | ||
| 291 | <div style="color:#FDD;font-weight:bold;">$statusmessage </div> | ||
| 292 | <div style="padding:6px;background-color:#80A9F3;"> | ||
| 293 | <div style="font-weight:bold;font-size:smaller;">Pages:</div> | ||
| 294 | <div> | ||
| 295 | EOF | ||
| 296 | |||
| 297 | my @pagefiles = glob("$datadir*"); # Get list of all pages | ||
| 298 | for (my $pnum=0; $pnum <= $#pagefiles; $pnum++) { | ||
| 299 | $pagefiles[$pnum] =~ m/^(?:.*\/){0,1}(.*)$/; | ||
| 300 | $response .= qq!<span style="font-size:smaller;">$1</span> <input class="smaller" type="submit" value="Edit" onclick="doedit('$1');"> !; | ||
| 301 | $response .= qq! <input class="smaller" type="submit" value="View" onclick="doview('$1');"><br>!; | ||
| 302 | } | ||
| 303 | |||
| 304 | $response .= <<"EOF"; | ||
| 305 | <input id="pagename" name="pagename" type="hidden" value=""> | ||
| 306 | <input id="edit" name="edit" type="hidden" value=""> | ||
| 307 | <input id="view" name="view" type="hidden" value=""> | ||
| 308 | </div> | ||
| 309 | <br> | ||
| 310 | </form> | ||
| 311 | <form action="" method="POST"> | ||
| 312 | <input type="text" name="newpagename" value=""> | ||
| 313 | <input type="submit" name="newpage" value="Create New Page"> | ||
| 314 | <input type="hidden" name="edit" value="yes"> | ||
| 315 | </form> | ||
| 316 | </div> | ||
| 317 | <br> | ||
| 318 | </body> | ||
| 319 | </html> | ||
| 320 | EOF | ||
| 321 | |||
| 322 | return $response; | ||
| 323 | } | ||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | # | ||
| 328 | # start_editsheet($pagename, $q, $statusmessage) | ||
| 329 | # - render initial editing display | ||
| 330 | # | ||
| 331 | |||
| 332 | sub start_editsheet { | ||
| 333 | |||
| 334 | my ($pagename, $q, $statusmessage) = @_; | ||
| 335 | |||
| 336 | my ($response, $sheetstr); | ||
| 337 | |||
| 338 | open (PAGEFILEIN, "$datadir$pagename"); | ||
| 339 | |||
| 340 | while (my $line = <PAGEFILEIN>) { | ||
| 341 | $sheetstr .= $line; | ||
| 342 | } | ||
| 343 | $sheetstr = special_chars($sheetstr); | ||
| 344 | |||
| 345 | close PAGEFILEIN; | ||
| 346 | |||
| 347 | $response = <<"EOF"; # output page with edit JS code | ||
| 348 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | ||
| 349 | <html> | ||
| 350 | <head> | ||
| 351 | <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| 352 | <title>$titlestr - $pagename</title> | ||
| 353 | <script type="text/javascript" src="${jsdir}socialcalcconstants.js"></script> | ||
| 354 | <script type="text/javascript" src="${jsdir}socialcalc-3.js"></script> | ||
| 355 | <script type="text/javascript" src="${jsdir}socialcalctableeditor.js"></script> | ||
| 356 | <script type="text/javascript" src="${jsdir}formatnumber2.js"></script> | ||
| 357 | <script type="text/javascript" src="${jsdir}formula1.js"></script> | ||
| 358 | <script type="text/javascript" src="${jsdir}socialcalcpopup.js"></script> | ||
| 359 | <script type="text/javascript" src="${jsdir}socialcalcspreadsheetcontrol.js"></script> | ||
| 360 | <style> | ||
| 361 | body, td, input, texarea | ||
| 362 | {font-family:verdana,helvetica,sans-serif;font-size:small;} | ||
| 363 | .smaller {font-size:smaller;} | ||
| 364 | </style> | ||
| 365 | </head> | ||
| 366 | <body> | ||
| 367 | <form name="f0" action="" method="POST"> | ||
| 368 | <div style="padding:6px;background-color:#80A9F3;"> | ||
| 369 | <div style="font-weight:bold;color:white;">SIMPLE SYSTEM FOR EDITING SOCIALCALC FILES</div> | ||
| 370 | <div style="color:#FDD;font-weight:bold;">$statusmessage </div> | ||
| 371 | <div style="margin-bottom:6px;">Editing page: <span style="font-style:italic;font-weight:bold;">$pagename</span></div> | ||
| 372 | <input class="smaller" type="submit" name="savespreadsheet" value="Save" onclick="dosave();"> | ||
| 373 | <input class="smaller" type="submit" name="cancelspreadsheet" value="Cancel"> | ||
| 374 | <textarea name="savestr" id="sheetdata" style="display:none;">$sheetstr</textarea> | ||
| 375 | <input type="hidden" name="newstr" id="newdata" value=""> | ||
| 376 | <input type="hidden" name="pagename" value="$pagename"> | ||
| 377 | </div> | ||
| 378 | </form> | ||
| 379 | <div id="tableeditor" style="margin:8px 0px 10px 0px;">editor goes here</div> | ||
| 380 | <script> | ||
| 381 | |||
| 382 | function dosave() { | ||
| 383 | var sheetstr = spreadsheet.CreateSpreadsheetSave(); | ||
| 384 | document.getElementById("newdata").value = sheetstr; | ||
| 385 | } | ||
| 386 | |||
| 387 | // start everything | ||
| 388 | |||
| 389 | SocialCalc.Constants.defaultImagePrefix = "$jsdir$imagedir"; | ||
| 390 | SocialCalc.Popup.imagePrefix = "$jsdir$imagedir"; | ||
| 391 | |||
| 392 | var spreadsheet = new SocialCalc.SpreadsheetControl(); | ||
| 393 | spreadsheet.InitializeSpreadsheetControl("tableeditor", 0, 0, 0); | ||
| 394 | |||
| 395 | SocialCalc.SheetCommandInfo.CmdExtensionCallbacks.docmd = {func:docmdext, data:spreadsheet}; | ||
| 396 | SocialCalc.SheetCommandInfo.CmdExtensionCallbacks.loadclipboard = {func:doloadclipboardext, data:spreadsheet}; | ||
| 397 | |||
| 398 | var savestr = document.getElementById("sheetdata").value; | ||
| 399 | var parts = spreadsheet.DecodeSpreadsheetSave(savestr); | ||
| 400 | if (parts) { | ||
| 401 | if (parts.sheet) { | ||
| 402 | spreadsheet.sheet.ResetSheet(); | ||
| 403 | spreadsheet.ParseSheetSave(savestr.substring(parts.sheet.start, parts.sheet.end)); | ||
| 404 | } | ||
| 405 | if (parts.edit) { | ||
| 406 | spreadsheet.editor.LoadEditorSettings(savestr.substring(parts.edit.start, parts.edit.end)); | ||
| 407 | } | ||
| 408 | if (parts.startupmacro) { | ||
| 409 | spreadsheet.editor.EditorScheduleSheetCommands(savestr.substring(parts.startupmacro.start, parts.startupmacro.end), false, true); | ||
| 410 | } | ||
| 411 | } | ||
| 412 | if (spreadsheet.sheet.attribs.recalc=="off") { | ||
| 413 | spreadsheet.sheet.attribs.needsrecalc = "yes"; // default turn it on | ||
| 414 | spreadsheet.ExecuteCommand('redisplay', ''); | ||
| 415 | } | ||
| 416 | else { | ||
| 417 | spreadsheet.ExecuteCommand('recalc', ''); | ||
| 418 | } | ||
| 419 | |||
| 420 | function docmdext (name, data, sheet, cmd, saveundo) { | ||
| 421 | |||
| 422 | var cmdstr = cmd.RestOfString(); | ||
| 423 | data.editor.EditorScheduleSheetCommands(cmdstr, false, false); | ||
| 424 | SocialCalc.SheetCommandInfo.cmdextensionbusy = "Do Cmd Ext "+cmdstr; | ||
| 425 | |||
| 426 | window.setTimeout(function(){SocialCalc.ResumeFromCmdExtension();}, 100); | ||
| 427 | |||
| 428 | } | ||
| 429 | |||
| 430 | function doloadclipboardext (name, data, sheet, cmd, saveundo) { | ||
| 431 | |||
| 432 | var cmdstr = cmd.RestOfString(); | ||
| 433 | SocialCalc.SheetCommandInfo.cmdextensionbusy = "Load Clipboard Ext "+cmdstr; | ||
| 434 | |||
| 435 | loaddata(cmdstr); | ||
| 436 | |||
| 437 | // window.setTimeout(function(){SocialCalc.ResumeFromCmdExtension();}, 100); | ||
| 438 | // SocialCalc.ResumeFromCmdExtension(); | ||
| 439 | } | ||
| 440 | |||
| 441 | var loaddatatimerobj; | ||
| 442 | |||
| 443 | function loaddata(url) { | ||
| 444 | |||
| 445 | var loadscript = document.createElement("script"); | ||
| 446 | loadscript.type = "text/javascript"; | ||
| 447 | loadscript.src = url+"?"+((new Date()).getTime()+'0'); | ||
| 448 | document.body.appendChild(loadscript); | ||
| 449 | |||
| 450 | loaddatatimerobj = window.setTimeout(loaddatatimeout, 4000); | ||
| 451 | |||
| 452 | } | ||
| 453 | |||
| 454 | function doloaddataload(val) { | ||
| 455 | |||
| 456 | if (loaddatatimerobj) { | ||
| 457 | window.clearTimeout(loaddatatimerobj); | ||
| 458 | loaddatatimerobj = null; | ||
| 459 | } | ||
| 460 | |||
| 461 | var sview = SocialCalc.GetSpreadsheetControlObject(); | ||
| 462 | parts = sview.DecodeSpreadsheetSave(val); | ||
| 463 | if (parts) { | ||
| 464 | if (parts.sheet) { | ||
| 465 | SocialCalc.Clipboard.clipboard = SocialCalc.decodeFromSave(val.substring(parts.sheet.start, parts.sheet.end)); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | // window.setTimeout(function(){SocialCalc.ResumeFromCmdExtension();}, 100); | ||
| 469 | SocialCalc.ResumeFromCmdExtension(); | ||
| 470 | } | ||
| 471 | |||
| 472 | function loaddatatimeout() { | ||
| 473 | |||
| 474 | if (loaddatatimerobj) { | ||
| 475 | window.clearTimeout(loaddatatimerobj); | ||
| 476 | loaddatatimerobj = null; | ||
| 477 | } | ||
| 478 | |||
| 479 | window.setTimeout(function(){SocialCalc.ResumeFromCmdExtension();}, 10); | ||
| 480 | |||
| 481 | } | ||
| 482 | |||
| 483 | // Remote data lookup demo code | ||
| 484 | |||
| 485 | var loadtimerobj; | ||
| 486 | |||
| 487 | function loadsheet(sheetname) { | ||
| 488 | |||
| 489 | var matches = sheetname.match(/^\\{scdata\\:\\s+(.+?)\\}\$/); // {scdata: URL w/o http://) | ||
| 490 | |||
| 491 | if (!matches) { | ||
| 492 | return false; | ||
| 493 | } | ||
| 494 | |||
| 495 | var loadscript = document.createElement("script"); | ||
| 496 | loadscript.type = "text/javascript"; | ||
| 497 | loadscript.src = "http://"+matches[1]+"?"+((new Date()).getTime()+'0'); | ||
| 498 | document.body.appendChild(loadscript); | ||
| 499 | |||
| 500 | loadtimerobj = window.setTimeout(loadframetimeout, 4000); | ||
| 501 | |||
| 502 | return true; | ||
| 503 | } | ||
| 504 | |||
| 505 | SocialCalc.RecalcInfo.LoadSheet = loadsheet; | ||
| 506 | |||
| 507 | function doloadframeload(val) { | ||
| 508 | |||
| 509 | if (loadtimerobj) { | ||
| 510 | window.clearTimeout(loadtimerobj); | ||
| 511 | loadtimerobj = null; | ||
| 512 | } | ||
| 513 | |||
| 514 | var sview = SocialCalc.GetSpreadsheetControlObject(); | ||
| 515 | parts = sview.DecodeSpreadsheetSave(val); | ||
| 516 | if (parts) { | ||
| 517 | if (parts.sheet) { | ||
| 518 | SocialCalc.RecalcLoadedSheet(null, val.substring(parts.sheet.start, parts.sheet.end), true); // notify recalc loop | ||
| 519 | } | ||
| 520 | } | ||
| 521 | if (val=="") { | ||
| 522 | SocialCalc.RecalcLoadedSheet(null, "", true); // notify recalc loop that it's not available, but that we tried | ||
| 523 | } | ||
| 524 | } | ||
| 525 | |||
| 526 | function loadframetimeout() { | ||
| 527 | |||
| 528 | if (loadtimerobj) { | ||
| 529 | window.clearTimeout(loadtimerobj); | ||
| 530 | loadtimerobj = null; | ||
| 531 | } | ||
| 532 | |||
| 533 | SocialCalc.RecalcLoadedSheet(null, "", true); // notify recalc loop that it's not available, but that we tried | ||
| 534 | |||
| 535 | } | ||
| 536 | |||
| 537 | </script> | ||
| 538 | </body> | ||
| 539 | </html> | ||
| 540 | EOF | ||
| 541 | |||
| 542 | return $response; | ||
| 543 | |||
| 544 | } | ||
| 545 | |||
| 546 | |||
| 547 | # | ||
| 548 | # start_viewsheet($pagename, $q, $statusmessage) | ||
| 549 | # - render sheet | ||
| 550 | # | ||
| 551 | |||
| 552 | sub start_viewsheet { | ||
| 553 | |||
| 554 | my ($pagename, $q, $statusmessage) = @_; | ||
| 555 | |||
| 556 | my ($response, $sheetstr); | ||
| 557 | |||
| 558 | open (PAGEFILEIN, "$datadir$pagename"); | ||
| 559 | |||
| 560 | while (my $line = <PAGEFILEIN>) { | ||
| 561 | $sheetstr .= $line; | ||
| 562 | } | ||
| 563 | $sheetstr = special_chars($sheetstr); | ||
| 564 | |||
| 565 | $response = <<"EOF"; # output page with JS code to render | ||
| 566 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | ||
| 567 | <html> | ||
| 568 | <head> | ||
| 569 | <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| 570 | <title>$titlestr - $pagename</title> | ||
| 571 | <script type="text/javascript" src="${jsdir}socialcalcconstants.js"></script> | ||
| 572 | <script type="text/javascript" src="${jsdir}socialcalc-3.js"></script> | ||
| 573 | <script type="text/javascript" src="${jsdir}socialcalctableeditor.js"></script> | ||
| 574 | <script type="text/javascript" src="${jsdir}formatnumber2.js"></script> | ||
| 575 | <script type="text/javascript" src="${jsdir}formula1.js"></script> | ||
| 576 | <script type="text/javascript" src="${jsdir}socialcalcpopup.js"></script> | ||
| 577 | <script type="text/javascript" src="${jsdir}socialcalcspreadsheetcontrol.js"></script> | ||
| 578 | <style> | ||
| 579 | body, td, input, texarea | ||
| 580 | {font-family:verdana,helvetica,sans-serif;font-size:small;} | ||
| 581 | .smaller {font-size:smaller;} | ||
| 582 | </style> | ||
| 583 | </head> | ||
| 584 | <body> | ||
| 585 | EOF | ||
| 586 | |||
| 587 | if (!$q->param('print')) { | ||
| 588 | $response .= <<"EOF"; | ||
| 589 | <div style="padding:6px;background-color:#80A9F3;"> | ||
| 590 | <div style="font-weight:bold;color:white;">SIMPLE SYSTEM FOR EDITING SOCIALCALC FILES</div> | ||
| 591 | <div style="color:#FDD;font-weight:bold;">$statusmessage </div> | ||
| 592 | <div style="margin-bottom:6px;">Editing page: <span style="font-style:italic;font-weight:bold;">$pagename</span></div> | ||
| 593 | <form name="f0" action="" method="POST"> | ||
| 594 | <input class="smaller" type="submit" name="doneview" value="Done"> | ||
| 595 | </form> | ||
| 596 | <a href="?pagename=$pagename&view=yes&print=yes" target="_blank">Printer-friendly</a> | ||
| 597 | </div> | ||
| 598 | EOF | ||
| 599 | } | ||
| 600 | |||
| 601 | $response .= <<"EOF"; | ||
| 602 | <textarea name="savestr" id="sheetdata" style="display:none;">$sheetstr</textarea> | ||
| 603 | <div id="main">Sheet goes here</div> | ||
| 604 | <script> | ||
| 605 | |||
| 606 | // start everything | ||
| 607 | |||
| 608 | var spreadsheet = new SocialCalc.SpreadsheetControl(); | ||
| 609 | |||
| 610 | SocialCalc.Constants.defaultImagePrefix = "$jsdir$imagedir"; | ||
| 611 | SocialCalc.Popup.imagePrefix = "$jsdir$imagedir"; | ||
| 612 | |||
| 613 | var savestr = document.getElementById("sheetdata").value; | ||
| 614 | var parts = spreadsheet.DecodeSpreadsheetSave(savestr); | ||
| 615 | if (parts) { | ||
| 616 | if (parts.sheet) { | ||
| 617 | spreadsheet.sheet.ResetSheet(); | ||
| 618 | spreadsheet.ParseSheetSave(savestr.substring(parts.sheet.start, parts.sheet.end)); | ||
| 619 | } | ||
| 620 | } | ||
| 621 | |||
| 622 | var html = spreadsheet.CreateSheetHTML(); | ||
| 623 | document.getElementById("main").innerHTML = html; | ||
| 624 | |||
| 625 | </script> | ||
| 626 | </body> | ||
| 627 | </html> | ||
| 628 | EOF | ||
| 629 | |||
| 630 | return $response; | ||
| 631 | |||
| 632 | } | ||
| 633 | |||
| 634 | |||
| 635 | # # # # # # # # # # | ||
| 636 | # special_chars($string) | ||
| 637 | # | ||
| 638 | # Returns $estring where &, <, >, " are HTML escaped | ||
| 639 | # | ||
| 640 | |||
| 641 | sub special_chars { | ||
| 642 | my $string = shift @_; | ||
| 643 | |||
| 644 | $string =~ s/&/&/g; | ||
| 645 | $string =~ s/</</g; | ||
| 646 | $string =~ s/>/>/g; | ||
| 647 | $string =~ s/"/"/g; | ||
| 648 | |||
| 649 | return $string; | ||
| 650 | } | ||
| 651 | |||
| 652 | |||
| 653 | # | ||
| 654 | # decode_from_ajax($string) - Returns a string with | ||
| 655 | # \n, \b, and \c escaped to \n, \, and : | ||
| 656 | # | ||
| 657 | |||
| 658 | sub decode_from_ajax { | ||
| 659 | my $string = shift @_; | ||
| 660 | |||
| 661 | $string =~ s/\\n/\n/g; | ||
| 662 | $string =~ s/\\c/:/g; | ||
| 663 | $string =~ s/\\b/\\/g; | ||
| 664 | |||
| 665 | return $string; | ||
| 666 | } | ||
| 667 | |||
| 668 | |||
| 669 | # | ||
| 670 | # encode_for_ajax($string) - Returns a string with | ||
| 671 | # \n, \, :, and ]]> escaped to \n, \b, \c, and \e | ||
| 672 | # | ||
| 673 | |||
| 674 | sub encode_for_ajax { | ||
| 675 | my $string = shift @_; | ||
| 676 | |||
| 677 | $string =~ s/\\/\\b/g; | ||
| 678 | $string =~ s/\n/\\n/g; | ||
| 679 | $string =~ s/\r//g; | ||
| 680 | $string =~ s/:/\\c/g; | ||
| 681 | $string =~ s/]]>/\\e/g; | ||
| 682 | |||
| 683 | return $string; | ||
| 684 | } | ||
| 685 |