private static function send_with_drupal($url, $options, $ignore_error) { ShareaholicUtilities::log($url); ShareaholicUtilities::log($options); ShareaholicUtilities::log('-----------------'); $request = array(); $result = array(); $request['method'] = isset($options['method']) ? $options['method'] : 'GET'; $request['headers'] = isset($options['headers']) ? $options['headers'] : array(); $request['max_redirects'] = isset($options['redirection']) ? $options['redirection'] : 5; $request['timeout'] = isset($options['timeout']) ? $options['timeout'] : 15; $request['headers']['User-Agent'] = isset($options['user-agent']) ? $options['user-agent'] : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0'; if (isset($options['body'])) { if (isset($request['headers']['Content-Type']) && $request['headers']['Content-Type'] === 'application/json') { $request['data'] = json_encode($options['body']); } else { $request['data'] = http_build_query($options['body']); } } else { $request['body'] = NULL; } $response = drupal_http_request($url, $request); if (isset($response->error)) { ShareaholicUtilities::log($response->error); if (!$ignore_error) { ShareaholicUtilities::log_event('CurlRequestFailure', array('error_message' => $response->error, 'url' => $url)); } return false; } $result['headers'] = $response->headers; $result['body'] = $response->data; $result['response'] = array('code' => $response->code, 'message' => $response->status_message); return $result; }
private static function send_with_wp($url, $options, $ignore_error) { $request = array(); $result = array(); $request['method'] = isset($options['method']) ? $options['method'] : 'GET'; $request['headers'] = isset($options['headers']) ? $options['headers'] : array(); $request['redirection'] = isset($options['redirection']) ? $options['redirection'] : 5; $request['timeout'] = isset($options['timeout']) ? $options['timeout'] : 15; $request['user-agent'] = isset($options['user-agent']) ? $options['user-agent'] : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0'; if (isset($options['body'])) { if (isset($request['headers']['Content-Type']) && $request['headers']['Content-Type'] === 'application/json') { $request['body'] = json_encode($options['body']); } else { $request['body'] = $options['body']; } } else { $request['body'] = NULL; } $request['sslverify'] = false; $response = wp_remote_request($url, $request); if (is_wp_error($response)) { $error_message = $response->get_error_message(); ShareaholicUtilities::log($error_message); if (!$ignore_error) { ShareaholicUtilities::log_event('HttpRequestFailure', array('error_message' => $error_message, 'url' => $url)); } return false; } return $response; }
/** * A job that clears up the shareaholic share counts transients */ public static function remove_transients() { global $wpdb; $older_than = time() - 60 * 60; // older than an hour ago ShareaholicUtilities::log('Start of Shareaholic transient cleanup'); $query = "SELECT REPLACE(option_name, '_transient_timeout_', '') AS transient_name FROM {$wpdb->options} WHERE option_name LIKE '\\_transient\\_timeout\\_shr\\_api\\_res-%%' AND option_value < %s LIMIT 5000"; $transients = $wpdb->get_col($wpdb->prepare($query, $older_than)); $options_names = array(); foreach ($transients as $transient) { $options_names[] = esc_sql('_transient_' . $transient); $options_names[] = esc_sql('_transient_timeout_' . $transient); } if ($options_names) { $options_names = "'" . implode("','", $options_names) . "'"; $result = $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name IN ({$options_names})"); if (!$result) { ShareaholicUtilities::log('Transient Query Error!!!'); } } ShareaholicUtilities::log('End of Shareaholic transient cleanup'); }
/** * * Performs a request using cURL * * @param string $url the url you are GETing to * @param array $data an associative array of the data you are posting * @param string $data_type either an empty string or 'json' * @param string $method the HTTP verb to be used * * @return array the returned data json decoded */ private static function send_request($url, $data, $data_type, $method) { $curl = curl_init(); curl_setopt_array($curl, array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false)); /* * Because many shared hosting providers set `open_basedir` in php.ini * that means we can't always set CURLOPT_FOLLOWLOCATION. * This next block is an attempt around that by sending head requests * to determine if there will be a redirect and then following it. * Shamelessly stolen from here: * http://us2.php.net/manual/en/function.curl-setopt.php#102121 */ $mr = 5; if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $mr > 0); curl_setopt($curl, CURLOPT_MAXREDIRS, $mr); } else { curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); if ($mr > 0) { $newurl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); $rch = curl_copy_handle($curl); curl_setopt($rch, CURLOPT_HEADER, true); curl_setopt($rch, CURLOPT_NOBODY, true); curl_setopt($rch, CURLOPT_FORBID_REUSE, false); curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); do { curl_setopt($rch, CURLOPT_URL, $newurl); $header = curl_exec($rch); if (curl_errno($rch)) { $code = 0; } else { $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); if ($code == 301 || $code == 302) { preg_match('/Location:(.*?)\\n/', $header, $matches); $newurl = trim(array_pop($matches)); } else { $code = 0; } } } while ($code && --$mr); curl_close($rch); if (!$mr) { if ($maxredirect === null) { trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING); } else { $maxredirect = 0; } return false; } curl_setopt($curl, CURLOPT_URL, $newurl); } } /* end stolen code */ if ($method == 'POST') { curl_setopt_array($curl, array(CURLOPT_POST => 1, CURLOPT_HTTPHEADER => array("Accept: application/json,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain"))); if ($data_type == 'json') { curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($data))))); } else { curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => ShareaholicQueryStringBuilder::build_query_string($data))); } } $result = curl_exec($curl); $info = curl_getinfo($curl); ShareaholicUtilities::log(curl_error($curl)); ShareaholicUtilities::log(curl_getinfo($curl)); curl_close($curl); if (preg_match('/^20*/', $info['http_code'])) { return ShareaholicUtilities::object_to_array(json_decode($result)) ? ShareaholicUtilities::object_to_array(json_decode($result)) : $result; } else { return false; } }
/** * Custom error handler * * @param integer $errno The error level as an integer * @param string $errstr The error string * @param string $errfile The file where the error occurred * @param string $errline The line number where the error occurred */ public static function custom_error_handler($errno, $errstr, $errfile, $errline) { ShareaholicUtilities::log($errstr . ' ' . $errfile . ' ' . $errline); }