-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.php
207 lines (138 loc) · 4.76 KB
/
index.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
<?php
//adds a sample comment
require_once './vendor/autoload.php';
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\CssSelector\CssSelector;
//print CssSelector::toXPath('div.item > h4 > a');
$db = null;
$datadir = __DIR__.'/data/v1';
$datafiles = glob($datadir .'/*.txt');
$action = isset($_GET['action'])?$_GET['action']:'';
$filename = isset($_GET['filename'])?$_GET['filename']:'';
$filehtml = '';
switch($action) {
case 'update_data':
print "updating data for $filename..";
set_filehtml($filename);
//now process filedata
$crawler = new Crawler($filehtml);
$surahName = mysql_real_escape_string($crawler->filter('#heading h1')->text());
$surahNameEN = mysql_real_escape_string($crawler->filter('#heading h2')->text());
var_dump($surahName);
var_dump($surahNameEN);
$surah = str_replace('.txt', '', $filename);
//remove existing words
$sql = "DELETE FROM words
WHERE surah = '$surah'";
db_query($sql);
$ayah_num = 1;
$tables = $crawler->filter('table.aya-W td')->each(function(Crawler $node, $i) use (&$ayah_num){
$class = $node->extract(array('class'))[0];
//var_dump($class);
if ($class == 'ww') {
$english = mysql_real_escape_string($node->filter('span.e')->text());
$word = mysql_real_escape_string($node->filter('span.a')->text());
var_dump($english);
var_dump($word);
var_dump('ayah #' . $ayah_num);
//insert words for current surah
global $surah;
global $surahName;
global $surahNameEN;
$sql = "INSERT INTO words VALUES (NULL, '$word', '$english', '$surah', '$surahName', '$surahNameEN', '$ayah_num', NULL, NOW() )";
//var_dump($sql);
if(!db_query($sql)){
print '<p style="color: red;">Error updating word ('.$english.')</p>';
}
}
elseif ($class == 'n') {
$ayah_num++;
}
});
/* foreach ($tables as $table) {
var_dump($table->childNodes);
print $table->nodeName;
} */
break;
case 'generate_images':
//print "generating images for $filename..";
$surah = str_replace('.txt', '', $filename);
//testing: get first word of the surah
$sql = "SELECT * FROM words WHERE surah = '$surah' LIMIT 1";
$words = db_select($sql);
$word = $words[0];
//var_dump($word);
header("Content-type: image/jpeg");
error_reporting(0);
require_once __DIR__.'/libs/I18N/Arabic.php';
require_once __DIR__.'/libs/word2uni/word2uni.php';
$ar = new I18N_Arabic('Glyphs');
$imgPath = __DIR__.'/templates/stc.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 0, 0, 0);
$string = $word['word'];
$fontSize = 50;
$x = 115;
$y = 300;
//imagettftext ( resource $im, int $size, int $angle, int $x, int $y, int $color, string $fontfile, string $text )
imagettftext ( $image, $fontSize, 0, $x, $y, $color, __DIR__.'/templates/fonts/tradbdo.ttf', word2uni($string) );
//imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
exit;
break;
}
function set_filehtml($filename) {
global $datadir;
global $filehtml;
$filepath = $datadir . "/$filename";
$filehtml = file_get_contents($filepath);
}
function revUni($text) {
$wordsArray = explode(" ", $text);
$rtlCompleteText='';
for ($i = sizeOf($wordsArray); $i > -1; $i = $i-1) {
//$lettersArray = explode("|", str_replace(";|", ";", $wordsArray[$i]));
$lettersArray = explode(";", $wordsArray[$i]);
$rtlWord='';
for ($k = sizeOf($lettersArray); $k > -1; $k = $k-1) {
if (strlen($lettersArray[$k]) > 1) { // make sure its full unicode letter
$rtlWord = $rtlWord."".$lettersArray[$k].";";
}
}
$rtlCompleteText = $rtlCompleteText." ".$rtlWord;
}
return $rtlCompleteText;
}
function proper_text($text){
$text = mb_convert_encoding($text, "HTML-ENTITIES", "UTF-8");
$text = preg_replace('~^(&([a-zA-Z0-9]);)~',htmlentities('${1}'),$text);
return($text);
}
function db_connect() {
global $db;
if (!$db) {
$db = mysql_connect('localhost', 'root', 'pa$$w0rd');
mysql_select_db('wordofquran');
}
}
function db_query($sql) {
global $db;
db_connect();
//print $sql; return;
return mysql_query($sql, $db);
}
function db_select($sql) {
$db_results = db_query($sql);
$rows = array();
while ($row = mysql_fetch_assoc($db_results)) {
$rows[] = $row;
}
return $rows;
}
foreach($datafiles as $datafile) {
$datafilebase = basename($datafile);
?>
<p><a href="?action=update_data&filename=<?php echo $datafilebase ?>">Get data for <?php echo $datafilebase ?></a>
<a href="?action=generate_images&filename=<?php echo $datafilebase ?>">Generate images for <?php echo $datafilebase ?></a> </p>
<?php
}