Skip to content

Commit

Permalink
added validation for Ids before calling communibase
Browse files Browse the repository at this point in the history
  • Loading branch information
fruitl00p committed Nov 17, 2014
1 parent 9439d68 commit caa55aa
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Communibase/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public function getById($entityType, $id, $params = array()) {
if (empty($id)) {
throw new Exception('Id is empty');
}
if (!$this->isIdValid($id)) {
throw new Exception('Id is invalid, please use a correctly formatted id');
}
$ch = $this->setupCurlHandle($entityType . '.json/crud/' . $id, $params);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
return $this->getResult($ch);
Expand Down Expand Up @@ -128,10 +131,13 @@ public function getByRef($ref, $parentEntity = array()) {
* @return array entities
*/
public function getByIds($entityType, $ids, $params = array()) {
if (empty($ids)) {
$validIds = array_filter($ids, array($this, 'isIdValid'));

if (empty($validIds)) {
return array();
}
return $this->search($entityType, array('_id' => array('$in' => $ids)), $params);

return $this->search($entityType, array('_id' => array('$in' => $validIds)), $params);
}

/**
Expand Down Expand Up @@ -392,4 +398,21 @@ private function getResult($ch) {

return $responseData;
}

/**
* @param string $id
*
* @return bool
*/
private function isIdValid($id) {
if (empty($id)) {
return false;
}

if (preg_match('#[0-9a-fA-F]{24}#', $id) === 0) {
return false;
}

return true;
}
}

0 comments on commit caa55aa

Please sign in to comment.