hackpoint.plugin.php 11 KB

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