| Conditions | 17 |
| Paths | 99 |
| Total Lines | 120 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 270 | function http_PUT() |
||
| 271 | { |
||
| 272 | $options = Array(); |
||
| 273 | $options["path"] = $this->path; |
||
| 274 | $options["content_length"] = $_SERVER["CONTENT_LENGTH"]; |
||
| 275 | |||
| 276 | // get the Content-type |
||
| 277 | if (isset($_SERVER["CONTENT_TYPE"])) { |
||
| 278 | // for now we do not support any sort of multipart requests |
||
| 279 | if (!strncmp($_SERVER["CONTENT_TYPE"], "multipart/", 10)) { |
||
| 280 | $this->http_status("501 not implemented"); |
||
| 281 | echo "The service does not support mulipart PUT requests"; |
||
| 282 | return; |
||
| 283 | } |
||
| 284 | $options["content_type"] = $_SERVER["CONTENT_TYPE"]; |
||
| 285 | } else { |
||
| 286 | // default content type if none given |
||
| 287 | $options["content_type"] = "application/octet-stream"; |
||
| 288 | } |
||
| 289 | |||
| 290 | /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT |
||
| 291 | ignore any Content-* (e.g. Content-Range) headers that it |
||
| 292 | does not understand or implement and MUST return a 501 |
||
| 293 | (Not Implemented) response in such cases." |
||
| 294 | */ |
||
| 295 | foreach ($_SERVER as $key => $val) { |
||
| 296 | if (strncmp($key, "HTTP_CONTENT", 11)) continue; |
||
| 297 | switch ($key) { |
||
| 298 | case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11 |
||
| 299 | // TODO support this if ext/zlib filters are available |
||
| 300 | $this->http_status("501 not implemented"); |
||
| 301 | echo "The service does not support '$val' content encoding"; |
||
| 302 | return; |
||
| 303 | |||
| 304 | case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12 |
||
| 305 | // we assume it is not critical if this one is ignored |
||
| 306 | // in the actual PUT implementation ... |
||
| 307 | $options["content_language"] = $val; |
||
| 308 | break; |
||
| 309 | |||
| 310 | case 'HTTP_CONTENT_LOCATION': // RFC 2616 14.14 |
||
| 311 | /* The meaning of the Content-Location header in PUT |
||
| 312 | or POST requests is undefined; servers are free |
||
| 313 | to ignore it in those cases. */ |
||
| 314 | break; |
||
| 315 | |||
| 316 | case 'HTTP_CONTENT_RANGE': // RFC 2616 14.16 |
||
| 317 | // single byte range requests are NOT supported |
||
| 318 | // the header format is also specified in RFC 2616 14.16 |
||
| 319 | // TODO we have to ensure that implementations support this or send 501 instead |
||
| 320 | $this->http_status("400 bad request"); |
||
| 321 | echo "The service does only support single byte ranges"; |
||
| 322 | return; |
||
| 323 | |||
| 324 | case 'HTTP_CONTENT_MD5': // RFC 2616 14.15 |
||
| 325 | // TODO: maybe we can just pretend here? |
||
| 326 | $this->http_status("501 not implemented"); |
||
| 327 | echo "The service does not support content MD5 checksum verification"; |
||
| 328 | return; |
||
| 329 | |||
| 330 | case 'HTTP_CONTENT_LENGTH': // RFC 2616 14.14 |
||
| 331 | /* The meaning of the Content-Location header in PUT |
||
| 332 | or POST requests is undefined; servers are free |
||
| 333 | to ignore it in those cases. */ |
||
| 334 | break; |
||
| 335 | |||
| 336 | default: |
||
| 337 | // any other unknown Content-* headers |
||
| 338 | $this->http_status("501 not implemented"); |
||
| 339 | echo "The service does not support '$key'"; |
||
| 340 | return; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | // DO AUTHORIZATION for publishing Free/busy to Sugar: |
||
| 345 | if ( empty($this->publish_key) || |
||
| 346 | $this->publish_key != $this->user_focus->getPreference('calendar_publish_key' )) |
||
| 347 | { |
||
| 348 | $this->http_status("401 not authorized"); |
||
| 349 | return; |
||
| 350 | |||
| 351 | } |
||
| 352 | |||
| 353 | // retrieve |
||
| 354 | $arr = array('user_id'=>$this->user_focus->id,'type'=>'vfb','source'=>$this->source); |
||
| 355 | $this->vcal_focus->retrieve_by_string_fields($arr); |
||
| 356 | |||
| 357 | $isUpdate = false; |
||
| 358 | |||
| 359 | if ( ! empty($this->vcal_focus->user_id ) && |
||
| 360 | $this->vcal_focus->user_id != -1 ) |
||
| 361 | { |
||
| 362 | $isUpdate = true; |
||
| 363 | } |
||
| 364 | |||
| 365 | // open input stream |
||
| 366 | $options["stream"] = fopen("php://input", "r"); |
||
| 367 | $content = ''; |
||
| 368 | |||
| 369 | // read in input stream |
||
| 370 | while (!feof($options["stream"])) |
||
| 371 | { |
||
| 372 | $content .= fread($options["stream"], 4096); |
||
| 373 | } |
||
| 374 | |||
| 375 | // set freebusy members and save |
||
| 376 | $this->vcal_focus->content = $content; |
||
| 377 | $this->vcal_focus->type = 'vfb'; |
||
| 378 | $this->vcal_focus->source = $this->source; |
||
| 379 | $focus->date_modified = null; |
||
| 380 | $this->vcal_focus->user_id = $this->user_focus->id; |
||
| 381 | $this->vcal_focus->save(); |
||
| 382 | |||
| 383 | if ( $isUpdate ) |
||
| 384 | { |
||
| 385 | $this->http_status("204 No Content"); |
||
| 386 | } else { |
||
| 387 | $this->http_status("201 Created"); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 444 |