Example #1
13
 function registerEventHooks()
 {
     \Idno\Core\site()->syndication()->registerService('soundcloud', function () {
         return $this->hasSoundcloud();
     }, array('media'));
     \Idno\Core\site()->addEventHook('user/auth/success', function (\Idno\Core\Event $event) {
         if ($this->hasSoundcloud()) {
             if (is_array(\Idno\Core\site()->session()->currentUser()->soundcloud)) {
                 foreach (\Idno\Core\site()->session()->currentUser()->soundcloud as $username => $details) {
                     if (!in_array($username, ['username', 'access_token'])) {
                         \Idno\Core\site()->syndication()->registerServiceAccount('soundcloud', $username, $username);
                     }
                 }
             }
         }
     });
     // Push "media" to Soundcloud
     \Idno\Core\site()->addEventHook('post/media/soundcloud', function (\Idno\Core\Event $event) {
         $eventdata = $event->data();
         $object = $eventdata['object'];
         if ($attachments = $object->getAttachments()) {
             foreach ($attachments as $attachment) {
                 if ($this->hasSoundcloud()) {
                     if (!empty($eventdata['syndication_account'])) {
                         $soundcloudAPI = $this->connect($eventdata['syndication_account']);
                         $user_details = \Idno\Core\site()->session()->currentUser()->soundcloud[$eventdata['syndication_account']];
                         if (!empty($user_details['username'])) {
                             $name = $user_details['username'];
                         }
                     } else {
                         $soundcloudAPI = $this->connect();
                         $user_details = \Idno\Core\site()->session()->currentUser()->soundcloud['access_token'];
                     }
                     if (empty($name)) {
                         $name = 'SoundCloud';
                     }
                     if ($soundcloudAPI && !empty($user_details)) {
                         $soundcloudAPI->setAccessToken($user_details['access_token']);
                         if ($bytes = \Idno\Entities\File::getFileDataFromAttachment($attachment)) {
                             $media = '';
                             $filename = tempnam(sys_get_temp_dir(), 'knownsoundcloud') . '.' . pathinfo($attachment['url'], PATHINFO_EXTENSION);
                             file_put_contents($filename, $bytes);
                             if (version_compare(phpversion(), '5.5', '>=')) {
                                 $media = new \CURLFile($filename);
                                 if (!empty($attachment['mime_type'])) {
                                     $media->setMimeType($attachment['mime_type']);
                                 }
                                 if (!empty($attachment['filename'])) {
                                     $media->setPostFilename($attachment['filename']);
                                 }
                             } else {
                                 $media .= "@{$filename}";
                                 //;type=" . $attachment['mime_type'] . ';filename=' . $attachment['filename'];
                             }
                         }
                         $message = strip_tags($object->getDescription());
                         $message .= "\n\nOriginal: " . $object->getURL();
                         try {
                             $track = json_decode($soundcloudAPI->post('tracks', array('track[title]' => $object->getTitle(), 'track[asset_data]' => $media, 'track[description]' => $message)));
                             if (!empty($track->permalink_url)) {
                                 $result['id'] = $track->id;
                                 $object->setPosseLink('soundcloud', $track->permalink_url, $name);
                                 $object->save();
                             }
                         } catch (\Exception $e) {
                             \Idno\Core\site()->session()->addMessage('Could not post sound to SoundCloud: ' . $e->getMessage());
                         }
                         @unlink($filename);
                     }
                 }
             }
         }
     });
 }
 public function testCurlFileWithMimeAndPostname()
 {
     $file = new CURLFile($this->testFilename);
     $file->setPostFilename('bar');
     $file->setMimeType('foo');
     curl_setopt($this->handle, CURLOPT_POSTFIELDS, array('file' => $file));
     $output = curl_exec($this->handle);
     $this->assertEquals('bar|foo|6', $output);
 }
 /**
  * Returns a value for the CURLOPT_POSTFIELDS option.
  *
  * @return string|array A post fields value
  */
 private static function getPostFields(RequestInterface $request)
 {
     if (!$request instanceof FormRequestInterface) {
         return $request->getContent();
     }
     $fields = $request->getFields();
     $multipart = false;
     foreach ($fields as $name => $value) {
         if (!$value instanceof FormUploadInterface) {
             continue;
         }
         if (!($file = $value->getFile())) {
             return $request->getContent();
         }
         $multipart = true;
         if (version_compare(PHP_VERSION, '5.5', '>=')) {
             $curlFile = new \CURLFile($file);
             if ($contentType = $value->getContentType()) {
                 $curlFile->setMimeType($contentType);
             }
             if (basename($file) != $value->getFilename()) {
                 $curlFile->setPostFilename($value->getFilename());
             }
             $fields[$name] = $curlFile;
         } else {
             // replace value with upload string
             $fields[$name] = '@' . $file;
             if ($contentType = $value->getContentType()) {
                 $fields[$name] .= ';type=' . $contentType;
             }
             if (basename($file) != $value->getFilename()) {
                 $fields[$name] .= ';filename=' . $value->getFilename();
             }
         }
     }
     return $multipart ? $fields : http_build_query($fields, '', '&');
 }
