2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This plugin is used to access alfresco repository
21 * @package repository_alfresco
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot
. '/repository/lib.php');
28 * repository_alfresco class
29 * This is a class used to browse files from alfresco
32 * @package repository_alfresco
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class repository_alfresco
extends repository
{
37 private $ticket = null;
38 private $user_session = null;
39 private $store = null;
42 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()) {
43 global $SESSION, $CFG;
44 parent
::__construct($repositoryid, $context, $options);
45 $this->sessname
= 'alfresco_ticket_'.$this->id
;
46 if (class_exists('SoapClient')) {
47 require_once($CFG->libdir
. '/alfresco/Service/Repository.php');
48 require_once($CFG->libdir
. '/alfresco/Service/Session.php');
49 require_once($CFG->libdir
. '/alfresco/Service/SpacesStore.php');
50 require_once($CFG->libdir
. '/alfresco/Service/Node.php');
53 if (!empty($this->options
['alfresco_url'])) {
54 $server_url = $this->options
['alfresco_url'];
58 $this->alfresco
= new Alfresco_Repository($this->options
['alfresco_url']);
59 $this->username
= optional_param('al_username', '', PARAM_RAW
);
60 $this->password
= optional_param('al_password', '', PARAM_RAW
);
62 // deal with user logging in
63 if (empty($SESSION->{$this->sessname
}) && !empty($this->username
) && !empty($this->password
)) {
64 $this->ticket
= $this->alfresco
->authenticate($this->username
, $this->password
);
65 $SESSION->{$this->sessname
} = $this->ticket
;
67 if (!empty($SESSION->{$this->sessname
})) {
68 $this->ticket
= $SESSION->{$this->sessname
};
71 $this->user_session
= $this->alfresco
->createSession($this->ticket
);
72 $this->store
= new SpacesStore($this->user_session
);
73 } catch (Exception
$e) {
76 $this->current_node
= null;
78 $this->disabled
= true;
82 public function print_login() {
83 if ($this->options
['ajax']) {
84 $user_field = new stdClass();
85 $user_field->label
= get_string('username', 'repository_alfresco').': ';
86 $user_field->id
= 'alfresco_username';
87 $user_field->type
= 'text';
88 $user_field->name
= 'al_username';
90 $passwd_field = new stdClass();
91 $passwd_field->label
= get_string('password', 'repository_alfresco').': ';
92 $passwd_field->id
= 'alfresco_password';
93 $passwd_field->type
= 'password';
94 $passwd_field->name
= 'al_password';
97 $ret['login'] = array($user_field, $passwd_field);
101 echo '<tr><td><label>'.get_string('username', 'repository_alfresco').'</label></td>';
102 echo '<td><input type="text" name="al_username" /></td></tr>';
103 echo '<tr><td><label>'.get_string('password', 'repository_alfresco').'</label></td>';
104 echo '<td><input type="password" name="al_password" /></td></tr>';
106 echo '<input type="submit" value="Enter" />';
110 public function logout() {
112 unset($SESSION->{$this->sessname
});
113 return $this->print_login();
116 public function check_login() {
118 return !empty($SESSION->{$this->sessname
});
121 private function get_url($node) {
123 if ($node->type
== "{http://www.alfresco.org/model/content/1.0}content") {
124 $contentData = $node->cm_content
;
125 if ($contentData != null) {
126 $result = $contentData->getUrl();
129 $result = "index.php?".
131 "&name=".$node->cm_name
.
132 "&path=".'Company Home';
138 * Get a file list from alfresco
140 * @param string $uuid a unique id of directory in alfresco
141 * @param string $path path to a directory
144 public function get_listing($uuid = '', $path = '') {
145 global $CFG, $SESSION, $OUTPUT;
147 $ret['dynload'] = true;
148 $ret['list'] = array();
149 $server_url = $this->options
['alfresco_url'];
150 $pattern = '#^(.*)api#';
151 if ($return = preg_match($pattern, $server_url, $matches)) {
152 $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp';
155 $ret['path'] = array(array('name'=>get_string('pluginname', 'repository_alfresco'), 'path'=>''));
159 $this->current_node
= $this->store
->companyHome
;
161 $this->current_node
= $this->user_session
->getNode($this->store
, $uuid);
164 $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
165 $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
167 // top level sites folder
168 $sites_filter = "{http://www.alfresco.org/model/site/1.0}sites";
170 $site_filter = "{http://www.alfresco.org/model/site/1.0}site";
172 foreach ($this->current_node
->children
as $child)
174 if ($child->child
->type
== $folder_filter or
175 $child->child
->type
== $sites_filter or
176 $child->child
->type
== $site_filter)
178 $ret['list'][] = array('title'=>$child->child
->cm_name
,
179 'path'=>$child->child
->id
,
180 'thumbnail'=>$OUTPUT->pix_url(file_folder_icon(90))->out(false),
181 'children'=>array());
182 } elseif ($child->child
->type
== $file_filter) {
183 $ret['list'][] = array('title'=>$child->child
->cm_name
,
184 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->child
->cm_name
, 90))->out(false),
185 'source'=>$child->child
->id
);
188 } catch (Exception
$e) {
189 unset($SESSION->{$this->sessname
});
190 $ret = $this->print_login();
196 * Download a file from alfresco
198 * @param string $uuid a unique id of directory in alfresco
199 * @param string $path path to a directory
202 public function get_file($uuid, $file = '') {
203 $node = $this->user_session
->getNode($this->store
, $uuid);
204 $url = $this->get_url($node);
205 return parent
::get_file($url, $file);
208 public function print_search() {
209 $str = parent
::print_search();
210 $str .= html_writer
::label(get_string('space', 'repository_alfresco'),
211 'repository_alfresco_space',
213 array('class' => 'accesshide'));
214 $str .= '<select id="repository_alfresco_space" class="alfresco-workplace" name="space">';
215 foreach ($this->user_session
->stores
as $v) {
217 if ($v->__toString() === 'workspace://SpacesStore') {
221 $str .= $v->__toString().'">';
222 $str .= $v->__toString();
232 * @param string $search_text
235 public function search($search_text, $page = 0) {
236 $space = optional_param('space', 'workspace://SpacesStore', PARAM_RAW
);
237 $currentStore = $this->user_session
->getStoreFromString($space);
238 $nodes = $this->user_session
->query($currentStore, $search_text);
240 $ret['list'] = array();
241 foreach($nodes as $v) {
242 $ret['list'][] = array('title'=>$v->cm_name
, 'source'=>$v->id
);
248 * Enable mulit-instance
252 public static function get_instance_option_names() {
253 return array('alfresco_url');
257 * define a configuration form
261 public static function instance_config_form($mform) {
262 if (!class_exists('SoapClient')) {
263 $mform->addElement('static', null, get_string('notice'), get_string('soapmustbeenabled', 'repository_alfresco'));
266 $mform->addElement('text', 'alfresco_url', get_string('alfresco_url', 'repository_alfresco'), array('size' => '40'));
267 $mform->setType('alfresco_url', PARAM_URL
);
268 $mform->addElement('static', 'alfreco_url_intro', '', get_string('alfrescourltext', 'repository_alfresco'));
269 $mform->addRule('alfresco_url', get_string('required'), 'required', null, 'client');
274 * Check if SOAP extension enabled
278 public static function plugin_init() {
279 if (!class_exists('SoapClient')) {
280 print_error('soapmustbeenabled', 'repository_alfresco');
286 public function supported_returntypes() {
287 return FILE_INTERNAL
;