Example #1
0
 /**
  * Save an uploaded file to a new location.
  *
  * @param   mixed    name of $_FILE input or array of upload data
  * @param   string   new filename
  * @param   string   new directory
  * @param   integer  chmod mask
  * @return  string   full path to new file
  */
 public static function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
 {
     // Load file data from FILES if not passed as array
     $file = is_array($file) ? $file : $_FILES[$file];
     if ($filename === NULL) {
         $filename = time() . $file['name'];
     }
     if (Ko::config('upload.remove_spaces') === TRUE) {
         $filename = preg_replace('/\\s+/', '_', $filename);
     }
     if ($directory === NULL) {
         $directory = Ko::config('upload.directory', TRUE);
     }
     // Make sure the directory ends with a slash
     $directory = rtrim($directory, '/') . '/';
     if (!is_dir($directory) and Ko::config('upload.create_dir') === TRUE) {
         mkdir($directory, 0777, TRUE);
     }
     if (!is_writable($directory)) {
         throw new KoException('The upload destination folder, :dir:, does not appear to be writable.', array(':dir:' => $directory));
     }
     if (is_uploaded_file($file['tmp_name']) and move_uploaded_file($file['tmp_name'], $filename = $directory . $filename)) {
         if ($chmod !== FALSE) {
             chmod($filename, $chmod);
         }
         return $filename;
     }
     return FALSE;
 }
Example #2
0
 /**
  * Loads the configured driver and validates it.
  *
  * @param   array|string  custom configuration or config group name
  * @return  void
  */
 public function __construct($config = FALSE)
 {
     if (is_string($config)) {
         $name = $config;
         // Test the config group name
         if (($config = Ko::config('cache')->{$config}) === NULL) {
             throw new KoException('Cache: Undefined group :name.', array(':name' => $name));
         }
     }
     if (!is_array($config)) {
         // Load the default group
         $config = Ko::config('cache')->default;
     }
     // Cache the config in the object
     $this->_config = $config;
     // Set driver name
     $driver = 'Cache_' . ucfirst($this->_config['driver']);
     // Load the driver
     if (!Ko::autoload($driver)) {
         throw new KoException('Class :name not found.', array(':name' => $driver));
     }
     // Initialize the driver
     $this->_driver = new $driver($this->_config);
     // Validate the driver
     if (!$this->_driver instanceof Cache_Driver) {
         throw new KoException('Cache: Not valid driver ' . $this->_config['driver']);
     }
 }
Example #3
0
 public function lang()
 {
     //		$config = Ko::config('cache');
     //		print_r($config);
     $store = Ko::lang('store', 'en_US');
     print_r($store);
 }
Example #4
0
 /**
  * 
  * @param $lang
  */
 public function __construct($lang = 'en-US')
 {
     static $data;
     if (!isset($data[$lang])) {
         $stores = (array) Ko::config('store');
         $lang = str_replace(array(' ', '_'), '-', $lang);
         if ($lang !== 'en-US') {
             $langData = Ko::lang('store', $lang);
             foreach ($stores as &$store) {
                 if (isset($store->name)) {
                     $store->name = $langData[$store->id]['name'];
                 }
                 // if(isset($store->desc))
                 if (isset($store->desc) && isset($langData[$store->id]['desc'])) {
                     $store->desc = $langData[$store->id]['desc'];
                 }
                 if (isset($store->product_name) && isset($langData[$store->id]['product_name'])) {
                     $store->product_name = $langData[$store->id]['product_name'];
                 }
             }
             unset($stores, $langData);
         }
         $data[$lang] = (array) Ko::config('store');
     }
     $this->data = $data[$lang];
 }
Example #5
0
 public function write($id, $data)
 {
     $data = base64_encode($data);
     if (strlen($data) > 4048) {
         return FALSE;
     }
     return cookie::set($this->cookie_name, $data, Ko::config('session.expiration'));
 }
Example #6
0
 /**
  * Merge all of the configuration files in this group.
  *
  * @param   string  group name
  * @param   array   configuration array
  * @return  $this   clone of the current object
  */
 public function load($group, array $config = NULL)
 {
     if (($files = Ko::findFile($this->_directory, $group)) !== FALSE) {
         $config = array();
         foreach ($files as $file) {
             $config = Arr::merge($config, require $file);
         }
     }
     return parent::load($group, $config);
 }
