From 88a5b39d23d8c687e22fd28e1d91d9247a8d4c9e Mon Sep 17 00:00:00 2001 From: Shikiryu Date: Fri, 3 Jul 2015 00:00:30 +0200 Subject: [PATCH] Creation of the world --- .gitignore | 1 + Abstract.php | 155 +++++++++++++++++++++++ Files.php | 38 ++++++ Mysql.php | 112 +++++++++++++++++ Transport/Abstract.php | 5 + Transport/Email.php | 273 +++++++++++++++++++++++++++++++++++++++++ Transport/FTP.php | 84 +++++++++++++ bu/newhtml.html | 14 +++ tests/newhtml.html | 14 +++ tests/test.php | 14 +++ 10 files changed, 710 insertions(+) create mode 100644 .gitignore create mode 100644 Abstract.php create mode 100644 Files.php create mode 100644 Mysql.php create mode 100644 Transport/Abstract.php create mode 100644 Transport/Email.php create mode 100644 Transport/FTP.php create mode 100644 bu/newhtml.html create mode 100644 tests/newhtml.html create mode 100644 tests/test.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/Abstract.php b/Abstract.php new file mode 100644 index 0000000..4bced31 --- /dev/null +++ b/Abstract.php @@ -0,0 +1,155 @@ +_filesToBackup = array(); + $this->_streamsToBackup = array(); + } + + /** + * Add the current date with the given format into the files names + * + * @param string $format + * + * @return $this + */ + function addDate($format = 'Ymd') + { + $tmpFiles = array(); + foreach($this->_filesToBackup as $file => $name) + { + $nameA = explode('.', $name); + $nameA[] = end($nameA); + $nameA[count($nameA)-2] = date($format); + $name = implode('.', $nameA); + $tmpFiles[$file] = $name; + } + $this->_filesToBackup = $tmpFiles; + + $tmpStream = array(); + foreach($this->_streamsToBackup as $name => $stream) + { + $tmpStream[$name . '-' . date($format)] = $stream; + } + $this->_streamsToBackup = $tmpStream; + + return $this; + } + + /** + * Add the current time with the given format into the files names + * + * @param string $format + * + * @return $this + */ + function addTime($format = 'his') + { + $tmpFiles = array(); + foreach($this->_filesToBackup as $file => $name) + { + $nameA = explode('.', $name); + $nameA[] = end($nameA); + $nameA[count($nameA)-2] = date($format); + $name = implode('.', $nameA); + $tmpFiles[$file] = $name; + } + $this->_filesToBackup = $tmpFiles; + + $tmpStream = array(); + foreach($this->_streamsToBackup as $name => $stream) + { + $tmpStream[$name . '-' . date($format)] = $stream; + } + $this->_streamsToBackup = $tmpStream; + + return $this; + } + + function backupToEmail($to, $from, $objet, $mes) + { + $email = new Shikiryu_Backup_Email(); + $email->addTo($to) + ->setFrom($from) + ->setSubject($objet) + ->setMessage($mes) + ->setFiles($this->_filesToBackup) + ->setStreams($this->_streamsToBackup); + return $email->send(); + } + + function backupToDropbox() + { + + } + + function backupToFTP($adress, $login = '', $pwd = '', $path ='/') + { + $ftp = new Shikiryu_Backup_FTP($adress, $login, $pwd, $path); + $ftp->setFiles($this->_filesToBackup) + ->setStreams($this->_streamsToBackup) + ->send(); + } + + function backupToFolder($folder) + { + if (!empty($folder)) { + $folder = sprintf('%s/',rtrim($folder, '/')); + } +// if($folder != '') +// { +// if(substr($folder, 0, -1) != '/') +// $folder .= '/'; +// } + foreach($this->_filesToBackup as $file => $name) + { + copy($file, $folder . $name); + } + foreach($this->_streamsToBackup as $name => $file) + { + if (count(explode('.', $name)) < 2) { + $name = 'backup' . $name . '.txt'; + } + file_put_contents($folder . $name, $file); + } + } + + /** + * Check if all files got the minimum given size. + * + * @param int $fs + * + * @return bool + */ + function checkMinimumFilesize($fs) + { + foreach($this->_filesToBackup as $file => $name) + { + if (filesize($file) < $fs) { + return false; + } + } + foreach($this->_streamsToBackup as $name => $file) + { + if (mb_strlen($file, 'utf-8') < $fs) { + return false; + } + } + return true; + } + +} + +?> diff --git a/Files.php b/Files.php new file mode 100644 index 0000000..250f48c --- /dev/null +++ b/Files.php @@ -0,0 +1,38 @@ +_filesToBackup = array_combine($filesToBackup,$names); + } + } + + /** + * + * + * @param string[] $filestobackup a list of file path + * + * @return $this + */ + function setFilePath($filestobackup = array()) + { + if(!empty($filestobackup) && is_array($filestobackup)) + { + $names = $filestobackup; + array_walk($names, 'basename'); + $this->_filesToBackup = array_combine($filestobackup,$names); + } + return $this; + } +} +?> diff --git a/Mysql.php b/Mysql.php new file mode 100644 index 0000000..083c31e --- /dev/null +++ b/Mysql.php @@ -0,0 +1,112 @@ +_tables = array(); + $this->_pdo = new PDO('mysql:host='.$host.';dbname='.$db, $login, $pwd); + } + + /** + * set the list of table to backup + * + * @param array $tables + * + * @return $this + */ + public function setTables(array $tables) + { + if(is_array($tables) && !empty($tables)) { + $this->_tables = $tables; + } + return $this; + } + +// function withSQL($sql) { +// $statement = $this->_pdo->query($sql); +// } + + /** + * @param array $tables + * @return $this|string + */ + public function fromTables($tables = array()) + { + if(!empty($tables)) { + $this->_tables = $tables; + } + $this->_streamsToBackup[] = $this->getFromTables(); + return $this; + } + + /** + * set the list of table to backup to all tables + * + * @return $this + */ + public function everything() { + $this->_tables = array(); + foreach($this->_pdo->query('SHOW TABLES') as $table) { + $this->_tables[] = $table; + } + $this->_streamsToBackup[] = $this->getFromTables(); + return $this; + } + + /** + * @return string + */ + private function getFromTables() + { + $return = ""; + foreach ($this->_tables as $table) { + if(is_array($table)) { + $table = $table[0]; + } + $result = $this->_pdo->prepare('SELECT * FROM ' . $table); + $result->execute(); + $num_fields = $result->columnCount(); + $return .= 'DROP TABLE IF EXISTS ' . $table . ';'; + $result2 = $this->_pdo->prepare('SHOW CREATE TABLE ' . $table); + $result2->execute(); + $row2 = $result2->fetch(); + $return.= "\n\n" . $row2[1] . ";\n\n"; + foreach($result as $i=>$row){ + $return.= 'INSERT INTO ' . $table . ' VALUES('; + for ($j = 0; $j < $num_fields; $j++) { + $row[$j] = addslashes($row[$j]); + $row[$j] = preg_replace("\n", "\\n", $row[$j]); + if (isset($row[$j])) { + $return.= '"' . $row[$j] . '"'; + } else { + $return.= '""'; + } + if ($j < ($num_fields - 1)) { + $return.= ','; + } + } + $return.= ");\n"; + } + $return.="\n\n\n"; + } + return $return; + } + +} + +?> diff --git a/Transport/Abstract.php b/Transport/Abstract.php new file mode 100644 index 0000000..2b055bf --- /dev/null +++ b/Transport/Abstract.php @@ -0,0 +1,5 @@ + 'text/plain', + 'htm' => 'text/html', + 'html' => 'text/html', + 'xhtml' => 'application/xhtml+xml', + 'xht' => 'application/xhtml+xml', + 'php' => 'text/html', + 'css' => 'text/css', + 'js' => 'application/javascript', + 'json' => 'application/json', + 'xml' => 'application/xml', + 'xslt' => 'application/xslt+xml', + 'xsl' => 'application/xml', + 'dtd' => 'application/xml-dtd', + 'atom' => 'application/atom+xml', + 'mathml' => 'application/mathml+xml', + 'rdf' => 'application/rdf+xml', + 'smi' => 'application/smil', + 'smil' => 'application/smil', + 'vxml' => 'application/voicexml+xml', + 'latex' => 'application/x-latex', + 'tcl' => 'application/x-tcl', + 'tex' => 'application/x-tex', + 'texinfo' => 'application/x-texinfo', + 'wrl' => 'model/vrml', + 'wrml' => 'model/vrml', + 'ics' => 'text/calendar', + 'ifb' => 'text/calendar', + 'sgml' => 'text/sgml', + 'htc' => 'text/x-component', + // images + 'png' => 'image/png', + 'jpe' => 'image/jpeg', + 'jpeg' => 'image/jpeg', + 'jpg' => 'image/jpeg', + 'gif' => 'image/gif', + 'bmp' => 'image/bmp', + 'ico' => 'image/x-icon', + 'tiff' => 'image/tiff', + 'tif' => 'image/tiff', + 'svg' => 'image/svg+xml', + 'svgz' => 'image/svg+xml', + 'djvu' => 'image/vnd.djvu', + 'djv' => 'image/vnd.djvu', + // archives + 'zip' => 'application/zip', + 'rar' => 'application/x-rar-compressed', + 'exe' => 'application/x-msdownload', + 'msi' => 'application/x-msdownload', + 'cab' => 'application/vnd.ms-cab-compressed', + 'tar' => 'application/x-tar', + 'gz' => 'application/x-gzip', + 'tgz' => 'application/x-gzip', + // audio/video + 'mp2' => 'audio/mpeg', + 'mp3' => 'audio/mpeg', + 'qt' => 'video/quicktime', + 'mov' => 'video/quicktime', + 'mpeg' => 'video/mpeg', + 'mpg' => 'video/mpeg', + 'mpe' => 'video/mpeg', + 'wav' => 'audio/wav', + 'aiff' => 'audio/aiff', + 'aif' => 'audio/aiff', + 'avi' => 'video/msvideo', + 'wmv' => 'video/x-ms-wmv', + 'ogg' => 'application/ogg', + 'flv' => 'video/x-flv', + 'dvi' => 'application/x-dvi', + 'au' => 'audio/basic', + 'snd' => 'audio/basic', + 'mid' => 'audio/midi', + 'midi' => 'audio/midi', + 'm3u' => 'audio/x-mpegurl', + 'm4u' => 'video/vnd.mpegurl', + 'ram' => 'audio/x-pn-realaudio', + 'ra' => 'audio/x-pn-realaudio', + 'rm' => 'application/vnd.rn-realmedia', + // adobe + 'pdf' => 'application/pdf', + 'psd' => 'image/vnd.adobe.photoshop', + 'ai' => 'application/postscript', + 'eps' => 'application/postscript', + 'ps' => 'application/postscript', + 'swf' => 'application/x-shockwave-flash', + // ms office + 'doc' => 'application/msword', + 'docx' => 'application/msword', + 'rtf' => 'application/rtf', + 'xls' => 'application/vnd.ms-excel', + 'xlm' => 'application/vnd.ms-excel', + 'xla' => 'application/vnd.ms-excel', + 'xld' => 'application/vnd.ms-excel', + 'xlt' => 'application/vnd.ms-excel', + 'xlc' => 'application/vnd.ms-excel', + 'xlw' => 'application/vnd.ms-excel', + 'xll' => 'application/vnd.ms-excel', + 'ppt' => 'application/vnd.ms-powerpoint', + 'pps' => 'application/vnd.ms-powerpoint', + // open office + 'odt' => 'application/vnd.oasis.opendocument.text', + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', + ); + + /** + * Determine mime type + * + * @param string $file path to the file + * + * @return string + */ + public static function getMimeType($file) { + if (function_exists('finfo_open')) { + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $type = (string) finfo_file($finfo, $file); + finfo_close($finfo); + return $type; + } else if (function_exists('mime_content_type')) { + return mime_content_type($file); + } else { + $ext = strtolower(array_pop(explode('.', $file))); + if (array_key_exists($ext, self::$mimeTypes)) + return self::$mimeTypes[$ext]; + else + return 'application/octet-stream'; + } + } + + /** + * @param string $enc encoding of the mail + */ + function __construct($enc = 'UTF-8') { + $this->_encoding = $enc; + } + + /** + * Add a recipient + * + * @param string $to + * + * @return $this + */ + function addTo($to) { + $this->_to = $to; + return $this; + } + + /** + * Add the sender + * + * @param $from + * + * @return $this + */ + function setFrom($from) { + $this->_from = $from; + return $this; + } + + /** + * Add the subject + * + * @param $sub + * + * @return $this + */ + function setSubject($sub) { + $this->_subject = strip_tags($sub); + return $this; + } + + /** + * Add the message (in text) + * + * @param $mes + * + * @return $this + */ + function setMessage($mes) { + $this->_message = strip_tags($mes); + return $this; + } + + function setFiles($files = array()) { + if (is_array($files) && !empty($files)) { + $this->_files = $files; + } + return $this; + } + + function setStreams($streams = array()) { + if (is_array($streams) && !empty($streams)) { + $this->_streams = $streams; + } + return $this; + } + + /** + * Send the mail + * + * @see #mail + * + * @return bool + */ + function send() { + + // Checking files are selected + $zip = new ZipArchive(); // Load zip library + $zip_name = time().".zip"; // Zip name + if($zip->open(dirname(__FILE__).'/bu/'.$zip_name, ZIPARCHIVE::CREATE)==TRUE) { + if(!empty($this->_files)) { + foreach($this->_files as $file) + { + $zip->addFile($file); // Adding files into zip + } + } + $zip->close(); + } + + $this->_files = array(dirname(__FILE__).'/bu/'.$zip_name=>dirname(__FILE__).'/bu/'.$zip_name); + + $random_hash = md5(date('r', time())); + $headers = "From: " . $this->_from . "\r\nReply-To: " . $this->_from; + $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"" . $random_hash . "\""; + $output = " + +--$random_hash +Content-Type: text/plain; charset='" . strtolower($this->_encoding) . "' +Content-Transfer-Encoding: 8bit + +" . $this->_message . "\r\n"; + + if(!empty($this->_files)) + foreach($this->_files as $file=>$name) { + $name = end(explode('/', $name)); + $output .= " +--$random_hash +Content-Type: " . self::getMimeType($file) . "; name=" . $name . " +Content-Transfer-Encoding: base64 +Content-Disposition: attachment; filename=" . $name . " + +" . chunk_split(base64_encode(file_get_contents($file))); + } + + if(!empty($this->_streams)) + foreach($this->_streams as $name=>$stream) { + if(count(explode('.',$name))<2) $name = 'backup'.$name.'.txt'; + $output .= " +--$random_hash +Content-Type: text/plain; name=" . $name . " +Content-Transfer-Encoding: base64 +Content-Disposition: attachment; filename=" . $name . " + +" . chunk_split(base64_encode($stream)); + } + $output.="--$random_hash--"; + return mail($this->_to, $this->_subject, $output, $headers); + } + +} + +?> diff --git a/Transport/FTP.php b/Transport/FTP.php new file mode 100644 index 0000000..55e35ab --- /dev/null +++ b/Transport/FTP.php @@ -0,0 +1,84 @@ +_path = $path; + $this->_connection = ftp_connect($server); + + $login = ftp_login($this->_connection, $login, $pwd); + if (!$this->_connection || !$login) { + throw new Exception('Connexion FTP refusée.'); + } + } + + function setFiles($files = array()) + { + if (is_array($files) && !empty($files)) + $this->_files = $files; + return $this; + } + + function setStreams($streams = array()) { + if (is_array($streams) && !empty($streams)) + $this->_streams = $streams; + return $this; + } + + function send() + { + if (!empty($this->_files)){ + foreach ($this->_files as $file => $name) { + $upload = ftp_put($this->_connection, $this->_path.$name, $file, FTP_ASCII); + if (!$upload) { + echo 'FTP upload manquée de '.$file.' vers '.$this->_path.$name; + } +// else echo 'upload réussi de '.$file.' vers '.$this->_path.$name; + } + } + + if (!empty($this->_streams)){ + foreach ($this->_streams as $name => $stream) { + if (count(explode('.', $name)) < 2) + $name = 'backup' . $name . '.txt'; + file_put_contents($name, $stream); + $upload = ftp_put($this->_connection, $this->_path.$name, $name, FTP_ASCII); + if (!$upload) { + echo 'FTP upload manquée de '.$name.' vers '.$this->_path.$name; + } +// else echo 'upload réussi de '.$name.' vers '.$this->_path.$name; + unlink($name); + } + } + } + + function __destruct() + { + ftp_close($this->_connection); + } + + + + + + + + + + +} +?> diff --git a/bu/newhtml.html b/bu/newhtml.html new file mode 100644 index 0000000..6bd86f2 --- /dev/null +++ b/bu/newhtml.html @@ -0,0 +1,14 @@ + + + + + + + + +
TODO write content
+ + diff --git a/tests/newhtml.html b/tests/newhtml.html new file mode 100644 index 0000000..6bd86f2 --- /dev/null +++ b/tests/newhtml.html @@ -0,0 +1,14 @@ + + + + + + + + +
TODO write content
+ + diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..1b046ab --- /dev/null +++ b/tests/test.php @@ -0,0 +1,14 @@ +addDate()->addTime()->backupToEmail('from@gmail.com', 'to@gmail.com', 'test class', 'coucou'); +$backup->addDate()->addTime()->backupToFTP('ftp.domain.com', 'login', 'password', '/folder'); +$backup->backupToFolder('bu'); + +$backup2 = new Shikiryu_Backup_MYSQL('localhost', 'login', 'password', 'db'); +$backup2->fromTables(array('table1'))->addDate()->addTime(); +$backup2->backupToFolder('bu'); +?>