source: main/trunk/greenstone2/web/script/gsajaxapi.js@ 27318

Last change on this file since 27318 was 27318, checked in by ak19, 11 years ago

Authentication at perl level for when setting user-added comments. 1. metadata-server.plnow encrypts the key, so that it can be checked against what's in the key db. 2. gdslCGI.pm now has an encrypt_key subroutine. 3. baseaction.pm's authentication_enabled is turned on and the authenticate_user() subroutine now follows recpt's userdb.cpp::check_key by first checking for a given key when no password is given, and if the key validates and isn't stale, then its timestamp in the key db is updated. The code for checking the group that the user belongs to (which had been commented out since user comments can be set by anyone with a GS account, they don't need to belong to a collection editing group) has been moved to a new function called check_group, with the line calling it commented out. 4. style.dm passes in un and ky cgi args to the gsapi object's constructor. 5. gsajaxapi.js's constructor takes the un and ky parameters and then uses these in the Get and Post methods when making calls to metadata-server.pl.

File size: 14.7 KB
Line 
1
2function GSAjaxAPI(gwcgi,collect,un,ky)
3{
4 var gwcgi_ = gwcgi;
5 var collect_ = collect;
6 var un_ = un;
7 var ky_ = ky;
8
9
10 this.fullDomainURL = function(localURL)
11 {
12 return window.location.protocol+'//'+window.location.host+localURL;
13 }
14
15 this.apiURL = function(apiProg)
16 {
17 //get the location of the cgi program
18 splitpos = gwcgi_.lastIndexOf("/");
19
20 var mdserver;
21 if (splitpos >= 0) {
22 mdserver = gwcgi.substring(0,(splitpos+1)) + apiProg;
23 }
24 else {
25 mdserver = apiProg;
26 }
27
28 return mdserver;
29 }
30
31 this.metadataserverURL = function()
32 {
33 return this.apiURL("metadata-server.pl");
34 }
35
36 this.indexserverURL = function()
37 {
38 return this.apiURL("index-server.pl");
39 }
40
41 this.buildserverURL = function()
42 {
43 return this.apiURL("build-server.pl");
44 }
45
46 this.explodeserverURL = function()
47 {
48 return this.apiURL("explode-server.pl");
49 }
50
51 this.myspaceserverURL = function()
52 {
53 return this.apiURL("myspace-server.pl");
54 }
55
56
57 this.urlGetAsync = function(url,callback)
58 {
59 var xmlHttp;
60 try {
61 // Firefox, Opera 8.0+, Safari
62 xmlHttp=new XMLHttpRequest();
63 }
64 catch (e) {
65 // Internet Explorer
66 try {
67 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
68 }
69 catch (e) {
70 try {
71 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
72 }
73 catch (e) {
74 alert("Your browser does not support AJAX!");
75 return false;
76 }
77 }
78 }
79
80 var typeof_callback = typeof(callback);
81 if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
82 var locid = callback;
83
84 xmlHttp.onreadystatechange=function() {
85 if(xmlHttp.readyState==4) {
86 if (locelem != null) {
87 var locelem = document.getElementById(locid);
88
89 locelem.innerHTML = xmlHttp.responseText;
90 }
91 }
92 }
93 }
94 else if (typeof_callback == "function") {
95 xmlHttp.onreadystatechange=function() {
96 if(xmlHttp.readyState==4) {
97 callback(xmlHttp);
98 }
99 }
100 }
101 else {
102 alert("Unrecognized type of callback value: " + typeof_callback);
103 }
104
105 if(un_ != null) {
106 url += "&un=" + un_;
107 }
108 if(ky_ != null) {
109 url += "&ky=" + ky_;
110 }
111
112 xmlHttp.open("GET",url,true);
113 xmlHttp.send(null);
114 }
115
116
117 this.urlGetSync = function(url)
118 {
119 // alert("url = " + url);
120
121 var xmlHttp;
122 try {
123 // Firefox, Opera 8.0+, Safari
124 xmlHttp=new XMLHttpRequest();
125 }
126 catch (e) {
127 // Internet Explorer
128 try {
129 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
130 }
131 catch (e) {
132 try {
133 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
134 }
135 catch (e) {
136 alert("Your browser does not support AJAX!");
137 return false;
138 }
139 }
140 }
141
142 if(un_ != null) {
143 url += "&un=" + un_;
144 }
145 if(ky_ != null) {
146 url += "&ky=" + ky_;
147 }
148
149 xmlHttp.open("GET",url,false);
150 xmlHttp.send(null);
151
152 // alert("response = '" + xmlHttp.responseText + "'");
153
154 return xmlHttp.responseText;
155 }
156
157 // New, an Ajax Synchronous Post method.
158// http://www.degraeve.com/reference/simple-ajax-example.php
159// Async vs Sync: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
160// Also:
161// http://stackoverflow.com/questions/6312447/in-an-ajax-post-do-i-need-to-urlencode-parameters-before-sending
162// http://api.jquery.com/jQuery.post/
163// http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
164 this.urlPostSync = function(scriptURL, params) {
165 var xmlHttp=false;
166 try {
167 // Firefox, Opera 8.0+, Safari
168 xmlHttp=new XMLHttpRequest();
169 }
170 catch (e) {
171 // Internet Explorer
172 try {
173 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
174 }
175 catch (e) {
176 try {
177 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
178 }
179 catch (e) {
180 alert("Your browser does not support AJAX!");
181 return false;
182 }
183 }
184 }
185
186 // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
187 xmlHttp.open('POST', scriptURL, false); // false means synchronous
188 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
189
190 // If asynchronous:
191// xmlHttp.onreadystatechange = function() {
192// if (xmlHttp.readyState == 4) {
193// updatepage(xmlHttp.responseText);
194// }
195// }
196
197 if(un_ != null) {
198 params += "&un=" + un_;
199 }
200 if(ky_ != null) {
201 params += "&ky=" + ky_;
202 }
203
204 xmlHttp.send(params); // needs to be escaped/encoded
205
206 //alert(scriptURL + "?" + params);
207 //alert(xmlHttp.responseText); // if synchronous, process xmlHttp.responseText AFTER send() call
208 return xmlHttp.responseText;
209}
210
211 this.setLiveMetadata = function(id,metaname,metavalue)
212 {
213 var mdserver = this.metadataserverURL();
214
215 var url = mdserver + "?a=set-live-metadata";
216 url += "&c="+collect_;
217 url += "&d="+id;
218 url += "&metaname=" + metaname;
219 url += "&metavalue=" + metavalue;
220
221 this.urlGetSync(url);
222 }
223
224 // New
225 // The where parameter can be specified as one or more of: import, archives, index, live
226 // separated by |. If null, it is assumed to be index which is the original default
227 // behaviour of calling set-metadata. E.g. where=import|archives|index
228 this.setMetadata = function(docid,metaname,metapos,metavalue,metamode,where)
229 {
230 var mdserver = this.metadataserverURL();
231
232 var params = "a=set-metadata";
233 if(where != null) {
234 params += "&where=" + where; // if where not specified, meta-server will default to setting index meta
235 //} else {
236 // params += "&where=import|archives|index";
237 }
238 params += "&c="+collect_;
239 params += "&d="+docid;
240 params += "&metaname=" + metaname;
241 if (metapos!=null) {
242 params += "&metapos=" + metapos;
243 }
244 params += "&metavalue=" + metavalue;
245 if (metamode!=null) {
246 params += "&metamode=" + metamode;
247 }
248
249 //this.urlGetSync(mdserver + "?" + params);
250 this.urlPostSync(mdserver,params);
251 }
252
253 // New
254 // The where parameter can be specified as one or more of: import, archives, index, live
255 // separated by |. If null, it is assumed to be index which is the original default
256 // behaviour of calling set-metadata-array). E.g. where=import|archives|index
257 this.setMetadataArray = function(docArray,metamode,where)
258 {
259 docArrayJSON = JSON.stringify(docArray);
260
261 var mdserver = this.metadataserverURL();
262
263 var params = "a=" + escape("set-metadata-array"); //"a=set-metadata-array";
264 if(where != null) {
265 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
266 //} else {
267 // params += "&where=import|archives|index";
268 }
269 params += "&c="+escape(collect_);
270 params += "&json="+escape(docArrayJSON);
271
272 if (metamode!=null) {
273 params += "&metamode=" + escape(metamode);
274 }
275
276 //this.urlGetSync(mdserver + "?" + params);
277 this.urlPostSync(mdserver,params);
278 }
279
280 // New
281 this.getArchivesMetadata = function(docoid,metaname,metapos)
282 {
283 var mdserver = this.metadataserverURL();
284
285 var url = mdserver + "?a=get-archives-metadata";
286 url += "&c="+collect_;
287 url += "&d="+docoid;
288 url += "&metaname=" + metaname;
289 if (metapos!=null) {
290 url += "&metapos=" + metapos;
291 }
292
293 //alert("In getArchivesMeta. URL: " + url)
294 return this.urlGetSync(url); //Once this works, make it POST
295 }
296
297 this.getMetadataArray = function(docArray,where)
298 {
299 docArrayJSON = JSON.stringify(docArray);
300
301 var mdserver = this.metadataserverURL();
302
303 var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
304 if(where != null) {
305 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
306 //} else {
307 // params += "&where=import|archives|index";
308 }
309 params += "&c="+escape(collect_);
310 params += "&json="+escape(docArrayJSON);
311
312 //this.urlGetSync(mdserver + "?" + params);
313 return this.urlPostSync(mdserver,params);
314 }
315
316
317 this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
318 {
319 var mdserver = this.metadataserverURL();
320
321 var params = "a=set" + mode + "-metadata";
322 params += "&c="+collect_;
323 params += "&d="+docid;
324 params += "&metaname=" + metaname;
325 if (metapos!=null) {
326 params += "&metapos=" + metapos;
327 }
328 params += "&metavalue=" + metavalue;
329 if (metamode!=null) {
330 params += "&metamode=" + metamode;
331 }
332
333 this.urlGetSync(mdserver + "?" + params);
334 //this.urlPostSync(mdserver,params);
335 }
336
337
338 this._setDocumentArrayMetadata = function(mode,docArray,metamode)
339 {
340 docArrayJSON = JSON.stringify(docArray);
341
342 var mdserver = this.metadataserverURL();
343
344 var params = "a=set" + mode + "-metadata-array";
345 params += "&c="+collect_;
346 params += "&json="+docArrayJSON;
347
348 if (metamode!=null) {
349 params += "&metamode=" + metamode;
350 }
351
352 this.urlGetSync(mdserver + "?" + params);
353
354 }
355
356
357 this.setDocumentMetadata = function(docid,metaname,metapos,metavalue)
358 {
359 // Allow for three param call to function, where metapos is missed out
360 if (metavalue==null) {
361 // 4 param case
362 metavalue = metapos;
363 metapos = null;
364 }
365
366 this._setMetadata("",docid,metaname,metapos,metavalue);
367 this._setMetadata("-archives",docid,metaname,metapos,metavalue,"override");
368
369 }
370
371 this.setDocumentArrayMetadata = function(docArray,metamode)
372 {
373 //showDialog('Greenstone Javascript API','This sequence of changes has been commited into the system.','success', 2);
374
375 this._setDocumentArrayMetadata("",docArray,metamode);
376 this._setDocumentArrayMetadata("-archives",docArray,metamode);
377 }
378
379 this.setNewDocumentMetadata = function(docid,metaname,metavalue)
380 {
381 this._setMetadata("",docid,metaname,null,metavalue);
382 this._setMetadata("-archives",docid,metaname,null,metavalue,"accumulate");
383 }
384
385 this.setImportMetadata = function(docid,metaname,metapos,metavalue)
386 {
387 this._setMetadata("-import",docid,metaname,metapos,metavalue,"override");
388 }
389
390
391 this.explodeDocument = function(docid)
392 {
393 var exserver = this.explodeserverURL();
394
395 var url = exserver + "?a=explode-document";
396 url += "&c="+collect_;
397 url += "&d="+docid;
398
399 this.urlGetSync(url);
400 }
401
402 this.deleteDocument = function(docid,onlyAdd)
403 {
404 var exserver = this.explodeserverURL();
405
406 var url = exserver + "?a=delete-document";
407 url += "&c="+collect_;
408 params += "&onlyadd="+onlyAdd;
409 url += "&d="+docid;
410
411 this.urlGetSync(url);
412 }
413
414 this.deleteDocumentArray = function(docArray,onlyAdd)
415 {
416 docArrayJSON = JSON.stringify(docArray);
417
418 var exserver = this.explodeserverURL();
419
420 var params = "a=delete-document-array";
421 params += "&c="+collect_;
422 params += "&onlyadd="+onlyAdd;
423 params += "&json="+docArrayJSON;
424
425 this.urlGetSync(exserver + "?" + params);
426
427 }
428
429
430 this.cloneDocument = function(docid,toCollect)
431 {
432 var msserver = this.myspaceserverURL();
433
434 var url = msserver + "?a=clone";
435 url += "&c="+collect_;
436 url += "&d="+docid;
437 url += "&toCollect="+toCollect;
438
439 this.urlGetSync(url);
440 }
441
442 // consider name change to reindexDocument
443 this.documentReindex = function(docid)
444 {
445 var mdserver = this.metadataserverURL();
446
447 var url = mdserver + "?a=reindex-document";
448 url += "&c="+collect_;
449 url += "&d="+docid;
450
451 this.urlGetSync(url);
452 }
453
454
455 this.reindexCollection = function(mode,callback)
456 {
457 if (mode==null) {
458 mode = "incremental";
459 }
460
461 var idserver = this.indexserverURL();
462
463 var url = idserver + "?a=" + mode + "-rebuild";
464 url += "&c="+collect_;
465
466 this.urlGetAsync(url,callback);
467 }
468
469
470 this.buildByManifestGeneral = function(hashargs)
471 {
472 var idserver = this.buildserverURL();
473
474 var url = idserver + "?a=build-by-manifest";
475 url += "&c="+collect_;
476
477 if (hashargs["index-files"] != undefined) {
478 url += "&index-files=" + JSON.stringify(hashargs["index-files"]);
479 }
480
481 if (hashargs["reindex-files"] != undefined) {
482 url += "&reindex-files=" + JSON.stringify(hashargs["reindex-files"]);
483 }
484 if (hashargs["delete-OIDs"] != undefined) {
485 url += "&delete-OIDs=" + JSON.stringify(hashargs["delete-OIDs"]);
486 }
487
488 this.urlGetSync(url);
489 }
490
491 this.indexByManifest = function(docidArray)
492 {
493 var hashargs = {};
494 hashargs["index-files"] = docidArray;
495 this.buildByManifestGeneral(hashargs);
496 }
497
498 this.reindexByManifest = function(docidArray)
499 {
500 var hashargs = {};
501 hashargs["reindex-files"] = docidArray;
502 this.buildByManifestGeneral(hashargs);
503 }
504 this.deleteByManifest = function(docidArray)
505 {
506 var hashargs = {};
507 hashargs["delete-OIDs"] = docidArray;
508 this.buildByManifestGeneral(hashargs);
509 }
510
511 this.getLiveMetadata = function(id,metaname)
512 {
513 var mdserver = this.metadataserverURL();
514
515 var url = mdserver + "?a=get-live-metadata";
516 url += "&c="+collect_;
517 url += "&d="+id;
518 url += "&metaname=" + metaname;
519
520 var metavalue = this.urlGetSync(url);
521
522 return metavalue;
523 }
524
525 this.getDocumentMetadata = function(docoid,metaname,metapos)
526 {
527 var mdserver = this.metadataserverURL();
528
529 var url = mdserver + "?a=get-metadata";
530 url += "&c="+collect_;
531 url += "&d="+docoid;
532 url += "&metaname=" + metaname;
533 if (metapos!=null) {
534 url += "&metapos=" + metapos;
535 }
536
537 return this.urlGetSync(url);
538 }
539
540 this.removeLiveMetadata = function(id,metaname)
541 {
542 var mdserver = this.metadataserverURL();
543
544 var url = mdserver + "?a=remove-live-metadata";
545 url += "&c="+collect_;
546 url += "&d="+id;
547 url += "&metaname=" + metaname;
548
549 this.urlGetSync(url);
550 }
551
552 this.removeDocumentMetadata = function(docid,metaname,metapos)
553 {
554 var mdserver = this.metadataserverURL();
555
556 var url = mdserver + "?a=remove-metadata";
557 url += "&c="+collect_;
558 url += "&d="+docid;
559 url += "&metaname=" + metaname;
560 if (metapos!=null) {
561 url += "&metapos=" + metapos;
562 }
563
564 this.urlGetSync(url);
565 }
566
567 return true;
568
569}
Note: See TracBrowser for help on using the repository browser.