FluxdServiceMod.Watch.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /* $Id: FluxdServiceMod.Watch.php 2980 2007-05-11 21:21:32Z warion $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. // class for the Fluxd-Service-module Watch
  16. class FluxdWatch extends FluxdServiceMod
  17. {
  18. // private fields
  19. // jobs-delim
  20. var $_delimJobs = ";";
  21. // job-delim
  22. var $_delimJob = ":";
  23. // component-delim
  24. var $_delimComponent = "=";
  25. // =========================================================================
  26. // public static methods
  27. // =========================================================================
  28. /**
  29. * accessor for singleton
  30. *
  31. * @return FluxdWatch
  32. */
  33. function getInstance() {
  34. global $instanceFluxdWatch;
  35. // initialize if needed
  36. if (!isset($instanceFluxdWatch))
  37. FluxdWatch::initialize();
  38. return $instanceFluxdWatch;
  39. }
  40. /**
  41. * initialize FluxdWatch.
  42. */
  43. function initialize() {
  44. global $instanceFluxdWatch;
  45. // create instance
  46. if (!isset($instanceFluxdWatch))
  47. $instanceFluxdWatch = new FluxdWatch();
  48. }
  49. /**
  50. * getState
  51. *
  52. * @return state
  53. */
  54. function getState() {
  55. global $instanceFluxdWatch;
  56. return (isset($instanceFluxdWatch))
  57. ? $instanceFluxdWatch->state
  58. : FLUXDMOD_STATE_NULL;
  59. }
  60. /**
  61. * getMessages
  62. *
  63. * @return array
  64. */
  65. function getMessages() {
  66. global $instanceFluxdWatch;
  67. return (isset($instanceFluxdWatch))
  68. ? $instanceFluxdWatch->messages
  69. : array();
  70. }
  71. /**
  72. * getModState
  73. *
  74. * @return state
  75. */
  76. function getModState() {
  77. global $instanceFluxdWatch;
  78. return (isset($instanceFluxdWatch))
  79. ? $instanceFluxdWatch->modstate
  80. : FLUXDMOD_STATE_NULL;
  81. }
  82. /**
  83. * isRunning
  84. *
  85. * @return boolean
  86. */
  87. function isRunning() {
  88. global $instanceFluxdWatch;
  89. return (isset($instanceFluxdWatch))
  90. ? ($instanceFluxdWatch->modstate == FLUXDMOD_STATE_RUNNING)
  91. : false;
  92. }
  93. /**
  94. * get jobs-list
  95. *
  96. * @return jobs-list as array or false on error
  97. */
  98. function jobsGetList() {
  99. global $instanceFluxdWatch;
  100. return $instanceFluxdWatch->instance_jobsGetList();
  101. }
  102. /**
  103. * get job-content
  104. *
  105. * @param $jobnumber
  106. * @return job as array or false on error
  107. */
  108. function jobGetContent($jobnumber) {
  109. global $instanceFluxdWatch;
  110. return $instanceFluxdWatch->instance_jobGetContent($jobnumber);
  111. }
  112. /**
  113. * add a job
  114. *
  115. * @param $watchdir
  116. * @param $user
  117. * @param $profile
  118. * @param $checkdir
  119. * @return boolean
  120. */
  121. function jobAdd($watchdir, $user, $profile, $checkdir = false) {
  122. global $instanceFluxdWatch;
  123. return $instanceFluxdWatch->instance_jobAdd($watchdir, $user, $profile, $checkdir);
  124. }
  125. /**
  126. * update a job
  127. *
  128. * @param $jobnumber
  129. * @param $watchdir
  130. * @param $user
  131. * @param $profile
  132. * @param $checkdir
  133. * @return boolean
  134. */
  135. function jobUpdate($jobnumber, $watchdir, $user, $profile, $checkdir = false) {
  136. global $instanceFluxdWatch;
  137. return $instanceFluxdWatch->instance_jobUpdate($jobnumber, $watchdir, $user, $profile, $checkdir);
  138. }
  139. /**
  140. * delete a job
  141. *
  142. * @param $jobnumber
  143. * @return boolean
  144. */
  145. function jobDelete($jobnumber) {
  146. global $instanceFluxdWatch;
  147. return $instanceFluxdWatch->instance_jobDelete($jobnumber);
  148. }
  149. // =========================================================================
  150. // ctor
  151. // =========================================================================
  152. /**
  153. * ctor
  154. */
  155. function FluxdWatch() {
  156. // name
  157. $this->moduleName = "Watch";
  158. // initialize
  159. $this->instance_initialize();
  160. }
  161. // =========================================================================
  162. // public methods
  163. // =========================================================================
  164. /**
  165. * get jobs-list
  166. *
  167. * @return jobs-list as array or false on error
  168. */
  169. function instance_jobsGetList() {
  170. global $cfg;
  171. // Jobs: job1;job2;job3
  172. // Job: U=user:[P=profile:]D=watchdir
  173. if (isset($cfg["fluxd_Watch_jobs"])) {
  174. $joblist = array();
  175. $jobs = explode($this->_delimJobs, trim($cfg["fluxd_Watch_jobs"]));
  176. if (count($jobs) > 0) {
  177. $jobCount = 0;
  178. foreach ($jobs as $job) {
  179. $rest = trim($job);
  180. if (!isset($rest) || strlen($rest) == 0)
  181. continue;
  182. $jobCount++;
  183. $jobEntry = array();
  184. while (true) {
  185. if (preg_match('/^D'.$this->_delimComponent.'/', $rest) > 0) {
  186. // Dir: final component.
  187. $jobEntry['D'] = substr($rest, 2);
  188. break;
  189. } else {
  190. // Other component.
  191. $jobAry = explode($this->_delimJob, $rest, 2);
  192. if (count($jobAry) != 2 || preg_match('/^\s*[A-Z]'.$this->_delimComponent.'/', $jobAry[1]) == 0) {
  193. array_push($this->messages, "invalid format for job ".$jobCount.".");
  194. // Can't really return an error here... still load other jobs.
  195. //return false;
  196. $jobEntry['D'] = '';
  197. break;
  198. }
  199. $rest = trim(array_shift($jobAry));
  200. $jobEntry[$rest[0]] = substr($rest, 2);
  201. $rest = trim(array_shift($jobAry));
  202. }
  203. }
  204. if (
  205. isset($jobEntry['U']) && strlen($jobEntry['U']) > 0 &&
  206. isset($jobEntry['D']) && strlen($jobEntry['D']) > 0
  207. )
  208. array_push($joblist, $jobEntry);
  209. }
  210. }
  211. // An empty job list is not an error.
  212. return $joblist;
  213. }
  214. return false;
  215. }
  216. /**
  217. * get job-content
  218. *
  219. * @param $jobnumber
  220. * @return job as array or false on error
  221. */
  222. function instance_jobGetContent($jobnumber) {
  223. $jobInt = intval($jobnumber);
  224. if ($jobInt > 0) {
  225. $jobs = $this->instance_jobsGetList();
  226. if ($jobs !== false && count($jobs) >= $jobInt)
  227. return $jobs[$jobInt - 1];
  228. }
  229. return false;
  230. }
  231. /**
  232. * add a job
  233. *
  234. * @param $watchdir
  235. * @param $user
  236. * @param $profile
  237. * @param $checkdir
  238. * @return boolean
  239. */
  240. function instance_jobAdd($watchdir, $user, $profile, $checkdir = false) {
  241. if (strlen($watchdir) > 0 && strlen($user) > 0) {
  242. $watchdir = checkDirPathString($watchdir);
  243. // Get current jobs and make sure new one is not a duplicate.
  244. $jobs = $this->instance_jobsGetList();
  245. if ($jobs !== false) {
  246. foreach ($jobs as $job)
  247. if (isset($job['D']) && $job['D'] == $watchdir) {
  248. array_push($this->messages, "dir ".$watchdir." is already begin watched.");
  249. return false;
  250. }
  251. // Check/create dir if needed.
  252. if ($checkdir && !checkDirectory($watchdir)) {
  253. array_push($this->messages, "dir ".$watchdir." does not exist and could not be created.");
  254. return false;
  255. }
  256. // Create new job.
  257. $job = array(
  258. 'U' => $user,
  259. 'D' => $watchdir
  260. );
  261. if (isset($profile))
  262. $job['P'] = $profile;
  263. array_push($jobs, $job);
  264. // Update settings.
  265. return $this->_jobsUpdateSetting($jobs);
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * update a job
  272. *
  273. * @param $jobnumber
  274. * @param $watchdir
  275. * @param $user
  276. * @param $profile
  277. * @param $checkdir
  278. * @return boolean
  279. */
  280. function instance_jobUpdate($jobnumber, $watchdir, $user, $profile, $checkdir = false) {
  281. $jobInt = intval($jobnumber);
  282. if ($jobInt > 0 && strlen($watchdir) > 0 && strlen($user) > 0) {
  283. $watchdir = checkDirPathString($watchdir);
  284. // Get current jobs and make sure modified one is not a duplicate.
  285. $jobs = $this->instance_jobsGetList();
  286. if ($jobs !== false && count($jobs) >= $jobInt) {
  287. $jobs[$jobInt - 1] = array();
  288. foreach ($jobs as $job)
  289. if (isset($job['D']) && $job['D'] == $watchdir) {
  290. array_push($this->messages, "dir ".$watchdir." is already begin watched.");
  291. return false;
  292. }
  293. // Check/create dir if needed.
  294. if ($checkdir && !checkDirectory($watchdir)) {
  295. array_push($this->messages, "dir ".$watchdir." does not exist and could not be created.");
  296. return false;
  297. }
  298. // Create new job.
  299. $job = array(
  300. 'U' => $user,
  301. 'D' => $watchdir
  302. );
  303. if (isset($profile))
  304. $job['P'] = $profile;
  305. $jobs[$jobInt - 1] = $job;
  306. // Update settings.
  307. return $this->_jobsUpdateSetting($jobs);
  308. }
  309. }
  310. return false;
  311. }
  312. /**
  313. * delete a job
  314. *
  315. * @param $jobnumber
  316. * @return boolean
  317. */
  318. function instance_jobDelete($jobnumber) {
  319. $jobInt = intval($jobnumber);
  320. if ($jobInt > 0) {
  321. $jobs = $this->instance_jobsGetList();
  322. if ($jobs !== false && count($jobs) >= $jobInt) {
  323. array_splice($jobs, $jobInt - 1, 1);
  324. return $this->_jobsUpdateSetting($jobs);
  325. }
  326. }
  327. return false;
  328. }
  329. // =========================================================================
  330. // private methods
  331. // =========================================================================
  332. /**
  333. * build new jobs-list string from jobs-list array
  334. *
  335. * @param $jobs jobs-list as array (each item being a job as array)
  336. * @return string
  337. */
  338. function _jobsSerialize($jobs) {
  339. $return = '';
  340. foreach ($jobs as $job) {
  341. if (
  342. !isset($job['U']) || strlen(trim($job['U'])) <= 0 ||
  343. !isset($job['D']) || strlen(trim($job['D'])) <= 0
  344. )
  345. continue;
  346. // New format: U= component must be first.
  347. $jobstr = 'U' . $this->_delimComponent . $job['U'] . $this->_delimJob;
  348. foreach ($job as $k => $v)
  349. if ($k != 'U' && $k != 'D' && strlen($k) > 0 && strlen($v) > 0)
  350. $jobstr .= $k . $this->_delimComponent . $v . $this->_delimJob;
  351. // D= component must be last -- make sure it has a trailing slash.
  352. $jobstr .= 'D' . $this->_delimComponent . checkDirPathString($job['D']);
  353. $return .= (strlen($return) == 0 ? '' : $this->_delimJobs) . $jobstr;
  354. }
  355. return $return;
  356. }
  357. /**
  358. * store new jobs-list setting
  359. *
  360. * @param $jobs jobs-list as array
  361. * @return boolean
  362. */
  363. function _jobsUpdateSetting($jobs) {
  364. global $cfg;
  365. // build setting value
  366. $setting = $this->_jobsSerialize($jobs);
  367. if ($setting === false)
  368. return false;
  369. // update setting
  370. updateSetting("tf_settings", "fluxd_Watch_jobs", $setting);
  371. // log
  372. AuditAction($cfg["constants"]["fluxd"], "Watch Jobs Saved : \n".$setting);
  373. return true;
  374. }
  375. }
  376. ?>