Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/webservice/rest/locallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once("$CFG->dirroot/webservice/lib.php");
00027 
00032 class webservice_rest_server extends webservice_base_server {
00033 
00035     protected $restformat;
00036 
00040     public function __construct($authmethod, $restformat = 'xml') {
00041         parent::__construct($authmethod);
00042         $this->wsname = 'rest';
00043         $this->restformat = ($restformat != 'xml' && $restformat != 'json')?'xml':$restformat; //sanity check, we accept only xml or json
00044     }
00045 
00055     protected function parse_request() {
00056 
00057         //Get GET and POST paramters
00058         $methodvariables = array_merge($_GET,$_POST);
00059 
00060         if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
00061             $this->username = isset($methodvariables['wsusername']) ? $methodvariables['wsusername'] : null;
00062             unset($methodvariables['wsusername']);
00063 
00064             $this->password = isset($methodvariables['wspassword']) ? $methodvariables['wspassword'] : null;
00065             unset($methodvariables['wspassword']);
00066 
00067             $this->functionname = isset($methodvariables['wsfunction']) ? $methodvariables['wsfunction'] : null;
00068             unset($methodvariables['wsfunction']);
00069 
00070             $this->parameters = $methodvariables;
00071 
00072         } else {
00073             $this->token = isset($methodvariables['wstoken']) ? $methodvariables['wstoken'] : null;
00074             unset($methodvariables['wstoken']);
00075 
00076             $this->functionname = isset($methodvariables['wsfunction']) ? $methodvariables['wsfunction'] : null;
00077             unset($methodvariables['wsfunction']);
00078 
00079             $this->parameters = $methodvariables;
00080         }
00081     }
00082 
00088     protected function send_response() {
00089 
00090         //Check that the returned values are valid
00091         try {
00092             if ($this->function->returns_desc != null) {
00093                 $validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
00094             } else {
00095                 $validatedvalues = null;
00096             }
00097         } catch (Exception $ex) {
00098             $exception = $ex;
00099         }
00100 
00101         if (!empty($exception)) {
00102             $response =  $this->generate_error($exception);
00103         } else {
00104             //We can now convert the response to the requested REST format
00105             if ($this->restformat == 'json') {
00106                 $response = json_encode($validatedvalues);
00107             } else {
00108                 $response = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
00109                 $response .= '<RESPONSE>'."\n";
00110                 $response .= self::xmlize_result($this->returns, $this->function->returns_desc);
00111                 $response .= '</RESPONSE>'."\n";
00112             }
00113         }
00114 
00115         $this->send_headers();
00116         echo $response;
00117     }
00118 
00127     protected function send_error($ex=null) {
00128         $this->send_headers();
00129         echo $this->generate_error($ex);
00130     }
00131 
00137     protected function generate_error($ex) {
00138         if ($this->restformat == 'json') {
00139             $errorobject = new stdClass;
00140             $errorobject->exception = get_class($ex);
00141             $errorobject->message = $ex->getMessage();
00142             if (debugging() and isset($ex->debuginfo)) {
00143                 $errorobject->debuginfo = $ex->debuginfo;
00144             }
00145             $error = json_encode($errorobject);
00146         } else {
00147             $error = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
00148             $error .= '<EXCEPTION class="'.get_class($ex).'">'."\n";
00149             $error .= '<MESSAGE>'.htmlspecialchars($ex->getMessage(), ENT_COMPAT, 'UTF-8').'</MESSAGE>'."\n";
00150             if (debugging() and isset($ex->debuginfo)) {
00151                 $error .= '<DEBUGINFO>'.htmlspecialchars($ex->debuginfo, ENT_COMPAT, 'UTF-8').'</DEBUGINFO>'."\n";
00152             }
00153             $error .= '</EXCEPTION>'."\n";
00154         }
00155         return $error;
00156     }
00157 
00162     protected function send_headers() {
00163         if ($this->restformat == 'json') {
00164             header('Content-type: application/json');
00165         } else {
00166             header('Content-Type: application/xml; charset=utf-8');
00167             header('Content-Disposition: inline; filename="response.xml"');
00168         }
00169         header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
00170         header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
00171         header('Pragma: no-cache');
00172         header('Accept-Ranges: none');
00173     }
00174 
00181     protected static function xmlize_result($returns, $desc) {
00182         if ($desc === null) {
00183             return '';
00184 
00185         } else if ($desc instanceof external_value) {
00186             if (is_bool($returns)) {
00187                 // we want 1/0 instead of true/false here
00188                 $returns = (int)$returns;
00189             }
00190             if (is_null($returns)) {
00191                 return '<VALUE null="null"/>'."\n";
00192             } else {
00193                 return '<VALUE>'.htmlspecialchars($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
00194             }
00195 
00196         } else if ($desc instanceof external_multiple_structure) {
00197             $mult = '<MULTIPLE>'."\n";
00198             if (!empty($returns)) {
00199                 foreach ($returns as $val) {
00200                     $mult .= self::xmlize_result($val, $desc->content);
00201                 }
00202             }
00203             $mult .= '</MULTIPLE>'."\n";
00204             return $mult;
00205 
00206         } else if ($desc instanceof external_single_structure) {
00207             $single = '<SINGLE>'."\n";
00208             foreach ($desc->keys as $key=>$subdesc) {
00209                 $single .= '<KEY name="'.$key.'">'.self::xmlize_result($returns[$key], $subdesc).'</KEY>'."\n";
00210             }
00211             $single .= '</SINGLE>'."\n";
00212             return $single;
00213         }
00214     }
00215 }
00216 
00217 
00221 class webservice_rest_test_client implements webservice_test_client_interface {
00229     public function simpletest($serverurl, $function, $params) {
00230         return download_file_content($serverurl.'&wsfunction='.$function, null, $params);
00231     }
00232 }
 All Data Structures Namespaces Files Functions Variables Enumerations