summaryrefslogtreecommitdiff
path: root/app/Core
diff options
context:
space:
mode:
Diffstat (limited to 'app/Core')
-rw-r--r--app/Core/Csv.php212
-rw-r--r--app/Core/Request.php12
-rw-r--r--app/Core/Response.php3
-rw-r--r--app/Core/Router.php3
-rw-r--r--app/Core/Tool.php21
5 files changed, 228 insertions, 23 deletions
diff --git a/app/Core/Csv.php b/app/Core/Csv.php
new file mode 100644
index 00000000..6e7816f6
--- /dev/null
+++ b/app/Core/Csv.php
@@ -0,0 +1,212 @@
+<?php
+
+namespace Core;
+
+use SplFileObject;
+
+/**
+ * CSV Writer/Reader
+ *
+ * @package core
+ * @author Frederic Guillot
+ */
+class Csv
+{
+ /**
+ * CSV delimiter
+ *
+ * @access private
+ * @var string
+ */
+ private $delimiter = ',';
+
+ /**
+ * CSV enclosure
+ *
+ * @access private
+ * @var string
+ */
+ private $enclosure = '"';
+
+ /**
+ * CSV/SQL columns
+ *
+ * @access private
+ * @var array
+ */
+ private $columns = array();
+
+ /**
+ * Constructor
+ *
+ * @access public
+ * @param string $delimiter
+ * @param string $enclosure
+ */
+ public function __construct($delimiter = ',', $enclosure = '"')
+ {
+ $this->delimiter = $delimiter;
+ $this->enclosure = $enclosure;
+ }
+
+ /**
+ * Get list of delimiters
+ *
+ * @static
+ * @access public
+ * @return array
+ */
+ public static function getDelimiters()
+ {
+ return array(
+ ',' => t('Comma'),
+ ';' => t('Semi-colon'),
+ '\t' => t('Tab'),
+ '|' => t('Vertical bar'),
+ );
+ }
+
+ /**
+ * Get list of enclosures
+ *
+ * @static
+ * @access public
+ * @return array
+ */
+ public static function getEnclosures()
+ {
+ return array(
+ '"' => t('Double Quote'),
+ "'" => t('Single Quote'),
+ '' => t('None'),
+ );
+ }
+
+ /**
+ * Check boolean field value
+ *
+ * @static
+ * @access public
+ * @return integer
+ */
+ public static function getBooleanValue($value)
+ {
+ if (! empty($value)) {
+ $value = trim(strtolower($value));
+ return $value === '1' || $value{0} === 't' ? 1 : 0;
+ }
+
+ return 0;
+ }
+
+ /**
+ * Output CSV file to standard output
+ *
+ * @static
+ * @access public
+ * @param array $rows
+ */
+ public static function output(array $rows)
+ {
+ $csv = new static;
+ $csv->write('php://output', $rows);
+ }
+
+ /**
+ * Define column mapping between CSV and SQL columns
+ *
+ * @access public
+ * @param array $columns
+ * @return Csv
+ */
+ public function setColumnMapping(array $columns)
+ {
+ $this->columns = $columns;
+ return $this;
+ }
+
+ /**
+ * Read CSV file
+ *
+ * @access public
+ * @param string $filename
+ * @param \Closure $callback Example: function(array $row, $line_number)
+ * @return Csv
+ */
+ public function read($filename, $callback)
+ {
+ $file = new SplFileObject($filename);
+ $file->setFlags(SplFileObject::READ_CSV);
+ $file->setCsvControl($this->delimiter, $this->enclosure);
+ $line_number = 0;
+
+ foreach ($file as $row) {
+ $row = $this->filterRow($row);
+
+ if (! empty($row) && $line_number > 0) {
+ call_user_func_array($callback, array($this->associateColumns($row), $line_number));
+ }
+
+ $line_number++;
+ }
+
+ return $this;
+ }
+
+ /**
+ * Write CSV file
+ *
+ * @access public
+ * @param string $filename
+ * @param array $rows
+ * @return Csv
+ */
+ public function write($filename, array $rows)
+ {
+ $file = new SplFileObject($filename, 'w');
+
+ foreach ($rows as $row) {
+ $file->fputcsv($row, $this->delimiter, $this->enclosure);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Associate columns header with row values
+ *
+ * @access private
+ * @param array $row
+ * @return array
+ */
+ private function associateColumns(array $row)
+ {
+ $line = array();
+ $index = 0;
+
+ foreach ($this->columns as $sql_name => $csv_name) {
+ if (isset($row[$index])) {
+ $line[$sql_name] = $row[$index];
+ }
+ else {
+ $line[$sql_name] = '';
+ }
+
+ $index++;
+ }
+
+ return $line;
+ }
+
+ /**
+ * Filter empty rows
+ *
+ * @access private
+ * @param array $row
+ * @return array
+ */
+ private function filterRow(array $row)
+ {
+ return array_filter($row);
+ }
+}
diff --git a/app/Core/Request.php b/app/Core/Request.php
index 1eff66fa..d0fcdb8e 100644
--- a/app/Core/Request.php
+++ b/app/Core/Request.php
@@ -103,6 +103,18 @@ class Request
}
/**
+ * Get the path of an uploaded file
+ *
+ * @access public
+ * @param string $name Form file name
+ * @return string
+ */
+ public function getFilePath($name)
+ {
+ return isset($_FILES[$name]['tmp_name']) ? $_FILES[$name]['tmp_name'] : '';
+ }
+
+ /**
* Return true if the HTTP request is sent with the POST method
*
* @access public
diff --git a/app/Core/Response.php b/app/Core/Response.php
index f8ca015c..71d99524 100644
--- a/app/Core/Response.php
+++ b/app/Core/Response.php
@@ -87,8 +87,9 @@ class Response
{
$this->status($status_code);
$this->nocache();
+
header('Content-Type: text/csv');
- Tool::csv($data);
+ Csv::output($data);
exit;
}
diff --git a/app/Core/Router.php b/app/Core/Router.php
index 93d266bb..55ebe4a8 100644
--- a/app/Core/Router.php
+++ b/app/Core/Router.php
@@ -197,7 +197,7 @@ class Router extends Base
*/
public function sanitize($value, $default_value)
{
- return ! ctype_alpha($value) || empty($value) ? $default_value : strtolower($value);
+ return ! preg_match('/^[a-zA-Z_0-9]+$/', $value) ? $default_value : $value;
}
/**
@@ -218,6 +218,7 @@ class Router extends Base
list($this->controller, $this->action) = $this->findRoute($this->getPath($uri, $query_string)); // TODO: add plugin for routes
$plugin = '';
}
+
$class = empty($plugin) ? '\Controller\\'.ucfirst($this->controller) : '\Plugin\\'.ucfirst($plugin).'\Controller\\'.ucfirst($this->controller);
$instance = new $class($this->container);
diff --git a/app/Core/Tool.php b/app/Core/Tool.php
index 887c8fb3..39e42b83 100644
--- a/app/Core/Tool.php
+++ b/app/Core/Tool.php
@@ -13,27 +13,6 @@ use Pimple\Container;
class Tool
{
/**
- * Write a CSV file
- *
- * @static
- * @access public
- * @param array $rows Array of rows
- * @param string $filename Output filename
- */
- public static function csv(array $rows, $filename = 'php://output')
- {
- $fp = fopen($filename, 'w');
-
- if (is_resource($fp)) {
- foreach ($rows as $fields) {
- fputcsv($fp, $fields);
- }
-
- fclose($fp);
- }
- }
-
- /**
* Get the mailbox hash from an email address
*
* @static