1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17:
18: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
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: public function __construct() {
68: $cfg = cRegistry::getConfig();
69: parent::__construct($cfg['tab']['url_shortener']['shorturl'], 'idshorturl');
70: $this->_setItemClass('cApiShortUrl');
71: }
72:
73: 74: 75: 76: 77: 78: 79: 80:
81: public function create($shorturl, $idart = NULL, $idlang = NULL, $idclient = NULL) {
82: if (is_null($idart)) {
83: $idart = cRegistry::getArticleId();
84: }
85: if (is_null($idlang)) {
86: $idlang = cRegistry::getLanguageId();
87: }
88: if (is_null($idclient)) {
89: $idclient = cRegistry::getClientId();
90: }
91:
92: $item = parent::createNewItem();
93: $item->set('shorturl', $shorturl);
94: $item->set('idart', $idart);
95: $item->set('idlang', $idlang);
96: $item->set('idclient', $idclient);
97: $item->set('created', date('Y-m-d H:i:s'));
98: $item->store();
99:
100: return $item;
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function isValidShortUrl($shorturl) {
115: $cfg = cRegistry::getConfig();
116:
117:
118: $exclude = scandir(cRegistry::getFrontendPath());
119: if (is_array($cfg['url_shortener']['exlude_dirs'])) {
120: $exclude = array_merge($exclude, $cfg['url_shortener']['exlude_dirs']);
121: }
122: if (in_array($shorturl, $exclude)) {
123: return self::ERR_IS_CLIENT_FOLDER;
124: }
125:
126:
127: $minLength = 3;
128: if (is_numeric($cfg['url_shortener']['minimum_length'])) {
129: $minLength = $cfg['url_shortener']['minimum_length'];
130: }
131: if (strlen($shorturl) < $minLength) {
132: return self::ERR_TOO_SHORT;
133: }
134:
135:
136: if (isset($cfg['url_shortener']['allowed_chars'])) {
137: if (!preg_match($cfg['url_shortener']['allowed_chars'], $shorturl)) {
138: return self::ERR_INVALID_CHARS;
139: }
140: }
141:
142:
143: $artLangColl = new cApiArticleLanguageCollection();
144: $artLangColl->select("urlname='" . $shorturl . "'");
145: if ($artLangColl->count() > 0) {
146: return self::ERR_IS_ARTICLE_ALIAS;
147: }
148: $catLangColl = new cApiCategoryLanguageCollection();
149: $catLangColl->select("urlname='" . $shorturl . "'");
150: if ($catLangColl->count() > 0) {
151: return self::ERR_IS_CATEGORY_ALIAS;
152: }
153:
154: return true;
155: }
156: }
157:
158: 159: 160: 161: 162: 163:
164: class cApiShortUrl extends Item {
165:
166: 167: 168: 169: 170:
171: public function __construct($id = false) {
172: $cfg = cRegistry::getConfig();
173: parent::__construct($cfg['tab']['url_shortener']['shorturl'], 'idshorturl');
174: if ($id !== false) {
175: $this->loadByPrimaryKey($id);
176: }
177: }
178: }
179: