/lib/alfresco/Service/Functions.php

https://github.com/nigeldaley/moodle · PHP · 115 lines · 57 code · 13 blank · 45 comment · 11 complexity · ae68bdaf12d1666326ea57811ccb2108 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (C) 2005 Alfresco, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * As a special exception to the terms and conditions of version 2.0 of
  17. * the GPL, you may redistribute this Program in connection with Free/Libre
  18. * and Open Source Software ("FLOSS") applications as described in Alfresco's
  19. * FLOSS exception. You should have recieved a copy of the text describing
  20. * the FLOSS exception, and it is also available here:
  21. * http://www.alfresco.com/legal/licensing"
  22. */
  23. // change by moodle
  24. require_once($CFG->libdir."/alfresco/Service/Repository.php");
  25. require_once($CFG->libdir."/alfresco/Service/Session.php");
  26. /**
  27. * Uploads a file into content store and returns the content data string which
  28. * can be used to populate a content property.
  29. *
  30. * @param $session the session
  31. * @param $filePath the file location
  32. * @return String the content data that can be used to update the content property
  33. */
  34. function upload_file($session, $filePath, $mimetype=null, $encoding=null)
  35. {
  36. $result = null;
  37. // Check for the existance of the file
  38. if (file_exists($filePath) == false)
  39. {
  40. throw new Exception("The file ".$filePath."does no exist.");
  41. }
  42. // Get the file name and size
  43. $fileName = basename($filePath);
  44. $fileSize = filesize($filePath);
  45. // Get the address and the
  46. $host = $session->repository->host;
  47. $port = $session->repository->port;
  48. // Get the IP address for the target host
  49. $address = gethostbyname($host);
  50. // Create a TCP/IP socket
  51. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  52. if ($socket === false)
  53. {
  54. throw new Exception ("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
  55. }
  56. // Connect the socket to the repository
  57. $result = socket_connect($socket, $address, $port);
  58. if ($result === false)
  59. {
  60. throw new Exception("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)));
  61. }
  62. // Write the request header onto the socket
  63. $url = "/alfresco/upload/".urlencode($fileName)."?ticket=".$session->ticket;
  64. if ($mimetype != null)
  65. {
  66. // Add mimetype if specified
  67. $url .= "&mimetype=".$mimetype;
  68. }
  69. if ($encoding != null)
  70. {
  71. // Add encoding if specified
  72. $url .= "&encoding=".$encoding;
  73. }
  74. $in = "PUT ".$url." HTTP/1.1\r\n".
  75. "Content-Length: ".$fileSize."\r\n".
  76. "Host: ".$address.":".$port."\r\n".
  77. "Connection: Keep-Alive\r\n".
  78. "\r\n";
  79. socket_write($socket, $in, strlen($in));
  80. // Write the content found in the file onto the socket
  81. $handle = fopen($filePath, "r");
  82. while (feof($handle) == false)
  83. {
  84. $content = fread($handle, 1024);
  85. socket_write($socket, $content, strlen($content));
  86. }
  87. fclose($handle);
  88. // Read the response
  89. $recv = socket_read ($socket, 2048, PHP_BINARY_READ);
  90. $index = strpos($recv, "contentUrl");
  91. if ($index !== false)
  92. {
  93. $result = substr($recv, $index);
  94. }
  95. // Close the socket
  96. socket_close($socket);
  97. return $result;
  98. }
  99. ?>