Example #4
0
 /**
  * Execute a request with the given parameters and return the response.
  *
  * @throws \Exception|Exception|NotFoundException|NotAuthorizedException An exception is thrown if an error occurs.
  * @param string $method The http method (GET, PUT, POST).
  * @param string $url The request url.
  * @param string|array $body Data that should be send or the filename of the file if PUT is used. If this is an array, it will be used as CURLOPT_POSTFIELDS
  * @param int $ignore_http_return_status Ignore the given http status code.
  * @return array An array holding the response content in 'content' and the response status in 'response'.
  */
 protected function request($method, $url, $body = null, $ignore_http_return_status = 0)
 {
     if (substr($url, 0, strlen('http://')) != 'http://' && substr($url, 0, strlen('https://')) != 'https://') {
         $url = $this->base_url . $url;
     }
     $this->http = curl_init($url);
     $headers = $this->headers;
     if ($method == 'PUT' || $method == 'POST') {
         if (is_string($body) && !file_exists($body)) {
             if (is_array($body)) {
                 curl_setopt($this->http, CURLOPT_POSTFIELDS, $body);
             } else {
                 $headers[CURLOPT_HTTPHEADER][] = 'Content-Type: application/xml; charset=UTF-8';
                 $headers[CURLOPT_HTTPHEADER][] = 'Content-Length: ' . strlen($body);
                 curl_setopt($this->http, CURLOPT_POSTFIELDS, $body);
             }
         }
     }
     switch ($method) {
         case 'GET':
             curl_setopt($this->http, CURLOPT_HTTPGET, true);
             break;
         case 'PUT':
             curl_setopt($this->http, CURLOPT_CUSTOMREQUEST, 'PUT');
             //                $handle = null;
             //                // Check if we got a file or just a string of data.
             //                if (is_string($body) && file_exists($body)) {
             //                    $size = filesize($body);
             //                    if (!$size) {
             //                        throw new \Exception("Can't open file $body!");
             //                    }
             //                    $handle = fopen($body, 'r');
             //                } else {
             //                    $size = strlen($body);
             //                    $handle = fopen('data://text/plain,' . $body, 'r');
             //                }
             //                curl_setopt($this->http, CURLOPT_PUT, true);
             //                curl_setopt($this->http, CURLOPT_INFILE, $handle);
             //                curl_setopt($this->http, CURLOPT_INFILESIZE, $size);
             break;
         case 'POST':
             curl_setopt($this->http, CURLOPT_POST, true);
             if (!empty($body)) {
                 $filename = null;
                 if (is_array($body) && isset($body['filename']) && isset($body['file'])) {
                     $filename = $body['filename'];
                     $body = $body['file'];
                 }
                 if (is_string($body) && file_exists($body)) {
                     if (version_compare(PHP_VERSION, '5.5', '>=') && class_exists('\\CURLFile')) {
                         $file = new \CURLFile($body);
                         $mimeType = $this->getMimeTypeByFileExtension($body);
                         if (null !== $mimeType) {
                             $file->setMimeType($mimeType);
                         }
                         if (isset($filename)) {
                             $file->setPostFilename($filename);
                         }
                     } else {
                         $file = '@' . $body;
                         if (isset($filename)) {
                             $file .= '; filename=' . $filename;
                         }
                     }
                     $body = array('file' => $file);
                 }
                 curl_setopt($this->http, CURLOPT_POSTFIELDS, $body);
             }
             break;
         case 'DELETE':
             curl_setopt($this->http, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         default:
             throw new \Exception("Unknown HTTP method {$method} for YouTrack!");
     }
     curl_setopt($this->http, CURLOPT_HTTPHEADER, $headers[CURLOPT_HTTPHEADER]);
     curl_setopt($this->http, CURLOPT_USERAGENT, $this->user_agent);
     curl_setopt($this->http, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($this->http, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
     curl_setopt($this->http, CURLOPT_VERBOSE, $this->debug_verbose);
     curl_setopt($this->http, CURLOPT_COOKIE, implode(';', $this->cookies));
     if (is_numeric($this->connectTimeout)) {
         curl_setopt($this->http, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
     }
     if (is_numeric($this->timeout)) {
         curl_setopt($this->http, CURLOPT_TIMEOUT, $this->timeout);
     }
     $content = curl_exec($this->http);
     $response = curl_getinfo($this->http);
     curl_close($this->http);
     if ((int) $response['http_code'] != 200 && (int) $response['http_code'] != 201 && (int) $response['http_code'] != $ignore_http_return_status) {
         if ((int) $response['http_code'] === 403) {
             throw new NotAuthorizedException($url, $response, $content);
         }
         if ((int) $response['http_code'] === 404) {
             throw new NotFoundException($url, $response, $content);
         }
         throw new Exception($url, $response, $content);
     }
     if ($this->responseLogging) {
         // for fetching results for test data
         if (!empty($content)) {
             file_put_contents($this->responseLoggingPath . '/' . md5($content) . '.xml', $content);
         }
     }
     return array('content' => $content, 'response' => $response);
 }
function send_post_pic($url, array $post_data = array())
{
    foreach ($post_data as $field => $value) {
        if (strpos($value, '@') === 0) {
            $file = substr($value, 1);
            $mimeType = mime_content_type($file);
            $fileObj = "@{$file};type={$mimeType}";
            if (class_exists('CURLFile')) {
                $fileObj = new CURLFile($file);
                $fileObj->setMimeType($mimeType);
                //var_dump($file, $mimeType);
                //exit();
            }
            $post_data[$field] = $fileObj;
        }
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 75);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
 /**
  * Create upload handle
  *
  * @param string $url Request URL
  * @param string $upload_file Filename
  *
  * @return resource
  */
 private function createUploadHandle($url, $upload_file)
 {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_URL, $url);
     // send file
     curl_setopt($ch, CURLOPT_POST, true);
     if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 5) {
         $attachments = realpath($upload_file);
         $filename = basename($upload_file);
         curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@' . $attachments . ';filename=' . $filename));
     } else {
         // CURLFile require PHP > 5.5
         $attachments = new \CURLFile(realpath($upload_file));
         $attachments->setPostFilename(basename($upload_file));
         curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $attachments));
     }
     $this->authorization($ch);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost());
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer());
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'Content-Type: multipart/form-data', 'X-Atlassian-Token: nocheck'));
     curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose());
     return $ch;
 }
 private function createUploadHandle($url, $upload_file)
 {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_URL, $url);
     // send file
     curl_setopt($ch, CURLOPT_POST, true);
     if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 5) {
         $attachments = realpath($upload_file);
         $filename = basename($upload_file);
         curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@' . $attachments . ';filename=' . $filename));
         $this->debug('using legacy file upload');
     } else {
         // CURLFile require PHP > 5.5
         $attachments = new \CURLFile(realpath($upload_file));
         $attachments->setPostFilename(basename($upload_file));
         curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $attachments));
         $this->debug('using CURLFile=' . var_export($attachments, true));
     }
     curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->CURLOPT_SSL_VERIFYHOST);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->CURLOPT_SSL_VERIFYPEER);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'Content-Type: multipart/form-data', 'X-Atlassian-Token: nocheck'));
     curl_setopt($ch, CURLOPT_VERBOSE, $this->CURLOPT_VERBOSE);
     $this->debug('Curl exec=' . $url);
     return $ch;
 }
