3a2d22b4c93ea0edfcc4736062d403133264141d
[moodle.git] / repository / alfresco / repository.class.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * repository_alfresco class
20  * This is a class used to browse files from alfresco
21  *
22  * @since 2.0
23  * @package moodlecore
24  * @subpackage repository
25  * @copyright 2009 Dongsheng Cai
26  * @author Dongsheng Cai <dongsheng@moodle.com>
27  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28  */
30 class repository_alfresco extends repository {
31     private $instance = null;
32     private $ticket = null;
33     private $user_session = null;
34     private $store = null;
36     public function __construct($repositoryid, $context = SITEID, $options = array()) {
37         global $SESSION, $CFG;
38         parent::__construct($repositoryid, $context, $options);
39         $this->sessname = 'alfresco_ticket_'.$this->id;
40         if (class_exists('SoapClient')) {
41             require_once($CFG->libdir . '/alfresco/Service/Repository.php');
42             require_once($CFG->libdir . '/alfresco/Service/Session.php');
43             require_once($CFG->libdir . '/alfresco/Service/SpacesStore.php');
44             require_once($CFG->libdir . '/alfresco/Service/Node.php');
45             // setup alfresco instance
46             $server_url = '';
47             if (!empty($this->options['alfresco_url'])) {
48                 $server_url = $this->options['alfresco_url'];
49             } else {
50                 return;
51             }
52             $this->instance = new Alfresco_Repository($this->options['alfresco_url']);
53             $this->username   = optional_param('al_username', '', PARAM_RAW);
54             $this->password   = optional_param('al_password', '', PARAM_RAW);
55             try{
56                 // deal with user logging in
57                 if (empty($SESSION->{$this->sessname}) && !empty($this->username) && !empty($this->password)) {
58                     $this->ticket = $this->instance->authenticate($this->username, $this->password);
59                     $SESSION->{$this->sessname} = $this->ticket;
60                 } else {
61                     if (!empty($SESSION->{$this->sessname})) {
62                         $this->ticket = $SESSION->{$this->sessname};
63                     }
64                 }
65                 $this->user_session = $this->instance->createSession($this->ticket);
66                 $this->store = new SpacesStore($this->user_session);
67             } catch (Exception $e) {
68                 $this->logout();
69             }
70             $this->current_node = null;
71         } else {
72             $this->disabled = true;
73         }
74     }
76     public function print_login() {
77         if ($this->options['ajax']) {
78             $user_field->label = get_string('username', 'repository_alfresco').': ';
79             $user_field->id    = 'alfresco_username';
80             $user_field->type  = 'text';
81             $user_field->name  = 'al_username';
83             $passwd_field->label = get_string('password', 'repository_alfresco').': ';
84             $passwd_field->id    = 'alfresco_password';
85             $passwd_field->type  = 'password';
86             $passwd_field->name  = 'al_password';
88             $ret = array();
89             $ret['login'] = array($user_field, $passwd_field);
90             return $ret;
91         } else {
92             echo '<table>';
93             echo '<tr><td><label>'.get_string('username', 'repository_alfresco').'</label></td>';
94             echo '<td><input type="text" name="al_username" /></td></tr>';
95             echo '<tr><td><label>'.get_string('password', 'repository_alfresco').'</label></td>';
96             echo '<td><input type="password" name="al_password" /></td></tr>';
97             echo '</table>';
98             echo '<input type="submit" value="Enter" />';
99         }
100     }
102     public function logout() {
103         global $SESSION;
104         unset($SESSION->{$this->sessname});
105         return $this->print_login();
106     }
108     public function check_login() {
109         global $SESSION;
110         return !empty($SESSION->{$this->sessname});
111     }
113     private function get_url($node) {
114         $result = null;
115         if ($node->type == "{http://www.alfresco.org/model/content/1.0}content") {
116             $contentData = $node->cm_content;
117             if ($contentData != null) {
118                 $result = $contentData->getUrl();
119             }
120         } else {
121             $result = "index.php?".
122                 "&uuid=".$node->id.
123                 "&name=".$node->cm_name.
124                 "&path=".'Company Home';
125         }
126         return $result;
127     }
129     /**
130      * Get a file list from alfresco
131      *
132      * @param string $uuid a unique id of directory in alfresco
133      * @param string $path path to a directory
134      * @return array
135      */
136     public function get_listing($uuid = '', $path = '') {
137         global $CFG, $SESSION, $OUTPUT;
138         $ret = array();
139         $ret['dynload'] = true;
140         $ret['list'] = array();
141         $server_url = $this->options['alfresco_url'];
142         $pattern = '#^(.*)api#';
143         if ($return = preg_match($pattern, $server_url, $matches)) {
144             $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp';
145         }
147         $ret['path'] = array(array('name'=>get_string('repositoryname', 'repository_alfresco'), 'path'=>''));
149         try {
150             if (empty($uuid)) {
151                 $this->current_node = $this->store->companyHome;
152             } else {
153                 $this->current_node = $this->user_session->getNode($this->store, $uuid);
154             }
156             $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
157             $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
159             // top level sites folder
160             $sites_filter = "{http://www.alfresco.org/model/content/1.0}sites";
161             // individual site
162             $site_filter = "{http://www.alfresco.org/model/content/1.0}site";
164             foreach ($this->current_node->children as $child)
165             {
166                 if ($child->child->type == $folder_filter or
167                     $child->child->type == $sites_filter or
168                     $child->child->type == $site_filter)
169                 {
170                     $ret['list'][] = array('title'=>$child->child->cm_name,
171                         'path'=>$child->child->id,
172                         'thumbnail'=>$OUTPUT->pix_url('f/folder-32') . '',
173                         'children'=>array());
174                 } elseif ($child->child->type == $file_filter) {
175                     $ret['list'][] = array('title'=>$child->child->cm_name,
176                         'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->child->cm_name, 32)),
177                         'source'=>$child->child->id);
178                 }
179             }
180         } catch (Exception $e) {
181             unset($SESSION->{$this->sessname});
182             $ret = $this->print_login();
183         }
184         return $ret;
185     }
187     /**
188      * Download a file from alfresco
189      *
190      * @param string $uuid a unique id of directory in alfresco
191      * @param string $path path to a directory
192      * @return array
193      */
194     public function get_file($uuid, $file = '') {
195         global $CFG;
196         $node = $this->user_session->getNode($this->store, $uuid);
197         $url = $this->get_url($node);
198         $path = $this->prepare_file($file);
199         $fp = fopen($path, 'w');
200         $c = new curl;
201         $c->download(array(array('url'=>$url, 'file'=>$fp)));
202         return $path;
203     }
205     public function print_search($client_id) {
206         $str = parent::print_search($client_id);
207         $str .= '<label>Space: </label><br /><select name="space">';
208         foreach ($this->user_session->stores as $v) {
209             $str .= '<option ';
210             if ($v->__toString() === 'workspace://SpacesStore') {
211                 $str .= 'selected ';
212             }
213             $str .= 'value="';
214             $str .= $v->__toString().'">';
215             $str .= $v->__toString();
216             $str .= '</option>';
217         }
218         $str .= '</select>';
219         return $str;
220     }
222     /**
223      * Look for a file
224      *
225      * @param string $search_text
226      * @return array
227      */
228     public function search($search_text) {
229         global $CFG;
230         $space = optional_param('space', 'workspace://SpacesStore', PARAM_RAW);
231         $currentStore = $this->user_session->getStoreFromString($space);
232         $nodes = $this->user_session->query($currentStore, $search_text);
233         $ret = array();
234         $ret['list'] = array();
235         foreach($nodes as $v) {
236             $ret['list'][] = array('title'=>$v->cm_name, 'source'=>$v->id);
237         }
238         return $ret;
239     }
241     /**
242      * Enable mulit-instance
243      *
244      * @return array
245      */
246     public static function get_instance_option_names() {
247         return array('alfresco_url');
248     }
250     /**
251      * define a configuration form
252      *
253      * @return bool
254      */
255     public function instance_config_form($mform) {
256         if (!class_exists('SoapClient')) {
257             $mform->addElement('static', null, get_string('notice'), get_string('soapmustbeenabled', 'repository_alfresco'));
258             return false;
259         }
260         $mform->addElement('text', 'alfresco_url', get_string('alfresco_url', 'repository_alfresco'), array('size' => '40'));
261         $mform->addElement('static', 'alfreco_url_intro', '', get_string('alfrescourltext', 'repository_alfresco'));
262         $mform->addRule('alfresco_url', get_string('required'), 'required', null, 'client');
263         return false;
264     }
266     /**
267      * Check if SOAP extension enabled
268      *
269      * @return bool
270      */
271     public static function plugin_init() {
272         if (!class_exists('SoapClient')) {
273             print_error('soapmustbeenabled', 'repository_alfresco');
274             return false;
275         } else {
276             return true;
277         }
278     }
279     public function supported_returntypes() {
280         return FILE_INTERNAL | FILE_EXTERNAL;
281     }