hackpoint.plugin.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. throw new Exception("Méthode non implémentée",501);
  126. };
  127. $server->do['download'] = function($isoPath){
  128. global $myUser,$_,$conf;
  129. $utf8Path = utf8_encode($isoPath);
  130. //pour verfiier si le fichier existe, on récupere son chemin système avec le bon encodage
  131. $osPath = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? $isoPath: $utf8Path;
  132. require_once(__DIR__.SLASH.'Resource.class.php');
  133. require_once(__DIR__.SLASH.'ResourceType.class.php');
  134. preg_match_all('/#([0-9]*)/i', $utf8Path,$matches,PREG_SET_ORDER);
  135. $id = end($matches);
  136. $id = $id[1];
  137. $resource = Resource::getById($id);
  138. $stream = 'not implemented';
  139. if(!$resource) throw new Exception("Fichier inexistant",404);
  140. $type = ResourceType::types($resource->type);
  141. $stream = $type['class']::toFile($resource);
  142. $stream = $stream['content'];
  143. return $stream;
  144. };
  145. $server->do['edit'] = function($isoPath,$body,$type){
  146. global $myUser,$conf;
  147. User::check_access('hackpoint','edit');
  148. $utf8Path = utf8_encode($isoPath);
  149. require_once(__DIR__.SLASH.'Resource.class.php');
  150. require_once(__DIR__.SLASH.'ResourceType.class.php');
  151. preg_match_all('/#([0-9]*)/i', $utf8Path,$matches,PREG_SET_ORDER);
  152. $id = end($matches);
  153. $id = $id[1];
  154. $resource = Resource::getById($id);
  155. if(!$resource) throw new Exception("Fichier inexistant",404);
  156. if($type!='file')throw new Exception("Non implémenté",501);
  157. /*$maxSize = $conf->get('document_allowed_size');*/
  158. $extension = getExt($utf8Path);
  159. /*$extensions = explode(',',str_replace(' ', '', $conf->get('document_allowed_extensions')));
  160. if(strlen($body) > $maxSize) throw new Exception("Taille du fichier trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)',406);
  161. if(!in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier ".$path." non permise, autorisé :".implode(', ',$extensions),415);
  162. if($conf->get('document_enable_logs') || $conf->get('document_enable_logs_verbose')) Log::put('Enregistrement fichier '.$utf8Path,'hackpoint');
  163. */
  164. $resource->content = $body;
  165. $resource->save();
  166. };
  167. $server->do['move'] = function($isoPath,$isoTo){
  168. global $myUser,$conf;
  169. $utf8Path = utf8_encode($isoPath);
  170. $utf8To = utf8_encode($isoTo);
  171. User::check_access('hackpoint','edit');
  172. throw new Exception("Méthode non implémentée",501);
  173. };
  174. $server->do['copy'] = function($isoPath,$isoTo){
  175. global $myUser,$conf;
  176. $utf8Path = utf8_encode($isoPath);
  177. $utf8To = utf8_encode($isoTo);
  178. User::check_access('hackpoint','edit');
  179. throw new Exception("Méthode non implémentée",501);
  180. };
  181. $server->do['list'] = function($isoPath,$depth =0){
  182. global $myUser,$conf;
  183. $utf8Path = utf8_encode($isoPath);
  184. require_once(__DIR__.SLASH.'Sketch.class.php');
  185. require_once(__DIR__.SLASH.'Resource.class.php');
  186. User::check_access('hackpoint','read');
  187. $files = array();
  188. $file = array();
  189. $file['label'] = '';
  190. $file['type'] = 'directory';
  191. $file['path'] = '';
  192. $files[] = $file;
  193. if($depth>0){
  194. if(!empty($utf8Path)){
  195. preg_match('/#([0-9]*)/i', $utf8Path,$matches);
  196. $sketch = Sketch::load(array('id'=>$matches[1]));
  197. foreach(Resource::loadAll(array('sketch'=>$sketch->id)) as $resource){
  198. $type = $resource->type();
  199. $file = array();
  200. $file['label'] = $resource->label.' #'.$resource->id.'.'.$type['toExtension'];
  201. $file['type'] = 'file';
  202. $file['create_time'] = $resource->created;
  203. $file['update_time'] = $resource->updated;
  204. $file['length'] = 1000;
  205. $files[] = $file;
  206. }
  207. }else{
  208. foreach(Sketch::loadAll() as $sketch){
  209. $file = array();
  210. $file['label'] = $sketch->label.' #'.$sketch->id;
  211. $file['type'] = 'directory';
  212. $file['create_time'] = $sketch->created;
  213. $file['update_time'] = $sketch->updated;
  214. $file['length'] = 1000;
  215. $files[] = $file;
  216. }
  217. }
  218. }
  219. return $files;
  220. };
  221. $server->start();
  222. }
  223. //Déclation des assets
  224. Plugin::addCss("/css/main.css?v=1");
  225. Plugin::addCss("/css/codemirror.css?v=1");
  226. Plugin::addCss("/css/codemirror-monokai.css?v=1");
  227. Plugin::addCss("/css/component.css?v=1",true);
  228. Plugin::addCss("/css/animate.min.css");
  229. Plugin::addJs("/js/main.js?v=1");
  230. Plugin::addJs("/js/codemirror.js?v=1");
  231. Plugin::addJs("/js/component.js?v=2",true);
  232. foreach(glob(__DIR__.SLASH.'js'.SLASH.'codemirror-mode'.SLASH.'*.js') as $file){
  233. Plugin::addJs("/js/codemirror-mode/".basename($file));
  234. }
  235. //Mapping hook / fonctions
  236. Plugin::addHook("install", "hackpoint_install");
  237. Plugin::addHook("uninstall", "hackpoint_uninstall");
  238. Plugin::addHook("section", "hackpoint_section");
  239. Plugin::addHook("menu_main", "hackpoint_menu");
  240. Plugin::addHook("page", "hackpoint_page");
  241. Plugin::addHook("action", "hackpoint_action");
  242. Plugin::addHook("menu_setting", "hackpoint_menu_setting");
  243. Plugin::addHook("content_setting", "hackpoint_content_setting");
  244. Plugin::addHook("hackpoint_resource_type", "hackpoint_manage_types");
  245. Plugin::addHook("rewrite", "hackpoint_dav");
  246. ?>