ScriptCaller.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. class ScriptCaller
  3. {
  4. // um/edit - ??
  5. static public $directory = '/opt/usic/bin/';
  6. static public $scripts = array(
  7. 'auth' => 'usiccheckpasswd',
  8. 'um/add' => 'usic_useradd',
  9. 'um/find' => 'usic_userinfo',
  10. 'um/delete' => 'usic_userdel',
  11. 'um/activate' => 'activateAccount',
  12. 'um/agreement' => 'usicagreement',
  13. 'um/edit' => 'usic_usermod',
  14. 'gm/add' => 'usicgroup_add',
  15. 'gm/addUser' => 'usicgroup_addUser',
  16. 'gm/checkUser' => 'usicgroup_checkUser',
  17. 'gm/show' => 'usicgroup_show',
  18. 'gm/showUser' => 'usicgroup_show',
  19. 'gm/remove' => 'usicgroup_remove',
  20. 'gm/removeUser' => 'usicgroup_remove');
  21. protected $type = null;
  22. /* public function __construct($type)
  23. {
  24. if ( !array_key_exists($type,self::$scripts) )
  25. throw Exception('You know such a script? i don\'t');
  26. $this->type = self::$scripts[$type];
  27. }*/
  28. public function callScript($type, $arg=null)
  29. {
  30. if ( !array_key_exists($type,self::$scripts) )
  31. throw Exception('You know such a script? i don\'t');
  32. $this->type = self::$scripts[$type];
  33. return $this->{$this->type}($arg);
  34. }
  35. protected function usiccheckpasswd($arg)
  36. {
  37. $command = "echo -e \"".$arg["password"]."\\n\" | ".self::$directory."usiccheckpasswd ".$arg["login"];
  38. exec($command, $values, $result);
  39. return $this->processAuthErrors($result);
  40. }
  41. protected function processAuthErrors($error)
  42. {
  43. switch ($error)
  44. {
  45. case 0:
  46. case 1:
  47. case 2:
  48. sfContext::getInstance()->getLogger()->info($this->type.' called with return code '.$error.' '.$this->authErrors[$error]);
  49. return $this->authErrors[$error];
  50. case 15:
  51. case 31:
  52. case 42:
  53. sfContext::getInstance()->getLogger()->alert($this->type.' called with return code '.$error.': '.$this->authErrors[$error]);
  54. throw new Exception('system isn\'t working properly, talk with administrator');
  55. return $this->authErrors[$error];
  56. default:
  57. sfContext::getInstance()->getLogger()->emerg($this->type.' called with return code '.$error);
  58. throw new Exception('system isn\'t working properly, talk with administrator');
  59. return 'wtf';
  60. }
  61. }
  62. protected function usic_useradd($arg)
  63. {
  64. $values = array();
  65. $result = 0;
  66. $command = "echo -e \"login=".$arg['login']."\\n name=".$arg['surname']." ".$arg['name']." ".$arg['middle_name'].
  67. "\\n password=".$arg['password']."\\n entry_year=".$arg['entering_year']."\\n profession=".$arg['profession'].
  68. "\\n class=".$arg['status'].
  69. "\\n reader_card_number=".$arg['reader_card'].
  70. "\\n student_card_number=".$arg['student_card'].
  71. "\\n passport_number=".$arg['passport'].
  72. "\\n\" | ".self::$directory."usic_useradd";
  73. // print_r($command);
  74. exec($command, $values, $result);
  75. // $this->doHistory('registrate',$command,$result); // for 'do not display password, idiot!' :)
  76. $this->doHistory('registrate',"echo -e \"login=".$arg['login']." ... \" | ..usic_useradd",$result);
  77. $return = $this->processUserErrors($result);
  78. return $return;
  79. }
  80. protected function usic_usermod($arg)
  81. {
  82. $values = array();
  83. $result = 0;
  84. $command = "echo -e \"login=".$arg['login'];
  85. if (!is_null($arg['surname'])) $command .= "\\n name=".$arg['surname']." ".$arg['name']." ".$arg['middle_name'];
  86. if (!is_null($arg['password'])) $command .= "\\n password=".$arg['password'];
  87. if (!is_null($arg['entering_year'])) $command .= "\\n entry_year=".$arg['entering_year'];
  88. if (!is_null($arg['profession'])) $command .= "\\n profession=".$arg['profession'];
  89. if (!is_null($arg['status'])) $command .= "\\n class=".$arg['status'];
  90. /* XXX: temporary fix : we are not editing cards and passport */
  91. // "\\n reader_card_number=".$arg['reader_card'].
  92. // "\\n student_card_number=".$arg['student_card'].
  93. // "\\n passport_number=".$arg['passport'].
  94. $command .= "\" | ".self::$directory."usic_usermod";
  95. exec($command, $values, $result);
  96. $this->doHistory('modify account',"echo -e \"login=".$arg['login']." ... \" | ..usic_usermod",$result);
  97. $return = $this->processUserErrors($result);
  98. return $return;
  99. }
  100. protected function usic_userinfo ($args)
  101. {
  102. $values = array();
  103. $result = 0;
  104. $command = "echo -e \"login=".$args['login']."\\n values=".
  105. (is_null($args['values']) ? implode(",", array_keys(self::$ldapFields)) : implode(',', $args['values']))
  106. ."\" | ".self::$directory."usic_userinfo";
  107. exec($command, $values, $result);
  108. // print_r($values);
  109. sfContext::getInstance()->getLogger()->info($this->type.' called as '.$command);
  110. $return = $this->processUserErrors($result);
  111. if (is_null($return))
  112. return $this->constructUserArray($values);
  113. return $return;
  114. }
  115. protected function usic_userdel($args)
  116. {
  117. $values = array();
  118. $result = 0;
  119. $command = "echo ".$args['login']." | ".self::$directory."usic_userdel";
  120. exec($command, $values, $result);
  121. $this->doHistory('delete account',$command,$result);
  122. $return = $this->processUserErrors($result);
  123. return $return;
  124. }
  125. protected function processUserErrors($error)
  126. {
  127. switch ($error)
  128. {
  129. case 0:
  130. case 32:
  131. case 33:
  132. case 34:
  133. case 35:
  134. case 36:
  135. sfContext::getInstance()->getLogger()->info($this->type.' called with return code '.$error.' '.$this->userErrors[$error]);
  136. return $this->userErrors[$error];
  137. case 11:
  138. case 15:
  139. case 21:
  140. case 22:
  141. case 23:
  142. case 31:
  143. sfContext::getInstance()->getLogger()->alert($this->type.' called with return code '.$error.': '.$this->userErrors[$error]);
  144. throw new Exception('system isn\'t working properly, talk with administrator');
  145. return $this->userErrors[$error];
  146. default:
  147. sfContext::getInstance()->getLogger()->emerg($this->type.' called with return code '.$error);
  148. throw new Exception('system isn\'t working properly, talk with administrator');
  149. return 'wtf';
  150. }
  151. }
  152. protected function activateAccount($args)
  153. {
  154. if ( $this->usickey($args)!=0 )
  155. return 'wrong code number';
  156. if ($this->usic_useractivate($args)==0)
  157. return null;
  158. return 'unsuccessful activation';
  159. }
  160. protected function usickey($args)
  161. {
  162. exec(self::$directory."usickey ".$args['login']." ".$args['code'], $values, $result);
  163. return $result;
  164. }
  165. protected function usic_useractivate($args)
  166. {
  167. $command = self::$directory."usic_useractivate ".$args['login'];
  168. exec($command, $values, $result);
  169. $this->doHistory('activate account',$command,$result);
  170. return $result;
  171. }
  172. protected function usic_userdeactivate($args)
  173. {
  174. $command = self::$directory."usic_userdeactivate ".$args['login'];
  175. exec($command, $values, $result);
  176. $this->doHistory('deactivate account',$command,$result);
  177. return $result;
  178. }
  179. protected function usicagreement($args)
  180. {
  181. $command = self::$directory."usicagreement ".$args['uid']." ".$args['login']." ".$args['surname']." ".$args['name']." ".$args['middle_name'];
  182. exec($command, $values, $result);
  183. $this->doHistory('print agreement',$command,$result);
  184. if ($result==0) return null;
  185. return 'unsuccessful printing';
  186. }
  187. protected function usicgroup_add($args)
  188. {
  189. $values = array();
  190. $result = 0;
  191. $command = self::$directory."usicgroup add ".$args['name'];
  192. exec($command, $values, $result);
  193. $this->doHistory('add group',$command,$result);
  194. return $this->processGroupErrors($result);
  195. }
  196. protected function usicgroup_addUser($args)
  197. {
  198. $values = array();
  199. $result = 0;
  200. $command = self::$directory."usicgroup add ".$args['name']." ".$args['login'];
  201. exec($command, $values, $result);
  202. $this->doHistory('add user to group',$command,$result);
  203. return $this->processGroupErrors($result);
  204. }
  205. protected function usicgroup_checkUser($args)
  206. {
  207. $values = array();
  208. $result = 0;
  209. $command = self::$directory."usicgroup check ".$args['name']." ".$args['login'];
  210. exec($command, $values, $result);
  211. return $this->processGroupErrors($result);
  212. }
  213. protected function usicgroup_show($args)
  214. {
  215. $values = array();
  216. $result = 0;
  217. $command = self::$directory."usicgroup show ".$args['name'];
  218. exec($command, $values, $result);
  219. $return = $this->processGroupErrors($result);
  220. if (is_null($return))
  221. return $values;
  222. return $return;
  223. }
  224. protected function usicgroup_remove($args)
  225. {
  226. $values = array();
  227. $result = 0;
  228. $command = self::$directory."usicgroup remove ".$args['name']." ".$args['login'];
  229. exec($command, $values, $result);
  230. $this->doHistory(is_null($args['login']) ? 'delete group' : 'delete user from group', $command, $result);
  231. return $this->processGroupErrors($result);
  232. }
  233. protected function processGroupErrors($error)
  234. {
  235. switch ($error)
  236. {
  237. case 0:
  238. case 1:
  239. case 2:
  240. case 32:
  241. case 33:
  242. sfContext::getInstance()->getLogger()->info($this->type.' called with return code '.$error.': '.$this->groupErrors[$error]);
  243. return $this->groupErrors[$error];
  244. case 15:
  245. case 22:
  246. case 31:
  247. sfContext::getInstance()->getLogger()->alert($this->type.' called with return code '.$error.': '.$this->groupErrors[$error]);
  248. throw new Exception('system isn\'t working properly, talk with administrator');
  249. return $this->groupErrors[$error];
  250. default:
  251. sfContext::getInstance()->getLogger()->emerg($this->type.' called with return code '.$error);
  252. throw new Exception('system isn\'t working properly, talk with administrator');
  253. return 'wtf';
  254. }
  255. }
  256. private $authErrors = array ( 1 => 'you have entered not valid password',
  257. 2 => 'you have entered not valid login',
  258. 15 => 'they dumped us',
  259. 31 => 'Calling Elvis, anybody home?',
  260. 42 => 'Goddamit, universal question solved!',
  261. 0 => null );
  262. private $userErrors = array ( 11 => 'bind with database failed',
  263. 15 => 'LDAP doesn\'t like you',
  264. 21 => 'your folder can\'t be created',
  265. 22 => 'your present haven\'t been received in a proper way',
  266. 23 => 'your folder can\'t be deleted',
  267. 31 => 'an internal error of the script',
  268. 32 => 'search can\'t find anything for you',
  269. 33 => 'you can\'t be duplicated, even if you want',
  270. 34 => 'your reader card already exists!',
  271. 35 => 'your student card already exists!',
  272. 36 => 'your passport already exists!',
  273. 0 => null );
  274. private $groupErrors = array ( 1 => 'there is no such user in this group',
  275. 2 => 'nothing to show',
  276. 15 => 'LDAP doesn\'t like you',
  277. 22 => 'script doesn\'t know such parameters',
  278. 31 => 'script has its own problems',
  279. 32 => 'there is no such group',
  280. 33 => 'such group already exists',
  281. 0 => null );
  282. static private $ldapFields = array('login' => 'login',
  283. 'name' => 'name',
  284. 'uid' => 'uid',
  285. 'gid' => 'gid',
  286. 'profession' => 'profession',
  287. 'class' => 'status',
  288. 'loginShell' => 'loginShell',
  289. 'entry_year' => 'entering_year',
  290. 'reader_card_number' => 'reader_card',
  291. 'student_card_number' => 'student_card',
  292. 'passport_number' => 'passport');
  293. private function constructUserArray($values)
  294. {
  295. $arr = array();
  296. foreach($values as $value)
  297. {
  298. $str = explode("=", $value);
  299. $arr[self::$ldapFields[$str[0]]] = $str[1];
  300. }
  301. if (key_exists('name', $arr))
  302. {
  303. $snm = explode(" ", $arr['name']);
  304. $arr['surname'] = $snm[0];
  305. $arr['name'] = $snm[1];
  306. $arr['middle_name'] = $snm[2];
  307. }
  308. /* for student card numbers like XX0XXXXXXX script returns the number without zero */
  309. /* returning to browser the numbers of 10 symbols */
  310. /* note: only if there is one leading zero (i.e. not less 9 symbols) */
  311. if (key_exists('student_card', $arr) && preg_match('/^..[0-9]{7}$/u', $arr['student_card']))
  312. {
  313. $arr['student_card'] = substr($arr['student_card'], 0, 4) . '0' . substr($arr['student_card'], 4);
  314. }
  315. //print_r($arr);
  316. return $arr;
  317. }
  318. private function doHistory($action, $command, $result)
  319. {
  320. $entry = new HistoryTable( sfContext::getInstance()->getUser()->getAttribute('login'),
  321. sfContext::getInstance()->getUser()->getAttribute('group'), $action, $command, $result);
  322. $entry->save();
  323. }
  324. }