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