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
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
   1: <?php
   2: /**
   3:  * This file contains the module handler class.
   4:  * TODO: Rework comments of this class.
   5:  *
   6:  * @package Core
   7:  * @subpackage Backend
   8:  * @version SVN Revision $Rev:$
   9:  *
  10:  * @author Rusmir Jusufovic
  11:  * @copyright four for business AG <www.4fb.de>
  12:  * @license http://www.contenido.org/license/LIZENZ.txt
  13:  * @link http://www.4fb.de
  14:  * @link http://www.contenido.org
  15:  */
  16: 
  17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
  18: 
  19: /**
  20:  * Class for new modul structere.
  21:  * Saves the Modul-Input in a file (input.php)
  22:  * and
  23:  * Modul-Output in a file(output.php).
  24:  * All moduls of a clients are in [frontend]/modules/.
  25:  *
  26:  * @package Core
  27:  * @subpackage Backend
  28:  */
  29: class cModuleHandler {
  30: 
  31:     /**
  32:      * Path to a modul dir
  33:      *
  34:      * @var string
  35:      */
  36:     private $_modulePath;
  37: 
  38:     /**
  39:      * Path to the modul dir where are all the moduls of a client (frontendpath)
  40:      *
  41:      * @var string
  42:      */
  43:     private $_path;
  44: 
  45:     /**
  46:      * Id of the Modul
  47:      *
  48:      * @var int
  49:      */
  50:     protected $_idmod = NULL;
  51: 
  52:     /**
  53:      * The name of the modul
  54:      *
  55:      * @var string
  56:      */
  57:     private $_moduleName = NULL;
  58: 
  59:     /**
  60:      * Description of the modul.
  61:      *
  62:      * @var string
  63:      */
  64:     protected $_description;
  65: 
  66:     /**
  67:      * The type of the modul.
  68:      *
  69:      * @var string
  70:      */
  71:     protected $_type;
  72: 
  73:     /**
  74:      * The alias name of the modul
  75:      *
  76:      * @var string
  77:      */
  78:     protected $_moduleAlias;
  79: 
  80:     /**
  81:      * The names of the modul directories.
  82:      *
  83:      * @var array
  84:      */
  85:     protected $_directories = array(
  86:         'css' => 'css/',
  87:         'js' => 'js/',
  88:         'template' => 'template/',
  89:         'image' => 'image/',
  90:         'lang' => 'lang/',
  91:         'php' => 'php/'
  92:     );
  93: 
  94:     /**
  95:      * CONTENIDO cfg
  96:      *
  97:      * @var array
  98:      */
  99:     protected $_cfg = NULL;
 100: 
 101:     /**
 102:      * Contenido cfgClient
 103:      *
 104:      * @var array
 105:      */
 106:     protected $_cfgClient = NULL;
 107: 
 108:     /**
 109:      * id of the Client
 110:      *
 111:      * @var int
 112:      */
 113:     protected $_client = '0';
 114: 
 115:     /**
 116:      * The code of the modul input
 117:      *
 118:      * @var string
 119:      */
 120:     protected $_input = '';
 121: 
 122:     /**
 123:      * The code of the modul output
 124:      *
 125:      * @var string
 126:      */
 127:     protected $_output = '';
 128: 
 129:     /**
 130:      * Encoding oft the site
 131:      *
 132:      * @var string
 133:      */
 134:     protected $_encoding = '';
 135: 
 136:     /**
 137:      * Which format of encoding should be files (input/output/translation...)
 138:      * getEffectiveSetting('encoding', 'file_encoding', 'UTF-8')
 139:      *
 140:      * @var string
 141:      */
 142:     protected $_fileEncoding = '';
 143: 
 144:     /**
 145:      * The id of the lang
 146:      *
 147:      * @var int
 148:      */
 149:     protected $_idlang = -1;
 150: 
 151:     /**
 152:      *
 153:      * @var cDb
 154:      */
 155:     private $_db = NULL;
 156: 
 157:     /**
 158:      *
 159:      * @var array
 160:      */
 161:     protected static $_encodingStore = array();
 162: 
 163:     /**
 164:      * Constructor for the class cModuleHandler.
 165:      * With this class you can
 166:      * create a new module, rename a module. You can save a Output from modules and
 167:      * Input in a
 168:      * file. The save rules are [Modulname] (is unique) the files input and
 169:      * output will be named
 170:      * [Modulname]_input.php , [Modulname]_output.php
 171:      *
 172:      * @param int $idmod the id of the module
 173:      * @throws cException if the module directory can not be created
 174:      */
 175:     public function __construct($idmod = NULL) {
 176:         global $cfg, $cfgClient, $lang, $client;
 177:         $this->_cfg = $cfg;
 178:         $this->_client = $client;
 179:         $this->_cfgClient = $cfgClient;
 180:         $this->_idlang = $lang;
 181:         $this->_encoding = self::getEncoding();
 182:         $this->_fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
 183: 
 184:         $this->_db = cRegistry::getDb();
 185: 
 186:         $this->_idmod = $idmod;
 187: 
 188:         $this->_initByModule($idmod);
 189: 
 190:         if ($this->_makeModuleDirectory() == false) {
 191:             throw new cException('Can not create main module directory.');
 192:         }
 193:     }
 194: 
 195:     public static function getEncoding($overrideLanguageId = 0) {
 196:         $lang = cRegistry::getLanguageId();
 197: 
 198:         if ((int) $overrideLanguageId != 0) {
 199:             $lang = $overrideLanguageId;
 200:         }
 201: 
 202:         if ((int) $lang == 0) {
 203:             $clientId = cRegistry::getClientId();
 204: 
 205:             $clientsLangColl = new cApiClientLanguageCollection();
 206:             $clientLanguages = $clientsLangColl->getLanguagesByClient($clientId);
 207:             sort($clientLanguages);
 208: 
 209:             if (isset($clientLanguages[0]) && (int) $clientLanguages[0] != 0) {
 210:                 $lang = $clientLanguages[0];
 211:             }
 212:         }
 213: 
 214:         if (!isset(self::$_encodingStore[$lang])) {
 215:             $cApiLanguage = new cApiLanguage($lang);
 216:             self::$_encodingStore[$lang] = $cApiLanguage->get('encoding');
 217:         }
 218: 
 219:         return self::$_encodingStore[$lang];
 220:     }
 221: 
 222:     /**
 223:      * Exist the modulname in directory
 224:      *
 225:      * @param string $name
 226:      * @param array $cfgClient
 227:      */
 228:     public function modulePathExistsInDirectory($name) {
 229:         return is_dir($this->_cfgClient[$this->_client]['module']['path'] . $name . '/');
 230:     }
 231: 
 232:     /**
 233:      * Save a content in the file, use for css/js
 234:      *
 235:      * @param string $frontendPath
 236:      * @param string $templateName
 237:      * @param string $fileType
 238:      * @param string $fileContent
 239:      * @return bool string
 240:      */
 241:     public function saveContentToFile($templateName, $fileType, $fileContent, $saveDirectory = 'cache') {
 242:         $sSaveDirectory = $this->_cfgClient[$this->_client]['path']['frontend'] . $saveDirectory . '/';
 243:         if (!is_dir($sSaveDirectory)) {
 244:             return false;
 245:         }
 246: 
 247:         $templateName = str_replace(' ', '_', $templateName);
 248:         $templateName = strtolower($templateName);
 249:         $fileOperation = cFileHandler::write($sSaveDirectory . $templateName . '.' . $fileType, $fileContent);
 250:         if ($fileOperation === false) {
 251:             return false;
 252:         }
 253:         $url = $this->_cfgClient[$this->_client]['path']['htmlpath'] . $saveDirectory . '/' . $templateName . '.' . $fileType;
 254: 
 255:         // Remove protocol so CSS & JS can be displayed for HTTPS too!
 256:         $url = str_replace('http://', '//', $url);
 257: 
 258:         return $url;
 259:     }
 260: 
 261:     /**
 262:      * Get the cleaned name
 263:      *
 264:      * @param string $name mod name
 265:      * @param string $defaultChar default character
 266:      */
 267:     public static function getCleanName($name, $defaultChar = '_') {
 268:         // the first character of modul/Layut name should be [a-zA-Z0-9]|_|-
 269:         $name = cApiStrCleanURLCharacters($name);
 270:         // get the first charcte
 271:         $firstChar = substr($name, 0, 1);
 272:         if (!preg_match('/^[a-zA-Z0-9]|_|-$/', $firstChar)) {
 273:             // replace the first character
 274:             $name = $defaultChar . substr($name, 1);
 275:         }
 276: 
 277:         return $name;
 278:     }
 279: 
 280:     /**
 281:      * Init the vars of the class.
 282:      *
 283:      * @param array $modulData
 284:      *            [idmod],[name],[input],[output],[forntedpath],[client]
 285:      */
 286:     public function initWithDatabaseRow($db) {
 287:         if (is_object($db)) {
 288:             $this->_initByModule($db->f('idmod'));
 289:         }
 290:     }
 291: 
 292:     /**
 293:      * Init the vars of the class, make a query to the Db
 294:      *
 295:      * @param int $idmod the id of the modul
 296:      */
 297:     protected function _initByModule($idmod = NULL) {
 298:         if ((int) $idmod == 0) {
 299:             return;
 300:         }
 301: 
 302:         $cApiModule = new cApiModule($idmod);
 303: 
 304:         if ($cApiModule->virgin == false) {
 305:             $this->_idmod = $idmod;
 306:             $this->_client = $cApiModule->get('idclient');
 307:             $this->_description = $cApiModule->get('description');
 308:             $this->_type = $cApiModule->get('type');
 309:             $this->_input = '';
 310:             $this->_output = '';
 311: 
 312:             $this->_moduleAlias = $cApiModule->get('alias');
 313:             $this->_moduleName = $cApiModule->get('name');
 314:             $this->_path = $this->_cfgClient[$this->_client]['module']['path'];
 315:             $this->_modulePath = $this->_path . $this->_moduleAlias . '/';
 316:         }
 317:     }
 318: 
 319:     /**
 320:      * Get the Modul Path also cms path + module + module name.
 321:      *
 322:      * @return string
 323:      */
 324:     public function getModulePath() {
 325:         return $this->_modulePath;
 326:     }
 327: 
 328:     /**
 329:      * Get the template path.
 330:      * If file is set it will
 331:      * return the complete path + file
 332:      *
 333:      * @param string $file
 334:      * @return string
 335:      */
 336:     public function getTemplatePath($file = '') {
 337:         return $this->_modulePath . $this->_directories['template'] . $file;
 338:     }
 339: 
 340:     /**
 341:      * Get the css path of the modul
 342:      *
 343:      * @return string
 344:      */
 345:     public function getCssPath() {
 346:         return $this->_modulePath . $this->_directories['css'];
 347:     }
 348: 
 349:     /**
 350:      * Get the php path of the modul
 351:      *
 352:      * @return string
 353:      */
 354:     public function getPhpPath() {
 355:         return $this->_modulePath . $this->_directories['php'];
 356:     }
 357: 
 358:     /**
 359:      * Get the js path of the modul
 360:      *
 361:      * @return string
 362:      */
 363:     public function getJsPath() {
 364:         return $this->_modulePath . $this->_directories['js'];
 365:     }
 366: 
 367:     /**
 368:      * Get the main css file modulenam.css
 369:      *
 370:      * @return string
 371:      */
 372:     public function getCssFileName() {
 373:         return $this->_moduleAlias . '.css';
 374:     }
 375: 
 376:     /**
 377:      * Returns random characters
 378:      *
 379:      * @param int $count amount of characters
 380:      * @return string
 381:      */
 382:     protected function getRandomCharacters($count) {
 383:         $micro1 = microtime();
 384:         $rand1 = rand(0, time());
 385:         $rand2 = rand(0, time());
 386:         return substr(md5($micro1 . $rand1 . $rand2), 0, $count);
 387:     }
 388: 
 389:     /**
 390:      * Check if exist a file
 391:      *
 392:      * @param string $type js | template | css the directory of the file
 393:      * @param string $fileName file name
 394:      */
 395:     public function existFile($type, $fileName) {
 396:         return cFileHandler::exists($this->_modulePath . $this->_directories[$type] . $fileName);
 397:     }
 398: 
 399:     /**
 400:      * Delete file
 401:      *
 402:      * @param string $type js |template | css directory of the file
 403:      * @param string $fileName file name
 404:      */
 405:     public function deleteFile($type, $fileName) {
 406:         if ($this->existFile($type, $fileName)) {
 407:             return unlink($this->_modulePath . $this->_directories[$type] . $fileName);
 408:         } else {
 409:             return false;
 410:         }
 411:     }
 412: 
 413:     /**
 414:      * Create and save new file
 415:      *
 416:      * @param string $type css | js | template directory of the file
 417:      * @param string $fileName file name
 418:      * @param string $content content of the file
 419:      * @return boolean true if file can be created, and false otherwise
 420:      */
 421:     public function createModuleFile($type, $fileName = NULL, $content = '') {
 422:         // make directory if not exist
 423:         if (!$this->createModuleDirectory($type)) {
 424:             return false;
 425:         }
 426: 
 427:         // if not set use default filename
 428:         if ($fileName == NULL || $fileName == '') {
 429:             $fileName = $this->_moduleAlias;
 430: 
 431:             if ($type == 'template') {
 432:                 $fileName = $fileName . '.html';
 433:             } else {
 434:                 $fileName = $fileName . '.' . $type;
 435:             }
 436:         }
 437: 
 438:         // make and save file contents
 439:         if ($type == 'css' || $type == 'js' || $type == 'template') {
 440:             if (!$this->existFile($type, $fileName)) {
 441:                 $content = iconv($this->_encoding, $this->_fileEncoding, $content);
 442:                 if (!$this->isWritable($this->_modulePath . $this->_directories[$type] . $fileName, $this->_modulePath . $this->_directories[$type])) {
 443:                     return false;
 444:                 }
 445: 
 446:                 if (cFileHandler::write($this->_modulePath . $this->_directories[$type] . $fileName, $content) === false) {
 447:                     $notification = new cGuiNotification();
 448:                     $notification->displayNotification('error', i18n("Can't make file: ") . $fileName);
 449:                     return false;
 450:                 }
 451:             } else {
 452:                 $content = iconv($this->_encoding, $this->_fileEncoding, $content);
 453:                 if (!$this->isWritable($this->_modulePath . $this->_directories[$type] . $fileName, $this->_modulePath . $this->_directories[$type])) {error_log("no write");
 454:                     return false;
 455:                 }
 456:                 if (cFileHandler::write($this->_modulePath . $this->_directories[$type] . $fileName, $content) === false) {
 457:                     $notification = new cGuiNotification();
 458:                     $notification->displayNotification('error', i18n("Can't make file: ") . $fileName);
 459:                     return false;
 460:                 }
 461:             }
 462:         } else {
 463:             return false;
 464:         }
 465: 
 466:         return true;
 467:     }
 468: 
 469:     /**
 470:      * Rename a file
 471:      *
 472:      * @param string $type css | js | template directory of the file
 473:      * @param string $oldFileName old name of the file
 474:      * @param string $newFileName the new name of the file
 475:      * @return boolean by success return true
 476:      */
 477:     public function renameModuleFile($type, $oldFileName, $newFileName) {
 478:         if ($this->existFile($type, $newFileName)) {
 479:             return false;
 480:         }
 481: 
 482:         if (!$this->existFile($type, $oldFileName)) {
 483:             return false;
 484:         }
 485: 
 486:         return rename($this->_modulePath . $this->_directories[$type] . $oldFileName, $this->_modulePath . $this->_directories[$type] . $newFileName);
 487:     }
 488: 
 489:     /**
 490:      * Get the name of the main js file (modulname.js)
 491:      *
 492:      * @return string the name of the js file
 493:      */
 494:     public function getJsFileName() {
 495:         return $this->_moduleAlias . '.js';
 496:     }
 497: 
 498:     /**
 499:      * Get the content of file, modul js or css or template or php
 500:      *
 501:      * @param string $directory where in module should we look
 502:      * @param string $fileTyp css or js
 503:      */
 504:     public function getFilesContent($directory, $fileTyp, $fileName = NULL) {
 505:         if ($fileName == NULL) {
 506:             $fileName = $this->_moduleAlias . '.' . $fileTyp;
 507:         }
 508: 
 509:         if ($this->existFile($directory, $fileName)) {
 510:             $content = cFileHandler::read($this->_modulePath . $this->_directories[$directory] . $fileName);
 511:             $content = iconv($this->_fileEncoding, $this->_encoding . '//IGNORE', $content);
 512:             return $content;
 513:         }
 514: 
 515:         return false;
 516:     }
 517: 
 518:     /**
 519:      * Make main module directory.
 520:      *
 521:      * @return boolean
 522:      */
 523:     protected function _makeModuleDirectory() {
 524:         // Do not display error on login page
 525:         if ((int) $this->_client == 0) {
 526:             return true;
 527:         }
 528: 
 529:         $sMainModuleDirectory = $this->_cfgClient[$this->_client]['module']['path'];
 530: 
 531:         // make
 532:         if (!is_dir($sMainModuleDirectory) && $sMainModuleDirectory != NULL) {
 533:             if (mkdir($sMainModuleDirectory, 0777, true) == false) {
 534:                 return false;
 535:             } else {
 536:                 cDirHandler::setDefaultDirPerms($sMainModuleDirectory);
 537:             }
 538:         }
 539: 
 540:         return true;
 541:     }
 542: 
 543:     /**
 544:      * Get all files from a module directory
 545:      *
 546:      * @param string $moduleDirectory template css or js...
 547:      * @return array
 548:      */
 549:     public function getAllFilesFromDirectory($moduleDirectory) {
 550:         $dir = $this->_modulePath . $this->_directories[$moduleDirectory];
 551:         return cDirHandler::read($dir);
 552:     }
 553: 
 554:     /**
 555:      * Set the new modul name.
 556:      *
 557:      * @var $name string
 558:      */
 559:     public function changeModuleName($name) {
 560:         $this->_moduleAlias = $name;
 561:         $this->_modulePath = $this->_path . $this->_moduleAlias . '/';
 562:     }
 563: 
 564:     /**
 565:      * Removes this module from the filesystem.
 566:      * Also deletes the version files.
 567:      *
 568:      * @return bool true on success or false on failure
 569:      */
 570:     public function eraseModule() {
 571:         // Delete modules only if we find info.xml at module path
 572:         if (cFileHandler::exists($this->_modulePath . 'info.xml')) {
 573:             return cDirHandler::recursiveRmdir($this->_modulePath);
 574:         } else {
 575:             return false;
 576:         }
 577:     }
 578: 
 579:     /**
 580:      * Read the input of the file _input.php
 581:      *
 582:      * @return string Contents of the Module file (_input.php)
 583:      */
 584:     public function readInput($issource = false) {
 585:         if (cFileHandler::exists($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_input.php') == false) {
 586:             return false;
 587:         }
 588: 
 589:         $content = cFileHandler::read($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_input.php');
 590: 
 591:         if ($issource == true) {
 592:             $content = conHtmlentities($content);
 593:         }
 594: 
 595:         return iconv($this->_fileEncoding, $this->_encoding . '//IGNORE', $content);
 596:     }
 597: 
 598:     /**
 599:      * Read the output of the file _output.php
 600:      *
 601:      * @return string Contents of the Module file( _output.php)
 602:      */
 603:     public function readOutput($issource = false) {
 604:         if (cFileHandler::exists($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_output.php') == false) {
 605:             return false;
 606:         }
 607: 
 608:         $content = cFileHandler::read($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_output.php');
 609: 
 610:         if ($issource == true) {
 611:             $content = conHtmlentities($content);
 612:         }
 613: 
 614:         return iconv($this->_fileEncoding, $this->_encoding . '//IGNORE', $content);
 615:     }
 616: 
 617:     /**
 618:      * Make a directory template/css/image/js/php if not exist
 619:      *
 620:      * @param string $type
 621:      */
 622:     protected function createModuleDirectory($type) {
 623:         if (array_key_exists($type, $this->_directories)) {
 624:             if (!is_dir($this->_modulePath . $this->_directories[$type])) {
 625:                 if (cDirHandler::create($this->_modulePath . $this->_directories[$type]) == false) {
 626:                     return false;
 627:                 } else
 628:                     cDirHandler::setDefaultDirPerms($this->_modulePath . $this->_directories[$type]);
 629:             } else {
 630:                 return true;
 631:             }
 632:         }
 633: 
 634:         return true;
 635:     }
 636: 
 637:     /**
 638:      * Can write/create a file
 639:      *
 640:      * @param string $fileName file name
 641:      * @param string $directory directory where is the file
 642:      * @return bool, success true else false
 643:      */
 644:     public function isWritable($fileName, $directory) {
 645:         if (cFileHandler::exists($fileName)) {
 646:             if (!is_writable($fileName)) {
 647:                 return false;
 648:             }
 649:         } else {
 650:             if (!is_writable($directory)) {
 651:                 return false;
 652:             }
 653:         }
 654:         return true;
 655:     }
 656: 
 657:     /**
 658:      * Check write permissions for this module
 659:      *
 660:      * @param string $type php oder template
 661:      * @return boolean true or false
 662:      */
 663:     public function moduleWriteable($type) {
 664:         // check if type directory inside module folder exists and has write permissions
 665:         if (true === cFileHandler::exists($this->_modulePath . $this->_directories[$type])) {
 666:             return cFileHandler::writeable($this->_modulePath . $this->_directories[$type]);
 667:         }
 668: 
 669:         // check if module folder exists and has write permissions
 670:         if (true === cFileHandler::exists($this->_modulePath)) {
 671:             return cFileHandler::writeable($this->_modulePath);
 672:         }
 673: 
 674:         return false;
 675:     }
 676: 
 677:     /**
 678:      * Save a string into the file (_output.php).
 679:      *
 680:      * @param string
 681:      * @return bool if the action (save contents into the file _output.php) is
 682:      *         successful return true else false
 683:      */
 684:     public function saveOutput($output = NULL) {
 685:         $fileName = $this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_output.php';
 686: 
 687:         if (!$this->createModuleDirectory('php') || !$this->isWritable($fileName, $this->_modulePath . $this->_directories['php'])) {
 688:             return false;
 689:         }
 690: 
 691:         if ($output == NULL) {
 692:             $output = $this->_output;
 693:         }
 694: 
 695:         $output = iconv($this->_encoding, $this->_fileEncoding, $output);
 696: 
 697:         $fileOperation = cFileHandler::write($fileName, $output);
 698: 
 699:         if ($fileOperation === false) {
 700:             return false; // return false if file_put_contents dont work
 701:         } else {
 702:             cFileHandler::setDefaultFilePerms($fileName);
 703:             return true; // return true if file_put_contents working
 704:         }
 705:     }
 706: 
 707:     /**
 708:      * Save a string into the file (_input.php)
 709:      *
 710:      * @param string
 711:      * @return bool if the action (save contents into the file _input.php) is
 712:      *         successful return true else false
 713:      */
 714:     public function saveInput($input = NULL) {
 715:         $fileName = $this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_input.php';
 716: 
 717:         if (!$this->createModuleDirectory('php') || !$this->isWritable($fileName, $this->_modulePath . $this->_directories['php'])) {
 718:             return false;
 719:         }
 720: 
 721:         if ($input == NULL) {
 722:             $input = $this->_input;
 723:         }
 724: 
 725:         $input = iconv($this->_encoding, $this->_fileEncoding, $input);
 726: 
 727:         $fileOperation = cFileHandler::write($fileName, $input);
 728: 
 729:         if ($fileOperation === false) {
 730:             return false; // return false if file_put_contents dont work
 731:         } else {
 732:             cFileHandler::setDefaultFilePerms($fileName);
 733:             return true; // return true if file_put_contents working
 734:         }
 735:     }
 736: 
 737:     /**
 738:      * This method save a xml file with modul information.
 739:      * If the params not set, get the value from this
 740:      *
 741:      * @param string $moduleName name of the modul
 742:      * @param string $description description of the modul
 743:      * @param string $type type of the modul
 744:      * @return true if success else false
 745:      */
 746:     public function saveInfoXML($moduleName = NULL, $description = NULL, $type = NULL, $alias = NULL) {
 747:         if ($moduleName == NULL) {
 748:             $moduleName = $this->_moduleName;
 749:         }
 750: 
 751:         if ($description == NULL) {
 752:             $description = $this->_description;
 753:         }
 754: 
 755:         if ($type == NULL) {
 756:             $type = $this->_type;
 757:         }
 758: 
 759:         if ($alias == NULL) {
 760:             $alias = $this->_moduleAlias;
 761:         }
 762: 
 763:         $oWriter = new cXmlWriter();
 764:         $oRootElement = $oWriter->addElement('module', '', NULL);
 765: 
 766:         $oWriter->addElement('name', conHtmlSpecialChars($moduleName), $oRootElement);
 767:         $oWriter->addElement('description', conHtmlSpecialChars($description), $oRootElement);
 768:         $oWriter->addElement('type', conHtmlSpecialChars($type), $oRootElement);
 769:         $oWriter->addElement('alias', conHtmlSpecialChars($alias), $oRootElement);
 770: 
 771:         return $oWriter->saveToFile($this->_modulePath, 'info.xml');
 772:     }
 773: 
 774:     /**
 775:      * Make a new module into the modul dir.
 776:      * The modul name will be [ModulName] example
 777:      * Contact_Form or GoogleMaps2.
 778:      *
 779:      * @return bool if modul exist or mkdir and saveInput and saveOutput success
 780:      *         return true.
 781:      *         Else if the mkdir or saveInput or saveOutput not success return
 782:      *         false.
 783:      */
 784:     public function createModule($input = '', $output = '') {
 785:         if ($input != '') {
 786:             $this->_input = $input;
 787:         }
 788: 
 789:         if ($output != '') {
 790:             $this->_output = $output;
 791:         }
 792: 
 793:         if ($this->modulePathExists()) {
 794:             return true;
 795:         }
 796: 
 797:         if (mkdir($this->_modulePath) == false) {
 798:             return false;
 799:         } else {
 800:             cDirHandler::setDefaultDirPerms($this->_modulePath);
 801:         }
 802: 
 803:         // make others directorys
 804:         foreach ($this->_directories as $directory) {
 805:             if (!is_dir($this->_modulePath . $directory)) {
 806:                 if (mkdir($this->_modulePath . $directory) == false) {
 807:                     return false;
 808:                 } else {
 809:                     cDirHandler::setDefaultDirPerms($this->_modulePath . $directory);
 810:                 }
 811:             }
 812:         }
 813: 
 814:         // could not save the info xml
 815:         if ($this->saveInfoXML() == false) {
 816:             return false;
 817:         }
 818: 
 819:         // Save empty strings into the modul files, if someone trying to read
 820:         // contents bevore save into the files
 821:         $retInput = $this->saveInput();
 822:         $retOutput = $this->saveOutput();
 823: 
 824:         if ($retInput == false || $retOutput == false) {
 825:             return false;
 826:         }
 827: 
 828:         return true;
 829:     }
 830: 
 831:     /**
 832:      * Rename a modul and the input and output files.
 833:      *
 834:      * @param string $old old name of the modul
 835:      * @param string $new new name of the modul
 836:      *
 837:      * @return bool true if success
 838:      */
 839:     public function renameModul($old, $new) {
 840:         // try to rename the dir
 841:         if (rename($this->_path . $old, $this->_path . $new) == false) {
 842:             return false;
 843:         } else {
 844:             $retInput = true;
 845:             $retOutput = true;
 846: 
 847:             // if file input exist rename it
 848:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['php'] . $old . '_input.php'))
 849:                 $retInput = rename($this->_path . $new . '/' . $this->_directories['php'] . $old . '_input.php', $this->_path . $new . '/' . $this->_directories['php'] . $new . '_input.php');
 850: 
 851:                 // if file output exist rename it
 852:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['php'] . $old . '_output.php'))
 853:                 $retOutput = rename($this->_path . $new . '/' . $this->_directories['php'] . $old . '_output.php', $this->_path . $new . '/' . $this->_directories['php'] . $new . '_output.php');
 854: 
 855:                 // rename the css file
 856:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['css'] . $old . '.css'))
 857:                 rename($this->_path . $new . '/' . $this->_directories['css'] . $old . '.css', $this->_path . $new . '/' . $this->_directories['css'] . $new . '.css');
 858: 
 859:                 // rename the javascript file
 860:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['js'] . $old . '.js'))
 861:                 rename($this->_path . $new . '/' . $this->_directories['js'] . $old . '.js', $this->_path . $new . '/' . $this->_directories['js'] . $new . '.js');
 862: 
 863:                 // rename the template file
 864:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['template'] . $old . '.html'))
 865:                 rename($this->_path . $new . '/' . $this->_directories['template'] . $old . '.html', $this->_path . $new . '/' . $this->_directories['template'] . $new . '.html');
 866: 
 867:             if ($retInput == true && $retOutput == true) {
 868:                 return true;
 869:             } else {
 870:                 return false;
 871:             }
 872:         }
 873:     }
 874: 
 875:     /**
 876:      * Show if the Modul with the modul name exist in modul dir.
 877:      *
 878:      * return bool if the modul exist return true, else false
 879:      */
 880:     public function modulePathExists() {
 881:         return is_dir($this->_modulePath);
 882:     }
 883: 
 884:     /*
 885:      * Test input code
 886:      *
 887:      * @return array (bool state, string errorMessage)
 888:      */
 889:     public function testInput() {
 890: 
 891:         return $this->_testCode('input');
 892: 
 893:     }
 894: 
 895:     /*
 896:      * Test output code
 897:      *
 898:      * @return array (bool state, string errorMessage)
 899:      */
 900:     public function testOutput() {
 901: 
 902:         return $this->_testCode('output');
 903: 
 904:     }
 905: 
 906:     /*
 907:      * Test module code
 908:      *
 909:      * @param $inputType string code field type
 910:      *
 911:      * @return array (bool state, string errorMessage)
 912:      */
 913:     protected function _testCode($inputType) {
 914: 
 915:         $result = array(
 916:             'state' => false,
 917:             'errorMessage' => 'Module path not exist'
 918:         );
 919: 
 920:         if (!$this->modulePathExists()) return $result;
 921: 
 922:         $module  = new cApiModule($this->_idmod);
 923:         $isError = 'none';
 924: 
 925:         //Set code as error before checking, if fatal exist
 926:         switch ($module->get("error")) {
 927:             case 'none';
 928:             $toCheck = $inputType;
 929:             break;
 930:             case 'input';
 931:             if ($inputType == 'output') $toCheck = 'both';
 932:             break;
 933:             case 'output';
 934:             if ($inputType == 'input') $toCheck = 'both';
 935:             break;
 936:             case 'both';
 937: 
 938:             break;
 939:         }
 940:         if ($toCheck !== $module->get("error")) {
 941:             $module->set("error", $toCheck);
 942: 
 943:             // do not rewrite cache on validation
 944:             // it is rewritten when saving module
 945:             $module->store(true);
 946:         }
 947: 
 948:         //check code
 949:         switch($inputType) {
 950:             case 'input':
 951: 
 952:                 $code       = $this->readInput();
 953:                 $result = $this->_verifyCode($code, $this->_idmod . "i");
 954:                 if ($result['state'] !== true) $isError = 'input';
 955: 
 956:                 break;
 957:             case 'output':
 958: 
 959:                 $code       = $this->readOutput();
 960:                 $result = $this->_verifyCode($code, $this->_idmod . "o", true);
 961:                 if ($result['state'] !== true) $isError = 'output';
 962: 
 963:                 break;
 964:         }
 965: 
 966:         //update error value for input and output
 967:         switch ($module->get("error")) {
 968:             case 'none';
 969: 
 970:             break;
 971:             case 'input';
 972:             if ($isError == 'none' && $inputType == 'output') $isError = 'input';
 973:             if ($isError == 'output') $isError = 'both';
 974:             break;
 975:             case 'output';
 976:             if ($isError == 'none' && $inputType == 'input') $isError = 'output';
 977:             if ($isError == 'input') $isError = 'both';
 978:             break;
 979:             case 'both';
 980:             if ($isError == 'none' && $inputType == 'input') $isError = 'output';
 981:             if ($isError == 'none' && $inputType == 'output') $isError = 'input';
 982:             break;
 983:         }
 984: 
 985:         //Store error information in the database (to avoid re-eval for module
 986:         //overview/menu)
 987:         if ($isError !== $module->get("error")) {
 988:             $module->set("error", $isError);
 989: 
 990:             // do not rewrite cache on validation
 991:             // it is rewritten when saving module
 992:             $module->store(true);
 993:         }
 994: 
 995:         return $result;
 996: 
 997:     }
 998: 
 999:     /**
1000:      * Check module php code
1001:      *
1002:      * @param string $code Code to evaluate
1003:      * @param string $id Unique ID for the test function
1004:      * @param string $mode true if start in php mode, otherwise false
1005:      *
1006:      * @return array (bool state, string errorMessage)
1007:      */
1008:     protected function _verifyCode($code, $id, $output = false) {
1009:         $isError = false;
1010:         $result = array(
1011:             'state' => false,
1012:             'errorMessage' => NULL
1013:         );
1014: 
1015:         // Put a $ in front of all CMS variables to prevent PHP error messages
1016:         $sql = 'SELECT type FROM ' . $this->_cfg['tab']['type'];
1017:         $this->_db->query($sql);
1018:         while ($this->_db->nextRecord()) {
1019:             $code = str_replace($this->_db->f('type') . '[', '$' . $this->_db->f('type') . '[', $code);
1020:         }
1021: 
1022:         $code = preg_replace(',\[(\d+)?CMS_VALUE\[(\d+)\](\d+)?\],i', '[\1\2\3]', $code);
1023:         $code = str_replace('CMS_VALUE', '$CMS_VALUE', $code);
1024:         $code = str_replace('CMS_VAR', '$CMS_VAR', $code);
1025: 
1026:         // If the module is an output module, escape PHP since all output modules
1027:         // enter php mode
1028:         if ($output == true) {
1029:             $code = "?>\n" . $code . "\n<?php";
1030:         }
1031: 
1032:         // Looks ugly: Paste a function declarator in front of the code
1033:         $code = 'function foo' . $id . ' () {' . $code;
1034:         $code .= "\n}\n";
1035: 
1036:         // To parse the error message, we prepend and append a phperror tag in front
1037:         // of the output
1038:         $sErs = ini_get('error_prepend_string'); // Save current setting (see below)
1039:         $sEas = ini_get('error_append_string'); // Save current setting (see below)
1040:         @ini_set('error_prepend_string', '<phperror>');
1041:         @ini_set('error_append_string', '</phperror>');
1042: 
1043:         // Turn off output buffering and error reporting, eval the code
1044:         ob_start();
1045:         $display_errors = ini_get('display_errors');
1046:         @ini_set('display_errors', true);
1047:         $output = eval($code);
1048:         @ini_set('display_errors', $display_errors);
1049: 
1050:         // Get the buffer contents and turn it on again
1051:         $output = ob_get_contents();
1052:         ob_end_clean();
1053: 
1054:         // Remove the prepend and append settings
1055:         /*
1056:          * 19.09.2006: Following lines have been disabled, as ini_restore has been
1057:          * disabled by some hosters as there is a security leak in PHP (PHP <= 5.1.6
1058:          * & <= 4.4.4)
1059:          */
1060:         // ini_restore('error_prepend_string');
1061:         // ini_restore('error_append_string');
1062:         @ini_set('error_prepend_string', $sErs); // Restoring settings (see above)
1063:         @ini_set('error_append_string', $sEas); // Restoring settings (see above)
1064: 
1065:         // Strip out the error message
1066:         if ($isError === false) {
1067:             $isError = strpos($output, '<phperror>');
1068:         }
1069: 
1070:         // More stripping: Users shouldnt see where the file is located, but they
1071:         // should see the error line
1072:         if ($isError !== false) {
1073:             if (isset($modErrorMessage) === false) {
1074:                 $pattern         = '/(<phperror>|<\/phperror>|<b>|<\/b>|<br>|<br \/>)/im';
1075:                 $modErrorMessage = trim(preg_replace($pattern, '', $output));
1076:                 $errorPart1      = substr($modErrorMessage, 0, strpos($modErrorMessage, ' in '));
1077:                 $errorPart2      = substr($modErrorMessage, strpos($modErrorMessage, ' on line '));
1078:                 $modErrorMessage = $errorPart1 . $errorPart2;
1079:             }
1080:             $result['errorMessage'] = sprintf(i18n("Error in module. Error location: %s"), $modErrorMessage);
1081: 
1082:         }
1083: 
1084:         // Check if there are any php short tags in code, and display error
1085:         $bHasShortTags = false;
1086:         if (preg_match('/<\?\s+/', $code)) {
1087:             $bHasShortTags = true;
1088:             $result['errorMessage'] = i18n('Please do not use short open tags. (Use <?php instead of <?).');
1089:         }
1090: 
1091:         // Now, check if the magic value is 941. If not, the function didn't compile
1092:         if ($bHasShortTags || $isError !== false) {
1093:             $result['state'] = false;
1094:         } else {
1095:             $result['state'] = true;
1096:         }
1097: 
1098:         return $result;
1099: 
1100:     }
1101: 
1102:     /**
1103:      * Get method for module path value
1104:      * Path to a module dir
1105:      *
1106:      * @return $modulePath string
1107:      */
1108:     protected static function _getModulePath() {
1109:          return $this->_modulePath;
1110:     }
1111: 
1112: }
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen