PageRenderTime 1876ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/wsh.php

https://github.com/CircleCode/dynacase-core
PHP | 217 lines | 188 code | 14 blank | 15 comment | 19 complexity | 7cce469c428f92af52767c58a605397e MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * @author Anakeen
  5. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  6. * @package FDL
  7. */
  8. /**
  9. * WHAT SHELL
  10. *
  11. * @author Anakeen
  12. * @version $Id: wsh.php,v 1.35 2008/05/06 08:43:33 jerome Exp $
  13. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  14. * @package FDL
  15. */
  16. ini_set("max_execution_time", "0");
  17. require_once 'WHAT/Lib.Prefix.php';
  18. require_once 'Class.Action.php';
  19. require_once 'Class.Application.php';
  20. require_once 'Class.Session.php';
  21. require_once 'Class.Log.php';
  22. require_once 'Lib.Main.php';
  23. function print_usage()
  24. {
  25. 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";
  26. }
  27. wbar(1, -1, "initialisation");
  28. $log = new Log("", "index.php");
  29. $CoreNull = "";
  30. global $CORE_LOGLEVEL;
  31. // get param
  32. global $_GET;
  33. global $_SERVER;
  34. if (isset($_SERVER['HTTP_HOST'])) {
  35. print "<BR><H1>:~(</H1>";
  36. exit(1);
  37. }
  38. if (count($argv) == 1) {
  39. print_usage();
  40. exit(1);
  41. }
  42. foreach ($argv as $k => $v) {
  43. if (preg_match("/--([^=]+)=(.*)/", $v, $reg)) {
  44. if (substr($reg[1],-2)=="[]") {
  45. $_GET[substr($reg[1],0,-2)][] = $reg[2];
  46. } else {
  47. $_GET[$reg[1]] = $reg[2];
  48. }
  49. } else if (preg_match("/--(.+)/", $v, $reg)) {
  50. if ($reg[1] == "listapi") {
  51. print "application list :\n";
  52. echo "\t- ";
  53. echo str_replace("\n", "\n\t- ", shell_exec(sprintf("cd %s/API;ls -1 *.php| cut -f1 -d'.'", escapeshellarg(DEFAULT_PUBDIR))));
  54. echo "\n";
  55. exit;
  56. }
  57. $_GET[$reg[1]] = true;
  58. }
  59. }
  60. if ((empty($_GET["api"])) && (empty($_GET["app"]) || empty($_GET["action"]))) {
  61. print_usage();
  62. exit(1);
  63. }
  64. $core = new Application();
  65. if ($core->dbid < 0) {
  66. print "Cannot access to main database";
  67. exit(1);
  68. }
  69. if (isset($_GET["userid"])) { //special user
  70. if (!is_numeric($_GET["userid"])) {
  71. $core->user = new Account();
  72. $core->user->setLoginName($_GET["userid"]);
  73. } else {
  74. $core->user = new Account("", $_GET["userid"]);
  75. }
  76. }
  77. $core->Set("CORE", $CoreNull);
  78. $core->session = new Session();
  79. if (!isset($_GET["userid"])) $core->user = new Account("", 1); //admin
  80. $CORE_LOGLEVEL = $core->GetParam("CORE_LOGLEVEL", "IWEF");
  81. ini_set("memory_limit", -1);
  82. initMainVolatileParam($core);
  83. initExplorerParam($core);
  84. if (!$core->user->isAffected()) {
  85. echo sprintf(_("Error : User [%s] doesn't exists\n") , $_GET["userid"]);
  86. exit(2);
  87. }
  88. if ($core->user->status == "D") {
  89. echo sprintf(_("Error : User account [%s] is desactivated\n") , $_GET["userid"]);
  90. exit(2);
  91. }
  92. try {
  93. if (isset($_GET["app"])) {
  94. $appl = new Application();
  95. $appl->Set($_GET["app"], $core);
  96. } else {
  97. $appl = $core;
  98. }
  99. $action = new Action();
  100. if (isset($_GET["action"])) {
  101. $action->Set($_GET["action"], $appl);
  102. } else {
  103. $action->Set("", $appl);
  104. }
  105. }
  106. catch(Dcp\Exception $e) {
  107. echo sprintf(_("Error : %s\n") , $e->getMessage());
  108. exit(1);
  109. }
  110. // init for gettext
  111. setLanguage($action->Getparam("CORE_LANG"));
  112. if (isset($_GET["api"])) {
  113. $apifile = trim($_GET["api"]);
  114. if (!file_exists(sprintf("%s/API/%s.php", DEFAULT_PUBDIR, $apifile))) {
  115. echo sprintf(_("API file %s not found\n") , "API/" . $apifile . ".php");
  116. exit(4);
  117. } else {
  118. try {
  119. include ("API/" . $apifile . ".php");
  120. }
  121. catch(Dcp\ApiUsage\Exception $e) {
  122. switch ($e->getDcpCode()) {
  123. case "CORE0002":
  124. echo sprintf(_("Error : %s\n") , $e->getDcpMessage());
  125. exit(1);
  126. break;
  127. case "CORE0003":
  128. echo sprintf($e->getDcpMessage());
  129. exit(0);
  130. break;
  131. default:
  132. echo sprintf(_("Caught Exception : %s\n") , $e->getMessage());
  133. exit(1);
  134. }
  135. }
  136. catch(Dcp\Exception $e) {
  137. echo sprintf(_("Error : %s\n") , $e->getMessage());
  138. exit(1);
  139. }
  140. catch(Exception $e) {
  141. echo sprintf(_("Caught Exception : %s\n") , $e->getMessage());
  142. exit(1);
  143. }
  144. }
  145. } else {
  146. if (!isset($_GET["wshfldid"])) {
  147. try {
  148. echo ($action->execute());
  149. }
  150. catch(Dcp\Exception $e) {
  151. echo sprintf(_("Error : %s\n") , $e->getMessage());
  152. exit(1);
  153. }
  154. catch(Exception $e) {
  155. echo sprintf(_("Caught Exception : %s\n") , $e->getMessage());
  156. exit(1);
  157. }
  158. } else {
  159. // REPEAT EXECUTION FOR FREEDOM FOLDERS
  160. $dbaccess = $appl->GetParam("FREEDOM_DB");
  161. if ($dbaccess == "") {
  162. print "Database not found : param FREEDOM_DB";
  163. exit;
  164. }
  165. include_once ("FDL/Class.Doc.php");
  166. $http_iddoc = "id"; // default correspondance
  167. if (isset($_GET["wshfldhttpdocid"])) $http_iddoc = $_GET["wshfldhttpdocid"];
  168. /**
  169. * @var Dir $fld
  170. */
  171. $fld = new_Doc($dbaccess, $_GET["wshfldid"]);
  172. $ld = $fld->getContent();
  173. foreach ($ld as $k => $v) {
  174. $_GET[$http_iddoc] = $v["id"];
  175. try {
  176. echo ($action->execute());
  177. }
  178. catch(Exception $e) {
  179. switch ($e->getCode()) {
  180. case THROW_EXITERROR:
  181. echo sprintf(_("Error : %s\n") , $e->getMessage());
  182. break;
  183. default:
  184. echo sprintf(_("Caught Exception : %s\n") , $e->getMessage());
  185. }
  186. }
  187. echo "<hr>";
  188. }
  189. }
  190. }
  191. wbar(-1, -1, "completed");
  192. return (0);
  193. ?>