/**
  * Gibt die Geokoordinaten anhand einer Adresse zurück
  *
  * @param string $address Die Adresse die geocodet werden woll
  * @return array|null $geocode Ein Array mit key 'lat' und 'lng'
  */
 public static function getCoordinates($address)
 {
     $q = $address;
     $q = str_replace(array('ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß'), array('ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss'), $q);
     $getParams = array('address' => $q, 'sensor' => 'false');
     $httpClientConfig = array('timeout' => 20, 'persistent' => false);
     $config = Kwf_Registry::get('config');
     if ($config->http && $config->http->proxy && $config->http->proxy->host && $config->http->proxy->port) {
         $httpClientConfig['adapter'] = 'Zend_Http_Client_Adapter_Proxy';
         $httpClientConfig['proxy_host'] = $config->http->proxy->host;
         $httpClientConfig['proxy_port'] = $config->http->proxy->port;
     }
     $client = new Zend_Http_Client("http://maps.googleapis.com/maps/api/geocode/json", $httpClientConfig);
     $client->setMethod(Zend_Http_Client::GET);
     $client->setParameterGet($getParams);
     $body = utf8_encode($client->request()->getBody());
     try {
         $result = Zend_Json::decode($body);
     } catch (Zend_Json_Exception $e) {
         $e = new Kwf_Exception_Other($e);
         $e->logOrThrow();
     }
     if (isset($result['results'][0]['geometry']['location']['lat']) && isset($result['results'][0]['geometry']['location']['lng'])) {
         return array('lat' => $result['results'][0]['geometry']['location']['lat'], 'lng' => $result['results'][0]['geometry']['location']['lng']);
     }
     return null;
 }
 public function jsonErrorAction()
 {
     $errors = $this->getRequest()->getParam('error_handler');
     $exception = $errors->exception;
     if ($exception instanceof Kwf_Exception_Abstract) {
         $this->getResponse()->setRawHeader($exception->getHeader());
     } else {
         $this->getResponse()->setRawHeader('HTTP/1.1 500 Internal Server Error');
     }
     if ($exception instanceof Kwf_Exception_Client) {
         $this->view->error = $exception->getMessage();
     } else {
         if (!$exception instanceof Kwf_Exception_Abstract) {
             $exception = new Kwf_Exception_Other($exception);
         }
         $this->view->error = $exception->getMessage();
         if (!$this->view->error) {
             $this->view->error = 'An error occurred';
         }
         if (Kwf_Exception::isDebug()) {
             $this->view->exception = explode("\n", $exception->getException()->__toString());
         }
     }
     $exception->log();
 }
 private function _processException($exception)
 {
     if (!$exception instanceof Kwf_Exception_Abstract) {
         $exception = new Kwf_Exception_Other($exception);
     }
     $view = new Kwf_Exception_TestView();
     Kwf_Debug::setView($view);
     $exception->render(true);
     return $view;
 }
 public function getValue($name)
 {
     $memcache = $this->getMemcache();
     try {
         return $memcache->get($this->_prefix . $name);
     } catch (ErrorException $e) {
         if ($e->getSeverity() == E_NOTICE) {
             $e = new Kwf_Exception_Other($e);
             $e->logOrThrow();
             return false;
         }
         throw $e;
     }
 }
 public function getTemplateVars(Kwf_Component_Renderer_Abstract $renderer)
 {
     $ret = parent::getTemplateVars($renderer);
     static $productsByPage = array();
     $products = array();
     if (!isset($productsByPage[$this->getData()->parent->componentId])) {
         $all = $this->getData()->parent->getChildComponents(array('componentClass' => $this->getData()->componentClass, 'ignoreVisible' => true));
         $asins = array();
         foreach ($all as $c) {
             $asins[] = $c->getComponent()->getRow()->asin;
         }
         $asinsChunks = array_chunk($asins, 10);
         foreach ($asinsChunks as $chunk) {
             $amazon = new Kwf_Service_Amazon();
             try {
                 $resultSet = $amazon->itemLookup(implode($chunk, ','), array('AssociateTag' => $this->_getSetting('associateTag'), 'ResponseGroup' => 'Small,ItemAttributes,Images'));
             } catch (Zend_Service_Exception $e) {
                 $e = new Kwf_Exception_Other($e);
                 $e->logOrThrow();
                 $resultSet = array();
             }
             if ($resultSet instanceof Kwf_Service_Amazon_Item) {
                 $resultSet = array($resultSet);
             }
             foreach ($resultSet as $i) {
                 if (!is_null($i) && isset($i->ASIN)) {
                     $products[$i->ASIN] = (object) array('item' => $i, 'title' => isset($i->Title) ? $i->Title : null, 'author' => isset($i->Author) ? is_array($i->Author) ? implode($i->Author, ', ') : $i->Author : null, 'asin' => $i->ASIN, 'detailPageURL' => isset($i->DetailPageURL) ? $i->DetailPageURL : null, 'currencyCode' => isset($i->CurrencyCode) ? $i->CurrencyCode : null, 'amount' => isset($i->Amount) ? $i->Amount : null, 'formattedPrice' => isset($i->FormattedPrice) ? $i->FormattedPrice : null, 'salesRank' => isset($i->SalesRank) ? $i->SalesRank : null, 'averageRating' => isset($i->AverageRating) ? $i->AverageRating : null);
                 }
             }
         }
         $productsByPage[$this->getData()->parent->componentId] = $products;
     }
     $ret['product'] = null;
     if ($this->getRow()->asin) {
         if (isset($productsByPage[$this->getData()->parent->componentId][strtoupper($this->getRow()->asin)])) {
             $ret['product'] = $productsByPage[$this->getData()->parent->componentId][strtoupper($this->getRow()->asin)];
         }
     }
     return $ret;
 }
