1#!/usr/local/bin/swipl 
    2/*  Part of SWISH
    3
    4    Author:        Jan Wielemaker
    5    E-mail:        J.Wielemaker@vu.nl
    6    WWW:           http://www.swi-prolog.org
    7    Copyright (c)  2015-2016, VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(swish_ide,
   37	  [ swish/0,
   38	    swish/1			% ?Port
   39	  ]).   40
   41:- set_prolog_flag(lm_no_autoload,false).   42:- set_prolog_flag(lm_pfc_lean,false).   43
   44
   45:- use_module(library(http/thread_httpd)).   46:- use_module(library(http/http_dispatch)).   47:- use_module(library(http/http_path)).   48:- use_module(library(www_browser)).   49:- if(exists_source(library(uid))).   50:- use_module(library(uid)).   51:- endif.   52
   53:- multifile(owl2_model:datatype/2).   54:- dynamic(owl2_model:datatype/2).

Open SWISH as an IDE for developing a remote application. */

   64:- meta_predicate(from_http(0)).   65from_http(G):- with_output_to(main_error,G).
   66
   67set_main_error:- stream_property(X,file_no(2)),
   68   stream_property(X,alias(Was)),
   69   set_stream(X,alias(main_error)),
   70   set_stream(X,alias(Was)).
   71    
   72%:- use_module(library(aleph),[]).
   73
   74
   75%:- use_module(library(must_trace)).
   76
   77reexport_from(ReExporter,From:P):- 
   78    From:export(From:P),
   79    ReExporter:import(From:P),
   80    ReExporter:export(From:P).
   81   
   82
   83
   84:- multifile(pldoc_register:process_stored_comments/0).   85:- dynamic(pldoc_register:process_stored_comments/0).   86:- assert(pldoc_register:process_stored_comments).   87
   88
   89		 /*******************************
   90		 *	       CONFIG		*
   91		 *******************************/
   92
   93:- multifile
   94	swish_config:config/2,			% Name, Value
   95	swish_config:source_alias/2,		% Alias, Options
   96	swish_config:verify_write_access/3,	% Request, File, Options
   97	pengines:authentication_hook/3,		% Request, Application, User
   98	pengines:not_sandboxed/2,		% User, Application
   99        pengines:allowed/2,			% Peer, Application
  100	user:file_search_path/2.		% Alias, Path
  101
  102is_symlink_to(File,AbsLink):- 
  103         read_link(File,Link,Link),
  104         absolute_file_name(Link,AbsLink,[access(read),file_errors(fail)]),
  105         absolute_file_name(File,AFile,[access(read),file_errors(fail)]), 
  106         AFile \== Link.
  107
  108swish_home(AbsDir):- swish_home0(Dir),resolve_symlinks(Dir,AbsDir).
  109
  110resolve_symlinks(File,Link):- is_symlink_to(File,AbsLink)-> resolve_symlinks(AbsLink,Link) ; File=Link.
  111
  112swish_home0(Dir):- absolute_file_name(pack(swish),Dir,[file_type(directory),access(read),file_errors(fail)]).
  113swish_home0(Dir):- absolute_file_name(cpack(swish),Dir,[file_type(directory),access(read),file_errors(fail)]).
  114swish_home0(Dir):- is_symlink_to('./remote_ide.pl',To),file_directory_name(To,Dir).
  115
  116:- discontiguous(user:file_search_path/2).  117user:file_search_path('swish', Dir):-  swish_home(Dir),!.
  118
  119:- dynamic(non_swish_file_search_path/2).  120:- forall(retract(user:file_search_path(config_enabled, Was)),
  121          assert(non_swish_file_search_path(config_enabled, Was))).  122
  123user:file_search_path(project, '.').
  124user:file_search_path(config_enabled, 'config-enabled-swish').
  125user:file_search_path(config_enabled, swish('config-enabled')).
  126
  127:- dynamic http:location/3.  128:- multifile http:location/3.  129http:location(root, '/', [priority(1100)]).
  130http:location(swish, root('swish'), [priority(500)]).
  131http:location(root, '/swish', []).
  132
  133% http:location(swish, '/example', [priority(500)]).
  134%http:location(root, root('example'), [priority(500)]).
  135%http:location(example, root('example'), [priority(500)]).
  136% http:location(root, '/example', []).
  137:- http_handler(root(example), swish,[]).  138
  139
  140
  141nowdmsg(_).
  142
  143% :- use_module(library(r/r_sandbox)).
  144
  145:- if((false, exists_source(library(trill)))).  146
  147:- system:use_module(library(trill)).  148
  149:- reexport_from(system,trill:end_bdd/1),
  150   reexport_from(system,trill:add_var/5),
  151   reexport_from(system,trill:and/4),
  152   reexport_from(system,trill:em/8),
  153   reexport_from(system,trill:randomize/1),
  154   reexport_from(system,trill:rand_seed/1),
  155   reexport_from(system,trill:or/4),
  156   reexport_from(system,trill:ret_prob/3),
  157   reexport_from(system,trill:init_test/2),
  158   reexport_from(system,trill:end/1),
  159   reexport_from(system,trill:end_test/1),
  160   reexport_from(system,trill:bdd_not/3),
  161   reexport_from(system,trill:zero/2),
  162   reexport_from(system,trill:one/2),
  163   reexport_from(system,trill:equality/4),
  164   reexport_from(system,trill:init_bdd/2),
  165   reexport_from(system,trill:init/3).  166
  167
  168:- use_module(library(pita),[]).
  169
  170:- endif.
  171 

  172:- if(false).  173:- use_module(library(cplint_r),[]).
  174:- use_module(library(mcintyre)).
  175:- use_module(library(slipcover)).
  176:- use_module(library(lemur),[]).
  177:- use_module(library(auc)).
  178:- use_module(library(matrix)).  179
  180%:- dynamic(user:db/1).
  181%:- thread_local(user:db/1).
  182
  183
  184:- use_module(library(clpr)).
  185
  186:- multifile sandbox:safe_primitive/1.
  187
  188sandbox:safe_primitive(nf_r:{_}).
  189
  190:- endif.  191
  192
  193/*
  194
  195%prolog:prolog_load_file(library(swish/X),How):- trace, prolog:load_files([swish(lib/X)],How),!.
  196%prolog:prolog_load_file(swish(lib/swish/X),How):- prolog:load_files([swish(lib/X)],How),!.
  197*/
  198
  199swish_config:config(show_beware,        false).
  200swish_config:config(community_examples, true).
  201
  202swish_config:source_alias(project, [access(both), search('*.pl')]).
  203swish_config:source_alias(library, []).
  204
  205swish_config:verify_write_access(Request, File, Options) :- currently_logged_in(swish_config:verify_write_access(Request, File, Options),_).
  206
  207pengines:authentication_hook(Request, swish, User) :- 
  208   fail, currently_logged_in(pengines:authentication_hook(Request, swish, User),User),!.
  209
  210:- multifile pengines:allowed/2.  211:- dynamic pengines:allowed/2.  212
  213% dmiles runs in very well protected VM
  214pengines:not_sandboxed(_Maybe, _Application) :- gethostname(X),X=gitlab.
  215pengines:not_sandboxed(Maybe, Application) :- currently_logged_in(pengines:not_sandboxed(Maybe, Application),_User),!.
  216
  217current_user(User):- currently_logged_in(why,User).
  218
  219currently_logged_in(_Why,D):- thread_self(main), ignore(D=default).
  220currently_logged_in(Why,User):- 
  221  from_http((http_session:
  222    (http_in_session(_SessionID),
  223     http_session_data(oauth2(OAuth, _)),
  224     http_session_data(user_info(OAuth, Info))),
  225   User=Info.name,!, 
  226    nop(wdmsg(currently_logged_in(Why,User=Info.name))))),!.
  227
  228currently_logged_in(Why,User):- 
  229  from_http((http_session:
  230    (session_data(S,oauth2(OAuth, Y)),
  231      nop(wdmsg(currently_logged_in(User=Why,
  232      session_data(S,oauth2(OAuth, Y)))))))),!,
  233  ignore(User="guest1"),!.
  234
  235
  236currently_logged_in(Why,D):- http_session:http_in_session(SessionID),!,
  237   
  238  from_http(
  239  ((wdmsg(fail_dispite_http_in_session(SessionID,D,Why)),  
  240    http_session:http_in_session(SessionID),
  241    listing(http_session: session_data(SessionID,_Data))))),!,fail.
  242
  243
  244
  245currently_logged_in(Why,D):- thread_self(S),wdmsg(fail_currently_logged_in(Why,S,D)),!,fail.
  246
  247
  248no_auth_needed(Request):- is_list(Request),memberchk(path_info(Path),Request),mimetype:file_mime_type(Path,Type),memberchk(Type,[image/_,_/javascript]),!.
  249no_auth_needed(Request):- is_list(Request),!,memberchk(path(Path),Request),no_auth_needed(Path).
  250no_auth_needed(X):- \+ atom(X),!,fail.
  251no_auth_needed(X):- atom_concat('/swish',XX,X),!,no_auth_needed(XX).
  252no_auth_needed('/chat').
  253%no_auth_needed('/login').
  254no_auth_needed('/').
  255no_auth_needed('').
  256
  257
  258:- multifile swish_config:authenticate/2.  259:- dynamic swish_config:authenticate/2.  260
  261swish_config:authenticate(_Request, User) :- 
  262  http_session:
  263    (http_in_session(_SessionID),
  264     http_session_data(oauth2(OAuth, _)),
  265     http_session_data(user_info(OAuth, Info))),
  266   User=Info.name,!.
  267
  268% swish_config:authenticate(Request, User) :- \+ http_session:http_in_session(_),current_user(User),with_output_to(current_error,nowdmsg((swish_config:authenticate(Request, User)))),!.
  269
  270% :- use_module(pack('swish-with-filesystem-interaction'/swish)).
  271
  272:- use_module(swish(swish)).  273
  274  
  275% swish_config:authenticate(Request, "bad_user") :- wdmsg(swish_config:authenticate(Request, "bad_user")),!.
  276swish_config:authenticate(Request, User) :-
  277        swish_http_authenticate:logged_in(Request, User), !.
  278
  279  
  280
  281:- maplist(  ( [F] >> (ensure_loaded('config-enabled-swish'/F))),
  282  [auth_google,  auth_stackoverflow,  data,   email,  hdt,    
  283          rlimit,  r_serve,  user_profile, % network,
  284   auth_unity,          debug,      logging,  notifications,  
  285    rpc , auth_http]).
 swish
