Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mnet/lib.php
Go to the documentation of this file.
00001 <?php
00010 require_once $CFG->dirroot.'/mnet/xmlrpc/xmlparser.php';
00011 require_once $CFG->dirroot.'/mnet/peer.php';
00012 require_once $CFG->dirroot.'/mnet/environment.php';
00013 
00015 
00016 define('RPC_OK',                0);
00017 define('RPC_NOSUCHFILE',        1);
00018 define('RPC_NOSUCHCLASS',       2);
00019 define('RPC_NOSUCHFUNCTION',    3);
00020 define('RPC_FORBIDDENFUNCTION', 4);
00021 define('RPC_NOSUCHMETHOD',      5);
00022 define('RPC_FORBIDDENMETHOD',   6);
00023 
00032 function mnet_get_hostname_from_uri($uri = null) {
00033     $count = preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $uri, $matches);
00034     if ($count > 0) return $matches[1];
00035     return false;
00036 }
00037 
00045 function mnet_get_public_key($uri, $application=null) {
00046     global $CFG, $DB;
00047     $mnet = get_mnet_environment();
00048     // The key may be cached in the mnet_set_public_key function...
00049     // check this first
00050     $key = mnet_set_public_key($uri);
00051     if ($key != false) {
00052         return $key;
00053     }
00054 
00055     if (empty($application)) {
00056         $application = $DB->get_record('mnet_application', array('name'=>'moodle'));
00057     }
00058 
00059     $rq = xmlrpc_encode_request('system/keyswap', array($CFG->wwwroot, $mnet->public_key, $application->name), array("encoding" => "utf-8"));
00060     $ch = curl_init($uri . $application->xmlrpc_server_url);
00061 
00062     curl_setopt($ch, CURLOPT_TIMEOUT, 60);
00063     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
00064     curl_setopt($ch, CURLOPT_POST, true);
00065     curl_setopt($ch, CURLOPT_USERAGENT, 'Moodle');
00066     curl_setopt($ch, CURLOPT_POSTFIELDS, $rq);
00067     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml charset=UTF-8"));
00068     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
00069     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
00070 
00071     // check for proxy
00072     if (!empty($CFG->proxyhost) and !is_proxybypass($uri)) {
00073         // SOCKS supported in PHP5 only
00074         if (!empty($CFG->proxytype) and ($CFG->proxytype == 'SOCKS5')) {
00075             if (defined('CURLPROXY_SOCKS5')) {
00076                 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
00077             } else {
00078                 curl_close($ch);
00079                 print_error( 'socksnotsupported','mnet' );
00080             }
00081         }
00082 
00083         curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
00084 
00085         if (empty($CFG->proxyport)) {
00086             curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
00087         } else {
00088             curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
00089         }
00090 
00091         if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
00092             curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
00093             if (defined('CURLOPT_PROXYAUTH')) {
00094                 // any proxy authentication if PHP 5.1
00095                 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
00096             }
00097         }
00098     }
00099 
00100     $res = xmlrpc_decode(curl_exec($ch));
00101 
00102     // check for curl errors
00103     $curlerrno = curl_errno($ch);
00104     if ($curlerrno!=0) {
00105         debugging("Request for $uri failed with curl error $curlerrno");
00106     }
00107 
00108     // check HTTP error code
00109     $info =  curl_getinfo($ch);
00110     if (!empty($info['http_code']) and ($info['http_code'] != 200)) {
00111         debugging("Request for $uri failed with HTTP code ".$info['http_code']);
00112     }
00113 
00114     curl_close($ch);
00115 
00116     if (!is_array($res)) { // ! error
00117         $public_certificate = $res;
00118         $credentials=array();
00119         if (strlen(trim($public_certificate))) {
00120             $credentials = openssl_x509_parse($public_certificate);
00121             $host = $credentials['subject']['CN'];
00122             if (array_key_exists( 'subjectAltName', $credentials['subject'])) {
00123                 $host = $credentials['subject']['subjectAltName'];
00124             }
00125             if (strpos($uri, $host) !== false) {
00126                 mnet_set_public_key($uri, $public_certificate);
00127                 return $public_certificate;
00128             }
00129             else {
00130                 debugging("Request for $uri returned public key for different URI - $host");
00131             }
00132         }
00133         else {
00134             debugging("Request for $uri returned empty response");
00135         }
00136     }
00137     else {
00138         debugging( "Request for $uri returned unexpected result");
00139     }
00140     return false;
00141 }
00142 
00153 function mnet_set_public_key($uri, $key = null) {
00154     static $keyarray = array();
00155     if (isset($keyarray[$uri]) && empty($key)) {
00156         return $keyarray[$uri];
00157     } elseif (!empty($key)) {
00158         $keyarray[$uri] = $key;
00159         return true;
00160     }
00161     return false;
00162 }
00163 
00189 function mnet_sign_message($message, $privatekey = null) {
00190     global $CFG;
00191     $digest = sha1($message);
00192 
00193     $mnet = get_mnet_environment();
00194     // If the user hasn't supplied a private key (for example, one of our older,
00195     //  expired private keys, we get the current default private key and use that.
00196     if ($privatekey == null) {
00197         $privatekey = $mnet->get_private_key();
00198     }
00199 
00200     // The '$sig' value below is returned by reference.
00201     // We initialize it first to stop my IDE from complaining.
00202     $sig  = '';
00203     $bool = openssl_sign($message, $sig, $privatekey); // TODO: On failure?
00204 
00205     $message = '<?xml version="1.0" encoding="iso-8859-1"?>
00206     <signedMessage>
00207         <Signature Id="MoodleSignature" xmlns="http://www.w3.org/2000/09/xmldsig#">
00208             <SignedInfo>
00209                 <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
00210                 <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
00211                 <Reference URI="#XMLRPC-MSG">
00212                     <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
00213                     <DigestValue>'.$digest.'</DigestValue>
00214                 </Reference>
00215             </SignedInfo>
00216             <SignatureValue>'.base64_encode($sig).'</SignatureValue>
00217             <KeyInfo>
00218                 <RetrievalMethod URI="'.$CFG->wwwroot.'/mnet/publickey.php"/>
00219             </KeyInfo>
00220         </Signature>
00221         <object ID="XMLRPC-MSG">'.base64_encode($message).'</object>
00222         <wwwroot>'.$mnet->wwwroot.'</wwwroot>
00223         <timestamp>'.time().'</timestamp>
00224     </signedMessage>';
00225     return $message;
00226 }
00227 
00253 function mnet_encrypt_message($message, $remote_certificate) {
00254     $mnet = get_mnet_environment();
00255 
00256     // Generate a key resource from the remote_certificate text string
00257     $publickey = openssl_get_publickey($remote_certificate);
00258 
00259     if ( gettype($publickey) != 'resource' ) {
00260         // Remote certificate is faulty.
00261         return false;
00262     }
00263 
00264     // Initialize vars
00265     $encryptedstring = '';
00266     $symmetric_keys = array();
00267 
00268     //        passed by ref ->     &$encryptedstring &$symmetric_keys
00269     $bool = openssl_seal($message, $encryptedstring, $symmetric_keys, array($publickey));
00270     $message = $encryptedstring;
00271     $symmetrickey = array_pop($symmetric_keys);
00272 
00273     $message = '<?xml version="1.0" encoding="iso-8859-1"?>
00274     <encryptedMessage>
00275         <EncryptedData Id="ED" xmlns="http://www.w3.org/2001/04/xmlenc#">
00276             <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#arcfour"/>
00277             <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
00278                 <ds:RetrievalMethod URI="#EK" Type="http://www.w3.org/2001/04/xmlenc#EncryptedKey"/>
00279                 <ds:KeyName>XMLENC</ds:KeyName>
00280             </ds:KeyInfo>
00281             <CipherData>
00282                 <CipherValue>'.base64_encode($message).'</CipherValue>
00283             </CipherData>
00284         </EncryptedData>
00285         <EncryptedKey Id="EK" xmlns="http://www.w3.org/2001/04/xmlenc#">
00286             <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
00287             <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
00288                 <ds:KeyName>SSLKEY</ds:KeyName>
00289             </ds:KeyInfo>
00290             <CipherData>
00291                 <CipherValue>'.base64_encode($symmetrickey).'</CipherValue>
00292             </CipherData>
00293             <ReferenceList>
00294                 <DataReference URI="#ED"/>
00295             </ReferenceList>
00296             <CarriedKeyName>XMLENC</CarriedKeyName>
00297         </EncryptedKey>
00298         <wwwroot>'.$mnet->wwwroot.'</wwwroot>
00299     </encryptedMessage>';
00300     return $message;
00301 }
00302 
00312 function mnet_get_keypair() {
00313     global $CFG, $DB;;
00314     static $keypair = null;
00315     if (!is_null($keypair)) return $keypair;
00316     if ($result = get_config('mnet', 'openssl')) {
00317         list($keypair['certificate'], $keypair['keypair_PEM']) = explode('@@@@@@@@', $result);
00318         $keypair['privatekey'] = openssl_pkey_get_private($keypair['keypair_PEM']);
00319         $keypair['publickey']  = openssl_pkey_get_public($keypair['certificate']);
00320         return $keypair;
00321     } else {
00322         $keypair = mnet_generate_keypair();
00323         return $keypair;
00324     }
00325 }
00326 
00339 function mnet_generate_keypair($dn = null, $days=28) {
00340     global $CFG, $USER, $DB;
00341 
00342     // check if lifetime has been overriden
00343     if (!empty($CFG->mnetkeylifetime)) {
00344         $days = $CFG->mnetkeylifetime;
00345     }
00346 
00347     $host = strtolower($CFG->wwwroot);
00348     $host = preg_replace("~^http(s)?://~",'',$host);
00349     $break = strpos($host.'/' , '/');
00350     $host   = substr($host, 0, $break);
00351 
00352     $site = get_site();
00353     $organization = $site->fullname;
00354 
00355     $keypair = array();
00356 
00357     $country  = 'NZ';
00358     $province = 'Wellington';
00359     $locality = 'Wellington';
00360     $email    = !empty($CFG->noreplyaddress) ? $CFG->noreplyaddress : 'noreply@'.$_SERVER['HTTP_HOST'];
00361 
00362     if(!empty($USER->country)) {
00363         $country  = $USER->country;
00364     }
00365     if(!empty($USER->city)) {
00366         $province = $USER->city;
00367         $locality = $USER->city;
00368     }
00369     if(!empty($USER->email)) {
00370         $email    = $USER->email;
00371     }
00372 
00373     if (is_null($dn)) {
00374         $dn = array(
00375            "countryName" => $country,
00376            "stateOrProvinceName" => $province,
00377            "localityName" => $locality,
00378            "organizationName" => $organization,
00379            "organizationalUnitName" => 'Moodle',
00380            "commonName" => substr($CFG->wwwroot, 0, 64),
00381            "subjectAltName" => $CFG->wwwroot,
00382            "emailAddress" => $email
00383         );
00384     }
00385 
00386     $dnlimits = array(
00387            'countryName'            => 2,
00388            'stateOrProvinceName'    => 128,
00389            'localityName'           => 128,
00390            'organizationName'       => 64,
00391            'organizationalUnitName' => 64,
00392            'commonName'             => 64,
00393            'emailAddress'           => 128
00394     );
00395 
00396     foreach ($dnlimits as $key => $length) {
00397         $dn[$key] = substr($dn[$key], 0, $length);
00398     }
00399 
00400     // ensure we remove trailing slashes
00401     $dn["commonName"] = preg_replace(':/$:', '', $dn["commonName"]);
00402     if (!empty($CFG->opensslcnf)) { //allow specification of openssl.cnf especially for Windows installs
00403         $new_key = openssl_pkey_new(array("config" => $CFG->opensslcnf));
00404     } else {
00405         $new_key = openssl_pkey_new();
00406     }
00407     if ($new_key === false) {
00408         // can not generate keys - missing openssl.cnf??
00409         return null;
00410     }
00411     if (!empty($CFG->opensslcnf)) { //allow specification of openssl.cnf especially for Windows installs
00412         $csr_rsc = openssl_csr_new($dn, $new_key, array("config" => $CFG->opensslcnf));
00413         $selfSignedCert = openssl_csr_sign($csr_rsc, null, $new_key, $days, array("config" => $CFG->opensslcnf));
00414     } else {
00415         $csr_rsc = openssl_csr_new($dn, $new_key, array('private_key_bits',2048));
00416         $selfSignedCert = openssl_csr_sign($csr_rsc, null, $new_key, $days);
00417     }
00418     unset($csr_rsc); // Free up the resource
00419 
00420     // We export our self-signed certificate to a string.
00421     openssl_x509_export($selfSignedCert, $keypair['certificate']);
00422     openssl_x509_free($selfSignedCert);
00423 
00424     // Export your public/private key pair as a PEM encoded string. You
00425     // can protect it with an optional passphrase if you wish.
00426     if (!empty($CFG->opensslcnf)) { //allow specification of openssl.cnf especially for Windows installs
00427         $export = openssl_pkey_export($new_key, $keypair['keypair_PEM'], null, array("config" => $CFG->opensslcnf));
00428     } else {
00429         $export = openssl_pkey_export($new_key, $keypair['keypair_PEM'] /* , $passphrase */);
00430     }
00431     openssl_pkey_free($new_key);
00432     unset($new_key); // Free up the resource
00433 
00434     return $keypair;
00435 }
00436 
00437 
00438 function mnet_update_sso_access_control($username, $mnet_host_id, $accessctrl) {
00439     global $DB;
00440 
00441     $mnethost = $DB->get_record('mnet_host', array('id'=>$mnet_host_id));
00442     if ($aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$username, 'mnet_host_id'=>$mnet_host_id))) {
00443         // update
00444         $aclrecord->accessctrl = $accessctrl;
00445         $DB->update_record('mnet_sso_access_control', $aclrecord);
00446         add_to_log(SITEID, 'admin/mnet', 'update', 'admin/mnet/access_control.php',
00447                 "SSO ACL: $accessctrl user '$username' from {$mnethost->name}");
00448     } else {
00449         // insert
00450         $aclrecord->username = $username;
00451         $aclrecord->accessctrl = $accessctrl;
00452         $aclrecord->mnet_host_id = $mnet_host_id;
00453         $id = $DB->insert_record('mnet_sso_access_control', $aclrecord);
00454         add_to_log(SITEID, 'admin/mnet', 'add', 'admin/mnet/access_control.php',
00455                 "SSO ACL: $accessctrl user '$username' from {$mnethost->name}");
00456     }
00457     return true;
00458 }
00459 
00460 function mnet_get_peer_host ($mnethostid) {
00461     global $DB;
00462     static $hosts;
00463     if (!isset($hosts[$mnethostid])) {
00464         $host = $DB->get_record('mnet_host', array('id' => $mnethostid));
00465         $hosts[$mnethostid] = $host;
00466     }
00467     return $hosts[$mnethostid];
00468 }
00469 
00489 function mnet_sso_apply_indirection ($jumpurl, $url) {
00490     global $USER, $CFG;
00491 
00492     $localpart='';
00493     $urlparts = parse_url($url[1]);
00494     if($urlparts) {
00495         if (isset($urlparts['path'])) {
00496             $path = $urlparts['path'];
00497             // if our wwwroot has a path component, need to strip that path from beginning of the
00498             // 'localpart' to make it relative to moodle's wwwroot
00499             $wwwrootpath = parse_url($CFG->wwwroot, PHP_URL_PATH);
00500             if (!empty($wwwrootpath) and strpos($path, $wwwrootpath) === 0) {
00501                 $path = substr($path, strlen($wwwrootpath));
00502             }
00503             $localpart .= $path;
00504         }
00505         if (isset($urlparts['query'])) {
00506             $localpart .= '?'.$urlparts['query'];
00507         }
00508         if (isset($urlparts['fragment'])) {
00509             $localpart .= '#'.$urlparts['fragment'];
00510         }
00511     }
00512     $indirecturl = $jumpurl . urlencode($localpart);
00513     //If we matched on more than just a url (ie an html link), return the url to an href format
00514     if ($url[0] != $url[1]) {
00515         $indirecturl = 'href="'.$indirecturl.'"';
00516     }
00517     return $indirecturl;
00518 }
00519 
00520 function mnet_get_app_jumppath ($applicationid) {
00521     global $DB;
00522     static $appjumppaths;
00523     if (!isset($appjumppaths[$applicationid])) {
00524         $ssojumpurl = $DB->get_field('mnet_application', 'sso_jump_url', array('id' => $applicationid));
00525         $appjumppaths[$applicationid] = $ssojumpurl;
00526     }
00527     return $appjumppaths[$applicationid];
00528 }
00529 
00530 
00537 function mnet_debug($debugdata, $debuglevel=1) {
00538     global $CFG;
00539     $setlevel = get_config('', 'mnet_rpcdebug');
00540     if (empty($setlevel) || $setlevel < $debuglevel) {
00541         return;
00542     }
00543     if (is_object($debugdata)) {
00544         $debugdata = (array)$debugdata;
00545     }
00546     if (is_array($debugdata)) {
00547         mnet_debug('DUMPING ARRAY');
00548         foreach ($debugdata as $key => $value) {
00549             mnet_debug("$key: $value");
00550         }
00551         mnet_debug('END DUMPING ARRAY');
00552         return;
00553     }
00554     $prefix = 'MNET DEBUG ';
00555     if (defined('MNET_SERVER')) {
00556         $prefix .= " (server $CFG->wwwroot";
00557         if ($peer = get_mnet_remote_client() && !empty($peer->wwwroot)) {
00558             $prefix .= ", remote peer " . $peer->wwwroot;
00559         }
00560         $prefix .= ')';
00561     } else {
00562         $prefix .= " (client $CFG->wwwroot) ";
00563     }
00564     error_log("$prefix $debugdata");
00565 }
00566 
00575 function mnet_profile_field_options() {
00576     global $DB;
00577     static $info;
00578     if (!empty($info)) {
00579         return $info;
00580     }
00581 
00582     $excludes = array(
00583         'id',              // makes no sense
00584         'mnethostid',      // makes no sense
00585         'timecreated',     // will be set to relative to the host anyway
00586         'timemodified',    // will be set to relative to the host anyway
00587         'auth',            // going to be set to 'mnet'
00588         'deleted',         // we should never get deleted users sent over, but don't send this anyway
00589         'confirmed',       // unconfirmed users can't log in to their home site, all remote users considered confirmed
00590         'password',        // no password for mnet users
00591         'theme',           // handled separately
00592         'lastip',          // will be set to relative to the host anyway
00593     );
00594 
00595     // these are the ones that user_not_fully_set_up will complain about
00596     // and also special case ones
00597     $forced = array(
00598         'username',
00599         'email',
00600         'firstname',
00601         'lastname',
00602         'auth',
00603         'wwwroot',
00604         'session.gc_lifetime',
00605         '_mnet_userpicture_timemodified',
00606         '_mnet_userpicture_mimetype',
00607     );
00608 
00609     // these are the ones we used to send/receive (pre 2.0)
00610     $legacy = array(
00611         'username',
00612         'email',
00613         'auth',
00614         'deleted',
00615         'firstname',
00616         'lastname',
00617         'city',
00618         'country',
00619         'lang',
00620         'timezone',
00621         'description',
00622         'mailformat',
00623         'maildigest',
00624         'maildisplay',
00625         'htmleditor',
00626         'wwwroot',
00627         'picture',
00628     );
00629 
00630     // get a random user record from the database to pull the fields off
00631     $randomuser = $DB->get_record('user', array(), '*', IGNORE_MULTIPLE);
00632     foreach ($randomuser as $key => $discard) {
00633         if (in_array($key, $excludes) || in_array($key, $forced)) {
00634             continue;
00635         }
00636         $fields[$key] = $key;
00637     }
00638     $info = array(
00639         'forced'   => $forced,
00640         'optional' => $fields,
00641         'legacy'   => $legacy,
00642     );
00643     return $info;
00644 }
00645 
00646 
00653 function mnet_get_hosts($withdeleted = false) {
00654     global $CFG, $DB;
00655 
00656     $sql = "SELECT h.id, h.deleted, h.wwwroot, h.ip_address, h.name, h.public_key, h.public_key_expires,
00657                    h.transport, h.portno, h.last_connect_time, h.last_log_id, h.applicationid,
00658                    a.name as app_name, a.display_name as app_display_name, a.xmlrpc_server_url
00659               FROM {mnet_host} h
00660               JOIN {mnet_application} a ON h.applicationid = a.id
00661              WHERE h.id <> ?";
00662 
00663     if (!$withdeleted) {
00664         $sql .= "  AND h.deleted = 0";
00665     }
00666 
00667     $sql .= " ORDER BY h.deleted, h.name, h.id";
00668 
00669     return $DB->get_records_sql($sql, array($CFG->mnet_localhost_id));
00670 }
00671 
00672 
00702 function mnet_get_service_info(mnet_peer $mnet_peer, $fulldata=true) {
00703     global $CFG, $DB;
00704 
00705     $requestkey = (!empty($fulldata) ? 'fulldata' : 'mydata');
00706 
00707     static $cache = array();
00708     if (array_key_exists($mnet_peer->id, $cache)) {
00709         return $cache[$mnet_peer->id][$requestkey];
00710     }
00711 
00712     $id_list = $mnet_peer->id;
00713     if (!empty($CFG->mnet_all_hosts_id)) {
00714         $id_list .= ', '.$CFG->mnet_all_hosts_id;
00715     }
00716 
00717     $concat = $DB->sql_concat('COALESCE(h2s.id,0) ', ' \'-\' ', ' svc.id', '\'-\'', 'r.plugintype', '\'-\'', 'r.pluginname');
00718 
00719     $query = "
00720         SELECT DISTINCT
00721             $concat as id,
00722             svc.id as serviceid,
00723             svc.name,
00724             svc.offer,
00725             svc.apiversion,
00726             r.plugintype,
00727             r.pluginname,
00728             h2s.hostid,
00729             h2s.publish,
00730             h2s.subscribe
00731         FROM
00732             {mnet_service2rpc} s2r,
00733             {mnet_rpc} r,
00734             {mnet_service} svc
00735         LEFT JOIN
00736             {mnet_host2service} h2s
00737         ON
00738             h2s.hostid in ($id_list) AND
00739             h2s.serviceid = svc.id
00740         WHERE
00741             svc.offer = '1' AND
00742             s2r.serviceid = svc.id AND
00743             s2r.rpcid = r.id
00744         ORDER BY
00745             svc.name ASC";
00746 
00747     $resultset = $DB->get_records_sql($query);
00748 
00749     if (is_array($resultset)) {
00750         $resultset = array_values($resultset);
00751     } else {
00752         $resultset = array();
00753     }
00754 
00755     require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
00756 
00757     $remoteservices = array();
00758     if ($mnet_peer->id != $CFG->mnet_all_hosts_id) {
00759         // Create a new request object
00760         $mnet_request = new mnet_xmlrpc_client();
00761 
00762         // Tell it the path to the method that we want to execute
00763         $mnet_request->set_method('system/listServices');
00764         $mnet_request->send($mnet_peer);
00765         if (is_array($mnet_request->response)) {
00766             foreach($mnet_request->response as $service) {
00767                 $remoteservices[$service['name']][$service['apiversion']] = $service;
00768             }
00769         }
00770     }
00771 
00772     $myservices = array();
00773     $mydata = array();
00774     foreach($resultset as $result) {
00775         $result->hostpublishes  = false;
00776         $result->hostsubscribes = false;
00777         if (isset($remoteservices[$result->name][$result->apiversion])) {
00778             if ($remoteservices[$result->name][$result->apiversion]['publish'] == 1) {
00779                 $result->hostpublishes  = true;
00780             }
00781             if ($remoteservices[$result->name][$result->apiversion]['subscribe'] == 1) {
00782                 $result->hostsubscribes  = true;
00783             }
00784         }
00785 
00786         if (empty($myservices[$result->name][$result->apiversion])) {
00787             $myservices[$result->name][$result->apiversion] = array('serviceid' => $result->serviceid,
00788                                                                     'name' => $result->name,
00789                                                                     'offer' => $result->offer,
00790                                                                     'apiversion' => $result->apiversion,
00791                                                                     'plugintype' => $result->plugintype,
00792                                                                     'pluginname' => $result->pluginname,
00793                                                                     'hostsubscribes' => $result->hostsubscribes,
00794                                                                     'hostpublishes' => $result->hostpublishes
00795                                                                     );
00796         }
00797 
00798         // allhosts_publish allows us to tell the admin that even though he
00799         // is disabling a service, it's still available to the host because
00800         // he's also publishing it to 'all hosts'
00801         if ($result->hostid == $CFG->mnet_all_hosts_id && $CFG->mnet_all_hosts_id != $mnet_peer->id) {
00802             $myservices[$result->name][$result->apiversion]['allhosts_publish'] = $result->publish;
00803             $myservices[$result->name][$result->apiversion]['allhosts_subscribe'] = $result->subscribe;
00804         } elseif (!empty($result->hostid)) {
00805             $myservices[$result->name][$result->apiversion]['I_publish'] = $result->publish;
00806             $myservices[$result->name][$result->apiversion]['I_subscribe'] = $result->subscribe;
00807         }
00808         $mydata['publish'][$result->serviceid] = $result->publish;
00809         $mydata['subscribe'][$result->serviceid] = $result->subscribe;
00810 
00811     }
00812 
00813     $cache[$mnet_peer->id]['fulldata'] = $myservices;
00814     $cache[$mnet_peer->id]['mydata'] = $mydata;
00815 
00816     return $cache[$mnet_peer->id][$requestkey];
00817 }
00818 
00827 function mnet_fields_to_send(mnet_peer $peer) {
00828     return _mnet_field_helper($peer, 'export');
00829 }
00830 
00839 function mnet_fields_to_import(mnet_peer $peer) {
00840     return _mnet_field_helper($peer, 'import');
00841 }
00842 
00853 function _mnet_field_helper(mnet_peer $peer, $key) {
00854     $tmp = mnet_profile_field_options();
00855     $defaults = explode(',', get_config('moodle', 'mnetprofile' . $key . 'fields'));
00856     if ('1' === get_config('mnet', 'host' . $peer->id . $key . 'default')) {
00857         return array_merge($tmp['forced'], $defaults);
00858     }
00859     $hostsettings = get_config('mnet', 'host' . $peer->id . $key . 'fields');
00860     if (false === $hostsettings) {
00861         return array_merge($tmp['forced'], $defaults);
00862     }
00863     return array_merge($tmp['forced'], explode(',', $hostsettings));
00864 }
00865 
00866 
00877 function mnet_strip_user($user, $fields) {
00878     if (is_object($user)) {
00879         $user = (array)$user;
00880         $wasobject = true; // so we can cast back before we return
00881     }
00882 
00883     foreach ($user as $key => $value) {
00884         if (!in_array($key, $fields)) {
00885             unset($user[$key]);
00886         }
00887     }
00888     if (!empty($wasobject)) {
00889         $user = (object)$user;
00890     }
00891     return $user;
00892 }
 All Data Structures Namespaces Files Functions Variables Enumerations