Example #7
0
 /**
  * Creates a new file logger.
  *
  * @param   string  $directory log directory
  * @param   string  $filename log filename
  * @return  void
  */
 public function __construct($directory, $filename = NULL)
 {
     if (!is_dir($directory)) {
         if (!mkdir($directory, 0700, TRUE) || !is_writable($directory)) {
             throw new KoException('Directory :dir must be writable', array(':dir' => Ko::debug_path($directory)));
         }
     }
     // Determine the directory path && filename.
     $this->_directory = realpath($directory) . '/';
     if (!is_null($filename) && !empty($filename)) {
         $this->_filename = $filename;
     }
 }
Example #8
0
 public function open($path, $name)
 {
     $config = Ko::config('session.storage');
     if (empty($config)) {
         // Load the default group
         $config = Ko::config('cache.default');
     } elseif (is_string($config)) {
         $name = $config;
         // Test the config group name
         if (($config = Ko::config('cache.' . $config)) === NULL) {
             throw new KoException('The :group: group is not defined in your configuration.', array(':group:' => $name));
         }
     }
     $this->_cache = $cache = Cache::instance($config);
     return is_object($this->_cache);
 }
Example #9
0
 public function __construct()
 {
     // Load configuration
     $config = Ko::config('session');
     if (is_array($config['storage'])) {
         if (!empty($config['storage']['group'])) {
             // Set the group name
             $this->db = $config['storage']['group'];
         }
         if (!empty($config['storage']['table'])) {
             // Set the table name
             $this->table = $config['storage']['table'];
         }
     }
     Ko_Log::add('debug', 'Session Database Driver Initialized');
 }
Example #10
0
 private function _auth($reverse = FALSE)
 {
     static $backup = array();
     $keys = array('auth_user', 'auth_pass');
     foreach ($keys as $key) {
         if ($reverse) {
             if (isset($backup[$key])) {
                 $_SERVER[$key] = $backup[$key];
                 unset($backup[$key]);
             } else {
                 unset($_SERVER[$key]);
             }
         } else {
             $value = getenv($key);
             if (!empty($value)) {
                 $backup[$key] = $value;
             }
             $_SERVER[$key] = Ko::config('cache')->xcache[$key];
         }
     }
 }
Example #11
0
 /**
  * Sets up the database configuration, loads the Database_Driver.
  *
  * @throws  KoException
  */
 public function __construct($config = array())
 {
     if (empty($config)) {
         $config = Ko::config('database.default');
     } elseif (is_string($config)) {
         $name = $config;
         if (($config = Ko::config('database.' . $config)) === NULL) {
             throw new KoException('database.undefined_group :group', array(':group' => $name));
         }
     }
     // Merge the default config with the passed config
     $this->_config = array_merge($this->_config, $config);
     $driver = 'Database_' . ucfirst($this->_config['type']);
     if (!Ko::autoload($driver)) {
         throw new KoException('core.driver_not_found :error', array(':error' => $this->_config['type'] . get_class($this)));
     }
     $this->_driver = new $driver($this->_config);
     if (!$this->_driver instanceof Database_Driver) {
         throw new KoException('core.driver_implements :error', array(':error' => $this->_config['type'] . get_class($this) . 'Database_Driver'));
     }
 }
Example #12
0
 /**
  * Constructs a new Captcha object.
  *
  * @throws  KoException
  * @param   string  config group name
  * @return  void
  */
 public function __construct($config = NULL)
 {
     if (empty($config)) {
         $config = Ko::config('captcha.default');
     } elseif (is_string($config)) {
         if (($config = Ko::config('captcha.' . $config)) === NULL) {
             throw new KoException('captcha.undefined_group :group', array(':group' => $config));
         }
     }
     $config_default = Ko::config('captcha.default');
     // Merge the default config with the passed config
     self::$config = array_merge($config_default, $config);
     // If using a background image, check if it exists
     if (!empty($config['background'])) {
         self::$config['background'] = str_replace('\\', '/', realpath($config['background']));
         if (!is_file(self::$config['background'])) {
             throw new KoException('captcha.file_not_found :background', array(':background' => self::$config['background']));
         }
     }
     // If using any fonts, check if they exist
     if (!empty($config['fonts'])) {
         self::$config['fontpath'] = str_replace('\\', '/', realpath($config['fontpath'])) . '/';
         foreach ($config['fonts'] as $font) {
             if (!is_file(self::$config['fontpath'] . $font)) {
                 throw new KoException('captcha.file_not_found :font', array(':font' => self::$config['fontpath'] . $font));
             }
         }
     }
     // Set driver name
     $driver = 'Captcha_' . ucfirst($config['style']);
     // Load the driver
     if (!Ko::autoload($driver)) {
         throw new KoException('core.driver_not_found :style', array(':style' => $driver));
     }
     $this->driver = new $driver();
     // Validate the driver
     if (!$this->driver instanceof Captcha_Driver) {
         throw new KoException('core.driver_implements :driver', array(':driver' => $config['style']));
     }
 }
