Blame view

sources/lib/private/ocs.php 7.35 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  <?php
  
  /**
  * ownCloud
  *
  * @author Frank Karlitschek
  * @author Michael Gapczynski
  * @copyright 2012 Frank Karlitschek frank@owncloud.org
  * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  * License as published by the Free Software Foundation; either
  * version 3 of the License, or any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  *
  * You should have received a copy of the GNU Affero General Public
  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
03e52840d   Kload   Init
25
26
27
28
29
30
31
32
33
  /**
   * Class to handle open collaboration services API requests
   *
   */
  class OC_OCS {
  
  	/**
  	* reads input date from get/post/cookies and converts the date to a special data-type
  	*
6d9380f96   Cédric Dupont   Update sources OC...
34
35
36
37
38
  	* @param string $method HTTP method to read the key from
  	* @param string $key Parameter to read
  	* @param string $type Variable type to format data
  	* @param string $default Default value to return if the key is not found
  	* @return string Data or if the key is not found and no default is set it will exit with a 400 Bad request
03e52840d   Kload   Init
39
40
  	*/
  	public static function readData($method, $key, $type = 'raw', $default = null) {
6d9380f96   Cédric Dupont   Update sources OC...
41
  		$data = false;
03e52840d   Kload   Init
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  		if ($method == 'get') {
  			if (isset($_GET[$key])) {
  				$data = $_GET[$key];
  			} else if (isset($default)) {
  				return $default;
  			} else {
  				$data = false;
  			}
  		} else if ($method == 'post') {
  			if (isset($_POST[$key])) {
  				$data = $_POST[$key];
  			} else if (isset($default)) {
  				return $default;
  			} else {
  				$data = false;
  			}
  		}
  		if ($data === false) {
  			echo self::generateXml('', 'fail', 400, 'Bad request. Please provide a valid '.$key);
  			exit();
  		} else {
  			// NOTE: Is the raw type necessary? It might be a little risky without sanitization
  			if ($type == 'raw') return $data;
  			elseif ($type == 'text') return OC_Util::sanitizeHTML($data);
  			elseif ($type == 'int')  return (int) $data;
  			elseif ($type == 'float') return (float) $data;
  			elseif ($type == 'array') return OC_Util::sanitizeHTML($data);
  			else return OC_Util::sanitizeHTML($data);
  		}
  	}
  
  	public static function notFound() {
  		if($_SERVER['REQUEST_METHOD'] == 'GET') {
  			$method='get';
  		}elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
  			$method='put';
  			parse_str(file_get_contents("php://input"), $put_vars);
  		}elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  			$method='post';
  		}else{
  			echo('internal server error: method not supported');
  			exit();
  		}
  
  		$format = self::readData($method, 'format', 'text', '');
  		$txt='Invalid query, please check the syntax. API specifications are here:'
  		.' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."
  ";
  		$txt.=OC_OCS::getDebugOutput();
  		echo(OC_OCS::generateXml($format, 'failed', 999, $txt));
  
  	}
  
  	/**
  	* generated some debug information to make it easier to find faild API calls
6d9380f96   Cédric Dupont   Update sources OC...
97
  	* @return string data string
03e52840d   Kload   Init
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  	*/
  	private static function getDebugOutput() {
  		$txt='';
  		$txt.="debug output:
  ";
  		if(isset($_SERVER['REQUEST_METHOD'])) $txt.='http request method: '.$_SERVER['REQUEST_METHOD']."
  ";
  		if(isset($_SERVER['REQUEST_URI'])) $txt.='http request uri: '.$_SERVER['REQUEST_URI']."
  ";
  		if(isset($_GET)) foreach($_GET as $key=>$value) $txt.='get parameter: '.$key.'->'.$value."
  ";
  		if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."
  ";
  		return($txt);
  	}
  
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
116
117
118
119
120
121
122
123
124
125
126
127
128
  	 * generates the xml or json response for the API call from an multidimenional data array.
  	 * @param string $format
  	 * @param string $status
  	 * @param string $statuscode
  	 * @param string $message
  	 * @param array $data
  	 * @param string $tag
  	 * @param string $tagattribute
  	 * @param int $dimension
  	 * @param int|string $itemscount
  	 * @param int|string $itemsperpage
  	 * @return string xml/json
  	 */
