forked from fititnt/plg_getgithubcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getgithubcode.php
148 lines (129 loc) · 5.46 KB
/
getgithubcode.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
<?php
/**
* Emerson Rocha Luiz { emerson@webdesign.eng.br - http://fititnt.org }
* Copyright (C) 2005 - 2011 Webdesign Assessoria em Tecnologia da Informacao.
* GPL3
*/
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
/**
* GetGitHub Content Plugin
*
* @since 1.6
*/
class plgContentGetgithubcode extends JPlugin
{
/**
* Example prepare content method
*
* Method is called by the view
*
* @param string The context of the content being passed to the plugin.
* @param object The content object. Note $article->text is also available
* @param object The content params
* @param int The 'page' number
* @since 1.6
*/
public function onContentPrepare($context, &$article, &$params, $limitstart)
{
$app = JFactory::getApplication();
// simple performance check to determine whether bot should process further
$tagname = $this->get('tagname', 'github');
if (strpos($article->text, $tagname) === false) {
return true;
}
// expression to search
// {github}https://raw.github.com/example...{/github}
// @todo: rewrite to make able to ask start and end lines
$regex = '~{'.$tagname.'}(.*?){/'.$tagname.'}~i';
$matches = array();
// find all instances of plugin and put in $matches
preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
// $match[0] is full pattern match, $match[1] is the url
$code = $this->_getGithubCode($match[1]);
// We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
$article->text = preg_replace("|$match[0]|", $code, $article->text, 1);
}
return '';
}
/* Function to get get and change the githubcode
* @author Emerson Rocha Luiz
* @var string $url: the url to get. Must be RAW url!
* @var int $start: line to start to show
* @var int $end: last line to show
* @return string $github: the final github code to show
*/
protected function _getGithubCode($url, $start = FALSE, $end = FALSE){
//Get Page
$page = $this->_getUrlContents($url, FALSE);
//Convert linebreaks
$page = $this->_Unix2Dos($page);
//Get only desired lines
if($start !== FALSE || $end !== FALSE){
$page = $this->_getStringLines($page, $start, $end);
}
//Clean up special chars
$github = htmlspecialchars($page);
//Get start and end tags and apply
$tagstart = $this->params->get('tagstart', '<pre>');
$tagstart = str_replace('<', '<', $tagstart);
$tagstart = str_replace('>', '>', $tagstart);
$tagsend = $this->params->get('tagend', '</pre>');
$tagsend = str_replace('<', '<', $tagsend);
$tagsend = str_replace('>', '>', $tagsend);
$github = $tagstart . $github . $tagsend;
return $github;
}
/*
* Return contents of url
* @author Emerson Rocha Luiz
* @var string $url
* @var string $certificate path to certificate if is https URL
* @return string
*/
protected function _getUrlContents($url, $certificate = FALSE){
//$page = file_get_contents($url);
$ch = curl_init(); //Inicializar a sessao
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Retorne os dados em vez de imprimir em tela
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $certificate);//Check certificate if is SSL, default FALSE
curl_setopt($ch, CURLOPT_URL, $url);//Setar URL
$content = curl_exec($ch);//Execute
curl_close($ch);//Feche
return $content;
}
/* Return just lines betwen betwen start and end lines
* @author Emerson Rocha Luiz
* @var string $string: the string to edit
* @var int $start: initial line
* @var int $end: end line
* @return string
*/
protected function _getStringLines($string, $start, $end){
$stringArray = explode(PHP_EOL, $string);
$nLines = count($stringArray)-1;
//Handle a few errors
if( $end < $start || $end > $nLines){
//return FALSE;
}
$result = '';
for( $i=($start-1); $i<=$end ; $i++ ){
$result .= $stringArray[$i] . PHP_EOL;
}
return $result;
}
/* Convert unix linebreaks to windows line breaks
* @author Emerson Rocha Luiz
* @var string $string: the string to edit
* @return string $newstring
*/
protected function _Unix2Dos($string){
if (strpos($string, "\n") === false) {
//$newstring = false;
$newstring = $string;
} else {
$newstring = str_replace("\n", "\r\n", $string);
}
return $newstring;
}
}