Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions oxrest/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Order Deny,Allow
Allow From All
Satisfy Any
5 changes: 3 additions & 2 deletions oxrest/oxrest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -47,14 +47,15 @@
$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) {
$response = new Tonic\Response(401, $e->getMessage());
} catch (Tonic\Exception $e) {
$response = new Tonic\Response($e->getCode(), $e->getMessage());
}

$response->output();
19 changes: 9 additions & 10 deletions oxrest/service/OxRestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 7 additions & 7 deletions oxrest/service/OxRestList.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,33 @@ public function getList($class)
$iLimitOffset = $iPerPage * $iCurPage;

$sSql = $sSql . " LIMIT $iLimitOffset,$iPerPage";

//$this->_doLog($sSql);

$l = array();
$list->selectString(
$sSql
);
foreach ($list->getArray() as $oxid => $oxObject) {
$l[$oxid] = $this->_oxObject2Array($oxObject);
}

$ret['numPages'] = $iNumPages;
$ret['curPage'] = $iCurPage;
$ret['numObjects'] = $cnt;
$ret['objectType'] = get_class($bo);
$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());
}

return new Response(404, "No data");
}

/**
* Saving list data back to DB
* @method PUT
Expand All @@ -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();
Expand Down Expand Up @@ -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!
Expand Down
56 changes: 46 additions & 10 deletions oxrest/service/OxRestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,28 @@ 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) {

}

return new Response(404, "Not found");
}

/**
* Saving object data back to DB
* @method PUT
* @json
* @provides application/json
* @hasRwAccessRights
* @param string $class
* @priority 3
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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__."<br>".PHP_EOL));
}

/**
Expand All @@ -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) {
Expand All @@ -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!
Expand All @@ -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);
Expand All @@ -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__."<br>".PHP_EOL));
}

/**
* @method DELETE
* @json
Expand Down