diff --git a/oxrest/.htaccess b/oxrest/.htaccess new file mode 100644 index 0000000..79b7d08 --- /dev/null +++ b/oxrest/.htaccess @@ -0,0 +1,3 @@ +Order Deny,Allow +Allow From All +Satisfy Any \ No newline at end of file diff --git a/oxrest/oxrest.php b/oxrest/oxrest.php index dcef955..6cf2b2c 100644 --- a/oxrest/oxrest.php +++ b/oxrest/oxrest.php @@ -31,7 +31,7 @@ else { // for older shops before 4.7., do some custom OXID bootstrapping // and also include oxRegistry - include __DIR__ . '/bootstrap_oxid.php'; + include __DIR__ . '/bootstrap_oxid.php'; } $config = array( @@ -47,8 +47,10 @@ $request = new Tonic\Request(array('uri' => $result)); try { + /** @var OxRestBase $resource */ $resource = $app->getResource($request); $response = $resource->exec(); + } catch (Tonic\NotFoundException $e) { $response = new Tonic\Response(404, $e->getMessage()); } catch (Tonic\UnauthorizedException $e) { @@ -56,5 +58,4 @@ } catch (Tonic\Exception $e) { $response = new Tonic\Response($e->getCode(), $e->getMessage()); } - $response->output(); diff --git a/oxrest/service/OxRestBase.php b/oxrest/service/OxRestBase.php index ff43cec..f7e0d26 100644 --- a/oxrest/service/OxRestBase.php +++ b/oxrest/service/OxRestBase.php @@ -178,21 +178,20 @@ protected function json() protected function _oxObject2Array($o) { $vars = get_object_vars($o); + $a = array(); - foreach ($vars as $key => $value) { - $vars = get_object_vars($o); - foreach ($vars as $key => $value) { - if (($pos = strpos($key, '__')) > 0) { - $key = substr($key, $pos + 2); - if ($this->_keyIsBlacklisted($key)) { - continue; - } - $value = $value->getRawValue(); - $a[$key] = $value; + foreach ($vars as $key => $value) { + if (($pos = strpos($key, '__')) > 0) { + $key = substr($key, $pos + 2); + if ($this->_keyIsBlacklisted($key)) { + continue; } + $value = $value->getRawValue(); + $a[$key] = $value; } } + return $a; } diff --git a/oxrest/service/OxRestList.php b/oxrest/service/OxRestList.php index 6960a2b..4de67aa 100644 --- a/oxrest/service/OxRestList.php +++ b/oxrest/service/OxRestList.php @@ -91,9 +91,9 @@ public function getList($class) $iLimitOffset = $iPerPage * $iCurPage; $sSql = $sSql . " LIMIT $iLimitOffset,$iPerPage"; - + //$this->_doLog($sSql); - + $l = array(); $list->selectString( $sSql @@ -101,7 +101,7 @@ public function getList($class) foreach ($list->getArray() as $oxid => $oxObject) { $l[$oxid] = $this->_oxObject2Array($oxObject); } - + $ret['numPages'] = $iNumPages; $ret['curPage'] = $iCurPage; $ret['numObjects'] = $cnt; @@ -109,7 +109,7 @@ public function getList($class) $ret['result'] = $l; $ret['numCurr'] = count($l); //$this->_doLog("list: " . print_r($l, true)); - + return new Response(200, $ret); } catch (Exception $ex) { $this->_doLog("Error getting list: " . $ex->getMessage()); @@ -117,7 +117,7 @@ public function getList($class) return new Response(404, "No data"); } - + /** * Saving list data back to DB * @method PUT @@ -134,7 +134,7 @@ public function saveList($class) // check for data in request if ($this->request->data && $this->request->data != "") { $this->_doLog("CLASS: $class\nDATA: " . print_r(json_encode($this->request->data), true)); - + /** @var oxList $list */ $list = oxNew($class); $bo = $list->getBaseObject(); @@ -187,7 +187,7 @@ public function createFromList($class) $oxObj = clone $bo; if (!isset($sOxid) || $sOxid == '') { // create new OXID - $sOxid = oxRegistry::get('oxUtilsObject')->generateUId(); + $sOxid = \OxidEsales\Eshop\Core\Registry::getUtilsObject()->generateUID(); $aObjData['oxid'] = $sOxid; } else { // object id must be new! diff --git a/oxrest/service/OxRestObject.php b/oxrest/service/OxRestObject.php index 85066de..03585e3 100644 --- a/oxrest/service/OxRestObject.php +++ b/oxrest/service/OxRestObject.php @@ -66,7 +66,15 @@ public function returnOxObject($class = null, $oxid = null) $aObject['_oxaddress'] = $aAddresses; break; } - return new Response(200, $aObject); + $jsonEncode = json_encode($aObject); + + if(false === $jsonEncode) { + + $jsonErrormsg = json_last_error_msg(); + return new Response(404, "jsonErrormsg: ". $jsonErrormsg); + } + + return new Response(200, $jsonEncode, array('content-type' => 'application/json')); } } catch (Exception $ex) { @@ -74,11 +82,12 @@ public function returnOxObject($class = null, $oxid = null) return new Response(404, "Not found"); } - + /** * Saving object data back to DB * @method PUT * @json + * @provides application/json * @hasRwAccessRights * @param string $class * @priority 3 @@ -91,8 +100,14 @@ public function saveObject($class) if ($this->request->data && $this->request->data != "") { $aAddonData = null; $aData = $this->request->data; + + if (false == is_array($aData)) { + $aData = json_decode($aData); + } + // convert stdObj to array $aObjData = $this->_objectToArray($aData); + foreach ($aObjData as $k => $v) { if (substr($k, 0, 1) === "_") { $aAddonData[substr($k, 1)] = $v; @@ -122,7 +137,7 @@ public function saveObject($class) // no OXID, create new object if (!isset($addOxid) || $addOxid == '') { // create new OXID - $addOxid = oxRegistry::get('oxUtilsObject')->generateUId(); + $addOxid = \OxidEsales\Eshop\Core\Registry::getUtilsObject()->generateUID(); $data['oxid'] = $addOxid; // assign new array data $oxAddObj->assign($data); @@ -145,14 +160,22 @@ public function saveObject($class) } } - return new Response(200, $aObject); + $jsonEncode = json_encode($aObject); + + if(false === $jsonEncode) { + + $jsonErrormsg = json_last_error_msg(); + return new Response(404, "jsonErrormsg: ". $jsonErrormsg); + } + + return new Response(200, $jsonEncode, array('content-type' => 'application/json')); } } } } catch (Exception $ex) { $this->_doLog("Error saving object: " . $ex->getMessage()); } - return new Response(500); + return new Response(500, (__METHOD__." ".__LINE__."
".PHP_EOL)); } /** @@ -171,6 +194,11 @@ public function addObject($class) if ($this->request->data && $this->request->data != "") { $aAddonData = null; $aData = $this->request->data; + + if (false == is_array($aData)) { + $aData = json_decode($aData); + } + // convert stdObj to array $aObjData = $this->_objectToArray($aData); foreach ($aObjData as $k => $v) { @@ -184,7 +212,7 @@ public function addObject($class) $oxObj = oxNew($class); if (!isset($sOxid) || $sOxid == '') { // create new OXID - $sOxid = oxRegistry('oxUtilsObject')->generateUId(); + $sOxid = \OxidEsales\Eshop\Core\Registry::getUtilsObject()->generateUID(); $aObjData['oxid'] = $sOxid; } else { // object id must be new! @@ -209,7 +237,7 @@ public function addObject($class) // no OXID, create new object if (!isset($addOxid) || $addOxid == '') { // create new OXID - $addOxid = oxRegistry::get('oxUtilsObject')->generateUId(); + $addOxid = \OxidEsales\Eshop\Core\Registry::getUtilsObject()->generateUID(); $data['oxid'] = $addOxid; // assign new array data $oxAddObj->assign($data); @@ -229,15 +257,23 @@ public function addObject($class) } } } - return new Response(200, $aObject); + $jsonEncode = json_encode($aObject); + + if(false === $jsonEncode) { + + $jsonErrormsg = json_last_error_msg(); + return new Response(404, "jsonErrormsg: ". $jsonErrormsg); + } + + return new Response(200, $jsonEncode, array('content-type' => 'application/json')); } } } catch (Exception $ex) { $this->_doLog("Error saving new object: " . $ex->getMessage()); } - return new Response(500); + return new Response(500, (__METHOD__." ".__LINE__."
".PHP_EOL)); } - + /** * @method DELETE * @json