From fb8103e6a030047b5650bf1aba81e6cd902ad822 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Fri, 5 Jun 2009 06:55:07 +0000 Subject: [PATCH] "MDL-16384, upgrade alfresco php library, fixed bugs in offical php sdk" --- lib/alfresco/README_MOODLE.txt | 8 +- lib/alfresco/Service/ContentData.php | 582 +++++++++--------- lib/alfresco/Service/Functions.php | 229 +++---- lib/alfresco/Service/Logger/Logger.php | 180 +++--- lib/alfresco/Service/Logger/LoggerConfig.php | 88 +-- lib/alfresco/Service/Node.php | 4 +- lib/alfresco/Service/Repository.php | 272 ++++---- lib/alfresco/Service/Session.php | 4 +- .../Service/WebService/AlfrescoWebService.php | 235 +++---- .../Service/WebService/WebServiceFactory.php | 127 ++-- repository/alfresco/repository.class.php | 45 +- 11 files changed, 894 insertions(+), 880 deletions(-) diff --git a/lib/alfresco/README_MOODLE.txt b/lib/alfresco/README_MOODLE.txt index 4a97f966c1..5f4df9fc1e 100644 --- a/lib/alfresco/README_MOODLE.txt +++ b/lib/alfresco/README_MOODLE.txt @@ -1,14 +1,14 @@ @VERSION $Id$ == CHANGELOG == - -Alfresco PHP Libarary was modified by Moodle, because of class name conflict. -See, Service/Repository.php & Service/WebService/AlfrescoWebService.php -All the changes are marked as "Moodle" +1. Rename class name , see Service/Repository.php & Service/WebService/AlfrescoWebService.php +2. Change library path name +3. In Alfresco_Repository class construct function, set _port to 80 if it is not specified == Alfresco PHP Libarary == Installation and developer documentation for the Alfresco PHP Library can be found on the Alfresco Wiki. +Get the source from http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/modules/php-sdk == Documentation Links == diff --git a/lib/alfresco/Service/ContentData.php b/lib/alfresco/Service/ContentData.php index 0f49351e9c..25a3d78325 100755 --- a/lib/alfresco/Service/ContentData.php +++ b/lib/alfresco/Service/ContentData.php @@ -1,290 +1,292 @@ -_node = $node; - $this->_property = $property; - $this->_mimetype = $mimetype; - $this->_encoding = $encoding; - if ($size != -1) - { - $this->size = $size; - } - $this->_isPopulated = false; - } - - public function setPropertyDetails($node, $property) - { - $this->_node = $node; - $this->_property = $property; - } - - public function __toString() - { - $this->populateContentData(); - } - - public function getNode() - { - return $this->_node; - } - - public function getProperty() - { - return $this->_property; - } - - public function getIsDirty() - { - return $this->_isDirty; - } - - public function getMimetype() - { - $this->populateContentData(); - return $this->_mimetype; - } - - public function setMimetype($mimetype) - { - $this->populateContentData(); - $this->_mimetype = $mimetype; - } - - public function getSize() - { - $this->populateContentData(); - return $this->_size; - } - - public function getEncoding() - { - $this->populateContentData(); - return $this->_encoding; - } - - public function setEncoding($encoding) - { - $this->populateContentData(); - $this->_encoding = $encoding; - } - - public function getUrl() - { - // TODO what should be returned if the content has been updated?? - - $this->populateContentData(); - $result = null; - if ($this->_url != null) - { - $result = $this->_url."?ticket=".$this->_node->session->ticket; - } - return $result; - } - - public function getGuestUrl() - { - // TODO what should be returned if the content has been updated?? - - $this->populateContentData(); - $result = null; - if ($this->_url != null) - { - $result = $this->_url."?guest=true"; - } - return $result; - } - - public function getContent() - { - $this->populateContentData(); - - $result = null; - if ($this->_isDirty == true) - { - if ($this->_newFileContent != null) - { - $handle = fopen($this->_newFileContent, "rb"); - $result = stream_get_contents($handle); - fclose($handle); - } - else if ($this->_newContent != null) - { - $result = $this->_newContent; - } - } - else - { - if ($this->getUrl() != null) - { - $handle = fopen($this->getUrl(), "rb"); - $result = stream_get_contents($handle); - fclose($handle); - } - } - return $result; - } - - public function setContent($content) - { - $this->populateContentData(); - $this->_isDirty = true; - $this->_newContent = $content; - } - - public function writeContentFromFile($fileName) - { - $this->populateContentData(); - $this->_isDirty = true; - $this->_newFileContent = $fileName; - } - - public function readContentToFile($fileName) - { - $handle = fopen($fileName, "wb"); - fwrite($handle, $this->getContent()); - fclose($handle); - } - - public function onBeforeSave(&$statements, $where) - { - if ($this->_isDirty == true) - { - // Check mimetype has been set - if ($this->_mimetype == null) - { - throw new Exception("A mime type for the content property ".$this->_property." on node ".$this->_node->__toString()." must be set"); - } - - // If a file has been specified then read content from there - //$content = null; - if ($this->_newFileContent != null) - { - // Upload the content to the repository - $contentData = upload_file($this->node->session, $this->_newFileContent, $this->_mimetype, $this->_encoding); - - // Set the content property value - $this->addStatement( - $statements, - "update", - array("property" => array( - "name" => $this->property, - "isMultiValue" => false, - "value" => $contentData)) + $where); - } - else - { - // Add the writeContent statement - $this->addStatement( - $statements, - "writeContent", - array( - "property" => $this->_property, - "content" => $this->_newContent, - "format" => array( - "mimetype" => $this->_mimetype, - "encoding" => $this->_encoding)) + - $where); - } - } - } - - public function onAfterSave() - { - $this->_isDirty = false; - $this->_isPopulated = false; - $this->_mimetype = null; - $this->__size = null; - $this->__encoding = null; - $this->__url = null; - $this->__newContent = null; - } - - private function populateContentData() - { - //echo "isPopulated:".$this->_isPopulated."; node:".$this->_node."; property:".$this->_property."
"; - if ($this->_isPopulated == false && $this->_node != null && $this->_property != null && $this->_node->isNewNode == false) - { - $result = $this->_node->session->contentService->read( array( - "items" => array( - "nodes" => array( - "store" => $this->_node->store->__toArray(), - "uuid" => $this->_node->id)), - "property" => $this->_property) ); - if (isset($result->content) == true) - { - if (isset($result->content->length) == true) - { - $this->_size = $result->content->length; - } - if (isset($result->content->format->mimetype) == true) - { - $this->_mimetype = $result->content->format->mimetype; - } - if (isset($result->content->format->encoding) == true) - { - $this->_encoding = $result->content->format->encoding; - } - if (isset($result->content->url) == true) - { - $this->_url = $result->content->url; - } - } - - $this->_isPopulated = true; - } - } - - private function addStatement(&$statements, $statement, $body) - { - $result = array(); - if (array_key_exists($statement, $statements) == true) - { - $result = $statements[$statement]; - } - $result[] = $body; - $statements[$statement] = $result; - } -} -?> +libdir."/alfresco/Service/Functions.php"); + +class ContentData extends BaseObject +{ + private $_isPopulated = false; + private $_isDirty = false; + + private $_node; + private $_property; + + private $_mimetype; + private $_size; + private $_encoding; + private $_url; + private $_newContent; + private $_newFileContent; + + public function __construct($node, $property, $mimetype=null, $encoding=null, $size=-1) + { + $this->_node = $node; + $this->_property = $property; + $this->_mimetype = $mimetype; + $this->_encoding = $encoding; + if ($size != -1) + { + $this->size = $size; + } + $this->_isPopulated = false; + } + + public function setPropertyDetails($node, $property) + { + $this->_node = $node; + $this->_property = $property; + } + + public function __toString() + { + $this->populateContentData(); + return "mimetype=".$this->mimetype."|encoding=".$this->encoding."|size=".$this->size; + } + + public function getNode() + { + return $this->_node; + } + + public function getProperty() + { + return $this->_property; + } + + public function getIsDirty() + { + return $this->_isDirty; + } + + public function getMimetype() + { + $this->populateContentData(); + return $this->_mimetype; + } + + public function setMimetype($mimetype) + { + $this->populateContentData(); + $this->_mimetype = $mimetype; + } + + public function getSize() + { + $this->populateContentData(); + return $this->_size; + } + + public function getEncoding() + { + $this->populateContentData(); + return $this->_encoding; + } + + public function setEncoding($encoding) + { + $this->populateContentData(); + $this->_encoding = $encoding; + } + + public function getUrl() + { + // TODO what should be returned if the content has been updated?? + + $this->populateContentData(); + $result = null; + if ($this->_url != null) + { + $result = $this->_url."?ticket=".$this->_node->session->ticket; + } + return $result; + } + + public function getGuestUrl() + { + // TODO what should be returned if the content has been updated?? + + $this->populateContentData(); + $result = null; + if ($this->_url != null) + { + $result = $this->_url."?guest=true"; + } + return $result; + } + + public function getContent() + { + $this->populateContentData(); + + $result = null; + if ($this->_isDirty == true) + { + if ($this->_newFileContent != null) + { + $handle = fopen($this->_newFileContent, "rb"); + $result = stream_get_contents($handle); + fclose($handle); + } + else if ($this->_newContent != null) + { + $result = $this->_newContent; + } + } + else + { + if ($this->getUrl() != null) + { + $handle = fopen($this->getUrl(), "rb"); + $result = stream_get_contents($handle); + fclose($handle); + } + } + return $result; + } + + public function setContent($content) + { + $this->populateContentData(); + $this->_isDirty = true; + $this->_newContent = $content; + } + + public function writeContentFromFile($fileName) + { + $this->populateContentData(); + $this->_isDirty = true; + $this->_newFileContent = $fileName; + } + + public function readContentToFile($fileName) + { + $handle = fopen($fileName, "wb"); + fwrite($handle, $this->getContent()); + fclose($handle); + } + + public function onBeforeSave(&$statements, $where) + { + if ($this->_isDirty == true) + { + // Check mimetype has been set + if ($this->_mimetype == null) + { + throw new Exception("A mime type for the content property ".$this->_property." on node ".$this->_node->__toString()." must be set"); + } + + // If a file has been specified then read content from there + //$content = null; + if ($this->_newFileContent != null) + { + // Upload the content to the repository + $contentData = upload_file($this->node->session, $this->_newFileContent, $this->_mimetype, $this->_encoding); + + // Set the content property value + $this->addStatement( + $statements, + "update", + array("property" => array( + "name" => $this->property, + "isMultiValue" => false, + "value" => $contentData)) + $where); + } + else + { + // Add the writeContent statement + $this->addStatement( + $statements, + "writeContent", + array( + "property" => $this->_property, + "content" => $this->_newContent, + "format" => array( + "mimetype" => $this->_mimetype, + "encoding" => $this->_encoding)) + + $where); + } + } + } + + public function onAfterSave() + { + $this->_isDirty = false; + $this->_isPopulated = false; + $this->_mimetype = null; + $this->__size = null; + $this->__encoding = null; + $this->__url = null; + $this->__newContent = null; + } + + private function populateContentData() + { + //echo "isPopulated:".$this->_isPopulated."; node:".$this->_node."; property:".$this->_property."
"; + if ($this->_isPopulated == false && $this->_node != null && $this->_property != null && $this->_node->isNewNode == false) + { + $result = $this->_node->session->contentService->read( array( + "items" => array( + "nodes" => array( + "store" => $this->_node->store->__toArray(), + "uuid" => $this->_node->id)), + "property" => $this->_property) ); + if (isset($result->content) == true) + { + if (isset($result->content->length) == true) + { + $this->_size = $result->content->length; + } + if (isset($result->content->format->mimetype) == true) + { + $this->_mimetype = $result->content->format->mimetype; + } + if (isset($result->content->format->encoding) == true) + { + $this->_encoding = $result->content->format->encoding; + } + if (isset($result->content->url) == true) + { + $this->_url = $result->content->url; + } + } + + $this->_isPopulated = true; + } + } + + private function addStatement(&$statements, $statement, $body) + { + $result = array(); + if (array_key_exists($statement, $statements) == true) + { + $result = $statements[$statement]; + } + $result[] = $body; + $statements[$statement] = $result; + } +} +?> diff --git a/lib/alfresco/Service/Functions.php b/lib/alfresco/Service/Functions.php index 84a014feee..fdc9b46188 100755 --- a/lib/alfresco/Service/Functions.php +++ b/lib/alfresco/Service/Functions.php @@ -1,114 +1,115 @@ -repository->host; - $port = $session->repository->port; - - // Get the IP address for the target host - $address = gethostbyname($host); - - // Create a TCP/IP socket - $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - if ($socket === false) - { - throw new Exception ("socket_create() failed: reason: " . socket_strerror(socket_last_error())); - } - - // Connect the socket to the repository - $result = socket_connect($socket, $address, $port); - if ($result === false) - { - throw new Exception("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket))); - } - - // Write the request header onto the socket - $url = "/alfresco/upload/".urlencode($fileName)."?ticket=".$session->ticket; - if ($mimetype != null) - { - // Add mimetype if specified - $url .= "&mimetype=".$mimetype; - } - if ($encoding != null) - { - // Add encoding if specified - $url .= "&encoding=".$encoding; - } - $in = "PUT ".$url." HTTP/1.1\r\n". - "Content-Length: ".$fileSize."\r\n". - "Host: ".$address.":".$port."\r\n". - "Connection: Keep-Alive\r\n". - "\r\n"; - socket_write($socket, $in, strlen($in)); - - // Write the content found in the file onto the socket - $handle = fopen($filePath, "r"); - while (feof($handle) == false) - { - $content = fread($handle, 1024); - socket_write($socket, $content, strlen($content)); - } - fclose($handle); - - // Read the response - $recv = socket_read ($socket, 2048, PHP_BINARY_READ); - $index = strpos($recv, "contentUrl"); - if ($index !== false) - { - $result = substr($recv, $index); - } - - // Close the socket - socket_close($socket); - - return $result; - } -?> +libdir."/alfresco/Service/Repository.php"); + require_once($CFG->libdir."/alfresco/Service/Session.php"); + + /** + * Uploads a file into content store and returns the content data string which + * can be used to populate a content property. + * + * @param $session the session + * @param $filePath the file location + * @return String the content data that can be used to update the content property + */ + function upload_file($session, $filePath, $mimetype=null, $encoding=null) + { + $result = null; + + // Check for the existance of the file + if (file_exists($filePath) == false) + { + throw new Exception("The file ".$filePath."does no exist."); + } + + // Get the file name and size + $fileName = basename($filePath); + $fileSize = filesize($filePath); + + // Get the address and the + $host = $session->repository->host; + $port = $session->repository->port; + + // Get the IP address for the target host + $address = gethostbyname($host); + + // Create a TCP/IP socket + $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + if ($socket === false) + { + throw new Exception ("socket_create() failed: reason: " . socket_strerror(socket_last_error())); + } + + // Connect the socket to the repository + $result = socket_connect($socket, $address, $port); + if ($result === false) + { + throw new Exception("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket))); + } + + // Write the request header onto the socket + $url = "/alfresco/upload/".urlencode($fileName)."?ticket=".$session->ticket; + if ($mimetype != null) + { + // Add mimetype if specified + $url .= "&mimetype=".$mimetype; + } + if ($encoding != null) + { + // Add encoding if specified + $url .= "&encoding=".$encoding; + } + $in = "PUT ".$url." HTTP/1.1\r\n". + "Content-Length: ".$fileSize."\r\n". + "Host: ".$address.":".$port."\r\n". + "Connection: Keep-Alive\r\n". + "\r\n"; + socket_write($socket, $in, strlen($in)); + + // Write the content found in the file onto the socket + $handle = fopen($filePath, "r"); + while (feof($handle) == false) + { + $content = fread($handle, 1024); + socket_write($socket, $content, strlen($content)); + } + fclose($handle); + + // Read the response + $recv = socket_read ($socket, 2048, PHP_BINARY_READ); + $index = strpos($recv, "contentUrl"); + if ($index !== false) + { + $result = substr($recv, $index); + } + + // Close the socket + socket_close($socket); + + return $result; + } +?> diff --git a/lib/alfresco/Service/Logger/Logger.php b/lib/alfresco/Service/Logger/Logger.php index f16bac1a8c..6d2645080f 100755 --- a/lib/alfresco/Service/Logger/Logger.php +++ b/lib/alfresco/Service/Logger/Logger.php @@ -1,90 +1,90 @@ -componentName = $componentName; - } - - public function isDebugEnabled() - { - return $this->isEnabled(DEBUG); - } - - public function debug($message) - { - $this->write(DEBUG, $message); - } - - public function isWarningEnabled() - { - return $this->isEnabled(WARNING); - } - - public function warning($message) - { - $this->write(WARNING, $message); - } - - public function isInformationEnabled() - { - return $this->isEnabled(INFORMATION); - } - - public function information($message) - { - $this->write(INFORMATION, $message); - } - - private function isEnabled($logLevel) - { - global $componentLogLevels, $defaultLogLevel; - - $logLevels = $defaultLogLevel; - if ($this->componentName != null && isset($componentLogLevels[$this->componentName]) == true) - { - $logLevels = $componentLogLevels[$this->componentName]; - } - - return in_array($logLevel, $logLevels); - } - - private function write($logLevel, $message) - { - global $logFile; - - $handle = fopen($logFile, "a"); - fwrite($handle, $logLevel." ".date("G:i:s d/m/Y")." - ".$message."\r\n"); - fclose($handle); - } - } -?> +componentName = $componentName; + } + + public function isDebugEnabled() + { + return $this->isEnabled(DEBUG); + } + + public function debug($message) + { + $this->write(DEBUG, $message); + } + + public function isWarningEnabled() + { + return $this->isEnabled(WARNING); + } + + public function warning($message) + { + $this->write(WARNING, $message); + } + + public function isInformationEnabled() + { + return $this->isEnabled(INFORMATION); + } + + public function information($message) + { + $this->write(INFORMATION, $message); + } + + private function isEnabled($logLevel) + { + global $componentLogLevels, $defaultLogLevel; + + $logLevels = $defaultLogLevel; + if ($this->componentName != null && isset($componentLogLevels[$this->componentName]) == true) + { + $logLevels = $componentLogLevels[$this->componentName]; + } + + return in_array($logLevel, $logLevels); + } + + private function write($logLevel, $message) + { + global $logFile; + + $handle = fopen($logFile, "a"); + fwrite($handle, $logLevel." ".date("G:i:s d/m/Y")." - ".$message."\r\n"); + fclose($handle); + } + } +?> diff --git a/lib/alfresco/Service/Logger/LoggerConfig.php b/lib/alfresco/Service/Logger/LoggerConfig.php index 566ea5b18a..4227098da0 100755 --- a/lib/alfresco/Service/Logger/LoggerConfig.php +++ b/lib/alfresco/Service/Logger/LoggerConfig.php @@ -1,44 +1,44 @@ - $debugLevel - ); - - -?> + $debugLevel + ); + + +?> diff --git a/lib/alfresco/Service/Node.php b/lib/alfresco/Service/Node.php index 4b6de64159..4508fe6ea2 100755 --- a/lib/alfresco/Service/Node.php +++ b/lib/alfresco/Service/Node.php @@ -92,12 +92,12 @@ class Node extends BaseObject // Set the property values foreach ($properties as $name=>$value) { - $name = $this->expandToFullName($name); + $name = $this->_session->namespaceMap->getFullName($name); $this->_properties[$name] = $value; } } - public function setContent($property, $mimetype=null, $encoding=null, $content=null) + public function updateContent($property, $mimetype, $encoding="UTF-8", $content=null) { list($property) = $this->_session->namespaceMap->getFullNames(array($property)); $contentData = new ContentData($this, $property, $mimetype, $encoding); diff --git a/lib/alfresco/Service/Repository.php b/lib/alfresco/Service/Repository.php index edc9fe29b2..6d1796728b 100755 --- a/lib/alfresco/Service/Repository.php +++ b/lib/alfresco/Service/Repository.php @@ -1,133 +1,139 @@ -_connectionUrl = $connectionUrl; - $parts = parse_url($connectionUrl); - $this->_host = $parts["host"]; - $this->_port = $parts["port"]; - } - - public function getConnectionUrl() - { - return $this->_connectionUrl; - } - - public function getHost() - { - return $this->_host; - } - - public function getPort() - { - return $this->_port; - } - - public function authenticate($userName, $password) - { - // TODO need to handle exceptions! - - $authenticationService = WebServiceFactory::getAuthenticationService($this->_connectionUrl); - $result = $authenticationService->startSession(array ( - "username" => $userName, - "password" => $password - )); - - // Get the ticket and sessionId - $ticket = $result->startSessionReturn->ticket; - $sessionId = $result->startSessionReturn->sessionid; - - // Store the session id for later use - if ($sessionId != null) - { - $sessionIds = null; - if (isset($_SESSION["sessionIds"]) == false) - { - $sessionIds = array(); - } - else - { - $sessionIds = $_SESSION["sessionIds"]; - } - $sessionIds[$ticket] = $sessionId; - $_SESSION["sessionIds"] = $sessionIds; - } - - return $ticket; - } - - public function createSession($ticket=null) - { - $session = null; - - if ($ticket == null) - { - // TODO get ticket from some well known location ie: the $_SESSION - } - else - { - // TODO would be nice to be able to check that the ticket is still valid! - - // Create new session - $session = new Session($this, $ticket); - } - - return $session; - } - - /** - * For a given ticket, returns the realated session id, null if one can not be found. - */ - public static function getSessionId($ticket) - { - $result = null; - if (isset($_SESSION["sessionIds"]) == true) - { - $result = $_SESSION["sessionIds"][$ticket]; - } - return $result; - - } - -} - - ?> +libdir.'/alfresco/Service/WebService/WebServiceFactory.php'; +require_once $CFG->libdir.'/alfresco/Service/BaseObject.php'; + +if (isset($_SESSION) == false) +{ + // Start the session + session_start(); +} + +// change by moodle +class Alfresco_Repository extends BaseObject +{ + private $_connectionUrl; + private $_host; + private $_port; + + public function __construct($connectionUrl="http://localhost:8080/alfresco/api") + { + $this->_connectionUrl = $connectionUrl; + $parts = parse_url($connectionUrl); + $this->_host = $parts["host"]; + if (empty($parts["port"])) { + $this->_port = 80; + } else { + $this->_port = $parts["port"]; + } + } + + public function getConnectionUrl() + { + return $this->_connectionUrl; + } + + public function getHost() + { + return $this->_host; + } + + public function getPort() + { + return $this->_port; + } + + public function authenticate($userName, $password) + { + // TODO need to handle exceptions! + + $authenticationService = WebServiceFactory::getAuthenticationService($this->_connectionUrl); + $result = $authenticationService->startSession(array ( + "username" => $userName, + "password" => $password + )); + + // Get the ticket and sessionId + $ticket = $result->startSessionReturn->ticket; + $sessionId = $result->startSessionReturn->sessionid; + + // Store the session id for later use + if ($sessionId != null) + { + $sessionIds = null; + if (isset($_SESSION["sessionIds"]) == false) + { + $sessionIds = array(); + } + else + { + $sessionIds = $_SESSION["sessionIds"]; + } + $sessionIds[$ticket] = $sessionId; + $_SESSION["sessionIds"] = $sessionIds; + } + + return $ticket; + } + + public function createSession($ticket=null) + { + $session = null; + + if ($ticket == null) + { + // TODO get ticket from some well known location ie: the $_SESSION + } + else + { + // TODO would be nice to be able to check that the ticket is still valid! + + // Create new session + $session = new Session($this, $ticket); + } + + return $session; + } + + /** + * For a given ticket, returns the realated session id, null if one can not be found. + */ + public static function getSessionId($ticket) + { + $result = null; + if (isset($_SESSION["sessionIds"]) == true) + { + $result = $_SESSION["sessionIds"][$ticket]; + } + return $result; + + } + +} + + ?> diff --git a/lib/alfresco/Service/Session.php b/lib/alfresco/Service/Session.php index 4baf57c1d8..175a3d3e18 100755 --- a/lib/alfresco/Service/Session.php +++ b/lib/alfresco/Service/Session.php @@ -122,7 +122,7 @@ class Session extends BaseObject public function getNodeFromString($value) { // TODO - throw Exception("getNode($value) no yet implemented"); + throw new Exception("getNode($value) not yet implemented"); } /** @@ -274,4 +274,4 @@ class Session extends BaseObject return $sessionId; } } -?> \ No newline at end of file +?> diff --git a/lib/alfresco/Service/WebService/AlfrescoWebService.php b/lib/alfresco/Service/WebService/AlfrescoWebService.php index e79cae1b20..8f2edf5d2e 100755 --- a/lib/alfresco/Service/WebService/AlfrescoWebService.php +++ b/lib/alfresco/Service/WebService/AlfrescoWebService.php @@ -1,117 +1,118 @@ - true, 'exceptions' => true), $ticket = null) - { - // Store the current ticket - $this->ticket = $ticket; - - // Call the base class - parent::__construct($wsdl, $options); - } - - public function __call($function_name, $arguments=array()) - { - return $this->__soapCall($function_name, $arguments); - } - - public function __soapCall($function_name, $arguments=array(), $options=array(), $input_headers=array(), $output_headers=array()) - { - if (isset($this->ticket)) - { - // Automatically add a security header - $input_headers[] = new SoapHeader($this->securityExtNS, "Security", null, 1); - - // Set the JSESSION cookie value - $sessionId = Al_Repository::getSessionId($this->ticket); // Moodle - if ($sessionId != null) - { - $this->__setCookie("JSESSIONID", $sessionId); - } - } - - return parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers); - } - - public function __doRequest($request, $location, $action, $version) - { - // If this request requires authentication we have to manually construct the - // security headers. - if (isset($this->ticket)) - { - $dom = new DOMDocument("1.0"); - $dom->loadXML($request); - - $securityHeader = $dom->getElementsByTagName("Security"); - - if ($securityHeader->length != 1) - { - throw new Exception("Expected length: 1, Received: " . $securityHeader->length . ". No Security Header, or more than one element called Security!"); - } - - $securityHeader = $securityHeader->item(0); - - // Construct Timestamp Header - $timeStamp = $dom->createElementNS($this->wsUtilityNS, "Timestamp"); - $createdDate = date("Y-m-d\TH:i:s\Z", mktime(date("H")+24, date("i"), date("s"), date("m"), date("d"), date("Y"))); - $expiresDate = date("Y-m-d\TH:i:s\Z", mktime(date("H")+25, date("i"), date("s"), date("m"), date("d"), date("Y"))); - $created = new DOMElement("Created", $createdDate, $this->wsUtilityNS); - $expires = new DOMElement("Expires", $expiresDate, $this->wsUtilityNS); - $timeStamp->appendChild($created); - $timeStamp->appendChild($expires); - - // Construct UsernameToken Header - $userNameToken = $dom->createElementNS($this->securityExtNS, "UsernameToken"); - $userName = new DOMElement("Username", "username", $this->securityExtNS); - $passWord = $dom->createElementNS($this->securityExtNS, "Password"); - $typeAttr = new DOMAttr("Type", $this->passwordType); - $passWord->appendChild($typeAttr); - $passWord->appendChild($dom->createTextNode($this->ticket)); - $userNameToken->appendChild($userName); - $userNameToken->appendChild($passWord); - - // Construct Security Header - $securityHeader->appendChild($timeStamp); - $securityHeader->appendChild($userNameToken); - - // Save the XML Request - $request = $dom->saveXML(); - } - - return parent::__doRequest($request, $location, $action, $version); - } -} - -?> + true, 'exceptions' => true), $ticket = null) + { + // Store the current ticket + $this->ticket = $ticket; + + // Call the base class + parent::__construct($wsdl, $options); + } + + public function __call($function_name, $arguments=array()) + { + return $this->__soapCall($function_name, $arguments); + } + + public function __soapCall($function_name, $arguments=array(), $options=array(), $input_headers=array(), $output_headers=array()) + { + if (isset($this->ticket)) + { + // Automatically add a security header + $input_headers[] = new SoapHeader($this->securityExtNS, "Security", null, 1); + + // Set the JSESSION cookie value + // change by moodle + $sessionId = Alfresco_Repository::getSessionId($this->ticket); + if ($sessionId != null) + { + $this->__setCookie("JSESSIONID", $sessionId); + } + } + + return parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers); + } + + public function __doRequest($request, $location, $action, $version) + { + // If this request requires authentication we have to manually construct the + // security headers. + if (isset($this->ticket)) + { + $dom = new DOMDocument("1.0"); + $dom->loadXML($request); + + $securityHeader = $dom->getElementsByTagName("Security"); + + if ($securityHeader->length != 1) + { + throw new Exception("Expected length: 1, Received: " . $securityHeader->length . ". No Security Header, or more than one element called Security!"); + } + + $securityHeader = $securityHeader->item(0); + + // Construct Timestamp Header + $timeStamp = $dom->createElementNS($this->wsUtilityNS, "Timestamp"); + $createdDate = date("Y-m-d\TH:i:s\Z", mktime(date("H")+24, date("i"), date("s"), date("m"), date("d"), date("Y"))); + $expiresDate = date("Y-m-d\TH:i:s\Z", mktime(date("H")+25, date("i"), date("s"), date("m"), date("d"), date("Y"))); + $created = new DOMElement("Created", $createdDate, $this->wsUtilityNS); + $expires = new DOMElement("Expires", $expiresDate, $this->wsUtilityNS); + $timeStamp->appendChild($created); + $timeStamp->appendChild($expires); + + // Construct UsernameToken Header + $userNameToken = $dom->createElementNS($this->securityExtNS, "UsernameToken"); + $userName = new DOMElement("Username", "username", $this->securityExtNS); + $passWord = $dom->createElementNS($this->securityExtNS, "Password"); + $typeAttr = new DOMAttr("Type", $this->passwordType); + $passWord->appendChild($typeAttr); + $passWord->appendChild($dom->createTextNode($this->ticket)); + $userNameToken->appendChild($userName); + $userNameToken->appendChild($passWord); + + // Construct Security Header + $securityHeader->appendChild($timeStamp); + $securityHeader->appendChild($userNameToken); + + // Save the XML Request + $request = $dom->saveXML(); + } + + return parent::__doRequest($request, $location, $action, $version); + } +} + +?> diff --git a/lib/alfresco/Service/WebService/WebServiceFactory.php b/lib/alfresco/Service/WebService/WebServiceFactory.php index acc6aa96d9..b312b6a1cc 100755 --- a/lib/alfresco/Service/WebService/WebServiceFactory.php +++ b/lib/alfresco/Service/WebService/WebServiceFactory.php @@ -1,63 +1,64 @@ - \ No newline at end of file +libdir.'/alfresco/Service/WebService/AlfrescoWebService.php'); + +class WebServiceFactory +{ + public static function getAuthenticationService($path) + { + $path .= '/AuthenticationService?wsdl'; + return new AlfrescoWebService($path, array('location'=>$path)); + } + + public static function getRepositoryService($path, $ticket) + { + $path .= '/RepositoryService?wsdl'; + return new AlfrescoWebService($path, array('location'=>$path), $ticket); + } + + public static function getContentService($path, $ticket) + { + $path .= '/ContentService?wsdl'; + return new AlfrescoWebService($path, array('location'=>$path), $ticket); + } + + public static function getAdministrationService($path, $ticket) + { + $path .= '/AdministrationService?wsdl'; + return new AlfrescoWebService($path, array('location'=>$path), $ticket); + } + + public static function getAuthoringService($path, $ticket) + { + $path .= '/AuthoringService?wsdl'; + return new AlfrescoWebService($path, array('location'=>$path), $ticket); + } +} + +?> diff --git a/repository/alfresco/repository.class.php b/repository/alfresco/repository.class.php index 88b957c40f..82908b5bb2 100755 --- a/repository/alfresco/repository.class.php +++ b/repository/alfresco/repository.class.php @@ -5,35 +5,38 @@ * @version $Id$ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ -require_once($CFG->libdir . '/soaplib.php'); class repository_alfresco extends repository { - private $repo = null; + private $instance = null; private $ticket = null; - private $sess = null; + private $user_session = null; private $store = null; public function __construct($repositoryid, $context = SITEID, $options = array()) { global $SESSION, $CFG; - parent::__construct ($repositoryid, $context, $options); + parent::__construct($repositoryid, $context, $options); + $this->sessname = 'alfresco_ticket_'.$this->id; if (class_exists('SoapClient')) { require_once($CFG->libdir . '/alfresco/Service/Repository.php'); require_once($CFG->libdir . '/alfresco/Service/Session.php'); require_once($CFG->libdir . '/alfresco/Service/SpacesStore.php'); require_once($CFG->libdir . '/alfresco/Service/Node.php'); - $this->repo = new Al_Repository($this->alfresco_url); - $this->sess_name = 'alfresco_ticket_'.$this->id; + // setup alfresco instance + $this->instance = new Alfresco_Repository($this->options['alfresco_url']); $this->username = optional_param('al_username', '', PARAM_RAW); $this->password = optional_param('al_password', '', PARAM_RAW); try{ - if ( empty($SESSION->{$this->sess_name}) && !empty($this->username) && !empty($this->password)) { - $this->ticket = $this->repo->authenticate($this->username, $this->password); - $SESSION->{$this->sess_name} = $this->ticket; + // deal with user logging in + if (empty($SESSION->{$this->sessname}) && !empty($this->username) && !empty($this->password)) { + $this->ticket = $this->instance->authenticate($this->username, $this->password); + $SESSION->{$this->sessname} = $this->ticket; } else { - $this->ticket = $SESSION->{$this->sess_name}; + if (!empty($SESSION->{$this->sessname})) { + $this->ticket = $SESSION->{$this->sessname}; + } } - $this->sess = $this->repo->createSession($this->ticket); - $this->store = new SpacesStore($this->sess); + $this->user_session = $this->instance->createSession($this->ticket); + $this->store = new SpacesStore($this->user_session); } catch (Exception $e) { $this->logout(); } @@ -62,13 +65,13 @@ class repository_alfresco extends repository { public function logout() { global $SESSION; - unset($SESSION->{$this->sess_name}); + unset($SESSION->{$this->sessname}); return $this->print_login(); } public function check_login() { global $SESSION; - return !empty($SESSION->{$this->sess_name}); + return !empty($SESSION->{$this->sessname}); } private function get_url($node) { @@ -93,7 +96,7 @@ class repository_alfresco extends repository { $ret = array(); $ret['dynload'] = true; $ret['list'] = array(); - $url = $this->alfresco_url; + $url = $this->options['alfresco_url']; $pattern = '#^(.*)api#'; preg_match($pattern, $url, $matches); $ret['manage'] = $matches[1].'faces/jsp/dashboards/container.jsp'; @@ -103,7 +106,7 @@ class repository_alfresco extends repository { if (empty($uuid)) { $this->current_node = $this->store->companyHome; } else { - $this->current_node = $this->sess->getNode($this->store, $uuid); + $this->current_node = $this->user_session->getNode($this->store, $uuid); } $folder_filter = "{http://www.alfresco.org/model/content/1.0}folder"; $file_filter = "{http://www.alfresco.org/model/content/1.0}content"; @@ -113,7 +116,7 @@ class repository_alfresco extends repository { { $ret['list'][] = array('title'=>$child->child->cm_name, 'path'=>$child->child->id, - 'thumbnail'=>$CFG->httpswwwroot.'/pix/f/folder.gif', + 'thumbnail'=>$CFG->httpswwwroot.'/pix/f/folder-32.png', 'children'=>array()); } elseif ($child->child->type == $file_filter) { $ret['list'][] = array('title'=>$child->child->cm_name, @@ -126,7 +129,7 @@ class repository_alfresco extends repository { public function get_file($uuid, $file = '') { global $CFG; - $node = $this->sess->getNode($this->store, $uuid); + $node = $this->user_session->getNode($this->store, $uuid); $url = $this->get_url($node); $path = $this->prepare_file($file); $fp = fopen($path, 'w'); @@ -138,7 +141,7 @@ class repository_alfresco extends repository { public function print_search($client_id) { $str = parent::print_search($client_id); $str .= '