1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Horde_Service_Facebook_UploadRequest extends Horde_Service_Facebook_Request
12: {
13: 14: 15: 16: 17:
18: protected $_filename;
19:
20: 21: 22: 23: 24: 25: 26: 27:
28: public function __construct(Horde_Service_Facebook $facebook, $method,
29: $file, array $params = array())
30: {
31: parent::__construct($facebook, $method, $params);
32: $this->_filename = $file;
33: }
34:
35: 36: 37: 38: 39:
40: public function run()
41: {
42:
43: $this->_params['format'] = 'json';
44: $result = $this->_multipartHttpTransaction();
45: return $result;
46: }
47:
48: 49: 50: 51: 52: 53: 54: 55: 56:
57: private function _multipartHttpTransaction()
58: {
59:
60:
61: $boundary = '--------------------------FbMuLtIpArT' .
62: sprintf("%010d", mt_rand()) .
63: sprintf("%010d", mt_rand());
64: $content_type = 'multipart/form-data; boundary=' . $boundary;
65:
66: $delimiter = '--' . $boundary;
67: $close_delimiter = $delimiter . '--';
68: $content_lines = array();
69: $this->_finalizeParams($this->_method, $this->_params);
70: foreach ($this->_params as $key => &$val) {
71: $content_lines[] = $delimiter;
72: $content_lines[] = 'Content-Disposition: form-data; name="' . $key . '"';
73: $content_lines[] = '';
74: $content_lines[] = $val;
75: }
76:
77:
78: $content_lines[] = $delimiter;
79: $content_lines[] = 'Content-Disposition: form-data; filename="' . $this->_filename . '"';
80: $content_lines[] = 'Content-Type: application/octet-stream';
81: $content_lines[] = '';
82: $content_lines[] = file_get_contents($this->_filename);
83: $content_lines[] = $close_delimiter;
84: $content_lines[] = '';
85: $content = implode("\r\n", $content_lines);
86: try {
87: $result = $this->_http->request('POST',
88: Horde_Service_Facebook::REST_SERVER_ADDR,
89: $content,
90: array('Content-Type' => $content_type,
91: 'Content-Length' => strlen($content)));
92: } catch (Exception $e) {
93: throw new Horde_Service_Facebook_Exception(sprintf(Horde_Service_Facebook_Translation::t("Upload failed: %s"), $e->getMessage()));
94: }
95:
96: return $result->getBody();
97: }
98:
99: }