public function read(\Model $model, array $queryData) { $request = $this->{'_' . $model->findQueryType . $this->config['authMethod']}($queryData); if (!empty($queryData['options'])) { $request = Hash::merge($request, $queryData['options']); } $response = $this->Http->request($request); if ($response->isOK()) { $json = array('application/json', 'application/javascript', 'text/javascript'); $contentType = explode(';', $response->getHeader('Content-Type')); if (in_array($contentType[0], $json)) { return json_decode($response->body(), true); } else { parse_str($response->body(), $token); return $token; } } else { $model->onError(); return false; } }
/** * The "C" in CRUD * * @param Model $model * @param array $fields containing the field names * @param array $values containing the fields' values * @return true on success, false on error */ function create(&$model, $fields = null, $values = null) { $basedn = $this->config['basedn']; $key = $model->primaryKey; $table = $model->useTable; $fieldsData = array(); $id = null; $objectclasses = null; if ($fields == null) { unset($fields, $values); $fields = array_keys($model->data); $values = array_values($model->data); } $count = count($fields); for ($i = 0; $i < $count; $i++) { if ($fields[$i] == $key) { $id = $values[$i]; } elseif ($fields[$i] == 'cn') { $cn = $values[$i]; } $fieldsData[$fields[$i]] = $values[$i]; } //Lets make our DN, this is made from the useTable & basedn + primary key. Logically this corelate to LDAP if (isset($table) && preg_match('/=/', $table)) { $table = $table . ', '; } else { $table = ''; } if (isset($key) && !empty($key)) { $key = "{$key}={$id}, "; } else { //Almost everything has a cn, this is a good fall back. $key = "cn={$cn}, "; } $dn = $key . $table . $basedn; $res = @ldap_add($this->database, $dn, $fieldsData); // Add the entry if ($res) { $model->setInsertID($id); $model->id = $id; return true; } else { $this->log("Failed to add ldap entry: dn:{$dn}\nData:" . print_r($fieldsData, true) . "\n" . ldap_error($this->database), 'ldap.error'); $model->onError(); return false; } }
public function onError() { parent::onError(); // == throw Expection if ($this->getDataSource()->config['throw_exception']) { $message = $this->getDataSource()->Http->response['body']; if (is_array($this->response) && !empty($this->response['error'])) { $message = $this->response['error']; } else { if (is_array($this->response) && !empty($this->response['errors'][0]['message'])) { $message = $this->response['errors'][0]['message']; } else { if ($this->getDataSource()->Http->response['status']['code'] == 503) { $message = __d('twim', 'Twitter is over capacity.'); } else { if ($this->getDataSource()->Http->response['status']['code'] == 404) { $message = sprintf('The requested URL %s was not found.', $this->getDataSource()->Http->url($this->getDataSource()->Http->request['uri'])); } } } } throw new RuntimeException($message, $this->getDataSource()->Http->response['status']['code']); } }
public function afterRequest(Model &$model, HttpSocketResponse &$response) { if (!$response->isOk()) { $model->onError(); return false; } else { return $this->decode($response); } }
/** * The "C" in CRUD * * @param Model $model * @param array $fields containing the field names * @param array $values containing the fields' values * @return true on success, false on error */ function create(&$model, $fields = null, $values = null) { $fieldsData = array(); $id = null; $objectclasses = null; if ($fields == null) { unset($fields, $values); $fields = array_keys($model->data); $values = array_values($model->data); } $count = count($fields); for ($i = 0; $i < $count; $i++) { if ($fields[$i] == '_DN_') { $id = $values[$i]; } else { $fieldsData[$fields[$i]] = $values[$i]; } } if (!$id) { $model->onError(); return false; } // Add the entry if (@ldap_add($this->connection, $id, $fieldsData)) { return true; } else { $model->onError(); return false; } }