AbstractRedirect.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. abstract class AbstractRedirect extends AbstractModel
  3. {
  4. use DomainLimitTrait;
  5. /**
  6. * Db table for find methods
  7. *
  8. * @var string
  9. */
  10. public static $table;
  11. /**
  12. * Db id attribute for find methods
  13. *
  14. * @var string
  15. */
  16. public static $idAttribute;
  17. /**
  18. * Mapping model attributes and database attributes for saving
  19. *
  20. * @var array
  21. */
  22. protected static $attributeDbAttributeMapping = null;
  23. /**
  24. * @var ModelCollection
  25. */
  26. protected $conflictingUsers = null;
  27. /**
  28. * @inheritdoc
  29. */
  30. protected static function initModel()
  31. {
  32. if(is_null(static::$attributeDbAttributeMapping)){
  33. static::$table = Config::get('schema.tables.aliases', 'aliases');
  34. static::$idAttribute = Config::get('schema.attributes.aliases.id', 'id');
  35. static::$attributeDbAttributeMapping = array(
  36. 'id' => Config::get('schema.attributes.aliases.id', 'id'),
  37. 'source' => Config::get('schema.attributes.aliases.source', 'source'),
  38. 'destination' => Config::get('schema.attributes.aliases.destination', 'destination'),
  39. 'multi_hash' => Config::get('schema.attributes.aliases.multi_source', 'multi_source'),
  40. );
  41. if(Config::get('options.enable_user_redirects', false)){
  42. static::$attributeDbAttributeMapping['is_created_by_user'] = Config::get('schema.attributes.aliases.is_created_by_user', 'is_created_by_user');
  43. }
  44. }
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. protected function preSave($data)
  50. {
  51. $data = parent::preSave($data);
  52. $data['source'] = emailsToString($data['source']);
  53. $data['destination'] = emailsToString($data['destination']);
  54. if(Config::get('options.enable_user_redirects', false)){
  55. $data['is_created_by_user'] = $data['is_created_by_user'] ? 1 : 0;
  56. }
  57. return $data;
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. protected function __construct($data)
  63. {
  64. parent::__construct($data);
  65. $source = stringToEmails($data[static::attr('source')]);
  66. $destination = stringToEmails($data[static::attr('destination')]);
  67. if(get_called_class() === 'Alias' || get_called_class() === 'Redirect'){
  68. $source = $source[0];
  69. }
  70. if(get_called_class() === 'Alias' || get_called_class() === 'MultiAlias'){
  71. $destination = $destination[0];
  72. }
  73. $this->setSource($source);
  74. $this->setDestination($destination);
  75. if(Config::get('options.enable_multi_source_redirects', false)){
  76. $this->setMultiHash($data[static::attr('multi_hash')]);
  77. }
  78. if(Config::get('options.enable_user_redirects', false)){
  79. $this->setIsCreatedByUser($data[static::attr('is_created_by_user')]);
  80. }
  81. }
  82. /**
  83. * @inheritdoc
  84. */
  85. public static function create($data)
  86. {
  87. if(get_called_class() !== 'AbstractRedirect'){
  88. return parent::create($data);
  89. }
  90. $hasMultipleSources = array_key_exists(static::attr('source'), $data)
  91. && strpos($data[static::attr('source')], ',') !== false;
  92. $hasMultipleDestinations = array_key_exists(static::attr('destination'), $data)
  93. && strpos($data[static::attr('destination')], ',') !== false;
  94. if(Config::get('options.enable_multi_source_redirects', false) && $hasMultipleSources
  95. ){
  96. if($hasMultipleDestinations){
  97. return MultiRedirect::create($data);
  98. }
  99. else{
  100. return MultiAlias::create($data);
  101. }
  102. }
  103. else{
  104. if($hasMultipleDestinations){
  105. return Redirect::create($data);
  106. }
  107. else{
  108. return Alias::create($data);
  109. }
  110. }
  111. }
  112. /**
  113. * @return array|string
  114. */
  115. public function getSource()
  116. {
  117. return $this->getAttribute('source');
  118. }
  119. /**
  120. * @param string|array $value
  121. */
  122. public function setSource($value)
  123. {
  124. if(is_array($value)){
  125. $this->setAttribute('source', array_map('strtolower', $value));
  126. }
  127. else{
  128. $this->setAttribute('source', strtolower($value));
  129. }
  130. }
  131. /**
  132. * @return array|string
  133. */
  134. public function getDestination()
  135. {
  136. return $this->getAttribute('destination');
  137. }
  138. /**
  139. * @param string|array $value
  140. */
  141. public function setDestination($value)
  142. {
  143. if(is_array($value)){
  144. $this->setAttribute('destination', array_map('strtolower', $value));
  145. }
  146. else{
  147. $this->setAttribute('destination', strtolower($value));
  148. }
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getMultiHash()
  154. {
  155. return $this->getAttribute('multi_hash');
  156. }
  157. /**
  158. * @param string $value
  159. */
  160. public function setMultiHash($value)
  161. {
  162. $this->setAttribute('multi_hash', $value);
  163. }
  164. /**
  165. * @return bool
  166. */
  167. public function isCreatedByUser()
  168. {
  169. return $this->getAttribute('is_created_by_user');
  170. }
  171. /**n
  172. * @param bool $value
  173. */
  174. public function setIsCreatedByUser($value)
  175. {
  176. $this->setAttribute('is_created_by_user', $value ? true : false);
  177. }
  178. /**
  179. * @return array
  180. */
  181. protected function getDomain()
  182. {
  183. $sources = $this->getSource();
  184. if(is_string($sources)){
  185. $sources = array($sources);
  186. }
  187. $domains = array();
  188. foreach($sources as $source){
  189. $emailParts = explode('@', $source);
  190. if(count($emailParts) === 2){
  191. $domains[] = $emailParts[1];
  192. }
  193. }
  194. return array_unique($domains);
  195. }
  196. /**
  197. * @return ModelCollection
  198. */
  199. public function getConflictingUsers()
  200. {
  201. if(is_null($this->conflictingUsers)){
  202. $sources = $this->getSource();
  203. if(is_string($sources)){
  204. $sources = array($sources);
  205. }
  206. $this->conflictingUsers = new ModelCollection();
  207. foreach($sources as $source){
  208. $user = User::findByEmail($source);
  209. if(!is_null($user)){
  210. $this->conflictingUsers->add($user);
  211. }
  212. }
  213. }
  214. return $this->conflictingUsers;
  215. }
  216. /**
  217. * @param string $template
  218. *
  219. * @return array|string
  220. */
  221. public function getConflictingMarkedSource($template = "<u>%email%</u>")
  222. {
  223. $conflictingUsers = $this->getConflictingUsers();
  224. $sources = $this->getSource();
  225. if(is_string($sources)){
  226. $sources = array($sources);
  227. }
  228. foreach($conflictingUsers as $user){
  229. if(($key = array_search($user->getEmail(), $sources)) !== false){
  230. $sources[$key] = str_replace('%email%', $sources[$key], $template);
  231. }
  232. }
  233. return $sources;
  234. }
  235. /**
  236. * @inheritdoc
  237. */
  238. public static function findAll($orderBy = null)
  239. {
  240. if(is_null($orderBy)){
  241. $orderBy = array(static::attr('source'));
  242. }
  243. return parent::findAll($orderBy);
  244. }
  245. /**
  246. * @return string
  247. */
  248. private static function generateRedirectBaseQuery()
  249. {
  250. if(Config::get('options.enable_multi_source_redirects', false)){
  251. return "SELECT r.* FROM (
  252. SELECT
  253. GROUP_CONCAT(g.`".static::$idAttribute."` ORDER BY g.`".static::$idAttribute."` SEPARATOR ',') AS `".static::$idAttribute."`,
  254. GROUP_CONCAT(g.`".static::attr('source')."` SEPARATOR ',') AS `".static::attr('source')."`,
  255. g.`".static::attr('destination')."`,
  256. g.`".static::attr('multi_hash')."`
  257. ".(Config::get('options.enable_user_redirects', false) ? ", g.`".static::attr('is_created_by_user')."`" : "")."
  258. FROM `".static::$table."` AS g
  259. WHERE g.`".static::attr('multi_hash')."` IS NOT NULL
  260. GROUP BY g.`".static::attr('multi_hash')."`
  261. UNION
  262. SELECT
  263. s.`".static::$idAttribute."`,
  264. s.`".static::attr('source')."`,
  265. s.`".static::attr('destination')."`,
  266. s.`".static::attr('multi_hash')."`
  267. ".(Config::get('options.enable_user_redirects', false) ? ", s.`".static::attr('is_created_by_user')."`" : "")."
  268. FROM `".static::$table."` AS s
  269. WHERE s.`".static::attr('multi_hash')."` IS NULL
  270. ) AS r";
  271. }
  272. else{
  273. return "SELECT * FROM `".static::$table."`";
  274. }
  275. }
  276. public static function findMultiAll($orderBy = null)
  277. {
  278. static::initModel();
  279. if(is_null($orderBy)){
  280. $orderBy = array(static::attr('source'));
  281. }
  282. $sql = static::generateRedirectBaseQuery()
  283. .Database::helperOrderBy($orderBy);
  284. return static::findAllRaw($sql);
  285. }
  286. public static function findMultiWhere($conditions = array(), $conditionConnector = 'AND', $orderBy = null, $limit = 0)
  287. {
  288. $sql = static::generateRedirectBaseQuery()
  289. .Database::helperWhere($conditions, $conditionConnector)
  290. .Database::helperOrderBy($orderBy)
  291. .Database::helperLimit($limit);
  292. if($limit === 1){
  293. return static::findRaw($sql);
  294. }
  295. return static::findAllRaw($sql);
  296. }
  297. public static function findMultiWhereFirst($conditions = array(), $conditionConnector = 'AND', $orderBy = null)
  298. {
  299. return static::findMultiWhere($conditions, $conditionConnector, $orderBy, 1);
  300. }
  301. public static function findMulti($id)
  302. {
  303. static::initModel();
  304. return static::findMultiWhereFirst(array(static::$idAttribute, $id));
  305. }
  306. /**
  307. * @param array|User|null $limitedBy
  308. *
  309. * @return ModelCollection|static[]
  310. */
  311. public static function getMultiByLimitedDomains($limitedBy = null)
  312. {
  313. return static::filterModelCollectionByLimitedDomains(static::findMultiAll(), $limitedBy);
  314. }
  315. }