PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/includes/application/controllers/admin/utility.php

http://68kb.googlecode.com/
PHP | 334 lines | 200 code | 36 blank | 98 comment | 19 complexity | 71cf89299bc4d987a992ad28eb96bde5 MD5 | raw file
Possible License(s): CC0-1.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * 68KB
  4. *
  5. * An open source knowledge base script
  6. *
  7. * @package 68kb
  8. * @author 68kb Dev Team
  9. * @copyright Copyright (c) 2009, 68 Designs, LLC
  10. * @license http://68kb.com/user_guide/license.html
  11. * @link http://68kb.com
  12. * @since Version 1.0
  13. */
  14. // ------------------------------------------------------------------------
  15. /**
  16. * Admin Utility Controller
  17. *
  18. * Handles utilities
  19. *
  20. * @package 68kb
  21. * @subpackage Admin_Controllers
  22. * @category Admin_Controllers
  23. * @author 68kb Dev Team
  24. * @link http://68kb.com/user_guide/overview/settings.html
  25. * @version $Id: utility.php 172 2009-12-11 16:18:32Z suzkaw68 $
  26. */
  27. class utility extends controller
  28. {
  29. /**
  30. * Constructor
  31. *
  32. * @access public
  33. */
  34. function __construct()
  35. {
  36. parent::__construct();
  37. $this->load->model('init_model');
  38. $this->load->helper('cookie');
  39. $this->load->library('auth');
  40. $this->auth->restrict();
  41. $this->load->dbutil();
  42. }
  43. // ------------------------------------------------------------------------
  44. /**
  45. * Show utility list
  46. *
  47. * @access public
  48. */
  49. function index()
  50. {
  51. $data['nav'] = 'settings';
  52. if ( ! $this->auth->check_level(1))
  53. {
  54. $data['not_allowed'] = TRUE;
  55. $this->init_model->display_template('content', $data, 'admin');
  56. return FALSE;
  57. }
  58. $this->init_model->display_template('settings/utilities', $data, 'admin');
  59. }
  60. // ------------------------------------------------------------------------
  61. /**
  62. * Repair DB
  63. *
  64. * @access public
  65. */
  66. function repair()
  67. {
  68. $data['nav'] = 'settings';
  69. $tables = $this->db->list_tables();
  70. foreach ($tables as $table)
  71. {
  72. if ($this->dbutil->repair_table($table))
  73. {
  74. $tb[] = $table;
  75. }
  76. }
  77. $data['table']=$tb;
  78. $this->init_model->display_template('settings/utilities', $data, 'admin');
  79. }
  80. // ------------------------------------------------------------------------
  81. /**
  82. * Optimize the db
  83. *
  84. * @access public
  85. */
  86. function optimize()
  87. {
  88. $data['nav'] = 'settings';
  89. $tables = $this->db->list_tables();
  90. foreach ($tables as $table)
  91. {
  92. if ($this->dbutil->optimize_table($table))
  93. {
  94. $tb[] = $table;
  95. }
  96. }
  97. $data['optimized'] = $tb;
  98. $this->init_model->display_template('settings/utilities', $data, 'admin');
  99. }
  100. // ------------------------------------------------------------------------
  101. /**
  102. * Remove cache files
  103. *
  104. * @access public
  105. */
  106. function delete_cache()
  107. {
  108. $this->load->helper('file');
  109. delete_files($this->config->item('cache_path'));
  110. $this->session->set_flashdata('message', lang('kb_cache_deleted'));
  111. redirect('admin/utility/');
  112. }
  113. // ------------------------------------------------------------------------
  114. /**
  115. * Backup the databse
  116. *
  117. * @access public
  118. */
  119. function backup()
  120. {
  121. // Backup your entire database and assign it to a variable
  122. $backup = & $this->dbutil->backup();
  123. $name = '68kb-'.time().'.gz';
  124. // Load the file helper and write the file to your server
  125. $this->load->helper('file');
  126. write_file(KBPATH .'uploads/'.$name, $backup);
  127. // Load the download helper and send the file to your desktop
  128. $this->load->helper('download');
  129. force_download($name, $backup);
  130. }
  131. // ------------------------------------------------------------------------
  132. /**
  133. * Export to html
  134. *
  135. * @access public
  136. */
  137. function export()
  138. {
  139. $this->load->helper('file');
  140. $this->load->model('category_model');
  141. $this->load->model('article_model');
  142. $dir = KBPATH.'uploads/user_guide/';
  143. $log[] = 'Deleting Files...';
  144. if (is_dir($dir))
  145. {
  146. delete_files($dir, TRUE);
  147. rmdir($dir);
  148. }
  149. mkdir($dir, 0755);
  150. //copy jquery stuff
  151. $src = KBPATH.'themes/admin/export/jquery-treeview';
  152. $dst = KBPATH.'uploads/user_guide/jquery-treeview';
  153. $this->recurse_copy($src, $dst);
  154. $src = KBPATH.'themes/admin/export/css';
  155. $dst = KBPATH.'uploads/user_guide/css';
  156. $this->recurse_copy($src, $dst);
  157. $log[] = 'Copying Files...';
  158. //create cat tree
  159. $this->db->select('cat_id,cat_uri,cat_name,cat_parent')->from('categories')->where('cat_display !=', 'N')->order_by('cat_order DESC, cat_name ASC');
  160. $query = $this->db->get();
  161. if ($query->num_rows() > 0)
  162. {
  163. //$items = $query->result_array();
  164. foreach ($query->result_array() as $row)
  165. {
  166. $categories[$row['cat_id']] = array(
  167. 'cat_id' => $row['cat_id'],
  168. 'cat_name' => $row['cat_name'],
  169. 'cat_parent' => $row['cat_parent'],
  170. 'cat_uri' => $row['cat_uri']
  171. );
  172. }
  173. }
  174. $log[] = 'Generating Tree...';
  175. ob_start();
  176. $this->generate_tree_list($categories, 0, 0);
  177. $r = ob_get_contents();
  178. ob_end_clean();
  179. $data['navigation'] = $r;
  180. $log[] = 'Writing Files...';
  181. $data['settings'] = $this->init_model->get_settings();
  182. $article_query = $this->article_model->get_articles();
  183. if ($article_query->num_rows() > 0)
  184. {
  185. foreach($article_query->result() as $rs)
  186. {
  187. $data['title'] = $rs->article_title;
  188. $data['description'] = $rs->article_description;
  189. $contents = $this->load->view('admin/export/layout', $data, TRUE);
  190. $filename = $dir.$rs->article_uri.'.html';
  191. //echo $filename.'<BR>';
  192. write_file($filename, $contents, 'x+');
  193. }
  194. }
  195. $log[] = 'Finishing...';
  196. $log[] = '<a href="'.base_url().'uploads/user_guide/">Preview</a>';
  197. $data['nav']='settings';
  198. $data['export']=$log;
  199. $this->init_model->display_template('settings/utilities', $data, 'admin');
  200. }
  201. function generate_tree_list($array, $parent = 0, $level = 0)
  202. {
  203. // Reset the flag each time the function is called
  204. $has_children = FALSE;
  205. // Loop through each item of the list array
  206. foreach($array as $key => $value)
  207. {
  208. // For the first run, get the first item with a parent_id of 0 (= root category)
  209. // (or whatever id is passed to the function)
  210. //
  211. // For every subsequent run, look for items with a parent_id matching the current item's key (id)
  212. // (eg. get all items with a parent_id of 2)
  213. //
  214. // This will return FALSE (stop) when it find no more matching items/children
  215. //
  216. // If this array item's parent_id value is the same as that passed to the function
  217. // eg. [parent_id] => 0 == $parent = 0 (TRUE)
  218. // eg. [parent_id] => 20 == $parent = 0 (FALSE)
  219. //
  220. if ($value['cat_parent'] == $parent)
  221. {
  222. // Only print the wrapper ('<ul>') if this is the first child (otherwise just print the item)
  223. // Will be FALSE each time the function is called again
  224. if ($has_children === FALSE)
  225. {
  226. // Switch the flag, start the list wrapper, increase the level count
  227. $has_children = TRUE;
  228. if($level==0)
  229. {
  230. echo '<ul id="browser" class="filetree">
  231. ';
  232. }
  233. else
  234. {
  235. echo "\n".'<ul>';
  236. }
  237. $level++;
  238. }
  239. // Print the list item
  240. echo "\n".'<li><span class="folder">' . $value['cat_name'] . '</span>';
  241. echo $this->get_articles($value['cat_id'], $value['cat_uri']);
  242. // Repeat function, using the current item's key (id) as the parent_id argument
  243. // Gives us a nested list of subcategories
  244. $this->generate_tree_list($array, $key, $level);
  245. // Close the item
  246. echo "</li>\n";
  247. }
  248. }
  249. // If we opened the wrapper above, close it.
  250. if ($has_children === TRUE) echo '</ul>'."\n";
  251. }
  252. function get_articles($id, $cat_uri = '')
  253. {
  254. $this->db->from('articles');
  255. $this->db->join('article2cat', 'articles.article_id = article2cat.article_id', 'left');
  256. $this->db->where('category_id', $id);
  257. $this->db->where('article_display', 'Y');
  258. $query = $this->db->get();
  259. $output = '<ul>';
  260. if ($query->num_rows() > 0)
  261. {
  262. foreach($query->result() as $rs)
  263. {
  264. $title = $rs->article_title;
  265. $uri = $rs->article_uri;
  266. $output .= '<li><span class="file"><a href="./'.$uri.'.html">'.$title.'</a></span></li>';
  267. }
  268. }
  269. else
  270. {
  271. //$output.='<li></li>';
  272. }
  273. $output.='</ul>';
  274. return $output;
  275. }
  276. function recurse_copy($src,$dst)
  277. {
  278. $dir = opendir($src);
  279. @mkdir($dst);
  280. while(FALSE !== ( $file = readdir($dir)) )
  281. {
  282. if (( $file != '.' ) && ( $file != '..' ))
  283. {
  284. if ( is_dir($src . '/' . $file) )
  285. {
  286. $this->recurse_copy($src . '/' . $file,$dst . '/' . $file);
  287. }
  288. else
  289. {
  290. copy($src . '/' . $file,$dst . '/' . $file);
  291. }
  292. }
  293. }
  294. closedir($dir);
  295. }
  296. }
  297. /* End of file utility.php */
  298. /* Location: ./upload/includes/application/controllers/admin/utility.php */