This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 
<?php defined('C5_EXECUTE') or die("Access Denied.");

/**
 * @package Core
 * @category Concrete
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */

/**
 * A wrapper for loading core files, libraries, applications and models. Whenever possible the loader class should be used because it will always know where to look for the proper files, in the proper order.
 * @package Core
 * @author Andrew Embler <[email protected]>
 * @category Concrete
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 */
 
 class Concrete5_Library_Loader {
        
        static $autoloadClasses = array();
        
        /** 
         * Loads a library file, either from the site's files or from Concrete's
         */
        public static function library($lib, $pkgHandle = null) {
            $env = Environment::get();
            require_once($env->getPath(DIRNAME_LIBRARIES . '/' . $lib . '.php', $pkgHandle));
        }

        /** 
         * Loads a job file, either from the site's files or from Concrete's
         */
        public static function job($job, $pkgHandle = null) {
            $env = Environment::get();
            require_once($env->getPath(DIRNAME_JOBS . '/' . $job . '.php', $pkgHandle));
        }

        /** 
         * Loads a model from either an application, the site, or the core Concrete directory
         */
        public static function model($mod, $pkgHandle = null) {
            $env = Environment::get();
            $r = self::legacyModel($mod);
            if (!$r) {
                require_once($env->getPath(DIRNAME_MODELS . '/' . $mod . '.php', $pkgHandle));
            }
        }
        
        protected static function legacyModel($model) {
            switch($model) {
                case 'collection_attributes':
                    self::model('attribute/categories/collection');
                    return true;
                    break;
                case 'user_attributes':
                    self::model('attribute/categories/user');
                    return true;
                    break;
                case 'file_attributes':
                    self::model('attribute/categories/file');
                    return true;
                    break;
                default:
                    return false;
                    break;
            }
        }
        
        /** 
         * @access private
         */
        public function packageElement($file, $pkgHandle, $args = null) {
            self::element($file, $args, $pkgHandle);
        }

        /** 
         * Loads an element from C5 or the site
         */
        public function element($_file, $args = null, $_pkgHandle= null) {
            if (is_array($args)) {
                $collisions = array_intersect(array('_file', '_pkgHandle'), array_keys($args));
                if ($collisions) {
                    throw new Exception(t("Illegal variable name '%s' in element args.", implode(', ', $collisions)));
                }
                $collisions = null;
                extract($args);
            }

            include(Environment::get()->getPath(DIRNAME_ELEMENTS . '/' . $_file . '.php', $_pkgHandle));
        }

         /**
         * Loads a tool file from c5 or site
         */
        public static function tool($file, $args = null, $pkgHandle= null) {
           if (is_array($args)) {
               extract($args);
           }
            $env = Environment::get();
            require_once($env->getPath(DIRNAME_TOOLS . '/' . $file . '.php', $pkgHandle));
        }
        
        /** 
         * Registers a component with concrete5's autoloader.
         */
        public static function registerAutoload($classes) {
            foreach($classes as $class => $data) {  
                if (strpos($class, ',') > -1) {
                    $subclasses = explode(',', $class);
                    foreach($subclasses as $subclass) {
                        self::$autoloadClasses[$subclass] = $data;
                    }
                } else {
                    self::$autoloadClasses[$class] = $data;
                }
            }               
        }
        
        protected static function getFileFromCorePath($found) {
            $cl = array_key_exists($found, self::$autoloadClasses) ? self::$autoloadClasses[$found] : false;
            if ($cl) {
                $file = $cl[1];
            } else {
                $file = str_replace('_', '/', $found);
                $path = explode('/', $file);
                if (count($path) > 0) {
                    $file = '';
                    for ($i = 0; $i < count($path); $i++) {
                        $p = $path[$i];
                        $file .= Object::uncamelcase($p);
                        if (($i + 1) < count($path)) {
                            $file .= '/';
                        }                           
                    }
                } else {
                    $file = Object::uncamelcase($file);             
                }
            }
            return $file;
        }
        
        public static function autoloadCore($class) {
            if (strpos($class, $m = 'Concrete5_Model_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_MODELS . '/' . $file . '.php');
            }
            elseif (strpos($class, $m = 'Concrete5_Library_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_LIBRARIES . '/' . $file . '.php');
            }
            elseif (strpos($class, $m = 'Concrete5_Helper_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_HELPERS . '/' . $file . '.php');
            }
            elseif (strpos($class, $m = 'Concrete5_Controller_Block_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_CONTROLLERS . '/' . DIRNAME_BLOCKS . '/' . $file. '.php');
            }
            elseif (strpos($class, $m = 'Concrete5_Controller_PageType_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_CONTROLLERS . '/' . DIRNAME_PAGE_TYPES . '/' . $file. '.php');
            }
            elseif (strpos($class, $m = 'Concrete5_Controller_AttributeType_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $file . '.php');
            }
            elseif (strpos($class, $m = 'Concrete5_Controller_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_CONTROLLERS . '/' . DIRNAME_PAGES . '/' . $file . '.php');
            }
            elseif (strpos($class, $m = 'Concrete5_Job_') === 0) {
                $file = self::getFileFromCorePath(substr($class, strlen($m)));
                require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_JOBS . '/' . $file . '.php');
            }
        }
        
        /** 
         * @private
         */
        public static function autoload($class) {
            $classes = self::$autoloadClasses;
            $cl = $classes[$class];
            if ($cl) {
                call_user_func_array(array(__CLASS__, $cl[0]), array_slice($cl, 1, 2));
            } else {
                /* lets handle some things slightly more dynamically */             
                if (strpos($class, 'BlockController') > 0) {
                    $class = substr($class, 0, strpos($class, 'BlockController'));
                    $handle = Object::uncamelcase($class);
                    self::block($handle);
                } else if (strpos($class, 'AttributeType') > 0) {
                    $class = substr($class, 0, strpos($class, 'AttributeType'));
                    $handle = Object::uncamelcase($class);
                    $at = AttributeType::getByHandle($handle);
                } else  if (strpos($class, 'Helper') > 0) {
                    $class = substr($class, 0, strpos($class, 'Helper'));
                    $handle = Object::uncamelcase($class);
                    $handle = preg_replace('/^site_/', '', $handle);
                    self::helper($handle);
                }
            }
        }
        
        /** 
         * Loads a block's controller/class into memory. 
         * <code>
         * <?php self::block('autonav'); ?>
         * </code>
         */
        public static function block($bl) {
            $db = self::db();
            $pkgHandle = $db->GetOne('select pkgHandle from Packages left join BlockTypes on BlockTypes.pkgID = Packages.pkgID where BlockTypes.btHandle = ?', array($bl));
            $env = Environment::get();
            require_once($env->getPath(DIRNAME_BLOCKS . '/' . $bl . '/' . FILENAME_BLOCK_CONTROLLER, $pkgHandle));
        }
        
        /** 
         * Loads the various files for the database abstraction layer. We would bundle these in with the db() method below but
         * these need to be loaded before the models which need to be loaded before db() 
         */
        public function database() {
            require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb.inc.php');
            require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb-exceptions.inc.php');
            require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb-active-record.inc.php');
            require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb-xmlschema03.inc.php');
            require(DIR_BASE_CORE . '/libraries/database.php');
        }
        
        /** 
         * Returns the database object, or loads it if not yet created
         * <code>
         * <?php
         * $db = Loader::db();
         * $db->query($sql);
         * </code>
         * @return ADOConnection
         */
        public static function db($server = null, $username = null, $password = null, $database = null, $create = false, $autoconnect = true) {
            static $_dba;
            if ((!isset($_dba) || $create) && ($autoconnect)) {
                if ($server == null && defined('DB_SERVER')) {  
                    $dsn = DB_TYPE . '://' . DB_USERNAME . ':' . rawurlencode(DB_PASSWORD) . '@' . rawurlencode(DB_SERVER) . '/' . DB_DATABASE;
                } else if ($server) {
                    $dsn = DB_TYPE . '://' . $username . ':' . rawurlencode($password) . '@' . rawurlencode($server) . '/' . $database;
                }

                if (isset($dsn) && $dsn) {
                    $_dba = @NewADOConnection($dsn);
                    if (is_object($_dba)) {
                        $_dba->setFetchMode(ADODB_FETCH_ASSOC);
                        if (DB_CHARSET != '') {
                            $names = 'SET NAMES \'' . DB_CHARSET . '\'';
                            if (DB_COLLATE != '') {
                                $names .= ' COLLATE \'' . DB_COLLATE . '\'';
                            }
                            $_dba->Execute($names);
                        }
                        
                        ADOdb_Active_Record::SetDatabaseAdapter($_dba);
                    } else if (defined('DB_SERVER')) {
                        $v = View::getInstance();
                        $v->renderError(t('Unable to connect to database.'), t('A database error occurred while processing this request.'));
                    }
                } else {
                    return false;
                }
            }
            
            //$_dba->LogSQL(true);
            //global $ADODB_PERF_MIN;
            //$ADODB_PERF_MIN = 0;

            return $_dba;
        }
        
        /** 
         * Loads a helper file. If the same helper file is contained in both the core concrete directory and the site's directory, it will load the site's first, which could then extend the core.
         */
        public static function helper($file, $pkgHandle = false) {
        
            static $instances = array();

            $class = Object::camelcase($file) . "Helper";
            $siteclass = "Site" . Object::camelcase($file) . "Helper";

            if (array_key_exists($class, $instances)) {
                $instance = $instances[$class];
            } else if (array_key_exists($siteclass, $instances)) {
                $instance = $instances[$siteclass];
            } else {

                $env = Environment::get();
                $f1 = $env->getRecord(DIRNAME_HELPERS . '/' . $file . '.php', $pkgHandle);
                require_once($f1->file);
                if ($f1->override) {
                    if (class_exists($siteclass, false)) {
                        $class = $siteclass;
                    }
                } else if ($pkgHandle) {
                    $pkgclass = Object::camelcase($pkgHandle . '_' . $file) . "Helper";
                    if (class_exists($pkgclass, false)) {
                        $class = $pkgclass;
                    }
                }

                $instances[$class] = new $class();
                $instance = $instances[$class];
            }
            
            if(method_exists($instance,'reset')) {
                $instance->reset();
            }
            
            return $instance;
        }
        
        /**
         * @access private
         */
        public function package($pkgHandle) {
            // loads and instantiates the object
            $env = Environment::get();
            $path = $env->getPath(FILENAME_PACKAGE_CONTROLLER, $pkgHandle);

            if (file_exists($path)) {
                require_once($path);
                $isValidPkgHandle = true;
            }
            else {
                $msg = t('Warning - failed to load package with pkgHandle \'%1$s\'. Could not find package controller file: \'%2$s\'',
                    $pkgHandle, $path);
                Log::addEntry($msg, 'packages');
            }

            $class = Object::camelcase($pkgHandle) . "Package";
            if (class_exists($class)) {
                $cl = new $class;
                return $cl;
            }
            else {
                // $class might not exist due to an invalid $pkgHandle (thus a wrong 
                // $class value), in which case a more relevant message will already be logged.
                if ($isValidPkgHandle) {
                    $msg = t('Warning - failed to load package in directory \'%1$s\'. The package controller does not define the expected class: \'%2$s\'',
                        $pkgHandle, $class);
                    Log::addEntry($msg, 'packages');
                }
            }
        }
        
        /**
         * @access private
         */
        public function startingPointPackage($pkgHandle) {
            // loads and instantiates the object
            $dir = (is_dir(DIR_STARTING_POINT_PACKAGES . '/' . $pkgHandle)) ? DIR_STARTING_POINT_PACKAGES : DIR_STARTING_POINT_PACKAGES_CORE;
            if (file_exists($dir . '/' . $pkgHandle . '/' . FILENAME_PACKAGE_CONTROLLER)) {
                require_once($dir . '/' . $pkgHandle . '/' . FILENAME_PACKAGE_CONTROLLER);
                $class = Object::camelcase($pkgHandle) . "StartingPointPackage";
                if (class_exists($class)) {
                    $cl = new $class;
                    return $cl;
                }
            }
        }
        

        /** 
         * Gets the path to a particular page type controller
         */
        public function pageTypeControllerPath($ctHandle) {         
            self::model('collection_types');
            $ct = CollectionType::getByHandle($ctHandle);
            if (!is_object($ct)) {
                return false;
            }           
            $pkgHandle = $ct->getPackageHandle();
            $env = Environment::get();
            $path = $env->getPath(DIRNAME_CONTROLLERS . '/' . DIRNAME_PAGE_TYPES . '/' . $ctHandle . '.php', $pkgHandle);
            if (file_exists($path)) {
                return $path;
            }
        }
        
        /** 
         * Loads a controller for either a page or view
         */
        public static function controller($item) {
            
            $include = false;
            
            if (is_string($item)) {
                $db = self::db();
                if (is_object($db)) {
                    try {
                        $_item = Page::getByPath($item);
                        if ($_item->isError()) {
                            $path = $item;
                        } else {
                            $item = $_item;
                        }
                    } catch(Exception $e) {
                        $path = $item;
                    }
                } else {
                    $path = $item;
                }
            }
            
            if ($item instanceof Page) {
                $c = $item;
                if ($c->getCollectionTypeID() > 0) {                    
                    $ctHandle = $c->getCollectionTypeHandle();
                    $path = self::pageTypeControllerPath($ctHandle, $item->getPackageHandle());
                    if ($path != false) {
                        require_once($path);
                        $class = Object::camelcase($ctHandle) . 'PageTypeController';
                    }
                } else if ($c->isGeneratedCollection()) {
                    $file = $c->getCollectionFilename();
                    if ($file != '') {
                        // strip off PHP suffix for the $path variable, which needs it gone
                        if (strpos($file, '/' . FILENAME_COLLECTION_VIEW) !== false) {
                            $path = substr($file, 0, strpos($file, '/'. FILENAME_COLLECTION_VIEW));
                        } else {
                            $path = substr($file, 0, strpos($file, '.php'));
                        }
                    }
                }
            } else if ($item instanceof Block || $item instanceof BlockType) {
                
                $class = Object::camelcase($item->getBlockTypeHandle()) . 'BlockController';
                if ($item instanceof BlockType) {
                    $controller = new $class($item);
                }
                
                if ($item instanceof Block) {
                    $c = $item->getBlockCollectionObject();
                }               
            }
            
            $controllerFile = $path . '.php';

            if ($path != '') {
                
                $env = Environment::get();
                $pkgHandle = false;
                if (is_object($item)) {
                    $pkgHandle = $item->getPackageHandle();
                }
                
                $f1 = $env->getPath(DIRNAME_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER, $pkgHandle);
                $f2 = $env->getPath(DIRNAME_CONTROLLERS . $controllerFile, $pkgHandle);
                if (file_exists($f2)) {
                    $include = true;
                    require_once($f2);
                } else if (file_exists($f1)) {
                    $include = true;
                    require_once($f1);
                }
                
                if ($include) {
                    $class = Object::camelcase($path) . 'Controller';
                }
            }
            
            if (!isset($controller)) {
                if ($class && class_exists($class)) {
                    // now we get just the filename for this guy, so we can extrapolate
                    // what our controller is named
                    $controller = new $class($item);
                } else {
                    $controller = new Controller($item);
                }
            }
            
            if (isset($c) && is_object($c)) {
                $controller->setCollectionObject($c);
            }
            
            return $controller;
        }

    }