3 * Library functions for mnet
5 * @author Donal McMullan donal@catalyst.net.nz
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * Parse a file to find out what functions/methods exist in it, and add entries
13 * for the remote-call-enabled functions to the database.
15 * The path to a file, e.g. auth/mnet/auth.php can be thought of as
16 * type/parentname/docname
18 * @param string $type mod, auth or enrol
19 * @param string $parentname Implementation of type, e.g. 'mnet' in the
20 * case of auth/mnet/auth.php
21 * @return bool True on success, else false
23 function mnet_get_functions($type, $parentname) {
25 $dataobject = new stdClass();
26 $docname = $type.'.php';
29 $docname = 'rpclib.php';
30 $relname = '/mod/'.$parentname.'/'.$docname;
31 $filename = $CFG->dirroot
.$relname;
32 if (file_exists($filename)) include $filename;
33 $mnet_publishes = $parentname.'_mnet_publishes';
34 if (function_exists($mnet_publishes)) {
35 (array)$publishes = $mnet_publishes();
39 $relname = '/'.$type.'/'.$parentname.'/'.$docname;
40 $filename = $CFG->dirroot
.$relname;
41 if (file_exists($filename)) include $filename;
42 $class = $type.($type=='enrol'?
'ment':'').'_plugin_'.$parentname;
43 if (class_exists($class)) {
44 $object = new $class();
45 if (method_exists($object, 'mnet_publishes')) {
46 (array)$publishes = $object->mnet_publishes();
51 $methodServiceArray = array();
52 foreach($publishes as $service) {
53 if (is_array($service['methods'])) {
54 foreach($service['methods'] as $methodname) {
55 $methodServiceArray[$methodname][] = $service;
60 // Disable functions that don't exist (any more) in the source
61 // Should these be deleted? What about their permissions records?
62 $rpcrecords = get_records_select('mnet_rpc', ' parent=\''.$parentname.'\' AND parent_type=\''.$type.'\' ', 'function_name ASC ');
63 if (!empty($rpcrecords)) {
64 foreach($rpcrecords as $rpc) {
65 if (!array_key_exists($rpc->function_name
, $methodServiceArray)) {
67 update_record('mnet_rpc', $rpc);
72 if (!file_exists($filename)) return false;
74 if (extension_loaded('tokenizer')) {
75 include_once $CFG->dirroot
.'/admin/mnet/MethodTable.php';
76 $functions = (array)MethodTable
::create($filename,false);
79 foreach($methodServiceArray as $method => $servicearray) {
80 if (!empty($functions[$method])) {
81 $details = $functions[$method];
82 $profile = $details['arguments'];
83 if (!isset($details['returns'])) {
84 array_unshift($profile, array('type' => 'void', 'description' => 'No return value'));
86 array_unshift($profile, $details['returns']);
88 $dataobject->profile
= serialize($profile);
89 $dataobject->help
= addslashes($details['description']);
91 $dataobject->profile
= serialize(array(array('type' => 'void', 'description' => 'No return value')));
92 $dataobject->help
= '';
95 $dataobject->function_name
= $method;
96 $dataobject->xmlrpc_path
= $type.'/'.$parentname.'/'.$docname.'/'.$method;
97 $dataobject->parent_type
= $type;
98 $dataobject->parent
= $parentname;
99 $dataobject->enabled
= '0';
101 if ($record_exists = get_record('mnet_rpc', 'xmlrpc_path', $dataobject->xmlrpc_path
)) {
102 $dataobject->id
= $record_exists->id
;
103 $dataobject->enabled
= $record_exists->enabled
;
104 update_record('mnet_rpc', $dataobject);
106 $dataobject->id
= insert_record('mnet_rpc', $dataobject, true);
109 foreach($servicearray as $service) {
110 $serviceobj = get_record('mnet_service', 'name', $service['name']);
111 if (false == $serviceobj) {
112 $serviceobj = new stdClass();
113 $serviceobj->name
= $service['name'];
114 $serviceobj->apiversion
= $service['apiversion'];
115 $serviceobj->offer
= 1;
116 $serviceobj->id
= insert_record('mnet_service', $serviceobj, true);
119 if (false == get_record('mnet_service2rpc', 'rpcid', $dataobject->id
, 'serviceid', $serviceobj->id
)) {
120 $obj = new stdClass();
121 $obj->rpcid
= $dataobject->id
;
122 $obj->serviceid
= $serviceobj->id
;
123 insert_record('mnet_service2rpc', $obj, true);
130 function upgrade_RPC_functions($returnurl) {
133 $basedir = $CFG->dirroot
.'/mod';
134 if (file_exists($basedir) && filetype($basedir) == 'dir') {
135 $dirhandle = opendir($basedir);
136 while (false !== ($dir = readdir($dirhandle))) {
137 $firstchar = substr($dir, 0, 1);
138 if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf') {
141 if (filetype($basedir .'/'. $dir) != 'dir') {
145 mnet_get_functions('mod', $dir);
150 $basedir = $CFG->dirroot
.'/auth';
151 if (file_exists($basedir) && filetype($basedir) == 'dir') {
152 $dirhandle = opendir($basedir);
153 while (false !== ($dir = readdir($dirhandle))) {
154 $firstchar = substr($dir, 0, 1);
155 if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf') {
158 if (filetype($basedir .'/'. $dir) != 'dir') {
162 mnet_get_functions('auth', $dir);
166 $basedir = $CFG->dirroot
.'/enrol';
167 if (file_exists($basedir) && filetype($basedir) == 'dir') {
168 $dirhandle = opendir($basedir);
169 while (false !== ($dir = readdir($dirhandle))) {
170 $firstchar = substr($dir, 0, 1);
171 if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf') {
174 if (filetype($basedir .'/'. $dir) != 'dir') {
178 mnet_get_functions('enrol', $dir);