Esempio n. 1
0
 /**
  * Make an HTTP request
  * @param  \Communique\RESTClientRequest  $request  Request object
  * @return \Communique\RESTClientResponse $response Response object
  * @throws \Communique\CommuniqueRESTConnectionException This is thrown if cURL is unable to connect (this could be because the server is not available or the DNS record is not resolvable)
  * @throws \Communique\CommuniqueRESTSSLException REST SSL exception. This is thrown for things such as SSL certificate errors or SSL handshake errors.
  */
 public function request(\Communique\RESTClientRequest $request)
 {
     $headers = array();
     foreach ($request->headers as $header_key => $header_value) {
         $headers[] = $header_key . ': ' . $header_value;
     }
     $this->curl->setopt_array(array(CURLOPT_URL => $request->url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => $headers));
     $this->curl->setopt(CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
     switch ($request->method) {
         case 'POST':
             $this->curl->setopt_array(array(CURLOPT_POST => true, CURLOPT_POSTFIELDS => $request->payload));
             break;
         case 'PUT':
             $this->curl->setopt_array(array(CURLOPT_PUT => true, CURLOPT_POSTFIELDS => $request->payload));
             break;
         case 'DELETE':
             if ($payload = http_build_query($request->payload)) {
                 $payload = '?' . $payload;
             } else {
                 $payload = '';
             }
             $this->curl->setopt_array(array(CURLOPT_CUSTOMREQUEST => 'DELETE', CURLOPT_URL => $request->url . $payload));
             break;
         default:
         case 'GET':
             if ($payload = http_build_query($request->payload)) {
                 $payload = '?' . $payload;
             } else {
                 $payload = '';
             }
             $this->curl->setopt_array(array(CURLOPT_HTTPGET => true, CURLOPT_URL => $request->url . $payload));
             break;
     }
     $raw_response = $this->curl->exec();
     if (!$raw_response) {
         $curl_error = $this->curl->errno();
         switch ($curl_error) {
             case CURLE_SSL_PEER_CERTIFICATE:
             case CURLE_SSL_ENGINE_NOTFOUND:
             case CURLE_SSL_ENGINE_SETFAILED:
             case CURLE_SSL_CERTPROBLEM:
             case CURLE_SSL_CIPHER:
             case CURLE_SSL_CACERT:
             case CURLE_SSL_CONNECT_ERROR:
                 throw new \Communique\CommuniqueRESTSSLException('cURL SSL Error: ' . $this->curl->error() . ' cURL Error Code: ' . $curl_error);
                 break;
             case CURLE_UNSUPPORTED_PROTOCOL:
             case CURLE_COULDNT_CONNECT:
             case CURLE_COULDNT_RESOLVE_HOST:
                 throw new \Communique\CommuniqueRESTConnectionException('cURL Error: ' . $this->curl->error() . ' cURL Error Code: ' . $curl_error);
                 break;
             default:
                 throw new \Communique\CommuniqueRESTException('cURL Error: ' . $this->curl->error() . ' cURL Error Code: ' . $curl_error);
                 break;
         }
     } else {
         return new \Communique\RESTClientResponse($raw_response['http_status_code'], $raw_response['body'], $raw_response['headers']);
     }
 }
Esempio n. 2
0
 /**
  * This method must be called to start the worker execution.
  * @codeCoverageIgnore
  */
 public function run()
 {
     $this->preStartWorker();
     while (true) {
         try {
             $this->worker->work();
         } catch (\Exception $e) {
             $this->logIt($e->getMessage());
             $error = $this->worker->error();
             if (!empty($error)) {
                 $this->logIt($error);
             }
             exit(0);
         }
     }
 }
 /**
  * Copy file from another volume.
  * Return new file path or false.
  *
  * @param  Object  $volume       source volume
  * @param  string  $src          source file hash
  * @param  string  $destination  destination dir path
  * @param  string  $name         file name
  * @return string|false
  * @author Dmitry (dio) Levashov
  **/
 protected function copyFrom($volume, $src, $destination, $name)
 {
     if (($source = $volume->file($src)) == false) {
         return $this->setError(elFinder::ERROR_COPY, '#' . $src, $volume->error());
     }
     $errpath = $volume->path($src);
     if (!$this->nameAccepted($source['name'])) {
         return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_INVALID_NAME);
     }
     if (!$source['read']) {
         return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
     }
     if ($source['mime'] == 'directory') {
         $stat = $this->stat($this->_joinPath($destination, $name));
         $this->clearcache();
         if ((!$stat || $stat['mime'] != 'directory') && !$this->_mkdir($destination, $name)) {
             return $this->setError(elFinder::ERROR_COPY, $errpath);
         }
         $path = $this->_joinPath($destination, $name);
         foreach ($volume->scandir($src) as $entr) {
             if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
                 return false;
             }
         }
     } else {
         $mime = $source['mime'];
         $w = $h = 0;
         if (strpos($mime, 'image') === 0 && ($dim = $volume->dimensions($src))) {
             $s = explode('x', $dim);
             $w = $s[0];
             $h = $s[1];
         }
         if (($fp = $volume->open($src)) == false || ($path = $this->_save($fp, $destination, $name, $mime, $w, $h)) == false) {
             $fp && $volume->close($fp, $src);
             return $this->setError(elFinder::ERROR_COPY, $errpath);
         }
         $volume->close($fp, $src);
     }
     return $path;
 }
 /**
  * Copy file from another volume.
  * Return new file path or false.
  *
  * @param  Object  $volume       source volume
  * @param  string  $src          source file hash
  * @param  string  $destination  destination dir path
  * @param  string  $name         file name
  * @return string|false
  * @author Dmitry (dio) Levashov
  **/
 protected function copyFrom($volume, $src, $destination, $name)
 {
     if (($source = $volume->file($src)) == false) {
         return $this->setError(elFinder::ERROR_COPY, '#' . $src, $volume->error());
     }
     $errpath = $volume->path($source['hash']);
     if (!$this->nameAccepted($source['name'])) {
         return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_INVALID_NAME);
     }
     if (!$source['read']) {
         return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
     }
     if ($source['mime'] == 'directory') {
         $stat = $this->stat($this->joinPathCE($destination, $name));
         $this->clearcache();
         if ((!$stat || $stat['mime'] != 'directory') && $this->convEncOut(!$this->_mkdir($this->convEncIn($destination), $this->convEncIn($name)))) {
             return $this->setError(elFinder::ERROR_COPY, $errpath);
         }
         $path = $this->joinPathCE($destination, $name);
         foreach ($volume->scandir($src) as $entr) {
             if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
                 $this->remove($path, true);
                 // fall back
                 return $this->setError($this->error, elFinder::ERROR_COPY, $errpath);
             }
         }
     } else {
         // $mime = $source['mime'];
         // $w = $h = 0;
         if ($dim = $volume->dimensions($src)) {
             $s = explode('x', $dim);
             $source['width'] = $s[0];
             $source['height'] = $s[1];
         }
         if (($fp = $volume->open($src)) == false || ($path = $this->saveCE($fp, $destination, $name, $source)) == false) {
             $fp && $volume->close($fp, $src);
             return $this->setError(elFinder::ERROR_COPY, $errpath);
         }
         $volume->close($fp, $src);
         // MIME check
         $stat = $this->stat($path);
         if (!$this->allowPutMime($stat['mime'])) {
             $this->remove($path, true);
             return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, $errpath);
         }
     }
     return $path;
 }
 /**
  * Copy file from another volume.
  * Return new file path or false.
  *
  * @param  Object  $volume       source volume
  * @param  string  $src          source file hash
  * @param  string  $destination  destination dir path
  * @param  string  $name         file name
  * @return string|false
  * @author Dmitry (dio) Levashov
  **/
 protected function copyFrom($volume, $src, $destination, $name)
 {
     $source = $volume->file($src);
     if ($source == false) {
         return $this->setError('errCopy', '#' . $src, $volume->error());
     }
     $errpath = $volume->path($src);
     if (!$this->nameAccepted($source['name'])) {
         return $this->setError('errCopy', $errpath, 'errInvName');
     }
     if (!$source['read']) {
         return $this->setError('errCopy', $errpath, 'errPerm');
     }
     //directory
     if ($source['mime'] == 'directory') {
         $path = $this->_joinPath($destination, $name);
         $stat = $this->stat($path);
         $this->clearcache();
         if ((!$stat || $stat['mime'] != 'directory') && !$this->_mkdir($destination, $name)) {
             return $this->setError('errCopy', $errpath);
         }
         foreach ($volume->scandir($src) as $entr) {
             if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
                 return false;
             }
         }
         return $path;
     }
     //file
     if (strpos($source['mime'], 'image') === 0 && ($dim = $volume->dimensions($src))) {
         $s = explode('x', $dim);
         $stat['width'] = $s[0];
         $stat['height'] = $s[1];
     }
     $fp = $volume->open($src);
     if (!$fp) {
         return $this->setError('errCopy', $errpath);
     }
     $path = $this->_save($fp, $destination, $name, $source);
     if (!$path) {
         $volume->close($fp, $src);
         return $this->setError('errCopy', $errpath);
     }
     $volume->close($fp, $src);
     return $path;
 }
 /**
  * Renders discount text field
  * @param RedBeanModel $model
  * @param Object $form
  * @param string $inputNameIdPrefix
  * @param string $attribute
  * @return string
  */
 protected function renderDiscountOrMarkupPercentageTextField($model, $form, $inputNameIdPrefix, $attribute)
 {
     $id = $this->getEditableInputId($inputNameIdPrefix, $attribute);
     $htmlOptions = array('name' => $this->getEditableInputName($inputNameIdPrefix, $attribute), 'id' => $id, 'style' => $this->resolveInputDisplayStyle($model), 'onkeyup' => 'calculateSellPriceBySellPriceFormula()');
     $textField = $form->textField($model, $attribute, $htmlOptions);
     $error = $form->error($model, $attribute, array('inputID' => $id));
     return $textField . $error;
 }
 /**
  * Dispatcher working method
  *
  * @return  mixed
  */
 public final function dispatch()
 {
     // Before building dispatcher instance, fire THE level1 event "dispatcher"
     // This is the only way (out of dispatcher-config) to disable dispatcher
     $fork = $this->events->fire("dispatcher", "VOID", $this);
     // if ( is_bool($fork)  ) $this->enabled = $fork;
     // After building dispatcher instance, fire THE level2 event "dispatcher.request"
     // This default hook will expose current request (ObjectRequest) to callbacks
     $fork = $this->events->fire("dispatcher.request", "REQUEST", $this->request);
     if ($fork instanceof \Comodojo\Dispatcher\ObjectRequest\ObjectRequest) {
         $this->request = $fork;
     }
     // Fire level3 event "dispatcher.request.[method]"
     $fork = $this->events->fire("dispatcher.request." . $this->request_method, "REQUEST", $this->request);
     if ($fork instanceof \Comodojo\Dispatcher\ObjectRequest\ObjectRequest) {
         $this->request = $fork;
     }
     // Fire level3 event "dispatcher.request.[service]"
     $fork = $this->events->fire("dispatcher.request." . $this->request->getService(), "REQUEST", $this->request);
     if ($fork instanceof \Comodojo\Dispatcher\ObjectRequest\ObjectRequest) {
         $this->request = $fork;
     }
     // Fire special event, it will not modify request
     $this->events->fire("dispatcher.request.#", "VOID", $this->request);
     // Check if dispatcher is enabled
     if ($this->enabled == false) {
         $route = new ObjectError();
         $route->setStatusCode(503);
         $return = $this->route($route);
         $this->logger->info('Shutting down dispatcher (administratively disabled)');
         ob_end_clean();
         return $return;
     }
     // Before calculating service route, expose the routing table via level2 event "dispatcher.routingtable"
     $fork = $this->events->fire("dispatcher.routingtable", "TABLE", $this->routingtable);
     if ($fork instanceof \Comodojo\Dispatcher\ObjectRoutingTable\ObjectRoutingTable) {
         $this->routingtable = $fork;
     }
     // Retrieve current route from routing table
     $service = $this->request->getService();
     $this->logger->debug('Querying routingtable for service route', array('SERVICE' => $service));
     $preroute = $this->routingtable->getRoute($service);
     $this->serviceroute = new ObjectRoute();
     $this->serviceroute->setService($service)->setType($preroute["type"])->setTarget($preroute["target"]);
     if (isset($preroute["parameters"]["class"])) {
         $this->serviceroute->setClass($preroute["parameters"]["class"]);
     } else {
         $t = pathinfo($preroute["target"]);
         $this->serviceroute->setClass(preg_replace('/\\.[^.\\s]{3,4}$/', '', $t["filename"]));
     }
     if (isset($preroute["parameters"]["redirectCode"])) {
         $this->serviceroute->setRedirectCode($preroute["parameters"]["redirectCode"]);
         unset($preroute["parameters"]["redirectCode"]);
     }
     if (isset($preroute["parameters"]["errorCode"])) {
         $this->serviceroute->setErrorCode($preroute["parameters"]["errorCode"]);
         unset($preroute["parameters"]["errorCode"]);
     }
     if (isset($preroute["parameters"]["cache"])) {
         $this->serviceroute->setCache($preroute["parameters"]["cache"]);
         unset($preroute["parameters"]["cache"]);
     }
     if (isset($preroute["parameters"]["ttl"])) {
         $this->serviceroute->setTtl($preroute["parameters"]["ttl"]);
         unset($preroute["parameters"]["ttl"]);
     }
     if (isset($preroute["parameters"]["headers"])) {
         if (is_array($preroute["parameters"]["headers"])) {
             foreach ($preroute["parameters"]["headers"] as $header => $value) {
                 $this->serviceroute->setHeader($header, $value);
             }
         }
         unset($preroute["parameters"]["headers"]);
     }
     if (isset($preroute["parameters"]["accessControl"])) {
         $this->serviceroute->setRedirectCode($preroute["parameters"]["accessControl"]);
         unset($preroute["parameters"]["accessControl"]);
     }
     foreach ($preroute["parameters"] as $parameter => $value) {
         $this->serviceroute->setParameter($parameter, $value);
     }
     // Now that we have a route, fire the level2 event "dispatcher.serviceroute"
     // and level3 events:
     // - "dispatcher.serviceroute.[type]"
     // - "dispatcher.serviceroute.[service]"
     $fork = $this->events->fire("dispatcher.serviceroute", "ROUTE", $this->serviceroute);
     if ($fork instanceof \Comodojo\Dispatcher\ObjectRoute\ObjectRoute) {
         $this->serviceroute = $fork;
     }
     $fork = $this->events->fire("dispatcher.serviceroute." . $this->serviceroute->getType(), "ROUTE", $this->serviceroute);
     if ($fork instanceof \Comodojo\Dispatcher\ObjectRoute\ObjectRoute) {
         $this->serviceroute = $fork;
     }
     $fork = $this->events->fire("dispatcher.serviceroute." . $this->serviceroute->getService(), "ROUTE", $this->serviceroute);
     if ($fork instanceof \Comodojo\Dispatcher\ObjectRoute\ObjectRoute) {
         $this->serviceroute = $fork;
     }
     // Fire special event, it will not modify route
     $this->events->fire("dispatcher.serviceroute.#", "VOID", $this->serviceroute);
     // Before using route to handle request, check if access control should block instance
     $accesscontrol = preg_replace('/\\s+/', '', $this->serviceroute->getAccessControl());
     if ($accesscontrol != null and $accesscontrol != "*") {
         $origins = explode(",", $accesscontrol);
         if (!in_array(@$_SERVER['HTTP_ORIGIN'], $origins)) {
             $route = new ObjectError();
             $route->setStatusCode(403)->setContent("Origin not allowed");
             $return = $this->route($route);
             ob_end_clean();
             return $return;
         }
     }
     $this->logger->debug('Service route acquired', array('SERVICE' => $this->serviceroute->getService(), 'TYPE' => $this->serviceroute->getType(), 'CLASS' => $this->serviceroute->getClass(), 'REDIRECTCODE' => $this->serviceroute->getRedirectCode(), 'ERRORCODE' => $this->serviceroute->getErrorCode(), 'CACHE' => $this->serviceroute->getCache(), 'TTL' => $this->serviceroute->getTtl(), 'HEADERS' => $this->serviceroute->getHeaders(), 'REDIRECTCODE' => $this->serviceroute->getRedirectCode()));
     switch ($this->serviceroute->getType()) {
         case "ERROR":
             $route = new ObjectError();
             $route->setService($this->serviceroute->getService())->setStatusCode($this->serviceroute->getErrorCode())->setContent($this->serviceroute->getTarget())->setHeaders($this->serviceroute->getHeaders());
             break;
         case "REDIRECT":
             $route = new ObjectRedirect();
             $route->setService($this->serviceroute->getService())->setStatusCode($this->serviceroute->getRedirectCode())->setLocation($this->serviceroute->getTarget())->setHeaders($this->serviceroute->getHeaders());
             break;
         case "ROUTE":
             try {
                 $route = $this->runService($this->request, $this->serviceroute);
             } catch (DispatcherException $de) {
                 $this->logger->error('Service returns a DispatcherException', array('SERVICE' => $this->serviceroute->getService(), 'CODE' => $de->getCode(), 'MESSAGE' => $de->getMessage()));
                 $route = new ObjectError();
                 $route->setService($this->serviceroute->getService())->setStatusCode($de->getCode())->setContent($de->getMessage());
             } catch (Exception $e) {
                 $this->logger->error('Error processing service', array('SERVICE' => $this->serviceroute->getService(), 'CODE' => $e->getCode(), 'MESSAGE' => $e->getMessage()));
                 $route = new ObjectError();
                 $route->setService($this->serviceroute->getService())->setStatusCode(500)->setContent($e->getMessage());
             }
             break;
     }
     $return = $this->route($route);
     ob_end_clean();
     return $return;
 }
 /**
  * Copy file from another volume.
  * Return new file path or false.
  *
  * @param  Object  $volume       source volume
  * @param  string  $src          source file hash
  * @param  string  $destination  destination dir path
  * @param  string  $name         file name
  * @return string|false
  * @author Dmitry (dio) Levashov
  **/
 protected function copyFrom($volume, $src, $destination, $name)
 {
     elFinder::extendTimeLimit();
     if (($source = $volume->file($src)) == false) {
         return $this->setError(elFinder::ERROR_COPY, '#' . $src, $volume->error());
     }
     $errpath = $volume->path($source['hash']);
     if (!$this->nameAccepted($source['name'])) {
         return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_INVALID_NAME);
     }
     if (!$source['read']) {
         return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED);
     }
     if ($source['mime'] == 'directory') {
         $test = $this->stat($this->joinPathCE($destination, $name));
         $this->clearcache();
         if ($test && $test['mime'] != 'directory' || !$test && !($test = $this->mkdir($this->encode($destination), $name))) {
             return $this->setError(elFinder::ERROR_COPY, $errpath);
         }
         $path = $this->joinPathCE($destination, $name);
         $path = $this->decode($test['hash']);
         foreach ($volume->scandir($src) as $entr) {
             if (!$this->copyFrom($volume, $entr['hash'], $path, $entr['name'])) {
                 $this->remove($path, true);
                 // fall back
                 return $this->setError($this->error, elFinder::ERROR_COPY, $errpath);
             }
         }
         $this->added[] = $test;
     } else {
         // $mime = $source['mime'];
         // $w = $h = 0;
         if ($dim = $volume->dimensions($src)) {
             $s = explode('x', $dim);
             $source['width'] = $s[0];
             $source['height'] = $s[1];
         }
         if (($fp = $volume->open($src)) == false || ($path = $this->saveCE($fp, $destination, $name, $source)) == false) {
             $fp && $volume->close($fp, $src);
             return $this->setError(elFinder::ERROR_COPY, $errpath);
         }
         $volume->close($fp, $src);
         // MIME check
         $stat = $this->stat($path);
         $mimeByName = elFinderVolumeDriver::mimetypeInternalDetect($stat['name']);
         if ($stat['mime'] === $mimeByName) {
             $mimeByName = '';
         }
         if (!$this->allowPutMime($stat['mime']) || $mimeByName && $mimeByName !== 'unknown' && !$this->allowPutMime($mimeByName)) {
             $this->remove($path, true);
             return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, $errpath);
         }
         $this->added[] = $stat;
     }
     return $path;
 }