Example #13
0
 /**
  * Return the mime type of an extension.
  *
  * @param   string  extension: php, pdf, txt, etc
  * @return  string  mime type on success
  * @return  FALSE   on failure
  */
 public static function mime_by_ext($extension)
 {
     // Load all of the mime types
     $mimes = Ko::config('mimes');
     return isset($mimes[$extension]) ? $mimes[$extension][0] : FALSE;
 }
Example #14
0
 * Enable the Ko auto-loader.
 *
 * @see  http://php.net/spl_autoload_register
 */
spl_autoload_register(array('Ko', 'autoload'));
/**
 * Initialize Ko, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 */
Ko::init(array('base_url' => '/', 'errors' => isset($show_debug_errors) && $show_debug_errors, 'index_file' => isset($index_file) ? $index_file : 'index.php', 'caching' => TRUE));
/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Ko::modules(isset($used_modules) ? $used_modules : array());
/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
Route::set('default', '(<controller>(/<action>))(/<__KO_VARS__>)', array('__KO_VARS__' => '.+'))->defaults(array('controller' => isset($default_controller) ? $default_controller : 'index', 'action' => isset($default_action) ? $default_action : 'index'));
/**
 * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
 * If no source is specified, the URI will be automatically detected.
 */
Request::instance()->execute()->sendHeaders()->response();
Example #15
0
 /**
  * Magic object-to-string method.
  *
  * @uses    Ko::textException
  * @return  string
  */
 public function __toString()
 {
     return Ko::textException($this);
 }
Example #16
0
 /**
  * Saves or loads the route cache.
  *
  * @param   boolean   cache the current routes
  * @return  void      when saving routes
  * @return  boolean   when loading routes
  */
 public static function cache($save = FALSE)
 {
     if ($save === TRUE) {
         Ko::cache('Route::cache()', Route::$routes);
     } else {
         if ($routes = Ko::cache('Route::cache()')) {
             Route::$routes = $routes;
             return TRUE;
         } else {
             return FALSE;
         }
     }
 }
Example #17
0
 /**
  * Magic method, returns the output of render(). If any exceptions are
  * thrown, the exception output will be returned instead.
  *
  * @return  string
  */
 public function __toString()
 {
     try {
         return $this->capture();
     } catch (Exception $e) {
         Ko::exception_handler($e);
         return '';
     }
 }
Example #18
0
 public function result_array($object = NULL, $type = MYSQLI_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === TRUE) {
             $fetch = 'fetch_object';
             // NOTE - The class set by $type must be defined before fetching the result,
             // autoloading is disabled to save a lot of stupid overhead.
             $type = (is_string($type) and Ko::autoload($type)) ? $type : 'stdClass';
         } else {
             $fetch = 'fetch_array';
         }
     } else {
         // Use the default config values
         $fetch = $this->fetch_type;
         if ($fetch == 'fetch_object') {
             $type = (is_string($type) and Ko::autoload($type)) ? $type : 'stdClass';
         }
     }
     if ($this->result->num_rows) {
         // Reset the pointer location to make sure things work properly
         $this->result->data_seek(0);
         while ($row = $this->result->{$fetch}($type)) {
             $rows[] = $row;
         }
     }
     return isset($rows) ? $rows : array();
 }
 /**
  * 初始化用户地块信息
  */
 public function initUserMapData()
 {
     $initMap = array();
     $initMap = (array) Ko::config('initmap');
     if (empty($initMap)) {
         return;
     }
     $map = new MapModel();
     // 使用AS脚本里生成的代码,放到initmap类里,然后判断是否有start_time,
     foreach ($initMap as $key => $value) {
         //@todo 临时加入start_time
         $model = new StoreModel($this->lang);
         if (isset($initMap[$key]['start_time'])) {
             // 如果有start_time的话,那么就是作物,计算一下成熟的时间即可
             $item_id = $initMap[$key]['id'];
             $item = $model->getStoreById($item_id);
             if ($item && isset($item->collect_in)) {
                 $initMap[$key]['start_time'] = $this->timestamp - $item->collect_in;
             }
         }
     }
     foreach ($initMap as $map_item) {
         $map->add(array('uid' => $this->uid, 'itemid' => isset($map_item['id']) ? $map_item['id'] : 0, 'map_x' => isset($map_item['x']) ? $map_item['x'] : 0, 'map_y' => isset($map_item['y']) ? $map_item['y'] : 0, 'flipped' => isset($map_item['flipped']) ? $map_item['flipped'] : 0, 'products' => isset($map_item['products']) ? $map_item['products'] : 0, 'start_time' => isset($map_item['start_time']) ? $map_item['start_time'] : 0, 'times_used' => isset($map_item['times_used']) ? $map_item['times_used'] : 0));
     }
     //输出转对象数组
     foreach ($initMap as $key => $value) {
         $initMap[$key] = (object) $value;
     }
     return $initMap;
 }
