Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SIWECOS
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cApiShortUrl
  • cApiShortUrlCollection

Functions

  • piUsAfterLoadPlugins
  • piUsConSaveArtAfter
  • piUseConDeleteArtAfter
  • piUsEditFormAdditionalRows
  • piUsGetErrorMessage
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the Plugin Manager configurations.
  4:  *
  5:  * @package Plugin
  6:  * @subpackage UrlShortener
  7:  * @author Simon Sprankel
  8:  * @copyright four for business AG <www.4fb.de>
  9:  * @license http://www.contenido.org/license/LIZENZ.txt
 10:  * @link http://www.4fb.de
 11:  * @link http://www.contenido.org
 12:  */
 13: 
 14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 15: 
 16: /**
 17:  * Constructs the HTML code containing table rows which are added to the end of
 18:  * the article edit form
 19:  *
 20:  * @param $idart
 21:  * @param $idlang
 22:  * @param $idclient
 23:  * @param $disabled
 24:  *
 25:  * @return string rendered HTML code
 26:  * @throws cDbException
 27:  * @throws cException
 28:  */
 29: function piUsEditFormAdditionalRows($idart, $idlang, $idclient, $disabled) {
 30:     $shortUrl = new cApiShortUrl();
 31:     $shortUrl->loadByMany(array(
 32:         'idart' => $idart,
 33:         'idlang' => $idlang,
 34:         'idclient' => $idclient
 35:     ));
 36: 
 37:     $tr = new cHTMLTableRow();
 38: 
 39:     $td = new cHTMLTableData();
 40:     $td->setClass('text_medium');
 41:     $td->setContent(i18n('Short URL', 'url_shortener'));
 42:     $tr->appendContent($td);
 43: 
 44:     $td = new cHTMLTableData();
 45:     $td->setClass('text_medium');
 46:     $textbox = new cHTMLTextbox('url_shortener_shorturl', $shortUrl->get('shorturl'), 24, '', '', $disabled);
 47:     $td->setContent($textbox.' <img class="vAlignMiddle" title="'. i18n('INFO', 'url_shortener') .'" src="images/info.gif" alt="">');
 48:     $tr->appendContent($td);
 49: 
 50:     return $tr->render();
 51: }
 52: 
 53: /**
 54:  * Function is called after an article has been saved.
 55:  * Checks whether a short URL has been given via $_POST and saves/deletes it.
 56:  *
 57:  * @param       $editedIdArt
 58:  * @param array $values the values which are saved
 59:  *
 60:  * @throws cDbException
 61:  * @throws cException
 62:  * @throws cInvalidArgumentException
 63:  */
 64: function piUsConSaveArtAfter($editedIdArt, $values) {
 65:     // if not all parameters have been given, do nothing
 66:     if (!isset($_POST['url_shortener_shorturl']) || !isset($editedIdArt)) {
 67:         return;
 68:     }
 69:     $shorturl = $_POST['url_shortener_shorturl'];
 70:     $idart = $editedIdArt;
 71:     $idlang = cRegistry::getLanguageId();
 72:     $idclient = cRegistry::getClientId();
 73:     $shortUrlItem = new cApiShortUrl();
 74:     $shortUrlItem->loadByMany(array(
 75:         'idart' => $idart,
 76:         'idlang' => $idlang,
 77:         'idclient' => $idclient
 78:     ));
 79:     // if given shorturl is already in use, show error message
 80:     $checkShortUrlItem = new cApiShortUrl();
 81:     $checkShortUrlItem->loadBy('shorturl', $shorturl);
 82:     if ($checkShortUrlItem->isLoaded()) {
 83:         // if shorturl has not been changed, do nothing
 84:         if ($shortUrlItem->get('shorturl') === $checkShortUrlItem->get('shorturl')) {
 85:             return;
 86:         }
 87:         // TODO add warning to session as soon as this is possible (depends
 88:         // CON-772)
 89:         // $session = cRegistry::getSession();
 90:         // $session->addWarning($message);
 91:         $message = piUsGetErrorMessage(cApiShortUrlCollection::ERR_ALREADY_EXISTS, $shortUrlItem);
 92:         $notification = new cGuiNotification();
 93:         $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
 94:         return;
 95:     }
 96:     // check if given shorturl is valid
 97:     $shortUrlColl = new cApiShortUrlCollection();
 98:     $errorCode = $shortUrlColl->isValidShortUrl($shorturl);
 99:     if ($errorCode !== true) {
100:         $message = piUsGetErrorMessage($errorCode);
101:         // TODO add warning to session as soon as this is possible (depends
102:         // CON-772)
103:         // $session = cRegistry::getSession();
104:         // $session->addWarning($message);
105:         $notification = new cGuiNotification();
106:         $notification->displayNotification(cGuiNotification::LEVEL_ERROR, $message);
107:         return;
108:     }
109:     if ($_POST['url_shortener_shorturl'] === '') {
110:         // delete short URL if it exists
111:         if ($shortUrlItem->isLoaded()) {
112:             $item = cApiCecHook::executeAndReturn('ContenidoPlugin.UrlShortener.BeforeRemove', $shortUrlItem);
113:             if ($item instanceof cApiShortUrl) {
114:                 $shortUrlItem = $item;
115:             }
116: 
117:             $shortUrlColl->delete($shortUrlItem->get('idshorturl'));
118:         }
119:     } else {
120:         // a short URL has been given, so save it
121:         if ($shortUrlItem->isLoaded()) {
122:             // short URL already exists, update it
123:             $oldShortUrlItem = clone $shortUrlItem;
124:             $shortUrlItem->set('shorturl', $shorturl);
125: 
126:             $item = cApiCecHook::executeAndReturn('ContenidoPlugin.UrlShortener.BeforeEdit', $shortUrlItem, $oldShortUrlItem);
127:             if ($item instanceof cApiShortUrl) {
128:                 $shortUrlItem = $item;
129:             }
130: 
131:             $shortUrlItem->store();
132:         } else {
133:             // short URL does not exist yet, create a new one
134:             $shortUrlItem = $shortUrlColl->create($shorturl, $idart, $idlang, $idclient);
135:             cApiCecHook::executeAndReturn('ContenidoPlugin.UrlShortener.AfterCreate', $shortUrlItem);
136:         }
137:     }
138: }
139: 
140: /**
141:  * Computes an error message which describes the given error code.
142:  *
143:  * @param int          $errorCode the error code
144:  * @param cApiShortUrl $shortUrlItem
145:  *
146:  * @return string the error message describing the given error code
147:  * @throws cDbException
148:  * @throws cException
149:  */
150: function piUsGetErrorMessage($errorCode, $shortUrlItem = NULL) {
151:     switch ($errorCode) {
152:         case cApiShortUrlCollection::ERR_INVALID_CHARS:
153:             return i18n('The entered short URL contains invalid characters!', 'url_shortener');
154:             break;
155:         case cApiShortUrlCollection::ERR_IS_ARTICLE_ALIAS:
156:             return i18n('The entered short URL is already an article alias!', 'url_shortener');
157:             break;
158:         case cApiShortUrlCollection::ERR_IS_CATEGORY_ALIAS:
159:             return i18n('The entered short URL is already a category alias!', 'url_shortener');
160:             break;
161:         case cApiShortUrlCollection::ERR_IS_CLIENT_FOLDER:
162:             return i18n('The entered short URL is a subdirectory of the client directory!', 'url_shortener');
163:             break;
164:         case cApiShortUrlCollection::ERR_TOO_SHORT:
165:             return i18n('The entered short URL is too short!', 'url_shortener');
166:             break;
167:         case cApiShortUrlCollection::ERR_ALREADY_EXISTS:
168:             $message = i18n('The entered short URL already exists!', 'url_shortener');
169:             $message .= '<br />';
170:             if ($shortUrlItem !== NULL) {
171:                 // add the client name to the error message
172:                 $clientColl = new cApiClientCollection();
173:                 $message .= i18n('Client', 'url_shortener') . ': ' . $clientColl->getClientname($shortUrlItem->get('idclient'));;
174:                 $message .= '<br />';
175:                 // add the language name to the error message
176:                 $langColl = new cApiLanguageCollection();
177:                 $message .= i18n('Language', 'url_shortener') . ': ' . $langColl->getLanguageName($shortUrlItem->get('idlang'));
178:                 $message .= '<br />';
179:                 // add the category name to the error message
180:                 $catArt = new cApiCategoryArticle();
181:                 $catArt->loadBy('idart', $shortUrlItem->get('idart'));
182:                 $catLang = new cApiCategoryLanguage();
183:                 $catLang->loadByCategoryIdAndLanguageId($catArt->get('idcat'), $shortUrlItem->get('idlang'));
184:                 $message .= i18n('Category', 'url_shortener') . ': ' . $catLang->get('name');
185:                 $message .= '<br />';
186:                 // add the article name to the error message
187:                 $artlang = new cApiArticleLanguage();
188:                 $artlang->loadByArticleAndLanguageId($shortUrlItem->get('idart'), $shortUrlItem->get('idlang'));
189:                 $message .= i18n('Article', 'url_shortener') . ': ' . $artlang->get('title');
190:             }
191:             return $message;
192:             break;
193:     }
194:     return i18n('The entered short URL is not valid!', 'url_shortener');
195: }
196: 
197: /**
198:  * Function is called after the plugins have been loaded.
199:  * If the string placeholder in the example URL http://www.domain.de/placeholder
200:  * is a defined short URL, the user is redirected to the correct URL.
201:  *
202:  * @throws cDbException
203:  * @throws cException
204:  * @throws cInvalidArgumentException
205:  */
206: function piUsAfterLoadPlugins() {
207:     $requestUri = $_SERVER['REQUEST_URI'];
208:     $shorturl = cString::getPartOfString($requestUri, cString::findLastPos($requestUri, '/') + 1);
209:     $shortUrlItem = new cApiShortUrl();
210:     $shortUrlItem->loadBy('shorturl', $shorturl);
211:     if ($shortUrlItem->isLoaded()) {
212:         $uriParams = array(
213:             'idart' => $shortUrlItem->get('idart'),
214:             'lang' => $shortUrlItem->get('idlang')
215:         );
216:         $url = cUri::getInstance()->build($uriParams, true);
217:         header('Location:' . $url);
218:         exit();
219:     }
220: }
221: 
222: /**
223:  * Chain for delete short urls at con_deleteart action
224:  *
225:  * @param int $idart
226:  *         ID of deleted article
227:  *
228:  * @return int
229:  *         Number of deleted entries
230:  * @throws cDbException
231:  * @throws cException
232:  * @throws cInvalidArgumentException
233:  */
234: function piUseConDeleteArtAfter($idart)
235: {
236:     $count = 0;
237:     if (cRegistry::getPerm()->have_perm_area_action('url_shortener', 'url_shortener_delete')) {
238:         $idart        = cSecurity::toInteger($idart);
239:         $shortUrlColl = new cApiShortUrlCollection();
240:         $count        = $shortUrlColl->deleteBy('idart', $idart);
241:     }
242: 
243:     return $count;
244: }
245: 
CMS CONTENIDO 4.10.1 API documentation generated by ApiGen 2.8.0