Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
wsh.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 /*
4  * @author Anakeen
5  * @package FDL
6 */
7 /**
8  * WHAT SHELL
9  *
10  * @author Anakeen
11  * @version $Id: wsh.php,v 1.35 2008/05/06 08:43:33 jerome Exp $
12  * @package FDL
13  */
14 
15 ini_set("max_execution_time", "0");
16 require_once 'WHAT/Lib.Prefix.php';
17 require_once 'Class.Action.php';
18 require_once 'Class.Application.php';
19 require_once 'Class.Session.php';
20 require_once 'Class.Log.php';
21 require_once 'Lib.Main.php';
22 
24 
25 checkWshExecUid(__FILE__);
26 
27 function print_usage()
28 {
29  print "Usage\twsh.php --app=APPLICATION --action=ACTION [--ARG=VAL] ...: execute an action\n" . "\twsh.php --api=API [--ARG=VAL] .... : execute an api function\n" . "\twsh.php --listapi : view api list\n";
30 }
31 
32 wbar(1, -1, "initialisation");
33 $log = new Log("", "index.php");
34 
35 $CoreNull = "";
36 // get param
37 global $_GET;
38 global $_SERVER;
39 
40 if (isset($_SERVER['HTTP_HOST'])) {
41  print "<BR><H1>:~(</H1>";
42  exit(1);
43 }
44 if (count($argv) == 1) {
45  print_usage();
46 
47  exit(1);
48 }
49 
50 foreach ($argv as $k => $v) {
51 
52  if (preg_match("/--([^=]+)=(.*)/", $v, $reg)) {
53  if (substr($reg[1], -2) == "[]") {
54  $_GET[substr($reg[1], 0, -2) ][] = $reg[2];
55  } else {
56  $_GET[$reg[1]] = $reg[2];
57  }
58  } else if (preg_match("/--(.+)/", $v, $reg)) {
59  if ($reg[1] == "listapi") {
60  $apiList = array();
61  foreach (new DirectoryIterator(DEFAULT_PUBDIR . DIRECTORY_SEPARATOR . 'API') as $entry) {
62  if (preg_match('/^(?<basename>.+)\.php$/', $entry->getFilename() , $m)) {
63  $apiList[] = $m['basename'];
64  }
65  }
66  sort($apiList, SORT_STRING | SORT_FLAG_CASE);
67  print "application list :\n";
68  foreach ($apiList as $api) {
69  printf("\t- %s\n", $api);
70  }
71  print "\n";
72  exit;
73  }
74  $_GET[$reg[1]] = true;
75  }
76 }
77 
78 if ((empty($_GET["api"])) && (empty($_GET["app"]) || empty($_GET["action"]))) {
79  print_usage();
80  exit(1);
81 }
82 
84 if ($core->dbid < 0) {
85  print "Cannot access to main database";
86  exit(1);
87 }
88 
89 if (isset($_GET["userid"])) { //special user
90  if (!is_numeric($_GET["userid"])) {
91  $core->user = new Account();
92  $core->user->setLoginName($_GET["userid"]);
93  } else {
94  $core->user = new Account("", $_GET["userid"]);
95  }
96 }
97 $core->Set("CORE", $CoreNull);
98 $core->session = new Session();
99 if (!isset($_GET["userid"])) $core->user = new Account("", Account::ADMIN_ID); //admin
100 ini_set("memory_limit", -1);
101 
104 /*
105  * @var Action $action
106  */
107 try {
108  if (isset($_GET["app"])) {
109  $appl = new Application();
110  $appl->Set($_GET["app"], $core);
111  } else {
112  $appl = $core;
113  }
114 
115  $action = new Action();
116  if (isset($_GET["action"])) {
117  $action->Set($_GET["action"], $appl);
118  } else {
119  $action->Set("", $appl);
120  }
121 
122  if (!$core->user->isAffected()) {
123  throw new Dcp\Core\Exception("CORE0013", $_GET["userid"]);
124  }
125 
126  if ($core->user->status === "D") {
127  throw new Dcp\Core\Exception("CORE0014", $_GET["userid"]);
128  }
129 
130  if ($action->canExecute("CORE_ADMIN_ROOT", "CORE_ADMIN") === '') {
131  // Authorize administrators to execute admin actions
132  $action->parent->setAdminMode();
133  }
134 }
135 catch(Dcp\Exception $e) {
137 }
138 // init for gettext
139 setLanguage($action->Getparam("CORE_LANG"));
140 
141 if (isset($_GET["api"])) {
142  $apifile = trim($_GET["api"]);
143  if (!file_exists(sprintf("%s/API/%s.php", DEFAULT_PUBDIR, $apifile))) {
144  echo sprintf(_("API file %s not found\n") , "API/" . $apifile . ".php");
145  exit(4);
146  } else {
147  try {
148  include ("API/" . $apifile . ".php");
149  }
150  catch(Dcp\ApiUsage\Exception $e) {
151  switch ($e->getDcpCode()) {
152  case "CORE0002":
153  echo sprintf(_("Error : %s\n") , $e->getDcpMessage());
154  _wsh_exception_handler($e, false);
155  exit(1);
156  break;
157 
158  case "CORE0003":
159  echo sprintf($e->getDcpMessage());
160  exit(0);
161  break;
162 
163  default:
164  echo sprintf($e->getDcpMessage());
165  _wsh_exception_handler($e, false);
166  exit(1);
167  }
168  }
169  catch(Dcp\Exception $e) {
171  }
172  catch(Exception $e) {
174  }
175  }
176 } else {
177  if (!isset($_GET["wshfldid"])) {
178  try {
179  echo ($action->execute());
180  }
181  catch(Dcp\ApiUsage\Exception $e) {
182  switch ($e->getDcpCode()) {
183  case "CORE0002":
184  echo sprintf(_("Error : %s\n") , $e->getDcpMessage());
185  exit(1);
186  break;
187 
188  case "CORE0003":
189  echo sprintf($e->getDcpMessage());
190  exit(0);
191  break;
192 
193  default:
194  echo sprintf($e->getDcpMessage());
195  exit(1);
196  }
197  }
198  catch(Dcp\Exception $e) {
200  }
201  catch(Exception $e) {
203  }
204  } else {
205  // REPEAT EXECUTION FOR FREEDOM FOLDERS
206  $dbaccess = $appl->dbaccess;
207  if ($dbaccess == "") {
208  print "Database not found : appl->dbaccess";
209  exit;
210  }
211  include_once ("FDL/Class.Doc.php");
212  $http_iddoc = "id"; // default correspondance
213  if (isset($_GET["wshfldhttpdocid"])) $http_iddoc = $_GET["wshfldhttpdocid"];
214  /*
215  * @var Dir $fld
216  */
217  $fld = new_Doc($dbaccess, $_GET["wshfldid"]);
218  $ld = $fld->getContent();
219  foreach ($ld as $k => $v) {
220  $_GET[$http_iddoc] = $v["id"];
221  try {
222  echo ($action->execute());
223  }
224  catch(Exception $e) {
225  switch ($e->getCode()) {
226  case THROW_EXITERROR:
227  errorLogException($e);
228  break;
229 
230  default:
231  errorLogException($e);
232  }
233  }
234  echo "<hr>";
235  }
236  }
237 }
238 
239 wbar(-1, -1, "completed");
240 
241 return (0);
errorLogException($e)
Definition: Lib.Main.php:555
global $appl
Definition: checkVault.php:9
enable_wsh_safetybelts()
Definition: Lib.Main.php:538
print_usage()
Definition: wsh.php:27
global $action
_wsh_exception_handler($e, $callStack=true)
Definition: Lib.Main.php:457
const ADMIN_ID
initMainVolatileParam(Application &$core, Session &$session=null)
Definition: Lib.Main.php:244
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
$log
Definition: wsh.php:33
wbar($reste, $total, $text="", $fbar=false)
Definition: Lib.Common.php:642
initExplorerParam(Application &$app, $defaultValue=false)
Definition: Lib.Main.php:142
global $_GET
Definition: wsh.php:37
setLanguage($lang)
Definition: Lib.Common.php:886
print
Definition: checklist.php:49
if(isset($_SERVER['HTTP_HOST'])) if(count($argv)==1) foreach($argv as $k=> $v) if((empty($_GET["api"]))&&(empty($_GET["app"])||empty($_GET["action"]))) $core
Definition: wsh.php:83
switch($command) exit
Definition: checkVault.php:46
new_Doc($dbaccess, $id= '', $latest=false)
global $_SERVER
Definition: wsh.php:38
checkWshExecUid($file)
Definition: Lib.Main.php:362
$dbaccess
Definition: checkVault.php:17
$CoreNull
Definition: wsh.php:35
const THROW_EXITERROR
Verify arguments for wsh programs.
← centre documentaire © anakeen