Start the SWISH server and open the main page in your browser.
  292:- initialization(swish).  293
  294swish :-
  295	swish('0.0.0.0':3020).
  296
  297swish(Port) :-
  298	http_server_property(Port, goal(swish_ide:http_dispatch)), !,
  299	open_browser(Port).
  300swish(_:Port) :-
  301	integer(Port),
  302	http_server_property(Port, goal(swish_ide:http_dispatch)), !,
  303	open_browser(Port).
  304swish(Port) :-
  305	http_server(http_dispatch,
  306		    [ port(Port),
  307		      workers(16)
  308		    ]),
  309	open_browser(Port).
  310
  311open_browser(Port):- !, dmsg(open_browser(Port)).
  312open_browser(Address) :-
  313	host_port(Address, Host, Port),
  314	http_server_property(Port, scheme(Scheme)),
  315	http_absolute_location(root(.), Path, []),
  316	format(atom(URL), '~w://~w:~w~w', [Scheme, Host, Port, Path]),
  317	wdmsg(www_open_url(URL)).
  318
  319host_port(Host:Port, Host, Port) :- !.
  320host_port(Port,Host, Port):- gethostname(Host),!.
  321host_port(Port,_, Port):-!.
  322
  323:- [library(pengines)].  324
  325:- multifile pengines:allowed/2.  326:- dynamic pengines:allowed/2.  327
  328:- asserta((
  329  pengines:allowed(Request, Application) :- 
  330  Application=swish-> true; 
  331   currently_logged_in(pengines:allowed(Request, Application),_User))).  332
  333% Or Sure we trust that our sandbox is good enough
  334% pengines:allowed(_,_). 
  335
  336:- listing(pengines:allowed/2).  337
  338
  339pet:- pengine_rpc("http://prologmoo.com:3020",
  340                       sin_table(X,Y),
  341                       [ src_text(':- dynamic(sin_table/2). sin_table(1,2).'),
  342                         application(swish)
  343                       ]),
  344   wdmsg(sin_table(X,Y)).
  345
  346
  347:- debug.  348
  349:- swish.  350
  351
  352user:file_search_path(What, Alias):- % maybe confirm this is not SWISH?
  353     non_swish_file_search_path(What, Alias).
  354
  355:- listing(swish_config:authenticate/2).  356:- listing(pengines:allowed/2).  357
  358:- debug(authenticate).  359:- debug(chat(_)).  360:- debug(cm(change)).  361:- debug(dot).  362:- debug(html(script)).  363:- debug(http(authenticate)).  364:- debug(http(error)).  365:- debug(http(redirect)).  366:- debug(http(request)).  367:- debug(http_authenticate).  368:- debug(http_path).  369:- debug(http_session).  370:- debug(hub(_)).  371:- debug(login).  372% :- debug(modules).
  373:- debug(notify(_)).  374:- debug(notify).  375:- debug(openid(_)).  376:- debug(openid).  377:- debug(openid_fake(_)).  378:- debug(pack(mirror)).  379:- debug(pengine(abort)).  380:- debug(pldoc).  381:- debug(plweb).  382:- debug(predicate_options).  383:- debug(setting).  384:- debug(settings).  385:- debug(stats).  386:- debug(storage).  387:- debug(swish(search)).  388:- debug(websocket(_)).  389% :- prolog_ide(debug_monitor).
  390
  391
  392
  393% :- initialization(user:ensure_loaded(run_clio)).