PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ser-0.9.6/serweb-0.9.4/application_layer/apu_aliases.php

#
PHP | 308 lines | 172 code | 60 blank | 76 comment | 25 complexity | bcc6e8f96218430a56be1cf493d8a638 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * Application unit aliases
  4. *
  5. * @author Karel Kozlik
  6. * @version $Id: apu_aliases.php,v 1.2 2005/04/28 14:22:37 kozlik Exp $
  7. * @package serweb
  8. */
  9. /** Application unit aliases
  10. *
  11. *
  12. * This application unit is used for manipulation with aliases
  13. * notice: manipulation still not dome. only get list of aliases may be used
  14. *
  15. * Configuration:
  16. * --------------
  17. * 'allow_edit' (bool) default: false
  18. * set true if instance of this APU should be used for change values
  19. * by default only get list of aliases is enabled
  20. *
  21. * 'msg_add' default: $lang_str['msg_alias_added_s'] and $lang_str['msg_alias_added_l']
  22. * message which should be showed on add new alias - assoc array with keys 'short' and 'long'
  23. *
  24. * 'msg_update' default: $lang_str['msg_alias_updated_s'] and $lang_str['msg_alias_updated_l']
  25. * message which should be showed on alias update - assoc array with keys 'short' and 'long'
  26. *
  27. * 'msg_delete' default: $lang_str['msg_alias_deleted_s'] and $lang_str['msg_alias_deleted_l']
  28. * message which should be showed on alias delete - assoc array with keys 'short' and 'long'
  29. *
  30. * 'form_name' (string) default: ''
  31. * name of html form
  32. *
  33. * 'form_submit' (assoc)
  34. * assotiative array describe submit element of form. For details see description
  35. * of method add_submit in class form_ext
  36. *
  37. * 'smarty_form' name of smarty variable - see below
  38. * 'smarty_action' name of smarty variable - see below
  39. * 'smarty_aliases' name of smarty variable - see below
  40. *
  41. *
  42. * Exported smarty variables:
  43. * --------------------------
  44. * opt['smarty_aliases'] (aliases)
  45. * associative array containing user's aliases
  46. * The array have same keys as function get_aliases (from data layer) returned.
  47. *
  48. * opt['smarty_form'] (form)
  49. * phplib html form
  50. *
  51. * opt['smarty_action'] (action)
  52. * tells what should smarty display. Values:
  53. * 'default' -
  54. * 'was_updated' - when user submited form and data was succefully stored
  55. * 'was_added' - when user submited form and new alias was succefully stored
  56. * 'was_deleted' - when user delete alias
  57. * 'edit' - when user is editing alias
  58. *
  59. */
  60. class apu_aliases extends apu_base_class{
  61. var $smarty_action='default';
  62. var $aliases = array();
  63. var $act_alias;
  64. /* return required data layer methods - static class */
  65. function get_required_data_layer_methods(){
  66. return array('get_aliases', 'delete_alias', 'is_user_exists', 'add_new_alias');
  67. }
  68. /* return array of strings - requred javascript files */
  69. function get_required_javascript(){
  70. return array();
  71. }
  72. /* constructor */
  73. function apu_aliases(){
  74. global $lang_str;
  75. parent::apu_base_class();
  76. /* set default values to $this->opt */
  77. $this->opt['allow_edit'] = false;
  78. /* message on alias update */
  79. $this->opt['msg_add']['short'] = &$lang_str['msg_alias_added_s'];
  80. $this->opt['msg_add']['long'] = &$lang_str['msg_alias_added_l'];
  81. $this->opt['msg_update']['short'] = &$lang_str['msg_alias_updated_s'];
  82. $this->opt['msg_update']['long'] = &$lang_str['msg_alias_updated_l'];
  83. $this->opt['msg_delete']['short'] = &$lang_str['msg_alias_deleted_s'];
  84. $this->opt['msg_delete']['long'] = &$lang_str['msg_alias_deleted_l'];
  85. /*** names of variables assigned to smarty ***/
  86. /* form */
  87. $this->opt['smarty_form'] = 'form';
  88. /* smarty action */
  89. $this->opt['smarty_action'] = 'action';
  90. $this->opt['smarty_aliases'] = 'aliases';
  91. /* name of html form */
  92. $this->opt['form_name'] = '';
  93. }
  94. function get_aliases(&$errors){
  95. global $data, $sess;
  96. if (false === $aliases = $data->get_aliases($this->user_id, $errors)) return false;
  97. foreach($aliases as $k=>$v){
  98. if ($this->action['action']=='edit' and
  99. $v['username']==$this->act_alias['username'] and
  100. $v['domain'] ==$this->act_alias['domain']){
  101. unset($this->aliases[$k]);
  102. continue;
  103. }
  104. $this->aliases[$k]['username'] = $v->username;
  105. $this->aliases[$k]['domain'] = $v->domain;
  106. $this->aliases[$k]['url_dele'] = $sess->url($_SERVER['PHP_SELF']."?kvrk=".uniqID("")."&al_dele_un=".RawURLEncode($v->username)."&al_dele_dom=".RawURLEncode($v->domain));
  107. $this->aliases[$k]['url_edit'] = $sess->url($_SERVER['PHP_SELF']."?kvrk=".uniqID("")."&al_edit_un=".RawURLEncode($v->username)."&al_edit_dom=".RawURLEncode($v->domain));
  108. }
  109. return true;
  110. }
  111. function action_add(&$errors){
  112. global $data;
  113. if (false === $data->add_new_alias($this->user_id, $_POST['al_username'], $_POST['al_domain'], $errors)) return false;
  114. return array("m_al_added=".RawURLEncode($this->opt['instance_id']));
  115. }
  116. function action_update(&$errors){
  117. global $data;
  118. if (false === $data->delete_alias($this->user_id, $this->act_alias['username'], $this->act_alias['domain'], $errors)) return false;
  119. if (false === $data->add_new_alias($this->user_id, $_POST['al_username'], $_POST['al_domain'], $errors)) return false;
  120. return array("m_al_updated=".RawURLEncode($this->opt['instance_id']));
  121. }
  122. function action_delete(&$errors){
  123. global $data;
  124. if (false === $data->delete_alias($this->user_id, $this->act_alias['username'], $this->act_alias['domain'], $errors)) return false;
  125. return array("m_al_deleted=".RawURLEncode($this->opt['instance_id']));
  126. }
  127. function action_edit(&$errors){
  128. if (false === $this->get_aliases($errors)) return false;
  129. $this->smarty_action="edit";
  130. }
  131. function action_default(&$errors){
  132. if (false === $this->get_aliases($errors)) return false;
  133. return true;
  134. }
  135. /* this metod is called always at begining */
  136. function init(){
  137. parent::init();
  138. }
  139. /* check _get and _post arrays and determine what we will do */
  140. function determine_action(){
  141. if ($this->opt['allow_edit']){
  142. if ($this->was_form_submited()){ // Is there data to process?
  143. if ($_POST['al_id_u']){
  144. $this->act_alias['username'] = $_POST['al_id_u'];
  145. $this->act_alias['domain'] = $_POST['al_id_d'];
  146. $this->action = array('action'=>"update",
  147. 'validate_form'=>true,
  148. 'reload'=>true);
  149. }
  150. else {
  151. $this->action = array('action'=>"add",
  152. 'validate_form'=>true,
  153. 'reload'=>true);
  154. }
  155. return;
  156. }
  157. if (isset($_GET['al_edit_un'])){
  158. $this->act_alias['username'] = $_GET['al_edit_un'];
  159. $this->act_alias['domain'] = $_GET['al_edit_dom'];
  160. $this->action = array('action'=>"edit",
  161. 'validate_form'=>false,
  162. 'reload'=>false);
  163. return;
  164. }
  165. if (isset($_GET['al_dele_un'])){
  166. $this->act_alias['username'] = $_GET['al_dele_un'];
  167. $this->act_alias['domain'] = $_GET['al_dele_dom'];
  168. $this->action = array('action'=>"delete",
  169. 'validate_form'=>false,
  170. 'reload'=>true);
  171. return;
  172. }
  173. }
  174. $this->action=array('action'=>"default",
  175. 'validate_form'=>false,
  176. 'reload'=>false);
  177. }
  178. /* create html form */
  179. function create_html_form(&$errors){
  180. parent::create_html_form($errors);
  181. if ($this->opt['allow_edit']){
  182. $this->f->add_element(array("type"=>"text",
  183. "name"=>"al_username",
  184. "size"=>16,
  185. "maxlength"=>64,
  186. "value"=>isset($this->act_alias['username']) ? $this->act_alias['username'] : ""));
  187. $this->f->add_element(array("type"=>"text",
  188. "name"=>"al_domain",
  189. "size"=>16,
  190. "maxlength"=>128,
  191. "value"=>isset($this->act_alias['domain']) ? $this->act_alias['domain'] : $this->user_id->domain));
  192. $this->f->add_element(array("type"=> "hidden",
  193. "name"=> "al_id_u",
  194. "value"=> $this->act_alias['username']));
  195. $this->f->add_element(array("type"=> "hidden",
  196. "name"=> "al_id_d",
  197. "value"=> $this->act_alias['domain']));
  198. }
  199. }
  200. /* validate html form */
  201. function validate_form(&$errors){
  202. global $lang_str, $data;
  203. if (false === parent::validate_form($errors)) return false;
  204. if (!isset($_POST['al_domain']) or !$_POST['al_domain'])
  205. $_POST['al_domain'] = $this->user_id->domain;
  206. //check if alias exists
  207. if (0 > $alias_exists = $data->is_user_exists($_POST['al_username'], $_POST['al_domain'], $errors)) return false;
  208. if ($alias_exists){
  209. $errors[]=$lang_str['err_alias_already_exists_1']." ".$_POST['al_username']."@".$_POST['al_domain']." ".$lang_str['err_alias_already_exists_2'];
  210. return false;
  211. }
  212. return true;
  213. }
  214. /* callback function when some html form is invalid */
  215. function form_invalid(){
  216. $this->get_aliases($errors);
  217. }
  218. /* add messages to given array */
  219. function return_messages(&$msgs){
  220. global $_GET;
  221. if (isset($_GET['m_al_updated']) and $_GET['m_al_updated'] == $this->opt['instance_id']){
  222. $msgs[]=&$this->opt['msg_update'];
  223. $this->smarty_action="was_updated";
  224. }
  225. if (isset($_GET['m_al_added']) and $_GET['m_al_added'] == $this->opt['instance_id']){
  226. $msgs[]=&$this->opt['msg_add'];
  227. $this->smarty_action="was_added";
  228. }
  229. if (isset($_GET['m_al_deleted']) and $_GET['m_al_deleted'] == $this->opt['instance_id']){
  230. $msgs[]=&$this->opt['msg_delete'];
  231. $this->smarty_action="was_deleted";
  232. }
  233. }
  234. /* assign variables to smarty */
  235. function pass_values_to_html(){
  236. global $smarty;
  237. $smarty->assign_by_ref($this->opt['smarty_aliases'], $this->aliases);
  238. $smarty->assign_by_ref($this->opt['smarty_action'], $this->smarty_action);
  239. }
  240. /* return info need to assign html form to smarty */
  241. function pass_form_to_html(){
  242. return array('smarty_name' => $this->opt['smarty_form'],
  243. 'form_name' => $this->opt['form_name'],
  244. 'after' => "",
  245. 'before' => "");
  246. }
  247. }
  248. ?>