hackpoint.plugin.php 11 KB

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