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
<?php
/*-----8<--------------------------------------------------------------------
*
* BEdita - a semantic content management framework
*
* Copyright 2008-2012 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with BEdita (see LICENSE.LGPL).
* If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
*
*------------------------------------------------------------------->8-----
*/
/**
* Handle BEdita addons (models, components, etc,..) to enable/disable it and so on
*
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $LastChangedDate$
*
* $Id$
*/
class Addon extends AppModel {
public $useTable = false;
/**
* return an array of available addons
*
* @return array in this form
* array(
* "models" => array(
* "objectTypes" => array(
* "on" => array(
* 0 => array(
* "name" => class name,
* "file" => file name,
* "objectType" => object type name,
* "path" => path where to find addon file available,
* "enabledPath" => path where to find addon file enabled,
* "type" => "models",
* "fileNameUsed" => true if file name is already used for model
* ),
* 1 => array()
* ....
* ),
* "off" => array(
* 0 => array(
* like "on" array
* ), 1 => array(...)), ...
* ),
* "others" => array(
* "on" => array(
* 0 => array(
* "name" => class name,
* "file" => file name,
* "objectType" => object type name,
* "path" => path where to find addon file available,
* "enabledPath" => path where to find addon file enabled,
* "type" => "models",
* "fileNameUsed" => true if file name is already used for model
* ),
* 1 => array()
* ....
* ),
* "off" => array(
* 0 => array(
* like "on" array
* ), 1 => array(...)), ...
* )
* ),
*
* "components" => array(like "others" array),
* "helpers" => array(like "others" array),
* "behaviors" => array(like "others" array),
* )
*/
public function getAddons() {
$conf = Configure::getInstance();
$addons = array();
$folder = new Folder();
$items = array("models", "components", "helpers", "models" . DS . "behaviors");
$Belib = BElib::getInstance();
foreach ($items as $val) {
if ($folder->cd(BEDITA_ADDONS_PATH . DS . $val)) {
$ls = $folder->read(true, true);
foreach ($ls[1] as $addonFileName) {
$addonFile = new File(BEDITA_ADDONS_PATH . DS . $val . DS . $addonFileName);
if ($addonFile->ext() == "php") {
$name = $addonFile->name();
$addonName = Inflector::camelize($name);
$type = (strstr($val, "behaviors"))? "behaviors" : $val;
$addonItem = array(
"name" => $addonName,
"file" => $addonFileName,
"path" => BEDITA_ADDONS_PATH . DS . $val,
"enabledPath" => BEDITA_ADDONS_PATH . DS . $val . DS . "enabled",
"type" => $type,
"update" => false
);
$alreadyUsed = $Belib->isFileNameUsed($addonFileName, $type, array(BEDITA_ADDONS_PATH . DS . $val . DS . "enabled" . DS));
$addonItem["fileNameUsed"] = $alreadyUsed;
// check if addon file enabled has to be updated
$addonFileEnabled = new File(BEDITA_ADDONS_PATH . DS . $val . DS . "enabled" . DS . $addonFileName);
$isEnabled = $addonFileEnabled->exists();
if ($isEnabled) {
if ($addonFile->lastChange() > $addonFileEnabled->lastChange() && $addonFile->md5() != $addonFileEnabled->md5()) {
$addonItem["update"] = true;
}
}
if ($type == "models") {
if ($Belib->isBeditaObjectType($addonName, BEDITA_ADDONS_PATH . DS . $val)) {
$addonItem["objectType"] = $name;
if (!empty($conf->objectTypes[$name]) && !$alreadyUsed && $isEnabled) {
$addons[$type]["objectTypes"]["on"][] = $addonItem;
} else {
$addons[$type]["objectTypes"]["off"][] = $addonItem;
}
} else {
if ($isEnabled) {
$addons[$type]["others"]["on"][] = $addonItem;
} else {
$addons[$type]["others"]["off"][] = $addonItem;
}
}
} else {
if ($isEnabled) {
$addons[$type]["on"][] = $addonItem;
} else {
$addons[$type]["off"][] = $addonItem;
}
}
}
}
}
}
return $addons;
}
/**
* enable addon copying the addon file in the related enabled folder.
* If addon is a BEdita object type a row on object_types table is created
*
* @param string $fileName, addon file name
* @param string $addonType, the type of addon, i.e. models, helpers, components, ...
* @throws BeditaException
*/
public function enable($fileName, $addonType) {
if (empty($fileName) || empty($addonType)) {
throw new BeditaException(__("Missing mandatory data"), "file name and/or addon type");
}
$filePath = $this->getFolderByType($addonType);
if (empty($filePath)) {
throw new BeditaException($addonType . " " . __("addon folder doesn't found", true));
}
$filePath .= DS . $fileName;
$addonFile = new File($filePath);
if (!$addonFile->exists()) {
throw new BeditaException($filePath . " " . __("doesn't found", true));
}
$addonFolder = $addonFile->Folder()->path;
$addonClassName = Inflector::camelize($addonFile->name());
$BeLib = BeLib::getInstance();
// BEdita object type
if ($BeLib->isBeditaObjectType($addonClassName, $addonFolder)) {
$model = $BeLib->getObject($addonClassName, $addonFolder);
$data["name"] = $addonFile->name();
if (!empty($model->module)) {
$data["module_name"] = $model->module;
}
$ObjectType = ClassRegistry::init("ObjectType");
$data["id"] = $ObjectType->newPluggedId();
if (!$ObjectType->save($data)) {
throw new BeditaException(__("Error saving object type", true));
}
}
$enabledPath = $this->getEnabledFolderByType($addonType) . DS . $addonFile->name;
if (!$addonFile->copy($enabledPath)) {
if (!empty($data["id"])) {
$ObjectType->delete($data["id"]);
}
throw new BeditaException(__("Error copying addon to enabled folder", true), array("file path" => $filePath, "enabled path" => $enabledPath));
}
BeLib::getObject("BeConfigure")->cacheConfig();
}
/**
* disable addon deleting the addon file from the related enabled folder.
* If addon is a BEdita object the row on object_types table and all objects of that type are removed
*
* @param string $fileName, addon file name
* @param string $addonType, the type of addon, i.e. models, helpers, components, ...
* @throws BeditaException
*/
public function disable($fileName, $addonType) {
if (empty($fileName) || empty($addonType)) {
throw new BeditaException(__("Missing mandatory data"), "file name and/or addon type");
}
$enabledPath = $this->getEnabledFolderByType($addonType) . DS . $fileName;
$addonFile = new File($enabledPath);
$addonClassName = Inflector::camelize($addonFile->name());
$BeLib = BeLib::getInstance();
// BEdita object type
if ($BeLib->isBeditaObjectType($addonClassName, $addonFile->Folder()->path)) {
$ObjectType = ClassRegistry::init("ObjectType");
$ObjectType->purgeType($addonFile->name());
}
// delete addon file to respective enabled folder
if (!$addonFile->delete()) {
throw new BeditaException(__("Error deleting addon to enabled folder", true), array("file path" => $filePath, "enabled path" => $enabledPath));
}
BeLib::getObject("BeConfigure")->cacheConfig();
}
/**
* update addon overriding the enabled addon file
*
* @param string $fileName, addon file name
* @param string $addonType, the type of addon, i.e. models, helpers, components, ...
* @throws BeditaException
*/
public function update($fileName, $addonType) {
if (empty($fileName) || empty($addonType)) {
throw new BeditaException(__("Missing mandatory data"), "file name and/or addon type");
}
$filePath = $this->getFolderByType($addonType);
if (empty($filePath)) {
throw new BeditaException($addonType . " " . __("addon folder doesn't found", true));
}
$filePath .= DS . $fileName;
$addonFile = new File($filePath);
if (!$addonFile->exists()) {
throw new BeditaException($filePath . " " . __("doesn't found", true));
}
$enabledPath = $this->getEnabledFolderByType($addonType) . DS . $addonFile->name;
if (!$addonFile->copy($enabledPath)) {
throw new BeditaException(__("Error copying addon to enabled folder", true), array("file path" => $filePath, "enabled path" => $enabledPath));
}
}
/**
* get an array with folders on which $fileName is found starting from BEDITA_ADDONS_PATH
*
* @param string $fileName, the file name
* @return array
*/
public function getFolderByFile($fileName) {
$Folder = new Folder(BEDITA_ADDONS_PATH);
return $Folder->findRecursive($fileName);
}
/**
* get the folder relative to an addon
*
* @param string $addonType, the addon type i.e. model, helper, component,...
* @return mixed, the folder string or false if folder doesn't found
*/
public function getFolderByType($addonType) {
$addonType = Inflector::pluralize($addonType);
switch ($addonType) {
case 'behaviors':
$addonType = 'models' . DS . 'behaviors';
break;
}
$path = BEDITA_ADDONS_PATH . DS . $addonType;
if (!BeLib::getObject("BeSystem")->checkAppDirPresence($path)) {
$path = false;
}
return $path;
}
/**
* get the "enabled" folder relative to an addon
*
* @param string $addonType, the addon type i.e. model, helper, component,...
* @return mixed, the folder string or false if folder doesn't found
*/
public function getEnabledFolderByType($addonType) {
$addonPath = $this->getFolderByType($addonType);
if (!$addonPath) {
return false;
}
$enabledPath = $addonPath . DS . "enabled";
if (!BeLib::getObject("BeSystem")->checkAppDirPresence($enabledPath)) {
$enabledPath = false;
}
return $enabledPath;
}
}
?>