Connect. Communicate. Collaborate. Securely.

Home » Kerio User Forums » Kerio Workspace » Invoke Search by URL (Any way to launch a Kerio search via url link?)
  •  
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
Is there any way to launch a Kerio search by url? The example might be "docs.servername.com/#search-some string of text i want to find" which then loads results for the search of "some string of text i want to find"

Joshua Giese
  •  
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
To follow up I have been able to perform a login and logout from a remote server via PHP and CURL [posted below] but when I try to invoke the search it complains about the CSRF which should be in my cookies file. Anyone do any work with this before? No API is absolutely absurd I can't justify purchasing this until it has one, or I can create one....

The result I get on the search invoke is the following

{"error":{"message":"Missing or invalid CSRF token","code":"INTERNAL_ERROR"}}

You will see below I am trying to avoid the CSRF by passing the tokens in the headers but no go.

(the ! marks in the links are because I can't post links yet.)


/*********************************************************** **************/

$curl_options = array(
CURLOPT_RETURNTRANSFER => true, /* return web page */
CURLOPT_HEADER => false, /* don't return headers */
CURLOPT_FOLLOWLOCATION => true, /* follow redirects */
CURLOPT_ENCODING => "", /* handle all encodings */
CURLOPT_AUTOREFERER => true, /* set referer on redirect */
CURLOPT_CONNECTTIMEOUT => 120, /* timeout on connect */
CURLOPT_TIMEOUT => 120, /* timeout on response */
CURLOPT_MAXREDIRS => 10, /* stop after 10 redirects */
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => "Kerio Workspace Client/2.0.1-505"
);

$cookie = "/tmp/cookies.txt";
if ( $ch = curl_init() )
{
curl_setopt_array($ch,$curl_options);
if ( $cookie )
{
echo "<h2>Login</h2><p>";

$post = "kerio_username=USERNAME&kerio_password=PASSWORD";
$url = "http!://docs.server.com/server/login";
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($post))
);
echo $r = curl_exec($ch);
echo "<p><br /><p>";
echo print_r(curl_getinfo($ch));

echo "<p><br /><p>";
echo "<h2>Search</h2><p>";

$arr = array("method"=>"Search","params"=>array("query"=>"earthlink ","limit"=>"20"));
$post = json_encode($arr);
$cookies = parseCookies($cookie);
//echo "<pre>" . print_r($cookies,true) . "</pre>";
$url = "http!://docs.server.com/server/data";
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json-rpc; charset=UTF-8',
'Cookie: SESSION_WORKSPACE=' . $cookies[1][6],
'Content-Length: ' . strlen($post),
'X-Token: ' . $cookies[2][6])
);
echo $r = curl_exec($ch);
echo "<p><br /><p>";
echo print_r(curl_getinfo($ch));

echo "<p><br /><p>";
echo "<h2>Logout</h2><p>";

$arr = array("method"=>"Logout");
$post = json_encode($arr);
$url = "http!://docs.server.com/server/data";
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json-rpc; charset=UTF-8',
'Content-Length: ' . strlen($post))
);
echo $r = curl_exec($ch);
echo "<p><br /><p>";
echo print_r(curl_getinfo($ch));
}
curl_close($ch);
}

function parseCookies($file){
$lines = file($file);
foreach($lines as $line) {

// we only care for valid cookie def lines
if($line[0] != '#') {

// get tokens in an array
$tokens[] = array_map('trim',explode("\t", $line));
}

}
return $tokens;
}

[Updated on: Thu, 10 January 2013 15:51]


Joshua Giese
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
Hello.

this could be done using the API. Kerio Workspace has ability to read and write data in the store remotely, using JSON-RPC and HTTP.

If you are interested in more, please visit our Kerio Developer Zone and check out the Kerio APIs Client Library for PHP.

Main target is Kerio Connect but with a small piece of work you will be able to communicate also with your Kerio Workspace easily in PHP.

For example, you can search on Kerio Workspace by API method Search where param query is your search keyword.

/* Code snippet */
$params = array('query' => 'What am I looking for?');
$result = $api->sendRequest('Search', $params);


I have prepared a small example in PHP for you using our kerio-api-php library. See the files bellow.

Because Kerio Workspace can send chunked data in the response, please replace file src/KerioWorkspaceApi.php file with that one I've attached. It will be released in the next kerio-api-php release.

Feel free to send me a reply with any help or have any questions!

Best regards,

  • Attachment: example.zip
    (Size: 3.77KB, Downloaded 230 times)

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
The code snipped is also available here: https://gist.github.com/0d69229c737284c39169

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
Miroslav Osladil (Kerio) wrote on Fri, 11 January 2013 09:43
Because Kerio Workspace can send chunked data in the response, please replace file src/KerioWorkspaceApi.php file with that one I've attached. It will be released in the next kerio-api-php release.


One more thing... I forgot to add the main socket class with the chunked data support (src/class/KerioApiSocket.php). I'm sorry, my bad.


Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
Thanks for the reply. The next question then becomes how do I delete a file and how do I upload a file? We got exactly what we needed out of the API files you provided.. Great thanks BTW. But we are not sure exactly how we would upload to specific Folders. Or delete a file.

