1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cApiShortUrlCollection extends ItemCollection {
25:
26: const ERR_IS_CLIENT_FOLDER = 1;
27:
28: const ERR_TOO_SHORT = 2;
29:
30: const ERR_INVALID_CHARS = 3;
31:
32: const ERR_IS_ARTICLE_ALIAS = 4;
33:
34: const ERR_IS_CATEGORY_ALIAS = 5;
35:
36: const ERR_ALREADY_EXISTS = 6;
37:
38: public function __construct() {
39: $cfg = cRegistry::getConfig();
40: parent::__construct($cfg['tab']['url_shortener']['shorturl'], 'idshorturl');
41: $this->_setItemClass('cApiShortUrl');
42: }
43:
44: public function create($shorturl, $idart = null, $idlang = null, $idclient = null) {
45: if (is_null($idart)) {
46: $idart = cRegistry::getArticleId();
47: }
48: if (is_null($idlang)) {
49: $idlang = cRegistry::getLanguageId();
50: }
51: if (is_null($idclient)) {
52: $idclient = cRegistry::getClientId();
53: }
54:
55: $item = parent::createNewItem();
56: $item->set('shorturl', $shorturl);
57: $item->set('idart', $idart);
58: $item->set('idlang', $idlang);
59: $item->set('idclient', $idclient);
60: $item->set('created', date('Y-m-d H:i:s'));
61: $item->store();
62:
63: return $item;
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76:
77: public function isValidShortUrl($shorturl) {
78: $cfg = cRegistry::getConfig();
79:
80:
81: $exclude = scandir(cRegistry::getFrontendPath());
82: if (is_array($cfg['url_shortener']['exlude_dirs'])) {
83: $exclude = array_merge($exclude, $cfg['url_shortener']['exlude_dirs']);
84: }
85: if (in_array($shorturl, $exclude)) {
86: return self::ERR_IS_CLIENT_FOLDER;
87: }
88:
89:
90: $minLength = 3;
91: if (is_numeric($cfg['url_shortener']['minimum_length'])) {
92: $minLength = $cfg['url_shortener']['minimum_length'];
93: }
94: if (strlen($shorturl) < $minLength) {
95: return self::ERR_TOO_SHORT;
96: }
97:
98:
99: if (isset($cfg['url_shortener']['allowed_chars'])) {
100: if (!preg_match($cfg['url_shortener']['allowed_chars'], $shorturl)) {
101: return self::ERR_INVALID_CHARS;
102: }
103: }
104:
105:
106: $artLangColl = new cApiArticleLanguageCollection();
107: $artLangColl->select("urlname='" . $shorturl . "'");
108: if ($artLangColl->count() > 0) {
109: return self::ERR_IS_ARTICLE_ALIAS;
110: }
111: $catLangColl = new cApiCategoryLanguageCollection();
112: $catLangColl->select("urlname='" . $shorturl . "'");
113: if ($catLangColl->count() > 0) {
114: return self::ERR_IS_CATEGORY_ALIAS;
115: }
116:
117: return true;
118: }
119:
120: }
121: class cApiShortUrl extends Item {
122:
123: 124: 125: 126: 127:
128: public function __construct($id = false) {
129: $cfg = cRegistry::getConfig();
130: parent::__construct($cfg['tab']['url_shortener']['shorturl'], 'idshorturl');
131: if ($id !== false) {
132: $this->loadByPrimaryKey($id);
133: }
134: }
135:
136: }
137: