hackpoint.plugin.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. //Déclaration d'un item de menu dans le menu principal
  3. function hackpoint_menu(&$menuItems){
  4. global $_,$myUser;
  5. if(!$myUser->can('hackpoint','read')) return;
  6. $menuItems[] = array(
  7. 'sort'=>3,
  8. 'url'=>'index.php?module=hackpoint',
  9. 'label'=>'Hackpoint',
  10. 'icon'=> 'fas fa-wrench',
  11. 'color'=> '#3498db'
  12. );
  13. $menuItems[] = array(
  14. 'sort'=>3,
  15. 'url'=>'index.php?module=hackpoint&page=list.part',
  16. 'label'=>'Composants',
  17. 'icon'=> 'fas fa-microchip',
  18. 'color'=> '#3498db'
  19. );
  20. }
  21. //Cette fonction va generer une page quand on clique sur hackpoint dans menu
  22. function hackpoint_page(){
  23. global $_,$myUser;
  24. if(!isset($_['module']) || $_['module'] !='hackpoint') return;
  25. $page = !isset($_['page']) ? 'list.sketch' : $_['page'];
  26. $file = __DIR__.SLASH.'page.'.$page.'.php';
  27. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  28. require_once($file);
  29. }
  30. //Fonction executée lors de l'activation du plugin
  31. function hackpoint_install($id){
  32. if($id != 'fr.idleman.hackpoint') return;
  33. Entity::install(__DIR__);
  34. }
  35. //Fonction executée lors de la désactivation du plugin
  36. function hackpoint_uninstall($id){
  37. if($id != 'fr.idleman.hackpoint') return;
  38. Entity::uninstall(__DIR__);
  39. }
  40. //Déclaration des sections de droits du plugin
  41. function hackpoint_section(&$sections){
  42. $sections['hackpoint'] = "Gestion des droits sur le plugin hackpoint";
  43. // $sections['part'] = "Gestion des droits sur l'entité part";
  44. }
  45. //cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
  46. function hackpoint_action(){
  47. require_once(__DIR__.SLASH.'action.php');
  48. }
  49. //Déclaration du menu de réglages
  50. function hackpoint_menu_setting(&$settingMenu){
  51. global $_, $myUser;
  52. if(!$myUser->can('hackpoint','configure')) return;
  53. $settingMenu[]= array(
  54. 'sort' =>1,
  55. 'url' => 'setting.php?section=global.hackpoint',
  56. 'icon' => 'fas fa-angle-right',
  57. 'label' => 'hackpoint Général'
  58. );
  59. }
  60. //Déclaration des pages de réglages
  61. function hackpoint_content_setting(){
  62. global $_;
  63. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  64. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  65. }
  66. //Déclaration des settings de base
  67. //Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
  68. Configuration::setting('hackpoint',array(
  69. "Général",
  70. 'hackpoint_enable' => array("label"=>"Activer","type"=>"checkbox"),
  71. ));
  72. function hackpoint_manage_types(&$types){
  73. $types = array_merge($types,glob(__DIR__.SLASH.'types'.SLASH.'*.class.php'));
  74. }
  75. function hackpoint_dav($requested){
  76. global $conf,$myUser;
  77. $requested = trim($requested, '/');
  78. if(substr($requested, 0,13)!='dav/hackpoint') return;
  79. if(empty($requested)) throw new Exception("Unspecified DAV path");
  80. require_once(__ROOT__.'common.php');
  81. require_once(__DIR__.SLASH.'WebDav.class.php');
  82. $projectPath = preg_replace('|https?\:\/\/'.$_SERVER['HTTP_HOST'].'|i', '', ROOT_URL);
  83. $server = new WebDav();
  84. $server->logs = WebDav::logPath().SLASH.'dav-logs.txt';
  85. $server->lockfile = WebDav::logPath().SLASH.'dav-lock.json';
  86. $server->root = str_replace('//','/',$projectPath.'/dav/hackpoint/');
  87. $server->folder = '';
  88. //Windows cherche desktop.ini a chaque ouverture de dossier pour le custom
  89. $server->ignoreFiles[] = 'desktop.ini';
  90. //Tortoise et autre client git d'explorer cherchent les .git a chaques ouverture
  91. $server->ignoreFiles[] = '.git';
  92. $server->do['login'] = function($login,$password){
  93. global $myUser;
  94. if($myUser->login !=''){
  95. return $myUser;
  96. }
  97. $myUser = User::check($login,$password);
  98. if(!$myUser)
  99. throw new Exception("Problème lors de la connexion, veuillez contacter l'administrateur",401);
  100. if(file_exists('enabled.maintenance') && $myUser->superadmin != 1)
  101. throw new Exception('Seul un compte Super Admin peut se connecter en mode maintenance',401);
  102. if(!$myUser->connected())
  103. throw new Exception('Identifiant ou mot de passe incorrect',401);
  104. if(is_numeric($myUser->preference('default_firm')) && $myUser->haveFirm($myUser->preference('default_firm'))){
  105. $_SESSION['firm'] = serialize(Firm::getById($myUser->preference('default_firm')));
  106. if(!$_SESSION['firm'])
  107. throw new Exception("Problème lors de la connexion, veuillez contacter l'administrateur",401);
  108. }else if(count($myUser->firms)!=0){
  109. $_SESSION['firm'] = serialize(reset($myUser->firms));
  110. if(!$_SESSION['firm'])
  111. throw new Exception(" Problème lors de la connexion, veuillez contacter l'administrateur",401);
  112. }else{
  113. throw new Exception('Ce compte n\'est actif sur aucune firm',401);
  114. }
  115. $myFirm = isset($_SESSION['firm']) ? unserialize($_SESSION['firm']) : new Firm();
  116. if(!$myFirm)
  117. throw new Exception("Problème lors de la connexion, veuillez contacter l'administrateur",401);
  118. $_SESSION['currentUser'] = serialize($myUser);
  119. if(!$_SESSION['currentUser'])
  120. throw new Exception("Problème lors de la connexion, veuillez contacter l'administrateur",401);
  121. };
  122. $server->do['delete'] = function($isoPath){
  123. global $myUser,$conf;
  124. $utf8Path = utf8_encode($isoPath);
  125. };
  126. $server->do['download'] = function($isoPath){
  127. global $myUser,$_,$conf;
  128. $utf8Path = utf8_encode($isoPath);
  129. //pour verfiier si le fichier existe, on récupere son chemin système avec le bon encodage
  130. $osPath = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? $isoPath: $utf8Path;
  131. if(!file_exists($osPath)) throw new Exception("Fichier inexistant",404);
  132. if(!is_file($osPath)) throw new Exception("Méthode non autorisée sur autre chose qu'un fichier",501);
  133. $stream = 'not implemented';
  134. return $stream;
  135. };
  136. $server->do['edit'] = function($isoPath,$body,$type){
  137. global $myUser,$conf;
  138. User::check_access('document','edit');
  139. $utf8Path = utf8_encode($isoPath);
  140. if($type=='file'){
  141. $maxSize = $conf->get('document_allowed_size');
  142. $extension = getExt($utf8Path);
  143. $extensions = explode(',',str_replace(' ', '', $conf->get('document_allowed_extensions')));
  144. if(strlen($body) > $maxSize) throw new Exception("Taille du fichier trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)',406);
  145. if(!in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier ".$path." non permise, autorisé :".implode(', ',$extensions),415);
  146. $element = Element::addFile($utf8Path, $body);
  147. if($conf->get('document_enable_logs') || $conf->get('document_enable_logs_verbose')) Log::put('Enregistrement fichier '.$utf8Path,'document');
  148. }else{
  149. if($conf->get('document_enable_logs') || $conf->get('document_enable_logs_verbose')) Log::put('Enregistrement dossier '.$utf8Path,'document');
  150. }
  151. };
  152. $server->do['move'] = function($isoPath,$isoTo){
  153. global $myUser,$conf;
  154. $utf8Path = utf8_encode($isoPath);
  155. $utf8To = utf8_encode($isoTo);
  156. User::check_access('document','edit');
  157. if(!document_check_element_name(mt_basename($utf8To), ENT_QUOTES)) throw new Exception("Caractères interdits : \\/:*?\"<>|");
  158. };
  159. $server->do['copy'] = function($isoPath,$isoTo){
  160. global $myUser,$conf;
  161. $utf8Path = utf8_encode($isoPath);
  162. $utf8To = utf8_encode($isoTo);
  163. User::check_access('document','edit');
  164. if(!document_check_element_name(mt_basename($utf8To), ENT_QUOTES)) throw new Exception("Caractères interdits : \\/:*?\"<>|");
  165. };
  166. $server->do['list'] = function($isoPath,$depth =0){
  167. global $myUser,$conf;
  168. $utf8Path = utf8_encode($isoPath);
  169. User::check_access('document','read');
  170. //pour verfier si le fichier existe, on récupere son chemin système avec le bon encodage
  171. $osPath = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? $isoPath: $utf8Path;
  172. /*if(!file_exists($osPath)) throw new Exception("Not found", 404);
  173. $files = array();
  174. //Si la cible est la racine dav, on la retourne sous forme d'element fictif, sinon on utilise le systeme d'element standard
  175. if($utf8Path == __DIR__.SLASH.'hackpoint'.SLASH.'dav'.SLASH.'hackpoint'){
  176. $file = (object) array();
  177. $file->label = '';
  178. $file->type = 'directory';
  179. $file->path = '';
  180. $toScan = array($file);
  181. }else{
  182. $file = (object) array();
  183. $file->label = '';
  184. $file->type = 'directory';
  185. $file->path = '';
  186. $toScan = array($file);
  187. }
  188. */
  189. //Si le dav demande un scan en profondeur du dossier, on scan les enfants du dossier ciblé
  190. if($depth>0)
  191. $toScan = array_merge($toScan,Element::browse($utf8Path.SLASH.'*'));
  192. foreach($toScan as $element){
  193. $file = array(
  194. 'type' => $element->type,
  195. 'label' => $element->label
  196. );
  197. if($element->type == 'directory'){
  198. $file['create_time'] = time();
  199. $file['update_time'] = time();
  200. $file['length'] = 1000;
  201. }else{
  202. $file['create_time'] = time();
  203. $file['update_time'] = time();
  204. $file['length'] = 1000;
  205. }
  206. $files[] = $file;
  207. }
  208. return $files;
  209. };
  210. $server->start();
  211. }
  212. //Déclation des assets
  213. Plugin::addCss("/css/main.css?v=1");
  214. Plugin::addCss("/css/codemirror.css?v=1");
  215. Plugin::addCss("/css/codemirror-monokai.css?v=1");
  216. Plugin::addCss("/css/component.css?v=1",true);
  217. Plugin::addCss("/css/animate.min.css");
  218. Plugin::addJs("/js/main.js?v=1");
  219. Plugin::addJs("/js/codemirror.js?v=1");
  220. Plugin::addJs("/js/component.js?v=2",true);
  221. foreach(glob(__DIR__.SLASH.'js'.SLASH.'codemirror-mode'.SLASH.'*.js') as $file){
  222. Plugin::addJs("/js/codemirror-mode/".basename($file));
  223. }
  224. //Mapping hook / fonctions
  225. Plugin::addHook("install", "hackpoint_install");
  226. Plugin::addHook("uninstall", "hackpoint_uninstall");
  227. Plugin::addHook("section", "hackpoint_section");
  228. Plugin::addHook("menu_main", "hackpoint_menu");
  229. Plugin::addHook("page", "hackpoint_page");
  230. Plugin::addHook("action", "hackpoint_action");
  231. Plugin::addHook("menu_setting", "hackpoint_menu_setting");
  232. Plugin::addHook("content_setting", "hackpoint_content_setting");
  233. Plugin::addHook("hackpoint_resource_type", "hackpoint_manage_types");
  234. Plugin::addHook("rewrite", "hackpoint_dav");
  235. ?>