1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys BlogBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\BlogBundle\Fixture
; 
  14 use Doctrine\Bundle\FixturesBundle\Fixture
; 
  15 use Doctrine\Persistence\ObjectManager
; 
  16 use Symfony\Component\DependencyInjection\ContainerAwareInterface
; 
  17 use Symfony\Component\DependencyInjection\ContainerInterface
; 
  19 use Symfony\Component\PasswordHasher\PasswordHasherInterface
; 
  20 use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory
; 
  22 use Rapsys\PackBundle\Util\SluggerUtil
; 
  24 use Rapsys\BlogBundle\Entity\Civility
; 
  25 use Rapsys\BlogBundle\Entity\Group
; 
  26 use Rapsys\BlogBundle\Entity\User
; 
  27 use Rapsys\BlogBundle\Entity\UserTranslation
; 
  28 use Rapsys\BlogBundle\Entity\Keyword
; 
  29 use Rapsys\BlogBundle\Entity\KeywordTranslation
; 
  30 use Rapsys\BlogBundle\Entity\Article
; 
  31 use Rapsys\BlogBundle\Entity\ArticleTranslation
; 
  33 #use Rapsys\AirBundle\Entity\Location; 
  34 #use Rapsys\AirBundle\Entity\Slot; 
  36 class BlogFixture 
