api.saymyname.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * You're Goddamn Right
  4. */
  5. class SayMyName {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Login generation mode mapped from LOGIN_GENERATION option
  14. *
  15. * @var string
  16. */
  17. protected $generationMode = '';
  18. /**
  19. * User city alias
  20. *
  21. * @var string
  22. */
  23. protected $cityAlias = '';
  24. /**
  25. * User street alias
  26. *
  27. * @var string
  28. */
  29. protected $streetAlias = '';
  30. /**
  31. * User build number
  32. *
  33. * @var string
  34. */
  35. protected $buildNum = '';
  36. /**
  37. * User apartament number
  38. *
  39. * @var string
  40. */
  41. protected $apt = '';
  42. /**
  43. * User IP
  44. *
  45. * @var string
  46. */
  47. protected $ipProposal = '';
  48. /**
  49. * User associated agent Id
  50. *
  51. * @var int
  52. */
  53. protected $agentId = 0;
  54. /**
  55. * Contains increment-like logins start offset.
  56. * Mapped from LOGIN_GENERATION_INCOFFSET option.
  57. *
  58. * @var int
  59. */
  60. protected $incrementsOffset = 1;
  61. /**
  62. * Contains default increments maximum value.
  63. * Mapped from LOGIN_GENERATION_INCMAX option.
  64. *
  65. * @var int
  66. */
  67. protected $incrementsMaxLimit = 100000;
  68. /**
  69. * Contains default apartments number delimiter for address based logins.
  70. * Mapped from LOGIN_GENERATION_AD option.
  71. *
  72. * @var string
  73. */
  74. protected $apartmentDelimiter = 'ap';
  75. /**
  76. * Contains shared prefix for further usage in some generators.
  77. * Mapped from LOGIN_GENERATION_SHPRFX option.
  78. *
  79. * @var string
  80. */
  81. protected $sharedPrefix = 'UB';
  82. /**
  83. * Contains all busy users logins
  84. *
  85. * @var array
  86. */
  87. protected $busyLogins = array();
  88. /**
  89. * New user login proposal
  90. *
  91. * @var string
  92. */
  93. protected $loginProposal = '';
  94. /**
  95. * Some predefined constants, options, routes etc..
  96. */
  97. const OPTION_MODE = 'LOGIN_GENERATION';
  98. const OPTION_INCOFFSET = 'LOGIN_GENERATION_INCOFFSET';
  99. const OPTION_INCMAX = 'LOGIN_GENERATION_INCMAX';
  100. const OPTION_APT_DELIMITER = 'LOGIN_GENERATION_AD';
  101. const OPTION_SHARED_PREFIX = 'LOGIN_GENERATION_SHPRFX';
  102. const GENERATORS_PATH = 'api/vendor/login_generators/';
  103. /**
  104. * Creates new login generator instance
  105. *
  106. * @param string $cityAlias
  107. * @param string $streetAlias
  108. * @param string $buildNum
  109. * @param string $apt
  110. * @param string $ipProposal
  111. * @param int $agentid
  112. *
  113. */
  114. public function __construct($cityAlias, $streetAlias, $buildNum, $apt, $ipProposal, $agentId = 0) {
  115. $this->loadConfig();
  116. if (isset($this->altCfg[self::OPTION_MODE])) {
  117. //setting generation mode
  118. $this->generationMode = $this->altCfg[self::OPTION_MODE];
  119. //optional custom increments offset
  120. if (isset($this->altCfg[self::OPTION_INCOFFSET])) {
  121. $this->incrementsOffset = $this->altCfg[self::OPTION_INCOFFSET];
  122. }
  123. //and custom optional maximum increments values
  124. if (isset($this->altCfg[self::OPTION_INCMAX])) {
  125. $this->incrementsMaxLimit = $this->altCfg[self::OPTION_INCMAX];
  126. }
  127. //custom optional apartments delimiter
  128. if (isset($this->altCfg[self::OPTION_APT_DELIMITER])) {
  129. $this->apartmentDelimiter = $this->altCfg[self::OPTION_APT_DELIMITER];
  130. }
  131. //custom shared prefix for some generators
  132. if (isset($this->altCfg[self::OPTION_SHARED_PREFIX])) {
  133. $this->sharedPrefix = $this->altCfg[self::OPTION_SHARED_PREFIX];
  134. }
  135. //setting some of new user parameters
  136. $this->cityAlias = zb_TranslitString($cityAlias);
  137. $this->streetAlias = zb_TranslitString($streetAlias);
  138. $this->buildNum = zb_TranslitString($buildNum);
  139. $this->apt = zb_TranslitString($apt);
  140. $this->ipProposal = $ipProposal;
  141. $this->agentId = $agentId;
  142. //loading all busy logins
  143. $this->loadBusyLogins();
  144. //validation of increment custom offsets
  145. if ($this->incrementsOffset >= $this->incrementsMaxLimit) {
  146. die(self::OPTION_INCOFFSET . ' >= ' . self::OPTION_INCMAX);
  147. }
  148. } else {
  149. die(__('You missed an important option') . ' ' . self::OPTION_MODE . '!');
  150. }
  151. }
  152. /**
  153. * Loads some required configs data
  154. *
  155. * @global object $ubillingConfig
  156. *
  157. * @return void
  158. */
  159. protected function loadConfig() {
  160. global $ubillingConfig;
  161. $this->altCfg = $ubillingConfig->getAlter();
  162. }
  163. /**
  164. * Loads currently existing users logind from database
  165. *
  166. * @return void
  167. */
  168. protected function loadBusyLogins() {
  169. $this->busyLogins = zb_AllBusyLogins();
  170. }
  171. /**
  172. * Filters user login for only allowed symbols
  173. *
  174. * @param string $login
  175. *
  176. * @return string
  177. */
  178. protected function filterLogin($login) {
  179. $login = str_replace(' ', '_', $login); //no spaces, lol
  180. $result = preg_replace("#[^a-z0-9A-Z_]#Uis", '', $login); //alphanumeric
  181. $result = zb_TranslitString($result); //force translit
  182. return($result);
  183. }
  184. /**
  185. * Returns new user login proposal
  186. *
  187. * @return string
  188. */
  189. public function getLogin() {
  190. $result = '';
  191. $generatorFullPath = self::GENERATORS_PATH . $this->generationMode;
  192. if (file_exists($generatorFullPath)) {
  193. $generatorCode = file_get_contents($generatorFullPath);
  194. if (!empty($generatorCode)) {
  195. eval($generatorCode);
  196. //any login suggestions?
  197. if (empty($this->loginProposal)) {
  198. log_register('LOGIN_GENERATION `' . $this->generationMode . '` FAIL EMPTY_PROPOSAL');
  199. throw new Exception(__('Generator code in') . ': ' . $generatorFullPath.' ' .__('doesnt set loginProposal property or returns empty proposal'));
  200. }
  201. } else {
  202. log_register('LOGIN_GENERATION `' . $this->generationMode . '` FAIL EMPTY_CODE');
  203. throw new Exception(__('Generator code is empty') . ': ' . $generatorFullPath);
  204. }
  205. } else {
  206. log_register('LOGIN_GENERATION `' . $this->generationMode . '` FAIL NOT_EXISTS');
  207. throw new Exception(__('Login generator definition not exists') . ': ' . $generatorFullPath);
  208. }
  209. $result = $this->filterLogin($this->loginProposal);
  210. if (isset($this->busyLogins[$result])) {
  211. log_register('LOGIN_GENERATION `' . $this->generationMode . '` FAIL DUPLICATE `' . $result . '`');
  212. throw new Exception(__('Generator').' ' . $generatorFullPath .' '. __('returned existing login as proposal'));
  213. }
  214. return($result);
  215. }
  216. }