Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/alfresco/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00030 class repository_alfresco extends repository {
00031     private $ticket = null;
00032     private $user_session = null;
00033     private $store = null;
00034     private $alfresco;
00035 
00036     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
00037         global $SESSION, $CFG;
00038         parent::__construct($repositoryid, $context, $options);
00039         $this->sessname = 'alfresco_ticket_'.$this->id;
00040         if (class_exists('SoapClient')) {
00041             require_once($CFG->libdir . '/alfresco/Service/Repository.php');
00042             require_once($CFG->libdir . '/alfresco/Service/Session.php');
00043             require_once($CFG->libdir . '/alfresco/Service/SpacesStore.php');
00044             require_once($CFG->libdir . '/alfresco/Service/Node.php');
00045             // setup alfresco
00046             $server_url = '';
00047             if (!empty($this->options['alfresco_url'])) {
00048                 $server_url = $this->options['alfresco_url'];
00049             } else {
00050                 return;
00051             }
00052             $this->alfresco = new Alfresco_Repository($this->options['alfresco_url']);
00053             $this->username   = optional_param('al_username', '', PARAM_RAW);
00054             $this->password   = optional_param('al_password', '', PARAM_RAW);
00055             try{
00056                 // deal with user logging in
00057                 if (empty($SESSION->{$this->sessname}) && !empty($this->username) && !empty($this->password)) {
00058                     $this->ticket = $this->alfresco->authenticate($this->username, $this->password);
00059                     $SESSION->{$this->sessname} = $this->ticket;
00060                 } else {
00061                     if (!empty($SESSION->{$this->sessname})) {
00062                         $this->ticket = $SESSION->{$this->sessname};
00063                     }
00064                 }
00065                 $this->user_session = $this->alfresco->createSession($this->ticket);
00066                 $this->store = new SpacesStore($this->user_session);
00067             } catch (Exception $e) {
00068                 $this->logout();
00069             }
00070             $this->current_node = null;
00071         } else {
00072             $this->disabled = true;
00073         }
00074     }
00075 
00076     public function print_login() {
00077         if ($this->options['ajax']) {
00078             $user_field = new stdClass();
00079             $user_field->label = get_string('username', 'repository_alfresco').': ';
00080             $user_field->id    = 'alfresco_username';
00081             $user_field->type  = 'text';
00082             $user_field->name  = 'al_username';
00083 
00084             $passwd_field = new stdClass();
00085             $passwd_field->label = get_string('password', 'repository_alfresco').': ';
00086             $passwd_field->id    = 'alfresco_password';
00087             $passwd_field->type  = 'password';
00088             $passwd_field->name  = 'al_password';
00089 
00090             $ret = array();
00091             $ret['login'] = array($user_field, $passwd_field);
00092             return $ret;
00093         } else {
00094             echo '<table>';
00095             echo '<tr><td><label>'.get_string('username', 'repository_alfresco').'</label></td>';
00096             echo '<td><input type="text" name="al_username" /></td></tr>';
00097             echo '<tr><td><label>'.get_string('password', 'repository_alfresco').'</label></td>';
00098             echo '<td><input type="password" name="al_password" /></td></tr>';
00099             echo '</table>';
00100             echo '<input type="submit" value="Enter" />';
00101         }
00102     }
00103 
00104     public function logout() {
00105         global $SESSION;
00106         unset($SESSION->{$this->sessname});
00107         return $this->print_login();
00108     }
00109 
00110     public function check_login() {
00111         global $SESSION;
00112         return !empty($SESSION->{$this->sessname});
00113     }
00114 
00115     private function get_url($node) {
00116         $result = null;
00117         if ($node->type == "{http://www.alfresco.org/model/content/1.0}content") {
00118             $contentData = $node->cm_content;
00119             if ($contentData != null) {
00120                 $result = $contentData->getUrl();
00121             }
00122         } else {
00123             $result = "index.php?".
00124                 "&uuid=".$node->id.
00125                 "&name=".$node->cm_name.
00126                 "&path=".'Company Home';
00127         }
00128         return $result;
00129     }
00130 
00138     public function get_listing($uuid = '', $path = '') {
00139         global $CFG, $SESSION, $OUTPUT;
00140         $ret = array();
00141         $ret['dynload'] = true;
00142         $ret['list'] = array();
00143         $server_url = $this->options['alfresco_url'];
00144         $pattern = '#^(.*)api#';
00145         if ($return = preg_match($pattern, $server_url, $matches)) {
00146             $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp';
00147         }
00148 
00149         $ret['path'] = array(array('name'=>get_string('pluginname', 'repository_alfresco'), 'path'=>''));
00150 
00151         try {
00152             if (empty($uuid)) {
00153                 $this->current_node = $this->store->companyHome;
00154             } else {
00155                 $this->current_node = $this->user_session->getNode($this->store, $uuid);
00156             }
00157 
00158             $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder";
00159             $file_filter = "{http://www.alfresco.org/model/content/1.0}content";
00160 
00161             // top level sites folder
00162             $sites_filter = "{http://www.alfresco.org/model/content/1.0}sites";
00163             // individual site
00164             $site_filter = "{http://www.alfresco.org/model/content/1.0}site";
00165 
00166             foreach ($this->current_node->children as $child)
00167             {
00168                 if ($child->child->type == $folder_filter or
00169                     $child->child->type == $sites_filter or
00170                     $child->child->type == $site_filter)
00171                 {
00172                     $ret['list'][] = array('title'=>$child->child->cm_name,
00173                         'path'=>$child->child->id,
00174                         'thumbnail'=>$OUTPUT->pix_url('f/folder-32') . '',
00175                         'children'=>array());
00176                 } elseif ($child->child->type == $file_filter) {
00177                     $ret['list'][] = array('title'=>$child->child->cm_name,
00178                         'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->child->cm_name, 32))->out(false),
00179                         'source'=>$child->child->id);
00180                 }
00181             }
00182         } catch (Exception $e) {
00183             unset($SESSION->{$this->sessname});
00184             $ret = $this->print_login();
00185         }
00186         return $ret;
00187     }
00188 
00196     public function get_file($uuid, $file = '') {
00197         $node = $this->user_session->getNode($this->store, $uuid);
00198         $url = $this->get_url($node);
00199         $path = $this->prepare_file($file);
00200         $fp = fopen($path, 'w');
00201         $c = new curl;
00202         $c->download(array(array('url'=>$url, 'file'=>$fp)));
00203         return array('path'=>$path, 'url'=>$url);
00204     }
00205 
00212     public function get_link($uuid) {
00213         $node = $this->user_session->getNode($this->store, $uuid);
00214         $url = $this->get_url($node);
00215         return $url;
00216     }
00217 
00218     public function print_search() {
00219         $str = parent::print_search();
00220         $str .= '<label>Space: </label><br /><select name="space">';
00221         foreach ($this->user_session->stores as $v) {
00222             $str .= '<option ';
00223             if ($v->__toString() === 'workspace://SpacesStore') {
00224                 $str .= 'selected ';
00225             }
00226             $str .= 'value="';
00227             $str .= $v->__toString().'">';
00228             $str .= $v->__toString();
00229             $str .= '</option>';
00230         }
00231         $str .= '</select>';
00232         return $str;
00233     }
00234 
00241     public function search($search_text) {
00242         $space = optional_param('space', 'workspace://SpacesStore', PARAM_RAW);
00243         $currentStore = $this->user_session->getStoreFromString($space);
00244         $nodes = $this->user_session->query($currentStore, $search_text);
00245         $ret = array();
00246         $ret['list'] = array();
00247         foreach($nodes as $v) {
00248             $ret['list'][] = array('title'=>$v->cm_name, 'source'=>$v->id);
00249         }
00250         return $ret;
00251     }
00252 
00258     public static function get_instance_option_names() {
00259         return array('alfresco_url');
00260     }
00261 
00267     public function instance_config_form($mform) {
00268         if (!class_exists('SoapClient')) {
00269             $mform->addElement('static', null, get_string('notice'), get_string('soapmustbeenabled', 'repository_alfresco'));
00270             return false;
00271         }
00272         $mform->addElement('text', 'alfresco_url', get_string('alfresco_url', 'repository_alfresco'), array('size' => '40'));
00273         $mform->addElement('static', 'alfreco_url_intro', '', get_string('alfrescourltext', 'repository_alfresco'));
00274         $mform->addRule('alfresco_url', get_string('required'), 'required', null, 'client');
00275         return true;
00276     }
00277 
00283     public static function plugin_init() {
00284         if (!class_exists('SoapClient')) {
00285             print_error('soapmustbeenabled', 'repository_alfresco');
00286             return false;
00287         } else {
00288             return true;
00289         }
00290     }
00291     public function supported_returntypes() {
00292         return (FILE_INTERNAL | FILE_EXTERNAL);
00293     }
00294 }
00295 
 All Data Structures Namespaces Files Functions Variables Enumerations