A little PHP Library for CmdBuild.
yours, auri
<?php
class PhpCmdBuild {
var $_u, $_p;
var $session;
public function __construct() {
$this->session = @file_get_contents("session.txt");
}
public function login($url, $user, $pass) {
$this->_url = $url;
$this->_u = $user;
$this->_p = $pass;
}
private function _checkOrReconnect() {
if ($this->session == null) { // first request
$this->_login();
}
if (!$this->_checkSession()) { // invalid Session
$this->_login();
}
if ($this->session == null) { // Connection error
die("CmdBuild Connection error");
}
}
private function _login() {
echo "login\n";
//$opts = array('http' => array('header' => 'Cookie: ' . $_SERVER['HTTP_COOKIE'] . "\r\n"));
$postdata = http_build_query(
array(
'username' => $this->_u,
'password' => $this->_p
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . '/services/json/login/login', false, $context), true);
if ($contents['success']) {
$this->session = str_replace('Set-', '', $http_response_header[2]);
echo "Session:" . $this->session;
file_put_contents("session.txt", $this->session);
return true;
}
return false;
}
private function _checkSession() {
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . '/services/json/schema/modsecurity/getuiconfiguration', false, $context), true);
if ($contents['success'])
return true;
else
return false;
}
public function getCardList($className, $page, $start, $limit) {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/management/modcard/getcardlist?className=$className&page=$page&start=$start&limit=$limit", false, $context), true);
if ($contents['success'])
return $contents['rows'];
else
return null;
}
public function getCard($className, $id) {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/management/modcard/getcardlist?className=$className&cardId=$id", false, $context), true);
if ($contents['success']) {
return $contents['rows'][0];
} else
return null;
}
public function getAttributeList($className) {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/schema/modclass/getattributelist?active=true&className=$className", false, $context), true);
if ($contents)
return $contents;
else
return null;
}
public function getAllClasses() {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/schema/modclass/getallclasses?active=true", false, $context), true);
if ($contents['success'])
return $contents['classes'];
else
return null;
}
public function getAllDomains() {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/schema/modclass/getalldomains?active=true", false, $context), true);
if ($contents['success'])
return $contents['domains'];
else
return null;
}
public function filterCardList($className, $filter, $page, $start, $limit) {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/management/modcard/getcardlist?className=$className&filter=$filter&page=$page&start=$start&limit=$limit", false, $context), true);
if ($contents['success'])
return $contents['rows'];
else
return null;
}
public function filterCardListBy($className, $attr, $type, $val, $page, $start, $limit) {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/management/modcard/getcardlist?className=$className&filter=" . '{"attribute":{"simple":{"attribute":"' . $attr . '","operator":"' . $type . '","value":["' . $val . '"],"parameterType":"fixed"}}}' . "&page=$page&start=$start&limit=$limit", false, $context), true);
if ($contents['success'])
return $contents['rows'];
else
return null;
}
public function updateCard($card) {
$card['cardId'] = $card['Id'];
unset($card['IdClass_value']);
unset($card['IdClass']);
unset($card['IdClass_value_default']);
unset($card['Id']);
$postdata = http_build_query($card);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => $this->session . "\n\r" . 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . '/services/json/management/modcard/updatecard', false, $context), true);
if ($contents['success']) {
return true;
}
return false;
}
public function createCard($className, $data) {
$card = array();
$card['cardId'] = -1;
$card['className'] = $className;
$card = array_merge($card, $data);
$postdata = http_build_query($card);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => $this->session . "\n\r" . 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . '/services/json/management/modcard/updatecard', false, $context), true);
if ($contents['success']) {
return $contents['id'];
}
return false;
}
public function deleteCard($idClass, $id) {
$card = array();
$card['Id'] = $id;
$card['IdClass'] = $idClass;
$postdata = http_build_query($card);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => $this->session . "\n\r" . 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . '/services/json/management/modcard/deletecard', false, $context), true);
if ($contents['success']) {
return true;
}
return false;
}
public function createRelations($rel, $class1, $id1,$class2, $id2) {
$card = array();
$card['domainName'] = $rel;
$card['master'] = "_1";
$card['attributes'] = '{"_1":[{"cardId":'.$id1.',"className":"'.$class1.'"}],"_2":[{"cardId":'.$id2.',"className":"'.$class2.'"}]}';
$postdata = http_build_query($card);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => $this->session . "\n\r" . 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . '/services/json/management/modcard/createrelations', false, $context), true);
print_r($contents);
if ($contents['success']) {
return true;
}
return false;
}
public function deleteRelation($rel, $relid) {
$card = array();
$card['domainName'] = $rel;
$card['relationId'] = $relid;
//$card['attributes'] = '{"_1":[{"cardId":'.$id1.',"className":"'.$class1.'"}],"_2":[{"cardId":'.$id2.',"className":"'.$class2.'"}]}';
$postdata = http_build_query($card);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => $this->session . "\n\r" . 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . '/services/json/management/modcard/deleterelation', false, $context), true);
print_r($contents);
if ($contents['success']) {
return true;
}
return false;
}
public function getRelationList($className, $id) {
$this->_checkOrReconnect();
$opts = array('http' =>
array(
'method' => 'GET',
'header' => $this->session . '\n\rContent-type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$contents = json_decode(file_get_contents($this->_url . "/services/json/management/modcard/getrelationlist?cardId=$id&className=$className&domainlimit=1000", false, $context), true);
if ($contents['success'])
return $contents['domains'];
else
return null;
}
}
/*
$a = new PhpCmdBuild();
//print_r($a->getAllClasses());
//print_r($a->getAllDomains());
//print_r($a->getAttributeList("Customer"));
//print_r($a->getCardList("Customer", 1, 0, 1));
//print_r($a->filterCardList("Customer",'{"query":"b"}', 1, 0, 100));
//print_r($a->filterCardList("Customer",'{"attribute":{"simple":{"attribute":"Description","operator":"contain","value":["b"],"parameterType":"fixed"}}}', 1, 0, 100));
//print_r($a->filterCardListBy("Customer", 'Code', 'contain', 'b', 1, 0, 100));
/*
$data = $a->getCardList("Customer", 1, 0, 1);
$card=$data[0];
$card['Description']=$card['Code']." updated now";
$a->updateCard($card);
$data1 = $a->filterCardListBy("Customer",'Code', 'contain',$card['Code'], 1, 0, 1);
$card1=$data1[0];
print_r($card1);
*/
/*
$data['Code'] = "d";
$data['Description'] = "d";
$id = $a->createCard("Customer", $data);
//print_r($a->getCard("Customer", $id));
$data1 = $a->filterCardListBy("Customer", 'Code', 'contain', 'd', 1, 0, 1);
$card1 = $data1[0];
$a->deleteCard($card1['IdClass'], $card1['Id']);
*/
/*
$data1['Code'] = "Class A Entry 1";
$data1['Description'] = "Class A Entry 1";
$id1 = $a->createCard("A", $data1);
$card1=$a->getCard("A", $id1);
$data2['Code'] = "Class B Entry 1";
$data2['Description'] = "Class B Entry 1";
$id2 = $a->createCard("B", $data2);
$card2=$a->getCard("B", $id2);
$a->createRelations("A2Brel", "A", $id1, "B", $id2);
$dom = $a->getRelationList("A", $id1);
$rel = $dom[0]['relations'][0];
//print_r($rel);
$a->deleteRelation("A2Brel", $rel['rel_id']);
$a->deleteCard($card1['IdClass'],$card1['Id']);
$a->deleteCard($card2['IdClass'],$card2['Id']);
*/