Merge branch 'm20_MDL-22449_wiki_help_buttons' of git://github.com/dongsheng/moodle
[moodle.git] / repository / alfresco / lib.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    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
28  */
30 class repository_alfresco extends repository {
31     private $ticket = null;
32     private $user_session = null;
33     private $store = null;
34     private $alfresco;
36     public function __construct($repositoryid, $context = SYSCONTEXTID, $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
46             $server_url = '';
47             if (!empty($this->options['alfresco_url'])) {
48                 $server_url = $this->options['alfresco_url'];
49             } else {
50                 return;
51             }
52             $this->alfresco = 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->alfresco->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->alfresco->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 = new stdClass();
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';
84             $passwd_field = new stdClass();
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';
90             $ret = array();
91             $ret['login'] = array($user_field, $passwd_field);
92             return $ret;
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" />';
101         }
102     }
104     public function logout() {
105         global $SESSION;
106         unset($SESSION->{$this->sessname});
107         return $this->print_login();
108     }
110     public function check_login() {
111         global $SESSION;
112         return !empty($SESSION->{$this->sessname});
113     }
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     }
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      */
138     public function get_listing($uuid = '', $path = '') {
139         global $CFG, $SESSION, $OUTPUT;
140         $ret = array();
141         $ret['dynload'] = true;
142         $ret['list'] = array();
143         $server_url = $this->options['alfresco_url'];
144         $pattern = '#^(.*)api#';
145         if ($return = preg_match($pattern, $server_url, $matches)) {
146             $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp';
147         }
149         $ret['path'] = array(array('name'=>get_string('pluginname', 'repository_alfresco'), 'path'=>''));
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             }
158             $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
159             $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
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";
166             foreach ($this->current_node->children as $child)
167             {
168                 if ($child->child->type == $folder_filter or
169                     $child->child->type == $sites_filter or
170                     $child->child->type == $site_filter)
171                 {
172                     $ret['list'][] = array('title'=>$child->child->cm_name,
173                         'path'=>$child->child->id,
174                         'thumbnail'=>$OUTPUT->pix_url('f/folder-32') . '',
175                         'children'=>array());
176                 } elseif ($child->child->type == $file_filter) {
177                     $ret['list'][] = array('title'=>$child->child->cm_name,
178                         'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->child->cm_name, 32))->out(false),
179                         'source'=>$child->child->id);
180                 }
181             }
182         } catch (Exception $e) {
183             unset($SESSION->{$this->sessname});
184             $ret = $this->print_login();
185         }
186         return $ret;
187     }
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      */
196     public function get_file($uuid, $file = '') {
197         $node = $this->user_session->getNode($this->store, $uuid);
198         $url = $this->get_url($node);
199         $path = $this->prepare_file($file);
200         $fp = fopen($path, 'w');
201         $c = new curl;
202         $c->download(array(array('url'=>$url, 'file'=>$fp)));
203         return array('path'=>$path, 'url'=>$url);
204     }
206     public function print_search() {
207         $str = parent::print_search();
208         $str .= '<label>Space: </label><br /><select name="space">';
209         foreach ($this->user_session->stores as $v) {
210             $str .= '<option ';
211             if ($v->__toString() === 'workspace://SpacesStore') {
212                 $str .= 'selected ';
213             }
214             $str .= 'value="';
215             $str .= $v->__toString().'">';
216             $str .= $v->__toString();
217             $str .= '</option>';
218         }
219         $str .= '</select>';
220         return $str;
221     }
223     /**
224      * Look for a file
225      *
226      * @param string $search_text
227      * @return array
228      */
229     public function search($search_text) {
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 true;
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;
281     }