[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/alfresco/Service/ -> Functions.php (source)

   1  <?php
   2  /*

   3   * Copyright (C) 2005-2010 Alfresco Software Limited.

   4   *

   5   * This file is part of Alfresco

   6   *

   7   * Alfresco is free software: you can redistribute it and/or modify

   8   * it under the terms of the GNU Lesser General Public License as published by

   9   * the Free Software Foundation, either version 3 of the License, or

  10   * (at your option) any later version.

  11   *

  12   * Alfresco is distributed in the hope that it will be useful,

  13   * but WITHOUT ANY WARRANTY; without even the implied warranty of

  14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

  15   * GNU Lesser General Public License for more details.

  16   *

  17   * You should have received a copy of the GNU Lesser General Public License

  18   * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.

  19   */
  20   
  21   require_once($CFG->libdir."/alfresco/Service/Repository.php");
  22   require_once($CFG->libdir."/alfresco/Service/Session.php");
  23   
  24   /**

  25    * Uploads a file into content store and returns the content data string which

  26    * can be used to populate a content property.

  27    * 

  28    * @param $session the session

  29    * @param $filePath the file location

  30    * @return String the content data that can be used to update the content property

  31    */
  32   function upload_file($session, $filePath, $mimetype=null, $encoding=null)
  33   {
  34       $result = null;
  35       
  36       // Check for the existance of the file

  37       if (file_exists($filePath) == false)
  38       {
  39           throw new Exception("The file ".$filePath."does no exist.");
  40       }
  41       
  42      // Get the file name and size

  43      $fileName = basename($filePath);
  44      $fileSize = filesize($filePath);
  45            
  46      // Get the address and the        

  47      $host = $session->repository->host; 
  48      $port = $session->repository->port;
  49       
  50      // Get the IP address for the target host

  51      $address = gethostbyname($host);
  52      
  53      // Create a TCP/IP socket

  54      $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  55      if ($socket === false) 
  56      {
  57          throw new Exception ("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
  58      }     
  59      
  60      // Connect the socket to the repository

  61      $result = socket_connect($socket, $address, $port);
  62      if ($result === false) 
  63      {
  64          throw new Exception("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)));
  65      } 
  66      
  67      // Write the request header onto the socket

  68      $url = "/alfresco/upload/".urlencode($fileName)."?ticket=".$session->ticket;
  69      if ($mimetype != null)
  70      {
  71          // Add mimetype if specified

  72          $url .= "&mimetype=".$mimetype;
  73      }
  74      if ($encoding != null)
  75      {
  76          // Add encoding if specified

  77          $url .= "&encoding=".$encoding;
  78      }
  79      $in = "PUT ".$url." HTTP/1.1\r\n".
  80                    "Content-Length: ".$fileSize."\r\n".
  81                    "Host: ".$address.":".$port."\r\n".
  82                    "Connection: Keep-Alive\r\n".
  83                    "\r\n";        
  84      socket_write($socket, $in, strlen($in));
  85      
  86      // Write the content found in the file onto the socket

  87      $handle = fopen($filePath, "r");
  88      while (feof($handle) == false)
  89      {
  90          $content = fread($handle, 1024);
  91          socket_write($socket, $content, strlen($content));        
  92      }        
  93      fclose($handle);
  94      
  95      // Read the response

  96      $recv = socket_read ($socket, 2048, PHP_BINARY_READ);
  97      $index = strpos($recv, "contentUrl");
  98      if ($index !== false)
  99      {
 100          $result = substr($recv, $index);    
 101      }
 102      
 103      // Close the socket

 104      socket_close($socket);
 105          
 106      return $result;
 107   } 
 108  ?>


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1