/**
  * Used to import package
  *
  */
 function importPackage($zipfile)
 {
     $zip = new ZipArchive();
     $pfDeployOrder = new PluginFusioninventoryDeployOrder();
     $pfDeployFile = new PluginFusioninventoryDeployFile();
     $filename = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/import/" . $zipfile;
     $extract_folder = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/import/" . $zipfile . ".extract";
     if ($zip->open($filename, ZipArchive::CREATE) == TRUE) {
         $zip->extractTo($extract_folder);
         $zip->close();
     }
     $json_string = file_get_contents($extract_folder . "/information.json");
     $a_info = json_decode($json_string, true);
     // Find package with this uuid
     $a_packages = $this->find("`uuid`='" . $a_info['package']['uuid'] . "'");
     if (count($a_packages) == 0) {
         // Create it
         $_SESSION['tmp_clone_package'] = true;
         $packages_id = $this->add($a_info['package']);
         unset($_SESSION['tmp_clone_package']);
         foreach ($a_info['orders'] as $input) {
             $input['plugin_fusioninventory_deploypackages_id'] = $packages_id;
             $pfDeployOrder->add($input);
             echo "|";
         }
         foreach ($a_info['files'] as $input) {
             $pfDeployFile->add($input);
         }
     } else {
         // Update current
     }
     // Copy files
     foreach ($a_info['manifests'] as $manifest) {
         rename($extract_folder . "/files/manifests/" . $manifest, GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/manifests/" . $manifest);
     }
     foreach ($a_info['repository'] as $repository) {
         $split = explode('/', $repository);
         array_pop($split);
         $folder = '';
         foreach ($split as $dir) {
             $folder .= '/' . $dir;
             if (!file_exists(GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/repository" . $folder)) {
                 mkdir(GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/repository" . $folder);
             }
         }
         rename($extract_folder . "/files/repository/" . $repository, GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/repository/" . $repository);
     }
 }
 static function removeFileInRepo($sha512, $orders_id)
 {
     $repoPath = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/repository/";
     $manifestsPath = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/manifests/";
     $order = new PluginFusioninventoryDeployOrder();
     $rows = $order->find("id != '{$orders_id}'\n            AND json LIKE '%" . substr($sha512, 0, 6) . "%'\n            AND json LIKE '%{$sha512}%'");
     if (count($rows) > 0) {
         //file found in other order, do not remove part in repo
         return FALSE;
     }
     //get current order json
     $datas = json_decode(PluginFusioninventoryDeployOrder::getJson($orders_id), TRUE);
     $multiparts = $datas['associatedFiles'][$sha512]['multiparts'];
     //parse all files part
     foreach ($multiparts as $part_sha512) {
         $dir = $repoPath . self::getDirBySha512($part_sha512) . '/';
         //delete file parts
         unlink($dir . $part_sha512);
     }
     return TRUE;
 }
 static function updateOrderJson($orders_id, $datas)
 {
     $order = new PluginFusioninventoryDeployOrder();
     $options = 0;
     if (version_compare(PHP_VERSION, '5.3.3') >= 0) {
         $options = $options | JSON_NUMERIC_CHECK;
     }
     if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
         $options = $options | JSON_UNESCAPED_SLASHES;
     }
     $json = json_encode($datas, $options);
     $json_error_consts = array(JSON_ERROR_NONE => "JSON_ERROR_NONE", JSON_ERROR_DEPTH => "JSON_ERROR_DEPTH", JSON_ERROR_STATE_MISMATCH => "JSON_ERROR_STATE_MISMATCH", JSON_ERROR_CTRL_CHAR => "JSON_ERROR_CTRL_CHAR", JSON_ERROR_SYNTAX => "JSON_ERROR_SYNTAX");
     if (version_compare(phpversion(), "5.3.3", "ge")) {
         $json_error_consts[JSON_ERROR_UTF8] = "JSON_ERROR_UTF8";
     }
     $error_json = json_last_error();
     if (version_compare(PHP_VERSION, '5.5.0', "ge")) {
         $error_json_message = json_last_error_msg();
     } else {
         $error_json_message = "";
     }
     $error = 0;
     if ($error_json != JSON_ERROR_NONE) {
         $error_msg = "";
         $error_msg = $json_error_consts[$error_json];
         Session::addMessageAfterRedirect(__("The modified JSON contained a syntax error :", "fusioninventory") . "<br/>" . $error_msg . "<br/>" . $error_json_message, FALSE, ERROR, FALSE);
         $error = 1;
     } else {
         $error = $order->update(array('id' => $orders_id, 'json' => Toolbox::addslashes_deep($json)));
     }
     return $error;
 }
    } else {
        $json_response['success'] = FALSE;
        $json_response['reason'] = __('Package modification is forbidden by your profile.');
    }
    echo json_encode($json_response);
    exit;
}
if (!isset($_REQUEST['orders_id']) && !isset($_REQUEST['rand']) && !isset($_REQUEST['subtype'])) {
    exit;
}
if (!is_numeric($_REQUEST['orders_id'])) {
    Toolbox::logDebug("Error: orders_id in request is not an integer");
    Toolbox::logDebug(var_dump($_REQUEST['orders_id']));
    exit;
}
$order = new PluginFusioninventoryDeployOrder();
$order->getFromDB($_REQUEST['orders_id']);
//TODO: In the displayForm function, $_REQUEST is somewhat too much for the '$datas' parameter
// I think we could use only $order -- Kevin 'kiniou' Roy
switch ($_REQUEST['subtype']) {
    case 'check':
        PluginFusioninventoryDeployCheck::displayForm($order, $_REQUEST, $_REQUEST['rand'], $_POST['mode']);
        break;
    case 'file':
        PluginFusioninventoryDeployFile::displayForm($order, $_REQUEST, $_REQUEST['rand'], $_POST['mode']);
        break;
    case 'action':
        PluginFusioninventoryDeployAction::displayForm($order, $_REQUEST, $_REQUEST['rand'], $_POST['mode']);
        break;
    case 'package_json_debug':
        if (isset($order->fields['json'])) {
 function run($taskjobstate)
 {
     //get order type label
     $order_type_label = str_replace("deploy", "", $taskjobstate->method);
     //get numeric order type from label
     $order_type = PluginFusioninventoryDeployOrder::getRender($order_type_label);
     //get order by type and package id
     $order = new PluginFusioninventoryDeployOrder($order_type, $taskjobstate->fields['items_id']);
     //decode order data
     $order_data = json_decode($order->fields['json'], TRUE);
     /* TODO:
      * This has to be done properly in each corresponding classes.
      * Meanwhile, I just split the data to rebuild a proper and compliant JSON
      */
     $order_job = $order_data['jobs'];
     //add uniqid to response data
     $order_job['uuid'] = $taskjobstate->fields['uniqid'];
     /* TODO:
      * Orders should only contain job data and associatedFiles should be retrieved from the
      * list inside Orders data like the following :
      *
      * $order_files = array()
      * foreach($order_job["associatedFiles"] as $hash) {
      *    if (!isset($order_files[$hash]) {
      *       $order_files[$hash] = PluginFusioninventoryDeployFile::getByHash($hash);
      *       $order_files[$hash]['mirrors'] = $mirrors
      *    }
      * }
      */
     $order_files = $order_data['associatedFiles'];
     //Add mirrors to associatedFiles
     $mirrors = PluginFusioninventoryDeployMirror::getList($taskjobstate->fields['plugin_fusioninventory_agents_id']);
     foreach ($order_files as $hash => $params) {
         $order_files[$hash]['mirrors'] = $mirrors;
         $manifest = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/manifests/" . $hash;
         $order_files[$hash]['multiparts'] = array();
         if (file_exists($manifest)) {
             $handle = fopen($manifest, "r");
             if ($handle) {
                 while (($buffer = fgets($handle)) !== FALSE) {
                     $order_files[$hash]['multiparts'][] = trim($buffer);
                 }
                 fclose($handle);
             }
         }
     }
     //Send an empty json dict instead of empty json list
     if (count($order_files) == 0) {
         $order_files = (object) array();
     }
     $order = array("job" => $order_job, "associatedFiles" => $order_files);
     return $order;
 }
 static function move_item($params)
 {
     //get current order json
     $datas = json_decode(PluginFusioninventoryDeployOrder::getJson($params['orders_id']), TRUE);
     //get data on old index
     $moved_check = $datas['jobs']['actions'][$params['old_index']];
     //remove this old index in json
     unset($datas['jobs']['actions'][$params['old_index']]);
     //insert it in new index (array_splice for insertion, ex : http://stackoverflow.com/a/3797526)
     array_splice($datas['jobs']['actions'], $params['new_index'], 0, array($moved_check));
     //update order
     PluginFusioninventoryDeployOrder::updateOrderJson($params['orders_id'], $datas);
 }
  @copyright Copyright (c) 2010-2014 FusionInventory team
  @license   AGPL License 3.0 or (at your option) any later version
             http://www.gnu.org/licenses/agpl-3.0-standalone.html
  @link      http://www.fusioninventory.org/
  @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
  @since     2010

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
Session::checkLoginUser();
if (isset($_POST['update_json'])) {
    $order = new PluginFusioninventoryDeployOrder();
    $json_clean = stripcslashes($_POST['json']);
    $json = json_decode($json_clean, TRUE);
    $ret = PluginFusioninventoryDeployOrder::updateOrderJson($_POST['orders_id'], $json);
    Html::back();
    exit;
} elseif (isset($_POST['add_item'])) {
    $data = array_map(array('Toolbox', 'stripslashes_deep'), $_POST);
    PluginFusioninventoryDeployPackage::alter_json('add_item', $data);
    Html::back();
} elseif (isset($_POST['save_item'])) {
    $data = array_map(array('Toolbox', 'stripslashes_deep'), $_POST);
    PluginFusioninventoryDeployPackage::alter_json('save_item', $data);
    Html::back();
} elseif (isset($_POST['remove_item'])) {
    $data = array_map(array('Toolbox', 'stripslashes_deep'), $_POST);
    PluginFusioninventoryDeployPackage::alter_json('remove_item', $data);
    Html::back();
}