Example #20
0
    ?>
</a></h3>
		<div id="<?php 
    echo $env_id;
    ?>
" class="collapsed">
			<table cellspacing="0">
				<?php 
    foreach ($GLOBALS[$var] as $key => $value) {
        ?>
				<tr>
					<td><code><?php 
        echo $key;
        ?>
</code></td>
					<td><pre><?php 
        echo Ko::dump($value);
        ?>
</pre></td>
				</tr>
				<?php 
    }
    ?>
			</table>
		</div>
		<?php 
}
?>
	</div>
</div>
Example #21
0
 /**
  * Save the image. If the filename is omitted, the original image will
  * be overwritten.
  *
  * @param   string   new image path
  * @param   integer  quality of image: 1-100
  * @return  boolean
  */
 public function save($file = NULL, $quality = 100)
 {
     if ($file === NULL) {
         // Overwrite the file
         $file = $this->file;
     }
     if (is_file($file)) {
         if (!is_writable($file)) {
             throw new KoException('File must be writable: :file', array(':file' => Ko::debug_path($file)));
         }
     } else {
         // Get the directory of the file
         $directory = realpath(pathinfo($file, PATHINFO_DIRNAME));
         if (!is_dir($directory) or !is_writable($directory)) {
             throw new KoException('Directory must be writable: :directory', array(':directory' => Ko::debug_path($directory)));
         }
     }
     return $this->_do_save($file, $quality);
 }
Example #22
0
 /**
  * Sends the response status and all set headers.
  *
  * @return  Request
  */
 public function sendHeaders()
 {
     if (!headers_sent()) {
         foreach ($this->headers as $name => $value) {
             if (is_string($name)) {
                 $value = "{$name}: {$value}";
             }
             header($value, TRUE, $this->status);
         }
         if ($this->status) {
             $errRequest = Ko::config('request');
             header($errRequest[$this->status], TRUE, $this->status);
             unset($errRequest);
         }
     }
     return $this;
 }
Example #23
0
 /**
  * Create a new session.
  *
  * @return  void
  */
 public function create()
 {
     // Validate the session name
     if (!preg_match('~^(?=.*[a-z])[a-z0-9_]++$~iD', self::$config['name'])) {
         throw new KoException('The session_name, :session:, is invalid. It must contain only alphanumeric characters and underscores. Also at least one letter must be present.', array(':session:' => self::$config['name']));
     }
     // Destroy any current sessions
     $this->destroy();
     if (self::$config['driver'] !== 'native') {
         $driver = 'Session_' . ucfirst(self::$config['driver']);
         if (!Ko::autoload($driver)) {
             throw new KoException('The :driver: driver for the :library: library could not be found', array(':driver:' => self::$config['driver'], ':library:' => get_class($this)));
         }
         self::$driver = new $driver();
         if (!self::$driver instanceof Session_Driver) {
             throw new KoException('The :driver: driver for the :library: library must implement the :interface: interface', array(':driver:' => self::$config['driver'], ':library:' => get_class($this), ':interface:' => 'Session_Driver'));
         }
         session_set_save_handler(array(self::$driver, 'open'), array(self::$driver, 'close'), array(self::$driver, 'read'), array(self::$driver, 'write'), array(self::$driver, 'destroy'), array(self::$driver, 'gc'));
     }
     // Name the session, this will also be the name of the cookie
     session_name(self::$config['name']);
     // Set the session cookie parameters
     // session_set_cookie_params(self::$config['lifetime']);
     // Start the session!
     session_start();
     // Put session_id in the session variable
     $_SESSION['session_id'] = session_id();
     // Update last activity
     $_SESSION['last_activity'] = time();
 }
Example #24
0
 /**
  * Deletes a cache item by id or tag
  *
  * @param   string   cache id
  * @return  boolean
  */
 public function delete($key)
 {
     $files = $this->exists($key);
     if (empty($files)) {
         return FALSE;
     }
     // Disable all error reporting while deleting
     $ER = error_reporting(0);
     foreach ($files as $file) {
         // Remove the cache file
         if (!unlink($file)) {
             Ko::log('error', 'Cache: Unable to delete cache file: ' . $file);
         }
     }
     // Turn on error reporting again
     error_reporting($ER);
     return TRUE;
 }