PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/application/libraries/phphgadmin.php

https://bitbucket.org/joshjcarrier/phphgadmin/
PHP | 301 lines | 186 code | 48 blank | 67 comment | 28 complexity | ef598021e905975b224efab928653211 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Moderates all actions between the application controllers and the underlying libraries,
  4. * - permissions
  5. * - optimistic locking
  6. * - error catching
  7. * @package CodeIgniter
  8. * @subpackage Libraries
  9. * @category Libraries
  10. * @author Josh Carrier
  11. * @link blog.joshjcarrier.com
  12. */
  13. class phpHgAdmin
  14. {
  15. private $_ci;
  16. private $_ofl_lock_hgweb;
  17. private $_ofl_lock_hgrc;
  18. private $_profile;
  19. private $_cache;
  20. function __construct()
  21. {
  22. $this->_ci = &get_instance();
  23. $this->_ci->load->library('Hg_Filesystem');
  24. $this->_ci->load->library('Hg_Ini');
  25. $this->flush_cache();
  26. }
  27. function start_tx(&$ofl_lock_hgweb, &$ofl_lock_hgrc)
  28. {
  29. $this->_ofl_lock_hgweb = &$ofl_lock_hgweb;
  30. $this->_ofl_lock_hgrc = &$ofl_lock_hgrc;
  31. }
  32. function end_tx()
  33. {
  34. $dummy = '';
  35. $this->_ofl_lock_hgweb = &$dummy;
  36. $this->_ofl_lock_hgrc = &$dummy;
  37. }
  38. function set_profile($profile = null)
  39. {
  40. $this->_profile = $profile;
  41. $this->flush_cache();
  42. }
  43. function get_profile()
  44. {
  45. return $this->_profile;
  46. }
  47. function flush_cache()
  48. {
  49. $this->_cache = array();
  50. }
  51. function lsdir()
  52. {
  53. if(!$this->_ci->auth->auth_is_user_authenticated())
  54. {
  55. return HGPHP_ERR_PERM_USR;
  56. }
  57. $webdir = $this->get_profile();
  58. if(isset($this->_cache['lsdir']))
  59. {
  60. return $this->_cache['lsdir'];
  61. }
  62. // retrieves current directory structure
  63. $realdir = $this->_ci->hg_filesystem->real_dirscan($webdir);
  64. // retrieves current
  65. $this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgweb);
  66. $hgwebrepos_compat = $this->_ci->hg_ini->getRepoList($webdir);
  67. $hgwebrepos = array();
  68. if(is_array($hgwebrepos_compat))
  69. {
  70. foreach($hgwebrepos_compat as $r_name => $r_path)
  71. {
  72. $hgwebrepos[] = base64_decode($r_name);
  73. }
  74. }
  75. else
  76. {
  77. // error code
  78. return $hgwebrepos;
  79. }
  80. $allrepo = $realdir;
  81. $allrepo = array_merge($realdir, $hgwebrepos);
  82. $hgrepos = array();
  83. foreach($allrepo as $repo)
  84. {
  85. $hgrepos[$repo]['name'] = $repo;
  86. if(isset($realdir[$repo]) && in_array($repo, $hgwebrepos))
  87. {
  88. $hgrepos[$repo]['status'] = HGPHP_REPO_STATUS_ENABLED;
  89. }
  90. else if(isset($realdir[$repo]) && !in_array($repo, $hgwebrepos))
  91. {
  92. $hgrepos[$repo]['status'] = HGPHP_REPO_STATUS_DISABLED;
  93. }
  94. else if(!isset($realdir[$repo]) && in_array($repo, $hgwebrepos))
  95. {
  96. $hgrepos[$repo]['status'] = HGPHP_REPO_STATUS_MISSING;
  97. }
  98. }
  99. $this->_cache['lsdir'] = $hgrepos;
  100. return $hgrepos;
  101. }
  102. /**
  103. * stat_repository
  104. * Returns the HGRC represented as an array for the specified repository
  105. * @param r_name name of the project whose hgrc to retrieve
  106. * @return array representing hgrc or status code
  107. */
  108. function stat_repository($r_id)
  109. {
  110. if(!$this->can_view($r_id))
  111. {
  112. return HGPHP_ERR_PERM_USR;
  113. }
  114. $profile = $this->get_profile();
  115. if(isset($this->_cache['stat'][$r_id]))
  116. {
  117. return $this->_cache['lsdir'];
  118. }
  119. $this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgrc);
  120. return $this->_ci->hg_ini->loadHgRC($r_id, $profile);
  121. }
  122. function create_repository($r_name)
  123. {
  124. $profile = $this->get_profile();
  125. if(!$this->can_create($r_name))
  126. {
  127. return HGPHP_ERR_PERM_USR;
  128. }
  129. $create_status = HGPHP_OK;
  130. $lsdir = $this->_ci->hg_ini->getRepoList($profile);
  131. // not registered in hgweb.config
  132. $hashr_name = base64_encode($r_name);
  133. if(!isset($lsdir[$hashr_name]))
  134. {
  135. // edit the directory
  136. $this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgweb);
  137. $create_status = $this->_ci->hg_ini->registerRepo($r_name, $profile);
  138. $this->flush_cache();
  139. if($create_status == HGPHP_OK)
  140. {
  141. // then create the repository
  142. $this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgrc);
  143. $create_status = $this->_ci->hg_filesystem->create_repository_dir($r_name, $profile);
  144. if($create_status == HGPHP_OK)
  145. {
  146. $create_status = $this->_ci->hg_ini->touchHgRC($r_name, $profile);
  147. }
  148. }
  149. }
  150. else
  151. {
  152. // repository already exists
  153. $create_status = HGPHP_ERR_FS_PREEXISTS;
  154. }
  155. return $create_status;
  156. }
  157. /**
  158. * update_repository
  159. * Update repository's hgrc
  160. * @param r_name name of the repository to update hgrc for
  161. * @param hgrc_data array representing new hgrc file
  162. * @return status code
  163. */
  164. function update_repository($r_name, $hgrc_data)
  165. {
  166. $profile = $this->get_profile();
  167. if(!$this->can_update($r_name))
  168. {
  169. return HGPHP_ERR_PERM_USR;
  170. }
  171. $this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgrc);
  172. $this->flush_cache();
  173. return $this->_ci->hg_ini->saveHgRC($r_name, $hgrc_data, $profile);
  174. }
  175. /**
  176. * delete_repository
  177. * Deletes a repository from the file system and unregisters it from hgweb.config
  178. * @param r_key id of the repo to delete permanently
  179. * @return status code
  180. */
  181. function delete_repository($r_key)
  182. {
  183. $hgweb = $this->get_profile();
  184. $hashr_key = base64_encode($r_key);
  185. if(!$this->can_delete($r_key))
  186. {
  187. return HGPHP_ERR_PERM_USR;
  188. }
  189. $del_status = HGPHP_OK;
  190. $lsdir = $this->_ci->hg_ini->getRepoList($hgweb);
  191. if(isset($lsdir[$hashr_key]))
  192. {
  193. // edit the directory
  194. // remove from filesystem
  195. $this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgweb);
  196. $del_status = $this->_ci->hg_filesystem->delete_repository_dir($r_key, $hgweb);
  197. // remove from hgweb.config
  198. if($del_status == HGPHP_OK)
  199. {
  200. $del_status = $this->_ci->hg_ini->unregisterRepo($r_key, $hgweb);
  201. }
  202. }
  203. else
  204. {
  205. $del_status = HGPHP_ERR_FS_PREEXISTS;
  206. }
  207. $this->flush_cache();
  208. return $del_status;
  209. }
  210. /**
  211. * can_create
  212. * Checks if user has permissions to create this repository.
  213. * Requires view permission.
  214. * @param r_name name of repository wanting to be created
  215. * @return true if allowed
  216. */
  217. function can_create($r_name)
  218. {
  219. return $this->_ci->config->item('global_allow_repo_create') && $this->_ci->auth->auth_user_can_create($r_name);
  220. }
  221. /**
  222. * can_update
  223. * Checks if user has permissions to update this repository
  224. * Requires view permission.
  225. * @param r_name name of repository wanting to be updated
  226. * @return true if allowed
  227. */
  228. function can_update($r_name)
  229. {
  230. return $this->_ci->config->item('global_allow_repo_update') && $this->_ci->auth->auth_user_can_update($r_name);
  231. }
  232. /**
  233. * can_create
  234. * Checks if user has permissions to view this repository
  235. * @param r_name name of repository wanting to be created
  236. * @return true if allowed
  237. */
  238. function can_view($r_name)
  239. {
  240. return $this->_ci->config->item('global_allow_repo_view') && $this->_ci->auth->auth_user_can_view($r_name);
  241. }
  242. /**
  243. * can_delete
  244. * Checks if user has permissions to delete this repository
  245. * Requires view permission.
  246. * @param r_name name of repository wanting to be deleted
  247. * @return true if allowed
  248. */
  249. function can_delete($r_name)
  250. {
  251. return $this->_ci->config->item('global_allow_repo_delete') && $this->_ci->auth->auth_user_can_delete($r_name);
  252. }
  253. }