Example #8
0
 public function sendFileToEamsCentral($model)
 {
     $targetDir = Yii::getPathOfAlias('webroot') . '/uploads/exports/';
     $cfile = new CURLFile($targetDir . $model->name);
     $cfile->setMimeType($model->mime_type);
     $cfile->setPostFilename($model->name);
     $fields = ['file' => $cfile, 'filename' => $model->name];
     switch ($model->export_key) {
         case 'dc':
             $url = 'http://www.eamscentral.org/admin_import_tmp_depository/admin_import_tmp_dc.aspx';
             break;
         case 'exdc':
             $url = 'http://www.eamscentral.org/admin_import_tmp_depository/admin_import_tmp_exdc.aspx';
             break;
         case 'su':
             $url = 'http://www.eamscentral.org/admin_import_tmp_depository/admin_import_tmp_su.aspx';
             break;
         case 'exsu':
             $url = 'http://www.eamscentral.org/admin_import_tmp_depository/admin_import_tmp_exsu.aspx';
             break;
         case 'sc':
             $url = 'http://www.eamscentral.org/admin_import_tmp_depository/admin_import_tmp_sc.aspx';
             break;
         case 'cm':
             $url = 'http://www.eamscentral.org/admin_import_tmp_depository/admin_import_tmp_cm.aspx';
             break;
         default:
             break;
     }
     try {
         //post data to eams central
         echo $this->httpPost($url, $fields);
     } catch (Exception $ex) {
         //update send status
         echo "Operation unsuccessful";
     }
 }
Example #9
0
 /**
  * Can be used to upload a file with CURLOPT_POSTFIELDS.
  *
  * @param string $filename Filename of file
  * @param string $mimetype MIME type of file
  * @param string $postname Name of file for post data
  * @see https://php.net/manual/en/class.curlfile.php
  */
 public final function curlFileCreate($filename, $mimetype = null, $postname = null)
 {
     $file = new \CURLFile($fname, $mime, $name);
     return [$file->getPostFilename(), $file];
 }
Example #10
-1
 /**
  * 上传临时素材
  *
  * image 1M, voice 2M, video 10M, thumb 64K     
  * @return boolean | array(type => , media_id => , created_at => )
  */
 public function uploadFile($type, $file)
 {
     static $url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s';
     $token = $this->getAccessToken();
     $body = Utility::http(sprintf($url, $token, $type), array('media' => CURLFile::realpath($file)));
     $json = json_decode($body, true);
     if (!$json || !empty($json['errcode'])) {
         return false;
     } else {
         return $json;
     }
 }