OPiQuotations  v.02.00.00 — October 26, 2015
 All Classes Namespaces Files Functions Variables Pages
index.php
Go to the documentation of this file.
1 <?php /* -*- coding: utf-8 -*- */
2 
3 /** \file index.php
4  * (October 26, 2015)
5  *
6  * \brief
7  * Main page to the Web application OPiCitations (over 3000 French quotes)
8  * http://www.opimedia.be/OPiCitations/
9  *
10  * Piece of OPiQuotations.
11  * https://bitbucket.org/OPiMedia/opiquotations
12  *
13  * GPLv3 --- Copyright (C) 2014, 2015 Olivier Pirson
14  * http://www.opimedia.be/
15  *
16  * @package OPiCitations
17  */
18 
19 require_once 'OPiQuotations/log.inc';
20 
21 #DEBUG
22 if (true) {
23  // Development configuration
24  ini_set('display_errors', 'stdout');
25  ini_set('display_startup_errors', 1);
26  ini_set('html_errors', 1);
27 
28  error_reporting(-1);
29 
30  assert_options(ASSERT_ACTIVE, true);
31  assert_options(ASSERT_WARNING, true);
32  assert_options(ASSERT_BAIL, true);
33 }
34 else {
35 #DEBUG_END
36  // Production configuration
37  ini_set('display_errors', 'stderr');
38  ini_set('display_startup_errors', 0);
39  ini_set('html_errors', 0);
40 
41  error_reporting(-1);
42 
43  assert_options(ASSERT_ACTIVE, false);
44  assert_options(ASSERT_WARNING, false);
45  assert_options(ASSERT_BAIL, false);
46 
47  set_error_handler('\OPiQuotations\error_handler');
48 #DEBUG
49 }
50 #DEBUG_END
51 
52 mb_internal_encoding('UTF-8');
53 mb_regex_encoding('UTF-8');
54 mb_http_output('UTF-8');
55 mb_detect_order('UTF-8');
56 
57 
58 require_once 'OPiQuotations/OPiQuotations.inc';
59 
61 
62 $lists = array('author' => $opiquotations->list_authors(),
63  'nation' => $opiquotations->list_nations(),
64  'subject' => $opiquotations->list_subjects(),
65  'work' => $opiquotations->list_works());
66 
67 $LABELS = array('ids' => 'Numéro(s)',
68  'subject' => 'Sujet',
69  'text' => 'Texte',
70  'translation' => 'Traduction',
71  'author' => 'Auteur',
72  'work' => '&OElig;uvre',
73  'nation' => 'Nation');
74 $ALL_KEYS = array_merge(array_keys($LABELS), ['id', 'all_marked', 'all']);
75 
76 
77 // GET correct parameters
78 $params = array();
79 
80 foreach ($_GET as $key=>$label) {
81  if (in_array($key, $ALL_KEYS) && !isset($params[$key])) {
82  $params[$key] = $_GET[$key];
83  }
84 }
85 
86 unset($key);
87 unset($ALL_KEYS);
88 
89 foreach (array('all', 'all_marked') as $key) {
90  if (isset($_params[$key])) {
91  $params[$key] = null;
92  }
93 }
94 
95 unset($key);
96 
97 foreach ($params as $key=>$value) {
98  if ($value !== null) {
99  $params[$key] = preg_replace('/\.\.\./', '…', # ellipsis
100  preg_replace('/\'/', '’', # apostrophe
101  trim($value)));
102  }
103 }
104 
105 unset($key);
106 unset($value);
107 
108 $param_key = null;
110 
111 if (!empty($params)) { // get the first param
112  reset($params);
113  $param_key = key($params);
114  $param_value = (in_array($param_key, ['all_marked', 'all'])
115  ? null
116  : $params[$param_key]);
117 }
118 
119 
120 // Get quotations
121 $search = null;
122 $param_id = null;
123 
124 if ($param_key === 'id') { // id searched
125  $param_id = (int)$param_value;
126 
127  $quots = $opiquotations->quotations_by_ids([$param_id]);
128  $header = null;
129  $params['ids'] = (string)$param_id;
130 }
131 else if ($param_key === 'ids') { // ids searched
132  // Analyze params
133  $ids = preg_replace('/(^[^0-9]+|[^0-9]+$)/', '', $param_value); // strip left and right
134  $ids = mb_split('[^\-0-9]+', $ids); // split on numbers or ranges (number-number)
135 
136  for ($i = 0; $i < count($ids); $i++) {
137  $id = &$ids[$i];
138  $id = preg_replace('/(^[^0-9]+|[^0-9]+$)/', '', $id); // strip left and right
139 
140  if ($id === '') {
141  $id = '0';
142  }
143 
144  $a = mb_split('-', $id); // split on numbers
145 
146  $id = (int)$a[0];
147 
148  $count_a = count($a);
149  if ($count_a > 1) { // it is a range
150  for ($j = (int)$a[0] + 1; $j <= (int)$a[$count_a - 1]; $j++) {
151  $ids[] = $j;
152  }
153  }
154  }
155 
156  unset($a);
157  unset($i);
158  unset($j);
159  unset($count_a);
160  unset($id);
161 
162  $ids = array_unique($ids, SORT_NUMERIC);
163  sort($ids);
164  if ((count($ids) > 0) && ($ids[0] === 0)) {
165  array_shift($ids);
166  }
167 
168  // Recompose numbers and ranges to display
169  $a = array();
170  $first = null;
171  if (count($ids) > 0) {
172  $first = $ids[0];
173  for ($i = 0; $i < count($ids) - 1; $i++) {
174  if ($ids[$i + 1] !== $ids[$i] + 1) {
175  $a[] = ($first === $ids[$i]
176  ? (string)$first
177  : "$first-$ids[$i]");
178  $first = $ids[$i + 1];
179  }
180  }
181  $a[] = ($first === $ids[$i]
182  ? (string)$first
183  : "$first-$ids[$i]");
184 
185  unset($first);
186  unset($i);
187  }
188 
189  // Query
190  $quots = $opiquotations->quotations_by_ids($ids);
191  $header = '<strong>Numéro(s)</strong> '.implode(', ', $a).'&thinsp;: '.count($quots).' résultat(s).';
192  $param_value = implode(',', $a);
193 
194  unset($a);
195 }
196 else if ($param_key === 'subject') { // subject searched
197  $quots = $opiquotations->quotations_by_subject($param_value);
198  $header = '<strong>Sujet</strong> &quot;'.htmlspecialchars($param_value).'&quot;&thinsp;: '.count($quots).' résultat(s).';
200 }
201 else if ($param_key === 'text') { // text searched
202  $quots = $opiquotations->quotations_by_text($param_value);
203  $header = '<strong>Texte</strong> &quot;'.htmlspecialchars($param_value).'&quot;&thinsp;: '.count($quots).' résultat(s).';
205 }
206 else if ($param_key === 'author') { // author searched
207  $quots = $opiquotations->quotations_by_author($param_value);
208  $header = '<strong>Auteur</strong> &quot;'.htmlspecialchars($param_value).'&quot;&thinsp;: '.count($quots).' résultat(s).';
210 }
211 else if ($param_key === 'work') { // work searched
212  $quots = $opiquotations->quotations_by_work($param_value);
213  $header = '<strong>&OElig;uvre</strong> &quot;'.htmlspecialchars($param_value).'&quot;&thinsp;: '.count($quots).' résultat(s).';
215 }
216 else if ($param_key === 'nation') { // nation searched
217  $quots = $opiquotations->quotations_by_nation($param_value);
218  $header = '<strong>Nation</strong> &quot;'.htmlspecialchars($param_value).'&quot;&thinsp;: '.count($quots).' résultat(s).';
220 }
221 else if ($param_key === 'translation') { // translation searched
222  $quots = $opiquotations->quotations_by_translation($param_value);
223  $header = '<strong>Traduction</strong> &quot;'.htmlspecialchars($param_value).'&quot;&thinsp;: '.count($quots).' résultat(s).';
225 }
226 else if ($param_key === 'all_marked') { // all quotations marked
227  $quots = $opiquotations->quotations_all_marked();
228  $header = count($quots).' citations <strong>marquées</strong>.';
229 }
230 else if ($param_key === 'all') { // all quotations
231  $quots = $opiquotations->quotations_all();
232  $header = '<strong>Toutes</strong> les '.count($quots).' citations.';
233 }
234 else { // 10 quotations at random
235  $quots = $opiquotations->quotations_by_random(10);
236  $header = count($quots).' citations choisies <strong>aléatoirement</strong>.';
237 }
238 
239 
240 // Init other informations
241 $only_one = (count($quots) === 1);
242 
243 $keywords = ['citation', 'citations', 'maxime', 'maximes', 'proverbe', 'proverbes', 'littérature', 'français'];
244 
245 if ($only_one) {
246  $title = 'OPiCitations n°'.$quots[0]->id().' &mdash; Dictionnaire de plus de 3000 citations';
247 
248  $desc = 'OPiCitations n°'.$quots[0]->id().' :
249 '.htmlspecialchars($quots[0]->to_text());
250 
251 
252  // Prepare keywords from quotation
253  $i = 0;
254  foreach ([$quots[0]->subject(), ($quots[0]->is_maxim()
255  ? $quots[0]->nation()
256  : $quots[0]->author()), $quots[0]->work()] as $keyword) {
257  if ($keyword !== null) {
258  if ($i < 2) { // if subject, nation/author then also add pieces
259  $a = explode(' ', $keyword);
260  if (count($a) > 1) {
261  $a[] = $keyword;
262  }
263  }
264  else {
265  $a = [$keyword];
266  }
267 
268  foreach ($a as $keyword) {
269  $keywords[] = htmlspecialchars($keyword);
270  }
271 
272  unset($a);
273  }
274 
275  ++$i;
276  }
277 
278  unset($i);
279  unset($keyword);
280 }
281 else {
282  $title = 'OPiCitations &mdash; Dictionnaire de plus de 3000 citations';
283 
284  $desc = 'Dictionnaire de plus de 3000 citations.';
285 
286  if ($header !== null) {
287  $desc .= '
288 '.strip_tags($header);
289  }
290 }
291 
294 
295 
296 // Set URL
297 $url_site = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/';
299 
300 if ($only_one) {
301  $url .= '?id='.$quots[0]->id();
302 }
303 else {
304  if ($param_key !== null) {
305  $url .= '?'.$param_key;
306  if ($param_value !== null) {
307  $url .= '='.rawurlencode($param_value);
308  }
309  }
310 }
311 
312 ?><!DOCTYPE html>
313 <html lang="fr">
314  <head>
315  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
316 
317  <meta name="viewport" content="width=device-width, initial-scale=1">
318 
319  <meta name="author" content="Olivier Pirson">
320  <meta name="description" content="<?php echo $desc; ?>">
321  <meta name="keywords" content="<?php echo implode(',', $keywords); ?>">
322  <meta name="keywords" lang="en" content="quotation,quotations,maxim,maxims,proverb,proverbs,literature,French">
323 
324  <title><?php echo $title; ?></title>
325 
326  <link rel="stylesheet" type="text/css" href="public/css/style.min.css">
327  <style type="text/css" title="highlight">
328 /* Stylesheet to activate/deactivate highlight with JavaScript. */
329  </style>
330 
331 <?php if ($only_one): ?>
332  <script type="text/javascript">window.___gcfg = {lang: 'fr', parsetags: 'explicit'};</script>
333  <script type="text/javascript" src="http://platform.twitter.com/widgets.js" async="async"></script>
334  <script type="text/javascript" src="http://connect.facebook.net/fr_FR/sdk.js" async="async"></script>
335  <script type="text/javascript" src="https://apis.google.com/js/platform.js" async="async"></script>
336 <?php endif; ?>
337  <script type="text/javascript" src="public/js/OPiQuotations.automatic-min.js" async="async"></script>
338 
339  <link rel="icon" type="image/x-icon" href="favicon.ico">
340 
341  <link rel="canonical" href="<?php echo $url; ?>">
342 
343  <meta property="og:image" content="<?php echo $url_site; ?>public/img/OPiQuotations<?php
344 
345 $all_is_maxim = !empty($quots) || ($param_key === 'nation');
346 
347 if ($all_is_maxim) {
348  foreach ($quots as $quot) {
349  if (!$quot->is_maxim()) {
350  $all_is_maxim = false;
351 
352  break;
353  }
354  }
355  if ($all_is_maxim) {
356  echo '-maxim';
357  }
358 }
359 
360 unset($all_is_maxim);
361 
362 ?>-256x256-t.png">
363  <meta property="og:description" content="<?php echo $desc_open_graph; ?>">
364  <meta property="og:title" content="<?php echo $title; ?>">
365  <meta property="og:type" content="website">
366  <meta property="og:url" content="<?php echo $url; ?>">
367 
368  <meta name="msapplication-TileColor" content="#fdfdd0">
369  <meta name="msapplication-square150x150logo" content="public/img/OPiQuotations-64x64-t.png">
370  </head>
371  <body>
372  <div id="top"></div>
373 <!--[if lt IE 8]>
374  <div class="outdated-IE">
375  Votre navigateur est complètement <strong>obsolète</strong>.
376  Je préconise
377  <strong><a href="https://www.mozilla.org/" target="_blank">Firefox</a></strong>.
378  </div>
379 <![endif]-->
380 
381  <nav id="lists">
382 <?php
383 
384 foreach ($LABELS as $key=>$label) {
385  if (isset($lists[$key])) {
386  echo '<section id="list-', $key, '" class="list" aria-haspopup="true">
387  <header>
388  <button onclick="lists_hide();">Fermer</button>
389  <button class="float-right" onclick="lists_hide();">Fermer</button>
390  <h2>', $label, 's</h2>
391  </header>
392  <div id="list-', $key, '-container">Chargement&hellip;</div>
393 </section>
394 ';
395  }
396 }
397 
398 unset($key);
399 unset($label);
400 
401 ?>
402  </nav>
403 
404  <nav id="panel">
405 <!--[if lt IE 9]>
406  <header id="please-don-t-use-IE">
407  <p>Je vous en supplie,<br>utilisez un <i>vrai</i> navigateur&nbsp;!</p>
408  <p>
409  Je préconise
410  <strong><a href="https://www.mozilla.org/" target="_blank">Firefox</a></strong>.
411  </p>
412  </header>
413 <![endif]-->
414  <form class="big-buttons" action="#">
415  <button name="random_10">10 choisies <strong>aléatoirement</strong></button>
416  <button name="all_marked">Toutes celles <strong>marquées</strong></button>
417  <button name="all"><strong>Toutes</strong> les citations/maximes</button>
418  </form>
419  <ul id="menu" class="smallskip">
420 <?php
421 
422 // HTML form inputs
423 foreach ($LABELS as $key=>$label) {
424  echo ' <li class="', $key, '">
425  <div>
426  <label for="', $key, '">', $label, '</label>&thinsp;:
427 ';
428  if (isset($lists[$key])) {
429  echo ' <button class="float-right" onclick="list_open(\''.$key.'\'); return false;">liste</button>
430 ';
431  }
432  echo ' </div>
433  <form action="#">
434  <input id="', $key, '" name="', $key, '" type="text"', (isset($params[$key])
435  ? ' value="'.htmlspecialchars($params[$key]).'"'
436  : null), '>
437  </form>
438  </li>
439 ';
440 }
441 
442 unset($LABELS);
443 unset($key);
444 unset($label);
445 
446 ?>
447  <li>
448  <button onclick="highlight_on_off();" title="Néglige certaines occurences utilisant certains caractères non-alphanumériques.">Surligner</button>
449  <a class="go-bottom" href="#bottom"><span>&darr;</span></a>
450  <a class="go-top" href="#top"><span>&uarr;</span></a>
451  </li>
452  </ul>
453 
454  <footer>
455  <div id="about">
456  <div>
457  <h2>&Agrave; propos de</h2>
458  <div aria-haspopup="true">
459  <h3>
460  <a class="nodecoration" href="<?php echo $url_site; ?>">O<span class="surname">Pi</span>Citations</a>
461  </h3>
462  <p class="right">version 02.00.00 &mdash; 26 octobre 2015</p>
463  <p>
464  Ensemble des citations pour l&rsquo;essentiel jadis récoltées par mes soins au hasard de mes lectures
465  (accumulées dans un programme créé à l&rsquo;époque en AMOS BASIC
466  et déjà réutilisées par ce
467  <a href="http://www.opimedia.be/DS/grenier/index.htm#enre" target="_blank">programme C</a>
468  sous M$-DO$).
469  </p>
470  <p>
471  Vous pouvez me contacter par e-mail pour me signaler toute erreur constatée&thinsp;:
472  <a class="monospace" href="mailto:olivier_pirson_opi@yahoo.fr">olivier_pirson_opi@yahoo.fr</a>
473  </p>
474  <p class="smallskip sep">
475  Accompagnent cette application&thinsp;:
476  </p>
477  <ul>
478  <li>
479  Une version réduite n&rsquo;affichant qu&rsquo;une seule citation
480  (pouvant être utilisée en tant qu&rsquo;<span class="monospace">iframe</span>
481  pour l&rsquo;inclure dans une autre page Web)&thinsp;:
482  <strong><a class="uneOPiQuotation" href="uneOPiCitation.php" target="_blank">une OPiCitation</a></strong>
483  </li>
484  <li>
485  Une citation postée chaque jour sur Twitter&thinsp;:
486  <strong><a href="https://twitter.com/OPiCitationJour" target="_blank">@OPiCitationJour</a></strong>
487  </li>
488  <li>
489  Une autre sur Facebook&thinsp;:
490  <strong><a href="https://www.facebook.com/1OPiCitationParJour" target="_blank">1OPiCitationParJour</a></strong>
491  </li>
492  </ul>
493  <p class="smallskip sep">
494  Programme PHP/MySQL/HTML/Sass/JavaScript sous licence libre <abbr title="GNU General Public License">GPL</abbr>.
495  </p>
496  <ul>
497  <li>
498  Sources complètes (avec le fichier de citations) sur Bitbucket&thinsp;:
499  <strong><a lang="en" class="monospace" href="https://bitbucket.org/OPiMedia/opiquotations" target="_blank">https://bitbucket.org/OPiMedia/opiquotations</a></strong>
500  </li>
501  <li><strong><a lang="en" href="http://www.opimedia.be/DS/webdev/PHP/OPiQuotations/docs/index.html" target="_blank">Documentation HTML en ligne</a></strong></li>
502  </ul>
503  <p class="smallskip sep ds-jf-opi">
504  D&rsquo;autres citations sur mes sites&thinsp;:
505  <a href="http://www.opimedia.be/DS/citations/index.htm" target="_blank"><img src="public/img/DS-t.png" width="80" height="34" alt="[DS]"></a>
506  <a href="http://www.opimedia.be/JF/citations/index.htm" target="_blank"><img src="public/img/JF-t.png" width="41" height="34" alt="[JF]"></a>
507  <a href="http://www.opimedia.be/citations/index.htm" target="_blank"><img src="public/img/OPi-t.png" width="41" height="34" alt="[OPi]"></a>
508  </p>
509  <p class="smallskip sep center">
510  <a class="donate" href="http://www.opimedia.be/donate/index.htm" target="_blank"></a>
511  <a class="opi" href="http://www.opimedia.be/" target="_blank">Olivier <span class="surname">Pirson</span> O<span class="surname">Pi</span></a>
512  </p>
513  </div>
514  </div>
515  </div>
516  <div>
517  <small>Une citation par jour sur <strong>Twitter</strong>&thinsp;:</small><br>
518  <a class="icon-Twitter" href="https://twitter.com/OPiCitationJour" target="_blank">@OPiCitationJour</a>
519  </div>
520  <div class="smallskip">
521  <small>Une citation par jour sur <strong>Facebook</strong>&thinsp;:</small><br>
522  <a class="icon-Facebook" href="https://www.facebook.com/1OPiCitationParJour" target="_blank">1OPiCitationParJour</a>
523  </div>
524  </footer>
525  </nav>
526 
527  <main>
528  <h1>
529  <a class="nodecoration" href="<?php echo $url_site; ?>"><span>O<span class="surname">Pi</span>Citations</span></a>
530  </h1>
531 
532  <header><?php
533 
534 if ($header !== null) {
535  echo $header;
536 }
537 echo '</header>
538 ';
539 
540 
541 // Display quotations
542 foreach ($quots as $quot) {
543  echo $quot->to_html($search);
544 }
545 
546 unset($quot);
547 
548 
549 if ($only_one) {
550  // Sharing buttons Twitter, Facebook and Google+
551  $max_length = 140 - 22 - 1; // 22 for length of short url
552  $quot_short = htmlspecialchars($quots[0]->to_text_twitter(null, null, $max_length));
553 
554  echo ' <footer id="sharing-global">
555  Partager cette citation&thinsp;:
556  <ul>
557  <li>
558  <a class="twitter-share-button" href="https://twitter.com/share" data-lang="fr" data-text="', $quot_short, '" data-url="', $url, '"',
559  (mb_strlen($quot_short) + 21 <= $max_length
560  ? ' data-via="OPiCitationJour"' # ' via @OPiCitationJour': length 21
561  : ''), '></a>
562  <li>
563  <div class="fb-like" data-href="', $url, '" data-layout="button_count" data-share="true" data-show-faces="true"></div>
564  <li>
565  <div class="g-plus" data-action="share" data-annotation="bubble" data-href="', $url, '" data-width="120"></div>
566  </ul>
567  </footer>
568 ';
569 }
570 
571 ?>
572  </main>
573 
574  <nav id="right-panel">
575  <a class="go-top" href="#top"><span>&uarr;</span></a>
576  <a class="go-bottom" href="#bottom"><span>&darr;</span></a>
577  </nav>
578 
579  <div id="bottom"></div>
580  </body>
581 </html>