extends Fixture 
implements ContainerAwareInterface 
{ 
  38          * @var ContainerInterface 
  40         private ContainerInterface 
$container; 
  43          * @var PasswordHasherFactory 
  45         private PasswordHasherFactory 
$hasher; 
  48          * @var Rapsys\PackBundle\Util\SluggerUtil 
  50         private SluggerUtil 
$slugger; 
  52         public function setContainer(ContainerInterface 
$container = null) { 
  53                 $this->container 
= $container; 
  54                 $this->hasher 
= $container->get('security.password_hasher_factory'); 
  55                 $this->slugger 
= $container->get('rapsys_pack.slugger_util'); 
  61         public function load(ObjectManager 
$manager) { 
  71                 foreach($civilityTree as $civilityData) { 
  72                         $civility = new Civility($civilityData); 
  73                         $manager->persist($civility); 
  74                         $civilitys[$civilityData] = $civility; 
  79                 //XXX: ROLE_XXX is required by 
  87                 foreach($groupTree as $groupData) { 
  88                         $group = new Group($groupData); 
  89                         $manager->persist($group); 
  90                         $groups[$groupData] = $group; 
  94                 //Flush to get the ids 
 100                                 'civility' => 'Mister', 
 102                                 'mail' => 'blog@rapsys.eu', 
 103                                 'password' => 'test', 
 104                                 'forename' => 'Raphaël', 
 105                                 'surname' => 'Gertz', 
 108                                 'pseudonym' => 'Rapsys', 
 109                                 'slug' => $this->slugger
->slug('Raphaël Gertz (rapsys)'), 
 111                                         'en_gb' => 'Raphaël Gertz, born in 1984, is a web developper since 2007. Interested in free software, since 2004, when he begin contributing to a linux distribution, known now as Mageia, some path has been traveled since then.', 
 112                                         'fr_fr' => 'Raphaël Gertz, né en 1984, est développeur web depuis 2007. Passionné par le monde du logiciel libre, depuis 2004, où il commence à contribuer à une distribution linux, connue maintenant sous le nom Mageia, un certain chemin a été parcouru dès lors.' 
 119                 foreach($userTree as $userData) { 
 120                         $user = new User($userData['mail'], $userData['password'], $civilitys[$userData['civility']], $userData['forename'], $userData['surname'], $userData['active'], $userData['disabled'], $userData['pseudonym'], $userData['slug']); 
 121                         #$user->setPassword($this->hasher->hashPassword($user, $userData['password'])); 
 122                         $user->addGroup($groups[$userData['group']]); 
 123                         $manager->persist($user); 
 124                         //Flush to get the id 
 126                         $users[$userData['mail']] = $user; 
 127                         foreach($userData['translations'] as $locale => $description) { 
 128                                 $userTranslation = new UserTranslation($users[$userData['mail']], $locale, $description); 
 129                                 $manager->persist($userTranslation); 
 130                                 unset($userTranslation); 
 135                 //Flush to get the ids 
 143                                         'description' => 'Portable Network Graphics (PNG) is an raster graphics file open format that supports lossless data compression' 
 147                                         'description' => 'Le Portable Network Graphics (PNG) est un format ouvert d’images numériques, qui a été créé pour remplacer le format GIF, à l’époque propriétaire et dont la compression était soumise à un brevet' 
 152                                         'title' => 'Imagick', 
 153                                         'description' => 'ImageMagick is a free and open-source software suite for displaying, converting, and editing raster image and vector image files' 
 156                                         'title' => 'Imagick', 
 157                                         'description' => 'Image Magick est une collection de logiciels libres pour afficher, convertir et modifier des images numériques matricielles ou vectorielles dans de nombreux formats' 
 163                                         'description' => 'An image is an artifact that depicts visual perception, for example, a photo or a two-dimensional picture, that has a similar appearance to some subject' 
 167                                         'description' => 'Une image est une représentation visuelle, voire mentale, de quelque chose' 
 172                                         'title' => 'Varnish', 
 173                                         'description' => 'Varnish is an HTTP cache server deployed as a reverse proxy between applications servers and clients' 
 176                                         'title' => 'Varnish', 
 177                                         'description' => 'Varnish est un serveur de cache HTTP déployé en tant que proxy inverse entre les serveurs d\'application et les clients' 
 182                                         'title' => 'Web service', 
 183                                         'description' => 'A web service is a service offered by an electronic device to another electronic device, communicating with each other via the World Wide Web' 
 186                                         'title' => 'Service web', 
 187                                         'description' => 'Un service web, ou service de la toile, est un protocole d\'interface informatique de la famille des technologies web permettant la communication et l\'échange de données entre applications et systèmes hétérogènes' 
 193                                         'description' => 'Representational state transfer (REST) or RESTful web services are a way of providing interoperability between computer systems on the Internet' 
 197                                         'description' => 'Representational state transfer (REST) ou services web RESTful est une manière de fournir de l\'intéropérabilité entre systèmes d\'information sur Internet' 
 202                                         'title' => 'HATEOAS', 
 203                                         'description' => 'HATEOAS, abbreviation of Hypermedia As The Engine Of Application State, is a constraint of the REST application architecture that distinguishes it from other network application architectures' 
 206                                         'title' => 'HATEOAS', 
 207                                         'description' => 'HATEOAS, abréviation d\'Hypermedia As Engine of Application State, Hypermédia en tant que moteur de l\'état d\'application, constitue une contrainte de l\'architecture d\'application REST qui la distingue de la plupart des autres architectures d\'applications réseau' 
 213                                         'description' => 'In information technology, a Uniform Resource Identifier (URI) is a string of characters used to identify a resource' 
 217                                         'description' => 'En technologie de l\'information, une URI, abréviation d\'Uniform Resource Identifier, Identifiant uniforme de ressource, est une chaine de caractères utilisée pour identifier une ressource' 
 223                                         'description' => 'Classless Inter-Domain Routing, CIDR, is a method for aggregating IP addresses and route them' 
 227                                         'description' => 'Routage inter-domaine sans classe, de l\'anglais Classless Inter-Domain Routing, CIDR, est une méthode pour agréger des adresses IP et les router' 
 233                                         'description' => 'Amazon Elastic Compute Cloud or EC2 is an Amazon server renting service allowing third party to run their own web application' 
 237                                         'description' => 'Amazon Elastic Compute Cloud ou EC2 est un service proposé par Amazon permettant à des tiers de louer des serveurs sur lesquels exécuter leurs propres applications web' 
 243                                         'description' => 'PHP: Hypertext Preprocessor, better known as PHP, is an open programming language, used mostly to produce dynamic web pages through an HTTP server' 
 247                                         'description' => 'PHP : Hypertext Preprocessor, plus connu sous son sigle PHP, est un langage de programmation libre, principalement utilisé pour produire des pages Web dynamiques via un serveur HTTP' 
 253                                         'description' => 'MySQL is an open-source relational database management system, RDBMS' 
 257                                         'description' => 'MySQL est un système de gestion de bases de données relationnelles libre' 
 263                                         'description' => 'Microsoft Azure, formerly Windows Azure, is a cloud computing service created by Microsoft for building, testing, deploying, and managing applications and services through a global network of Microsoft-managed data centers' 
 267                                         'description' => 'Microsoft Azure, anciennement Windows Azure, est une plateforme applicative en nuage crée par Microsoft pour construire, tester, déployer et gérer des applications et services sur un réseau global de centres de données opéré par Microsoft' 
 272                                         'title' => 'Microsoft', 
 273                                         'description' => 'Microsoft Corporation is an american multinational technology company, founded in 1975 by Bill Gates and Paul Allen' 
 276                                         'title' => 'Microsoft', 
 277                                         'description' => 'Microsoft Corporation est une multinationale informatique et micro-informatique américaine, fondée en 1975 par Bill Gates et Paul Allen' 
 284                 foreach($keywordTree as $name => $data) { 
 285                         $keyword = new Keyword(); 
 286                         $manager->persist($keyword); 
 287                         //Flush to get the id 
 289                         $keywords[$name] = $keyword; 
 290                         foreach($data as $locale => $translation) { 
 291                                 $keywordTranslation = new KeywordTranslation($keywords[$name], $locale, $translation['description'], $this->slugger
->slug($translation['title']), $translation['title']); 
 292                                 $manager->persist($keywordTranslation); 
 293                                 unset($keywordTranslation); 
 298                 //Flush to get the ids 
 304                                 'mail' => 'blog@rapsys.eu', 
 305                                 'keywords' => ['image', 'imagick', 'png'], 
 308                                                 'title' => 'How to reliably detect PNG image transparency with PHP', 
 309                                                 'description' => 'I recently had to find out if a PNG has transparency using PHP. 
 310         All the code I found didn\'t seemed to work correctly for the various kind of PNG I had to deal with. 
 311         Here is the function I used.', 
 312                                                 'body' => 'I recently had to find out if a PNG has transparency using PHP. 
 313 All the code I found didn\'t seemed to work correctly for the various kind of PNG I had to deal with. 
 315 I finished using the following function: 
 318 function png_has_transparency($im) { 
 319         //Retrieve content from imagick object 
 320         $content = $im->getImageBlob(); 
 322         //Detect 32-bit png (each pixel has tranparency level) 
 323         if (ord(substr($content, 25, 1)) & 4) { 
 325                 $p = $im->getPixelIterator(); 
 329                         //Loop on each row pixel 
 330                         foreach($r as $pix) { 
 331                                 //Check if pixel has partial transparency 
 332                                 if ($pix->getColorValue(Imagick::COLOR_ALPHA) != 1) { 
 337         //Check 8-bit png transparency 
 338         } elseif (stripos($content, \'PLTE\') !== false || stripos($content, \'tRNS\') !== false) { 
 342         //Didn\'t found clue of transparency 
 347 This function works with the only two transparency possibilities: 8 and 32-bit PNG. 
 349 The first case is a 32-bit PNG with transparency enabled, we have then to check every pixel to detect if it has transparent part or not. 
 351 The second case is a 8-bit PNG, then we only have to look the file content for transparency markers. 
 353 In this function configuration, we only read part of the file in 32-bit PNG until we detect one transparent pixel or parse content until transparency marker is detected in 8-bit PNG. 
 355 The worst case scenario will be 32-bit PNG with transparency flag without transparency or 8-bit PNG without transparency flag. 
 357 Depending on how likely you are to have transparency in each cases you might want to reverse the flow of this function. 
 359 Big thanks to these articles which expains how these parts work in a bit more detail: 
 360 - <https://www.jonefox.com/blog/2011/04/15/how-to-detect-transparency-in-png-images> 
 361 - <http://camendesign.com/code/uth1_is-png-32bit> 
 362 - <https://stackoverflow.com/questions/5495275/how-to-check-if-an-image-has-transparency-using-gd> 
 364 Hope this helps someone else out there.' 
 367                                                 'title' => 'Comment détecter la tranparence dans des images PNG en PHP de manière fiable', 
 368                                                 'description' => 'J\'ai récemment du trouver comment détecter en PHP les images PNG transparentes. 
 369         Les codes trouvés ne semblaient pas fonctionner de manière satisfaisante pour les différents types de PNG à contrôler. 
 370         Voici la fonction que j\'ai fini par utiliser.', 
 371                                                 'body' => 'J\'ai récemment du trouver comment détecter en PHP les images PNG transparentes. 
 372 Les codes trouvés ne semblaient pas fonctionner de manière satisfaisante pour les différents types de PNG à contrôler. 
 373 J\'ai fini par utiliser la fonction suivante: 
 376 function png_has_transparency($im) { 
 377         //Retrieve content from imagick object 
 378         $content = $im->getImageBlob(); 
 380         //Detect 32bit png (each pixel has tranparency level) 
 381         if (ord(substr($content, 25, 1)) & 4) { 
 383                 $p = $im->getPixelIterator(); 
 387                         //Loop on each row pixel 
 388                         foreach($r as $pix) { 
 389                                 //Check if pixel has partial transparency 
 390                                 if ($pix->getColorValue(Imagick::COLOR_ALPHA) != 1) { 
 395         //Check 8bit png transparency 
 396         } elseif (stripos($content, \'PLTE\') !== false || stripos($content, \'tRNS\') !== false) { 
 400         //Didn\'t found clue of transparency 
 405 Cette fonction fonctionne avec les deux seules possibilités : PNG 8 et 32 bits. 
 407 Le premier cas est un PNG 32 bits avec transparence activée, on doit alors vérifier l\'opacité de chaque pixel savoir si l\'image a de la transparence ou non. 
 409 Le second cas est un PNG 8 bits, on a simplement à détecter un marqueur de transparence dans le contenu du fichier. 
 411 Dans cette configuration de fonction, on lit seulement une partie du PNG 32 bits jusqu\'à détection d\'un pixel transparent où on analyse le contenu jusqu\'à trouver un marqueur de transparence dans un PNG 8 bits. 
 413 Les pires cas seront un PNG 32 bits avec marqueur de transparence sans pixel transparent ou PNG 8 bits sans marqueur de transparence. 
 415 Selon les probabilités de rencontrer les différents cas de transparence vous pouvez être intéressé pour renverser l\'ordre des tests de cette fonction. 
 417 Un grand merci à ces articles qui expliquent plus en détail comment fonctionnent les différentes parties de ce code: 
 418 - <https://www.jonefox.com/blog/2011/04/15/how-to-detect-transparency-in-png-images> 
 419 - <http://camendesign.com/code/uth1_is-png-32bit> 
 420 - <https://stackoverflow.com/questions/5495275/how-to-check-if-an-image-has-transparency-using-gd> 
 422 En espérant que cela puisse aider quelques personnes.' 
 427                                 'mail' => 'blog@rapsys.eu', 
 428                                 'keywords' => ['hateoas', 'rest', 'uri', 'varnish', 'webservice'], 
 431                                                 'title' => 'Caching webservice with varnish', 
 432                                                 'description' => 'I recently had to find a way to cache a webservice anwsers. 
 433         Here is the Varnish configuration fitting my needs.', 
 434                                                 'body' => 'I recently had to find a way to cache a webservice anwsers. 
 436 The webservice is a RESTfull API serving as a gateway between a private HATEOAS API and a client generating more than 500 000 requests a day. 
 438 The first surprise is that if your well educated client, sending you a header Authorization: Bearer, will not be cached by default by Varnish ! 
 440 Let\'s force back the standard behaviour with this header for our webservice uri prefix: 
 444         # Force cache response even with req.http.Authorization set 
 445         if (req.http.Authorization) { 
 446                 if (req.url ~ "^/webservice/uri/prefix/") { 
 453 This has security implication, because anyone allowed to request varnish will be able to retrieve a cached result without authentification. 
 455 It is important to validate the Authorization header value before serving the result from cache. 
 457 Now, our webservice has three possibles answers : 
 458 - 200: the data in JSON 
 459 - 404: data was not found 
 460 - 410: data is not available anymore 
 462 Let\'s cache our results depending on the reponse code: 
 466        if (req.url ~ "^/webservice/uri/prefix/") { 
 467                 if (beresp.status == 404) { 
 470                 if (beresp.status == 410) { 
 473                 if (beresp.status == 200) { 
 474                         set beresp.ttl = 24h; 
 480 With this configuration, we divided by 5 the quantity of request on our gateway from the client who was not able to cache our result himself.' 
 483                                                 'title' => 'Mise en cache de webservice avec varnish', 
 484                                                 'description' => 'J\'ai eu récemment à trouver comment mettre en cache les réponses d\'un webservice. 
 485         Voici la configuration varnish qui a répondu à mes besoins.', 
 486                                                 'body' => 'J\'ai eu récemment à trouver comment mettre en cache les réponses d\'un webservice. 
 488 L\'API RESTfull du webservice sert de passerelle entre un API privé HATEOAS et un client générant plus de 500 000 requêtes par jour. 
 490 La première surprise est qu\'un client bien élevé, envoyant un en-tête Authorization: Bearer, ne sera pas mis en cache par Varnish par défaut ! 
 492 Forçons le fonctionnement standard avec l\'en-tête pour le préfixe de l\'uri de notre webservice: 
 496         # Force la mise en cache de la réponse même avec req.http.Authorization présent 
 497         if (req.http.Authorization) { 
 498                 if (req.url ~ "^/webservice/uri/prefix/") { 
 505 Ce changement a des conséquences sur la sécurité, puisque n\'importe quelle personne autorisée à interroger Varnish sera en mesure de récupérer un résultat en cache sans s\'identifier. 
 507 Il est important de valider la valeur de l\'en-tête Authorization avant de fournir le résultat depuis le cache. 
 509 Notre webservice a trois réponses possibles : 
 510 - 200: les données en JSON 
 511 - 404: données non trouvées 
 512 - 410: données plus jamais disponibles 
 514 Mettons en cache les résultats selon le code de retour : 
 518        if (req.url ~ "^/webservice/uri/prefix/") { 
 519                 if (beresp.status == 404) { 
 522                 if (beresp.status == 410) { 
 525                 if (beresp.status == 200) { 
 526                         set beresp.ttl = 24h; 
 532 Avec cette configuration, on a divisé par 5 la quantité de demandes sur notre passerelle pour le client qui n\'était pas en mesure de mettre en cache lui-même nos résultats.' 
 537                                 'mail' => 'blog@rapsys.eu', 
 538                                 'keywords' => ['amazon', 'azure', 'cidr', 'microsoft', 'mysql', 'php', 'webservice'], 
 541                                                 'title' => 'Dealing with IP range in PHP/MySQL', 
 542                                                 'description' => 'I recently had to deal with CIDR blocks to tighten some webservice security. 
 543         Here is how I designed it to fulfill my needs.', 
 544                                                 'body' => 'I recently had to deal with CIDR blocks to tighten some webservice security. 
 546 First let\'s see how to compute the first and last address of an IP range with just the block base IP and mask: 
 549 $range = [\'127.0.0.1\', 8]; 
 550 function rangeBegin($range) { 
 553 function rangeEnd($range) { 
 554         return long2ip(ip2long($range[0]) | ((1 << (32 - $range[1])) - 1)); 
 558 How to detect if an IP is present in a CIDR block: 
 562 $range = [\'127.0.0.1\', 8]; 
 563 function ipInRange($ip, $range) { 
 564         if (ip2long($range[0]) <= ip2long($ip) && ip2long($ip) <= (ip2long($range[0]) | ((1 << (32 - $range[1])) - 1))) { 
 571 As a first bonus, how to retrieve amazon IP ranges: 
 574 function fetchAmazonRange() { 
 578         $ctx = stream_context_create( 
 581                                 \'method\' => \'GET\', 
 582                                 \'max_redirects\' => 0, 
 584                                 \'ignore_errors\' => false, 
 586                                         \'Connection: close\', 
 587                                         \'Accept: application/json\' 
 594         if (($json = file_get_contents(\'https://ip-ranges.amazonaws.com/ip-ranges.json\', false, $ctx)) === false) { 
 599         if (($json = json_decode($json)) === null || empty($json->prefixes)) { 
 604         foreach($json->prefixes as $range) { 
 605                 //Skip ipv6 and invalid ranges 
 606                 if (empty($range->ip_prefix)||!preg_match(\'/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+)$/\', $range->ip_prefix, $matche 
 611                 array_shift($matches); 
 613                 $amazonRanges[] = $matches; 
 617         return $amazonRanges; 
 621 Microsoft Azure Ip ranges urls: 
 622 - <https://www.microsoft.com/en-us/download/details.aspx?id=41653> 
 623 - <https://msdn.microsoft.com/library/mt757330.aspx>' 
 626                                                 'title' => 'Gestion des plages d\'IP en PHP/MySQL', 
 627                                                 'description' => 'J\'ai eu récemment à trouver comment restreindre l\'accès à un service en ligne à certaines plages d\'IP. Voici la solution qui a répondu à mes besoins.', 
 628                                                 'body' => 'J\'ai récemment du autoriser l\'accès à un service en ligne à seulement quelques plages d\'IP. 
 630 Premièrement, voyons comment calculer la première et la dernière adresse IP d\'une plage (bloc CIDR) avec sa base et son masque : 
 633 $range = [\'127.0.0.1\', 8]; 
 634 function rangeBegin($range) { 
 637 function rangeEnd($range) { 
 638         return long2ip(ip2long($range[0]) | ((1 << (32 - $range[1])) - 1)); 
 642 Maintenant comment vérifier si une IP est présente dans une plage (bloc CIDR) : 
 646 $range = [\'127.0.0.1\', 8]; 
 647 function ipInRange($ip, $range) { 
 648         if (ip2long($range[0]) <= ip2long($ip) && ip2long($ip) <= (ip2long($range[0]) | ((1 << (32 - $range[1])) - 1))) { 
 655 En premier bonus, comment récupérer les plages d\'IP d\'amazon : 
 658 function fetchAmazonRange() { 
 662         $ctx = stream_context_create( 
 665                                 \'method\' => \'GET\', 
 666                                 \'max_redirects\' => 0, 
 668                                 \'ignore_errors\' => false, 
 670                                         \'Connection: close\', 
 671                                         \'Accept: application/json\' 
 678         if (($json = file_get_contents(\'https://ip-ranges.amazonaws.com/ip-ranges.json\', false, $ctx)) === false) { 
 683         if (($json = json_decode($json)) === null || empty($json->prefixes)) { 
 688         foreach($json->prefixes as $range) { 
 689                 //Skip ipv6 and invalid ranges 
 690                 if (empty($range->ip_prefix)||!preg_match(\'/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+)$/\', $range->ip_prefix, $matche 
 695                 array_shift($matches); 
 697                 $amazonRanges[] = $matches; 
 701         return $amazonRanges; 
 705 Urls pour les plages d\'IP de Microsoft Azure : 
 706 - <https://www.microsoft.com/en-us/download/details.aspx?id=41653> 
 707 - <https://msdn.microsoft.com/library/mt757330.aspx>' 
 714                 foreach($articleTree as $i => $data) { 
 715                         $article = new Article($users[$data['mail']]); 
 716                         foreach($data['keywords'] as $keyword) { 
 717                                 $article->addKeyword($keywords[$keyword]); 
 719                         $manager->persist($article); 
 720                         //Flush to get the id 
 722                         $articles[$i] = $article; 
 723                         foreach($data['translations'] as $locale => $translation) { 
 724                                 $articleTranslation = new ArticleTranslation($articles[$i], $locale, $translation['body'], $translation['description'], $this->slugger
->slug($translation['title']), $translation['title']); 
 725                                 $manager->persist($articleTranslation); 
 730                 //Flush to get the ids