forked from afshinm/tehlug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
83 lines (60 loc) · 2.08 KB
/
functions.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
<?php
define('ENTRIES_DIRECTORY', 'contents/entries');
function getEntries($type = Null, $limit = Null) {
$entries = array();
$dir = opendir(ENTRIES_DIRECTORY);
$count = 0;
while($entryFile = readdir($dir)) {
if($entryFile{0} == '.') //Dont show hidden files
continue;
$entryName = str_replace('.php', Null, $entryFile);
$entry = getEntry($entryName);
if($type && $entry->type != $type) {
continue;
}
$entries[$entry->date] = $entry;
$count++;
}
krsort($entries);
if($limit)
$entries = array_slice($entries, 0, $limit, True);
return $entries;
}
function getEntry($id) {
$path = ENTRIES_DIRECTORY.'/'.$id.'.php';
$dom = New DOMDocument;
$dom->loadHTML('<?xml version="1.0" encoding="UTF-8"?>'.file_get_contents($path));
$xpath = New DOMXPath($dom);
$entry = New stdClass;
$entry->id = $id;
$entry->path = $path;
foreach($xpath->query('//*[@class]') as $element) {
$attributeName = $element->attributes->getNamedItem('class')->value;
if($element->childNodes->length > 1) {
$childs = Array();
foreach($element->childNodes as $childNode)
if(strlen(trim($childNode->textContent)))
$childs[] = trim($childNode->textContent);
$entry->$attributeName = $childs;
} else
if(strlen($element->textContent))
$entry->$attributeName = trim($element->textContent);
}
include_once('jdf.php');
$parts = explode('/', $entry->date);
$entry->timestamp = jmaketime(0,0,0,$parts[1],$parts[2],$parts[0]);
$entry->url = 'index.php?page=entries/'.$entry->id;
return $entry;
}
function getNextSession() {
$last = current(getEntries('Session', 1));
if($last->timestamp >= time())
return $last;
}
function toPersian($num) {
return str_replace(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0), array('۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '۰'), $num);
}
function filenameToId($fileName) {
return str_replace(array('contents/', '.php'), Null, $fileName);
}
?>