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: 25: 26:
27: class cApiShortUrlCollection extends ItemCollection {
28:
29: 30: 31: 32:
33: const ERR_IS_CLIENT_FOLDER = 1;
34:
35: 36: 37: 38:
39: const ERR_TOO_SHORT = 2;
40:
41: 42: 43: 44:
45: const ERR_INVALID_CHARS = 3;
46:
47: 48: 49: 50:
51: const ERR_IS_ARTICLE_ALIAS = 4;
52:
53: 54: 55: 56:
57: const ERR_IS_CATEGORY_ALIAS = 5;
58:
59: 60: 61: 62:
63: const ERR_ALREADY_EXISTS = 6;
64:
65: 66: 67:
68: public function __construct() {
69: $cfg = cRegistry::getConfig();
70: parent::__construct($cfg['tab']['url_shortener']['shorturl'], 'idshorturl');
71: $this->_setItemClass('cApiShortUrl');
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public function create($shorturl, $idart = NULL, $idlang = NULL, $idclient = NULL) {
87: if (is_null($idart)) {
88: $idart = cRegistry::getArticleId();
89: }
90: if (is_null($idlang)) {
91: $idlang = cRegistry::getLanguageId();
92: }
93: if (is_null($idclient)) {
94: $idclient = cRegistry::getClientId();
95: }
96:
97:
98: $item = $this->createNewItem();
99: $item->set('shorturl', $shorturl);
100: $item->set('idart', $idart);
101: $item->set('idlang', $idlang);
102: $item->set('idclient', $idclient);
103: $item->set('created', date('Y-m-d H:i:s'));
104: $item->store();
105:
106: return $item;
107: }
108:
109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
122: public function isValidShortUrl($shorturl) {
123: $cfg = cRegistry::getConfig();
124:
125: if (cString::getStringLength(trim($shorturl)) === 0) {
126: return true;
127: }
128:
129:
130: $exclude = scandir(cRegistry::getFrontendPath());
131: if (is_array($cfg['url_shortener']['exlude_dirs'])) {
132: $exclude = array_merge($exclude, $cfg['url_shortener']['exlude_dirs']);
133: }
134: if (in_array($shorturl, $exclude)) {
135: return self::ERR_IS_CLIENT_FOLDER;
136: }
137:
138:
139: $minLength = 3;
140: if (is_numeric($cfg['url_shortener']['minimum_length'])) {
141: $minLength = $cfg['url_shortener']['minimum_length'];
142: }
143: if (cString::getStringLength($shorturl) < $minLength) {
144: return self::ERR_TOO_SHORT;
145: }
146:
147:
148: if (isset($cfg['url_shortener']['allowed_chars'])) {
149: if (!preg_match($cfg['url_shortener']['allowed_chars'], $shorturl)) {
150: return self::ERR_INVALID_CHARS;
151: }
152: }
153:
154:
155: $artLangColl = new cApiArticleLanguageCollection();
156: $artLangColl->select("urlname='" . $shorturl . "'");
157: if ($artLangColl->count() > 0) {
158: return self::ERR_IS_ARTICLE_ALIAS;
159: }
160: $catLangColl = new cApiCategoryLanguageCollection();
161: $catLangColl->select("urlname='" . $shorturl . "'");
162: if ($catLangColl->count() > 0) {
163: return self::ERR_IS_CATEGORY_ALIAS;
164: }
165:
166: return true;
167: }
168: }
169:
170: 171: 172: 173: 174: 175:
176: class cApiShortUrl extends Item {
177: 178: 179: 180: 181: 182: 183: 184:
185: public function __construct($id = false) {
186: $cfg = cRegistry::getConfig();
187: parent::__construct($cfg['tab']['url_shortener']['shorturl'], 'idshorturl');
188: if ($id !== false) {
189: $this->loadByPrimaryKey($id);
190: }
191: }
192: }
193: