protected function getConnection() { if ((extension_loaded('memcached') && class_exists('Memcached')) != true) { return false; } $config = MFactory::getConfig(); $this->_persistent = $config->get('memcache_persist', true); $this->_compress = $config->get('memcache_compress', false) == false ? 0 : Memcached::OPT_COMPRESSION; $server = array(); $server['host'] = $config->get('memcache_server_host', 'localhost'); $server['port'] = $config->get('memcache_server_port', 11211); if ($this->_persistent) { $session = MFactory::getSession(); self::$_db = new Memcached($session->getId()); } else { self::$_db = new Memcached(); } $memcachedtest = self::$_db->addServer($server['host'], $server['port']); if ($memcachedtest == false) { return MError::raiseError(404, "Could not connect to memcached server"); } self::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress); if (self::$_db->get($this->_hash . '-index') === false) { $empty = array(); self::$_db->set($this->_hash . '-index', $empty, 0); } return; }
public static function getInstance($type = 'output', $options = array()) { MCacheController::addIncludePath(MPATH_WP_CNT . '/miwi/framework/cache/controller'); $type = strtolower(preg_replace('/[^A-Z0-9_\\.-]/i', '', $type)); $class = 'MCacheController' . ucfirst($type); if (!class_exists($class)) { mimport('framework.filesystem.path'); if ($path = MPath::find(MCacheController::addIncludePath(), strtolower($type) . '.php')) { include_once $path; } else { MError::raiseError(500, 'Unable to load Cache Controller: ' . $type); } } return new $class($options); }
protected function getConnection() { if ((extension_loaded('memcache') && class_exists('Memcache')) != true) { return false; } $config = MFactory::getConfig(); $this->_persistent = $config->get('memcache_persist', true); $this->_compress = $config->get('memcache_compress', false) == false ? 0 : MEMCACHE_COMPRESSED; $server = array(); $server['host'] = $config->get('memcache_server_host', 'localhost'); $server['port'] = $config->get('memcache_server_port', 11211); // Create the memcache connection self::$_db = new Memcache(); self::$_db->addServer($server['host'], $server['port'], $this->_persistent); $memcachetest = @self::$_db->connect($server['host'], $server['port']); if ($memcachetest == false) { return MError::raiseError(404, "Could not connect to memcache server"); } if (self::$_db->get($this->_hash . '-index') === false) { $empty = array(); self::$_db->set($this->_hash . '-index', $empty, $this->_compress, 0); } return; }
public function execute() { if (!is_resource($this->connection)) { if (MError::$legacy) { if ($this->debug) { MError::raiseError(500, 'MDatabaseMySQL::query: ' . $this->errorNum . ' - ' . $this->errorMsg); } return false; } else { MLog::add(MText::sprintf('MLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), MLog::ERROR, 'database'); throw new MDatabaseException($this->errorMsg, $this->errorNum); } } // Take a local copy so that we don't modify the original query and cause issues later $sql = $this->replacePrefix((string) $this->sql); if ($this->limit > 0 || $this->offset > 0) { $sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit; } // If debugging is enabled then let's log the query. if ($this->debug) { // Increment the query counter and add the query to the object queue. $this->count++; $this->log[] = $sql; MLog::add($sql, MLog::DEBUG, 'databasequery'); } // Reset the error values. $this->errorNum = 0; $this->errorMsg = ''; #for-multi-db global $wpdb; $multidb_file = MPATH_WP_CNT . '/db.php'; if (!empty($wpdb) and file_exists($multidb_file)) { $this->connection = $wpdb->db_connect($sql); #for-multi-db $sql = $wpdb->sanitize_multidb_query_tables($sql); } // Execute the query. $this->cursor = mysql_query($sql, $this->connection); // If an error occurred handle it. if (!$this->cursor) { $this->errorNum = (int) mysql_errno($this->connection); $this->errorMsg = (string) mysql_error($this->connection) . ' SQL=' . $sql; if (MError::$legacy) { if ($this->debug) { MError::raiseError(500, 'MDatabaseMySQL::query: ' . $this->errorNum . ' - ' . $this->errorMsg); } return false; } else { MLog::add(MText::sprintf('MLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), MLog::ERROR, 'databasequery'); throw new MDatabaseException($this->errorMsg, $this->errorNum); } } return $this->cursor; }
public function loadTemplate($tpl = null) { // Clear prior output $this->_output = null; $template = MFactory::getApplication()->getTemplate(); $layout = $this->getLayout(); $layoutTemplate = $this->getLayoutTemplate(); // Create the template file name based on the layout $file = isset($tpl) ? $layout . '_' . $tpl : $layout; // Clean the file name $file = preg_replace('/[^A-Z0-9_\\.-]/i', '', $file); $tpl = isset($tpl) ? preg_replace('/[^A-Z0-9_\\.-]/i', '', $tpl) : $tpl; // Load the language file for the template $lang = MFactory::getLanguage(); $lang->load('tpl_' . $template, MPATH_BASE, null, false, true) || $lang->load('tpl_' . $template, MPATH_THEMES . "/{$template}", null, false, true); // Change the template folder if alternative layout is in different template if (isset($layoutTemplate) && $layoutTemplate != '_' && $layoutTemplate != $template) { $this->_path['template'] = str_replace($template, $layoutTemplate, $this->_path['template']); } // Load the template script mimport('framework.filesystem.path'); $filetofind = $this->_createFileName('template', array('name' => $file)); $this->_template = MPath::find($this->_path['template'], $filetofind); // If alternate layout can't be found, fall back to default layout if ($this->_template == false) { $filetofind = $this->_createFileName('', array('name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl))); $this->_template = MPath::find($this->_path['template'], $filetofind); } if ($this->_template != false) { // Unset so as not to introduce into template scope unset($tpl); unset($file); // Never allow a 'this' property if (isset($this->this)) { unset($this->this); } // Start capturing output into a buffer ob_start(); // Include the requested template filename in the local scope // (this will execute the view logic). include $this->_template; // Done with the requested template; get the buffer and // clear it. $this->_output = ob_get_contents(); ob_end_clean(); return $this->_output; } else { return MError::raiseError(500, MText::sprintf('MLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file)); } }
public function getName() { $name = $this->_name; if (empty($name)) { $r = null; if (!preg_match('/M(.*)/i', get_class($this), $r)) { MError::raiseError(500, MText::_('MLIB_APPLICATION_ERROR_APPLICATION_GET_NAME')); } $name = strtolower($r[1]); } return $name; }
public function getTable($name = '', $prefix = 'Table', $options = array()) { if (empty($name)) { $name = $this->getName(); } if ($table = $this->_createTable($name, $prefix, $options)) { return $table; } MError::raiseError(0, MText::sprintf('MLIB_APPLICATION_ERROR_TABLE_NAME_NOT_SUPPORTED', $name)); return null; }
public static function getAdapter($type) { static $adapters; if (!isset($adapters)) { $adapters = array(); } if (!isset($adapters[$type])) { // Try to load the adapter object $class = 'MArchive' . ucfirst($type); if (!class_exists($class)) { $path = dirname(__FILE__) . '/archive/' . strtolower($type) . '.php'; if (file_exists($path)) { require_once $path; } else { MError::raiseError(500, MText::_('MLIB_FILESYSTEM_UNABLE_TO_LOAD_ARCHIVE')); } } $adapters[$type] = new $class(); } return $adapters[$type]; }
public static function getInstance($options = array()) { // Sanitize the database connector options. $options['driver'] = isset($options['driver']) ? preg_replace('/[^A-Z0-9_\\.-]/i', '', $options['driver']) : 'mysql'; $options['database'] = isset($options['database']) ? $options['database'] : null; $options['select'] = isset($options['select']) ? $options['select'] : true; // Get the options signature for the database connector. $signature = md5(serialize($options)); // If we already have a database connector instance for these options then just use that. if (empty(self::$instances[$signature])) { // Derive the class name from the driver. $class = 'MDatabase' . ucfirst($options['driver']); // If the class doesn't exist, let's look for it and register it. if (!class_exists($class)) { // Derive the file path for the driver class. $path = dirname(__FILE__) . '/database/' . $options['driver'] . '.php'; // If the file exists register the class with our class loader. if (file_exists($path)) { MLoader::register($class, $path); } else { // Legacy error handling switch based on the MError::$legacy switch. // @deprecated 12.1 if (MError::$legacy) { // Deprecation warning. MLog::add('MError is deprecated.', MLog::WARNING, 'deprecated'); MError::setErrorHandling(E_ERROR, 'die'); return MError::raiseError(500, MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver'])); } else { throw new MDatabaseException(MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver'])); } } } // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best. if (!class_exists($class)) { if (MError::$legacy) { // Deprecation warning. MLog::add('MError() is deprecated.', MLog::WARNING, 'deprecated'); MError::setErrorHandling(E_ERROR, 'die'); return MError::raiseError(500, MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver'])); } else { throw new MDatabaseException(MText::sprintf('MLIB_DATABASE_ERROR_LOAD_DATABASE_DRIVER', $options['driver'])); } } // Create our new MDatabase connector based on the options given. try { $instance = new $class($options); } catch (MDatabaseException $e) { // Legacy error handling switch based on the MError::$legacy switch. // @deprecated 12.1 if (MError::$legacy) { // Deprecation warning. MLog::add('MError() is deprecated.', MLog::WARNING, 'deprecated'); MError::setErrorHandling(E_ERROR, 'ignore'); return MError::raiseError(500, MText::sprintf('MLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage())); } else { throw new MDatabaseException(MText::sprintf('MLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage())); } } // Set the new connector to the global instances based on signature. self::$instances[$signature] = $instance; } return self::$instances[$signature]; }
public static function check($path, $ds = DIRECTORY_SEPARATOR) { if (strpos($path, '..') !== false) { MError::raiseError(20, 'MPath::check Use of relative paths not permitted'); mexit(); } $path = MPath::clean($path); if (MPATH_ROOT != '' && strpos($path, MPath::clean(MPATH_ROOT)) !== 0) { // Don't translate //MError::raiseError(20, 'MPath::check Snooping out of bounds @ ' . $path); //mexit(); } return $path; }
public function loadRenderer($type) { $class = 'MDocumentRenderer' . $type; if (!class_exists($class)) { $path_p = MPATH_MIWI . '/proxy/document/' . $this->_type . '/renderer/' . $type . '.php'; $path_f = MPATH_MIWI . '/framework/document/' . $this->_type . '/renderer/' . $type . '.php'; $path = $path_f; if (file_exists($path_p)) { $path = $path_p; } if (file_exists($path)) { require_once $path; } else { MError::raiseError(500, MText::_('Unable to load renderer class')); } } if (!class_exists($class)) { return null; } $instance = new $class($this); return $instance; }
public static function setErrorHandling($level, $mode, $options = null) { // Deprecation warning. MLog::add('MError::setErrorHandling() is deprecated.', MLog::WARNING, 'deprecated'); $levels = MError::$levels; $function = 'handle' . ucfirst($mode); if (!is_callable(array('MError', $function))) { return MError::raiseError(E_ERROR, 'MError:' . MERROR_ILLEGAL_MODE, 'Error Handling mode is not known', 'Mode: ' . $mode . ' is not implemented.'); } foreach ($levels as $eLevel => $eTitle) { if (($level & $eLevel) != $eLevel) { continue; } // Set callback options if ($mode == 'callback') { if (!is_array($options)) { return MError::raiseError(E_ERROR, 'MError:' . MERROR_ILLEGAL_OPTIONS, 'Options for callback not valid'); } if (!is_callable($options)) { $tmp = array('GLOBAL'); if (is_array($options)) { $tmp[0] = $options[0]; $tmp[1] = $options[1]; } else { $tmp[1] = $options; } return MError::raiseError(E_ERROR, 'MError:' . MERROR_CALLBACK_NOT_CALLABLE, 'Function is not callable', 'Function:' . $tmp[1] . ' scope ' . $tmp[0] . '.'); } } // Save settings MError::$handlers[$eLevel] = array('mode' => $mode); if ($options != null) { MError::$handlers[$eLevel]['options'] = $options; } } return true; }
public function getView($name = '', $type = '', $prefix = '', $config = array()) { static $views; if (!isset($views)) { $views = array(); } if (empty($name)) { $name = $this->getName(); } if (empty($prefix)) { $prefix = $this->getName() . 'View'; } if (empty($views[$name])) { if ($view = $this->createView($name, $prefix, $type, $config)) { $views[$name] =& $view; } else { $result = MError::raiseError(500, MText::sprintf('MLIB_APPLICATION_ERROR_VIEW_NOT_FOUND', $name, $type, $prefix)); return $result; } } return $views[$name]; }
public static function copy($src, $dest, $path = '', $force = false, $use_streams = false) { @set_time_limit(ini_get('max_execution_time')); // Initialise variables. $FTPOptions = MClientHelper::getCredentials('ftp'); if ($path) { $src = MPath::clean($path . '/' . $src); $dest = MPath::clean($path . '/' . $dest); } // Eliminate trailing directory separators, if any $src = rtrim($src, DIRECTORY_SEPARATOR); $dest = rtrim($dest, DIRECTORY_SEPARATOR); if (!self::exists($src)) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_FIND_SOURCE_FOLDER')); } if (self::exists($dest) && !$force) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_FOLDER_EXISTS')); } // Make sure the destination exists if (!self::create($dest)) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_FOLDER_CREATE')); } // If we're using ftp and don't have streams enabled if ($FTPOptions['enabled'] == 1 && !$use_streams) { // Connect the FTP client mimport('framework.client.ftp'); $ftp = MFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); if (!($dh = @opendir($src))) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_FOLDER_OPEN')); } // Walk through the directory copying files and recursing into folders. while (($file = readdir($dh)) !== false) { $sfid = $src . '/' . $file; $dfid = $dest . '/' . $file; switch (filetype($sfid)) { case 'dir': if ($file != '.' && $file != '..') { $ret = self::copy($sfid, $dfid, null, $force); if ($ret !== true) { return $ret; } } break; case 'file': // Translate path for the FTP account $dfid = MPath::clean(str_replace(MPATH_ROOT, $FTPOptions['root'], $dfid), '/'); if (!$ftp->store($sfid, $dfid)) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_COPY_FAILED')); } break; } } } else { if (!($dh = @opendir($src))) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_FOLDER_OPEN')); } // Walk through the directory copying files and recursing into folders. while (($file = readdir($dh)) !== false) { $sfid = $src . '/' . $file; $dfid = $dest . '/' . $file; switch (filetype($sfid)) { case 'dir': if ($file != '.' && $file != '..') { $ret = self::copy($sfid, $dfid, null, $force, $use_streams); if ($ret !== true) { return $ret; } } break; case 'file': if ($use_streams) { $stream = MFactory::getStream(); if (!$stream->copy($sfid, $dfid)) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_COPY_FAILED') . ': ' . $stream->getError()); } } else { if (!@copy($sfid, $dfid)) { return MError::raiseError(-1, MText::_('MLIB_FILESYSTEM_ERROR_COPY_FAILED')); } } break; } } } return true; }
protected static function call($function, $args) { if (is_callable($function)) { // PHP 5.3 workaround $temp = array(); foreach ($args as &$arg) { $temp[] =& $arg; } return call_user_func_array($function, $temp); } else { MError::raiseError(500, MText::_('MLIB_HTML_ERROR_FUNCTION_NOT_SUPPORTED')); return false; } }