public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / lib / classes / module / class.cms_module_loader.php
100644 220 lines (200 sloc) 6.228 kb
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
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
/**
* Class to load modules.
*
* @author Ted Kulp
* @since 1.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license GPL
**/
class CmsModuleLoader extends CmsObject
{
  /**
  * Loads modules from the filesystem. If loadall is true, then it will load all
  * modules whether they're installed, or active. If it is false, then it will
  * only load modules which are installed and active.
  *
  * @param boolean Whether or not all modules should be loaded
  * @param boolean Whether or not we should load modules that are only admin panel related
  * @return void
  * @author Ted Kulp
  */
  public static function load_modules($loadall = false, $noadmin = false)
  {
    $gCms = cmsms();
    $cmsmodules = &$gCms->modules;
 
    $dir = cms_join_path(ROOT_DIR, 'modules');
    
    $loaded_modules = array();
 
    if ($loadall == true)
    {
      $handle=opendir($dir);
      while ($file = readdir($handle))
      {
        if (@is_file("$dir/$file/$file.module.php"))
        {
          include("$dir/$file/$file.module.php");
        }
        else
        {
          unset($cmsmodules[$file]);
        }
      }
      closedir($handle);
      
      //Find modules and instantiate them
      $allmodules = CmsModuleLoader::find_modules();
      foreach ($allmodules as $onemodule)
      {
        if (class_exists($onemodule))
        {
          $newmodule = new $onemodule;
          $name = $newmodule->get_name();
          $loaded_modules[] = $name;
          $cmsmodules[$name]['object'] = $newmodule;
          $cmsmodules[$name]['installed'] = false;
          $cmsmodules[$name]['active'] = false;
        }
        else
        {
          unset($cmsmodules[$name]);
        }
      }
    }
 
    #Figger out what modules are active and/or installed
    #Load them if loadall is false
    $query = '';
    if ($noadmin)
      $query = "SELECT module_name, active, version FROM ".cms_db_prefix()."modules WHERE admin_only = 0 ORDER BY module_name";
    else
      $query = "SELECT module_name, active, version FROM ".cms_db_prefix()."modules ORDER BY module_name";
 
    $result = cms_db()->Execute($query);
    while ($result && !$result->EOF)
    {
      $modulename = $result->fields['module_name'];
      if ($loadall == true)
      {
        if (array_key_exists($modulename, $cmsmodules))
        {
          $cmsmodules[$modulename]['installed'] = true;
          $cmsmodules[$modulename]['active'] = ($result->fields['active'] == 1?true:false);
        }
      }
      else
      {
        if ($result->fields['active'] == 1)
        {
          if (@is_file("$dir/$modulename/$modulename.module.php"))
          {
            #var_dump('loading module:' . $modulename);
            include_once("$dir/$modulename/$modulename.module.php");
            if (class_exists($modulename))
            {
              $newmodule = new $modulename;
              $name = $newmodule->get_name();
              $loaded_modules[] = $name;
 
              $dbversion = $result->fields['version'];
 
              #Check to see if there is an update and wether or not we should perform it
              if (version_compare($dbversion, $newmodule->get_version()) == -1 && $newmodule->allow_auto_upgrade())
              {
                $newmodule->Upgrade($dbversion, $newmodule->get_version());
                $query = "UPDATE ".cms_db_prefix()."modules SET version = ? WHERE module_name = ?";
                cms_db()->Execute($query, array($newmodule->get_version(), $name));
                CmsEvents::SendEvent('Core', 'ModuleUpgraded', array('name' => $name, 'oldversion' => $dbversion, 'newversion' => $newmodule->get_version()));
                $dbversion = $newmodule->get_version();
              }
 
              #Check to see if version in db matches file version
              if ($dbversion == $newmodule->get_version() && version_compare($newmodule->minimum_core_version(), CMS_VERSION) != 1)
              {
                $cmsmodules[$name]['object'] = $newmodule;
                $cmsmodules[$name]['installed'] = true;
                $cmsmodules[$name]['active'] = ($result->fields['active'] == 1?true:false);
              }
              else
              {
                unset($cmsmodules[$name]);
              }
            }
            else //No point in doing anything with it
            {
              unset($cmsmodules[$modulename]);
            }
          }
          else
          {
            unset($cmsmodules[$modulename]);
          }
        }
      }
      $result->MoveNext();
    }
    
    if ($result) $result->Close();
    
    CmsEventOperations::send_event('Core', 'AllModulesLoaded', array('loaded_modules' => $loaded_modules));
  }
  
  /**
   * @deprecated Deprecated. Use CmsModuleLoader::load_modules instead.
   **/
  public static function LoadModules($loadall = false, $noadmin = false)
  {
    return CmsModuleLoader::load_modules($loadall, $noadmin);
  }
 
  /**
   * Finds all classes extending cmsmodule for loading
   *
   * @return array The list of module classes loaded
   * @author Ted Kulp
   */
  public static function find_modules()
  {
    $result = array();
 
    foreach (get_declared_classes() as $oneclass)
    {
      $parent = get_parent_class($oneclass);
      while( $parent !== FALSE )
      {
        $str = strtolower($parent);
        if(starts_with($str, 'cmsmodule'))
        {
          $str = strtolower($oneclass);
          if(!starts_with($str, 'cmsmodule'))
          {
            if (!in_array($oneclass, $result))
            {
              $result[] = $oneclass;
              break;
            }
          }
        }
        $parent = get_parent_class($parent);
      }
    }
 
    sort($result);
 
    return $result;
  }
  
  /**
   * @deprecated Deprecated. Use CmsModuleLoader::find_modules instead.
   **/
  public static function FindModules()
  {
    return CmsModuleLoader::find_modules();
  }
}
 
?>