Joshua Giese
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
The kerio-api-php library has ability to uploadFile() and downloadFile().

You need to specify the item id of the folder or file library when uploading a file.

When deleting a file, the file has an item id, e.g. 123 which is used then in query params.
$api->sendRequest('RemoveItem', array('id' => 123));

If you are interested in more I would like to invite you to our API forum which is currently under Kerio Betatesting program. We can there share more deep information as there is no official Kerio Workspace API documentation at this moment.

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
I really appreciate the info on the beta program I filled out the form right away! Also any chance you have recommendations on where to find what parameters the search method (and others) accepts? I have up to this point been un-compiling the java classes and watching the traffic packets to discover how kerio works natively to use the API. As you can imagine it's a pain.

[Updated on: Sat, 12 January 2013 16:04]


Joshua Giese
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
Unfortunately there is no documentation I can share with you right now.

The only params you can specify in Search method are:

- query - The query you search for
- spaceList - List of spaces you search in. Default is whole server

For example:
$query = array(
    'query' => 'Apples and bananas',
    'spaceList' => array(333,666,999)
);

The code above will search for apples and bananas in spaces with id 333, 666 and 999.

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
All working good so far! One issue I'm seeing is when Kerio tries to return a large dataset to the API I am getting this error "Invalid JSON data, cannot parse response."

For example a search for "1039" in our actual Kerio system generates 55 results. If I do "1039 Ecology" I get 2 results. Through the API the search for "1039" gives me "Invalid JSON data, cannot parse response." but the search for "1039 Ecology" gives me the same 2 results.

Attached is my file that is generating the error. It is almost identical to what you provided.


Joshua Giese
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
Please, try enable debug mode by

$api->setDebug(TRUE);

and check the response where it failed. I guess there might be some issue with chunked data in the response.

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
Attached is the debug. Looks like all the data is there but something happens along the way?

The KerioException is what generates the JSON error so I'm not sure that it's a PHP issue...?

Is it possible that a character in document descriptions is making JSON mad? I ran the response in the debug against http://www.jsonlint.com and there were 3 descriptions that we're upsetting it.

I should add we are running OCR against documents as they are routed to make them searchable PDFs in Kerio.

Is there some cleansing that needs to be added?
If so is there something I can do with them on my end, or is it something that needs to be done in the Java classes?

[Updated on: Thu, 17 January 2013 15:14]


Joshua Giese
  •  
Miroslav Osladil (Kerio)

Messages: 188

Karma: 27
Send a private message to this user
Yes, the issue is caused by chunked response. Just to be sure, have you updated the KerioApiSocket.php attached in previous comment?

Miroslav Osladil
Senior Developer
Kerio Technologies

The views I express are my own and do not necessarily reflect the views of Kerio Technologies.
  •  
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
Just to verify I replaced it on the server and no change in behavior.

I found this line in the API file I suppose it could very well be a PHP issue.

/* Decode JSON response */
$response = json_decode($content, TRUE);
if (($method == 'POST') && empty($response)) {
throw new KerioApiException('Invalid JSON data, cannot parse response.');
}

I am going to clean the "content" of everything but letters and numbers before json_decode gets it and see what happens.

[Updated on: Thu, 17 January 2013 15:02]


Joshua Giese
WCI IT

Messages: 10
Karma: 1
Send a private message to this user
In case there is any interest for others out there I created a quick 'syntax highlighter' like function for the search results. Just pass the search terms and returned description through it.

/* Perform 'syntax' like highlighting */
function doHighlight($search,$string,$highlightColor="yellow"){
/* Function level vars */
$searchArr = explode(" ",$search);
$out = "";
$ignoreArr = array("OR","||","AND","&&","+","-");

/* Loop search terms to highlight */
foreach($searchArr as $i => $word){
if(!in_array($word,$ignoreArr)){
$string = str_replace($word,"<span style='background: {$highlightColor};'>{$word}</span>",$string);
}
}

/* Return completed string */
return $string;
}

[Updated on: Thu, 17 January 2013 16:46]


Joshua Giese
Previous Topic: Corrupt Page Deletion
Next Topic: Windows XP 64 bit
Goto Forum:
  


Disclaimer:
Kerio discussion forums are intended for open communication between forum members and may contain information and material posted by members which may be useful in learning about Kerio products. The discussion forums are not intended to provide technical support for any specific product. Any information implied or expressed in the discussion forums is that of the posting member. Kerio is in no way responsible for the information posted in the forums, or its accuracy. Kerio employees may participate in the discussions, but their postings do not represent an offical position of the company on any issues raised or discussed. Kerio reserves the right to monitor and maintain the forums to promote free and accurate exchange of information.

Current Time: Sun Sep 24 23:20:07 CEST 2017

Total time taken to generate the page: 0.00572 seconds
.:: Contact :: Home ::.
Powered by: FUDforum 3.0.4.