Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat(package): analysis of packages with Orion webservice
Browse files Browse the repository at this point in the history
  • Loading branch information
btry authored and ajsb85 committed Oct 26, 2017
1 parent 1a7a7fb commit fbf2054
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 56 deletions.
8 changes: 6 additions & 2 deletions hook.php
Expand Up @@ -63,7 +63,11 @@ function plugin_flyvemdm_uninstall() {
function plugin_flyvemdm_postinit() {
$plugin = new Plugin();
if ($plugin->isActivated('orion')) {
PluginOrionItem_Report::registerItemtype(PluginFlyvemdmPackage::class);
try {
PluginOrionReport::registerItemtype(PluginFlyvemdmPackage::class);
} catch (Exception $e) {
Toolbox::logInFile('php-errors', $e->getMessage());
}
}
}

Expand Down Expand Up @@ -190,7 +194,7 @@ function plugin_flyvemdm_hook_entity_purge(CommonDBTM $item) {
*
* @param CommonDBTM $item
*/
function plugin_flyvemdm_computer_purge(CommonDBTM $item) {
function plugin_flyvemdm_hook_computer_purge(CommonDBTM $item) {
$geolocation = new PluginFlyvemdmGeolocation();
$geolocation->hook_computer_purge($item);
$agent = new PluginFlyvemdmAgent();
Expand Down
13 changes: 9 additions & 4 deletions inc/agent.class.php
Expand Up @@ -293,24 +293,29 @@ public static function showDangerZone(PluginFlyvemdmAgent $item) {
* @return string an html with the agents
*/
public static function showForFleet(PluginFlyvemdmFleet $item) {
$items_id = $item->getField('id');

if (isset($_GET["start"])) {
$start = intval($_GET["start"]);
} else {
$start = 0;
}

$dbUtils = new DbUtils();

// Total Number of agents
$number = countElementsInTableForMyEntities(static::getTable(), ['plugin_flyvemdm_fleets_id' => $items_id ]);
$items_id = $item->getField('id');
$itemFk = $item::getForeignKeyField();
$number = $dbUtils->countElementsInTableForMyEntities(
static::getTable(),
[$itemFk => $items_id]
);

// get the pager
$pager = Html::printAjaxPager(self::getTypeName(1), $start, $number, '', false);
$pager = ''; // disabled because the results are not paged yet

// get items
$condition = "`plugin_flyvemdm_fleets_id` = '$items_id' " . getEntitiesRestrictRequest();
$agent = new static();
$condition = "`$itemFk` = '$items_id' " . $dbUtils->getEntitiesRestrictRequest();
$rows = $agent->find($condition, '', '');

$data = [
Expand Down
2 changes: 1 addition & 1 deletion inc/config.class.php
Expand Up @@ -42,7 +42,7 @@ class PluginFlyvemdmConfig extends CommonDBTM {
const RESERVED_TYPE_RANGE_MIN = 11000;
const RESERVED_TYPE_RANGE_MAX = 11049;

const PLUGIN_FLYVEMDM_MQTT_CLIENT = "flyvemdm";
const PLUGIN_FLYVEMDM_MQTT_CLIENT = 'flyvemdm';

static $config = [];

Expand Down
6 changes: 4 additions & 2 deletions inc/graph.class.php
Expand Up @@ -43,12 +43,14 @@ class PluginFlyvemdmGraph extends CommonDBTM
public function showInvitationsGraph() {
$out = '';

$pendingCount = countElementsInTableForMyEntities(
$dbUtils = new DbUtils();

$pendingCount = $dbUtils->countElementsInTableForMyEntities(
PluginFlyvemdmInvitation::getTable(),
"`status` = 'pending'"
);

$doneCount = countElementsInTableForMyEntities(
$doneCount = $dbUtils->countElementsInTableForMyEntities(
PluginFlyvemdmInvitation::getTable(),
"`status` = 'done'"
);
Expand Down
62 changes: 34 additions & 28 deletions inc/package.class.php
Expand Up @@ -76,8 +76,12 @@ public static function getMenuPicture() {
public function defineTabs($options = []) {
$tab = [];
$this->addDefaultFormTab($tab);
$this->addStandardTab('Notepad', $tab, $options);
$this->addStandardTab('Log', $tab, $options);
$plugin = new Plugin();
if ($plugin->isActivated('orion')) {
$this->addStandardTab(PluginOrionReport::class, $tab, $options);
}
$this->addStandardTab(Notepad::class, $tab, $options);
$this->addStandardTab(Log::class, $tab, $options);

return $tab;
}
Expand Down Expand Up @@ -117,7 +121,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
* @param integer $ID ID of the item to show
* @param array $options
*/
public function showForm($ID, $options = []) {
public function showForm($ID, $options=[]) {
$this->initForm($ID, $options);
$this->showFormHeader($options);

Expand Down Expand Up @@ -188,8 +192,8 @@ public function prepareInputForAdd($input) {
}
try {
$uploadedFile = $preparedFile['uploadedFile'];
$input['filename'] = $this->fields['entities_id'] . '/' . uniqid() . '_' . basename($uploadedFile);
$destination = FLYVEMDM_PACKAGE_PATH . '/' . $input['filename'];
$input['filename'] = 'flyvemdm/package/' . $this->fields['entities_id'] . '/' . uniqid() . '_' . basename($uploadedFile);
$destination = GLPI_DOC_DIR . '/' . $input['filename'];
$this->createEntityDirectory(dirname($destination));
if (rename($uploadedFile, $destination)) {
$input['filesize'] = fileSize($destination);
Expand Down Expand Up @@ -222,15 +226,15 @@ public function prepareInputForUpdate($input) {
return false;
}
$uploadedFile = $preparedFile['uploadedFile'];
$input['filename'] = $this->fields['entities_id'] . "/" . uniqid() . "_" . basename($uploadedFile);
$destination = FLYVEMDM_PACKAGE_PATH . "/" . $input['filename'];
$input['filename'] = 'flyvemdm/package/' . $this->fields['entities_id'] . "/" . uniqid() . "_" . basename($uploadedFile);
$destination = GLPI_DOC_DIR . '/' . $input['filename'];
$this->createEntityDirectory(dirname($destination));
if (rename($uploadedFile, $destination)) {
$filename = pathinfo($destination, PATHINFO_FILENAME);
$input['filesize'] = fileSize($destination);
$input['dl_filename'] = $filename;
$input['dl_filename'] = basename($destination);
if ($filename != $this->fields['filename']) {
unlink(FLYVEMDM_PACKAGE_PATH . "/" . $this->fields['filename']);
unlink(GLPI_DOC_DIR . "/" . $this->fields['filename']);
}
} else {
$this->logErrorIfDirNotWritable($destination);
Expand Down Expand Up @@ -260,25 +264,19 @@ public function post_getFromDB() {
}

public function post_addItem() {
$this->createOrionTask();
$this->createOrionReport();
}

/**
* Create a file analysis task with the Orion plugin
*/
private function createOrionTask() {
private function createOrionReport() {
$plugin = new Plugin();
if ($plugin->isActivated('orion')) {
$filename = FLYVEMDM_PACKAGE_PATH . "/" . $this->getField('filename');

// Crop the GLPI_DOC_DIR prefix
if (substr($filename, 0, strlen(GLPI_DOC_DIR)) == GLPI_DOC_DIR) {
$filename = substr($filename, strlen(GLPI_DOC_DIR));
}

$orionTask = new PluginOrionReport();
$orionTask->add([
'filename' => $filename,
$orionReport = new PluginOrionReport();
$orionReport->add([
'itemtype' => $this->getType(),
'items_id' => $this->getID(),
]);
}
}
Expand Down Expand Up @@ -307,6 +305,8 @@ public function post_updateItem($history = 1) {
}
}
}

$this->createOrionReport();
}

/**
Expand Down Expand Up @@ -425,7 +425,7 @@ public function getFileURL() {
* Sends a file
*/
protected function sendFile() {
$streamSource = FLYVEMDM_PACKAGE_PATH . "/" . $this->fields['filename'];
$streamSource = GLPI_DOC_DIR . "/" . $this->fields['filename'];

if (!file_exists($streamSource) || !is_file($streamSource)) {
header("HTTP/1.0 404 Not Found");
Expand Down Expand Up @@ -665,12 +665,6 @@ private function prepareFileUpload() {
ERROR);
return false;
}

// With GLPI < 9.2, the file was not moved by the API
if (!move_uploaded_file($fileTmpName, $destination)) {
Session::addMessageAfterRedirect(__('Failed to save the file', 'flyvemdm'));
return false;
}
}

$actualFilename = $fileName;
Expand All @@ -679,4 +673,16 @@ private function prepareFileUpload() {

return ['uploadedFile' => $uploadedFile, 'filename' => $actualFilename];
}

/**
* Gets the filename of a package
* @return string|NULL the path to tie file relative to the DOC ROOT àf GLPI
*/
public function getFilename() {
if (!$this->isNewItem()) {
return $this->fields['filename'];
}

return null;
}
}
32 changes: 16 additions & 16 deletions install/installer.class.php
Expand Up @@ -229,22 +229,22 @@ protected function createFirstAccess() {
$profileRight = new ProfileRight();

$newRights = [
PluginFlyvemdmProfile::$rightname => PluginFlyvemdmProfile::RIGHT_FLYVEMDM_USE,
PluginFlyvemdmInvitation::$rightname => CREATE | READ | DELETE | PURGE,
PluginFlyvemdmAgent::$rightname => READ | UPDATE | PURGE | READNOTE | UPDATENOTE,
PluginFlyvemdmFleet::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmPackage::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmFile::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmGeolocation::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmPolicy::$rightname => READ,
PluginFlyvemdmPolicyCategory::$rightname => READ,
PluginFlyvemdmWellknownpath::$rightname => ALLSTANDARDRIGHT,
PluginFlyvemdmEntityconfig::$rightname => READ
| PluginFlyvemdmEntityconfig::RIGHT_FLYVEMDM_DEVICE_COUNT_LIMIT
| PluginFlyvemdmEntityconfig::RIGHT_FLYVEMDM_APP_DOWNLOAD_URL
| PluginFlyvemdmEntityconfig::RIGHT_FLYVEMDM_INVITATION_TOKEN_LIFE,
PluginFlyvemdmInvitationLog::$rightname => READ,
PluginFlyvemdmTaskstatus::$rightname => READ,
PluginFlyvemdmProfile::$rightname => PluginFlyvemdmProfile::RIGHT_FLYVEMDM_USE,
PluginFlyvemdmInvitation::$rightname => CREATE | READ | DELETE | PURGE,
PluginFlyvemdmAgent::$rightname => READ | UPDATE | PURGE | READNOTE | UPDATENOTE,
PluginFlyvemdmFleet::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmPackage::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmFile::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmGeolocation::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
PluginFlyvemdmPolicy::$rightname => READ,
PluginFlyvemdmPolicyCategory::$rightname => READ,
PluginFlyvemdmWellknownpath::$rightname => ALLSTANDARDRIGHT,
PluginFlyvemdmEntityconfig::$rightname => READ
| PluginFlyvemdmEntityconfig::RIGHT_FLYVEMDM_DEVICE_COUNT_LIMIT
| PluginFlyvemdmEntityconfig::RIGHT_FLYVEMDM_APP_DOWNLOAD_URL
| PluginFlyvemdmEntityconfig::RIGHT_FLYVEMDM_INVITATION_TOKEN_LIFE,
PluginFlyvemdmInvitationLog::$rightname => READ,
PluginFlyvemdmTaskstatus::$rightname => READ,
];

$profileRight->updateProfileRights($_SESSION['glpiactiveprofile']['id'], $newRights);
Expand Down
3 changes: 2 additions & 1 deletion install/upgrade/update_to_dev.php
Expand Up @@ -39,7 +39,7 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {
$migration->setVersion(PLUGIN_FLYVEMDM_VERSION);

// update Entity config table
$table = PluginFlyvemdmEntityconfig::getTable();
$table = 'glpi_plugin_flyvemdm_entityconfigs';
$migration->addField($table, 'support_name', 'text', ['after' => 'agent_token_life']);
$migration->addField($table, 'support_phone', 'string', ['after' => 'support_name']);
$migration->addField($table, 'support_website', 'string', ['after' => 'support_phone']);
Expand Down Expand Up @@ -121,6 +121,7 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {
['after' => 'dl_filename', 'default' => 'pending']);
$migration->addKey($table, 'entities_id', 'entities_id');
$migration->addPostQuery("UPDATE `$table` SET `parse_status` = 'parsed'");
$migration->addPostQuery("UPDATE `$table` SET `filename` = CONCAT('" . addslashes(GLPI_DOC_DIR) . "', `filename`)");
$migration->addfield($table, 'plugin_orion_tasks_id', 'integer', ['after' => 'dl_filename']);

$table = 'glpi_plugin_flyvemdm_files';
Expand Down
2 changes: 1 addition & 1 deletion setup.php
Expand Up @@ -149,7 +149,7 @@ function plugin_flyvemdm_addHooks() {
];
$PLUGIN_HOOKS['item_purge']['flyvemdm'] = [
Entity::class => 'plugin_flyvemdm_hook_entity_purge',
Computer::class => 'plugin_flyvemdm_computer_purge',
Computer::class => 'plugin_flyvemdm_hook_computer_purge',
];
$PLUGIN_HOOKS['pre_item_purge']['flyvemdm'] = [
PluginFlyvemdmInvitation::class => [PluginFlyvemdmInvitation::class, 'hook_pre_self_purge'],
Expand Down
5 changes: 4 additions & 1 deletion tpl/package.html
@@ -1,7 +1,10 @@
{% if not isNewID %}
<tr class='tab_bg_1'>
<td colspan="4">
<img width="64px" height="64px" src="data:image/png;base64,{{ package.icon }}" />
{% if not package.icon %}
{% else %}
<img width="64px" height="64px" src="data:image/png;base64,{{ package.icon }}" />
{% endif %}
</td>
</tr>
{% endif %}
Expand Down

0 comments on commit fbf2054

Please sign in to comment.