Exemple #1
0
$customConds = null;
$conditions = array();
if (strlen($filter['error']) !== 0) {
    if ((bool) $filter['error']) {
        $conditions['response_error'] = array('<>', '');
    } else {
        $customConds .= ' AND response_error IS NULL';
    }
}
if (strlen($filter['request']) !== 0) {
    $conditions['request'] = array('like', '%' . $filter['request'] . '%');
}
if (strlen($filter['response']) !== 0) {
    $conditions['response'] = array('like', '%' . $filter['response'] . '%');
}
if (count($conditions) === 0) {
    $conditions = null;
    $customConds = ' WHERE 1 ' . $customConds;
}
$params = $Params['Module']->UserParameters;
$offset = isset($params['offset']) ? (int) $params['offset'] : 0;
$limit = isset($params['limit']) ? (int) $params['limit'] : 20;
$limitations = array('limit' => $limit, 'offset' => $offset);
$tpl = eZTemplate::factory();
$tpl->setVariable('logs', ResponsysLog::fetchList($conditions, $limitations, $customConds));
$tpl->setVariable('filter', $filter);
$tpl->setVariable('offset', $offset);
$tpl->setVariable('limit', $limit);
$tpl->setVariable('total_count', ResponsysLog::countAll($conditions));
$Result['content'] = $tpl->fetch('design:responsys/logs.tpl');
$Result['path'] = array(array('text' => ezpI18n::tr('extension/responsys', 'Responsys logs'), 'url' => false));
Exemple #2
0
 /**
  * Send HTTP request
  * @param string $uri
  * @param array $data
  * @param string $method
  * @param array $headers
  * @return array
  */
 private function sendRequest($uri, array $data = array(), $method = 'POST', array $headers = array())
 {
     $ch = curl_init($uri);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
     $defaultHeders = array('content_type' => 'Content-Type: application/json', 'user_agent' => 'User-Agent: eZ Publish Responsys API');
     $headers = array_merge($defaultHeders, $headers);
     $dataJSON = json_encode($data);
     // Fix double slashes for unicode sequences
     $dataJSON = preg_replace('/\\\\\\\\u([0-9a-fA-F]{4})/u', '\\u$1', $dataJSON);
     switch ($method) {
         case 'POST':
             curl_setopt($ch, CURLOPT_POST, true);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJSON);
             break;
         case 'PUT':
             curl_setopt($curl, CURLOPT_PUT, true);
             break;
     }
     if (count($headers) > 0) {
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     }
     if ($this->ignoreSSL) {
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     }
     $response = curl_exec($ch);
     $info = curl_getinfo($ch);
     $header = trim(substr($response, 0, $info['header_size']));
     $body = trim(substr($response, $info['header_size']));
     $log = new ResponsysLog();
     $log->setAttribute('request_uri', $uri);
     $log->setAttribute('request', self::json_pretty($dataJSON));
     $log->setAttribute('request_headers', implode("\n", $headers));
     $log->setAttribute('response_status', $info['http_code']);
     $log->setAttribute('response_headers', $header);
     $log->setAttribute('response_time', $info['total_time']);
     $log->setAttribute('response', $body);
     if (curl_error($ch)) {
         $log->setAttribute('response_error', curl_error($curl));
     }
     $log->store();
     self::$lastLogItem = $log;
     curl_close($ch);
     $result = json_decode($response, true);
     if ($result === null) {
         $error = 'Invalid response from Responsys.';
         if (strlen($log->attribute('response_error')) === 0) {
             $log->setAttribute('response_error', $error);
             $log->store();
         }
         throw new Exception($error);
     }
     $error = null;
     if (isset($result['errorCode'])) {
         $error = isset($result['detail']) && empty($result['detail']) === false ? $result['detail'] : $result['title'];
     } elseif (isset($result[0]['errorMessage'])) {
         $error = $result[0]['errorMessage'];
     }
     if ($error !== null) {
         if (strlen($log->attribute('response_error')) === 0) {
             $log->setAttribute('response_error', $error);
             $log->store();
         }
         throw new Exception($error);
     }
     return $result;
 }