PageRenderTime 23ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/Account_Import.php

https://gitlab.com/clearos/app-account-import
PHP | 320 lines | 137 code | 60 blank | 123 comment | 10 complexity | fc2720405c8ce9ea22daa3638dc5643b MD5 | raw file
  1. <?php
  2. /**
  3. * Account import/export class.
  4. *
  5. * @category apps
  6. * @package account-import
  7. * @subpackage libraries
  8. * @author ClearFoundation <developer@clearfoundation.com>
  9. * @copyright 2003-2014 ClearFoundation
  10. * @license http://www.gnu.org/copyleft/lgpl.html GNU Lesser General Public License version 3 or later
  11. * @link http://www.clearfoundation.com/docs/developer/apps/account_import/
  12. */
  13. ///////////////////////////////////////////////////////////////////////////////
  14. //
  15. // This program is free software: you can redistribute it and/or modify
  16. // it under the terms of the GNU Lesser General Public License as published by
  17. // the Free Software Foundation, either version 3 of the License, or
  18. // (at your option) any later version.
  19. //
  20. // This program is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. // GNU Lesser General Public License for more details.
  24. //
  25. // You should have received a copy of the GNU Lesser General Public License
  26. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // N A M E S P A C E
  31. ///////////////////////////////////////////////////////////////////////////////
  32. namespace clearos\apps\account_import;
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // B O O T S T R A P
  35. ///////////////////////////////////////////////////////////////////////////////
  36. $bootstrap = getenv('CLEAROS_BOOTSTRAP') ? getenv('CLEAROS_BOOTSTRAP') : '/usr/clearos/framework/shared';
  37. require_once $bootstrap . '/bootstrap.php';
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // T R A N S L A T I O N S
  40. ///////////////////////////////////////////////////////////////////////////////
  41. clearos_load_language('base');
  42. clearos_load_language('account_import');
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // D E P E N D E N C I E S
  45. ///////////////////////////////////////////////////////////////////////////////
  46. // Classes
  47. //--------
  48. use \clearos\apps\base\Engine as Engine;
  49. use \clearos\apps\base\File as File;
  50. use \clearos\apps\base\Script as Script;
  51. use \clearos\apps\base\Shell as Shell;
  52. clearos_load_library('base/Engine');
  53. clearos_load_library('base/File');
  54. clearos_load_library('base/Script');
  55. clearos_load_library('base/Shell');
  56. // Exceptions
  57. //-----------
  58. use \Exception as Exception;
  59. use \clearos\apps\base\Engine_Exception as Engine_Exception;
  60. use \clearos\apps\base\File_Not_Found_Exception as File_Not_Found_Exception;
  61. clearos_load_library('base/Engine_Exception');
  62. clearos_load_library('base/File_Not_Found_Exception');
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // C L A S S
  65. ///////////////////////////////////////////////////////////////////////////////
  66. /**
  67. * Account import/export class.
  68. *
  69. * @category apps
  70. * @package account-import
  71. * @subpackage libraries
  72. * @author ClearFoundation <developer@clearfoundation.com>
  73. * @copyright 2003-2014 ClearFoundation
  74. * @license http://www.gnu.org/copyleft/lgpl.html GNU Lesser General Public License version 3 or later
  75. * @link http://www.clearfoundation.com/docs/developer/apps/account_import/
  76. */
  77. class Account_Import extends Engine
  78. {
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // C O N S T A N T S
  81. ///////////////////////////////////////////////////////////////////////////////
  82. const FILE_CSV = 'import.csv';
  83. const FILE_CSV_TEMPLATE = 'import_template.csv';
  84. const FILE_STATUS = 'account_import.json';
  85. const COMMAND_IMPORT = '/usr/sbin/account-import';
  86. const COMMAND_PS = '/bin/ps';
  87. const FOLDER_ACCOUNT_IMPORT = '/var/clearos/account_import';
  88. ///////////////////////////////////////////////////////////////////////////////
  89. // M E T H O D S
  90. ///////////////////////////////////////////////////////////////////////////////
  91. /**
  92. * Account Import/Export constructor.
  93. */
  94. function __construct()
  95. {
  96. clearos_profile(__METHOD__, __LINE__);
  97. }
  98. /**
  99. * Returns boolean indicating whether import is currently running.
  100. *
  101. * @return boolean
  102. * @throws Engine_Exception
  103. */
  104. function is_import_in_progress()
  105. {
  106. clearos_profile(__METHOD__, __LINE__);
  107. $script = new Script(basename(self::COMMAND_IMPORT));
  108. return $script->is_running();
  109. }
  110. /**
  111. * Returns JSON-encoded data indicating progress of import currently running.
  112. *
  113. * @return string
  114. * @throws Engine_Exception
  115. */
  116. function get_progress()
  117. {
  118. clearos_profile(__METHOD__, __LINE__);
  119. $file = new File(CLEAROS_TEMP_DIR . "/" . self::FILE_STATUS, FALSE);
  120. $status = array();
  121. if (!$file->exists())
  122. throw new Engine_Exception(lang('account_import_no_data_found'));
  123. $lines = $file->get_contents_as_array();
  124. if (empty($lines))
  125. throw new Engine_Exception(lang('account_import_no_data_found'));
  126. else
  127. $lines = array_reverse($lines);
  128. foreach ($lines as $line)
  129. $status[] = json_decode($line);
  130. return $status;
  131. }
  132. /**
  133. * Performs an account import.
  134. *
  135. * @return void
  136. * @throws Engine_Exception, File_Not_Found_Exception
  137. */
  138. function import()
  139. {
  140. clearos_profile(__METHOD__, __LINE__);
  141. if ($this->is_import_in_progress())
  142. throw new Engine_Exception(lang('account_import_import_already_in_progress'));
  143. $file = new File(self::FOLDER_ACCOUNT_IMPORT . '/' . self::FILE_CSV, TRUE);
  144. if (!$file->exists())
  145. throw new File_Not_Found_Exception(lang('account_import_csv_file_not_found'));
  146. $this->delete_log();
  147. $options = array();
  148. $options['background'] = TRUE;
  149. $shell = new Shell();
  150. $shell->execute(self::COMMAND_IMPORT, '', TRUE, $options);
  151. }
  152. /**
  153. * Puts the CSV file in the cache directory, ready for import begin.
  154. *
  155. * @param string $filename string CSV filename
  156. *
  157. * @return void
  158. * @throws Engine_Exception, File_Not_Found_Exception
  159. */
  160. function set_csv_file($filename)
  161. {
  162. clearos_profile(__METHOD__, __LINE__);
  163. try {
  164. $file = new File(CLEAROS_TEMP_DIR . '/' . $filename, TRUE);
  165. if (!$file->exists())
  166. throw new File_Not_Found_Exception(clearos_exception_message($e));
  167. // Move uploaded file to cache
  168. $file->move_to(self::FOLDER_ACCOUNT_IMPORT . '/' . self::FILE_CSV);
  169. $file->chown('root', 'root');
  170. $file->chmod(600);
  171. } catch (File_Not_Found_Exception $e) {
  172. throw new File_Not_Found_Exception(clearos_exception_message($e));
  173. }
  174. }
  175. /**
  176. * Returns state of CSV file upload.
  177. *
  178. * @return boolean state of CSV file upload
  179. * @throws Engine_Exception, File_Not_Found_Exception
  180. */
  181. function is_csv_file_uploaded()
  182. {
  183. clearos_profile(__METHOD__, __LINE__);
  184. try {
  185. $file = new File(self::FOLDER_ACCOUNT_IMPORT . '/' . self::FILE_CSV, TRUE);
  186. if (!$file->exists())
  187. return FALSE;
  188. return TRUE;
  189. } catch (Exception $e) {
  190. return FALSE;
  191. }
  192. }
  193. /**
  194. * Deletes log file.
  195. *
  196. * @return void
  197. * @throws Engine_Exception
  198. */
  199. function delete_log()
  200. {
  201. clearos_profile(__METHOD__, __LINE__);
  202. $file = new File(CLEAROS_TEMP_DIR . "/" . self::FILE_STATUS, FALSE);
  203. if ($file->exists())
  204. $file->delete();
  205. }
  206. /**
  207. * Resets (deletes) the CSV file.
  208. *
  209. * @return void
  210. * @throws Engine_Exception, File_Not_Found_Exception
  211. */
  212. function delete_csv_file()
  213. {
  214. clearos_profile(__METHOD__, __LINE__);
  215. try {
  216. $file = new File(self::FOLDER_ACCOUNT_IMPORT . '/' . self::FILE_CSV, TRUE);
  217. if (!$file->exists())
  218. throw new File_Not_Found_Exception(lang('account_import_csv_file_not_found'));
  219. $file->delete();
  220. } catch (File_Not_Found_Exception $e) {
  221. throw new File_Not_Found_Exception(clearos_exception_message($e));
  222. }
  223. }
  224. /**
  225. * Resets (deletes) the CSV file.
  226. *
  227. * @return integer size
  228. * @throws Engine_Exception, File_Not_Found_Exception
  229. */
  230. function get_csv_size()
  231. {
  232. clearos_profile(__METHOD__, __LINE__);
  233. try {
  234. $file = new File(self::FOLDER_ACCOUNT_IMPORT . '/' . self::FILE_CSV, TRUE);
  235. if (!$file->exists())
  236. throw new File_Not_Found_Exception(lang('account_import_csv_file_not_found'));
  237. return $file->get_size();
  238. } catch (File_Not_Found_Exception $e) {
  239. throw new File_Not_Found_Exception(clearos_exception_message($e));
  240. }
  241. }
  242. /**
  243. * Returns the number of records.
  244. *
  245. * @return integer the number of records
  246. * @throws Engine_Exception, File_Not_Found_Exception
  247. */
  248. function get_number_of_records()
  249. {
  250. clearos_profile(__METHOD__, __LINE__);
  251. try {
  252. $file = new File(self::FOLDER_ACCOUNT_IMPORT . '/' . self::FILE_CSV, TRUE);
  253. if (!$file->exists())
  254. throw new File_Not_Found_Exception(lang('account_import_csv_file_not_found'));
  255. return count($file->get_contents_as_array()) - 1;
  256. } catch (File_Not_Found_Exception $e) {
  257. throw new File_Not_Found_Exception(clearos_exception_message($e));
  258. }
  259. }
  260. }