function Dwoo_Plugin_makeThumb(Dwoo $dwoo, $file = '', $prefix = '', $height = 300, $width = 200, $method = 'fit', $overwrite = false) { if ($file == '') { return false; } // check if the file contains the Path to the image $file = str_replace(CAT_URL, CAT_PATH, $file); $file = strpos($file, CAT_PATH) === false ? CAT_PATH . $file : $file; // Set some values $temp_path = CAT_PATH . '/temp/' . MEDIA_DIRECTORY . '/'; $temp_url = CAT_URL . '/temp/' . MEDIA_DIRECTORY . '/'; $info = pathinfo($file); $new_path = CAT_Helper_Directory::sanitizePath($temp_path . $prefix . $info['filename'] . '_' . $width . '_' . $height . '.' . $info['extension']); $new_url = str_replace(CAT_PATH, CAT_URL, $new_path); // Create temp directory, if the folder doesn't exist if (!file_exists($temp_path)) { CAT_Helper_Directory::createDirectory($temp_path, NULL, true); } // Create the file, if the file does not exist or overwrite is set to true if (!file_exists($new_path) || $overwrite == true) { CAT_Helper_Image::getInstance()->make_thumb($file, $new_path, $height, $width, $method); } return $new_url; // end make_thumb() }
/** * Create directories recursive * * @param string $dir_name - directory to create * @param ocatal $dir_mode - access mode * @return boolean result of operation * * The function was moved to Directory helper class * */ function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE) { if (!class_exists('CAT_Helper_Directory')) { @(require_once dirname(__FILE__) . '/CAT/Helper/Directory.php'); } $addons_helper = new CAT_Helper_Directory(); return $addons_helper->createDirectory($dir_name, $dir_mode); }
/** * copy directory structure with files * * @access public * @param string $dirsource * @param string $dirdest **/ public static function copyRecursive($dirsource, $dirdest) { if (is_dir($dirsource)) { $dir_handle = dir($dirsource); } else { return false; } if (!is_object($dir_handle)) { return false; } while ($file = $dir_handle->read()) { if ($file != "." && $file != "..") { if (!is_dir($dirsource . "/" . $file)) { copy($dirsource . "/" . $file, $dirdest . '/' . $file); if ($file != '.svn' && $file != '.git') { CAT_Helper_Directory::setPerms($dirdest . "/" . $file); } } else { CAT_Helper_Directory::createDirectory($dirdest . '/' . $file); self::copyRecursive($dirsource . "/" . $file, $dirdest . '/' . $file); } } } $dir_handle->close(); return true; }
/** * This function is used to install a module (addon); requires an * already existing ZIP file. Use installUploaded() to handle uploads. * * @access public * @param **/ public static function installModule($zipfile, $silent = false, $remove_zip_on_error = false) { // keep old modules happy global $wb, $admin, $database, $backend; if (!is_object($admin) && is_object($backend)) { $admin =& $backend; } // keep old modules happy $self = self::getInstance(); $self->log()->LogDebug('installModule'); $extension = pathinfo($zipfile, PATHINFO_EXTENSION); $sourcedir = pathinfo($zipfile, PATHINFO_DIRNAME); // Set temp vars $temp_dir = CAT_PATH . '/temp/'; $temp_unzip = $temp_dir . '/unzip_' . pathinfo($zipfile, PATHINFO_FILENAME) . '/'; $self->log()->LogDebug(sprintf('file extension [%s], source dir [%s], remove zip [%s]', $extension, $sourcedir, $remove_zip_on_error)); $self->log()->LogDebug(sprintf('temp dir [%s], unzip dir [%s]', $temp_dir, $temp_unzip)); // Check for language or template/module if ($extension == 'php') { $temp_unzip = $zipfile; } elseif ($extension == 'zip') { $self->log()->LogDebug(sprintf('creating temp. unzip dir [%s]', $temp_unzip)); CAT_Helper_Directory::createDirectory($temp_unzip); $self->log()->LogDebug(sprintf('zip file [%s], output dir [%s]', $zipfile, $temp_unzip)); // Setup the PclZip object and unzip the files to the temp unzip folder $list = CAT_Helper_Zip::getInstance($zipfile)->config('Path', CAT_Helper_Directory::sanitizePath($temp_unzip))->extract(); // check if anything was extracted if (!$list) { $self->log()->LogDebug(sprintf('No $list from ZIP-Helper, removing [%s]', $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if ($remove_zip_on_error) { CAT_Helper_Directory::removeDirectory($zipfile); } if (!$silent) { self::printError('Unable to extract the file. Please check the ZIP format.'); } return false; } // check for info.php if (!file_exists($temp_unzip . '/info.php')) { // check subfolders for info.php $info = CAT_Helper_Directory::getInstance(1)->maxRecursionDepth(4)->findFile('info.php', $temp_unzip); if (!$info) { $self->log()->LogDebug(sprintf('No info.php found, removing [%s]', $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if ($remove_zip_on_error) { CAT_Helper_Directory::removeDirectory($zipfile); } if (!$silent) { self::printError('Invalid installation file. No info.php found. Please check the ZIP format.'); } return false; } else { $temp_infofile = pathinfo($info, PATHINFO_DIRNAME); $self->log()->LogDebug(sprintf('set $temp_infofile to [%s]', $temp_infofile)); } } else { $temp_infofile = $temp_unzip; } } else { $self->log()->LogDebug(sprintf('Unknown extension [%s], "php" or "zip" expected, removing [%s]', $extension, $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if ($remove_zip_on_error) { CAT_Helper_Directory::removeDirectory($zipfile); } if (!$silent) { self::printError('Invalid installation file. Wrong extension. Please check the ZIP format.'); } return false; } // Check the info.php file / language file $precheck_errors = NULL; if ($addon_info = self::checkInfo($temp_infofile)) { $precheck_errors = self::preCheckAddon($zipfile, $temp_infofile, false); } else { $self->log()->LogDebug(sprintf('Unable to load info file [%s], removing [%s]', $temp_infofile, $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if ($remove_zip_on_error) { CAT_Helper_Directory::removeDirectory($zipfile); } if (!$silent) { self::printError($self->lang()->translate('Invalid installation file. {{error}}', array('error' => $self->lang()->translate('Unable to find info.php')))); } return false; } // precheck failed if ($precheck_errors != '' && !is_bool($precheck_errors)) { $self->log()->LogDebug(sprintf('Pre-installation check(s) failed, removing [%s]', $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if (!$silent) { self::printError($precheck_errors, $_SERVER['SCRIPT_NAME'], false); } return false; } // So, now we have done all preinstall checks, lets see what to do next $addon_directory = $addon_info['addon_function'] == 'language' ? $addon_info['module_code'] . '.php' : $addon_info['module_directory']; // Set module directory $addon_dir = CAT_PATH . '/' . $addon_info['addon_function'] . 's/' . $addon_directory; $action = 'install'; if (file_exists($addon_dir) && $addon_info['addon_function'] != 'language') { $action = 'upgrade'; // look for old info.php $previous_info = self::checkInfo($addon_dir); if ($previous_info) { // compare versions if (self::versionCompare($previous_info['module_version'], $addon_info['module_version'], '>=')) { $self->log()->LogDebug(sprintf('Version check found no difference between installed and uploaded version, removing [%s]', $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if ($remove_zip_on_error) { CAT_Helper_Directory::removeDirectory($zipfile); } if (!$silent) { self::printError('Already installed'); } else { self::$error = 'already installed'; } return false; } } } // Make sure the module dir exists, and chmod if needed if ($addon_info['addon_function'] != 'language') { $self->log()->LogDebug(sprintf('Creating addon directory [%s]', $addon_dir)); CAT_Helper_Directory::createDirectory($addon_dir); // copy files from temp folder // we use $temp_infofile here as source as it is the folder the // info.php file resides if (CAT_Helper_Directory::copyRecursive($temp_infofile, $addon_dir) !== true) { $self->log()->LogDebug(sprintf('Copy failed, removing [%s]', $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if ($remove_zip_on_error) { CAT_Helper_Directory::removeDirectory($zipfile); } if (!$silent) { self::printError('Unable to install - error copying files'); } return false; } // remove temp $self->log()->LogDebug(sprintf('removing [%s]', $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); if ($remove_zip_on_error) { CAT_Helper_Directory::removeDirectory($zipfile); } } // load the module info into the database if (!self::loadModuleIntoDB($addon_dir, $action, self::checkInfo($addon_dir))) { $self->log()->LogDebug(sprintf('Loading module into DB failed, removing [%s]', $temp_unzip)); CAT_Helper_Directory::removeDirectory($temp_unzip); CAT_Helper_Directory::removeDirectory($addon_dir); if (!$silent) { self::printError($self->db()->getError()); } return false; } // Run the modules install // upgrade script if there is one if (file_exists($addon_dir . '/' . $action . '.php')) { $self->log()->LogDebug(sprintf('Running [%s]', $addon_dir . '/' . $action . '.php')); require $addon_dir . '/' . $action . '.php'; } if ($action == 'install' && $addon_info['addon_function'] == 'language') { $target = CAT_Helper_Directory::sanitizePath($addon_dir); // for manual install... if ($zipfile !== $target) { rename($zipfile, $addon_directory); CAT_Helper_Directory::setPerms($addon_directory); } } // set module permissions if ($addon_info['addon_function'] == 'module' && ($addon_info['module_function'] == 'page' || $addon_info['module_function'] == 'tool') || $addon_info['addon_function'] == 'template') { self::setModulePermissions($addon_info); } return true; }
// ================================ if (is_writable(CAT_PATH . $ajax['folder_path'])) { $folder_name = $val->sanitizePost('name'); if (!$folder_name) { $folder_name = $backend->lang()->translate('New folder'); } $create_folder = CAT_PATH . $ajax['folder_path'] . '/' . $folder_name; $counter = 1; while (is_dir($create_folder)) { $create_folder = CAT_PATH . $ajax['folder_path'] . '/' . $folder_name . ' ' . $counter; $counter++; } // ===================================================== // ! Try to create new folder; also creates an index.php // ===================================================== if (CAT_Helper_Directory::createDirectory($create_folder, NULL, true)) { CAT_Helper_Directory::setPerms($create_folder); if (is_writable($create_folder)) { $ajax['message'] = $backend->lang()->translate('Folder created successfully'); $ajax['created'] = true; } else { $ajax['message'] = $backend->lang()->translate('Unable to write to the target directory'); $ajax['created'] = false; } } else { $ajax['message'] = $backend->lang()->translate('Unable to write to the target directory'); $ajax['created'] = false; } } else { $ajax['message'] = $backend->lang()->translate('Unable to write to the target directory'); $ajax['created'] = false;
array_push($files, 'add', 'view', 'modify'); } foreach ($files as $n) { $fh = fopen($full . '/' . $n . '.php', 'w'); if ($fh) { writeHeader($fh, $name, $author, $type); fclose($fh); } } } // if it's a template... if ($type == 'template') { $contents = file_get_contents($full . '/index.php'); $contents .= "\n\$dwoodata\t= array(); // if you need to set some additional template vars, add them here\nglobal \$page_id;\n\$variant = CAT_Helper_Page::getPageSettings(\$page_id,'internal','template_variant');\nif ( \$variant == '' ) \$variant = DEFAULT_TEMPLATE_VARIANT;\nif ( \$variant == '' || !file_exists(CAT_PATH.'/templates/bootstrap/templates/'.\$variant.'/index.tpl' ) )\n \$variant = 'default';\n\$parser->setPath(CAT_TEMPLATE_DIR.'/templates/'.\$variant);\n\$parser->setFallbackPath(CAT_TEMPLATE_DIR.'/templates/default');\n\$parser->output('index.tpl',\$dwoodata);\n"; file_put_contents($full . '/index.php', $contents); CAT_Helper_Directory::createDirectory($full . '/templates/default'); CAT_Helper_Directory::recursiveCreateIndex($full . '/templates'); } // insert module into DB foreach ($info as $key => $value) { $key = str_replace($pre, 'module_', $key); $info[$key] = $value; } $info['addon_function'] = $type; CAT_Helper_Addons::loadModuleIntoDB($dir, 'install', $info); $success = true; $message = $backend->lang()->translate('Module created successfully!'); printResult(); function printResult() { global $message, $success;
/** * Install a Droplet from a ZIP file (the ZIP may contain more than one * Droplet) * * @access public * @param string $temp_file - name of the ZIP file * @return array see droplets_import() method * **/ public static function installDroplet($temp_file) { $self = self::getInstance(); $temp_unzip = CAT_PATH . '/temp/droplets_unzip/'; CAT_Helper_Directory::createDirectory($temp_unzip); $errors = array(); $imports = array(); $count = 0; // extract file $list = CAT_Helper_Zip::getInstance($temp_file)->config('Path', $temp_unzip)->extract(); // get .php files $files = CAT_Helper_Directory::getPHPFiles($temp_unzip, $temp_unzip . '/'); // now, open all *.php files and search for the header; // an exported droplet starts with "//:" foreach ($files as $file) { if (pathinfo($file, PATHINFO_FILENAME) !== 'index' && pathinfo($file, PATHINFO_EXTENSION) == 'php') { $description = NULL; $usage = NULL; $code = NULL; // Name of the Droplet = Filename $name = pathinfo($file, PATHINFO_FILENAME); // Slurp file contents $lines = file($temp_unzip . '/' . $file); // First line: Description if (preg_match('#^//\\:(.*)$#', $lines[0], $match)) { $description = addslashes($match[1]); array_shift($lines); } // Second line: Usage instructions if (preg_match('#^//\\:(.*)$#', $lines[0], $match)) { $usage = addslashes($match[1]); array_shift($lines); } // there may be more comment lines; they will be added to the usage instructions while (preg_match('#^//(.*)$#', $lines[0], $match)) { $usage .= addslashes(trim($match[1])); array_shift($lines); } if (!$description && !$usage) { // invalid file $errors[$file] = CAT_Helper_Directory::getInstance()->lang()->translate('No valid Droplet file (missing description and/or usage instructions)'); continue; } // Remaining: Droplet code $code = implode('', $lines); // replace 'evil' chars in code $tags = array('<?php', '?>', '<?'); //$code = addslashes(str_replace($tags, '', $code)); $code = str_replace($tags, '', $code); // Already in the DB? $stmt = 'INSERT'; $id = NULL; $found = $self->db()->query("SELECT * FROM `:prefix:mod_droplets` WHERE name=:name", array('name' => $name)); if ($found->rowCount()) { $stmt = 'REPLACE'; $id = $found->fetchColumn(); } // execute $q = "{$stmt} INTO `:prefix:mod_droplets` SET " . ($id ? 'id=' . $id . ', ' : '') . '`name`=:name, `code`=:code, `description`=:desc, ' . '`modified_when`=:when, `modified_by`=:userid, ' . '`active`=:active, `comments`=:usage'; $params = array('name' => $name, 'code' => $code, 'desc' => $description, 'when' => time(), 'userid' => CAT_Users::get_user_id(), 'active' => 1, 'usage' => $usage); $result = $self->db()->query($q, $params); if (!$self->db()->isError()) { $count++; $imports[$name] = 1; } else { $errors[$name] = $self->db()->getError(); } } // check for data directory if (file_exists($temp_unzip . '/data')) { // copy all files CAT_Helper_Directory::copyRecursive($temp_unzip . '/data', dirname(__FILE__) . '/data/'); } } // cleanup; ignore errors here CAT_Helper_Directory::removeDirectory($temp_unzip); return array('count' => $count, 'errors' => $errors, 'imported' => $imports); }
/** * * **/ public static function createAccessFile($filename, $page_id) { $filename = CAT_Helper_Directory::sanitizePath($filename); // check if $filename is a full path (may be 'link' db value) if (!preg_match('~^' . CAT_Helper_Directory::sanitizePath(CAT_PATH . PAGES_DIRECTORY) . '~i', $filename)) { $filename = CAT_Helper_Directory::sanitizePath(CAT_PATH . PAGES_DIRECTORY . '/' . dirname($filename) . '/' . self::getFilename(basename($filename)) . PAGE_EXTENSION); } $pages_path = CAT_Helper_Directory::sanitizePath(CAT_PATH . PAGES_DIRECTORY); $rel_pages_dir = str_replace($pages_path, '', CAT_Helper_Directory::sanitizePath(dirname($filename))); $rel_filename = str_replace($pages_path, '', CAT_Helper_Directory::sanitizePath($filename)); // prevent system directories and files from being overwritten (level 0) $denied = false; if (PAGES_DIRECTORY == '') { $forbidden_dirs = self::$instance->_config['forbidden_l0']; $forbidden_files = self::$instance->_config['forbidden_filenames_l0']; $search = explode('/', $rel_filename); $denied = in_array($search[1], $forbidden_dirs); $denied = in_array($search[1], $forbidden_files); } if (true === is_writable($pages_path) && false == $denied) { // First make sure parent folder exists $parent_folders = explode('/', $rel_pages_dir); $parents = ''; foreach ($parent_folders as $parent_folder) { if ($parent_folder != '/' && $parent_folder != '') { $parents .= '/' . $parent_folder; if (!file_exists($pages_path . $parents)) { // create dir; also creates index.php (last param = true) CAT_Helper_Directory::createDirectory($pages_path . $parents, OCTAL_DIR_MODE, true); CAT_Helper_Directory::setPerms($pages_path . $parents); } } } $step_back = str_repeat('../', substr_count($rel_pages_dir, '/') + (PAGES_DIRECTORY == "" ? 0 : 1)); $content = '<?php' . "\n"; $content .= "/**\n *\tThis file is autogenerated by BlackCat CMS Version " . CAT_VERSION . "\n"; $content .= " *\tDo not modify this file!\n */\n"; $content .= "\t" . '$page_id = ' . $page_id . ';' . "\n"; $content .= "\t" . 'require_once \'' . $step_back . 'index.php\';' . "\n"; $content .= '?>'; /** * write the file */ $fp = fopen($filename, 'w'); if ($fp) { fwrite($fp, $content, strlen($content)); fclose($fp); /** * Chmod the file */ CAT_Helper_Directory::getInstance()->setPerms($filename); } else { CAT_Backend::getInstance()->print_error('Error creating access file in the pages directory, cannot open file'); return false; } return true; } else { CAT_Backend::getInstance()->print_error('Error creating access file in the pages directory, path not writable or forbidden file / directory name'); return false; } }
/** * Accessor to KLogger class; this makes using the class significant faster! * * @access public * @return object * **/ public function log() { // 8 = OFF if ($this->debugLevel < 8) { if (!is_object($this->logObj)) { if (!CAT_Registry::exists('CAT_PATH', false)) { CAT_Registry::define('CAT_PATH', dirname(__FILE__) . '/../..', 1); } $debug_dir = CAT_PATH . '/temp/logs' . ($this->debugLevel == 7 ? '/debug_' . get_class($this) : ''); if (get_class($this) != 'CAT_Helper_Directory') { $debug_dir = CAT_Helper_Directory::sanitizePath($debug_dir); } if (!file_exists($debug_dir)) { if (get_class($this) != 'CAT_Helper_Directory') { CAT_Helper_Directory::createDirectory($debug_dir, 0777); } else { mkdir($debug_dir, 0777); } } $this->logObj = CAT_Helper_KLogger::instance($debug_dir, $this->debugLevel); } return $this->logObj; } return $this; }