Skip to content

Instantly share code, notes, and snippets.

@bassauer-storms-media
Last active July 18, 2018 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bassauer-storms-media/f030da19826b47f0d1b9273f7c80df9a to your computer and use it in GitHub Desktop.
Save bassauer-storms-media/f030da19826b47f0d1b9273f7c80df9a to your computer and use it in GitHub Desktop.
<?php
// index.php
require_once('libs/CockpitHelper.class.php');
CockpitHelper::$_COCKPIT_RESTAPI_TOKEN = ''; // set this if you are not using public access to entries
CockpitHelper::$_COCKPIT_BACKEND_URL = 'http://cockpit.yourdomain.com';
// ---------------------------
/**
* libs/CockpitHelper.class.php
*/
class CockpitHelper {
public static $_COCKPIT_RESTAPI_TOKEN = null;
public static $_COCKPIT_BACKEND_URL = null;
/*
* get the full path to a resource with external cockpit url
*/
public static function getFullAssetPath($path) {
return sprintf('%s/storage/uploads/%s', self::$_COCKPIT_BACKEND_URL, $path);
}
/*
* get all entries of an collection detirmined by the name of the collection
*/
public static function getCollectionEntries($collectionName='News', $filter=[], $limit = 0) {
$filter_processed = [];
foreach($filter as $key => $val) $filter_processed[] = "&filter[$key]=$val";
$data = json_decode(file_get_contents(self::$_COCKPIT_BACKEND_URL . '/api/collections/get/'.$collectionName.'?token=' . self::$_COCKPIT_RESTAPI_TOKEN . '&sort[_o]=1' . $filter_processed . '&limit='.$limit), true)['entries'];
/*array_walk_recursive($data, function(&$item) {
if (strpos($item, '/storage/uploads/') !== false) {
$item = str_replace('/storage/uploads/', CockpitHelper::$_COCKPIT_BACKEND_URL . '/storage/uploads/', $item);
}
});*/
return $limit===1 ? $data[0] : $data;
}
/*
* check if a user could be authenticated at cockpit with the given data
*/
public static function checkAuth($user, $pass){
$url = self::$_COCKPIT_BACKEND_URL . '/auth/check';
$fields = [
'auth' => [
'user' => $user,
'password' => $pass,
]
];
$data_string = json_encode($fields);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
return json_decode(curl_exec($ch), true);
}
/*
* get a list of collections a user has access to
*/
public static function getCollections($api_key=null) {
if(!$api_key)
$api_key = self::$_COCKPIT_RESTAPI_TOKEN;
return json_decode(file_get_contents(self::$_COCKPIT_BACKEND_URL . '/api/collections/listCollections?token=' . $api_key), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment