Exemplo n.º 1
0
/**
 * Read a folder node and process a directory node reference to get a list of
 * folders and files within it.
 *
 * @param string $uuid     The node UUID.
 * @param bool   $useadmin Set to false to make sure that the administrative user configured in
 *                         the plug-in is not used for this operation (default: true).
 * @return object An object containing an array of folders and file node references.
 */
function alfresco_read_dir($uuid = '', $useadmin = true)
{
    global $USER;
    $return = new stdClass();
    $return->folders = array();
    $return->files = array();
    if (empty($uuid)) {
        if (repository_plugin_alfresco::is_version('3.2')) {
            $services = alfresco_get_services();
            $response = alfresco_request($services['root']);
        } else {
            if (repository_plugin_alfresco::is_version('3.4')) {
                // Force the usage of the configured Alfresco admin account, if requested.
                if ($useadmin) {
                    $username = '';
                } else {
                    if (isloggedin()) {
                        $username = $USER->username;
                        // Fix username
                        $username = repository_plugin_alfresco::fix_username($username);
                    } else {
                        $username = '';
                    }
                }
                $response = alfresco_request('/cmis/p/children', $username);
            }
        }
    } else {
        if (alfresco_get_type($uuid) != ALFRESCO_TYPE_FOLDER) {
            return;
        }
        // Force the usage of the configured Alfresco admin account, if requested.
        if ($useadmin) {
            $username = '';
        } else {
            if (isloggedin()) {
                $username = $USER->username;
                // Fix username
                $username = repository_plugin_alfresco::fix_username($username);
            } else {
                $username = '';
            }
        }
        if (repository_plugin_alfresco::is_version('3.2')) {
            $response = alfresco_request(alfresco_get_uri($uuid, 'children'), $username);
        } else {
            if (repository_plugin_alfresco::is_version('3.4')) {
                $response = alfresco_request('/cmis/i/' . $uuid . '/children', $username);
            }
        }
    }
    if (empty($response)) {
        return $return;
    }
    $response = preg_replace('/(&[^amp;])+/', '&', $response);
    $dom = new DOMDocument();
    $dom->preserveWhiteSpace = false;
    $dom->loadXML($response);
    $nodes = $dom->getElementsByTagName('entry');
    for ($i = 0; $i < $nodes->length; $i++) {
        $node = $nodes->item($i);
        $type = '';
        $contentNode = alfresco_process_node($dom, $node, $type);
        if ($type == ALFRESCO_TYPE_FOLDER) {
            $return->folders[] = $contentNode;
            // Only include a file in the list if it's title does not start with a period '.'
        } else {
            if ($type == ALFRESCO_TYPE_DOCUMENT && !empty($contentNode->title) && $contentNode->title[0] !== '.') {
                $return->files[] = $contentNode;
            }
        }
    }
    usort($return->folders, 'alfresco_ls_sort');
    usort($return->files, 'alfresco_ls_sort');
    return $return;
}
Exemplo n.º 2
0
 /**
  * Verify that the Alfresco repository is currently setup and ready to be
  * used with Moodle (i.e. the needed directory structure is in place).
  *
  * @uses $CFG
  * @param none
  * @return bool True if setup, False otherwise.
  */
 function verify_setup()
 {
     global $CFG, $USER;
     if (ALFRESCO_DEBUG_TRACE) {
         mtrace('verify_setup()');
     }
     if (!$this->get_defaults()) {
         return false;
     }
     if (self::is_version('3.2')) {
         if (!alfresco_get_services()) {
             return false;
         }
         // Set up the root node
         $response = alfresco_request(alfresco_get_uri('', 'sites'));
         $response = preg_replace('/(&[^amp;])+/', '&amp;', $response);
         $dom = new DOMDocument();
         $dom->preserveWhiteSpace = false;
         $dom->loadXML($response);
         $nodes = $dom->getElementsByTagName('entry');
         $type = '';
         $this->root = alfresco_process_node($dom, $nodes->item(0), $type);
     } else {
         if (self::is_version('3.4')) {
             if (empty($this->cmis)) {
                 $this->cmis = new CMISService(alfresco_base_url() . '/api/cmis', $CFG->repository_alfresco_server_username, $CFG->repository_alfresco_server_password);
                 if (empty($this->cmis->authenticated)) {
                     return false;
                 }
                 if (!($root = $this->cmis->getObjectByPath('/'))) {
                     return false;
                 }
                 $type = '';
                 $this->root = alfresco_process_node_new($root, $type);
             }
         }
     }
     // If there is no root folder saved or it's set to default,
     // make sure there is a default '/moodle' folder.
     if (empty($CFG->repository_alfresco_root_folder) || $CFG->repository_alfresco_root_folder == '/moodle') {
         $root = $this->get_root();
         if ($root == false || !isset($root->uuid)) {
             return false;
         }
         $dir = $this->read_dir($root->uuid, true);
         if (!empty($dir->folders)) {
             foreach ($dir->folders as $folder) {
                 if ($folder->title == 'moodle') {
                     $muuid = $folder->uuid;
                 }
             }
         }
         // Create the main Moodle directory.
         if (empty($muuid)) {
             $muuid = $this->create_dir('moodle', $root->uuid, '', true);
             if ($muuid === false) {
                 return false;
             }
         }
         if (empty($muuid)) {
             debugging(get_string('invalidpath', 'repository_alfresco'));
             return false;
         }
         $this->muuid = $muuid;
         $this->node_inherit($muuid, false);
         // Otherwise, use the folder that the plug-in has been configured with.
     } else {
         if (!($uuid = alfresco_uuid_from_path($CFG->repository_alfresco_root_folder))) {
             debugging(get_string('invalidpath', 'repository_alfresco'));
             return false;
         }
         $this->muuid = $uuid;
         $this->node_inherit($uuid, false);
     }
     // Attempt to find the UUID of the main storage folders within the root.
     $dir = $this->read_dir($this->muuid, true);
     if (!empty($dir->folders)) {
         foreach ($dir->folders as $folder) {
             if ($folder->title == 'shared') {
                 $this->suuid = $folder->uuid;
             } else {
                 if ($folder->title == 'course') {
                     $this->cuuid = $folder->uuid;
                 } else {
                     if ($folder->title == 'user') {
                         $this->uuuid = $folder->uuid;
                     } else {
                         if ($folder->title == 'organization') {
                             $this->ouuid = $folder->uuid;
                         }
                     }
                 }
             }
         }
     }
     // Create the shared storage directory.
     if (empty($this->suuid)) {
         $suuid = $this->create_dir('shared', $this->muuid, true);
         if ($suuid === false) {
             return false;
         }
         $this->suuid = $suuid;
         $this->node_inherit($suuid, false);
     }
     // Create the course space directory.
     if (empty($this->cuuid)) {
         $cuuid = $this->create_dir('course', $this->muuid, true);
         if ($cuuid === false) {
             return false;
         }
         $this->cuuid = $cuuid;
         $this->node_inherit($cuuid, false);
     }
     // Create the organization shared storage directory.
     if (empty($this->ouuid)) {
         $ouuid = $this->create_dir('organization', $this->muuid, true);
         if ($ouuid === false) {
             return false;
         }
         $this->ouuid = $ouuid;
         $this->node_inherit($ouuid, false);
     }
     // We no longer will automatically create the course space directory as it's no longer needed.
     // Make sure the temp directory is enabled.
     if (!is_dir($CFG->dataroot . '/temp/alfresco')) {
         mkdir($CFG->dataroot . '/temp/alfresco', $CFG->directorypermissions, true);
     }
     return true;
 }