From 2adec1cec68f75b470d48e111c1c2bfe0cd59f58 Mon Sep 17 00:00:00 2001 From: korial29 Date: Wed, 22 Apr 2015 17:54:35 +0200 Subject: [PATCH 1/6] Bug fix : - Notification PDF download url - Migration bug fix - Profile form / update - Bookmark add for graph --- front/download.form.php | 59 ++++++++++++++++++++ hook.php | 7 ++- inc/common.class.php | 2 +- inc/download.class.php | 6 ++ inc/notification.class.php | 14 +++-- inc/notificationtargetnotification.class.php | 19 +++++-- inc/profile.class.php | 26 +++++---- 7 files changed, 107 insertions(+), 26 deletions(-) create mode 100644 front/download.form.php create mode 100644 inc/download.class.php diff --git a/front/download.form.php b/front/download.form.php new file mode 100644 index 00000000..e899707a --- /dev/null +++ b/front/download.form.php @@ -0,0 +1,59 @@ +. + -------------------------------------------------------------------------- + */ + +$USEDBREPLICATE = 1; +$DBCONNECTION_REQUIRED = 0; // Not really a big SQL request + +include ("../../../inc/includes.php"); + +header("Content-Type: text/html; charset=UTF-8"); +Html::header_nocache(); + +if (isset($_GET['id'])){ + $notificationTarget = new PluginMreportingNotificationTargetNotification(); + $file = $notificationTarget->getFileDir()."/".$notificationTarget->getFileName($_GET['id']); + + if (file_exists($file)) { + header('Content-Description: File Transfer'); + header('Content-Type: application/octet-stream'); + header('Content-Disposition: attachment; filename='.basename($file)); + header('Content-Transfer-Encoding: binary'); + header('Expires: 0'); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header('Pragma: public'); + header('Content-Length: ' . filesize($file)); + ob_clean(); + flush(); + readfile($file); + exit; + } +} + +exit; + diff --git a/hook.php b/hook.php index 6982f596..4f76b664 100644 --- a/hook.php +++ b/hook.php @@ -137,8 +137,6 @@ function plugin_mreporting_install() { // == Update to 2.3 == if (!fieldExists('glpi_plugin_mreporting_profiles', 'right') && fieldExists('glpi_plugin_mreporting_profiles', 'reports')) { - //save all profile with right READ - $right = PluginMreportingProfile::getRight(); //truncate profile table $query = "TRUNCATE TABLE `glpi_plugin_mreporting_profiles`"; @@ -186,8 +184,11 @@ function plugin_mreporting_install() { require_once "inc/config.class.php"; $config = new PluginMreportingConfig(); $config->createFirstConfig(); - + + //save all profile with right READ + $right = PluginMreportingProfile::getRight(); PluginMreportingProfile::addRightToProfiles($right); + return true; } diff --git a/inc/common.class.php b/inc/common.class.php index e9893956..36d2461f 100644 --- a/inc/common.class.php +++ b/inc/common.class.php @@ -1633,7 +1633,7 @@ static function showSelector($date1, $date2, $randname) { } $_SERVER['REQUEST_URI'] .= "&date1".$randname."=".$date1; $_SERVER['REQUEST_URI'] .= "&date2".$randname."=".$date2; - Bookmark::showSaveButton(Bookmark::URI); + Bookmark::showSaveButton(Bookmark::URI, self::getType()); //If there's no selector for the report, there's no need for a reset button ! if ($has_selector) { diff --git a/inc/download.class.php b/inc/download.class.php new file mode 100644 index 00000000..3d5d1f8d --- /dev/null +++ b/inc/download.class.php @@ -0,0 +1,6 @@ +add(array( + $notification = new Notification(); + if ($notification_id = $notification->add(array( 'name' => __("Notification for \"More Reporting\"", 'mreporting'), 'comment' => "", 'entities_id' => 0, @@ -57,12 +57,14 @@ static function install() { 'itemtype' => 'PluginMreportingNotification', 'notificationtemplates_id' => $template_id, 'event' => 'sendReporting', - 'mode' => 'mail', - )); + 'mode' => 'mail'))) { + + $DB->query('INSERT INTO glpi_notificationtargets (items_id, type, notifications_id) + VALUES (1, 1, '.$notification_id.');'); + } } - $DB->query('INSERT INTO glpi_notificationtargets (items_id, type, notifications_id) - VALUES (1, 1, ' . $notification_id . ');'); + return array('success' => true); } diff --git a/inc/notificationtargetnotification.class.php b/inc/notificationtargetnotification.class.php index 7c7b0062..e94e01a6 100644 --- a/inc/notificationtargetnotification.class.php +++ b/inc/notificationtargetnotification.class.php @@ -27,7 +27,7 @@ function getTags() { function getDatasForTemplate($event, $options = array()) { global $CFG_GLPI; - $user_name = mt_rand().'_'; + $user_name = mt_rand(); $file_name = $this->_buildPDF($user_name); $path = GLPI_PLUGIN_DOC_DIR."/mreporting/notifications/$file_name"; @@ -35,12 +35,19 @@ function getDatasForTemplate($event, $options = array()) { $this->additionalData['attachment']['path'] = $path; $this->additionalData['attachment']['name'] = $file_name; - $link = $CFG_GLPI['url_base']."/index.php?redirect=plugin_mreporting_$file_name"; + $link = $CFG_GLPI['url_base']."/index.php?redirect=PluginMreportingDownload_$user_name"; $this->datas['##lang.mreporting.file_url##'] = __('Link'); $this->datas['##mreporting.file_url##'] = $link; } - + + public function getFileName($user_name){ + return 'glpi_report_'.$user_name."_".date('d-m-Y').'.pdf'; + } + + public function getFileDir(){ + return GLPI_PLUGIN_DOC_DIR.'/mreporting/notifications'; + } /** * Generate a PDF file with mreporting reports to be send in the notifications @@ -50,8 +57,8 @@ function getDatasForTemplate($event, $options = array()) { private function _buildPDF($user_name = '') { global $CFG_GLPI, $DB, $LANG; - $dir = GLPI_PLUGIN_DOC_DIR.'/mreporting/notifications'; - $file_name = 'glpi_report_'.$user_name.date('d-m-Y').'.pdf'; + $dir = $this->getFileDir(); + $file_name = $this->getFileName($user_name); if(!is_dir($dir)) return false; @@ -106,7 +113,7 @@ private function _buildPDF($user_name = '') { continue; } $image_base64 = $matches[1][2]; - if (strpos($image_base64, 'data:image/png;base64,') === false) { + if (strpos($image_base64, 'data:image/png;base64,') === false && isset($matches[1][3])) { $image_base64 = $matches[1][3]; } if (strpos($image_base64, 'data:image/png;base64,') === false) { diff --git a/inc/profile.class.php b/inc/profile.class.php index 855af3d9..dd609e6b 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -31,8 +31,7 @@ die("Sorry. You can't access directly to this file"); } -class PluginMreportingProfile extends CommonDBTM { - static $rightname = 'profile'; +class PluginMreportingProfile extends Profile { static function getTypeName($nb = 0) { return __("More Reporting", 'mreporting'); @@ -152,8 +151,8 @@ static function getRight(){ global $DB; $query = "SELECT `profiles_id` - FROM `glpi_plugin_mreporting_profiles` - WHERE `reports` = READ "; + FROM `glpi_plugin_mreporting_profiles` + WHERE `reports` = '".READ."'"; $right = array(); foreach ($DB->request($query) as $profile) { @@ -178,8 +177,8 @@ function addRightToProfile($idProfile){ $reportProfile1 = new PluginMreportingProfile(); $reportProfile1->add(array( 'profiles_id' => $idProfile, - 'reports' => $report['id'], - 'right' => READ + 'reports' => $report['id'], + 'right' => READ )); } @@ -198,8 +197,8 @@ function addRightToReports($report_id){ $reportProfile1 = new PluginMreportingProfile(); $reportProfile1->add(array( 'profiles_id' => $prof['id'], - 'reports' => $report_id, - 'right' => READ + 'reports' => $report_id, + 'right' => READ )); } } @@ -232,6 +231,13 @@ function showForm ($ID, $options=array()) { $config = new PluginMreportingConfig(); $res = $config->find(); + + if ($ID > 0) { + $this->check($ID, READ); + } else { + // Create item + $this->check(-1, CREATE); + } $this->showFormHeader($options); echo "\n"; @@ -299,8 +305,8 @@ function showFormForManageProfile($items,$options=array()){ ORDER BY `name`"; foreach ($DB->request($query) as $profile) { - $reportProfiles=new self(); - $reportProfiles = $reportProfiles->findByProfileAndReport($profile['id'],$items->fields['id']); + $reportProfiles = new self(); + $reportProfiles = $reportProfiles->findByProfileAndReport($profile['id'], $items->fields['id']); $prof = new Profile(); $prof->getFromDB($profile['id']); From 055215cf82450bbfdcec2abcf300f0f22737bb41 Mon Sep 17 00:00:00 2001 From: korial29 Date: Mon, 27 Apr 2015 10:57:50 +0200 Subject: [PATCH 2/6] Bug fix migration --- hook.php | 27 ++++++++++++++--------- inc/download.class.php | 6 +++++- inc/profile.class.php | 49 +++++++++++++----------------------------- 3 files changed, 37 insertions(+), 45 deletions(-) diff --git a/hook.php b/hook.php index 4f76b664..f9a73a8d 100644 --- a/hook.php +++ b/hook.php @@ -135,15 +135,16 @@ function plugin_mreporting_install() { $migration->migrationOneTable('glpi_plugin_mreporting_configs'); // == Update to 2.3 == + $right = array(); if (!fieldExists('glpi_plugin_mreporting_profiles', 'right') && fieldExists('glpi_plugin_mreporting_profiles', 'reports')) { - - //truncate profile table + $right = PluginMreportingProfile::getRight(); + // truncate profile table $query = "TRUNCATE TABLE `glpi_plugin_mreporting_profiles`"; $DB->query($query); //migration of field - $migration->addField('glpi_plugin_mreporting_profiles', 'right', 'char'); + $migration->addField('glpi_plugin_mreporting_profiles', 'right', 'integer'); $migration->changeField('glpi_plugin_mreporting_profiles', 'reports', 'reports','integer'); $migration->changeField('glpi_plugin_mreporting_profiles', 'profiles_id', @@ -154,7 +155,7 @@ function plugin_mreporting_install() { } // == UPDATE to 0.84+1.0 == - $query = "UPDATE `glpi_plugin_mreporting_profiles` pr SET pr.right = ".READ." WHERE pr.right = 'r'"; + $query = "UPDATE `glpi_plugin_mreporting_profiles` SET `right` = '".READ."' WHERE `right` = 'r'"; $DB->query($query); if (!isIndex('glpi_plugin_mreporting_profiles', 'profiles_id_reports')) { $query = "ALTER IGNORE TABLE glpi_plugin_mreporting_profiles @@ -177,17 +178,21 @@ function plugin_mreporting_install() { $migration->addField("glpi_plugin_mreporting_preferences", "selectors", "text"); $migration->migrationOneTable('glpi_plugin_mreporting_preferences'); - + // == Init available reports require_once "inc/baseclass.class.php"; require_once "inc/common.class.php"; require_once "inc/config.class.php"; $config = new PluginMreportingConfig(); - $config->createFirstConfig(); - - //save all profile with right READ - $right = PluginMreportingProfile::getRight(); - PluginMreportingProfile::addRightToProfiles($right); + if (!countElementsInTable("glpi_plugin_mreporting_configs")) { + $config->createFirstConfig(); + } + + if (!empty($right)) { + PluginMreportingProfile::addRightToProfiles($right); + $query = "UPDATE `glpi_plugin_mreporting_profiles` SET `right` = '".READ."'"; + $DB->query($query); + } return true; } @@ -221,6 +226,8 @@ function plugin_mreporting_uninstall() { require_once "inc/notification.class.php"; PluginMreportingNotification::uninstall(); + + CronTask::Unregister('MreportingNotification'); return true; } diff --git a/inc/download.class.php b/inc/download.class.php index 3d5d1f8d..cc31d25b 100644 --- a/inc/download.class.php +++ b/inc/download.class.php @@ -2,5 +2,9 @@ class PluginMreportingDownload extends CommonDBTM { -} + public function __construct() { + $this->forceTable("glpi_plugin_mreporting_configs"); + parent::__construct(); + } +} diff --git a/inc/profile.class.php b/inc/profile.class.php index dd609e6b..4e406f44 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -31,8 +31,10 @@ die("Sorry. You can't access directly to this file"); } -class PluginMreportingProfile extends Profile { +class PluginMreportingProfile extends CommonDBTM { + static $rightname = 'profile'; + static function getTypeName($nb = 0) { return __("More Reporting", 'mreporting'); } @@ -110,49 +112,35 @@ function getFromDBByProfile($profiles_id) { /** * @param $right array - */ - static function addRightToProfiles($right){ + */ + static function addRightToProfiles($right) { global $DB; - $profiles = "SELECT `id` FROM `glpi_profiles`"; - $reports = "SELECT `id` FROM `glpi_plugin_mreporting_configs`"; +// $profiles = "SELECT `id` FROM `glpi_profiles`"; + $reports = "SELECT `id` FROM `glpi_plugin_mreporting_configs`"; //TODO : We need to reload cache before else glpi don't show migration table $myreport = new PluginMreportingProfile(); - $table_fields = $DB->list_fields($myreport->getTable(),false); + foreach ($right as $prof) { + foreach ($DB->request($reports) as $report) { - foreach ($DB->request($profiles) as $prof) { - foreach($DB->request($reports) as $report){ - - //If profiles have right - if(in_array($prof['id'],$right)){ - $tmp = array( - 'profiles_id' => $prof['id'], - 'reports' => $report['id'], - 'right' => READ); - $myreport->add($tmp); - }else{ - $tmp = array( - 'profiles_id' => $prof['id'], - 'reports' => $report['id'] - ); - $myreport->add($tmp); - } + $tmp = array( + 'profiles_id' => $prof, + 'reports' => $report['id'], + 'right' => READ); + $myreport->add($tmp); } } - - } - static function getRight(){ global $DB; $query = "SELECT `profiles_id` FROM `glpi_plugin_mreporting_profiles` - WHERE `reports` = '".READ."'"; + WHERE `reports` = 'r'"; $right = array(); foreach ($DB->request($query) as $profile) { @@ -346,13 +334,6 @@ function findByProfileAndReport($profil_id, $report_id){ return $prof; } - function findReportByProfiles($profil_id){ - $prof = new self(); - $prof->getFromDBByQuery(" WHERE `profiles_id` = ".$profil_id); - return $prof; - } - - static function canViewReports($profil_id, $report_id){ $prof = new self(); $res = $prof->getFromDBByQuery(" WHERE `reports` = ".$report_id." AND `profiles_id` = ".$profil_id); From 91e5f10d828fd5f50dcf6127f05202350be390c2 Mon Sep 17 00:00:00 2001 From: korial29 Date: Wed, 6 May 2015 12:22:44 +0200 Subject: [PATCH 3/6] Profile form bug fix --- front/profile.form.php | 26 ++++++++++++++++++-------- inc/profile.class.php | 10 ++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/front/profile.form.php b/front/profile.form.php index 980fb2d7..02740a75 100644 --- a/front/profile.form.php +++ b/front/profile.form.php @@ -36,18 +36,28 @@ } else if (isset($_REQUEST['giveReadAccessForAllReport'])){ foreach($res as $report) { - $profil->getFromDBByQuery("WHERE profiles_id = ".$_REQUEST['profile_id']. - " AND reports = ".$report['id']); - $profil->fields['right'] = READ; - $profil->update($profil->fields); + if ($profil->getFromDBByQuery(" WHERE profiles_id = ".$_REQUEST['profile_id']. + " AND reports = ".$report['id'])) { + $profil->fields['right'] = READ; + $profil->update($profil->fields); + } else { + $profil->add(array('profiles_id' => $_REQUEST['profile_id'], + 'reports' => $report['id'], + 'right' => READ)); + } } } else if (isset($_REQUEST['giveNoneAccessForAllReport'])){ foreach($res as $report) { - $profil->getFromDBByQuery("WHERE profiles_id = ".$_REQUEST['profile_id']. - " AND reports = ".$report['id']); - $profil->fields['right'] = 'NULL'; - $profil->update($profil->fields); + if ($profil->getFromDBByQuery(" WHERE profiles_id = ".$_REQUEST['profile_id']. + " AND reports = ".$report['id'])) { + $profil->fields['right'] = 'NULL'; + $profil->update($profil->fields); + } else { + $profil->add(array('profiles_id' => $_REQUEST['profile_id'], + 'reports' => $report['id'], + 'right' => 'NULL')); + } } } else if (isset($_REQUEST['giveNoneAccessForAllProfile'])){ diff --git a/inc/profile.class.php b/inc/profile.class.php index 4e406f44..815e593c 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -219,15 +219,8 @@ function showForm ($ID, $options=array()) { $config = new PluginMreportingConfig(); $res = $config->find(); - - if ($ID > 0) { - $this->check($ID, READ); - } else { - // Create item - $this->check(-1, CREATE); - } - $this->showFormHeader($options); + echo "
"; echo "
\n"; echo "\n"; @@ -330,6 +323,7 @@ function showFormForManageProfile($items,$options=array()){ function findByProfileAndReport($profil_id, $report_id){ $prof = new self(); + $prof->getEmpty(); $prof->getFromDBByQuery(" WHERE `reports` = ".$report_id." AND `profiles_id` = ".$profil_id); return $prof; } From 5fd7a787edae2d088d7303479f36a4f36b1720b1 Mon Sep 17 00:00:00 2001 From: korial29 Date: Wed, 6 May 2015 18:08:05 +0200 Subject: [PATCH 4/6] Bug fix : - png graph generation - helpdeskplus minor fix --- inc/graphpng.class.php | 2 +- inc/helpdeskplus.class.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/graphpng.class.php b/inc/graphpng.class.php index db2946c2..c72365eb 100644 --- a/inc/graphpng.class.php +++ b/inc/graphpng.class.php @@ -127,7 +127,7 @@ function showImage($contents, $export="png") { $show_inline = false; $rand=mt_rand(); $filename = "mreporting_img_$rand.png"; - $filedir = GLPI_ROOT."/files/_plugins/mreporting/$filename"; + $filedir = GLPI_PLUGIN_DOC_DIR."/mreporting/$filename"; file_put_contents($filedir, $contents); echo ""; Html::Closeform(); echo "
".__("Rights management", 'mreporting')."