03e52840d   Kload   Init
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  	private static function generateXml($format, $status, $statuscode,
  		$message, $data=array(), $tag='', $tagattribute='', $dimension=-1, $itemscount='', $itemsperpage='') {
  		if($format=='json') {
  			$json=array();
  			$json['status']=$status;
  			$json['statuscode']=$statuscode;
  			$json['message']=$message;
  			$json['totalitems']=$itemscount;
  			$json['itemsperpage']=$itemsperpage;
  			$json['data']=$data;
  			return(json_encode($json));
  		}else{
  			$txt='';
  			$writer = xmlwriter_open_memory();
  			xmlwriter_set_indent( $writer, 2 );
  			xmlwriter_start_document($writer );
  			xmlwriter_start_element($writer, 'ocs');
  			xmlwriter_start_element($writer, 'meta');
  			xmlwriter_write_element($writer, 'status', $status);
  			xmlwriter_write_element($writer, 'statuscode', $statuscode);
  			xmlwriter_write_element($writer, 'message', $message);
  			if($itemscount<>'') xmlwriter_write_element($writer, 'totalitems', $itemscount);
  			if(!empty($itemsperpage)) xmlwriter_write_element($writer, 'itemsperpage', $itemsperpage);
  			xmlwriter_end_element($writer);
  			if($dimension=='0') {
  				// 0 dimensions
  				xmlwriter_write_element($writer, 'data', $data);
  
  			}elseif($dimension=='1') {
  				xmlwriter_start_element($writer, 'data');
  				foreach($data as $key=>$entry) {
  					xmlwriter_write_element($writer, $key, $entry);
  				}
  				xmlwriter_end_element($writer);
  
  			}elseif($dimension=='2') {
  				xmlwriter_start_element($writer, 'data');
  				foreach($data as $entry) {
  					xmlwriter_start_element($writer, $tag);
  					if(!empty($tagattribute)) {
  						xmlwriter_write_attribute($writer, 'details', $tagattribute);
  					}
  					foreach($entry as $key=>$value) {
  						if(is_array($value)) {
  							foreach($value as $k=>$v) {
  								xmlwriter_write_element($writer, $k, $v);
  							}
  						} else {
  							xmlwriter_write_element($writer, $key, $value);
  						}
  					}
  					xmlwriter_end_element($writer);
  				}
  				xmlwriter_end_element($writer);
  
  			}elseif($dimension=='3') {
  				xmlwriter_start_element($writer, 'data');
  				foreach($data as $entrykey=>$entry) {
  					xmlwriter_start_element($writer, $tag);
  					if(!empty($tagattribute)) {
  						xmlwriter_write_attribute($writer, 'details', $tagattribute);
  					}
  					foreach($entry as $key=>$value) {
  						if(is_array($value)) {
  							xmlwriter_start_element($writer, $entrykey);
  							foreach($value as $k=>$v) {
  								xmlwriter_write_element($writer, $k, $v);
  							}
  							xmlwriter_end_element($writer);
  						} else {
  							xmlwriter_write_element($writer, $key, $value);
  						}
  					}
  					xmlwriter_end_element($writer);
  				}
  				xmlwriter_end_element($writer);
  			}elseif($dimension=='dynamic') {
  				xmlwriter_start_element($writer, 'data');
  				OC_OCS::toxml($writer, $data, 'comment');
  				xmlwriter_end_element($writer);
  			}
  
  			xmlwriter_end_element($writer);
  
  			xmlwriter_end_document( $writer );
  			$txt.=xmlwriter_output_memory( $writer );
  			unset($writer);
  			return($txt);
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
219
220
221
222
223
  	/**
  	 * @param resource $writer
  	 * @param array $data
  	 * @param string $node
  	 */
03e52840d   Kload   Init
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  	public static function toXml($writer, $data, $node) {
  		foreach($data as $key => $value) {
  			if (is_numeric($key)) {
  				$key = $node;
  			}
  			if (is_array($value)) {
  				xmlwriter_start_element($writer, $key);
  				OC_OCS::toxml($writer, $value, $node);
  				xmlwriter_end_element($writer);
  			}else{
  				xmlwriter_write_element($writer, $key, $value);
  			}
  		}
  	}
03e52840d   Kload   Init
238
  }