hackpoint.plugin.php 10 KB

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