aboutsummaryrefslogtreecommitdiff
path: root/modules/video_zencoder/includes/Zencoder.php
blob: 1017ee92265288bd5c49e181bdd69060720ca65a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php

/*

  Zencoder API PHP Library
  Version: 1.0
  See the README file for info on how to use this library.

 */
define('ZENCODER_LIBRARY_NAME', "ZencoderPHP");
define('ZENCODER_LIBRARY_VERSION', "1.0");

// Add JSON functions for PHP < 5.2.0
if (!function_exists('json_encode')) {
  require_once('lib/JSON.php');
  $GLOBALS['JSON_OBJECT'] = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);

  function json_encode($value) {
    return $GLOBALS['JSON_OBJECT']->encode($value);
  }

  function json_decode($value) {
    return $GLOBALS['JSON_OBJECT']->decode($value);
  }

}

class ZencoderJob {

  var $new_job_url = "https://app.zencoder.com/api/jobs"; //https://app.zencoder.com/api/jobs
  //https://zencoder-staging.heroku.com/api/jobs
  var $new_job_params = array();
  var $new_job_json;
  var $created = false;
  var $errors = array();
  // Attributes
  var $id;
  var $outputs = array();

  // Initialize
  function ZencoderJob($params, $options = array()) {

    // Build using params if not sending request
    if ($options["build"]) {
      $this->update_attributes($params);
      return true;
    }

    $this->new_job_params = $params;
    $this->created = $this->create();
  }

  // Send Job Request to API
  function create() {
    // Send request
    $request = new ZencoderRequest($this->new_job_url, false, $this->new_job_params);

    if ($request->successful) {
      $this->update_attributes($request->results);
      return true;
    } else {
      $this->errors = array_merge($this->errors, $request->errors);
      return false;
    }
  }

  // Add/Update attributes on the job object.
  function update_attributes($attributes = array()) {
    foreach ($attributes as $attr_name => $attr_value) {
      // Create output file objects
      if ($attr_name == "outputs" && is_array($attr_value)) {
        $this->create_outputs($attr_value);
      } elseif (!function_exists($this->$attr_name)) {
        $this->$attr_name = $attr_value;
      }
    }
  }

  // Create output file objects from returned parameters.
  // Use the Label for the key if avaiable.
  function create_outputs($outputs = array()) {
    foreach ($outputs as $output_attrs) {
      if ($output_attrs["label"]) {
        $this->outputs[$output_attrs["label"]] = new ZencoderOutputFile($output_attrs);
      } else {
        $this->outputs[] = new ZencoderOutputFile($output_attrs);
      }
    }
  }

}

class ZencoderOutputFile {

  var $id;
  var $label;
  var $url;
  var $state;
  var $error_message;
  var $error_link;

  function ZencoderOutputFile($attributes = array()) {
    $this->update_attributes($attributes);
  }

  // Add/Update attributes on the file object.
  function update_attributes($attributes = array()) {
    foreach ($attributes as $attr_name => $attr_value) {
      if (!function_exists($this->$attr_name)) {
        $this->$attr_name = $attr_value;
      }
    }
  }

}

// General API request class
class ZencoderRequest {

  var $successful = false;
  var $errors = array();
  var $raw_results;
  var $results;

  function ZencoderRequest($url, $api_key = "", $params = "") {

    // Add api_key to url if supplied
    if ($api_key) {
      $url .= "?api_key=" . $api_key;
    }

    // Get JSON
    if (is_string($params)) {
      $json = trim($params);
    } else if (is_array($params)) {
      $json = json_encode($params);
    } else {
      $json = false;
    }

    // Create request
    $request = new ZencoderCURL($url, $json);

    // Check for connection errors
    if ($request->connected == false) {
      $this->errors[] = $request->error;
      return;
    }

    $status_code = intval($request->status_code);
    $this->raw_results = $request->results;

    // Parse returned JSON
    $this->results = json_decode($this->raw_results, true);

    // Return based on HTTP status code
    if ($status_code >= 200 && $status_code <= 206) {
      $this->successful = true;
    } else {
      // Get job request errors if any
      if (is_array($this->results["errors"])) {
        foreach ($this->results["errors"] as $error) {
          $this->errors[] = $error;
        }
      } else {
        $this->errors[] = "Unknown Error\n\nHTTP Status Code: " . $request->status_code . "\n" . "Raw Results: \n" . $request->raw_results;
      }
    }
  }

}

// ZencoderCURL
// The connection class to perform the actual request to the surver
// using cURL http://php.net/manual/en/book.curl.php
class ZencoderCURL {

  var $options = array(
    CURLOPT_RETURNTRANSFER => 1, // Return content of the url
    CURLOPT_HEADER => 0, // Don't return the header in result
    CURLOPT_HTTPHEADER => array("Content-Type: application/json", "Accept: application/json"),
    CURLOPT_CONNECTTIMEOUT => 0, // Time in seconds to timeout send request. 0 is no timeout.
    CURLOPT_FOLLOWLOCATION => 1, // Follow redirects.
  );
  var $connected;
  var $results;
  var $status_code;
  var $error;

  // Initialize
  function ZencoderCURL($url, $json, $options = array()) {

    // Add library details to request
    $this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Name: " . ZENCODER_LIBRARY_NAME;
    $this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Version: " . ZENCODER_LIBRARY_VERSION;

    // If posting data
    if ($json) {
      $this->options[CURLOPT_POST] = 1;
      $this->options[CURLOPT_POSTFIELDS] = $json;
    }

    // Add cURL options to defaults (can't use array_merge)
    foreach ($options as $option_key => $option) {
      $this->options[$option_key] = $option;
    }

    // Initialize session
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // Set transfer options
    curl_setopt_array($ch, $this->options);

    // Execute session and store returned results
    $this->results = curl_exec($ch);

    // Store the HTTP status code given (201, 404, etc.)
    $this->status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // Check for cURL error
    if (curl_errno($ch)) {
      $this->error = 'cURL connection error (' . curl_errno($ch) . '): ' . htmlspecialchars(curl_error($ch)) . ' <a href="http://www.google.com/search?q=' . urlencode("curl error " . curl_error($ch)) . '">Search</a>';
      $this->connected = false;
    } else {
      $this->connected = true;
    }

    // Close session
    curl_close($ch);
  }

}

// Capture incoming notifications from Zencoder to your app
class ZencoderOutputNotification {

  var $output;
  var $job;

  function ZencoderOutputNotification($params) {
    if ($params["output"])
      $this->output = new ZencoderOutputFile($params["output"]);
    if ($params["job"])
      $this->job = new ZencoderJob($params["job"], array("build" => true));
  }

  function catch_and_parse() {
    $notificiation_data = json_decode(trim(file_get_contents('php://input')), true);
    return new ZencoderOutputNotification($notificiation_data);
  }

}