MDL-17457 migrated all modules to new db/install.php; added upgrade.txt file for...
[moodle.git] / repository / alfresco / repository.class.php
1 <?php
2 /**
3  * repository_alfresco class
4  *
5  * @version $Id$
6  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7  */
8 require_once($CFG->libdir . '/soaplib.php');
10 class repository_alfresco extends repository {
11     private $repo = null;
12     private $ticket = null;
13     private $sess = null;
14     private $store = null;
16     public function __construct($repositoryid, $context = SITEID, $options = array()) {
17         global $SESSION, $CFG;
18         parent::__construct ($repositoryid, $context, $options);
19         if (class_exists('SoapClient')) {
20             require_once($CFG->libdir . '/alfresco/Service/Repository.php');
21             require_once($CFG->libdir . '/alfresco/Service/Session.php');
22             require_once($CFG->libdir . '/alfresco/Service/SpacesStore.php');
23             require_once($CFG->libdir . '/alfresco/Service/Node.php');
24             $this->repo = new Al_Repository($this->alfresco_url);
25             $this->sess_name = 'alfresco_ticket_'.$this->id;
26             $this->username   = optional_param('al_username', '', PARAM_RAW);
27             $this->password   = optional_param('al_password', '', PARAM_RAW);
28             try{
29                 if ( empty($SESSION->{$this->sess_name}) && !empty($this->username) && !empty($this->password)) {
30                     $this->ticket = $this->repo->authenticate($this->username, $this->password);
31                     $SESSION->{$this->sess_name} = $this->ticket;       
32                 } else {
33                     $this->ticket = $SESSION->{$this->sess_name};       
34                 }
35                 $this->sess = $this->repo->createSession($this->ticket);
36                 $this->store = new SpacesStore($this->sess);
37             } catch (Exception $e) {
38                 $this->logout();
39             }
40             $this->current_node = null;
41         } else {
42             $this->disabled = true;
43         }
44     }
45     public function print_login() {
46         if ($this->options['ajax']) {
47             $user_field->label = get_string('username', 'repository_alfresco').': ';
48             $user_field->id    = 'alfresco_username';
49             $user_field->type  = 'text';
50             $user_field->name  = 'al_username';
51             
52             $passwd_field->label = get_string('password', 'repository_alfresco').': ';
53             $passwd_field->id    = 'alfresco_password';
54             $passwd_field->type  = 'password';
55             $passwd_field->name  = 'al_password';
57             $ret = array();
58             $ret['login'] = array($user_field, $passwd_field);
59             return $ret;
60         }
61     }
63     public function logout() {
64         global $SESSION;
65         unset($SESSION->{$this->sess_name});
66         return $this->print_login();
67     }
69     public function check_login() {
70         global $SESSION;
71         return !empty($SESSION->{$this->sess_name});
72     }
74     private function get_url($node) {
75         $result = null;
76         if ($node->type == "{http://www.alfresco.org/model/content/1.0}content") {
77             $contentData = $node->cm_content;
78             if ($contentData != null) {
79                 $result = $contentData->getUrl();
80             }
81         } else {
82             $result = "index.php?".
83                 "&uuid=".$node->id.
84                 "&name=".$node->cm_name.
85                 "&path=".'Company Home';
86         }
87         return $result;
88     }
90     public function get_listing($uuid = '', $search = '') {
91         global $CFG;
93         $ret = array();
94         $ret['dynload'] = true;
95         $ret['list'] = array();
96         $url = $this->alfresco_url;
97         $pattern = '#^(.*)api#';
98         preg_match($pattern, $url, $matches);
99         $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp';
101         $ret['path'] = array(array('name'=>'Root', 'path'=>''));
103         if (empty($uuid)) {
104             $this->current_node = $this->store->companyHome;
105         } else {
106             $this->current_node = $this->sess->getNode($this->store, $uuid);
107         }
108         $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
109         $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
110         foreach ($this->current_node->children as $child)
111         {
112             if ($child->child->type == $folder_filter)
113             {
114                 $ret['list'][] = array('title'=>$child->child->cm_name,
115                     'path'=>$child->child->id,
116                     'thumbnail'=>$CFG->pixpath.'/f/folder.gif',
117                     'children'=>array());
118             } elseif ($child->child->type == $file_filter) {
119                 $ret['list'][] = array('title'=>$child->child->cm_name,
120                     'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo("icon", $child->child->cm_name),
121                     'source'=>$child->child->id);
122             }
123         }
124         return $ret;
125     }
127     public function get_file($uuid, $file = '') {
128         global $CFG;
129         $node = $this->sess->getNode($this->store, $uuid);
130         $url = $this->get_url($node);
131         if (!file_exists($CFG->dataroot.'/repository/download')) {
132             mkdir($CFG->dataroot.'/repository/download/', 0777, true);
133         }
134         if(is_dir($CFG->dataroot.'/repository/download')) {
135             $dir = $CFG->dataroot.'/repository/download/';
136         }
138         if (empty($file)) {
139             $file = $uuid.'_'.time();
140         }
141         if (file_exists($dir.$file)) {
142             $file = uniqid('al_').$file;
143         }
144         $fp = fopen($dir.$file, 'w');
145         $c = new curl;
146         $c->download(array(array('url'=>$url, 'file'=>$fp)));
147         return $dir.$file;
148     }
150     public function print_search() {
151         parent::print_search();
152         echo '<label>Space: </label><br /><select name="space">';
153         foreach ($this->sess->stores as $v) {   
154             echo '<option ';
155             if ($v->__toString() === 'workspace://SpacesStore') {
156                 echo 'selected ';
157             }
158             echo 'value="';
159             echo $v->__toString().'">';
160             echo $v->__toString();
161             echo '</option>';
162         }
163         echo '</select>';
164         return true;
165     }
167     public function search($search_text) {
168         global $CFG;
169         $space = optional_param('space', 'workspace://SpacesStore', PARAM_RAW);
170         $currentStore = $this->sess->getStoreFromString($space);        
171         $nodes = $this->sess->query($currentStore, $search_text);
172         $ret = array();
173         $ret['list'] = array();
174         foreach($nodes as $v) {
175             $ret['list'][] = array('title'=>$v->cm_name, 'source'=>$v->id);
176         }
177         return $ret;
178     }
180     public static function get_instance_option_names() {
181         return array('alfresco_url');
182     }
184     public function instance_config_form(&$mform) {
185         if (!class_exists('SoapClient')) {
186             $mform->addElement('static', null, get_string('notice'), get_string('soapmustbeenabled', 'repository_alfresco'));
187             return false;
188         }
189         $mform->addElement('text', 'alfresco_url', get_string('alfresco_url', 'repository_alfresco'), array('size' => '40'));
190         $mform->addRule('alfresco_url', get_string('required'), 'required', null, 'client');
191         return false;
192     }
193     public static function plugin_init() {
194         if (!class_exists('SoapClient')) {
195             print_error('soapmustbeenabled', 'repository_alfresco');
196             return false;
197         } else {
198             return true;
199         }
200     }
202 ?>