PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/server/model.json.php

https://github.com/praaxe/damas-core
PHP | 343 lines | 305 code | 10 blank | 28 comment | 54 complexity | 9d7e79751ed13918083b97cfa7d19457 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * JSON web service of DAMAS software (damas-software.org)
  4. *
  5. * Copyright 2005-2012 Remy Lalanne
  6. *
  7. * This file is part of damas-core.
  8. *
  9. * damas-core is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * damas-core is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with damas-core. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. session_start();
  24. include_once "service.php";
  25. include_once "../php/data_model_1.json.php";
  26. damas_service::init_http();
  27. damas_service::accessGranted();
  28. damas_service::allowed( "model::" . arg("cmd") );
  29. $cmd = arg("cmd");
  30. $ret = false;
  31. if( !$cmd )
  32. {
  33. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  34. echo "Bad command";
  35. exit;
  36. }
  37. header('Content-type: application/json');
  38. switch( $cmd )
  39. {
  40. case "createNode":
  41. if( is_null( arg('id') ) || is_null( arg('type') ) )
  42. {
  43. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  44. echo "Bad command";
  45. exit;
  46. }
  47. $id = model::createNode( arg("id"), arg("type") );
  48. if( !$id )
  49. {
  50. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_CREATE;
  51. echo "Error during the creation of node, please change your values";
  52. exit;
  53. }
  54. echo json_encode( model_json::node( $id, 1, $NODE_TAG | $NODE_PRM ) );
  55. break;
  56. case "duplicate":
  57. if( is_null( arg('id') ) )
  58. {
  59. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  60. echo "Bad command";
  61. exit;
  62. }
  63. $id = model::copyBranch( arg("id"), false );
  64. if( !$id )
  65. {
  66. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_CREATE;
  67. echo "Error during the copy of nodes, please change your values";
  68. exit;
  69. }
  70. echo json_encode( model_json::node( $id, 1, $NODE_TAG | $NODE_PRM ) );
  71. break;
  72. case "removeNode":
  73. if( is_null( arg('id') ) )
  74. {
  75. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  76. echo "Bad command";
  77. exit;
  78. }
  79. if( !model::removeNode( arg("id") ) )
  80. {
  81. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_DELETE;
  82. echo "Error during the deletion of node, please change your values";
  83. exit;
  84. }
  85. break;
  86. case "setKey":
  87. if( is_null( arg('id') ) || is_null( arg('name') ) || is_null( arg('value') ) )
  88. {
  89. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  90. echo "Bad command";
  91. exit;
  92. }
  93. if( !model::setKey( arg("id"), arg("name"), arg("value") ) )
  94. {
  95. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  96. echo "Error during the updating of node, please change your values";
  97. exit;
  98. }
  99. break;
  100. case "removeKey":
  101. if( is_null( arg('id') ) || is_null( arg('name') ) )
  102. {
  103. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  104. echo "Bad command";
  105. exit;
  106. }
  107. if( !model::removeKey( arg("id"), arg("name") ) )
  108. {
  109. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  110. echo "Error during the updating of node, please change your values";
  111. exit;
  112. }
  113. break;
  114. case "move":
  115. if( is_null( arg('id') ) || is_null( arg('target') ) )
  116. {
  117. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  118. echo "Bad command";
  119. exit;
  120. }
  121. if( !model::move( arg("id"), arg("target") ) )
  122. {
  123. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_MOVE;
  124. echo "Error during the moving of node, please change your values";
  125. exit;
  126. }
  127. break;
  128. case "tag":
  129. if( is_null( arg('id') ) || is_null( arg('name') ) )
  130. {
  131. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  132. echo "Bad command";
  133. exit;
  134. }
  135. if( !model::tag( arg("id"), arg("name") ) )
  136. {
  137. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  138. echo "Error during the updating of node, please change your values";
  139. exit;
  140. }
  141. break;
  142. case "untag":
  143. if( is_null( arg('id') ) || is_null( arg('name') ) )
  144. {
  145. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  146. echo "Bad command";
  147. exit;
  148. }
  149. if( !model::untag( arg("id"), arg("name") ) )
  150. {
  151. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  152. echo "Error during the updating of node, please change your values";
  153. exit;
  154. }
  155. break;
  156. case "link":
  157. if( is_null( arg('src') ) || is_null( arg('tgt') ) )
  158. {
  159. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  160. echo "Bad command";
  161. exit;
  162. }
  163. if( !model::link( arg("src"), arg("tgt") ) )
  164. {
  165. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  166. echo "Error during the updating of node, please change your values";
  167. exit;
  168. }
  169. break;
  170. case "unlink":
  171. if( is_null( arg('id') ) )
  172. {
  173. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  174. echo "Bad command";
  175. exit;
  176. }
  177. if( !model::unlink( arg("id") ) )
  178. {
  179. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  180. echo "Error during the updating of node, please change your values";
  181. exit;
  182. }
  183. break;
  184. case "setType":
  185. if( is_null( arg('id') ) || is_null( arg('type') ) )
  186. {
  187. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  188. echo "Bad command";
  189. exit;
  190. }
  191. if( ! model::setType( arg("id"), arg("type") ) )
  192. {
  193. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  194. echo "Error during the updating of node, please change your values";
  195. exit;
  196. }
  197. break;
  198. case "setTags":
  199. if( is_null( arg('id') ) || is_null( arg('tags') ) )
  200. {
  201. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  202. echo "Bad command";
  203. exit;
  204. }
  205. if( !model::setTags( arg("id"), arg("tags") ) )
  206. {
  207. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  208. echo "Error during the updating of node, please change your values";
  209. exit;
  210. }
  211. break;
  212. case "setKeys":
  213. if( is_null( arg('id') ) || is_null( arg('old') ) || is_null( arg('new') ) )
  214. {
  215. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  216. echo "Bad command";
  217. exit;
  218. }
  219. if( !model::setKeys( arg("id"), arg("old"), arg("new") ) )
  220. {
  221. header("HTTP/1.1: 409 Conflict"); //$err = $ERR_NODE_UPDATE;
  222. echo "Error during the updating of node, please change your values";
  223. exit;
  224. }
  225. break;
  226. /**
  227. *
  228. * json functions
  229. * model_json namespace
  230. *
  231. */
  232. case "ancestors":
  233. if( is_null( arg('id') ) )
  234. {
  235. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  236. echo "Bad command";
  237. exit;
  238. }
  239. echo json_encode( model_json::multi( model::ancestors( arg('id') ) ) );
  240. break;
  241. case "searchKey":
  242. if( is_null(arg('key')) || arg('value') == null )
  243. {
  244. header("HTTP/1.1: 400 Bad Request"); //ERR_COMMAND
  245. echo "Bad command";
  246. exit;
  247. }
  248. echo json_encode( model_json::multi( model::searchKey( arg('key'), arg('value') ) ) );
  249. break;
  250. case "single":
  251. if( is_null( arg('id') ) )
  252. {
  253. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND; break;
  254. exit;
  255. }
  256. $ret = model_json::node( arg( "id" ), 1, $NODE_TAG | $NODE_PRM );
  257. if( !$ret )
  258. {
  259. header('HTTP/1.1: 404 Not Found'); //$err = $ERR_NODE_ID;
  260. exit;
  261. }
  262. echo json_encode( $ret );
  263. break;
  264. case "children":
  265. if( is_null( arg('id') ) )
  266. {
  267. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND; break;
  268. exit;
  269. }
  270. //$ret = model_json::children( arg("id") );
  271. echo json_encode( model_json::multi( model::children( arg('id') ) ) );
  272. break;
  273. case "links":
  274. if( is_null( arg('id') ) )
  275. {
  276. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND; break;
  277. exit;
  278. }
  279. echo json_encode( model_json::links( arg("id") ) );
  280. break;
  281. case "multi":
  282. if( is_null( arg('id') ) )
  283. {
  284. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND; break;
  285. exit;
  286. }
  287. echo json_encode( model_json::multi( split( ",", arg("id") ) ) );
  288. break;
  289. case "graph":
  290. if( is_null( arg('id') ) )
  291. {
  292. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND; break;
  293. exit;
  294. }
  295. $ret = model_json::graph( arg("id") );
  296. if (!$ret)
  297. {
  298. header('HTTP/1.1: 404 Not Found'); //$err = $ERR_NODE_ID;
  299. echo json_encode( $ret );
  300. exit;
  301. }
  302. echo json_encode( $ret );
  303. break;
  304. case "export":
  305. if( is_null( arg('id') ) )
  306. {
  307. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND; break;
  308. exit;
  309. }
  310. $ret = model_json::node( arg('id'), 0, $NODE_TAG | $NODE_PRM);
  311. if( !$ret )
  312. {
  313. header('HTTP/1.1: 404 Not Found'); //$err = $ERR_NODE_ID;
  314. exit;
  315. }
  316. echo json_encode( $ret );
  317. break;
  318. case "search":
  319. if( is_null( arg('value') ) )
  320. {
  321. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND; break;
  322. exit;
  323. }
  324. echo json_encode( model::search( arg('value') ) );
  325. break;
  326. default:
  327. header('HTTP/1.1: 400 Bad Request'); //$err = $ERR_COMMAND;
  328. exit;
  329. } // switch / case
  330. $nolog = array( 'single', 'children', 'multi', 'stats', 'types', 'tags', 'search' );
  331. if( !in_array( arg('cmd'), $nolog ) )
  332. damas_service::log_event();
  333. ?>