Merge branch 'm20_MDL-22449_wiki_help_buttons' of git://github.com/dongsheng/moodle
[moodle.git] / repository / alfresco / lib.php
CommitLineData
17e31bb0 1<?php
10d53fd3
DC
2
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/>.
17
17e31bb0 18/**
19 * repository_alfresco class
b9f0ecb3 20 * This is a class used to browse files from alfresco
17e31bb0 21 *
d078f6d3
PS
22 * @since 2.0
23 * @package repository
24 * @subpackage alfresco
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
17e31bb0 28 */
17e31bb0 29
30class repository_alfresco extends repository {
17e31bb0 31 private $ticket = null;
fb8103e6 32 private $user_session = null;
17e31bb0 33 private $store = null;
e8c3e06e 34 private $alfresco;
17e31bb0 35
447c7a19 36 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
17e31bb0 37 global $SESSION, $CFG;
fb8103e6 38 parent::__construct($repositoryid, $context, $options);
39 $this->sessname = 'alfresco_ticket_'.$this->id;
3e0794ed 40 if (class_exists('SoapClient')) {
efe018b4 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');
e8c3e06e 45 // setup alfresco
b9f0ecb3
DC
46 $server_url = '';
47 if (!empty($this->options['alfresco_url'])) {
48 $server_url = $this->options['alfresco_url'];
49 } else {
50 return;
51 }
e8c3e06e 52 $this->alfresco = new Alfresco_Repository($this->options['alfresco_url']);
3e0794ed 53 $this->username = optional_param('al_username', '', PARAM_RAW);
54 $this->password = optional_param('al_password', '', PARAM_RAW);
55 try{
fb8103e6 56 // deal with user logging in
57 if (empty($SESSION->{$this->sessname}) && !empty($this->username) && !empty($this->password)) {
e8c3e06e 58 $this->ticket = $this->alfresco->authenticate($this->username, $this->password);
4454447d 59 $SESSION->{$this->sessname} = $this->ticket;
3e0794ed 60 } else {
fb8103e6 61 if (!empty($SESSION->{$this->sessname})) {
4454447d 62 $this->ticket = $SESSION->{$this->sessname};
fb8103e6 63 }
3e0794ed 64 }
e8c3e06e 65 $this->user_session = $this->alfresco->createSession($this->ticket);
fb8103e6 66 $this->store = new SpacesStore($this->user_session);
3e0794ed 67 } catch (Exception $e) {
68 $this->logout();
26e411c5 69 }
3e0794ed 70 $this->current_node = null;
efe018b4 71 } else {
72 $this->disabled = true;
17e31bb0 73 }
17e31bb0 74 }
b9f0ecb3 75
17e31bb0 76 public function print_login() {
77 if ($this->options['ajax']) {
a27ab6fd 78 $user_field = new stdClass();
17e31bb0 79 $user_field->label = get_string('username', 'repository_alfresco').': ';
80 $user_field->id = 'alfresco_username';
81 $user_field->type = 'text';
82 $user_field->name = 'al_username';
4317f92f 83
a27ab6fd 84 $passwd_field = new stdClass();
17e31bb0 85 $passwd_field->label = get_string('password', 'repository_alfresco').': ';
86 $passwd_field->id = 'alfresco_password';
87 $passwd_field->type = 'password';
88 $passwd_field->name = 'al_password';
89
90 $ret = array();
91 $ret['login'] = array($user_field, $passwd_field);
92 return $ret;
761265ad 93 } else {
94 echo '<table>';
95 echo '<tr><td><label>'.get_string('username', 'repository_alfresco').'</label></td>';
96 echo '<td><input type="text" name="al_username" /></td></tr>';
97 echo '<tr><td><label>'.get_string('password', 'repository_alfresco').'</label></td>';
98 echo '<td><input type="password" name="al_password" /></td></tr>';
99 echo '</table>';
100 echo '<input type="submit" value="Enter" />';
17e31bb0 101 }
102 }
103
104 public function logout() {
105 global $SESSION;
fb8103e6 106 unset($SESSION->{$this->sessname});
17e31bb0 107 return $this->print_login();
108 }
109
110 public function check_login() {
111 global $SESSION;
fb8103e6 112 return !empty($SESSION->{$this->sessname});
17e31bb0 113 }
114
115 private function get_url($node) {
116 $result = null;
117 if ($node->type == "{http://www.alfresco.org/model/content/1.0}content") {
118 $contentData = $node->cm_content;
119 if ($contentData != null) {
120 $result = $contentData->getUrl();
121 }
122 } else {
123 $result = "index.php?".
124 "&uuid=".$node->id.
125 "&name=".$node->cm_name.
126 "&path=".'Company Home';
127 }
128 return $result;
129 }
130
b9f0ecb3
DC
131 /**
132 * Get a file list from alfresco
133 *
134 * @param string $uuid a unique id of directory in alfresco
135 * @param string $path path to a directory
136 * @return array
137 */
5fa358d1 138 public function get_listing($uuid = '', $path = '') {
390baf46 139 global $CFG, $SESSION, $OUTPUT;
17e31bb0 140 $ret = array();
17e31bb0 141 $ret['dynload'] = true;
142 $ret['list'] = array();
b9f0ecb3 143 $server_url = $this->options['alfresco_url'];
e6b45160 144 $pattern = '#^(.*)api#';
b9f0ecb3
DC
145 if ($return = preg_match($pattern, $server_url, $matches)) {
146 $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp';
147 }
e6b45160 148
614d18d2 149 $ret['path'] = array(array('name'=>get_string('pluginname', 'repository_alfresco'), 'path'=>''));
17e31bb0 150
81f4ff72 151 try {
152 if (empty($uuid)) {
153 $this->current_node = $this->store->companyHome;
154 } else {
155 $this->current_node = $this->user_session->getNode($this->store, $uuid);
156 }
b9f0ecb3 157
81f4ff72 158 $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
159 $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
b9f0ecb3
DC
160
161 // top level sites folder
162 $sites_filter = "{http://www.alfresco.org/model/content/1.0}sites";
163 // individual site
164 $site_filter = "{http://www.alfresco.org/model/content/1.0}site";
165
81f4ff72 166 foreach ($this->current_node->children as $child)
17e31bb0 167 {
b9f0ecb3
DC
168 if ($child->child->type == $folder_filter or
169 $child->child->type == $sites_filter or
170 $child->child->type == $site_filter)
81f4ff72 171 {
172 $ret['list'][] = array('title'=>$child->child->cm_name,
173 'path'=>$child->child->id,
b5d0cafc 174 'thumbnail'=>$OUTPUT->pix_url('f/folder-32') . '',
81f4ff72 175 'children'=>array());
176 } elseif ($child->child->type == $file_filter) {
177 $ret['list'][] = array('title'=>$child->child->cm_name,
ede72522 178 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->child->cm_name, 32))->out(false),
81f4ff72 179 'source'=>$child->child->id);
180 }
17e31bb0 181 }
81f4ff72 182 } catch (Exception $e) {
183 unset($SESSION->{$this->sessname});
184 $ret = $this->print_login();
17e31bb0 185 }
186 return $ret;
187 }
188
b9f0ecb3
DC
189 /**
190 * Download a file from alfresco
191 *
192 * @param string $uuid a unique id of directory in alfresco
193 * @param string $path path to a directory
194 * @return array
195 */
17e31bb0 196 public function get_file($uuid, $file = '') {
fb8103e6 197 $node = $this->user_session->getNode($this->store, $uuid);
17e31bb0 198 $url = $this->get_url($node);
a53d4f45 199 $path = $this->prepare_file($file);
200 $fp = fopen($path, 'w');
17e31bb0 201 $c = new curl;
202 $c->download(array(array('url'=>$url, 'file'=>$fp)));
1dce6261 203 return array('path'=>$path, 'url'=>$url);
17e31bb0 204 }
205
e35194be
DC
206 public function print_search() {
207 $str = parent::print_search();
b763c2d9 208 $str .= '<label>Space: </label><br /><select name="space">';
4454447d 209 foreach ($this->user_session->stores as $v) {
b763c2d9 210 $str .= '<option ';
26e411c5 211 if ($v->__toString() === 'workspace://SpacesStore') {
b763c2d9 212 $str .= 'selected ';
26e411c5 213 }
b763c2d9 214 $str .= 'value="';
215 $str .= $v->__toString().'">';
216 $str .= $v->__toString();
217 $str .= '</option>';
26e411c5 218 }
b763c2d9 219 $str .= '</select>';
220 return $str;
17e31bb0 221 }
222
b9f0ecb3
DC
223 /**
224 * Look for a file
225 *
226 * @param string $search_text
227 * @return array
228 */
17e31bb0 229 public function search($search_text) {
26e411c5 230 $space = optional_param('space', 'workspace://SpacesStore', PARAM_RAW);
4454447d 231 $currentStore = $this->user_session->getStoreFromString($space);
fb8103e6 232 $nodes = $this->user_session->query($currentStore, $search_text);
17e31bb0 233 $ret = array();
234 $ret['list'] = array();
26e411c5 235 foreach($nodes as $v) {
236 $ret['list'][] = array('title'=>$v->cm_name, 'source'=>$v->id);
237 }
17e31bb0 238 return $ret;
239 }
240
b9f0ecb3
DC
241 /**
242 * Enable mulit-instance
243 *
244 * @return array
245 */
17e31bb0 246 public static function get_instance_option_names() {
247 return array('alfresco_url');
248 }
249
b9f0ecb3
DC
250 /**
251 * define a configuration form
252 *
253 * @return bool
254 */
7c5c69d6 255 public function instance_config_form($mform) {
f1cfe56e 256 if (!class_exists('SoapClient')) {
3e0794ed 257 $mform->addElement('static', null, get_string('notice'), get_string('soapmustbeenabled', 'repository_alfresco'));
f1cfe56e 258 return false;
3e0794ed 259 }
17e31bb0 260 $mform->addElement('text', 'alfresco_url', get_string('alfresco_url', 'repository_alfresco'), array('size' => '40'));
2b70aa51 261 $mform->addElement('static', 'alfreco_url_intro', '', get_string('alfrescourltext', 'repository_alfresco'));
17e31bb0 262 $mform->addRule('alfresco_url', get_string('required'), 'required', null, 'client');
4f51f6f8 263 return true;
17e31bb0 264 }
b9f0ecb3
DC
265
266 /**
267 * Check if SOAP extension enabled
268 *
269 * @return bool
270 */
3e0794ed 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 }
41076c58 279 public function supported_returntypes() {
3f57d82b 280 return FILE_INTERNAL;
41076c58 281 }
17e31bb0 282}
4317f92f 283