|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 * Copyright (C) 2005 Alfresco, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 00010 * This program 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 this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 00019 * As a special exception to the terms and conditions of version 2.0 of 00020 * the GPL, you may redistribute this Program in connection with Free/Libre 00021 * and Open Source Software ("FLOSS") applications as described in Alfresco's 00022 * FLOSS exception. You should have recieved a copy of the text describing 00023 * the FLOSS exception, and it is also available here: 00024 * http://www.alfresco.com/legal/licensing" 00025 */ 00026 00027 // change by moodle 00028 require_once($CFG->libdir."/alfresco/Service/Repository.php"); 00029 require_once($CFG->libdir."/alfresco/Service/Session.php"); 00030 00039 function upload_file($session, $filePath, $mimetype=null, $encoding=null) 00040 { 00041 $result = null; 00042 00043 // Check for the existance of the file 00044 if (file_exists($filePath) == false) 00045 { 00046 throw new Exception("The file ".$filePath."does no exist."); 00047 } 00048 00049 // Get the file name and size 00050 $fileName = basename($filePath); 00051 $fileSize = filesize($filePath); 00052 00053 // Get the address and the 00054 $host = $session->repository->host; 00055 $port = $session->repository->port; 00056 00057 // Get the IP address for the target host 00058 $address = gethostbyname($host); 00059 00060 // Create a TCP/IP socket 00061 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 00062 if ($socket === false) 00063 { 00064 throw new Exception ("socket_create() failed: reason: " . socket_strerror(socket_last_error())); 00065 } 00066 00067 // Connect the socket to the repository 00068 $result = socket_connect($socket, $address, $port); 00069 if ($result === false) 00070 { 00071 throw new Exception("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket))); 00072 } 00073 00074 // Write the request header onto the socket 00075 $url = "/alfresco/upload/".urlencode($fileName)."?ticket=".$session->ticket; 00076 if ($mimetype != null) 00077 { 00078 // Add mimetype if specified 00079 $url .= "&mimetype=".$mimetype; 00080 } 00081 if ($encoding != null) 00082 { 00083 // Add encoding if specified 00084 $url .= "&encoding=".$encoding; 00085 } 00086 $in = "PUT ".$url." HTTP/1.1\r\n". 00087 "Content-Length: ".$fileSize."\r\n". 00088 "Host: ".$address.":".$port."\r\n". 00089 "Connection: Keep-Alive\r\n". 00090 "\r\n"; 00091 socket_write($socket, $in, strlen($in)); 00092 00093 // Write the content found in the file onto the socket 00094 $handle = fopen($filePath, "r"); 00095 while (feof($handle) == false) 00096 { 00097 $content = fread($handle, 1024); 00098 socket_write($socket, $content, strlen($content)); 00099 } 00100 fclose($handle); 00101 00102 // Read the response 00103 $recv = socket_read ($socket, 2048, PHP_BINARY_READ); 00104 $index = strpos($recv, "contentUrl"); 00105 if ($index !== false) 00106 { 00107 $result = substr($recv, $index); 00108 } 00109 00110 // Close the socket 00111 socket_close($socket); 00112 00113 return $result; 00114 } 00115 ?>