/** * Look for a file * * @param string $search_text The search text entered, which contains tokens * representing either file name or file content * @param int $page The page we are showing the contents for * @param int $categories An array of category ids to filter on (i.e. results * must be in one or more to be included), or NULL to not filter * by category * @return array A data structure equivalent to the return value of "get_listing", * containing the filter listing */ function search($search_text, $page = 1, $categories = NULL) { global $CFG, $COURSE, $OUTPUT, $USER; require_once($CFG->dirroot.'/repository/elisfiles/lib/lib.php'); $ret = array(); $shared = 0; $oid = 0; // Set id if (!empty($USER->id)) { $uid = $USER->id; $cid = 0; } if (!empty($COURSE->id)) { $cid = $COURSE->id; $uid = 0; } // setting userid... if ($this->context->contextlevel === CONTEXT_USER) { $userid = $USER->id; } else { $userid = 0; } $courseid = $COURSE->id; $uuid = false; if ($ruuid = $this->elis_files->get_repository_location($courseid, $userid, $shared, $oid)) { $uuid = $ruuid; } else if ($duuid = $this->elis_files->get_default_browsing_location($courseid, $userid, $shared, $oid)) { $uuid = $duuid; } $uuuid = $this->elis_files->get_user_store($USER->id); if ($uuid == $uuuid) { $uid = $USER->id; } else { $uid = 0; } $canedit = self::check_editing_permissions($COURSE->id, $shared, $oid, $uuid, $uid); $ret['canedit'] = $canedit; $ret['detailcols'] = array(array('field'=>'created', 'title'=>get_string('datecreated','repository_elisfiles')), array('field'=>'modified', 'title'=>get_string('datemodified','repository_elisfiles')), array('field'=>'owner', 'title'=>get_string('modifiedby','repository_elisfiles')) ); $ret['dynload'] = true; $ret['nologin'] = true; $ret['showselectedactions'] = true; $ret['showcurrentactions'] = false; // do not show current actions for search results because we are not in a folder // Can only have either course or user id set if (!empty($cid) && !empty($uid)) { $cid = 0; } // Set permissible browsing locations $ret['locations'] = array(); $this->elis_files->file_browse_options($cid, $uid, $shared, $oid, $ret['locations']); $ret['list'] = array(); // Obtain the list of matching files $query = $this->get_search_query($search_text, $categories); $response = elis_files_utils_invoke_service('/moodle/lucenesearch?searchquery='.rawurlencode($query)); $sxml = RLsimpleXMLelement($response); if ($sxml and $entries = $sxml->xpath('//entry')) { foreach ($entries as $entry) { // Include shared and oid parameters $uuid = (string)$entry->uuid; if ($properties = $this->elis_files->get_info($uuid)) { if (strcmp($properties->type, "cmis:folder") !== 0 && strcmp($properties->type, "folder") !== 0) { $params = array('path'=>$uuid, 'shared'=>(boolean)$shared, 'oid'=>(int)$oid, 'cid'=>(int)$cid, 'uid'=>(int)$uid); $encodedpath = base64_encode(serialize($params)); $filesize = isset($properties->filesize) ? $properties->filesize : ''; $created = isset($properties->created) ? $properties->created : ''; $modified = isset($properties->modified) ? $properties->modified : ''; $owner = isset($properties->owner) ? $properties->owner : ''; $alfresco_version = elis_files_get_repository_version(); if ($alfresco_version == '3.2.1') { $thumbnail = $OUTPUT->pix_url(file_extension_icon($entry->filename, 90))->out(false); } else { $thumbnail = $OUTPUT->pix_url(file_extension_icon($entry->icon, 90))->out(false); } $ret['list'][] = array('title'=>$properties->title, 'path'=>$encodedpath, 'size' => $filesize, 'thumbnail' => $thumbnail, 'datecreated'=>$created, 'datemodified'=>$modified, 'author'=>$owner, 'source'=>$uuid); } } } } return $ret; }
/** * Utility function for getting alfresco ticket. * * @uses $CFG * @param string $op Option for refreshing ticket or not. * @param string $username The username to use for user authentication. * @return string|bool The ticket value or, False on error. */ function elis_files_utils_get_ticket($op = 'norefresh', $username = '') { global $CFG; if (ELIS_FILES_DEBUG_TRACE) print_object('elis_files_utils_get_ticket(' . $op . ', ' . $username . ')'); $alf_username = !empty($_SESSION['elis_files_username']) ? $_SESSION['elis_files_username'] : NULL; $alf_ticket = !empty($_SESSION['elis_files_ticket']) ? $_SESSION['elis_files_ticket'] : NULL; if (!empty($username)) { $username = elis_files_transform_username($username); $user = $username; $pass = '******'; } else { if (!isset(elis::$config->elisfiles->server_username) || !isset(elis::$config->elisfiles->server_password)) { return false; } $user = elis::$config->elisfiles->server_username; $pass = elis::$config->elisfiles->server_password; } // We must include the tenant portion of the username here if it is not already included. if ($user != elis::$config->elisfiles->server_username) { if (($tenantpos = strpos(elis::$config->elisfiles->server_username, '@')) > 0) { $tenantname = substr(elis::$config->elisfiles->server_username, $tenantpos); if (strpos($user, $tenantname) === false) { $user .= $tenantname; } } } // Make sure that we refresh the ticket if we're authenticating with a different user than what // the current ticket was generated for. if (empty($alf_username) || (!empty($alf_username) && $alf_username !== $user)) { $op = 'refresh'; } if ($alf_ticket == NULL || $op == 'refresh') { // Authenticate and store the ticket $url = elis_files_utils_get_url('/api/login?u=' . urlencode($user) . '&pw=' . urlencode($pass)); // Prepare curl session $session = curl_init($url); if (ELIS_FILES_DEBUG_TRACE) { curl_setopt($session, CURLOPT_VERBOSE, true); } // Don't return HTTP headers. Do return the contents of the call curl_setopt($session, CURLOPT_RETURNTRANSFER, true); curl_setopt($session, CURLOPT_USERPWD, "$user:$pass"); elis_files_set_curl_timeouts($session); // Make the call $return_data = curl_exec($session); // Get return http status code $httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE); // Close HTTP session curl_close($session); if ($httpcode == 200 || $httpcode == 201) { $sxml = RLsimpleXMLelement($return_data); if (empty($sxml)) { return false; } $alf_ticket = current($sxml); } else { return false; debugging(get_string('errorreceivedfromendpoint', 'repository_elisfiles') . (!empty($response->code) ? $response->code . ' ' : ' ') . $response->error, DEBUG_DEVELOPER); } if ($alf_ticket == '') { debug(get_string('unabletoauthenticatewithendpoint', 'repository_elisfiles'), DEBUG_DEVELOPER); } $_SESSION['elis_files_ticket'] = $alf_ticket; $_SESSION['elis_files_username'] = $user; } return !empty($alf_ticket) ? $alf_ticket : false; }
/** * Utility function for getting alfresco ticket. * * @uses $CFG * @param string $op Option for refreshing ticket or not. * @param string $username The username to use for user authentication. * @return string|bool The ticket value or, False on error. */ function alfresco_utils_get_ticket($op = 'norefresh', $username = '') { global $CFG; if (ALFRESCO_DEBUG_TRACE) { print_object('alfresco_utils_get_ticket(' . $op . ', ' . $username . ')'); } $alf_username = !empty($_SESSION['alfresco_username']) ? $_SESSION['alfresco_username'] : NULL; $alf_ticket = !empty($_SESSION['alfresco_ticket']) ? $_SESSION['alfresco_ticket'] : NULL; if (!empty($username)) { // So that we don't conflict with the default Alfresco admin account. $username = $username == 'admin' ? $CFG->repository_alfresco_admin_username : $username; // We must include the tenant portion of the username here. if (($tenantname = strpos($CFG->repository_alfresco_server_username, '@')) > 0) { $username .= substr($CFG->repository_alfresco_server_username, $tenantname); } $user = $username; $pass = '******'; } else { $user = $CFG->repository_alfresco_server_username; $pass = $CFG->repository_alfresco_server_password; } // We must include the tenant portion of the username here if it is not already included. if ($user != $CFG->repository_alfresco_server_username) { if (($tenantpos = strpos($CFG->repository_alfresco_server_username, '@')) > 0) { $tenantname = substr($CFG->repository_alfresco_server_username, $tenantpos); if (strpos($user, $tenantname) === false) { $user .= $tenantname; } } } // Make sure that we refresh the ticket if we're authenticating with a different user than what // the current ticket was generated for. if (empty($alf_username) || !empty($alf_username) && $alf_username !== $user) { $op = 'refresh'; } if ($alf_ticket == NULL || $op == 'refresh') { // Authenticate and store the ticket $url = alfresco_utils_get_url('/api/login?u=' . urlencode($user) . '&pw=' . urlencode($pass)); // Prepare curl session $session = curl_init($url); if (ALFRESCO_DEBUG_TRACE) { curl_setopt($session, CURLOPT_VERBOSE, true); } // Don't return HTTP headers. Do return the contents of the call curl_setopt($session, CURLOPT_RETURNTRANSFER, true); curl_setopt($session, CURLOPT_USERPWD, "{$user}:{$pass}"); // Make the call $return_data = curl_exec($session); // Get return http status code $httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE); // Close HTTP session curl_close($session); if ($httpcode == 200 || $httpcode == 201) { $sxml = RLsimpleXMLelement($return_data); if (empty($sxml)) { return false; } $alf_ticket = current($sxml); } else { /* debugging(get_string('errorreceivedfromendpoint', 'repository_alfresco') . (!empty($response->code) ? $response->code . ' ' : ' ') . $response->error, DEBUG_DEVELOPER); */ return false; } if ($alf_ticket == '') { debug(get_string('unabletoauthenticatewithendpoint', 'repository_alfresco'), DEBUG_DEVELOPER); } $_SESSION['alfresco_ticket'] = $alf_ticket; $_SESSION['alfresco_username'] = $user; } return !empty($alf_ticket) ? $alf_ticket : false; }
/** * Get info about a specific node reference. * * @param string $uuid A node UUID value. * @return object|bool A node object or false on error. */ function get_info($uuid) { if (ELIS_FILES_DEBUG_TRACE) mtrace('get_info(' . $uuid . ')'); if (!$this->get_defaults()) { return false; } if (self::is_version('3.2')) { $node = elis_files_node_properties($uuid); } else if (self::is_version('3.4')) { //Check that the uuid is valid before trying to get the object if (!elis_files_request(elis_files_get_uri($uuid, 'self'))) { return false; } if (!$node = $this->cmis->getObject('workspace://SpacesStore/' . $uuid)) { return false; } $type = ''; $node = elis_files_process_node_new($node, $type); $node->type = $type; } // ELIS-5750: the following requires the updated webscript: nodeowner.get.js if (!empty($node->uuid) && strpos($node->type, 'document') !== false && ($response = elis_files_request('/moodle/nodeowner/'.$node->uuid)) && ($sxml = RLsimpleXMLelement($response)) && !empty($sxml->owner)) { foreach ((array)$sxml->owner AS $val) { // error_log("nodeOwner: {$val}"); $node->owner = $val; // ($val != 'admin') ? $val : $this->config->admin_username; break; } } return $node; }