Blame view

php/router.php 3.05 KB
16035364   Benjamin Renard   First commit
1
2
3
<?php
/**
  *  router.php
10200969   Roipoussiere   Remove whitespace...
4
  *  http://www.ibm.com/developerworks/opensource/library/wa-aj-streamline/
16035364   Benjamin Renard   First commit
5
6
  *  @version $Id: router.php 687 2011-12-24 14:36:27Z elena $
  */
10200969   Roipoussiere   Remove whitespace...
7

16035364   Benjamin Renard   First commit
8
9
10
11
12
13
14
15
   require('config.php');

   class ExtAction {
	  public $action;
	  public $method;
	  public $data;
	  public $tid;
      }
10200969   Roipoussiere   Remove whitespace...
16

16035364   Benjamin Renard   First commit
17
18
   function doRpc($cdata){
	  global $API;
10200969   Roipoussiere   Remove whitespace...
19

16035364   Benjamin Renard   First commit
20
21
22
23
24
25
	  try {
	      if(!isset($API[$cdata->action])) {
		  throw new Exception('Call to undefined action: ' . $cdata->action);
	      }
	      $action = $cdata->action;
	      $a = $API[$action];
10200969   Roipoussiere   Remove whitespace...
26

16035364   Benjamin Renard   First commit
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	      doAroundCalls($a['before'], $cdata);

	      $method = $cdata->method;
	      $mdef = $a['methods'][$method];
	      if(!$mdef){
		  throw new Exception("Call to undefined method: $method on action $action");
	      }
	      doAroundCalls($mdef['before'], $cdata);

	      $r = array(
		  'type'=>'rpc',
		  'tid'=>$cdata->tid,
		  'action'=>$action,
		  'method'=>$method
	      );

	      $o = new $action();
10200969   Roipoussiere   Remove whitespace...
44
45
46
47

	      $params = isset($cdata->data) && is_array($cdata->data) ?
						  $cdata->data : array();

16035364   Benjamin Renard   First commit
48
	      $r['result'] = call_user_func_array(array($o, $method), $params);
10200969   Roipoussiere   Remove whitespace...
49

16035364   Benjamin Renard   First commit
50
51
52
53
54
55
56
	      doAroundCalls($mdef['after'], $cdata, $r);
	      doAroundCalls($a['after'], $cdata, $r);
	  } catch(Exception $e) {
	      $r['type'] = 'exception';
	      $r['message'] = $e->getMessage();
	      $r['where'] = $e->getTraceAsString();
	  }
10200969   Roipoussiere   Remove whitespace...
57

16035364   Benjamin Renard   First commit
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	  return $r;
      }

   function doAroundCalls(&$fns, &$cdata, &$returnData=null) {
	  if(!$fns) {
	      return;
	  }
	  if(is_array($fns)) {
	      foreach($fns as $f) {
		  $f($cdata, $returnData);
	      }
	  } else {
	      $fns($cdata, $returnData);
	  }
      }


10200969   Roipoussiere   Remove whitespace...
75
/**
16035364   Benjamin Renard   First commit
76
77
78
79
80
81
*  Main
*/

    define('log',fopen('log','w'));
    $isForm = false;
    $isUpload = false;
10200969   Roipoussiere   Remove whitespace...
82
83


16035364   Benjamin Renard   First commit
84
85
86
/*
*  artificial truc to get $HTTP_RAW_POST_DATA from POST AJAX
*/
10200969   Roipoussiere   Remove whitespace...
87
88
     if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($_POST['extAction'])) {
// Read the input from stdin
16035364   Benjamin Renard   First commit
89
90
91
92
93
94
95
	  $HTTP_RAW_POST_DATA = trim(file_get_contents('php://input'));
      }

    if(!isset($_GET['sessionID'])) die('NO SESSION ID SPECIFIED');

    $usrMgr = new UserMgr();
    $usrMgr->setPath();
10200969   Roipoussiere   Remove whitespace...
96

16035364   Benjamin Renard   First commit
97
98
99
100
101
102
103
104
105
    if (isset($HTTP_RAW_POST_DATA)) {
	header('Content-Type: text/javascript');
	$data = json_decode($HTTP_RAW_POST_DATA);
    } else if(isset($_POST['extAction'])) {
	$isForm = true;
	$isUpload = $_POST['extUpload'] == 'true';
	$data = new ExtAction();
	$data->action = $_POST['extAction'];
	$data->method = $_POST['extMethod'];
10200969   Roipoussiere   Remove whitespace...
106
	$data->tid = isset($_POST['extTID']) ? $_POST['extTID'] : null;
16035364   Benjamin Renard   First commit
107
	$data->data = array($_POST, $_FILES);
10200969   Roipoussiere   Remove whitespace...
108
109
110

    }
   else {
16035364   Benjamin Renard   First commit
111
112
 	  die('INVALID REQUEST');
      }
10200969   Roipoussiere   Remove whitespace...
113
114
115
116
117

  // $RRR = print_r($data->data, true);
 //  $RRR = print_r($data, true);
//  fwrite(log,$RRR.PHP_EOL);

16035364   Benjamin Renard   First commit
118
119
120
121
122
123
124
125

      $response = null;
      if(is_array($data)) {
	  $response = array();
	  foreach($data as $d) {
	      $response[] = doRpc($d);
	  }
      } else {
10200969   Roipoussiere   Remove whitespace...
126

16035364   Benjamin Renard   First commit
127
128
129
130
131
132
133
134
	  $response = doRpc($data);
      }

      if($isForm && $isUpload) {
	  echo '<html><body><textarea>';
	  echo json_encode($response);
	  echo '</textarea></body></html>';
      } else {
10200969   Roipoussiere   Remove whitespace...
135
        echo json_encode($response);
16035364   Benjamin Renard   First commit
136
137
138
      }

      fclose(log);
10200969   Roipoussiere   Remove whitespace...
139
?>