Beispiel #6
0
 protected function _handleProcessException(Exception $e)
 {
     if ($e instanceof Kwf_Exception_Client) {
         $this->_errors[] = array('message' => $e->getMessage());
     } else {
         if (!$e instanceof Kwf_Exception) {
             $e = new Kwf_Exception_Other($e);
         }
         $e->logOrThrow();
         $this->_errors[] = array('message' => trlKwf('An error occured while processing the form. Please try to submit again later.'));
     }
 }
 public function touch($id, $extraLifetime)
 {
     $id = $this->_processId($id);
     try {
         return parent::touch($id, $extraLifetime);
     } catch (ErrorException $e) {
         if ($e->getSeverity() == E_NOTICE) {
             $e = new Kwf_Exception_Other($e);
             $e->logOrThrow();
             return false;
         }
         throw $e;
     }
 }
Beispiel #8
0
 public static function handleException($exception)
 {
     if ($exception instanceof Zend_Controller_Exception && $exception->getPrevious()) {
         $exception = $exception->getPrevious();
     }
     if (!$exception instanceof Kwf_Exception_Abstract) {
         $exception = new Kwf_Exception_Other($exception);
     }
     $exception->render();
     Kwf_Benchmark::shutDown();
     Kwf_Benchmark::output();
 }
 public static function executeJobs($jobFrequency, $debug)
 {
     foreach (self::getAllMaintenanceJobs() as $job) {
         if ($job->getFrequency() == $jobFrequency) {
             if ($debug) {
                 echo "executing " . get_class($job) . "\n";
             }
             $maxTime = $job->getMaxTime();
             $t = microtime(true);
             if ($jobFrequency == Kwf_Util_Maintenance_Job_Abstract::FREQUENCY_DAILY || $jobFrequency == Kwf_Util_Maintenance_Job_Abstract::FREQUENCY_HOURLY) {
                 $cmd = "php bootstrap.php maintenance-jobs run-job --job=" . escapeshellarg(get_class($job));
                 if ($debug) {
                     $cmd .= " --debug";
                 }
                 $descriptorspec = array();
                 $pipes = array();
                 $process = proc_open($cmd, $descriptorspec, $pipes);
                 if (!is_resource($process)) {
                     $e = new Kwf_Exception("Couldn't start maintenance job " . get_class($job));
                     $e->logOrThrow();
                     continue;
                 }
                 $retVar = null;
                 while (true) {
                     $status = proc_get_status($process);
                     if (!$status['running']) {
                         $retVar = $status['exitcode'];
                         break;
                     }
                     if (microtime(true) - $t > $maxTime * 2) {
                         //when jobs runs maxTime twice kill it
                         file_put_contents('php://stderr', "\nWARNING: Killing maintenance-jobs process (running > maxTime*2)...\n");
                         proc_terminate($process);
                         break;
                     }
                     sleep(1);
                 }
                 proc_close($process);
                 if ($retVar) {
                     $e = new Kwf_Exception("Maintenance job " . get_class($job) . " failed with exit code {$retVar}");
                     $e->logOrThrow();
                 }
             } else {
                 try {
                     $job->execute($debug);
                 } catch (Exception $e) {
                     file_put_contents('php://stderr', $e->toString() . "\n");
                     if (!$e instanceof Kwf_Exception_Abstract) {
                         $e = new Kwf_Exception_Other($e);
                     }
                     $e->logOrThrow();
                 }
             }
             $t = microtime(true) - $t;
             if ($debug) {
                 echo "executed " . get_class($job) . " in " . round($t, 3) . "s\n";
             }
             if ($t > $maxTime) {
                 $msg = "Maintenance job " . get_class($job) . " took " . round($t, 3) . "s to execute which is above the limit of {$maxTime}.";
                 file_put_contents('php://stderr', $msg . "\n");
                 $e = new Kwf_Exception($msg);
                 $e->logOrThrow();
             }
             if ($debug) {
                 echo "\n";
             }
         }
     }
 }