diff --git a/configure.php b/configure.php index 85e2e10..6886043 100644 --- a/configure.php +++ b/configure.php @@ -3,7 +3,7 @@ set_include_path ( get_include_path () - .PATH_SEPARATOR.__DIR__.'/lib' + .PATH_SEPARATOR.__DIR__ ); ?> diff --git a/debian/changelog b/debian/changelog index a5440be..9776523 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -php-vn-lib (1.208-deb8) stable; urgency=low +php-vn-lib (1.210-deb8) stable; urgency=low * Initial Release. diff --git a/debian/install b/debian/install index bdbef13..d38f7e5 100644 --- a/debian/install +++ b/debian/install @@ -1 +1 @@ -lib/* usr/share/php +vn/* usr/share/php/vn diff --git a/lib/vn/lib/lib.php b/lib/vn/lib/lib.php deleted file mode 100755 index 39489dd..0000000 --- a/lib/vn/lib/lib.php +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/lib/vn/db/conn.php b/vn/db/conn.php similarity index 69% rename from lib/vn/db/conn.php rename to vn/db/conn.php index cd87a06..a433272 100755 --- a/lib/vn/db/conn.php +++ b/vn/db/conn.php @@ -24,6 +24,7 @@ class Conn { $conn = $this->conn = mysqli_init (); $conn->options (MYSQLI_READ_DEFAULT_FILE, __DIR__.'/my.cnf'); + $conn->options (MYSQLI_OPT_LOCAL_INFILE, TRUE); @$conn->real_connect ($host, $user, $pass, $name, $port); if (mysqli_connect_errno ()) @@ -48,81 +49,16 @@ class Conn $this->isOpen = FALSE; } - - /** - * Checks whether the connection is open. - * - * @return boolean %TRUE if connection is open, %FALSE otherwise - **/ - function isOpen () - { - return $this->isOpen; - } - - /** - * Executes the query and gets it's first row. - * - * @param string $query The SQL query - * @param mixed[] $params The query parameters - * - * @return mixed[] An associative array with the first row, %NULL if error - **/ - function getRow ($query, $params = NULL) - { - $result = $this->query ($query, $params); - if ($result) - { - $row = $result->fetch_assoc (); - $result->free (); - return $row; - } - - return NULL; - } - /** - * Executes the query and gets the first value of the first row. + * Changes the default schema for the current connection. * - * @param string $query The SQL query - * @param mixed[] $params The query parameters - * - * @return mixed The value or %NULL if error + * @param string $schema The schema name + * @return boolean %TRUE if success, %FALSE otherwise **/ - function getValue ($query, $params = NULL) + function selectDb ($dbName) { - $value = NULL; - $result = $this->query ($query, $params); - - if ($result) - { - $row = $result->fetch_row (); - - if ($row && count ($row) > 0) - $value = $row[0]; - - $result->free (); - } - - return $value; - } - - /** - * Executes the query stored in the specified file and gets the result. - * - * @param string $query The SQL query - * @param mixed[] $params The query parameters - * - * @return mixed The value or %NULL if error - **/ - function queryFromFile ($file, $params = NULL) - { - $query = file_get_contents ($file); - - if ($query) - return $this->query ($query, $params); - - return NULL; + return $this->conn->select_db ($dbName); } /** @@ -145,6 +81,151 @@ class Conn return $result; } + + /** + * Checks whether the connection is open. + * + * @return boolean %TRUE if connection is open, %FALSE otherwise + **/ + function isOpen () + { + return $this->isOpen; + } + + /** + * Gets the first row value of the first column from a result. + * + * @param resource $result The database result + * + * @return mixed[] An associative array with the first row, %NULL if error + **/ + function getRowFromResult ($result) + { + if ($result) + { + $row = $result->fetch_assoc (); + $result->free (); + return $row; + } + + return NULL; + } + + /** + * Gets the first row value of the first column from a result. + * + * @param resource $result The database result + * + * @return mixed The value or %NULL if error + **/ + function getValueFromResult ($result) + { + $value = NULL; + + if ($result) + { + $row = $result->fetch_row (); + + if ($row && count ($row) > 0) + $value = $row[0]; + + $result->free (); + } + + return $value; + } + + /** + * Executes the query and gets it's first row. + * + * @param string $query The SQL query + * @param mixed[] $params The query parameters + * + * @return mixed[] An associative array with the first row, %NULL if error + **/ + function getRow ($query, $params = NULL) + { + $result = $this->query ($query, $params); + return $this->getRowFromResult ($result); + } + + /** + * Executes the query and gets the first value of the first row. + * + * @param string $query The SQL query + * @param mixed[] $params The query parameters + * + * @return mixed The value or %NULL if error + **/ + function getValue ($query, $params = NULL) + { + $result = $this->query ($query, $params); + return $this->getValueFromResult ($result); + } + + /** + * Loads a query from a file and renders it. + * + * @param string $file The file path + * @param mixed[] $params The query parameters + * + * @return mixed The query string + **/ + function loadFromFile ($file, $params = NULL) + { + $query = file_get_contents ($file .'.sql'); + + if ($query === FALSE) + throw new Exception (NULL, 'Can not load query from file'); + + return $this->render ($query, $params); + } + + /** + * Executes the query stored in the specified file and gets the result. + * + * @param string $file The file path + * @param mixed[] $params The query parameters + * + * @return mixed The value or %NULL if error + **/ + function queryFromFile ($file, $params = NULL) + { + $query = $this->loadFromFile ($file, $params); + + if ($query) + return $this->query ($query); + + return NULL; + } + + /** + * Executes the file query and gets it's first row. + * + * @param string $file The file path + * @param mixed[] $params The query parameters + * + * @return mixed[] An associative array with the first row, %NULL if error + **/ + function getRowFromFile ($file, $params = NULL) + { + $result = $this->queryFromFile ($query, $params); + return $this->getRowFromResult ($result); + } + + /** + * Executes the file query and gets the first value of the first row. + * + * @param string $file The file path + * @param mixed[] $params The query parameters + * + * @return mixed The value or %NULL if error + **/ + function getValueFromFile ($file, $params = NULL) + { + $result = $this->queryFromFile ($query, $params); + return $this->getValueFromResult ($result); + } /** * Execute multiple queries separated by semicolons. @@ -193,7 +274,9 @@ class Conn function nextResult () { - return $this->conn->next_result (); + $hasNext = $this->conn->next_result (); + $this->checkError (); + return $hasNext; } /** @@ -222,7 +305,7 @@ class Conn * @param string $query The SQL string * @param mixed[] $paramsMap The query parameters * - * @return mixed The rendered SQL string + * @return string The rendered SQL string **/ function render (&$query, &$paramsMap = NULL) { @@ -252,6 +335,13 @@ class Conn return $query; } + /** + * Gets an SQL respresentation from a PHP value. + * + * @param mixed $value The value + * + * @return string The SQL value + **/ function renderValue ($value) { if ($value !== NULL) diff --git a/lib/vn/db/db.php b/vn/db/db.php similarity index 100% rename from lib/vn/db/db.php rename to vn/db/db.php diff --git a/lib/vn/db/exception.php b/vn/db/exception.php similarity index 100% rename from lib/vn/db/exception.php rename to vn/db/exception.php diff --git a/lib/vn/db/my.cnf b/vn/db/my.cnf similarity index 100% rename from lib/vn/db/my.cnf rename to vn/db/my.cnf diff --git a/vn/lib/app.php b/vn/lib/app.php new file mode 100644 index 0000000..db8db30 --- /dev/null +++ b/vn/lib/app.php @@ -0,0 +1,181 @@ +name = $name; + } + + /** + * Initializes Application. Should be the first called function in any + * application that uses this class. + **/ + function init () + { + ini_set ('log_errors', TRUE); +// ini_set ('error_log', _LOG_DIR .'/'. $this->name .'.log'); + + $configFile = $this->getConfigFile (); + $this->conf = include ($configFile); + + register_shutdown_function ([$this, 'deinit']); + } + + /** + * Deinitializes the Application. When init method is called, this + * function is called automatically at the end of the script . + **/ + function deinit () + { + if ($this->sysConn) + { + $this->sysConn->close (); + $this->sysConn = NULL; + } + } + + /** + * Obtains the application version number. It is based on de last + * modification date of the main script. + **/ + function getVersion () + { + return (int) filectime (__FILE__); + } + + /** + * Gets the configuration file name. + **/ + function getConfigFile () + { + $configDir = _CONFIG_DIR .'/'. $this->name; + $customFile = "$configDir/config.my.php"; + + if (file_exists ($customFile)) + return $customFile; + else + return "$configDir/config.php"; + } + + /** + * Creates a new connection object using the configuration parameters and + * the passed user and password. + **/ + function createConnection ($user, $password, $persistent = FALSE) + { + $dbConf = $this->conf['db']; + $host = $dbConf['host']; + + if ($persistent) + $host = 'p:'.$host; + + $conn = new Conn (); + $conn->open ( + $host + ,$user + ,$password + ,$dbConf['schema'] + ,$dbConf['port'] + ); + $conn->query ('SET @lang = #', [Locale::get ()]); + return $conn; + } + + /** + * Opens the system database connection. + * + * @return Vn\Db\Conn The connection + **/ + function getSysConn () + { + if (!$this->sysConn) + { + $dbConf = $this->conf['db']; + $this->sysConn = $this->createConnection ( + $dbConf['user'], base64_decode ($dbConf['pass']), TRUE); + } + + return $this->sysConn; + } + + /** + * Starts the application. Should be implemented by child classes. + **/ + function run () {} + + /** + * Authenticates the user. Should be reimplemented by child classes. + **/ + function login () {} + + /** + * Deauthenticates the user. Should be reimplemented by child classes. + **/ + function logout () {} + + /** + * Runs a method. + **/ + function runMethod ($baseDir, $methodName) + { + if (empty ($methodName)) + throw new \Exception ('Method not defined'); + if (!isHyphen ($methodName)) + throw new \Exception ('Method contains invalid characters'); + + $className = hyphenToCamelCase ($methodName, TRUE); + $methodFile = "$baseDir/$methodName.php"; + include_once ($methodFile); + + if (!class_exists ($className)) + throw new \Exception ('Method class not exists'); + if (!is_subclass_of ($className, __NAMESPACE__ .'\Method')) + throw new \Exception ('Class is not a Method class child'); + + Locale::addPath ("$baseDir/$methodName"); + return new $className ($this); + } + +} + +?> diff --git a/vn/lib/cli-app.php b/vn/lib/cli-app.php new file mode 100755 index 0000000..c6fc16e --- /dev/null +++ b/vn/lib/cli-app.php @@ -0,0 +1,57 @@ +init (); + + if ($lang = substr (getenv ('LANG'), 0, 2)) + Locale::set ($lang); + + try { + $options = getopt ('m:'); + + if (!empty ($options['m'])) + $this->usage (); + + switch ($options['m']) + { + default: + $this->usage (); + } + + echo s('Action completed.')."\n"; + } + catch (Exception $e) + { + echo $e->getMessage ()."\n"; + exit (2); + } + } + + function login () + { + $this->conn = $this->getSysConn (); + } + + function logout () + { + $this->conn = NULL; + } + + function usage () + { + echo "app.php -m method\n"; + exit (1); + } +} + +?> diff --git a/vn/lib/lib.php b/vn/lib/lib.php new file mode 100755 index 0000000..fc670c5 --- /dev/null +++ b/vn/lib/lib.php @@ -0,0 +1,11 @@ + diff --git a/lib/vn/lib/locale.php b/vn/lib/locale.php similarity index 100% rename from lib/vn/lib/locale.php rename to vn/lib/locale.php diff --git a/lib/vn/lib/log.php b/vn/lib/log.php similarity index 100% rename from lib/vn/lib/log.php rename to vn/lib/log.php diff --git a/vn/lib/method.php b/vn/lib/method.php new file mode 100644 index 0000000..4f2ec16 --- /dev/null +++ b/vn/lib/method.php @@ -0,0 +1,85 @@ +app = $app; + } + + /** + * Executes the method. Shoud be defined by child classes. + * + * @return mixed The result of the method + **/ + abstract function run (); + + /** + * Authenticates the user agaisnt database and returns its associated + * database connection. + * + * return Db\Conn The database connection + **/ + function login () + { + return $this->app->login (); + } + + /** + * Logouts the current user. + **/ + function logout () + { + $this->app->logout (); + } + + /** + * Returns the system database connection. + * + * return Db\Conn The database connection + **/ + function getSysConn () + { + return $this->app->getSysConn (); + } + + /** + * Check if a set of keys are defined and not empty inside the passed + * associative array. + * + * @param {string} $map The map to check + * @param {array} $params The list of keys to check + * @return {boolean} %TRUE if all key are defined, %FALSE otherwise + **/ + function checkParams ($map, $params) + { + if (!isset ($map)) + return FALSE; + + foreach ($params as $param) + if (empty ($map[$param])) + { + return FALSE; + break; + } + + return TRUE; + } +} + +?> diff --git a/lib/vn/lib/type.php b/vn/lib/type.php similarity index 100% rename from lib/vn/lib/type.php rename to vn/lib/type.php diff --git a/vn/lib/util.php b/vn/lib/util.php new file mode 100755 index 0000000..803b614 --- /dev/null +++ b/vn/lib/util.php @@ -0,0 +1,50 @@ +