-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathopen-graph-protocol.php
666 lines (621 loc) · 17.2 KB
/
open-graph-protocol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<?php
/**
* Open Graph Protocol data class. Define and validate OGP values.
*
* @package open-graph-protocol-tools
* @author Niall Kennedy <[email protected]>
* @version 1.3
* @copyright Public Domain
*/
/**
* Open Graph protocol type labels are passed through gettext message interpreters for the current context.
* Fake the interpreter function alias if not defined
*/
if ( !function_exists('_') ):
function _( $text, $domain='' ) {
return $text;
}
endif;
/**
* Validate inputted text against Open Graph Protocol requirements by parameter.
*
* @link http://ogp.me/ Open Graph Protocol
* @version 1.3
*/
class OpenGraphProtocol {
/**
* Version
* @var string
*/
const VERSION = '1.3';
/**
* Should we remotely request each referenced URL to make sure it exists and returns the expected Internet media type?
* @var bool
*/
const VERIFY_URLS = false;
/**
* Meta attribute name. Use 'property' if you prefer RDF or 'name' if you prefer HTML validation
* @var string
*/
const META_ATTR = 'property';
/**
* Property prefix
* @var string
*/
const PREFIX = 'og';
/**
* prefix namespace
* @var string
*/
const NS = 'http://ogp.me/ns#';
/**
* Page classification according to a pre-defined set of base types.
*
* @var string
* @since 1.0
*/
protected $type;
/**
* The title of your object as it should appear within the graph.
*
* @var string
* @since 1.0
*/
protected $title;
/**
* If your object is part of a larger web site, the name which should be displayed for the overall site.
*
* @var string
* @since 1.0
*/
protected $site_name;
/**
* A one to two sentence description of your object.
*
* @var string
* @since 1.0
*/
protected $description;
/**
* The canonical URL of your object that will be used as its permanent ID in the graph.
*
* @var string
* @since 1.0
*/
protected $url;
/**
* The word that appears before this object's title in a sentence
*
* @var string
* @since 1.3
*/
protected $determiner;
/**
* Language and optional territory of page content.
* @var string
* @since 1.3
*/
protected $locale;
/**
* An array of OpenGraphProtocolImage objects
*
* @var array
* @since 1.0
*/
protected $image;
/**
* An array of OpenGraphProtocolAudio objects
*
* @var array
* @since 1.2
*/
protected $audio;
/**
* An array of OpenGraphProtocolVideo objects
*
* @var array
* @since 1.2
*/
protected $video;
/**
* Build Open Graph protocol HTML markup based on an array
*
* @param array $og associative array of OGP properties and values
* @param string $prefix optional prefix to prepend to all properties
*/
public static function buildHTML( array $og, $prefix=self::PREFIX ) {
if ( empty($og) )
return;
$s = '';
foreach ( $og as $property => $content ) {
if ( is_object( $content ) || is_array( $content ) ) {
if ( is_object( $content ) )
$content = $content->toArray();
if ( empty($property) || !is_string($property) )
$s .= static::buildHTML( $content, $prefix );
else
$s .= static::buildHTML( $content, $prefix . ':' . $property );
} elseif ( !empty($content) ) {
$s .= '<meta ' . self::META_ATTR . '="' . $prefix;
if ( is_string($property) && !empty($property) )
$s .= ':' . htmlspecialchars( $property );
$s .= '" content="' . htmlspecialchars($content) . '">' . PHP_EOL;
}
}
return $s;
}
/**
* A list of allowed page types in the Open Graph Protocol
*
* @param Bool $flatten true for grouped types one level deep
* @link http://ogp.me/#types Open Graph Protocol object types
* @return array Array of Open Graph Protocol object types
*/
public static function supported_types( $flatten=false ) {
$types = array(
_('Activities') => array(
'activity' => _('Activity'),
'sport' => _('Sport')
),
_('Businesses') => array(
'company' => _('Company'),
'bar' => _('Bar'),
'cafe' => _('Cafe'),
'hotel' => _('Hotel'),
'restaurant' => _('Restaurant')
),
_('Groups') => array(
'cause' => _('Cause'),
'sports_league' => _('Sports league'),
'sports_team' => _('Sports team')
),
_('Organizations') => array(
'band' => _('Band'),
'government' => _('Government'),
'non_profit' => _('Non-profit'),
'school' => _('School'),
'university' => _('University')
),
_('People') => array(
'actor' => _('Actor or actress'),
'athlete' => _('Athlete'),
'author' => _('Author'),
'director' => _('Director'),
'musician' => _('Musician'),
'politician' => _('Politician'),
'profile' => _('Profile'),
'public_figure' => _('Public Figure')
),
_('Places') => array(
'city' => _('City or locality'),
'country' => _('Country'),
'landmark' => _('Landmark'),
'state_province' => _('State or province')
),
_('Products and Entertainment') => array(
'music.album' => _('Music Album'),
'book' => _('Book'),
'drink' => _('Drink'),
'video.episode' => _('Video episode'),
'food' => _('Food'),
'game' => _('Game'),
'video.movie' => _('Movie'),
'music.playlist' => _('Music playlist'),
'product' => _('Product'),
'music.radio_station' => _('Radio station'),
'music.song' => _('Song'),
'video.tv_show' => _('Television show'),
'video.other' => _('Video')
),
_('Websites') => array(
'article' => _('Article'),
'blog' => _('Blog'),
'website' => _('Website')
)
);
if ( $flatten === true ) {
$types_values = array();
foreach ( $types as $category=>$values ) {
$types_values = array_merge( $types_values, array_keys($values) );
}
return $types_values;
} else {
return $types;
}
}
/**
* Facebook maps languages to a default territory and only accepts locales in this list. A few popular languages such as English and French support multiple territories.
* Map the Facebook list to avoid throwing errors in Facebook parsers that prevent further content indexing
*
* @link https://www.facebook.com/translations/FacebookLocales.xml Facebook locales
* @param bool $keys_only return only keys
* @return array associative array of locale code and locale name. locale code is in the format language_TERRITORY where language is an ISO 639-1 alpha-2 code and territory is an ISO 3166-1 alpha-2 code with special regions 'AR' and 'LA' for Arab region and Latin America respectively.
*/
public static function supported_locales( $keys_only=false ) {
$locales = array(
'af_ZA' => _('Afrikaans'),
'ak_GH' => _('Akan'),
'am_ET' => _('Amharic'),
'ar_AR' => _('Arabic'),
'as_IN' => _('Assamese'),
'ay_BO' => _('Aymara'),
'az_AZ' => _('Azerbaijani'),
'be_BY' => _('Belarusian'),
'bg_BG' => _('Bulgarian'),
'bn_IN' => _('Bengali'),
'br_FR' => _('Breton'),
'bs_BA' => _('Bosnian'),
'ca_ES' => _('Catalan'),
'cb_IQ' => _('Sorani Kurdish'),
'ck_US' => _('Cherokee'),
'co_FR' => _('Corsican'),
'cs_CZ' => _('Czech'),
'cx_PH' => _('Cebuano'),
'cy_GB' => _('Welsh'),
'da_DK' => _('Danish'),
'de_DE' => _('German'),
'el_GR' => _('Greek'),
'en_GB' => _('English (UK)'),
'en_IN' => _('English (India)'),
'en_US' => _('English (US)'),
'eo_EO' => _('Esperanto'),
'es_CO' => _('Spanish (Colombia)'),
'es_ES' => _('Spanish (Spain)'),
'es_LA' => _('Spanish'),
'et_EE' => _('Estonian'),
'eu_ES' => _('Basque'),
'fa_IR' => _('Persian'),
'ff_NG' => _('Fulah'),
'fi_FI' => _('Finnish'),
'fo_FO' => _('Faroese'),
'fr_CA' => _('French (Canada)'),
'fr_FR' => _('French (France)'),
'fy_NL' => _('Frisian'),
'ga_IE' => _('Irish'),
'gl_ES' => _('Galician'),
'gn_PY' => _('Guarani'),
'gu_IN' => _('Gujarati'),
'gx_GR' => _('Classical Greek'),
'ha_NG' => _('Hausa'),
'he_IL' => _('Hebrew'),
'hi_IN' => _('Hindi'),
'hr_HR' => _('Croatian'),
'hu_HU' => _('Hungarian'),
'hy_AM' => _('Armenian'),
'id_ID' => _('Indonesian'),
'ig_NG' => _('Igbo'),
'is_IS' => _('Icelandic'),
'it_IT' => _('Italian'),
'ja_JP' => _('Japanese'),
'ja_KS' => _('Japanese (Kansai)'),
'jv_ID' => _('Javanese'),
'ka_GE' => _('Georgian'),
'kk_KZ' => _('Kazakh'),
'km_KH' => _('Khmer'),
'kn_IN' => _('Kannada'),
'ko_KR' => _('Korean'),
'ku_TR' => _('Kurdish (Kurmanji)'),
'la_VA' => _('Latin'),
'lg_UG' => _('Ganda'),
'li_NL' => _('Limburgish'),
'ln_CD' => _('Lingala'),
'lo_LA' => _('Lao'),
'lt_LT' => _('Lithuanian'),
'lv_LV' => _('Latvian'),
'mg_MG' => _('Malagasy'),
'mk_MK' => _('Macedonian'),
'ml_IN' => _('Malayalam'),
'mn_MN' => _('Mongolian'),
'mr_IN' => _('Marathi'),
'ms_MY' => _('Malay'),
'mt_MT' => _('Maltese'),
'my_MM' => _('Burmese'),
'nb_NO' => _('Norwegian (bokmal)'),
'nd_ZW' => _('Ndebele'),
'ne_NP' => _('Nepali'),
'nl_BE' => _('Dutch (België)'),
'nl_NL' => _('Dutch'),
'nn_NO' => _('Norwegian (nynorsk)'),
'ny_MW' => _('Chewa'),
'or_IN' => _('Oriya'),
'pa_IN' => _('Punjabi'),
'pl_PL' => _('Polish'),
'ps_AF' => _('Pashto'),
'pt_BR' => _('Portuguese (Brazil)'),
'pt_PT' => _('Portuguese (Portugal)'),
'qu_PE' => _('Quechua'),
'rm_CH' => _('Romansh'),
'ro_RO' => _('Romanian'),
'ru_RU' => _('Russian'),
'rw_RW' => _('Kinyarwanda'),
'sa_IN' => _('Sanskrit'),
'sc_IT' => _('Sardinian'),
'se_NO' => _('Northern Sámi'),
'si_LK' => _('Sinhala'),
'sk_SK' => _('Slovak'),
'sl_SI' => _('Slovenian'),
'sn_ZW' => _('Shona'),
'so_SO' => _('Somali'),
'sq_AL' => _('Albanian'),
'sr_RS' => _('Serbian'),
'sv_SE' => _('Swedish'),
'sw_KE' => _('Swahili'),
'sy_SY' => _('Syriac'),
'sz_PL' => _('Silesian'),
'ta_IN' => _('Tamil'),
'te_IN' => _('Telugu'),
'tg_TJ' => _('Tajik'),
'th_TH' => _('Thai'),
'tk_TM' => _('Turkmen'),
'tl_PH' => _('Filipino'),
'tr_TR' => _('Turkish'),
'tt_RU' => _('Tatar'),
'tz_MA' => _('Tamazight'),
'uk_UA' => _('Ukrainian'),
'ur_PK' => _('Urdu'),
'uz_UZ' => _('Uzbek'),
'vi_VN' => _('Vietnamese'),
'wo_SN' => _('Wolof'),
'xh_ZA' => _('Xhosa'),
'yi_DE' => _('Yiddish'),
'yo_NG' => _('Yoruba'),
'zh_CN' => _('Simplified Chinese (China)'),
'zh_HK' => _('Traditional Chinese (Hong Kong)'),
'zh_TW' => _('Traditional Chinese (Taiwan)'),
'zu_ZA' => _('Zulu'),
'zz_TR' => _('Zazaki')
);
if ( $keys_only === true ) {
return array_keys($locales);
} else {
return $locales;
}
}
/**
* Cleans a URL string, then checks to see if a given URL is addressable, returns a 200 OK response, and matches the accepted Internet media types (if provided).
*
* @param string $url Publicly addressable URL
* @param array $accepted_mimes Given URL correspond to an accepted Internet media (MIME) type.
* @return string cleaned URL string, or empty string on failure.
*/
public static function is_valid_url( $url, array $accepted_mimes = array() ) {
if ( !is_string( $url ) || empty( $url ) )
return '';
/*
* Validate URI string by letting PHP break up the string and put it back together again
* Excludes username:password and port number URI parts
*/
$url_parts = parse_url( $url );
$url = '';
if ( isset( $url_parts['scheme'] ) && in_array( $url_parts['scheme'], array('http', 'https'), true ) ) {
$url = "{$url_parts['scheme']}://{$url_parts['host']}{$url_parts['path']}";
if ( empty( $url_parts['path'] ) )
$url .= '/';
if ( !empty( $url_parts['query'] ) )
$url .= '?' . $url_parts['query'];
if ( !empty( $url_parts['fragment'] ) )
$url .= '#' . $url_parts['fragment'];
}
if ( !empty( $url ) ) {
// test if URL exists
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_TIMEOUT, 5 );
curl_setopt( $ch, CURLOPT_FORBID_REUSE, true );
curl_setopt( $ch, CURLOPT_NOBODY, true ); // HEAD
curl_setopt( $ch, CURLOPT_USERAGENT, 'Open Graph protocol validator ' . self::VERSION . ' (+http://ogp.me/)' );
if ( !empty($accepted_mimes) )
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Accept: ' . implode( ',', $accepted_mimes ) ) );
$response = curl_exec( $ch );
if ( curl_getinfo( $ch, CURLINFO_HTTP_CODE ) == 200 ) {
if ( !empty($accepted_mimes) ) {
$content_type = explode( ';', curl_getinfo( $ch, CURLINFO_CONTENT_TYPE ) );
if ( empty( $content_type ) || !in_array( $content_type[0], $accepted_mimes ) )
return '';
}
} else {
return '';
}
}
return $url;
}
/**
* Output the OpenGraphProtocol object as HTML elements string
*
* @return string meta elements
*/
public function toHTML() {
return rtrim( static::buildHTML( get_object_vars($this) ), PHP_EOL );
}
/**
* @return String the type slug
*/
public function getType() {
return $this->type;
}
/**
*
* @param String type slug
*/
public function setType( $type ) {
if ( is_string($type) && in_array( $type, self::supported_types(true), true ) )
$this->type = $type;
return $this;
}
/**
* @return String document title
*/
public function getTitle() {
return $this->title;
}
/**
* @param String $title document title
*/
public function setTitle( $title ) {
if ( is_string($title) ) {
$title = trim( $title );
if ( strlen( $title ) > 128 )
$this->title = substr( $title, 0, 128 );
else
$this->title = $title;
}
return $this;
}
/**
* @return String Site name
*/
public function getSiteName() {
return $this->site_name;
}
/**
* @param String $site_name Site name
*/
public function setSiteName( $site_name ) {
if ( is_string($site_name) && !empty($site_name) ) {
$site_name = trim( $site_name );
if ( strlen( $site_name ) > 128 )
$this->site_name = substr( $site_name, 0, 128 );
else
$this->site_name = $site_name;
}
return $this;
}
/**
* @return String Description
*/
public function getDescription() {
return $this->description;
}
/**
* @param String $description Document description
*/
public function setDescription( $description ) {
if ( is_string($description) && !empty($description) ) {
$description = trim( $description );
if ( strlen( $description ) > 255 )
$this->description = substr( $description, 0, 255 );
else
$this->description = $description;
}
return $this;
}
/**
* @return String URL
*/
public function getURL() {
return $this->url;
}
/**
* @param String $url Canonical URL
*/
public function setURL( $url ) {
if ( is_string( $url ) && !empty( $url ) ) {
$url = trim($url);
if (self::VERIFY_URLS) {
$url = self::is_valid_url( $url, array( 'text/html', 'application/xhtml+xml' ) );
}
if ( !empty( $url ) )
$this->url = $url;
}
return $this;
}
/**
* @return string the determiner
*/
public function getDeterminer() {
return $this->determiner;
}
public function setDeterminer( $determiner ) {
if ( in_array($determiner, array('a','an','auto','the'), true) )
$this->determiner = $determiner;
return $this;
}
/**
* @return string language_TERRITORY
*/
public function getLocale() {
return $this->locale;
}
/**
* @var string $locale locale in the format language_TERRITORY
*/
public function setLocale( $locale ) {
if ( is_string($locale) && in_array($locale, static::supported_locales(true)) )
$this->locale = $locale;
return $this;
}
/**
* @return array OpenGraphProtocolImage array
*/
public function getImage() {
return $this->image;
}
/**
* Add an image.
* The first image added is given priority by the Open Graph Protocol spec. Implementors may choose a different image based on size requirements or preferences.
*
* @param OpenGraphProtocolImage $image image object to add
*/
public function addImage( OpenGraphProtocolImage $image ) {
$image_url = $image->getURL();
if ( empty($image_url) )
return;
$image->removeURL();
$value = array( $image_url, array($image) );
if ( ! isset( $this->image ) )
$this->image = array( $value );
else
$this->image[] = $value;
return $this;
}
/**
* @return array OpenGraphProtocolAudio objects
*/
public function getAudio() {
return $this->audio;
}
/**
* Add an audio reference
* The first audio is given priority by the Open Graph protocol spec.
*
* @param OpenGraphProtocolAudio $audio audio object to add
*/
public function addAudio( OpenGraphProtocolAudio $audio ) {
$audio_url = $audio->getURL();
if ( empty($audio_url) )
return;
$audio->removeURL();
$value = array( $audio_url, array($audio) );
if ( ! isset($this->audio) )
$this->audio = array($value);
else
$this->audio[] = $value;
return $this;
}
/**
* @return array OpenGraphProtocolVideo objects
*/
public function getVideo() {
return $this->video;
}
/**
* Add a video reference
* The first video is given priority by the Open Graph protocol spec. Implementors may choose a different video based on size requirements or preferences.
*
* @param OpenGraphProtocolVideo $video video object to add
*/
public function addVideo( OpenGraphProtocolVideo $video ) {
$video_url = $video->getURL();
if ( empty($video_url) )
return;
$video->removeURL();
$value = array( $video_url, array($video) );
if ( ! isset( $this->video ) )
$this->video = array( $value );
else
$this->video[] = $value;
return $this;
}
}
include_once dirname(__FILE__) . '/media.php'; // image, video, audio
include_once dirname(__FILE__) . '/objects.php'; // global objects: profile, article
?>